xchar-test.cc 18 KB

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