Include fmt 11.0.2
[openal-soft.git] / fmt-11.0.2 / test / xchar-test.cc
blob312e632bdccd661e26be3b619de58f21d3be98dc
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.
8 #include "fmt/xchar.h"
10 #include <algorithm>
11 #include <complex>
12 #include <cwchar>
13 #include <vector>
15 #include "fmt/chrono.h"
16 #include "fmt/color.h"
17 #include "fmt/ostream.h"
18 #include "fmt/ranges.h"
19 #include "fmt/std.h"
20 #include "gtest-extra.h" // Contains
21 #include "util.h" // get_locale
23 using fmt::detail::max_value;
24 using testing::Contains;
26 #if defined(__MINGW32__) && !defined(_UCRT)
27 // Only C89 conversion specifiers when using MSVCRT instead of UCRT
28 # define FMT_HAS_C99_STRFTIME 0
29 #else
30 # define FMT_HAS_C99_STRFTIME 1
31 #endif
33 struct non_string {};
35 template <typename T> class has_to_string_view_test : public testing::Test {};
37 using string_char_types = testing::Types<char, wchar_t, char16_t, char32_t>;
38 TYPED_TEST_SUITE(has_to_string_view_test, string_char_types);
40 template <typename Char>
41 struct derived_from_string_view : fmt::basic_string_view<Char> {};
43 TYPED_TEST(has_to_string_view_test, has_to_string_view) {
44 EXPECT_TRUE(fmt::detail::has_to_string_view<TypeParam*>::value);
45 EXPECT_TRUE(fmt::detail::has_to_string_view<const TypeParam*>::value);
46 EXPECT_TRUE(fmt::detail::has_to_string_view<TypeParam[2]>::value);
47 EXPECT_TRUE(fmt::detail::has_to_string_view<const TypeParam[2]>::value);
48 EXPECT_TRUE(
49 fmt::detail::has_to_string_view<std::basic_string<TypeParam>>::value);
50 EXPECT_TRUE(fmt::detail::has_to_string_view<
51 fmt::basic_string_view<TypeParam>>::value);
52 EXPECT_TRUE(fmt::detail::has_to_string_view<
53 derived_from_string_view<TypeParam>>::value);
54 using fmt_string_view = fmt::detail::std_string_view<TypeParam>;
55 EXPECT_TRUE(std::is_empty<fmt_string_view>::value !=
56 fmt::detail::has_to_string_view<fmt_string_view>::value);
57 EXPECT_FALSE(fmt::detail::has_to_string_view<non_string>::value);
60 // std::is_constructible is broken in MSVC until version 2015.
61 #if !FMT_MSC_VERSION || FMT_MSC_VERSION >= 1900
62 struct explicitly_convertible_to_wstring_view {
63 explicit operator fmt::wstring_view() const { return L"foo"; }
66 TEST(xchar_test, format_explicitly_convertible_to_wstring_view) {
67 // Types explicitly convertible to wstring_view are not formattable by
68 // default because it may introduce ODR violations.
69 static_assert(
70 !fmt::is_formattable<explicitly_convertible_to_wstring_view>::value, "");
72 #endif
74 TEST(xchar_test, format) {
75 EXPECT_EQ(L"42", fmt::format(L"{}", 42));
76 EXPECT_EQ(L"4.2", fmt::format(L"{}", 4.2));
77 EXPECT_EQ(L"abc", fmt::format(L"{}", L"abc"));
78 EXPECT_EQ(L"z", fmt::format(L"{}", L'z'));
79 EXPECT_THROW(fmt::format(fmt::runtime(L"{:*\x343E}"), 42), fmt::format_error);
80 EXPECT_EQ(L"true", fmt::format(L"{}", true));
81 EXPECT_EQ(L"a", fmt::format(L"{0}", 'a'));
82 EXPECT_EQ(L"a", fmt::format(L"{0}", L'a'));
83 EXPECT_EQ(L"Cyrillic letter \x42e",
84 fmt::format(L"Cyrillic letter {}", L'\x42e'));
85 EXPECT_EQ(L"abc1", fmt::format(L"{}c{}", L"ab", 1));
88 TEST(xchar_test, is_formattable) {
89 static_assert(!fmt::is_formattable<const wchar_t*>::value, "");
92 TEST(xchar_test, compile_time_string) {
93 EXPECT_EQ(fmt::format(fmt::wformat_string<int>(L"{}"), 42), L"42");
94 #if defined(FMT_USE_STRING_VIEW) && FMT_CPLUSPLUS >= 201703L
95 EXPECT_EQ(fmt::format(FMT_STRING(std::wstring_view(L"{}")), 42), L"42");
96 #endif
99 #if FMT_CPLUSPLUS > 201103L
100 struct custom_char {
101 int value;
102 custom_char() = default;
104 template <typename T>
105 constexpr custom_char(T val) : value(static_cast<int>(val)) {}
107 constexpr operator char() const {
108 return value <= 0xff ? static_cast<char>(value) : '\0';
110 constexpr bool operator<(custom_char c) const { return value < c.value; }
113 namespace std {
115 template <> struct char_traits<custom_char> {
116 using char_type = custom_char;
117 using int_type = int;
118 using off_type = streamoff;
119 using pos_type = streampos;
120 using state_type = mbstate_t;
122 static constexpr void assign(char_type& r, const char_type& a) { r = a; }
123 static constexpr bool eq(char_type a, char_type b) { return a == b; }
124 static constexpr bool lt(char_type a, char_type b) { return a < b; }
125 static FMT_CONSTEXPR int compare(const char_type* s1, const char_type* s2,
126 size_t count) {
127 for (; count; count--, s1++, s2++) {
128 if (lt(*s1, *s2)) return -1;
129 if (lt(*s2, *s1)) return 1;
131 return 0;
133 static FMT_CONSTEXPR size_t length(const char_type* s) {
134 size_t count = 0;
135 while (!eq(*s++, custom_char(0))) count++;
136 return count;
138 static const char_type* find(const char_type*, size_t, const char_type&);
139 static FMT_CONSTEXPR char_type* move(char_type* dest, const char_type* src,
140 size_t count) {
141 if (count == 0) return dest;
142 char_type* ret = dest;
143 if (src < dest) {
144 dest += count;
145 src += count;
146 for (; count; count--) assign(*--dest, *--src);
147 } else if (src > dest)
148 copy(dest, src, count);
149 return ret;
151 static FMT_CONSTEXPR char_type* copy(char_type* dest, const char_type* src,
152 size_t count) {
153 char_type* ret = dest;
154 for (; count; count--) assign(*dest++, *src++);
155 return ret;
157 static FMT_CONSTEXPR char_type* assign(char_type* dest, std::size_t count,
158 char_type a) {
159 char_type* ret = dest;
160 for (; count; count--) assign(*dest++, a);
161 return ret;
163 static int_type not_eof(int_type);
164 static char_type to_char_type(int_type);
165 static int_type to_int_type(char_type);
166 static bool eq_int_type(int_type, int_type);
167 static int_type eof();
170 } // namespace std
172 auto to_ascii(custom_char c) -> char { return c; }
174 FMT_BEGIN_NAMESPACE
175 template <> struct is_char<custom_char> : std::true_type {};
176 FMT_END_NAMESPACE
178 TEST(xchar_test, format_custom_char) {
179 const custom_char format[] = {'{', '}', 0};
180 auto result = fmt::format(format, custom_char('x'));
181 EXPECT_EQ(result.size(), 1);
182 EXPECT_EQ(result[0], custom_char('x'));
184 #endif
186 TEST(xchar_test, format_to) {
187 auto buf = std::vector<wchar_t>();
188 fmt::format_to(std::back_inserter(buf), L"{}{}", 42, L'\0');
189 EXPECT_STREQ(buf.data(), L"42");
192 TEST(xchar_test, compile_time_string_format_to) {
193 std::wstring ws;
194 fmt::format_to(std::back_inserter(ws), FMT_STRING(L"{}"), 42);
195 EXPECT_EQ(L"42", ws);
198 TEST(xchar_test, vformat_to) {
199 int n = 42;
200 auto args = fmt::make_wformat_args(n);
201 auto w = std::wstring();
202 fmt::vformat_to(std::back_inserter(w), L"{}", args);
203 EXPECT_EQ(L"42", w);
206 namespace test {
207 struct struct_as_wstring_view {};
208 auto format_as(struct_as_wstring_view) -> fmt::wstring_view { return L"foo"; }
209 } // namespace test
211 TEST(xchar_test, format_as) {
212 EXPECT_EQ(fmt::format(L"{}", test::struct_as_wstring_view()), L"foo");
215 TEST(format_test, wide_format_to_n) {
216 wchar_t buffer[4];
217 buffer[3] = L'x';
218 auto result = fmt::format_to_n(buffer, 3, L"{}", 12345);
219 EXPECT_EQ(5u, result.size);
220 EXPECT_EQ(buffer + 3, result.out);
221 EXPECT_EQ(L"123x", fmt::wstring_view(buffer, 4));
222 buffer[0] = L'x';
223 buffer[1] = L'x';
224 buffer[2] = L'x';
225 result = fmt::format_to_n(buffer, 3, L"{}", L'A');
226 EXPECT_EQ(1u, result.size);
227 EXPECT_EQ(buffer + 1, result.out);
228 EXPECT_EQ(L"Axxx", fmt::wstring_view(buffer, 4));
229 result = fmt::format_to_n(buffer, 3, L"{}{} ", L'B', L'C');
230 EXPECT_EQ(3u, result.size);
231 EXPECT_EQ(buffer + 3, result.out);
232 EXPECT_EQ(L"BC x", fmt::wstring_view(buffer, 4));
235 #if FMT_USE_USER_DEFINED_LITERALS
236 TEST(xchar_test, named_arg_udl) {
237 using namespace fmt::literals;
238 auto udl_a =
239 fmt::format(L"{first}{second}{first}{third}", L"first"_a = L"abra",
240 L"second"_a = L"cad", L"third"_a = 99);
241 EXPECT_EQ(
242 fmt::format(L"{first}{second}{first}{third}", fmt::arg(L"first", L"abra"),
243 fmt::arg(L"second", L"cad"), fmt::arg(L"third", 99)),
244 udl_a);
246 #endif // FMT_USE_USER_DEFINED_LITERALS
248 TEST(xchar_test, print) {
249 // Check that the wide print overload compiles.
250 if (fmt::detail::const_check(false)) {
251 fmt::print(L"test");
252 fmt::println(L"test");
256 TEST(xchar_test, join) {
257 int v[3] = {1, 2, 3};
258 EXPECT_EQ(fmt::format(L"({})", fmt::join(v, v + 3, L", ")), L"(1, 2, 3)");
259 auto t = std::tuple<wchar_t, int, float>('a', 1, 2.0f);
260 EXPECT_EQ(fmt::format(L"({})", fmt::join(t, L", ")), L"(a, 1, 2)");
263 enum streamable_enum {};
265 std::wostream& operator<<(std::wostream& os, streamable_enum) {
266 return os << L"streamable_enum";
269 namespace fmt {
270 template <>
271 struct formatter<streamable_enum, wchar_t> : basic_ostream_formatter<wchar_t> {
273 } // namespace fmt
275 enum unstreamable_enum {};
276 auto format_as(unstreamable_enum e) -> int { return e; }
278 TEST(xchar_test, enum) {
279 EXPECT_EQ(L"streamable_enum", fmt::format(L"{}", streamable_enum()));
280 EXPECT_EQ(L"0", fmt::format(L"{}", unstreamable_enum()));
283 struct streamable_and_unformattable {};
285 auto operator<<(std::wostream& os, streamable_and_unformattable)
286 -> std::wostream& {
287 return os << L"foo";
290 TEST(xchar_test, streamed) {
291 EXPECT_FALSE(fmt::is_formattable<streamable_and_unformattable>());
292 EXPECT_EQ(fmt::format(L"{}", fmt::streamed(streamable_and_unformattable())),
293 L"foo");
296 TEST(xchar_test, sign_not_truncated) {
297 wchar_t format_str[] = {
298 L'{', L':',
299 '+' | static_cast<wchar_t>(1 << fmt::detail::num_bits<char>()), L'}', 0};
300 EXPECT_THROW(fmt::format(fmt::runtime(format_str), 42), fmt::format_error);
303 TEST(xchar_test, chrono) {
304 auto tm = std::tm();
305 tm.tm_year = 116;
306 tm.tm_mon = 3;
307 tm.tm_mday = 25;
308 tm.tm_hour = 11;
309 tm.tm_min = 22;
310 tm.tm_sec = 33;
311 EXPECT_EQ(fmt::format("The date is {:%Y-%m-%d %H:%M:%S}.", tm),
312 "The date is 2016-04-25 11:22:33.");
313 EXPECT_EQ(L"42s", fmt::format(L"{}", std::chrono::seconds(42)));
314 EXPECT_EQ(fmt::format(L"{:%F}", tm), L"2016-04-25");
315 EXPECT_EQ(fmt::format(L"{:%T}", tm), L"11:22:33");
318 std::wstring system_wcsftime(const std::wstring& format, const std::tm* timeptr,
319 std::locale* locptr = nullptr) {
320 auto loc = locptr ? *locptr : std::locale::classic();
321 auto& facet = std::use_facet<std::time_put<wchar_t>>(loc);
322 std::wostringstream os;
323 os.imbue(loc);
324 facet.put(os, os, L' ', timeptr, format.c_str(),
325 format.c_str() + format.size());
326 #ifdef _WIN32
327 // Workaround a bug in older versions of Universal CRT.
328 auto str = os.str();
329 if (str == L"-0000") str = L"+0000";
330 return str;
331 #else
332 return os.str();
333 #endif
336 TEST(chrono_test_wchar, time_point) {
337 auto t1 = std::chrono::time_point_cast<std::chrono::seconds>(
338 std::chrono::system_clock::now());
340 std::vector<std::wstring> spec_list = {
341 L"%%", L"%n", L"%t", L"%Y", L"%EY", L"%y", L"%Oy", L"%Ey", L"%C",
342 L"%EC", L"%G", L"%g", L"%b", L"%h", L"%B", L"%m", L"%Om", L"%U",
343 L"%OU", L"%W", L"%OW", L"%V", L"%OV", L"%j", L"%d", L"%Od", L"%e",
344 L"%Oe", L"%a", L"%A", L"%w", L"%Ow", L"%u", L"%Ou", L"%H", L"%OH",
345 L"%I", L"%OI", L"%M", L"%OM", L"%S", L"%OS", L"%x", L"%Ex", L"%X",
346 L"%EX", L"%D", L"%F", L"%R", L"%T", L"%p"};
347 #ifndef _WIN32
348 // Disabled on Windows, because these formats is not consistent among
349 // platforms.
350 spec_list.insert(spec_list.end(), {L"%c", L"%Ec", L"%r"});
351 #elif !FMT_HAS_C99_STRFTIME
352 // Only C89 conversion specifiers when using MSVCRT instead of UCRT
353 spec_list = {L"%%", L"%Y", L"%y", L"%b", L"%B", L"%m", L"%U",
354 L"%W", L"%j", L"%d", L"%a", L"%A", L"%w", L"%H",
355 L"%I", L"%M", L"%S", L"%x", L"%X", L"%p"};
356 #endif
357 spec_list.push_back(L"%Y-%m-%d %H:%M:%S");
359 for (const auto& spec : spec_list) {
360 auto t = std::chrono::system_clock::to_time_t(t1);
361 auto tm = *std::gmtime(&t);
363 auto sys_output = system_wcsftime(spec, &tm);
365 auto fmt_spec = fmt::format(L"{{:{}}}", spec);
366 EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), t1));
367 EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), tm));
370 // Timezone formatters tests makes sense for localtime.
371 #if FMT_HAS_C99_STRFTIME
372 spec_list = {L"%z", L"%Z"};
373 #else
374 spec_list = {L"%Z"};
375 #endif
376 for (const auto& spec : spec_list) {
377 auto t = std::chrono::system_clock::to_time_t(t1);
378 auto tm = *std::localtime(&t);
380 auto sys_output = system_wcsftime(spec, &tm);
382 auto fmt_spec = fmt::format(L"{{:{}}}", spec);
383 EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), tm));
385 if (spec == L"%z") {
386 sys_output.insert(sys_output.end() - 2, 1, L':');
387 EXPECT_EQ(sys_output, fmt::format(L"{:%Ez}", tm));
388 EXPECT_EQ(sys_output, fmt::format(L"{:%Oz}", tm));
392 // Separate tests for UTC, since std::time_put can use local time and ignoring
393 // the timezone in std::tm (if it presents on platform).
394 if (fmt::detail::has_member_data_tm_zone<std::tm>::value) {
395 auto t = std::chrono::system_clock::to_time_t(t1);
396 auto tm = *std::gmtime(&t);
398 std::vector<std::wstring> tz_names = {L"GMT", L"UTC"};
399 EXPECT_THAT(tz_names, Contains(fmt::format(L"{:%Z}", t1)));
400 EXPECT_THAT(tz_names, Contains(fmt::format(L"{:%Z}", tm)));
403 if (fmt::detail::has_member_data_tm_gmtoff<std::tm>::value) {
404 auto t = std::chrono::system_clock::to_time_t(t1);
405 auto tm = *std::gmtime(&t);
407 EXPECT_EQ(L"+0000", fmt::format(L"{:%z}", t1));
408 EXPECT_EQ(L"+0000", fmt::format(L"{:%z}", tm));
410 EXPECT_EQ(L"+00:00", fmt::format(L"{:%Ez}", t1));
411 EXPECT_EQ(L"+00:00", fmt::format(L"{:%Ez}", tm));
413 EXPECT_EQ(L"+00:00", fmt::format(L"{:%Oz}", t1));
414 EXPECT_EQ(L"+00:00", fmt::format(L"{:%Oz}", tm));
418 TEST(xchar_test, color) {
419 EXPECT_EQ(fmt::format(fg(fmt::rgb(255, 20, 30)), L"rgb(255,20,30) wide"),
420 L"\x1b[38;2;255;020;030mrgb(255,20,30) wide\x1b[0m");
423 TEST(xchar_test, ostream) {
424 #if !FMT_GCC_VERSION || FMT_GCC_VERSION >= 409
426 std::wostringstream wos;
427 fmt::print(wos, L"Don't {}!", L"panic");
428 EXPECT_EQ(wos.str(), L"Don't panic!");
432 std::wostringstream wos;
433 fmt::println(wos, L"Don't {}!", L"panic");
434 EXPECT_EQ(wos.str(), L"Don't panic!\n");
436 #endif
439 TEST(xchar_test, format_map) {
440 auto m = std::map<std::wstring, int>{{L"one", 1}, {L"t\"wo", 2}};
441 EXPECT_EQ(fmt::format(L"{}", m), L"{\"one\": 1, \"t\\\"wo\": 2}");
444 TEST(xchar_test, escape_string) {
445 using vec = std::vector<std::wstring>;
446 EXPECT_EQ(fmt::format(L"{}", vec{L"\n\r\t\"\\"}), L"[\"\\n\\r\\t\\\"\\\\\"]");
447 EXPECT_EQ(fmt::format(L"{}", vec{L"понедельник"}), L"[\"понедельник\"]");
450 TEST(xchar_test, to_wstring) { EXPECT_EQ(L"42", fmt::to_wstring(42)); }
452 #ifndef FMT_STATIC_THOUSANDS_SEPARATOR
454 template <typename Char> struct numpunct : std::numpunct<Char> {
455 protected:
456 Char do_decimal_point() const override { return '?'; }
457 std::string do_grouping() const override { return "\03"; }
458 Char do_thousands_sep() const override { return '~'; }
461 template <typename Char> struct no_grouping : std::numpunct<Char> {
462 protected:
463 Char do_decimal_point() const override { return '.'; }
464 std::string do_grouping() const override { return ""; }
465 Char do_thousands_sep() const override { return ','; }
468 template <typename Char> struct special_grouping : std::numpunct<Char> {
469 protected:
470 Char do_decimal_point() const override { return '.'; }
471 std::string do_grouping() const override { return "\03\02"; }
472 Char do_thousands_sep() const override { return ','; }
475 template <typename Char> struct small_grouping : std::numpunct<Char> {
476 protected:
477 Char do_decimal_point() const override { return '.'; }
478 std::string do_grouping() const override { return "\01"; }
479 Char do_thousands_sep() const override { return ','; }
482 TEST(locale_test, localized_double) {
483 auto loc = std::locale(std::locale(), new numpunct<char>());
484 EXPECT_EQ(fmt::format(loc, "{:L}", 1.23), "1?23");
485 EXPECT_EQ(fmt::format(loc, "{:Lf}", 1.23), "1?230000");
486 EXPECT_EQ(fmt::format(loc, "{:L}", 1234.5), "1~234?5");
487 EXPECT_EQ(fmt::format(loc, "{:L}", 12000.0), "12~000");
488 EXPECT_EQ(fmt::format(loc, "{:8L}", 1230.0), " 1~230");
489 EXPECT_EQ(fmt::format(loc, "{:15.6Lf}", 0.1), " 0?100000");
490 EXPECT_EQ(fmt::format(loc, "{:15.6Lf}", 1.0), " 1?000000");
491 EXPECT_EQ(fmt::format(loc, "{:15.6Lf}", 1e3), " 1~000?000000");
494 TEST(locale_test, format) {
495 auto loc = std::locale(std::locale(), new numpunct<char>());
496 EXPECT_EQ("1234567", fmt::format(std::locale(), "{:L}", 1234567));
497 EXPECT_EQ("1~234~567", fmt::format(loc, "{:L}", 1234567));
498 EXPECT_EQ("-1~234~567", fmt::format(loc, "{:L}", -1234567));
499 EXPECT_EQ("-256", fmt::format(loc, "{:L}", -256));
500 auto n = 1234567;
501 EXPECT_EQ("1~234~567", fmt::vformat(loc, "{:L}", fmt::make_format_args(n)));
502 auto s = std::string();
503 fmt::format_to(std::back_inserter(s), loc, "{:L}", 1234567);
504 EXPECT_EQ("1~234~567", s);
506 auto no_grouping_loc = std::locale(std::locale(), new no_grouping<char>());
507 EXPECT_EQ("1234567", fmt::format(no_grouping_loc, "{:L}", 1234567));
509 auto special_grouping_loc =
510 std::locale(std::locale(), new special_grouping<char>());
511 EXPECT_EQ("1,23,45,678", fmt::format(special_grouping_loc, "{:L}", 12345678));
512 EXPECT_EQ("12,345", fmt::format(special_grouping_loc, "{:L}", 12345));
514 auto small_grouping_loc =
515 std::locale(std::locale(), new small_grouping<char>());
516 EXPECT_EQ("4,2,9,4,9,6,7,2,9,5",
517 fmt::format(small_grouping_loc, "{:L}", max_value<uint32_t>()));
520 TEST(locale_test, format_default_align) {
521 auto loc = std::locale({}, new special_grouping<char>());
522 EXPECT_EQ(" 12,345", fmt::format(loc, "{:8L}", 12345));
525 TEST(locale_test, format_plus) {
526 auto loc = std::locale({}, new special_grouping<char>());
527 EXPECT_EQ("+100", fmt::format(loc, "{:+L}", 100));
530 TEST(locale_test, wformat) {
531 auto loc = std::locale(std::locale(), new numpunct<wchar_t>());
532 EXPECT_EQ(L"1234567", fmt::format(std::locale(), L"{:L}", 1234567));
533 EXPECT_EQ(L"1~234~567", fmt::format(loc, L"{:L}", 1234567));
534 int n = 1234567;
535 EXPECT_EQ(L"1~234~567",
536 fmt::vformat(loc, L"{:L}", fmt::make_wformat_args(n)));
537 EXPECT_EQ(L"1234567", fmt::format(std::locale("C"), L"{:L}", 1234567));
539 auto no_grouping_loc = std::locale(std::locale(), new no_grouping<wchar_t>());
540 EXPECT_EQ(L"1234567", fmt::format(no_grouping_loc, L"{:L}", 1234567));
542 auto special_grouping_loc =
543 std::locale(std::locale(), new special_grouping<wchar_t>());
544 EXPECT_EQ(L"1,23,45,678",
545 fmt::format(special_grouping_loc, L"{:L}", 12345678));
547 auto small_grouping_loc =
548 std::locale(std::locale(), new small_grouping<wchar_t>());
549 EXPECT_EQ(L"4,2,9,4,9,6,7,2,9,5",
550 fmt::format(small_grouping_loc, L"{:L}", max_value<uint32_t>()));
553 TEST(locale_test, int_formatter) {
554 auto loc = std::locale(std::locale(), new special_grouping<char>());
555 auto f = fmt::formatter<int>();
556 auto parse_ctx = fmt::format_parse_context("L");
557 f.parse(parse_ctx);
558 auto buf = fmt::memory_buffer();
559 fmt::basic_format_context<fmt::appender, char> format_ctx(
560 fmt::appender(buf), {}, fmt::detail::locale_ref(loc));
561 f.format(12345, format_ctx);
562 EXPECT_EQ(fmt::to_string(buf), "12,345");
565 TEST(locale_test, chrono_weekday) {
566 auto loc = get_locale("es_ES.UTF-8", "Spanish_Spain.1252");
567 auto loc_old = std::locale::global(loc);
568 auto sat = fmt::weekday(6);
569 EXPECT_EQ(fmt::format(L"{}", sat), L"Sat");
570 if (loc != std::locale::classic()) {
571 // L'\xE1' is 'á'.
572 auto saturdays = std::vector<std::wstring>{
573 L"s\xE1"
574 "b",
575 L"s\xE1."};
576 EXPECT_THAT(saturdays, Contains(fmt::format(loc, L"{:L}", sat)));
578 std::locale::global(loc_old);
581 TEST(locale_test, sign) {
582 EXPECT_EQ(fmt::format(std::locale(), L"{:L}", -50), L"-50");
585 TEST(std_test_xchar, complex) {
586 auto s = fmt::format(L"{}", std::complex<double>(1, 2));
587 EXPECT_EQ(s, L"(1+2i)");
588 EXPECT_EQ(fmt::format(L"{:.2f}", std::complex<double>(1, 2)), L"(1.00+2.00i)");
589 EXPECT_EQ(fmt::format(L"{:8}", std::complex<double>(1, 2)), L"(1+2i) ");
592 TEST(std_test_xchar, optional) {
593 # ifdef __cpp_lib_optional
594 EXPECT_EQ(fmt::format(L"{}", std::optional{L'C'}), L"optional(\'C\')");
595 EXPECT_EQ(fmt::format(L"{}", std::optional{std::wstring{L"wide string"}}),
596 L"optional(\"wide string\")");
597 # endif
600 #endif // FMT_STATIC_THOUSANDS_SEPARATOR