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.
7
8 #include "fmt/printf.h"
9
10 #include <cctype>
11 #include <climits>
12 #include <cstring>
13
14 #include "fmt/core.h"
15 #include "gtest-extra.h"
16 #include "util.h"
17
18 using fmt::format;
19 using fmt::format_error;
20 using fmt::detail::max_value;
21
22 const unsigned BIG_NUM = INT_MAX + 1u;
23
24 // Makes format string argument positional.
make_positional(fmt::string_view format)25 static std::string make_positional(fmt::string_view format) {
26 std::string s(format.data(), format.size());
27 s.replace(s.find('%'), 1, "%1$");
28 return s;
29 }
30
make_positional(fmt::wstring_view format)31 static std::wstring make_positional(fmt::wstring_view format) {
32 std::wstring s(format.data(), format.size());
33 s.replace(s.find(L'%'), 1, L"%1$");
34 return s;
35 }
36
37 // A wrapper around fmt::sprintf to workaround bogus warnings about invalid
38 // format strings in MSVC.
39 template <typename... Args>
test_sprintf(fmt::string_view format,const Args &...args)40 std::string test_sprintf(fmt::string_view format, const Args&... args) {
41 return fmt::sprintf(format, args...);
42 }
43 template <typename... Args>
test_sprintf(fmt::wstring_view format,const Args &...args)44 std::wstring test_sprintf(fmt::wstring_view format, const Args&... args) {
45 return fmt::sprintf(format, args...);
46 }
47
48 #define EXPECT_PRINTF(expected_output, format, arg) \
49 EXPECT_EQ(expected_output, test_sprintf(format, arg)) \
50 << "format: " << format; \
51 EXPECT_EQ(expected_output, fmt::sprintf(make_positional(format), arg))
52
TEST(PrintfTest,NoArgs)53 TEST(PrintfTest, NoArgs) {
54 EXPECT_EQ("test", test_sprintf("test"));
55 EXPECT_EQ(L"test", fmt::sprintf(L"test"));
56 }
57
TEST(PrintfTest,Escape)58 TEST(PrintfTest, Escape) {
59 EXPECT_EQ("%", test_sprintf("%%"));
60 EXPECT_EQ("before %", test_sprintf("before %%"));
61 EXPECT_EQ("% after", test_sprintf("%% after"));
62 EXPECT_EQ("before % after", test_sprintf("before %% after"));
63 EXPECT_EQ("%s", test_sprintf("%%s"));
64 EXPECT_EQ(L"%", fmt::sprintf(L"%%"));
65 EXPECT_EQ(L"before %", fmt::sprintf(L"before %%"));
66 EXPECT_EQ(L"% after", fmt::sprintf(L"%% after"));
67 EXPECT_EQ(L"before % after", fmt::sprintf(L"before %% after"));
68 EXPECT_EQ(L"%s", fmt::sprintf(L"%%s"));
69 }
70
TEST(PrintfTest,PositionalArgs)71 TEST(PrintfTest, PositionalArgs) {
72 EXPECT_EQ("42", test_sprintf("%1$d", 42));
73 EXPECT_EQ("before 42", test_sprintf("before %1$d", 42));
74 EXPECT_EQ("42 after", test_sprintf("%1$d after", 42));
75 EXPECT_EQ("before 42 after", test_sprintf("before %1$d after", 42));
76 EXPECT_EQ("answer = 42", test_sprintf("%1$s = %2$d", "answer", 42));
77 EXPECT_EQ("42 is the answer", test_sprintf("%2$d is the %1$s", "answer", 42));
78 EXPECT_EQ("abracadabra", test_sprintf("%1$s%2$s%1$s", "abra", "cad"));
79 }
80
TEST(PrintfTest,AutomaticArgIndexing)81 TEST(PrintfTest, AutomaticArgIndexing) {
82 EXPECT_EQ("abc", test_sprintf("%c%c%c", 'a', 'b', 'c'));
83 }
84
TEST(PrintfTest,NumberIsTooBigInArgIndex)85 TEST(PrintfTest, NumberIsTooBigInArgIndex) {
86 EXPECT_THROW_MSG(test_sprintf(format("%{}$", BIG_NUM)), format_error,
87 "number is too big");
88 EXPECT_THROW_MSG(test_sprintf(format("%{}$d", BIG_NUM)), format_error,
89 "number is too big");
90 }
91
TEST(PrintfTest,SwitchArgIndexing)92 TEST(PrintfTest, SwitchArgIndexing) {
93 EXPECT_THROW_MSG(test_sprintf("%1$d%", 1, 2), format_error,
94 "cannot switch from manual to automatic argument indexing");
95 EXPECT_THROW_MSG(test_sprintf(format("%1$d%{}d", BIG_NUM), 1, 2),
96 format_error, "number is too big");
97 EXPECT_THROW_MSG(test_sprintf("%1$d%d", 1, 2), format_error,
98 "cannot switch from manual to automatic argument indexing");
99
100 EXPECT_THROW_MSG(test_sprintf("%d%1$", 1, 2), format_error,
101 "cannot switch from automatic to manual argument indexing");
102 EXPECT_THROW_MSG(test_sprintf(format("%d%{}$d", BIG_NUM), 1, 2), format_error,
103 "number is too big");
104 EXPECT_THROW_MSG(test_sprintf("%d%1$d", 1, 2), format_error,
105 "cannot switch from automatic to manual argument indexing");
106
107 // Indexing errors override width errors.
108 EXPECT_THROW_MSG(test_sprintf(format("%d%1${}d", BIG_NUM), 1, 2),
109 format_error, "number is too big");
110 EXPECT_THROW_MSG(test_sprintf(format("%1$d%{}d", BIG_NUM), 1, 2),
111 format_error, "number is too big");
112 }
113
TEST(PrintfTest,InvalidArgIndex)114 TEST(PrintfTest, InvalidArgIndex) {
115 EXPECT_THROW_MSG(test_sprintf("%0$d", 42), format_error,
116 "argument not found");
117 EXPECT_THROW_MSG(test_sprintf("%2$d", 42), format_error,
118 "argument not found");
119 EXPECT_THROW_MSG(test_sprintf(format("%{}$d", INT_MAX), 42), format_error,
120 "argument not found");
121
122 EXPECT_THROW_MSG(test_sprintf("%2$", 42), format_error, "argument not found");
123 EXPECT_THROW_MSG(test_sprintf(format("%{}$d", BIG_NUM), 42), format_error,
124 "number is too big");
125 }
126
TEST(PrintfTest,DefaultAlignRight)127 TEST(PrintfTest, DefaultAlignRight) {
128 EXPECT_PRINTF(" 42", "%5d", 42);
129 EXPECT_PRINTF(" abc", "%5s", "abc");
130 }
131
TEST(PrintfTest,ZeroFlag)132 TEST(PrintfTest, ZeroFlag) {
133 EXPECT_PRINTF("00042", "%05d", 42);
134 EXPECT_PRINTF("-0042", "%05d", -42);
135
136 EXPECT_PRINTF("00042", "%05d", 42);
137 EXPECT_PRINTF("-0042", "%05d", -42);
138 EXPECT_PRINTF("-004.2", "%06g", -4.2);
139
140 EXPECT_PRINTF("+00042", "%00+6d", 42);
141
142 EXPECT_PRINTF(" 42", "%05.d", 42);
143 EXPECT_PRINTF(" 0042", "%05.4d", 42);
144
145 // '0' flag is ignored for non-numeric types.
146 EXPECT_PRINTF(" x", "%05c", 'x');
147 }
148
TEST(PrintfTest,PlusFlag)149 TEST(PrintfTest, PlusFlag) {
150 EXPECT_PRINTF("+42", "%+d", 42);
151 EXPECT_PRINTF("-42", "%+d", -42);
152 EXPECT_PRINTF("+0042", "%+05d", 42);
153 EXPECT_PRINTF("+0042", "%0++5d", 42);
154
155 // '+' flag is ignored for non-numeric types.
156 EXPECT_PRINTF("x", "%+c", 'x');
157
158 // '+' flag wins over space flag
159 EXPECT_PRINTF("+42", "%+ d", 42);
160 EXPECT_PRINTF("-42", "%+ d", -42);
161 EXPECT_PRINTF("+42", "% +d", 42);
162 EXPECT_PRINTF("-42", "% +d", -42);
163 EXPECT_PRINTF("+0042", "% +05d", 42);
164 EXPECT_PRINTF("+0042", "%0+ 5d", 42);
165
166 // '+' flag and space flag are both ignored for non-numeric types.
167 EXPECT_PRINTF("x", "%+ c", 'x');
168 EXPECT_PRINTF("x", "% +c", 'x');
169 }
170
TEST(PrintfTest,MinusFlag)171 TEST(PrintfTest, MinusFlag) {
172 EXPECT_PRINTF("abc ", "%-5s", "abc");
173 EXPECT_PRINTF("abc ", "%0--5s", "abc");
174
175 EXPECT_PRINTF("7 ", "%-5d", 7);
176 EXPECT_PRINTF("97 ", "%-5hhi", 'a');
177 EXPECT_PRINTF("a ", "%-5c", 'a');
178
179 // '0' flag is ignored if '-' flag is given
180 EXPECT_PRINTF("7 ", "%-05d", 7);
181 EXPECT_PRINTF("7 ", "%0-5d", 7);
182 EXPECT_PRINTF("a ", "%-05c", 'a');
183 EXPECT_PRINTF("a ", "%0-5c", 'a');
184 EXPECT_PRINTF("97 ", "%-05hhi", 'a');
185 EXPECT_PRINTF("97 ", "%0-5hhi", 'a');
186
187 // '-' and space flag don't interfere
188 EXPECT_PRINTF(" 42", "%- d", 42);
189 }
190
TEST(PrintfTest,SpaceFlag)191 TEST(PrintfTest, SpaceFlag) {
192 EXPECT_PRINTF(" 42", "% d", 42);
193 EXPECT_PRINTF("-42", "% d", -42);
194 EXPECT_PRINTF(" 0042", "% 05d", 42);
195 EXPECT_PRINTF(" 0042", "%0 5d", 42);
196
197 // ' ' flag is ignored for non-numeric types.
198 EXPECT_PRINTF("x", "% c", 'x');
199 }
200
TEST(PrintfTest,HashFlag)201 TEST(PrintfTest, HashFlag) {
202 EXPECT_PRINTF("042", "%#o", 042);
203 EXPECT_PRINTF(fmt::format("0{:o}", static_cast<unsigned>(-042)), "%#o", -042);
204 EXPECT_PRINTF("0", "%#o", 0);
205
206 EXPECT_PRINTF("0x42", "%#x", 0x42);
207 EXPECT_PRINTF("0X42", "%#X", 0x42);
208 EXPECT_PRINTF(fmt::format("0x{:x}", static_cast<unsigned>(-0x42)), "%#x",
209 -0x42);
210 EXPECT_PRINTF("0", "%#x", 0);
211
212 EXPECT_PRINTF("0x0042", "%#06x", 0x42);
213 EXPECT_PRINTF("0x0042", "%0##6x", 0x42);
214
215 EXPECT_PRINTF("-42.000000", "%#f", -42.0);
216 EXPECT_PRINTF("-42.000000", "%#F", -42.0);
217
218 char buffer[BUFFER_SIZE];
219 safe_sprintf(buffer, "%#e", -42.0);
220 EXPECT_PRINTF(buffer, "%#e", -42.0);
221 safe_sprintf(buffer, "%#E", -42.0);
222 EXPECT_PRINTF(buffer, "%#E", -42.0);
223
224 EXPECT_PRINTF("-42.0000", "%#g", -42.0);
225 EXPECT_PRINTF("-42.0000", "%#G", -42.0);
226
227 safe_sprintf(buffer, "%#a", 16.0);
228 EXPECT_PRINTF(buffer, "%#a", 16.0);
229 safe_sprintf(buffer, "%#A", 16.0);
230 EXPECT_PRINTF(buffer, "%#A", 16.0);
231
232 // '#' flag is ignored for non-numeric types.
233 EXPECT_PRINTF("x", "%#c", 'x');
234 }
235
TEST(PrintfTest,Width)236 TEST(PrintfTest, Width) {
237 EXPECT_PRINTF(" abc", "%5s", "abc");
238
239 // Width cannot be specified twice.
240 EXPECT_THROW_MSG(test_sprintf("%5-5d", 42), format_error,
241 "invalid type specifier");
242
243 EXPECT_THROW_MSG(test_sprintf(format("%{}d", BIG_NUM), 42), format_error,
244 "number is too big");
245 EXPECT_THROW_MSG(test_sprintf(format("%1${}d", BIG_NUM), 42), format_error,
246 "number is too big");
247 }
248
TEST(PrintfTest,DynamicWidth)249 TEST(PrintfTest, DynamicWidth) {
250 EXPECT_EQ(" 42", test_sprintf("%*d", 5, 42));
251 EXPECT_EQ("42 ", test_sprintf("%*d", -5, 42));
252 EXPECT_THROW_MSG(test_sprintf("%*d", 5.0, 42), format_error,
253 "width is not integer");
254 EXPECT_THROW_MSG(test_sprintf("%*d"), format_error, "argument not found");
255 EXPECT_THROW_MSG(test_sprintf("%*d", BIG_NUM, 42), format_error,
256 "number is too big");
257 }
258
TEST(PrintfTest,IntPrecision)259 TEST(PrintfTest, IntPrecision) {
260 EXPECT_PRINTF("00042", "%.5d", 42);
261 EXPECT_PRINTF("-00042", "%.5d", -42);
262 EXPECT_PRINTF("00042", "%.5x", 0x42);
263 EXPECT_PRINTF("0x00042", "%#.5x", 0x42);
264 EXPECT_PRINTF("00042", "%.5o", 042);
265 EXPECT_PRINTF("00042", "%#.5o", 042);
266
267 EXPECT_PRINTF(" 00042", "%7.5d", 42);
268 EXPECT_PRINTF(" 00042", "%7.5x", 0x42);
269 EXPECT_PRINTF(" 0x00042", "%#10.5x", 0x42);
270 EXPECT_PRINTF(" 00042", "%7.5o", 042);
271 EXPECT_PRINTF(" 00042", "%#10.5o", 042);
272
273 EXPECT_PRINTF("00042 ", "%-7.5d", 42);
274 EXPECT_PRINTF("00042 ", "%-7.5x", 0x42);
275 EXPECT_PRINTF("0x00042 ", "%-#10.5x", 0x42);
276 EXPECT_PRINTF("00042 ", "%-7.5o", 042);
277 EXPECT_PRINTF("00042 ", "%-#10.5o", 042);
278 }
279
TEST(PrintfTest,FloatPrecision)280 TEST(PrintfTest, FloatPrecision) {
281 char buffer[BUFFER_SIZE];
282 safe_sprintf(buffer, "%.3e", 1234.5678);
283 EXPECT_PRINTF(buffer, "%.3e", 1234.5678);
284 EXPECT_PRINTF("1234.568", "%.3f", 1234.5678);
285 EXPECT_PRINTF("1.23e+03", "%.3g", 1234.5678);
286 safe_sprintf(buffer, "%.3a", 1234.5678);
287 EXPECT_PRINTF(buffer, "%.3a", 1234.5678);
288 }
289
TEST(PrintfTest,StringPrecision)290 TEST(PrintfTest, StringPrecision) {
291 char test[] = {'H', 'e', 'l', 'l', 'o'};
292 EXPECT_EQ(fmt::sprintf("%.4s", test), "Hell");
293 }
294
TEST(PrintfTest,IgnorePrecisionForNonNumericArg)295 TEST(PrintfTest, IgnorePrecisionForNonNumericArg) {
296 EXPECT_PRINTF("abc", "%.5s", "abc");
297 }
298
TEST(PrintfTest,DynamicPrecision)299 TEST(PrintfTest, DynamicPrecision) {
300 EXPECT_EQ("00042", test_sprintf("%.*d", 5, 42));
301 EXPECT_EQ("42", test_sprintf("%.*d", -5, 42));
302 EXPECT_THROW_MSG(test_sprintf("%.*d", 5.0, 42), format_error,
303 "precision is not integer");
304 EXPECT_THROW_MSG(test_sprintf("%.*d"), format_error, "argument not found");
305 EXPECT_THROW_MSG(test_sprintf("%.*d", BIG_NUM, 42), format_error,
306 "number is too big");
307 if (sizeof(long long) != sizeof(int)) {
308 long long prec = static_cast<long long>(INT_MIN) - 1;
309 EXPECT_THROW_MSG(test_sprintf("%.*d", prec, 42), format_error,
310 "number is too big");
311 }
312 }
313
314 template <typename T> struct make_signed { typedef T type; };
315
316 #define SPECIALIZE_MAKE_SIGNED(T, S) \
317 template <> struct make_signed<T> { typedef S type; }
318
319 SPECIALIZE_MAKE_SIGNED(char, signed char);
320 SPECIALIZE_MAKE_SIGNED(unsigned char, signed char);
321 SPECIALIZE_MAKE_SIGNED(unsigned short, short);
322 SPECIALIZE_MAKE_SIGNED(unsigned, int);
323 SPECIALIZE_MAKE_SIGNED(unsigned long, long);
324 SPECIALIZE_MAKE_SIGNED(unsigned long long, long long);
325
326 // Test length format specifier ``length_spec``.
327 template <typename T, typename U>
TestLength(const char * length_spec,U value)328 void TestLength(const char* length_spec, U value) {
329 long long signed_value = 0;
330 unsigned long long unsigned_value = 0;
331 // Apply integer promotion to the argument.
332 unsigned long long max = max_value<U>();
333 using fmt::detail::const_check;
334 if (const_check(max <= static_cast<unsigned>(max_value<int>()))) {
335 signed_value = static_cast<int>(value);
336 unsigned_value = static_cast<unsigned long long>(value);
337 } else if (const_check(max <= max_value<unsigned>())) {
338 signed_value = static_cast<unsigned>(value);
339 unsigned_value = static_cast<unsigned long long>(value);
340 }
341 if (sizeof(U) <= sizeof(int) && sizeof(int) < sizeof(T)) {
342 signed_value = static_cast<long long>(value);
343 unsigned_value = static_cast<unsigned long long>(
344 static_cast<typename std::make_unsigned<unsigned>::type>(value));
345 } else {
346 signed_value = static_cast<typename make_signed<T>::type>(value);
347 unsigned_value = static_cast<typename std::make_unsigned<T>::type>(value);
348 }
349 std::ostringstream os;
350 os << signed_value;
351 EXPECT_PRINTF(os.str(), fmt::format("%{}d", length_spec), value);
352 EXPECT_PRINTF(os.str(), fmt::format("%{}i", length_spec), value);
353 os.str("");
354 os << unsigned_value;
355 EXPECT_PRINTF(os.str(), fmt::format("%{}u", length_spec), value);
356 os.str("");
357 os << std::oct << unsigned_value;
358 EXPECT_PRINTF(os.str(), fmt::format("%{}o", length_spec), value);
359 os.str("");
360 os << std::hex << unsigned_value;
361 EXPECT_PRINTF(os.str(), fmt::format("%{}x", length_spec), value);
362 os.str("");
363 os << std::hex << std::uppercase << unsigned_value;
364 EXPECT_PRINTF(os.str(), fmt::format("%{}X", length_spec), value);
365 }
366
TestLength(const char * length_spec)367 template <typename T> void TestLength(const char* length_spec) {
368 T min = std::numeric_limits<T>::min(), max = max_value<T>();
369 TestLength<T>(length_spec, 42);
370 TestLength<T>(length_spec, -42);
371 TestLength<T>(length_spec, min);
372 TestLength<T>(length_spec, max);
373 long long long_long_min = std::numeric_limits<long long>::min();
374 if (static_cast<long long>(min) > long_long_min)
375 TestLength<T>(length_spec, static_cast<long long>(min) - 1);
376 unsigned long long long_long_max = max_value<long long>();
377 if (static_cast<unsigned long long>(max) < long_long_max)
378 TestLength<T>(length_spec, static_cast<long long>(max) + 1);
379 TestLength<T>(length_spec, std::numeric_limits<short>::min());
380 TestLength<T>(length_spec, max_value<unsigned short>());
381 TestLength<T>(length_spec, std::numeric_limits<int>::min());
382 TestLength<T>(length_spec, max_value<int>());
383 TestLength<T>(length_spec, std::numeric_limits<unsigned>::min());
384 TestLength<T>(length_spec, max_value<unsigned>());
385 TestLength<T>(length_spec, std::numeric_limits<long long>::min());
386 TestLength<T>(length_spec, max_value<long long>());
387 TestLength<T>(length_spec, std::numeric_limits<unsigned long long>::min());
388 TestLength<T>(length_spec, max_value<unsigned long long>());
389 }
390
TEST(PrintfTest,Length)391 TEST(PrintfTest, Length) {
392 TestLength<char>("hh");
393 TestLength<signed char>("hh");
394 TestLength<unsigned char>("hh");
395 TestLength<short>("h");
396 TestLength<unsigned short>("h");
397 TestLength<long>("l");
398 TestLength<unsigned long>("l");
399 TestLength<long long>("ll");
400 TestLength<unsigned long long>("ll");
401 TestLength<intmax_t>("j");
402 TestLength<size_t>("z");
403 TestLength<std::ptrdiff_t>("t");
404 long double max = max_value<long double>();
405 EXPECT_PRINTF(fmt::format("{:.6}", max), "%g", max);
406 EXPECT_PRINTF(fmt::format("{:.6}", max), "%Lg", max);
407 }
408
TEST(PrintfTest,Bool)409 TEST(PrintfTest, Bool) {
410 EXPECT_PRINTF("1", "%d", true);
411 EXPECT_PRINTF("true", "%s", true);
412 }
413
TEST(PrintfTest,Int)414 TEST(PrintfTest, Int) {
415 EXPECT_PRINTF("-42", "%d", -42);
416 EXPECT_PRINTF("-42", "%i", -42);
417 unsigned u = 0 - 42u;
418 EXPECT_PRINTF(fmt::format("{}", u), "%u", -42);
419 EXPECT_PRINTF(fmt::format("{:o}", u), "%o", -42);
420 EXPECT_PRINTF(fmt::format("{:x}", u), "%x", -42);
421 EXPECT_PRINTF(fmt::format("{:X}", u), "%X", -42);
422 }
423
TEST(PrintfTest,long_long)424 TEST(PrintfTest, long_long) {
425 // fmt::printf allows passing long long arguments to %d without length
426 // specifiers.
427 long long max = max_value<long long>();
428 EXPECT_PRINTF(fmt::format("{}", max), "%d", max);
429 }
430
TEST(PrintfTest,Float)431 TEST(PrintfTest, Float) {
432 EXPECT_PRINTF("392.650000", "%f", 392.65);
433 EXPECT_PRINTF("392.65", "%.2f", 392.65);
434 EXPECT_PRINTF("392.6", "%.1f", 392.65);
435 EXPECT_PRINTF("393", "%.f", 392.65);
436 EXPECT_PRINTF("392.650000", "%F", 392.65);
437 char buffer[BUFFER_SIZE];
438 safe_sprintf(buffer, "%e", 392.65);
439 EXPECT_PRINTF(buffer, "%e", 392.65);
440 safe_sprintf(buffer, "%E", 392.65);
441 EXPECT_PRINTF(buffer, "%E", 392.65);
442 EXPECT_PRINTF("392.65", "%g", 392.65);
443 EXPECT_PRINTF("392.65", "%G", 392.65);
444 EXPECT_PRINTF("392", "%g", 392.0);
445 EXPECT_PRINTF("392", "%G", 392.0);
446 EXPECT_PRINTF("4.56e-07", "%g", 0.000000456);
447 safe_sprintf(buffer, "%a", -392.65);
448 EXPECT_EQ(buffer, format("{:a}", -392.65));
449 safe_sprintf(buffer, "%A", -392.65);
450 EXPECT_EQ(buffer, format("{:A}", -392.65));
451 }
452
TEST(PrintfTest,Inf)453 TEST(PrintfTest, Inf) {
454 double inf = std::numeric_limits<double>::infinity();
455 for (const char* type = "fega"; *type; ++type) {
456 EXPECT_PRINTF("inf", fmt::format("%{}", *type), inf);
457 char upper = static_cast<char>(std::toupper(*type));
458 EXPECT_PRINTF("INF", fmt::format("%{}", upper), inf);
459 }
460 }
461
TEST(PrintfTest,Char)462 TEST(PrintfTest, Char) {
463 EXPECT_PRINTF("x", "%c", 'x');
464 int max = max_value<int>();
465 EXPECT_PRINTF(fmt::format("{}", static_cast<char>(max)), "%c", max);
466 // EXPECT_PRINTF("x", "%lc", L'x');
467 EXPECT_PRINTF(L"x", L"%c", L'x');
468 EXPECT_PRINTF(fmt::format(L"{}", static_cast<wchar_t>(max)), L"%c", max);
469 }
470
TEST(PrintfTest,String)471 TEST(PrintfTest, String) {
472 EXPECT_PRINTF("abc", "%s", "abc");
473 const char* null_str = nullptr;
474 EXPECT_PRINTF("(null)", "%s", null_str);
475 EXPECT_PRINTF(" (null)", "%10s", null_str);
476 EXPECT_PRINTF(L"abc", L"%s", L"abc");
477 const wchar_t* null_wstr = nullptr;
478 EXPECT_PRINTF(L"(null)", L"%s", null_wstr);
479 EXPECT_PRINTF(L" (null)", L"%10s", null_wstr);
480 }
481
TEST(PrintfTest,UCharString)482 TEST(PrintfTest, UCharString) {
483 unsigned char str[] = "test";
484 unsigned char* pstr = str;
485 EXPECT_EQ("test", fmt::sprintf("%s", pstr));
486 }
487
TEST(PrintfTest,Pointer)488 TEST(PrintfTest, Pointer) {
489 int n;
490 void* p = &n;
491 EXPECT_PRINTF(fmt::format("{}", p), "%p", p);
492 p = nullptr;
493 EXPECT_PRINTF("(nil)", "%p", p);
494 EXPECT_PRINTF(" (nil)", "%10p", p);
495 const char* s = "test";
496 EXPECT_PRINTF(fmt::format("{:p}", s), "%p", s);
497 const char* null_str = nullptr;
498 EXPECT_PRINTF("(nil)", "%p", null_str);
499
500 p = &n;
501 EXPECT_PRINTF(fmt::format(L"{}", p), L"%p", p);
502 p = nullptr;
503 EXPECT_PRINTF(L"(nil)", L"%p", p);
504 EXPECT_PRINTF(L" (nil)", L"%10p", p);
505 const wchar_t* w = L"test";
506 EXPECT_PRINTF(fmt::format(L"{:p}", w), L"%p", w);
507 const wchar_t* null_wstr = nullptr;
508 EXPECT_PRINTF(L"(nil)", L"%p", null_wstr);
509 }
510
TEST(PrintfTest,Location)511 TEST(PrintfTest, Location) {
512 // TODO: test %n
513 }
514
515 enum test_enum { answer = 42 };
516
TEST(PrintfTest,Enum)517 TEST(PrintfTest, Enum) {
518 EXPECT_PRINTF("42", "%d", answer);
519 volatile test_enum volatile_enum = answer;
520 EXPECT_PRINTF("42", "%d", volatile_enum);
521 }
522
523 #if FMT_USE_FCNTL
TEST(PrintfTest,Examples)524 TEST(PrintfTest, Examples) {
525 const char* weekday = "Thursday";
526 const char* month = "August";
527 int day = 21;
528 EXPECT_WRITE(stdout, fmt::printf("%1$s, %3$d %2$s", weekday, month, day),
529 "Thursday, 21 August");
530 }
531
TEST(PrintfTest,PrintfError)532 TEST(PrintfTest, PrintfError) {
533 fmt::file read_end, write_end;
534 fmt::file::pipe(read_end, write_end);
535 int result = fmt::fprintf(read_end.fdopen("r").get(), "test");
536 EXPECT_LT(result, 0);
537 }
538 #endif
539
TEST(PrintfTest,WideString)540 TEST(PrintfTest, WideString) { EXPECT_EQ(L"abc", fmt::sprintf(L"%s", L"abc")); }
541
TEST(PrintfTest,PrintfCustom)542 TEST(PrintfTest, PrintfCustom) {
543 EXPECT_EQ("abc", test_sprintf("%s", TestString("abc")));
544 }
545
TEST(PrintfTest,OStream)546 TEST(PrintfTest, OStream) {
547 std::ostringstream os;
548 int ret = fmt::fprintf(os, "Don't %s!", "panic");
549 EXPECT_EQ("Don't panic!", os.str());
550 EXPECT_EQ(12, ret);
551 }
552
TEST(PrintfTest,VPrintf)553 TEST(PrintfTest, VPrintf) {
554 fmt::format_arg_store<fmt::printf_context, int> as{42};
555 fmt::basic_format_args<fmt::printf_context> args(as);
556 EXPECT_EQ(fmt::vsprintf("%d", args), "42");
557 EXPECT_WRITE(stdout, fmt::vprintf("%d", args), "42");
558 EXPECT_WRITE(stdout, fmt::vfprintf(stdout, "%d", args), "42");
559 EXPECT_WRITE(stdout, fmt::vfprintf(std::cout, "%d", args), "42");
560 }
561
562 template <typename... Args>
check_format_string_regression(fmt::string_view s,const Args &...args)563 void check_format_string_regression(fmt::string_view s, const Args&... args) {
564 fmt::sprintf(s, args...);
565 }
566
TEST(PrintfTest,CheckFormatStringRegression)567 TEST(PrintfTest, CheckFormatStringRegression) {
568 check_format_string_regression("%c%s", 'x', "");
569 }
570
TEST(PrintfTest,FixedLargeExponent)571 TEST(PrintfTest, FixedLargeExponent) {
572 EXPECT_EQ("1000000000000000000000", fmt::sprintf("%.*f", -13, 1e21));
573 }
574
TEST(PrintfTest,VSPrintfMakeArgsExample)575 TEST(PrintfTest, VSPrintfMakeArgsExample) {
576 fmt::format_arg_store<fmt::printf_context, int, const char*> as{42,
577 "something"};
578 fmt::basic_format_args<fmt::printf_context> args(as);
579 EXPECT_EQ("[42] something happened", fmt::vsprintf("[%d] %s happened", args));
580 auto as2 = fmt::make_printf_args(42, "something");
581 fmt::basic_format_args<fmt::printf_context> args2(as2);
582 EXPECT_EQ("[42] something happened",
583 fmt::vsprintf("[%d] %s happened", args2));
584 // The older gcc versions can't cast the return value.
585 #if !defined(__GNUC__) || (__GNUC__ > 4)
586 EXPECT_EQ("[42] something happened",
587 fmt::vsprintf("[%d] %s happened",
588 {fmt::make_printf_args(42, "something")}));
589 #endif
590 }
591
TEST(PrintfTest,VSPrintfMakeWArgsExample)592 TEST(PrintfTest, VSPrintfMakeWArgsExample) {
593 fmt::format_arg_store<fmt::wprintf_context, int, const wchar_t*> as{
594 42, L"something"};
595 fmt::basic_format_args<fmt::wprintf_context> args(as);
596 EXPECT_EQ(L"[42] something happened",
597 fmt::vsprintf(L"[%d] %s happened", args));
598 auto as2 = fmt::make_wprintf_args(42, L"something");
599 fmt::basic_format_args<fmt::wprintf_context> args2(as2);
600 EXPECT_EQ(L"[42] something happened",
601 fmt::vsprintf(L"[%d] %s happened", args2));
602 // the older gcc versions can't cast the return value
603 #if !defined(__GNUC__) || (__GNUC__ > 4)
604 EXPECT_EQ(L"[42] something happened",
605 fmt::vsprintf(L"[%d] %s happened",
606 {fmt::make_wprintf_args(42, L"something")}));
607 #endif
608 }
609
TEST(PrintfTest,PrintfDetermineOutputSize)610 TEST(PrintfTest, PrintfDetermineOutputSize) {
611 using backit = std::back_insert_iterator<std::vector<char>>;
612 using truncated_printf_context =
613 fmt::basic_printf_context<fmt::detail::truncating_iterator<backit>, char>;
614
615 auto v = std::vector<char>{};
616 auto it = std::back_inserter(v);
617
618 const auto format_string = "%s";
619 const auto format_arg = "Hello";
620 const auto expected_size = fmt::sprintf(format_string, format_arg).size();
621
622 EXPECT_EQ((truncated_printf_context(
623 fmt::detail::truncating_iterator<backit>(it, 0), format_string,
624 fmt::make_format_args<truncated_printf_context>(format_arg))
625 .format()
626 .count()),
627 expected_size);
628 }
629