number_formatting_unittest.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #include <stddef.h>
  5. #include <stdint.h>
  6. #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 "build/build_config.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. #include "third_party/icu/source/i18n/unicode/usearch.h"
  14. namespace base {
  15. namespace {
  16. TEST(NumberFormattingTest, FormatNumber) {
  17. static const struct {
  18. int64_t number;
  19. const char* expected_english;
  20. const char* expected_german;
  21. } cases[] = {
  22. {0, "0", "0"},
  23. {1024, "1,024", "1.024"},
  24. {std::numeric_limits<int64_t>::max(),
  25. "9,223,372,036,854,775,807", "9.223.372.036.854.775.807"},
  26. {std::numeric_limits<int64_t>::min(),
  27. "-9,223,372,036,854,775,808", "-9.223.372.036.854.775.808"},
  28. {-42, "-42", "-42"},
  29. };
  30. test::ScopedRestoreICUDefaultLocale restore_locale;
  31. for (const auto& i : cases) {
  32. i18n::SetICUDefaultLocale("en");
  33. ResetFormattersForTesting();
  34. EXPECT_EQ(i.expected_english, UTF16ToUTF8(FormatNumber(i.number)));
  35. i18n::SetICUDefaultLocale("de");
  36. ResetFormattersForTesting();
  37. EXPECT_EQ(i.expected_german, UTF16ToUTF8(FormatNumber(i.number)));
  38. }
  39. }
  40. TEST(NumberFormattingTest, FormatDouble) {
  41. static const struct {
  42. double number;
  43. int frac_digits;
  44. const char* expected_english;
  45. const char* expected_german;
  46. } cases[] = {
  47. {0.0, 0, "0", "0"},
  48. #if !BUILDFLAG(IS_ANDROID)
  49. // Bionic can't printf negative zero correctly.
  50. {-0.0, 4, "-0.0000", "-0,0000"},
  51. #endif
  52. {1024.2, 0, "1,024", "1.024"},
  53. {-1024.223, 2, "-1,024.22", "-1.024,22"},
  54. {std::numeric_limits<double>::max(), 6,
  55. "179,769,313,486,231,570,000,000,000,000,000,000,000,000,000,000,000,"
  56. "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
  57. "000,000,000,000,000,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.000000",
  62. "179.769.313.486.231.570.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. {std::numeric_limits<double>::min(), 2, "0.00", "0,00"},
  70. {-42.7, 3, "-42.700", "-42,700"},
  71. };
  72. test::ScopedRestoreICUDefaultLocale restore_locale;
  73. for (const auto& i : cases) {
  74. i18n::SetICUDefaultLocale("en");
  75. ResetFormattersForTesting();
  76. EXPECT_EQ(i.expected_english,
  77. UTF16ToUTF8(FormatDouble(i.number, i.frac_digits)));
  78. i18n::SetICUDefaultLocale("de");
  79. ResetFormattersForTesting();
  80. EXPECT_EQ(i.expected_german,
  81. UTF16ToUTF8(FormatDouble(i.number, i.frac_digits)));
  82. }
  83. }
  84. TEST(NumberFormattingTest, FormatPercent) {
  85. static const struct {
  86. int64_t number;
  87. const char* expected_english;
  88. const char* expected_german; // Note: Space before % isn't \x20.
  89. // Note: Eastern Arabic-Indic digits (U+06Fx) for Persian and
  90. // Arabic-Indic digits (U+066x) for Arabic in Egypt(ar-EG). In Arabic (ar),
  91. // uses European digits (Google-patch).
  92. // See https://unicode.org/cldr/trac/ticket/9040 for details.
  93. // See also https://unicode.org/cldr/trac/ticket/10176 .
  94. // For now, take what CLDR 32 has (percent sign to the right of
  95. // a number in Persian).
  96. const char* expected_persian;
  97. const char* expected_arabic;
  98. const char* expected_arabic_egypt;
  99. } cases[] = {
  100. {0, "0%", "0\u00a0%", "\u06f0\u066a", "0\u200e%\u200e",
  101. "\u0660\u066a\u061c"},
  102. {42, "42%", "42\u00a0%", "\u06f4\u06f2\u066a", "42\u200e%\u200e",
  103. "\u0664\u0662\u066a\u061c"},
  104. {1024, "1,024%", "1.024\u00a0%", "\u06f1\u066c\u06f0\u06f2\u06f4\u066a",
  105. "1,024\u200e%\u200e", "\u0661\u066c\u0660\u0662\u0664\u066a\u061c"},
  106. };
  107. test::ScopedRestoreICUDefaultLocale restore_locale;
  108. for (const auto& i : cases) {
  109. i18n::SetICUDefaultLocale("en");
  110. EXPECT_EQ(ASCIIToUTF16(i.expected_english), FormatPercent(i.number));
  111. i18n::SetICUDefaultLocale("de");
  112. EXPECT_EQ(UTF8ToUTF16(i.expected_german), FormatPercent(i.number));
  113. i18n::SetICUDefaultLocale("fa");
  114. EXPECT_EQ(UTF8ToUTF16(i.expected_persian), FormatPercent(i.number));
  115. i18n::SetICUDefaultLocale("ar");
  116. EXPECT_EQ(UTF8ToUTF16(i.expected_arabic), FormatPercent(i.number));
  117. i18n::SetICUDefaultLocale("ar-EG");
  118. EXPECT_EQ(UTF8ToUTF16(i.expected_arabic_egypt), FormatPercent(i.number));
  119. }
  120. }
  121. } // namespace
  122. } // namespace base