xref: /aosp_15_r20/external/cronet/base/i18n/case_conversion_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/i18n/case_conversion.h"
6 #include "base/i18n/rtl.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "base/test/icu_test_util.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/icu/source/i18n/unicode/usearch.h"
11 
12 namespace base {
13 namespace i18n {
14 
15 namespace {
16 
17 const char16_t kNonASCIIMixed[] =
18     u"\xC4\xD6\xE4\xF6\x20\xCF\xEF\x20\xF7\x25"
19     u"\xA4\x23\x2A\x5E\x60\x40\xA3\x24\x2030\x201A\x7E\x20\x1F07\x1F0F"
20     u"\x20\x1E00\x1E01";
21 const char16_t kNonASCIILower[] =
22     u"\xE4\xF6\xE4\xF6\x20\xEF\xEF"
23     u"\x20\xF7\x25\xA4\x23\x2A\x5E\x60\x40\xA3\x24\x2030\x201A\x7E\x20\x1F07"
24     u"\x1F07\x20\x1E01\x1E01";
25 const char16_t kNonASCIIUpper[] =
26     u"\xC4\xD6\xC4\xD6\x20\xCF\xCF"
27     u"\x20\xF7\x25\xA4\x23\x2A\x5E\x60\x40\xA3\x24\x2030\x201A\x7E\x20\x1F0F"
28     u"\x1F0F\x20\x1E00\x1E00";
29 
30 }  // namespace
31 
32 // Test upper and lower case string conversion.
TEST(CaseConversionTest,UpperLower)33 TEST(CaseConversionTest, UpperLower) {
34   const std::u16string mixed(u"Text with UPPer & lowER casE.");
35   const std::u16string expected_lower(u"text with upper & lower case.");
36   const std::u16string expected_upper(u"TEXT WITH UPPER & LOWER CASE.");
37 
38   std::u16string result = ToLower(mixed);
39   EXPECT_EQ(expected_lower, result);
40 
41   result = ToUpper(mixed);
42   EXPECT_EQ(expected_upper, result);
43 }
44 
TEST(CaseConversionTest,NonASCII)45 TEST(CaseConversionTest, NonASCII) {
46   const std::u16string mixed(kNonASCIIMixed);
47   const std::u16string expected_lower(kNonASCIILower);
48   const std::u16string expected_upper(kNonASCIIUpper);
49 
50   std::u16string result = ToLower(mixed);
51   EXPECT_EQ(expected_lower, result);
52 
53   result = ToUpper(mixed);
54   EXPECT_EQ(expected_upper, result);
55 }
56 
TEST(CaseConversionTest,TurkishLocaleConversion)57 TEST(CaseConversionTest, TurkishLocaleConversion) {
58   const std::u16string mixed(u"\x49\x131");
59   const std::u16string expected_lower(u"\x69\x131");
60   const std::u16string expected_upper(u"\x49\x49");
61 
62   test::ScopedRestoreICUDefaultLocale restore_locale;
63   i18n::SetICUDefaultLocale("en_US");
64 
65   std::u16string result = ToLower(mixed);
66   EXPECT_EQ(expected_lower, result);
67 
68   result = ToUpper(mixed);
69   EXPECT_EQ(expected_upper, result);
70 
71   i18n::SetICUDefaultLocale("tr");
72 
73   const std::u16string expected_lower_turkish(u"\x131\x131");
74   const std::u16string expected_upper_turkish(u"\x49\x49");
75 
76   result = ToLower(mixed);
77   EXPECT_EQ(expected_lower_turkish, result);
78 
79   result = ToUpper(mixed);
80   EXPECT_EQ(expected_upper_turkish, result);
81 }
82 
TEST(CaseConversionTest,FoldCase)83 TEST(CaseConversionTest, FoldCase) {
84   // Simple ASCII, should lower-case.
85   EXPECT_EQ(u"hello, world", FoldCase(u"Hello, World"));
86 
87   // Non-ASCII cases from above. They should all fold to the same result.
88   EXPECT_EQ(FoldCase(kNonASCIIMixed), FoldCase(kNonASCIILower));
89   EXPECT_EQ(FoldCase(kNonASCIIMixed), FoldCase(kNonASCIIUpper));
90 
91   // Turkish cases from above. This is the lower-case expected result from the
92   // US locale. It should be the same even when the current locale is Turkish.
93   const std::u16string turkish(u"\x49\x131");
94   const std::u16string turkish_expected(u"\x69\x131");
95 
96   test::ScopedRestoreICUDefaultLocale restore_locale;
97   i18n::SetICUDefaultLocale("en_US");
98   EXPECT_EQ(turkish_expected, FoldCase(turkish));
99 
100   i18n::SetICUDefaultLocale("tr");
101   EXPECT_EQ(turkish_expected, FoldCase(turkish));
102 
103   // Test a case that gets bigger when processed.
104   // U+130 = LATIN CAPITAL LETTER I WITH DOT ABOVE gets folded to a lower case
105   // "i" followed by U+307 COMBINING DOT ABOVE.
106   EXPECT_EQ(u"i\u0307j", FoldCase(u"\u0130j"));
107 
108   // U+00DF (SHARP S) and U+1E9E (CAPIRAL SHARP S) are both folded to "ss".
109   EXPECT_EQ(u"ssss", FoldCase(u"\u00DF\u1E9E"));
110 }
111 
112 }  // namespace i18n
113 }  // namespace base
114 
115 
116 
117