Upload upstream chromium 85.0.4183.84
[platform/framework/web/chromium-efl.git] / base / i18n / number_formatting_unittest.cc
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.
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/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"
18
19 namespace base {
20 namespace {
21
22 TEST(NumberFormattingTest, FormatNumber) {
23   static const struct {
24     int64_t number;
25     const char* expected_english;
26     const char* expected_german;
27   } cases[] = {
28     {0, "0", "0"},
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"},
34     {-42, "-42", "-42"},
35   };
36
37   test::ScopedRestoreICUDefaultLocale restore_locale;
38
39   for (const auto& i : cases) {
40     i18n::SetICUDefaultLocale("en");
41     ResetFormattersForTesting();
42     EXPECT_EQ(i.expected_english, UTF16ToUTF8(FormatNumber(i.number)));
43     i18n::SetICUDefaultLocale("de");
44     ResetFormattersForTesting();
45     EXPECT_EQ(i.expected_german, UTF16ToUTF8(FormatNumber(i.number)));
46   }
47 }
48
49 TEST(NumberFormattingTest, FormatDouble) {
50   static const struct {
51     double number;
52     int frac_digits;
53     const char* expected_english;
54     const char* expected_german;
55   } cases[] = {
56     {0.0, 0, "0", "0"},
57 #if !defined(OS_ANDROID)
58     // Bionic can't printf negative zero correctly.
59     {-0.0, 4, "-0.0000", "-0,0000"},
60 #endif
61     {1024.2, 0, "1,024", "1.024"},
62     {-1024.223, 2, "-1,024.22", "-1.024,22"},
63     {std::numeric_limits<double>::max(), 6,
64      "179,769,313,486,231,570,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,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
70      "000.000000",
71      "179.769.313.486.231.570.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.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
77      "000,000000"},
78     {std::numeric_limits<double>::min(), 2, "0.00", "0,00"},
79     {-42.7, 3, "-42.700", "-42,700"},
80   };
81
82   test::ScopedRestoreICUDefaultLocale restore_locale;
83   for (const auto& i : cases) {
84     i18n::SetICUDefaultLocale("en");
85     ResetFormattersForTesting();
86     EXPECT_EQ(i.expected_english,
87               UTF16ToUTF8(FormatDouble(i.number, i.frac_digits)));
88     i18n::SetICUDefaultLocale("de");
89     ResetFormattersForTesting();
90     EXPECT_EQ(i.expected_german,
91               UTF16ToUTF8(FormatDouble(i.number, i.frac_digits)));
92   }
93 }
94
95 TEST(NumberFormattingTest, FormatPercent) {
96   static const struct {
97     int64_t number;
98     const char* expected_english;
99     const char* expected_german;  // Note: Space before % isn't \x20.
100     // Note: Eastern Arabic-Indic digits (U+06Fx) for Persian and
101     // Arabic-Indic digits (U+066x) for Arabic in Egypt(ar-EG). In Arabic (ar),
102     // uses European digits (Google-patch).
103     // See https://unicode.org/cldr/trac/ticket/9040 for details.
104     // See also https://unicode.org/cldr/trac/ticket/10176 .
105     // For now, take what CLDR 32 has (percent sign to the right of
106     // a number in Persian).
107     const char* expected_persian;
108     const char* expected_arabic;
109     const char* expected_arabic_egypt;
110   } cases[] = {
111       {0, "0%", u8"0\u00a0%", u8"\u06f0\u066a", u8"0\u200e%\u200e",
112        u8"\u0660\u066a\u061c"},
113       {42, "42%", "42\u00a0%", u8"\u06f4\u06f2\u066a", u8"42\u200e%\u200e",
114        "\u0664\u0662\u066a\u061c"},
115       {1024, "1,024%", "1.024\u00a0%", u8"\u06f1\u066c\u06f0\u06f2\u06f4\u066a",
116        "1,024\u200e%\u200e", "\u0661\u066c\u0660\u0662\u0664\u066a\u061c"},
117   };
118
119   test::ScopedRestoreICUDefaultLocale restore_locale;
120   for (const auto& i : cases) {
121     i18n::SetICUDefaultLocale("en");
122     EXPECT_EQ(ASCIIToUTF16(i.expected_english), FormatPercent(i.number));
123     i18n::SetICUDefaultLocale("de");
124     EXPECT_EQ(UTF8ToUTF16(i.expected_german), FormatPercent(i.number));
125     i18n::SetICUDefaultLocale("fa");
126     EXPECT_EQ(UTF8ToUTF16(i.expected_persian), FormatPercent(i.number));
127     i18n::SetICUDefaultLocale("ar");
128     EXPECT_EQ(UTF8ToUTF16(i.expected_arabic), FormatPercent(i.number));
129     i18n::SetICUDefaultLocale("ar-EG");
130     EXPECT_EQ(UTF8ToUTF16(i.expected_arabic_egypt), FormatPercent(i.number));
131   }
132 }
133
134 }  // namespace
135 }  // namespace base