printf-test.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. safe_sprintf(buffer, "%#a", 16.0);
  187. EXPECT_PRINTF(buffer, "%#a", 16.0);
  188. safe_sprintf(buffer, "%#A", 16.0);
  189. EXPECT_PRINTF(buffer, "%#A", 16.0);
  190. // '#' flag is ignored for non-numeric types.
  191. EXPECT_PRINTF("x", "%#c", 'x');
  192. }
  193. TEST(printf_test, width) {
  194. EXPECT_PRINTF(" abc", "%5s", "abc");
  195. // Width cannot be specified twice.
  196. EXPECT_THROW_MSG(test_sprintf("%5-5d", 42), format_error,
  197. "invalid type specifier");
  198. EXPECT_THROW_MSG(test_sprintf(format("%{}d", big_num), 42), format_error,
  199. "number is too big");
  200. EXPECT_THROW_MSG(test_sprintf(format("%1${}d", big_num), 42), format_error,
  201. "number is too big");
  202. }
  203. TEST(printf_test, dynamic_width) {
  204. EXPECT_EQ(" 42", test_sprintf("%*d", 5, 42));
  205. EXPECT_EQ("42 ", test_sprintf("%*d", -5, 42));
  206. EXPECT_THROW_MSG(test_sprintf("%*d", 5.0, 42), format_error,
  207. "width is not integer");
  208. EXPECT_THROW_MSG(test_sprintf("%*d"), format_error, "argument not found");
  209. EXPECT_THROW_MSG(test_sprintf("%*d", big_num, 42), format_error,
  210. "number is too big");
  211. }
  212. TEST(printf_test, int_precision) {
  213. EXPECT_PRINTF("00042", "%.5d", 42);
  214. EXPECT_PRINTF("-00042", "%.5d", -42);
  215. EXPECT_PRINTF("00042", "%.5x", 0x42);
  216. EXPECT_PRINTF("0x00042", "%#.5x", 0x42);
  217. EXPECT_PRINTF("00042", "%.5o", 042);
  218. EXPECT_PRINTF("00042", "%#.5o", 042);
  219. EXPECT_PRINTF(" 00042", "%7.5d", 42);
  220. EXPECT_PRINTF(" 00042", "%7.5x", 0x42);
  221. EXPECT_PRINTF(" 0x00042", "%#10.5x", 0x42);
  222. EXPECT_PRINTF(" 00042", "%7.5o", 042);
  223. EXPECT_PRINTF(" 00042", "%#10.5o", 042);
  224. EXPECT_PRINTF("00042 ", "%-7.5d", 42);
  225. EXPECT_PRINTF("00042 ", "%-7.5x", 0x42);
  226. EXPECT_PRINTF("0x00042 ", "%-#10.5x", 0x42);
  227. EXPECT_PRINTF("00042 ", "%-7.5o", 042);
  228. EXPECT_PRINTF("00042 ", "%-#10.5o", 042);
  229. }
  230. TEST(printf_test, float_precision) {
  231. char buffer[256];
  232. safe_sprintf(buffer, "%.3e", 1234.5678);
  233. EXPECT_PRINTF(buffer, "%.3e", 1234.5678);
  234. EXPECT_PRINTF("1234.568", "%.3f", 1234.5678);
  235. EXPECT_PRINTF("1.23e+03", "%.3g", 1234.5678);
  236. safe_sprintf(buffer, "%.3a", 1234.5678);
  237. EXPECT_PRINTF(buffer, "%.3a", 1234.5678);
  238. }
  239. TEST(printf_test, string_precision) {
  240. char test[] = {'H', 'e', 'l', 'l', 'o'};
  241. EXPECT_EQ(fmt::sprintf("%.4s", test), "Hell");
  242. }
  243. TEST(printf_test, ignore_precision_for_non_numeric_arg) {
  244. EXPECT_PRINTF("abc", "%.5s", "abc");
  245. }
  246. TEST(printf_test, dynamic_precision) {
  247. EXPECT_EQ("00042", test_sprintf("%.*d", 5, 42));
  248. EXPECT_EQ("42", test_sprintf("%.*d", -5, 42));
  249. EXPECT_THROW_MSG(test_sprintf("%.*d", 5.0, 42), format_error,
  250. "precision is not integer");
  251. EXPECT_THROW_MSG(test_sprintf("%.*d"), format_error, "argument not found");
  252. EXPECT_THROW_MSG(test_sprintf("%.*d", big_num, 42), format_error,
  253. "number is too big");
  254. if (sizeof(long long) != sizeof(int)) {
  255. long long prec = static_cast<long long>(INT_MIN) - 1;
  256. EXPECT_THROW_MSG(test_sprintf("%.*d", prec, 42), format_error,
  257. "number is too big");
  258. }
  259. }
  260. template <typename T> struct make_signed { typedef T type; };
  261. #define SPECIALIZE_MAKE_SIGNED(T, S) \
  262. template <> struct make_signed<T> { typedef S type; }
  263. SPECIALIZE_MAKE_SIGNED(char, signed char);
  264. SPECIALIZE_MAKE_SIGNED(unsigned char, signed char);
  265. SPECIALIZE_MAKE_SIGNED(unsigned short, short);
  266. SPECIALIZE_MAKE_SIGNED(unsigned, int);
  267. SPECIALIZE_MAKE_SIGNED(unsigned long, long);
  268. SPECIALIZE_MAKE_SIGNED(unsigned long long, long long);
  269. // Test length format specifier ``length_spec``.
  270. template <typename T, typename U>
  271. void test_length(const char* length_spec, U value) {
  272. long long signed_value = 0;
  273. unsigned long long unsigned_value = 0;
  274. // Apply integer promotion to the argument.
  275. unsigned long long max = max_value<U>();
  276. using fmt::detail::const_check;
  277. if (const_check(max <= static_cast<unsigned>(max_value<int>()))) {
  278. signed_value = static_cast<int>(value);
  279. unsigned_value = static_cast<unsigned long long>(value);
  280. } else if (const_check(max <= max_value<unsigned>())) {
  281. signed_value = static_cast<unsigned>(value);
  282. unsigned_value = static_cast<unsigned long long>(value);
  283. }
  284. if (sizeof(U) <= sizeof(int) && sizeof(int) < sizeof(T)) {
  285. signed_value = static_cast<long long>(value);
  286. unsigned_value = static_cast<unsigned long long>(
  287. static_cast<typename std::make_unsigned<unsigned>::type>(value));
  288. } else {
  289. signed_value = static_cast<typename make_signed<T>::type>(value);
  290. unsigned_value = static_cast<typename std::make_unsigned<T>::type>(value);
  291. }
  292. std::ostringstream os;
  293. os << signed_value;
  294. EXPECT_PRINTF(os.str(), fmt::format("%{}d", length_spec), value);
  295. EXPECT_PRINTF(os.str(), fmt::format("%{}i", length_spec), value);
  296. os.str("");
  297. os << unsigned_value;
  298. EXPECT_PRINTF(os.str(), fmt::format("%{}u", length_spec), value);
  299. os.str("");
  300. os << std::oct << unsigned_value;
  301. EXPECT_PRINTF(os.str(), fmt::format("%{}o", length_spec), value);
  302. os.str("");
  303. os << std::hex << unsigned_value;
  304. EXPECT_PRINTF(os.str(), fmt::format("%{}x", length_spec), value);
  305. os.str("");
  306. os << std::hex << std::uppercase << unsigned_value;
  307. EXPECT_PRINTF(os.str(), fmt::format("%{}X", length_spec), value);
  308. }
  309. template <typename T> void test_length(const char* length_spec) {
  310. T min = std::numeric_limits<T>::min(), max = max_value<T>();
  311. test_length<T>(length_spec, 42);
  312. test_length<T>(length_spec, -42);
  313. test_length<T>(length_spec, min);
  314. test_length<T>(length_spec, max);
  315. long long long_long_min = std::numeric_limits<long long>::min();
  316. if (static_cast<long long>(min) > long_long_min)
  317. test_length<T>(length_spec, static_cast<long long>(min) - 1);
  318. unsigned long long long_long_max = max_value<long long>();
  319. if (static_cast<unsigned long long>(max) < long_long_max)
  320. test_length<T>(length_spec, static_cast<long long>(max) + 1);
  321. test_length<T>(length_spec, std::numeric_limits<short>::min());
  322. test_length<T>(length_spec, max_value<unsigned short>());
  323. test_length<T>(length_spec, std::numeric_limits<int>::min());
  324. test_length<T>(length_spec, max_value<int>());
  325. test_length<T>(length_spec, std::numeric_limits<unsigned>::min());
  326. test_length<T>(length_spec, max_value<unsigned>());
  327. test_length<T>(length_spec, std::numeric_limits<long long>::min());
  328. test_length<T>(length_spec, max_value<long long>());
  329. test_length<T>(length_spec, std::numeric_limits<unsigned long long>::min());
  330. test_length<T>(length_spec, max_value<unsigned long long>());
  331. }
  332. TEST(printf_test, length) {
  333. test_length<char>("hh");
  334. test_length<signed char>("hh");
  335. test_length<unsigned char>("hh");
  336. test_length<short>("h");
  337. test_length<unsigned short>("h");
  338. test_length<long>("l");
  339. test_length<unsigned long>("l");
  340. test_length<long long>("ll");
  341. test_length<unsigned long long>("ll");
  342. test_length<intmax_t>("j");
  343. test_length<size_t>("z");
  344. test_length<std::ptrdiff_t>("t");
  345. long double max = max_value<long double>();
  346. EXPECT_PRINTF(fmt::format("{:.6}", max), "%g", max);
  347. EXPECT_PRINTF(fmt::format("{:.6}", max), "%Lg", max);
  348. }
  349. TEST(printf_test, bool) {
  350. EXPECT_PRINTF("1", "%d", true);
  351. EXPECT_PRINTF("true", "%s", true);
  352. }
  353. TEST(printf_test, int) {
  354. EXPECT_PRINTF("-42", "%d", -42);
  355. EXPECT_PRINTF("-42", "%i", -42);
  356. unsigned u = 0 - 42u;
  357. EXPECT_PRINTF(fmt::format("{}", u), "%u", -42);
  358. EXPECT_PRINTF(fmt::format("{:o}", u), "%o", -42);
  359. EXPECT_PRINTF(fmt::format("{:x}", u), "%x", -42);
  360. EXPECT_PRINTF(fmt::format("{:X}", u), "%X", -42);
  361. }
  362. TEST(printf_test, long_long) {
  363. // fmt::printf allows passing long long arguments to %d without length
  364. // specifiers.
  365. long long max = max_value<long long>();
  366. EXPECT_PRINTF(fmt::format("{}", max), "%d", max);
  367. }
  368. TEST(printf_test, float) {
  369. EXPECT_PRINTF("392.650000", "%f", 392.65);
  370. EXPECT_PRINTF("392.65", "%.2f", 392.65);
  371. EXPECT_PRINTF("392.6", "%.1f", 392.65);
  372. EXPECT_PRINTF("393", "%.f", 392.65);
  373. EXPECT_PRINTF("392.650000", "%F", 392.65);
  374. char buffer[256];
  375. safe_sprintf(buffer, "%e", 392.65);
  376. EXPECT_PRINTF(buffer, "%e", 392.65);
  377. safe_sprintf(buffer, "%E", 392.65);
  378. EXPECT_PRINTF(buffer, "%E", 392.65);
  379. EXPECT_PRINTF("392.65", "%g", 392.65);
  380. EXPECT_PRINTF("392.65", "%G", 392.65);
  381. EXPECT_PRINTF("392", "%g", 392.0);
  382. EXPECT_PRINTF("392", "%G", 392.0);
  383. EXPECT_PRINTF("4.56e-07", "%g", 0.000000456);
  384. safe_sprintf(buffer, "%a", -392.65);
  385. EXPECT_EQ(buffer, format("{:a}", -392.65));
  386. safe_sprintf(buffer, "%A", -392.65);
  387. EXPECT_EQ(buffer, format("{:A}", -392.65));
  388. }
  389. TEST(printf_test, inf) {
  390. double inf = std::numeric_limits<double>::infinity();
  391. for (const char* type = "fega"; *type; ++type) {
  392. EXPECT_PRINTF("inf", fmt::format("%{}", *type), inf);
  393. char upper = static_cast<char>(std::toupper(*type));
  394. EXPECT_PRINTF("INF", fmt::format("%{}", upper), inf);
  395. }
  396. }
  397. TEST(printf_test, char) {
  398. EXPECT_PRINTF("x", "%c", 'x');
  399. int max = max_value<int>();
  400. EXPECT_PRINTF(fmt::format("{}", static_cast<char>(max)), "%c", max);
  401. // EXPECT_PRINTF("x", "%lc", L'x');
  402. EXPECT_PRINTF(L"x", L"%c", L'x');
  403. EXPECT_PRINTF(fmt::format(L"{}", static_cast<wchar_t>(max)), L"%c", max);
  404. }
  405. TEST(printf_test, string) {
  406. EXPECT_PRINTF("abc", "%s", "abc");
  407. const char* null_str = nullptr;
  408. EXPECT_PRINTF("(null)", "%s", null_str);
  409. EXPECT_PRINTF(" (null)", "%10s", null_str);
  410. EXPECT_PRINTF(L"abc", L"%s", L"abc");
  411. const wchar_t* null_wstr = nullptr;
  412. EXPECT_PRINTF(L"(null)", L"%s", null_wstr);
  413. EXPECT_PRINTF(L" (null)", L"%10s", null_wstr);
  414. }
  415. TEST(printf_test, pointer) {
  416. int n;
  417. void* p = &n;
  418. EXPECT_PRINTF(fmt::format("{}", p), "%p", p);
  419. p = nullptr;
  420. EXPECT_PRINTF("(nil)", "%p", p);
  421. EXPECT_PRINTF(" (nil)", "%10p", p);
  422. const char* s = "test";
  423. EXPECT_PRINTF(fmt::format("{:p}", s), "%p", s);
  424. const char* null_str = nullptr;
  425. EXPECT_PRINTF("(nil)", "%p", null_str);
  426. p = &n;
  427. EXPECT_PRINTF(fmt::format(L"{}", p), L"%p", p);
  428. p = nullptr;
  429. EXPECT_PRINTF(L"(nil)", L"%p", p);
  430. EXPECT_PRINTF(L" (nil)", L"%10p", p);
  431. const wchar_t* w = L"test";
  432. EXPECT_PRINTF(fmt::format(L"{:p}", w), L"%p", w);
  433. const wchar_t* null_wstr = nullptr;
  434. EXPECT_PRINTF(L"(nil)", L"%p", null_wstr);
  435. }
  436. enum test_enum { answer = 42 };
  437. auto format_as(test_enum e) -> int { return e; }
  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, vprintf) {
  462. fmt::format_arg_store<fmt::printf_context, int> as{42};
  463. fmt::basic_format_args<fmt::printf_context> args(as);
  464. EXPECT_EQ(fmt::vsprintf("%d", args), "42");
  465. EXPECT_WRITE(stdout, fmt::vprintf("%d", args), "42");
  466. EXPECT_WRITE(stdout, fmt::vfprintf(stdout, "%d", args), "42");
  467. }
  468. template <typename... Args>
  469. void check_format_string_regression(fmt::string_view s, const Args&... args) {
  470. fmt::sprintf(s, args...);
  471. }
  472. TEST(printf_test, check_format_string_regression) {
  473. check_format_string_regression("%c%s", 'x', "");
  474. }
  475. TEST(printf_test, fixed_large_exponent) {
  476. EXPECT_EQ("1000000000000000000000", fmt::sprintf("%.*f", -13, 1e21));
  477. }
  478. TEST(printf_test, vsprintf_make_args_example) {
  479. fmt::format_arg_store<fmt::printf_context, int, const char*> as{42,
  480. "something"};
  481. fmt::basic_format_args<fmt::printf_context> args(as);
  482. EXPECT_EQ("[42] something happened", fmt::vsprintf("[%d] %s happened", args));
  483. auto as2 = fmt::make_printf_args(42, "something");
  484. fmt::basic_format_args<fmt::printf_context> args2(as2);
  485. EXPECT_EQ("[42] something happened",
  486. fmt::vsprintf("[%d] %s happened", args2));
  487. EXPECT_EQ("[42] something happened",
  488. fmt::vsprintf("[%d] %s happened",
  489. {fmt::make_printf_args(42, "something")}));
  490. }
  491. TEST(printf_test, vsprintf_make_wargs_example) {
  492. fmt::format_arg_store<fmt::wprintf_context, int, const wchar_t*> as{
  493. 42, L"something"};
  494. fmt::basic_format_args<fmt::wprintf_context> args(as);
  495. EXPECT_EQ(L"[42] something happened",
  496. fmt::vsprintf(L"[%d] %s happened", args));
  497. auto as2 = fmt::make_wprintf_args(42, L"something");
  498. fmt::basic_format_args<fmt::wprintf_context> args2(as2);
  499. EXPECT_EQ(L"[42] something happened",
  500. fmt::vsprintf(L"[%d] %s happened", args2));
  501. EXPECT_EQ(L"[42] something happened",
  502. fmt::vsprintf(L"[%d] %s happened",
  503. {fmt::make_wprintf_args(42, L"something")}));
  504. }