Made WebRTC tests report JS errors better.
[chromium-blink-merge.git] / base / i18n / number_formatting_unittest.cc
blob3b0718d454f399395cb20215cad5490b6e1fb7c8
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 "testing/gtest/include/gtest/gtest.h"
12 namespace base {
13 namespace {
15 TEST(NumberFormattingTest, FormatNumber) {
16 static const struct {
17 int64 number;
18 const char* expected_english;
19 const char* expected_german;
20 } cases[] = {
21 {0, "0", "0"},
22 {1024, "1,024", "1.024"},
23 {std::numeric_limits<int64>::max(),
24 "9,223,372,036,854,775,807", "9.223.372.036.854.775.807"},
25 {std::numeric_limits<int64>::min(),
26 "-9,223,372,036,854,775,808", "-9.223.372.036.854.775.808"},
27 {-42, "-42", "-42"},
30 for (size_t i = 0; i < arraysize(cases); ++i) {
31 i18n::SetICUDefaultLocale("en");
32 testing::ResetFormatters();
33 EXPECT_EQ(cases[i].expected_english,
34 UTF16ToUTF8(FormatNumber(cases[i].number)));
35 i18n::SetICUDefaultLocale("de");
36 testing::ResetFormatters();
37 EXPECT_EQ(cases[i].expected_german,
38 UTF16ToUTF8(FormatNumber(cases[i].number)));
42 TEST(NumberFormattingTest, FormatDouble) {
43 static const struct {
44 double number;
45 int frac_digits;
46 const char* expected_english;
47 const char* expected_german;
48 } cases[] = {
49 {0.0, 0, "0", "0"},
50 #if !defined(OS_ANDROID)
51 // Bionic can't printf negative zero correctly.
52 {-0.0, 4, "-0.0000", "-0,0000"},
53 #endif
54 {1024.2, 0, "1,024", "1.024"},
55 {-1024.223, 2, "-1,024.22", "-1.024,22"},
56 {std::numeric_limits<double>::max(), 6,
57 "179,769,313,486,232,000,000,000,000,000,000,000,000,000,000,000,000,"
58 "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
59 "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
60 "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
61 "000,000,000,000,000,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.000000",
64 "179.769.313.486.232.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.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,000000"},
71 {std::numeric_limits<double>::min(), 2, "0.00", "0,00"},
72 {-42.7, 3, "-42.700", "-42,700"},
75 for (size_t i = 0; i < arraysize(cases); ++i) {
76 i18n::SetICUDefaultLocale("en");
77 testing::ResetFormatters();
78 EXPECT_EQ(cases[i].expected_english,
79 UTF16ToUTF8(FormatDouble(cases[i].number, cases[i].frac_digits)));
80 i18n::SetICUDefaultLocale("de");
81 testing::ResetFormatters();
82 EXPECT_EQ(cases[i].expected_german,
83 UTF16ToUTF8(FormatDouble(cases[i].number, cases[i].frac_digits)));
87 } // namespace
88 } // namespace base