printf-test.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. #include "fmt/printf.h"
  8. #include <cctype>
  9. #include <climits>
  10. #include <cstring>
  11. #include "fmt/xchar.h"
  12. #include "gtest-extra.h"
  13. #include "util.h"
  14. using fmt::format;
  15. using fmt::format_error;
  16. using fmt::detail::max_value;
  17. const unsigned big_num = INT_MAX + 1u;
  18. // Makes format string argument positional.
  19. static std::string make_positional(fmt::string_view format) {
  20. std::string s(format.data(), format.size());
  21. s.replace(s.find('%'), 1, "%1$");
  22. return s;
  23. }
  24. static std::wstring make_positional(fmt::basic_string_view<wchar_t> format) {
  25. std::wstring s(format.data(), format.size());
  26. s.replace(s.find(L'%'), 1, L"%1$");
  27. return s;
  28. }
  29. // A wrapper around fmt::sprintf to workaround bogus warnings about invalid
  30. // format strings in MSVC.
  31. template <typename... Args>
  32. std::string test_sprintf(fmt::string_view format, const Args&... args) {
  33. return fmt::sprintf(format, args...);
  34. }
  35. template <typename... Args>
  36. std::wstring test_sprintf(fmt::basic_string_view<wchar_t> format,
  37. const Args&... args) {
  38. return fmt::sprintf(format, args...);
  39. }
  40. #define EXPECT_PRINTF(expected_output, format, arg) \
  41. EXPECT_EQ(expected_output, test_sprintf(format, arg)) \
  42. << "format: " << format; \
  43. EXPECT_EQ(expected_output, fmt::sprintf(make_positional(format), arg))
  44. TEST(printf_test, no_args) {
  45. EXPECT_EQ("test", test_sprintf("test"));
  46. EXPECT_EQ(L"test", fmt::sprintf(L"test"));
  47. }
  48. TEST(printf_test, escape) {
  49. EXPECT_EQ("%", test_sprintf("%%"));
  50. EXPECT_EQ("before %", test_sprintf("before %%"));
  51. EXPECT_EQ("% after", test_sprintf("%% after"));
  52. EXPECT_EQ("before % after", test_sprintf("before %% after"));
  53. EXPECT_EQ("%s", test_sprintf("%%s"));
  54. EXPECT_EQ(L"%", fmt::sprintf(L"%%"));
  55. EXPECT_EQ(L"before %", fmt::sprintf(L"before %%"));
  56. EXPECT_EQ(L"% after", fmt::sprintf(L"%% after"));
  57. EXPECT_EQ(L"before % after", fmt::sprintf(L"before %% after"));
  58. EXPECT_EQ(L"%s", fmt::sprintf(L"%%s"));
  59. }
  60. TEST(printf_test, positional_args) {
  61. EXPECT_EQ("42", test_sprintf("%1$d", 42));
  62. EXPECT_EQ("before 42", test_sprintf("before %1$d", 42));
  63. EXPECT_EQ("42 after", test_sprintf("%1$d after", 42));
  64. EXPECT_EQ("before 42 after", test_sprintf("before %1$d after", 42));
  65. EXPECT_EQ("answer = 42", test_sprintf("%1$s = %2$d", "answer", 42));
  66. EXPECT_EQ("42 is the answer", test_sprintf("%2$d is the %1$s", "answer", 42));
  67. EXPECT_EQ("abracadabra", test_sprintf("%1$s%2$s%1$s", "abra", "cad"));
  68. }
  69. TEST(printf_test, automatic_arg_indexing) {
  70. EXPECT_EQ("abc", test_sprintf("%c%c%c", 'a', 'b', 'c'));
  71. }
  72. TEST(printf_test, number_is_too_big_in_arg_index) {
  73. EXPECT_THROW_MSG(test_sprintf(format("%{}$", big_num)), format_error,
  74. "argument not found");
  75. EXPECT_THROW_MSG(test_sprintf(format("%{}$d", big_num)), format_error,
  76. "argument not found");
  77. }
  78. TEST(printf_test, switch_arg_indexing) {
  79. EXPECT_THROW_MSG(test_sprintf("%1$d%", 1, 2), format_error,
  80. "cannot switch from manual to automatic argument indexing");
  81. EXPECT_THROW_MSG(test_sprintf(format("%1$d%{}d", big_num), 1, 2),
  82. format_error, "number is too big");
  83. EXPECT_THROW_MSG(test_sprintf("%1$d%d", 1, 2), format_error,
  84. "cannot switch from manual to automatic argument indexing");
  85. EXPECT_THROW_MSG(test_sprintf("%d%1$", 1, 2), format_error,
  86. "cannot switch from automatic to manual argument indexing");
  87. EXPECT_THROW_MSG(test_sprintf(format("%d%{}$d", big_num), 1, 2), format_error,
  88. "cannot switch from automatic to manual argument indexing");
  89. EXPECT_THROW_MSG(test_sprintf("%d%1$d", 1, 2), format_error,
  90. "cannot switch from automatic to manual argument indexing");
  91. // Indexing errors override width errors.
  92. EXPECT_THROW_MSG(test_sprintf(format("%d%1${}d", big_num), 1, 2),
  93. format_error, "number is too big");
  94. EXPECT_THROW_MSG(test_sprintf(format("%1$d%{}d", big_num), 1, 2),
  95. format_error, "number is too big");
  96. }
  97. TEST(printf_test, invalid_arg_index) {
  98. EXPECT_THROW_MSG(test_sprintf("%0$d", 42), format_error,
  99. "argument not found");
  100. EXPECT_THROW_MSG(test_sprintf("%2$d", 42), format_error,
  101. "argument not found");
  102. EXPECT_THROW_MSG(test_sprintf(format("%{}$d", INT_MAX), 42), format_error,
  103. "argument not found");
  104. EXPECT_THROW_MSG(test_sprintf("%2$", 42), format_error, "argument not found");
  105. EXPECT_THROW_MSG(test_sprintf(format("%{}$d", big_num), 42), format_error,
  106. "argument not found");
  107. }
  108. TEST(printf_test, default_align_right) {
  109. EXPECT_PRINTF(" 42", "%5d", 42);
  110. EXPECT_PRINTF(" abc", "%5s", "abc");
  111. }
  112. TEST(printf_test, zero_flag) {
  113. EXPECT_PRINTF("00042", "%05d", 42);
  114. EXPECT_PRINTF("-0042", "%05d", -42);
  115. EXPECT_PRINTF("00042", "%05d", 42);
  116. EXPECT_PRINTF("-0042", "%05d", -42);
  117. EXPECT_PRINTF("-004.2", "%06g", -4.2);
  118. EXPECT_PRINTF("+00042", "%00+6d", 42);
  119. EXPECT_PRINTF(" 42", "%05.d", 42);
  120. EXPECT_PRINTF(" 0042", "%05.4d", 42);
  121. // '0' flag is ignored for non-numeric types.
  122. EXPECT_PRINTF(" x", "%05c", 'x');
  123. }
  124. TEST(printf_test, plus_flag) {
  125. EXPECT_PRINTF("+42", "%+d", 42);
  126. EXPECT_PRINTF("-42", "%+d", -42);
  127. EXPECT_PRINTF("+0042", "%+05d", 42);
  128. EXPECT_PRINTF("+0042", "%0++5d", 42);
  129. // '+' flag is ignored for non-numeric types.
  130. EXPECT_PRINTF("x", "%+c", 'x');
  131. // '+' flag wins over space flag
  132. EXPECT_PRINTF("+42", "%+ d", 42);
  133. EXPECT_PRINTF("-42", "%+ d", -42);
  134. EXPECT_PRINTF("+42", "% +d", 42);
  135. EXPECT_PRINTF("-42", "% +d", -42);
  136. EXPECT_PRINTF("+0042", "% +05d", 42);
  137. EXPECT_PRINTF("+0042", "%0+ 5d", 42);
  138. // '+' flag and space flag are both ignored for non-numeric types.
  139. EXPECT_PRINTF("x", "%+ c", 'x');
  140. EXPECT_PRINTF("x", "% +c", 'x');
  141. }
  142. TEST(printf_test, minus_flag) {
  143. EXPECT_PRINTF("abc ", "%-5s", "abc");
  144. EXPECT_PRINTF("abc ", "%0--5s", "abc");
  145. EXPECT_PRINTF("7 ", "%-5d", 7);
  146. EXPECT_PRINTF("97 ", "%-5hhi", 'a');
  147. EXPECT_PRINTF("a ", "%-5c", 'a');
  148. // '0' flag is ignored if '-' flag is given
  149. EXPECT_PRINTF("7 ", "%-05d", 7);
  150. EXPECT_PRINTF("7 ", "%0-5d", 7);
  151. EXPECT_PRINTF("a ", "%-05c", 'a');
  152. EXPECT_PRINTF("a ", "%0-5c", 'a');
  153. EXPECT_PRINTF("97 ", "%-05hhi", 'a');
  154. EXPECT_PRINTF("97 ", "%0-5hhi", 'a');
  155. // '-' and space flag don't interfere
  156. EXPECT_PRINTF(" 42", "%- d", 42);
  157. }
  158. TEST(printf_test, space_flag) {
  159. EXPECT_PRINTF(" 42", "% d", 42);
  160. EXPECT_PRINTF("-42", "% d", -42);
  161. EXPECT_PRINTF(" 0042", "% 05d", 42);
  162. EXPECT_PRINTF(" 0042", "%0 5d", 42);
  163. // ' ' flag is ignored for non-numeric types.
  164. EXPECT_PRINTF("x", "% c", 'x');
  165. }
  166. TEST(printf_test, hash_flag) {
  167. EXPECT_PRINTF("042", "%#o", 042);
  168. EXPECT_PRINTF(fmt::format("0{:o}", static_cast<unsigned>(-042)), "%#o", -042);
  169. EXPECT_PRINTF("0", "%#o", 0);
  170. EXPECT_PRINTF("0x42", "%#x", 0x42);
  171. EXPECT_PRINTF("0X42", "%#X", 0x42);
  172. EXPECT_PRINTF(fmt::format("0x{:x}", static_cast<unsigned>(-0x42)), "%#x",
  173. -0x42);
  174. EXPECT_PRINTF("0", "%#x", 0);
  175. EXPECT_PRINTF("0x0042", "%#06x", 0x42);
  176. EXPECT_PRINTF("0x0042", "%0##6x", 0x42);
  177. EXPECT_PRINTF("-42.000000", "%#f", -42.0);
  178. EXPECT_PRINTF("-42.000000", "%#F", -42.0);
  179. char buffer[256];
  180. safe_sprintf(buffer, "%#e", -42.0);
  181. EXPECT_PRINTF(buffer, "%#e", -42.0);
  182. safe_sprintf(buffer, "%#E", -42.0);
  183. EXPECT_PRINTF(buffer, "%#E", -42.0);
  184. EXPECT_PRINTF("-42.0000", "%#g", -42.0);
  185. EXPECT_PRINTF("-42.0000", "%#G", -42.0);
  186. EXPECT_PRINTF("0x1.p+4", "%#a", 16.0);
  187. EXPECT_PRINTF("0X1.P+4", "%#A", 16.0);
  188. // '#' flag is ignored for non-numeric types.
  189. EXPECT_PRINTF("x", "%#c", 'x');
  190. }
  191. TEST(printf_test, width) {
  192. EXPECT_PRINTF(" abc", "%5s", "abc");
  193. // Width cannot be specified twice.
  194. EXPECT_THROW_MSG(test_sprintf("%5-5d", 42), format_error,
  195. "invalid format specifier");
  196. EXPECT_THROW_MSG(test_sprintf(format("%{}d", big_num), 42), format_error,
  197. "number is too big");
  198. EXPECT_THROW_MSG(test_sprintf(format("%1${}d", big_num), 42), format_error,
  199. "number is too big");
  200. }
  201. TEST(printf_test, dynamic_width) {
  202. EXPECT_EQ(" 42", test_sprintf("%*d", 5, 42));
  203. EXPECT_EQ("42 ", test_sprintf("%*d", -5, 42));
  204. EXPECT_THROW_MSG(test_sprintf("%*d", 5.0, 42), format_error,
  205. "width is not integer");
  206. EXPECT_THROW_MSG(test_sprintf("%*d"), format_error, "argument not found");
  207. EXPECT_THROW_MSG(test_sprintf("%*d", big_num, 42), format_error,
  208. "number is too big");
  209. }
  210. TEST(printf_test, int_precision) {
  211. EXPECT_PRINTF("00042", "%.5d", 42);
  212. EXPECT_PRINTF("-00042", "%.5d", -42);
  213. EXPECT_PRINTF("00042", "%.5x", 0x42);
  214. EXPECT_PRINTF("0x00042", "%#.5x", 0x42);
  215. EXPECT_PRINTF("00042", "%.5o", 042);
  216. EXPECT_PRINTF("00042", "%#.5o", 042);
  217. EXPECT_PRINTF(" 00042", "%7.5d", 42);
  218. EXPECT_PRINTF(" 00042", "%7.5x", 0x42);
  219. EXPECT_PRINTF(" 0x00042", "%#10.5x", 0x42);
  220. EXPECT_PRINTF(" 00042", "%7.5o", 042);
  221. EXPECT_PRINTF(" 00042", "%#10.5o", 042);
  222. EXPECT_PRINTF("00042 ", "%-7.5d", 42);
  223. EXPECT_PRINTF("00042 ", "%-7.5x", 0x42);
  224. EXPECT_PRINTF("0x00042 ", "%-#10.5x", 0x42);
  225. EXPECT_PRINTF("00042 ", "%-7.5o", 042);
  226. EXPECT_PRINTF("00042 ", "%-#10.5o", 042);
  227. }
  228. TEST(printf_test, float_precision) {
  229. char buffer[256];
  230. safe_sprintf(buffer, "%.3e", 1234.5678);
  231. EXPECT_PRINTF(buffer, "%.3e", 1234.5678);
  232. EXPECT_PRINTF("1234.568", "%.3f", 1234.5678);
  233. EXPECT_PRINTF("1.23e+03", "%.3g", 1234.5678);
  234. safe_sprintf(buffer, "%.3a", 1234.5678);
  235. EXPECT_PRINTF(buffer, "%.3a", 1234.5678);
  236. }
  237. TEST(printf_test, string_precision) {
  238. char test[] = {'H', 'e', 'l', 'l', 'o'};
  239. EXPECT_EQ(fmt::sprintf("%.4s", test), "Hell");
  240. }
  241. TEST(printf_test, ignore_precision_for_non_numeric_arg) {
  242. EXPECT_PRINTF("abc", "%.5s", "abc");
  243. }
  244. TEST(printf_test, dynamic_precision) {
  245. EXPECT_EQ("00042", test_sprintf("%.*d", 5, 42));
  246. EXPECT_EQ("42", test_sprintf("%.*d", -5, 42));
  247. EXPECT_THROW_MSG(test_sprintf("%.*d", 5.0, 42), format_error,
  248. "precision is not integer");
  249. EXPECT_THROW_MSG(test_sprintf("%.*d"), format_error, "argument not found");
  250. EXPECT_THROW_MSG(test_sprintf("%.*d", big_num, 42), format_error,
  251. "number is too big");
  252. if (sizeof(long long) != sizeof(int)) {
  253. long long prec = static_cast<long long>(INT_MIN) - 1;
  254. EXPECT_THROW_MSG(test_sprintf("%.*d", prec, 42), format_error,
  255. "number is too big");
  256. }
  257. }
  258. template <typename T> struct make_signed { typedef T type; };
  259. #define SPECIALIZE_MAKE_SIGNED(T, S) \
  260. template <> struct make_signed<T> { typedef S type; }
  261. SPECIALIZE_MAKE_SIGNED(char, signed char);
  262. SPECIALIZE_MAKE_SIGNED(unsigned char, signed char);
  263. SPECIALIZE_MAKE_SIGNED(unsigned short, short);
  264. SPECIALIZE_MAKE_SIGNED(unsigned, int);
  265. SPECIALIZE_MAKE_SIGNED(unsigned long, long);
  266. SPECIALIZE_MAKE_SIGNED(unsigned long long, long long);
  267. // Test length format specifier ``length_spec``.
  268. template <typename T, typename U>
  269. void test_length(const char* length_spec, U value) {
  270. long long signed_value = 0;
  271. unsigned long long unsigned_value = 0;
  272. // Apply integer promotion to the argument.
  273. unsigned long long max = max_value<U>();
  274. using fmt::detail::const_check;
  275. if (const_check(max <= static_cast<unsigned>(max_value<int>()))) {
  276. signed_value = static_cast<int>(value);
  277. unsigned_value = static_cast<unsigned long long>(value);
  278. } else if (const_check(max <= max_value<unsigned>())) {
  279. signed_value = static_cast<unsigned>(value);
  280. unsigned_value = static_cast<unsigned long long>(value);
  281. }
  282. if (sizeof(U) <= sizeof(int) && sizeof(int) < sizeof(T)) {
  283. signed_value = static_cast<long long>(value);
  284. unsigned_value = static_cast<unsigned long long>(
  285. static_cast<typename std::make_unsigned<unsigned>::type>(value));
  286. } else {
  287. signed_value = static_cast<typename make_signed<T>::type>(value);
  288. unsigned_value = static_cast<typename std::make_unsigned<T>::type>(value);
  289. }
  290. std::ostringstream os;
  291. os << signed_value;
  292. EXPECT_PRINTF(os.str(), fmt::format("%{}d", length_spec), value);
  293. EXPECT_PRINTF(os.str(), fmt::format("%{}i", length_spec), value);
  294. os.str("");
  295. os << unsigned_value;
  296. EXPECT_PRINTF(os.str(), fmt::format("%{}u", length_spec), value);
  297. os.str("");
  298. os << std::oct << unsigned_value;
  299. EXPECT_PRINTF(os.str(), fmt::format("%{}o", length_spec), value);
  300. os.str("");
  301. os << std::hex << unsigned_value;
  302. EXPECT_PRINTF(os.str(), fmt::format("%{}x", length_spec), value);
  303. os.str("");
  304. os << std::hex << std::uppercase << unsigned_value;
  305. EXPECT_PRINTF(os.str(), fmt::format("%{}X", length_spec), value);
  306. }
  307. template <typename T> void test_length(const char* length_spec) {
  308. T min = std::numeric_limits<T>::min(), max = max_value<T>();
  309. test_length<T>(length_spec, 42);
  310. test_length<T>(length_spec, -42);
  311. test_length<T>(length_spec, min);
  312. test_length<T>(length_spec, max);
  313. long long long_long_min = std::numeric_limits<long long>::min();
  314. if (static_cast<long long>(min) > long_long_min)
  315. test_length<T>(length_spec, static_cast<long long>(min) - 1);
  316. unsigned long long long_long_max = max_value<long long>();
  317. if (static_cast<unsigned long long>(max) < long_long_max)
  318. test_length<T>(length_spec, static_cast<long long>(max) + 1);
  319. test_length<T>(length_spec, std::numeric_limits<short>::min());
  320. test_length<T>(length_spec, max_value<unsigned short>());
  321. test_length<T>(length_spec, std::numeric_limits<int>::min());
  322. test_length<T>(length_spec, max_value<int>());
  323. test_length<T>(length_spec, std::numeric_limits<unsigned>::min());
  324. test_length<T>(length_spec, max_value<unsigned>());
  325. test_length<T>(length_spec, std::numeric_limits<long long>::min());
  326. test_length<T>(length_spec, max_value<long long>());
  327. test_length<T>(length_spec, std::numeric_limits<unsigned long long>::min());
  328. test_length<T>(length_spec, max_value<unsigned long long>());
  329. }
  330. TEST(printf_test, length) {
  331. test_length<char>("hh");
  332. test_length<signed char>("hh");
  333. test_length<unsigned char>("hh");
  334. test_length<short>("h");
  335. test_length<unsigned short>("h");
  336. test_length<long>("l");
  337. test_length<unsigned long>("l");
  338. test_length<long long>("ll");
  339. test_length<unsigned long long>("ll");
  340. test_length<intmax_t>("j");
  341. test_length<size_t>("z");
  342. test_length<std::ptrdiff_t>("t");
  343. long double max = max_value<long double>();
  344. EXPECT_PRINTF(fmt::format("{:.6}", max), "%g", max);
  345. EXPECT_PRINTF(fmt::format("{:.6}", max), "%Lg", max);
  346. }
  347. TEST(printf_test, bool) { EXPECT_PRINTF("1", "%d", true); }
  348. TEST(printf_test, int) {
  349. EXPECT_PRINTF("-42", "%d", -42);
  350. EXPECT_PRINTF("-42", "%i", -42);
  351. unsigned u = 0 - 42u;
  352. EXPECT_PRINTF(fmt::format("{}", u), "%u", -42);
  353. EXPECT_PRINTF(fmt::format("{:o}", u), "%o", -42);
  354. EXPECT_PRINTF(fmt::format("{:x}", u), "%x", -42);
  355. EXPECT_PRINTF(fmt::format("{:X}", u), "%X", -42);
  356. }
  357. TEST(printf_test, long_long) {
  358. // fmt::printf allows passing long long arguments to %d without length
  359. // specifiers.
  360. long long max = max_value<long long>();
  361. EXPECT_PRINTF(fmt::format("{}", max), "%d", max);
  362. }
  363. TEST(printf_test, float) {
  364. EXPECT_PRINTF("392.650000", "%f", 392.65);
  365. EXPECT_PRINTF("392.65", "%.2f", 392.65);
  366. EXPECT_PRINTF("392.6", "%.1f", 392.65);
  367. EXPECT_PRINTF("393", "%.f", 392.65);
  368. EXPECT_PRINTF("392.650000", "%F", 392.65);
  369. char buffer[256];
  370. safe_sprintf(buffer, "%e", 392.65);
  371. EXPECT_PRINTF(buffer, "%e", 392.65);
  372. safe_sprintf(buffer, "%E", 392.65);
  373. EXPECT_PRINTF(buffer, "%E", 392.65);
  374. EXPECT_PRINTF("392.65", "%g", 392.65);
  375. EXPECT_PRINTF("392.65", "%G", 392.65);
  376. EXPECT_PRINTF("392", "%g", 392.0);
  377. EXPECT_PRINTF("392", "%G", 392.0);
  378. EXPECT_PRINTF("4.56e-07", "%g", 0.000000456);
  379. safe_sprintf(buffer, "%a", -392.65);
  380. EXPECT_EQ(buffer, format("{:a}", -392.65));
  381. safe_sprintf(buffer, "%A", -392.65);
  382. EXPECT_EQ(buffer, format("{:A}", -392.65));
  383. }
  384. TEST(printf_test, inf) {
  385. double inf = std::numeric_limits<double>::infinity();
  386. for (const char* type = "fega"; *type; ++type) {
  387. EXPECT_PRINTF("inf", fmt::format("%{}", *type), inf);
  388. char upper = static_cast<char>(std::toupper(*type));
  389. EXPECT_PRINTF("INF", fmt::format("%{}", upper), inf);
  390. }
  391. }
  392. TEST(printf_test, char) {
  393. EXPECT_PRINTF("x", "%c", 'x');
  394. int max = max_value<int>();
  395. EXPECT_PRINTF(fmt::format("{}", static_cast<char>(max)), "%c", max);
  396. // EXPECT_PRINTF("x", "%lc", L'x');
  397. EXPECT_PRINTF(L"x", L"%c", L'x');
  398. EXPECT_PRINTF(fmt::format(L"{}", static_cast<wchar_t>(max)), L"%c", max);
  399. }
  400. TEST(printf_test, string) {
  401. EXPECT_PRINTF("abc", "%s", "abc");
  402. const char* null_str = nullptr;
  403. EXPECT_PRINTF("(null)", "%s", null_str);
  404. EXPECT_PRINTF(" (null)", "%10s", null_str);
  405. EXPECT_PRINTF(L"abc", L"%s", L"abc");
  406. const wchar_t* null_wstr = nullptr;
  407. EXPECT_PRINTF(L"(null)", L"%s", null_wstr);
  408. EXPECT_PRINTF(L" (null)", L"%10s", null_wstr);
  409. }
  410. TEST(printf_test, pointer) {
  411. int n;
  412. void* p = &n;
  413. EXPECT_PRINTF(fmt::format("{}", p), "%p", p);
  414. p = nullptr;
  415. EXPECT_PRINTF("(nil)", "%p", p);
  416. EXPECT_PRINTF(" (nil)", "%10p", p);
  417. const char* s = "test";
  418. EXPECT_PRINTF(fmt::format("{:p}", s), "%p", s);
  419. const char* null_str = nullptr;
  420. EXPECT_PRINTF("(nil)", "%p", null_str);
  421. p = &n;
  422. EXPECT_PRINTF(fmt::format(L"{}", p), L"%p", p);
  423. p = nullptr;
  424. EXPECT_PRINTF(L"(nil)", L"%p", p);
  425. EXPECT_PRINTF(L" (nil)", L"%10p", p);
  426. const wchar_t* w = L"test";
  427. EXPECT_PRINTF(fmt::format(L"{:p}", w), L"%p", w);
  428. const wchar_t* null_wstr = nullptr;
  429. EXPECT_PRINTF(L"(nil)", L"%p", null_wstr);
  430. }
  431. enum test_enum { answer = 42 };
  432. auto format_as(test_enum e) -> int { return e; }
  433. TEST(printf_test, enum) {
  434. EXPECT_PRINTF("42", "%d", answer);
  435. volatile test_enum volatile_enum = answer;
  436. EXPECT_PRINTF("42", "%d", volatile_enum);
  437. }
  438. #if FMT_USE_FCNTL
  439. TEST(printf_test, examples) {
  440. const char* weekday = "Thursday";
  441. const char* month = "August";
  442. int day = 21;
  443. EXPECT_WRITE(stdout, fmt::printf("%1$s, %3$d %2$s", weekday, month, day),
  444. "Thursday, 21 August");
  445. }
  446. TEST(printf_test, printf_error) {
  447. fmt::file read_end, write_end;
  448. fmt::file::pipe(read_end, write_end);
  449. int result = fmt::fprintf(read_end.fdopen("r").get(), "test");
  450. EXPECT_LT(result, 0);
  451. }
  452. #endif
  453. TEST(printf_test, wide_string) {
  454. EXPECT_EQ(L"abc", fmt::sprintf(L"%s", L"abc"));
  455. }
  456. TEST(printf_test, vprintf) {
  457. int n = 42;
  458. auto store = fmt::format_arg_store<fmt::printf_context, int>(n);
  459. auto args = fmt::basic_format_args<fmt::printf_context>(store);
  460. EXPECT_EQ(fmt::vsprintf(fmt::string_view("%d"), args), "42");
  461. EXPECT_WRITE(stdout, fmt::vfprintf(stdout, fmt::string_view("%d"), args),
  462. "42");
  463. }
  464. template <typename... Args>
  465. void check_format_string_regression(fmt::string_view s, const Args&... args) {
  466. fmt::sprintf(s, args...);
  467. }
  468. TEST(printf_test, check_format_string_regression) {
  469. check_format_string_regression("%c%s", 'x', "");
  470. }
  471. TEST(printf_test, fixed_large_exponent) {
  472. EXPECT_EQ("1000000000000000000000", fmt::sprintf("%.*f", -13, 1e21));
  473. }
  474. TEST(printf_test, make_printf_args) {
  475. EXPECT_EQ("[42] something happened",
  476. fmt::vsprintf(fmt::string_view("[%d] %s happened"),
  477. {fmt::make_printf_args(42, "something")}));
  478. EXPECT_EQ(L"[42] something happened",
  479. fmt::vsprintf(fmt::basic_string_view<wchar_t>(L"[%d] %s happened"),
  480. {fmt::make_wprintf_args(42, L"something")}));
  481. }