1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183

base / i18n / number_formatting_unittest.cc [blame]

// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stddef.h>
#include <stdint.h>

#include <limits>

#include "base/i18n/number_formatting.h"
#include "base/i18n/rtl.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/icu_test_util.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/icu/source/i18n/unicode/usearch.h"

namespace base {
namespace {

TEST(NumberFormattingTest, FormatNumber) {
  static const struct {
    int64_t number;
    const char* expected_english;
    const char* expected_german;
  } cases[] = {
    {0, "0", "0"},
    {1024, "1,024", "1.024"},
    {std::numeric_limits<int64_t>::max(),
        "9,223,372,036,854,775,807", "9.223.372.036.854.775.807"},
    {std::numeric_limits<int64_t>::min(),
        "-9,223,372,036,854,775,808", "-9.223.372.036.854.775.808"},
    {-42, "-42", "-42"},
  };

  test::ScopedRestoreICUDefaultLocale restore_locale;

  for (const auto& i : cases) {
    i18n::SetICUDefaultLocale("en");
    ResetFormattersForTesting();
    EXPECT_EQ(i.expected_english, UTF16ToUTF8(FormatNumber(i.number)));
    i18n::SetICUDefaultLocale("de");
    ResetFormattersForTesting();
    EXPECT_EQ(i.expected_german, UTF16ToUTF8(FormatNumber(i.number)));
  }
}

TEST(NumberFormattingTest, FormatDoubleWithFixedFractionalDigits) {
  static const struct {
    double number;
    int frac_digits;
    const char* expected_english;
    const char* expected_german;
  } cases[] = {
    {0.0, 0, "0", "0"},
#if !BUILDFLAG(IS_ANDROID)
    // Bionic can't printf negative zero correctly.
    {-0.0, 4, "-0.0000", "-0,0000"},
#endif
    {1024.2, 0, "1,024", "1.024"},
    {-1024.223, 2, "-1,024.22", "-1.024,22"},
    {std::numeric_limits<double>::max(), 6,
     "179,769,313,486,231,570,000,000,000,000,000,000,000,000,000,000,000,"
     "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
     "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
     "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
     "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
     "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
     "000.000000",
     "179.769.313.486.231.570.000.000.000.000.000.000.000.000.000.000.000."
     "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
     "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
     "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
     "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
     "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
     "000,000000"},
    {std::numeric_limits<double>::min(), 2, "0.00", "0,00"},
    {-42.7, 3, "-42.700", "-42,700"},
  };

  test::ScopedRestoreICUDefaultLocale restore_locale;
  for (const auto& i : cases) {
    i18n::SetICUDefaultLocale("en");
    ResetFormattersForTesting();
    EXPECT_EQ(i.expected_english,
              UTF16ToUTF8(FormatDouble(i.number, i.frac_digits)));
    i18n::SetICUDefaultLocale("de");
    ResetFormattersForTesting();
    EXPECT_EQ(i.expected_german,
              UTF16ToUTF8(FormatDouble(i.number, i.frac_digits)));
  }
}

TEST(NumberFormattingTest, FormatDoubleWithFractionalDigitRange) {
  static const struct {
    double number;
    int min_frac_digits;
    int max_frac_digits;
    const char* expected_english;
    const char* expected_german;
  } cases[] = {
    {0.0, 0, 0, "0", "0"},
#if !BUILDFLAG(IS_ANDROID)
    // Bionic can't printf negative zero correctly.
    {-0.0, 0, 4, "-0", "-0"},
#endif
    {1024.2, 0, 0, "1,024", "1.024"},
    {-1024.223, 0, 2, "-1,024.22", "-1.024,22"},
    {std::numeric_limits<double>::max(), 0, 6,
     "179,769,313,486,231,570,000,000,000,000,000,000,000,000,000,000,000,"
     "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
     "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
     "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
     "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
     "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
     "000",
     "179.769.313.486.231.570.000.000.000.000.000.000.000.000.000.000.000."
     "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
     "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
     "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
     "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
     "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
     "000"},
    {std::numeric_limits<double>::min(), 2, 2, "0.00", "0,00"},
    {-42.7, 0, 3, "-42.7", "-42,7"},
  };

  test::ScopedRestoreICUDefaultLocale restore_locale;
  for (const auto& i : cases) {
    i18n::SetICUDefaultLocale("en");
    ResetFormattersForTesting();
    EXPECT_EQ(i.expected_english,
              UTF16ToUTF8(FormatDouble(i.number, i.min_frac_digits,
                                       i.max_frac_digits)));
    i18n::SetICUDefaultLocale("de");
    ResetFormattersForTesting();
    EXPECT_EQ(i.expected_german,
              UTF16ToUTF8(FormatDouble(i.number, i.min_frac_digits,
                                       i.max_frac_digits)));
  }
}

TEST(NumberFormattingTest, FormatPercent) {
  static const struct {
    int64_t number;
    const char* expected_english;
    const char* expected_german;  // Note: Space before % isn't \x20.
    // Note: Eastern Arabic-Indic digits (U+06Fx) for Persian and
    // Arabic-Indic digits (U+066x) for Arabic in Egypt(ar-EG). In Arabic (ar),
    // uses European digits (Google-patch).
    // See https://unicode.org/cldr/trac/ticket/9040 for details.
    // See also https://unicode.org/cldr/trac/ticket/10176 .
    // For now, take what CLDR 32 has (percent sign to the right of
    // a number in Persian).
    const char* expected_persian;
    const char* expected_arabic;
    const char* expected_arabic_egypt;
  } cases[] = {
      {0, "0%", "0\u00a0%", "\u06f0\u066a", "0\u200e%\u200e",
       "\u0660\u066a\u061c"},
      {42, "42%", "42\u00a0%", "\u06f4\u06f2\u066a", "42\u200e%\u200e",
       "\u0664\u0662\u066a\u061c"},
      {1024, "1,024%", "1.024\u00a0%", "\u06f1\u066c\u06f0\u06f2\u06f4\u066a",
       "1,024\u200e%\u200e", "\u0661\u066c\u0660\u0662\u0664\u066a\u061c"},
  };

  test::ScopedRestoreICUDefaultLocale restore_locale;
  for (const auto& i : cases) {
    i18n::SetICUDefaultLocale("en");
    EXPECT_EQ(ASCIIToUTF16(i.expected_english), FormatPercent(i.number));
    i18n::SetICUDefaultLocale("de");
    EXPECT_EQ(UTF8ToUTF16(i.expected_german), FormatPercent(i.number));
    i18n::SetICUDefaultLocale("fa");
    EXPECT_EQ(UTF8ToUTF16(i.expected_persian), FormatPercent(i.number));
    i18n::SetICUDefaultLocale("ar");
    EXPECT_EQ(UTF8ToUTF16(i.expected_arabic), FormatPercent(i.number));
    i18n::SetICUDefaultLocale("ar-EG");
    EXPECT_EQ(UTF8ToUTF16(i.expected_arabic_egypt), FormatPercent(i.number));
  }
}

}  // namespace
}  // namespace base