format-test.cc 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190
  1. // Formatting library for C++ - formatting library tests
  2. //
  3. // Copyright (c) 2012 - present, Victor Zverovich
  4. // All rights reserved.
  5. //
  6. // For the license information refer to format.h.
  7. // Check if fmt/format.h compiles with windows.h included before it.
  8. #ifdef _WIN32
  9. # include <windows.h>
  10. #endif
  11. // clang-format off
  12. #include "fmt/format.h"
  13. // clang-format on
  14. #include <stdint.h> // uint32_t
  15. #include <climits> // INT_MAX
  16. #include <cmath> // std::signbit
  17. #include <cstring> // std::strlen
  18. #include <iterator> // std::back_inserter
  19. #include <list> // std::list
  20. #include <memory> // std::unique_ptr
  21. #include <type_traits> // std::is_default_constructible
  22. #include "gtest-extra.h"
  23. #include "mock-allocator.h"
  24. #include "util.h"
  25. using fmt::basic_memory_buffer;
  26. using fmt::format_error;
  27. using fmt::memory_buffer;
  28. using fmt::runtime;
  29. using fmt::string_view;
  30. using fmt::detail::max_value;
  31. using testing::Return;
  32. using testing::StrictMock;
  33. enum { buffer_size = 256 };
  34. struct uint32_pair {
  35. uint32_t u[2];
  36. };
  37. TEST(util_test, bit_cast) {
  38. auto s = fmt::detail::bit_cast<uint32_pair>(uint64_t{42});
  39. EXPECT_EQ(fmt::detail::bit_cast<uint64_t>(s), 42ull);
  40. s = fmt::detail::bit_cast<uint32_pair>(~uint64_t{0});
  41. EXPECT_EQ(fmt::detail::bit_cast<uint64_t>(s), ~0ull);
  42. }
  43. // Increment a number in a string.
  44. void increment(char* s) {
  45. for (int i = static_cast<int>(std::strlen(s)) - 1; i >= 0; --i) {
  46. if (s[i] != '9') {
  47. ++s[i];
  48. break;
  49. }
  50. s[i] = '0';
  51. }
  52. }
  53. TEST(util_test, increment) {
  54. char s[10] = "123";
  55. increment(s);
  56. EXPECT_STREQ("124", s);
  57. s[2] = '8';
  58. increment(s);
  59. EXPECT_STREQ("129", s);
  60. increment(s);
  61. EXPECT_STREQ("130", s);
  62. s[1] = s[2] = '9';
  63. increment(s);
  64. EXPECT_STREQ("200", s);
  65. }
  66. TEST(util_test, parse_nonnegative_int) {
  67. auto s = fmt::string_view("10000000000");
  68. auto begin = s.begin(), end = s.end();
  69. EXPECT_EQ(fmt::detail::parse_nonnegative_int(begin, end, -1), -1);
  70. s = "2147483649";
  71. begin = s.begin();
  72. end = s.end();
  73. EXPECT_EQ(fmt::detail::parse_nonnegative_int(begin, end, -1), -1);
  74. }
  75. TEST(util_test, utf8_to_utf16) {
  76. auto u = fmt::detail::utf8_to_utf16("лошадка");
  77. EXPECT_EQ(L"\x043B\x043E\x0448\x0430\x0434\x043A\x0430", u.str());
  78. EXPECT_EQ(7, u.size());
  79. // U+10437 { DESERET SMALL LETTER YEE }
  80. EXPECT_EQ(L"\xD801\xDC37", fmt::detail::utf8_to_utf16("𐐷").str());
  81. EXPECT_THROW_MSG(fmt::detail::utf8_to_utf16("\xc3\x28"), std::runtime_error,
  82. "invalid utf8");
  83. EXPECT_THROW_MSG(fmt::detail::utf8_to_utf16(fmt::string_view("л", 1)),
  84. std::runtime_error, "invalid utf8");
  85. EXPECT_EQ(L"123456", fmt::detail::utf8_to_utf16("123456").str());
  86. }
  87. TEST(util_test, utf8_to_utf16_empty_string) {
  88. auto s = std::string();
  89. auto u = fmt::detail::utf8_to_utf16(s.c_str());
  90. EXPECT_EQ(L"", u.str());
  91. EXPECT_EQ(s.size(), u.size());
  92. }
  93. TEST(util_test, allocator_ref) {
  94. using test_allocator_ref = allocator_ref<mock_allocator<int>>;
  95. auto check_forwarding = [](mock_allocator<int>& alloc,
  96. test_allocator_ref& ref) {
  97. int mem;
  98. // Check if value_type is properly defined.
  99. allocator_ref<mock_allocator<int>>::value_type* ptr = &mem;
  100. // Check forwarding.
  101. EXPECT_CALL(alloc, allocate(42)).WillOnce(Return(ptr));
  102. ref.allocate(42);
  103. EXPECT_CALL(alloc, deallocate(ptr, 42));
  104. ref.deallocate(ptr, 42);
  105. };
  106. StrictMock<mock_allocator<int>> alloc;
  107. auto ref = test_allocator_ref(&alloc);
  108. // Check if allocator_ref forwards to the underlying allocator.
  109. check_forwarding(alloc, ref);
  110. test_allocator_ref ref2(ref);
  111. check_forwarding(alloc, ref2);
  112. test_allocator_ref ref3;
  113. EXPECT_EQ(nullptr, ref3.get());
  114. ref3 = ref;
  115. check_forwarding(alloc, ref3);
  116. }
  117. TEST(util_test, format_system_error) {
  118. fmt::memory_buffer message;
  119. fmt::format_system_error(message, EDOM, "test");
  120. auto ec = std::error_code(EDOM, std::generic_category());
  121. EXPECT_EQ(to_string(message), std::system_error(ec, "test").what());
  122. message = fmt::memory_buffer();
  123. // Check if std::allocator throws on allocating max size_t / 2 chars.
  124. size_t max_size = max_value<size_t>() / 2;
  125. bool throws_on_alloc = false;
  126. try {
  127. auto alloc = std::allocator<char>();
  128. alloc.deallocate(alloc.allocate(max_size), max_size);
  129. } catch (const std::bad_alloc&) {
  130. throws_on_alloc = true;
  131. }
  132. if (!throws_on_alloc) {
  133. fmt::print("warning: std::allocator allocates {} chars", max_size);
  134. return;
  135. }
  136. }
  137. TEST(util_test, system_error) {
  138. auto test_error = fmt::system_error(EDOM, "test");
  139. auto ec = std::error_code(EDOM, std::generic_category());
  140. EXPECT_STREQ(test_error.what(), std::system_error(ec, "test").what());
  141. EXPECT_EQ(test_error.code(), ec);
  142. auto error = std::system_error(std::error_code());
  143. try {
  144. throw fmt::system_error(EDOM, "test {}", "error");
  145. } catch (const std::system_error& e) {
  146. error = e;
  147. }
  148. fmt::memory_buffer message;
  149. fmt::format_system_error(message, EDOM, "test error");
  150. EXPECT_EQ(error.what(), to_string(message));
  151. EXPECT_EQ(error.code(), std::error_code(EDOM, std::generic_category()));
  152. }
  153. TEST(util_test, report_system_error) {
  154. fmt::memory_buffer out;
  155. fmt::format_system_error(out, EDOM, "test error");
  156. out.push_back('\n');
  157. EXPECT_WRITE(stderr, fmt::report_system_error(EDOM, "test error"),
  158. to_string(out));
  159. }
  160. TEST(memory_buffer_test, ctor) {
  161. basic_memory_buffer<char, 123> buffer;
  162. EXPECT_EQ(static_cast<size_t>(0), buffer.size());
  163. EXPECT_EQ(123u, buffer.capacity());
  164. }
  165. using std_allocator = allocator_ref<std::allocator<char>>;
  166. TEST(memory_buffer_test, move_ctor_inline_buffer) {
  167. auto check_move_buffer =
  168. [](const char* str, basic_memory_buffer<char, 5, std_allocator>& buffer) {
  169. std::allocator<char>* alloc = buffer.get_allocator().get();
  170. basic_memory_buffer<char, 5, std_allocator> buffer2(std::move(buffer));
  171. // Move shouldn't destroy the inline content of the first buffer.
  172. EXPECT_EQ(str, std::string(&buffer[0], buffer.size()));
  173. EXPECT_EQ(str, std::string(&buffer2[0], buffer2.size()));
  174. EXPECT_EQ(5u, buffer2.capacity());
  175. // Move should transfer allocator.
  176. EXPECT_EQ(nullptr, buffer.get_allocator().get());
  177. EXPECT_EQ(alloc, buffer2.get_allocator().get());
  178. };
  179. auto alloc = std::allocator<char>();
  180. basic_memory_buffer<char, 5, std_allocator> buffer((std_allocator(&alloc)));
  181. const char test[] = "test";
  182. buffer.append(string_view(test, 4));
  183. check_move_buffer("test", buffer);
  184. // Adding one more character fills the inline buffer, but doesn't cause
  185. // dynamic allocation.
  186. buffer.push_back('a');
  187. check_move_buffer("testa", buffer);
  188. }
  189. TEST(memory_buffer_test, move_ctor_dynamic_buffer) {
  190. auto alloc = std::allocator<char>();
  191. basic_memory_buffer<char, 4, std_allocator> buffer((std_allocator(&alloc)));
  192. const char test[] = "test";
  193. buffer.append(test, test + 4);
  194. const char* inline_buffer_ptr = &buffer[0];
  195. // Adding one more character causes the content to move from the inline to
  196. // a dynamically allocated buffer.
  197. buffer.push_back('a');
  198. basic_memory_buffer<char, 4, std_allocator> buffer2(std::move(buffer));
  199. // Move should rip the guts of the first buffer.
  200. EXPECT_EQ(inline_buffer_ptr, &buffer[0]);
  201. EXPECT_EQ("testa", std::string(&buffer2[0], buffer2.size()));
  202. EXPECT_GT(buffer2.capacity(), 4u);
  203. }
  204. void check_move_assign_buffer(const char* str,
  205. basic_memory_buffer<char, 5>& buffer) {
  206. basic_memory_buffer<char, 5> buffer2;
  207. buffer2 = std::move(buffer);
  208. // Move shouldn't destroy the inline content of the first buffer.
  209. EXPECT_EQ(str, std::string(&buffer[0], buffer.size()));
  210. EXPECT_EQ(str, std::string(&buffer2[0], buffer2.size()));
  211. EXPECT_EQ(5u, buffer2.capacity());
  212. }
  213. TEST(memory_buffer_test, move_assignment) {
  214. basic_memory_buffer<char, 5> buffer;
  215. const char test[] = "test";
  216. buffer.append(test, test + 4);
  217. check_move_assign_buffer("test", buffer);
  218. // Adding one more character fills the inline buffer, but doesn't cause
  219. // dynamic allocation.
  220. buffer.push_back('a');
  221. check_move_assign_buffer("testa", buffer);
  222. const char* inline_buffer_ptr = &buffer[0];
  223. // Adding one more character causes the content to move from the inline to
  224. // a dynamically allocated buffer.
  225. buffer.push_back('b');
  226. basic_memory_buffer<char, 5> buffer2;
  227. buffer2 = std::move(buffer);
  228. // Move should rip the guts of the first buffer.
  229. EXPECT_EQ(inline_buffer_ptr, &buffer[0]);
  230. EXPECT_EQ("testab", std::string(&buffer2[0], buffer2.size()));
  231. EXPECT_GT(buffer2.capacity(), 5u);
  232. }
  233. TEST(memory_buffer_test, grow) {
  234. typedef allocator_ref<mock_allocator<int>> Allocator;
  235. mock_allocator<int> alloc;
  236. basic_memory_buffer<int, 10, Allocator> buffer((Allocator(&alloc)));
  237. buffer.resize(7);
  238. using fmt::detail::to_unsigned;
  239. for (int i = 0; i < 7; ++i) buffer[to_unsigned(i)] = i * i;
  240. EXPECT_EQ(10u, buffer.capacity());
  241. int mem[20];
  242. mem[7] = 0xdead;
  243. EXPECT_CALL(alloc, allocate(20)).WillOnce(Return(mem));
  244. buffer.try_reserve(20);
  245. EXPECT_EQ(20u, buffer.capacity());
  246. // Check if size elements have been copied
  247. for (int i = 0; i < 7; ++i) EXPECT_EQ(i * i, buffer[to_unsigned(i)]);
  248. // and no more than that.
  249. EXPECT_EQ(0xdead, buffer[7]);
  250. EXPECT_CALL(alloc, deallocate(mem, 20));
  251. }
  252. TEST(memory_buffer_test, allocator) {
  253. using test_allocator = allocator_ref<mock_allocator<char>>;
  254. basic_memory_buffer<char, 10, test_allocator> buffer;
  255. EXPECT_EQ(nullptr, buffer.get_allocator().get());
  256. StrictMock<mock_allocator<char>> alloc;
  257. char mem;
  258. {
  259. basic_memory_buffer<char, 10, test_allocator> buffer2(
  260. (test_allocator(&alloc)));
  261. EXPECT_EQ(&alloc, buffer2.get_allocator().get());
  262. size_t size = 2 * fmt::inline_buffer_size;
  263. EXPECT_CALL(alloc, allocate(size)).WillOnce(Return(&mem));
  264. buffer2.reserve(size);
  265. EXPECT_CALL(alloc, deallocate(&mem, size));
  266. }
  267. }
  268. TEST(memory_buffer_test, exception_in_deallocate) {
  269. using test_allocator = allocator_ref<mock_allocator<char>>;
  270. StrictMock<mock_allocator<char>> alloc;
  271. basic_memory_buffer<char, 10, test_allocator> buffer(
  272. (test_allocator(&alloc)));
  273. size_t size = 2 * fmt::inline_buffer_size;
  274. auto mem = std::vector<char>(size);
  275. {
  276. EXPECT_CALL(alloc, allocate(size)).WillOnce(Return(&mem[0]));
  277. buffer.resize(size);
  278. std::fill(&buffer[0], &buffer[0] + size, 'x');
  279. }
  280. auto mem2 = std::vector<char>(2 * size);
  281. {
  282. EXPECT_CALL(alloc, allocate(2 * size)).WillOnce(Return(&mem2[0]));
  283. auto e = std::exception();
  284. EXPECT_CALL(alloc, deallocate(&mem[0], size)).WillOnce(testing::Throw(e));
  285. EXPECT_THROW(buffer.reserve(2 * size), std::exception);
  286. EXPECT_EQ(&mem2[0], &buffer[0]);
  287. // Check that the data has been copied.
  288. for (size_t i = 0; i < size; ++i) EXPECT_EQ('x', buffer[i]);
  289. }
  290. EXPECT_CALL(alloc, deallocate(&mem2[0], 2 * size));
  291. }
  292. template <typename Allocator, size_t MaxSize>
  293. class max_size_allocator : public Allocator {
  294. public:
  295. using typename Allocator::value_type;
  296. size_t max_size() const FMT_NOEXCEPT { return MaxSize; }
  297. value_type* allocate(size_t n) {
  298. if (n > max_size()) {
  299. throw std::length_error("size > max_size");
  300. }
  301. return std::allocator_traits<Allocator>::allocate(
  302. *static_cast<Allocator*>(this), n);
  303. }
  304. void deallocate(value_type* p, size_t n) {
  305. std::allocator_traits<Allocator>::deallocate(*static_cast<Allocator*>(this),
  306. p, n);
  307. }
  308. };
  309. TEST(memory_buffer_test, max_size_allocator) {
  310. // 160 = 128 + 32
  311. using test_allocator = max_size_allocator<std::allocator<char>, 160>;
  312. basic_memory_buffer<char, 10, test_allocator> buffer;
  313. buffer.resize(128);
  314. // new_capacity = 128 + 128/2 = 192 > 160
  315. buffer.resize(160); // Shouldn't throw.
  316. }
  317. TEST(memory_buffer_test, max_size_allocator_overflow) {
  318. using test_allocator = max_size_allocator<std::allocator<char>, 160>;
  319. basic_memory_buffer<char, 10, test_allocator> buffer;
  320. EXPECT_THROW(buffer.resize(161), std::exception);
  321. }
  322. TEST(format_test, escape) {
  323. EXPECT_EQ("{", fmt::format("{{"));
  324. EXPECT_EQ("before {", fmt::format("before {{"));
  325. EXPECT_EQ("{ after", fmt::format("{{ after"));
  326. EXPECT_EQ("before { after", fmt::format("before {{ after"));
  327. EXPECT_EQ("}", fmt::format("}}"));
  328. EXPECT_EQ("before }", fmt::format("before }}"));
  329. EXPECT_EQ("} after", fmt::format("}} after"));
  330. EXPECT_EQ("before } after", fmt::format("before }} after"));
  331. EXPECT_EQ("{}", fmt::format("{{}}"));
  332. EXPECT_EQ("{42}", fmt::format("{{{0}}}", 42));
  333. }
  334. TEST(format_test, unmatched_braces) {
  335. EXPECT_THROW_MSG((void)fmt::format(runtime("{")), format_error,
  336. "invalid format string");
  337. EXPECT_THROW_MSG((void)fmt::format(runtime("}")), format_error,
  338. "unmatched '}' in format string");
  339. EXPECT_THROW_MSG((void)fmt::format(runtime("{0{}")), format_error,
  340. "invalid format string");
  341. }
  342. TEST(format_test, no_args) { EXPECT_EQ("test", fmt::format("test")); }
  343. TEST(format_test, args_in_different_positions) {
  344. EXPECT_EQ("42", fmt::format("{0}", 42));
  345. EXPECT_EQ("before 42", fmt::format("before {0}", 42));
  346. EXPECT_EQ("42 after", fmt::format("{0} after", 42));
  347. EXPECT_EQ("before 42 after", fmt::format("before {0} after", 42));
  348. EXPECT_EQ("answer = 42", fmt::format("{0} = {1}", "answer", 42));
  349. EXPECT_EQ("42 is the answer", fmt::format("{1} is the {0}", "answer", 42));
  350. EXPECT_EQ("abracadabra", fmt::format("{0}{1}{0}", "abra", "cad"));
  351. }
  352. TEST(format_test, arg_errors) {
  353. EXPECT_THROW_MSG((void)fmt::format(runtime("{")), format_error,
  354. "invalid format string");
  355. EXPECT_THROW_MSG((void)fmt::format(runtime("{?}")), format_error,
  356. "invalid format string");
  357. EXPECT_THROW_MSG((void)fmt::format(runtime("{0")), format_error,
  358. "invalid format string");
  359. EXPECT_THROW_MSG((void)fmt::format(runtime("{0}")), format_error,
  360. "argument not found");
  361. EXPECT_THROW_MSG((void)fmt::format(runtime("{00}"), 42), format_error,
  362. "invalid format string");
  363. char format_str[buffer_size];
  364. safe_sprintf(format_str, "{%u", INT_MAX);
  365. EXPECT_THROW_MSG((void)fmt::format(runtime(format_str)), format_error,
  366. "invalid format string");
  367. safe_sprintf(format_str, "{%u}", INT_MAX);
  368. EXPECT_THROW_MSG((void)fmt::format(runtime(format_str)), format_error,
  369. "argument not found");
  370. safe_sprintf(format_str, "{%u", INT_MAX + 1u);
  371. EXPECT_THROW_MSG((void)fmt::format(runtime(format_str)), format_error,
  372. "invalid format string");
  373. safe_sprintf(format_str, "{%u}", INT_MAX + 1u);
  374. EXPECT_THROW_MSG((void)fmt::format(runtime(format_str)), format_error,
  375. "argument not found");
  376. }
  377. template <int N> struct test_format {
  378. template <typename... T>
  379. static std::string format(fmt::string_view fmt, const T&... args) {
  380. return test_format<N - 1>::format(fmt, N - 1, args...);
  381. }
  382. };
  383. template <> struct test_format<0> {
  384. template <typename... T>
  385. static std::string format(fmt::string_view fmt, const T&... args) {
  386. return fmt::format(runtime(fmt), args...);
  387. }
  388. };
  389. TEST(format_test, many_args) {
  390. EXPECT_EQ("19", test_format<20>::format("{19}"));
  391. EXPECT_THROW_MSG(test_format<20>::format("{20}"), format_error,
  392. "argument not found");
  393. EXPECT_THROW_MSG(test_format<21>::format("{21}"), format_error,
  394. "argument not found");
  395. using fmt::detail::max_packed_args;
  396. std::string format_str = fmt::format("{{{}}}", max_packed_args + 1);
  397. EXPECT_THROW_MSG(test_format<max_packed_args>::format(format_str),
  398. format_error, "argument not found");
  399. }
  400. TEST(format_test, named_arg) {
  401. EXPECT_EQ("1/a/A", fmt::format("{_1}/{a_}/{A_}", fmt::arg("a_", 'a'),
  402. fmt::arg("A_", "A"), fmt::arg("_1", 1)));
  403. EXPECT_EQ(" -42", fmt::format("{0:{width}}", -42, fmt::arg("width", 4)));
  404. EXPECT_EQ("st",
  405. fmt::format("{0:.{precision}}", "str", fmt::arg("precision", 2)));
  406. EXPECT_EQ("1 2", fmt::format("{} {two}", 1, fmt::arg("two", 2)));
  407. EXPECT_EQ("42",
  408. fmt::format("{c}", fmt::arg("a", 0), fmt::arg("b", 0),
  409. fmt::arg("c", 42), fmt::arg("d", 0), fmt::arg("e", 0),
  410. fmt::arg("f", 0), fmt::arg("g", 0), fmt::arg("h", 0),
  411. fmt::arg("i", 0), fmt::arg("j", 0), fmt::arg("k", 0),
  412. fmt::arg("l", 0), fmt::arg("m", 0), fmt::arg("n", 0),
  413. fmt::arg("o", 0), fmt::arg("p", 0)));
  414. EXPECT_THROW_MSG((void)fmt::format(runtime("{a}")), format_error,
  415. "argument not found");
  416. EXPECT_THROW_MSG((void)fmt::format(runtime("{a}"), 42), format_error,
  417. "argument not found");
  418. }
  419. TEST(format_test, auto_arg_index) {
  420. EXPECT_EQ("abc", fmt::format("{}{}{}", 'a', 'b', 'c'));
  421. EXPECT_THROW_MSG((void)fmt::format(runtime("{0}{}"), 'a', 'b'), format_error,
  422. "cannot switch from manual to automatic argument indexing");
  423. EXPECT_THROW_MSG((void)fmt::format(runtime("{}{0}"), 'a', 'b'), format_error,
  424. "cannot switch from automatic to manual argument indexing");
  425. EXPECT_EQ("1.2", fmt::format("{:.{}}", 1.2345, 2));
  426. EXPECT_THROW_MSG((void)fmt::format(runtime("{0}:.{}"), 1.2345, 2),
  427. format_error,
  428. "cannot switch from manual to automatic argument indexing");
  429. EXPECT_THROW_MSG((void)fmt::format(runtime("{:.{0}}"), 1.2345, 2),
  430. format_error,
  431. "cannot switch from automatic to manual argument indexing");
  432. EXPECT_THROW_MSG((void)fmt::format(runtime("{}")), format_error,
  433. "argument not found");
  434. }
  435. TEST(format_test, empty_specs) { EXPECT_EQ("42", fmt::format("{0:}", 42)); }
  436. TEST(format_test, left_align) {
  437. EXPECT_EQ("42 ", fmt::format("{0:<4}", 42));
  438. EXPECT_EQ("42 ", fmt::format("{0:<4o}", 042));
  439. EXPECT_EQ("42 ", fmt::format("{0:<4x}", 0x42));
  440. EXPECT_EQ("-42 ", fmt::format("{0:<5}", -42));
  441. EXPECT_EQ("42 ", fmt::format("{0:<5}", 42u));
  442. EXPECT_EQ("-42 ", fmt::format("{0:<5}", -42l));
  443. EXPECT_EQ("42 ", fmt::format("{0:<5}", 42ul));
  444. EXPECT_EQ("-42 ", fmt::format("{0:<5}", -42ll));
  445. EXPECT_EQ("42 ", fmt::format("{0:<5}", 42ull));
  446. EXPECT_EQ("-42 ", fmt::format("{0:<5}", -42.0));
  447. EXPECT_EQ("-42 ", fmt::format("{0:<5}", -42.0l));
  448. EXPECT_EQ("c ", fmt::format("{0:<5}", 'c'));
  449. EXPECT_EQ("abc ", fmt::format("{0:<5}", "abc"));
  450. EXPECT_EQ("0xface ", fmt::format("{0:<8}", reinterpret_cast<void*>(0xface)));
  451. }
  452. TEST(format_test, right_align) {
  453. EXPECT_EQ(" 42", fmt::format("{0:>4}", 42));
  454. EXPECT_EQ(" 42", fmt::format("{0:>4o}", 042));
  455. EXPECT_EQ(" 42", fmt::format("{0:>4x}", 0x42));
  456. EXPECT_EQ(" -42", fmt::format("{0:>5}", -42));
  457. EXPECT_EQ(" 42", fmt::format("{0:>5}", 42u));
  458. EXPECT_EQ(" -42", fmt::format("{0:>5}", -42l));
  459. EXPECT_EQ(" 42", fmt::format("{0:>5}", 42ul));
  460. EXPECT_EQ(" -42", fmt::format("{0:>5}", -42ll));
  461. EXPECT_EQ(" 42", fmt::format("{0:>5}", 42ull));
  462. EXPECT_EQ(" -42", fmt::format("{0:>5}", -42.0));
  463. EXPECT_EQ(" -42", fmt::format("{0:>5}", -42.0l));
  464. EXPECT_EQ(" c", fmt::format("{0:>5}", 'c'));
  465. EXPECT_EQ(" abc", fmt::format("{0:>5}", "abc"));
  466. EXPECT_EQ(" 0xface", fmt::format("{0:>8}", reinterpret_cast<void*>(0xface)));
  467. }
  468. TEST(format_test, center_align) {
  469. EXPECT_EQ(" 42 ", fmt::format("{0:^5}", 42));
  470. EXPECT_EQ(" 42 ", fmt::format("{0:^5o}", 042));
  471. EXPECT_EQ(" 42 ", fmt::format("{0:^5x}", 0x42));
  472. EXPECT_EQ(" -42 ", fmt::format("{0:^5}", -42));
  473. EXPECT_EQ(" 42 ", fmt::format("{0:^5}", 42u));
  474. EXPECT_EQ(" -42 ", fmt::format("{0:^5}", -42l));
  475. EXPECT_EQ(" 42 ", fmt::format("{0:^5}", 42ul));
  476. EXPECT_EQ(" -42 ", fmt::format("{0:^5}", -42ll));
  477. EXPECT_EQ(" 42 ", fmt::format("{0:^5}", 42ull));
  478. EXPECT_EQ(" -42 ", fmt::format("{0:^5}", -42.0));
  479. EXPECT_EQ(" -42 ", fmt::format("{0:^5}", -42.0l));
  480. EXPECT_EQ(" c ", fmt::format("{0:^5}", 'c'));
  481. EXPECT_EQ(" abc ", fmt::format("{0:^6}", "abc"));
  482. EXPECT_EQ(" 0xface ", fmt::format("{0:^8}", reinterpret_cast<void*>(0xface)));
  483. }
  484. TEST(format_test, fill) {
  485. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{<5}"), 'c'), format_error,
  486. "invalid fill character '{'");
  487. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{<5}}"), 'c'), format_error,
  488. "invalid fill character '{'");
  489. EXPECT_EQ("**42", fmt::format("{0:*>4}", 42));
  490. EXPECT_EQ("**-42", fmt::format("{0:*>5}", -42));
  491. EXPECT_EQ("***42", fmt::format("{0:*>5}", 42u));
  492. EXPECT_EQ("**-42", fmt::format("{0:*>5}", -42l));
  493. EXPECT_EQ("***42", fmt::format("{0:*>5}", 42ul));
  494. EXPECT_EQ("**-42", fmt::format("{0:*>5}", -42ll));
  495. EXPECT_EQ("***42", fmt::format("{0:*>5}", 42ull));
  496. EXPECT_EQ("**-42", fmt::format("{0:*>5}", -42.0));
  497. EXPECT_EQ("**-42", fmt::format("{0:*>5}", -42.0l));
  498. EXPECT_EQ("c****", fmt::format("{0:*<5}", 'c'));
  499. EXPECT_EQ("abc**", fmt::format("{0:*<5}", "abc"));
  500. EXPECT_EQ("**0xface",
  501. fmt::format("{0:*>8}", reinterpret_cast<void*>(0xface)));
  502. EXPECT_EQ("foo=", fmt::format("{:}=", "foo"));
  503. EXPECT_EQ(std::string("\0\0\0*", 4),
  504. fmt::format(string_view("{:\0>4}", 6), '*'));
  505. EXPECT_EQ("жж42", fmt::format("{0:ж>4}", 42));
  506. EXPECT_THROW_MSG((void)fmt::format(runtime("{:\x80\x80\x80\x80\x80>}"), 0),
  507. format_error, "invalid type specifier");
  508. }
  509. TEST(format_test, plus_sign) {
  510. EXPECT_EQ("+42", fmt::format("{0:+}", 42));
  511. EXPECT_EQ("-42", fmt::format("{0:+}", -42));
  512. EXPECT_EQ("+42", fmt::format("{0:+}", 42));
  513. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:+}"), 42u), format_error,
  514. "format specifier requires signed argument");
  515. EXPECT_EQ("+42", fmt::format("{0:+}", 42l));
  516. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:+}"), 42ul), format_error,
  517. "format specifier requires signed argument");
  518. EXPECT_EQ("+42", fmt::format("{0:+}", 42ll));
  519. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:+}"), 42ull), format_error,
  520. "format specifier requires signed argument");
  521. EXPECT_EQ("+42", fmt::format("{0:+}", 42.0));
  522. EXPECT_EQ("+42", fmt::format("{0:+}", 42.0l));
  523. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:+"), 'c'), format_error,
  524. "missing '}' in format string");
  525. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:+}"), 'c'), format_error,
  526. "invalid format specifier for char");
  527. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:+}"), "abc"), format_error,
  528. "format specifier requires numeric argument");
  529. EXPECT_THROW_MSG(
  530. (void)fmt::format(runtime("{0:+}"), reinterpret_cast<void*>(0x42)),
  531. format_error, "format specifier requires numeric argument");
  532. }
  533. TEST(format_test, minus_sign) {
  534. EXPECT_EQ("42", fmt::format("{0:-}", 42));
  535. EXPECT_EQ("-42", fmt::format("{0:-}", -42));
  536. EXPECT_EQ("42", fmt::format("{0:-}", 42));
  537. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:-}"), 42u), format_error,
  538. "format specifier requires signed argument");
  539. EXPECT_EQ("42", fmt::format("{0:-}", 42l));
  540. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:-}"), 42ul), format_error,
  541. "format specifier requires signed argument");
  542. EXPECT_EQ("42", fmt::format("{0:-}", 42ll));
  543. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:-}"), 42ull), format_error,
  544. "format specifier requires signed argument");
  545. EXPECT_EQ("42", fmt::format("{0:-}", 42.0));
  546. EXPECT_EQ("42", fmt::format("{0:-}", 42.0l));
  547. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:-"), 'c'), format_error,
  548. "missing '}' in format string");
  549. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:-}"), 'c'), format_error,
  550. "invalid format specifier for char");
  551. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:-}"), "abc"), format_error,
  552. "format specifier requires numeric argument");
  553. EXPECT_THROW_MSG(
  554. (void)fmt::format(runtime("{0:-}"), reinterpret_cast<void*>(0x42)),
  555. format_error, "format specifier requires numeric argument");
  556. }
  557. TEST(format_test, space_sign) {
  558. EXPECT_EQ(" 42", fmt::format("{0: }", 42));
  559. EXPECT_EQ("-42", fmt::format("{0: }", -42));
  560. EXPECT_EQ(" 42", fmt::format("{0: }", 42));
  561. EXPECT_THROW_MSG((void)fmt::format(runtime("{0: }"), 42u), format_error,
  562. "format specifier requires signed argument");
  563. EXPECT_EQ(" 42", fmt::format("{0: }", 42l));
  564. EXPECT_THROW_MSG((void)fmt::format(runtime("{0: }"), 42ul), format_error,
  565. "format specifier requires signed argument");
  566. EXPECT_EQ(" 42", fmt::format("{0: }", 42ll));
  567. EXPECT_THROW_MSG((void)fmt::format(runtime("{0: }"), 42ull), format_error,
  568. "format specifier requires signed argument");
  569. EXPECT_EQ(" 42", fmt::format("{0: }", 42.0));
  570. EXPECT_EQ(" 42", fmt::format("{0: }", 42.0l));
  571. EXPECT_THROW_MSG((void)fmt::format(runtime("{0: "), 'c'), format_error,
  572. "missing '}' in format string");
  573. EXPECT_THROW_MSG((void)fmt::format(runtime("{0: }"), 'c'), format_error,
  574. "invalid format specifier for char");
  575. EXPECT_THROW_MSG((void)fmt::format(runtime("{0: }"), "abc"), format_error,
  576. "format specifier requires numeric argument");
  577. EXPECT_THROW_MSG(
  578. (void)fmt::format(runtime("{0: }"), reinterpret_cast<void*>(0x42)),
  579. format_error, "format specifier requires numeric argument");
  580. }
  581. TEST(format_test, hash_flag) {
  582. EXPECT_EQ("42", fmt::format("{0:#}", 42));
  583. EXPECT_EQ("-42", fmt::format("{0:#}", -42));
  584. EXPECT_EQ("0b101010", fmt::format("{0:#b}", 42));
  585. EXPECT_EQ("0B101010", fmt::format("{0:#B}", 42));
  586. EXPECT_EQ("-0b101010", fmt::format("{0:#b}", -42));
  587. EXPECT_EQ("0x42", fmt::format("{0:#x}", 0x42));
  588. EXPECT_EQ("0X42", fmt::format("{0:#X}", 0x42));
  589. EXPECT_EQ("-0x42", fmt::format("{0:#x}", -0x42));
  590. EXPECT_EQ("0", fmt::format("{0:#o}", 0));
  591. EXPECT_EQ("042", fmt::format("{0:#o}", 042));
  592. EXPECT_EQ("-042", fmt::format("{0:#o}", -042));
  593. EXPECT_EQ("42", fmt::format("{0:#}", 42u));
  594. EXPECT_EQ("0x42", fmt::format("{0:#x}", 0x42u));
  595. EXPECT_EQ("042", fmt::format("{0:#o}", 042u));
  596. EXPECT_EQ("-42", fmt::format("{0:#}", -42l));
  597. EXPECT_EQ("0x42", fmt::format("{0:#x}", 0x42l));
  598. EXPECT_EQ("-0x42", fmt::format("{0:#x}", -0x42l));
  599. EXPECT_EQ("042", fmt::format("{0:#o}", 042l));
  600. EXPECT_EQ("-042", fmt::format("{0:#o}", -042l));
  601. EXPECT_EQ("42", fmt::format("{0:#}", 42ul));
  602. EXPECT_EQ("0x42", fmt::format("{0:#x}", 0x42ul));
  603. EXPECT_EQ("042", fmt::format("{0:#o}", 042ul));
  604. EXPECT_EQ("-42", fmt::format("{0:#}", -42ll));
  605. EXPECT_EQ("0x42", fmt::format("{0:#x}", 0x42ll));
  606. EXPECT_EQ("-0x42", fmt::format("{0:#x}", -0x42ll));
  607. EXPECT_EQ("042", fmt::format("{0:#o}", 042ll));
  608. EXPECT_EQ("-042", fmt::format("{0:#o}", -042ll));
  609. EXPECT_EQ("42", fmt::format("{0:#}", 42ull));
  610. EXPECT_EQ("0x42", fmt::format("{0:#x}", 0x42ull));
  611. EXPECT_EQ("042", fmt::format("{0:#o}", 042ull));
  612. EXPECT_EQ("-42.0", fmt::format("{0:#}", -42.0));
  613. EXPECT_EQ("-42.0", fmt::format("{0:#}", -42.0l));
  614. EXPECT_EQ("4.e+01", fmt::format("{:#.0e}", 42.0));
  615. EXPECT_EQ("0.", fmt::format("{:#.0f}", 0.01));
  616. EXPECT_EQ("0.50", fmt::format("{:#.2g}", 0.5));
  617. EXPECT_EQ("0.", fmt::format("{:#.0f}", 0.5));
  618. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:#"), 'c'), format_error,
  619. "missing '}' in format string");
  620. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:#}"), 'c'), format_error,
  621. "invalid format specifier for char");
  622. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:#}"), "abc"), format_error,
  623. "format specifier requires numeric argument");
  624. EXPECT_THROW_MSG(
  625. (void)fmt::format(runtime("{0:#}"), reinterpret_cast<void*>(0x42)),
  626. format_error, "format specifier requires numeric argument");
  627. }
  628. TEST(format_test, zero_flag) {
  629. EXPECT_EQ("42", fmt::format("{0:0}", 42));
  630. EXPECT_EQ("-0042", fmt::format("{0:05}", -42));
  631. EXPECT_EQ("00042", fmt::format("{0:05}", 42u));
  632. EXPECT_EQ("-0042", fmt::format("{0:05}", -42l));
  633. EXPECT_EQ("00042", fmt::format("{0:05}", 42ul));
  634. EXPECT_EQ("-0042", fmt::format("{0:05}", -42ll));
  635. EXPECT_EQ("00042", fmt::format("{0:05}", 42ull));
  636. EXPECT_EQ("-000042", fmt::format("{0:07}", -42.0));
  637. EXPECT_EQ("-000042", fmt::format("{0:07}", -42.0l));
  638. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:0"), 'c'), format_error,
  639. "missing '}' in format string");
  640. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:05}"), 'c'), format_error,
  641. "invalid format specifier for char");
  642. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:05}"), "abc"), format_error,
  643. "format specifier requires numeric argument");
  644. EXPECT_THROW_MSG(
  645. (void)fmt::format(runtime("{0:05}"), reinterpret_cast<void*>(0x42)),
  646. format_error, "format specifier requires numeric argument");
  647. }
  648. TEST(format_test, width) {
  649. char format_str[buffer_size];
  650. safe_sprintf(format_str, "{0:%u", UINT_MAX);
  651. increment(format_str + 3);
  652. EXPECT_THROW_MSG((void)fmt::format(runtime(format_str), 0), format_error,
  653. "number is too big");
  654. size_t size = std::strlen(format_str);
  655. format_str[size] = '}';
  656. format_str[size + 1] = 0;
  657. EXPECT_THROW_MSG((void)fmt::format(runtime(format_str), 0), format_error,
  658. "number is too big");
  659. safe_sprintf(format_str, "{0:%u", INT_MAX + 1u);
  660. EXPECT_THROW_MSG((void)fmt::format(runtime(format_str), 0), format_error,
  661. "number is too big");
  662. safe_sprintf(format_str, "{0:%u}", INT_MAX + 1u);
  663. EXPECT_THROW_MSG((void)fmt::format(runtime(format_str), 0), format_error,
  664. "number is too big");
  665. EXPECT_EQ(" -42", fmt::format("{0:4}", -42));
  666. EXPECT_EQ(" 42", fmt::format("{0:5}", 42u));
  667. EXPECT_EQ(" -42", fmt::format("{0:6}", -42l));
  668. EXPECT_EQ(" 42", fmt::format("{0:7}", 42ul));
  669. EXPECT_EQ(" -42", fmt::format("{0:6}", -42ll));
  670. EXPECT_EQ(" 42", fmt::format("{0:7}", 42ull));
  671. EXPECT_EQ(" -1.23", fmt::format("{0:8}", -1.23));
  672. EXPECT_EQ(" -1.23", fmt::format("{0:9}", -1.23l));
  673. EXPECT_EQ(" 0xcafe",
  674. fmt::format("{0:10}", reinterpret_cast<void*>(0xcafe)));
  675. EXPECT_EQ("x ", fmt::format("{0:11}", 'x'));
  676. EXPECT_EQ("str ", fmt::format("{0:12}", "str"));
  677. EXPECT_EQ(fmt::format("{:*^6}", "🤡"), "**🤡**");
  678. EXPECT_EQ(fmt::format("{:*^8}", "你好"), "**你好**");
  679. EXPECT_EQ(fmt::format("{:#6}", 42.0), " 42.0");
  680. EXPECT_EQ(fmt::format("{:6c}", static_cast<int>('x')), "x ");
  681. EXPECT_EQ(fmt::format("{:>06.0f}", 0.00884311), "000000");
  682. }
  683. TEST(format_test, runtime_width) {
  684. char format_str[buffer_size];
  685. safe_sprintf(format_str, "{0:{%u", UINT_MAX);
  686. increment(format_str + 4);
  687. EXPECT_THROW_MSG((void)fmt::format(runtime(format_str), 0), format_error,
  688. "invalid format string");
  689. size_t size = std::strlen(format_str);
  690. format_str[size] = '}';
  691. format_str[size + 1] = 0;
  692. EXPECT_THROW_MSG((void)fmt::format(runtime(format_str), 0), format_error,
  693. "argument not found");
  694. format_str[size + 1] = '}';
  695. format_str[size + 2] = 0;
  696. EXPECT_THROW_MSG((void)fmt::format(runtime(format_str), 0), format_error,
  697. "argument not found");
  698. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{"), 0), format_error,
  699. "invalid format string");
  700. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{}"), 0), format_error,
  701. "cannot switch from manual to automatic argument indexing");
  702. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{?}}"), 0), format_error,
  703. "invalid format string");
  704. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{1}}"), 0), format_error,
  705. "argument not found");
  706. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{0:}}"), 0), format_error,
  707. "invalid format string");
  708. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{1}}"), 0, -1), format_error,
  709. "negative width");
  710. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{1}}"), 0, (INT_MAX + 1u)),
  711. format_error, "number is too big");
  712. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{1}}"), 0, -1l), format_error,
  713. "negative width");
  714. if (fmt::detail::const_check(sizeof(long) > sizeof(int))) {
  715. long value = INT_MAX;
  716. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{1}}"), 0, (value + 1)),
  717. format_error, "number is too big");
  718. }
  719. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{1}}"), 0, (INT_MAX + 1ul)),
  720. format_error, "number is too big");
  721. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{1}}"), 0, '0'), format_error,
  722. "width is not integer");
  723. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{1}}"), 0, 0.0), format_error,
  724. "width is not integer");
  725. EXPECT_EQ(" -42", fmt::format("{0:{1}}", -42, 4));
  726. EXPECT_EQ(" 42", fmt::format("{0:{1}}", 42u, 5));
  727. EXPECT_EQ(" -42", fmt::format("{0:{1}}", -42l, 6));
  728. EXPECT_EQ(" 42", fmt::format("{0:{1}}", 42ul, 7));
  729. EXPECT_EQ(" -42", fmt::format("{0:{1}}", -42ll, 6));
  730. EXPECT_EQ(" 42", fmt::format("{0:{1}}", 42ull, 7));
  731. EXPECT_EQ(" -1.23", fmt::format("{0:{1}}", -1.23, 8));
  732. EXPECT_EQ(" -1.23", fmt::format("{0:{1}}", -1.23l, 9));
  733. EXPECT_EQ(" 0xcafe",
  734. fmt::format("{0:{1}}", reinterpret_cast<void*>(0xcafe), 10));
  735. EXPECT_EQ("x ", fmt::format("{0:{1}}", 'x', 11));
  736. EXPECT_EQ("str ", fmt::format("{0:{1}}", "str", 12));
  737. }
  738. TEST(format_test, precision) {
  739. char format_str[buffer_size];
  740. safe_sprintf(format_str, "{0:.%u", UINT_MAX);
  741. increment(format_str + 4);
  742. EXPECT_THROW_MSG((void)fmt::format(runtime(format_str), 0), format_error,
  743. "number is too big");
  744. size_t size = std::strlen(format_str);
  745. format_str[size] = '}';
  746. format_str[size + 1] = 0;
  747. EXPECT_THROW_MSG((void)fmt::format(runtime(format_str), 0), format_error,
  748. "number is too big");
  749. safe_sprintf(format_str, "{0:.%u", INT_MAX + 1u);
  750. EXPECT_THROW_MSG((void)fmt::format(runtime(format_str), 0), format_error,
  751. "number is too big");
  752. safe_sprintf(format_str, "{0:.%u}", INT_MAX + 1u);
  753. EXPECT_THROW_MSG((void)fmt::format(runtime(format_str), 0), format_error,
  754. "number is too big");
  755. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:."), 0), format_error,
  756. "missing precision specifier");
  757. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.}"), 0), format_error,
  758. "missing precision specifier");
  759. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.2"), 0), format_error,
  760. "precision not allowed for this argument type");
  761. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.2}"), 42), format_error,
  762. "precision not allowed for this argument type");
  763. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.2f}"), 42), format_error,
  764. "precision not allowed for this argument type");
  765. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.2}"), 42u), format_error,
  766. "precision not allowed for this argument type");
  767. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.2f}"), 42u), format_error,
  768. "precision not allowed for this argument type");
  769. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.2}"), 42l), format_error,
  770. "precision not allowed for this argument type");
  771. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.2f}"), 42l), format_error,
  772. "precision not allowed for this argument type");
  773. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.2}"), 42ul), format_error,
  774. "precision not allowed for this argument type");
  775. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.2f}"), 42ul), format_error,
  776. "precision not allowed for this argument type");
  777. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.2}"), 42ll), format_error,
  778. "precision not allowed for this argument type");
  779. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.2f}"), 42ll), format_error,
  780. "precision not allowed for this argument type");
  781. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.2}"), 42ull), format_error,
  782. "precision not allowed for this argument type");
  783. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.2f}"), 42ull), format_error,
  784. "precision not allowed for this argument type");
  785. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:3.0}"), 'x'), format_error,
  786. "precision not allowed for this argument type");
  787. EXPECT_EQ("1.2", fmt::format("{0:.2}", 1.2345));
  788. EXPECT_EQ("1.2", fmt::format("{0:.2}", 1.2345l));
  789. EXPECT_EQ("1.2e+56", fmt::format("{:.2}", 1.234e56));
  790. EXPECT_EQ("1.1", fmt::format("{0:.3}", 1.1));
  791. EXPECT_EQ("1e+00", fmt::format("{:.0e}", 1.0L));
  792. EXPECT_EQ(" 0.0e+00", fmt::format("{:9.1e}", 0.0));
  793. EXPECT_EQ(
  794. fmt::format("{:.494}", 4.9406564584124654E-324),
  795. "4.9406564584124654417656879286822137236505980261432476442558568250067550"
  796. "727020875186529983636163599237979656469544571773092665671035593979639877"
  797. "479601078187812630071319031140452784581716784898210368871863605699873072"
  798. "305000638740915356498438731247339727316961514003171538539807412623856559"
  799. "117102665855668676818703956031062493194527159149245532930545654440112748"
  800. "012970999954193198940908041656332452475714786901472678015935523861155013"
  801. "480352649347201937902681071074917033322268447533357208324319361e-324");
  802. EXPECT_EQ(
  803. fmt::format("{:.1074f}", 1.1125369292536e-308),
  804. "0.0000000000000000000000000000000000000000000000000000000000000000000000"
  805. "000000000000000000000000000000000000000000000000000000000000000000000000"
  806. "000000000000000000000000000000000000000000000000000000000000000000000000"
  807. "000000000000000000000000000000000000000000000000000000000000000000000000"
  808. "000000000000000000000111253692925360019747947051741965785554081512200979"
  809. "355021686109411883779182127659725163430929750364498219730822952552570601"
  810. "152163505899912777129583674906301179059298598412303893909188340988729019"
  811. "014361467448914817838555156840459458527907308695109202499990850735085304"
  812. "478476991912072201449236975063640913461919914396877093174125167509869762"
  813. "482369631100360266123742648159508919592746619553246586039571522788247697"
  814. "156360766271842991667238355464496455107749716934387136380536472531224398"
  815. "559833794807213172371254492216255558078524900147957309382830827524104234"
  816. "530961756787819847850302379672357738807808384667004752163416921762619527"
  817. "462847642037420991432005657440259928195996762610375541867198059294212446"
  818. "81962777939941034720757232455434770912461317493580281734466552734375");
  819. std::string outputs[] = {
  820. "-0X1.41FE3FFE71C9E000000000000000000000000000000000000000000000000000000"
  821. "000000000000000000000000000000000000000000000000000000000000000000000000"
  822. "000000000000000000000000000000000000000000000000000000000000000000000000"
  823. "000000000000000000000000000000000000000000000000000000000000000000000000"
  824. "000000000000000000000000000000000000000000000000000000000000000000000000"
  825. "000000000000000000000000000000000000000000000000000000000000000000000000"
  826. "000000000000000000000000000000000000000000000000000000000000000000000000"
  827. "000000000000000000000000000000000000000000000000000000000000000000000000"
  828. "000000000000000000000000000000000000000000000000000000000000000000000000"
  829. "000000000000000000000000000000000000000000000000000000000000000000000000"
  830. "000000000000000000000000000000000000000000000000000000000000000000000000"
  831. "000000000000000000000000000000000000000000000000000P+127",
  832. "-0XA.0FF1FFF38E4F0000000000000000000000000000000000000000000000000000000"
  833. "000000000000000000000000000000000000000000000000000000000000000000000000"
  834. "000000000000000000000000000000000000000000000000000000000000000000000000"
  835. "000000000000000000000000000000000000000000000000000000000000000000000000"
  836. "000000000000000000000000000000000000000000000000000000000000000000000000"
  837. "000000000000000000000000000000000000000000000000000000000000000000000000"
  838. "000000000000000000000000000000000000000000000000000000000000000000000000"
  839. "000000000000000000000000000000000000000000000000000000000000000000000000"
  840. "000000000000000000000000000000000000000000000000000000000000000000000000"
  841. "000000000000000000000000000000000000000000000000000000000000000000000000"
  842. "000000000000000000000000000000000000000000000000000000000000000000000000"
  843. "000000000000000000000000000000000000000000000000000P+124"};
  844. EXPECT_THAT(outputs,
  845. testing::Contains(fmt::format("{:.838A}", -2.14001164E+38)));
  846. EXPECT_EQ("123.", fmt::format("{:#.0f}", 123.0));
  847. EXPECT_EQ("1.23", fmt::format("{:.02f}", 1.234));
  848. EXPECT_EQ("0.001", fmt::format("{:.1g}", 0.001));
  849. EXPECT_EQ("1019666400", fmt::format("{}", 1019666432.0f));
  850. EXPECT_EQ("1e+01", fmt::format("{:.0e}", 9.5));
  851. EXPECT_EQ("1.0e-34", fmt::format("{:.1e}", 1e-34));
  852. EXPECT_THROW_MSG(
  853. (void)fmt::format(runtime("{0:.2}"), reinterpret_cast<void*>(0xcafe)),
  854. format_error, "precision not allowed for this argument type");
  855. EXPECT_THROW_MSG(
  856. (void)fmt::format(runtime("{0:.2f}"), reinterpret_cast<void*>(0xcafe)),
  857. format_error, "precision not allowed for this argument type");
  858. EXPECT_THROW_MSG((void)fmt::format(runtime("{:.{}e}"), 42.0,
  859. fmt::detail::max_value<int>()),
  860. format_error, "number is too big");
  861. EXPECT_THROW_MSG(
  862. (void)fmt::format("{:.2147483646f}", -2.2121295195081227E+304),
  863. format_error, "number is too big");
  864. EXPECT_EQ("st", fmt::format("{0:.2}", "str"));
  865. }
  866. TEST(format_test, runtime_precision) {
  867. char format_str[buffer_size];
  868. safe_sprintf(format_str, "{0:.{%u", UINT_MAX);
  869. increment(format_str + 5);
  870. EXPECT_THROW_MSG((void)fmt::format(runtime(format_str), 0), format_error,
  871. "invalid format string");
  872. size_t size = std::strlen(format_str);
  873. format_str[size] = '}';
  874. format_str[size + 1] = 0;
  875. EXPECT_THROW_MSG((void)fmt::format(runtime(format_str), 0), format_error,
  876. "argument not found");
  877. format_str[size + 1] = '}';
  878. format_str[size + 2] = 0;
  879. EXPECT_THROW_MSG((void)fmt::format(runtime(format_str), 0), format_error,
  880. "argument not found");
  881. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{"), 0), format_error,
  882. "invalid format string");
  883. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{}"), 0), format_error,
  884. "cannot switch from manual to automatic argument indexing");
  885. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{?}}"), 0), format_error,
  886. "invalid format string");
  887. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}"), 0, 0), format_error,
  888. "precision not allowed for this argument type");
  889. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 0), format_error,
  890. "argument not found");
  891. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{0:}}"), 0), format_error,
  892. "invalid format string");
  893. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 0, -1), format_error,
  894. "negative precision");
  895. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 0, (INT_MAX + 1u)),
  896. format_error, "number is too big");
  897. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 0, -1l), format_error,
  898. "negative precision");
  899. if (fmt::detail::const_check(sizeof(long) > sizeof(int))) {
  900. long value = INT_MAX;
  901. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 0, (value + 1)),
  902. format_error, "number is too big");
  903. }
  904. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 0, (INT_MAX + 1ul)),
  905. format_error, "number is too big");
  906. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 0, '0'), format_error,
  907. "precision is not integer");
  908. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 0, 0.0), format_error,
  909. "precision is not integer");
  910. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 42, 2), format_error,
  911. "precision not allowed for this argument type");
  912. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}f}"), 42, 2), format_error,
  913. "precision not allowed for this argument type");
  914. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 42u, 2), format_error,
  915. "precision not allowed for this argument type");
  916. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}f}"), 42u, 2),
  917. format_error,
  918. "precision not allowed for this argument type");
  919. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 42l, 2), format_error,
  920. "precision not allowed for this argument type");
  921. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}f}"), 42l, 2),
  922. format_error,
  923. "precision not allowed for this argument type");
  924. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 42ul, 2),
  925. format_error,
  926. "precision not allowed for this argument type");
  927. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}f}"), 42ul, 2),
  928. format_error,
  929. "precision not allowed for this argument type");
  930. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 42ll, 2),
  931. format_error,
  932. "precision not allowed for this argument type");
  933. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}f}"), 42ll, 2),
  934. format_error,
  935. "precision not allowed for this argument type");
  936. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 42ull, 2),
  937. format_error,
  938. "precision not allowed for this argument type");
  939. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}f}"), 42ull, 2),
  940. format_error,
  941. "precision not allowed for this argument type");
  942. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:3.{1}}"), 'x', 0),
  943. format_error,
  944. "precision not allowed for this argument type");
  945. EXPECT_EQ("1.2", fmt::format("{0:.{1}}", 1.2345, 2));
  946. EXPECT_EQ("1.2", fmt::format("{1:.{0}}", 2, 1.2345l));
  947. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"),
  948. reinterpret_cast<void*>(0xcafe), 2),
  949. format_error,
  950. "precision not allowed for this argument type");
  951. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}f}"),
  952. reinterpret_cast<void*>(0xcafe), 2),
  953. format_error,
  954. "precision not allowed for this argument type");
  955. EXPECT_EQ("st", fmt::format("{0:.{1}}", "str", 2));
  956. }
  957. TEST(format_test, format_bool) {
  958. EXPECT_EQ("true", fmt::format("{}", true));
  959. EXPECT_EQ("false", fmt::format("{}", false));
  960. EXPECT_EQ("1", fmt::format("{:d}", true));
  961. EXPECT_EQ("true ", fmt::format("{:5}", true));
  962. EXPECT_EQ("true", fmt::format("{:s}", true));
  963. EXPECT_EQ("false", fmt::format("{:s}", false));
  964. EXPECT_EQ("false ", fmt::format("{:6s}", false));
  965. }
  966. TEST(format_test, format_short) {
  967. short s = 42;
  968. EXPECT_EQ("42", fmt::format("{0:d}", s));
  969. unsigned short us = 42;
  970. EXPECT_EQ("42", fmt::format("{0:d}", us));
  971. }
  972. template <typename T>
  973. void check_unknown_types(const T& value, const char* types, const char*) {
  974. char format_str[buffer_size];
  975. const char* special = ".0123456789L}";
  976. for (int i = CHAR_MIN; i <= CHAR_MAX; ++i) {
  977. char c = static_cast<char>(i);
  978. if (std::strchr(types, c) || std::strchr(special, c) || !c) continue;
  979. safe_sprintf(format_str, "{0:10%c}", c);
  980. const char* message = "invalid type specifier";
  981. EXPECT_THROW_MSG((void)fmt::format(runtime(format_str), value),
  982. format_error, message)
  983. << format_str << " " << message;
  984. }
  985. }
  986. TEST(format_test, format_int) {
  987. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:v"), 42), format_error,
  988. "invalid type specifier");
  989. check_unknown_types(42, "bBdoxXnLc", "integer");
  990. EXPECT_EQ("x", fmt::format("{:c}", static_cast<int>('x')));
  991. }
  992. TEST(format_test, format_bin) {
  993. EXPECT_EQ("0", fmt::format("{0:b}", 0));
  994. EXPECT_EQ("101010", fmt::format("{0:b}", 42));
  995. EXPECT_EQ("101010", fmt::format("{0:b}", 42u));
  996. EXPECT_EQ("-101010", fmt::format("{0:b}", -42));
  997. EXPECT_EQ("11000000111001", fmt::format("{0:b}", 12345));
  998. EXPECT_EQ("10010001101000101011001111000", fmt::format("{0:b}", 0x12345678));
  999. EXPECT_EQ("10010000101010111100110111101111",
  1000. fmt::format("{0:b}", 0x90ABCDEF));
  1001. EXPECT_EQ("11111111111111111111111111111111",
  1002. fmt::format("{0:b}", max_value<uint32_t>()));
  1003. }
  1004. #if FMT_USE_INT128
  1005. constexpr auto int128_max = static_cast<__int128_t>(
  1006. (static_cast<__uint128_t>(1) << ((__SIZEOF_INT128__ * CHAR_BIT) - 1)) - 1);
  1007. constexpr auto int128_min = -int128_max - 1;
  1008. constexpr auto uint128_max = ~static_cast<__uint128_t>(0);
  1009. #endif
  1010. TEST(format_test, format_dec) {
  1011. EXPECT_EQ("0", fmt::format("{0}", 0));
  1012. EXPECT_EQ("42", fmt::format("{0}", 42));
  1013. EXPECT_EQ("42", fmt::format("{0:d}", 42));
  1014. EXPECT_EQ("42", fmt::format("{0}", 42u));
  1015. EXPECT_EQ("-42", fmt::format("{0}", -42));
  1016. EXPECT_EQ("12345", fmt::format("{0}", 12345));
  1017. EXPECT_EQ("67890", fmt::format("{0}", 67890));
  1018. #if FMT_USE_INT128
  1019. EXPECT_EQ("0", fmt::format("{0}", static_cast<__int128_t>(0)));
  1020. EXPECT_EQ("0", fmt::format("{0}", static_cast<__uint128_t>(0)));
  1021. EXPECT_EQ("9223372036854775808",
  1022. fmt::format("{0}", static_cast<__int128_t>(INT64_MAX) + 1));
  1023. EXPECT_EQ("-9223372036854775809",
  1024. fmt::format("{0}", static_cast<__int128_t>(INT64_MIN) - 1));
  1025. EXPECT_EQ("18446744073709551616",
  1026. fmt::format("{0}", static_cast<__int128_t>(UINT64_MAX) + 1));
  1027. EXPECT_EQ("170141183460469231731687303715884105727",
  1028. fmt::format("{0}", int128_max));
  1029. EXPECT_EQ("-170141183460469231731687303715884105728",
  1030. fmt::format("{0}", int128_min));
  1031. EXPECT_EQ("340282366920938463463374607431768211455",
  1032. fmt::format("{0}", uint128_max));
  1033. #endif
  1034. char buffer[buffer_size];
  1035. safe_sprintf(buffer, "%d", INT_MIN);
  1036. EXPECT_EQ(buffer, fmt::format("{0}", INT_MIN));
  1037. safe_sprintf(buffer, "%d", INT_MAX);
  1038. EXPECT_EQ(buffer, fmt::format("{0}", INT_MAX));
  1039. safe_sprintf(buffer, "%u", UINT_MAX);
  1040. EXPECT_EQ(buffer, fmt::format("{0}", UINT_MAX));
  1041. safe_sprintf(buffer, "%ld", 0 - static_cast<unsigned long>(LONG_MIN));
  1042. EXPECT_EQ(buffer, fmt::format("{0}", LONG_MIN));
  1043. safe_sprintf(buffer, "%ld", LONG_MAX);
  1044. EXPECT_EQ(buffer, fmt::format("{0}", LONG_MAX));
  1045. safe_sprintf(buffer, "%lu", ULONG_MAX);
  1046. EXPECT_EQ(buffer, fmt::format("{0}", ULONG_MAX));
  1047. }
  1048. TEST(format_test, format_hex) {
  1049. EXPECT_EQ("0", fmt::format("{0:x}", 0));
  1050. EXPECT_EQ("42", fmt::format("{0:x}", 0x42));
  1051. EXPECT_EQ("42", fmt::format("{0:x}", 0x42u));
  1052. EXPECT_EQ("-42", fmt::format("{0:x}", -0x42));
  1053. EXPECT_EQ("12345678", fmt::format("{0:x}", 0x12345678));
  1054. EXPECT_EQ("90abcdef", fmt::format("{0:x}", 0x90abcdef));
  1055. EXPECT_EQ("12345678", fmt::format("{0:X}", 0x12345678));
  1056. EXPECT_EQ("90ABCDEF", fmt::format("{0:X}", 0x90ABCDEF));
  1057. #if FMT_USE_INT128
  1058. EXPECT_EQ("0", fmt::format("{0:x}", static_cast<__int128_t>(0)));
  1059. EXPECT_EQ("0", fmt::format("{0:x}", static_cast<__uint128_t>(0)));
  1060. EXPECT_EQ("8000000000000000",
  1061. fmt::format("{0:x}", static_cast<__int128_t>(INT64_MAX) + 1));
  1062. EXPECT_EQ("-8000000000000001",
  1063. fmt::format("{0:x}", static_cast<__int128_t>(INT64_MIN) - 1));
  1064. EXPECT_EQ("10000000000000000",
  1065. fmt::format("{0:x}", static_cast<__int128_t>(UINT64_MAX) + 1));
  1066. EXPECT_EQ("7fffffffffffffffffffffffffffffff",
  1067. fmt::format("{0:x}", int128_max));
  1068. EXPECT_EQ("-80000000000000000000000000000000",
  1069. fmt::format("{0:x}", int128_min));
  1070. EXPECT_EQ("ffffffffffffffffffffffffffffffff",
  1071. fmt::format("{0:x}", uint128_max));
  1072. #endif
  1073. char buffer[buffer_size];
  1074. safe_sprintf(buffer, "-%x", 0 - static_cast<unsigned>(INT_MIN));
  1075. EXPECT_EQ(buffer, fmt::format("{0:x}", INT_MIN));
  1076. safe_sprintf(buffer, "%x", INT_MAX);
  1077. EXPECT_EQ(buffer, fmt::format("{0:x}", INT_MAX));
  1078. safe_sprintf(buffer, "%x", UINT_MAX);
  1079. EXPECT_EQ(buffer, fmt::format("{0:x}", UINT_MAX));
  1080. safe_sprintf(buffer, "-%lx", 0 - static_cast<unsigned long>(LONG_MIN));
  1081. EXPECT_EQ(buffer, fmt::format("{0:x}", LONG_MIN));
  1082. safe_sprintf(buffer, "%lx", LONG_MAX);
  1083. EXPECT_EQ(buffer, fmt::format("{0:x}", LONG_MAX));
  1084. safe_sprintf(buffer, "%lx", ULONG_MAX);
  1085. EXPECT_EQ(buffer, fmt::format("{0:x}", ULONG_MAX));
  1086. }
  1087. TEST(format_test, format_oct) {
  1088. EXPECT_EQ("0", fmt::format("{0:o}", 0));
  1089. EXPECT_EQ("42", fmt::format("{0:o}", 042));
  1090. EXPECT_EQ("42", fmt::format("{0:o}", 042u));
  1091. EXPECT_EQ("-42", fmt::format("{0:o}", -042));
  1092. EXPECT_EQ("12345670", fmt::format("{0:o}", 012345670));
  1093. #if FMT_USE_INT128
  1094. EXPECT_EQ("0", fmt::format("{0:o}", static_cast<__int128_t>(0)));
  1095. EXPECT_EQ("0", fmt::format("{0:o}", static_cast<__uint128_t>(0)));
  1096. EXPECT_EQ("1000000000000000000000",
  1097. fmt::format("{0:o}", static_cast<__int128_t>(INT64_MAX) + 1));
  1098. EXPECT_EQ("-1000000000000000000001",
  1099. fmt::format("{0:o}", static_cast<__int128_t>(INT64_MIN) - 1));
  1100. EXPECT_EQ("2000000000000000000000",
  1101. fmt::format("{0:o}", static_cast<__int128_t>(UINT64_MAX) + 1));
  1102. EXPECT_EQ("1777777777777777777777777777777777777777777",
  1103. fmt::format("{0:o}", int128_max));
  1104. EXPECT_EQ("-2000000000000000000000000000000000000000000",
  1105. fmt::format("{0:o}", int128_min));
  1106. EXPECT_EQ("3777777777777777777777777777777777777777777",
  1107. fmt::format("{0:o}", uint128_max));
  1108. #endif
  1109. char buffer[buffer_size];
  1110. safe_sprintf(buffer, "-%o", 0 - static_cast<unsigned>(INT_MIN));
  1111. EXPECT_EQ(buffer, fmt::format("{0:o}", INT_MIN));
  1112. safe_sprintf(buffer, "%o", INT_MAX);
  1113. EXPECT_EQ(buffer, fmt::format("{0:o}", INT_MAX));
  1114. safe_sprintf(buffer, "%o", UINT_MAX);
  1115. EXPECT_EQ(buffer, fmt::format("{0:o}", UINT_MAX));
  1116. safe_sprintf(buffer, "-%lo", 0 - static_cast<unsigned long>(LONG_MIN));
  1117. EXPECT_EQ(buffer, fmt::format("{0:o}", LONG_MIN));
  1118. safe_sprintf(buffer, "%lo", LONG_MAX);
  1119. EXPECT_EQ(buffer, fmt::format("{0:o}", LONG_MAX));
  1120. safe_sprintf(buffer, "%lo", ULONG_MAX);
  1121. EXPECT_EQ(buffer, fmt::format("{0:o}", ULONG_MAX));
  1122. }
  1123. TEST(format_test, format_int_locale) {
  1124. EXPECT_EQ("1234", fmt::format("{:L}", 1234));
  1125. }
  1126. TEST(format_test, format_float) {
  1127. EXPECT_EQ("0", fmt::format("{}", 0.0f));
  1128. EXPECT_EQ("392.500000", fmt::format("{0:f}", 392.5f));
  1129. }
  1130. TEST(format_test, format_double) {
  1131. EXPECT_EQ("0", fmt::format("{}", 0.0));
  1132. check_unknown_types(1.2, "eEfFgGaAnL%", "double");
  1133. EXPECT_EQ("0", fmt::format("{:}", 0.0));
  1134. EXPECT_EQ("0.000000", fmt::format("{:f}", 0.0));
  1135. EXPECT_EQ("0", fmt::format("{:g}", 0.0));
  1136. EXPECT_EQ("392.65", fmt::format("{:}", 392.65));
  1137. EXPECT_EQ("392.65", fmt::format("{:g}", 392.65));
  1138. EXPECT_EQ("392.65", fmt::format("{:G}", 392.65));
  1139. EXPECT_EQ("4.9014e+06", fmt::format("{:g}", 4.9014e6));
  1140. EXPECT_EQ("392.650000", fmt::format("{:f}", 392.65));
  1141. EXPECT_EQ("392.650000", fmt::format("{:F}", 392.65));
  1142. EXPECT_EQ("42", fmt::format("{:L}", 42.0));
  1143. EXPECT_EQ(" 0x1.0cccccccccccdp+2", fmt::format("{:24a}", 4.2));
  1144. EXPECT_EQ("0x1.0cccccccccccdp+2 ", fmt::format("{:<24a}", 4.2));
  1145. char buffer[buffer_size];
  1146. safe_sprintf(buffer, "%e", 392.65);
  1147. EXPECT_EQ(buffer, fmt::format("{0:e}", 392.65));
  1148. safe_sprintf(buffer, "%E", 392.65);
  1149. EXPECT_EQ(buffer, fmt::format("{0:E}", 392.65));
  1150. EXPECT_EQ("+0000392.6", fmt::format("{0:+010.4g}", 392.65));
  1151. safe_sprintf(buffer, "%a", -42.0);
  1152. EXPECT_EQ(buffer, fmt::format("{:a}", -42.0));
  1153. safe_sprintf(buffer, "%A", -42.0);
  1154. EXPECT_EQ(buffer, fmt::format("{:A}", -42.0));
  1155. EXPECT_EQ("9223372036854775808.000000",
  1156. fmt::format("{:f}", 9223372036854775807.0));
  1157. }
  1158. TEST(format_test, precision_rounding) {
  1159. EXPECT_EQ("0", fmt::format("{:.0f}", 0.0));
  1160. EXPECT_EQ("0", fmt::format("{:.0f}", 0.01));
  1161. EXPECT_EQ("0", fmt::format("{:.0f}", 0.1));
  1162. EXPECT_EQ("0.000", fmt::format("{:.3f}", 0.00049));
  1163. EXPECT_EQ("0.001", fmt::format("{:.3f}", 0.0005));
  1164. EXPECT_EQ("0.001", fmt::format("{:.3f}", 0.00149));
  1165. EXPECT_EQ("0.002", fmt::format("{:.3f}", 0.0015));
  1166. EXPECT_EQ("1.000", fmt::format("{:.3f}", 0.9999));
  1167. EXPECT_EQ("0.00123", fmt::format("{:.3}", 0.00123));
  1168. EXPECT_EQ("0.1", fmt::format("{:.16g}", 0.1));
  1169. EXPECT_EQ("1", fmt::format("{:.0}", 1.0));
  1170. EXPECT_EQ("225.51575035152063720",
  1171. fmt::format("{:.17f}", 225.51575035152064));
  1172. EXPECT_EQ("-761519619559038.2", fmt::format("{:.1f}", -761519619559038.2));
  1173. EXPECT_EQ("1.9156918820264798e-56",
  1174. fmt::format("{}", 1.9156918820264798e-56));
  1175. EXPECT_EQ("0.0000", fmt::format("{:.4f}", 7.2809479766055470e-15));
  1176. // Trigger a rounding error in Grisu by a specially chosen number.
  1177. EXPECT_EQ("3788512123356.985352", fmt::format("{:f}", 3788512123356.985352));
  1178. }
  1179. TEST(format_test, prettify_float) {
  1180. EXPECT_EQ("0.0001", fmt::format("{}", 1e-4));
  1181. EXPECT_EQ("1e-05", fmt::format("{}", 1e-5));
  1182. EXPECT_EQ("1000000000000000", fmt::format("{}", 1e15));
  1183. EXPECT_EQ("1e+16", fmt::format("{}", 1e16));
  1184. EXPECT_EQ("9.999e-05", fmt::format("{}", 9.999e-5));
  1185. EXPECT_EQ("10000000000", fmt::format("{}", 1e10));
  1186. EXPECT_EQ("100000000000", fmt::format("{}", 1e11));
  1187. EXPECT_EQ("12340000000", fmt::format("{}", 1234e7));
  1188. EXPECT_EQ("12.34", fmt::format("{}", 1234e-2));
  1189. EXPECT_EQ("0.001234", fmt::format("{}", 1234e-6));
  1190. EXPECT_EQ("0.1", fmt::format("{}", 0.1f));
  1191. EXPECT_EQ("0.10000000149011612", fmt::format("{}", double(0.1f)));
  1192. EXPECT_EQ("1.3563156e-19", fmt::format("{}", 1.35631564e-19f));
  1193. }
  1194. TEST(format_test, format_nan) {
  1195. double nan = std::numeric_limits<double>::quiet_NaN();
  1196. EXPECT_EQ("nan", fmt::format("{}", nan));
  1197. EXPECT_EQ("+nan", fmt::format("{:+}", nan));
  1198. EXPECT_EQ(" +nan", fmt::format("{:+06}", nan));
  1199. EXPECT_EQ("+nan ", fmt::format("{:<+06}", nan));
  1200. EXPECT_EQ(" +nan ", fmt::format("{:^+06}", nan));
  1201. EXPECT_EQ(" +nan", fmt::format("{:>+06}", nan));
  1202. if (std::signbit(-nan)) {
  1203. EXPECT_EQ("-nan", fmt::format("{}", -nan));
  1204. EXPECT_EQ(" -nan", fmt::format("{:+06}", -nan));
  1205. } else {
  1206. fmt::print("Warning: compiler doesn't handle negative NaN correctly");
  1207. }
  1208. EXPECT_EQ(" nan", fmt::format("{: }", nan));
  1209. EXPECT_EQ("NAN", fmt::format("{:F}", nan));
  1210. EXPECT_EQ("nan ", fmt::format("{:<7}", nan));
  1211. EXPECT_EQ(" nan ", fmt::format("{:^7}", nan));
  1212. EXPECT_EQ(" nan", fmt::format("{:>7}", nan));
  1213. }
  1214. TEST(format_test, format_infinity) {
  1215. double inf = std::numeric_limits<double>::infinity();
  1216. EXPECT_EQ("inf", fmt::format("{}", inf));
  1217. EXPECT_EQ("+inf", fmt::format("{:+}", inf));
  1218. EXPECT_EQ("-inf", fmt::format("{}", -inf));
  1219. EXPECT_EQ(" +inf", fmt::format("{:+06}", inf));
  1220. EXPECT_EQ(" -inf", fmt::format("{:+06}", -inf));
  1221. EXPECT_EQ("+inf ", fmt::format("{:<+06}", inf));
  1222. EXPECT_EQ(" +inf ", fmt::format("{:^+06}", inf));
  1223. EXPECT_EQ(" +inf", fmt::format("{:>+06}", inf));
  1224. EXPECT_EQ(" inf", fmt::format("{: }", inf));
  1225. EXPECT_EQ("INF", fmt::format("{:F}", inf));
  1226. EXPECT_EQ("inf ", fmt::format("{:<7}", inf));
  1227. EXPECT_EQ(" inf ", fmt::format("{:^7}", inf));
  1228. EXPECT_EQ(" inf", fmt::format("{:>7}", inf));
  1229. }
  1230. TEST(format_test, format_long_double) {
  1231. EXPECT_EQ("0", fmt::format("{0:}", 0.0l));
  1232. EXPECT_EQ("0.000000", fmt::format("{0:f}", 0.0l));
  1233. EXPECT_EQ("392.65", fmt::format("{0:}", 392.65l));
  1234. EXPECT_EQ("392.65", fmt::format("{0:g}", 392.65l));
  1235. EXPECT_EQ("392.65", fmt::format("{0:G}", 392.65l));
  1236. EXPECT_EQ("392.650000", fmt::format("{0:f}", 392.65l));
  1237. EXPECT_EQ("392.650000", fmt::format("{0:F}", 392.65l));
  1238. char buffer[buffer_size];
  1239. safe_sprintf(buffer, "%Le", 392.65l);
  1240. EXPECT_EQ(buffer, fmt::format("{0:e}", 392.65l));
  1241. EXPECT_EQ("+0000392.6", fmt::format("{0:+010.4g}", 392.64l));
  1242. safe_sprintf(buffer, "%La", 3.31l);
  1243. EXPECT_EQ(buffer, fmt::format("{:a}", 3.31l));
  1244. }
  1245. TEST(format_test, format_char) {
  1246. const char types[] = "cbBdoxX";
  1247. check_unknown_types('a', types, "char");
  1248. EXPECT_EQ("a", fmt::format("{0}", 'a'));
  1249. EXPECT_EQ("z", fmt::format("{0:c}", 'z'));
  1250. int n = 'x';
  1251. for (const char* type = types + 1; *type; ++type) {
  1252. std::string format_str = fmt::format("{{:{}}}", *type);
  1253. EXPECT_EQ(fmt::format(runtime(format_str), n),
  1254. fmt::format(runtime(format_str), 'x'))
  1255. << format_str;
  1256. }
  1257. EXPECT_EQ(fmt::format("{:02X}", n), fmt::format("{:02X}", 'x'));
  1258. }
  1259. TEST(format_test, format_volatile_char) {
  1260. volatile char c = 'x';
  1261. EXPECT_EQ("x", fmt::format("{}", c));
  1262. }
  1263. TEST(format_test, format_unsigned_char) {
  1264. EXPECT_EQ("42", fmt::format("{}", static_cast<unsigned char>(42)));
  1265. EXPECT_EQ("42", fmt::format("{}", static_cast<uint8_t>(42)));
  1266. }
  1267. TEST(format_test, format_cstring) {
  1268. check_unknown_types("test", "sp", "string");
  1269. EXPECT_EQ("test", fmt::format("{0}", "test"));
  1270. EXPECT_EQ("test", fmt::format("{0:s}", "test"));
  1271. char nonconst[] = "nonconst";
  1272. EXPECT_EQ("nonconst", fmt::format("{0}", nonconst));
  1273. EXPECT_THROW_MSG(
  1274. (void)fmt::format(runtime("{0}"), static_cast<const char*>(nullptr)),
  1275. format_error, "string pointer is null");
  1276. }
  1277. void function_pointer_test(int, double, std::string) {}
  1278. TEST(format_test, format_pointer) {
  1279. check_unknown_types(reinterpret_cast<void*>(0x1234), "p", "pointer");
  1280. EXPECT_EQ("0x0", fmt::format("{0}", static_cast<void*>(nullptr)));
  1281. EXPECT_EQ("0x1234", fmt::format("{0}", reinterpret_cast<void*>(0x1234)));
  1282. EXPECT_EQ("0x1234", fmt::format("{0:p}", reinterpret_cast<void*>(0x1234)));
  1283. EXPECT_EQ("0x" + std::string(sizeof(void*) * CHAR_BIT / 4, 'f'),
  1284. fmt::format("{0}", reinterpret_cast<void*>(~uintptr_t())));
  1285. EXPECT_EQ("0x1234",
  1286. fmt::format("{}", fmt::ptr(reinterpret_cast<int*>(0x1234))));
  1287. std::unique_ptr<int> up(new int(1));
  1288. EXPECT_EQ(fmt::format("{}", fmt::ptr(up.get())),
  1289. fmt::format("{}", fmt::ptr(up)));
  1290. std::shared_ptr<int> sp(new int(1));
  1291. EXPECT_EQ(fmt::format("{}", fmt::ptr(sp.get())),
  1292. fmt::format("{}", fmt::ptr(sp)));
  1293. EXPECT_EQ(fmt::format("{}", fmt::detail::bit_cast<const void*>(
  1294. &function_pointer_test)),
  1295. fmt::format("{}", fmt::ptr(function_pointer_test)));
  1296. EXPECT_EQ("0x0", fmt::format("{}", nullptr));
  1297. }
  1298. TEST(format_test, format_string) {
  1299. EXPECT_EQ("test", fmt::format("{0}", std::string("test")));
  1300. EXPECT_THROW((void)fmt::format(fmt::runtime("{:x}"), std::string("test")),
  1301. fmt::format_error);
  1302. }
  1303. TEST(format_test, format_string_view) {
  1304. EXPECT_EQ("test", fmt::format("{}", string_view("test")));
  1305. EXPECT_EQ("", fmt::format("{}", string_view()));
  1306. }
  1307. #ifdef FMT_USE_STRING_VIEW
  1308. struct string_viewable {};
  1309. FMT_BEGIN_NAMESPACE
  1310. template <> struct formatter<string_viewable> : formatter<std::string_view> {
  1311. auto format(string_viewable, format_context& ctx) -> decltype(ctx.out()) {
  1312. return formatter<std::string_view>::format("foo", ctx);
  1313. }
  1314. };
  1315. FMT_END_NAMESPACE
  1316. TEST(format_test, format_std_string_view) {
  1317. EXPECT_EQ("test", fmt::format("{}", std::string_view("test")));
  1318. EXPECT_EQ("foo", fmt::format("{}", string_viewable()));
  1319. }
  1320. struct explicitly_convertible_to_std_string_view {
  1321. explicit operator std::string_view() const { return "foo"; }
  1322. };
  1323. template <>
  1324. struct fmt::formatter<explicitly_convertible_to_std_string_view>
  1325. : formatter<std::string_view> {
  1326. auto format(explicitly_convertible_to_std_string_view v, format_context& ctx)
  1327. -> decltype(ctx.out()) {
  1328. return format_to(ctx.out(), "'{}'", std::string_view(v));
  1329. }
  1330. };
  1331. TEST(format_test, format_explicitly_convertible_to_std_string_view) {
  1332. EXPECT_EQ("'foo'",
  1333. fmt::format("{}", explicitly_convertible_to_std_string_view()));
  1334. }
  1335. #endif
  1336. struct converible_to_anything {
  1337. template <typename T> operator T() const { return T(); }
  1338. };
  1339. FMT_BEGIN_NAMESPACE
  1340. template <> struct formatter<converible_to_anything> {
  1341. FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
  1342. return ctx.begin();
  1343. }
  1344. auto format(converible_to_anything, format_context& ctx)
  1345. -> decltype(ctx.out()) {
  1346. return format_to(ctx.out(), "foo");
  1347. }
  1348. };
  1349. FMT_END_NAMESPACE
  1350. TEST(format_test, format_convertible_to_anything) {
  1351. EXPECT_EQ("foo", fmt::format("{}", converible_to_anything()));
  1352. }
  1353. class Answer {};
  1354. FMT_BEGIN_NAMESPACE
  1355. template <> struct formatter<date> {
  1356. template <typename ParseContext>
  1357. FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
  1358. auto it = ctx.begin();
  1359. if (it != ctx.end() && *it == 'd') ++it;
  1360. return it;
  1361. }
  1362. auto format(const date& d, format_context& ctx) -> decltype(ctx.out()) {
  1363. format_to(ctx.out(), "{}-{}-{}", d.year(), d.month(), d.day());
  1364. return ctx.out();
  1365. }
  1366. };
  1367. template <> struct formatter<Answer> : formatter<int> {
  1368. template <typename FormatContext>
  1369. auto format(Answer, FormatContext& ctx) -> decltype(ctx.out()) {
  1370. return formatter<int>::format(42, ctx);
  1371. }
  1372. };
  1373. FMT_END_NAMESPACE
  1374. TEST(format_test, format_custom) {
  1375. EXPECT_THROW_MSG((void)fmt::format(runtime("{:s}"), date(2012, 12, 9)),
  1376. format_error, "unknown format specifier");
  1377. EXPECT_EQ("42", fmt::format("{0}", Answer()));
  1378. EXPECT_EQ("0042", fmt::format("{:04}", Answer()));
  1379. }
  1380. TEST(format_test, format_to_custom) {
  1381. char buf[10] = {};
  1382. auto end =
  1383. &*fmt::format_to(fmt::detail::make_checked(buf, 10), "{}", Answer());
  1384. EXPECT_EQ(end, buf + 2);
  1385. EXPECT_STREQ(buf, "42");
  1386. }
  1387. TEST(format_test, format_string_from_speed_test) {
  1388. EXPECT_EQ("1.2340000000:0042:+3.13:str:0x3e8:X:%",
  1389. fmt::format("{0:0.10f}:{1:04}:{2:+g}:{3}:{4}:{5}:%", 1.234, 42,
  1390. 3.13, "str", reinterpret_cast<void*>(1000), 'X'));
  1391. }
  1392. TEST(format_test, format_examples) {
  1393. std::string message = fmt::format("The answer is {}", 42);
  1394. EXPECT_EQ("The answer is 42", message);
  1395. EXPECT_EQ("42", fmt::format("{}", 42));
  1396. memory_buffer out;
  1397. format_to(std::back_inserter(out), "The answer is {}.", 42);
  1398. EXPECT_EQ("The answer is 42.", to_string(out));
  1399. const char* filename = "nonexistent";
  1400. FILE* ftest = safe_fopen(filename, "r");
  1401. if (ftest) fclose(ftest);
  1402. int error_code = errno;
  1403. EXPECT_TRUE(ftest == nullptr);
  1404. EXPECT_SYSTEM_ERROR(
  1405. {
  1406. FILE* f = safe_fopen(filename, "r");
  1407. if (!f)
  1408. throw fmt::system_error(errno, "Cannot open file '{}'", filename);
  1409. fclose(f);
  1410. },
  1411. error_code, "Cannot open file 'nonexistent'");
  1412. EXPECT_EQ("First, thou shalt count to three",
  1413. fmt::format("First, thou shalt count to {0}", "three"));
  1414. EXPECT_EQ("Bring me a shrubbery", fmt::format("Bring me a {}", "shrubbery"));
  1415. EXPECT_EQ("From 1 to 3", fmt::format("From {} to {}", 1, 3));
  1416. char buffer[buffer_size];
  1417. safe_sprintf(buffer, "%03.2f", -1.2);
  1418. EXPECT_EQ(buffer, fmt::format("{:03.2f}", -1.2));
  1419. EXPECT_EQ("a, b, c", fmt::format("{0}, {1}, {2}", 'a', 'b', 'c'));
  1420. EXPECT_EQ("a, b, c", fmt::format("{}, {}, {}", 'a', 'b', 'c'));
  1421. EXPECT_EQ("c, b, a", fmt::format("{2}, {1}, {0}", 'a', 'b', 'c'));
  1422. EXPECT_EQ("abracadabra", fmt::format("{0}{1}{0}", "abra", "cad"));
  1423. EXPECT_EQ("left aligned ",
  1424. fmt::format("{:<30}", "left aligned"));
  1425. EXPECT_EQ(" right aligned",
  1426. fmt::format("{:>30}", "right aligned"));
  1427. EXPECT_EQ(" centered ",
  1428. fmt::format("{:^30}", "centered"));
  1429. EXPECT_EQ("***********centered***********",
  1430. fmt::format("{:*^30}", "centered"));
  1431. EXPECT_EQ("+3.140000; -3.140000", fmt::format("{:+f}; {:+f}", 3.14, -3.14));
  1432. EXPECT_EQ(" 3.140000; -3.140000", fmt::format("{: f}; {: f}", 3.14, -3.14));
  1433. EXPECT_EQ("3.140000; -3.140000", fmt::format("{:-f}; {:-f}", 3.14, -3.14));
  1434. EXPECT_EQ("int: 42; hex: 2a; oct: 52",
  1435. fmt::format("int: {0:d}; hex: {0:x}; oct: {0:o}", 42));
  1436. EXPECT_EQ("int: 42; hex: 0x2a; oct: 052",
  1437. fmt::format("int: {0:d}; hex: {0:#x}; oct: {0:#o}", 42));
  1438. EXPECT_EQ("The answer is 42", fmt::format("The answer is {}", 42));
  1439. EXPECT_THROW_MSG(
  1440. (void)fmt::format(runtime("The answer is {:d}"), "forty-two"),
  1441. format_error, "invalid type specifier");
  1442. EXPECT_WRITE(
  1443. stdout, fmt::print("{}", std::numeric_limits<double>::infinity()), "inf");
  1444. }
  1445. TEST(format_test, print) {
  1446. EXPECT_WRITE(stdout, fmt::print("Don't {}!", "panic"), "Don't panic!");
  1447. EXPECT_WRITE(stderr, fmt::print(stderr, "Don't {}!", "panic"),
  1448. "Don't panic!");
  1449. }
  1450. TEST(format_test, variadic) {
  1451. EXPECT_EQ("abc1", fmt::format("{}c{}", "ab", 1));
  1452. }
  1453. TEST(format_test, dynamic) {
  1454. using ctx = fmt::format_context;
  1455. auto args = std::vector<fmt::basic_format_arg<ctx>>();
  1456. args.emplace_back(fmt::detail::make_arg<ctx>(42));
  1457. args.emplace_back(fmt::detail::make_arg<ctx>("abc1"));
  1458. args.emplace_back(fmt::detail::make_arg<ctx>(1.5f));
  1459. std::string result = fmt::vformat(
  1460. "{} and {} and {}",
  1461. fmt::format_args(args.data(), static_cast<int>(args.size())));
  1462. EXPECT_EQ("42 and abc1 and 1.5", result);
  1463. }
  1464. TEST(format_test, bytes) {
  1465. auto s = fmt::format("{:10}", fmt::bytes("ёжик"));
  1466. EXPECT_EQ("ёжик ", s);
  1467. EXPECT_EQ(10, s.size());
  1468. }
  1469. TEST(format_test, group_digits_view) {
  1470. EXPECT_EQ(fmt::format("{}", fmt::group_digits(10000000)), "10,000,000");
  1471. EXPECT_EQ(fmt::format("{:8}", fmt::group_digits(1000)), " 1,000");
  1472. }
  1473. enum test_enum { foo, bar };
  1474. TEST(format_test, join) {
  1475. using fmt::join;
  1476. int v1[3] = {1, 2, 3};
  1477. auto v2 = std::vector<float>();
  1478. v2.push_back(1.2f);
  1479. v2.push_back(3.4f);
  1480. void* v3[2] = {&v1[0], &v1[1]};
  1481. EXPECT_EQ("(1, 2, 3)", fmt::format("({})", join(v1, v1 + 3, ", ")));
  1482. EXPECT_EQ("(1)", fmt::format("({})", join(v1, v1 + 1, ", ")));
  1483. EXPECT_EQ("()", fmt::format("({})", join(v1, v1, ", ")));
  1484. EXPECT_EQ("(001, 002, 003)", fmt::format("({:03})", join(v1, v1 + 3, ", ")));
  1485. EXPECT_EQ("(+01.20, +03.40)",
  1486. fmt::format("({:+06.2f})", join(v2.begin(), v2.end(), ", ")));
  1487. EXPECT_EQ("1, 2, 3", fmt::format("{0:{1}}", join(v1, v1 + 3, ", "), 1));
  1488. EXPECT_EQ(fmt::format("{}, {}", v3[0], v3[1]),
  1489. fmt::format("{}", join(v3, v3 + 2, ", ")));
  1490. EXPECT_EQ("(1, 2, 3)", fmt::format("({})", join(v1, ", ")));
  1491. EXPECT_EQ("(+01.20, +03.40)", fmt::format("({:+06.2f})", join(v2, ", ")));
  1492. auto v4 = std::vector<test_enum>{foo, bar, foo};
  1493. EXPECT_EQ("0 1 0", fmt::format("{}", join(v4, " ")));
  1494. }
  1495. #ifdef __cpp_lib_byte
  1496. TEST(format_test, join_bytes) {
  1497. auto v = std::vector<std::byte>{std::byte(1), std::byte(2), std::byte(3)};
  1498. EXPECT_EQ("1, 2, 3", fmt::format("{}", fmt::join(v, ", ")));
  1499. }
  1500. #endif
  1501. std::string vformat_message(int id, const char* format, fmt::format_args args) {
  1502. auto buffer = fmt::memory_buffer();
  1503. format_to(fmt::appender(buffer), "[{}] ", id);
  1504. vformat_to(fmt::appender(buffer), format, args);
  1505. return to_string(buffer);
  1506. }
  1507. template <typename... Args>
  1508. std::string format_message(int id, const char* format, const Args&... args) {
  1509. auto va = fmt::make_format_args(args...);
  1510. return vformat_message(id, format, va);
  1511. }
  1512. TEST(format_test, format_message_example) {
  1513. EXPECT_EQ("[42] something happened",
  1514. format_message(42, "{} happened", "something"));
  1515. }
  1516. template <typename... Args>
  1517. void print_error(const char* file, int line, const char* format,
  1518. const Args&... args) {
  1519. fmt::print("{}: {}: ", file, line);
  1520. fmt::print(format, args...);
  1521. }
  1522. TEST(format_test, unpacked_args) {
  1523. EXPECT_EQ("0123456789abcdefg",
  1524. fmt::format("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 0, 1, 2, 3, 4, 5,
  1525. 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f', 'g'));
  1526. }
  1527. struct string_like {};
  1528. fmt::string_view to_string_view(string_like) { return "foo"; }
  1529. constexpr char with_null[3] = {'{', '}', '\0'};
  1530. constexpr char no_null[2] = {'{', '}'};
  1531. static FMT_CONSTEXPR_DECL const char static_with_null[3] = {'{', '}', '\0'};
  1532. static FMT_CONSTEXPR_DECL const char static_no_null[2] = {'{', '}'};
  1533. TEST(format_test, compile_time_string) {
  1534. EXPECT_EQ("foo", fmt::format(FMT_STRING("foo")));
  1535. EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), 42));
  1536. EXPECT_EQ("foo", fmt::format(FMT_STRING("{}"), string_like()));
  1537. #if FMT_USE_NONTYPE_TEMPLATE_PARAMETERS
  1538. using namespace fmt::literals;
  1539. EXPECT_EQ("foobar", fmt::format(FMT_STRING("{foo}{bar}"), "bar"_a = "bar",
  1540. "foo"_a = "foo"));
  1541. EXPECT_EQ("", fmt::format(FMT_STRING("")));
  1542. EXPECT_EQ("", fmt::format(FMT_STRING(""), "arg"_a = 42));
  1543. #endif
  1544. (void)static_with_null;
  1545. (void)static_no_null;
  1546. #ifndef _MSC_VER
  1547. EXPECT_EQ("42", fmt::format(FMT_STRING(static_with_null), 42));
  1548. EXPECT_EQ("42", fmt::format(FMT_STRING(static_no_null), 42));
  1549. #endif
  1550. (void)with_null;
  1551. (void)no_null;
  1552. #if __cplusplus >= 201703L
  1553. EXPECT_EQ("42", fmt::format(FMT_STRING(with_null), 42));
  1554. EXPECT_EQ("42", fmt::format(FMT_STRING(no_null), 42));
  1555. #endif
  1556. #if defined(FMT_USE_STRING_VIEW) && __cplusplus >= 201703L
  1557. EXPECT_EQ("42", fmt::format(FMT_STRING(std::string_view("{}")), 42));
  1558. #endif
  1559. }
  1560. TEST(format_test, custom_format_compile_time_string) {
  1561. EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), Answer()));
  1562. auto answer = Answer();
  1563. EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), answer));
  1564. char buf[10] = {};
  1565. fmt::format_to(buf, FMT_STRING("{}"), answer);
  1566. const Answer const_answer = Answer();
  1567. EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), const_answer));
  1568. }
  1569. #if FMT_USE_USER_DEFINED_LITERALS
  1570. // Passing user-defined literals directly to EXPECT_EQ causes problems
  1571. // with macro argument stringification (#) on some versions of GCC.
  1572. // Workaround: Assing the UDL result to a variable before the macro.
  1573. using namespace fmt::literals;
  1574. # if FMT_GCC_VERSION
  1575. # define FMT_CHECK_DEPRECATED_UDL_FORMAT 1
  1576. # elif FMT_CLANG_VERSION && defined(__has_warning)
  1577. # if __has_warning("-Wdeprecated-declarations")
  1578. # define FMT_CHECK_DEPRECATED_UDL_FORMAT 1
  1579. # endif
  1580. # endif
  1581. # ifndef FMT_CHECK_DEPRECATED_UDL_FORMAT
  1582. # define FMT_CHECK_DEPRECATED_UDL_FORMAT 0
  1583. # endif
  1584. # if FMT_CHECK_DEPRECATED_UDL_FORMAT
  1585. # pragma GCC diagnostic push
  1586. # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  1587. TEST(format_test, format_udl) {
  1588. EXPECT_EQ("{}c{}"_format("ab", 1), fmt::format("{}c{}", "ab", 1));
  1589. EXPECT_EQ("foo"_format(), "foo");
  1590. EXPECT_EQ("{0:10}"_format(42), " 42");
  1591. EXPECT_EQ("{}"_format(date(2015, 10, 21)), "2015-10-21");
  1592. }
  1593. # pragma GCC diagnostic pop
  1594. # endif
  1595. TEST(format_test, named_arg_udl) {
  1596. auto udl_a = fmt::format("{first}{second}{first}{third}", "first"_a = "abra",
  1597. "second"_a = "cad", "third"_a = 99);
  1598. EXPECT_EQ(
  1599. fmt::format("{first}{second}{first}{third}", fmt::arg("first", "abra"),
  1600. fmt::arg("second", "cad"), fmt::arg("third", 99)),
  1601. udl_a);
  1602. }
  1603. #endif // FMT_USE_USER_DEFINED_LITERALS
  1604. TEST(format_test, enum) { EXPECT_EQ("0", fmt::format("{}", foo)); }
  1605. TEST(format_test, formatter_not_specialized) {
  1606. static_assert(!fmt::has_formatter<fmt::formatter<test_enum>,
  1607. fmt::format_context>::value,
  1608. "");
  1609. }
  1610. #if FMT_HAS_FEATURE(cxx_strong_enums)
  1611. enum big_enum : unsigned long long { big_enum_value = 5000000000ULL };
  1612. TEST(format_test, strong_enum) {
  1613. EXPECT_EQ("5000000000", fmt::format("{}", big_enum_value));
  1614. }
  1615. #endif
  1616. TEST(format_test, non_null_terminated_format_string) {
  1617. EXPECT_EQ("42", fmt::format(string_view("{}foo", 2), 42));
  1618. }
  1619. struct variant {
  1620. enum { int_type, string_type } type;
  1621. explicit variant(int) : type(int_type) {}
  1622. explicit variant(const char*) : type(string_type) {}
  1623. };
  1624. FMT_BEGIN_NAMESPACE
  1625. template <> struct formatter<variant> : dynamic_formatter<> {
  1626. auto format(variant value, format_context& ctx) -> decltype(ctx.out()) {
  1627. if (value.type == variant::int_type)
  1628. return dynamic_formatter<>::format(42, ctx);
  1629. return dynamic_formatter<>::format("foo", ctx);
  1630. }
  1631. };
  1632. FMT_END_NAMESPACE
  1633. TEST(format_test, dynamic_formatter) {
  1634. auto num = variant(42);
  1635. auto str = variant("foo");
  1636. EXPECT_EQ("42", fmt::format("{:d}", num));
  1637. EXPECT_EQ("foo", fmt::format("{:s}", str));
  1638. EXPECT_EQ(" 42 foo ", fmt::format("{:{}} {:{}}", num, 3, str, 4));
  1639. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{}}"), num), format_error,
  1640. "cannot switch from manual to automatic argument indexing");
  1641. EXPECT_THROW_MSG((void)fmt::format(runtime("{:{0}}"), num), format_error,
  1642. "cannot switch from automatic to manual argument indexing");
  1643. EXPECT_THROW_MSG((void)fmt::format(runtime("{:+}"), str), format_error,
  1644. "format specifier requires numeric argument");
  1645. EXPECT_THROW_MSG((void)fmt::format(runtime("{:-}"), str), format_error,
  1646. "format specifier requires numeric argument");
  1647. EXPECT_THROW_MSG((void)fmt::format(runtime("{: }"), str), format_error,
  1648. "format specifier requires numeric argument");
  1649. EXPECT_THROW_MSG((void)fmt::format(runtime("{:#}"), str), format_error,
  1650. "format specifier requires numeric argument");
  1651. EXPECT_THROW_MSG((void)fmt::format(runtime("{:0}"), str), format_error,
  1652. "format specifier requires numeric argument");
  1653. EXPECT_THROW_MSG((void)fmt::format(runtime("{:.2}"), num), format_error,
  1654. "precision not allowed for this argument type");
  1655. }
  1656. namespace adl_test {
  1657. namespace fmt {
  1658. namespace detail {
  1659. struct foo {};
  1660. template <typename, typename OutputIt> void write(OutputIt, foo) = delete;
  1661. } // namespace detail
  1662. } // namespace fmt
  1663. } // namespace adl_test
  1664. FMT_BEGIN_NAMESPACE
  1665. template <>
  1666. struct formatter<adl_test::fmt::detail::foo> : formatter<std::string> {
  1667. template <typename FormatContext>
  1668. auto format(adl_test::fmt::detail::foo, FormatContext& ctx)
  1669. -> decltype(ctx.out()) {
  1670. return formatter<std::string>::format("foo", ctx);
  1671. }
  1672. };
  1673. FMT_END_NAMESPACE
  1674. struct convertible_to_int {
  1675. operator int() const { return value; }
  1676. int value = 42;
  1677. };
  1678. TEST(format_test, to_string) {
  1679. EXPECT_EQ(fmt::to_string(42), "42");
  1680. EXPECT_EQ(fmt::to_string(reinterpret_cast<void*>(0x1234)), "0x1234");
  1681. EXPECT_EQ(fmt::to_string(adl_test::fmt::detail::foo()), "foo");
  1682. EXPECT_EQ(fmt::to_string(convertible_to_int()), "42");
  1683. enum foo : unsigned char { zero };
  1684. EXPECT_EQ(fmt::to_string(zero), "0");
  1685. }
  1686. TEST(format_test, output_iterators) {
  1687. std::list<char> out;
  1688. fmt::format_to(std::back_inserter(out), "{}", 42);
  1689. EXPECT_EQ("42", std::string(out.begin(), out.end()));
  1690. std::stringstream s;
  1691. fmt::format_to(std::ostream_iterator<char>(s), "{}", 42);
  1692. EXPECT_EQ("42", s.str());
  1693. }
  1694. TEST(format_test, formatted_size) {
  1695. EXPECT_EQ(2u, fmt::formatted_size("{}", 42));
  1696. }
  1697. TEST(format_test, format_to_no_args) {
  1698. std::string s;
  1699. fmt::format_to(std::back_inserter(s), "test");
  1700. EXPECT_EQ("test", s);
  1701. }
  1702. TEST(format_test, format_to) {
  1703. std::string s;
  1704. fmt::format_to(std::back_inserter(s), "part{0}", 1);
  1705. EXPECT_EQ("part1", s);
  1706. fmt::format_to(std::back_inserter(s), "part{0}", 2);
  1707. EXPECT_EQ("part1part2", s);
  1708. }
  1709. TEST(format_test, format_to_memory_buffer) {
  1710. auto buf = fmt::basic_memory_buffer<char, 100>();
  1711. fmt::format_to(fmt::appender(buf), "{}", "foo");
  1712. EXPECT_EQ("foo", to_string(buf));
  1713. }
  1714. TEST(format_test, format_to_vector) {
  1715. std::vector<char> v;
  1716. fmt::format_to(std::back_inserter(v), "{}", "foo");
  1717. EXPECT_EQ(string_view(v.data(), v.size()), "foo");
  1718. }
  1719. struct nongrowing_container {
  1720. using value_type = char;
  1721. void push_back(char) { throw std::runtime_error("can't take it any more"); }
  1722. };
  1723. TEST(format_test, format_to_propagates_exceptions) {
  1724. auto c = nongrowing_container();
  1725. EXPECT_THROW(fmt::format_to(std::back_inserter(c), "{}", 42),
  1726. std::runtime_error);
  1727. }
  1728. TEST(format_test, format_to_n) {
  1729. char buffer[4];
  1730. buffer[3] = 'x';
  1731. auto result = fmt::format_to_n(buffer, 3, "{}", 12345);
  1732. EXPECT_EQ(5u, result.size);
  1733. EXPECT_EQ(buffer + 3, result.out);
  1734. EXPECT_EQ("123x", fmt::string_view(buffer, 4));
  1735. result = fmt::format_to_n(buffer, 3, "{:s}", "foobar");
  1736. EXPECT_EQ(6u, result.size);
  1737. EXPECT_EQ(buffer + 3, result.out);
  1738. EXPECT_EQ("foox", fmt::string_view(buffer, 4));
  1739. buffer[0] = 'x';
  1740. buffer[1] = 'x';
  1741. buffer[2] = 'x';
  1742. result = fmt::format_to_n(buffer, 3, "{}", 'A');
  1743. EXPECT_EQ(1u, result.size);
  1744. EXPECT_EQ(buffer + 1, result.out);
  1745. EXPECT_EQ("Axxx", fmt::string_view(buffer, 4));
  1746. result = fmt::format_to_n(buffer, 3, "{}{} ", 'B', 'C');
  1747. EXPECT_EQ(3u, result.size);
  1748. EXPECT_EQ(buffer + 3, result.out);
  1749. EXPECT_EQ("BC x", fmt::string_view(buffer, 4));
  1750. result = fmt::format_to_n(buffer, 4, "{}", "ABCDE");
  1751. EXPECT_EQ(5u, result.size);
  1752. EXPECT_EQ("ABCD", fmt::string_view(buffer, 4));
  1753. buffer[3] = 'x';
  1754. result = fmt::format_to_n(buffer, 3, "{}", std::string(1000, '*'));
  1755. EXPECT_EQ(1000u, result.size);
  1756. EXPECT_EQ("***x", fmt::string_view(buffer, 4));
  1757. }
  1758. struct test_output_iterator {
  1759. char* data;
  1760. using iterator_category = std::output_iterator_tag;
  1761. using value_type = void;
  1762. using difference_type = void;
  1763. using pointer = void;
  1764. using reference = void;
  1765. test_output_iterator& operator++() {
  1766. ++data;
  1767. return *this;
  1768. }
  1769. test_output_iterator operator++(int) {
  1770. auto tmp = *this;
  1771. ++data;
  1772. return tmp;
  1773. }
  1774. char& operator*() { return *data; }
  1775. };
  1776. TEST(format_test, format_to_n_output_iterator) {
  1777. char buf[10] = {};
  1778. fmt::format_to_n(test_output_iterator{buf}, 10, "{}", 42);
  1779. EXPECT_STREQ(buf, "42");
  1780. }
  1781. #if FMT_USE_CONSTEXPR
  1782. struct test_error_handler {
  1783. const char*& error;
  1784. FMT_CONSTEXPR test_error_handler(const char*& err) : error(err) {}
  1785. FMT_CONSTEXPR test_error_handler(const test_error_handler& other)
  1786. : error(other.error) {}
  1787. FMT_CONSTEXPR void on_error(const char* message) {
  1788. if (!error) error = message;
  1789. }
  1790. };
  1791. FMT_CONSTEXPR size_t len(const char* s) {
  1792. size_t len = 0;
  1793. while (*s++) ++len;
  1794. return len;
  1795. }
  1796. FMT_CONSTEXPR bool equal(const char* s1, const char* s2) {
  1797. if (!s1 || !s2) return s1 == s2;
  1798. while (*s1 && *s1 == *s2) {
  1799. ++s1;
  1800. ++s2;
  1801. }
  1802. return *s1 == *s2;
  1803. }
  1804. template <typename... Args>
  1805. FMT_CONSTEXPR bool test_error(const char* fmt, const char* expected_error) {
  1806. const char* actual_error = nullptr;
  1807. auto s = string_view(fmt, len(fmt));
  1808. auto checker =
  1809. fmt::detail::format_string_checker<char, test_error_handler, Args...>(
  1810. s, test_error_handler(actual_error));
  1811. fmt::detail::parse_format_string<true>(s, checker);
  1812. return equal(actual_error, expected_error);
  1813. }
  1814. # define EXPECT_ERROR_NOARGS(fmt, error) \
  1815. static_assert(test_error(fmt, error), "")
  1816. # define EXPECT_ERROR(fmt, error, ...) \
  1817. static_assert(test_error<__VA_ARGS__>(fmt, error), "")
  1818. TEST(format_test, format_string_errors) {
  1819. EXPECT_ERROR_NOARGS("foo", nullptr);
  1820. EXPECT_ERROR_NOARGS("}", "unmatched '}' in format string");
  1821. EXPECT_ERROR("{0:s", "unknown format specifier", date);
  1822. # if !FMT_MSC_VER || FMT_MSC_VER >= 1916
  1823. // This causes an detail compiler error in MSVC2017.
  1824. EXPECT_ERROR("{:{<}", "invalid fill character '{'", int);
  1825. EXPECT_ERROR("{:10000000000}", "number is too big", int);
  1826. EXPECT_ERROR("{:.10000000000}", "number is too big", int);
  1827. EXPECT_ERROR_NOARGS("{:x}", "argument not found");
  1828. EXPECT_ERROR("{:+}", "format specifier requires numeric argument",
  1829. const char*);
  1830. EXPECT_ERROR("{:-}", "format specifier requires numeric argument",
  1831. const char*);
  1832. EXPECT_ERROR("{:#}", "format specifier requires numeric argument",
  1833. const char*);
  1834. EXPECT_ERROR("{: }", "format specifier requires numeric argument",
  1835. const char*);
  1836. EXPECT_ERROR("{:0}", "format specifier requires numeric argument",
  1837. const char*);
  1838. EXPECT_ERROR("{:+}", "format specifier requires signed argument", unsigned);
  1839. EXPECT_ERROR("{:-}", "format specifier requires signed argument", unsigned);
  1840. EXPECT_ERROR("{: }", "format specifier requires signed argument", unsigned);
  1841. EXPECT_ERROR("{:{}}", "argument not found", int);
  1842. EXPECT_ERROR("{:.{}}", "argument not found", double);
  1843. EXPECT_ERROR("{:.2}", "precision not allowed for this argument type", int);
  1844. EXPECT_ERROR("{:s}", "invalid type specifier", int);
  1845. EXPECT_ERROR("{:s}", "invalid type specifier", char);
  1846. EXPECT_ERROR("{:+}", "invalid format specifier for char", char);
  1847. EXPECT_ERROR("{:s}", "invalid type specifier", double);
  1848. EXPECT_ERROR("{:d}", "invalid type specifier", const char*);
  1849. EXPECT_ERROR("{:d}", "invalid type specifier", std::string);
  1850. EXPECT_ERROR("{:s}", "invalid type specifier", void*);
  1851. # else
  1852. fmt::print("warning: constexpr is broken in this version of MSVC\n");
  1853. # endif
  1854. # if FMT_USE_NONTYPE_TEMPLATE_PARAMETERS
  1855. EXPECT_ERROR("{foo}", "named argument is not found", decltype("bar"_a = 42));
  1856. EXPECT_ERROR("{foo}", "named argument is not found",
  1857. decltype(fmt::arg("foo", 42)));
  1858. # else
  1859. EXPECT_ERROR("{foo}",
  1860. "compile-time checks for named arguments require C++20 support",
  1861. int);
  1862. # endif
  1863. EXPECT_ERROR_NOARGS("{10000000000}", "argument not found");
  1864. EXPECT_ERROR_NOARGS("{0x}", "invalid format string");
  1865. EXPECT_ERROR_NOARGS("{-}", "invalid format string");
  1866. EXPECT_ERROR("{:{0x}}", "invalid format string", int);
  1867. EXPECT_ERROR("{:{-}}", "invalid format string", int);
  1868. EXPECT_ERROR("{:.{0x}}", "invalid format string", int);
  1869. EXPECT_ERROR("{:.{-}}", "invalid format string", int);
  1870. EXPECT_ERROR("{:.x}", "missing precision specifier", int);
  1871. EXPECT_ERROR_NOARGS("{}", "argument not found");
  1872. EXPECT_ERROR("{1}", "argument not found", int);
  1873. EXPECT_ERROR("{1}{}",
  1874. "cannot switch from manual to automatic argument indexing", int,
  1875. int);
  1876. EXPECT_ERROR("{}{1}",
  1877. "cannot switch from automatic to manual argument indexing", int,
  1878. int);
  1879. }
  1880. TEST(format_test, vformat_to) {
  1881. using context = fmt::format_context;
  1882. fmt::basic_format_arg<context> arg = fmt::detail::make_arg<context>(42);
  1883. auto args = fmt::basic_format_args<context>(&arg, 1);
  1884. auto s = std::string();
  1885. fmt::vformat_to(std::back_inserter(s), "{}", args);
  1886. EXPECT_EQ("42", s);
  1887. s.clear();
  1888. fmt::vformat_to(std::back_inserter(s), FMT_STRING("{}"), args);
  1889. EXPECT_EQ("42", s);
  1890. }
  1891. #endif // FMT_USE_CONSTEXPR
  1892. TEST(format_test, char_traits_is_not_ambiguous) {
  1893. // Test that we don't inject detail names into the std namespace.
  1894. using namespace std;
  1895. auto c = char_traits<char>::char_type();
  1896. (void)c;
  1897. #if __cplusplus >= 201103L
  1898. auto s = std::string();
  1899. auto lval = begin(s);
  1900. (void)lval;
  1901. #endif
  1902. }
  1903. struct check_back_appender {};
  1904. FMT_BEGIN_NAMESPACE
  1905. template <> struct formatter<check_back_appender> {
  1906. auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
  1907. return ctx.begin();
  1908. }
  1909. template <typename Context>
  1910. auto format(check_back_appender, Context& ctx) -> decltype(ctx.out()) {
  1911. auto out = ctx.out();
  1912. static_assert(std::is_same<decltype(++out), decltype(out)&>::value,
  1913. "needs to satisfy weakly_incrementable");
  1914. *out = 'y';
  1915. return ++out;
  1916. }
  1917. };
  1918. FMT_END_NAMESPACE
  1919. TEST(format_test, back_insert_slicing) {
  1920. EXPECT_EQ(fmt::format("{}", check_back_appender{}), "y");
  1921. }
  1922. template <typename Char, typename T> bool check_enabled_formatter() {
  1923. static_assert(std::is_default_constructible<fmt::formatter<T, Char>>::value,
  1924. "");
  1925. return true;
  1926. }
  1927. template <typename Char, typename... T> void check_enabled_formatters() {
  1928. auto dummy = {check_enabled_formatter<Char, T>()...};
  1929. (void)dummy;
  1930. }
  1931. TEST(format_test, test_formatters_enabled) {
  1932. check_enabled_formatters<char, bool, char, signed char, unsigned char, short,
  1933. unsigned short, int, unsigned, long, unsigned long,
  1934. long long, unsigned long long, float, double,
  1935. long double, void*, const void*, char*, const char*,
  1936. std::string, std::nullptr_t>();
  1937. check_enabled_formatters<wchar_t, bool, wchar_t, signed char, unsigned char,
  1938. short, unsigned short, int, unsigned, long,
  1939. unsigned long, long long, unsigned long long, float,
  1940. double, long double, void*, const void*, wchar_t*,
  1941. const wchar_t*, std::wstring, std::nullptr_t>();
  1942. }
  1943. TEST(format_int_test, data) {
  1944. fmt::format_int format_int(42);
  1945. EXPECT_EQ("42", std::string(format_int.data(), format_int.size()));
  1946. }
  1947. TEST(format_int_test, format_int) {
  1948. EXPECT_EQ("42", fmt::format_int(42).str());
  1949. EXPECT_EQ(2u, fmt::format_int(42).size());
  1950. EXPECT_EQ("-42", fmt::format_int(-42).str());
  1951. EXPECT_EQ(3u, fmt::format_int(-42).size());
  1952. EXPECT_EQ("42", fmt::format_int(42ul).str());
  1953. EXPECT_EQ("-42", fmt::format_int(-42l).str());
  1954. EXPECT_EQ("42", fmt::format_int(42ull).str());
  1955. EXPECT_EQ("-42", fmt::format_int(-42ll).str());
  1956. std::ostringstream os;
  1957. os << max_value<int64_t>();
  1958. EXPECT_EQ(os.str(), fmt::format_int(max_value<int64_t>()).str());
  1959. }