xchar-test.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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.
  7. #include "fmt/xchar.h"
  8. #include <algorithm>
  9. #include <complex>
  10. #include <cwchar>
  11. #include <vector>
  12. #include "fmt/chrono.h"
  13. #include "fmt/color.h"
  14. #include "fmt/ostream.h"
  15. #include "fmt/ranges.h"
  16. #include "fmt/std.h"
  17. #include "gtest-extra.h" // Contains
  18. #include "util.h" // get_locale
  19. using fmt::detail::max_value;
  20. using testing::Contains;
  21. #if defined(__MINGW32__) && !defined(_UCRT)
  22. // Only C89 conversion specifiers when using MSVCRT instead of UCRT
  23. # define FMT_HAS_C99_STRFTIME 0
  24. #else
  25. # define FMT_HAS_C99_STRFTIME 1
  26. #endif
  27. struct non_string {};
  28. template <typename T> class has_to_string_view_test : public testing::Test {};
  29. using string_char_types = testing::Types<char, wchar_t, char16_t, char32_t>;
  30. TYPED_TEST_SUITE(has_to_string_view_test, string_char_types);
  31. template <typename Char>
  32. struct derived_from_string_view : fmt::basic_string_view<Char> {};
  33. TYPED_TEST(has_to_string_view_test, has_to_string_view) {
  34. EXPECT_TRUE(fmt::detail::has_to_string_view<TypeParam*>::value);
  35. EXPECT_TRUE(fmt::detail::has_to_string_view<const TypeParam*>::value);
  36. EXPECT_TRUE(fmt::detail::has_to_string_view<TypeParam[2]>::value);
  37. EXPECT_TRUE(fmt::detail::has_to_string_view<const TypeParam[2]>::value);
  38. EXPECT_TRUE(
  39. fmt::detail::has_to_string_view<std::basic_string<TypeParam>>::value);
  40. EXPECT_TRUE(fmt::detail::has_to_string_view<
  41. fmt::basic_string_view<TypeParam>>::value);
  42. EXPECT_TRUE(fmt::detail::has_to_string_view<
  43. derived_from_string_view<TypeParam>>::value);
  44. using fmt_string_view = fmt::detail::std_string_view<TypeParam>;
  45. EXPECT_TRUE(std::is_empty<fmt_string_view>::value !=
  46. fmt::detail::has_to_string_view<fmt_string_view>::value);
  47. EXPECT_FALSE(fmt::detail::has_to_string_view<non_string>::value);
  48. }
  49. // std::is_constructible is broken in MSVC until version 2015.
  50. #if !FMT_MSC_VERSION || FMT_MSC_VERSION >= 1900
  51. struct explicitly_convertible_to_wstring_view {
  52. explicit operator fmt::wstring_view() const { return L"foo"; }
  53. };
  54. TEST(xchar_test, format_explicitly_convertible_to_wstring_view) {
  55. // Types explicitly convertible to wstring_view are not formattable by
  56. // default because it may introduce ODR violations.
  57. static_assert(
  58. !fmt::is_formattable<explicitly_convertible_to_wstring_view>::value, "");
  59. }
  60. #endif
  61. TEST(xchar_test, format) {
  62. EXPECT_EQ(fmt::format(L"{}", 42), L"42");
  63. EXPECT_EQ(fmt::format(L"{}", 4.2), L"4.2");
  64. EXPECT_EQ(fmt::format(L"{}", L"abc"), L"abc");
  65. EXPECT_EQ(fmt::format(L"{}", L'z'), L"z");
  66. EXPECT_THROW(fmt::format(fmt::runtime(L"{:*\x343E}"), 42), fmt::format_error);
  67. EXPECT_EQ(fmt::format(L"{}", true), L"true");
  68. EXPECT_EQ(fmt::format(L"{0}", L'a'), L"a");
  69. EXPECT_EQ(fmt::format(L"Letter {}", L'\x40e'), L"Letter \x40e"); // Ў
  70. if (sizeof(wchar_t) == 4)
  71. EXPECT_EQ(fmt::format(fmt::runtime(L"{:𓀨>3}"), 42), L"𓀨42");
  72. EXPECT_EQ(fmt::format(L"{}c{}", L"ab", 1), L"abc1");
  73. }
  74. TEST(xchar_test, is_formattable) {
  75. static_assert(!fmt::is_formattable<const wchar_t*>::value, "");
  76. }
  77. TEST(xchar_test, compile_time_string) {
  78. EXPECT_EQ(fmt::format(fmt::wformat_string<int>(L"{}"), 42), L"42");
  79. #if defined(FMT_USE_STRING_VIEW) && FMT_CPLUSPLUS >= 201703L
  80. EXPECT_EQ(fmt::format(FMT_STRING(std::wstring_view(L"{}")), 42), L"42");
  81. #endif
  82. }
  83. TEST(xchar_test, format_to) {
  84. auto buf = std::vector<wchar_t>();
  85. fmt::format_to(std::back_inserter(buf), L"{}{}", 42, L'\0');
  86. EXPECT_STREQ(buf.data(), L"42");
  87. }
  88. TEST(xchar_test, compile_time_string_format_to) {
  89. std::wstring ws;
  90. fmt::format_to(std::back_inserter(ws), FMT_STRING(L"{}"), 42);
  91. EXPECT_EQ(L"42", ws);
  92. }
  93. TEST(xchar_test, vformat_to) {
  94. int n = 42;
  95. auto args = fmt::make_wformat_args(n);
  96. auto w = std::wstring();
  97. fmt::vformat_to(std::back_inserter(w), L"{}", args);
  98. EXPECT_EQ(L"42", w);
  99. }
  100. namespace test {
  101. struct struct_as_wstring_view {};
  102. auto format_as(struct_as_wstring_view) -> fmt::wstring_view { return L"foo"; }
  103. } // namespace test
  104. TEST(xchar_test, format_as) {
  105. EXPECT_EQ(fmt::format(L"{}", test::struct_as_wstring_view()), L"foo");
  106. }
  107. TEST(format_test, wide_format_to_n) {
  108. wchar_t buffer[4];
  109. buffer[3] = L'x';
  110. auto result = fmt::format_to_n(buffer, 3, L"{}", 12345);
  111. EXPECT_EQ(5u, result.size);
  112. EXPECT_EQ(buffer + 3, result.out);
  113. EXPECT_EQ(L"123x", fmt::wstring_view(buffer, 4));
  114. buffer[0] = L'x';
  115. buffer[1] = L'x';
  116. buffer[2] = L'x';
  117. result = fmt::format_to_n(buffer, 3, L"{}", L'A');
  118. EXPECT_EQ(1u, result.size);
  119. EXPECT_EQ(buffer + 1, result.out);
  120. EXPECT_EQ(L"Axxx", fmt::wstring_view(buffer, 4));
  121. result = fmt::format_to_n(buffer, 3, L"{}{} ", L'B', L'C');
  122. EXPECT_EQ(3u, result.size);
  123. EXPECT_EQ(buffer + 3, result.out);
  124. EXPECT_EQ(L"BC x", fmt::wstring_view(buffer, 4));
  125. }
  126. TEST(xchar_test, named_arg_udl) {
  127. using namespace fmt::literals;
  128. auto udl_a =
  129. fmt::format(L"{first}{second}{first}{third}", L"first"_a = L"abra",
  130. L"second"_a = L"cad", L"third"_a = 99);
  131. EXPECT_EQ(
  132. fmt::format(L"{first}{second}{first}{third}", fmt::arg(L"first", L"abra"),
  133. fmt::arg(L"second", L"cad"), fmt::arg(L"third", 99)),
  134. udl_a);
  135. }
  136. TEST(xchar_test, print) {
  137. // Check that the wide print overload compiles.
  138. if (fmt::detail::const_check(false)) {
  139. fmt::print(L"test");
  140. fmt::println(L"test");
  141. }
  142. }
  143. TEST(xchar_test, join) {
  144. int v[3] = {1, 2, 3};
  145. EXPECT_EQ(fmt::format(L"({})", fmt::join(v, v + 3, L", ")), L"(1, 2, 3)");
  146. auto t = std::tuple<wchar_t, int, float>('a', 1, 2.0f);
  147. EXPECT_EQ(fmt::format(L"({})", fmt::join(t, L", ")), L"(a, 1, 2)");
  148. }
  149. enum streamable_enum {};
  150. std::wostream& operator<<(std::wostream& os, streamable_enum) {
  151. return os << L"streamable_enum";
  152. }
  153. namespace fmt {
  154. template <>
  155. struct formatter<streamable_enum, wchar_t> : basic_ostream_formatter<wchar_t> {
  156. };
  157. } // namespace fmt
  158. enum unstreamable_enum {};
  159. auto format_as(unstreamable_enum e) -> int { return e; }
  160. TEST(xchar_test, enum) {
  161. EXPECT_EQ(L"streamable_enum", fmt::format(L"{}", streamable_enum()));
  162. EXPECT_EQ(L"0", fmt::format(L"{}", unstreamable_enum()));
  163. }
  164. struct streamable_and_unformattable {};
  165. auto operator<<(std::wostream& os, streamable_and_unformattable)
  166. -> std::wostream& {
  167. return os << L"foo";
  168. }
  169. TEST(xchar_test, streamed) {
  170. EXPECT_FALSE(fmt::is_formattable<streamable_and_unformattable>());
  171. EXPECT_EQ(fmt::format(L"{}", fmt::streamed(streamable_and_unformattable())),
  172. L"foo");
  173. }
  174. TEST(xchar_test, sign_not_truncated) {
  175. wchar_t format_str[] = {
  176. L'{', L':',
  177. '+' | static_cast<wchar_t>(1 << fmt::detail::num_bits<char>()), L'}', 0};
  178. EXPECT_THROW(fmt::format(fmt::runtime(format_str), 42), fmt::format_error);
  179. }
  180. TEST(xchar_test, chrono) {
  181. auto tm = std::tm();
  182. tm.tm_year = 116;
  183. tm.tm_mon = 3;
  184. tm.tm_mday = 25;
  185. tm.tm_hour = 11;
  186. tm.tm_min = 22;
  187. tm.tm_sec = 33;
  188. EXPECT_EQ(fmt::format("The date is {:%Y-%m-%d %H:%M:%S}.", tm),
  189. "The date is 2016-04-25 11:22:33.");
  190. EXPECT_EQ(L"42s", fmt::format(L"{}", std::chrono::seconds(42)));
  191. EXPECT_EQ(fmt::format(L"{:%F}", tm), L"2016-04-25");
  192. EXPECT_EQ(fmt::format(L"{:%T}", tm), L"11:22:33");
  193. }
  194. std::wstring system_wcsftime(const std::wstring& format, const std::tm* timeptr,
  195. std::locale* locptr = nullptr) {
  196. auto loc = locptr ? *locptr : std::locale::classic();
  197. auto& facet = std::use_facet<std::time_put<wchar_t>>(loc);
  198. std::wostringstream os;
  199. os.imbue(loc);
  200. facet.put(os, os, L' ', timeptr, format.c_str(),
  201. format.c_str() + format.size());
  202. #ifdef _WIN32
  203. // Workaround a bug in older versions of Universal CRT.
  204. auto str = os.str();
  205. if (str == L"-0000") str = L"+0000";
  206. return str;
  207. #else
  208. return os.str();
  209. #endif
  210. }
  211. TEST(chrono_test_wchar, time_point) {
  212. auto t1 = std::chrono::time_point_cast<std::chrono::seconds>(
  213. std::chrono::system_clock::now());
  214. std::vector<std::wstring> spec_list = {
  215. L"%%", L"%n", L"%t", L"%Y", L"%EY", L"%y", L"%Oy", L"%Ey", L"%C",
  216. L"%EC", L"%G", L"%g", L"%b", L"%h", L"%B", L"%m", L"%Om", L"%U",
  217. L"%OU", L"%W", L"%OW", L"%V", L"%OV", L"%j", L"%d", L"%Od", L"%e",
  218. L"%Oe", L"%a", L"%A", L"%w", L"%Ow", L"%u", L"%Ou", L"%H", L"%OH",
  219. L"%I", L"%OI", L"%M", L"%OM", L"%S", L"%OS", L"%x", L"%Ex", L"%X",
  220. L"%EX", L"%D", L"%F", L"%R", L"%T", L"%p"};
  221. #ifndef _WIN32
  222. // Disabled on Windows, because these formats is not consistent among
  223. // platforms.
  224. spec_list.insert(spec_list.end(), {L"%c", L"%Ec", L"%r"});
  225. #elif !FMT_HAS_C99_STRFTIME
  226. // Only C89 conversion specifiers when using MSVCRT instead of UCRT
  227. spec_list = {L"%%", L"%Y", L"%y", L"%b", L"%B", L"%m", L"%U",
  228. L"%W", L"%j", L"%d", L"%a", L"%A", L"%w", L"%H",
  229. L"%I", L"%M", L"%S", L"%x", L"%X", L"%p"};
  230. #endif
  231. spec_list.push_back(L"%Y-%m-%d %H:%M:%S");
  232. for (const auto& spec : spec_list) {
  233. auto t = std::chrono::system_clock::to_time_t(t1);
  234. auto tm = *std::gmtime(&t);
  235. auto sys_output = system_wcsftime(spec, &tm);
  236. auto fmt_spec = fmt::format(L"{{:{}}}", spec);
  237. EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), t1));
  238. EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), tm));
  239. }
  240. // Timezone formatters tests makes sense for localtime.
  241. #if FMT_HAS_C99_STRFTIME
  242. spec_list = {L"%z", L"%Z"};
  243. #else
  244. spec_list = {L"%Z"};
  245. #endif
  246. for (const auto& spec : spec_list) {
  247. auto t = std::chrono::system_clock::to_time_t(t1);
  248. auto tm = *std::localtime(&t);
  249. auto sys_output = system_wcsftime(spec, &tm);
  250. auto fmt_spec = fmt::format(L"{{:{}}}", spec);
  251. EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), tm));
  252. if (spec == L"%z") {
  253. sys_output.insert(sys_output.end() - 2, 1, L':');
  254. EXPECT_EQ(sys_output, fmt::format(L"{:%Ez}", tm));
  255. EXPECT_EQ(sys_output, fmt::format(L"{:%Oz}", tm));
  256. }
  257. }
  258. // Separate tests for UTC, since std::time_put can use local time and ignoring
  259. // the timezone in std::tm (if it presents on platform).
  260. if (fmt::detail::has_member_data_tm_zone<std::tm>::value) {
  261. auto t = std::chrono::system_clock::to_time_t(t1);
  262. auto tm = *std::gmtime(&t);
  263. std::vector<std::wstring> tz_names = {L"GMT", L"UTC"};
  264. EXPECT_THAT(tz_names, Contains(fmt::format(L"{:%Z}", t1)));
  265. EXPECT_THAT(tz_names, Contains(fmt::format(L"{:%Z}", tm)));
  266. }
  267. if (fmt::detail::has_member_data_tm_gmtoff<std::tm>::value) {
  268. auto t = std::chrono::system_clock::to_time_t(t1);
  269. auto tm = *std::gmtime(&t);
  270. EXPECT_EQ(L"+0000", fmt::format(L"{:%z}", t1));
  271. EXPECT_EQ(L"+0000", fmt::format(L"{:%z}", tm));
  272. EXPECT_EQ(L"+00:00", fmt::format(L"{:%Ez}", t1));
  273. EXPECT_EQ(L"+00:00", fmt::format(L"{:%Ez}", tm));
  274. EXPECT_EQ(L"+00:00", fmt::format(L"{:%Oz}", t1));
  275. EXPECT_EQ(L"+00:00", fmt::format(L"{:%Oz}", tm));
  276. }
  277. }
  278. TEST(xchar_test, color) {
  279. EXPECT_EQ(fmt::format(fg(fmt::rgb(255, 20, 30)), L"rgb(255,20,30) wide"),
  280. L"\x1b[38;2;255;020;030mrgb(255,20,30) wide\x1b[0m");
  281. }
  282. TEST(xchar_test, ostream) {
  283. #if !FMT_GCC_VERSION || FMT_GCC_VERSION >= 409
  284. {
  285. std::wostringstream wos;
  286. fmt::print(wos, L"Don't {}!", L"panic");
  287. EXPECT_EQ(wos.str(), L"Don't panic!");
  288. }
  289. {
  290. std::wostringstream wos;
  291. fmt::println(wos, L"Don't {}!", L"panic");
  292. EXPECT_EQ(wos.str(), L"Don't panic!\n");
  293. }
  294. #endif
  295. }
  296. TEST(xchar_test, format_map) {
  297. auto m = std::map<std::wstring, int>{{L"one", 1}, {L"t\"wo", 2}};
  298. EXPECT_EQ(fmt::format(L"{}", m), L"{\"one\": 1, \"t\\\"wo\": 2}");
  299. }
  300. TEST(xchar_test, escape_string) {
  301. using vec = std::vector<std::wstring>;
  302. EXPECT_EQ(fmt::format(L"{}", vec{L"\n\r\t\"\\"}), L"[\"\\n\\r\\t\\\"\\\\\"]");
  303. EXPECT_EQ(fmt::format(L"{}", vec{L"понедельник"}), L"[\"понедельник\"]");
  304. }
  305. TEST(xchar_test, to_wstring) { EXPECT_EQ(L"42", fmt::to_wstring(42)); }
  306. #ifndef FMT_STATIC_THOUSANDS_SEPARATOR
  307. template <typename Char> struct numpunct : std::numpunct<Char> {
  308. protected:
  309. Char do_decimal_point() const override { return '?'; }
  310. std::string do_grouping() const override { return "\03"; }
  311. Char do_thousands_sep() const override { return '~'; }
  312. };
  313. template <typename Char> struct no_grouping : std::numpunct<Char> {
  314. protected:
  315. Char do_decimal_point() const override { return '.'; }
  316. std::string do_grouping() const override { return ""; }
  317. Char do_thousands_sep() const override { return ','; }
  318. };
  319. template <typename Char> struct special_grouping : std::numpunct<Char> {
  320. protected:
  321. Char do_decimal_point() const override { return '.'; }
  322. std::string do_grouping() const override { return "\03\02"; }
  323. Char do_thousands_sep() const override { return ','; }
  324. };
  325. template <typename Char> struct small_grouping : std::numpunct<Char> {
  326. protected:
  327. Char do_decimal_point() const override { return '.'; }
  328. std::string do_grouping() const override { return "\01"; }
  329. Char do_thousands_sep() const override { return ','; }
  330. };
  331. TEST(locale_test, localized_double) {
  332. auto loc = std::locale(std::locale(), new numpunct<char>());
  333. EXPECT_EQ(fmt::format(loc, "{:L}", 1.23), "1?23");
  334. EXPECT_EQ(fmt::format(loc, "{:Lf}", 1.23), "1?230000");
  335. EXPECT_EQ(fmt::format(loc, "{:L}", 1234.5), "1~234?5");
  336. EXPECT_EQ(fmt::format(loc, "{:L}", 12000.0), "12~000");
  337. EXPECT_EQ(fmt::format(loc, "{:8L}", 1230.0), " 1~230");
  338. EXPECT_EQ(fmt::format(loc, "{:15.6Lf}", 0.1), " 0?100000");
  339. EXPECT_EQ(fmt::format(loc, "{:15.6Lf}", 1.0), " 1?000000");
  340. EXPECT_EQ(fmt::format(loc, "{:15.6Lf}", 1e3), " 1~000?000000");
  341. }
  342. TEST(locale_test, format) {
  343. auto loc = std::locale(std::locale(), new numpunct<char>());
  344. EXPECT_EQ("1234567", fmt::format(std::locale(), "{:L}", 1234567));
  345. EXPECT_EQ("1~234~567", fmt::format(loc, "{:L}", 1234567));
  346. EXPECT_EQ("-1~234~567", fmt::format(loc, "{:L}", -1234567));
  347. EXPECT_EQ("-256", fmt::format(loc, "{:L}", -256));
  348. auto n = 1234567;
  349. EXPECT_EQ("1~234~567", fmt::vformat(loc, "{:L}", fmt::make_format_args(n)));
  350. auto s = std::string();
  351. fmt::format_to(std::back_inserter(s), loc, "{:L}", 1234567);
  352. EXPECT_EQ("1~234~567", s);
  353. auto no_grouping_loc = std::locale(std::locale(), new no_grouping<char>());
  354. EXPECT_EQ("1234567", fmt::format(no_grouping_loc, "{:L}", 1234567));
  355. auto special_grouping_loc =
  356. std::locale(std::locale(), new special_grouping<char>());
  357. EXPECT_EQ("1,23,45,678", fmt::format(special_grouping_loc, "{:L}", 12345678));
  358. EXPECT_EQ("12,345", fmt::format(special_grouping_loc, "{:L}", 12345));
  359. auto small_grouping_loc =
  360. std::locale(std::locale(), new small_grouping<char>());
  361. EXPECT_EQ("4,2,9,4,9,6,7,2,9,5",
  362. fmt::format(small_grouping_loc, "{:L}", max_value<uint32_t>()));
  363. }
  364. TEST(locale_test, format_default_align) {
  365. auto loc = std::locale({}, new special_grouping<char>());
  366. EXPECT_EQ(" 12,345", fmt::format(loc, "{:8L}", 12345));
  367. }
  368. TEST(locale_test, format_plus) {
  369. auto loc = std::locale({}, new special_grouping<char>());
  370. EXPECT_EQ("+100", fmt::format(loc, "{:+L}", 100));
  371. }
  372. TEST(locale_test, wformat) {
  373. auto loc = std::locale(std::locale(), new numpunct<wchar_t>());
  374. EXPECT_EQ(L"1234567", fmt::format(std::locale(), L"{:L}", 1234567));
  375. EXPECT_EQ(L"1~234~567", fmt::format(loc, L"{:L}", 1234567));
  376. int n = 1234567;
  377. EXPECT_EQ(L"1~234~567",
  378. fmt::vformat(loc, L"{:L}", fmt::make_wformat_args(n)));
  379. EXPECT_EQ(L"1234567", fmt::format(std::locale("C"), L"{:L}", 1234567));
  380. auto no_grouping_loc = std::locale(std::locale(), new no_grouping<wchar_t>());
  381. EXPECT_EQ(L"1234567", fmt::format(no_grouping_loc, L"{:L}", 1234567));
  382. auto special_grouping_loc =
  383. std::locale(std::locale(), new special_grouping<wchar_t>());
  384. EXPECT_EQ(L"1,23,45,678",
  385. fmt::format(special_grouping_loc, L"{:L}", 12345678));
  386. auto small_grouping_loc =
  387. std::locale(std::locale(), new small_grouping<wchar_t>());
  388. EXPECT_EQ(L"4,2,9,4,9,6,7,2,9,5",
  389. fmt::format(small_grouping_loc, L"{:L}", max_value<uint32_t>()));
  390. }
  391. TEST(locale_test, int_formatter) {
  392. auto loc = std::locale(std::locale(), new special_grouping<char>());
  393. auto f = fmt::formatter<int>();
  394. auto parse_ctx = fmt::format_parse_context("L");
  395. f.parse(parse_ctx);
  396. auto buf = fmt::memory_buffer();
  397. fmt::basic_format_context<fmt::appender, char> format_ctx(
  398. fmt::appender(buf), {}, fmt::detail::locale_ref(loc));
  399. f.format(12345, format_ctx);
  400. EXPECT_EQ(fmt::to_string(buf), "12,345");
  401. }
  402. TEST(locale_test, chrono_weekday) {
  403. auto loc = get_locale("es_ES.UTF-8", "Spanish_Spain.1252");
  404. auto loc_old = std::locale::global(loc);
  405. auto sat = fmt::weekday(6);
  406. EXPECT_EQ(fmt::format(L"{}", sat), L"Sat");
  407. if (loc != std::locale::classic()) {
  408. // L'\341' is 'á'.
  409. auto saturdays =
  410. std::vector<std::wstring>{L"s\341b", L"s\341.", L"s\341b."};
  411. EXPECT_THAT(saturdays, Contains(fmt::format(loc, L"{:L}", sat)));
  412. }
  413. std::locale::global(loc_old);
  414. }
  415. TEST(locale_test, sign) {
  416. EXPECT_EQ(fmt::format(std::locale(), L"{:L}", -50), L"-50");
  417. }
  418. TEST(std_test_xchar, format_bitset) {
  419. auto bs = std::bitset<6>(42);
  420. EXPECT_EQ(fmt::format(L"{}", bs), L"101010");
  421. EXPECT_EQ(fmt::format(L"{:0>8}", bs), L"00101010");
  422. EXPECT_EQ(fmt::format(L"{:-^12}", bs), L"---101010---");
  423. }
  424. TEST(std_test_xchar, complex) {
  425. auto s = fmt::format(L"{}", std::complex<double>(1, 2));
  426. EXPECT_EQ(s, L"(1+2i)");
  427. EXPECT_EQ(fmt::format(L"{:.2f}", std::complex<double>(1, 2)),
  428. L"(1.00+2.00i)");
  429. EXPECT_EQ(fmt::format(L"{:8}", std::complex<double>(1, 2)), L"(1+2i) ");
  430. EXPECT_EQ(fmt::format(L"{:-<8}", std::complex<double>(1, 2)), L"(1+2i)--");
  431. }
  432. TEST(std_test_xchar, optional) {
  433. # ifdef __cpp_lib_optional
  434. EXPECT_EQ(fmt::format(L"{}", std::optional{L'C'}), L"optional(\'C\')");
  435. EXPECT_EQ(fmt::format(L"{}", std::optional{std::wstring{L"wide string"}}),
  436. L"optional(\"wide string\")");
  437. # endif
  438. }
  439. #endif // FMT_STATIC_THOUSANDS_SEPARATOR