compile-test.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. #include "fmt/compile.h"
  8. #include <type_traits>
  9. #include "fmt/chrono.h"
  10. #include "gmock/gmock.h"
  11. #include "gtest-extra.h"
  12. TEST(iterator_test, counting_iterator) {
  13. auto it = fmt::detail::counting_iterator();
  14. auto prev = it++;
  15. EXPECT_EQ(prev.count(), 0);
  16. EXPECT_EQ(it.count(), 1);
  17. EXPECT_EQ((it + 41).count(), 42);
  18. }
  19. TEST(iterator_test, truncating_iterator) {
  20. char* p = nullptr;
  21. auto it = fmt::detail::truncating_iterator<char*>(p, 3);
  22. auto prev = it++;
  23. EXPECT_EQ(prev.base(), p);
  24. EXPECT_EQ(it.base(), p + 1);
  25. }
  26. TEST(iterator_test, truncating_iterator_default_construct) {
  27. auto it = fmt::detail::truncating_iterator<char*>();
  28. EXPECT_EQ(nullptr, it.base());
  29. EXPECT_EQ(std::size_t{0}, it.count());
  30. }
  31. #ifdef __cpp_lib_ranges
  32. TEST(iterator_test, truncating_iterator_is_output_iterator) {
  33. static_assert(
  34. std::output_iterator<fmt::detail::truncating_iterator<char*>, char>);
  35. }
  36. #endif
  37. TEST(iterator_test, truncating_back_inserter) {
  38. auto buffer = std::string();
  39. auto bi = std::back_inserter(buffer);
  40. auto it = fmt::detail::truncating_iterator<decltype(bi)>(bi, 2);
  41. *it++ = '4';
  42. *it++ = '2';
  43. *it++ = '1';
  44. EXPECT_EQ(buffer.size(), 2);
  45. EXPECT_EQ(buffer, "42");
  46. }
  47. TEST(compile_test, compile_fallback) {
  48. // FMT_COMPILE should fallback on runtime formatting when `if constexpr` is
  49. // not available.
  50. EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42));
  51. }
  52. struct type_with_get {
  53. template <int> friend void get(type_with_get);
  54. };
  55. FMT_BEGIN_NAMESPACE
  56. template <> struct formatter<type_with_get> : formatter<int> {
  57. template <typename FormatContext>
  58. auto format(type_with_get, FormatContext& ctx) -> decltype(ctx.out()) {
  59. return formatter<int>::format(42, ctx);
  60. }
  61. };
  62. FMT_END_NAMESPACE
  63. TEST(compile_test, compile_type_with_get) {
  64. EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), type_with_get()));
  65. }
  66. #if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
  67. struct test_formattable {};
  68. FMT_BEGIN_NAMESPACE
  69. template <> struct formatter<test_formattable> : formatter<const char*> {
  70. char word_spec = 'f';
  71. constexpr auto parse(format_parse_context& ctx) {
  72. auto it = ctx.begin(), end = ctx.end();
  73. if (it == end || *it == '}') return it;
  74. if (it != end && (*it == 'f' || *it == 'b')) word_spec = *it++;
  75. if (it != end && *it != '}') throw format_error("invalid format");
  76. return it;
  77. }
  78. template <typename FormatContext>
  79. constexpr auto format(test_formattable, FormatContext& ctx) const
  80. -> decltype(ctx.out()) {
  81. return formatter<const char*>::format(word_spec == 'f' ? "foo" : "bar",
  82. ctx);
  83. }
  84. };
  85. FMT_END_NAMESPACE
  86. TEST(compile_test, format_default) {
  87. EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42));
  88. EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42u));
  89. EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42ll));
  90. EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42ull));
  91. EXPECT_EQ("true", fmt::format(FMT_COMPILE("{}"), true));
  92. EXPECT_EQ("x", fmt::format(FMT_COMPILE("{}"), 'x'));
  93. EXPECT_EQ("4.2", fmt::format(FMT_COMPILE("{}"), 4.2));
  94. EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), "foo"));
  95. EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), std::string("foo")));
  96. EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), test_formattable()));
  97. auto t = std::chrono::system_clock::now();
  98. EXPECT_EQ(fmt::format("{}", t), fmt::format(FMT_COMPILE("{}"), t));
  99. # ifdef __cpp_lib_byte
  100. EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), std::byte{42}));
  101. # endif
  102. }
  103. TEST(compile_test, format_wide_string) {
  104. EXPECT_EQ(L"42", fmt::format(FMT_COMPILE(L"{}"), 42));
  105. }
  106. TEST(compile_test, format_specs) {
  107. EXPECT_EQ("42", fmt::format(FMT_COMPILE("{:x}"), 0x42));
  108. EXPECT_EQ("1.2 ms ",
  109. fmt::format(FMT_COMPILE("{:7.1%Q %q}"),
  110. std::chrono::duration<double, std::milli>(1.234)));
  111. }
  112. TEST(compile_test, dynamic_format_specs) {
  113. EXPECT_EQ("foo ", fmt::format(FMT_COMPILE("{:{}}"), "foo", 5));
  114. EXPECT_EQ(" 3.14", fmt::format(FMT_COMPILE("{:{}.{}f}"), 3.141592, 6, 2));
  115. EXPECT_EQ(
  116. "=1.234ms=",
  117. fmt::format(FMT_COMPILE("{:=^{}.{}}"),
  118. std::chrono::duration<double, std::milli>(1.234), 9, 3));
  119. }
  120. TEST(compile_test, manual_ordering) {
  121. EXPECT_EQ("42", fmt::format(FMT_COMPILE("{0}"), 42));
  122. EXPECT_EQ(" -42", fmt::format(FMT_COMPILE("{0:4}"), -42));
  123. EXPECT_EQ("41 43", fmt::format(FMT_COMPILE("{0} {1}"), 41, 43));
  124. EXPECT_EQ("41 43", fmt::format(FMT_COMPILE("{1} {0}"), 43, 41));
  125. EXPECT_EQ("41 43", fmt::format(FMT_COMPILE("{0} {2}"), 41, 42, 43));
  126. EXPECT_EQ(" 41 43", fmt::format(FMT_COMPILE("{1:{2}} {0:4}"), 43, 41, 4));
  127. EXPECT_EQ("42 1.2 ms ",
  128. fmt::format(FMT_COMPILE("{0} {1:7.1%Q %q}"), 42,
  129. std::chrono::duration<double, std::milli>(1.234)));
  130. EXPECT_EQ(
  131. "true 42 42 foo 0x1234 foo",
  132. fmt::format(FMT_COMPILE("{0} {1} {2} {3} {4} {5}"), true, 42, 42.0f,
  133. "foo", reinterpret_cast<void*>(0x1234), test_formattable()));
  134. EXPECT_EQ(L"42", fmt::format(FMT_COMPILE(L"{0}"), 42));
  135. }
  136. TEST(compile_test, named) {
  137. auto runtime_named_field_compiled =
  138. fmt::detail::compile<decltype(fmt::arg("arg", 42))>(FMT_COMPILE("{arg}"));
  139. static_assert(std::is_same_v<decltype(runtime_named_field_compiled),
  140. fmt::detail::runtime_named_field<char>>);
  141. EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), fmt::arg("arg", 42)));
  142. EXPECT_EQ("41 43", fmt::format(FMT_COMPILE("{} {}"), fmt::arg("arg", 41),
  143. fmt::arg("arg", 43)));
  144. EXPECT_EQ("foobar",
  145. fmt::format(FMT_COMPILE("{a0}{a1}"), fmt::arg("a0", "foo"),
  146. fmt::arg("a1", "bar")));
  147. EXPECT_EQ("foobar", fmt::format(FMT_COMPILE("{}{a1}"), fmt::arg("a0", "foo"),
  148. fmt::arg("a1", "bar")));
  149. EXPECT_EQ("foofoo", fmt::format(FMT_COMPILE("{a0}{}"), fmt::arg("a0", "foo"),
  150. fmt::arg("a1", "bar")));
  151. EXPECT_EQ("foobar", fmt::format(FMT_COMPILE("{0}{a1}"), fmt::arg("a0", "foo"),
  152. fmt::arg("a1", "bar")));
  153. EXPECT_EQ("foobar", fmt::format(FMT_COMPILE("{a0}{1}"), fmt::arg("a0", "foo"),
  154. fmt::arg("a1", "bar")));
  155. EXPECT_EQ("foobar",
  156. fmt::format(FMT_COMPILE("{}{a1}"), "foo", fmt::arg("a1", "bar")));
  157. EXPECT_EQ("foobar",
  158. fmt::format(FMT_COMPILE("{a0}{a1}"), fmt::arg("a1", "bar"),
  159. fmt::arg("a2", "baz"), fmt::arg("a0", "foo")));
  160. EXPECT_EQ(" bar foo ",
  161. fmt::format(FMT_COMPILE(" {foo} {bar} "), fmt::arg("foo", "bar"),
  162. fmt::arg("bar", "foo")));
  163. EXPECT_THROW(fmt::format(FMT_COMPILE("{invalid}"), fmt::arg("valid", 42)),
  164. fmt::format_error);
  165. # if FMT_USE_NONTYPE_TEMPLATE_PARAMETERS
  166. using namespace fmt::literals;
  167. auto statically_named_field_compiled =
  168. fmt::detail::compile<decltype("arg"_a = 42)>(FMT_COMPILE("{arg}"));
  169. static_assert(std::is_same_v<decltype(statically_named_field_compiled),
  170. fmt::detail::field<char, int, 0>>);
  171. EXPECT_EQ("41 43",
  172. fmt::format(FMT_COMPILE("{a0} {a1}"), "a0"_a = 41, "a1"_a = 43));
  173. EXPECT_EQ("41 43",
  174. fmt::format(FMT_COMPILE("{a1} {a0}"), "a0"_a = 43, "a1"_a = 41));
  175. # endif
  176. }
  177. TEST(compile_test, format_to) {
  178. char buf[8];
  179. auto end = fmt::format_to(buf, FMT_COMPILE("{}"), 42);
  180. *end = '\0';
  181. EXPECT_STREQ("42", buf);
  182. end = fmt::format_to(buf, FMT_COMPILE("{:x}"), 42);
  183. *end = '\0';
  184. EXPECT_STREQ("2a", buf);
  185. }
  186. TEST(compile_test, format_to_n) {
  187. constexpr auto buffer_size = 8;
  188. char buffer[buffer_size];
  189. auto res = fmt::format_to_n(buffer, buffer_size, FMT_COMPILE("{}"), 42);
  190. *res.out = '\0';
  191. EXPECT_STREQ("42", buffer);
  192. res = fmt::format_to_n(buffer, buffer_size, FMT_COMPILE("{:x}"), 42);
  193. *res.out = '\0';
  194. EXPECT_STREQ("2a", buffer);
  195. }
  196. TEST(compile_test, formatted_size) {
  197. EXPECT_EQ(2, fmt::formatted_size(FMT_COMPILE("{0}"), 42));
  198. EXPECT_EQ(5, fmt::formatted_size(FMT_COMPILE("{0:<4.2f}"), 42.0));
  199. }
  200. TEST(compile_test, text_and_arg) {
  201. EXPECT_EQ(">>>42<<<", fmt::format(FMT_COMPILE(">>>{}<<<"), 42));
  202. EXPECT_EQ("42!", fmt::format(FMT_COMPILE("{}!"), 42));
  203. }
  204. TEST(compile_test, unknown_format_fallback) {
  205. EXPECT_EQ(" 42 ",
  206. fmt::format(FMT_COMPILE("{name:^4}"), fmt::arg("name", 42)));
  207. std::vector<char> v;
  208. fmt::format_to(std::back_inserter(v), FMT_COMPILE("{name:^4}"),
  209. fmt::arg("name", 42));
  210. EXPECT_EQ(" 42 ", fmt::string_view(v.data(), v.size()));
  211. char buffer[4];
  212. auto result = fmt::format_to_n(buffer, 4, FMT_COMPILE("{name:^5}"),
  213. fmt::arg("name", 42));
  214. EXPECT_EQ(5u, result.size);
  215. EXPECT_EQ(buffer + 4, result.out);
  216. EXPECT_EQ(" 42 ", fmt::string_view(buffer, 4));
  217. }
  218. TEST(compile_test, empty) { EXPECT_EQ("", fmt::format(FMT_COMPILE(""))); }
  219. struct to_stringable {
  220. friend fmt::string_view to_string_view(to_stringable) { return {}; }
  221. };
  222. FMT_BEGIN_NAMESPACE
  223. template <> struct formatter<to_stringable> {
  224. auto parse(format_parse_context& ctx) const -> decltype(ctx.begin()) {
  225. return ctx.begin();
  226. }
  227. template <typename FormatContext>
  228. auto format(const to_stringable&, FormatContext& ctx) -> decltype(ctx.out()) {
  229. return ctx.out();
  230. }
  231. };
  232. FMT_END_NAMESPACE
  233. TEST(compile_test, to_string_and_formatter) {
  234. fmt::format(FMT_COMPILE("{}"), to_stringable());
  235. }
  236. TEST(compile_test, print) {
  237. EXPECT_WRITE(stdout, fmt::print(FMT_COMPILE("Don't {}!"), "panic"),
  238. "Don't panic!");
  239. EXPECT_WRITE(stderr, fmt::print(stderr, FMT_COMPILE("Don't {}!"), "panic"),
  240. "Don't panic!");
  241. }
  242. #endif
  243. #if FMT_USE_NONTYPE_TEMPLATE_PARAMETERS
  244. TEST(compile_test, compile_format_string_literal) {
  245. using namespace fmt::literals;
  246. EXPECT_EQ("", fmt::format(""_cf));
  247. EXPECT_EQ("42", fmt::format("{}"_cf, 42));
  248. EXPECT_EQ(L"42", fmt::format(L"{}"_cf, 42));
  249. }
  250. #endif
  251. #if __cplusplus >= 202002L || \
  252. (__cplusplus >= 201709L && FMT_GCC_VERSION >= 1002)
  253. template <size_t max_string_length, typename Char = char> struct test_string {
  254. template <typename T> constexpr bool operator==(const T& rhs) const noexcept {
  255. return fmt::basic_string_view<Char>(rhs).compare(buffer) == 0;
  256. }
  257. Char buffer[max_string_length]{};
  258. };
  259. template <size_t max_string_length, typename Char = char, typename... Args>
  260. consteval auto test_format(auto format, const Args&... args) {
  261. test_string<max_string_length, Char> string{};
  262. fmt::format_to(string.buffer, format, args...);
  263. return string;
  264. }
  265. TEST(compile_time_formatting_test, bool) {
  266. EXPECT_EQ("true", test_format<5>(FMT_COMPILE("{}"), true));
  267. EXPECT_EQ("false", test_format<6>(FMT_COMPILE("{}"), false));
  268. EXPECT_EQ("true ", test_format<6>(FMT_COMPILE("{:5}"), true));
  269. EXPECT_EQ("1", test_format<2>(FMT_COMPILE("{:d}"), true));
  270. }
  271. TEST(compile_time_formatting_test, integer) {
  272. EXPECT_EQ("42", test_format<3>(FMT_COMPILE("{}"), 42));
  273. EXPECT_EQ("420", test_format<4>(FMT_COMPILE("{}"), 420));
  274. EXPECT_EQ("42 42", test_format<6>(FMT_COMPILE("{} {}"), 42, 42));
  275. EXPECT_EQ("42 42",
  276. test_format<6>(FMT_COMPILE("{} {}"), uint32_t{42}, uint64_t{42}));
  277. EXPECT_EQ("+42", test_format<4>(FMT_COMPILE("{:+}"), 42));
  278. EXPECT_EQ("42", test_format<3>(FMT_COMPILE("{:-}"), 42));
  279. EXPECT_EQ(" 42", test_format<4>(FMT_COMPILE("{: }"), 42));
  280. EXPECT_EQ("-0042", test_format<6>(FMT_COMPILE("{:05}"), -42));
  281. EXPECT_EQ("101010", test_format<7>(FMT_COMPILE("{:b}"), 42));
  282. EXPECT_EQ("0b101010", test_format<9>(FMT_COMPILE("{:#b}"), 42));
  283. EXPECT_EQ("0B101010", test_format<9>(FMT_COMPILE("{:#B}"), 42));
  284. EXPECT_EQ("042", test_format<4>(FMT_COMPILE("{:#o}"), 042));
  285. EXPECT_EQ("0x4a", test_format<5>(FMT_COMPILE("{:#x}"), 0x4a));
  286. EXPECT_EQ("0X4A", test_format<5>(FMT_COMPILE("{:#X}"), 0x4a));
  287. EXPECT_EQ(" 42", test_format<6>(FMT_COMPILE("{:5}"), 42));
  288. EXPECT_EQ(" 42", test_format<6>(FMT_COMPILE("{:5}"), 42ll));
  289. EXPECT_EQ(" 42", test_format<6>(FMT_COMPILE("{:5}"), 42ull));
  290. EXPECT_EQ("42 ", test_format<5>(FMT_COMPILE("{:<4}"), 42));
  291. EXPECT_EQ(" 42", test_format<5>(FMT_COMPILE("{:>4}"), 42));
  292. EXPECT_EQ(" 42 ", test_format<5>(FMT_COMPILE("{:^4}"), 42));
  293. EXPECT_EQ("**-42", test_format<6>(FMT_COMPILE("{:*>5}"), -42));
  294. }
  295. TEST(compile_time_formatting_test, char) {
  296. EXPECT_EQ("c", test_format<2>(FMT_COMPILE("{}"), 'c'));
  297. EXPECT_EQ("c ", test_format<4>(FMT_COMPILE("{:3}"), 'c'));
  298. EXPECT_EQ("99", test_format<3>(FMT_COMPILE("{:d}"), 'c'));
  299. }
  300. TEST(compile_time_formatting_test, string) {
  301. EXPECT_EQ("42", test_format<3>(FMT_COMPILE("{}"), "42"));
  302. EXPECT_EQ("The answer is 42",
  303. test_format<17>(FMT_COMPILE("{} is {}"), "The answer", "42"));
  304. EXPECT_EQ("abc**", test_format<6>(FMT_COMPILE("{:*<5}"), "abc"));
  305. EXPECT_EQ("**🤡**", test_format<9>(FMT_COMPILE("{:*^6}"), "🤡"));
  306. }
  307. TEST(compile_time_formatting_test, combination) {
  308. EXPECT_EQ("420, true, answer",
  309. test_format<18>(FMT_COMPILE("{}, {}, {}"), 420, true, "answer"));
  310. EXPECT_EQ(" -42", test_format<5>(FMT_COMPILE("{:{}}"), -42, 4));
  311. }
  312. TEST(compile_time_formatting_test, custom_type) {
  313. EXPECT_EQ("foo", test_format<4>(FMT_COMPILE("{}"), test_formattable()));
  314. EXPECT_EQ("bar", test_format<4>(FMT_COMPILE("{:b}"), test_formattable()));
  315. }
  316. TEST(compile_time_formatting_test, multibyte_fill) {
  317. EXPECT_EQ("жж42", test_format<8>(FMT_COMPILE("{:ж>4}"), 42));
  318. }
  319. #endif