1 // Formatting library for C++ - formatting library implementation tests
3 // Copyright (c) 2012 - present, Victor Zverovich
4 // All rights reserved.
6 // For the license information refer to format.h.
12 #include "test-assert.h"
15 #include "fmt/format.h"
16 #include "gmock/gmock.h"
19 using fmt::detail::bigint
;
20 using fmt::detail::fp
;
21 using fmt::detail::max_value
;
23 static_assert(!std::is_copy_constructible
<bigint
>::value
, "");
24 static_assert(!std::is_copy_assignable
<bigint
>::value
, "");
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");
32 TEST(bigint_test
, compare
) {
35 EXPECT_EQ(compare(n1
, n2
), 0);
37 EXPECT_LT(compare(n1
, n2
), 0);
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);
46 TEST(bigint_test
, add_compare
) {
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),
58 EXPECT_GT(add_compare(bigint(0x1ffffffff), bigint(0x100000002),
61 EXPECT_EQ(add_compare(bigint(0x1ffffffff), bigint(0x100000002),
64 EXPECT_LT(add_compare(bigint(0x1ffffffff), bigint(0x100000002),
67 EXPECT_LT(add_compare(bigint(0x1ffffffff), bigint(0x100000002),
72 TEST(bigint_test
, shift_left
) {
75 EXPECT_EQ(fmt::to_string(n
), "42");
77 EXPECT_EQ(fmt::to_string(n
), "84");
79 EXPECT_EQ(fmt::to_string(n
), "108000000");
82 TEST(bigint_test
, multiply
) {
84 EXPECT_THROW(n
*= 0, assertion_failure
);
86 EXPECT_EQ(fmt::to_string(n
), "42");
89 EXPECT_EQ(fmt::to_string(n
), "84");
91 EXPECT_EQ(fmt::to_string(n
), "962fc95e0");
93 bigint
bigmax(max_value
<uint32_t>());
94 bigmax
*= max_value
<uint32_t>();
95 EXPECT_EQ(fmt::to_string(bigmax
), "fffffffe00000001");
97 const auto max64
= max_value
<uint64_t>();
100 EXPECT_EQ(fmt::to_string(bigmax
), "fffffffffffffffe0000000000000001");
102 const auto max128
= (fmt::detail::uint128_t(max64
) << 64) | max64
;
105 EXPECT_EQ(fmt::to_string(bigmax
),
106 "fffffffffffffffffffffffffffffffe00000000000000000000000000000001");
109 TEST(bigint_test
, square
) {
112 EXPECT_EQ(fmt::to_string(n0
), "0");
115 EXPECT_EQ(fmt::to_string(n1
), "10000");
116 bigint
n2(0xfffffffff);
118 EXPECT_EQ(fmt::to_string(n2
), "ffffffffe000000001");
119 bigint
n3(max_value
<uint64_t>());
121 EXPECT_EQ(fmt::to_string(n3
), "fffffffffffffffe0000000000000001");
124 EXPECT_EQ(fmt::to_string(n4
), "2540be400");
127 TEST(bigint_test
, divmod_assign_zero_divisor
) {
129 EXPECT_THROW(bigint(0).divmod_assign(zero
), assertion_failure
);
130 EXPECT_THROW(bigint(42).divmod_assign(zero
), assertion_failure
);
133 TEST(bigint_test
, divmod_assign_self
) {
135 EXPECT_THROW(n
.divmod_assign(n
), assertion_failure
);
138 TEST(bigint_test
, divmod_assign_unaligned
) {
139 // (42 << 340) / pow(10, 100):
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");
150 TEST(bigint_test
, divmod_assign
) {
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");
165 result
= n2
.divmod_assign(n1
);
166 EXPECT_EQ(result
, 0);
167 EXPECT_EQ(fmt::to_string(n2
), "2a");
170 template <bool is_iec559
> void run_double_tests() {
171 fmt::print("warning: double is not IEC559, skipping FP tests\n");
174 template <> void run_double_tests
<true>() {
175 // Construct from double.
176 EXPECT_EQ(fp(1.23), fp(0x13ae147ae147aeu
, -52));
179 TEST(fp_test
, double_tests
) {
180 run_double_tests
<std::numeric_limits
<double>::is_iec559
>();
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);
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);
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
) {
203 auto power
= fmt::detail::get_cached_power(exp
, dec_exp
);
204 bigint exact
, cache(power
.f
);
206 exact
.assign_pow10(dec_exp
);
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];
219 EXPECT_GT(exact_str
[16], '8');
223 cache
.assign_pow10(-dec_exp
);
224 cache
*= power
.f
+ 1; // Inexact check.
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));
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>;
240 fmt::detail::const_check(float_info::max_k
),
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>;
246 fmt::detail::const_check(double_info::max_k
),
248 floor_log10_pow2(std::numeric_limits
<double>::min_exponent
-
249 fmt::detail::num_significand_bits
<double>() - 1));
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
);
276 TEST(fp_test
, fixed_handler
) {
277 struct handler
: fmt::detail::gen_digits_handler
{
279 handler(int prec
= 0) : fmt::detail::gen_digits_handler() {
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
);
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
);
300 TEST(format_impl_test
, format_error_code
) {
301 std::string msg
= "error 42", sep
= ": ";
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
);
309 auto buffer
= fmt::memory_buffer();
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
));
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
;
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());
327 // Test with a message that doesn't fit into the buffer.
329 fmt::detail::format_error_code(buffer
, codes
[i
], prefix
);
330 EXPECT_EQ(to_string(buffer
), msg
);
334 TEST(format_impl_test
, compute_width
) {
336 fmt::detail::compute_width(
337 fmt::basic_string_view
<fmt::detail::char8_type
>(
338 reinterpret_cast<const fmt::detail::char8_type
*>("ёжик"))));
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
) {
346 EXPECT_EQ(fmt::detail::count_digits(n
- 1), i
);
347 EXPECT_EQ(fmt::detail::count_digits(n
), i
+ 1);
351 TEST(format_impl_test
, count_digits
) {
352 test_count_digits
<uint32_t>();
353 test_count_digits
<uint64_t>();
357 TEST(format_impl_test
, write_float128
) {
358 auto s
= std::string();
359 fmt::detail::write
<char>(std::back_inserter(s
), __float128(42));
364 struct double_double
{
368 explicit constexpr double_double(double a_val
= 0, double b_val
= 0)
369 : a(a_val
), b(b_val
) {}
371 operator double() const { return a
+ b
; }
372 auto operator-() const -> double_double
{ return double_double(-a
, -b
); }
375 bool operator>=(const double_double
& lhs
, const double_double
& rhs
) {
376 return lhs
.a
+ lhs
.b
>= rhs
.a
+ rhs
.b
;
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
); }
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;
395 template <> struct is_floating_point
<slow_float
> : std::true_type
{};
396 template <> struct numeric_limits
<slow_float
> : numeric_limits
<float> {};
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;
407 } // namespace dragonbox
408 } // namespace detail
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");
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");
426 # include <windows.h>
428 TEST(format_impl_test
, write_console_signature
) {
429 decltype(::WriteConsoleW
)* p
= fmt::detail::WriteConsoleW
;
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
;
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));
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));
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));
457 s
[0] = static_cast<char>(c
);
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
)) {
469 char* end
= utf8_encode(buf
, i
);
470 const char* res
= fmt::detail::utf8_decode(buf
, &c
, &e
);
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
++) {
485 const char* end
= fmt::detail::utf8_decode(buf
, &c
, &e
);
487 EXPECT_EQ(end
- buf
, 4);
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
++) {
498 fmt::detail::utf8_decode(buf
, &c
, &e
);
503 // How about non-canonical encodings?
504 TEST(format_impl_test
, utf8_decode_non_canonical_encodings
) {
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
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
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
525 // Let's try some bogus byte sequences
526 TEST(format_impl_test
, utf8_decode_bogus_byte_sequences
) {
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);
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);
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);