OLD | NEW |
(Empty) | |
| 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 <limits> |
| 6 |
| 7 #include "base/i18n/number_formatting.h" |
| 8 #include "base/utf_string_conversions.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "unicode/locid.h" |
| 11 |
| 12 namespace { |
| 13 |
| 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) { |
| 24 static const struct { |
| 25 int64 number; |
| 26 const char* expected_english; |
| 27 const char* expected_german; |
| 28 } cases[] = { |
| 29 {0, "0", "0"}, |
| 30 {1024, "1,024", "1.024"}, |
| 31 {std::numeric_limits<int64>::max(), |
| 32 "9,223,372,036,854,775,807", "9.223.372.036.854.775.807"}, |
| 33 {std::numeric_limits<int64>::min(), |
| 34 "-9,223,372,036,854,775,808", "-9.223.372.036.854.775.808"}, |
| 35 {-42, "-42", "-42"}, |
| 36 }; |
| 37 |
| 38 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 39 SetICUDefaultLocale("en"); |
| 40 base::testing::ResetFormatters(); |
| 41 EXPECT_EQ(cases[i].expected_english, |
| 42 UTF16ToUTF8(base::FormatNumber(cases[i].number))); |
| 43 SetICUDefaultLocale("de"); |
| 44 base::testing::ResetFormatters(); |
| 45 EXPECT_EQ(cases[i].expected_german, |
| 46 UTF16ToUTF8(base::FormatNumber(cases[i].number))); |
| 47 } |
| 48 } |
| 49 |
| 50 TEST(NumberFormattingTest, FormatDouble) { |
| 51 static const struct { |
| 52 double number; |
| 53 int frac_digits; |
| 54 const char* expected_english; |
| 55 const char* expected_german; |
| 56 } cases[] = { |
| 57 {0.0, 0, "0", "0"}, |
| 58 {-0.0, 4, "-0.0000", "-0,0000"}, |
| 59 {1024.2, 0, "1,024", "1.024"}, |
| 60 {-1024.223, 2, "-1,024.22", "-1.024,22"}, |
| 61 {std::numeric_limits<double>::max(), 6, |
| 62 "179,769,313,486,232,000,000,000,000,000,000,000,000,000,000,000,000," |
| 63 "000,000,000,000,000,000,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.000000", |
| 69 "179.769.313.486.232.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.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,000000"}, |
| 76 {std::numeric_limits<double>::min(), 2, "0.00", "0,00"}, |
| 77 {-42.7, 3, "-42.700", "-42,700"}, |
| 78 }; |
| 79 |
| 80 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 81 SetICUDefaultLocale("en"); |
| 82 base::testing::ResetFormatters(); |
| 83 EXPECT_EQ(cases[i].expected_english, |
| 84 UTF16ToUTF8(base::FormatDouble(cases[i].number, |
| 85 cases[i].frac_digits))); |
| 86 SetICUDefaultLocale("de"); |
| 87 base::testing::ResetFormatters(); |
| 88 EXPECT_EQ(cases[i].expected_german, |
| 89 UTF16ToUTF8(base::FormatDouble(cases[i].number, |
| 90 cases[i].frac_digits))); |
| 91 } |
| 92 } |
OLD | NEW |