Chromium Code Reviews| 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/i18n/rtl.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "third_party/icu/source/i18n/unicode/usearch.h" | |
| 11 | 12 |
| 12 namespace base { | 13 namespace base { |
| 13 namespace { | 14 namespace { |
| 14 | 15 |
| 16 class ScopedSetICUDefaultLocale { | |
|
Mark Mentovai
2015/08/18 13:18:14
You shouldn’t have to write this class in every fi
| |
| 17 std::string default_locale; | |
| 18 | |
| 19 public: | |
| 20 ScopedSetICUDefaultLocale(std::string locale); | |
| 21 ~ScopedSetICUDefaultLocale(); | |
| 22 }; | |
| 23 | |
| 24 ScopedSetICUDefaultLocale::ScopedSetICUDefaultLocale(std::string locale) | |
| 25 : default_locale(locale) {} | |
| 26 | |
| 27 ScopedSetICUDefaultLocale::~ScopedSetICUDefaultLocale() { | |
| 28 i18n::SetICUDefaultLocale(default_locale.data()); | |
| 29 } | |
| 30 | |
| 15 TEST(NumberFormattingTest, FormatNumber) { | 31 TEST(NumberFormattingTest, FormatNumber) { |
| 16 static const struct { | 32 static const struct { |
| 17 int64 number; | 33 int64 number; |
| 18 const char* expected_english; | 34 const char* expected_english; |
| 19 const char* expected_german; | 35 const char* expected_german; |
| 20 } cases[] = { | 36 } cases[] = { |
| 21 {0, "0", "0"}, | 37 {0, "0", "0"}, |
| 22 {1024, "1,024", "1.024"}, | 38 {1024, "1,024", "1.024"}, |
| 23 {std::numeric_limits<int64>::max(), | 39 {std::numeric_limits<int64>::max(), |
| 24 "9,223,372,036,854,775,807", "9.223.372.036.854.775.807"}, | 40 "9,223,372,036,854,775,807", "9.223.372.036.854.775.807"}, |
| 25 {std::numeric_limits<int64>::min(), | 41 {std::numeric_limits<int64>::min(), |
| 26 "-9,223,372,036,854,775,808", "-9.223.372.036.854.775.808"}, | 42 "-9,223,372,036,854,775,808", "-9.223.372.036.854.775.808"}, |
| 27 {-42, "-42", "-42"}, | 43 {-42, "-42", "-42"}, |
| 28 }; | 44 }; |
| 29 | 45 |
| 46 ScopedSetICUDefaultLocale restore_object(uloc_getDefault()); | |
| 47 | |
| 30 for (size_t i = 0; i < arraysize(cases); ++i) { | 48 for (size_t i = 0; i < arraysize(cases); ++i) { |
| 31 i18n::SetICUDefaultLocale("en"); | 49 i18n::SetICUDefaultLocale("en"); |
| 32 testing::ResetFormatters(); | 50 testing::ResetFormatters(); |
| 33 EXPECT_EQ(cases[i].expected_english, | 51 EXPECT_EQ(cases[i].expected_english, |
| 34 UTF16ToUTF8(FormatNumber(cases[i].number))); | 52 UTF16ToUTF8(FormatNumber(cases[i].number))); |
| 35 i18n::SetICUDefaultLocale("de"); | 53 i18n::SetICUDefaultLocale("de"); |
| 36 testing::ResetFormatters(); | 54 testing::ResetFormatters(); |
| 37 EXPECT_EQ(cases[i].expected_german, | 55 EXPECT_EQ(cases[i].expected_german, |
| 38 UTF16ToUTF8(FormatNumber(cases[i].number))); | 56 UTF16ToUTF8(FormatNumber(cases[i].number))); |
| 39 } | 57 } |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 65 "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000." | 83 "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." | 84 "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." | 85 "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." | 86 "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." | 87 "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000." |
| 70 "000,000000"}, | 88 "000,000000"}, |
| 71 {std::numeric_limits<double>::min(), 2, "0.00", "0,00"}, | 89 {std::numeric_limits<double>::min(), 2, "0.00", "0,00"}, |
| 72 {-42.7, 3, "-42.700", "-42,700"}, | 90 {-42.7, 3, "-42.700", "-42,700"}, |
| 73 }; | 91 }; |
| 74 | 92 |
| 93 ScopedSetICUDefaultLocale restore_object(uloc_getDefault()); | |
| 75 for (size_t i = 0; i < arraysize(cases); ++i) { | 94 for (size_t i = 0; i < arraysize(cases); ++i) { |
| 76 i18n::SetICUDefaultLocale("en"); | 95 i18n::SetICUDefaultLocale("en"); |
| 77 testing::ResetFormatters(); | 96 testing::ResetFormatters(); |
| 78 EXPECT_EQ(cases[i].expected_english, | 97 EXPECT_EQ(cases[i].expected_english, |
| 79 UTF16ToUTF8(FormatDouble(cases[i].number, cases[i].frac_digits))); | 98 UTF16ToUTF8(FormatDouble(cases[i].number, cases[i].frac_digits))); |
| 80 i18n::SetICUDefaultLocale("de"); | 99 i18n::SetICUDefaultLocale("de"); |
| 81 testing::ResetFormatters(); | 100 testing::ResetFormatters(); |
| 82 EXPECT_EQ(cases[i].expected_german, | 101 EXPECT_EQ(cases[i].expected_german, |
| 83 UTF16ToUTF8(FormatDouble(cases[i].number, cases[i].frac_digits))); | 102 UTF16ToUTF8(FormatDouble(cases[i].number, cases[i].frac_digits))); |
| 84 } | 103 } |
| 85 } | 104 } |
| 86 | 105 |
| 87 } // namespace | 106 } // namespace |
| 88 } // namespace base | 107 } // namespace base |
| OLD | NEW |