1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
10 #include "base/i18n/number_formatting.h"
11 #include "base/i18n/rtl.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/test/icu_test_util.h"
14 #include "build/build_config.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/icu/source/i18n/unicode/usearch.h"
21 TEST(NumberFormattingTest, FormatNumber) {
24 const char* expected_english;
25 const char* expected_german;
28 {1024, "1,024", "1.024"},
29 {std::numeric_limits<int64_t>::max(),
30 "9,223,372,036,854,775,807", "9.223.372.036.854.775.807"},
31 {std::numeric_limits<int64_t>::min(),
32 "-9,223,372,036,854,775,808", "-9.223.372.036.854.775.808"},
36 test::ScopedRestoreICUDefaultLocale restore_locale;
38 for (const auto& i : cases) {
39 i18n::SetICUDefaultLocale("en");
40 ResetFormattersForTesting();
41 EXPECT_EQ(i.expected_english, UTF16ToUTF8(FormatNumber(i.number)));
42 i18n::SetICUDefaultLocale("de");
43 ResetFormattersForTesting();
44 EXPECT_EQ(i.expected_german, UTF16ToUTF8(FormatNumber(i.number)));
48 TEST(NumberFormattingTest, FormatDoubleWithFixedFractionalDigits) {
52 const char* expected_english;
53 const char* expected_german;
56 #if !BUILDFLAG(IS_ANDROID)
57 // Bionic can't printf negative zero correctly.
58 {-0.0, 4, "-0.0000", "-0,0000"},
60 {1024.2, 0, "1,024", "1.024"},
61 {-1024.223, 2, "-1,024.22", "-1.024,22"},
62 {std::numeric_limits<double>::max(), 6,
63 "179,769,313,486,231,570,000,000,000,000,000,000,000,000,000,000,000,"
64 "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
65 "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
66 "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
67 "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
68 "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
70 "179.769.313.486.231.570.000.000.000.000.000.000.000.000.000.000.000."
71 "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
72 "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
73 "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
74 "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
75 "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
77 {std::numeric_limits<double>::min(), 2, "0.00", "0,00"},
78 {-42.7, 3, "-42.700", "-42,700"},
81 test::ScopedRestoreICUDefaultLocale restore_locale;
82 for (const auto& i : cases) {
83 i18n::SetICUDefaultLocale("en");
84 ResetFormattersForTesting();
85 EXPECT_EQ(i.expected_english,
86 UTF16ToUTF8(FormatDouble(i.number, i.frac_digits)));
87 i18n::SetICUDefaultLocale("de");
88 ResetFormattersForTesting();
89 EXPECT_EQ(i.expected_german,
90 UTF16ToUTF8(FormatDouble(i.number, i.frac_digits)));
94 TEST(NumberFormattingTest, FormatDoubleWithFractionalDigitRange) {
99 const char* expected_english;
100 const char* expected_german;
102 {0.0, 0, 0, "0", "0"},
103 #if !BUILDFLAG(IS_ANDROID)
104 // Bionic can't printf negative zero correctly.
105 {-0.0, 0, 4, "-0", "-0"},
107 {1024.2, 0, 0, "1,024", "1.024"},
108 {-1024.223, 0, 2, "-1,024.22", "-1.024,22"},
109 {std::numeric_limits<double>::max(), 0, 6,
110 "179,769,313,486,231,570,000,000,000,000,000,000,000,000,000,000,000,"
111 "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
112 "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
113 "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
114 "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
115 "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
117 "179.769.313.486.231.570.000.000.000.000.000.000.000.000.000.000.000."
118 "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
119 "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
120 "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
121 "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
122 "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
124 {std::numeric_limits<double>::min(), 2, 2, "0.00", "0,00"},
125 {-42.7, 0, 3, "-42.7", "-42,7"},
128 test::ScopedRestoreICUDefaultLocale restore_locale;
129 for (const auto& i : cases) {
130 i18n::SetICUDefaultLocale("en");
131 ResetFormattersForTesting();
132 EXPECT_EQ(i.expected_english,
133 UTF16ToUTF8(FormatDouble(i.number, i.min_frac_digits,
134 i.max_frac_digits)));
135 i18n::SetICUDefaultLocale("de");
136 ResetFormattersForTesting();
137 EXPECT_EQ(i.expected_german,
138 UTF16ToUTF8(FormatDouble(i.number, i.min_frac_digits,
139 i.max_frac_digits)));
143 TEST(NumberFormattingTest, FormatPercent) {
144 static const struct {
146 const char* expected_english;
147 const char* expected_german; // Note: Space before % isn't \x20.
148 // Note: Eastern Arabic-Indic digits (U+06Fx) for Persian and
149 // Arabic-Indic digits (U+066x) for Arabic in Egypt(ar-EG). In Arabic (ar),
150 // uses European digits (Google-patch).
151 // See https://unicode.org/cldr/trac/ticket/9040 for details.
152 // See also https://unicode.org/cldr/trac/ticket/10176 .
153 // For now, take what CLDR 32 has (percent sign to the right of
154 // a number in Persian).
155 const char* expected_persian;
156 const char* expected_arabic;
157 const char* expected_arabic_egypt;
159 {0, "0%", "0\u00a0%", "\u06f0\u066a", "0\u200e%\u200e",
160 "\u0660\u066a\u061c"},
161 {42, "42%", "42\u00a0%", "\u06f4\u06f2\u066a", "42\u200e%\u200e",
162 "\u0664\u0662\u066a\u061c"},
163 {1024, "1,024%", "1.024\u00a0%", "\u06f1\u066c\u06f0\u06f2\u06f4\u066a",
164 "1,024\u200e%\u200e", "\u0661\u066c\u0660\u0662\u0664\u066a\u061c"},
167 test::ScopedRestoreICUDefaultLocale restore_locale;
168 for (const auto& i : cases) {
169 i18n::SetICUDefaultLocale("en");
170 EXPECT_EQ(ASCIIToUTF16(i.expected_english), FormatPercent(i.number));
171 i18n::SetICUDefaultLocale("de");
172 EXPECT_EQ(UTF8ToUTF16(i.expected_german), FormatPercent(i.number));
173 i18n::SetICUDefaultLocale("fa");
174 EXPECT_EQ(UTF8ToUTF16(i.expected_persian), FormatPercent(i.number));
175 i18n::SetICUDefaultLocale("ar");
176 EXPECT_EQ(UTF8ToUTF16(i.expected_arabic), FormatPercent(i.number));
177 i18n::SetICUDefaultLocale("ar-EG");
178 EXPECT_EQ(UTF8ToUTF16(i.expected_arabic_egypt), FormatPercent(i.number));