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