OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <limits> | 5 #include <limits> |
6 | 6 |
7 #include "base/i18n/number_formatting.h" | 7 #include "base/i18n/number_formatting.h" |
| 8 #include "base/i18n/rtl.h" |
8 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "unicode/locid.h" | |
11 | 11 |
| 12 namespace base { |
12 namespace { | 13 namespace { |
13 | 14 |
14 void SetICUDefaultLocale(const std::string& locale_string) { | |
15 icu::Locale locale(locale_string.c_str()); | |
16 UErrorCode error_code = U_ZERO_ERROR; | |
17 icu::Locale::setDefault(locale, error_code); | |
18 EXPECT_TRUE(U_SUCCESS(error_code)); | |
19 } | |
20 | |
21 } // namespace | |
22 | |
23 TEST(NumberFormattingTest, FormatNumber) { | 15 TEST(NumberFormattingTest, FormatNumber) { |
24 static const struct { | 16 static const struct { |
25 int64 number; | 17 int64 number; |
26 const char* expected_english; | 18 const char* expected_english; |
27 const char* expected_german; | 19 const char* expected_german; |
28 } cases[] = { | 20 } cases[] = { |
29 {0, "0", "0"}, | 21 {0, "0", "0"}, |
30 {1024, "1,024", "1.024"}, | 22 {1024, "1,024", "1.024"}, |
31 {std::numeric_limits<int64>::max(), | 23 {std::numeric_limits<int64>::max(), |
32 "9,223,372,036,854,775,807", "9.223.372.036.854.775.807"}, | 24 "9,223,372,036,854,775,807", "9.223.372.036.854.775.807"}, |
33 {std::numeric_limits<int64>::min(), | 25 {std::numeric_limits<int64>::min(), |
34 "-9,223,372,036,854,775,808", "-9.223.372.036.854.775.808"}, | 26 "-9,223,372,036,854,775,808", "-9.223.372.036.854.775.808"}, |
35 {-42, "-42", "-42"}, | 27 {-42, "-42", "-42"}, |
36 }; | 28 }; |
37 | 29 |
38 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 30 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
39 SetICUDefaultLocale("en"); | 31 i18n::SetICUDefaultLocale("en"); |
40 base::testing::ResetFormatters(); | 32 testing::ResetFormatters(); |
41 EXPECT_EQ(cases[i].expected_english, | 33 EXPECT_EQ(cases[i].expected_english, |
42 UTF16ToUTF8(base::FormatNumber(cases[i].number))); | 34 UTF16ToUTF8(FormatNumber(cases[i].number))); |
43 SetICUDefaultLocale("de"); | 35 i18n::SetICUDefaultLocale("de"); |
44 base::testing::ResetFormatters(); | 36 testing::ResetFormatters(); |
45 EXPECT_EQ(cases[i].expected_german, | 37 EXPECT_EQ(cases[i].expected_german, |
46 UTF16ToUTF8(base::FormatNumber(cases[i].number))); | 38 UTF16ToUTF8(FormatNumber(cases[i].number))); |
47 } | 39 } |
48 } | 40 } |
49 | 41 |
50 TEST(NumberFormattingTest, FormatDouble) { | 42 TEST(NumberFormattingTest, FormatDouble) { |
51 static const struct { | 43 static const struct { |
52 double number; | 44 double number; |
53 int frac_digits; | 45 int frac_digits; |
54 const char* expected_english; | 46 const char* expected_english; |
55 const char* expected_german; | 47 const char* expected_german; |
56 } cases[] = { | 48 } cases[] = { |
(...skipping 17 matching lines...) Expand all Loading... |
74 "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." |
75 "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." |
76 "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." |
77 "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." |
78 "000,000000"}, | 70 "000,000000"}, |
79 {std::numeric_limits<double>::min(), 2, "0.00", "0,00"}, | 71 {std::numeric_limits<double>::min(), 2, "0.00", "0,00"}, |
80 {-42.7, 3, "-42.700", "-42,700"}, | 72 {-42.7, 3, "-42.700", "-42,700"}, |
81 }; | 73 }; |
82 | 74 |
83 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 75 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
84 SetICUDefaultLocale("en"); | 76 i18n::SetICUDefaultLocale("en"); |
85 base::testing::ResetFormatters(); | 77 testing::ResetFormatters(); |
86 EXPECT_EQ(cases[i].expected_english, | 78 EXPECT_EQ(cases[i].expected_english, |
87 UTF16ToUTF8(base::FormatDouble(cases[i].number, | 79 UTF16ToUTF8(FormatDouble(cases[i].number, cases[i].frac_digits))); |
88 cases[i].frac_digits))); | 80 i18n::SetICUDefaultLocale("de"); |
89 SetICUDefaultLocale("de"); | 81 testing::ResetFormatters(); |
90 base::testing::ResetFormatters(); | 82 EXPECT_EQ(cases[i].expected_german, |
91 EXPECT_EQ(cases[i].expected_german, | 83 UTF16ToUTF8(FormatDouble(cases[i].number, cases[i].frac_digits))); |
92 UTF16ToUTF8(base::FormatDouble(cases[i].number, | |
93 cases[i].frac_digits))); | |
94 } | 84 } |
95 } | 85 } |
| 86 |
| 87 } // namespace |
| 88 } // namespace base |
OLD | NEW |