2018-05-10 14:11:00 +00:00
|
|
|
// Formatting library for C++ - the core API
|
|
|
|
//
|
|
|
|
// Copyright (c) 2012 - present, Victor Zverovich
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// For the license information refer to format.h.
|
|
|
|
//
|
|
|
|
// Copyright (c) 2018 - present, Remotion (Igor Schulz)
|
|
|
|
// All Rights Reserved
|
|
|
|
// {fmt} support for ranges, containers and types tuple interface.
|
|
|
|
|
2019-09-28 16:13:32 +00:00
|
|
|
#include "fmt/ranges.h"
|
2020-03-14 14:41:08 +00:00
|
|
|
|
2021-01-30 15:42:58 +00:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2021-04-28 22:59:43 +00:00
|
|
|
#include "gtest/gtest.h"
|
2019-09-28 16:13:32 +00:00
|
|
|
|
2021-01-30 15:42:58 +00:00
|
|
|
#if !FMT_GCC_VERSION || FMT_GCC_VERSION >= 601
|
|
|
|
# define FMT_RANGES_TEST_ENABLE_C_STYLE_ARRAY
|
|
|
|
#endif
|
2018-07-04 20:17:03 +00:00
|
|
|
|
2021-01-30 15:42:58 +00:00
|
|
|
#if !FMT_MSC_VER || FMT_MSC_VER > 1910
|
|
|
|
# define FMT_RANGES_TEST_ENABLE_JOIN
|
|
|
|
# define FMT_RANGES_TEST_ENABLE_FORMAT_STRUCT
|
|
|
|
#endif
|
2018-05-10 14:11:00 +00:00
|
|
|
|
2021-01-30 15:42:58 +00:00
|
|
|
#ifdef FMT_RANGES_TEST_ENABLE_C_STYLE_ARRAY
|
2021-05-04 23:54:20 +00:00
|
|
|
TEST(ranges_test, format_array) {
|
|
|
|
int arr[] = {1, 2, 3, 5, 7, 11};
|
2021-05-05 04:04:21 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", arr), "[1, 2, 3, 5, 7, 11]");
|
2020-10-24 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 04:04:21 +00:00
|
|
|
TEST(ranges_test, format_2d_array) {
|
2021-05-04 23:54:20 +00:00
|
|
|
int arr[][2] = {{1, 2}, {3, 5}, {7, 11}};
|
2021-05-05 04:04:21 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", arr), "[[1, 2], [3, 5], [7, 11]]");
|
2020-10-24 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 23:54:20 +00:00
|
|
|
TEST(ranges_test, format_array_of_literals) {
|
|
|
|
const char* arr[] = {"1234", "abcd"};
|
2021-05-05 04:04:21 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", arr), "[\"1234\", \"abcd\"]");
|
2021-01-30 15:42:58 +00:00
|
|
|
}
|
|
|
|
#endif // FMT_RANGES_TEST_ENABLE_C_STYLE_ARRAY
|
|
|
|
|
2021-05-04 23:54:20 +00:00
|
|
|
TEST(ranges_test, format_vector) {
|
2021-05-05 03:53:56 +00:00
|
|
|
auto v = std::vector<int>{1, 2, 3, 5, 7, 11};
|
2021-05-05 04:04:21 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", v), "[1, 2, 3, 5, 7, 11]");
|
2018-05-10 14:11:00 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 23:54:20 +00:00
|
|
|
TEST(ranges_test, format_vector2) {
|
2021-05-05 03:53:56 +00:00
|
|
|
auto v = std::vector<std::vector<int>>{{1, 2}, {3, 5}, {7, 11}};
|
2021-05-05 04:04:21 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", v), "[[1, 2], [3, 5], [7, 11]]");
|
2018-05-10 14:11:00 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
TEST(ranges_test, format_map) {
|
|
|
|
auto m = std::map<std::string, int>{{"one", 1}, {"two", 2}};
|
2021-05-05 04:04:21 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", m), "[(\"one\", 1), (\"two\", 2)]");
|
2018-05-10 14:11:00 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
TEST(ranges_test, format_pair) {
|
|
|
|
auto p = std::pair<int, float>(42, 1.5f);
|
|
|
|
EXPECT_EQ(fmt::format("{}", p), "(42, 1.5)");
|
2018-05-10 14:11:00 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
TEST(ranges_test, format_tuple) {
|
|
|
|
auto t =
|
|
|
|
std::tuple<int, float, std::string, char>(42, 1.5f, "this is tuple", 'i');
|
|
|
|
EXPECT_EQ(fmt::format("{}", t), "(42, 1.5, \"this is tuple\", 'i')");
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::tuple<>()), "()");
|
2018-05-10 14:11:00 +00:00
|
|
|
}
|
|
|
|
|
2021-01-30 15:42:58 +00:00
|
|
|
#ifdef FMT_RANGES_TEST_ENABLE_FORMAT_STRUCT
|
2021-05-05 03:53:56 +00:00
|
|
|
struct tuple_like {
|
|
|
|
int i;
|
|
|
|
std::string str;
|
|
|
|
|
|
|
|
template <size_t N> fmt::enable_if_t<N == 0, int> get() const noexcept {
|
2021-01-30 15:42:58 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
template <size_t N>
|
|
|
|
fmt::enable_if_t<N == 1, fmt::string_view> get() const noexcept {
|
2021-05-05 03:53:56 +00:00
|
|
|
return str;
|
2018-05-10 14:11:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-01-30 15:42:58 +00:00
|
|
|
template <size_t N>
|
2021-05-05 03:53:56 +00:00
|
|
|
auto get(const tuple_like& t) noexcept -> decltype(t.get<N>()) {
|
|
|
|
return t.get<N>();
|
2018-05-10 14:11:00 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 04:04:21 +00:00
|
|
|
namespace std {
|
2021-05-05 03:53:56 +00:00
|
|
|
template <>
|
2021-05-05 04:04:21 +00:00
|
|
|
struct tuple_size<tuple_like> : std::integral_constant<size_t, 2> {};
|
2018-05-10 14:11:00 +00:00
|
|
|
|
2021-05-05 04:04:21 +00:00
|
|
|
template <size_t N> struct tuple_element<N, tuple_like> {
|
2021-05-05 03:53:56 +00:00
|
|
|
using type = decltype(std::declval<tuple_like>().get<N>());
|
2018-05-10 14:11:00 +00:00
|
|
|
};
|
2021-05-05 04:04:21 +00:00
|
|
|
} // namespace std
|
2018-05-10 14:11:00 +00:00
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
TEST(ranges_test, format_struct) {
|
|
|
|
auto t = tuple_like{42, "foo"};
|
|
|
|
EXPECT_EQ(fmt::format("{}", t), "(42, \"foo\")");
|
2018-05-10 14:11:00 +00:00
|
|
|
}
|
2021-01-30 15:42:58 +00:00
|
|
|
#endif // FMT_RANGES_TEST_ENABLE_FORMAT_STRUCT
|
2018-05-10 14:11:00 +00:00
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
TEST(ranges_test, format_to) {
|
2019-03-06 15:59:23 +00:00
|
|
|
char buf[10];
|
2021-01-30 15:42:58 +00:00
|
|
|
auto end = fmt::format_to(buf, "{}", std::vector<int>{1, 2, 3});
|
2019-03-06 15:59:23 +00:00
|
|
|
*end = '\0';
|
2021-05-05 04:04:21 +00:00
|
|
|
EXPECT_STREQ(buf, "[1, 2, 3]");
|
2019-03-06 15:59:23 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 18:22:59 +00:00
|
|
|
struct path_like {
|
|
|
|
const path_like* begin() const;
|
|
|
|
const path_like* end() const;
|
|
|
|
|
|
|
|
operator std::string() const;
|
|
|
|
};
|
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
TEST(ranges_test, path_like) {
|
2019-08-21 18:22:59 +00:00
|
|
|
EXPECT_FALSE((fmt::is_range<path_like, char>::value));
|
|
|
|
}
|
|
|
|
|
2019-09-28 16:13:32 +00:00
|
|
|
#ifdef FMT_USE_STRING_VIEW
|
|
|
|
struct string_like {
|
|
|
|
const char* begin();
|
|
|
|
const char* end();
|
|
|
|
explicit operator fmt::string_view() const { return "foo"; }
|
|
|
|
explicit operator std::string_view() const { return "foo"; }
|
|
|
|
};
|
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
TEST(ranges_test, format_string_like) {
|
|
|
|
EXPECT_EQ(fmt::format("{}", string_like()), "foo");
|
2019-09-28 16:13:32 +00:00
|
|
|
}
|
|
|
|
#endif // FMT_USE_STRING_VIEW
|
2020-05-18 13:31:01 +00:00
|
|
|
|
2020-07-21 19:13:00 +00:00
|
|
|
// A range that provides non-const only begin()/end() to test fmt::join handles
|
2021-05-05 03:53:56 +00:00
|
|
|
// that.
|
2020-07-21 19:13:00 +00:00
|
|
|
//
|
2021-05-05 03:53:56 +00:00
|
|
|
// Some ranges (e.g. those produced by range-v3's views::filter()) can cache
|
2020-07-21 19:13:00 +00:00
|
|
|
// information during iteration so they only provide non-const begin()/end().
|
|
|
|
template <typename T> class non_const_only_range {
|
|
|
|
private:
|
|
|
|
std::vector<T> vec;
|
|
|
|
|
|
|
|
public:
|
|
|
|
using const_iterator = typename ::std::vector<T>::const_iterator;
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
explicit non_const_only_range(Args&&... args)
|
2021-05-05 03:53:56 +00:00
|
|
|
: vec(std::forward<Args>(args)...) {}
|
2020-07-21 19:13:00 +00:00
|
|
|
|
|
|
|
const_iterator begin() { return vec.begin(); }
|
|
|
|
const_iterator end() { return vec.end(); }
|
|
|
|
};
|
|
|
|
|
2020-10-24 09:25:29 +00:00
|
|
|
template <typename T> class noncopyable_range {
|
|
|
|
private:
|
|
|
|
std::vector<T> vec;
|
|
|
|
|
|
|
|
public:
|
|
|
|
using const_iterator = typename ::std::vector<T>::const_iterator;
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
explicit noncopyable_range(Args&&... args)
|
2021-05-05 03:53:56 +00:00
|
|
|
: vec(std::forward<Args>(args)...) {}
|
2020-10-24 09:25:29 +00:00
|
|
|
|
|
|
|
noncopyable_range(noncopyable_range const&) = delete;
|
|
|
|
noncopyable_range(noncopyable_range&) = delete;
|
|
|
|
|
|
|
|
const_iterator begin() const { return vec.begin(); }
|
|
|
|
const_iterator end() const { return vec.end(); }
|
|
|
|
};
|
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
TEST(ranges_test, range) {
|
2020-10-24 09:25:29 +00:00
|
|
|
noncopyable_range<int> w(3u, 0);
|
2021-05-05 04:04:21 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", w), "[0, 0, 0]");
|
|
|
|
EXPECT_EQ(fmt::format("{}", noncopyable_range<int>(3u, 0)), "[0, 0, 0]");
|
2020-10-24 09:25:29 +00:00
|
|
|
|
|
|
|
non_const_only_range<int> x(3u, 0);
|
2021-05-05 04:04:21 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", x), "[0, 0, 0]");
|
|
|
|
EXPECT_EQ(fmt::format("{}", non_const_only_range<int>(3u, 0)), "[0, 0, 0]");
|
2020-10-24 09:25:29 +00:00
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
auto y = std::vector<int>(3u, 0);
|
2021-05-05 04:04:21 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", y), "[0, 0, 0]");
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::vector<int>(3u, 0)), "[0, 0, 0]");
|
2020-10-24 09:25:29 +00:00
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
const auto z = std::vector<int>(3u, 0);
|
2021-05-05 04:04:21 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", z), "[0, 0, 0]");
|
2020-10-24 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
2020-09-20 14:53:13 +00:00
|
|
|
#if !FMT_MSC_VER || FMT_MSC_VER >= 1927
|
|
|
|
struct unformattable {};
|
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
TEST(ranges_test, unformattable_range) {
|
2020-09-20 14:53:13 +00:00
|
|
|
EXPECT_FALSE((fmt::has_formatter<std::vector<unformattable>,
|
|
|
|
fmt::format_context>::value));
|
|
|
|
}
|
|
|
|
#endif
|
2021-01-30 15:42:58 +00:00
|
|
|
|
|
|
|
#ifdef FMT_RANGES_TEST_ENABLE_JOIN
|
2021-05-05 03:53:56 +00:00
|
|
|
TEST(ranges_test, join_tuple) {
|
|
|
|
// Value tuple args.
|
|
|
|
auto t1 = std::tuple<char, int, float>('a', 1, 2.0f);
|
|
|
|
EXPECT_EQ(fmt::format("({})", fmt::join(t1, ", ")), "(a, 1, 2)");
|
2021-01-30 15:42:58 +00:00
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
// Testing lvalue tuple args.
|
2021-01-30 15:42:58 +00:00
|
|
|
int x = 4;
|
2021-05-05 03:53:56 +00:00
|
|
|
auto t2 = std::tuple<char, int&>('b', x);
|
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join(t2, " + ")), "b + 4");
|
2021-01-30 15:42:58 +00:00
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
// Empty tuple.
|
|
|
|
auto t3 = std::tuple<>();
|
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join(t3, "|")), "");
|
2021-01-30 15:42:58 +00:00
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
// Single element tuple.
|
|
|
|
auto t4 = std::tuple<float>(4.0f);
|
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join(t4, "/")), "4");
|
2021-01-30 15:42:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
TEST(ranges_test, join_initializer_list) {
|
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join({1, 2, 3}, ", ")), "1, 2, 3");
|
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join({"fmt", "rocks", "!"}, " ")),
|
|
|
|
"fmt rocks !");
|
2021-01-30 15:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct zstring_sentinel {};
|
|
|
|
|
|
|
|
bool operator==(const char* p, zstring_sentinel) { return *p == '\0'; }
|
|
|
|
bool operator!=(const char* p, zstring_sentinel) { return *p != '\0'; }
|
|
|
|
|
|
|
|
struct zstring {
|
|
|
|
const char* p;
|
|
|
|
const char* begin() const { return p; }
|
|
|
|
zstring_sentinel end() const { return {}; }
|
|
|
|
};
|
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
TEST(ranges_test, join_sentinel) {
|
|
|
|
auto hello = zstring{"hello"};
|
2021-05-05 04:04:21 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", hello), "['h', 'e', 'l', 'l', 'o']");
|
2021-05-05 03:53:56 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join(hello, "_")), "h_e_l_l_o");
|
2021-01-30 15:42:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
TEST(ranges_test, join_range) {
|
2021-01-30 15:42:58 +00:00
|
|
|
noncopyable_range<int> w(3u, 0);
|
2021-05-05 03:53:56 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join(w, ",")), "0,0,0");
|
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join(noncopyable_range<int>(3u, 0), ",")),
|
|
|
|
"0,0,0");
|
2021-01-30 15:42:58 +00:00
|
|
|
|
|
|
|
non_const_only_range<int> x(3u, 0);
|
2021-05-05 03:53:56 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join(x, ",")), "0,0,0");
|
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join(non_const_only_range<int>(3u, 0), ",")),
|
|
|
|
"0,0,0");
|
|
|
|
|
|
|
|
auto y = std::vector<int>(3u, 0);
|
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join(y, ",")), "0,0,0");
|
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join(std::vector<int>(3u, 0), ",")),
|
|
|
|
"0,0,0");
|
|
|
|
|
|
|
|
const auto z = std::vector<int>(3u, 0);
|
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join(z, ",")), "0,0,0");
|
2021-01-30 15:42:58 +00:00
|
|
|
}
|
2021-05-05 03:53:56 +00:00
|
|
|
#endif // FMT_RANGES_TEST_ENABLE_JOIN
|
2021-08-19 21:12:26 +00:00
|
|
|
|
2021-08-22 17:24:51 +00:00
|
|
|
TEST(ranges_test, is_printable) {
|
|
|
|
using fmt::detail::is_printable;
|
|
|
|
EXPECT_TRUE(is_printable(0x0323));
|
|
|
|
EXPECT_FALSE(is_printable(0x0378));
|
|
|
|
}
|
|
|
|
|
2021-08-19 21:12:26 +00:00
|
|
|
TEST(ranges_test, escape_string) {
|
2021-08-21 16:35:05 +00:00
|
|
|
using vec = std::vector<std::string>;
|
|
|
|
EXPECT_EQ(fmt::format("{}", vec{"\n\r\t\"\\"}), "[\"\\n\\r\\t\\\"\\\\\"]");
|
|
|
|
EXPECT_EQ(fmt::format("{}", vec{"\x07"}), "[\"\\x07\"]");
|
|
|
|
EXPECT_EQ(fmt::format("{}", vec{"\x7f"}), "[\"\\x7f\"]");
|
2021-08-22 14:38:22 +00:00
|
|
|
|
|
|
|
// Unassigned Unicode code points.
|
|
|
|
if (fmt::detail::is_utf8()) {
|
|
|
|
EXPECT_EQ(fmt::format("{}", vec{"\xf0\xaa\x9b\x9e"}), "[\"\\U0002a6de\"]");
|
|
|
|
EXPECT_EQ(fmt::format("{}", vec{"\xf4\x8f\xbf\xbf"}), "[\"\\U0010ffff\"]");
|
|
|
|
}
|
2021-08-21 16:35:05 +00:00
|
|
|
}
|