std-test.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. // Formatting library for C++ - tests of formatters for standard library types
  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/std.h"
  8. #include <bitset>
  9. #include <stdexcept>
  10. #include <string>
  11. #include <vector>
  12. #include "fmt/os.h" // fmt::system_category
  13. #include "gtest-extra.h" // StartsWith
  14. #ifdef __cpp_lib_filesystem
  15. TEST(std_test, path) {
  16. using std::filesystem::path;
  17. EXPECT_EQ(fmt::format("{}", path("/usr/bin")), "/usr/bin");
  18. // see #4303
  19. const path p = "/usr/bin";
  20. EXPECT_EQ(fmt::format("{}", p), "/usr/bin");
  21. EXPECT_EQ(fmt::format("{:?}", path("/usr/bin")), "\"/usr/bin\"");
  22. EXPECT_EQ(fmt::format("{:8}", path("foo")), "foo ");
  23. EXPECT_EQ(fmt::format("{}", path("foo\"bar")), "foo\"bar");
  24. EXPECT_EQ(fmt::format("{:?}", path("foo\"bar")), "\"foo\\\"bar\"");
  25. EXPECT_EQ(fmt::format("{:g}", path("/usr/bin")), "/usr/bin");
  26. # ifdef _WIN32
  27. EXPECT_EQ(fmt::format("{}", path("C:\\foo")), "C:\\foo");
  28. EXPECT_EQ(fmt::format("{:g}", path("C:\\foo")), "C:/foo");
  29. EXPECT_EQ(fmt::format("{}", path(L"\x0428\x0447\x0443\x0447\x044B\x043D\x0448"
  30. L"\x0447\x044B\x043D\x0430")),
  31. "Шчучыншчына");
  32. EXPECT_EQ(fmt::format("{}", path(L"\xd800")), "�");
  33. EXPECT_EQ(fmt::format("{}", path(L"HEAD \xd800 TAIL")), "HEAD � TAIL");
  34. EXPECT_EQ(fmt::format("{}", path(L"HEAD \xD83D\xDE00 TAIL")),
  35. "HEAD \xF0\x9F\x98\x80 TAIL");
  36. EXPECT_EQ(fmt::format("{}", path(L"HEAD \xD83D\xD83D\xDE00 TAIL")),
  37. "HEAD �\xF0\x9F\x98\x80 TAIL");
  38. EXPECT_EQ(fmt::format("{:?}", path(L"\xd800")), "\"\\ud800\"");
  39. # endif
  40. }
  41. // Intentionally delayed include to test #4303
  42. #include "fmt/ranges.h"
  43. // Test ambiguity problem described in #2954.
  44. TEST(ranges_std_test, format_vector_path) {
  45. auto p = std::filesystem::path("foo/bar.txt");
  46. auto c = std::vector<std::string>{"abc", "def"};
  47. EXPECT_EQ(fmt::format("path={}, range={}", p, c),
  48. "path=foo/bar.txt, range=[\"abc\", \"def\"]");
  49. }
  50. // Test that path is not escaped twice in the debug mode.
  51. TEST(ranges_std_test, format_quote_path) {
  52. auto vec =
  53. std::vector<std::filesystem::path>{"path1/file1.txt", "path2/file2.txt"};
  54. EXPECT_EQ(fmt::format("{}", vec),
  55. "[\"path1/file1.txt\", \"path2/file2.txt\"]");
  56. # ifdef __cpp_lib_optional
  57. auto o = std::optional<std::filesystem::path>("path/file.txt");
  58. EXPECT_EQ(fmt::format("{}", o), "optional(\"path/file.txt\")");
  59. EXPECT_EQ(fmt::format("{:?}", o), "optional(\"path/file.txt\")");
  60. # endif
  61. }
  62. #endif
  63. TEST(std_test, thread_id) {
  64. EXPECT_FALSE(fmt::format("{}", std::this_thread::get_id()).empty());
  65. }
  66. TEST(std_test, complex) {
  67. using limits = std::numeric_limits<double>;
  68. EXPECT_EQ(fmt::format("{}", std::complex<double>(1, limits::quiet_NaN())),
  69. "(1+nan i)");
  70. EXPECT_EQ(fmt::format("{}", std::complex<double>(1, -limits::infinity())),
  71. "(1-inf i)");
  72. EXPECT_EQ(fmt::format("{}", std::complex<int>(1, 2)), "(1+2i)");
  73. EXPECT_EQ(fmt::format("{}", std::complex<double>(1, 2.2)), "(1+2.2i)");
  74. EXPECT_EQ(fmt::format("{}", std::complex<double>(1, -2.2)), "(1-2.2i)");
  75. EXPECT_EQ(fmt::format("{}", std::complex<double>(0, 2.2)), "2.2i");
  76. EXPECT_EQ(fmt::format("{}", std::complex<double>(0, -2.2)), "-2.2i");
  77. EXPECT_EQ(fmt::format("{:+}", std::complex<double>(0, 2.2)), "+2.2i");
  78. EXPECT_EQ(fmt::format("{:+}", std::complex<double>(0, -2.2)), "-2.2i");
  79. EXPECT_EQ(fmt::format("{:+}", std::complex<double>(1, -2.2)), "(+1-2.2i)");
  80. EXPECT_EQ(fmt::format("{:+}", std::complex<double>(1, 2.2)), "(+1+2.2i)");
  81. EXPECT_EQ(fmt::format("{: }", std::complex<double>(1, 2.2)), "( 1+2.2i)");
  82. EXPECT_EQ(fmt::format("{: }", std::complex<double>(1, -2.2)), "( 1-2.2i)");
  83. EXPECT_EQ(fmt::format("{:8}", std::complex<double>(1, 2)), "(1+2i) ");
  84. EXPECT_EQ(fmt::format("{:-<8}", std::complex<double>(1, 2)), "(1+2i)--");
  85. EXPECT_EQ(fmt::format("{:>20.2f}", std::complex<double>(1, 2.2)),
  86. " (1.00+2.20i)");
  87. EXPECT_EQ(fmt::format("{:<20.2f}", std::complex<double>(1, 2.2)),
  88. "(1.00+2.20i) ");
  89. EXPECT_EQ(fmt::format("{:<20.2f}", std::complex<double>(1, -2.2)),
  90. "(1.00-2.20i) ");
  91. EXPECT_EQ(fmt::format("{:<{}.{}f}", std::complex<double>(1, -2.2), 20, 2),
  92. "(1.00-2.20i) ");
  93. }
  94. #ifdef __cpp_lib_source_location
  95. TEST(std_test, source_location) {
  96. std::source_location loc = std::source_location::current();
  97. EXPECT_EQ(fmt::format("{}", loc),
  98. fmt::format("{}:{}:{}: {}", loc.file_name(), loc.line(),
  99. loc.column(), loc.function_name()));
  100. }
  101. #endif
  102. TEST(std_test, optional) {
  103. #ifdef __cpp_lib_optional
  104. EXPECT_EQ(fmt::format("{}", std::optional<int>{}), "none");
  105. EXPECT_EQ(fmt::format("{}", std::pair{1, "second"}), "(1, \"second\")");
  106. EXPECT_EQ(fmt::format("{}", std::vector{std::optional{1}, std::optional{2},
  107. std::optional{3}}),
  108. "[optional(1), optional(2), optional(3)]");
  109. EXPECT_EQ(
  110. fmt::format("{}", std::optional<std::optional<const char*>>{{"nested"}}),
  111. "optional(optional(\"nested\"))");
  112. EXPECT_EQ(
  113. fmt::format("{:<{}}", std::optional{std::string{"left aligned"}}, 30),
  114. "optional(\"left aligned\" )");
  115. EXPECT_EQ(
  116. fmt::format("{::d}", std::optional{std::vector{'h', 'e', 'l', 'l', 'o'}}),
  117. "optional([104, 101, 108, 108, 111])");
  118. EXPECT_EQ(fmt::format("{}", std::optional{std::string{"string"}}),
  119. "optional(\"string\")");
  120. EXPECT_EQ(fmt::format("{}", std::optional{'C'}), "optional(\'C\')");
  121. EXPECT_EQ(fmt::format("{:.{}f}", std::optional{3.14}, 1), "optional(3.1)");
  122. struct unformattable {};
  123. EXPECT_FALSE((fmt::is_formattable<unformattable>::value));
  124. EXPECT_FALSE((fmt::is_formattable<std::optional<unformattable>>::value));
  125. EXPECT_TRUE((fmt::is_formattable<std::optional<int>>::value));
  126. #endif
  127. }
  128. TEST(std_test, expected) {
  129. #ifdef __cpp_lib_expected
  130. EXPECT_EQ(fmt::format("{}", std::expected<void, int>{}), "expected()");
  131. EXPECT_EQ(fmt::format("{}", std::expected<int, int>{1}), "expected(1)");
  132. EXPECT_EQ(fmt::format("{}", std::expected<int, int>{std::unexpected(1)}),
  133. "unexpected(1)");
  134. EXPECT_EQ(fmt::format("{}", std::expected<std::string, int>{"test"}),
  135. "expected(\"test\")");
  136. EXPECT_EQ(fmt::format(
  137. "{}", std::expected<int, std::string>{std::unexpected("test")}),
  138. "unexpected(\"test\")");
  139. EXPECT_EQ(fmt::format("{}", std::expected<char, int>{'a'}), "expected('a')");
  140. EXPECT_EQ(fmt::format("{}", std::expected<int, char>{std::unexpected('a')}),
  141. "unexpected('a')");
  142. struct unformattable1 {};
  143. struct unformattable2 {};
  144. EXPECT_FALSE((fmt::is_formattable<unformattable1>::value));
  145. EXPECT_FALSE((fmt::is_formattable<unformattable2>::value));
  146. EXPECT_FALSE((fmt::is_formattable<
  147. std::expected<unformattable1, unformattable2>>::value));
  148. EXPECT_FALSE(
  149. (fmt::is_formattable<std::expected<unformattable1, int>>::value));
  150. EXPECT_FALSE(
  151. (fmt::is_formattable<std::expected<int, unformattable2>>::value));
  152. EXPECT_TRUE((fmt::is_formattable<std::expected<int, int>>::value));
  153. EXPECT_TRUE((fmt::is_formattable<std::expected<void, int>>::value));
  154. #endif
  155. }
  156. namespace my_nso {
  157. enum class my_number {
  158. one,
  159. two,
  160. };
  161. auto format_as(my_number number) -> fmt::string_view {
  162. return number == my_number::one ? "first" : "second";
  163. }
  164. class my_class {
  165. public:
  166. int av;
  167. private:
  168. friend auto format_as(const my_class& elm) -> std::string {
  169. return fmt::to_string(elm.av);
  170. }
  171. };
  172. } // namespace my_nso
  173. TEST(std_test, optional_format_as) {
  174. #ifdef __cpp_lib_optional
  175. EXPECT_EQ(fmt::format("{}", std::optional<my_nso::my_number>{}), "none");
  176. EXPECT_EQ(fmt::format("{}", std::optional{my_nso::my_number::one}),
  177. "optional(\"first\")");
  178. EXPECT_EQ(fmt::format("{}", std::optional<my_nso::my_class>{}), "none");
  179. EXPECT_EQ(fmt::format("{}", std::optional{my_nso::my_class{7}}),
  180. "optional(\"7\")");
  181. #endif
  182. }
  183. struct throws_on_move {
  184. throws_on_move() = default;
  185. [[noreturn]] throws_on_move(throws_on_move&&) {
  186. throw std::runtime_error("Thrown by throws_on_move");
  187. }
  188. throws_on_move(const throws_on_move&) = default;
  189. };
  190. namespace fmt {
  191. template <> struct formatter<throws_on_move> : formatter<string_view> {
  192. auto format(const throws_on_move&, format_context& ctx) const
  193. -> decltype(ctx.out()) {
  194. string_view str("<throws_on_move>");
  195. return formatter<string_view>::format(str, ctx);
  196. }
  197. };
  198. } // namespace fmt
  199. TEST(std_test, variant) {
  200. #ifdef __cpp_lib_variant
  201. EXPECT_EQ(fmt::format("{}", std::monostate{}), "monostate");
  202. using V0 = std::variant<int, float, std::string, char>;
  203. V0 v0(42);
  204. V0 v1(1.5f);
  205. V0 v2("hello");
  206. V0 v3('i');
  207. EXPECT_EQ(fmt::format("{}", v0), "variant(42)");
  208. EXPECT_EQ(fmt::format("{}", v1), "variant(1.5)");
  209. EXPECT_EQ(fmt::format("{}", v2), "variant(\"hello\")");
  210. EXPECT_EQ(fmt::format("{}", v3), "variant('i')");
  211. struct unformattable {};
  212. EXPECT_FALSE((fmt::is_formattable<unformattable>::value));
  213. EXPECT_FALSE((fmt::is_formattable<std::variant<unformattable>>::value));
  214. EXPECT_FALSE((fmt::is_formattable<std::variant<unformattable, int>>::value));
  215. EXPECT_FALSE((fmt::is_formattable<std::variant<int, unformattable>>::value));
  216. EXPECT_FALSE(
  217. (fmt::is_formattable<std::variant<unformattable, unformattable>>::value));
  218. EXPECT_TRUE((fmt::is_formattable<std::variant<int, float>>::value));
  219. using V1 = std::variant<std::monostate, std::string, std::string>;
  220. V1 v4{};
  221. V1 v5{std::in_place_index<1>, "yes, this is variant"};
  222. EXPECT_EQ(fmt::format("{}", v4), "variant(monostate)");
  223. EXPECT_EQ(fmt::format("{}", v5), "variant(\"yes, this is variant\")");
  224. volatile int i = 42; // Test compile error before GCC 11 described in #3068.
  225. EXPECT_EQ(fmt::format("{}", i), "42");
  226. std::variant<std::monostate, throws_on_move> v6;
  227. try {
  228. throws_on_move thrower;
  229. v6.emplace<throws_on_move>(std::move(thrower));
  230. } catch (const std::runtime_error&) {
  231. }
  232. // v6 is now valueless by exception
  233. EXPECT_EQ(fmt::format("{}", v6), "variant(valueless by exception)");
  234. #endif
  235. }
  236. TEST(std_test, error_code) {
  237. auto& generic = std::generic_category();
  238. EXPECT_EQ("generic:42",
  239. fmt::format(FMT_STRING("{0}"), std::error_code(42, generic)));
  240. EXPECT_EQ(" generic:42",
  241. fmt::format(FMT_STRING("{:>12}"), std::error_code(42, generic)));
  242. EXPECT_EQ("generic:42 ",
  243. fmt::format(FMT_STRING("{:12}"), std::error_code(42, generic)));
  244. EXPECT_EQ("system:42",
  245. fmt::format(FMT_STRING("{0}"),
  246. std::error_code(42, fmt::system_category())));
  247. EXPECT_EQ("system:-42",
  248. fmt::format(FMT_STRING("{0}"),
  249. std::error_code(-42, fmt::system_category())));
  250. }
  251. template <typename Catch> void exception_test() {
  252. try {
  253. throw std::runtime_error("Test Exception");
  254. } catch (const Catch& ex) {
  255. EXPECT_EQ("Test Exception", fmt::format("{}", ex));
  256. EXPECT_EQ("std::runtime_error: Test Exception", fmt::format("{:t}", ex));
  257. }
  258. }
  259. namespace my_ns1 {
  260. namespace my_ns2 {
  261. struct my_exception : public std::exception {
  262. private:
  263. std::string msg;
  264. public:
  265. my_exception(const std::string& s) : msg(s) {}
  266. const char* what() const noexcept override;
  267. };
  268. const char* my_exception::what() const noexcept { return msg.c_str(); }
  269. } // namespace my_ns2
  270. } // namespace my_ns1
  271. TEST(std_test, exception) {
  272. using testing::StartsWith;
  273. exception_test<std::exception>();
  274. exception_test<std::runtime_error>();
  275. try {
  276. using namespace my_ns1::my_ns2;
  277. throw my_exception("My Exception");
  278. } catch (const std::exception& ex) {
  279. EXPECT_EQ("my_ns1::my_ns2::my_exception: My Exception",
  280. fmt::format("{:t}", ex));
  281. EXPECT_EQ("My Exception", fmt::format("{:}", ex));
  282. }
  283. try {
  284. throw std::system_error(std::error_code(), "message");
  285. } catch (const std::system_error& ex) {
  286. EXPECT_THAT(fmt::format("{:t}", ex), StartsWith("std::system_error: "));
  287. }
  288. #ifdef __cpp_lib_filesystem
  289. // Tests that the inline namespace is stripped out, e.g.
  290. // std::filesystem::__cxx11::* -> std::filesystem::*.
  291. try {
  292. throw std::filesystem::filesystem_error("message", std::error_code());
  293. } catch (const std::filesystem::filesystem_error& ex) {
  294. EXPECT_THAT(fmt::format("{:t}", ex),
  295. StartsWith("std::filesystem::filesystem_error: "));
  296. }
  297. #endif
  298. }
  299. #if FMT_USE_RTTI
  300. TEST(std_test, type_info) {
  301. EXPECT_EQ(fmt::format("{}", typeid(std::runtime_error)),
  302. "std::runtime_error");
  303. }
  304. #endif
  305. TEST(std_test, format_bit_reference) {
  306. std::bitset<2> bs(1);
  307. EXPECT_EQ(fmt::format("{} {}", bs[0], bs[1]), "true false");
  308. std::vector<bool> v = {true, false};
  309. EXPECT_EQ(fmt::format("{} {}", v[0], v[1]), "true false");
  310. }
  311. TEST(std_test, format_const_bit_reference) {
  312. const std::bitset<2> bs(1);
  313. EXPECT_EQ(fmt::format("{} {}", bs[0], bs[1]), "true false");
  314. const std::vector<bool> v = {true, false};
  315. EXPECT_EQ(fmt::format("{} {}", v[0], v[1]), "true false");
  316. }
  317. TEST(std_test, format_bitset) {
  318. auto bs = std::bitset<6>(42);
  319. EXPECT_EQ(fmt::format("{}", bs), "101010");
  320. EXPECT_EQ(fmt::format("{:0>8}", bs), "00101010");
  321. EXPECT_EQ(fmt::format("{:-^12}", bs), "---101010---");
  322. }
  323. TEST(std_test, format_atomic) {
  324. std::atomic<bool> b(false);
  325. EXPECT_EQ(fmt::format("{}", b), "false");
  326. const std::atomic<bool> cb(true);
  327. EXPECT_EQ(fmt::format("{}", cb), "true");
  328. }
  329. #ifdef __cpp_lib_atomic_flag_test
  330. TEST(std_test, format_atomic_flag) {
  331. std::atomic_flag f;
  332. (void)f.test_and_set();
  333. EXPECT_EQ(fmt::format("{}", f), "true");
  334. f.clear();
  335. const std::atomic_flag& cf = f;
  336. EXPECT_EQ(fmt::format("{}", cf), "false");
  337. }
  338. #endif // __cpp_lib_atomic_flag_test
  339. TEST(std_test, format_unique_ptr) {
  340. std::unique_ptr<int> up(new int(1));
  341. EXPECT_EQ(fmt::format("{}", fmt::ptr(up.get())),
  342. fmt::format("{}", fmt::ptr(up)));
  343. struct custom_deleter {
  344. void operator()(int* p) const { delete p; }
  345. };
  346. std::unique_ptr<int, custom_deleter> upcd(new int(1));
  347. EXPECT_EQ(fmt::format("{}", fmt::ptr(upcd.get())),
  348. fmt::format("{}", fmt::ptr(upcd)));
  349. }
  350. TEST(std_test, format_shared_ptr) {
  351. std::shared_ptr<int> sp(new int(1));
  352. EXPECT_EQ(fmt::format("{}", fmt::ptr(sp.get())),
  353. fmt::format("{}", fmt::ptr(sp)));
  354. }
  355. TEST(std_test, format_reference_wrapper) {
  356. int num = 35;
  357. EXPECT_EQ("35", fmt::to_string(std::cref(num)));
  358. }