case_conversion_unittest.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "base/i18n/case_conversion.h"
  5. #include "base/i18n/rtl.h"
  6. #include "base/strings/utf_string_conversions.h"
  7. #include "base/test/icu_test_util.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. #include "third_party/icu/source/i18n/unicode/usearch.h"
  10. namespace base {
  11. namespace i18n {
  12. namespace {
  13. const char16_t kNonASCIIMixed[] =
  14. u"\xC4\xD6\xE4\xF6\x20\xCF\xEF\x20\xF7\x25"
  15. u"\xA4\x23\x2A\x5E\x60\x40\xA3\x24\x2030\x201A\x7E\x20\x1F07\x1F0F"
  16. u"\x20\x1E00\x1E01";
  17. const char16_t kNonASCIILower[] =
  18. u"\xE4\xF6\xE4\xF6\x20\xEF\xEF"
  19. u"\x20\xF7\x25\xA4\x23\x2A\x5E\x60\x40\xA3\x24\x2030\x201A\x7E\x20\x1F07"
  20. u"\x1F07\x20\x1E01\x1E01";
  21. const char16_t kNonASCIIUpper[] =
  22. u"\xC4\xD6\xC4\xD6\x20\xCF\xCF"
  23. u"\x20\xF7\x25\xA4\x23\x2A\x5E\x60\x40\xA3\x24\x2030\x201A\x7E\x20\x1F0F"
  24. u"\x1F0F\x20\x1E00\x1E00";
  25. } // namespace
  26. // Test upper and lower case string conversion.
  27. TEST(CaseConversionTest, UpperLower) {
  28. const std::u16string mixed(u"Text with UPPer & lowER casE.");
  29. const std::u16string expected_lower(u"text with upper & lower case.");
  30. const std::u16string expected_upper(u"TEXT WITH UPPER & LOWER CASE.");
  31. std::u16string result = ToLower(mixed);
  32. EXPECT_EQ(expected_lower, result);
  33. result = ToUpper(mixed);
  34. EXPECT_EQ(expected_upper, result);
  35. }
  36. TEST(CaseConversionTest, NonASCII) {
  37. const std::u16string mixed(kNonASCIIMixed);
  38. const std::u16string expected_lower(kNonASCIILower);
  39. const std::u16string expected_upper(kNonASCIIUpper);
  40. std::u16string result = ToLower(mixed);
  41. EXPECT_EQ(expected_lower, result);
  42. result = ToUpper(mixed);
  43. EXPECT_EQ(expected_upper, result);
  44. }
  45. TEST(CaseConversionTest, TurkishLocaleConversion) {
  46. const std::u16string mixed(u"\x49\x131");
  47. const std::u16string expected_lower(u"\x69\x131");
  48. const std::u16string expected_upper(u"\x49\x49");
  49. test::ScopedRestoreICUDefaultLocale restore_locale;
  50. i18n::SetICUDefaultLocale("en_US");
  51. std::u16string result = ToLower(mixed);
  52. EXPECT_EQ(expected_lower, result);
  53. result = ToUpper(mixed);
  54. EXPECT_EQ(expected_upper, result);
  55. i18n::SetICUDefaultLocale("tr");
  56. const std::u16string expected_lower_turkish(u"\x131\x131");
  57. const std::u16string expected_upper_turkish(u"\x49\x49");
  58. result = ToLower(mixed);
  59. EXPECT_EQ(expected_lower_turkish, result);
  60. result = ToUpper(mixed);
  61. EXPECT_EQ(expected_upper_turkish, result);
  62. }
  63. TEST(CaseConversionTest, FoldCase) {
  64. // Simple ASCII, should lower-case.
  65. EXPECT_EQ(u"hello, world", FoldCase(u"Hello, World"));
  66. // Non-ASCII cases from above. They should all fold to the same result.
  67. EXPECT_EQ(FoldCase(kNonASCIIMixed), FoldCase(kNonASCIILower));
  68. EXPECT_EQ(FoldCase(kNonASCIIMixed), FoldCase(kNonASCIIUpper));
  69. // Turkish cases from above. This is the lower-case expected result from the
  70. // US locale. It should be the same even when the current locale is Turkish.
  71. const std::u16string turkish(u"\x49\x131");
  72. const std::u16string turkish_expected(u"\x69\x131");
  73. test::ScopedRestoreICUDefaultLocale restore_locale;
  74. i18n::SetICUDefaultLocale("en_US");
  75. EXPECT_EQ(turkish_expected, FoldCase(turkish));
  76. i18n::SetICUDefaultLocale("tr");
  77. EXPECT_EQ(turkish_expected, FoldCase(turkish));
  78. // Test a case that gets bigger when processed.
  79. // U+130 = LATIN CAPITAL LETTER I WITH DOT ABOVE gets folded to a lower case
  80. // "i" followed by U+307 COMBINING DOT ABOVE.
  81. EXPECT_EQ(u"i\u0307j", FoldCase(u"\u0130j"));
  82. // U+00DF (SHARP S) and U+1E9E (CAPIRAL SHARP S) are both folded to "ss".
  83. EXPECT_EQ(u"ssss", FoldCase(u"\u00DF\u1E9E"));
  84. }
  85. } // namespace i18n
  86. } // namespace base