]>
Commit | Line | Data |
---|---|---|
eafe8130 TL |
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" | |
f67539c2 | 9 | |
eafe8130 TL |
10 | #include "gmock.h" |
11 | ||
f67539c2 TL |
12 | using fmt::detail::max_value; |
13 | ||
9f95a23c TL |
14 | #ifndef FMT_STATIC_THOUSANDS_SEPARATOR |
15 | template <typename Char> struct numpunct : std::numpunct<Char> { | |
eafe8130 | 16 | protected: |
f67539c2 TL |
17 | Char do_decimal_point() const FMT_OVERRIDE { return '?'; } |
18 | std::string do_grouping() const FMT_OVERRIDE { return "\03"; } | |
eafe8130 TL |
19 | Char do_thousands_sep() const FMT_OVERRIDE { return '~'; } |
20 | }; | |
21 | ||
f67539c2 TL |
22 | template <typename Char> struct no_grouping : std::numpunct<Char> { |
23 | protected: | |
24 | Char do_decimal_point() const FMT_OVERRIDE { return '.'; } | |
25 | std::string do_grouping() const FMT_OVERRIDE { return ""; } | |
26 | Char do_thousands_sep() const FMT_OVERRIDE { return ','; } | |
27 | }; | |
28 | ||
29 | template <typename Char> struct special_grouping : std::numpunct<Char> { | |
30 | protected: | |
31 | Char do_decimal_point() const FMT_OVERRIDE { return '.'; } | |
32 | std::string do_grouping() const FMT_OVERRIDE { return "\03\02"; } | |
33 | Char do_thousands_sep() const FMT_OVERRIDE { return ','; } | |
34 | }; | |
35 | ||
36 | template <typename Char> struct small_grouping : std::numpunct<Char> { | |
37 | protected: | |
38 | Char do_decimal_point() const FMT_OVERRIDE { return '.'; } | |
39 | std::string do_grouping() const FMT_OVERRIDE { return "\01"; } | |
40 | Char do_thousands_sep() const FMT_OVERRIDE { return ','; } | |
41 | }; | |
42 | ||
43 | TEST(LocaleTest, DoubleDecimalPoint) { | |
44 | std::locale loc(std::locale(), new numpunct<char>()); | |
45 | EXPECT_EQ("1?23", fmt::format(loc, "{:n}", 1.23)); | |
46 | } | |
47 | ||
eafe8130 TL |
48 | TEST(LocaleTest, Format) { |
49 | std::locale loc(std::locale(), new numpunct<char>()); | |
f67539c2 | 50 | EXPECT_EQ("1234567", fmt::format(std::locale(), "{:n}", 1234567)); |
eafe8130 TL |
51 | EXPECT_EQ("1~234~567", fmt::format(loc, "{:n}", 1234567)); |
52 | fmt::format_arg_store<fmt::format_context, int> as{1234567}; | |
53 | EXPECT_EQ("1~234~567", fmt::vformat(loc, "{:n}", fmt::format_args(as))); | |
54 | std::string s; | |
55 | fmt::format_to(std::back_inserter(s), loc, "{:n}", 1234567); | |
56 | EXPECT_EQ("1~234~567", s); | |
f67539c2 TL |
57 | |
58 | std::locale no_grouping_loc(std::locale(), new no_grouping<char>()); | |
59 | EXPECT_EQ("1234567", fmt::format(no_grouping_loc, "{:n}", 1234567)); | |
60 | ||
61 | std::locale special_grouping_loc(std::locale(), new special_grouping<char>()); | |
62 | EXPECT_EQ("1,23,45,678", fmt::format(special_grouping_loc, "{:n}", 12345678)); | |
63 | ||
64 | std::locale small_grouping_loc(std::locale(), new small_grouping<char>()); | |
65 | EXPECT_EQ("4,2,9,4,9,6,7,2,9,5", | |
66 | fmt::format(small_grouping_loc, "{:n}", max_value<uint32_t>())); | |
eafe8130 TL |
67 | } |
68 | ||
69 | TEST(LocaleTest, WFormat) { | |
70 | std::locale loc(std::locale(), new numpunct<wchar_t>()); | |
f67539c2 | 71 | EXPECT_EQ(L"1234567", fmt::format(std::locale(), L"{:n}", 1234567)); |
eafe8130 TL |
72 | EXPECT_EQ(L"1~234~567", fmt::format(loc, L"{:n}", 1234567)); |
73 | fmt::format_arg_store<fmt::wformat_context, int> as{1234567}; | |
74 | EXPECT_EQ(L"1~234~567", fmt::vformat(loc, L"{:n}", fmt::wformat_args(as))); | |
f67539c2 TL |
75 | EXPECT_EQ(L"1234567", fmt::format(std::locale("C"), L"{:n}", 1234567)); |
76 | ||
77 | std::locale no_grouping_loc(std::locale(), new no_grouping<wchar_t>()); | |
78 | EXPECT_EQ(L"1234567", fmt::format(no_grouping_loc, L"{:n}", 1234567)); | |
79 | ||
80 | std::locale special_grouping_loc(std::locale(), | |
81 | new special_grouping<wchar_t>()); | |
82 | EXPECT_EQ(L"1,23,45,678", | |
83 | fmt::format(special_grouping_loc, L"{:n}", 12345678)); | |
84 | ||
85 | std::locale small_grouping_loc(std::locale(), new small_grouping<wchar_t>()); | |
86 | EXPECT_EQ(L"4,2,9,4,9,6,7,2,9,5", | |
87 | fmt::format(small_grouping_loc, L"{:n}", max_value<uint32_t>())); | |
eafe8130 | 88 | } |
f67539c2 | 89 | |
9f95a23c | 90 | #endif // FMT_STATIC_THOUSANDS_SEPARATOR |