Include fmt 11.0.2
[openal-soft.git] / fmt-11.0.2 / test / printf-test.cc
blobee5cf0b0e0a11d775be1391c5c1d86b4dda9545d
1 // Formatting library for C++ - printf tests
2 //
3 // Copyright (c) 2012 - present, Victor Zverovich
4 // All rights reserved.
5 //
6 // For the license information refer to format.h.
8 #include "fmt/printf.h"
9 // include <format> if possible for https://github.com/fmtlib/fmt/pull/4042
10 #if FMT_HAS_INCLUDE(<format>) && FMT_CPLUSPLUS > 201703L
11 # include <format>
12 #endif
14 #include <cctype>
15 #include <climits>
16 #include <cstring>
18 #include "fmt/xchar.h"
19 #include "gtest-extra.h"
20 #include "util.h"
22 using fmt::format;
23 using fmt::format_error;
24 using fmt::detail::max_value;
26 const unsigned big_num = INT_MAX + 1u;
28 // Makes format string argument positional.
29 static std::string make_positional(fmt::string_view format) {
30 std::string s(format.data(), format.size());
31 s.replace(s.find('%'), 1, "%1$");
32 return s;
35 static std::wstring make_positional(fmt::basic_string_view<wchar_t> format) {
36 std::wstring s(format.data(), format.size());
37 s.replace(s.find(L'%'), 1, L"%1$");
38 return s;
41 // A wrapper around fmt::sprintf to workaround bogus warnings about invalid
42 // format strings in MSVC.
43 template <typename... Args>
44 std::string test_sprintf(fmt::string_view format, const Args&... args) {
45 return fmt::sprintf(format, args...);
47 template <typename... Args>
48 std::wstring test_sprintf(fmt::basic_string_view<wchar_t> format,
49 const Args&... args) {
50 return fmt::sprintf(format, args...);
53 #define EXPECT_PRINTF(expected_output, format, arg) \
54 EXPECT_EQ(expected_output, test_sprintf(format, arg)) \
55 << "format: " << format; \
56 EXPECT_EQ(expected_output, fmt::sprintf(make_positional(format), arg))
58 TEST(printf_test, no_args) {
59 EXPECT_EQ("test", test_sprintf("test"));
60 EXPECT_EQ(L"test", fmt::sprintf(L"test"));
63 TEST(printf_test, escape) {
64 EXPECT_EQ("%", test_sprintf("%%"));
65 EXPECT_EQ("before %", test_sprintf("before %%"));
66 EXPECT_EQ("% after", test_sprintf("%% after"));
67 EXPECT_EQ("before % after", test_sprintf("before %% after"));
68 EXPECT_EQ("%s", test_sprintf("%%s"));
69 EXPECT_EQ(L"%", fmt::sprintf(L"%%"));
70 EXPECT_EQ(L"before %", fmt::sprintf(L"before %%"));
71 EXPECT_EQ(L"% after", fmt::sprintf(L"%% after"));
72 EXPECT_EQ(L"before % after", fmt::sprintf(L"before %% after"));
73 EXPECT_EQ(L"%s", fmt::sprintf(L"%%s"));
76 TEST(printf_test, positional_args) {
77 EXPECT_EQ("42", test_sprintf("%1$d", 42));
78 EXPECT_EQ("before 42", test_sprintf("before %1$d", 42));
79 EXPECT_EQ("42 after", test_sprintf("%1$d after", 42));
80 EXPECT_EQ("before 42 after", test_sprintf("before %1$d after", 42));
81 EXPECT_EQ("answer = 42", test_sprintf("%1$s = %2$d", "answer", 42));
82 EXPECT_EQ("42 is the answer", test_sprintf("%2$d is the %1$s", "answer", 42));
83 EXPECT_EQ("abracadabra", test_sprintf("%1$s%2$s%1$s", "abra", "cad"));
86 TEST(printf_test, automatic_arg_indexing) {
87 EXPECT_EQ("abc", test_sprintf("%c%c%c", 'a', 'b', 'c'));
90 TEST(printf_test, number_is_too_big_in_arg_index) {
91 EXPECT_THROW_MSG(test_sprintf(format("%{}$", big_num)), format_error,
92 "argument not found");
93 EXPECT_THROW_MSG(test_sprintf(format("%{}$d", big_num)), format_error,
94 "argument not found");
97 TEST(printf_test, switch_arg_indexing) {
98 EXPECT_THROW_MSG(test_sprintf("%1$d%", 1, 2), format_error,
99 "cannot switch from manual to automatic argument indexing");
100 EXPECT_THROW_MSG(test_sprintf(format("%1$d%{}d", big_num), 1, 2),
101 format_error, "number is too big");
102 EXPECT_THROW_MSG(test_sprintf("%1$d%d", 1, 2), format_error,
103 "cannot switch from manual to automatic argument indexing");
105 EXPECT_THROW_MSG(test_sprintf("%d%1$", 1, 2), format_error,
106 "cannot switch from automatic to manual argument indexing");
107 EXPECT_THROW_MSG(test_sprintf(format("%d%{}$d", big_num), 1, 2), format_error,
108 "cannot switch from automatic to manual argument indexing");
109 EXPECT_THROW_MSG(test_sprintf("%d%1$d", 1, 2), format_error,
110 "cannot switch from automatic to manual argument indexing");
112 // Indexing errors override width errors.
113 EXPECT_THROW_MSG(test_sprintf(format("%d%1${}d", big_num), 1, 2),
114 format_error, "number is too big");
115 EXPECT_THROW_MSG(test_sprintf(format("%1$d%{}d", big_num), 1, 2),
116 format_error, "number is too big");
119 TEST(printf_test, invalid_arg_index) {
120 EXPECT_THROW_MSG(test_sprintf("%0$d", 42), format_error,
121 "argument not found");
122 EXPECT_THROW_MSG(test_sprintf("%2$d", 42), format_error,
123 "argument not found");
124 EXPECT_THROW_MSG(test_sprintf(format("%{}$d", INT_MAX), 42), format_error,
125 "argument not found");
127 EXPECT_THROW_MSG(test_sprintf("%2$", 42), format_error, "argument not found");
128 EXPECT_THROW_MSG(test_sprintf(format("%{}$d", big_num), 42), format_error,
129 "argument not found");
132 TEST(printf_test, default_align_right) {
133 EXPECT_PRINTF(" 42", "%5d", 42);
134 EXPECT_PRINTF(" abc", "%5s", "abc");
137 TEST(printf_test, zero_flag) {
138 EXPECT_PRINTF("00042", "%05d", 42);
139 EXPECT_PRINTF("-0042", "%05d", -42);
141 EXPECT_PRINTF("00042", "%05d", 42);
142 EXPECT_PRINTF("-0042", "%05d", -42);
143 EXPECT_PRINTF("-004.2", "%06g", -4.2);
145 EXPECT_PRINTF("+00042", "%00+6d", 42);
147 EXPECT_PRINTF(" 42", "%05.d", 42);
148 EXPECT_PRINTF(" 0042", "%05.4d", 42);
150 // '0' flag is ignored for non-numeric types.
151 EXPECT_PRINTF(" x", "%05c", 'x');
154 TEST(printf_test, plus_flag) {
155 EXPECT_PRINTF("+42", "%+d", 42);
156 EXPECT_PRINTF("-42", "%+d", -42);
157 EXPECT_PRINTF("+0042", "%+05d", 42);
158 EXPECT_PRINTF("+0042", "%0++5d", 42);
160 // '+' flag is ignored for non-numeric types.
161 EXPECT_PRINTF("x", "%+c", 'x');
163 // '+' flag wins over space flag
164 EXPECT_PRINTF("+42", "%+ d", 42);
165 EXPECT_PRINTF("-42", "%+ d", -42);
166 EXPECT_PRINTF("+42", "% +d", 42);
167 EXPECT_PRINTF("-42", "% +d", -42);
168 EXPECT_PRINTF("+0042", "% +05d", 42);
169 EXPECT_PRINTF("+0042", "%0+ 5d", 42);
171 // '+' flag and space flag are both ignored for non-numeric types.
172 EXPECT_PRINTF("x", "%+ c", 'x');
173 EXPECT_PRINTF("x", "% +c", 'x');
176 TEST(printf_test, minus_flag) {
177 EXPECT_PRINTF("abc ", "%-5s", "abc");
178 EXPECT_PRINTF("abc ", "%0--5s", "abc");
180 EXPECT_PRINTF("7 ", "%-5d", 7);
181 EXPECT_PRINTF("97 ", "%-5hhi", 'a');
182 EXPECT_PRINTF("a ", "%-5c", 'a');
184 // '0' flag is ignored if '-' flag is given
185 EXPECT_PRINTF("7 ", "%-05d", 7);
186 EXPECT_PRINTF("7 ", "%0-5d", 7);
187 EXPECT_PRINTF("a ", "%-05c", 'a');
188 EXPECT_PRINTF("a ", "%0-5c", 'a');
189 EXPECT_PRINTF("97 ", "%-05hhi", 'a');
190 EXPECT_PRINTF("97 ", "%0-5hhi", 'a');
192 // '-' and space flag don't interfere
193 EXPECT_PRINTF(" 42", "%- d", 42);
196 TEST(printf_test, space_flag) {
197 EXPECT_PRINTF(" 42", "% d", 42);
198 EXPECT_PRINTF("-42", "% d", -42);
199 EXPECT_PRINTF(" 0042", "% 05d", 42);
200 EXPECT_PRINTF(" 0042", "%0 5d", 42);
202 // ' ' flag is ignored for non-numeric types.
203 EXPECT_PRINTF("x", "% c", 'x');
206 TEST(printf_test, hash_flag) {
207 EXPECT_PRINTF("042", "%#o", 042);
208 EXPECT_PRINTF(fmt::format("0{:o}", static_cast<unsigned>(-042)), "%#o", -042);
209 EXPECT_PRINTF("0", "%#o", 0);
211 EXPECT_PRINTF("0x42", "%#x", 0x42);
212 EXPECT_PRINTF("0X42", "%#X", 0x42);
213 EXPECT_PRINTF(fmt::format("0x{:x}", static_cast<unsigned>(-0x42)), "%#x",
214 -0x42);
215 EXPECT_PRINTF("0", "%#x", 0);
217 EXPECT_PRINTF("0x0042", "%#06x", 0x42);
218 EXPECT_PRINTF("0x0042", "%0##6x", 0x42);
220 EXPECT_PRINTF("-42.000000", "%#f", -42.0);
221 EXPECT_PRINTF("-42.000000", "%#F", -42.0);
223 char buffer[256];
224 safe_sprintf(buffer, "%#e", -42.0);
225 EXPECT_PRINTF(buffer, "%#e", -42.0);
226 safe_sprintf(buffer, "%#E", -42.0);
227 EXPECT_PRINTF(buffer, "%#E", -42.0);
229 EXPECT_PRINTF("-42.0000", "%#g", -42.0);
230 EXPECT_PRINTF("-42.0000", "%#G", -42.0);
232 EXPECT_PRINTF("0x1.p+4", "%#a", 16.0);
233 EXPECT_PRINTF("0X1.P+4", "%#A", 16.0);
235 // '#' flag is ignored for non-numeric types.
236 EXPECT_PRINTF("x", "%#c", 'x');
239 TEST(printf_test, width) {
240 EXPECT_PRINTF(" abc", "%5s", "abc");
242 // Width cannot be specified twice.
243 EXPECT_THROW_MSG(test_sprintf("%5-5d", 42), format_error,
244 "invalid format specifier");
246 EXPECT_THROW_MSG(test_sprintf(format("%{}d", big_num), 42), format_error,
247 "number is too big");
248 EXPECT_THROW_MSG(test_sprintf(format("%1${}d", big_num), 42), format_error,
249 "number is too big");
252 TEST(printf_test, dynamic_width) {
253 EXPECT_EQ(" 42", test_sprintf("%*d", 5, 42));
254 EXPECT_EQ("42 ", test_sprintf("%*d", -5, 42));
255 EXPECT_THROW_MSG(test_sprintf("%*d", 5.0, 42), format_error,
256 "width is not integer");
257 EXPECT_THROW_MSG(test_sprintf("%*d"), format_error, "argument not found");
258 EXPECT_THROW_MSG(test_sprintf("%*d", big_num, 42), format_error,
259 "number is too big");
262 TEST(printf_test, int_precision) {
263 EXPECT_PRINTF("00042", "%.5d", 42);
264 EXPECT_PRINTF("-00042", "%.5d", -42);
265 EXPECT_PRINTF("00042", "%.5x", 0x42);
266 EXPECT_PRINTF("0x00042", "%#.5x", 0x42);
267 EXPECT_PRINTF("00042", "%.5o", 042);
268 EXPECT_PRINTF("00042", "%#.5o", 042);
270 EXPECT_PRINTF(" 00042", "%7.5d", 42);
271 EXPECT_PRINTF(" 00042", "%7.5x", 0x42);
272 EXPECT_PRINTF(" 0x00042", "%#10.5x", 0x42);
273 EXPECT_PRINTF(" 00042", "%7.5o", 042);
274 EXPECT_PRINTF(" 00042", "%#10.5o", 042);
276 EXPECT_PRINTF("00042 ", "%-7.5d", 42);
277 EXPECT_PRINTF("00042 ", "%-7.5x", 0x42);
278 EXPECT_PRINTF("0x00042 ", "%-#10.5x", 0x42);
279 EXPECT_PRINTF("00042 ", "%-7.5o", 042);
280 EXPECT_PRINTF("00042 ", "%-#10.5o", 042);
283 TEST(printf_test, float_precision) {
284 char buffer[256];
285 safe_sprintf(buffer, "%.3e", 1234.5678);
286 EXPECT_PRINTF(buffer, "%.3e", 1234.5678);
287 EXPECT_PRINTF("1234.568", "%.3f", 1234.5678);
288 EXPECT_PRINTF("1.23e+03", "%.3g", 1234.5678);
289 safe_sprintf(buffer, "%.3a", 1234.5678);
290 EXPECT_PRINTF(buffer, "%.3a", 1234.5678);
293 TEST(printf_test, string_precision) {
294 char test[] = {'H', 'e', 'l', 'l', 'o'};
295 EXPECT_EQ(fmt::sprintf("%.4s", test), "Hell");
298 TEST(printf_test, ignore_precision_for_non_numeric_arg) {
299 EXPECT_PRINTF("abc", "%.5s", "abc");
302 TEST(printf_test, dynamic_precision) {
303 EXPECT_EQ("00042", test_sprintf("%.*d", 5, 42));
304 EXPECT_EQ("42", test_sprintf("%.*d", -5, 42));
305 EXPECT_THROW_MSG(test_sprintf("%.*d", 5.0, 42), format_error,
306 "precision is not integer");
307 EXPECT_THROW_MSG(test_sprintf("%.*d"), format_error, "argument not found");
308 EXPECT_THROW_MSG(test_sprintf("%.*d", big_num, 42), format_error,
309 "number is too big");
310 if (sizeof(long long) != sizeof(int)) {
311 long long prec = static_cast<long long>(INT_MIN) - 1;
312 EXPECT_THROW_MSG(test_sprintf("%.*d", prec, 42), format_error,
313 "number is too big");
317 template <typename T> struct make_signed {
318 using type = T;
321 #define SPECIALIZE_MAKE_SIGNED(T, S) \
322 template <> struct make_signed<T> { \
323 using type = S; \
326 SPECIALIZE_MAKE_SIGNED(char, signed char);
327 SPECIALIZE_MAKE_SIGNED(unsigned char, signed char);
328 SPECIALIZE_MAKE_SIGNED(unsigned short, short);
329 SPECIALIZE_MAKE_SIGNED(unsigned, int);
330 SPECIALIZE_MAKE_SIGNED(unsigned long, long);
331 SPECIALIZE_MAKE_SIGNED(unsigned long long, long long);
333 // Test length format specifier ``length_spec``.
334 template <typename T, typename U>
335 void test_length(const char* length_spec, U value) {
336 long long signed_value = 0;
337 unsigned long long unsigned_value = 0;
338 // Apply integer promotion to the argument.
339 unsigned long long max = max_value<U>();
340 using fmt::detail::const_check;
341 if (const_check(max <= static_cast<unsigned>(max_value<int>()))) {
342 signed_value = static_cast<int>(value);
343 unsigned_value = static_cast<unsigned long long>(value);
344 } else if (const_check(max <= max_value<unsigned>())) {
345 signed_value = static_cast<unsigned>(value);
346 unsigned_value = static_cast<unsigned long long>(value);
348 if (sizeof(U) <= sizeof(int) && sizeof(int) < sizeof(T)) {
349 signed_value = static_cast<long long>(value);
350 unsigned_value = static_cast<unsigned long long>(
351 static_cast<typename std::make_unsigned<unsigned>::type>(value));
352 } else {
353 signed_value = static_cast<typename make_signed<T>::type>(value);
354 unsigned_value = static_cast<typename std::make_unsigned<T>::type>(value);
356 std::ostringstream os;
357 os << signed_value;
358 EXPECT_PRINTF(os.str(), fmt::format("%{}d", length_spec), value);
359 EXPECT_PRINTF(os.str(), fmt::format("%{}i", length_spec), value);
360 os.str("");
361 os << unsigned_value;
362 EXPECT_PRINTF(os.str(), fmt::format("%{}u", length_spec), value);
363 os.str("");
364 os << std::oct << unsigned_value;
365 EXPECT_PRINTF(os.str(), fmt::format("%{}o", length_spec), value);
366 os.str("");
367 os << std::hex << unsigned_value;
368 EXPECT_PRINTF(os.str(), fmt::format("%{}x", length_spec), value);
369 os.str("");
370 os << std::hex << std::uppercase << unsigned_value;
371 EXPECT_PRINTF(os.str(), fmt::format("%{}X", length_spec), value);
374 template <typename T> void test_length(const char* length_spec) {
375 T min = std::numeric_limits<T>::min(), max = max_value<T>();
376 test_length<T>(length_spec, 42);
377 test_length<T>(length_spec, -42);
378 test_length<T>(length_spec, min);
379 test_length<T>(length_spec, max);
380 long long long_long_min = std::numeric_limits<long long>::min();
381 if (static_cast<long long>(min) > long_long_min)
382 test_length<T>(length_spec, static_cast<long long>(min) - 1);
383 unsigned long long long_long_max = max_value<long long>();
384 if (static_cast<unsigned long long>(max) < long_long_max)
385 test_length<T>(length_spec, static_cast<long long>(max) + 1);
386 test_length<T>(length_spec, std::numeric_limits<short>::min());
387 test_length<T>(length_spec, max_value<unsigned short>());
388 test_length<T>(length_spec, std::numeric_limits<int>::min());
389 test_length<T>(length_spec, max_value<int>());
390 test_length<T>(length_spec, std::numeric_limits<unsigned>::min());
391 test_length<T>(length_spec, max_value<unsigned>());
392 test_length<T>(length_spec, std::numeric_limits<long long>::min());
393 test_length<T>(length_spec, max_value<long long>());
394 test_length<T>(length_spec, std::numeric_limits<unsigned long long>::min());
395 test_length<T>(length_spec, max_value<unsigned long long>());
398 TEST(printf_test, length) {
399 test_length<char>("hh");
400 test_length<signed char>("hh");
401 test_length<unsigned char>("hh");
402 test_length<short>("h");
403 test_length<unsigned short>("h");
404 test_length<long>("l");
405 test_length<unsigned long>("l");
406 test_length<long long>("ll");
407 test_length<unsigned long long>("ll");
408 test_length<intmax_t>("j");
409 test_length<size_t>("z");
410 test_length<std::ptrdiff_t>("t");
411 long double max = max_value<long double>();
412 EXPECT_PRINTF(fmt::format("{:.6}", max), "%g", max);
413 EXPECT_PRINTF(fmt::format("{:.6}", max), "%Lg", max);
416 TEST(printf_test, bool) { EXPECT_PRINTF("1", "%d", true); }
418 TEST(printf_test, int) {
419 EXPECT_PRINTF("-42", "%d", -42);
420 EXPECT_PRINTF("-42", "%i", -42);
421 unsigned u = 0 - 42u;
422 EXPECT_PRINTF(fmt::format("{}", u), "%u", -42);
423 EXPECT_PRINTF(fmt::format("{:o}", u), "%o", -42);
424 EXPECT_PRINTF(fmt::format("{:x}", u), "%x", -42);
425 EXPECT_PRINTF(fmt::format("{:X}", u), "%X", -42);
428 TEST(printf_test, long_long) {
429 // fmt::printf allows passing long long arguments to %d without length
430 // specifiers.
431 long long max = max_value<long long>();
432 EXPECT_PRINTF(fmt::format("{}", max), "%d", max);
435 TEST(printf_test, float) {
436 EXPECT_PRINTF("392.650000", "%f", 392.65);
437 EXPECT_PRINTF("392.65", "%.2f", 392.65);
438 EXPECT_PRINTF("392.6", "%.1f", 392.65);
439 EXPECT_PRINTF("393", "%.f", 392.65);
440 EXPECT_PRINTF("392.650000", "%F", 392.65);
441 char buffer[256];
442 safe_sprintf(buffer, "%e", 392.65);
443 EXPECT_PRINTF(buffer, "%e", 392.65);
444 safe_sprintf(buffer, "%E", 392.65);
445 EXPECT_PRINTF(buffer, "%E", 392.65);
446 EXPECT_PRINTF("392.65", "%g", 392.65);
447 EXPECT_PRINTF("392.65", "%G", 392.65);
448 EXPECT_PRINTF("392", "%g", 392.0);
449 EXPECT_PRINTF("392", "%G", 392.0);
450 EXPECT_PRINTF("4.56e-07", "%g", 0.000000456);
451 safe_sprintf(buffer, "%a", -392.65);
452 EXPECT_EQ(buffer, format("{:a}", -392.65));
453 safe_sprintf(buffer, "%A", -392.65);
454 EXPECT_EQ(buffer, format("{:A}", -392.65));
457 TEST(printf_test, inf) {
458 double inf = std::numeric_limits<double>::infinity();
459 for (const char* type = "fega"; *type; ++type) {
460 EXPECT_PRINTF("inf", fmt::format("%{}", *type), inf);
461 char upper = static_cast<char>(std::toupper(*type));
462 EXPECT_PRINTF("INF", fmt::format("%{}", upper), inf);
466 TEST(printf_test, char) {
467 EXPECT_PRINTF("x", "%c", 'x');
468 int max = max_value<int>();
469 EXPECT_PRINTF(fmt::format("{}", static_cast<char>(max)), "%c", max);
470 // EXPECT_PRINTF("x", "%lc", L'x');
471 EXPECT_PRINTF(L"x", L"%c", L'x');
472 EXPECT_PRINTF(fmt::format(L"{}", static_cast<wchar_t>(max)), L"%c", max);
475 TEST(printf_test, string) {
476 EXPECT_PRINTF("abc", "%s", "abc");
477 const char* null_str = nullptr;
478 EXPECT_PRINTF("(null)", "%s", null_str);
479 EXPECT_PRINTF(" (null)", "%10s", null_str);
480 EXPECT_PRINTF(L"abc", L"%s", L"abc");
481 const wchar_t* null_wstr = nullptr;
482 EXPECT_PRINTF(L"(null)", L"%s", null_wstr);
483 EXPECT_PRINTF(L" (null)", L"%10s", null_wstr);
486 TEST(printf_test, pointer) {
487 int n;
488 void* p = &n;
489 EXPECT_PRINTF(fmt::format("{}", p), "%p", p);
490 p = nullptr;
491 EXPECT_PRINTF("(nil)", "%p", p);
492 EXPECT_PRINTF(" (nil)", "%10p", p);
493 const char* s = "test";
494 EXPECT_PRINTF(fmt::format("{:p}", s), "%p", s);
495 const char* null_str = nullptr;
496 EXPECT_PRINTF("(nil)", "%p", null_str);
498 p = &n;
499 EXPECT_PRINTF(fmt::format(L"{}", p), L"%p", p);
500 p = nullptr;
501 EXPECT_PRINTF(L"(nil)", L"%p", p);
502 EXPECT_PRINTF(L" (nil)", L"%10p", p);
503 const wchar_t* w = L"test";
504 EXPECT_PRINTF(fmt::format(L"{:p}", w), L"%p", w);
505 const wchar_t* null_wstr = nullptr;
506 EXPECT_PRINTF(L"(nil)", L"%p", null_wstr);
509 enum test_enum { answer = 42 };
510 auto format_as(test_enum e) -> int { return e; }
512 TEST(printf_test, enum) {
513 EXPECT_PRINTF("42", "%d", answer);
514 volatile test_enum volatile_enum = answer;
515 EXPECT_PRINTF("42", "%d", volatile_enum);
518 #if FMT_USE_FCNTL
519 TEST(printf_test, examples) {
520 const char* weekday = "Thursday";
521 const char* month = "August";
522 int day = 21;
523 EXPECT_WRITE(stdout, fmt::printf("%1$s, %3$d %2$s", weekday, month, day),
524 "Thursday, 21 August");
527 TEST(printf_test, printf_error) {
528 auto pipe = fmt::pipe();
529 int result = fmt::fprintf(pipe.read_end.fdopen("r").get(), "test");
530 EXPECT_LT(result, 0);
532 #endif
534 TEST(printf_test, wide_string) {
535 EXPECT_EQ(L"abc", fmt::sprintf(L"%s", L"abc"));
538 TEST(printf_test, vprintf) {
539 int n = 42;
540 auto store = fmt::make_format_args<fmt::printf_context>(n);
541 auto args = fmt::basic_format_args<fmt::printf_context>(store);
542 EXPECT_EQ(fmt::vsprintf(fmt::string_view("%d"), args), "42");
543 EXPECT_WRITE(stdout, fmt::vfprintf(stdout, fmt::string_view("%d"), args),
544 "42");
547 template <typename... Args>
548 void check_format_string_regression(fmt::string_view s, const Args&... args) {
549 fmt::sprintf(s, args...);
552 TEST(printf_test, check_format_string_regression) {
553 check_format_string_regression("%c%s", 'x', "");
556 TEST(printf_test, fixed_large_exponent) {
557 EXPECT_EQ("1000000000000000000000", fmt::sprintf("%.*f", -13, 1e21));
560 TEST(printf_test, make_printf_args) {
561 int n = 42;
562 EXPECT_EQ("[42] something happened",
563 fmt::vsprintf(fmt::string_view("[%d] %s happened"),
564 {fmt::make_printf_args(n, "something")}));
565 EXPECT_EQ(L"[42] something happened",
566 fmt::vsprintf(fmt::basic_string_view<wchar_t>(L"[%d] %s happened"),
567 {fmt::make_printf_args<wchar_t>(n, L"something")}));