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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
/* 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 <string.h>
#include <string_view>
#include "mozilla/intl/Collator.h"
#include "mozilla/Span.h"
#include "TestBuffer.h"
namespace mozilla::intl {
TEST(IntlCollator, SetAttributesInternal)
{
// Run through each settings to make sure MOZ_ASSERT is not triggered for
// misconfigured attributes.
auto result = Collator::TryCreate("en-US");
ASSERT_TRUE(result.isOk());
auto collator = result.unwrap();
collator->SetStrength(Collator::Strength::Primary);
collator->SetStrength(Collator::Strength::Secondary);
collator->SetStrength(Collator::Strength::Tertiary);
collator->SetStrength(Collator::Strength::Quaternary);
collator->SetStrength(Collator::Strength::Identical);
collator->SetStrength(Collator::Strength::Default);
collator->SetAlternateHandling(Collator::AlternateHandling::NonIgnorable)
.unwrap();
collator->SetAlternateHandling(Collator::AlternateHandling::Shifted).unwrap();
collator->SetAlternateHandling(Collator::AlternateHandling::Default).unwrap();
collator->SetCaseFirst(Collator::CaseFirst::False).unwrap();
collator->SetCaseFirst(Collator::CaseFirst::Upper).unwrap();
collator->SetCaseFirst(Collator::CaseFirst::Lower).unwrap();
collator->SetCaseLevel(Collator::Feature::On).unwrap();
collator->SetCaseLevel(Collator::Feature::Off).unwrap();
collator->SetCaseLevel(Collator::Feature::Default).unwrap();
collator->SetNumericCollation(Collator::Feature::On).unwrap();
collator->SetNumericCollation(Collator::Feature::Off).unwrap();
collator->SetNumericCollation(Collator::Feature::Default).unwrap();
collator->SetNormalizationMode(Collator::Feature::On).unwrap();
collator->SetNormalizationMode(Collator::Feature::Off).unwrap();
collator->SetNormalizationMode(Collator::Feature::Default).unwrap();
}
TEST(IntlCollator, GetSortKey)
{
// Do some light sort key comparisons to ensure everything is wired up
// correctly. This is not doing extensive correctness testing.
auto result = Collator::TryCreate("en-US");
ASSERT_TRUE(result.isOk());
auto collator = result.unwrap();
TestBuffer<uint8_t> bufferA;
TestBuffer<uint8_t> bufferB;
auto compareSortKeys = [&](const char16_t* a, const char16_t* b) {
collator->GetSortKey(MakeStringSpan(a), bufferA).unwrap();
collator->GetSortKey(MakeStringSpan(b), bufferB).unwrap();
return strcmp(reinterpret_cast<const char*>(bufferA.data()),
reinterpret_cast<const char*>(bufferB.data()));
};
ASSERT_TRUE(compareSortKeys(u"aaa", u"bbb") < 0);
ASSERT_TRUE(compareSortKeys(u"bbb", u"aaa") > 0);
ASSERT_TRUE(compareSortKeys(u"aaa", u"aaa") == 0);
ASSERT_TRUE(compareSortKeys(u"👍", u"👎") < 0);
}
TEST(IntlCollator, CompareStrings)
{
// Do some light string comparisons to ensure everything is wired up
// correctly. This is not doing extensive correctness testing.
auto result = Collator::TryCreate("en-US");
ASSERT_TRUE(result.isOk());
auto collator = result.unwrap();
TestBuffer<uint8_t> bufferA;
TestBuffer<uint8_t> bufferB;
ASSERT_EQ(collator->CompareStrings(u"aaa", u"bbb"), -1);
ASSERT_EQ(collator->CompareStrings(u"bbb", u"aaa"), 1);
ASSERT_EQ(collator->CompareStrings(u"aaa", u"aaa"), 0);
ASSERT_EQ(collator->CompareStrings(u"👍", u"👎"), -1);
}
TEST(IntlCollator, SetOptionsSensitivity)
{
// Test the ECMA 402 sensitivity behavior per:
// https://tc39.es/ecma402/#sec-collator-comparestrings
auto result = Collator::TryCreate("en-US");
ASSERT_TRUE(result.isOk());
auto collator = result.unwrap();
TestBuffer<uint8_t> bufferA;
TestBuffer<uint8_t> bufferB;
ICUResult optResult = Ok();
Collator::Options options{};
options.sensitivity = Collator::Sensitivity::Base;
optResult = collator->SetOptions(options);
ASSERT_TRUE(optResult.isOk());
ASSERT_EQ(collator->CompareStrings(u"a", u"b"), -1);
ASSERT_EQ(collator->CompareStrings(u"a", u"á"), 0);
ASSERT_EQ(collator->CompareStrings(u"a", u"A"), 0);
options.sensitivity = Collator::Sensitivity::Accent;
optResult = collator->SetOptions(options);
ASSERT_TRUE(optResult.isOk());
ASSERT_EQ(collator->CompareStrings(u"a", u"b"), -1);
ASSERT_EQ(collator->CompareStrings(u"a", u"á"), -1);
ASSERT_EQ(collator->CompareStrings(u"a", u"A"), 0);
options.sensitivity = Collator::Sensitivity::Case;
optResult = collator->SetOptions(options);
ASSERT_TRUE(optResult.isOk());
ASSERT_EQ(collator->CompareStrings(u"a", u"b"), -1);
ASSERT_EQ(collator->CompareStrings(u"a", u"á"), 0);
ASSERT_EQ(collator->CompareStrings(u"a", u"A"), -1);
options.sensitivity = Collator::Sensitivity::Variant;
optResult = collator->SetOptions(options);
ASSERT_TRUE(optResult.isOk());
ASSERT_EQ(collator->CompareStrings(u"a", u"b"), -1);
ASSERT_EQ(collator->CompareStrings(u"a", u"á"), -1);
ASSERT_EQ(collator->CompareStrings(u"a", u"A"), -1);
}
TEST(IntlCollator, LocaleSensitiveCollations)
{
UniquePtr<Collator> collator = nullptr;
TestBuffer<uint8_t> bufferA;
TestBuffer<uint8_t> bufferB;
auto changeLocale = [&](const char* locale) {
auto result = Collator::TryCreate(locale);
ASSERT_TRUE(result.isOk());
collator = result.unwrap();
Collator::Options options{};
options.sensitivity = Collator::Sensitivity::Base;
auto optResult = collator->SetOptions(options);
ASSERT_TRUE(optResult.isOk());
};
// Swedish treats "Ö" as a separate character, which sorts after "Z".
changeLocale("en-US");
ASSERT_EQ(collator->CompareStrings(u"Österreich", u"Västervik"), -1);
changeLocale("sv-SE");
ASSERT_EQ(collator->CompareStrings(u"Österreich", u"Västervik"), 1);
// Country names in their respective scripts.
auto china = MakeStringSpan(u"中国");
auto japan = MakeStringSpan(u"日本");
auto korea = MakeStringSpan(u"한국");
changeLocale("en-US");
ASSERT_EQ(collator->CompareStrings(china, japan), -1);
ASSERT_EQ(collator->CompareStrings(china, korea), 1);
changeLocale("zh");
ASSERT_EQ(collator->CompareStrings(china, japan), 1);
ASSERT_EQ(collator->CompareStrings(china, korea), -1);
changeLocale("ja");
ASSERT_EQ(collator->CompareStrings(china, japan), -1);
ASSERT_EQ(collator->CompareStrings(china, korea), -1);
changeLocale("ko");
ASSERT_EQ(collator->CompareStrings(china, japan), 1);
ASSERT_EQ(collator->CompareStrings(china, korea), -1);
}
TEST(IntlCollator, IgnorePunctuation)
{
TestBuffer<uint8_t> bufferA;
TestBuffer<uint8_t> bufferB;
auto result = Collator::TryCreate("en-US");
ASSERT_TRUE(result.isOk());
auto collator = result.unwrap();
Collator::Options options{};
options.ignorePunctuation = true;
auto optResult = collator->SetOptions(options);
ASSERT_TRUE(optResult.isOk());
ASSERT_EQ(collator->CompareStrings(u"aa", u".bb"), -1);
options.ignorePunctuation = false;
optResult = collator->SetOptions(options);
ASSERT_TRUE(optResult.isOk());
ASSERT_EQ(collator->CompareStrings(u"aa", u".bb"), 1);
}
TEST(IntlCollator, GetBcp47KeywordValuesForLocale)
{
auto extsResult = Collator::GetBcp47KeywordValuesForLocale("de");
ASSERT_TRUE(extsResult.isOk());
auto extensions = extsResult.unwrap();
// Since this list is dependent on ICU, and may change between upgrades, only
// test a subset of the keywords.
auto standard = MakeStringSpan("standard");
auto search = MakeStringSpan("search");
auto phonebk = MakeStringSpan("phonebk"); // Valid BCP 47.
auto phonebook = MakeStringSpan("phonebook"); // Not valid BCP 47.
bool hasStandard = false;
bool hasSearch = false;
bool hasPhonebk = false;
bool hasPhonebook = false;
for (auto extensionResult : extensions) {
ASSERT_TRUE(extensionResult.isOk());
auto extension = extensionResult.unwrap();
hasStandard |= extension == standard;
hasSearch |= extension == search;
hasPhonebk |= extension == phonebk;
hasPhonebook |= extension == phonebook;
}
ASSERT_TRUE(hasStandard);
ASSERT_TRUE(hasSearch);
ASSERT_TRUE(hasPhonebk);
ASSERT_FALSE(hasPhonebook); // Not valid BCP 47.
}
TEST(IntlCollator, GetBcp47KeywordValuesForLocaleCommonlyUsed)
{
auto extsResult = Collator::GetBcp47KeywordValuesForLocale(
"fr", Collator::CommonlyUsed::Yes);
ASSERT_TRUE(extsResult.isOk());
auto extensions = extsResult.unwrap();
// Since this list is dependent on ICU, and may change between upgrades, only
// test a subset of the keywords.
auto standard = MakeStringSpan("standard");
auto search = MakeStringSpan("search");
auto phonebk = MakeStringSpan("phonebk"); // Valid BCP 47.
auto phonebook = MakeStringSpan("phonebook"); // Not valid BCP 47.
bool hasStandard = false;
bool hasSearch = false;
bool hasPhonebk = false;
bool hasPhonebook = false;
for (auto extensionResult : extensions) {
ASSERT_TRUE(extensionResult.isOk());
auto extension = extensionResult.unwrap();
hasStandard |= extension == standard;
hasSearch |= extension == search;
hasPhonebk |= extension == phonebk;
hasPhonebook |= extension == phonebook;
}
ASSERT_TRUE(hasStandard);
ASSERT_TRUE(hasSearch);
ASSERT_FALSE(hasPhonebk); // Not commonly used in French.
ASSERT_FALSE(hasPhonebook); // Not valid BCP 47.
}
TEST(IntlCollator, GetBcp47KeywordValues)
{
auto extsResult = Collator::GetBcp47KeywordValues();
ASSERT_TRUE(extsResult.isOk());
auto extensions = extsResult.unwrap();
// Since this list is dependent on ICU, and may change between upgrades, only
// test a subset of the keywords.
auto standard = MakeStringSpan("standard");
auto search = MakeStringSpan("search");
auto phonebk = MakeStringSpan("phonebk"); // Valid BCP 47.
auto phonebook = MakeStringSpan("phonebook"); // Not valid BCP 47.
bool hasStandard = false;
bool hasSearch = false;
bool hasPhonebk = false;
bool hasPhonebook = false;
for (auto extensionResult : extensions) {
ASSERT_TRUE(extensionResult.isOk());
auto extension = extensionResult.unwrap();
hasStandard |= extension == standard;
hasSearch |= extension == search;
hasPhonebk |= extension == phonebk;
hasPhonebook |= extension == phonebook;
}
ASSERT_TRUE(hasStandard);
ASSERT_TRUE(hasSearch);
ASSERT_TRUE(hasPhonebk);
ASSERT_FALSE(hasPhonebook); // Not valid BCP 47.
}
TEST(IntlCollator, GetAvailableLocales)
{
using namespace std::literals;
int32_t english = 0;
int32_t german = 0;
int32_t chinese = 0;
// Since this list is dependent on ICU, and may change between upgrades, only
// test a subset of the available locales.
for (const char* locale : Collator::GetAvailableLocales()) {
if (locale == "en"sv) {
english++;
} else if (locale == "de"sv) {
german++;
} else if (locale == "zh"sv) {
chinese++;
}
}
// Each locale should be found exactly once.
ASSERT_EQ(english, 1);
ASSERT_EQ(german, 1);
ASSERT_EQ(chinese, 1);
}
TEST(IntlCollator, GetCaseFirst)
{
auto result = Collator::TryCreate("en-US");
ASSERT_TRUE(result.isOk());
auto collator = result.unwrap();
auto caseFirst = collator->GetCaseFirst();
ASSERT_TRUE(caseFirst.isOk());
ASSERT_EQ(caseFirst.unwrap(), Collator::CaseFirst::False);
for (auto kf : {Collator::CaseFirst::Upper, Collator::CaseFirst::Lower,
Collator::CaseFirst::False}) {
Collator::Options options{};
options.caseFirst = kf;
auto optResult = collator->SetOptions(options);
ASSERT_TRUE(optResult.isOk());
auto caseFirst = collator->GetCaseFirst();
ASSERT_TRUE(caseFirst.isOk());
ASSERT_EQ(caseFirst.unwrap(), kf);
}
}
} // namespace mozilla::intl
|