• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include <stddef.h>
6 #include <stdint.h>
7 
8 #include <limits>
9 
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"
17 
18 namespace base {
19 namespace {
20 
TEST(NumberFormattingTest,FormatNumber)21 TEST(NumberFormattingTest, FormatNumber) {
22   static const struct {
23     int64_t number;
24     const char* expected_english;
25     const char* expected_german;
26   } cases[] = {
27     {0, "0", "0"},
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"},
33     {-42, "-42", "-42"},
34   };
35 
36   test::ScopedRestoreICUDefaultLocale restore_locale;
37 
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)));
45   }
46 }
47 
TEST(NumberFormattingTest,FormatDouble)48 TEST(NumberFormattingTest, FormatDouble) {
49   static const struct {
50     double number;
51     int frac_digits;
52     const char* expected_english;
53     const char* expected_german;
54   } cases[] = {
55     {0.0, 0, "0", "0"},
56 #if !BUILDFLAG(IS_ANDROID)
57     // Bionic can't printf negative zero correctly.
58     {-0.0, 4, "-0.0000", "-0,0000"},
59 #endif
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,"
69      "000.000000",
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."
76      "000,000000"},
77     {std::numeric_limits<double>::min(), 2, "0.00", "0,00"},
78     {-42.7, 3, "-42.700", "-42,700"},
79   };
80 
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)));
91   }
92 }
93 
TEST(NumberFormattingTest,FormatPercent)94 TEST(NumberFormattingTest, FormatPercent) {
95   static const struct {
96     int64_t number;
97     const char* expected_english;
98     const char* expected_german;  // Note: Space before % isn't \x20.
99     // Note: Eastern Arabic-Indic digits (U+06Fx) for Persian and
100     // Arabic-Indic digits (U+066x) for Arabic in Egypt(ar-EG). In Arabic (ar),
101     // uses European digits (Google-patch).
102     // See https://unicode.org/cldr/trac/ticket/9040 for details.
103     // See also https://unicode.org/cldr/trac/ticket/10176 .
104     // For now, take what CLDR 32 has (percent sign to the right of
105     // a number in Persian).
106     const char* expected_persian;
107     const char* expected_arabic;
108     const char* expected_arabic_egypt;
109   } cases[] = {
110       {0, "0%", "0\u00a0%", "\u06f0\u066a", "0\u200e%\u200e",
111        "\u0660\u066a\u061c"},
112       {42, "42%", "42\u00a0%", "\u06f4\u06f2\u066a", "42\u200e%\u200e",
113        "\u0664\u0662\u066a\u061c"},
114       {1024, "1,024%", "1.024\u00a0%", "\u06f1\u066c\u06f0\u06f2\u06f4\u066a",
115        "1,024\u200e%\u200e", "\u0661\u066c\u0660\u0662\u0664\u066a\u061c"},
116   };
117 
118   test::ScopedRestoreICUDefaultLocale restore_locale;
119   for (const auto& i : cases) {
120     i18n::SetICUDefaultLocale("en");
121     EXPECT_EQ(ASCIIToUTF16(i.expected_english), FormatPercent(i.number));
122     i18n::SetICUDefaultLocale("de");
123     EXPECT_EQ(UTF8ToUTF16(i.expected_german), FormatPercent(i.number));
124     i18n::SetICUDefaultLocale("fa");
125     EXPECT_EQ(UTF8ToUTF16(i.expected_persian), FormatPercent(i.number));
126     i18n::SetICUDefaultLocale("ar");
127     EXPECT_EQ(UTF8ToUTF16(i.expected_arabic), FormatPercent(i.number));
128     i18n::SetICUDefaultLocale("ar-EG");
129     EXPECT_EQ(UTF8ToUTF16(i.expected_arabic_egypt), FormatPercent(i.number));
130   }
131 }
132 
133 }  // namespace
134 }  // namespace base
135