1 // Formatting library for C++ - formatting library tests
3 // Copyright (c) 2012 - present, Victor Zverovich
4 // All rights reserved.
6 // For the license information refer to format.h.
8 // Check if fmt/format.h compiles with windows.h included before it.
13 #include "fmt/format.h"
16 #include <stdint.h> // uint32_t
18 #include <cfenv> // fegetexceptflag and FE_ALL_EXCEPT
19 #include <climits> // INT_MAX
20 #include <cmath> // std::signbit
21 #include <condition_variable> // std::condition_variable
22 #include <cstring> // std::strlen
23 #include <iterator> // std::back_inserter
24 #include <list> // std::list
25 #include <mutex> // std::mutex
26 #include <string> // std::string
27 #include <thread> // std::thread
28 #include <type_traits> // std::is_default_constructible
30 #include "gtest-extra.h"
31 #include "mock-allocator.h"
34 using fmt::basic_memory_buffer
;
35 using fmt::format_error
;
36 using fmt::memory_buffer
;
38 using fmt::string_view
;
39 using fmt::detail::max_value
;
40 using fmt::detail::uint128_fallback
;
42 using testing::Return
;
43 using testing::StrictMock
;
45 enum { buffer_size
= 256 };
47 TEST(uint128_test
, ctor
) {
48 auto n
= uint128_fallback();
50 n
= uint128_fallback(42);
52 EXPECT_EQ(static_cast<uint64_t>(n
), 42);
55 TEST(uint128_test
, shift
) {
56 auto n
= uint128_fallback(42);
58 EXPECT_EQ(static_cast<uint64_t>(n
), 0);
60 EXPECT_EQ(static_cast<uint64_t>(n
), 42);
62 EXPECT_EQ(static_cast<uint64_t>(n
>> 64), 0xa);
63 EXPECT_EQ(static_cast<uint64_t>(n
), 0x8000000000000000);
65 EXPECT_EQ(static_cast<uint64_t>(n
), 42);
66 EXPECT_EQ(uint128_fallback(1) << 112, uint128_fallback(0x1000000000000, 0));
67 EXPECT_EQ(uint128_fallback(0x1000000000000, 0) >> 112, uint128_fallback(1));
70 TEST(uint128_test
, minus
) {
71 auto n
= uint128_fallback(42);
75 TEST(uint128_test
, plus_assign
) {
76 auto n
= uint128_fallback(32);
77 n
+= uint128_fallback(10);
79 n
= uint128_fallback(max_value
<uint64_t>());
80 n
+= uint128_fallback(1);
81 EXPECT_EQ(n
, uint128_fallback(1) << 64);
84 TEST(uint128_test
, multiply
) {
85 auto n
= uint128_fallback(2251799813685247);
87 EXPECT_EQ(static_cast<uint64_t>(n
>> 64), 440901);
90 template <typename Float
> void check_isfinite() {
91 using fmt::detail::isfinite
;
92 EXPECT_TRUE(isfinite(Float(0.0)));
93 EXPECT_TRUE(isfinite(Float(42.0)));
94 EXPECT_TRUE(isfinite(Float(-42.0)));
95 EXPECT_TRUE(isfinite(Float(fmt::detail::max_value
<double>())));
96 // Use double because std::numeric_limits is broken for __float128.
97 using limits
= std::numeric_limits
<double>;
98 FMT_CONSTEXPR20
auto result
= isfinite(Float(limits::infinity()));
100 EXPECT_FALSE(isfinite(Float(limits::infinity())));
101 EXPECT_FALSE(isfinite(Float(-limits::infinity())));
102 EXPECT_FALSE(isfinite(Float(limits::quiet_NaN())));
103 EXPECT_FALSE(isfinite(Float(-limits::quiet_NaN())));
106 TEST(float_test
, isfinite
) {
107 check_isfinite
<double>();
109 check_isfinite
<fmt::detail::float128
>();
113 void check_no_fp_exception() {
115 fegetexceptflag(&fe
, FE_ALL_EXCEPT
);
117 // No exception flags should have been set
118 EXPECT_TRUE(fe
== 0);
121 template <typename Float
> void check_isnan() {
122 using fmt::detail::isnan
;
123 EXPECT_FALSE(isnan(Float(0.0)));
124 EXPECT_FALSE(isnan(Float(42.0)));
125 EXPECT_FALSE(isnan(Float(-42.0)));
126 EXPECT_FALSE(isnan(Float(fmt::detail::max_value
<double>())));
127 // Use double because std::numeric_limits is broken for __float128.
128 using limits
= std::numeric_limits
<double>;
129 EXPECT_FALSE(isnan(Float(limits::infinity())));
130 EXPECT_FALSE(isnan(Float(-limits::infinity())));
131 EXPECT_TRUE(isnan(Float(limits::quiet_NaN())));
132 EXPECT_TRUE(isnan(Float(-limits::quiet_NaN())));
134 // Sanity check: make sure no error has occurred before we start
135 check_no_fp_exception();
137 // Check that no exception is raised for the non-NaN case
139 check_no_fp_exception();
141 // Check that no exception is raised for the NaN case
142 isnan(Float(limits::quiet_NaN()));
143 check_no_fp_exception();
146 TEST(float_test
, isnan
) {
147 check_isnan
<double>();
149 check_isnan
<fmt::detail::float128
>();
157 TEST(util_test
, bit_cast
) {
158 auto s
= fmt::detail::bit_cast
<uint32_pair
>(uint64_t{42});
159 EXPECT_EQ(fmt::detail::bit_cast
<uint64_t>(s
), 42ull);
160 s
= fmt::detail::bit_cast
<uint32_pair
>(~uint64_t{0});
161 EXPECT_EQ(fmt::detail::bit_cast
<uint64_t>(s
), ~0ull);
164 // Increment a number in a string.
165 void increment(char* s
) {
166 for (int i
= static_cast<int>(std::strlen(s
)) - 1; i
>= 0; --i
) {
175 TEST(util_test
, increment
) {
178 EXPECT_STREQ("124", s
);
181 EXPECT_STREQ("129", s
);
183 EXPECT_STREQ("130", s
);
186 EXPECT_STREQ("200", s
);
189 TEST(util_test
, parse_nonnegative_int
) {
190 auto s
= fmt::string_view("10000000000");
191 auto begin
= s
.begin(), end
= s
.end();
192 EXPECT_EQ(fmt::detail::parse_nonnegative_int(begin
, end
, -1), -1);
196 EXPECT_EQ(fmt::detail::parse_nonnegative_int(begin
, end
, -1), -1);
199 TEST(format_impl_test
, compute_width
) {
200 EXPECT_EQ(fmt::detail::compute_width("вожык"), 5);
203 TEST(util_test
, utf8_to_utf16
) {
204 auto u
= fmt::detail::utf8_to_utf16("лошадка");
205 EXPECT_EQ(L
"\x043B\x043E\x0448\x0430\x0434\x043A\x0430", u
.str());
206 EXPECT_EQ(7, u
.size());
207 // U+10437 { DESERET SMALL LETTER YEE }
208 EXPECT_EQ(L
"\xD801\xDC37", fmt::detail::utf8_to_utf16("𐐷").str());
209 EXPECT_THROW_MSG(fmt::detail::utf8_to_utf16("\xc3\x28"), std::runtime_error
,
211 EXPECT_THROW_MSG(fmt::detail::utf8_to_utf16(fmt::string_view("л", 1)),
212 std::runtime_error
, "invalid utf8");
213 EXPECT_EQ(L
"123456", fmt::detail::utf8_to_utf16("123456").str());
216 TEST(util_test
, utf8_to_utf16_empty_string
) {
217 auto s
= std::string();
218 auto u
= fmt::detail::utf8_to_utf16(s
.c_str());
219 EXPECT_EQ(L
"", u
.str());
220 EXPECT_EQ(s
.size(), u
.size());
223 TEST(util_test
, allocator_ref
) {
224 using test_allocator_ref
= allocator_ref
<mock_allocator
<int>>;
225 auto check_forwarding
= [](mock_allocator
<int>& alloc
,
226 test_allocator_ref
& ref
) {
228 // Check if value_type is properly defined.
229 allocator_ref
<mock_allocator
<int>>::value_type
* ptr
= &mem
;
231 EXPECT_CALL(alloc
, allocate(42)).WillOnce(Return(ptr
));
233 EXPECT_CALL(alloc
, deallocate(ptr
, 42));
234 ref
.deallocate(ptr
, 42);
237 StrictMock
<mock_allocator
<int>> alloc
;
238 auto ref
= test_allocator_ref(&alloc
);
239 // Check if allocator_ref forwards to the underlying allocator.
240 check_forwarding(alloc
, ref
);
241 test_allocator_ref
ref2(ref
);
242 check_forwarding(alloc
, ref2
);
243 test_allocator_ref ref3
;
244 EXPECT_EQ(nullptr, ref3
.get());
246 check_forwarding(alloc
, ref3
);
249 TEST(util_test
, format_system_error
) {
250 fmt::memory_buffer message
;
251 fmt::format_system_error(message
, EDOM
, "test");
252 auto ec
= std::error_code(EDOM
, std::generic_category());
253 EXPECT_EQ(to_string(message
), std::system_error(ec
, "test").what());
254 message
= fmt::memory_buffer();
256 // Check if std::allocator throws on allocating max size_t / 2 chars.
257 size_t max_size
= max_value
<size_t>() / 2;
258 bool throws_on_alloc
= false;
260 auto alloc
= std::allocator
<char>();
261 alloc
.deallocate(alloc
.allocate(max_size
), max_size
);
262 } catch (const std::bad_alloc
&) {
263 throws_on_alloc
= true;
265 if (!throws_on_alloc
) {
266 fmt::print(stderr
, "warning: std::allocator allocates {} chars\n",
272 TEST(util_test
, system_error
) {
273 auto test_error
= fmt::system_error(EDOM
, "test");
274 auto ec
= std::error_code(EDOM
, std::generic_category());
275 EXPECT_STREQ(test_error
.what(), std::system_error(ec
, "test").what());
276 EXPECT_EQ(test_error
.code(), ec
);
278 auto error
= std::system_error(std::error_code());
280 throw fmt::system_error(EDOM
, "test {}", "error");
281 } catch (const std::system_error
& e
) {
284 fmt::memory_buffer message
;
285 fmt::format_system_error(message
, EDOM
, "test error");
286 EXPECT_EQ(error
.what(), to_string(message
));
287 EXPECT_EQ(error
.code(), std::error_code(EDOM
, std::generic_category()));
290 TEST(util_test
, report_system_error
) {
291 fmt::memory_buffer out
;
292 fmt::format_system_error(out
, EDOM
, "test error");
294 EXPECT_WRITE(stderr
, fmt::report_system_error(EDOM
, "test error"),
298 TEST(memory_buffer_test
, ctor
) {
299 basic_memory_buffer
<char, 123> buffer
;
300 EXPECT_EQ(static_cast<size_t>(0), buffer
.size());
301 EXPECT_EQ(123u, buffer
.capacity());
304 using std_allocator
= allocator_ref
<std::allocator
<char>>;
306 TEST(memory_buffer_test
, move_ctor_inline_buffer
) {
307 auto check_move_buffer
=
308 [](const char* str
, basic_memory_buffer
<char, 5, std_allocator
>& buffer
) {
309 std::allocator
<char>* alloc
= buffer
.get_allocator().get();
310 basic_memory_buffer
<char, 5, std_allocator
> buffer2(std::move(buffer
));
311 // Move shouldn't destroy the inline content of the first buffer.
312 EXPECT_EQ(str
, std::string(&buffer
[0], buffer
.size()));
313 EXPECT_EQ(str
, std::string(&buffer2
[0], buffer2
.size()));
314 EXPECT_EQ(5u, buffer2
.capacity());
315 // Move should transfer allocator.
316 EXPECT_EQ(nullptr, buffer
.get_allocator().get());
317 EXPECT_EQ(alloc
, buffer2
.get_allocator().get());
320 auto alloc
= std::allocator
<char>();
321 basic_memory_buffer
<char, 5, std_allocator
> buffer((std_allocator(&alloc
)));
322 const char test
[] = "test";
323 buffer
.append(string_view(test
, 4));
324 check_move_buffer("test", buffer
);
325 // Adding one more character fills the inline buffer, but doesn't cause
326 // dynamic allocation.
327 buffer
.push_back('a');
328 check_move_buffer("testa", buffer
);
331 TEST(memory_buffer_test
, move_ctor_dynamic_buffer
) {
332 auto alloc
= std::allocator
<char>();
333 basic_memory_buffer
<char, 4, std_allocator
> buffer((std_allocator(&alloc
)));
334 const char test
[] = "test";
335 buffer
.append(test
, test
+ 4);
336 const char* inline_buffer_ptr
= &buffer
[0];
337 // Adding one more character causes the content to move from the inline to
338 // a dynamically allocated buffer.
339 buffer
.push_back('a');
340 basic_memory_buffer
<char, 4, std_allocator
> buffer2(std::move(buffer
));
341 // Move should rip the guts of the first buffer.
342 EXPECT_EQ(&buffer
[0], inline_buffer_ptr
);
343 EXPECT_EQ(buffer
.size(), 0);
344 EXPECT_EQ(std::string(&buffer2
[0], buffer2
.size()), "testa");
345 EXPECT_GT(buffer2
.capacity(), 4u);
348 void check_move_assign_buffer(const char* str
,
349 basic_memory_buffer
<char, 5>& buffer
) {
350 basic_memory_buffer
<char, 5> buffer2
;
351 buffer2
= std::move(buffer
);
352 // Move shouldn't destroy the inline content of the first buffer.
353 EXPECT_EQ(str
, std::string(&buffer
[0], buffer
.size()));
354 EXPECT_EQ(str
, std::string(&buffer2
[0], buffer2
.size()));
355 EXPECT_EQ(5u, buffer2
.capacity());
358 TEST(memory_buffer_test
, move_assignment
) {
359 basic_memory_buffer
<char, 5> buffer
;
360 const char test
[] = "test";
361 buffer
.append(test
, test
+ 4);
362 check_move_assign_buffer("test", buffer
);
363 // Adding one more character fills the inline buffer, but doesn't cause
364 // dynamic allocation.
365 buffer
.push_back('a');
366 check_move_assign_buffer("testa", buffer
);
367 const char* inline_buffer_ptr
= &buffer
[0];
368 // Adding one more character causes the content to move from the inline to
369 // a dynamically allocated buffer.
370 buffer
.push_back('b');
371 basic_memory_buffer
<char, 5> buffer2
;
372 buffer2
= std::move(buffer
);
373 // Move should rip the guts of the first buffer.
374 EXPECT_EQ(inline_buffer_ptr
, &buffer
[0]);
375 EXPECT_EQ("testab", std::string(&buffer2
[0], buffer2
.size()));
376 EXPECT_GT(buffer2
.capacity(), 5u);
379 TEST(memory_buffer_test
, grow
) {
380 using allocator
= allocator_ref
<mock_allocator
<int>>;
381 mock_allocator
<int> alloc
;
382 basic_memory_buffer
<int, 10, allocator
> buffer((allocator(&alloc
)));
384 using fmt::detail::to_unsigned
;
385 for (int i
= 0; i
< 7; ++i
) buffer
[to_unsigned(i
)] = i
* i
;
386 EXPECT_EQ(10u, buffer
.capacity());
389 EXPECT_CALL(alloc
, allocate(20)).WillOnce(Return(mem
));
390 buffer
.try_reserve(20);
391 EXPECT_EQ(20u, buffer
.capacity());
392 // Check if size elements have been copied
393 for (int i
= 0; i
< 7; ++i
) EXPECT_EQ(i
* i
, buffer
[to_unsigned(i
)]);
394 // and no more than that.
395 EXPECT_EQ(0xdead, buffer
[7]);
396 EXPECT_CALL(alloc
, deallocate(mem
, 20));
399 TEST(memory_buffer_test
, allocator
) {
400 using test_allocator
= allocator_ref
<mock_allocator
<char>>;
401 basic_memory_buffer
<char, 10, test_allocator
> buffer
;
402 EXPECT_EQ(nullptr, buffer
.get_allocator().get());
403 StrictMock
<mock_allocator
<char>> alloc
;
406 basic_memory_buffer
<char, 10, test_allocator
> buffer2(
407 (test_allocator(&alloc
)));
408 EXPECT_EQ(&alloc
, buffer2
.get_allocator().get());
409 size_t size
= 2 * fmt::inline_buffer_size
;
410 EXPECT_CALL(alloc
, allocate(size
)).WillOnce(Return(&mem
));
411 buffer2
.reserve(size
);
412 EXPECT_CALL(alloc
, deallocate(&mem
, size
));
416 TEST(memory_buffer_test
, exception_in_deallocate
) {
417 using test_allocator
= allocator_ref
<mock_allocator
<char>>;
418 StrictMock
<mock_allocator
<char>> alloc
;
419 basic_memory_buffer
<char, 10, test_allocator
> buffer(
420 (test_allocator(&alloc
)));
421 size_t size
= 2 * fmt::inline_buffer_size
;
422 auto mem
= std::vector
<char>(size
);
424 EXPECT_CALL(alloc
, allocate(size
)).WillOnce(Return(&mem
[0]));
426 std::fill(&buffer
[0], &buffer
[0] + size
, 'x');
428 auto mem2
= std::vector
<char>(2 * size
);
430 EXPECT_CALL(alloc
, allocate(2 * size
)).WillOnce(Return(&mem2
[0]));
431 auto e
= std::exception();
432 EXPECT_CALL(alloc
, deallocate(&mem
[0], size
)).WillOnce(testing::Throw(e
));
433 EXPECT_THROW(buffer
.reserve(2 * size
), std::exception
);
434 EXPECT_EQ(&mem2
[0], &buffer
[0]);
435 // Check that the data has been copied.
436 for (size_t i
= 0; i
< size
; ++i
) EXPECT_EQ('x', buffer
[i
]);
438 EXPECT_CALL(alloc
, deallocate(&mem2
[0], 2 * size
));
441 template <typename Allocator
, size_t MaxSize
>
442 class max_size_allocator
: public Allocator
{
444 using typename
Allocator::value_type
;
445 size_t max_size() const noexcept
{ return MaxSize
; }
446 value_type
* allocate(size_t n
) {
447 if (n
> max_size()) {
448 throw std::length_error("size > max_size");
450 return std::allocator_traits
<Allocator
>::allocate(
451 *static_cast<Allocator
*>(this), n
);
453 void deallocate(value_type
* p
, size_t n
) {
454 std::allocator_traits
<Allocator
>::deallocate(*static_cast<Allocator
*>(this),
459 TEST(memory_buffer_test
, max_size_allocator
) {
461 using test_allocator
= max_size_allocator
<std::allocator
<char>, 160>;
462 basic_memory_buffer
<char, 10, test_allocator
> buffer
;
464 // new_capacity = 128 + 128/2 = 192 > 160
465 buffer
.resize(160); // Shouldn't throw.
468 TEST(memory_buffer_test
, max_size_allocator_overflow
) {
469 using test_allocator
= max_size_allocator
<std::allocator
<char>, 160>;
470 basic_memory_buffer
<char, 10, test_allocator
> buffer
;
471 EXPECT_THROW(buffer
.resize(161), std::exception
);
474 TEST(format_test
, exception_from_lib
) {
475 EXPECT_THROW_MSG(fmt::report_error("test"), format_error
, "test");
478 TEST(format_test
, escape
) {
479 EXPECT_EQ(fmt::format("{{"), "{");
480 EXPECT_EQ(fmt::format("before {{"), "before {");
481 EXPECT_EQ(fmt::format("{{ after"), "{ after");
482 EXPECT_EQ(fmt::format("before {{ after"), "before { after");
484 EXPECT_EQ(fmt::format("}}"), "}");
485 EXPECT_EQ(fmt::format("before }}"), "before }");
486 EXPECT_EQ(fmt::format("}} after"), "} after");
487 EXPECT_EQ(fmt::format("before }} after"), "before } after");
489 EXPECT_EQ(fmt::format("{{}}"), "{}");
490 EXPECT_EQ(fmt::format("{{{0}}}", 42), "{42}");
493 TEST(format_test
, unmatched_braces
) {
494 EXPECT_THROW_MSG((void)fmt::format(runtime("{")), format_error
,
495 "invalid format string");
496 EXPECT_THROW_MSG((void)fmt::format(runtime("}")), format_error
,
497 "unmatched '}' in format string");
498 EXPECT_THROW_MSG((void)fmt::format(runtime("{0{}")), format_error
,
499 "invalid format string");
502 TEST(format_test
, no_args
) { EXPECT_EQ(fmt::format("test"), "test"); }
504 TEST(format_test
, args_in_different_positions
) {
505 EXPECT_EQ(fmt::format("{0}", 42), "42");
506 EXPECT_EQ(fmt::format("before {0}", 42), "before 42");
507 EXPECT_EQ(fmt::format("{0} after", 42), "42 after");
508 EXPECT_EQ(fmt::format("before {0} after", 42), "before 42 after");
509 EXPECT_EQ(fmt::format("{0} = {1}", "answer", 42), "answer = 42");
510 EXPECT_EQ(fmt::format("{1} is the {0}", "answer", 42), "42 is the answer");
511 EXPECT_EQ(fmt::format("{0}{1}{0}", "abra", "cad"), "abracadabra");
514 TEST(format_test
, arg_errors
) {
515 EXPECT_THROW_MSG((void)fmt::format(runtime("{")), format_error
,
516 "invalid format string");
517 EXPECT_THROW_MSG((void)fmt::format(runtime("{?}")), format_error
,
518 "invalid format string");
519 EXPECT_THROW_MSG((void)fmt::format(runtime("{0")), format_error
,
520 "invalid format string");
521 EXPECT_THROW_MSG((void)fmt::format(runtime("{0}")), format_error
,
522 "argument not found");
523 EXPECT_THROW_MSG((void)fmt::format(runtime("{00}"), 42), format_error
,
524 "invalid format string");
526 auto int_max
= std::to_string(INT_MAX
);
527 EXPECT_THROW_MSG((void)fmt::format(runtime("{" + int_max
)), format_error
,
528 "invalid format string");
529 EXPECT_THROW_MSG((void)fmt::format(runtime("{" + int_max
+ "}")),
530 format_error
, "argument not found");
532 auto int_maxer
= std::to_string(INT_MAX
+ 1u);
533 EXPECT_THROW_MSG((void)fmt::format(runtime("{" + int_maxer
)), format_error
,
534 "invalid format string");
535 EXPECT_THROW_MSG((void)fmt::format(runtime("{" + int_maxer
+ "}")),
536 format_error
, "argument not found");
539 template <int N
> struct test_format
{
540 template <typename
... T
>
541 static auto format(fmt::string_view fmt
, const T
&... args
) -> std::string
{
542 return test_format
<N
- 1>::format(fmt
, N
- 1, args
...);
546 template <> struct test_format
<0> {
547 template <typename
... T
>
548 static auto format(fmt::string_view fmt
, const T
&... args
) -> std::string
{
549 return fmt::format(runtime(fmt
), args
...);
553 TEST(format_test
, many_args
) {
554 EXPECT_EQ("19", test_format
<20>::format("{19}"));
555 EXPECT_THROW_MSG(test_format
<20>::format("{20}"), format_error
,
556 "argument not found");
557 EXPECT_THROW_MSG(test_format
<21>::format("{21}"), format_error
,
558 "argument not found");
559 using fmt::detail::max_packed_args
;
560 std::string format_str
= fmt::format("{{{}}}", max_packed_args
+ 1);
561 EXPECT_THROW_MSG(test_format
<max_packed_args
>::format(format_str
),
562 format_error
, "argument not found");
565 TEST(format_test
, named_arg
) {
566 EXPECT_EQ("1/a/A", fmt::format("{_1}/{a_}/{A_}", fmt::arg("a_", 'a'),
567 fmt::arg("A_", "A"), fmt::arg("_1", 1)));
568 EXPECT_EQ(fmt::format("{0:{width}}", -42, fmt::arg("width", 4)), " -42");
570 fmt::format("{0:.{precision}}", "str", fmt::arg("precision", 2)));
571 EXPECT_EQ(fmt::format("{} {two}", 1, fmt::arg("two", 2)), "1 2");
573 fmt::format("{c}", fmt::arg("a", 0), fmt::arg("b", 0),
574 fmt::arg("c", 42), fmt::arg("d", 0), fmt::arg("e", 0),
575 fmt::arg("f", 0), fmt::arg("g", 0), fmt::arg("h", 0),
576 fmt::arg("i", 0), fmt::arg("j", 0), fmt::arg("k", 0),
577 fmt::arg("l", 0), fmt::arg("m", 0), fmt::arg("n", 0),
578 fmt::arg("o", 0), fmt::arg("p", 0)));
579 EXPECT_THROW_MSG((void)fmt::format(runtime("{a}")), format_error
,
580 "argument not found");
581 EXPECT_THROW_MSG((void)fmt::format(runtime("{a}"), 42), format_error
,
582 "argument not found");
583 EXPECT_THROW_MSG((void)fmt::format(runtime("{a} {}"), fmt::arg("a", 2), 42),
585 "cannot switch from manual to automatic argument indexing");
588 TEST(format_test
, auto_arg_index
) {
589 EXPECT_EQ(fmt::format("{}{}{}", 'a', 'b', 'c'), "abc");
590 EXPECT_THROW_MSG((void)fmt::format(runtime("{0}{}"), 'a', 'b'), format_error
,
591 "cannot switch from manual to automatic argument indexing");
592 EXPECT_THROW_MSG((void)fmt::format(runtime("{}{0}"), 'a', 'b'), format_error
,
593 "cannot switch from automatic to manual argument indexing");
594 EXPECT_EQ(fmt::format("{:.{}}", 1.2345, 2), "1.2");
595 EXPECT_THROW_MSG((void)fmt::format(runtime("{0}:.{}"), 1.2345, 2),
597 "cannot switch from manual to automatic argument indexing");
598 EXPECT_THROW_MSG((void)fmt::format(runtime("{:.{0}}"), 1.2345, 2),
600 "cannot switch from automatic to manual argument indexing");
601 EXPECT_THROW_MSG((void)fmt::format(runtime("{}")), format_error
,
602 "argument not found");
605 TEST(format_test
, empty_specs
) { EXPECT_EQ(fmt::format("{0:}", 42), "42"); }
607 TEST(format_test
, left_align
) {
608 EXPECT_EQ(fmt::format("{0:<4}", 42), "42 ");
609 EXPECT_EQ(fmt::format("{0:<4o}", 042), "42 ");
610 EXPECT_EQ(fmt::format("{0:<4x}", 0x42), "42 ");
611 EXPECT_EQ(fmt::format("{0:<5}", -42), "-42 ");
612 EXPECT_EQ(fmt::format("{0:<5}", 42u), "42 ");
613 EXPECT_EQ(fmt::format("{0:<5}", -42l), "-42 ");
614 EXPECT_EQ(fmt::format("{0:<5}", 42ul), "42 ");
615 EXPECT_EQ(fmt::format("{0:<5}", -42ll), "-42 ");
616 EXPECT_EQ(fmt::format("{0:<5}", 42ull), "42 ");
617 EXPECT_EQ(fmt::format("{0:<5}", -42.0), "-42 ");
618 EXPECT_EQ(fmt::format("{0:<5}", -42.0l), "-42 ");
619 EXPECT_EQ(fmt::format("{0:<5}", 'c'), "c ");
620 EXPECT_EQ(fmt::format("{0:<5}", "abc"), "abc ");
621 EXPECT_EQ(fmt::format("{0:<8}", reinterpret_cast<void*>(0xface)), "0xface ");
624 TEST(format_test
, right_align
) {
625 EXPECT_EQ(fmt::format("{0:>4}", 42), " 42");
626 EXPECT_EQ(fmt::format("{0:>4o}", 042), " 42");
627 EXPECT_EQ(fmt::format("{0:>4x}", 0x42), " 42");
628 EXPECT_EQ(fmt::format("{0:>5}", -42), " -42");
629 EXPECT_EQ(fmt::format("{0:>5}", 42u), " 42");
630 EXPECT_EQ(fmt::format("{0:>5}", -42l), " -42");
631 EXPECT_EQ(fmt::format("{0:>5}", 42ul), " 42");
632 EXPECT_EQ(fmt::format("{0:>5}", -42ll), " -42");
633 EXPECT_EQ(fmt::format("{0:>5}", 42ull), " 42");
634 EXPECT_EQ(fmt::format("{0:>5}", -42.0), " -42");
635 EXPECT_EQ(fmt::format("{0:>5}", -42.0l), " -42");
636 EXPECT_EQ(fmt::format("{0:>5}", 'c'), " c");
637 EXPECT_EQ(fmt::format("{0:>5}", "abc"), " abc");
638 EXPECT_EQ(fmt::format("{0:>8}", reinterpret_cast<void*>(0xface)), " 0xface");
641 TEST(format_test
, center_align
) {
642 EXPECT_EQ(fmt::format("{0:^5}", 42), " 42 ");
643 EXPECT_EQ(fmt::format("{0:^5o}", 042), " 42 ");
644 EXPECT_EQ(fmt::format("{0:^5x}", 0x42), " 42 ");
645 EXPECT_EQ(fmt::format("{0:^5}", -42), " -42 ");
646 EXPECT_EQ(fmt::format("{0:^5}", 42u), " 42 ");
647 EXPECT_EQ(fmt::format("{0:^5}", -42l), " -42 ");
648 EXPECT_EQ(fmt::format("{0:^5}", 42ul), " 42 ");
649 EXPECT_EQ(fmt::format("{0:^5}", -42ll), " -42 ");
650 EXPECT_EQ(fmt::format("{0:^5}", 42ull), " 42 ");
651 EXPECT_EQ(fmt::format("{0:^5}", -42.0), " -42 ");
652 EXPECT_EQ(fmt::format("{0:^5}", -42.0l), " -42 ");
653 EXPECT_EQ(fmt::format("{0:^5}", 'c'), " c ");
654 EXPECT_EQ(fmt::format("{0:^6}", "abc"), " abc ");
655 EXPECT_EQ(fmt::format("{0:^8}", reinterpret_cast<void*>(0xface)), " 0xface ");
658 TEST(format_test
, fill
) {
659 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{<5}"), 'c'), format_error
,
660 "invalid fill character '{'");
661 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{<5}}"), 'c'), format_error
,
662 "invalid fill character '{'");
663 EXPECT_EQ(fmt::format("{0:*>4}", 42), "**42");
664 EXPECT_EQ(fmt::format("{0:*>5}", -42), "**-42");
665 EXPECT_EQ(fmt::format("{0:*>5}", 42u), "***42");
666 EXPECT_EQ(fmt::format("{0:*>5}", -42l), "**-42");
667 EXPECT_EQ(fmt::format("{0:*>5}", 42ul), "***42");
668 EXPECT_EQ(fmt::format("{0:*>5}", -42ll), "**-42");
669 EXPECT_EQ(fmt::format("{0:*>5}", 42ull), "***42");
670 EXPECT_EQ(fmt::format("{0:*>5}", -42.0), "**-42");
671 EXPECT_EQ(fmt::format("{0:*>5}", -42.0l), "**-42");
672 EXPECT_EQ(fmt::format("{0:*<5}", 'c'), "c****");
673 EXPECT_EQ(fmt::format("{0:*<5}", "abc"), "abc**");
674 EXPECT_EQ("**0xface",
675 fmt::format("{0:*>8}", reinterpret_cast<void*>(0xface)));
676 EXPECT_EQ(fmt::format("{:}=", "foo"), "foo=");
677 EXPECT_EQ(std::string("\0\0\0*", 4),
678 fmt::format(string_view("{:\0>4}", 6), '*'));
679 EXPECT_EQ(fmt::format("{0:ж>4}", 42), "жж42");
680 EXPECT_THROW_MSG((void)fmt::format(runtime("{:\x80\x80\x80\x80\x80>}"), 0),
681 format_error
, "invalid format specifier");
684 TEST(format_test
, plus_sign
) {
685 EXPECT_EQ(fmt::format("{0:+}", 42), "+42");
686 EXPECT_EQ(fmt::format("{0:+}", -42), "-42");
687 EXPECT_EQ(fmt::format("{0:+}", 42), "+42");
688 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:+}"), 42u), format_error
,
689 "invalid format specifier");
690 EXPECT_EQ(fmt::format("{0:+}", 42l), "+42");
691 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:+}"), 42ul), format_error
,
692 "invalid format specifier");
693 EXPECT_EQ(fmt::format("{0:+}", 42ll), "+42");
695 EXPECT_EQ(fmt::format("{0:+}", __int128_t(42)), "+42");
697 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:+}"), 42ull), format_error
,
698 "invalid format specifier");
699 EXPECT_EQ(fmt::format("{0:+}", 42.0), "+42");
700 EXPECT_EQ(fmt::format("{0:+}", 42.0l), "+42");
701 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:+}"), 'c'), format_error
,
702 "invalid format specifier");
703 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:+}"), "abc"), format_error
,
704 "invalid format specifier");
706 (void)fmt::format(runtime("{0:+}"), reinterpret_cast<void*>(0x42)),
707 format_error
, "invalid format specifier");
710 TEST(format_test
, minus_sign
) {
711 EXPECT_EQ(fmt::format("{0:-}", 42), "42");
712 EXPECT_EQ(fmt::format("{0:-}", -42), "-42");
713 EXPECT_EQ(fmt::format("{0:-}", 42), "42");
714 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:-}"), 42u), format_error
,
715 "invalid format specifier");
716 EXPECT_EQ(fmt::format("{0:-}", 42l), "42");
717 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:-}"), 42ul), format_error
,
718 "invalid format specifier");
719 EXPECT_EQ(fmt::format("{0:-}", 42ll), "42");
720 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:-}"), 42ull), format_error
,
721 "invalid format specifier");
722 EXPECT_EQ(fmt::format("{0:-}", 42.0), "42");
723 EXPECT_EQ(fmt::format("{0:-}", 42.0l), "42");
724 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:-}"), 'c'), format_error
,
725 "invalid format specifier");
726 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:-}"), "abc"), format_error
,
727 "invalid format specifier");
729 (void)fmt::format(runtime("{0:-}"), reinterpret_cast<void*>(0x42)),
730 format_error
, "invalid format specifier");
733 TEST(format_test
, space_sign
) {
734 EXPECT_EQ(fmt::format("{0: }", 42), " 42");
735 EXPECT_EQ(fmt::format("{0: }", -42), "-42");
736 EXPECT_EQ(fmt::format("{0: }", 42), " 42");
737 EXPECT_THROW_MSG((void)fmt::format(runtime("{0: }"), 42u), format_error
,
738 "invalid format specifier");
739 EXPECT_EQ(fmt::format("{0: }", 42l), " 42");
740 EXPECT_THROW_MSG((void)fmt::format(runtime("{0: }"), 42ul), format_error
,
741 "invalid format specifier");
742 EXPECT_EQ(fmt::format("{0: }", 42ll), " 42");
743 EXPECT_THROW_MSG((void)fmt::format(runtime("{0: }"), 42ull), format_error
,
744 "invalid format specifier");
745 EXPECT_EQ(fmt::format("{0: }", 42.0), " 42");
746 EXPECT_EQ(fmt::format("{0: }", 42.0l), " 42");
747 EXPECT_THROW_MSG((void)fmt::format(runtime("{0: }"), 'c'), format_error
,
748 "invalid format specifier");
749 EXPECT_THROW_MSG((void)fmt::format(runtime("{0: }"), "abc"), format_error
,
750 "invalid format specifier");
752 (void)fmt::format(runtime("{0: }"), reinterpret_cast<void*>(0x42)),
753 format_error
, "invalid format specifier");
756 TEST(format_test
, hash_flag
) {
757 EXPECT_EQ(fmt::format("{0:#}", 42), "42");
758 EXPECT_EQ(fmt::format("{0:#}", -42), "-42");
759 EXPECT_EQ(fmt::format("{0:#b}", 42), "0b101010");
760 EXPECT_EQ(fmt::format("{0:#B}", 42), "0B101010");
761 EXPECT_EQ(fmt::format("{0:#b}", -42), "-0b101010");
762 EXPECT_EQ(fmt::format("{0:#x}", 0x42), "0x42");
763 EXPECT_EQ(fmt::format("{0:#X}", 0x42), "0X42");
764 EXPECT_EQ(fmt::format("{0:#x}", -0x42), "-0x42");
765 EXPECT_EQ(fmt::format("{0:#o}", 0), "0");
766 EXPECT_EQ(fmt::format("{0:#o}", 042), "042");
767 EXPECT_EQ(fmt::format("{0:#o}", -042), "-042");
768 EXPECT_EQ(fmt::format("{0:#}", 42u), "42");
769 EXPECT_EQ(fmt::format("{0:#x}", 0x42u
), "0x42");
770 EXPECT_EQ(fmt::format("{0:#o}", 042u), "042");
772 EXPECT_EQ(fmt::format("{0:#}", -42l), "-42");
773 EXPECT_EQ(fmt::format("{0:#x}", 0x42l
), "0x42");
774 EXPECT_EQ(fmt::format("{0:#x}", -0x42l
), "-0x42");
775 EXPECT_EQ(fmt::format("{0:#o}", 042l), "042");
776 EXPECT_EQ(fmt::format("{0:#o}", -042l), "-042");
777 EXPECT_EQ(fmt::format("{0:#}", 42ul), "42");
778 EXPECT_EQ(fmt::format("{0:#x}", 0x42ul
), "0x42");
779 EXPECT_EQ(fmt::format("{0:#o}", 042ul), "042");
781 EXPECT_EQ(fmt::format("{0:#}", -42ll), "-42");
782 EXPECT_EQ(fmt::format("{0:#x}", 0x42ll
), "0x42");
783 EXPECT_EQ(fmt::format("{0:#x}", -0x42ll
), "-0x42");
784 EXPECT_EQ(fmt::format("{0:#o}", 042ll), "042");
785 EXPECT_EQ(fmt::format("{0:#o}", -042ll), "-042");
786 EXPECT_EQ(fmt::format("{0:#}", 42ull), "42");
787 EXPECT_EQ(fmt::format("{0:#x}", 0x42ull
), "0x42");
788 EXPECT_EQ(fmt::format("{0:#o}", 042ull), "042");
790 EXPECT_EQ(fmt::format("{0:#}", -42.0), "-42.");
791 EXPECT_EQ(fmt::format("{0:#}", -42.0l), "-42.");
792 EXPECT_EQ(fmt::format("{:#.0e}", 42.0), "4.e+01");
793 EXPECT_EQ(fmt::format("{:#.0f}", 0.01), "0.");
794 EXPECT_EQ(fmt::format("{:#.2g}", 0.5), "0.50");
795 EXPECT_EQ(fmt::format("{:#.0f}", 0.5), "0.");
796 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:#"), 'c'), format_error
,
797 "missing '}' in format string");
798 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:#}"), 'c'), format_error
,
799 "invalid format specifier for char");
800 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:#}"), "abc"), format_error
,
801 "invalid format specifier");
803 (void)fmt::format(runtime("{0:#}"), reinterpret_cast<void*>(0x42)),
804 format_error
, "invalid format specifier");
807 TEST(format_test
, zero_flag
) {
808 EXPECT_EQ(fmt::format("{0:0}", 42), "42");
809 EXPECT_EQ(fmt::format("{0:05}", -42), "-0042");
810 EXPECT_EQ(fmt::format("{0:05}", 42u), "00042");
811 EXPECT_EQ(fmt::format("{0:05}", -42l), "-0042");
812 EXPECT_EQ(fmt::format("{0:05}", 42ul), "00042");
813 EXPECT_EQ(fmt::format("{0:05}", -42ll), "-0042");
814 EXPECT_EQ(fmt::format("{0:05}", 42ull), "00042");
815 EXPECT_EQ(fmt::format("{0:07}", -42.0), "-000042");
816 EXPECT_EQ(fmt::format("{0:07}", -42.0l), "-000042");
817 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:0"), 'c'), format_error
,
818 "missing '}' in format string");
819 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:05}"), 'c'), format_error
,
820 "invalid format specifier for char");
821 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:05}"), "abc"), format_error
,
822 "format specifier requires numeric argument");
824 (void)fmt::format(runtime("{0:05}"), reinterpret_cast<void*>(0x42)),
825 format_error
, "format specifier requires numeric argument");
828 TEST(format_test
, zero_flag_and_align
) {
829 // If the 0 character and an align option both appear, the 0 character is
831 EXPECT_EQ(fmt::format("{:<05}", 42), "42 ");
832 EXPECT_EQ(fmt::format("{:<05}", -42), "-42 ");
833 EXPECT_EQ(fmt::format("{:^05}", 42), " 42 ");
834 EXPECT_EQ(fmt::format("{:^05}", -42), " -42 ");
835 EXPECT_EQ(fmt::format("{:>05}", 42), " 42");
836 EXPECT_EQ(fmt::format("{:>05}", -42), " -42");
839 TEST(format_test
, width
) {
840 auto int_maxer
= std::to_string(INT_MAX
+ 1u);
841 EXPECT_THROW_MSG((void)fmt::format(runtime("{:" + int_maxer
), 0),
842 format_error
, "number is too big");
843 EXPECT_THROW_MSG((void)fmt::format(runtime("{:" + int_maxer
+ "}"), 0),
844 format_error
, "number is too big");
846 EXPECT_EQ(fmt::format("{:4}", -42), " -42");
847 EXPECT_EQ(fmt::format("{:5}", 42u), " 42");
848 EXPECT_EQ(fmt::format("{:6}", -42l), " -42");
849 EXPECT_EQ(fmt::format("{:7}", 42ul), " 42");
850 EXPECT_EQ(fmt::format("{:6}", -42ll), " -42");
851 EXPECT_EQ(fmt::format("{:7}", 42ull), " 42");
852 EXPECT_EQ(fmt::format("{:8}", -1.23), " -1.23");
853 EXPECT_EQ(fmt::format("{:9}", -1.23l), " -1.23");
854 EXPECT_EQ(fmt::format("{:10}", reinterpret_cast<void*>(0xcafe)),
856 EXPECT_EQ(fmt::format("{:11}", 'x'), "x ");
857 EXPECT_EQ(fmt::format("{:12}", "str"), "str ");
858 EXPECT_EQ(fmt::format("{:*^6}", "🤡"), "**🤡**");
859 EXPECT_EQ(fmt::format("{:*^8}", "你好"), "**你好**");
860 EXPECT_EQ(fmt::format("{:#6}", 42.0), " 42.");
861 EXPECT_EQ(fmt::format("{:6c}", static_cast<int>('x')), "x ");
862 EXPECT_EQ(fmt::format("{:>06.0f}", 0.00884311), " 0");
865 TEST(format_test
, runtime_width
) {
866 auto int_maxer
= std::to_string(INT_MAX
+ 1u);
867 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{" + int_maxer
), 0),
868 format_error
, "invalid format string");
869 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{" + int_maxer
+ "}"), 0),
870 format_error
, "argument not found");
871 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{" + int_maxer
+ "}}"), 0),
872 format_error
, "argument not found");
874 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{"), 0), format_error
,
875 "invalid format string");
876 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{}"), 0), format_error
,
877 "cannot switch from manual to automatic argument indexing");
878 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{?}}"), 0), format_error
,
879 "invalid format string");
880 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{1}}"), 0), format_error
,
881 "argument not found");
883 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{0:}}"), 0), format_error
,
884 "invalid format string");
886 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{1}}"), 0, -1), format_error
,
888 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{1}}"), 0, (INT_MAX
+ 1u)),
889 format_error
, "number is too big");
890 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{1}}"), 0, -1l), format_error
,
892 if (fmt::detail::const_check(sizeof(long) > sizeof(int))) {
893 long value
= INT_MAX
;
894 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{1}}"), 0, (value
+ 1)),
895 format_error
, "number is too big");
897 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{1}}"), 0, (INT_MAX
+ 1ul)),
898 format_error
, "number is too big");
900 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{1}}"), 0, '0'), format_error
,
901 "width is not integer");
902 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{1}}"), 0, 0.0), format_error
,
903 "width is not integer");
905 EXPECT_EQ(fmt::format("{0:{1}}", -42, 4), " -42");
906 EXPECT_EQ(fmt::format("{0:{1}}", 42u, 5), " 42");
907 EXPECT_EQ(fmt::format("{0:{1}}", -42l, 6), " -42");
908 EXPECT_EQ(fmt::format("{0:{1}}", 42ul, 7), " 42");
909 EXPECT_EQ(fmt::format("{0:{1}}", -42ll, 6), " -42");
910 EXPECT_EQ(fmt::format("{0:{1}}", 42ull, 7), " 42");
911 EXPECT_EQ(fmt::format("{0:{1}}", -1.23, 8), " -1.23");
912 EXPECT_EQ(fmt::format("{0:{1}}", -1.23l, 9), " -1.23");
914 fmt::format("{0:{1}}", reinterpret_cast<void*>(0xcafe), 10));
915 EXPECT_EQ(fmt::format("{0:{1}}", 'x', 11), "x ");
916 EXPECT_EQ(fmt::format("{0:{1}}", "str", 12), "str ");
917 EXPECT_EQ(fmt::format("{:{}}", 42, short(4)), " 42");
920 TEST(format_test
, precision
) {
921 char format_str
[buffer_size
];
922 safe_sprintf(format_str
, "{0:.%u", UINT_MAX
);
923 increment(format_str
+ 4);
924 EXPECT_THROW_MSG((void)fmt::format(runtime(format_str
), 0.0), format_error
,
925 "number is too big");
926 size_t size
= std::strlen(format_str
);
927 format_str
[size
] = '}';
928 format_str
[size
+ 1] = 0;
929 EXPECT_THROW_MSG((void)fmt::format(runtime(format_str
), 0.0), format_error
,
930 "number is too big");
932 safe_sprintf(format_str
, "{0:.%u", INT_MAX
+ 1u);
933 EXPECT_THROW_MSG((void)fmt::format(runtime(format_str
), 0.0), format_error
,
934 "number is too big");
935 safe_sprintf(format_str
, "{0:.%u}", INT_MAX
+ 1u);
936 EXPECT_THROW_MSG((void)fmt::format(runtime(format_str
), 0.0), format_error
,
937 "number is too big");
939 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:."), 0.0), format_error
,
940 "invalid precision");
941 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.}"), 0.0), format_error
,
942 "invalid precision");
944 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.2"), 0), format_error
,
945 "invalid format specifier");
946 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.2}"), 42), format_error
,
947 "invalid format specifier");
948 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.2f}"), 42), format_error
,
949 "invalid format specifier");
950 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.2}"), 42u), format_error
,
951 "invalid format specifier");
952 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.2f}"), 42u), format_error
,
953 "invalid format specifier");
954 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.2}"), 42l), format_error
,
955 "invalid format specifier");
956 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.2f}"), 42l), format_error
,
957 "invalid format specifier");
958 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.2}"), 42ul), format_error
,
959 "invalid format specifier");
960 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.2f}"), 42ul), format_error
,
961 "invalid format specifier");
962 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.2}"), 42ll), format_error
,
963 "invalid format specifier");
964 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.2f}"), 42ll), format_error
,
965 "invalid format specifier");
966 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.2}"), 42ull), format_error
,
967 "invalid format specifier");
968 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.2f}"), 42ull), format_error
,
969 "invalid format specifier");
970 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:3.0}"), 'x'), format_error
,
971 "invalid format specifier");
972 EXPECT_EQ(fmt::format("{0:.2}", 1.2345), "1.2");
973 EXPECT_EQ(fmt::format("{0:.2}", 1.2345l), "1.2");
974 EXPECT_EQ(fmt::format("{:.2}", 1.234e56
), "1.2e+56");
975 EXPECT_EQ(fmt::format("{0:.3}", 1.1), "1.1");
976 EXPECT_EQ(fmt::format("{:.0e}", 1.0L), "1e+00");
977 EXPECT_EQ(fmt::format("{:9.1e}", 0.0), " 0.0e+00");
978 EXPECT_EQ(fmt::format("{:.7f}", 0.0000000000000071054273576010018587L),
982 fmt::format("{:.494}", 4.9406564584124654E-324),
983 "4.9406564584124654417656879286822137236505980261432476442558568250067550"
984 "727020875186529983636163599237979656469544571773092665671035593979639877"
985 "479601078187812630071319031140452784581716784898210368871863605699873072"
986 "305000638740915356498438731247339727316961514003171538539807412623856559"
987 "117102665855668676818703956031062493194527159149245532930545654440112748"
988 "012970999954193198940908041656332452475714786901472678015935523861155013"
989 "480352649347201937902681071074917033322268447533357208324319361e-324");
991 fmt::format("{:.1074f}", 1.1125369292536e-308),
992 "0.0000000000000000000000000000000000000000000000000000000000000000000000"
993 "000000000000000000000000000000000000000000000000000000000000000000000000"
994 "000000000000000000000000000000000000000000000000000000000000000000000000"
995 "000000000000000000000000000000000000000000000000000000000000000000000000"
996 "000000000000000000000111253692925360019747947051741965785554081512200979"
997 "355021686109411883779182127659725163430929750364498219730822952552570601"
998 "152163505899912777129583674906301179059298598412303893909188340988729019"
999 "014361467448914817838555156840459458527907308695109202499990850735085304"
1000 "478476991912072201449236975063640913461919914396877093174125167509869762"
1001 "482369631100360266123742648159508919592746619553246586039571522788247697"
1002 "156360766271842991667238355464496455107749716934387136380536472531224398"
1003 "559833794807213172371254492216255558078524900147957309382830827524104234"
1004 "530961756787819847850302379672357738807808384667004752163416921762619527"
1005 "462847642037420991432005657440259928195996762610375541867198059294212446"
1006 "81962777939941034720757232455434770912461317493580281734466552734375");
1008 std::string outputs
[] = {
1009 "-0X1.41FE3FFE71C9E000000000000000000000000000000000000000000000000000000"
1010 "000000000000000000000000000000000000000000000000000000000000000000000000"
1011 "000000000000000000000000000000000000000000000000000000000000000000000000"
1012 "000000000000000000000000000000000000000000000000000000000000000000000000"
1013 "000000000000000000000000000000000000000000000000000000000000000000000000"
1014 "000000000000000000000000000000000000000000000000000000000000000000000000"
1015 "000000000000000000000000000000000000000000000000000000000000000000000000"
1016 "000000000000000000000000000000000000000000000000000000000000000000000000"
1017 "000000000000000000000000000000000000000000000000000000000000000000000000"
1018 "000000000000000000000000000000000000000000000000000000000000000000000000"
1019 "000000000000000000000000000000000000000000000000000000000000000000000000"
1020 "000000000000000000000000000000000000000000000000000P+127",
1021 "-0XA.0FF1FFF38E4F0000000000000000000000000000000000000000000000000000000"
1022 "000000000000000000000000000000000000000000000000000000000000000000000000"
1023 "000000000000000000000000000000000000000000000000000000000000000000000000"
1024 "000000000000000000000000000000000000000000000000000000000000000000000000"
1025 "000000000000000000000000000000000000000000000000000000000000000000000000"
1026 "000000000000000000000000000000000000000000000000000000000000000000000000"
1027 "000000000000000000000000000000000000000000000000000000000000000000000000"
1028 "000000000000000000000000000000000000000000000000000000000000000000000000"
1029 "000000000000000000000000000000000000000000000000000000000000000000000000"
1030 "000000000000000000000000000000000000000000000000000000000000000000000000"
1031 "000000000000000000000000000000000000000000000000000000000000000000000000"
1032 "000000000000000000000000000000000000000000000000000P+124"};
1033 EXPECT_THAT(outputs
,
1034 testing::Contains(fmt::format("{:.838A}", -2.14001164E+38)));
1036 if (std::numeric_limits
<long double>::digits
== 64) {
1037 auto ld
= (std::numeric_limits
<long double>::min
)();
1038 EXPECT_EQ(fmt::format("{:.0}", ld
), "3e-4932");
1040 fmt::format("{:0g}", std::numeric_limits
<long double>::denorm_min()),
1044 EXPECT_EQ(fmt::format("{:#.0f}", 123.0), "123.");
1045 EXPECT_EQ(fmt::format("{:.02f}", 1.234), "1.23");
1046 EXPECT_EQ(fmt::format("{:.1g}", 0.001), "0.001");
1047 EXPECT_EQ(fmt::format("{}", 1019666432.0f
), "1019666400");
1048 EXPECT_EQ(fmt::format("{:.0e}", 9.5), "1e+01");
1049 EXPECT_EQ(fmt::format("{:.1e}", 1e-34), "1.0e-34");
1052 (void)fmt::format(runtime("{0:.2}"), reinterpret_cast<void*>(0xcafe)),
1053 format_error
, "invalid format specifier");
1055 (void)fmt::format(runtime("{0:.2f}"), reinterpret_cast<void*>(0xcafe)),
1056 format_error
, "invalid format specifier");
1057 EXPECT_THROW_MSG((void)fmt::format(runtime("{:.{}e}"), 42.0,
1058 fmt::detail::max_value
<int>()),
1059 format_error
, "number is too big");
1061 (void)fmt::format("{:.2147483646f}", -2.2121295195081227E+304),
1062 format_error
, "number is too big");
1064 EXPECT_EQ(fmt::format("{0:.2}", "str"), "st");
1065 EXPECT_EQ(fmt::format("{0:.5}", "вожыкі"), "вожык");
1066 EXPECT_EQ(fmt::format("{0:.6}", "123456\xad"), "123456");
1069 TEST(xchar_test
, utf8_precision
) {
1070 auto result
= fmt::format("{:.4}", "caf\u00e9s"); // cafés
1071 EXPECT_EQ(fmt::detail::compute_width(result
), 4);
1072 EXPECT_EQ(result
, "caf\u00e9");
1075 TEST(format_test
, runtime_precision
) {
1076 char format_str
[buffer_size
];
1077 safe_sprintf(format_str
, "{0:.{%u", UINT_MAX
);
1078 increment(format_str
+ 5);
1079 EXPECT_THROW_MSG((void)fmt::format(runtime(format_str
), 0.0), format_error
,
1080 "invalid format string");
1081 size_t size
= std::strlen(format_str
);
1082 format_str
[size
] = '}';
1083 format_str
[size
+ 1] = 0;
1084 EXPECT_THROW_MSG((void)fmt::format(runtime(format_str
), 0.0), format_error
,
1085 "argument not found");
1086 format_str
[size
+ 1] = '}';
1087 format_str
[size
+ 2] = 0;
1088 EXPECT_THROW_MSG((void)fmt::format(runtime(format_str
), 0.0), format_error
,
1089 "argument not found");
1091 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{"), 0.0), format_error
,
1092 "invalid format string");
1093 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{}"), 0.0), format_error
,
1094 "cannot switch from manual to automatic argument indexing");
1095 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{?}}"), 0.0), format_error
,
1096 "invalid format string");
1097 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}"), 0, 0), format_error
,
1098 "invalid format specifier");
1099 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 0.0), format_error
,
1100 "argument not found");
1102 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{0:}}"), 0.0), format_error
,
1103 "invalid format string");
1105 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 0.0, -1),
1106 format_error
, "negative precision");
1107 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 0.0, (INT_MAX
+ 1u)),
1108 format_error
, "number is too big");
1109 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 0.0, -1l),
1110 format_error
, "negative precision");
1111 if (fmt::detail::const_check(sizeof(long) > sizeof(int))) {
1112 long value
= INT_MAX
;
1113 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 0.0, (value
+ 1)),
1114 format_error
, "number is too big");
1116 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 0.0, (INT_MAX
+ 1ul)),
1117 format_error
, "number is too big");
1119 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 0.0, '0'),
1120 format_error
, "precision is not integer");
1121 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 0.0, 0.0),
1122 format_error
, "precision is not integer");
1124 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 42, 2), format_error
,
1125 "invalid format specifier");
1126 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}f}"), 42, 2), format_error
,
1127 "invalid format specifier");
1128 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 42u, 2), format_error
,
1129 "invalid format specifier");
1130 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}f}"), 42u, 2),
1131 format_error
, "invalid format specifier");
1132 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 42l, 2), format_error
,
1133 "invalid format specifier");
1134 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}f}"), 42l, 2),
1135 format_error
, "invalid format specifier");
1136 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 42ul, 2),
1137 format_error
, "invalid format specifier");
1138 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}f}"), 42ul, 2),
1139 format_error
, "invalid format specifier");
1140 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 42ll, 2),
1141 format_error
, "invalid format specifier");
1142 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}f}"), 42ll, 2),
1143 format_error
, "invalid format specifier");
1144 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 42ull, 2),
1145 format_error
, "invalid format specifier");
1146 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}f}"), 42ull, 2),
1147 format_error
, "invalid format specifier");
1148 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:3.{1}}"), 'x', 0),
1149 format_error
, "invalid format specifier");
1150 EXPECT_EQ(fmt::format("{0:.{1}}", 1.2345, 2), "1.2");
1151 EXPECT_EQ(fmt::format("{1:.{0}}", 2, 1.2345l), "1.2");
1153 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"),
1154 reinterpret_cast<void*>(0xcafe), 2),
1155 format_error
, "invalid format specifier");
1156 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}f}"),
1157 reinterpret_cast<void*>(0xcafe), 2),
1158 format_error
, "invalid format specifier");
1160 EXPECT_EQ(fmt::format("{0:.{1}}", "str", 2), "st");
1163 TEST(format_test
, format_bool
) {
1164 EXPECT_EQ(fmt::format("{}", true), "true");
1165 EXPECT_EQ(fmt::format("{}", false), "false");
1166 EXPECT_EQ(fmt::format("{:d}", true), "1");
1167 EXPECT_EQ(fmt::format("{:5}", true), "true ");
1168 EXPECT_EQ(fmt::format("{:s}", true), "true");
1169 EXPECT_EQ(fmt::format("{:s}", false), "false");
1170 EXPECT_EQ(fmt::format("{:6s}", false), "false ");
1171 EXPECT_THROW_MSG((void)fmt::format(runtime("{:c}"), false), format_error
,
1172 "invalid format specifier");
1175 TEST(format_test
, format_short
) {
1177 EXPECT_EQ(fmt::format("{0:d}", s
), "42");
1178 unsigned short us
= 42;
1179 EXPECT_EQ(fmt::format("{0:d}", us
), "42");
1182 template <typename T
>
1183 void check_unknown_types(const T
& value
, const char* types
, const char*) {
1184 char format_str
[buffer_size
];
1185 const char* special
= ".0123456789L?}";
1186 for (int i
= CHAR_MIN
; i
<= CHAR_MAX
; ++i
) {
1187 char c
= static_cast<char>(i
);
1188 if (std::strchr(types
, c
) || std::strchr(special
, c
) || !c
) continue;
1189 safe_sprintf(format_str
, "{0:10%c}", c
);
1190 const char* message
= "invalid format specifier";
1191 EXPECT_THROW_MSG((void)fmt::format(runtime(format_str
), value
),
1192 format_error
, message
)
1193 << format_str
<< " " << message
;
1197 TEST(format_test
, format_int
) {
1198 EXPECT_THROW_MSG((void)fmt::format(runtime("{0:v"), 42), format_error
,
1199 "invalid format specifier");
1200 check_unknown_types(42, "bBdoxXnLc", "integer");
1201 EXPECT_EQ(fmt::format("{:c}", static_cast<int>('x')), "x");
1204 TEST(format_test
, format_bin
) {
1205 EXPECT_EQ(fmt::format("{0:b}", 0), "0");
1206 EXPECT_EQ(fmt::format("{0:b}", 42), "101010");
1207 EXPECT_EQ(fmt::format("{0:b}", 42u), "101010");
1208 EXPECT_EQ(fmt::format("{0:b}", -42), "-101010");
1209 EXPECT_EQ(fmt::format("{0:b}", 12345), "11000000111001");
1210 EXPECT_EQ(fmt::format("{0:b}", 0x12345678), "10010001101000101011001111000");
1211 EXPECT_EQ("10010000101010111100110111101111",
1212 fmt::format("{0:b}", 0x90ABCDEF));
1213 EXPECT_EQ("11111111111111111111111111111111",
1214 fmt::format("{0:b}", max_value
<uint32_t>()));
1218 constexpr auto int128_max
= static_cast<__int128_t
>(
1219 (static_cast<__uint128_t
>(1) << ((__SIZEOF_INT128__
* CHAR_BIT
) - 1)) - 1);
1220 constexpr auto int128_min
= -int128_max
- 1;
1222 constexpr auto uint128_max
= ~static_cast<__uint128_t
>(0);
1225 TEST(format_test
, format_dec
) {
1226 EXPECT_EQ(fmt::format("{0}", 0), "0");
1227 EXPECT_EQ(fmt::format("{0}", 42), "42");
1228 EXPECT_EQ(fmt::format("{:}>", 42), "42>");
1229 EXPECT_EQ(fmt::format("{0:d}", 42), "42");
1230 EXPECT_EQ(fmt::format("{0}", 42u), "42");
1231 EXPECT_EQ(fmt::format("{0}", -42), "-42");
1232 EXPECT_EQ(fmt::format("{0}", 12345), "12345");
1233 EXPECT_EQ(fmt::format("{0}", 67890), "67890");
1235 EXPECT_EQ(fmt::format("{0}", static_cast<__int128_t
>(0)), "0");
1236 EXPECT_EQ(fmt::format("{0}", static_cast<__uint128_t
>(0)), "0");
1237 EXPECT_EQ("9223372036854775808",
1238 fmt::format("{0}", static_cast<__int128_t
>(INT64_MAX
) + 1));
1239 EXPECT_EQ("-9223372036854775809",
1240 fmt::format("{0}", static_cast<__int128_t
>(INT64_MIN
) - 1));
1241 EXPECT_EQ("18446744073709551616",
1242 fmt::format("{0}", static_cast<__int128_t
>(UINT64_MAX
) + 1));
1243 EXPECT_EQ("170141183460469231731687303715884105727",
1244 fmt::format("{0}", int128_max
));
1245 EXPECT_EQ("-170141183460469231731687303715884105728",
1246 fmt::format("{0}", int128_min
));
1247 EXPECT_EQ("340282366920938463463374607431768211455",
1248 fmt::format("{0}", uint128_max
));
1251 char buffer
[buffer_size
];
1252 safe_sprintf(buffer
, "%d", INT_MIN
);
1253 EXPECT_EQ(buffer
, fmt::format("{0}", INT_MIN
));
1254 safe_sprintf(buffer
, "%d", INT_MAX
);
1255 EXPECT_EQ(buffer
, fmt::format("{0}", INT_MAX
));
1256 safe_sprintf(buffer
, "%u", UINT_MAX
);
1257 EXPECT_EQ(buffer
, fmt::format("{0}", UINT_MAX
));
1258 safe_sprintf(buffer
, "%ld", 0 - static_cast<unsigned long>(LONG_MIN
));
1259 EXPECT_EQ(buffer
, fmt::format("{0}", LONG_MIN
));
1260 safe_sprintf(buffer
, "%ld", LONG_MAX
);
1261 EXPECT_EQ(buffer
, fmt::format("{0}", LONG_MAX
));
1262 safe_sprintf(buffer
, "%lu", ULONG_MAX
);
1263 EXPECT_EQ(buffer
, fmt::format("{0}", ULONG_MAX
));
1266 TEST(format_test
, format_hex
) {
1267 EXPECT_EQ(fmt::format("{0:x}", 0), "0");
1268 EXPECT_EQ(fmt::format("{0:x}", 0x42), "42");
1269 EXPECT_EQ(fmt::format("{0:x}", 0x42u
), "42");
1270 EXPECT_EQ(fmt::format("{0:x}", -0x42), "-42");
1271 EXPECT_EQ(fmt::format("{0:x}", 0x12345678), "12345678");
1272 EXPECT_EQ(fmt::format("{0:x}", 0x90abcdef), "90abcdef");
1273 EXPECT_EQ(fmt::format("{0:X}", 0x12345678), "12345678");
1274 EXPECT_EQ(fmt::format("{0:X}", 0x90ABCDEF), "90ABCDEF");
1276 EXPECT_EQ(fmt::format("{0:x}", static_cast<__int128_t
>(0)), "0");
1277 EXPECT_EQ(fmt::format("{0:x}", static_cast<__uint128_t
>(0)), "0");
1278 EXPECT_EQ("8000000000000000",
1279 fmt::format("{0:x}", static_cast<__int128_t
>(INT64_MAX
) + 1));
1280 EXPECT_EQ("-8000000000000001",
1281 fmt::format("{0:x}", static_cast<__int128_t
>(INT64_MIN
) - 1));
1282 EXPECT_EQ("10000000000000000",
1283 fmt::format("{0:x}", static_cast<__int128_t
>(UINT64_MAX
) + 1));
1284 EXPECT_EQ("7fffffffffffffffffffffffffffffff",
1285 fmt::format("{0:x}", int128_max
));
1286 EXPECT_EQ("-80000000000000000000000000000000",
1287 fmt::format("{0:x}", int128_min
));
1288 EXPECT_EQ("ffffffffffffffffffffffffffffffff",
1289 fmt::format("{0:x}", uint128_max
));
1292 char buffer
[buffer_size
];
1293 safe_sprintf(buffer
, "-%x", 0 - static_cast<unsigned>(INT_MIN
));
1294 EXPECT_EQ(buffer
, fmt::format("{0:x}", INT_MIN
));
1295 safe_sprintf(buffer
, "%x", INT_MAX
);
1296 EXPECT_EQ(buffer
, fmt::format("{0:x}", INT_MAX
));
1297 safe_sprintf(buffer
, "%x", UINT_MAX
);
1298 EXPECT_EQ(buffer
, fmt::format("{0:x}", UINT_MAX
));
1299 safe_sprintf(buffer
, "-%lx", 0 - static_cast<unsigned long>(LONG_MIN
));
1300 EXPECT_EQ(buffer
, fmt::format("{0:x}", LONG_MIN
));
1301 safe_sprintf(buffer
, "%lx", LONG_MAX
);
1302 EXPECT_EQ(buffer
, fmt::format("{0:x}", LONG_MAX
));
1303 safe_sprintf(buffer
, "%lx", ULONG_MAX
);
1304 EXPECT_EQ(buffer
, fmt::format("{0:x}", ULONG_MAX
));
1307 TEST(format_test
, format_oct
) {
1308 EXPECT_EQ(fmt::format("{0:o}", 0), "0");
1309 EXPECT_EQ(fmt::format("{0:o}", 042), "42");
1310 EXPECT_EQ(fmt::format("{0:o}", 042u), "42");
1311 EXPECT_EQ(fmt::format("{0:o}", -042), "-42");
1312 EXPECT_EQ(fmt::format("{0:o}", 012345670), "12345670");
1314 EXPECT_EQ(fmt::format("{0:o}", static_cast<__int128_t
>(0)), "0");
1315 EXPECT_EQ(fmt::format("{0:o}", static_cast<__uint128_t
>(0)), "0");
1316 EXPECT_EQ("1000000000000000000000",
1317 fmt::format("{0:o}", static_cast<__int128_t
>(INT64_MAX
) + 1));
1318 EXPECT_EQ("-1000000000000000000001",
1319 fmt::format("{0:o}", static_cast<__int128_t
>(INT64_MIN
) - 1));
1320 EXPECT_EQ("2000000000000000000000",
1321 fmt::format("{0:o}", static_cast<__int128_t
>(UINT64_MAX
) + 1));
1322 EXPECT_EQ("1777777777777777777777777777777777777777777",
1323 fmt::format("{0:o}", int128_max
));
1324 EXPECT_EQ("-2000000000000000000000000000000000000000000",
1325 fmt::format("{0:o}", int128_min
));
1326 EXPECT_EQ("3777777777777777777777777777777777777777777",
1327 fmt::format("{0:o}", uint128_max
));
1330 char buffer
[buffer_size
];
1331 safe_sprintf(buffer
, "-%o", 0 - static_cast<unsigned>(INT_MIN
));
1332 EXPECT_EQ(buffer
, fmt::format("{0:o}", INT_MIN
));
1333 safe_sprintf(buffer
, "%o", INT_MAX
);
1334 EXPECT_EQ(buffer
, fmt::format("{0:o}", INT_MAX
));
1335 safe_sprintf(buffer
, "%o", UINT_MAX
);
1336 EXPECT_EQ(buffer
, fmt::format("{0:o}", UINT_MAX
));
1337 safe_sprintf(buffer
, "-%lo", 0 - static_cast<unsigned long>(LONG_MIN
));
1338 EXPECT_EQ(buffer
, fmt::format("{0:o}", LONG_MIN
));
1339 safe_sprintf(buffer
, "%lo", LONG_MAX
);
1340 EXPECT_EQ(buffer
, fmt::format("{0:o}", LONG_MAX
));
1341 safe_sprintf(buffer
, "%lo", ULONG_MAX
);
1342 EXPECT_EQ(buffer
, fmt::format("{0:o}", ULONG_MAX
));
1345 TEST(format_test
, format_int_locale
) {
1346 EXPECT_EQ(fmt::format("{:L}", 1234), "1234");
1349 TEST(format_test
, format_float
) {
1350 EXPECT_EQ(fmt::format("{}", 0.0f
), "0");
1351 EXPECT_EQ(fmt::format("{0:f}", 392.5f
), "392.500000");
1354 TEST(format_test
, format_double
) {
1355 EXPECT_EQ(fmt::format("{}", 0.0), "0");
1356 check_unknown_types(1.2, "eEfFgGaAnL%", "double");
1357 EXPECT_EQ(fmt::format("{:}", 0.0), "0");
1358 EXPECT_EQ(fmt::format("{:f}", 0.0), "0.000000");
1359 EXPECT_EQ(fmt::format("{:g}", 0.0), "0");
1360 EXPECT_EQ(fmt::format("{:}", 392.65), "392.65");
1361 EXPECT_EQ(fmt::format("{:g}", 392.65), "392.65");
1362 EXPECT_EQ(fmt::format("{:G}", 392.65), "392.65");
1363 EXPECT_EQ(fmt::format("{:g}", 4.9014e6
), "4.9014e+06");
1364 EXPECT_EQ(fmt::format("{:f}", 392.65), "392.650000");
1365 EXPECT_EQ(fmt::format("{:F}", 392.65), "392.650000");
1366 EXPECT_EQ(fmt::format("{:L}", 42.0), "42");
1367 EXPECT_EQ(fmt::format("{:24a}", 4.2f
), " 0x1.0cccccp+2");
1368 EXPECT_EQ(fmt::format("{:24a}", 4.2), " 0x1.0cccccccccccdp+2");
1369 EXPECT_EQ(fmt::format("{:<24a}", 4.2), "0x1.0cccccccccccdp+2 ");
1370 EXPECT_EQ(fmt::format("{0:e}", 392.65), "3.926500e+02");
1371 EXPECT_EQ(fmt::format("{0:E}", 392.65), "3.926500E+02");
1372 EXPECT_EQ(fmt::format("{0:+010.4g}", 392.65), "+0000392.6");
1374 #if FMT_CPLUSPLUS >= 201703L
1375 double xd
= 0x1.ffffffffffp
+2;
1376 EXPECT_EQ(fmt::format("{:.10a}", xd
), "0x1.ffffffffffp+2");
1377 EXPECT_EQ(fmt::format("{:.9a}", xd
), "0x2.000000000p+2");
1379 if (std::numeric_limits
<long double>::digits
== 64) {
1380 auto ld
= 0xf.ffffffffffp
-3l;
1381 EXPECT_EQ(fmt::format("{:a}", ld
), "0xf.ffffffffffp-3");
1382 EXPECT_EQ(fmt::format("{:.10a}", ld
), "0xf.ffffffffffp-3");
1383 EXPECT_EQ(fmt::format("{:.9a}", ld
), "0x1.000000000p+1");
1387 if (fmt::detail::const_check(std::numeric_limits
<double>::is_iec559
)) {
1388 double d
= (std::numeric_limits
<double>::min
)();
1389 EXPECT_EQ(fmt::format("{:a}", d
), "0x1p-1022");
1390 EXPECT_EQ(fmt::format("{:#a}", d
), "0x1.p-1022");
1392 d
= (std::numeric_limits
<double>::max
)();
1393 EXPECT_EQ(fmt::format("{:a}", d
), "0x1.fffffffffffffp+1023");
1395 d
= std::numeric_limits
<double>::denorm_min();
1396 EXPECT_EQ(fmt::format("{:a}", d
), "0x0.0000000000001p-1022");
1399 if (std::numeric_limits
<long double>::digits
== 64) {
1400 auto ld
= (std::numeric_limits
<long double>::min
)();
1401 EXPECT_EQ(fmt::format("{:a}", ld
), "0x8p-16385");
1403 ld
= (std::numeric_limits
<long double>::max
)();
1404 EXPECT_EQ(fmt::format("{:a}", ld
), "0xf.fffffffffffffffp+16380");
1406 ld
= std::numeric_limits
<long double>::denorm_min();
1407 EXPECT_EQ(fmt::format("{:a}", ld
), "0x0.000000000000001p-16382");
1410 EXPECT_EQ(fmt::format("{:.10a}", 4.2), "0x1.0ccccccccdp+2");
1412 EXPECT_EQ(fmt::format("{:a}", -42.0), "-0x1.5p+5");
1413 EXPECT_EQ(fmt::format("{:A}", -42.0), "-0X1.5P+5");
1415 EXPECT_EQ(fmt::format("{:f}", 9223372036854775807.0),
1416 "9223372036854775808.000000");
1419 TEST(format_test
, precision_rounding
) {
1420 EXPECT_EQ(fmt::format("{:.0f}", 0.0), "0");
1421 EXPECT_EQ(fmt::format("{:.0f}", 0.01), "0");
1422 EXPECT_EQ(fmt::format("{:.0f}", 0.1), "0");
1423 EXPECT_EQ(fmt::format("{:.3f}", 0.00049), "0.000");
1424 EXPECT_EQ(fmt::format("{:.3f}", 0.0005), "0.001");
1425 EXPECT_EQ(fmt::format("{:.3f}", 0.00149), "0.001");
1426 EXPECT_EQ(fmt::format("{:.3f}", 0.0015), "0.002");
1427 EXPECT_EQ(fmt::format("{:.3f}", 0.9999), "1.000");
1428 EXPECT_EQ(fmt::format("{:.3}", 0.00123), "0.00123");
1429 EXPECT_EQ(fmt::format("{:.16g}", 0.1), "0.1");
1430 EXPECT_EQ(fmt::format("{:.0}", 1.0), "1");
1431 EXPECT_EQ("225.51575035152063720",
1432 fmt::format("{:.17f}", 225.51575035152064));
1433 EXPECT_EQ(fmt::format("{:.1f}", -761519619559038.2), "-761519619559038.2");
1434 EXPECT_EQ("1.9156918820264798e-56",
1435 fmt::format("{}", 1.9156918820264798e-56));
1436 EXPECT_EQ(fmt::format("{:.4f}", 7.2809479766055470e-15), "0.0000");
1439 TEST(format_test
, prettify_float
) {
1440 EXPECT_EQ(fmt::format("{}", 1e-4), "0.0001");
1441 EXPECT_EQ(fmt::format("{}", 1e-5), "1e-05");
1442 EXPECT_EQ(fmt::format("{}", 1e15
), "1000000000000000");
1443 EXPECT_EQ(fmt::format("{}", 1e16
), "1e+16");
1444 EXPECT_EQ(fmt::format("{}", 9.999e-5), "9.999e-05");
1445 EXPECT_EQ(fmt::format("{}", 1e10
), "10000000000");
1446 EXPECT_EQ(fmt::format("{}", 1e11
), "100000000000");
1447 EXPECT_EQ(fmt::format("{}", 1234e7
), "12340000000");
1448 EXPECT_EQ(fmt::format("{}", 1234e-2), "12.34");
1449 EXPECT_EQ(fmt::format("{}", 1234e-6), "0.001234");
1450 EXPECT_EQ(fmt::format("{}", 0.1f
), "0.1");
1451 EXPECT_EQ(fmt::format("{}", 1.35631564e-19f
), "1.3563156e-19");
1454 TEST(format_test
, format_nan
) {
1455 double nan
= std::numeric_limits
<double>::quiet_NaN();
1456 EXPECT_EQ(fmt::format("{}", nan
), "nan");
1457 EXPECT_EQ(fmt::format("{:+}", nan
), "+nan");
1458 EXPECT_EQ(fmt::format("{:+06}", nan
), " +nan");
1459 EXPECT_EQ(fmt::format("{:<+06}", nan
), "+nan ");
1460 EXPECT_EQ(fmt::format("{:^+06}", nan
), " +nan ");
1461 EXPECT_EQ(fmt::format("{:>+06}", nan
), " +nan");
1462 if (std::signbit(-nan
)) {
1463 EXPECT_EQ(fmt::format("{}", -nan
), "-nan");
1464 EXPECT_EQ(fmt::format("{:+06}", -nan
), " -nan");
1466 fmt::print("Warning: compiler doesn't handle negative NaN correctly");
1468 EXPECT_EQ(fmt::format("{: }", nan
), " nan");
1469 EXPECT_EQ(fmt::format("{:F}", nan
), "NAN");
1470 EXPECT_EQ(fmt::format("{:<7}", nan
), "nan ");
1471 EXPECT_EQ(fmt::format("{:^7}", nan
), " nan ");
1472 EXPECT_EQ(fmt::format("{:>7}", nan
), " nan");
1475 TEST(format_test
, format_infinity
) {
1476 double inf
= std::numeric_limits
<double>::infinity();
1477 EXPECT_EQ(fmt::format("{}", inf
), "inf");
1478 EXPECT_EQ(fmt::format("{:+}", inf
), "+inf");
1479 EXPECT_EQ(fmt::format("{}", -inf
), "-inf");
1480 EXPECT_EQ(fmt::format("{:+06}", inf
), " +inf");
1481 EXPECT_EQ(fmt::format("{:+06}", -inf
), " -inf");
1482 EXPECT_EQ(fmt::format("{:<+06}", inf
), "+inf ");
1483 EXPECT_EQ(fmt::format("{:^+06}", inf
), " +inf ");
1484 EXPECT_EQ(fmt::format("{:>+06}", inf
), " +inf");
1485 EXPECT_EQ(fmt::format("{: }", inf
), " inf");
1486 EXPECT_EQ(fmt::format("{:F}", inf
), "INF");
1487 EXPECT_EQ(fmt::format("{:<7}", inf
), "inf ");
1488 EXPECT_EQ(fmt::format("{:^7}", inf
), " inf ");
1489 EXPECT_EQ(fmt::format("{:>7}", inf
), " inf");
1492 TEST(format_test
, format_long_double
) {
1493 EXPECT_EQ(fmt::format("{0:}", 0.0l), "0");
1494 EXPECT_EQ(fmt::format("{0:f}", 0.0l), "0.000000");
1495 EXPECT_EQ(fmt::format("{:.1f}", 0.000000001l), "0.0");
1496 EXPECT_EQ(fmt::format("{:.2f}", 0.099l), "0.10");
1497 EXPECT_EQ(fmt::format("{0:}", 392.65l), "392.65");
1498 EXPECT_EQ(fmt::format("{0:g}", 392.65l), "392.65");
1499 EXPECT_EQ(fmt::format("{0:G}", 392.65l), "392.65");
1500 EXPECT_EQ(fmt::format("{0:f}", 392.65l), "392.650000");
1501 EXPECT_EQ(fmt::format("{0:F}", 392.65l), "392.650000");
1502 char buffer
[buffer_size
];
1503 safe_sprintf(buffer
, "%Le", 392.65l);
1504 EXPECT_EQ(buffer
, fmt::format("{0:e}", 392.65l));
1505 EXPECT_EQ(fmt::format("{0:+010.4g}", 392.64l), "+0000392.6");
1508 if (fmt::detail::is_double_double
<decltype(ld
)>::value
) {
1509 safe_sprintf(buffer
, "%a", static_cast<double>(ld
));
1510 EXPECT_EQ(buffer
, fmt::format("{:a}", ld
));
1511 } else if (std::numeric_limits
<long double>::digits
== 64) {
1512 EXPECT_EQ(fmt::format("{:a}", ld
), "0xd.3d70a3d70a3d70ap-2");
1516 TEST(format_test
, format_char
) {
1517 const char types
[] = "cbBdoxX";
1518 check_unknown_types('a', types
, "char");
1519 EXPECT_EQ(fmt::format("{0}", 'a'), "a");
1520 EXPECT_EQ(fmt::format("{0:c}", 'z'), "z");
1522 for (const char* type
= types
+ 1; *type
; ++type
) {
1523 std::string format_str
= fmt::format("{{:{}}}", *type
);
1524 EXPECT_EQ(fmt::format(runtime(format_str
), n
),
1525 fmt::format(runtime(format_str
), 'x'))
1528 EXPECT_EQ(fmt::format("{:02X}", n
), fmt::format("{:02X}", 'x'));
1530 EXPECT_EQ(fmt::format("{}", '\n'), "\n");
1531 EXPECT_EQ(fmt::format("{:?}", '\n'), "'\\n'");
1532 EXPECT_EQ(fmt::format("{:x}", '\xff'), "ff");
1535 TEST(format_test
, format_volatile_char
) {
1536 volatile char c
= 'x';
1537 EXPECT_EQ(fmt::format("{}", c
), "x");
1540 TEST(format_test
, format_unsigned_char
) {
1541 EXPECT_EQ(fmt::format("{}", static_cast<unsigned char>(42)), "42");
1542 EXPECT_EQ(fmt::format("{}", static_cast<uint8_t>(42)), "42");
1545 TEST(format_test
, format_cstring
) {
1546 check_unknown_types("test", "sp", "string");
1547 EXPECT_EQ(fmt::format("{0}", "test"), "test");
1548 EXPECT_EQ(fmt::format("{0:s}", "test"), "test");
1549 char nonconst
[] = "nonconst";
1550 EXPECT_EQ(fmt::format("{0}", nonconst
), "nonconst");
1551 auto nullstr
= static_cast<const char*>(nullptr);
1552 EXPECT_THROW_MSG((void)fmt::format("{}", nullstr
), format_error
,
1553 "string pointer is null");
1554 EXPECT_THROW_MSG((void)fmt::format("{:s}", nullstr
), format_error
,
1555 "string pointer is null");
1558 void function_pointer_test(int, double, std::string
) {}
1560 TEST(format_test
, format_pointer
) {
1561 check_unknown_types(reinterpret_cast<void*>(0x1234), "p", "pointer");
1562 EXPECT_EQ(fmt::format("{0}", static_cast<void*>(nullptr)), "0x0");
1563 EXPECT_EQ(fmt::format("{0}", reinterpret_cast<void*>(0x1234)), "0x1234");
1564 EXPECT_EQ(fmt::format("{0:p}", reinterpret_cast<void*>(0x1234)), "0x1234");
1565 // On CHERI (or other fat-pointer) systems, the size of a pointer is greater
1566 // than the size an integer that can hold a virtual address. There is no
1567 // portable address-as-an-integer type (yet) in C++, so we use `size_t` as
1568 // the closest equivalent for now.
1569 EXPECT_EQ("0x" + std::string(sizeof(size_t) * CHAR_BIT
/ 4, 'f'),
1570 fmt::format("{0}", reinterpret_cast<void*>(~uintptr_t())));
1572 fmt::format("{}", fmt::ptr(reinterpret_cast<int*>(0x1234))));
1573 EXPECT_EQ(fmt::format("{}", fmt::detail::bit_cast
<const void*>(
1574 &function_pointer_test
)),
1575 fmt::format("{}", fmt::ptr(function_pointer_test
)));
1576 EXPECT_EQ(fmt::format("{}", nullptr), "0x0");
1579 TEST(format_test
, write_uintptr_fallback
) {
1580 // Test that formatting a pointer by converting it to uint128_fallback works.
1581 // This is needed to support systems without uintptr_t.
1582 auto s
= std::string();
1583 fmt::detail::write_ptr
<char>(
1584 std::back_inserter(s
),
1585 fmt::detail::bit_cast
<fmt::detail::uint128_fallback
>(
1586 reinterpret_cast<void*>(0xface)),
1588 EXPECT_EQ(s
, "0xface");
1591 enum class color
{ red
, green
, blue
};
1594 enum class color
{ red
, green
, blue
};
1595 using fmt::enums::format_as
;
1596 } // namespace test_ns
1598 TEST(format_test
, format_enum_class
) {
1599 EXPECT_EQ(fmt::format("{}", fmt::underlying(color::red
)), "0");
1600 EXPECT_EQ(fmt::format("{}", test_ns::color::red
), "0");
1603 TEST(format_test
, format_string
) {
1604 EXPECT_EQ(fmt::format("{0}", std::string("test")), "test");
1605 EXPECT_EQ(fmt::format("{0}", std::string("test")), "test");
1606 EXPECT_EQ(fmt::format("{:?}", std::string("test")), "\"test\"");
1607 EXPECT_EQ(fmt::format("{:*^10?}", std::string("test")), "**\"test\"**");
1608 EXPECT_EQ(fmt::format("{:?}", std::string("\test")), "\"\\test\"");
1609 EXPECT_THROW((void)fmt::format(fmt::runtime("{:x}"), std::string("test")),
1613 TEST(format_test
, format_string_view
) {
1614 EXPECT_EQ(fmt::format("{}", string_view("test")), "test");
1615 EXPECT_EQ(fmt::format("{:?}", string_view("t\nst")), "\"t\\nst\"");
1616 EXPECT_EQ(fmt::format("{}", string_view()), "");
1619 #ifdef FMT_USE_STRING_VIEW
1620 struct string_viewable
{};
1623 template <> struct formatter
<string_viewable
> : formatter
<std::string_view
> {
1624 auto format(string_viewable
, format_context
& ctx
) const
1625 -> decltype(ctx
.out()) {
1626 return formatter
<std::string_view
>::format("foo", ctx
);
1631 TEST(format_test
, format_std_string_view
) {
1632 EXPECT_EQ(fmt::format("{}", std::string_view("test")), "test");
1633 EXPECT_EQ(fmt::format("{}", string_viewable()), "foo");
1636 struct explicitly_convertible_to_std_string_view
{
1637 explicit operator std::string_view() const { return "foo"; }
1641 struct fmt::formatter
<explicitly_convertible_to_std_string_view
>
1642 : formatter
<std::string_view
> {
1643 auto format(explicitly_convertible_to_std_string_view v
,
1644 format_context
& ctx
) const -> decltype(ctx
.out()) {
1645 return fmt::format_to(ctx
.out(), "'{}'", std::string_view(v
));
1649 TEST(format_test
, format_explicitly_convertible_to_std_string_view
) {
1651 fmt::format("{}", explicitly_convertible_to_std_string_view()));
1654 struct convertible_to_std_string_view
{
1655 operator std::string_view() const noexcept
{ return "Hi there"; }
1659 class formatter
<convertible_to_std_string_view
>
1660 : public formatter
<std::string_view
> {};
1663 TEST(format_test
, format_implicitly_convertible_and_inherits_string_view
) {
1664 static_assert(fmt::is_formattable
<convertible_to_std_string_view
>{}, "");
1665 EXPECT_EQ("Hi there", fmt::format("{}", convertible_to_std_string_view
{}));
1672 template <> struct formatter
<date
> {
1673 template <typename ParseContext
>
1674 FMT_CONSTEXPR
auto parse(ParseContext
& ctx
) -> decltype(ctx
.begin()) {
1675 auto it
= ctx
.begin();
1676 if (it
!= ctx
.end() && *it
== 'd') ++it
;
1680 auto format(const date
& d
, format_context
& ctx
) const -> decltype(ctx
.out()) {
1681 // Namespace-qualify to avoid ambiguity with std::format_to.
1682 fmt::format_to(ctx
.out(), "{}-{}-{}", d
.year(), d
.month(), d
.day());
1687 template <> struct formatter
<Answer
> : formatter
<int> {
1688 template <typename FormatContext
>
1689 auto format(Answer
, FormatContext
& ctx
) const -> decltype(ctx
.out()) {
1690 return formatter
<int>::format(42, ctx
);
1695 TEST(format_test
, format_custom
) {
1696 EXPECT_THROW_MSG((void)fmt::format(runtime("{:s}"), date(2012, 12, 9)),
1697 format_error
, "unknown format specifier");
1698 EXPECT_EQ(fmt::format("{0}", Answer()), "42");
1699 EXPECT_EQ(fmt::format("{:04}", Answer()), "0042");
1702 TEST(format_test
, format_to_custom
) {
1704 auto end
= fmt::format_to(buf
, "{}", Answer());
1705 EXPECT_EQ(end
, buf
+ 2);
1706 EXPECT_STREQ(buf
, "42");
1709 TEST(format_test
, format_string_from_speed_test
) {
1710 EXPECT_EQ("1.2340000000:0042:+3.13:str:0x3e8:X:%",
1711 fmt::format("{0:0.10f}:{1:04}:{2:+g}:{3}:{4}:{5}:%", 1.234, 42,
1712 3.13, "str", reinterpret_cast<void*>(1000), 'X'));
1715 TEST(format_test
, format_examples
) {
1716 std::string message
= fmt::format("The answer is {}", 42);
1717 EXPECT_EQ("The answer is 42", message
);
1719 EXPECT_EQ(fmt::format("{}", 42), "42");
1722 fmt::format_to(std::back_inserter(out
), "The answer is {}.", 42);
1723 EXPECT_EQ("The answer is 42.", to_string(out
));
1725 const char* filename
= "nonexistent";
1726 FILE* ftest
= safe_fopen(filename
, "r");
1727 if (ftest
) fclose(ftest
);
1728 int error_code
= errno
;
1729 EXPECT_TRUE(ftest
== nullptr);
1730 EXPECT_SYSTEM_ERROR(
1732 FILE* f
= safe_fopen(filename
, "r");
1734 throw fmt::system_error(errno
, "Cannot open file '{}'", filename
);
1737 error_code
, "Cannot open file 'nonexistent'");
1739 EXPECT_EQ("First, thou shalt count to three",
1740 fmt::format("First, thou shalt count to {0}", "three"));
1741 EXPECT_EQ(fmt::format("Bring me a {}", "shrubbery"), "Bring me a shrubbery");
1742 EXPECT_EQ(fmt::format("From {} to {}", 1, 3), "From 1 to 3");
1744 char buffer
[buffer_size
];
1745 safe_sprintf(buffer
, "%03.2f", -1.2);
1746 EXPECT_EQ(buffer
, fmt::format("{:03.2f}", -1.2));
1748 EXPECT_EQ(fmt::format("{0}, {1}, {2}", 'a', 'b', 'c'), "a, b, c");
1749 EXPECT_EQ(fmt::format("{}, {}, {}", 'a', 'b', 'c'), "a, b, c");
1750 EXPECT_EQ(fmt::format("{2}, {1}, {0}", 'a', 'b', 'c'), "c, b, a");
1751 EXPECT_EQ(fmt::format("{0}{1}{0}", "abra", "cad"), "abracadabra");
1753 EXPECT_EQ("left aligned ",
1754 fmt::format("{:<30}", "left aligned"));
1755 EXPECT_EQ(" right aligned",
1756 fmt::format("{:>30}", "right aligned"));
1757 EXPECT_EQ(" centered ",
1758 fmt::format("{:^30}", "centered"));
1759 EXPECT_EQ("***********centered***********",
1760 fmt::format("{:*^30}", "centered"));
1762 EXPECT_EQ(fmt::format("{:+f}; {:+f}", 3.14, -3.14), "+3.140000; -3.140000");
1763 EXPECT_EQ(fmt::format("{: f}; {: f}", 3.14, -3.14), " 3.140000; -3.140000");
1764 EXPECT_EQ(fmt::format("{:-f}; {:-f}", 3.14, -3.14), "3.140000; -3.140000");
1766 EXPECT_EQ("int: 42; hex: 2a; oct: 52",
1767 fmt::format("int: {0:d}; hex: {0:x}; oct: {0:o}", 42));
1768 EXPECT_EQ("int: 42; hex: 0x2a; oct: 052",
1769 fmt::format("int: {0:d}; hex: {0:#x}; oct: {0:#o}", 42));
1771 EXPECT_EQ(fmt::format("The answer is {}", 42), "The answer is 42");
1773 (void)fmt::format(runtime("The answer is {:d}"), "forty-two"),
1774 format_error
, "invalid format specifier");
1777 stdout
, fmt::print("{}", std::numeric_limits
<double>::infinity()), "inf");
1780 TEST(format_test
, print
) {
1781 EXPECT_WRITE(stdout
, fmt::print("Don't {}!", "panic"), "Don't panic!");
1782 EXPECT_WRITE(stderr
, fmt::print(stderr
, "Don't {}!", "panic"),
1784 EXPECT_WRITE(stdout
, fmt::println("Don't {}!", "panic"), "Don't panic!\n");
1785 EXPECT_WRITE(stderr
, fmt::println(stderr
, "Don't {}!", "panic"),
1789 TEST(format_test
, big_print
) {
1790 enum { count
= 5000 };
1791 auto big_print
= []() {
1792 for (int i
= 0; i
< count
/ 5; ++i
) fmt::print("xxxxx");
1794 EXPECT_WRITE(stdout
, big_print(), std::string(count
, 'x'));
1797 // Windows CRT implements _IOLBF incorrectly (full buffering).
1798 #if FMT_USE_FCNTL && !defined(_WIN32)
1799 TEST(format_test
, line_buffering
) {
1800 auto pipe
= fmt::pipe();
1802 int write_fd
= pipe
.write_end
.descriptor();
1803 auto write_end
= pipe
.write_end
.fdopen("w");
1804 setvbuf(write_end
.get(), nullptr, _IOLBF
, 4096);
1805 write_end
.print("42\n");
1809 } catch (const std::system_error
&) {
1812 auto read_end
= pipe
.read_end
.fdopen("r");
1813 std::thread
reader([&]() {
1815 int result
= fscanf(read_end
.get(), "%d", &n
);
1824 struct deadlockable
{
1826 mutable std::mutex mutex
;
1830 template <> struct formatter
<deadlockable
> {
1831 FMT_CONSTEXPR
auto parse(format_parse_context
& ctx
) -> decltype(ctx
.begin()) {
1835 auto format(const deadlockable
& d
, format_context
& ctx
) const
1836 -> decltype(ctx
.out()) {
1837 std::lock_guard
<std::mutex
> lock(d
.mutex
);
1838 return format_to(ctx
.out(), "{}", d
.value
);
1843 TEST(format_test
, locking_formatter
) {
1844 auto f
= fmt::buffered_file();
1846 f
= fmt::buffered_file("/dev/null", "w");
1847 } catch (const std::system_error
&) {
1848 fmt::print(stderr
, "warning: /dev/null is not supported\n");
1852 auto t
= std::thread([&]() {
1853 fmt::print(f
.get(), "start t\n");
1854 std::lock_guard
<std::mutex
> lock(d
.mutex
);
1855 for (int i
= 0; i
< 1000000; ++i
) d
.value
+= 10;
1856 fmt::print(f
.get(), "done\n");
1858 for (int i
= 0; i
< 100; ++i
) fmt::print(f
.get(), "{}", d
);
1862 TEST(format_test
, variadic
) {
1863 EXPECT_EQ(fmt::format("{}c{}", "ab", 1), "abc1");
1866 TEST(format_test
, bytes
) {
1867 auto s
= fmt::format("{:10}", fmt::bytes("ёжик"));
1868 EXPECT_EQ("ёжик ", s
);
1869 EXPECT_EQ(10, s
.size());
1872 TEST(format_test
, group_digits_view
) {
1873 EXPECT_EQ(fmt::format("{}", fmt::group_digits(10000000)), "10,000,000");
1874 EXPECT_EQ(fmt::format("{:8}", fmt::group_digits(1000)), " 1,000");
1875 EXPECT_EQ(fmt::format("{}", fmt::group_digits(-10000000)), "-10,000,000");
1876 EXPECT_EQ(fmt::format("{:8}", fmt::group_digits(-1000)), " -1,000");
1877 EXPECT_EQ(fmt::format("{:8}", fmt::group_digits(-100)), " -100");
1880 #ifdef __cpp_generic_lambdas
1886 template <> struct formatter
<point
> : nested_formatter
<double> {
1887 auto format(point p
, format_context
& ctx
) const -> decltype(ctx
.out()) {
1888 return write_padded(ctx
, [this, p
](auto out
) -> decltype(out
) {
1889 return fmt::format_to(out
, "({}, {})", this->nested(p
.x
),
1896 TEST(format_test
, nested_formatter
) {
1897 EXPECT_EQ(fmt::format("{:>16.2f}", point
{1, 2}), " (1.00, 2.00)");
1899 #endif // __cpp_generic_lambdas
1901 enum test_enum
{ foo
, bar
};
1902 auto format_as(test_enum e
) -> int { return e
; }
1904 std::string
vformat_message(int id
, const char* format
, fmt::format_args args
) {
1905 auto buffer
= fmt::memory_buffer();
1906 fmt::format_to(fmt::appender(buffer
), "[{}] ", id
);
1907 vformat_to(fmt::appender(buffer
), format
, args
);
1908 return to_string(buffer
);
1911 template <typename
... Args
>
1912 std::string
format_message(int id
, const char* format
, const Args
&... args
) {
1913 auto va
= fmt::make_format_args(args
...);
1914 return vformat_message(id
, format
, va
);
1917 TEST(format_test
, format_message_example
) {
1918 EXPECT_EQ("[42] something happened",
1919 format_message(42, "{} happened", "something"));
1922 template <typename
... Args
>
1923 void print_error(const char* file
, int line
, const char* format
,
1924 const Args
&... args
) {
1925 fmt::print("{}: {}: ", file
, line
);
1926 fmt::print(format
, args
...);
1929 TEST(format_test
, unpacked_args
) {
1930 EXPECT_EQ("0123456789abcdefg",
1931 fmt::format("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 0, 1, 2, 3, 4, 5,
1932 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f', 'g'));
1935 constexpr char with_null
[3] = {'{', '}', '\0'};
1936 constexpr char no_null
[2] = {'{', '}'};
1937 static constexpr const char static_with_null
[3] = {'{', '}', '\0'};
1938 static constexpr const char static_no_null
[2] = {'{', '}'};
1940 TEST(format_test
, compile_time_string
) {
1941 EXPECT_EQ(fmt::format(FMT_STRING("foo")), "foo");
1942 EXPECT_EQ(fmt::format(FMT_STRING("{}"), 42), "42");
1944 #if FMT_USE_NONTYPE_TEMPLATE_ARGS
1945 using namespace fmt::literals
;
1946 EXPECT_EQ("foobar", fmt::format(FMT_STRING("{foo}{bar}"), "bar"_a
= "bar",
1948 EXPECT_EQ(fmt::format(FMT_STRING("")), "");
1949 EXPECT_EQ(fmt::format(FMT_STRING(""), "arg"_a
= 42), "");
1950 EXPECT_EQ(fmt::format(FMT_STRING("{answer}"), "answer"_a
= Answer()), "42");
1951 EXPECT_EQ(fmt::format(FMT_STRING("{} {two}"), 1, "two"_a
= 2), "1 2");
1954 (void)static_with_null
;
1955 (void)static_no_null
;
1957 EXPECT_EQ(fmt::format(FMT_STRING(static_with_null
), 42), "42");
1958 EXPECT_EQ(fmt::format(FMT_STRING(static_no_null
), 42), "42");
1963 #if FMT_CPLUSPLUS >= 201703L
1964 EXPECT_EQ(fmt::format(FMT_STRING(with_null
), 42), "42");
1965 EXPECT_EQ(fmt::format(FMT_STRING(no_null
), 42), "42");
1967 #if defined(FMT_USE_STRING_VIEW) && FMT_CPLUSPLUS >= 201703L
1968 EXPECT_EQ(fmt::format(FMT_STRING(std::string_view("{}")), 42), "42");
1972 TEST(format_test
, custom_format_compile_time_string
) {
1973 EXPECT_EQ(fmt::format(FMT_STRING("{}"), Answer()), "42");
1974 auto answer
= Answer();
1975 EXPECT_EQ(fmt::format(FMT_STRING("{}"), answer
), "42");
1977 fmt::format_to(buf
, FMT_STRING("{}"), answer
);
1978 const Answer const_answer
= Answer();
1979 EXPECT_EQ(fmt::format(FMT_STRING("{}"), const_answer
), "42");
1982 #if FMT_USE_USER_DEFINED_LITERALS
1983 TEST(format_test
, named_arg_udl
) {
1984 using namespace fmt::literals
;
1985 auto udl_a
= fmt::format("{first}{second}{first}{third}", "first"_a
= "abra",
1986 "second"_a
= "cad", "third"_a
= 99);
1988 fmt::format("{first}{second}{first}{third}", fmt::arg("first", "abra"),
1989 fmt::arg("second", "cad"), fmt::arg("third", 99)),
1992 EXPECT_EQ(fmt::format("{answer}", "answer"_a
= Answer()), "42");
1994 #endif // FMT_USE_USER_DEFINED_LITERALS
1996 TEST(format_test
, enum) { EXPECT_EQ(fmt::format("{}", foo
), "0"); }
1998 TEST(format_test
, formatter_not_specialized
) {
1999 static_assert(!fmt::has_formatter
<fmt::formatter
<test_enum
>,
2000 fmt::format_context
>::value
,
2004 #if FMT_HAS_FEATURE(cxx_strong_enums)
2005 enum big_enum
: unsigned long long { big_enum_value
= 5000000000ULL };
2006 auto format_as(big_enum e
) -> unsigned long long { return e
; }
2008 TEST(format_test
, strong_enum
) {
2009 EXPECT_EQ(fmt::format("{}", big_enum_value
), "5000000000");
2013 TEST(format_test
, non_null_terminated_format_string
) {
2014 EXPECT_EQ(fmt::format(string_view("{}foo", 2), 42), "42");
2017 namespace adl_test
{
2021 template <typename
, typename OutputIt
> void write(OutputIt
, foo
) = delete;
2022 } // namespace detail
2024 } // namespace adl_test
2028 struct formatter
<adl_test::fmt::detail::foo
> : formatter
<std::string
> {
2029 auto format(adl_test::fmt::detail::foo
, format_context
& ctx
) const
2030 -> decltype(ctx
.out()) {
2031 return formatter
<std::string
>::format("foo", ctx
);
2036 TEST(format_test
, to_string
) {
2037 EXPECT_EQ(fmt::to_string(42), "42");
2038 EXPECT_EQ(fmt::to_string(reinterpret_cast<void*>(0x1234)), "0x1234");
2039 EXPECT_EQ(fmt::to_string(adl_test::fmt::detail::foo()), "foo");
2040 EXPECT_EQ(fmt::to_string(foo
), "0");
2042 #if FMT_USE_FLOAT128
2043 EXPECT_EQ(fmt::to_string(__float128(0.5)), "0.5");
2046 #if defined(FMT_USE_STRING_VIEW) && FMT_CPLUSPLUS >= 201703L
2047 EXPECT_EQ(fmt::to_string(std::string_view()), "");
2051 TEST(format_test
, output_iterators
) {
2052 std::list
<char> out
;
2053 fmt::format_to(std::back_inserter(out
), "{}", 42);
2054 EXPECT_EQ("42", std::string(out
.begin(), out
.end()));
2055 std::stringstream s
;
2056 fmt::format_to(std::ostream_iterator
<char>(s
), "{}", 42);
2057 EXPECT_EQ("42", s
.str());
2060 TEST(format_test
, fill_via_appender
) {
2061 fmt::memory_buffer buf
;
2062 auto it
= fmt::appender(buf
);
2063 std::fill_n(it
, 3, '~');
2064 EXPECT_EQ(fmt::to_string(buf
), "~~~");
2067 TEST(format_test
, formatted_size
) {
2068 EXPECT_EQ(2u, fmt::formatted_size("{}", 42));
2069 EXPECT_EQ(2u, fmt::formatted_size(std::locale(), "{}", 42));
2072 TEST(format_test
, format_to_no_args
) {
2074 fmt::format_to(std::back_inserter(s
), "test");
2075 EXPECT_EQ("test", s
);
2078 TEST(format_test
, format_to
) {
2080 fmt::format_to(std::back_inserter(s
), "part{0}", 1);
2081 EXPECT_EQ("part1", s
);
2082 fmt::format_to(std::back_inserter(s
), "part{0}", 2);
2083 EXPECT_EQ("part1part2", s
);
2086 TEST(format_test
, format_to_memory_buffer
) {
2087 auto buf
= fmt::basic_memory_buffer
<char, 100>();
2088 fmt::format_to(fmt::appender(buf
), "{}", "foo");
2089 EXPECT_EQ("foo", to_string(buf
));
2092 TEST(format_test
, format_to_vector
) {
2093 std::vector
<char> v
;
2094 fmt::format_to(std::back_inserter(v
), "{}", "foo");
2095 EXPECT_EQ(string_view(v
.data(), v
.size()), "foo");
2098 struct nongrowing_container
{
2099 using value_type
= char;
2100 void push_back(char) { throw std::runtime_error("can't take it any more"); }
2103 TEST(format_test
, format_to_propagates_exceptions
) {
2104 auto c
= nongrowing_container();
2105 EXPECT_THROW(fmt::format_to(std::back_inserter(c
), "{}", 42),
2106 std::runtime_error
);
2109 TEST(format_test
, format_to_n
) {
2112 auto result
= fmt::format_to_n(buffer
, 3, "{}", 12345);
2113 EXPECT_EQ(5u, result
.size
);
2114 EXPECT_EQ(buffer
+ 3, result
.out
);
2115 EXPECT_EQ("123x", fmt::string_view(buffer
, 4));
2117 result
= fmt::format_to_n(buffer
, 3, "{:s}", "foobar");
2118 EXPECT_EQ(6u, result
.size
);
2119 EXPECT_EQ(buffer
+ 3, result
.out
);
2120 EXPECT_EQ("foox", fmt::string_view(buffer
, 4));
2125 result
= fmt::format_to_n(buffer
, 3, "{}", 'A');
2126 EXPECT_EQ(1u, result
.size
);
2127 EXPECT_EQ(buffer
+ 1, result
.out
);
2128 EXPECT_EQ("Axxx", fmt::string_view(buffer
, 4));
2130 result
= fmt::format_to_n(buffer
, 3, "{}{} ", 'B', 'C');
2131 EXPECT_EQ(3u, result
.size
);
2132 EXPECT_EQ(buffer
+ 3, result
.out
);
2133 EXPECT_EQ("BC x", fmt::string_view(buffer
, 4));
2135 result
= fmt::format_to_n(buffer
, 4, "{}", "ABCDE");
2136 EXPECT_EQ(5u, result
.size
);
2137 EXPECT_EQ("ABCD", fmt::string_view(buffer
, 4));
2140 result
= fmt::format_to_n(buffer
, 3, "{}", std::string(1000, '*'));
2141 EXPECT_EQ(1000u, result
.size
);
2142 EXPECT_EQ("***x", fmt::string_view(buffer
, 4));
2145 struct test_output_iterator
{
2148 using iterator_category
= std::output_iterator_tag
;
2149 using value_type
= void;
2150 using difference_type
= void;
2151 using pointer
= void;
2152 using reference
= void;
2154 auto operator++() -> test_output_iterator
& {
2158 auto operator++(int) -> test_output_iterator
{
2163 auto operator*() -> char& { return *data
; }
2166 TEST(format_test
, format_to_n_output_iterator
) {
2168 fmt::format_to_n(test_output_iterator
{buf
}, 10, "{}", 42);
2169 EXPECT_STREQ(buf
, "42");
2172 TEST(format_test
, vformat_to
) {
2173 using context
= fmt::format_context
;
2175 auto args
= fmt::make_format_args
<context
>(n
);
2176 auto s
= std::string();
2177 fmt::vformat_to(std::back_inserter(s
), "{}", args
);
2180 fmt::vformat_to(std::back_inserter(s
), FMT_STRING("{}"), args
);
2184 TEST(format_test
, char_traits_not_ambiguous
) {
2185 // Test that we don't inject detail names into the std namespace.
2186 using namespace std
;
2187 auto c
= char_traits
<char>::char_type();
2189 #if FMT_CPLUSPLUS >= 201103L
2190 auto s
= std::string();
2191 auto lval
= begin(s
);
2196 struct check_back_appender
{};
2199 template <> struct formatter
<check_back_appender
> {
2200 FMT_CONSTEXPR
auto parse(format_parse_context
& ctx
) -> decltype(ctx
.begin()) {
2204 template <typename Context
>
2205 auto format(check_back_appender
, Context
& ctx
) const -> decltype(ctx
.out()) {
2206 auto out
= ctx
.out();
2207 static_assert(std::is_same
<decltype(++out
), decltype(out
)&>::value
,
2208 "needs to satisfy weakly_incrementable");
2215 TEST(format_test
, back_insert_slicing
) {
2216 EXPECT_EQ(fmt::format("{}", check_back_appender
{}), "y");
2220 enum class scoped_enum_as_int
{};
2221 auto format_as(scoped_enum_as_int
) -> int { return 42; }
2223 enum class scoped_enum_as_string_view
{};
2224 auto format_as(scoped_enum_as_string_view
) -> fmt::string_view
{ return "foo"; }
2226 enum class scoped_enum_as_string
{};
2227 auto format_as(scoped_enum_as_string
) -> std::string
{ return "foo"; }
2229 struct struct_as_int
{};
2230 auto format_as(struct_as_int
) -> int { return 42; }
2232 struct struct_as_const_reference
{
2233 const std::string name
= "foo";
2235 auto format_as(const struct_as_const_reference
& s
) -> const std::string
& {
2240 TEST(format_test
, format_as
) {
2241 EXPECT_EQ(fmt::format("{}", test::scoped_enum_as_int()), "42");
2242 EXPECT_EQ(fmt::format("{}", test::scoped_enum_as_string_view()), "foo");
2243 EXPECT_EQ(fmt::format("{}", test::scoped_enum_as_string()), "foo");
2244 EXPECT_EQ(fmt::format("{}", test::struct_as_int()), "42");
2245 EXPECT_EQ(fmt::format("{}", test::struct_as_const_reference()), "foo");
2248 TEST(format_test
, format_as_to_string
) {
2249 EXPECT_EQ(fmt::to_string(test::scoped_enum_as_int()), "42");
2250 EXPECT_EQ(fmt::to_string(test::scoped_enum_as_string_view()), "foo");
2251 EXPECT_EQ(fmt::to_string(test::scoped_enum_as_string()), "foo");
2252 EXPECT_EQ(fmt::to_string(test::struct_as_int()), "42");
2255 template <typename Char
, typename T
> auto check_enabled_formatter() -> bool {
2256 static_assert(std::is_default_constructible
<fmt::formatter
<T
, Char
>>::value
,
2261 template <typename Char
, typename
... T
> void check_enabled_formatters() {
2262 auto dummy
= {check_enabled_formatter
<Char
, T
>()...};
2266 TEST(format_test
, test_formatters_enabled
) {
2267 using custom_string
=
2268 std::basic_string
<char, std::char_traits
<char>, mock_allocator
<char>>;
2269 using custom_wstring
= std::basic_string
<wchar_t, std::char_traits
<wchar_t>,
2270 mock_allocator
<wchar_t>>;
2272 check_enabled_formatters
<char, bool, char, signed char, unsigned char, short,
2273 unsigned short, int, unsigned, long, unsigned long,
2274 long long, unsigned long long, float, double,
2275 long double, void*, const void*, char*, const char*,
2276 std::string
, custom_string
, std::nullptr_t
>();
2277 check_enabled_formatters
<
2278 wchar_t, bool, wchar_t, signed char, unsigned char, short, unsigned short,
2279 int, unsigned, long, unsigned long, long long, unsigned long long, float,
2280 double, long double, void*, const void*, wchar_t*, const wchar_t*,
2281 std::wstring
, custom_wstring
, std::nullptr_t
>();
2284 TEST(format_int_test
, data
) {
2285 fmt::format_int
format_int(42);
2286 EXPECT_EQ(std::string(format_int
.data(), format_int
.size()), "42");
2289 TEST(format_int_test
, format_int
) {
2290 EXPECT_EQ(fmt::format_int(42).str(), "42");
2291 EXPECT_EQ(fmt::format_int(42).size(), 2u);
2292 EXPECT_EQ(fmt::format_int(-42).str(), "-42");
2293 EXPECT_EQ(fmt::format_int(-42).size(), 3u);
2294 EXPECT_EQ(fmt::format_int(42ul).str(), "42");
2295 EXPECT_EQ(fmt::format_int(-42l).str(), "-42");
2296 EXPECT_EQ(fmt::format_int(42ull).str(), "42");
2297 EXPECT_EQ(fmt::format_int(-42ll).str(), "-42");
2298 EXPECT_EQ(fmt::format_int(max_value
<int64_t>()).str(),
2299 std::to_string(max_value
<int64_t>()));
2302 #ifndef FMT_STATIC_THOUSANDS_SEPARATOR
2306 class format_facet
: public fmt::format_facet
<std::locale
> {
2308 struct int_formatter
{
2311 template <typename T
, FMT_ENABLE_IF(fmt::detail::is_integer
<T
>::value
)>
2312 auto operator()(T value
) -> bool {
2313 fmt::format_to(out
, "[{}]", value
);
2317 template <typename T
, FMT_ENABLE_IF(!fmt::detail::is_integer
<T
>::value
)>
2318 auto operator()(T
) -> bool {
2323 auto do_put(fmt::appender out
, fmt::loc_value val
,
2324 const fmt::format_specs
&) const -> bool override
;
2327 auto format_facet::do_put(fmt::appender out
, fmt::loc_value val
,
2328 const fmt::format_specs
&) const -> bool {
2329 return val
.visit(int_formatter
{out
});
2332 TEST(format_test
, format_facet
) {
2333 auto loc
= std::locale(std::locale(), new format_facet());
2334 EXPECT_EQ(fmt::format(loc
, "{:L}", 42), "[42]");
2335 EXPECT_EQ(fmt::format(loc
, "{:L}", -42), "[-42]");
2338 TEST(format_test
, format_facet_separator
) {
2339 // U+2019 RIGHT SINGLE QUOTATION MARK is a digit separator in the de_CH
2342 std::locale({}, new fmt::format_facet
<std::locale
>("\xe2\x80\x99"));
2343 EXPECT_EQ(fmt::format(loc
, "{:L}", 1000),
2348 TEST(format_test
, format_facet_grouping
) {
2350 std::locale({}, new fmt::format_facet
<std::locale
>(",", {1, 2, 3}));
2351 EXPECT_EQ(fmt::format(loc
, "{:L}", 1234567890), "1,234,567,89,0");
2354 TEST(format_test
, format_named_arg_with_locale
) {
2355 EXPECT_EQ(fmt::format(std::locale(), "{answer}", fmt::arg("answer", 42)),
2359 TEST(format_test
, format_locale
) {
2360 auto loc
= std::locale({}, new fmt::format_facet
<std::locale
>(","));
2361 EXPECT_EQ(fmt::format(loc
, "{:Lx}", 123456789), "7,5bc,d15");
2362 EXPECT_EQ(fmt::format(loc
, "{:#Lb}", -123456789),
2363 "-0b111,010,110,111,100,110,100,010,101");
2364 EXPECT_EQ(fmt::format(loc
, "{:10Lo}", 12345), " 30,071");
2367 #endif // FMT_STATIC_THOUSANDS_SEPARATOR
2369 struct convertible_to_nonconst_cstring
{
2370 operator char*() const {
2371 static char c
[] = "bar";
2378 struct formatter
<convertible_to_nonconst_cstring
> : formatter
<char*> {};
2381 TEST(format_test
, formatter_nonconst_char
) {
2382 EXPECT_EQ(fmt::format("{}", convertible_to_nonconst_cstring()), "bar");
2385 namespace adl_test
{
2386 template <typename
... T
> void make_format_args(const T
&...) = delete;
2388 struct string
: std::string
{};
2389 } // namespace adl_test
2391 // Test that formatting functions compile when make_format_args is found by ADL.
2392 TEST(format_test
, adl
) {
2393 // Only check compilation and don't run the code to avoid polluting the output
2394 // and since the output is tested elsewhere.
2395 if (fmt::detail::const_check(true)) return;
2396 auto s
= adl_test::string();
2398 (void)fmt::format("{}", s
);
2399 fmt::format_to(buf
, "{}", s
);
2400 fmt::format_to_n(buf
, 10, "{}", s
);
2401 (void)fmt::formatted_size("{}", s
);
2402 fmt::print("{}", s
);
2403 fmt::print(stdout
, "{}", s
);
2406 struct convertible_to_int
{
2407 operator int() const { return 42; }
2410 struct convertible_to_cstring
{
2411 operator const char*() const { return "foo"; }
2415 template <> struct formatter
<convertible_to_int
> {
2416 FMT_CONSTEXPR
auto parse(format_parse_context
& ctx
) -> decltype(ctx
.begin()) {
2419 auto format(convertible_to_int
, format_context
& ctx
) const
2420 -> decltype(ctx
.out()) {
2421 auto out
= ctx
.out();
2427 template <> struct formatter
<convertible_to_cstring
> {
2428 FMT_CONSTEXPR
auto parse(format_parse_context
& ctx
) -> decltype(ctx
.begin()) {
2431 auto format(convertible_to_cstring
, format_context
& ctx
) const
2432 -> decltype(ctx
.out()) {
2433 auto out
= ctx
.out();
2440 TEST(format_test
, formatter_overrides_implicit_conversion
) {
2441 EXPECT_EQ(fmt::format("{}", convertible_to_int()), "x");
2442 EXPECT_EQ(fmt::format("{}", convertible_to_cstring()), "y");
2446 using value_type
= unsigned;
2448 auto find_first_of(value_type
, size_t) const -> size_t;
2450 auto data() const -> const char*;
2451 auto size() const -> size_t;
2455 template <> struct formatter
<ustring
> : formatter
<std::string
> {
2456 auto format(const ustring
&, format_context
& ctx
) const
2457 -> decltype(ctx
.out()) {
2458 return formatter
<std::string
>::format("ustring", ctx
);
2463 TEST(format_test
, ustring
) {
2464 EXPECT_EQ(fmt::format("{}", ustring()), "ustring");