// Formatting library for C++ - ranges tests // // Copyright (c) 2012 - present, Victor Zverovich and {fmt} contributors // All rights reserved. // // For the license information refer to format.h. #include "fmt/ranges.h" #include #include #include #include #include #include #include #include #if FMT_HAS_INCLUDE() # include #endif #include "gtest/gtest.h" #if !FMT_GCC_VERSION || FMT_GCC_VERSION >= 601 # define FMT_RANGES_TEST_ENABLE_C_STYLE_ARRAY #endif #if !FMT_MSC_VERSION || FMT_MSC_VERSION > 1910 # define FMT_RANGES_TEST_ENABLE_JOIN # define FMT_RANGES_TEST_ENABLE_FORMAT_STRUCT #endif #ifdef FMT_RANGES_TEST_ENABLE_C_STYLE_ARRAY TEST(ranges_test, format_array) { int arr[] = {1, 2, 3, 5, 7, 11}; EXPECT_EQ(fmt::format("{}", arr), "[1, 2, 3, 5, 7, 11]"); } TEST(ranges_test, format_2d_array) { int arr[][2] = {{1, 2}, {3, 5}, {7, 11}}; EXPECT_EQ(fmt::format("{}", arr), "[[1, 2], [3, 5], [7, 11]]"); } TEST(ranges_test, format_array_of_literals) { const char* arr[] = {"1234", "abcd"}; EXPECT_EQ(fmt::format("{}", arr), "[\"1234\", \"abcd\"]"); EXPECT_EQ(fmt::format("{:n}", arr), "\"1234\", \"abcd\""); EXPECT_EQ(fmt::format("{:n:}", arr), "1234, abcd"); } #endif // FMT_RANGES_TEST_ENABLE_C_STYLE_ARRAY TEST(ranges_test, format_vector) { auto v = std::vector{1, 2, 3, 5, 7, 11}; EXPECT_EQ(fmt::format("{}", v), "[1, 2, 3, 5, 7, 11]"); EXPECT_EQ(fmt::format("{::#x}", v), "[0x1, 0x2, 0x3, 0x5, 0x7, 0xb]"); EXPECT_EQ(fmt::format("{:n:#x}", v), "0x1, 0x2, 0x3, 0x5, 0x7, 0xb"); auto vc = std::vector{'a', 'b', 'c'}; auto vvc = std::vector>{vc, vc}; EXPECT_EQ(fmt::format("{}", vc), "['a', 'b', 'c']"); EXPECT_EQ(fmt::format("{}", vvc), "[['a', 'b', 'c'], ['a', 'b', 'c']]"); EXPECT_EQ(fmt::format("{:n}", vvc), "['a', 'b', 'c'], ['a', 'b', 'c']"); EXPECT_EQ(fmt::format("{:n:n}", vvc), "'a', 'b', 'c', 'a', 'b', 'c'"); EXPECT_EQ(fmt::format("{:n:n:}", vvc), "a, b, c, a, b, c"); } TEST(ranges_test, format_nested_vector) { auto v = std::vector>{{1, 2}, {3, 5}, {7, 11}}; EXPECT_EQ(fmt::format("{}", v), "[[1, 2], [3, 5], [7, 11]]"); EXPECT_EQ(fmt::format("{:::#x}", v), "[[0x1, 0x2], [0x3, 0x5], [0x7, 0xb]]"); EXPECT_EQ(fmt::format("{:n:n:#x}", v), "0x1, 0x2, 0x3, 0x5, 0x7, 0xb"); } TEST(ranges_test, to_string_vector) { auto v = std::vector{"a", "b", "c"}; EXPECT_EQ(fmt::to_string(v), "[\"a\", \"b\", \"c\"]"); } TEST(ranges_test, format_map) { auto m = std::map{{"one", 1}, {"two", 2}}; EXPECT_EQ(fmt::format("{}", m), "{\"one\": 1, \"two\": 2}"); EXPECT_EQ(fmt::format("{:n}", m), "\"one\": 1, \"two\": 2"); } TEST(ranges_test, format_set) { EXPECT_EQ(fmt::format("{}", std::set{"one", "two"}), "{\"one\", \"two\"}"); } // Models std::flat_set close enough to test if no ambiguous lookup of a // formatter happens due to the flat_set type matching is_set and // is_container_adaptor_like. template class flat_set { public: using key_type = T; using container_type = std::vector; using iterator = typename std::vector::iterator; using const_iterator = typename std::vector::const_iterator; template explicit flat_set(Ts&&... args) : c{std::forward(args)...} {} auto begin() -> iterator { return c.begin(); } auto end() -> iterator { return c.end(); } auto begin() const -> const_iterator { return c.begin(); } auto end() const -> const_iterator { return c.end(); } private: std::vector c; }; TEST(ranges_test, format_flat_set) { EXPECT_EQ(fmt::format("{}", flat_set{"one", "two"}), "{\"one\", \"two\"}"); } namespace adl { struct box { int value; }; auto begin(const box& b) -> const int* { return &b.value; } auto end(const box& b) -> const int* { return &b.value + 1; } } // namespace adl TEST(ranges_test, format_adl_begin_end) { auto b = adl::box{42}; EXPECT_EQ(fmt::format("{}", b), "[42]"); } TEST(ranges_test, format_pair) { auto p = std::pair(42, 1.5f); EXPECT_EQ(fmt::format("{}", p), "(42, 1.5)"); } struct unformattable {}; TEST(ranges_test, format_tuple) { auto t = std::tuple(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<>()), "()"); EXPECT_TRUE((fmt::is_formattable>::value)); EXPECT_FALSE((fmt::is_formattable::value)); EXPECT_FALSE((fmt::is_formattable>::value)); EXPECT_FALSE((fmt::is_formattable>::value)); EXPECT_FALSE((fmt::is_formattable>::value)); EXPECT_FALSE( (fmt::is_formattable>::value)); EXPECT_TRUE((fmt::is_formattable>::value)); } struct not_default_formattable {}; struct bad_format {}; FMT_BEGIN_NAMESPACE template <> struct formatter { auto parse(format_parse_context&) -> const char* { throw bad_format(); } auto format(not_default_formattable, format_context& ctx) -> format_context::iterator { return ctx.out(); } }; FMT_END_NAMESPACE TEST(ranges_test, tuple_parse_calls_element_parse) { auto f = fmt::formatter>(); auto ctx = fmt::format_parse_context(""); EXPECT_THROW(f.parse(ctx), bad_format); } #ifdef FMT_RANGES_TEST_ENABLE_FORMAT_STRUCT struct tuple_like { int i; std::string str; template auto get() const noexcept -> fmt::enable_if_t { return i; } template auto get() const noexcept -> fmt::enable_if_t { return str; } }; template auto get(const tuple_like& t) noexcept -> decltype(t.get()) { return t.get(); } namespace std { template <> struct tuple_size : std::integral_constant {}; template struct tuple_element { using type = decltype(std::declval().get()); }; } // namespace std TEST(ranges_test, format_struct) { auto t = tuple_like{42, "foo"}; EXPECT_EQ(fmt::format("{}", t), "(42, \"foo\")"); } #endif // FMT_RANGES_TEST_ENABLE_FORMAT_STRUCT TEST(ranges_test, format_to) { char buf[10]; auto end = fmt::format_to(buf, "{}", std::vector{1, 2, 3}); *end = '\0'; EXPECT_STREQ(buf, "[1, 2, 3]"); } template struct path_like { auto begin() const -> const path_like*; auto end() const -> const path_like*; operator std::basic_string() const; }; TEST(ranges_test, disabled_range_formatting_of_path) { // Range formatting of path is disabled because of infinite recursion // (path element is itself a path). EXPECT_EQ((fmt::range_format_kind, char>::value), fmt::range_format::disabled); EXPECT_EQ((fmt::range_format_kind, char>::value), fmt::range_format::disabled); } // A range that provides non-const only begin()/end() to test fmt::join // handles that. // // Some ranges (e.g. those produced by range-v3's views::filter()) can cache // information during iteration so they only provide non-const begin()/end(). template class non_const_only_range { private: std::vector vec; public: using const_iterator = typename ::std::vector::const_iterator; template explicit non_const_only_range(Args&&... args) : vec(std::forward(args)...) {} auto begin() -> const_iterator { return vec.begin(); } auto end() -> const_iterator { return vec.end(); } }; template class noncopyable_range { private: std::vector vec; public: using iterator = typename ::std::vector::iterator; template explicit noncopyable_range(Args&&... args) : vec(std::forward(args)...) {} noncopyable_range(noncopyable_range const&) = delete; noncopyable_range(noncopyable_range&) = delete; auto begin() -> iterator { return vec.begin(); } auto end() -> iterator { return vec.end(); } }; TEST(ranges_test, range) { auto&& w = noncopyable_range(3u, 0); EXPECT_EQ(fmt::format("{}", w), "[0, 0, 0]"); EXPECT_EQ(fmt::format("{}", noncopyable_range(3u, 0)), "[0, 0, 0]"); auto x = non_const_only_range(3u, 0); EXPECT_EQ(fmt::format("{}", x), "[0, 0, 0]"); EXPECT_EQ(fmt::format("{}", non_const_only_range(3u, 0)), "[0, 0, 0]"); auto y = std::vector(3u, 0); EXPECT_EQ(fmt::format("{}", y), "[0, 0, 0]"); EXPECT_EQ(fmt::format("{}", std::vector(3u, 0)), "[0, 0, 0]"); const auto z = std::vector(3u, 0); EXPECT_EQ(fmt::format("{}", z), "[0, 0, 0]"); } enum test_enum { foo }; auto format_as(test_enum e) -> int { return e; } TEST(ranges_test, enum_range) { auto v = std::vector{test_enum::foo}; EXPECT_EQ(fmt::format("{}", v), "[0]"); } #if !FMT_MSC_VERSION TEST(ranges_test, unformattable_range) { EXPECT_FALSE((fmt::has_formatter, fmt::format_context>::value)); } #endif #ifdef FMT_RANGES_TEST_ENABLE_JOIN TEST(ranges_test, join_tuple) { // Value tuple args. auto t1 = std::tuple('a', 1, 2.0f); EXPECT_EQ(fmt::format("({})", fmt::join(t1, ", ")), "(a, 1, 2)"); // Testing lvalue tuple args. int x = 4; auto t2 = std::tuple('b', x); EXPECT_EQ(fmt::format("{}", fmt::join(t2, " + ")), "b + 4"); // Empty tuple. auto t3 = std::tuple<>(); EXPECT_EQ(fmt::format("{}", fmt::join(t3, "|")), ""); // Single element tuple. auto t4 = std::tuple(4.0f); EXPECT_EQ(fmt::format("{}", fmt::join(t4, "/")), "4"); # if FMT_TUPLE_JOIN_SPECIFIERS // Specs applied to each element. auto t5 = std::tuple(-3, 100, 1); EXPECT_EQ(fmt::format("{:+03}", fmt::join(t5, ", ")), "-03, +100, +01"); auto t6 = std::tuple(3, 3.14, 3.1415); EXPECT_EQ(fmt::format("{:5.5f}", fmt::join(t6, ", ")), "3.00000, 3.14000, 3.14150"); // Testing lvalue tuple args. int y = -1; auto t7 = std::tuple(3, y, y); EXPECT_EQ(fmt::format("{:03}", fmt::join(t7, ", ")), "003, -01, -01"); # endif } 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 !"); } 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; auto begin() const -> const char* { return p; } auto end() const -> zstring_sentinel { return {}; } }; # ifdef __cpp_lib_ranges struct cpp20_only_range { struct iterator { int val = 0; using value_type = int; using difference_type = std::ptrdiff_t; using iterator_concept = std::input_iterator_tag; iterator() = default; iterator(int i) : val(i) {} auto operator*() const -> int { return val; } auto operator++() -> iterator& { ++val; return *this; } void operator++(int) { ++*this; } auto operator==(const iterator& rhs) const -> bool { return val == rhs.val; } }; int lo; int hi; auto begin() const -> iterator { return iterator(lo); } auto end() const -> iterator { return iterator(hi); } }; static_assert(std::input_iterator); # endif TEST(ranges_test, join_sentinel) { auto hello = zstring{"hello"}; EXPECT_EQ(fmt::format("{}", hello), "['h', 'e', 'l', 'l', 'o']"); EXPECT_EQ(fmt::format("{::}", hello), "[h, e, l, l, o]"); EXPECT_EQ(fmt::format("{}", fmt::join(hello, "_")), "h_e_l_l_o"); } TEST(ranges_test, join_range) { auto&& w = noncopyable_range(3u, 0); EXPECT_EQ(fmt::format("{}", fmt::join(w, ",")), "0,0,0"); EXPECT_EQ(fmt::format("{}", fmt::join(noncopyable_range(3u, 0), ",")), "0,0,0"); auto x = non_const_only_range(3u, 0); EXPECT_EQ(fmt::format("{}", fmt::join(x, ",")), "0,0,0"); EXPECT_EQ(fmt::format("{}", fmt::join(non_const_only_range(3u, 0), ",")), "0,0,0"); auto y = std::vector(3u, 0); EXPECT_EQ(fmt::format("{}", fmt::join(y, ",")), "0,0,0"); EXPECT_EQ(fmt::format("{}", fmt::join(std::vector(3u, 0), ",")), "0,0,0"); const auto z = std::vector(3u, 0); EXPECT_EQ(fmt::format("{}", fmt::join(z, ",")), "0,0,0"); # ifdef __cpp_lib_ranges EXPECT_EQ(fmt::format("{}", cpp20_only_range{.lo = 0, .hi = 5}), "[0, 1, 2, 3, 4]"); EXPECT_EQ( fmt::format("{}", fmt::join(cpp20_only_range{.lo = 0, .hi = 5}, ",")), "0,1,2,3,4"); # endif } #endif // FMT_RANGES_TEST_ENABLE_JOIN #if defined(__cpp_lib_ranges) && __cpp_lib_ranges >= 202302L TEST(ranges_test, nested_ranges) { auto l = std::list{1, 2, 3}; auto r = std::views::iota(0, 3) | std::views::transform([&l](auto i) { return std::views::take(std::ranges::subrange(l), i); }) | std::views::transform(std::views::reverse); EXPECT_EQ(fmt::format("{}", r), "[[], [1], [2, 1]]"); } #endif TEST(ranges_test, is_printable) { using fmt::detail::is_printable; EXPECT_TRUE(is_printable(0x0323)); EXPECT_FALSE(is_printable(0x0378)); EXPECT_FALSE(is_printable(0x110000)); } TEST(ranges_test, escape) { using vec = std::vector; 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\"]"); EXPECT_EQ(fmt::format("{}", vec{"n\xcc\x83"}), "[\"n\xcc\x83\"]"); if (fmt::detail::is_utf8()) { EXPECT_EQ(fmt::format("{}", vec{"\xcd\xb8"}), "[\"\\u0378\"]"); // Unassigned Unicode code points. EXPECT_EQ(fmt::format("{}", vec{"\xf0\xaa\x9b\x9e"}), "[\"\\U0002a6de\"]"); // Broken utf-8. EXPECT_EQ(fmt::format("{}", vec{"\xf4\x8f\xbf\xc0"}), "[\"\\xf4\\x8f\\xbf\\xc0\"]"); EXPECT_EQ(fmt::format("{}", vec{"\xf0\x28"}), "[\"\\xf0(\"]"); EXPECT_EQ(fmt::format("{}", vec{"\xe1\x28"}), "[\"\\xe1(\"]"); EXPECT_EQ(fmt::format("{}", vec{std::string("\xf0\x28\0\0anything", 12)}), "[\"\\xf0(\\x00\\x00anything\"]"); // Correct utf-8. EXPECT_EQ(fmt::format("{}", vec{"🦄"}), "[\"🦄\"]"); } EXPECT_EQ(fmt::format("{}", std::vector>{{'x'}}), "[['x']]"); EXPECT_EQ(fmt::format("{}", std::tuple>{{'x'}}), "(['x'])"); } template struct fmt_ref_view { R* r; auto begin() const -> decltype(r->begin()) { return r->begin(); } auto end() const -> decltype(r->end()) { return r->end(); } }; TEST(ranges_test, range_of_range_of_mixed_const) { auto v = std::vector>{{1, 2, 3}, {4, 5}}; EXPECT_EQ(fmt::format("{}", v), "[[1, 2, 3], [4, 5]]"); auto r = fmt_ref_view{&v}; EXPECT_EQ(fmt::format("{}", r), "[[1, 2, 3], [4, 5]]"); } TEST(ranges_test, vector_char) { EXPECT_EQ(fmt::format("{}", std::vector{'a', 'b'}), "['a', 'b']"); } TEST(ranges_test, container_adaptor) { { using fmt::detail::is_container_adaptor_like; using T = std::nullptr_t; static_assert(is_container_adaptor_like>::value, ""); static_assert(is_container_adaptor_like>::value, ""); static_assert(is_container_adaptor_like>::value, ""); static_assert(!is_container_adaptor_like>::value, ""); } { auto s = std::stack(); s.push(1); s.push(2); EXPECT_EQ(fmt::format("{}", s), "[1, 2]"); EXPECT_EQ(fmt::format("{}", const_cast(s)), "[1, 2]"); } { auto q = std::queue(); q.push(1); q.push(2); EXPECT_EQ(fmt::format("{}", q), "[1, 2]"); } { auto q = std::priority_queue(); q.push(3); q.push(1); q.push(2); q.push(4); EXPECT_EQ(fmt::format("{}", q), "[4, 3, 2, 1]"); } { auto s = std::stack(); s.push('a'); s.push('b'); // See https://cplusplus.github.io/LWG/issue3881. EXPECT_EQ(fmt::format("{}", s), "['a', 'b']"); } { struct my_container_adaptor { using value_type = int; using container_type = std::vector; void push(const value_type& v) { c.push_back(v); } protected: container_type c; }; auto m = my_container_adaptor(); m.push(1); m.push(2); EXPECT_EQ(fmt::format("{}", m), "[1, 2]"); } } struct tieable { int a = 3; double b = 0.42; }; auto format_as(const tieable& t) -> std::tuple { return std::tie(t.a, t.b); } TEST(ranges_test, format_as_tie) { EXPECT_EQ(fmt::format("{}", tieable()), "(3, 0.42)"); }