locale-test.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Formatting library for C++ - locale 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/locale.h"
  8. #include <complex>
  9. #include <gmock/gmock.h>
  10. using fmt::detail::max_value;
  11. #ifndef FMT_STATIC_THOUSANDS_SEPARATOR
  12. template <typename Char> struct numpunct : std::numpunct<Char> {
  13. protected:
  14. Char do_decimal_point() const FMT_OVERRIDE { return '?'; }
  15. std::string do_grouping() const FMT_OVERRIDE { return "\03"; }
  16. Char do_thousands_sep() const FMT_OVERRIDE { return '~'; }
  17. };
  18. template <typename Char> struct no_grouping : std::numpunct<Char> {
  19. protected:
  20. Char do_decimal_point() const FMT_OVERRIDE { return '.'; }
  21. std::string do_grouping() const FMT_OVERRIDE { return ""; }
  22. Char do_thousands_sep() const FMT_OVERRIDE { return ','; }
  23. };
  24. template <typename Char> struct special_grouping : std::numpunct<Char> {
  25. protected:
  26. Char do_decimal_point() const FMT_OVERRIDE { return '.'; }
  27. std::string do_grouping() const FMT_OVERRIDE { return "\03\02"; }
  28. Char do_thousands_sep() const FMT_OVERRIDE { return ','; }
  29. };
  30. template <typename Char> struct small_grouping : std::numpunct<Char> {
  31. protected:
  32. Char do_decimal_point() const FMT_OVERRIDE { return '.'; }
  33. std::string do_grouping() const FMT_OVERRIDE { return "\01"; }
  34. Char do_thousands_sep() const FMT_OVERRIDE { return ','; }
  35. };
  36. TEST(LocaleTest, DoubleDecimalPoint) {
  37. std::locale loc(std::locale(), new numpunct<char>());
  38. EXPECT_EQ("1?23", fmt::format(loc, "{:L}", 1.23));
  39. }
  40. TEST(LocaleTest, Format) {
  41. std::locale loc(std::locale(), new numpunct<char>());
  42. EXPECT_EQ("1234567", fmt::format(std::locale(), "{:L}", 1234567));
  43. EXPECT_EQ("1~234~567", fmt::format(loc, "{:L}", 1234567));
  44. EXPECT_EQ("-1~234~567", fmt::format(loc, "{:L}", -1234567));
  45. EXPECT_EQ("-256", fmt::format(loc, "{:L}", -256));
  46. fmt::format_arg_store<fmt::format_context, int> as{1234567};
  47. EXPECT_EQ("1~234~567", fmt::vformat(loc, "{:L}", fmt::format_args(as)));
  48. std::string s;
  49. fmt::format_to(std::back_inserter(s), loc, "{:L}", 1234567);
  50. EXPECT_EQ("1~234~567", s);
  51. std::locale no_grouping_loc(std::locale(), new no_grouping<char>());
  52. EXPECT_EQ("1234567", fmt::format(no_grouping_loc, "{:L}", 1234567));
  53. std::locale special_grouping_loc(std::locale(), new special_grouping<char>());
  54. EXPECT_EQ("1,23,45,678", fmt::format(special_grouping_loc, "{:L}", 12345678));
  55. EXPECT_EQ("12,345", fmt::format(special_grouping_loc, "{:L}", 12345));
  56. std::locale small_grouping_loc(std::locale(), new small_grouping<char>());
  57. EXPECT_EQ("4,2,9,4,9,6,7,2,9,5",
  58. fmt::format(small_grouping_loc, "{:L}", max_value<uint32_t>()));
  59. }
  60. TEST(LocaleTest, FormatDetaultAlign) {
  61. std::locale special_grouping_loc(std::locale(), new special_grouping<char>());
  62. EXPECT_EQ(" 12,345", fmt::format(special_grouping_loc, "{:8L}", 12345));
  63. }
  64. TEST(LocaleTest, WFormat) {
  65. std::locale loc(std::locale(), new numpunct<wchar_t>());
  66. EXPECT_EQ(L"1234567", fmt::format(std::locale(), L"{:L}", 1234567));
  67. EXPECT_EQ(L"1~234~567", fmt::format(loc, L"{:L}", 1234567));
  68. fmt::format_arg_store<fmt::wformat_context, int> as{1234567};
  69. EXPECT_EQ(L"1~234~567", fmt::vformat(loc, L"{:L}", fmt::wformat_args(as)));
  70. EXPECT_EQ(L"1234567", fmt::format(std::locale("C"), L"{:L}", 1234567));
  71. std::locale no_grouping_loc(std::locale(), new no_grouping<wchar_t>());
  72. EXPECT_EQ(L"1234567", fmt::format(no_grouping_loc, L"{:L}", 1234567));
  73. std::locale special_grouping_loc(std::locale(),
  74. new special_grouping<wchar_t>());
  75. EXPECT_EQ(L"1,23,45,678",
  76. fmt::format(special_grouping_loc, L"{:L}", 12345678));
  77. std::locale small_grouping_loc(std::locale(), new small_grouping<wchar_t>());
  78. EXPECT_EQ(L"4,2,9,4,9,6,7,2,9,5",
  79. fmt::format(small_grouping_loc, L"{:L}", max_value<uint32_t>()));
  80. }
  81. TEST(LocaleTest, DoubleFormatter) {
  82. auto loc = std::locale(std::locale(), new special_grouping<char>());
  83. auto f = fmt::formatter<int>();
  84. auto parse_ctx = fmt::format_parse_context("L");
  85. f.parse(parse_ctx);
  86. char buf[10] = {};
  87. fmt::basic_format_context<char*, char> format_ctx(
  88. buf, {}, fmt::detail::locale_ref(loc));
  89. *f.format(12345, format_ctx) = 0;
  90. EXPECT_STREQ("12,345", buf);
  91. }
  92. FMT_BEGIN_NAMESPACE
  93. template <class charT> struct formatter<std::complex<double>, charT> {
  94. private:
  95. detail::dynamic_format_specs<char> specs_;
  96. public:
  97. typename basic_format_parse_context<charT>::iterator parse(
  98. basic_format_parse_context<charT>& ctx) {
  99. using handler_type =
  100. detail::dynamic_specs_handler<basic_format_parse_context<charT>>;
  101. detail::specs_checker<handler_type> handler(handler_type(specs_, ctx),
  102. detail::type::string_type);
  103. auto it = parse_format_specs(ctx.begin(), ctx.end(), handler);
  104. detail::parse_float_type_spec(specs_, ctx.error_handler());
  105. return it;
  106. }
  107. template <class FormatContext>
  108. typename FormatContext::iterator format(const std::complex<double>& c,
  109. FormatContext& ctx) {
  110. detail::handle_dynamic_spec<detail::precision_checker>(
  111. specs_.precision, specs_.precision_ref, ctx);
  112. auto format_specs = std::string();
  113. if (specs_.precision > 0)
  114. format_specs = fmt::format(".{}", specs_.precision);
  115. if (specs_.type)
  116. format_specs += specs_.type;
  117. auto real = fmt::format(ctx.locale().template get<std::locale>(),
  118. "{:" + format_specs + "}", c.real());
  119. auto imag = fmt::format(ctx.locale().template get<std::locale>(),
  120. "{:" + format_specs + "}", c.imag());
  121. auto fill_align_width = std::string();
  122. if (specs_.width > 0)
  123. fill_align_width = fmt::format(">{}", specs_.width);
  124. return format_to(
  125. ctx.out(), "{:" + fill_align_width + "}",
  126. fmt::format(c.real() != 0 ? "({0}+{1}i)" : "{1}i", real, imag));
  127. }
  128. };
  129. FMT_END_NAMESPACE
  130. TEST(FormatTest, Complex) {
  131. std::string s = fmt::format("{}", std::complex<double>(1, 2));
  132. EXPECT_EQ(s, "(1+2i)");
  133. EXPECT_EQ(fmt::format("{:.2f}", std::complex<double>(1, 2)), "(1.00+2.00i)");
  134. EXPECT_EQ(fmt::format("{:8}", std::complex<double>(1, 2)), " (1+2i)");
  135. }
  136. #endif // FMT_STATIC_THOUSANDS_SEPARATOR