printf-test.cc 19 KB

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