printf-test.cc 20 KB

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