compile-test.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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_ARGS
  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, join) {
  178. unsigned char data[] = {0x1, 0x2, 0xaf};
  179. EXPECT_EQ("0102af", fmt::format(FMT_COMPILE("{:02x}"), fmt::join(data, "")));
  180. }
  181. TEST(compile_test, format_to) {
  182. char buf[8];
  183. auto end = fmt::format_to(buf, FMT_COMPILE("{}"), 42);
  184. *end = '\0';
  185. EXPECT_STREQ("42", buf);
  186. end = fmt::format_to(buf, FMT_COMPILE("{:x}"), 42);
  187. *end = '\0';
  188. EXPECT_STREQ("2a", buf);
  189. }
  190. TEST(compile_test, format_to_n) {
  191. constexpr auto buffer_size = 8;
  192. char buffer[buffer_size];
  193. auto res = fmt::format_to_n(buffer, buffer_size, FMT_COMPILE("{}"), 42);
  194. *res.out = '\0';
  195. EXPECT_STREQ("42", buffer);
  196. res = fmt::format_to_n(buffer, buffer_size, FMT_COMPILE("{:x}"), 42);
  197. *res.out = '\0';
  198. EXPECT_STREQ("2a", buffer);
  199. }
  200. #ifdef __cpp_lib_bit_cast
  201. TEST(compile_test, constexpr_formatted_size) {
  202. FMT_CONSTEXPR20 size_t s1 = fmt::formatted_size(FMT_COMPILE("{0}"), 42);
  203. EXPECT_EQ(2, s1);
  204. FMT_CONSTEXPR20 size_t s2 = fmt::formatted_size(FMT_COMPILE("{0:<4.2f}"), 42.0);
  205. EXPECT_EQ(5, s2);
  206. }
  207. #endif
  208. TEST(compile_test, text_and_arg) {
  209. EXPECT_EQ(">>>42<<<", fmt::format(FMT_COMPILE(">>>{}<<<"), 42));
  210. EXPECT_EQ("42!", fmt::format(FMT_COMPILE("{}!"), 42));
  211. }
  212. TEST(compile_test, unknown_format_fallback) {
  213. EXPECT_EQ(" 42 ",
  214. fmt::format(FMT_COMPILE("{name:^4}"), fmt::arg("name", 42)));
  215. std::vector<char> v;
  216. fmt::format_to(std::back_inserter(v), FMT_COMPILE("{name:^4}"),
  217. fmt::arg("name", 42));
  218. EXPECT_EQ(" 42 ", fmt::string_view(v.data(), v.size()));
  219. char buffer[4];
  220. auto result = fmt::format_to_n(buffer, 4, FMT_COMPILE("{name:^5}"),
  221. fmt::arg("name", 42));
  222. EXPECT_EQ(5u, result.size);
  223. EXPECT_EQ(buffer + 4, result.out);
  224. EXPECT_EQ(" 42 ", fmt::string_view(buffer, 4));
  225. }
  226. TEST(compile_test, empty) { EXPECT_EQ("", fmt::format(FMT_COMPILE(""))); }
  227. struct to_stringable {
  228. friend fmt::string_view to_string_view(to_stringable) { return {}; }
  229. };
  230. FMT_BEGIN_NAMESPACE
  231. template <> struct formatter<to_stringable> {
  232. auto parse(format_parse_context& ctx) const -> decltype(ctx.begin()) {
  233. return ctx.begin();
  234. }
  235. template <typename FormatContext>
  236. auto format(const to_stringable&, FormatContext& ctx) -> decltype(ctx.out()) {
  237. return ctx.out();
  238. }
  239. };
  240. FMT_END_NAMESPACE
  241. TEST(compile_test, to_string_and_formatter) {
  242. fmt::format(FMT_COMPILE("{}"), to_stringable());
  243. }
  244. TEST(compile_test, print) {
  245. EXPECT_WRITE(stdout, fmt::print(FMT_COMPILE("Don't {}!"), "panic"),
  246. "Don't panic!");
  247. EXPECT_WRITE(stderr, fmt::print(stderr, FMT_COMPILE("Don't {}!"), "panic"),
  248. "Don't panic!");
  249. }
  250. #endif
  251. #if FMT_USE_NONTYPE_TEMPLATE_ARGS
  252. TEST(compile_test, compile_format_string_literal) {
  253. using namespace fmt::literals;
  254. EXPECT_EQ("", fmt::format(""_cf));
  255. EXPECT_EQ("42", fmt::format("{}"_cf, 42));
  256. EXPECT_EQ(L"42", fmt::format(L"{}"_cf, 42));
  257. }
  258. #endif
  259. // MSVS 2019 19.29.30145.0 - Support C++20 and OK.
  260. // MSVS 2022 19.32.31332.0 - compile-test.cc(362,3): fatal error C1001: Internal
  261. // compiler error.
  262. // (compiler file
  263. // 'D:\a\_work\1\s\src\vctools\Compiler\CxxFE\sl\p1\c\constexpr\constexpr.cpp',
  264. // line 8635)
  265. #if ((FMT_CPLUSPLUS >= 202002L) && \
  266. (!FMT_MSC_VERSION || FMT_MSC_VERSION < 1930)) || \
  267. (FMT_CPLUSPLUS >= 201709L && FMT_GCC_VERSION >= 1002)
  268. template <size_t max_string_length, typename Char = char> struct test_string {
  269. template <typename T> constexpr bool operator==(const T& rhs) const noexcept {
  270. return fmt::basic_string_view<Char>(rhs).compare(buffer) == 0;
  271. }
  272. Char buffer[max_string_length]{};
  273. };
  274. template <size_t max_string_length, typename Char = char, typename... Args>
  275. consteval auto test_format(auto format, const Args&... args) {
  276. test_string<max_string_length, Char> string{};
  277. fmt::format_to(string.buffer, format, args...);
  278. return string;
  279. }
  280. TEST(compile_time_formatting_test, bool) {
  281. EXPECT_EQ("true", test_format<5>(FMT_COMPILE("{}"), true));
  282. EXPECT_EQ("false", test_format<6>(FMT_COMPILE("{}"), false));
  283. EXPECT_EQ("true ", test_format<6>(FMT_COMPILE("{:5}"), true));
  284. EXPECT_EQ("1", test_format<2>(FMT_COMPILE("{:d}"), true));
  285. }
  286. TEST(compile_time_formatting_test, integer) {
  287. EXPECT_EQ("42", test_format<3>(FMT_COMPILE("{}"), 42));
  288. EXPECT_EQ("420", test_format<4>(FMT_COMPILE("{}"), 420));
  289. EXPECT_EQ("42 42", test_format<6>(FMT_COMPILE("{} {}"), 42, 42));
  290. EXPECT_EQ("42 42",
  291. test_format<6>(FMT_COMPILE("{} {}"), uint32_t{42}, uint64_t{42}));
  292. EXPECT_EQ("+42", test_format<4>(FMT_COMPILE("{:+}"), 42));
  293. EXPECT_EQ("42", test_format<3>(FMT_COMPILE("{:-}"), 42));
  294. EXPECT_EQ(" 42", test_format<4>(FMT_COMPILE("{: }"), 42));
  295. EXPECT_EQ("-0042", test_format<6>(FMT_COMPILE("{:05}"), -42));
  296. EXPECT_EQ("101010", test_format<7>(FMT_COMPILE("{:b}"), 42));
  297. EXPECT_EQ("0b101010", test_format<9>(FMT_COMPILE("{:#b}"), 42));
  298. EXPECT_EQ("0B101010", test_format<9>(FMT_COMPILE("{:#B}"), 42));
  299. EXPECT_EQ("042", test_format<4>(FMT_COMPILE("{:#o}"), 042));
  300. EXPECT_EQ("0x4a", test_format<5>(FMT_COMPILE("{:#x}"), 0x4a));
  301. EXPECT_EQ("0X4A", test_format<5>(FMT_COMPILE("{:#X}"), 0x4a));
  302. EXPECT_EQ(" 42", test_format<6>(FMT_COMPILE("{:5}"), 42));
  303. EXPECT_EQ(" 42", test_format<6>(FMT_COMPILE("{:5}"), 42ll));
  304. EXPECT_EQ(" 42", test_format<6>(FMT_COMPILE("{:5}"), 42ull));
  305. EXPECT_EQ("42 ", test_format<5>(FMT_COMPILE("{:<4}"), 42));
  306. EXPECT_EQ(" 42", test_format<5>(FMT_COMPILE("{:>4}"), 42));
  307. EXPECT_EQ(" 42 ", test_format<5>(FMT_COMPILE("{:^4}"), 42));
  308. EXPECT_EQ("**-42", test_format<6>(FMT_COMPILE("{:*>5}"), -42));
  309. }
  310. TEST(compile_time_formatting_test, char) {
  311. EXPECT_EQ("c", test_format<2>(FMT_COMPILE("{}"), 'c'));
  312. EXPECT_EQ("c ", test_format<4>(FMT_COMPILE("{:3}"), 'c'));
  313. EXPECT_EQ("99", test_format<3>(FMT_COMPILE("{:d}"), 'c'));
  314. }
  315. TEST(compile_time_formatting_test, string) {
  316. EXPECT_EQ("42", test_format<3>(FMT_COMPILE("{}"), "42"));
  317. EXPECT_EQ("The answer is 42",
  318. test_format<17>(FMT_COMPILE("{} is {}"), "The answer", "42"));
  319. EXPECT_EQ("abc**", test_format<6>(FMT_COMPILE("{:*<5}"), "abc"));
  320. EXPECT_EQ("**🤡**", test_format<9>(FMT_COMPILE("{:*^6}"), "🤡"));
  321. }
  322. TEST(compile_time_formatting_test, combination) {
  323. EXPECT_EQ("420, true, answer",
  324. test_format<18>(FMT_COMPILE("{}, {}, {}"), 420, true, "answer"));
  325. EXPECT_EQ(" -42", test_format<5>(FMT_COMPILE("{:{}}"), -42, 4));
  326. }
  327. TEST(compile_time_formatting_test, custom_type) {
  328. EXPECT_EQ("foo", test_format<4>(FMT_COMPILE("{}"), test_formattable()));
  329. EXPECT_EQ("bar", test_format<4>(FMT_COMPILE("{:b}"), test_formattable()));
  330. }
  331. TEST(compile_time_formatting_test, multibyte_fill) {
  332. EXPECT_EQ("жж42", test_format<8>(FMT_COMPILE("{:ж>4}"), 42));
  333. }
  334. #endif