]> git.proxmox.com Git - ceph.git/blob - ceph/src/fmt/test/format-impl-test.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / fmt / test / format-impl-test.cc
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, get_cached_power) {
200 using limits = std::numeric_limits<double>;
201 for (auto exp = limits::min_exponent; exp <= limits::max_exponent; ++exp) {
202 int dec_exp = 0;
203 auto power = fmt::detail::get_cached_power(exp, dec_exp);
204 bigint exact, cache(power.f);
205 if (dec_exp >= 0) {
206 exact.assign_pow10(dec_exp);
207 if (power.e <= 0)
208 exact <<= -power.e;
209 else
210 cache <<= power.e;
211 exact.align(cache);
212 cache.align(exact);
213 auto exact_str = fmt::to_string(exact);
214 auto cache_str = fmt::to_string(cache);
215 EXPECT_EQ(exact_str.size(), cache_str.size());
216 EXPECT_EQ(exact_str.substr(0, 15), cache_str.substr(0, 15));
217 int diff = cache_str[15] - exact_str[15];
218 if (diff == 1)
219 EXPECT_GT(exact_str[16], '8');
220 else
221 EXPECT_EQ(diff, 0);
222 } else {
223 cache.assign_pow10(-dec_exp);
224 cache *= power.f + 1; // Inexact check.
225 exact = 1;
226 exact <<= -power.e;
227 exact.align(cache);
228 auto exact_str = fmt::to_string(exact);
229 auto cache_str = fmt::to_string(cache);
230 EXPECT_EQ(exact_str.size(), cache_str.size());
231 EXPECT_EQ(exact_str.substr(0, 16), cache_str.substr(0, 16));
232 }
233 }
234 }
235
236 TEST(fp_test, dragonbox_max_k) {
237 using fmt::detail::dragonbox::floor_log10_pow2;
238 using float_info = fmt::detail::dragonbox::float_info<float>;
239 EXPECT_EQ(
240 fmt::detail::const_check(float_info::max_k),
241 float_info::kappa -
242 floor_log10_pow2(std::numeric_limits<float>::min_exponent -
243 fmt::detail::num_significand_bits<float>() - 1));
244 using double_info = fmt::detail::dragonbox::float_info<double>;
245 EXPECT_EQ(
246 fmt::detail::const_check(double_info::max_k),
247 double_info::kappa -
248 floor_log10_pow2(std::numeric_limits<double>::min_exponent -
249 fmt::detail::num_significand_bits<double>() - 1));
250 }
251
252 TEST(fp_test, get_round_direction) {
253 using fmt::detail::get_round_direction;
254 using fmt::detail::round_direction;
255 EXPECT_EQ(get_round_direction(100, 50, 0), round_direction::down);
256 EXPECT_EQ(get_round_direction(100, 51, 0), round_direction::up);
257 EXPECT_EQ(get_round_direction(100, 40, 10), round_direction::down);
258 EXPECT_EQ(get_round_direction(100, 60, 10), round_direction::up);
259 for (size_t i = 41; i < 60; ++i)
260 EXPECT_EQ(get_round_direction(100, i, 10), round_direction::unknown);
261 uint64_t max = max_value<uint64_t>();
262 EXPECT_THROW(get_round_direction(100, 100, 0), assertion_failure);
263 EXPECT_THROW(get_round_direction(100, 0, 100), assertion_failure);
264 EXPECT_THROW(get_round_direction(100, 0, 50), assertion_failure);
265 // Check that remainder + error doesn't overflow.
266 EXPECT_EQ(get_round_direction(max, max - 1, 2), round_direction::up);
267 // Check that 2 * (remainder + error) doesn't overflow.
268 EXPECT_EQ(get_round_direction(max, max / 2 + 1, max / 2),
269 round_direction::unknown);
270 // Check that remainder - error doesn't overflow.
271 EXPECT_EQ(get_round_direction(100, 40, 41), round_direction::unknown);
272 // Check that 2 * (remainder - error) doesn't overflow.
273 EXPECT_EQ(get_round_direction(max, max - 1, 1), round_direction::up);
274 }
275
276 TEST(fp_test, fixed_handler) {
277 struct handler : fmt::detail::gen_digits_handler {
278 char buffer[10];
279 handler(int prec = 0) : fmt::detail::gen_digits_handler() {
280 buf = buffer;
281 precision = prec;
282 }
283 };
284 handler().on_digit('0', 100, 99, 0, false);
285 EXPECT_THROW(handler().on_digit('0', 100, 100, 0, false), assertion_failure);
286 namespace digits = fmt::detail::digits;
287 EXPECT_EQ(handler(1).on_digit('0', 100, 10, 10, false), digits::error);
288 // Check that divisor - error doesn't overflow.
289 EXPECT_EQ(handler(1).on_digit('0', 100, 10, 101, false), digits::error);
290 // Check that 2 * error doesn't overflow.
291 uint64_t max = max_value<uint64_t>();
292 EXPECT_EQ(handler(1).on_digit('0', max, 10, max - 1, false), digits::error);
293 }
294
295 TEST(fp_test, grisu_format_compiles_with_on_ieee_double) {
296 auto buf = fmt::memory_buffer();
297 format_float(0.42, -1, fmt::detail::float_specs(), buf);
298 }
299
300 TEST(format_impl_test, format_error_code) {
301 std::string msg = "error 42", sep = ": ";
302 {
303 auto buffer = fmt::memory_buffer();
304 format_to(fmt::appender(buffer), "garbage");
305 fmt::detail::format_error_code(buffer, 42, "test");
306 EXPECT_EQ(to_string(buffer), "test: " + msg);
307 }
308 {
309 auto buffer = fmt::memory_buffer();
310 auto prefix =
311 std::string(fmt::inline_buffer_size - msg.size() - sep.size() + 1, 'x');
312 fmt::detail::format_error_code(buffer, 42, prefix);
313 EXPECT_EQ(msg, to_string(buffer));
314 }
315 int codes[] = {42, -1};
316 for (size_t i = 0, n = sizeof(codes) / sizeof(*codes); i < n; ++i) {
317 // Test maximum buffer size.
318 msg = fmt::format("error {}", codes[i]);
319 fmt::memory_buffer buffer;
320 auto prefix =
321 std::string(fmt::inline_buffer_size - msg.size() - sep.size(), 'x');
322 fmt::detail::format_error_code(buffer, codes[i], prefix);
323 EXPECT_EQ(prefix + sep + msg, to_string(buffer));
324 size_t size = fmt::inline_buffer_size;
325 EXPECT_EQ(size, buffer.size());
326 buffer.resize(0);
327 // Test with a message that doesn't fit into the buffer.
328 prefix += 'x';
329 fmt::detail::format_error_code(buffer, codes[i], prefix);
330 EXPECT_EQ(to_string(buffer), msg);
331 }
332 }
333
334 TEST(format_impl_test, compute_width) {
335 EXPECT_EQ(4,
336 fmt::detail::compute_width(
337 fmt::basic_string_view<fmt::detail::char8_type>(
338 reinterpret_cast<const fmt::detail::char8_type*>("ёжик"))));
339 }
340
341 // Tests fmt::detail::count_digits for integer type Int.
342 template <typename Int> void test_count_digits() {
343 for (Int i = 0; i < 10; ++i) EXPECT_EQ(1u, fmt::detail::count_digits(i));
344 for (Int i = 1, n = 1, end = max_value<Int>() / 10; n <= end; ++i) {
345 n *= 10;
346 EXPECT_EQ(fmt::detail::count_digits(n - 1), i);
347 EXPECT_EQ(fmt::detail::count_digits(n), i + 1);
348 }
349 }
350
351 TEST(format_impl_test, count_digits) {
352 test_count_digits<uint32_t>();
353 test_count_digits<uint64_t>();
354 }
355
356 #if FMT_USE_FLOAT128
357 TEST(format_impl_test, write_float128) {
358 auto s = std::string();
359 fmt::detail::write<char>(std::back_inserter(s), __float128(42));
360 EXPECT_EQ(s, "42");
361 }
362 #endif
363
364 struct double_double {
365 double a;
366 double b;
367
368 explicit constexpr double_double(double a_val = 0, double b_val = 0)
369 : a(a_val), b(b_val) {}
370
371 operator double() const { return a + b; }
372 auto operator-() const -> double_double { return double_double(-a, -b); }
373 };
374
375 bool operator>=(const double_double& lhs, const double_double& rhs) {
376 return lhs.a + lhs.b >= rhs.a + rhs.b;
377 }
378
379 struct slow_float {
380 float value;
381
382 explicit constexpr slow_float(float val = 0) : value(val) {}
383 operator float() const { return value; }
384 auto operator-() const -> slow_float { return slow_float(-value); }
385 };
386
387 namespace std {
388 template <> struct is_floating_point<double_double> : std::true_type {};
389 template <> struct numeric_limits<double_double> {
390 // is_iec559 is true for double-double in libstdc++.
391 static constexpr bool is_iec559 = true;
392 static constexpr int digits = 106;
393 };
394
395 template <> struct is_floating_point<slow_float> : std::true_type {};
396 template <> struct numeric_limits<slow_float> : numeric_limits<float> {};
397 } // namespace std
398
399 FMT_BEGIN_NAMESPACE
400 namespace detail {
401 template <> struct is_fast_float<slow_float> : std::false_type {};
402 namespace dragonbox {
403 template <> struct float_info<slow_float> {
404 using carrier_uint = uint32_t;
405 static const int exponent_bits = 8;
406 };
407 } // namespace dragonbox
408 } // namespace detail
409 FMT_END_NAMESPACE
410
411 TEST(format_impl_test, write_double_double) {
412 auto s = std::string();
413 fmt::detail::write<char>(std::back_inserter(s), double_double(42), {});
414 // Specializing is_floating_point is broken in MSVC.
415 if (!FMT_MSC_VERSION) EXPECT_EQ(s, "42");
416 }
417
418 TEST(format_impl_test, write_dragon_even) {
419 auto s = std::string();
420 fmt::detail::write<char>(std::back_inserter(s), slow_float(33554450.0f), {});
421 // Specializing is_floating_point is broken in MSVC.
422 if (!FMT_MSC_VERSION) EXPECT_EQ(s, "33554450");
423 }
424
425 #ifdef _WIN32
426 # include <windows.h>
427
428 TEST(format_impl_test, write_console_signature) {
429 decltype(::WriteConsoleW)* p = fmt::detail::WriteConsoleW;
430 (void)p;
431 }
432 #endif
433
434 // A public domain branchless UTF-8 decoder by Christopher Wellons:
435 // https://github.com/skeeto/branchless-utf8
436 constexpr bool unicode_is_surrogate(uint32_t c) {
437 return c >= 0xD800U && c <= 0xDFFFU;
438 }
439
440 FMT_CONSTEXPR char* utf8_encode(char* s, uint32_t c) {
441 if (c >= (1UL << 16)) {
442 s[0] = static_cast<char>(0xf0 | (c >> 18));
443 s[1] = static_cast<char>(0x80 | ((c >> 12) & 0x3f));
444 s[2] = static_cast<char>(0x80 | ((c >> 6) & 0x3f));
445 s[3] = static_cast<char>(0x80 | ((c >> 0) & 0x3f));
446 return s + 4;
447 } else if (c >= (1UL << 11)) {
448 s[0] = static_cast<char>(0xe0 | (c >> 12));
449 s[1] = static_cast<char>(0x80 | ((c >> 6) & 0x3f));
450 s[2] = static_cast<char>(0x80 | ((c >> 0) & 0x3f));
451 return s + 3;
452 } else if (c >= (1UL << 7)) {
453 s[0] = static_cast<char>(0xc0 | (c >> 6));
454 s[1] = static_cast<char>(0x80 | ((c >> 0) & 0x3f));
455 return s + 2;
456 } else {
457 s[0] = static_cast<char>(c);
458 return s + 1;
459 }
460 }
461
462 // Make sure it can decode every character
463 TEST(format_impl_test, utf8_decode_decode_all) {
464 for (uint32_t i = 0; i < 0x10ffff; i++) {
465 if (!unicode_is_surrogate(i)) {
466 int e;
467 uint32_t c;
468 char buf[8] = {0};
469 char* end = utf8_encode(buf, i);
470 const char* res = fmt::detail::utf8_decode(buf, &c, &e);
471 EXPECT_EQ(end, res);
472 EXPECT_EQ(c, i);
473 EXPECT_EQ(e, 0);
474 }
475 }
476 }
477
478 // Reject everything outside of U+0000..U+10FFFF
479 TEST(format_impl_test, utf8_decode_out_of_range) {
480 for (uint32_t i = 0x110000; i < 0x1fffff; i++) {
481 int e;
482 uint32_t c;
483 char buf[8] = {0};
484 utf8_encode(buf, i);
485 const char* end = fmt::detail::utf8_decode(buf, &c, &e);
486 EXPECT_NE(e, 0);
487 EXPECT_EQ(end - buf, 4);
488 }
489 }
490
491 // Does it reject all surrogate halves?
492 TEST(format_impl_test, utf8_decode_surrogate_halves) {
493 for (uint32_t i = 0xd800; i <= 0xdfff; i++) {
494 int e;
495 uint32_t c;
496 char buf[8] = {0};
497 utf8_encode(buf, i);
498 fmt::detail::utf8_decode(buf, &c, &e);
499 EXPECT_NE(e, 0);
500 }
501 }
502
503 // How about non-canonical encodings?
504 TEST(format_impl_test, utf8_decode_non_canonical_encodings) {
505 int e;
506 uint32_t c;
507 const char* end;
508
509 char buf2[8] = {char(0xc0), char(0xA4)};
510 end = fmt::detail::utf8_decode(buf2, &c, &e);
511 EXPECT_NE(e, 0); // non-canonical len 2
512 EXPECT_EQ(end, buf2 + 2); // non-canonical recover 2
513
514 char buf3[8] = {char(0xe0), char(0x80), char(0xA4)};
515 end = fmt::detail::utf8_decode(buf3, &c, &e);
516 EXPECT_NE(e, 0); // non-canonical len 3
517 EXPECT_EQ(end, buf3 + 3); // non-canonical recover 3
518
519 char buf4[8] = {char(0xf0), char(0x80), char(0x80), char(0xA4)};
520 end = fmt::detail::utf8_decode(buf4, &c, &e);
521 EXPECT_NE(e, 0); // non-canonical encoding len 4
522 EXPECT_EQ(end, buf4 + 4); // non-canonical recover 4
523 }
524
525 // Let's try some bogus byte sequences
526 TEST(format_impl_test, utf8_decode_bogus_byte_sequences) {
527 int e;
528 uint32_t c;
529
530 // Invalid first byte
531 char buf0[4] = {char(0xff)};
532 auto len = fmt::detail::utf8_decode(buf0, &c, &e) - buf0;
533 EXPECT_NE(e, 0); // "bogus [ff] 0x%02x U+%04lx", e, (unsigned long)c);
534 EXPECT_EQ(len, 1); // "bogus [ff] recovery %d", len);
535
536 // Invalid first byte
537 char buf1[4] = {char(0x80)};
538 len = fmt::detail::utf8_decode(buf1, &c, &e) - buf1;
539 EXPECT_NE(e, 0); // "bogus [80] 0x%02x U+%04lx", e, (unsigned long)c);
540 EXPECT_EQ(len, 1); // "bogus [80] recovery %d", len);
541
542 // Looks like a two-byte sequence but second byte is wrong
543 char buf2[4] = {char(0xc0), char(0x0a)};
544 len = fmt::detail::utf8_decode(buf2, &c, &e) - buf2;
545 EXPECT_NE(e, 0); // "bogus [c0 0a] 0x%02x U+%04lx", e, (unsigned long)c
546 EXPECT_EQ(len, 2); // "bogus [c0 0a] recovery %d", len);
547 }