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