Upload upstream chromium 67.0.3396
[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 (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)));
48   }
49 }
50
51 TEST(NumberFormattingTest, FormatDouble) {
52   static const struct {
53     double number;
54     int frac_digits;
55     const char* expected_english;
56     const char* expected_german;
57   } cases[] = {
58     {0.0, 0, "0", "0"},
59 #if !defined(OS_ANDROID)
60     // Bionic can't printf negative zero correctly.
61     {-0.0, 4, "-0.0000", "-0,0000"},
62 #endif
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,232,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,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,"
72         "000.000000",
73         "179.769.313.486.232.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.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."
79         "000,000000"},
80     {std::numeric_limits<double>::min(), 2, "0.00", "0,00"},
81     {-42.7, 3, "-42.700", "-42,700"},
82   };
83
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)));
94   }
95 }
96
97 TEST(NumberFormattingTest, FormatPercent) {
98   static const struct {
99     int64_t number;
100     const char* expected_english;
101     const wchar_t* 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.
104     // See https://unicode.org/cldr/trac/ticket/9040 for details.
105     // See also https://unicode.org/cldr/trac/ticket/10176 .
106     // For now, take what CLDR 32 has (percent sign to the right of
107     // a number in Persian).
108     const wchar_t* expected_persian;
109     const wchar_t* expected_arabic;
110   } cases[] = {
111     {0, "0%", L"0\xa0%", L"\x6f0\x66a", L"\x660\xa0\x66a\x61c"},
112     {42, "42%", L"42\xa0%", L"\x6f4\x6f2\x66a",
113         L"\x664\x662\xa0\x66a\x61c"},
114     {1024, "1,024%", L"1.024\xa0%",
115         L"\x6f1\x66c\x6f0\x6f2\x6f4\x66a",
116         L"\x661\x66c\x660\x662\x664\xa0\x66a\x61c"},
117   };
118
119   test::ScopedRestoreICUDefaultLocale restore_locale;
120   for (size_t i = 0; i < arraysize(cases); ++i) {
121     i18n::SetICUDefaultLocale("en");
122     EXPECT_EQ(ASCIIToUTF16(cases[i].expected_english),
123               FormatPercent(cases[i].number));
124     i18n::SetICUDefaultLocale("de");
125     EXPECT_EQ(WideToUTF16(cases[i].expected_german),
126               FormatPercent(cases[i].number));
127     i18n::SetICUDefaultLocale("fa");
128     EXPECT_EQ(WideToUTF16(cases[i].expected_persian),
129               FormatPercent(cases[i].number));
130     i18n::SetICUDefaultLocale("ar");
131     EXPECT_EQ(WideToUTF16(cases[i].expected_arabic),
132               FormatPercent(cases[i].number));
133   }
134 }
135
136 }  // namespace
137 }  // namespace base