comparison dep/fmt/test/format-impl-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 implementation 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 <algorithm>
9 #include <cstring>
10
11 // clang-format off
12 #include "test-assert.h"
13 // clang-format on
14
15 #include "fmt/format.h"
16 #include "gmock/gmock.h"
17 #include "util.h"
18
19 using fmt::detail::bigint;
20 using fmt::detail::fp;
21 using fmt::detail::max_value;
22
23 static_assert(!std::is_copy_constructible<bigint>::value, "");
24 static_assert(!std::is_copy_assignable<bigint>::value, "");
25
26 TEST(bigint_test, construct) {
27 EXPECT_EQ(fmt::to_string(bigint()), "");
28 EXPECT_EQ(fmt::to_string(bigint(0x42)), "42");
29 EXPECT_EQ(fmt::to_string(bigint(0x123456789abcedf0)), "123456789abcedf0");
30 }
31
32 TEST(bigint_test, compare) {
33 bigint n1(42);
34 bigint n2(42);
35 EXPECT_EQ(compare(n1, n2), 0);
36 n2 <<= 32;
37 EXPECT_LT(compare(n1, n2), 0);
38 bigint n3(43);
39 EXPECT_LT(compare(n1, n3), 0);
40 EXPECT_GT(compare(n3, n1), 0);
41 bigint n4(42 * 0x100000001);
42 EXPECT_LT(compare(n2, n4), 0);
43 EXPECT_GT(compare(n4, n2), 0);
44 }
45
46 TEST(bigint_test, add_compare) {
47 EXPECT_LT(
48 add_compare(bigint(0xffffffff), bigint(0xffffffff), bigint(1) <<= 64), 0);
49 EXPECT_LT(add_compare(bigint(1) <<= 32, bigint(1), bigint(1) <<= 96), 0);
50 EXPECT_GT(add_compare(bigint(1) <<= 32, bigint(0), bigint(0xffffffff)), 0);
51 EXPECT_GT(add_compare(bigint(0), bigint(1) <<= 32, bigint(0xffffffff)), 0);
52 EXPECT_GT(add_compare(bigint(42), bigint(1), bigint(42)), 0);
53 EXPECT_GT(add_compare(bigint(0xffffffff), bigint(1), bigint(0xffffffff)), 0);
54 EXPECT_LT(add_compare(bigint(10), bigint(10), bigint(22)), 0);
55 EXPECT_LT(add_compare(bigint(0x100000010), bigint(0x100000010),
56 bigint(0x300000010)),
57 0);
58 EXPECT_GT(add_compare(bigint(0x1ffffffff), bigint(0x100000002),
59 bigint(0x300000000)),
60 0);
61 EXPECT_EQ(add_compare(bigint(0x1ffffffff), bigint(0x100000002),
62 bigint(0x300000001)),
63 0);
64 EXPECT_LT(add_compare(bigint(0x1ffffffff), bigint(0x100000002),
65 bigint(0x300000002)),
66 0);
67 EXPECT_LT(add_compare(bigint(0x1ffffffff), bigint(0x100000002),
68 bigint(0x300000003)),
69 0);
70 }
71
72 TEST(bigint_test, shift_left) {
73 bigint n(0x42);
74 n <<= 0;
75 EXPECT_EQ(fmt::to_string(n), "42");
76 n <<= 1;
77 EXPECT_EQ(fmt::to_string(n), "84");
78 n <<= 25;
79 EXPECT_EQ(fmt::to_string(n), "108000000");
80 }
81
82 TEST(bigint_test, multiply) {
83 bigint n(0x42);
84 EXPECT_THROW(n *= 0, assertion_failure);
85 n *= 1;
86 EXPECT_EQ(fmt::to_string(n), "42");
87
88 n *= 2;
89 EXPECT_EQ(fmt::to_string(n), "84");
90 n *= 0x12345678;
91 EXPECT_EQ(fmt::to_string(n), "962fc95e0");
92
93 bigint bigmax(max_value<uint32_t>());
94 bigmax *= max_value<uint32_t>();
95 EXPECT_EQ(fmt::to_string(bigmax), "fffffffe00000001");
96
97 const auto max64 = max_value<uint64_t>();
98 bigmax = max64;
99 bigmax *= max64;
100 EXPECT_EQ(fmt::to_string(bigmax), "fffffffffffffffe0000000000000001");
101
102 const auto max128 = (fmt::detail::uint128_t(max64) << 64) | max64;
103 bigmax = max128;
104 bigmax *= max128;
105 EXPECT_EQ(fmt::to_string(bigmax),
106 "fffffffffffffffffffffffffffffffe00000000000000000000000000000001");
107 }
108
109 TEST(bigint_test, square) {
110 bigint n0(0);
111 n0.square();
112 EXPECT_EQ(fmt::to_string(n0), "0");
113 bigint n1(0x100);
114 n1.square();
115 EXPECT_EQ(fmt::to_string(n1), "10000");
116 bigint n2(0xfffffffff);
117 n2.square();
118 EXPECT_EQ(fmt::to_string(n2), "ffffffffe000000001");
119 bigint n3(max_value<uint64_t>());
120 n3.square();
121 EXPECT_EQ(fmt::to_string(n3), "fffffffffffffffe0000000000000001");
122 bigint n4;
123 n4.assign_pow10(10);
124 EXPECT_EQ(fmt::to_string(n4), "2540be400");
125 }
126
127 TEST(bigint_test, divmod_assign_zero_divisor) {
128 bigint zero(0);
129 EXPECT_THROW(bigint(0).divmod_assign(zero), assertion_failure);
130 EXPECT_THROW(bigint(42).divmod_assign(zero), assertion_failure);
131 }
132
133 TEST(bigint_test, divmod_assign_self) {
134 bigint n(100);
135 EXPECT_THROW(n.divmod_assign(n), assertion_failure);
136 }
137
138 TEST(bigint_test, divmod_assign_unaligned) {
139 // (42 << 340) / pow(10, 100):
140 bigint n1(42);
141 n1 <<= 340;
142 bigint n2;
143 n2.assign_pow10(100);
144 int result = n1.divmod_assign(n2);
145 EXPECT_EQ(result, 9406);
146 EXPECT_EQ(fmt::to_string(n1),
147 "10f8353019583bfc29ffc8f564e1b9f9d819dbb4cf783e4507eca1539220p96");
148 }
149
150 TEST(bigint_test, divmod_assign) {
151 // 100 / 10:
152 bigint n1(100);
153 int result = n1.divmod_assign(bigint(10));
154 EXPECT_EQ(result, 10);
155 EXPECT_EQ(fmt::to_string(n1), "0");
156 // pow(10, 100) / (42 << 320):
157 n1.assign_pow10(100);
158 result = n1.divmod_assign(bigint(42) <<= 320);
159 EXPECT_EQ(result, 111);
160 EXPECT_EQ(fmt::to_string(n1),
161 "13ad2594c37ceb0b2784c4ce0bf38ace408e211a7caab24308a82e8f10p96");
162 // 42 / 100:
163 bigint n2(42);
164 n1.assign_pow10(2);
165 result = n2.divmod_assign(n1);
166 EXPECT_EQ(result, 0);
167 EXPECT_EQ(fmt::to_string(n2), "2a");
168 }
169
170 template <bool is_iec559> void run_double_tests() {
171 fmt::print("warning: double is not IEC559, skipping FP tests\n");
172 }
173
174 template <> void run_double_tests<true>() {
175 // Construct from double.
176 EXPECT_EQ(fp(1.23), fp(0x13ae147ae147aeu, -52));
177 }
178
179 TEST(fp_test, double_tests) {
180 run_double_tests<std::numeric_limits<double>::is_iec559>();
181 }
182
183 TEST(fp_test, normalize) {
184 const auto v = fp(0xbeef, 42);
185 auto normalized = normalize(v);
186 EXPECT_EQ(normalized.f, 0xbeef000000000000);
187 EXPECT_EQ(normalized.e, -6);
188 }
189
190 TEST(fp_test, multiply) {
191 auto v = fp(123ULL << 32, 4) * fp(56ULL << 32, 7);
192 EXPECT_EQ(v.f, 123u * 56u);
193 EXPECT_EQ(v.e, 4 + 7 + 64);
194 v = fp(123ULL << 32, 4) * fp(567ULL << 31, 8);
195 EXPECT_EQ(v.f, (123 * 567 + 1u) / 2);
196 EXPECT_EQ(v.e, 4 + 8 + 64);
197 }
198
199 TEST(fp_test, dragonbox_max_k) {
200 using fmt::detail::dragonbox::floor_log10_pow2;
201 using float_info = fmt::detail::dragonbox::float_info<float>;
202 EXPECT_EQ(
203 fmt::detail::const_check(float_info::max_k),
204 float_info::kappa -
205 floor_log10_pow2(std::numeric_limits<float>::min_exponent -
206 fmt::detail::num_significand_bits<float>() - 1));
207 using double_info = fmt::detail::dragonbox::float_info<double>;
208 EXPECT_EQ(fmt::detail::const_check(double_info::max_k),
209 double_info::kappa -
210 floor_log10_pow2(
211 std::numeric_limits<double>::min_exponent -
212 2 * fmt::detail::num_significand_bits<double>() - 1));
213 }
214
215 TEST(format_impl_test, format_error_code) {
216 std::string msg = "error 42", sep = ": ";
217 {
218 auto buffer = fmt::memory_buffer();
219 fmt::format_to(fmt::appender(buffer), "garbage");
220 fmt::detail::format_error_code(buffer, 42, "test");
221 EXPECT_EQ(to_string(buffer), "test: " + msg);
222 }
223 {
224 auto buffer = fmt::memory_buffer();
225 auto prefix =
226 std::string(fmt::inline_buffer_size - msg.size() - sep.size() + 1, 'x');
227 fmt::detail::format_error_code(buffer, 42, prefix);
228 EXPECT_EQ(msg, to_string(buffer));
229 }
230 int codes[] = {42, -1};
231 for (size_t i = 0, n = sizeof(codes) / sizeof(*codes); i < n; ++i) {
232 // Test maximum buffer size.
233 msg = fmt::format("error {}", codes[i]);
234 fmt::memory_buffer buffer;
235 auto prefix =
236 std::string(fmt::inline_buffer_size - msg.size() - sep.size(), 'x');
237 fmt::detail::format_error_code(buffer, codes[i], prefix);
238 EXPECT_EQ(prefix + sep + msg, to_string(buffer));
239 size_t size = fmt::inline_buffer_size;
240 EXPECT_EQ(size, buffer.size());
241 buffer.resize(0);
242 // Test with a message that doesn't fit into the buffer.
243 prefix += 'x';
244 fmt::detail::format_error_code(buffer, codes[i], prefix);
245 EXPECT_EQ(to_string(buffer), msg);
246 }
247 }
248
249 // Tests fmt::detail::count_digits for integer type Int.
250 template <typename Int> void test_count_digits() {
251 for (Int i = 0; i < 10; ++i) EXPECT_EQ(1u, fmt::detail::count_digits(i));
252 for (Int i = 1, n = 1, end = max_value<Int>() / 10; n <= end; ++i) {
253 n *= 10;
254 EXPECT_EQ(fmt::detail::count_digits(n - 1), i);
255 EXPECT_EQ(fmt::detail::count_digits(n), i + 1);
256 }
257 }
258
259 TEST(format_impl_test, count_digits) {
260 test_count_digits<uint32_t>();
261 test_count_digits<uint64_t>();
262 }
263
264 TEST(format_impl_test, countl_zero) {
265 constexpr auto num_bits = fmt::detail::num_bits<uint32_t>();
266 uint32_t n = 1u;
267 for (int i = 1; i < num_bits - 1; i++) {
268 n <<= 1;
269 EXPECT_EQ(fmt::detail::countl_zero(n - 1), num_bits - i);
270 EXPECT_EQ(fmt::detail::countl_zero(n), num_bits - i - 1);
271 }
272 }
273
274 #if FMT_USE_FLOAT128
275 TEST(format_impl_test, write_float128) {
276 auto s = std::string();
277 fmt::detail::write<char>(std::back_inserter(s), __float128(42));
278 EXPECT_EQ(s, "42");
279 }
280 #endif
281
282 struct double_double {
283 double a;
284 double b;
285
286 explicit constexpr double_double(double a_val = 0, double b_val = 0)
287 : a(a_val), b(b_val) {}
288
289 operator double() const { return a + b; }
290 auto operator-() const -> double_double { return double_double(-a, -b); }
291 };
292
293 auto format_as(double_double d) -> double { return d; }
294
295 bool operator>=(const double_double& lhs, const double_double& rhs) {
296 return lhs.a + lhs.b >= rhs.a + rhs.b;
297 }
298
299 struct slow_float {
300 float value;
301
302 explicit constexpr slow_float(float val = 0) : value(val) {}
303 operator float() const { return value; }
304 auto operator-() const -> slow_float { return slow_float(-value); }
305 };
306
307 auto format_as(slow_float f) -> float { return f; }
308
309 namespace std {
310 template <> struct is_floating_point<double_double> : std::true_type {};
311 template <> struct numeric_limits<double_double> {
312 // is_iec559 is true for double-double in libstdc++.
313 static constexpr bool is_iec559 = true;
314 static constexpr int digits = 106;
315 };
316
317 template <> struct is_floating_point<slow_float> : std::true_type {};
318 template <> struct numeric_limits<slow_float> : numeric_limits<float> {};
319 } // namespace std
320
321 FMT_BEGIN_NAMESPACE
322 namespace detail {
323 template <> struct is_fast_float<slow_float> : std::false_type {};
324 namespace dragonbox {
325 template <> struct float_info<slow_float> {
326 using carrier_uint = uint32_t;
327 static const int exponent_bits = 8;
328 };
329 } // namespace dragonbox
330 } // namespace detail
331 FMT_END_NAMESPACE
332
333 TEST(format_impl_test, write_double_double) {
334 auto s = std::string();
335 fmt::detail::write<char>(std::back_inserter(s), double_double(42), {});
336 // Specializing is_floating_point is broken in MSVC.
337 if (!FMT_MSC_VERSION) EXPECT_EQ(s, "42");
338 }
339
340 TEST(format_impl_test, write_dragon_even) {
341 auto s = std::string();
342 fmt::detail::write<char>(std::back_inserter(s), slow_float(33554450.0f), {});
343 // Specializing is_floating_point is broken in MSVC.
344 if (!FMT_MSC_VERSION) EXPECT_EQ(s, "33554450");
345 }
346
347 #if defined(_WIN32) && !defined(FMT_WINDOWS_NO_WCHAR)
348 # include <windows.h>
349
350 TEST(format_impl_test, write_console_signature) {
351 decltype(::WriteConsoleW)* p = fmt::detail::WriteConsoleW;
352 (void)p;
353 }
354 #endif
355
356 // A public domain branchless UTF-8 decoder by Christopher Wellons:
357 // https://github.com/skeeto/branchless-utf8
358 constexpr bool unicode_is_surrogate(uint32_t c) {
359 return c >= 0xD800U && c <= 0xDFFFU;
360 }
361
362 FMT_CONSTEXPR char* utf8_encode(char* s, uint32_t c) {
363 if (c >= (1UL << 16)) {
364 s[0] = static_cast<char>(0xf0 | (c >> 18));
365 s[1] = static_cast<char>(0x80 | ((c >> 12) & 0x3f));
366 s[2] = static_cast<char>(0x80 | ((c >> 6) & 0x3f));
367 s[3] = static_cast<char>(0x80 | ((c >> 0) & 0x3f));
368 return s + 4;
369 } else if (c >= (1UL << 11)) {
370 s[0] = static_cast<char>(0xe0 | (c >> 12));
371 s[1] = static_cast<char>(0x80 | ((c >> 6) & 0x3f));
372 s[2] = static_cast<char>(0x80 | ((c >> 0) & 0x3f));
373 return s + 3;
374 } else if (c >= (1UL << 7)) {
375 s[0] = static_cast<char>(0xc0 | (c >> 6));
376 s[1] = static_cast<char>(0x80 | ((c >> 0) & 0x3f));
377 return s + 2;
378 } else {
379 s[0] = static_cast<char>(c);
380 return s + 1;
381 }
382 }
383
384 // Make sure it can decode every character
385 TEST(format_impl_test, utf8_decode_decode_all) {
386 for (uint32_t i = 0; i < 0x10ffff; i++) {
387 if (!unicode_is_surrogate(i)) {
388 int e;
389 uint32_t c;
390 char buf[8] = {0};
391 char* end = utf8_encode(buf, i);
392 const char* res = fmt::detail::utf8_decode(buf, &c, &e);
393 EXPECT_EQ(end, res);
394 EXPECT_EQ(c, i);
395 EXPECT_EQ(e, 0);
396 }
397 }
398 }
399
400 // Reject everything outside of U+0000..U+10FFFF
401 TEST(format_impl_test, utf8_decode_out_of_range) {
402 for (uint32_t i = 0x110000; i < 0x1fffff; i++) {
403 int e;
404 uint32_t c;
405 char buf[8] = {0};
406 utf8_encode(buf, i);
407 const char* end = fmt::detail::utf8_decode(buf, &c, &e);
408 EXPECT_NE(e, 0);
409 EXPECT_EQ(end - buf, 4);
410 }
411 }
412
413 // Does it reject all surrogate halves?
414 TEST(format_impl_test, utf8_decode_surrogate_halves) {
415 for (uint32_t i = 0xd800; i <= 0xdfff; i++) {
416 int e;
417 uint32_t c;
418 char buf[8] = {0};
419 utf8_encode(buf, i);
420 fmt::detail::utf8_decode(buf, &c, &e);
421 EXPECT_NE(e, 0);
422 }
423 }
424
425 // How about non-canonical encodings?
426 TEST(format_impl_test, utf8_decode_non_canonical_encodings) {
427 int e;
428 uint32_t c;
429 const char* end;
430
431 char buf2[8] = {char(0xc0), char(0xA4)};
432 end = fmt::detail::utf8_decode(buf2, &c, &e);
433 EXPECT_NE(e, 0); // non-canonical len 2
434 EXPECT_EQ(end, buf2 + 2); // non-canonical recover 2
435
436 char buf3[8] = {char(0xe0), char(0x80), char(0xA4)};
437 end = fmt::detail::utf8_decode(buf3, &c, &e);
438 EXPECT_NE(e, 0); // non-canonical len 3
439 EXPECT_EQ(end, buf3 + 3); // non-canonical recover 3
440
441 char buf4[8] = {char(0xf0), char(0x80), char(0x80), char(0xA4)};
442 end = fmt::detail::utf8_decode(buf4, &c, &e);
443 EXPECT_NE(e, 0); // non-canonical encoding len 4
444 EXPECT_EQ(end, buf4 + 4); // non-canonical recover 4
445 }
446
447 // Let's try some bogus byte sequences
448 TEST(format_impl_test, utf8_decode_bogus_byte_sequences) {
449 int e;
450 uint32_t c;
451
452 // Invalid first byte
453 char buf0[4] = {char(0xff)};
454 auto len = fmt::detail::utf8_decode(buf0, &c, &e) - buf0;
455 EXPECT_NE(e, 0); // "bogus [ff] 0x%02x U+%04lx", e, (unsigned long)c);
456 EXPECT_EQ(len, 1); // "bogus [ff] recovery %d", len);
457
458 // Invalid first byte
459 char buf1[4] = {char(0x80)};
460 len = fmt::detail::utf8_decode(buf1, &c, &e) - buf1;
461 EXPECT_NE(e, 0); // "bogus [80] 0x%02x U+%04lx", e, (unsigned long)c);
462 EXPECT_EQ(len, 1); // "bogus [80] recovery %d", len);
463
464 // Looks like a two-byte sequence but second byte is wrong
465 char buf2[4] = {char(0xc0), char(0x0a)};
466 len = fmt::detail::utf8_decode(buf2, &c, &e) - buf2;
467 EXPECT_NE(e, 0); // "bogus [c0 0a] 0x%02x U+%04lx", e, (unsigned long)c
468 EXPECT_EQ(len, 2); // "bogus [c0 0a] recovery %d", len);
469 }
470
471 TEST(format_impl_test, to_utf8) {
472 auto s = std::string("ёжик");
473 auto u = fmt::detail::to_utf8<wchar_t>(L"\x0451\x0436\x0438\x043A");
474 EXPECT_EQ(s, u.str());
475 EXPECT_EQ(s.size(), u.size());
476 }