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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gtest/gtest.h"
#include "mozilla/intl/String.h"
#include "mozilla/Span.h"
#include "mozilla/TextUtils.h"
#include "mozilla/Try.h"
#include <algorithm>
#include "TestBuffer.h"
namespace mozilla::intl {
static Result<std::u16string_view, ICUError> ToLocaleLowerCase(
const char* aLocale, const char16_t* aString,
TestBuffer<char16_t>& aBuffer) {
aBuffer.clear();
MOZ_TRY(String::ToLocaleLowerCase(aLocale, MakeStringSpan(aString), aBuffer));
return aBuffer.get_string_view();
}
static Result<std::u16string_view, ICUError> ToLocaleUpperCase(
const char* aLocale, const char16_t* aString,
TestBuffer<char16_t>& aBuffer) {
aBuffer.clear();
MOZ_TRY(String::ToLocaleUpperCase(aLocale, MakeStringSpan(aString), aBuffer));
return aBuffer.get_string_view();
}
TEST(IntlString, ToLocaleLowerCase)
{
TestBuffer<char16_t> buf;
ASSERT_EQ(ToLocaleLowerCase("en", u"test", buf).unwrap(), u"test");
ASSERT_EQ(ToLocaleLowerCase("en", u"TEST", buf).unwrap(), u"test");
// Turkish dotless i.
ASSERT_EQ(ToLocaleLowerCase("tr", u"I", buf).unwrap(), u"ı");
ASSERT_EQ(ToLocaleLowerCase("tr", u"İ", buf).unwrap(), u"i");
ASSERT_EQ(ToLocaleLowerCase("tr", u"I\u0307", buf).unwrap(), u"i");
}
TEST(IntlString, ToLocaleUpperCase)
{
TestBuffer<char16_t> buf;
ASSERT_EQ(ToLocaleUpperCase("en", u"test", buf).unwrap(), u"TEST");
ASSERT_EQ(ToLocaleUpperCase("en", u"TEST", buf).unwrap(), u"TEST");
// Turkish dotless i.
ASSERT_EQ(ToLocaleUpperCase("tr", u"i", buf).unwrap(), u"İ");
ASSERT_EQ(ToLocaleUpperCase("tr", u"ı", buf).unwrap(), u"I");
// Output can be longer than the input string.
ASSERT_EQ(ToLocaleUpperCase("en", u"Größenmaßstäbe", buf).unwrap(),
u"GRÖSSENMASSSTÄBE");
}
TEST(IntlString, NormalizeNFC)
{
using namespace std::literals;
using NormalizationForm = String::NormalizationForm;
using AlreadyNormalized = String::AlreadyNormalized;
TestBuffer<char16_t> buf;
auto alreadyNormalized =
String::Normalize(NormalizationForm::NFC, u""sv, buf);
ASSERT_EQ(alreadyNormalized.unwrap(), AlreadyNormalized::Yes);
ASSERT_EQ(buf.get_string_view(), u"");
alreadyNormalized =
String::Normalize(NormalizationForm::NFC, u"abcdef"sv, buf);
ASSERT_EQ(alreadyNormalized.unwrap(), AlreadyNormalized::Yes);
ASSERT_EQ(buf.get_string_view(), u"");
alreadyNormalized =
String::Normalize(NormalizationForm::NFC, u"a\u0308"sv, buf);
ASSERT_EQ(alreadyNormalized.unwrap(), AlreadyNormalized::No);
ASSERT_EQ(buf.get_string_view(), u"ä");
buf.clear();
alreadyNormalized = String::Normalize(NormalizationForm::NFC, u"½"sv, buf);
ASSERT_EQ(alreadyNormalized.unwrap(), AlreadyNormalized::Yes);
ASSERT_EQ(buf.get_string_view(), u"");
}
TEST(IntlString, NormalizeNFD)
{
using namespace std::literals;
using NormalizationForm = String::NormalizationForm;
using AlreadyNormalized = String::AlreadyNormalized;
TestBuffer<char16_t> buf;
auto alreadyNormalized =
String::Normalize(NormalizationForm::NFD, u""sv, buf);
ASSERT_EQ(alreadyNormalized.unwrap(), AlreadyNormalized::Yes);
ASSERT_EQ(buf.get_string_view(), u"");
alreadyNormalized =
String::Normalize(NormalizationForm::NFD, u"abcdef"sv, buf);
ASSERT_EQ(alreadyNormalized.unwrap(), AlreadyNormalized::Yes);
ASSERT_EQ(buf.get_string_view(), u"");
alreadyNormalized = String::Normalize(NormalizationForm::NFD, u"ä"sv, buf);
ASSERT_EQ(alreadyNormalized.unwrap(), AlreadyNormalized::No);
ASSERT_EQ(buf.get_string_view(), u"a\u0308");
buf.clear();
alreadyNormalized = String::Normalize(NormalizationForm::NFD, u"½"sv, buf);
ASSERT_EQ(alreadyNormalized.unwrap(), AlreadyNormalized::Yes);
ASSERT_EQ(buf.get_string_view(), u"");
// Test with inline capacity.
TestBuffer<char16_t, 2> buf2;
alreadyNormalized = String::Normalize(NormalizationForm::NFD, u" ç"sv, buf2);
ASSERT_EQ(alreadyNormalized.unwrap(), AlreadyNormalized::No);
ASSERT_EQ(buf2.get_string_view(), u" c\u0327");
}
TEST(IntlString, NormalizeNFKC)
{
using namespace std::literals;
using NormalizationForm = String::NormalizationForm;
using AlreadyNormalized = String::AlreadyNormalized;
TestBuffer<char16_t> buf;
auto alreadyNormalized =
String::Normalize(NormalizationForm::NFKC, u""sv, buf);
ASSERT_EQ(alreadyNormalized.unwrap(), AlreadyNormalized::Yes);
ASSERT_EQ(buf.get_string_view(), u"");
alreadyNormalized =
String::Normalize(NormalizationForm::NFKC, u"abcdef"sv, buf);
ASSERT_EQ(alreadyNormalized.unwrap(), AlreadyNormalized::Yes);
ASSERT_EQ(buf.get_string_view(), u"");
alreadyNormalized =
String::Normalize(NormalizationForm::NFKC, u"a\u0308"sv, buf);
ASSERT_EQ(alreadyNormalized.unwrap(), AlreadyNormalized::No);
ASSERT_EQ(buf.get_string_view(), u"ä");
buf.clear();
alreadyNormalized = String::Normalize(NormalizationForm::NFKC, u"½"sv, buf);
ASSERT_EQ(alreadyNormalized.unwrap(), AlreadyNormalized::No);
ASSERT_EQ(buf.get_string_view(), u"1⁄2");
}
TEST(IntlString, NormalizeNFKD)
{
using namespace std::literals;
using NormalizationForm = String::NormalizationForm;
using AlreadyNormalized = String::AlreadyNormalized;
TestBuffer<char16_t> buf;
auto alreadyNormalized =
String::Normalize(NormalizationForm::NFKD, u""sv, buf);
ASSERT_EQ(alreadyNormalized.unwrap(), AlreadyNormalized::Yes);
ASSERT_EQ(buf.get_string_view(), u"");
alreadyNormalized =
String::Normalize(NormalizationForm::NFKD, u"abcdef"sv, buf);
ASSERT_EQ(alreadyNormalized.unwrap(), AlreadyNormalized::Yes);
ASSERT_EQ(buf.get_string_view(), u"");
alreadyNormalized = String::Normalize(NormalizationForm::NFKD, u"ä"sv, buf);
ASSERT_EQ(alreadyNormalized.unwrap(), AlreadyNormalized::No);
ASSERT_EQ(buf.get_string_view(), u"a\u0308");
buf.clear();
alreadyNormalized = String::Normalize(NormalizationForm::NFKD, u"½"sv, buf);
ASSERT_EQ(alreadyNormalized.unwrap(), AlreadyNormalized::No);
ASSERT_EQ(buf.get_string_view(), u"1⁄2");
}
TEST(IntlString, ComposePairNFC)
{
// Pair of base characters do not compose
ASSERT_EQ(String::ComposePairNFC(U'a', U'b'), U'\0');
// Base letter + accent
ASSERT_EQ(String::ComposePairNFC(U'a', U'\u0308'), U'ä');
// Accented letter + a further accent
ASSERT_EQ(String::ComposePairNFC(U'ä', U'\u0304'), U'ǟ');
// Accented letter + a further accent, but doubly-accented form is not
// available
ASSERT_EQ(String::ComposePairNFC(U'ä', U'\u0301'), U'\0');
// These do not compose because although U+0344 has the decomposition <0308,
// 0301> (see below), it also has the Full_Composition_Exclusion property.
ASSERT_EQ(String::ComposePairNFC(U'\u0308', U'\u0301'), U'\0');
// Supplementary-plane letter + accent
ASSERT_EQ(String::ComposePairNFC(U'\U00011099', U'\U000110BA'),
U'\U0001109A');
}
TEST(IntlString, DecomposeRawNFD)
{
char32_t buf[2];
// Non-decomposable character
ASSERT_EQ(String::DecomposeRawNFD(U'a', buf), 0);
// Singleton decomposition
ASSERT_EQ(String::DecomposeRawNFD(U'\u212A', buf), 1);
ASSERT_EQ(buf[0], U'K');
// Simple accented letter
ASSERT_EQ(String::DecomposeRawNFD(U'ä', buf), 2);
ASSERT_EQ(buf[0], U'a');
ASSERT_EQ(buf[1], U'\u0308');
// Double-accented letter decomposes by only one level
ASSERT_EQ(String::DecomposeRawNFD(U'ǟ', buf), 2);
ASSERT_EQ(buf[0], U'ä');
ASSERT_EQ(buf[1], U'\u0304');
// Non-starter can decompose, but will not recompose (see above)
ASSERT_EQ(String::DecomposeRawNFD(U'\u0344', buf), 2);
ASSERT_EQ(buf[0], U'\u0308');
ASSERT_EQ(buf[1], U'\u0301');
// Supplementary-plane letter with decomposition
ASSERT_EQ(String::DecomposeRawNFD(U'\U0001109A', buf), 2);
ASSERT_EQ(buf[0], U'\U00011099');
ASSERT_EQ(buf[1], U'\U000110BA');
}
TEST(IntlString, IsCased)
{
ASSERT_TRUE(String::IsCased(U'a'));
ASSERT_FALSE(String::IsCased(U'0'));
}
TEST(IntlString, IsCaseIgnorable)
{
ASSERT_FALSE(String::IsCaseIgnorable(U'a'));
ASSERT_TRUE(String::IsCaseIgnorable(U'.'));
}
TEST(IntlString, GetUnicodeVersion)
{
auto version = String::GetUnicodeVersion();
ASSERT_TRUE(std::all_of(version.begin(), version.end(), [](char ch) {
return IsAsciiDigit(ch) || ch == '.';
}));
}
} // namespace mozilla::intl
|