std-test.cc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 "fmt/ranges.h"
  14. #include "gtest-extra.h" // StartsWith
  15. #ifdef __cpp_lib_filesystem
  16. TEST(std_test, path) {
  17. using std::filesystem::path;
  18. EXPECT_EQ(fmt::format("{}", path("/usr/bin")), "/usr/bin");
  19. EXPECT_EQ(fmt::format("{:?}", path("/usr/bin")), "\"/usr/bin\"");
  20. EXPECT_EQ(fmt::format("{:8}", path("foo")), "foo ");
  21. EXPECT_EQ(fmt::format("{}", path("foo\"bar")), "foo\"bar");
  22. EXPECT_EQ(fmt::format("{:?}", path("foo\"bar")), "\"foo\\\"bar\"");
  23. # ifdef _WIN32
  24. EXPECT_EQ(fmt::format("{}", path(
  25. L"\x0428\x0447\x0443\x0447\x044B\x043D\x0448"
  26. L"\x0447\x044B\x043D\x0430")),
  27. "Шчучыншчына");
  28. EXPECT_EQ(fmt::format("{}", path(L"\xd800")), "�");
  29. EXPECT_EQ(fmt::format("{:?}", path(L"\xd800")), "\"\\ud800\"");
  30. # endif
  31. }
  32. // Test ambiguity problem described in #2954.
  33. TEST(ranges_std_test, format_vector_path) {
  34. auto p = std::filesystem::path("foo/bar.txt");
  35. auto c = std::vector<std::string>{"abc", "def"};
  36. EXPECT_EQ(fmt::format("path={}, range={}", p, c),
  37. "path=foo/bar.txt, range=[\"abc\", \"def\"]");
  38. }
  39. // Test that path is not escaped twice in the debug mode.
  40. TEST(ranges_std_test, format_quote_path) {
  41. auto vec =
  42. std::vector<std::filesystem::path>{"path1/file1.txt", "path2/file2.txt"};
  43. EXPECT_EQ(fmt::format("{}", vec),
  44. "[\"path1/file1.txt\", \"path2/file2.txt\"]");
  45. # ifdef __cpp_lib_optional
  46. auto o = std::optional<std::filesystem::path>("path/file.txt");
  47. EXPECT_EQ(fmt::format("{}", o), "optional(\"path/file.txt\")");
  48. EXPECT_EQ(fmt::format("{:?}", o), "optional(\"path/file.txt\")");
  49. # endif
  50. }
  51. #endif
  52. TEST(std_test, thread_id) {
  53. EXPECT_FALSE(fmt::format("{}", std::this_thread::get_id()).empty());
  54. }
  55. TEST(std_test, optional) {
  56. #ifdef __cpp_lib_optional
  57. EXPECT_EQ(fmt::format("{}", std::optional<int>{}), "none");
  58. EXPECT_EQ(fmt::format("{}", std::pair{1, "second"}), "(1, \"second\")");
  59. EXPECT_EQ(fmt::format("{}", std::vector{std::optional{1}, std::optional{2},
  60. std::optional{3}}),
  61. "[optional(1), optional(2), optional(3)]");
  62. EXPECT_EQ(
  63. fmt::format("{}", std::optional<std::optional<const char*>>{{"nested"}}),
  64. "optional(optional(\"nested\"))");
  65. EXPECT_EQ(
  66. fmt::format("{:<{}}", std::optional{std::string{"left aligned"}}, 30),
  67. "optional(\"left aligned\" )");
  68. EXPECT_EQ(
  69. fmt::format("{::d}", std::optional{std::vector{'h', 'e', 'l', 'l', 'o'}}),
  70. "optional([104, 101, 108, 108, 111])");
  71. EXPECT_EQ(fmt::format("{}", std::optional{std::string{"string"}}),
  72. "optional(\"string\")");
  73. EXPECT_EQ(fmt::format("{}", std::optional{'C'}), "optional(\'C\')");
  74. EXPECT_EQ(fmt::format("{:.{}f}", std::optional{3.14}, 1), "optional(3.1)");
  75. struct unformattable {};
  76. EXPECT_FALSE((fmt::is_formattable<unformattable>::value));
  77. EXPECT_FALSE((fmt::is_formattable<std::optional<unformattable>>::value));
  78. EXPECT_TRUE((fmt::is_formattable<std::optional<int>>::value));
  79. #endif
  80. }
  81. struct throws_on_move {
  82. throws_on_move() = default;
  83. [[noreturn]] throws_on_move(throws_on_move&&) {
  84. throw std::runtime_error("Thrown by throws_on_move");
  85. }
  86. throws_on_move(const throws_on_move&) = default;
  87. };
  88. namespace fmt {
  89. template <> struct formatter<throws_on_move> : formatter<string_view> {
  90. auto format(const throws_on_move&, format_context& ctx) const
  91. -> decltype(ctx.out()) {
  92. string_view str("<throws_on_move>");
  93. return formatter<string_view>::format(str, ctx);
  94. }
  95. };
  96. } // namespace fmt
  97. TEST(std_test, variant) {
  98. #ifdef __cpp_lib_variant
  99. EXPECT_EQ(fmt::format("{}", std::monostate{}), "monostate");
  100. using V0 = std::variant<int, float, std::string, char>;
  101. V0 v0(42);
  102. V0 v1(1.5f);
  103. V0 v2("hello");
  104. V0 v3('i');
  105. EXPECT_EQ(fmt::format("{}", v0), "variant(42)");
  106. EXPECT_EQ(fmt::format("{}", v1), "variant(1.5)");
  107. EXPECT_EQ(fmt::format("{}", v2), "variant(\"hello\")");
  108. EXPECT_EQ(fmt::format("{}", v3), "variant('i')");
  109. struct unformattable {};
  110. EXPECT_FALSE((fmt::is_formattable<unformattable>::value));
  111. EXPECT_FALSE((fmt::is_formattable<std::variant<unformattable>>::value));
  112. EXPECT_FALSE((fmt::is_formattable<std::variant<unformattable, int>>::value));
  113. EXPECT_FALSE((fmt::is_formattable<std::variant<int, unformattable>>::value));
  114. EXPECT_FALSE(
  115. (fmt::is_formattable<std::variant<unformattable, unformattable>>::value));
  116. EXPECT_TRUE((fmt::is_formattable<std::variant<int, float>>::value));
  117. using V1 = std::variant<std::monostate, std::string, std::string>;
  118. V1 v4{};
  119. V1 v5{std::in_place_index<1>, "yes, this is variant"};
  120. EXPECT_EQ(fmt::format("{}", v4), "variant(monostate)");
  121. EXPECT_EQ(fmt::format("{}", v5), "variant(\"yes, this is variant\")");
  122. volatile int i = 42; // Test compile error before GCC 11 described in #3068.
  123. EXPECT_EQ(fmt::format("{}", i), "42");
  124. std::variant<std::monostate, throws_on_move> v6;
  125. try {
  126. throws_on_move thrower;
  127. v6.emplace<throws_on_move>(std::move(thrower));
  128. } catch (const std::runtime_error&) {
  129. }
  130. // v6 is now valueless by exception
  131. EXPECT_EQ(fmt::format("{}", v6), "variant(valueless by exception)");
  132. #endif
  133. }
  134. TEST(std_test, error_code) {
  135. EXPECT_EQ("generic:42",
  136. fmt::format(FMT_STRING("{0}"),
  137. std::error_code(42, std::generic_category())));
  138. EXPECT_EQ("system:42",
  139. fmt::format(FMT_STRING("{0}"),
  140. std::error_code(42, fmt::system_category())));
  141. EXPECT_EQ("system:-42",
  142. fmt::format(FMT_STRING("{0}"),
  143. std::error_code(-42, fmt::system_category())));
  144. }
  145. template <typename Catch> void exception_test() {
  146. try {
  147. throw std::runtime_error("Test Exception");
  148. } catch (const Catch& ex) {
  149. EXPECT_EQ("Test Exception", fmt::format("{}", ex));
  150. EXPECT_EQ("std::runtime_error: Test Exception", fmt::format("{:t}", ex));
  151. }
  152. }
  153. namespace my_ns1 {
  154. namespace my_ns2 {
  155. struct my_exception : public std::exception {
  156. private:
  157. std::string msg;
  158. public:
  159. my_exception(const std::string& s) : msg(s) {}
  160. const char* what() const noexcept override;
  161. };
  162. const char* my_exception::what() const noexcept { return msg.c_str(); }
  163. } // namespace my_ns2
  164. } // namespace my_ns1
  165. TEST(std_test, exception) {
  166. using testing::StartsWith;
  167. exception_test<std::exception>();
  168. exception_test<std::runtime_error>();
  169. try {
  170. using namespace my_ns1::my_ns2;
  171. throw my_exception("My Exception");
  172. } catch (const std::exception& ex) {
  173. EXPECT_EQ("my_ns1::my_ns2::my_exception: My Exception",
  174. fmt::format("{:t}", ex));
  175. EXPECT_EQ("My Exception", fmt::format("{:}", ex));
  176. }
  177. try {
  178. throw std::system_error(std::error_code(), "message");
  179. } catch (const std::system_error& ex) {
  180. EXPECT_THAT(fmt::format("{:t}", ex), StartsWith("std::system_error: "));
  181. }
  182. #ifdef __cpp_lib_filesystem
  183. // Tests that the inline namespace is stripped out, e.g.
  184. // std::filesystem::__cxx11::* -> std::filesystem::*.
  185. try {
  186. throw std::filesystem::filesystem_error("message", std::error_code());
  187. } catch (const std::filesystem::filesystem_error& ex) {
  188. EXPECT_THAT(fmt::format("{:t}", ex),
  189. StartsWith("std::filesystem::filesystem_error: "));
  190. }
  191. #endif
  192. }
  193. TEST(std_test, format_bit_reference) {
  194. std::bitset<2> bs(1);
  195. EXPECT_EQ(fmt::format("{} {}", bs[0], bs[1]), "true false");
  196. std::vector<bool> v = {true, false};
  197. EXPECT_EQ(fmt::format("{} {}", v[0], v[1]), "true false");
  198. }
  199. TEST(std_test, format_const_bit_reference) {
  200. const std::bitset<2> bs(1);
  201. EXPECT_EQ(fmt::format("{} {}", bs[0], bs[1]), "true false");
  202. const std::vector<bool> v = {true, false};
  203. EXPECT_EQ(fmt::format("{} {}", v[0], v[1]), "true false");
  204. }
  205. TEST(std_test, format_atomic) {
  206. std::atomic<bool> b(false);
  207. EXPECT_EQ(fmt::format("{}", b), "false");
  208. const std::atomic<bool> cb(true);
  209. EXPECT_EQ(fmt::format("{}", cb), "true");
  210. }
  211. #ifdef __cpp_lib_atomic_flag_test
  212. TEST(std_test, format_atomic_flag) {
  213. std::atomic_flag f = ATOMIC_FLAG_INIT;
  214. (void) f.test_and_set();
  215. EXPECT_EQ(fmt::format("{}", f), "true");
  216. const std::atomic_flag cf = ATOMIC_FLAG_INIT;
  217. EXPECT_EQ(fmt::format("{}", cf), "false");
  218. }
  219. #endif // __cpp_lib_atomic_flag_test