Mercurial > minori
comparison dep/fmt/test/ranges-test.cc @ 343:1faa72660932
*: transfer back to cmake from autotools
autotools just made lots of things more complicated than
they should have and many things broke (i.e. translations)
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Thu, 20 Jun 2024 05:56:06 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
342:adb79bdde329 | 343:1faa72660932 |
---|---|
1 // Formatting library for C++ - ranges tests | |
2 // | |
3 // Copyright (c) 2012 - present, Victor Zverovich and {fmt} contributors | |
4 // All rights reserved. | |
5 // | |
6 // For the license information refer to format.h. | |
7 | |
8 #include "fmt/ranges.h" | |
9 | |
10 #include <list> | |
11 #include <map> | |
12 #include <numeric> | |
13 #include <queue> | |
14 #include <stack> | |
15 #include <string> | |
16 #include <utility> | |
17 #include <vector> | |
18 | |
19 #if FMT_HAS_INCLUDE(<ranges>) | |
20 # include <ranges> | |
21 #endif | |
22 | |
23 #include "gtest/gtest.h" | |
24 | |
25 #if !FMT_GCC_VERSION || FMT_GCC_VERSION >= 601 | |
26 # define FMT_RANGES_TEST_ENABLE_C_STYLE_ARRAY | |
27 #endif | |
28 | |
29 #if !FMT_MSC_VERSION || FMT_MSC_VERSION > 1910 | |
30 # define FMT_RANGES_TEST_ENABLE_JOIN | |
31 # define FMT_RANGES_TEST_ENABLE_FORMAT_STRUCT | |
32 #endif | |
33 | |
34 #ifdef FMT_RANGES_TEST_ENABLE_C_STYLE_ARRAY | |
35 TEST(ranges_test, format_array) { | |
36 int arr[] = {1, 2, 3, 5, 7, 11}; | |
37 EXPECT_EQ(fmt::format("{}", arr), "[1, 2, 3, 5, 7, 11]"); | |
38 } | |
39 | |
40 TEST(ranges_test, format_2d_array) { | |
41 int arr[][2] = {{1, 2}, {3, 5}, {7, 11}}; | |
42 EXPECT_EQ(fmt::format("{}", arr), "[[1, 2], [3, 5], [7, 11]]"); | |
43 } | |
44 | |
45 TEST(ranges_test, format_array_of_literals) { | |
46 const char* arr[] = {"1234", "abcd"}; | |
47 EXPECT_EQ(fmt::format("{}", arr), "[\"1234\", \"abcd\"]"); | |
48 EXPECT_EQ(fmt::format("{:n}", arr), "\"1234\", \"abcd\""); | |
49 EXPECT_EQ(fmt::format("{:n:}", arr), "1234, abcd"); | |
50 } | |
51 #endif // FMT_RANGES_TEST_ENABLE_C_STYLE_ARRAY | |
52 | |
53 TEST(ranges_test, format_vector) { | |
54 auto v = std::vector<int>{1, 2, 3, 5, 7, 11}; | |
55 EXPECT_EQ(fmt::format("{}", v), "[1, 2, 3, 5, 7, 11]"); | |
56 EXPECT_EQ(fmt::format("{::#x}", v), "[0x1, 0x2, 0x3, 0x5, 0x7, 0xb]"); | |
57 EXPECT_EQ(fmt::format("{:n:#x}", v), "0x1, 0x2, 0x3, 0x5, 0x7, 0xb"); | |
58 | |
59 auto vc = std::vector<char>{'a', 'b', 'c'}; | |
60 auto vvc = std::vector<std::vector<char>>{vc, vc}; | |
61 EXPECT_EQ(fmt::format("{}", vc), "['a', 'b', 'c']"); | |
62 EXPECT_EQ(fmt::format("{}", vvc), "[['a', 'b', 'c'], ['a', 'b', 'c']]"); | |
63 EXPECT_EQ(fmt::format("{:n}", vvc), "['a', 'b', 'c'], ['a', 'b', 'c']"); | |
64 EXPECT_EQ(fmt::format("{:n:n}", vvc), "'a', 'b', 'c', 'a', 'b', 'c'"); | |
65 EXPECT_EQ(fmt::format("{:n:n:}", vvc), "a, b, c, a, b, c"); | |
66 } | |
67 | |
68 TEST(ranges_test, format_nested_vector) { | |
69 auto v = std::vector<std::vector<int>>{{1, 2}, {3, 5}, {7, 11}}; | |
70 EXPECT_EQ(fmt::format("{}", v), "[[1, 2], [3, 5], [7, 11]]"); | |
71 EXPECT_EQ(fmt::format("{:::#x}", v), "[[0x1, 0x2], [0x3, 0x5], [0x7, 0xb]]"); | |
72 EXPECT_EQ(fmt::format("{:n:n:#x}", v), "0x1, 0x2, 0x3, 0x5, 0x7, 0xb"); | |
73 } | |
74 | |
75 TEST(ranges_test, to_string_vector) { | |
76 auto v = std::vector<std::string>{"a", "b", "c"}; | |
77 EXPECT_EQ(fmt::to_string(v), "[\"a\", \"b\", \"c\"]"); | |
78 } | |
79 | |
80 TEST(ranges_test, format_map) { | |
81 auto m = std::map<std::string, int>{{"one", 1}, {"two", 2}}; | |
82 EXPECT_EQ(fmt::format("{}", m), "{\"one\": 1, \"two\": 2}"); | |
83 EXPECT_EQ(fmt::format("{:n}", m), "\"one\": 1, \"two\": 2"); | |
84 } | |
85 | |
86 TEST(ranges_test, format_set) { | |
87 EXPECT_EQ(fmt::format("{}", std::set<std::string>{"one", "two"}), | |
88 "{\"one\", \"two\"}"); | |
89 } | |
90 | |
91 // Models std::flat_set close enough to test if no ambiguous lookup of a | |
92 // formatter happens due to the flat_set type matching is_set and | |
93 // is_container_adaptor_like. | |
94 template <typename T> class flat_set { | |
95 public: | |
96 using key_type = T; | |
97 using container_type = std::vector<T>; | |
98 | |
99 using iterator = typename std::vector<T>::iterator; | |
100 using const_iterator = typename std::vector<T>::const_iterator; | |
101 | |
102 template <typename... Ts> | |
103 explicit flat_set(Ts&&... args) : c{std::forward<Ts>(args)...} {} | |
104 | |
105 auto begin() -> iterator { return c.begin(); } | |
106 auto end() -> iterator { return c.end(); } | |
107 | |
108 auto begin() const -> const_iterator { return c.begin(); } | |
109 auto end() const -> const_iterator { return c.end(); } | |
110 | |
111 private: | |
112 std::vector<T> c; | |
113 }; | |
114 | |
115 TEST(ranges_test, format_flat_set) { | |
116 EXPECT_EQ(fmt::format("{}", flat_set<std::string>{"one", "two"}), | |
117 "{\"one\", \"two\"}"); | |
118 } | |
119 | |
120 namespace adl { | |
121 struct box { | |
122 int value; | |
123 }; | |
124 | |
125 auto begin(const box& b) -> const int* { return &b.value; } | |
126 auto end(const box& b) -> const int* { return &b.value + 1; } | |
127 } // namespace adl | |
128 | |
129 TEST(ranges_test, format_adl_begin_end) { | |
130 auto b = adl::box{42}; | |
131 EXPECT_EQ(fmt::format("{}", b), "[42]"); | |
132 } | |
133 | |
134 TEST(ranges_test, format_pair) { | |
135 auto p = std::pair<int, float>(42, 1.5f); | |
136 EXPECT_EQ(fmt::format("{}", p), "(42, 1.5)"); | |
137 } | |
138 | |
139 struct unformattable {}; | |
140 | |
141 TEST(ranges_test, format_tuple) { | |
142 auto t = | |
143 std::tuple<int, float, std::string, char>(42, 1.5f, "this is tuple", 'i'); | |
144 EXPECT_EQ(fmt::format("{}", t), "(42, 1.5, \"this is tuple\", 'i')"); | |
145 | |
146 EXPECT_EQ(fmt::format("{}", std::tuple<>()), "()"); | |
147 | |
148 EXPECT_TRUE((fmt::is_formattable<std::tuple<>>::value)); | |
149 EXPECT_FALSE((fmt::is_formattable<unformattable>::value)); | |
150 EXPECT_FALSE((fmt::is_formattable<std::tuple<unformattable>>::value)); | |
151 EXPECT_FALSE((fmt::is_formattable<std::tuple<unformattable, int>>::value)); | |
152 EXPECT_FALSE((fmt::is_formattable<std::tuple<int, unformattable>>::value)); | |
153 EXPECT_FALSE( | |
154 (fmt::is_formattable<std::tuple<unformattable, unformattable>>::value)); | |
155 EXPECT_TRUE((fmt::is_formattable<std::tuple<int, float>>::value)); | |
156 } | |
157 | |
158 struct not_default_formattable {}; | |
159 struct bad_format {}; | |
160 | |
161 FMT_BEGIN_NAMESPACE | |
162 template <> struct formatter<not_default_formattable> { | |
163 auto parse(format_parse_context&) -> const char* { throw bad_format(); } | |
164 auto format(not_default_formattable, format_context& ctx) | |
165 -> format_context::iterator { | |
166 return ctx.out(); | |
167 } | |
168 }; | |
169 FMT_END_NAMESPACE | |
170 | |
171 TEST(ranges_test, tuple_parse_calls_element_parse) { | |
172 auto f = fmt::formatter<std::tuple<not_default_formattable>>(); | |
173 auto ctx = fmt::format_parse_context(""); | |
174 EXPECT_THROW(f.parse(ctx), bad_format); | |
175 } | |
176 | |
177 #ifdef FMT_RANGES_TEST_ENABLE_FORMAT_STRUCT | |
178 struct tuple_like { | |
179 int i; | |
180 std::string str; | |
181 | |
182 template <size_t N> | |
183 auto get() const noexcept -> fmt::enable_if_t<N == 0, int> { | |
184 return i; | |
185 } | |
186 template <size_t N> | |
187 auto get() const noexcept -> fmt::enable_if_t<N == 1, fmt::string_view> { | |
188 return str; | |
189 } | |
190 }; | |
191 | |
192 template <size_t N> | |
193 auto get(const tuple_like& t) noexcept -> decltype(t.get<N>()) { | |
194 return t.get<N>(); | |
195 } | |
196 | |
197 namespace std { | |
198 template <> | |
199 struct tuple_size<tuple_like> : std::integral_constant<size_t, 2> {}; | |
200 | |
201 template <size_t N> struct tuple_element<N, tuple_like> { | |
202 using type = decltype(std::declval<tuple_like>().get<N>()); | |
203 }; | |
204 } // namespace std | |
205 | |
206 TEST(ranges_test, format_struct) { | |
207 auto t = tuple_like{42, "foo"}; | |
208 EXPECT_EQ(fmt::format("{}", t), "(42, \"foo\")"); | |
209 } | |
210 #endif // FMT_RANGES_TEST_ENABLE_FORMAT_STRUCT | |
211 | |
212 TEST(ranges_test, format_to) { | |
213 char buf[10]; | |
214 auto end = fmt::format_to(buf, "{}", std::vector<int>{1, 2, 3}); | |
215 *end = '\0'; | |
216 EXPECT_STREQ(buf, "[1, 2, 3]"); | |
217 } | |
218 | |
219 template <typename Char> struct path_like { | |
220 auto begin() const -> const path_like*; | |
221 auto end() const -> const path_like*; | |
222 | |
223 operator std::basic_string<Char>() const; | |
224 }; | |
225 | |
226 TEST(ranges_test, disabled_range_formatting_of_path) { | |
227 // Range formatting of path is disabled because of infinite recursion | |
228 // (path element is itself a path). | |
229 EXPECT_EQ((fmt::range_format_kind<path_like<char>, char>::value), | |
230 fmt::range_format::disabled); | |
231 EXPECT_EQ((fmt::range_format_kind<path_like<wchar_t>, char>::value), | |
232 fmt::range_format::disabled); | |
233 } | |
234 | |
235 // A range that provides non-const only begin()/end() to test fmt::join | |
236 // handles that. | |
237 // | |
238 // Some ranges (e.g. those produced by range-v3's views::filter()) can cache | |
239 // information during iteration so they only provide non-const begin()/end(). | |
240 template <typename T> class non_const_only_range { | |
241 private: | |
242 std::vector<T> vec; | |
243 | |
244 public: | |
245 using const_iterator = typename ::std::vector<T>::const_iterator; | |
246 | |
247 template <typename... Args> | |
248 explicit non_const_only_range(Args&&... args) | |
249 : vec(std::forward<Args>(args)...) {} | |
250 | |
251 auto begin() -> const_iterator { return vec.begin(); } | |
252 auto end() -> const_iterator { return vec.end(); } | |
253 }; | |
254 | |
255 template <typename T> class noncopyable_range { | |
256 private: | |
257 std::vector<T> vec; | |
258 | |
259 public: | |
260 using iterator = typename ::std::vector<T>::iterator; | |
261 | |
262 template <typename... Args> | |
263 explicit noncopyable_range(Args&&... args) | |
264 : vec(std::forward<Args>(args)...) {} | |
265 | |
266 noncopyable_range(noncopyable_range const&) = delete; | |
267 noncopyable_range(noncopyable_range&) = delete; | |
268 | |
269 auto begin() -> iterator { return vec.begin(); } | |
270 auto end() -> iterator { return vec.end(); } | |
271 }; | |
272 | |
273 TEST(ranges_test, range) { | |
274 auto&& w = noncopyable_range<int>(3u, 0); | |
275 EXPECT_EQ(fmt::format("{}", w), "[0, 0, 0]"); | |
276 EXPECT_EQ(fmt::format("{}", noncopyable_range<int>(3u, 0)), "[0, 0, 0]"); | |
277 | |
278 auto x = non_const_only_range<int>(3u, 0); | |
279 EXPECT_EQ(fmt::format("{}", x), "[0, 0, 0]"); | |
280 EXPECT_EQ(fmt::format("{}", non_const_only_range<int>(3u, 0)), "[0, 0, 0]"); | |
281 | |
282 auto y = std::vector<int>(3u, 0); | |
283 EXPECT_EQ(fmt::format("{}", y), "[0, 0, 0]"); | |
284 EXPECT_EQ(fmt::format("{}", std::vector<int>(3u, 0)), "[0, 0, 0]"); | |
285 | |
286 const auto z = std::vector<int>(3u, 0); | |
287 EXPECT_EQ(fmt::format("{}", z), "[0, 0, 0]"); | |
288 } | |
289 | |
290 enum test_enum { foo }; | |
291 auto format_as(test_enum e) -> int { return e; } | |
292 | |
293 TEST(ranges_test, enum_range) { | |
294 auto v = std::vector<test_enum>{test_enum::foo}; | |
295 EXPECT_EQ(fmt::format("{}", v), "[0]"); | |
296 } | |
297 | |
298 #if !FMT_MSC_VERSION | |
299 TEST(ranges_test, unformattable_range) { | |
300 EXPECT_FALSE((fmt::has_formatter<std::vector<unformattable>, | |
301 fmt::format_context>::value)); | |
302 } | |
303 #endif | |
304 | |
305 #ifdef FMT_RANGES_TEST_ENABLE_JOIN | |
306 TEST(ranges_test, join_tuple) { | |
307 // Value tuple args. | |
308 auto t1 = std::tuple<char, int, float>('a', 1, 2.0f); | |
309 EXPECT_EQ(fmt::format("({})", fmt::join(t1, ", ")), "(a, 1, 2)"); | |
310 | |
311 // Testing lvalue tuple args. | |
312 int x = 4; | |
313 auto t2 = std::tuple<char, int&>('b', x); | |
314 EXPECT_EQ(fmt::format("{}", fmt::join(t2, " + ")), "b + 4"); | |
315 | |
316 // Empty tuple. | |
317 auto t3 = std::tuple<>(); | |
318 EXPECT_EQ(fmt::format("{}", fmt::join(t3, "|")), ""); | |
319 | |
320 // Single element tuple. | |
321 auto t4 = std::tuple<float>(4.0f); | |
322 EXPECT_EQ(fmt::format("{}", fmt::join(t4, "/")), "4"); | |
323 | |
324 # if FMT_TUPLE_JOIN_SPECIFIERS | |
325 // Specs applied to each element. | |
326 auto t5 = std::tuple<int, int, long>(-3, 100, 1); | |
327 EXPECT_EQ(fmt::format("{:+03}", fmt::join(t5, ", ")), "-03, +100, +01"); | |
328 | |
329 auto t6 = std::tuple<float, double, long double>(3, 3.14, 3.1415); | |
330 EXPECT_EQ(fmt::format("{:5.5f}", fmt::join(t6, ", ")), | |
331 "3.00000, 3.14000, 3.14150"); | |
332 | |
333 // Testing lvalue tuple args. | |
334 int y = -1; | |
335 auto t7 = std::tuple<int, int&, const int&>(3, y, y); | |
336 EXPECT_EQ(fmt::format("{:03}", fmt::join(t7, ", ")), "003, -01, -01"); | |
337 # endif | |
338 } | |
339 | |
340 TEST(ranges_test, join_initializer_list) { | |
341 EXPECT_EQ(fmt::format("{}", fmt::join({1, 2, 3}, ", ")), "1, 2, 3"); | |
342 EXPECT_EQ(fmt::format("{}", fmt::join({"fmt", "rocks", "!"}, " ")), | |
343 "fmt rocks !"); | |
344 } | |
345 | |
346 struct zstring_sentinel {}; | |
347 | |
348 bool operator==(const char* p, zstring_sentinel) { return *p == '\0'; } | |
349 bool operator!=(const char* p, zstring_sentinel) { return *p != '\0'; } | |
350 | |
351 struct zstring { | |
352 const char* p; | |
353 auto begin() const -> const char* { return p; } | |
354 auto end() const -> zstring_sentinel { return {}; } | |
355 }; | |
356 | |
357 # ifdef __cpp_lib_ranges | |
358 struct cpp20_only_range { | |
359 struct iterator { | |
360 int val = 0; | |
361 | |
362 using value_type = int; | |
363 using difference_type = std::ptrdiff_t; | |
364 using iterator_concept = std::input_iterator_tag; | |
365 | |
366 iterator() = default; | |
367 iterator(int i) : val(i) {} | |
368 auto operator*() const -> int { return val; } | |
369 auto operator++() -> iterator& { | |
370 ++val; | |
371 return *this; | |
372 } | |
373 void operator++(int) { ++*this; } | |
374 auto operator==(const iterator& rhs) const -> bool { | |
375 return val == rhs.val; | |
376 } | |
377 }; | |
378 | |
379 int lo; | |
380 int hi; | |
381 | |
382 auto begin() const -> iterator { return iterator(lo); } | |
383 auto end() const -> iterator { return iterator(hi); } | |
384 }; | |
385 | |
386 static_assert(std::input_iterator<cpp20_only_range::iterator>); | |
387 # endif | |
388 | |
389 TEST(ranges_test, join_sentinel) { | |
390 auto hello = zstring{"hello"}; | |
391 EXPECT_EQ(fmt::format("{}", hello), "['h', 'e', 'l', 'l', 'o']"); | |
392 EXPECT_EQ(fmt::format("{::}", hello), "[h, e, l, l, o]"); | |
393 EXPECT_EQ(fmt::format("{}", fmt::join(hello, "_")), "h_e_l_l_o"); | |
394 } | |
395 | |
396 TEST(ranges_test, join_range) { | |
397 auto&& w = noncopyable_range<int>(3u, 0); | |
398 EXPECT_EQ(fmt::format("{}", fmt::join(w, ",")), "0,0,0"); | |
399 EXPECT_EQ(fmt::format("{}", fmt::join(noncopyable_range<int>(3u, 0), ",")), | |
400 "0,0,0"); | |
401 | |
402 auto x = non_const_only_range<int>(3u, 0); | |
403 EXPECT_EQ(fmt::format("{}", fmt::join(x, ",")), "0,0,0"); | |
404 EXPECT_EQ(fmt::format("{}", fmt::join(non_const_only_range<int>(3u, 0), ",")), | |
405 "0,0,0"); | |
406 | |
407 auto y = std::vector<int>(3u, 0); | |
408 EXPECT_EQ(fmt::format("{}", fmt::join(y, ",")), "0,0,0"); | |
409 EXPECT_EQ(fmt::format("{}", fmt::join(std::vector<int>(3u, 0), ",")), | |
410 "0,0,0"); | |
411 | |
412 const auto z = std::vector<int>(3u, 0); | |
413 EXPECT_EQ(fmt::format("{}", fmt::join(z, ",")), "0,0,0"); | |
414 | |
415 # ifdef __cpp_lib_ranges | |
416 EXPECT_EQ(fmt::format("{}", cpp20_only_range{.lo = 0, .hi = 5}), | |
417 "[0, 1, 2, 3, 4]"); | |
418 EXPECT_EQ( | |
419 fmt::format("{}", fmt::join(cpp20_only_range{.lo = 0, .hi = 5}, ",")), | |
420 "0,1,2,3,4"); | |
421 # endif | |
422 } | |
423 #endif // FMT_RANGES_TEST_ENABLE_JOIN | |
424 | |
425 #if defined(__cpp_lib_ranges) && __cpp_lib_ranges >= 202302L | |
426 TEST(ranges_test, nested_ranges) { | |
427 auto l = std::list{1, 2, 3}; | |
428 auto r = std::views::iota(0, 3) | std::views::transform([&l](auto i) { | |
429 return std::views::take(std::ranges::subrange(l), i); | |
430 }) | | |
431 std::views::transform(std::views::reverse); | |
432 EXPECT_EQ(fmt::format("{}", r), "[[], [1], [2, 1]]"); | |
433 } | |
434 #endif | |
435 | |
436 TEST(ranges_test, is_printable) { | |
437 using fmt::detail::is_printable; | |
438 EXPECT_TRUE(is_printable(0x0323)); | |
439 EXPECT_FALSE(is_printable(0x0378)); | |
440 EXPECT_FALSE(is_printable(0x110000)); | |
441 } | |
442 | |
443 TEST(ranges_test, escape) { | |
444 using vec = std::vector<std::string>; | |
445 EXPECT_EQ(fmt::format("{}", vec{"\n\r\t\"\\"}), "[\"\\n\\r\\t\\\"\\\\\"]"); | |
446 EXPECT_EQ(fmt::format("{}", vec{"\x07"}), "[\"\\x07\"]"); | |
447 EXPECT_EQ(fmt::format("{}", vec{"\x7f"}), "[\"\\x7f\"]"); | |
448 EXPECT_EQ(fmt::format("{}", vec{"n\xcc\x83"}), "[\"n\xcc\x83\"]"); | |
449 | |
450 if (fmt::detail::is_utf8()) { | |
451 EXPECT_EQ(fmt::format("{}", vec{"\xcd\xb8"}), "[\"\\u0378\"]"); | |
452 // Unassigned Unicode code points. | |
453 EXPECT_EQ(fmt::format("{}", vec{"\xf0\xaa\x9b\x9e"}), "[\"\\U0002a6de\"]"); | |
454 // Broken utf-8. | |
455 EXPECT_EQ(fmt::format("{}", vec{"\xf4\x8f\xbf\xc0"}), | |
456 "[\"\\xf4\\x8f\\xbf\\xc0\"]"); | |
457 EXPECT_EQ(fmt::format("{}", vec{"\xf0\x28"}), "[\"\\xf0(\"]"); | |
458 EXPECT_EQ(fmt::format("{}", vec{"\xe1\x28"}), "[\"\\xe1(\"]"); | |
459 EXPECT_EQ(fmt::format("{}", vec{std::string("\xf0\x28\0\0anything", 12)}), | |
460 "[\"\\xf0(\\x00\\x00anything\"]"); | |
461 | |
462 // Correct utf-8. | |
463 EXPECT_EQ(fmt::format("{}", vec{"🦄"}), "[\"🦄\"]"); | |
464 } | |
465 | |
466 EXPECT_EQ(fmt::format("{}", std::vector<std::vector<char>>{{'x'}}), | |
467 "[['x']]"); | |
468 EXPECT_EQ(fmt::format("{}", std::tuple<std::vector<char>>{{'x'}}), "(['x'])"); | |
469 } | |
470 | |
471 template <typename R> struct fmt_ref_view { | |
472 R* r; | |
473 | |
474 auto begin() const -> decltype(r->begin()) { return r->begin(); } | |
475 auto end() const -> decltype(r->end()) { return r->end(); } | |
476 }; | |
477 | |
478 TEST(ranges_test, range_of_range_of_mixed_const) { | |
479 auto v = std::vector<std::vector<int>>{{1, 2, 3}, {4, 5}}; | |
480 EXPECT_EQ(fmt::format("{}", v), "[[1, 2, 3], [4, 5]]"); | |
481 | |
482 auto r = fmt_ref_view<decltype(v)>{&v}; | |
483 EXPECT_EQ(fmt::format("{}", r), "[[1, 2, 3], [4, 5]]"); | |
484 } | |
485 | |
486 TEST(ranges_test, vector_char) { | |
487 EXPECT_EQ(fmt::format("{}", std::vector<char>{'a', 'b'}), "['a', 'b']"); | |
488 } | |
489 | |
490 TEST(ranges_test, container_adaptor) { | |
491 { | |
492 using fmt::detail::is_container_adaptor_like; | |
493 using T = std::nullptr_t; | |
494 static_assert(is_container_adaptor_like<std::stack<T>>::value, ""); | |
495 static_assert(is_container_adaptor_like<std::queue<T>>::value, ""); | |
496 static_assert(is_container_adaptor_like<std::priority_queue<T>>::value, ""); | |
497 static_assert(!is_container_adaptor_like<std::vector<T>>::value, ""); | |
498 } | |
499 | |
500 { | |
501 auto s = std::stack<int>(); | |
502 s.push(1); | |
503 s.push(2); | |
504 EXPECT_EQ(fmt::format("{}", s), "[1, 2]"); | |
505 EXPECT_EQ(fmt::format("{}", const_cast<const decltype(s)&>(s)), "[1, 2]"); | |
506 } | |
507 | |
508 { | |
509 auto q = std::queue<int>(); | |
510 q.push(1); | |
511 q.push(2); | |
512 EXPECT_EQ(fmt::format("{}", q), "[1, 2]"); | |
513 } | |
514 | |
515 { | |
516 auto q = std::priority_queue<int>(); | |
517 q.push(3); | |
518 q.push(1); | |
519 q.push(2); | |
520 q.push(4); | |
521 EXPECT_EQ(fmt::format("{}", q), "[4, 3, 2, 1]"); | |
522 } | |
523 | |
524 { | |
525 auto s = std::stack<char, std::string>(); | |
526 s.push('a'); | |
527 s.push('b'); | |
528 // See https://cplusplus.github.io/LWG/issue3881. | |
529 EXPECT_EQ(fmt::format("{}", s), "['a', 'b']"); | |
530 } | |
531 | |
532 { | |
533 struct my_container_adaptor { | |
534 using value_type = int; | |
535 using container_type = std::vector<value_type>; | |
536 void push(const value_type& v) { c.push_back(v); } | |
537 | |
538 protected: | |
539 container_type c; | |
540 }; | |
541 | |
542 auto m = my_container_adaptor(); | |
543 m.push(1); | |
544 m.push(2); | |
545 EXPECT_EQ(fmt::format("{}", m), "[1, 2]"); | |
546 } | |
547 } | |
548 | |
549 struct tieable { | |
550 int a = 3; | |
551 double b = 0.42; | |
552 }; | |
553 | |
554 auto format_as(const tieable& t) -> std::tuple<int, double> { | |
555 return std::tie(t.a, t.b); | |
556 } | |
557 | |
558 TEST(ranges_test, format_as_tie) { | |
559 EXPECT_EQ(fmt::format("{}", tieable()), "(3, 0.42)"); | |
560 } |