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
, dragonbox_max_k
) {
200 using fmt::detail::dragonbox::floor_log10_pow2
;
201 using float_info
= fmt::detail::dragonbox::float_info
<float>;
203 fmt::detail::const_check(float_info::max_k
),
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
),
211 std::numeric_limits
<double>::min_exponent
-
212 2 * fmt::detail::num_significand_bits
<double>() - 1));
215 TEST(format_impl_test
, format_error_code
) {
216 std::string msg
= "error 42", sep
= ": ";
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
);
224 auto buffer
= fmt::memory_buffer();
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
));
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
;
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());
242 // Test with a message that doesn't fit into the buffer.
244 fmt::detail::format_error_code(buffer
, codes
[i
], prefix
);
245 EXPECT_EQ(to_string(buffer
), msg
);
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
) {
254 EXPECT_EQ(fmt::detail::count_digits(n
- 1), i
);
255 EXPECT_EQ(fmt::detail::count_digits(n
), i
+ 1);
259 TEST(format_impl_test
, count_digits
) {
260 test_count_digits
<uint32_t>();
261 test_count_digits
<uint64_t>();
264 TEST(format_impl_test
, countl_zero
) {
265 constexpr auto num_bits
= fmt::detail::num_bits
<uint32_t>();
267 for (int i
= 1; i
< num_bits
- 1; i
++) {
269 EXPECT_EQ(fmt::detail::countl_zero(n
- 1), num_bits
- i
);
270 EXPECT_EQ(fmt::detail::countl_zero(n
), num_bits
- i
- 1);
275 TEST(format_impl_test
, write_float128
) {
276 auto s
= std::string();
277 fmt::detail::write
<char>(std::back_inserter(s
), __float128(42));
282 struct double_double
{
286 explicit constexpr double_double(double a_val
= 0, double b_val
= 0)
287 : a(a_val
), b(b_val
) {}
289 operator double() const { return a
+ b
; }
290 auto operator-() const -> double_double
{ return double_double(-a
, -b
); }
293 auto format_as(double_double d
) -> double { return d
; }
295 bool operator>=(const double_double
& lhs
, const double_double
& rhs
) {
296 return lhs
.a
+ lhs
.b
>= rhs
.a
+ rhs
.b
;
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
); }
307 auto format_as(slow_float f
) -> float { return f
; }
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;
317 template <> struct is_floating_point
<slow_float
> : std::true_type
{};
318 template <> struct numeric_limits
<slow_float
> : numeric_limits
<float> {};
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;
329 } // namespace dragonbox
330 } // namespace detail
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");
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");
347 #if defined(_WIN32) && !defined(FMT_USE_WRITE_CONSOLE)
348 # include <windows.h>
350 TEST(format_impl_test
, write_console_signature
) {
351 decltype(::WriteConsoleW
)* p
= fmt::detail::WriteConsoleW
;
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
;
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));
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));
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));
379 s
[0] = static_cast<char>(c
);
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
)) {
391 char* end
= utf8_encode(buf
, i
);
392 const char* res
= fmt::detail::utf8_decode(buf
, &c
, &e
);
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
++) {
407 const char* end
= fmt::detail::utf8_decode(buf
, &c
, &e
);
409 EXPECT_EQ(end
- buf
, 4);
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
++) {
420 fmt::detail::utf8_decode(buf
, &c
, &e
);
425 // How about non-canonical encodings?
426 TEST(format_impl_test
, utf8_decode_non_canonical_encodings
) {
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
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
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
447 // Let's try some bogus byte sequences
448 TEST(format_impl_test
, utf8_decode_bogus_byte_sequences
) {
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);
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);
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);
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());