printf-test.cc 21 KB

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