Include fmt 11.0.2
[openal-soft.git] / fmt-11.0.2 / test / std-test.cc
blob1327fe8279fd29a6156033e4f6ba13f843816fc7
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.
8 #include "fmt/std.h"
10 #include <bitset>
11 #include <stdexcept>
12 #include <string>
13 #include <vector>
15 #include "fmt/os.h" // fmt::system_category
16 #include "fmt/ranges.h"
17 #include "gtest-extra.h" // StartsWith
19 #ifdef __cpp_lib_filesystem
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 ");
26 EXPECT_EQ(fmt::format("{}", path("foo\"bar")), "foo\"bar");
27 EXPECT_EQ(fmt::format("{:?}", path("foo\"bar")), "\"foo\\\"bar\"");
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");
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"\xd800")), "\"\\ud800\"");
39 # endif
42 // Test ambiguity problem described in #2954.
43 TEST(ranges_std_test, format_vector_path) {
44 auto p = std::filesystem::path("foo/bar.txt");
45 auto c = std::vector<std::string>{"abc", "def"};
46 EXPECT_EQ(fmt::format("path={}, range={}", p, c),
47 "path=foo/bar.txt, range=[\"abc\", \"def\"]");
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
62 #endif
64 TEST(std_test, thread_id) {
65 EXPECT_FALSE(fmt::format("{}", std::this_thread::get_id()).empty());
68 TEST(std_test, complex) {
69 using limits = std::numeric_limits<double>;
70 EXPECT_EQ(fmt::format("{}", std::complex<double>(1, limits::quiet_NaN())),
71 "(1+nan i)");
72 EXPECT_EQ(fmt::format("{}", std::complex<double>(1, -limits::infinity())),
73 "(1-inf i)");
75 EXPECT_EQ(fmt::format("{}", std::complex<int>(1, 2)), "(1+2i)");
77 EXPECT_EQ(fmt::format("{}", std::complex<double>(1, 2.2)), "(1+2.2i)");
78 EXPECT_EQ(fmt::format("{}", std::complex<double>(1, -2.2)), "(1-2.2i)");
79 EXPECT_EQ(fmt::format("{}", std::complex<double>(0, 2.2)), "2.2i");
80 EXPECT_EQ(fmt::format("{}", std::complex<double>(0, -2.2)), "-2.2i");
82 EXPECT_EQ(fmt::format("{:+}", std::complex<double>(0, 2.2)), "+2.2i");
83 EXPECT_EQ(fmt::format("{:+}", std::complex<double>(0, -2.2)), "-2.2i");
84 EXPECT_EQ(fmt::format("{:+}", std::complex<double>(1, -2.2)), "(+1-2.2i)");
85 EXPECT_EQ(fmt::format("{:+}", std::complex<double>(1, 2.2)), "(+1+2.2i)");
86 EXPECT_EQ(fmt::format("{: }", std::complex<double>(1, 2.2)), "( 1+2.2i)");
87 EXPECT_EQ(fmt::format("{: }", std::complex<double>(1, -2.2)), "( 1-2.2i)");
89 EXPECT_EQ(fmt::format("{:>20.2f}", std::complex<double>(1, 2.2)),
90 " (1.00+2.20i)");
91 EXPECT_EQ(fmt::format("{:<20.2f}", std::complex<double>(1, 2.2)),
92 "(1.00+2.20i) ");
93 EXPECT_EQ(fmt::format("{:<20.2f}", std::complex<double>(1, -2.2)),
94 "(1.00-2.20i) ");
95 EXPECT_EQ(fmt::format("{:<{}.{}f}", std::complex<double>(1, -2.2), 20, 2),
96 "(1.00-2.20i) ");
99 #ifdef __cpp_lib_source_location
100 TEST(std_test, source_location) {
101 std::source_location loc = std::source_location::current();
102 EXPECT_EQ(fmt::format("{}", loc),
103 fmt::format("{}:{}:{}: {}", loc.file_name(), loc.line(),
104 loc.column(), loc.function_name()));
106 #endif
108 TEST(std_test, optional) {
109 #ifdef __cpp_lib_optional
110 EXPECT_EQ(fmt::format("{}", std::optional<int>{}), "none");
111 EXPECT_EQ(fmt::format("{}", std::pair{1, "second"}), "(1, \"second\")");
112 EXPECT_EQ(fmt::format("{}", std::vector{std::optional{1}, std::optional{2},
113 std::optional{3}}),
114 "[optional(1), optional(2), optional(3)]");
115 EXPECT_EQ(
116 fmt::format("{}", std::optional<std::optional<const char*>>{{"nested"}}),
117 "optional(optional(\"nested\"))");
118 EXPECT_EQ(
119 fmt::format("{:<{}}", std::optional{std::string{"left aligned"}}, 30),
120 "optional(\"left aligned\" )");
121 EXPECT_EQ(
122 fmt::format("{::d}", std::optional{std::vector{'h', 'e', 'l', 'l', 'o'}}),
123 "optional([104, 101, 108, 108, 111])");
124 EXPECT_EQ(fmt::format("{}", std::optional{std::string{"string"}}),
125 "optional(\"string\")");
126 EXPECT_EQ(fmt::format("{}", std::optional{'C'}), "optional(\'C\')");
127 EXPECT_EQ(fmt::format("{:.{}f}", std::optional{3.14}, 1), "optional(3.1)");
129 struct unformattable {};
130 EXPECT_FALSE((fmt::is_formattable<unformattable>::value));
131 EXPECT_FALSE((fmt::is_formattable<std::optional<unformattable>>::value));
132 EXPECT_TRUE((fmt::is_formattable<std::optional<int>>::value));
133 #endif
136 TEST(std_test, expected) {
137 #ifdef __cpp_lib_expected
138 EXPECT_EQ(fmt::format("{}", std::expected<int, int>{1}), "expected(1)");
139 EXPECT_EQ(fmt::format("{}", std::expected<int, int>{std::unexpected(1)}),
140 "unexpected(1)");
141 EXPECT_EQ(fmt::format("{}", std::expected<std::string, int>{"test"}),
142 "expected(\"test\")");
143 EXPECT_EQ(fmt::format(
144 "{}", std::expected<int, std::string>{std::unexpected("test")}),
145 "unexpected(\"test\")");
146 EXPECT_EQ(fmt::format("{}", std::expected<char, int>{'a'}), "expected('a')");
147 EXPECT_EQ(fmt::format("{}", std::expected<int, char>{std::unexpected('a')}),
148 "unexpected('a')");
150 struct unformattable1 {};
151 struct unformattable2 {};
152 EXPECT_FALSE((fmt::is_formattable<unformattable1>::value));
153 EXPECT_FALSE((fmt::is_formattable<unformattable2>::value));
154 EXPECT_FALSE((fmt::is_formattable<
155 std::expected<unformattable1, unformattable2>>::value));
156 EXPECT_FALSE(
157 (fmt::is_formattable<std::expected<unformattable1, int>>::value));
158 EXPECT_FALSE(
159 (fmt::is_formattable<std::expected<int, unformattable2>>::value));
160 EXPECT_TRUE((fmt::is_formattable<std::expected<int, int>>::value));
161 #endif
164 namespace my_nso {
165 enum class my_number {
166 one,
167 two,
169 auto format_as(my_number number) -> fmt::string_view {
170 return number == my_number::one ? "first" : "second";
173 class my_class {
174 public:
175 int av;
177 private:
178 friend auto format_as(const my_class& elm) -> std::string {
179 return fmt::to_string(elm.av);
182 } // namespace my_nso
183 TEST(std_test, optional_format_as) {
184 #ifdef __cpp_lib_optional
185 EXPECT_EQ(fmt::format("{}", std::optional<my_nso::my_number>{}), "none");
186 EXPECT_EQ(fmt::format("{}", std::optional{my_nso::my_number::one}),
187 "optional(\"first\")");
188 EXPECT_EQ(fmt::format("{}", std::optional<my_nso::my_class>{}), "none");
189 EXPECT_EQ(fmt::format("{}", std::optional{my_nso::my_class{7}}),
190 "optional(\"7\")");
191 #endif
194 struct throws_on_move {
195 throws_on_move() = default;
197 [[noreturn]] throws_on_move(throws_on_move&&) {
198 throw std::runtime_error("Thrown by throws_on_move");
201 throws_on_move(const throws_on_move&) = default;
204 namespace fmt {
205 template <> struct formatter<throws_on_move> : formatter<string_view> {
206 auto format(const throws_on_move&, format_context& ctx) const
207 -> decltype(ctx.out()) {
208 string_view str("<throws_on_move>");
209 return formatter<string_view>::format(str, ctx);
212 } // namespace fmt
214 TEST(std_test, variant) {
215 #ifdef __cpp_lib_variant
216 EXPECT_EQ(fmt::format("{}", std::monostate{}), "monostate");
217 using V0 = std::variant<int, float, std::string, char>;
218 V0 v0(42);
219 V0 v1(1.5f);
220 V0 v2("hello");
221 V0 v3('i');
222 EXPECT_EQ(fmt::format("{}", v0), "variant(42)");
223 EXPECT_EQ(fmt::format("{}", v1), "variant(1.5)");
224 EXPECT_EQ(fmt::format("{}", v2), "variant(\"hello\")");
225 EXPECT_EQ(fmt::format("{}", v3), "variant('i')");
227 struct unformattable {};
228 EXPECT_FALSE((fmt::is_formattable<unformattable>::value));
229 EXPECT_FALSE((fmt::is_formattable<std::variant<unformattable>>::value));
230 EXPECT_FALSE((fmt::is_formattable<std::variant<unformattable, int>>::value));
231 EXPECT_FALSE((fmt::is_formattable<std::variant<int, unformattable>>::value));
232 EXPECT_FALSE(
233 (fmt::is_formattable<std::variant<unformattable, unformattable>>::value));
234 EXPECT_TRUE((fmt::is_formattable<std::variant<int, float>>::value));
236 using V1 = std::variant<std::monostate, std::string, std::string>;
237 V1 v4{};
238 V1 v5{std::in_place_index<1>, "yes, this is variant"};
240 EXPECT_EQ(fmt::format("{}", v4), "variant(monostate)");
241 EXPECT_EQ(fmt::format("{}", v5), "variant(\"yes, this is variant\")");
243 volatile int i = 42; // Test compile error before GCC 11 described in #3068.
244 EXPECT_EQ(fmt::format("{}", i), "42");
246 std::variant<std::monostate, throws_on_move> v6;
248 try {
249 throws_on_move thrower;
250 v6.emplace<throws_on_move>(std::move(thrower));
251 } catch (const std::runtime_error&) {
253 // v6 is now valueless by exception
255 EXPECT_EQ(fmt::format("{}", v6), "variant(valueless by exception)");
257 #endif
260 TEST(std_test, error_code) {
261 EXPECT_EQ("generic:42",
262 fmt::format(FMT_STRING("{0}"),
263 std::error_code(42, std::generic_category())));
264 EXPECT_EQ("system:42",
265 fmt::format(FMT_STRING("{0}"),
266 std::error_code(42, fmt::system_category())));
267 EXPECT_EQ("system:-42",
268 fmt::format(FMT_STRING("{0}"),
269 std::error_code(-42, fmt::system_category())));
272 template <typename Catch> void exception_test() {
273 try {
274 throw std::runtime_error("Test Exception");
275 } catch (const Catch& ex) {
276 EXPECT_EQ("Test Exception", fmt::format("{}", ex));
277 EXPECT_EQ("std::runtime_error: Test Exception", fmt::format("{:t}", ex));
281 namespace my_ns1 {
282 namespace my_ns2 {
283 struct my_exception : public std::exception {
284 private:
285 std::string msg;
287 public:
288 my_exception(const std::string& s) : msg(s) {}
289 const char* what() const noexcept override;
291 const char* my_exception::what() const noexcept { return msg.c_str(); }
292 } // namespace my_ns2
293 } // namespace my_ns1
295 TEST(std_test, exception) {
296 using testing::StartsWith;
297 exception_test<std::exception>();
298 exception_test<std::runtime_error>();
300 try {
301 using namespace my_ns1::my_ns2;
302 throw my_exception("My Exception");
303 } catch (const std::exception& ex) {
304 EXPECT_EQ("my_ns1::my_ns2::my_exception: My Exception",
305 fmt::format("{:t}", ex));
306 EXPECT_EQ("My Exception", fmt::format("{:}", ex));
309 try {
310 throw std::system_error(std::error_code(), "message");
311 } catch (const std::system_error& ex) {
312 EXPECT_THAT(fmt::format("{:t}", ex), StartsWith("std::system_error: "));
315 #ifdef __cpp_lib_filesystem
316 // Tests that the inline namespace is stripped out, e.g.
317 // std::filesystem::__cxx11::* -> std::filesystem::*.
318 try {
319 throw std::filesystem::filesystem_error("message", std::error_code());
320 } catch (const std::filesystem::filesystem_error& ex) {
321 EXPECT_THAT(fmt::format("{:t}", ex),
322 StartsWith("std::filesystem::filesystem_error: "));
324 #endif
327 #if FMT_USE_RTTI
328 TEST(std_test, type_info) {
329 EXPECT_EQ(fmt::format("{}", typeid(std::runtime_error)),
330 "std::runtime_error");
332 #endif
334 TEST(std_test, format_bit_reference) {
335 std::bitset<2> bs(1);
336 EXPECT_EQ(fmt::format("{} {}", bs[0], bs[1]), "true false");
337 std::vector<bool> v = {true, false};
338 EXPECT_EQ(fmt::format("{} {}", v[0], v[1]), "true false");
341 TEST(std_test, format_const_bit_reference) {
342 const std::bitset<2> bs(1);
343 EXPECT_EQ(fmt::format("{} {}", bs[0], bs[1]), "true false");
344 const std::vector<bool> v = {true, false};
345 EXPECT_EQ(fmt::format("{} {}", v[0], v[1]), "true false");
348 TEST(std_test, format_bitset) {
349 auto bs = std::bitset<6>(42);
350 EXPECT_EQ(fmt::format("{}", bs), "101010");
351 EXPECT_EQ(fmt::format("{:0>8}", bs), "00101010");
352 EXPECT_EQ(fmt::format("{:-^12}", bs), "---101010---");
355 TEST(std_test, format_atomic) {
356 std::atomic<bool> b(false);
357 EXPECT_EQ(fmt::format("{}", b), "false");
359 const std::atomic<bool> cb(true);
360 EXPECT_EQ(fmt::format("{}", cb), "true");
363 #ifdef __cpp_lib_atomic_flag_test
364 TEST(std_test, format_atomic_flag) {
365 std::atomic_flag f = ATOMIC_FLAG_INIT;
366 (void)f.test_and_set();
367 EXPECT_EQ(fmt::format("{}", f), "true");
369 const std::atomic_flag cf = ATOMIC_FLAG_INIT;
370 EXPECT_EQ(fmt::format("{}", cf), "false");
372 #endif // __cpp_lib_atomic_flag_test
374 TEST(std_test, format_unique_ptr) {
375 std::unique_ptr<int> up(new int(1));
376 EXPECT_EQ(fmt::format("{}", fmt::ptr(up.get())),
377 fmt::format("{}", fmt::ptr(up)));
378 struct custom_deleter {
379 void operator()(int* p) const { delete p; }
381 std::unique_ptr<int, custom_deleter> upcd(new int(1));
382 EXPECT_EQ(fmt::format("{}", fmt::ptr(upcd.get())),
383 fmt::format("{}", fmt::ptr(upcd)));
386 TEST(std_test, format_shared_ptr) {
387 std::shared_ptr<int> sp(new int(1));
388 EXPECT_EQ(fmt::format("{}", fmt::ptr(sp.get())),
389 fmt::format("{}", fmt::ptr(sp)));