1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
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/macros.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/test/icu_test_util.h"
15 #include "build/build_config.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/icu/source/i18n/unicode/usearch.h"
22 TEST(NumberFormattingTest, FormatNumber) {
25 const char* expected_english;
26 const char* expected_german;
29 {1024, "1,024", "1.024"},
30 {std::numeric_limits<int64_t>::max(),
31 "9,223,372,036,854,775,807", "9.223.372.036.854.775.807"},
32 {std::numeric_limits<int64_t>::min(),
33 "-9,223,372,036,854,775,808", "-9.223.372.036.854.775.808"},
37 test::ScopedRestoreICUDefaultLocale restore_locale;
39 for (size_t i = 0; i < arraysize(cases); ++i) {
40 i18n::SetICUDefaultLocale("en");
41 testing::ResetFormatters();
42 EXPECT_EQ(cases[i].expected_english,
43 UTF16ToUTF8(FormatNumber(cases[i].number)));
44 i18n::SetICUDefaultLocale("de");
45 testing::ResetFormatters();
46 EXPECT_EQ(cases[i].expected_german,
47 UTF16ToUTF8(FormatNumber(cases[i].number)));
51 TEST(NumberFormattingTest, FormatDouble) {
55 const char* expected_english;
56 const char* expected_german;
59 #if !defined(OS_ANDROID)
60 // Bionic can't printf negative zero correctly.
61 {-0.0, 4, "-0.0000", "-0,0000"},
63 {1024.2, 0, "1,024", "1.024"},
64 {-1024.223, 2, "-1,024.22", "-1.024,22"},
65 {std::numeric_limits<double>::max(), 6,
66 "179,769,313,486,231,570,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,"
69 "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
70 "000,000,000,000,000,000,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,"
73 "179.769.313.486.231.570.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."
76 "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
77 "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
78 "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
80 {std::numeric_limits<double>::min(), 2, "0.00", "0,00"},
81 {-42.7, 3, "-42.700", "-42,700"},
84 test::ScopedRestoreICUDefaultLocale restore_locale;
85 for (size_t i = 0; i < arraysize(cases); ++i) {
86 i18n::SetICUDefaultLocale("en");
87 testing::ResetFormatters();
88 EXPECT_EQ(cases[i].expected_english,
89 UTF16ToUTF8(FormatDouble(cases[i].number, cases[i].frac_digits)));
90 i18n::SetICUDefaultLocale("de");
91 testing::ResetFormatters();
92 EXPECT_EQ(cases[i].expected_german,
93 UTF16ToUTF8(FormatDouble(cases[i].number, cases[i].frac_digits)));
97 TEST(NumberFormattingTest, FormatPercent) {
100 const char* expected_english;
101 const char* expected_german; // Note: Space before % isn't \x20.
102 // Note: Eastern Arabic-Indic digits (U+06Fx) for Persian and
103 // Arabic-Indic digits (U+066x) for Arabic in Egypt(ar-EG). In Arabic (ar),
104 // uses European digits (Google-patch).
105 // See https://unicode.org/cldr/trac/ticket/9040 for details.
106 // See also https://unicode.org/cldr/trac/ticket/10176 .
107 // For now, take what CLDR 32 has (percent sign to the right of
108 // a number in Persian).
109 const char* expected_persian;
110 const char* expected_arabic;
111 const char* expected_arabic_egypt;
113 {0, "0%", u8"0\u00a0%", u8"\u06f0\u066a", u8"0\u200e%\u200e",
114 u8"\u0660\u066a\u061c"},
115 {42, "42%", "42\u00a0%", u8"\u06f4\u06f2\u066a", u8"42\u200e%\u200e",
116 "\u0664\u0662\u066a\u061c"},
117 {1024, "1,024%", "1.024\u00a0%", u8"\u06f1\u066c\u06f0\u06f2\u06f4\u066a",
118 "1,024\u200e%\u200e", "\u0661\u066c\u0660\u0662\u0664\u066a\u061c"},
121 test::ScopedRestoreICUDefaultLocale restore_locale;
122 for (size_t i = 0; i < arraysize(cases); ++i) {
123 i18n::SetICUDefaultLocale("en");
124 EXPECT_EQ(ASCIIToUTF16(cases[i].expected_english),
125 FormatPercent(cases[i].number));
126 i18n::SetICUDefaultLocale("de");
127 EXPECT_EQ(UTF8ToUTF16(cases[i].expected_german),
128 FormatPercent(cases[i].number));
129 i18n::SetICUDefaultLocale("fa");
130 EXPECT_EQ(UTF8ToUTF16(cases[i].expected_persian),
131 FormatPercent(cases[i].number));
132 i18n::SetICUDefaultLocale("ar");
133 EXPECT_EQ(UTF8ToUTF16(cases[i].expected_arabic),
134 FormatPercent(cases[i].number));
135 i18n::SetICUDefaultLocale("ar-EG");
136 EXPECT_EQ(UTF8ToUTF16(cases[i].expected_arabic_egypt),
137 FormatPercent(cases[i].number));