Roll src/third_party/WebKit 5bc05e9:3d59927 (svn 202625:202627)
[chromium-blink-merge.git] / base / i18n / number_formatting_unittest.cc
blobdc6de2bbb6dc0b96b01d430d8b1957a407274f09
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.
5 #include <limits>
7 #include "base/i18n/number_formatting.h"
8 #include "base/i18n/rtl.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/test/icu_test_util.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/icu/source/i18n/unicode/usearch.h"
14 namespace base {
15 namespace {
17 TEST(NumberFormattingTest, FormatNumber) {
18 static const struct {
19 int64 number;
20 const char* expected_english;
21 const char* expected_german;
22 } cases[] = {
23 {0, "0", "0"},
24 {1024, "1,024", "1.024"},
25 {std::numeric_limits<int64>::max(),
26 "9,223,372,036,854,775,807", "9.223.372.036.854.775.807"},
27 {std::numeric_limits<int64>::min(),
28 "-9,223,372,036,854,775,808", "-9.223.372.036.854.775.808"},
29 {-42, "-42", "-42"},
32 test::ScopedRestoreICUDefaultLocale restore_locale;
34 for (size_t i = 0; i < arraysize(cases); ++i) {
35 i18n::SetICUDefaultLocale("en");
36 testing::ResetFormatters();
37 EXPECT_EQ(cases[i].expected_english,
38 UTF16ToUTF8(FormatNumber(cases[i].number)));
39 i18n::SetICUDefaultLocale("de");
40 testing::ResetFormatters();
41 EXPECT_EQ(cases[i].expected_german,
42 UTF16ToUTF8(FormatNumber(cases[i].number)));
46 TEST(NumberFormattingTest, FormatDouble) {
47 static const struct {
48 double number;
49 int frac_digits;
50 const char* expected_english;
51 const char* expected_german;
52 } cases[] = {
53 {0.0, 0, "0", "0"},
54 #if !defined(OS_ANDROID)
55 // Bionic can't printf negative zero correctly.
56 {-0.0, 4, "-0.0000", "-0,0000"},
57 #endif
58 {1024.2, 0, "1,024", "1.024"},
59 {-1024.223, 2, "-1,024.22", "-1.024,22"},
60 {std::numeric_limits<double>::max(), 6,
61 "179,769,313,486,232,000,000,000,000,000,000,000,000,000,000,000,000,"
62 "000,000,000,000,000,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.000000",
68 "179.769.313.486.232.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.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,000000"},
75 {std::numeric_limits<double>::min(), 2, "0.00", "0,00"},
76 {-42.7, 3, "-42.700", "-42,700"},
79 test::ScopedRestoreICUDefaultLocale restore_locale;
80 for (size_t i = 0; i < arraysize(cases); ++i) {
81 i18n::SetICUDefaultLocale("en");
82 testing::ResetFormatters();
83 EXPECT_EQ(cases[i].expected_english,
84 UTF16ToUTF8(FormatDouble(cases[i].number, cases[i].frac_digits)));
85 i18n::SetICUDefaultLocale("de");
86 testing::ResetFormatters();
87 EXPECT_EQ(cases[i].expected_german,
88 UTF16ToUTF8(FormatDouble(cases[i].number, cases[i].frac_digits)));
92 } // namespace
93 } // namespace base