Mercurial > minori
comparison dep/fmt/test/compile-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++ - formatting library tests | |
2 // | |
3 // Copyright (c) 2012 - present, Victor Zverovich | |
4 // All rights reserved. | |
5 // | |
6 // For the license information refer to format.h. | |
7 | |
8 #include "fmt/compile.h" | |
9 | |
10 #include <type_traits> | |
11 | |
12 #include "fmt/chrono.h" | |
13 #include "gmock/gmock.h" | |
14 #include "gtest-extra.h" | |
15 | |
16 TEST(iterator_test, counting_iterator) { | |
17 auto it = fmt::detail::counting_iterator(); | |
18 auto prev = it++; | |
19 EXPECT_EQ(prev.count(), 0); | |
20 EXPECT_EQ(it.count(), 1); | |
21 EXPECT_EQ((it + 41).count(), 42); | |
22 } | |
23 | |
24 TEST(compile_test, compile_fallback) { | |
25 // FMT_COMPILE should fallback on runtime formatting when `if constexpr` is | |
26 // not available. | |
27 EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42)); | |
28 } | |
29 | |
30 struct type_with_get { | |
31 template <int> friend void get(type_with_get); | |
32 }; | |
33 | |
34 FMT_BEGIN_NAMESPACE | |
35 template <> struct formatter<type_with_get> : formatter<int> { | |
36 template <typename FormatContext> | |
37 auto format(type_with_get, FormatContext& ctx) -> decltype(ctx.out()) { | |
38 return formatter<int>::format(42, ctx); | |
39 } | |
40 }; | |
41 FMT_END_NAMESPACE | |
42 | |
43 TEST(compile_test, compile_type_with_get) { | |
44 EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), type_with_get())); | |
45 } | |
46 | |
47 #if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction) | |
48 struct test_formattable {}; | |
49 | |
50 FMT_BEGIN_NAMESPACE | |
51 template <> struct formatter<test_formattable> : formatter<const char*> { | |
52 char word_spec = 'f'; | |
53 constexpr auto parse(format_parse_context& ctx) { | |
54 auto it = ctx.begin(), end = ctx.end(); | |
55 if (it == end || *it == '}') return it; | |
56 if (it != end && (*it == 'f' || *it == 'b')) word_spec = *it++; | |
57 if (it != end && *it != '}') throw format_error("invalid format"); | |
58 return it; | |
59 } | |
60 template <typename FormatContext> | |
61 constexpr auto format(test_formattable, FormatContext& ctx) const | |
62 -> decltype(ctx.out()) { | |
63 return formatter<const char*>::format(word_spec == 'f' ? "foo" : "bar", | |
64 ctx); | |
65 } | |
66 }; | |
67 FMT_END_NAMESPACE | |
68 | |
69 TEST(compile_test, format_default) { | |
70 EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42)); | |
71 EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42u)); | |
72 EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42ll)); | |
73 EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42ull)); | |
74 EXPECT_EQ("true", fmt::format(FMT_COMPILE("{}"), true)); | |
75 EXPECT_EQ("x", fmt::format(FMT_COMPILE("{}"), 'x')); | |
76 EXPECT_EQ("4.2", fmt::format(FMT_COMPILE("{}"), 4.2)); | |
77 EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), "foo")); | |
78 EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), std::string("foo"))); | |
79 EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), test_formattable())); | |
80 auto t = std::chrono::system_clock::now(); | |
81 EXPECT_EQ(fmt::format("{}", t), fmt::format(FMT_COMPILE("{}"), t)); | |
82 # ifdef __cpp_lib_byte | |
83 EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), std::byte{42})); | |
84 # endif | |
85 } | |
86 | |
87 TEST(compile_test, format_wide_string) { | |
88 EXPECT_EQ(L"42", fmt::format(FMT_COMPILE(L"{}"), 42)); | |
89 } | |
90 | |
91 TEST(compile_test, format_specs) { | |
92 EXPECT_EQ("42", fmt::format(FMT_COMPILE("{:x}"), 0x42)); | |
93 EXPECT_EQ("1.2 ms ", | |
94 fmt::format(FMT_COMPILE("{:7.1%Q %q}"), | |
95 std::chrono::duration<double, std::milli>(1.234))); | |
96 } | |
97 | |
98 TEST(compile_test, dynamic_format_specs) { | |
99 EXPECT_EQ("foo ", fmt::format(FMT_COMPILE("{:{}}"), "foo", 5)); | |
100 EXPECT_EQ(" 3.14", fmt::format(FMT_COMPILE("{:{}.{}f}"), 3.141592, 6, 2)); | |
101 EXPECT_EQ( | |
102 "=1.234ms=", | |
103 fmt::format(FMT_COMPILE("{:=^{}.{}}"), | |
104 std::chrono::duration<double, std::milli>(1.234), 9, 3)); | |
105 } | |
106 | |
107 TEST(compile_test, manual_ordering) { | |
108 EXPECT_EQ("42", fmt::format(FMT_COMPILE("{0}"), 42)); | |
109 EXPECT_EQ(" -42", fmt::format(FMT_COMPILE("{0:4}"), -42)); | |
110 EXPECT_EQ("41 43", fmt::format(FMT_COMPILE("{0} {1}"), 41, 43)); | |
111 EXPECT_EQ("41 43", fmt::format(FMT_COMPILE("{1} {0}"), 43, 41)); | |
112 EXPECT_EQ("41 43", fmt::format(FMT_COMPILE("{0} {2}"), 41, 42, 43)); | |
113 EXPECT_EQ(" 41 43", fmt::format(FMT_COMPILE("{1:{2}} {0:4}"), 43, 41, 4)); | |
114 EXPECT_EQ("42 1.2 ms ", | |
115 fmt::format(FMT_COMPILE("{0} {1:7.1%Q %q}"), 42, | |
116 std::chrono::duration<double, std::milli>(1.234))); | |
117 EXPECT_EQ( | |
118 "true 42 42 foo 0x1234 foo", | |
119 fmt::format(FMT_COMPILE("{0} {1} {2} {3} {4} {5}"), true, 42, 42.0f, | |
120 "foo", reinterpret_cast<void*>(0x1234), test_formattable())); | |
121 EXPECT_EQ(L"42", fmt::format(FMT_COMPILE(L"{0}"), 42)); | |
122 } | |
123 | |
124 TEST(compile_test, named) { | |
125 auto runtime_named_field_compiled = | |
126 fmt::detail::compile<decltype(fmt::arg("arg", 42))>(FMT_COMPILE("{arg}")); | |
127 static_assert(std::is_same_v<decltype(runtime_named_field_compiled), | |
128 fmt::detail::runtime_named_field<char>>); | |
129 | |
130 EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), fmt::arg("arg", 42))); | |
131 EXPECT_EQ("41 43", fmt::format(FMT_COMPILE("{} {}"), fmt::arg("arg", 41), | |
132 fmt::arg("arg", 43))); | |
133 | |
134 EXPECT_EQ("foobar", | |
135 fmt::format(FMT_COMPILE("{a0}{a1}"), fmt::arg("a0", "foo"), | |
136 fmt::arg("a1", "bar"))); | |
137 EXPECT_EQ("foobar", fmt::format(FMT_COMPILE("{}{a1}"), fmt::arg("a0", "foo"), | |
138 fmt::arg("a1", "bar"))); | |
139 EXPECT_EQ("foofoo", fmt::format(FMT_COMPILE("{a0}{}"), fmt::arg("a0", "foo"), | |
140 fmt::arg("a1", "bar"))); | |
141 EXPECT_EQ("foobar", fmt::format(FMT_COMPILE("{0}{a1}"), fmt::arg("a0", "foo"), | |
142 fmt::arg("a1", "bar"))); | |
143 EXPECT_EQ("foobar", fmt::format(FMT_COMPILE("{a0}{1}"), fmt::arg("a0", "foo"), | |
144 fmt::arg("a1", "bar"))); | |
145 | |
146 EXPECT_EQ("foobar", | |
147 fmt::format(FMT_COMPILE("{}{a1}"), "foo", fmt::arg("a1", "bar"))); | |
148 EXPECT_EQ("foobar", | |
149 fmt::format(FMT_COMPILE("{a0}{a1}"), fmt::arg("a1", "bar"), | |
150 fmt::arg("a2", "baz"), fmt::arg("a0", "foo"))); | |
151 EXPECT_EQ(" bar foo ", | |
152 fmt::format(FMT_COMPILE(" {foo} {bar} "), fmt::arg("foo", "bar"), | |
153 fmt::arg("bar", "foo"))); | |
154 | |
155 EXPECT_THROW(fmt::format(FMT_COMPILE("{invalid}"), fmt::arg("valid", 42)), | |
156 fmt::format_error); | |
157 | |
158 # if FMT_USE_NONTYPE_TEMPLATE_ARGS | |
159 using namespace fmt::literals; | |
160 auto statically_named_field_compiled = | |
161 fmt::detail::compile<decltype("arg"_a = 42)>(FMT_COMPILE("{arg}")); | |
162 static_assert(std::is_same_v<decltype(statically_named_field_compiled), | |
163 fmt::detail::field<char, int, 0>>); | |
164 | |
165 EXPECT_EQ("41 43", | |
166 fmt::format(FMT_COMPILE("{a0} {a1}"), "a0"_a = 41, "a1"_a = 43)); | |
167 EXPECT_EQ("41 43", | |
168 fmt::format(FMT_COMPILE("{a1} {a0}"), "a0"_a = 43, "a1"_a = 41)); | |
169 # endif | |
170 } | |
171 | |
172 TEST(compile_test, join) { | |
173 unsigned char data[] = {0x1, 0x2, 0xaf}; | |
174 EXPECT_EQ("0102af", fmt::format(FMT_COMPILE("{:02x}"), fmt::join(data, ""))); | |
175 } | |
176 | |
177 TEST(compile_test, format_to) { | |
178 char buf[8]; | |
179 auto end = fmt::format_to(buf, FMT_COMPILE("{}"), 42); | |
180 *end = '\0'; | |
181 EXPECT_STREQ("42", buf); | |
182 end = fmt::format_to(buf, FMT_COMPILE("{:x}"), 42); | |
183 *end = '\0'; | |
184 EXPECT_STREQ("2a", buf); | |
185 } | |
186 | |
187 TEST(compile_test, format_to_n) { | |
188 constexpr auto buffer_size = 8; | |
189 char buffer[buffer_size]; | |
190 auto res = fmt::format_to_n(buffer, buffer_size, FMT_COMPILE("{}"), 42); | |
191 *res.out = '\0'; | |
192 EXPECT_STREQ("42", buffer); | |
193 res = fmt::format_to_n(buffer, buffer_size, FMT_COMPILE("{:x}"), 42); | |
194 *res.out = '\0'; | |
195 EXPECT_STREQ("2a", buffer); | |
196 } | |
197 | |
198 # ifdef __cpp_lib_bit_cast | |
199 TEST(compile_test, constexpr_formatted_size) { | |
200 FMT_CONSTEXPR20 size_t size = fmt::formatted_size(FMT_COMPILE("{}"), 42); | |
201 EXPECT_EQ(size, 2); | |
202 FMT_CONSTEXPR20 size_t hex_size = | |
203 fmt::formatted_size(FMT_COMPILE("{:x}"), 15); | |
204 EXPECT_EQ(hex_size, 1); | |
205 FMT_CONSTEXPR20 size_t binary_size = | |
206 fmt::formatted_size(FMT_COMPILE("{:b}"), 15); | |
207 EXPECT_EQ(binary_size, 4); | |
208 FMT_CONSTEXPR20 size_t padded_size = | |
209 fmt::formatted_size(FMT_COMPILE("{:*^6}"), 42); | |
210 EXPECT_EQ(padded_size, 6); | |
211 FMT_CONSTEXPR20 size_t float_size = | |
212 fmt::formatted_size(FMT_COMPILE("{:.3}"), 12.345); | |
213 EXPECT_EQ(float_size, 4); | |
214 FMT_CONSTEXPR20 size_t str_size = | |
215 fmt::formatted_size(FMT_COMPILE("{:s}"), "abc"); | |
216 EXPECT_EQ(str_size, 3); | |
217 } | |
218 # endif | |
219 | |
220 TEST(compile_test, text_and_arg) { | |
221 EXPECT_EQ(">>>42<<<", fmt::format(FMT_COMPILE(">>>{}<<<"), 42)); | |
222 EXPECT_EQ("42!", fmt::format(FMT_COMPILE("{}!"), 42)); | |
223 } | |
224 | |
225 TEST(compile_test, unknown_format_fallback) { | |
226 EXPECT_EQ(" 42 ", | |
227 fmt::format(FMT_COMPILE("{name:^4}"), fmt::arg("name", 42))); | |
228 | |
229 std::vector<char> v; | |
230 fmt::format_to(std::back_inserter(v), FMT_COMPILE("{name:^4}"), | |
231 fmt::arg("name", 42)); | |
232 EXPECT_EQ(" 42 ", fmt::string_view(v.data(), v.size())); | |
233 | |
234 char buffer[4]; | |
235 auto result = fmt::format_to_n(buffer, 4, FMT_COMPILE("{name:^5}"), | |
236 fmt::arg("name", 42)); | |
237 EXPECT_EQ(5u, result.size); | |
238 EXPECT_EQ(buffer + 4, result.out); | |
239 EXPECT_EQ(" 42 ", fmt::string_view(buffer, 4)); | |
240 } | |
241 | |
242 TEST(compile_test, empty) { EXPECT_EQ("", fmt::format(FMT_COMPILE(""))); } | |
243 | |
244 struct to_stringable { | |
245 friend fmt::string_view to_string_view(to_stringable) { return {}; } | |
246 }; | |
247 | |
248 FMT_BEGIN_NAMESPACE | |
249 template <> struct formatter<to_stringable> { | |
250 auto parse(format_parse_context& ctx) const -> decltype(ctx.begin()) { | |
251 return ctx.begin(); | |
252 } | |
253 | |
254 template <typename FormatContext> | |
255 auto format(const to_stringable&, FormatContext& ctx) -> decltype(ctx.out()) { | |
256 return ctx.out(); | |
257 } | |
258 }; | |
259 FMT_END_NAMESPACE | |
260 | |
261 TEST(compile_test, to_string_and_formatter) { | |
262 fmt::format(FMT_COMPILE("{}"), to_stringable()); | |
263 } | |
264 | |
265 TEST(compile_test, print) { | |
266 EXPECT_WRITE(stdout, fmt::print(FMT_COMPILE("Don't {}!"), "panic"), | |
267 "Don't panic!"); | |
268 EXPECT_WRITE(stderr, fmt::print(stderr, FMT_COMPILE("Don't {}!"), "panic"), | |
269 "Don't panic!"); | |
270 } | |
271 #endif | |
272 | |
273 #if FMT_USE_NONTYPE_TEMPLATE_ARGS | |
274 TEST(compile_test, compile_format_string_literal) { | |
275 using namespace fmt::literals; | |
276 EXPECT_EQ("", fmt::format(""_cf)); | |
277 EXPECT_EQ("42", fmt::format("{}"_cf, 42)); | |
278 EXPECT_EQ(L"42", fmt::format(L"{}"_cf, 42)); | |
279 } | |
280 #endif | |
281 | |
282 // MSVS 2019 19.29.30145.0 - Support C++20 and OK. | |
283 // MSVS 2022 19.32.31332.0, 19.37.32826.1 - compile-test.cc(362,3): fatal error | |
284 // C1001: Internal compiler error. | |
285 // (compiler file | |
286 // 'D:\a\_work\1\s\src\vctools\Compiler\CxxFE\sl\p1\c\constexpr\constexpr.cpp', | |
287 // line 8635) | |
288 #if (FMT_CPLUSPLUS >= 202002L || \ | |
289 (FMT_CPLUSPLUS >= 201709L && FMT_GCC_VERSION >= 1002)) && \ | |
290 ((!defined(_GLIBCXX_RELEASE) || _GLIBCXX_RELEASE >= 10) && \ | |
291 (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 10000) && \ | |
292 (!FMT_MSC_VERSION || \ | |
293 (FMT_MSC_VERSION >= 1928 && FMT_MSC_VERSION < 1930))) && \ | |
294 defined(__cpp_lib_is_constant_evaluated) | |
295 template <size_t max_string_length, typename Char = char> struct test_string { | |
296 template <typename T> constexpr bool operator==(const T& rhs) const noexcept { | |
297 return fmt::basic_string_view<Char>(rhs).compare(buffer) == 0; | |
298 } | |
299 Char buffer[max_string_length]{}; | |
300 }; | |
301 | |
302 template <size_t max_string_length, typename Char = char, typename... Args> | |
303 consteval auto test_format(auto format, const Args&... args) { | |
304 test_string<max_string_length, Char> string{}; | |
305 fmt::format_to(string.buffer, format, args...); | |
306 return string; | |
307 } | |
308 | |
309 TEST(compile_time_formatting_test, bool) { | |
310 EXPECT_EQ("true", test_format<5>(FMT_COMPILE("{}"), true)); | |
311 EXPECT_EQ("false", test_format<6>(FMT_COMPILE("{}"), false)); | |
312 EXPECT_EQ("true ", test_format<6>(FMT_COMPILE("{:5}"), true)); | |
313 EXPECT_EQ("1", test_format<2>(FMT_COMPILE("{:d}"), true)); | |
314 } | |
315 | |
316 TEST(compile_time_formatting_test, integer) { | |
317 EXPECT_EQ("42", test_format<3>(FMT_COMPILE("{}"), 42)); | |
318 EXPECT_EQ("420", test_format<4>(FMT_COMPILE("{}"), 420)); | |
319 EXPECT_EQ("42 42", test_format<6>(FMT_COMPILE("{} {}"), 42, 42)); | |
320 EXPECT_EQ("42 42", | |
321 test_format<6>(FMT_COMPILE("{} {}"), uint32_t{42}, uint64_t{42})); | |
322 | |
323 EXPECT_EQ("+42", test_format<4>(FMT_COMPILE("{:+}"), 42)); | |
324 EXPECT_EQ("42", test_format<3>(FMT_COMPILE("{:-}"), 42)); | |
325 EXPECT_EQ(" 42", test_format<4>(FMT_COMPILE("{: }"), 42)); | |
326 | |
327 EXPECT_EQ("-0042", test_format<6>(FMT_COMPILE("{:05}"), -42)); | |
328 | |
329 EXPECT_EQ("101010", test_format<7>(FMT_COMPILE("{:b}"), 42)); | |
330 EXPECT_EQ("0b101010", test_format<9>(FMT_COMPILE("{:#b}"), 42)); | |
331 EXPECT_EQ("0B101010", test_format<9>(FMT_COMPILE("{:#B}"), 42)); | |
332 EXPECT_EQ("042", test_format<4>(FMT_COMPILE("{:#o}"), 042)); | |
333 EXPECT_EQ("0x4a", test_format<5>(FMT_COMPILE("{:#x}"), 0x4a)); | |
334 EXPECT_EQ("0X4A", test_format<5>(FMT_COMPILE("{:#X}"), 0x4a)); | |
335 | |
336 EXPECT_EQ(" 42", test_format<6>(FMT_COMPILE("{:5}"), 42)); | |
337 EXPECT_EQ(" 42", test_format<6>(FMT_COMPILE("{:5}"), 42ll)); | |
338 EXPECT_EQ(" 42", test_format<6>(FMT_COMPILE("{:5}"), 42ull)); | |
339 | |
340 EXPECT_EQ("42 ", test_format<5>(FMT_COMPILE("{:<4}"), 42)); | |
341 EXPECT_EQ(" 42", test_format<5>(FMT_COMPILE("{:>4}"), 42)); | |
342 EXPECT_EQ(" 42 ", test_format<5>(FMT_COMPILE("{:^4}"), 42)); | |
343 EXPECT_EQ("**-42", test_format<6>(FMT_COMPILE("{:*>5}"), -42)); | |
344 } | |
345 | |
346 TEST(compile_time_formatting_test, char) { | |
347 EXPECT_EQ("c", test_format<2>(FMT_COMPILE("{}"), 'c')); | |
348 | |
349 EXPECT_EQ("c ", test_format<4>(FMT_COMPILE("{:3}"), 'c')); | |
350 EXPECT_EQ("99", test_format<3>(FMT_COMPILE("{:d}"), 'c')); | |
351 } | |
352 | |
353 TEST(compile_time_formatting_test, string) { | |
354 EXPECT_EQ("42", test_format<3>(FMT_COMPILE("{}"), "42")); | |
355 EXPECT_EQ("The answer is 42", | |
356 test_format<17>(FMT_COMPILE("{} is {}"), "The answer", "42")); | |
357 | |
358 EXPECT_EQ("abc**", test_format<6>(FMT_COMPILE("{:*<5}"), "abc")); | |
359 EXPECT_EQ("**🤡**", test_format<9>(FMT_COMPILE("{:*^6}"), "🤡")); | |
360 } | |
361 | |
362 TEST(compile_time_formatting_test, combination) { | |
363 EXPECT_EQ("420, true, answer", | |
364 test_format<18>(FMT_COMPILE("{}, {}, {}"), 420, true, "answer")); | |
365 | |
366 EXPECT_EQ(" -42", test_format<5>(FMT_COMPILE("{:{}}"), -42, 4)); | |
367 } | |
368 | |
369 TEST(compile_time_formatting_test, custom_type) { | |
370 EXPECT_EQ("foo", test_format<4>(FMT_COMPILE("{}"), test_formattable())); | |
371 EXPECT_EQ("bar", test_format<4>(FMT_COMPILE("{:b}"), test_formattable())); | |
372 } | |
373 | |
374 TEST(compile_time_formatting_test, multibyte_fill) { | |
375 EXPECT_EQ("жж42", test_format<8>(FMT_COMPILE("{:ж>4}"), 42)); | |
376 } | |
377 #endif |