| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
use core::cmp::Ordering; |
| 6 |
|
| 7 |
use atoi::FromRadix16; |
| 8 |
use icu_collator::provider::*; |
| 9 |
use icu_collator::{options::*, preferences::*, *}; |
| 10 |
use icu_locale_core::{Locale, data_locale, locale}; |
| 11 |
use icu_provider::prelude::*; |
| 12 |
|
| 13 |
struct TestingProvider; |
| 14 |
|
| 15 |
#[allow(unused_imports)] |
| 16 |
const _: () = { |
| 17 |
use icu_collator_data::*; |
| 18 |
pub mod icu { |
| 19 |
pub use crate as collator; |
| 20 |
pub use icu_collections as collections; |
| 21 |
pub use icu_normalizer as normalizer; |
| 22 |
} |
| 23 |
make_provider!(TestingProvider); |
| 24 |
impl_collation_root_v1!(TestingProvider); |
| 25 |
impl_collation_tailoring_v1!(TestingProvider); |
| 26 |
impl_collation_diacritics_v1!(TestingProvider); |
| 27 |
impl_collation_jamo_v1!(TestingProvider); |
| 28 |
impl_collation_metadata_v1!(TestingProvider); |
| 29 |
impl_collation_special_primaries_v1!(TestingProvider); |
| 30 |
impl_collation_reordering_v1!(TestingProvider); |
| 31 |
|
| 32 |
icu_normalizer_data::impl_normalizer_nfc_v1!(TestingProvider); |
| 33 |
icu_normalizer_data::impl_normalizer_nfd_data_v1!(TestingProvider); |
| 34 |
icu_normalizer_data::impl_normalizer_nfd_supplement_v1!(TestingProvider); |
| 35 |
icu_normalizer_data::impl_normalizer_nfd_tables_v1!(TestingProvider); |
| 36 |
icu_normalizer_data::impl_normalizer_nfkd_data_v1!(TestingProvider); |
| 37 |
icu_normalizer_data::impl_normalizer_nfkd_tables_v1!(TestingProvider); |
| 38 |
icu_normalizer_data::impl_normalizer_uts46_data_v1!(TestingProvider); |
| 39 |
}; |
| 40 |
|
| 41 |
fn assert_all_comparisons( |
| 42 |
collator: &CollatorBorrowed, |
| 43 |
left: &str, |
| 44 |
right: &str, |
| 45 |
expected: Ordering, |
| 46 |
) { |
| 47 |
// UTF-8 |
| 48 |
let result = collator.compare(left, right); |
| 49 |
assert_eq!(result, expected, "{left:?} ≟ {right:?}"); |
| 50 |
|
| 51 |
// Sort keys |
| 52 |
let mut sort_key_left_buffer = Vec::new(); |
| 53 |
let mut sort_key_right_buffer = Vec::new(); |
| 54 |
|
| 55 |
collator |
| 56 |
.write_sort_key_to(left, &mut sort_key_left_buffer) |
| 57 |
.unwrap(); |
| 58 |
collator |
| 59 |
.write_sort_key_to(right, &mut sort_key_right_buffer) |
| 60 |
.unwrap(); |
| 61 |
|
| 62 |
assert_eq!(sort_key_left_buffer.cmp(&sort_key_right_buffer), expected); |
| 63 |
|
| 64 |
// UTF-16 |
| 65 |
let left_encode = left.encode_utf16().collect::<Vec<u16>>(); |
| 66 |
let right_encode = right.encode_utf16().collect::<Vec<u16>>(); |
| 67 |
|
| 68 |
let result_utf16 = collator.compare_utf16(&left_encode, &right_encode); |
| 69 |
assert_eq!(result_utf16, expected); |
| 70 |
} |
| 71 |
|
| 72 |
/// Parse a string of space-separated hexadecimal code points (ending in end of input or semicolon) |
| 73 |
fn parse_hex(mut hexes: &[u8]) -> Option<String> { |
| 74 |
let mut buf = String::new(); |
| 75 |
loop { |
| 76 |
let (scalar, mut offset) = u32::from_radix_16(hexes); |
| 77 |
buf.push(char::from_u32(scalar)?); |
| 78 |
if offset == hexes.len() { |
| 79 |
return Some(buf); |
| 80 |
} |
| 81 |
match hexes[offset] { |
| 82 |
// '\r' is for git on Windows touching the line ends |
| 83 |
b';' | b'\r' => { |
| 84 |
return Some(buf); |
| 85 |
} |
| 86 |
b' ' => { |
| 87 |
offset += 1; |
| 88 |
} |
| 89 |
_ => { |
| 90 |
panic!("Bad format: Garbage"); |
| 91 |
} |
| 92 |
} |
| 93 |
hexes = &hexes[offset..]; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
#[test] |
| 98 |
fn test_parse_hex() { |
| 99 |
assert_eq!( |
| 100 |
&parse_hex(b"1F926 1F3FC 200D 2642 FE0F").unwrap(), |
| 101 |
"\u{1F926}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}" |
| 102 |
); |
| 103 |
assert_eq!( |
| 104 |
&parse_hex(b"1F926 1F3FC 200D 2642 FE0F; whatever").unwrap(), |
| 105 |
"\u{1F926}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}" |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
fn check_expectations( |
| 110 |
collator: &CollatorBorrowed, |
| 111 |
left: &[&str], |
| 112 |
right: &[&str], |
| 113 |
expectations: &[Ordering], |
| 114 |
) { |
| 115 |
for ((left_str, right_str), expected) in left.iter().zip(right.iter()).zip(expectations.iter()) |
| 116 |
{ |
| 117 |
assert_all_comparisons(collator, left_str, right_str, *expected); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
#[test] |
| 122 |
fn test_implicit_unihan() { |
| 123 |
// Adapted from `CollationTest::TestImplicits()` in collationtest.cpp of ICU4C. |
| 124 |
// The radical-stroke order of the characters tested agrees with their code point |
| 125 |
// order. |
| 126 |
|
| 127 |
let mut options = CollatorOptions::default(); |
| 128 |
options.strength = Some(Strength::Quaternary); |
| 129 |
|
| 130 |
let collator = Collator::try_new(Default::default(), options).unwrap(); |
| 131 |
assert_all_comparisons(&collator, "\u{4E00}", "\u{4E00}", Ordering::Equal); |
| 132 |
assert_all_comparisons(&collator, "\u{4E00}", "\u{4E01}", Ordering::Less); |
| 133 |
assert_all_comparisons(&collator, "\u{4E01}", "\u{4E00}", Ordering::Greater); |
| 134 |
|
| 135 |
assert_all_comparisons(&collator, "\u{4E18}", "\u{4E42}", Ordering::Less); |
| 136 |
assert_all_comparisons(&collator, "\u{4E94}", "\u{50F6}", Ordering::Less); |
| 137 |
} |
| 138 |
|
| 139 |
#[test] |
| 140 |
fn test_currency() { |
| 141 |
// Adapted from `CollationCurrencyTest::currencyTest` in currcoll.cpp of ICU4C. |
| 142 |
// All the currency symbols, in collation order. |
| 143 |
let currencies = "\u{00A4}\u{00A2}\u{FFE0}\u{0024}\u{FF04}\u{FE69}\u{00A3}\u{FFE1}\u{00A5}\u{FFE5}\u{09F2}\u{09F3}\u{0E3F}\u{17DB}\u{20A0}\u{20A1}\u{20A2}\u{20A3}\u{20A4}\u{20A5}\u{20A6}\u{20A9}\u{FFE6}\u{20AA}\u{20AB}\u{20AC}\u{20AD}\u{20AE}\u{20AF}"; |
| 144 |
|
| 145 |
let mut options = CollatorOptions::default(); |
| 146 |
options.strength = Some(Strength::Quaternary); |
| 147 |
|
| 148 |
let collator = Collator::try_new(Default::default(), options).unwrap(); |
| 149 |
let mut chars = currencies.char_indices(); |
| 150 |
let mut lower_start = chars.offset(); |
| 151 |
while let Some(_) = chars.next() { |
| 152 |
let lower_end = chars.offset(); |
| 153 |
let higher_start = lower_end; |
| 154 |
let mut tail = chars.clone(); |
| 155 |
while let Some(_) = tail.next() { |
| 156 |
let higher_end = tail.offset(); |
| 157 |
assert_all_comparisons( |
| 158 |
&collator, |
| 159 |
¤cies[lower_start..lower_end], |
| 160 |
¤cies[higher_start..higher_end], |
| 161 |
Ordering::Less, |
| 162 |
); |
| 163 |
} |
| 164 |
lower_start = higher_start; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
#[test] |
| 169 |
fn test_bo() { |
| 170 |
let prefs = locale!("bo").into(); |
| 171 |
let options = CollatorOptions::default(); |
| 172 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 173 |
|
| 174 |
assert_all_comparisons( |
| 175 |
&collator, |
| 176 |
"\u{0F40}\u{0F71}\u{0F62}\u{0F92}\u{0F72}", |
| 177 |
"\u{0F40}\u{0F71}\u{0F62}\u{0F0B}\u{0F92}\u{0F72}", |
| 178 |
Ordering::Greater, |
| 179 |
); |
| 180 |
|
| 181 |
assert_all_comparisons( |
| 182 |
&collator, |
| 183 |
"\u{0F40}\u{0F71}\u{0F62}\u{0F0B}\u{0F92}\u{0F72}", |
| 184 |
"\u{0F40}\u{0F62}\u{0F92}\u{0F72}", |
| 185 |
Ordering::Greater, |
| 186 |
); |
| 187 |
|
| 188 |
assert_all_comparisons( |
| 189 |
&collator, |
| 190 |
"\u{0F40}\u{0F62}\u{0F92}\u{0F72}", |
| 191 |
"\u{0F42}\u{0F7C}\u{0F51}\u{0F0B}", |
| 192 |
Ordering::Less, |
| 193 |
); |
| 194 |
|
| 195 |
assert_all_comparisons( |
| 196 |
&collator, |
| 197 |
"\u{0F42}\u{0F7C}\u{0F51}\u{0F0B}", |
| 198 |
"\u{0F42}\u{0F7C}\u{0F51}", |
| 199 |
Ordering::Greater, |
| 200 |
); |
| 201 |
|
| 202 |
assert_all_comparisons( |
| 203 |
&collator, |
| 204 |
"\u{0F42}\u{0F7C}\u{0F51}", |
| 205 |
"\u{0F40}\u{0FB1}\u{0F72}", |
| 206 |
Ordering::Greater, |
| 207 |
); |
| 208 |
|
| 209 |
assert_all_comparisons( |
| 210 |
&collator, |
| 211 |
"\u{0F40}\u{0FB1}\u{0F72}", |
| 212 |
"\u{0F40}\u{0F71}\u{0FB1}\u{0F72}", |
| 213 |
Ordering::Less, |
| 214 |
); |
| 215 |
|
| 216 |
assert_all_comparisons( |
| 217 |
&collator, |
| 218 |
"\u{0F40}\u{0F71}\u{0FB1}\u{0F72}", |
| 219 |
"\u{0F40}\u{0F71}", |
| 220 |
Ordering::Greater, |
| 221 |
); |
| 222 |
} |
| 223 |
|
| 224 |
#[test] |
| 225 |
fn test_bs() { |
| 226 |
let left_side = vec![ |
| 227 |
"\u{0107}", // ć |
| 228 |
"\u{0161}", // š |
| 229 |
"\u{0448}", // ш |
| 230 |
"d\u{017E}", // dž |
| 231 |
"l\u{006A}", // lj |
| 232 |
"\u{0459}", // љ |
| 233 |
]; |
| 234 |
let right_side = vec![ |
| 235 |
"\u{010D}", // č |
| 236 |
"\u{0073}", // s |
| 237 |
"\u{0441}", // с |
| 238 |
"d\u{006A}", // dj |
| 239 |
"\u{006C}", // l |
| 240 |
"\u{043B}", // л |
| 241 |
]; |
| 242 |
|
| 243 |
{ |
| 244 |
let prefs = locale!("bs").into(); |
| 245 |
let collator = Collator::try_new(prefs, Default::default()).unwrap(); |
| 246 |
for (left, right) in left_side.iter().zip(right_side.iter()) { |
| 247 |
assert_eq!(collator.compare(left, right), Ordering::Greater); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
{ |
| 252 |
let prefs = locale!("bs-Cyrl").into(); |
| 253 |
let collator = Collator::try_new(prefs, Default::default()).unwrap(); |
| 254 |
let expect_bs_cyrl = vec![ |
| 255 |
Ordering::Less, // Ordering changes in Cyrillic |
| 256 |
Ordering::Greater, |
| 257 |
Ordering::Greater, |
| 258 |
Ordering::Greater, |
| 259 |
Ordering::Greater, |
| 260 |
Ordering::Greater, |
| 261 |
]; |
| 262 |
check_expectations(&collator, &left_side, &right_side, &expect_bs_cyrl); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
#[test] |
| 267 |
fn test_de() { |
| 268 |
// Adapted from `CollationGermanTest` in decoll.cpp of ICU4C. |
| 269 |
let left = [ |
| 270 |
"Größe", "abc", "Töne", "Töne", "Töne", "äbc", "äbc", "äbc", "Straße", "efg", "äbc", |
| 271 |
"Straße", |
| 272 |
]; |
| 273 |
|
| 274 |
let right = [ |
| 275 |
"Grossist", |
| 276 |
"a\u{0308}bc", |
| 277 |
"Ton", |
| 278 |
"Tod", |
| 279 |
"Tofu", |
| 280 |
"A\u{0308}bc", |
| 281 |
"a\u{0308}bc", |
| 282 |
"aebc", |
| 283 |
"Strasse", |
| 284 |
"efg", |
| 285 |
"aebc", |
| 286 |
"Strasse", |
| 287 |
]; |
| 288 |
|
| 289 |
let expect_primary = [ |
| 290 |
Ordering::Less, |
| 291 |
Ordering::Equal, |
| 292 |
Ordering::Greater, |
| 293 |
Ordering::Greater, |
| 294 |
Ordering::Greater, |
| 295 |
Ordering::Equal, |
| 296 |
Ordering::Equal, |
| 297 |
Ordering::Less, |
| 298 |
Ordering::Equal, |
| 299 |
Ordering::Equal, |
| 300 |
Ordering::Less, |
| 301 |
Ordering::Equal, |
| 302 |
]; |
| 303 |
|
| 304 |
let expect_tertiary = [ |
| 305 |
Ordering::Less, |
| 306 |
Ordering::Less, |
| 307 |
Ordering::Greater, |
| 308 |
Ordering::Greater, |
| 309 |
Ordering::Greater, |
| 310 |
Ordering::Less, |
| 311 |
Ordering::Equal, |
| 312 |
Ordering::Less, |
| 313 |
Ordering::Greater, |
| 314 |
Ordering::Equal, |
| 315 |
Ordering::Less, |
| 316 |
Ordering::Greater, |
| 317 |
]; |
| 318 |
|
| 319 |
let expect_de_at = [ |
| 320 |
Ordering::Greater, |
| 321 |
Ordering::Less, |
| 322 |
Ordering::Greater, |
| 323 |
Ordering::Greater, |
| 324 |
Ordering::Greater, |
| 325 |
Ordering::Less, |
| 326 |
Ordering::Equal, |
| 327 |
Ordering::Greater, |
| 328 |
Ordering::Greater, |
| 329 |
Ordering::Equal, |
| 330 |
Ordering::Greater, |
| 331 |
Ordering::Greater, |
| 332 |
]; |
| 333 |
|
| 334 |
let expect_de_de = [ |
| 335 |
Ordering::Less, |
| 336 |
Ordering::Less, |
| 337 |
Ordering::Less, |
| 338 |
Ordering::Greater, |
| 339 |
Ordering::Less, |
| 340 |
Ordering::Less, |
| 341 |
Ordering::Equal, |
| 342 |
Ordering::Greater, |
| 343 |
Ordering::Greater, |
| 344 |
Ordering::Equal, |
| 345 |
Ordering::Greater, |
| 346 |
Ordering::Greater, |
| 347 |
]; |
| 348 |
|
| 349 |
let mut options = CollatorOptions::default(); |
| 350 |
options.strength = Some(Strength::Primary); |
| 351 |
|
| 352 |
{ |
| 353 |
// Note: German uses the root collation |
| 354 |
let collator = Collator::try_new(Default::default(), options).unwrap(); |
| 355 |
check_expectations(&collator, &left, &right, &expect_primary); |
| 356 |
} |
| 357 |
|
| 358 |
options.strength = Some(Strength::Tertiary); |
| 359 |
|
| 360 |
{ |
| 361 |
// Note: German uses the root collation |
| 362 |
let collator = Collator::try_new(Default::default(), options).unwrap(); |
| 363 |
check_expectations(&collator, &left, &right, &expect_tertiary); |
| 364 |
} |
| 365 |
|
| 366 |
{ |
| 367 |
let prefs = locale!("de-AT-u-co-phonebk").into(); |
| 368 |
let collator = Collator::try_new(prefs, Default::default()).unwrap(); |
| 369 |
check_expectations(&collator, &left, &right, &expect_de_at); |
| 370 |
} |
| 371 |
|
| 372 |
{ |
| 373 |
let prefs = locale!("de-DE-u-co-phonebk").into(); |
| 374 |
let collator = Collator::try_new(prefs, Default::default()).unwrap(); |
| 375 |
check_expectations(&collator, &left, &right, &expect_de_de); |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
#[test] |
| 380 |
fn test_en() { |
| 381 |
// Adapted from encoll.cpp in ICU4C |
| 382 |
let left = [ |
| 383 |
"ab", |
| 384 |
"black-bird", |
| 385 |
"black bird", |
| 386 |
"black-bird", |
| 387 |
"Hello", |
| 388 |
"ABC", |
| 389 |
"abc", |
| 390 |
"blackbird", |
| 391 |
"black-bird", |
| 392 |
"black-bird", |
| 393 |
"pêche", |
| 394 |
"péché", |
| 395 |
"ÄB̈C̈", |
| 396 |
"äbc", |
| 397 |
"pécher", |
| 398 |
"roles", |
| 399 |
"abc", |
| 400 |
"A", |
| 401 |
"A", |
| 402 |
"ab", |
| 403 |
"tcompareplain", |
| 404 |
"ab", |
| 405 |
"a#b", |
| 406 |
"a#b", |
| 407 |
"abc", |
| 408 |
"Abcda", |
| 409 |
"abcda", |
| 410 |
"abcda", |
| 411 |
"æbcda", |
| 412 |
"äbcda", |
| 413 |
"abc", |
| 414 |
"abc", |
| 415 |
"abc", |
| 416 |
"abc", |
| 417 |
"abc", |
| 418 |
"acHc", |
| 419 |
"äbc", |
| 420 |
"thîs", |
| 421 |
"pêche", |
| 422 |
"abc", |
| 423 |
"abc", |
| 424 |
"abc", |
| 425 |
"aæc", |
| 426 |
"abc", |
| 427 |
"abc", |
| 428 |
"aæc", |
| 429 |
"abc", |
| 430 |
"abc", |
| 431 |
"péché", |
| 432 |
]; // 49 |
| 433 |
|
| 434 |
let right = [ |
| 435 |
"abc", |
| 436 |
"blackbird", |
| 437 |
"black-bird", |
| 438 |
"black", |
| 439 |
"hello", |
| 440 |
"ABC", |
| 441 |
"ABC", |
| 442 |
"blackbirds", |
| 443 |
"blackbirds", |
| 444 |
"blackbird", |
| 445 |
"péché", |
| 446 |
"pécher", |
| 447 |
"ÄB̈C̈", |
| 448 |
"Äbc", |
| 449 |
"péche", |
| 450 |
"rôle", |
| 451 |
"Aácd", |
| 452 |
"Aácd", |
| 453 |
"abc", |
| 454 |
"abc", |
| 455 |
"TComparePlain", |
| 456 |
"aBc", |
| 457 |
"a#B", |
| 458 |
"a&b", |
| 459 |
"a#c", |
| 460 |
"abcda", |
| 461 |
"Äbcda", |
| 462 |
"äbcda", |
| 463 |
"Äbcda", |
| 464 |
"Äbcda", |
| 465 |
"ab#c", |
| 466 |
"abc", |
| 467 |
"ab=c", |
| 468 |
"abd", |
| 469 |
"äbc", |
| 470 |
"aCHc", |
| 471 |
"äbc", |
| 472 |
"thîs", |
| 473 |
"péché", |
| 474 |
"aBC", |
| 475 |
"abd", |
| 476 |
"äbc", |
| 477 |
"aÆc", |
| 478 |
"aBd", |
| 479 |
"äbc", |
| 480 |
"aÆc", |
| 481 |
"aBd", |
| 482 |
"äbc", |
| 483 |
"pêche", |
| 484 |
]; // 49 |
| 485 |
|
| 486 |
let expectations = [ |
| 487 |
Ordering::Less, |
| 488 |
Ordering::Less, /* Ordering::Greater, */ |
| 489 |
Ordering::Less, |
| 490 |
Ordering::Greater, |
| 491 |
Ordering::Greater, |
| 492 |
Ordering::Equal, |
| 493 |
Ordering::Less, |
| 494 |
Ordering::Less, |
| 495 |
Ordering::Less, |
| 496 |
Ordering::Less, /* Ordering::Greater, */ |
| 497 |
/* 10 */ |
| 498 |
Ordering::Greater, |
| 499 |
Ordering::Less, |
| 500 |
Ordering::Equal, |
| 501 |
Ordering::Less, |
| 502 |
Ordering::Greater, |
| 503 |
Ordering::Greater, |
| 504 |
Ordering::Greater, |
| 505 |
Ordering::Less, |
| 506 |
Ordering::Less, |
| 507 |
Ordering::Less, /* 20 */ |
| 508 |
Ordering::Less, |
| 509 |
Ordering::Less, |
| 510 |
Ordering::Less, |
| 511 |
Ordering::Greater, |
| 512 |
Ordering::Greater, |
| 513 |
Ordering::Greater, |
| 514 |
/* Test Tertiary > 26 */ |
| 515 |
Ordering::Less, |
| 516 |
Ordering::Less, |
| 517 |
Ordering::Greater, |
| 518 |
Ordering::Less, /* 30 */ |
| 519 |
Ordering::Greater, |
| 520 |
Ordering::Equal, |
| 521 |
Ordering::Greater, |
| 522 |
Ordering::Less, |
| 523 |
Ordering::Less, |
| 524 |
Ordering::Less, |
| 525 |
/* test identical > 36 */ |
| 526 |
Ordering::Equal, |
| 527 |
Ordering::Equal, |
| 528 |
/* test primary > 38 */ |
| 529 |
Ordering::Equal, |
| 530 |
Ordering::Equal, /* 40 */ |
| 531 |
Ordering::Less, |
| 532 |
Ordering::Equal, |
| 533 |
Ordering::Equal, |
| 534 |
/* test secondary > 43 */ |
| 535 |
Ordering::Less, |
| 536 |
Ordering::Less, |
| 537 |
Ordering::Equal, |
| 538 |
Ordering::Less, |
| 539 |
Ordering::Less, |
| 540 |
Ordering::Less, // 49 |
| 541 |
]; |
| 542 |
|
| 543 |
let mut options = CollatorOptions::default(); |
| 544 |
options.strength = Some(Strength::Tertiary); |
| 545 |
|
| 546 |
{ |
| 547 |
// Note: English uses the root collation |
| 548 |
let collator = Collator::try_new(Default::default(), options).unwrap(); |
| 549 |
check_expectations(&collator, &left[..38], &right[..38], &expectations[..38]); |
| 550 |
} |
| 551 |
|
| 552 |
options.strength = Some(Strength::Primary); |
| 553 |
|
| 554 |
{ |
| 555 |
// Note: English uses the root collation |
| 556 |
let collator = Collator::try_new(Default::default(), options).unwrap(); |
| 557 |
check_expectations( |
| 558 |
&collator, |
| 559 |
&left[38..43], |
| 560 |
&right[38..43], |
| 561 |
&expectations[38..43], |
| 562 |
); |
| 563 |
} |
| 564 |
|
| 565 |
options.strength = Some(Strength::Secondary); |
| 566 |
|
| 567 |
{ |
| 568 |
// Note: English uses the root collation |
| 569 |
let collator = Collator::try_new(Default::default(), options).unwrap(); |
| 570 |
check_expectations(&collator, &left[43..], &right[43..], &expectations[43..]); |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
#[test] |
| 575 |
fn test_en_bugs() { |
| 576 |
// Adapted from encoll.cpp in ICU4C |
| 577 |
let bugs = ["a", "A", "e", "E", "é", "è", "ê", "ë", "ea", "x"]; |
| 578 |
// let prefs = locale!("en").into(); |
| 579 |
let prefs = Default::default(); // English uses the root collation |
| 580 |
|
| 581 |
let mut options = CollatorOptions::default(); |
| 582 |
options.strength = Some(Strength::Tertiary); |
| 583 |
|
| 584 |
{ |
| 585 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 586 |
let mut outer = bugs.iter(); |
| 587 |
while let Some(left) = outer.next() { |
| 588 |
let inner = outer.clone(); |
| 589 |
for right in inner { |
| 590 |
assert_eq!(collator.compare(left, right), Ordering::Less); |
| 591 |
} |
| 592 |
} |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
#[test] |
| 597 |
fn test_ja_tertiary() { |
| 598 |
// Adapted from `CollationKanaTest::TestTertiary` in jacoll.cpp in ICU4C |
| 599 |
let left = [ |
| 600 |
"゙", // half-width |
| 601 |
"あ", |
| 602 |
"ア", |
| 603 |
"ああ", |
| 604 |
"アー", |
| 605 |
"アート", |
| 606 |
]; |
| 607 |
let right = [ |
| 608 |
"゚", // half-width |
| 609 |
"ア", |
| 610 |
"ああ", |
| 611 |
"アー", |
| 612 |
"アート", |
| 613 |
"ああと", |
| 614 |
]; |
| 615 |
let expectations = [ |
| 616 |
Ordering::Less, |
| 617 |
Ordering::Equal, // Katakanas and Hiraganas are equal on tertiary level |
| 618 |
Ordering::Less, |
| 619 |
Ordering::Greater, // Prolonged sound mark sorts BEFORE equivalent vowel |
| 620 |
Ordering::Less, |
| 621 |
Ordering::Less, // Prolonged sound mark sorts BEFORE equivalent vowel |
| 622 |
]; |
| 623 |
let prefs = locale!("ja").into(); |
| 624 |
|
| 625 |
let mut options = CollatorOptions::default(); |
| 626 |
options.strength = Some(Strength::Tertiary); |
| 627 |
options.case_level = Some(CaseLevel::On); |
| 628 |
|
| 629 |
{ |
| 630 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 631 |
check_expectations(&collator, &left, &right, &expectations); |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
#[test] |
| 636 |
fn test_ja_base() { |
| 637 |
// Adapted from `CollationKanaTest::TestBase` in jacoll.cpp of ICU4C. |
| 638 |
let cases = ["カ", "カキ", "キ", "キキ"]; |
| 639 |
|
| 640 |
let prefs = locale!("ja").into(); |
| 641 |
|
| 642 |
let mut options = CollatorOptions::default(); |
| 643 |
options.strength = Some(Strength::Primary); |
| 644 |
|
| 645 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 646 |
let mut case_iter = cases.iter(); |
| 647 |
while let Some(lower) = case_iter.next() { |
| 648 |
let tail = case_iter.clone(); |
| 649 |
for higher in tail { |
| 650 |
assert_eq!(collator.compare(lower, higher), Ordering::Less); |
| 651 |
} |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
#[test] |
| 656 |
fn test_ja_plain_dakuten_handakuten() { |
| 657 |
// Adapted from `CollationKanaTest::TestPlainDakutenHandakuten` in jacoll.cpp of ICU4C. |
| 658 |
let cases = ["ハカ", "バカ", "ハキ", "バキ"]; |
| 659 |
|
| 660 |
let prefs = locale!("ja").into(); |
| 661 |
|
| 662 |
let mut options = CollatorOptions::default(); |
| 663 |
options.strength = Some(Strength::Secondary); |
| 664 |
|
| 665 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 666 |
let mut case_iter = cases.iter(); |
| 667 |
while let Some(lower) = case_iter.next() { |
| 668 |
let tail = case_iter.clone(); |
| 669 |
for higher in tail { |
| 670 |
assert_eq!(collator.compare(lower, higher), Ordering::Less); |
| 671 |
} |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
#[test] |
| 676 |
fn test_ja_small_large() { |
| 677 |
// Adapted from `CollationKanaTest::TestSmallLarge` in jacoll.cpp of ICU4C. |
| 678 |
let cases = ["ッハ", "ツハ", "ッバ", "ツバ"]; |
| 679 |
|
| 680 |
let prefs = locale!("ja").into(); |
| 681 |
|
| 682 |
let mut options = CollatorOptions::default(); |
| 683 |
options.strength = Some(Strength::Tertiary); |
| 684 |
options.case_level = Some(CaseLevel::On); |
| 685 |
|
| 686 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 687 |
let mut case_iter = cases.iter(); |
| 688 |
while let Some(lower) = case_iter.next() { |
| 689 |
let tail = case_iter.clone(); |
| 690 |
for higher in tail { |
| 691 |
assert_eq!(collator.compare(lower, higher), Ordering::Less); |
| 692 |
} |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
#[test] |
| 697 |
fn test_ja_hiragana_katakana() { |
| 698 |
// Adapted from `CollationKanaTest::TestKatakanaHiragana` in jacoll.cpp of ICU4C. |
| 699 |
let cases = ["あッ", "アッ", "あツ", "アツ"]; |
| 700 |
|
| 701 |
let prefs = locale!("ja").into(); |
| 702 |
|
| 703 |
let mut options = CollatorOptions::default(); |
| 704 |
options.strength = Some(Strength::Quaternary); |
| 705 |
options.case_level = Some(CaseLevel::On); |
| 706 |
|
| 707 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 708 |
let mut case_iter = cases.iter(); |
| 709 |
while let Some(lower) = case_iter.next() { |
| 710 |
let tail = case_iter.clone(); |
| 711 |
for higher in tail { |
| 712 |
assert_eq!(collator.compare(lower, higher), Ordering::Less); |
| 713 |
} |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
#[test] |
| 718 |
fn test_ja_hiragana_katakana_utf16() { |
| 719 |
// Adapted from `CollationKanaTest::TestKatakanaHiragana` in jacoll.cpp of ICU4C. |
| 720 |
let cases = [ |
| 721 |
&[0x3042u16, 0x30C3u16], |
| 722 |
&[0x30A2u16, 0x30C3u16], |
| 723 |
&[0x3042u16, 0x30C4u16], |
| 724 |
&[0x30A2u16, 0x30C4u16], |
| 725 |
]; |
| 726 |
|
| 727 |
let prefs = locale!("ja").into(); |
| 728 |
|
| 729 |
let mut options = CollatorOptions::default(); |
| 730 |
options.strength = Some(Strength::Quaternary); |
| 731 |
options.case_level = Some(CaseLevel::On); |
| 732 |
|
| 733 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 734 |
let mut case_iter = cases.iter(); |
| 735 |
while let Some(lower) = case_iter.next() { |
| 736 |
let tail = case_iter.clone(); |
| 737 |
for higher in tail { |
| 738 |
assert_eq!( |
| 739 |
collator.compare_utf16(&lower[..], &higher[..]), |
| 740 |
Ordering::Less |
| 741 |
); |
| 742 |
} |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
#[test] |
| 747 |
fn test_ja_chooon_kigoo() { |
| 748 |
// Adapted from `CollationKanaTest::TestChooonKigoo` in jacoll.cpp of ICU4C. |
| 749 |
let cases = [ |
| 750 |
"カーあ", |
| 751 |
"カーア", |
| 752 |
"カイあ", |
| 753 |
"カイア", |
| 754 |
"キーあ", /* Prolonged sound mark sorts BEFORE equivalent vowel (ICU 2.0) */ |
| 755 |
"キーア", /* Prolonged sound mark sorts BEFORE equivalent vowel (ICU 2.0) */ |
| 756 |
"キイあ", |
| 757 |
"キイア", |
| 758 |
]; |
| 759 |
|
| 760 |
let prefs = locale!("ja").into(); |
| 761 |
|
| 762 |
let mut options = CollatorOptions::default(); |
| 763 |
options.strength = Some(Strength::Quaternary); |
| 764 |
options.case_level = Some(CaseLevel::On); |
| 765 |
|
| 766 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 767 |
let mut case_iter = cases.iter(); |
| 768 |
while let Some(lower) = case_iter.next() { |
| 769 |
let tail = case_iter.clone(); |
| 770 |
for higher in tail { |
| 771 |
assert_eq!(collator.compare(lower, higher), Ordering::Less); |
| 772 |
} |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
#[test] |
| 777 |
fn test_ja_unihan() { |
| 778 |
let left = vec!["川", "東京", "あい", "飛行機"]; |
| 779 |
let right = vec!["州", "京都", "愛", "飞行机"]; |
| 780 |
|
| 781 |
{ |
| 782 |
let prefs = locale!("ja").into(); |
| 783 |
let options = CollatorOptions::default(); |
| 784 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 785 |
let expectations_ja = vec![ |
| 786 |
Ordering::Greater, |
| 787 |
Ordering::Greater, |
| 788 |
Ordering::Less, |
| 789 |
Ordering::Less, |
| 790 |
]; |
| 791 |
check_expectations(&collator, &left, &right, &expectations_ja); |
| 792 |
} |
| 793 |
|
| 794 |
{ |
| 795 |
let prefs = locale!("ja-u-co-unihan").into(); |
| 796 |
let collator = Collator::try_new(prefs, Default::default()).unwrap(); |
| 797 |
let expectations_ja_unihan = vec![ |
| 798 |
Ordering::Less, // Ordering changes |
| 799 |
Ordering::Greater, |
| 800 |
Ordering::Less, |
| 801 |
Ordering::Less, |
| 802 |
]; |
| 803 |
check_expectations(&collator, &left, &right, &expectations_ja_unihan); |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
// TODO: Test Swedish and Chinese also, since they have unusual |
| 808 |
// variant defaults. (But are currently not part of the test data.) |
| 809 |
#[test] |
| 810 |
fn test_region_fallback() { |
| 811 |
// There's no explicit fi-FI data. |
| 812 |
let prefs = locale!("fi-FI").into(); |
| 813 |
|
| 814 |
let collator = Collator::try_new(prefs, Default::default()).unwrap(); |
| 815 |
assert_all_comparisons(&collator, "ä", "z", Ordering::Greater); |
| 816 |
} |
| 817 |
|
| 818 |
#[test] |
| 819 |
fn test_reordering() { |
| 820 |
let prefs = locale!("bn").into(); |
| 821 |
|
| 822 |
// অ is Bangla |
| 823 |
// ऄ is Devanagari |
| 824 |
|
| 825 |
{ |
| 826 |
let collator = Collator::try_new(Default::default(), Default::default()).unwrap(); |
| 827 |
assert_eq!(collator.compare("অ", "a"), Ordering::Greater); |
| 828 |
assert_eq!(collator.compare("ऄ", "a"), Ordering::Greater); |
| 829 |
assert_eq!(collator.compare("অ", "ऄ"), Ordering::Greater); |
| 830 |
} |
| 831 |
|
| 832 |
{ |
| 833 |
let collator = Collator::try_new(prefs, Default::default()).unwrap(); |
| 834 |
assert_eq!(collator.compare("অ", "a"), Ordering::Less); |
| 835 |
assert_eq!(collator.compare("ऄ", "a"), Ordering::Less); |
| 836 |
assert_eq!(collator.compare("অ", "ऄ"), Ordering::Less); |
| 837 |
} |
| 838 |
} |
| 839 |
|
| 840 |
#[test] |
| 841 |
fn test_reordering_owned() { |
| 842 |
let prefs = locale!("bn").into(); |
| 843 |
|
| 844 |
// অ is Bangla |
| 845 |
// ऄ is Devanagari |
| 846 |
|
| 847 |
{ |
| 848 |
let owned = Collator::try_new_unstable( |
| 849 |
&TestingProvider, |
| 850 |
Default::default(), |
| 851 |
CollatorOptions::default(), |
| 852 |
) |
| 853 |
.unwrap(); |
| 854 |
let collator = owned.as_borrowed(); |
| 855 |
assert_eq!(collator.compare("অ", "a"), Ordering::Greater); |
| 856 |
assert_eq!(collator.compare("ऄ", "a"), Ordering::Greater); |
| 857 |
assert_eq!(collator.compare("অ", "ऄ"), Ordering::Greater); |
| 858 |
} |
| 859 |
|
| 860 |
{ |
| 861 |
let owned = |
| 862 |
Collator::try_new_unstable(&TestingProvider, prefs, Default::default()).unwrap(); |
| 863 |
let collator = owned.as_borrowed(); |
| 864 |
assert_eq!(collator.compare("অ", "a"), Ordering::Less); |
| 865 |
assert_eq!(collator.compare("ऄ", "a"), Ordering::Less); |
| 866 |
assert_eq!(collator.compare("অ", "ऄ"), Ordering::Less); |
| 867 |
} |
| 868 |
} |
| 869 |
|
| 870 |
#[test] |
| 871 |
fn test_vi() { |
| 872 |
{ |
| 873 |
let prefs = locale!("vi").into(); |
| 874 |
let collator = Collator::try_new(prefs, Default::default()).unwrap(); |
| 875 |
|
| 876 |
assert_eq!(collator.compare("a", "b"), Ordering::Less); |
| 877 |
assert_eq!(collator.compare("a", "á"), Ordering::Less); |
| 878 |
assert_eq!(collator.compare("à", "á"), Ordering::Less); |
| 879 |
assert_eq!(collator.compare("ả", "ã"), Ordering::Less); |
| 880 |
assert_eq!(collator.compare("ạ", "a"), Ordering::Greater); |
| 881 |
assert_eq!(collator.compare("ê", "ế"), Ordering::Less); |
| 882 |
assert_eq!(collator.compare("u", "ư"), Ordering::Less); |
| 883 |
assert_eq!(collator.compare("d", "đ"), Ordering::Less); |
| 884 |
assert_eq!(collator.compare("ô", "ơ"), Ordering::Less); |
| 885 |
assert_eq!(collator.compare("â", "ấ"), Ordering::Less); // Similar sounding |
| 886 |
} |
| 887 |
|
| 888 |
{ |
| 889 |
let collator = Collator::try_new(Default::default(), Default::default()).unwrap(); |
| 890 |
|
| 891 |
assert_eq!(collator.compare("a", "b"), Ordering::Less); |
| 892 |
assert_eq!(collator.compare("a", "á"), Ordering::Less); |
| 893 |
assert_eq!(collator.compare("à", "á"), Ordering::Greater); |
| 894 |
assert_eq!(collator.compare("ả", "ã"), Ordering::Greater); |
| 895 |
assert_eq!(collator.compare("ạ", "a"), Ordering::Greater); |
| 896 |
assert_eq!(collator.compare("ê", "ế"), Ordering::Less); |
| 897 |
assert_eq!(collator.compare("u", "ư"), Ordering::Less); |
| 898 |
assert_eq!(collator.compare("d", "đ"), Ordering::Less); |
| 899 |
assert_eq!(collator.compare("ô", "ơ"), Ordering::Less); |
| 900 |
assert_eq!(collator.compare("â", "ấ"), Ordering::Less); // Similar sounding |
| 901 |
} |
| 902 |
} |
| 903 |
|
| 904 |
#[test] |
| 905 |
fn test_vi_owned() { |
| 906 |
{ |
| 907 |
let prefs = locale!("vi").into(); |
| 908 |
let owned = |
| 909 |
Collator::try_new_unstable(&TestingProvider, prefs, Default::default()).unwrap(); |
| 910 |
let collator = owned.as_borrowed(); |
| 911 |
|
| 912 |
assert_eq!(collator.compare("a", "b"), Ordering::Less); |
| 913 |
assert_eq!(collator.compare("a", "á"), Ordering::Less); |
| 914 |
assert_eq!(collator.compare("à", "á"), Ordering::Less); |
| 915 |
assert_eq!(collator.compare("ả", "ã"), Ordering::Less); |
| 916 |
assert_eq!(collator.compare("ạ", "a"), Ordering::Greater); |
| 917 |
assert_eq!(collator.compare("ê", "ế"), Ordering::Less); |
| 918 |
assert_eq!(collator.compare("u", "ư"), Ordering::Less); |
| 919 |
assert_eq!(collator.compare("d", "đ"), Ordering::Less); |
| 920 |
assert_eq!(collator.compare("ô", "ơ"), Ordering::Less); |
| 921 |
assert_eq!(collator.compare("â", "ấ"), Ordering::Less); // Similar sounding |
| 922 |
} |
| 923 |
|
| 924 |
{ |
| 925 |
let owned = Collator::try_new_unstable( |
| 926 |
&TestingProvider, |
| 927 |
Default::default(), |
| 928 |
CollatorOptions::default(), |
| 929 |
) |
| 930 |
.unwrap(); |
| 931 |
let collator = owned.as_borrowed(); |
| 932 |
|
| 933 |
assert_eq!(collator.compare("a", "b"), Ordering::Less); |
| 934 |
assert_eq!(collator.compare("a", "á"), Ordering::Less); |
| 935 |
assert_eq!(collator.compare("à", "á"), Ordering::Greater); |
| 936 |
assert_eq!(collator.compare("ả", "ã"), Ordering::Greater); |
| 937 |
assert_eq!(collator.compare("ạ", "a"), Ordering::Greater); |
| 938 |
assert_eq!(collator.compare("ê", "ế"), Ordering::Less); |
| 939 |
assert_eq!(collator.compare("u", "ư"), Ordering::Less); |
| 940 |
assert_eq!(collator.compare("d", "đ"), Ordering::Less); |
| 941 |
assert_eq!(collator.compare("ô", "ơ"), Ordering::Less); |
| 942 |
assert_eq!(collator.compare("â", "ấ"), Ordering::Less); // Similar sounding |
| 943 |
} |
| 944 |
} |
| 945 |
|
| 946 |
#[test] |
| 947 |
fn test_zh() { |
| 948 |
// Note: ㄅ is Bopomofo. |
| 949 |
|
| 950 |
assert_root(Default::default()); |
| 951 |
|
| 952 |
assert_pinyin(locale!("zh").into()); |
| 953 |
assert_pinyin(locale!("zh-Hans").into()); |
| 954 |
assert_pinyin(locale!("zh-Hans-HK").into()); |
| 955 |
assert_pinyin(locale!("zh-Hant-u-co-pinyin").into()); |
| 956 |
assert_pinyin(locale!("zh-TW-u-co-pinyin").into()); |
| 957 |
assert_pinyin(locale!("yue-CN").into()); |
| 958 |
|
| 959 |
assert_stroke(locale!("zh-Hant").into()); |
| 960 |
assert_stroke(locale!("zh-HK").into()); |
| 961 |
assert_stroke(locale!("zh-MO").into()); |
| 962 |
assert_stroke(locale!("zh-TW").into()); |
| 963 |
assert_stroke(locale!("zh-CN-u-co-stroke").into()); |
| 964 |
assert_stroke(locale!("zh-Hans-u-co-stroke").into()); |
| 965 |
assert_stroke(locale!("yue").into()); |
| 966 |
|
| 967 |
assert_zhuyin(locale!("zh-u-co-zhuyin").into()); |
| 968 |
assert_unihan(locale!("zh-u-co-unihan").into()); |
| 969 |
|
| 970 |
fn assert_root(prefs: CollatorPreferences) { |
| 971 |
let collator = Collator::try_new(prefs, Default::default()).unwrap(); |
| 972 |
assert_eq!(collator.compare("艾", "a"), Ordering::Greater); |
| 973 |
assert_eq!(collator.compare("佰", "a"), Ordering::Greater); |
| 974 |
assert_eq!(collator.compare("ㄅ", "a"), Ordering::Greater); |
| 975 |
assert_eq!(collator.compare("ㄅ", "ж"), Ordering::Greater); |
| 976 |
assert_eq!(collator.compare("艾", "佰"), Ordering::Greater); |
| 977 |
assert_eq!(collator.compare("艾", "ㄅ"), Ordering::Greater); |
| 978 |
assert_eq!(collator.compare("佰", "ㄅ"), Ordering::Greater); |
| 979 |
assert_eq!(collator.compare("不", "把"), Ordering::Less); |
| 980 |
} |
| 981 |
|
| 982 |
fn assert_pinyin(prefs: CollatorPreferences) { |
| 983 |
let collator = Collator::try_new(prefs, Default::default()).unwrap(); |
| 984 |
assert_eq!(collator.compare("艾", "a"), Ordering::Less); |
| 985 |
assert_eq!(collator.compare("佰", "a"), Ordering::Less); |
| 986 |
assert_eq!(collator.compare("ㄅ", "a"), Ordering::Greater); |
| 987 |
assert_eq!(collator.compare("ㄅ", "ж"), Ordering::Greater); |
| 988 |
assert_eq!(collator.compare("艾", "佰"), Ordering::Less); |
| 989 |
assert_eq!(collator.compare("艾", "ㄅ"), Ordering::Less); |
| 990 |
assert_eq!(collator.compare("佰", "ㄅ"), Ordering::Less); |
| 991 |
assert_eq!(collator.compare("不", "把"), Ordering::Greater); |
| 992 |
} |
| 993 |
|
| 994 |
fn assert_stroke(prefs: CollatorPreferences) { |
| 995 |
let collator = Collator::try_new(prefs, Default::default()).unwrap(); |
| 996 |
assert_eq!(collator.compare("艾", "a"), Ordering::Less); |
| 997 |
assert_eq!(collator.compare("佰", "a"), Ordering::Less); |
| 998 |
assert_eq!(collator.compare("ㄅ", "a"), Ordering::Less); |
| 999 |
assert_eq!(collator.compare("ㄅ", "ж"), Ordering::Less); |
| 1000 |
assert_eq!(collator.compare("艾", "佰"), Ordering::Less); |
| 1001 |
assert_eq!(collator.compare("艾", "ㄅ"), Ordering::Less); |
| 1002 |
assert_eq!(collator.compare("佰", "ㄅ"), Ordering::Less); |
| 1003 |
assert_eq!(collator.compare("不", "把"), Ordering::Less); |
| 1004 |
} |
| 1005 |
|
| 1006 |
fn assert_zhuyin(prefs: CollatorPreferences) { |
| 1007 |
let collator = Collator::try_new(prefs, Default::default()).unwrap(); |
| 1008 |
assert_eq!(collator.compare("艾", "a"), Ordering::Less); |
| 1009 |
assert_eq!(collator.compare("佰", "a"), Ordering::Less); |
| 1010 |
assert_eq!(collator.compare("ㄅ", "a"), Ordering::Less); |
| 1011 |
assert_eq!(collator.compare("ㄅ", "ж"), Ordering::Less); |
| 1012 |
assert_eq!(collator.compare("艾", "佰"), Ordering::Greater); |
| 1013 |
assert_eq!(collator.compare("艾", "ㄅ"), Ordering::Less); |
| 1014 |
assert_eq!(collator.compare("佰", "ㄅ"), Ordering::Less); |
| 1015 |
assert_eq!(collator.compare("不", "把"), Ordering::Greater); |
| 1016 |
} |
| 1017 |
|
| 1018 |
fn assert_unihan(prefs: CollatorPreferences) { |
| 1019 |
let collator = Collator::try_new(prefs, Default::default()).unwrap(); |
| 1020 |
assert_eq!(collator.compare("艾", "a"), Ordering::Less); |
| 1021 |
assert_eq!(collator.compare("佰", "a"), Ordering::Less); |
| 1022 |
assert_eq!(collator.compare("ㄅ", "a"), Ordering::Less); |
| 1023 |
assert_eq!(collator.compare("ㄅ", "ж"), Ordering::Less); |
| 1024 |
assert_eq!(collator.compare("艾", "佰"), Ordering::Greater); |
| 1025 |
assert_eq!(collator.compare("艾", "ㄅ"), Ordering::Less); |
| 1026 |
assert_eq!(collator.compare("佰", "ㄅ"), Ordering::Less); |
| 1027 |
assert_eq!(collator.compare("不", "把"), Ordering::Less); |
| 1028 |
} |
| 1029 |
// TODO: Test script and region aliases |
| 1030 |
} |
| 1031 |
|
| 1032 |
// TODO: frcoll requires support for fr-CA |
| 1033 |
|
| 1034 |
// TODO: Write a test for Bangla |
| 1035 |
|
| 1036 |
#[test] |
| 1037 |
fn test_es_tertiary() { |
| 1038 |
// Adapted from `CollationSpanishTest::TestTertiary` in escoll.cpp in ICU4C |
| 1039 |
let left = ["alias", "Elliot", "Hello", "acHc", "acc"]; |
| 1040 |
let right = ["allias", "Emiot", "hellO", "aCHc", "aCHc"]; |
| 1041 |
let expectations = [ |
| 1042 |
Ordering::Less, |
| 1043 |
Ordering::Less, |
| 1044 |
Ordering::Greater, |
| 1045 |
Ordering::Less, |
| 1046 |
Ordering::Less, |
| 1047 |
]; |
| 1048 |
let prefs = locale!("es").into(); |
| 1049 |
|
| 1050 |
let mut options = CollatorOptions::default(); |
| 1051 |
options.strength = Some(Strength::Tertiary); |
| 1052 |
|
| 1053 |
{ |
| 1054 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 1055 |
check_expectations(&collator, &left, &right, &expectations); |
| 1056 |
} |
| 1057 |
} |
| 1058 |
|
| 1059 |
#[test] |
| 1060 |
fn test_es_primary() { |
| 1061 |
// Adapted from `CollationSpanishTest::TestPrimary` in escoll.cpp in ICU4C |
| 1062 |
let left = ["alias", "acHc", "acc", "Hello"]; |
| 1063 |
let right = ["allias", "aCHc", "aCHc", "hellO"]; |
| 1064 |
let expectations = [ |
| 1065 |
Ordering::Less, |
| 1066 |
Ordering::Equal, |
| 1067 |
Ordering::Less, |
| 1068 |
Ordering::Equal, |
| 1069 |
]; |
| 1070 |
let prefs = locale!("es").into(); |
| 1071 |
|
| 1072 |
let mut options = CollatorOptions::default(); |
| 1073 |
options.strength = Some(Strength::Primary); |
| 1074 |
|
| 1075 |
{ |
| 1076 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 1077 |
check_expectations(&collator, &left, &right, &expectations); |
| 1078 |
} |
| 1079 |
} |
| 1080 |
|
| 1081 |
#[test] |
| 1082 |
fn test_el_secondary() { |
| 1083 |
// Adapted from `CollationRegressionTest::Test4095316` in regcoll.cpp of ICU4C. |
| 1084 |
let prefs = Default::default(); // Greek uses the root collation |
| 1085 |
|
| 1086 |
let mut options = CollatorOptions::default(); |
| 1087 |
options.strength = Some(Strength::Secondary); |
| 1088 |
|
| 1089 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 1090 |
assert_eq!(collator.compare("ϔ", "Ϋ"), Ordering::Equal); |
| 1091 |
} |
| 1092 |
|
| 1093 |
#[test] |
| 1094 |
fn test_th_dictionary() { |
| 1095 |
// Adapted from `CollationThaiTest::TestDictionary` of thcoll.cpp in ICU4C. |
| 1096 |
let dict = include_str!("data/riwords.txt") |
| 1097 |
.strip_prefix('\u{FEFF}') |
| 1098 |
.unwrap(); |
| 1099 |
let prefs = locale!("th").into(); |
| 1100 |
|
| 1101 |
let mut options = CollatorOptions::default(); |
| 1102 |
options.strength = Some(Strength::Quaternary); |
| 1103 |
|
| 1104 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 1105 |
let mut lines = dict.lines(); |
| 1106 |
let mut prev = loop { |
| 1107 |
if let Some(line) = lines.next() { |
| 1108 |
if line.starts_with('#') { |
| 1109 |
continue; |
| 1110 |
} |
| 1111 |
break line; |
| 1112 |
} else { |
| 1113 |
panic!("Malformed dictionary"); |
| 1114 |
} |
| 1115 |
}; |
| 1116 |
|
| 1117 |
for line in lines { |
| 1118 |
assert_eq!(collator.compare(prev, line), Ordering::Less); |
| 1119 |
prev = line; |
| 1120 |
} |
| 1121 |
} |
| 1122 |
|
| 1123 |
#[test] |
| 1124 |
fn test_th_corner_cases() { |
| 1125 |
// Adapted from `CollationThaiTest::TestCornerCases` in thcoll.cpp in ICU4C |
| 1126 |
let left = [ |
| 1127 |
// Shorter words precede longer |
| 1128 |
"\u{0E01}", |
| 1129 |
// Tone marks are considered after letters (i.e. are primary ignorable) |
| 1130 |
"\u{0E01}\u{0E32}", |
| 1131 |
// ditto for other over-marks |
| 1132 |
"\u{0E01}\u{0E32}", |
| 1133 |
// commonly used mark-in-context order. |
| 1134 |
// In effect, marks are sorted after each syllable. |
| 1135 |
"\u{0E01}\u{0E32}\u{0E01}\u{0E49}\u{0E32}", |
| 1136 |
// Hyphens and other punctuation follow whitespace but come before letters |
| 1137 |
// "\u{0E01}\u{0E32}", |
| 1138 |
"\u{0E01}\u{0E32}-", |
| 1139 |
// Doubler follows an identical word without the doubler |
| 1140 |
// "\u{0E01}\u{0E32}", |
| 1141 |
"\u{0E01}\u{0E32}\u{0E46}", |
| 1142 |
// \u{0E45} after either \u{0E24} or \{u0E26} is treated as a single |
| 1143 |
// combining character, similar to "c < ch" in traditional spanish. |
| 1144 |
"\u{0E24}\u{0E29}\u{0E35}", |
| 1145 |
"\u{0E26}\u{0E29}\u{0E35}", |
| 1146 |
// Vowels reorder, should compare \u{0E2D} and \u{0E34} |
| 1147 |
// (Should the middle code point differ between the two strings?) |
| 1148 |
"\u{0E40}\u{0E01}\u{0E2D}", |
| 1149 |
// Tones are compared after the rest of the word (e.g. primary ignorable) |
| 1150 |
"\u{0E01}\u{0E32}\u{0E01}\u{0E48}\u{0E32}", |
| 1151 |
// Periods are ignored entirely |
| 1152 |
"\u{0E01}.\u{0E01}.", |
| 1153 |
]; |
| 1154 |
let right = [ |
| 1155 |
"\u{0E01}\u{0E01}", |
| 1156 |
"\u{0E01}\u{0E49}\u{0E32}", |
| 1157 |
"\u{0E01}\u{0E32}\u{0E4C}", |
| 1158 |
"\u{0E01}\u{0E48}\u{0E32}\u{0E01}\u{0E49}\u{0E32}", |
| 1159 |
// "\u{0E01}\u{0E32}-", |
| 1160 |
"\u{0E01}\u{0E32}\u{0E01}\u{0E32}", |
| 1161 |
// "\u{0E01}\u{0E32}\u{0E46}", |
| 1162 |
"\u{0E01}\u{0E32}\u{0E01}\u{0E32}", |
| 1163 |
"\u{0E24}\u{0E45}\u{0E29}\u{0E35}", |
| 1164 |
"\u{0E26}\u{0E45}\u{0E29}\u{0E35}", |
| 1165 |
"\u{0E40}\u{0E01}\u{0E34}", |
| 1166 |
"\u{0E01}\u{0E49}\u{0E32}\u{0E01}\u{0E32}", |
| 1167 |
"\u{0E01}\u{0E32}", |
| 1168 |
]; |
| 1169 |
let expectations = [ |
| 1170 |
Ordering::Less, |
| 1171 |
Ordering::Less, |
| 1172 |
Ordering::Less, |
| 1173 |
Ordering::Less, |
| 1174 |
// Ordering::Equal, |
| 1175 |
Ordering::Less, |
| 1176 |
// Ordering::Equal, |
| 1177 |
Ordering::Less, |
| 1178 |
Ordering::Less, |
| 1179 |
Ordering::Less, |
| 1180 |
Ordering::Less, |
| 1181 |
Ordering::Less, |
| 1182 |
Ordering::Less, |
| 1183 |
]; |
| 1184 |
let prefs = locale!("th").into(); |
| 1185 |
{ |
| 1186 |
// TODO(#2013): Check why the commented-out cases fail |
| 1187 |
let collator = Collator::try_new(prefs, Default::default()).unwrap(); |
| 1188 |
check_expectations(&collator, &left, &right, &expectations); |
| 1189 |
} |
| 1190 |
} |
| 1191 |
|
| 1192 |
#[test] |
| 1193 |
fn test_th_reordering() { |
| 1194 |
// Adapted from `CollationThaiTest::TestReordering` in thcoll.cpp in ICU4C |
| 1195 |
let left = [ |
| 1196 |
// composition |
| 1197 |
"\u{0E41}c\u{0301}", |
| 1198 |
// supplementaries |
| 1199 |
// TODO(#2013): Why does this fail? |
| 1200 |
// "\u{0E41}\u{1D7CE}", |
| 1201 |
// supplementary composition decomps to supplementary |
| 1202 |
"\u{0E41}\u{1D15F}", |
| 1203 |
// supplementary composition decomps to BMP |
| 1204 |
"\u{0E41}\u{2F802}", /* omit backward iteration tests |
| 1205 |
* contraction bug |
| 1206 |
* "\u{0E24}\u{0E41}", |
| 1207 |
* TODO: Support contracting starters, then add more here */ |
| 1208 |
]; |
| 1209 |
let right = [ |
| 1210 |
"\u{0E41}\u{0107}", |
| 1211 |
// "\u{0E41}\u{1D7CF}", |
| 1212 |
"\u{0E41}\u{1D158}\u{1D165}", |
| 1213 |
"\u{0E41}\u{4E41}", // "\u{0E41}\u{0E24}", |
| 1214 |
]; |
| 1215 |
let expectations = [ |
| 1216 |
Ordering::Equal, |
| 1217 |
// Ordering::Less, |
| 1218 |
Ordering::Equal, |
| 1219 |
Ordering::Equal, |
| 1220 |
// Ordering::Equal, |
| 1221 |
]; |
| 1222 |
let prefs = locale!("th").into(); |
| 1223 |
|
| 1224 |
let mut options = CollatorOptions::default(); |
| 1225 |
options.strength = Some(Strength::Secondary); |
| 1226 |
|
| 1227 |
{ |
| 1228 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 1229 |
check_expectations(&collator, &left, &right, &expectations); |
| 1230 |
} |
| 1231 |
} |
| 1232 |
|
| 1233 |
#[test] |
| 1234 |
fn test_tr_tertiary() { |
| 1235 |
// Adapted from `CollationTurkishTest::TestTertiary` in trcoll.cpp in ICU4C |
| 1236 |
let left = ["ş", "vät", "old", "üoid", "hĞalt", "stresŞ", "voıd", "idea"]; |
| 1237 |
let right = ["ü", "vbt", "Öay", "void", "halt", "ŞtreŞs", "void", "Idea"]; |
| 1238 |
let expectations = [ |
| 1239 |
Ordering::Less, |
| 1240 |
Ordering::Less, |
| 1241 |
Ordering::Less, |
| 1242 |
Ordering::Less, |
| 1243 |
Ordering::Greater, |
| 1244 |
Ordering::Less, |
| 1245 |
Ordering::Less, |
| 1246 |
Ordering::Greater, |
| 1247 |
]; |
| 1248 |
let prefs = locale!("tr").into(); |
| 1249 |
|
| 1250 |
let mut options = CollatorOptions::default(); |
| 1251 |
options.strength = Some(Strength::Tertiary); |
| 1252 |
|
| 1253 |
{ |
| 1254 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 1255 |
check_expectations(&collator, &left, &right, &expectations); |
| 1256 |
} |
| 1257 |
} |
| 1258 |
|
| 1259 |
#[test] |
| 1260 |
fn test_tr_primary() { |
| 1261 |
// Adapted from `CollationTurkishTest::TestPrimary` in trcoll.cpp in ICU4C |
| 1262 |
let left = ["üoid", "voıd", "idea"]; |
| 1263 |
let right = ["void", "void", "Idea"]; |
| 1264 |
let expectations = [Ordering::Less, Ordering::Less, Ordering::Greater]; |
| 1265 |
let prefs = locale!("tr").into(); |
| 1266 |
|
| 1267 |
let mut options = CollatorOptions::default(); |
| 1268 |
options.strength = Some(Strength::Tertiary); |
| 1269 |
|
| 1270 |
{ |
| 1271 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 1272 |
check_expectations(&collator, &left, &right, &expectations); |
| 1273 |
} |
| 1274 |
} |
| 1275 |
|
| 1276 |
#[test] |
| 1277 |
fn test_tr_primary_owned() { |
| 1278 |
// Adapted from `CollationTurkishTest::TestPrimary` in trcoll.cpp in ICU4C |
| 1279 |
let left = ["üoid", "voıd", "idea"]; |
| 1280 |
let right = ["void", "void", "Idea"]; |
| 1281 |
let expectations = [Ordering::Less, Ordering::Less, Ordering::Greater]; |
| 1282 |
let prefs = locale!("tr").into(); |
| 1283 |
|
| 1284 |
let mut options = CollatorOptions::default(); |
| 1285 |
options.strength = Some(Strength::Tertiary); |
| 1286 |
|
| 1287 |
{ |
| 1288 |
let owned = Collator::try_new_unstable(&TestingProvider, prefs, options).unwrap(); |
| 1289 |
let collator = owned.as_borrowed(); |
| 1290 |
check_expectations(&collator, &left, &right, &expectations); |
| 1291 |
} |
| 1292 |
} |
| 1293 |
|
| 1294 |
#[test] |
| 1295 |
fn test_lt_tertiary() { |
| 1296 |
let left = [ |
| 1297 |
"a\u{0307}\u{0300}a", |
| 1298 |
"a\u{0307}\u{0301}a", |
| 1299 |
"a\u{0307}\u{0302}a", |
| 1300 |
"a\u{0307}\u{0303}a", |
| 1301 |
"ž", |
| 1302 |
]; |
| 1303 |
let right = [ |
| 1304 |
"a\u{0300}a", |
| 1305 |
"a\u{0301}a", |
| 1306 |
"a\u{0302}a", |
| 1307 |
"a\u{0303}a", |
| 1308 |
"z\u{033F}", |
| 1309 |
]; |
| 1310 |
let expectations = [ |
| 1311 |
Ordering::Equal, |
| 1312 |
Ordering::Equal, |
| 1313 |
Ordering::Greater, |
| 1314 |
Ordering::Equal, |
| 1315 |
Ordering::Greater, |
| 1316 |
]; |
| 1317 |
let prefs = locale!("lt").into(); |
| 1318 |
|
| 1319 |
let mut options = CollatorOptions::default(); |
| 1320 |
options.strength = Some(Strength::Tertiary); |
| 1321 |
|
| 1322 |
{ |
| 1323 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 1324 |
check_expectations(&collator, &left, &right, &expectations); |
| 1325 |
} |
| 1326 |
} |
| 1327 |
|
| 1328 |
#[test] |
| 1329 |
fn test_lt_tertiary_owned() { |
| 1330 |
let left = [ |
| 1331 |
"a\u{0307}\u{0300}a", |
| 1332 |
"a\u{0307}\u{0301}a", |
| 1333 |
"a\u{0307}\u{0302}a", |
| 1334 |
"a\u{0307}\u{0303}a", |
| 1335 |
"ž", |
| 1336 |
]; |
| 1337 |
let right = [ |
| 1338 |
"a\u{0300}a", |
| 1339 |
"a\u{0301}a", |
| 1340 |
"a\u{0302}a", |
| 1341 |
"a\u{0303}a", |
| 1342 |
"z\u{033F}", |
| 1343 |
]; |
| 1344 |
let expectations = [ |
| 1345 |
Ordering::Equal, |
| 1346 |
Ordering::Equal, |
| 1347 |
Ordering::Greater, |
| 1348 |
Ordering::Equal, |
| 1349 |
Ordering::Greater, |
| 1350 |
]; |
| 1351 |
let prefs = locale!("lt").into(); |
| 1352 |
|
| 1353 |
let mut options = CollatorOptions::default(); |
| 1354 |
options.strength = Some(Strength::Tertiary); |
| 1355 |
|
| 1356 |
{ |
| 1357 |
let owned = Collator::try_new_unstable(&TestingProvider, prefs, options).unwrap(); |
| 1358 |
let collator = owned.as_borrowed(); |
| 1359 |
check_expectations(&collator, &left, &right, &expectations); |
| 1360 |
} |
| 1361 |
} |
| 1362 |
|
| 1363 |
#[test] |
| 1364 |
fn test_lt_primary() { |
| 1365 |
let left = ["ž"]; |
| 1366 |
let right = ["z"]; |
| 1367 |
let expectations = [Ordering::Greater]; |
| 1368 |
let prefs = locale!("lt").into(); |
| 1369 |
|
| 1370 |
let mut options = CollatorOptions::default(); |
| 1371 |
options.strength = Some(Strength::Primary); |
| 1372 |
|
| 1373 |
{ |
| 1374 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 1375 |
check_expectations(&collator, &left, &right, &expectations); |
| 1376 |
} |
| 1377 |
} |
| 1378 |
#[test] |
| 1379 |
fn test_fi() { |
| 1380 |
// Adapted from ficoll.cpp in ICU4C |
| 1381 |
// Testing that w and v behave as in the root collation is for checking |
| 1382 |
// that the sorting collation doesn't exhibit the behavior of the search |
| 1383 |
// collation, which (somewhat questionably) treats w and v as primary-equal. |
| 1384 |
let left = [ |
| 1385 |
"wat", |
| 1386 |
"vat", |
| 1387 |
"aübeck", |
| 1388 |
"Låvi", |
| 1389 |
// ICU4C has a duplicate of the case below. |
| 1390 |
// The duplicate is omitted here. |
| 1391 |
// Instead, the subsequent tests are added for ICU4X. |
| 1392 |
"ä", |
| 1393 |
"a\u{0308}", |
| 1394 |
]; |
| 1395 |
let right = ["vat", "way", "axbeck", "Läwe", "o", "ä"]; |
| 1396 |
let expectation = [ |
| 1397 |
Ordering::Greater, |
| 1398 |
Ordering::Less, |
| 1399 |
Ordering::Greater, |
| 1400 |
Ordering::Less, |
| 1401 |
Ordering::Greater, |
| 1402 |
Ordering::Equal, |
| 1403 |
]; |
| 1404 |
let prefs = locale!("fi").into(); |
| 1405 |
let mut options = CollatorOptions::default(); |
| 1406 |
|
| 1407 |
options.strength = Some(Strength::Tertiary); |
| 1408 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 1409 |
check_expectations(&collator, &left, &right, &expectation); |
| 1410 |
|
| 1411 |
options.strength = Some(Strength::Primary); |
| 1412 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 1413 |
check_expectations(&collator, &left, &right, &expectation); |
| 1414 |
} |
| 1415 |
|
| 1416 |
#[test] |
| 1417 |
fn test_sv() { |
| 1418 |
// This is the same as test_fi. The purpose of this copy is to test that |
| 1419 |
// Swedish defaults to "reformed", which behaves like Finnish "standard", |
| 1420 |
// and not to "standard", which behaves like Finnish "traditional". |
| 1421 |
|
| 1422 |
// Adapted from ficoll.cpp in ICU4C |
| 1423 |
// Testing that w and v behave as in the root collation is for checking |
| 1424 |
// that the sorting collation doesn't exhibit the behavior of the search |
| 1425 |
// collation, which (somewhat questionably) treats w and v as primary-equal. |
| 1426 |
|
| 1427 |
let left = [ |
| 1428 |
"wat", |
| 1429 |
"vat", |
| 1430 |
"aübeck", |
| 1431 |
"Låvi", |
| 1432 |
// ICU4C has a duplicate of the case below. |
| 1433 |
// The duplicate is omitted here. |
| 1434 |
// Instead, the subsequent tests are added for ICU4X. |
| 1435 |
"ä", |
| 1436 |
"a\u{0308}", |
| 1437 |
]; |
| 1438 |
let right = ["vat", "way", "axbeck", "Läwe", "o", "ä"]; |
| 1439 |
let expectations = [ |
| 1440 |
Ordering::Greater, |
| 1441 |
Ordering::Less, |
| 1442 |
Ordering::Greater, |
| 1443 |
Ordering::Less, |
| 1444 |
Ordering::Greater, |
| 1445 |
Ordering::Equal, |
| 1446 |
]; |
| 1447 |
let prefs = locale!("sv").into(); |
| 1448 |
let mut options = CollatorOptions::default(); |
| 1449 |
|
| 1450 |
options.strength = Some(Strength::Tertiary); |
| 1451 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 1452 |
check_expectations(&collator, &left, &right, &expectations); |
| 1453 |
|
| 1454 |
options.strength = Some(Strength::Primary); |
| 1455 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 1456 |
check_expectations(&collator, &left, &right, &expectations); |
| 1457 |
} |
| 1458 |
|
| 1459 |
#[test] |
| 1460 |
fn test_nb_nn_no() { |
| 1461 |
let input = vec!["ü", "y", "å", "ø"]; |
| 1462 |
let expected = &["y", "ü", "ø", "å"]; |
| 1463 |
|
| 1464 |
// Test "no" macro language WITH fallback (should equal expected) |
| 1465 |
let prefs = locale!("no").into(); |
| 1466 |
let collator = Collator::try_new(prefs, Default::default()).unwrap(); |
| 1467 |
let mut strs = input.clone(); |
| 1468 |
strs.sort_by(|a, b| collator.compare(a, b)); |
| 1469 |
assert_eq!(strs, expected); |
| 1470 |
assert_eq!( |
| 1471 |
DataProvider::<CollationTailoringV1>::load( |
| 1472 |
&Baked, |
| 1473 |
DataRequest { |
| 1474 |
id: DataIdentifierCow::from_locale(CollationTailoringV1::make_locale( |
| 1475 |
prefs.locale_preferences |
| 1476 |
)) |
| 1477 |
.as_borrowed(), |
| 1478 |
..Default::default() |
| 1479 |
} |
| 1480 |
) |
| 1481 |
.unwrap() |
| 1482 |
.metadata |
| 1483 |
.locale, |
| 1484 |
None, |
| 1485 |
); |
| 1486 |
|
| 1487 |
// Now "nb" should work |
| 1488 |
let prefs = locale!("nb").into(); |
| 1489 |
let collator = Collator::try_new(prefs, Default::default()).unwrap(); |
| 1490 |
let mut strs = input.clone(); |
| 1491 |
strs.sort_by(|a, b| collator.compare(a, b)); |
| 1492 |
assert_eq!(strs, expected); |
| 1493 |
assert_eq!( |
| 1494 |
DataProvider::<CollationTailoringV1>::load( |
| 1495 |
&Baked, |
| 1496 |
DataRequest { |
| 1497 |
id: DataIdentifierCow::from_locale(CollationTailoringV1::make_locale( |
| 1498 |
prefs.locale_preferences |
| 1499 |
)) |
| 1500 |
.as_borrowed(), |
| 1501 |
..Default::default() |
| 1502 |
} |
| 1503 |
) |
| 1504 |
.unwrap() |
| 1505 |
.metadata |
| 1506 |
.locale, |
| 1507 |
Some(data_locale!("no")) |
| 1508 |
); |
| 1509 |
|
| 1510 |
// And "nn" should work, too |
| 1511 |
let prefs = locale!("nn").into(); |
| 1512 |
let collator = Collator::try_new(prefs, Default::default()).unwrap(); |
| 1513 |
let mut strs = input.clone(); |
| 1514 |
strs.sort_by(|a, b| collator.compare(a, b)); |
| 1515 |
assert_eq!(strs, expected); |
| 1516 |
assert_eq!( |
| 1517 |
DataProvider::<CollationTailoringV1>::load( |
| 1518 |
&Baked, |
| 1519 |
DataRequest { |
| 1520 |
id: DataIdentifierCow::from_locale(CollationTailoringV1::make_locale( |
| 1521 |
prefs.locale_preferences |
| 1522 |
)) |
| 1523 |
.as_borrowed(), |
| 1524 |
..Default::default() |
| 1525 |
} |
| 1526 |
) |
| 1527 |
.unwrap() |
| 1528 |
.metadata |
| 1529 |
.locale, |
| 1530 |
Some(data_locale!("no")) |
| 1531 |
); |
| 1532 |
} |
| 1533 |
|
| 1534 |
#[test] |
| 1535 |
fn test_basics() { |
| 1536 |
// Adapted from `CollationAPITest::TestProperty` in apicoll.cpp in ICU4C |
| 1537 |
let left = ["ab", "ab", "blackbird", "black bird", "Hello", "", "abä"]; |
| 1538 |
let right = ["abc", "AB", "black-bird", "black-bird", "hello", "", "abß"]; |
| 1539 |
let expectations = [ |
| 1540 |
Ordering::Less, |
| 1541 |
Ordering::Less, |
| 1542 |
Ordering::Greater, |
| 1543 |
Ordering::Less, |
| 1544 |
Ordering::Greater, |
| 1545 |
Ordering::Equal, |
| 1546 |
]; |
| 1547 |
|
| 1548 |
let mut options = CollatorOptions::default(); |
| 1549 |
options.strength = Some(Strength::Tertiary); |
| 1550 |
{ |
| 1551 |
let collator = Collator::try_new(Default::default(), options).unwrap(); |
| 1552 |
|
| 1553 |
check_expectations(&collator, &left, &right, &expectations); |
| 1554 |
} |
| 1555 |
} |
| 1556 |
|
| 1557 |
#[test] |
| 1558 |
fn test_numeric_long() { |
| 1559 |
let mut prefs = CollatorPreferences::default(); |
| 1560 |
prefs.numeric_ordering = Some(CollationNumericOrdering::True); |
| 1561 |
|
| 1562 |
let collator = Collator::try_new(prefs, CollatorOptions::default()).unwrap(); |
| 1563 |
let mut left = String::new(); |
| 1564 |
let mut right = String::new(); |
| 1565 |
// We'll make left larger than right numerically. However, first, let's use |
| 1566 |
// a leading zero to make left look less than right non-numerically. |
| 1567 |
left.push('0'); |
| 1568 |
left.push('1'); |
| 1569 |
right.push('1'); |
| 1570 |
// Now, let's make sure we end up with segments longer than |
| 1571 |
// 254 digits |
| 1572 |
for _ in 0..256 { |
| 1573 |
left.push('2'); |
| 1574 |
right.push('2'); |
| 1575 |
} |
| 1576 |
// Now the difference: |
| 1577 |
left.push('4'); |
| 1578 |
right.push('3'); |
| 1579 |
// Again making segments long |
| 1580 |
for _ in 0..256 { |
| 1581 |
left.push('5'); |
| 1582 |
right.push('5'); |
| 1583 |
} |
| 1584 |
// And some trailing zeros |
| 1585 |
for _ in 0..7 { |
| 1586 |
left.push('0'); |
| 1587 |
right.push('0'); |
| 1588 |
} |
| 1589 |
assert_eq!(collator.compare(&left, &right), Ordering::Greater); |
| 1590 |
} |
| 1591 |
|
| 1592 |
#[test] |
| 1593 |
fn test_numeric_after() { |
| 1594 |
let mut prefs = CollatorPreferences::default(); |
| 1595 |
prefs.numeric_ordering = Some(CollationNumericOrdering::True); |
| 1596 |
|
| 1597 |
let collator = Collator::try_new(prefs, CollatorOptions::default()).unwrap(); |
| 1598 |
assert_eq!(collator.compare("0001000b", "1000a"), Ordering::Greater); |
| 1599 |
} |
| 1600 |
|
| 1601 |
#[test] |
| 1602 |
fn test_unpaired_surrogates() { |
| 1603 |
let mut options = CollatorOptions::default(); |
| 1604 |
options.strength = Some(Strength::Quaternary); |
| 1605 |
|
| 1606 |
let collator = Collator::try_new(Default::default(), options).unwrap(); |
| 1607 |
assert_eq!( |
| 1608 |
collator.compare_utf16(&[0xD801u16], &[0xD802u16]), |
| 1609 |
Ordering::Equal |
| 1610 |
); |
| 1611 |
} |
| 1612 |
|
| 1613 |
#[test] |
| 1614 |
fn test_backward_second_level() { |
| 1615 |
let mut options = CollatorOptions::default(); |
| 1616 |
options.strength = Some(Strength::Secondary); |
| 1617 |
|
| 1618 |
{ |
| 1619 |
let collator = Collator::try_new(Default::default(), options).unwrap(); |
| 1620 |
|
| 1621 |
let cases = ["cote", "coté", "côte", "côté"]; |
| 1622 |
let mut case_iter = cases.iter(); |
| 1623 |
while let Some(lower) = case_iter.next() { |
| 1624 |
let tail = case_iter.clone(); |
| 1625 |
for higher in tail { |
| 1626 |
assert_eq!(collator.compare(lower, higher), Ordering::Less); |
| 1627 |
} |
| 1628 |
} |
| 1629 |
} |
| 1630 |
|
| 1631 |
{ |
| 1632 |
let collator = Collator::try_new(locale!("fr-CA").into(), options).unwrap(); |
| 1633 |
|
| 1634 |
{ |
| 1635 |
let cases = ["cote", "côte", "coté", "côté"]; |
| 1636 |
let mut case_iter = cases.iter(); |
| 1637 |
while let Some(lower) = case_iter.next() { |
| 1638 |
let tail = case_iter.clone(); |
| 1639 |
for higher in tail { |
| 1640 |
assert_eq!(collator.compare(lower, higher), Ordering::Less); |
| 1641 |
} |
| 1642 |
} |
| 1643 |
} |
| 1644 |
{ |
| 1645 |
let cases = ["cote\u{FFFE}coté", "côte\u{FFFE}cote"]; |
| 1646 |
let mut case_iter = cases.iter(); |
| 1647 |
while let Some(lower) = case_iter.next() { |
| 1648 |
let tail = case_iter.clone(); |
| 1649 |
for higher in tail { |
| 1650 |
assert_eq!(collator.compare(lower, higher), Ordering::Less); |
| 1651 |
} |
| 1652 |
} |
| 1653 |
} |
| 1654 |
} |
| 1655 |
} |
| 1656 |
|
| 1657 |
#[test] |
| 1658 |
fn test_cantillation() { |
| 1659 |
let prefs = Default::default(); |
| 1660 |
|
| 1661 |
let mut options = CollatorOptions::default(); |
| 1662 |
options.strength = Some(Strength::Quaternary); |
| 1663 |
|
| 1664 |
{ |
| 1665 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 1666 |
assert_eq!( |
| 1667 |
collator.compare( |
| 1668 |
"\u{05D3}\u{05D7}\u{05D9}\u{05AD}", |
| 1669 |
"\u{05D3}\u{05D7}\u{05D9}" |
| 1670 |
), |
| 1671 |
Ordering::Equal |
| 1672 |
); |
| 1673 |
} |
| 1674 |
|
| 1675 |
options.strength = Some(Strength::Identical); |
| 1676 |
|
| 1677 |
{ |
| 1678 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 1679 |
assert_eq!( |
| 1680 |
collator.compare( |
| 1681 |
"\u{05D3}\u{05D7}\u{05D9}\u{05AD}", |
| 1682 |
"\u{05D3}\u{05D7}\u{05D9}" |
| 1683 |
), |
| 1684 |
Ordering::Greater |
| 1685 |
); |
| 1686 |
} |
| 1687 |
} |
| 1688 |
|
| 1689 |
#[test] |
| 1690 |
fn test_cantillation_utf8() { |
| 1691 |
let prefs = Default::default(); |
| 1692 |
|
| 1693 |
let mut options = CollatorOptions::default(); |
| 1694 |
options.strength = Some(Strength::Quaternary); |
| 1695 |
|
| 1696 |
{ |
| 1697 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 1698 |
assert_eq!( |
| 1699 |
collator.compare_utf8( |
| 1700 |
"\u{05D3}\u{05D7}\u{05D9}\u{05AD}".as_bytes(), |
| 1701 |
"\u{05D3}\u{05D7}\u{05D9}".as_bytes() |
| 1702 |
), |
| 1703 |
Ordering::Equal |
| 1704 |
); |
| 1705 |
} |
| 1706 |
|
| 1707 |
options.strength = Some(Strength::Identical); |
| 1708 |
|
| 1709 |
{ |
| 1710 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 1711 |
assert_eq!( |
| 1712 |
collator.compare( |
| 1713 |
"\u{05D3}\u{05D7}\u{05D9}\u{05AD}", |
| 1714 |
"\u{05D3}\u{05D7}\u{05D9}" |
| 1715 |
), |
| 1716 |
Ordering::Greater |
| 1717 |
); |
| 1718 |
} |
| 1719 |
} |
| 1720 |
|
| 1721 |
#[test] |
| 1722 |
fn test_conformance_shifted() { |
| 1723 |
// Adapted from `UCAConformanceTest::TestTableShifted` of ucaconf.cpp in ICU4C. |
| 1724 |
let bugs = []; |
| 1725 |
let dict = include_bytes!("data/CollationTest_CLDR_SHIFTED_SHORT.txt"); |
| 1726 |
|
| 1727 |
let mut options = CollatorOptions::default(); |
| 1728 |
options.strength = Some(Strength::Quaternary); |
| 1729 |
options.alternate_handling = Some(AlternateHandling::Shifted); |
| 1730 |
|
| 1731 |
let collator = Collator::try_new(Default::default(), options).unwrap(); |
| 1732 |
let mut lines = dict.split(|b| b == &b'\n'); |
| 1733 |
let mut prev = loop { |
| 1734 |
if let Some(line) = lines.next() { |
| 1735 |
if line.is_empty() { |
| 1736 |
continue; |
| 1737 |
} |
| 1738 |
if line.starts_with(b"#") { |
| 1739 |
continue; |
| 1740 |
} |
| 1741 |
if let Some(parsed) = parse_hex(line) { |
| 1742 |
break parsed; |
| 1743 |
} |
| 1744 |
} else { |
| 1745 |
panic!("Malformed dictionary"); |
| 1746 |
} |
| 1747 |
}; |
| 1748 |
|
| 1749 |
for line in lines { |
| 1750 |
if line.is_empty() { |
| 1751 |
continue; |
| 1752 |
} |
| 1753 |
if let Some(parsed) = parse_hex(line) { |
| 1754 |
if !bugs.contains(&parsed.as_str()) |
| 1755 |
&& collator.compare(&prev, &parsed) == Ordering::Greater |
| 1756 |
{ |
| 1757 |
assert_eq!(&prev[..], &parsed[..]); |
| 1758 |
} |
| 1759 |
prev = parsed; |
| 1760 |
} |
| 1761 |
} |
| 1762 |
} |
| 1763 |
|
| 1764 |
#[test] |
| 1765 |
fn test_conformance_non_ignorable() { |
| 1766 |
// Adapted from `UCAConformanceTest::TestTableNonIgnorable` of ucaconf.cpp in ICU4C. |
| 1767 |
let bugs = []; |
| 1768 |
let dict = include_bytes!("data/CollationTest_CLDR_NON_IGNORABLE_SHORT.txt"); |
| 1769 |
|
| 1770 |
let mut options = CollatorOptions::default(); |
| 1771 |
options.strength = Some(Strength::Quaternary); |
| 1772 |
options.alternate_handling = Some(AlternateHandling::NonIgnorable); |
| 1773 |
|
| 1774 |
let collator = Collator::try_new(Default::default(), options).unwrap(); |
| 1775 |
let mut lines = dict.split(|b| b == &b'\n'); |
| 1776 |
let mut prev = loop { |
| 1777 |
if let Some(line) = lines.next() { |
| 1778 |
if line.is_empty() { |
| 1779 |
continue; |
| 1780 |
} |
| 1781 |
if line.starts_with(b"#") { |
| 1782 |
continue; |
| 1783 |
} |
| 1784 |
if let Some(parsed) = parse_hex(line) { |
| 1785 |
break parsed; |
| 1786 |
} |
| 1787 |
} else { |
| 1788 |
panic!("Malformed dictionary"); |
| 1789 |
} |
| 1790 |
}; |
| 1791 |
|
| 1792 |
for line in lines { |
| 1793 |
if line.is_empty() { |
| 1794 |
continue; |
| 1795 |
} |
| 1796 |
if let Some(parsed) = parse_hex(line) { |
| 1797 |
if !bugs.contains(&parsed.as_str()) |
| 1798 |
&& collator.compare(&prev, &parsed) == Ordering::Greater |
| 1799 |
{ |
| 1800 |
assert_eq!(&prev[..], &parsed[..]); |
| 1801 |
} |
| 1802 |
prev = parsed; |
| 1803 |
} |
| 1804 |
} |
| 1805 |
} |
| 1806 |
|
| 1807 |
#[test] |
| 1808 |
fn test_case_level() { |
| 1809 |
let mut options = CollatorOptions::default(); |
| 1810 |
options.strength = Some(Strength::Primary); |
| 1811 |
options.case_level = Some(CaseLevel::On); |
| 1812 |
let collator_with_case = Collator::try_new(Default::default(), options).unwrap(); |
| 1813 |
assert_all_comparisons(&collator_with_case, "aA", "Aa", Ordering::Less); |
| 1814 |
} |
| 1815 |
|
| 1816 |
#[test] |
| 1817 |
fn test_default_resolved_options() { |
| 1818 |
let collator = Collator::try_new(Default::default(), Default::default()).unwrap(); |
| 1819 |
let resolved = collator.resolved_options(); |
| 1820 |
assert_eq!(resolved.strength, Strength::Tertiary); |
| 1821 |
assert_eq!(resolved.alternate_handling, AlternateHandling::NonIgnorable); |
| 1822 |
assert_eq!(resolved.case_first, CollationCaseFirst::False); |
| 1823 |
assert_eq!(resolved.max_variable, MaxVariable::Punctuation); |
| 1824 |
assert_eq!(resolved.case_level, CaseLevel::Off); |
| 1825 |
assert_eq!(resolved.numeric, CollationNumericOrdering::False); |
| 1826 |
|
| 1827 |
assert_eq!(collator.compare("𝕒", "A"), Ordering::Less); |
| 1828 |
assert_eq!(collator.compare("coté", "côte"), Ordering::Less); |
| 1829 |
} |
| 1830 |
|
| 1831 |
#[test] |
| 1832 |
fn test_data_resolved_options_th() { |
| 1833 |
let prefs = locale!("th").into(); |
| 1834 |
let collator = Collator::try_new(prefs, Default::default()).unwrap(); |
| 1835 |
let resolved = collator.resolved_options(); |
| 1836 |
assert_eq!(resolved.strength, Strength::Tertiary); |
| 1837 |
assert_eq!(resolved.alternate_handling, AlternateHandling::Shifted); |
| 1838 |
assert_eq!(resolved.case_first, CollationCaseFirst::False); |
| 1839 |
assert_eq!(resolved.max_variable, MaxVariable::Punctuation); |
| 1840 |
assert_eq!(resolved.case_level, CaseLevel::Off); |
| 1841 |
assert_eq!(resolved.numeric, CollationNumericOrdering::False); |
| 1842 |
|
| 1843 |
// There's a separate more comprehensive test for the shifted behavior |
| 1844 |
assert_eq!(collator.compare("𝕒", "A"), Ordering::Less); |
| 1845 |
assert_eq!(collator.compare("coté", "côte"), Ordering::Less); |
| 1846 |
} |
| 1847 |
|
| 1848 |
#[test] |
| 1849 |
fn test_data_resolved_options_da() { |
| 1850 |
let prefs = locale!("da").into(); |
| 1851 |
let collator = Collator::try_new(prefs, Default::default()).unwrap(); |
| 1852 |
let resolved = collator.resolved_options(); |
| 1853 |
assert_eq!(resolved.strength, Strength::Tertiary); |
| 1854 |
assert_eq!(resolved.alternate_handling, AlternateHandling::NonIgnorable); |
| 1855 |
assert_eq!(resolved.case_first, CollationCaseFirst::Upper); |
| 1856 |
assert_eq!(resolved.max_variable, MaxVariable::Punctuation); |
| 1857 |
assert_eq!(resolved.case_level, CaseLevel::Off); |
| 1858 |
assert_eq!(resolved.numeric, CollationNumericOrdering::False); |
| 1859 |
|
| 1860 |
assert_eq!(collator.compare("𝕒", "A"), Ordering::Greater); |
| 1861 |
assert_eq!(collator.compare("coté", "côte"), Ordering::Less); |
| 1862 |
} |
| 1863 |
|
| 1864 |
#[test] |
| 1865 |
fn test_data_resolved_options_fr_ca() { |
| 1866 |
let prefs = locale!("fr-CA").into(); |
| 1867 |
let collator = Collator::try_new(prefs, Default::default()).unwrap(); |
| 1868 |
let resolved = collator.resolved_options(); |
| 1869 |
assert_eq!(resolved.strength, Strength::Tertiary); |
| 1870 |
assert_eq!(resolved.alternate_handling, AlternateHandling::NonIgnorable); |
| 1871 |
assert_eq!(resolved.case_first, CollationCaseFirst::False); |
| 1872 |
assert_eq!(resolved.max_variable, MaxVariable::Punctuation); |
| 1873 |
assert_eq!(resolved.case_level, CaseLevel::Off); |
| 1874 |
assert_eq!(resolved.numeric, CollationNumericOrdering::False); |
| 1875 |
// bacward second level is hidden from the API |
| 1876 |
|
| 1877 |
assert_eq!(collator.compare("𝕒", "A"), Ordering::Less); |
| 1878 |
assert_eq!(collator.compare("coté", "côte"), Ordering::Greater); |
| 1879 |
} |
| 1880 |
|
| 1881 |
#[test] |
| 1882 |
fn test_manual_and_data_resolved_options_fr_ca() { |
| 1883 |
let mut prefs: CollatorPreferences = locale!("fr-CA").into(); |
| 1884 |
prefs.case_first = Some(CollationCaseFirst::Upper); |
| 1885 |
|
| 1886 |
let collator = Collator::try_new(prefs, CollatorOptions::default()).unwrap(); |
| 1887 |
let resolved = collator.resolved_options(); |
| 1888 |
assert_eq!(resolved.strength, Strength::Tertiary); |
| 1889 |
assert_eq!(resolved.alternate_handling, AlternateHandling::NonIgnorable); |
| 1890 |
assert_eq!(resolved.case_first, CollationCaseFirst::Upper); |
| 1891 |
assert_eq!(resolved.max_variable, MaxVariable::Punctuation); |
| 1892 |
assert_eq!(resolved.case_level, CaseLevel::Off); |
| 1893 |
assert_eq!(resolved.numeric, CollationNumericOrdering::False); |
| 1894 |
// backwards second level is hidden from the API |
| 1895 |
|
| 1896 |
assert_eq!(collator.compare("𝕒", "A"), Ordering::Greater); |
| 1897 |
assert_eq!(collator.compare("coté", "côte"), Ordering::Greater); |
| 1898 |
} |
| 1899 |
|
| 1900 |
#[test] |
| 1901 |
fn test_manual_resolved_options_da() { |
| 1902 |
let mut prefs: CollatorPreferences = locale!("da").into(); |
| 1903 |
prefs.case_first = Some(CollationCaseFirst::False); |
| 1904 |
|
| 1905 |
let collator = Collator::try_new(prefs, CollatorOptions::default()).unwrap(); |
| 1906 |
let resolved = collator.resolved_options(); |
| 1907 |
assert_eq!(resolved.strength, Strength::Tertiary); |
| 1908 |
assert_eq!(resolved.alternate_handling, AlternateHandling::NonIgnorable); |
| 1909 |
assert_eq!(resolved.case_first, CollationCaseFirst::False); |
| 1910 |
assert_eq!(resolved.max_variable, MaxVariable::Punctuation); |
| 1911 |
assert_eq!(resolved.case_level, CaseLevel::Off); |
| 1912 |
assert_eq!(resolved.numeric, CollationNumericOrdering::False); |
| 1913 |
|
| 1914 |
assert_eq!(collator.compare("𝕒", "A"), Ordering::Less); |
| 1915 |
assert_eq!(collator.compare("coté", "côte"), Ordering::Less); |
| 1916 |
} |
| 1917 |
|
| 1918 |
#[test] |
| 1919 |
fn test_ecma_sensitivity() { |
| 1920 |
{ |
| 1921 |
// base |
| 1922 |
let mut options = CollatorOptions::default(); |
| 1923 |
options.strength = Some(Strength::Primary); |
| 1924 |
let collator = Collator::try_new(Default::default(), options).unwrap(); |
| 1925 |
assert_eq!(collator.compare("a", "á"), Ordering::Equal); |
| 1926 |
assert_eq!(collator.compare("a", "A"), Ordering::Equal); |
| 1927 |
} |
| 1928 |
{ |
| 1929 |
// accent |
| 1930 |
let mut options = CollatorOptions::default(); |
| 1931 |
options.strength = Some(Strength::Secondary); |
| 1932 |
let collator = Collator::try_new(Default::default(), options).unwrap(); |
| 1933 |
assert_ne!(collator.compare("a", "á"), Ordering::Equal); |
| 1934 |
assert_eq!(collator.compare("a", "A"), Ordering::Equal); |
| 1935 |
} |
| 1936 |
{ |
| 1937 |
// case |
| 1938 |
let mut options = CollatorOptions::default(); |
| 1939 |
options.strength = Some(Strength::Primary); |
| 1940 |
options.case_level = Some(CaseLevel::On); |
| 1941 |
let collator = Collator::try_new(Default::default(), options).unwrap(); |
| 1942 |
assert_eq!(collator.compare("a", "á"), Ordering::Equal); |
| 1943 |
assert_ne!(collator.compare("a", "A"), Ordering::Equal); |
| 1944 |
} |
| 1945 |
{ |
| 1946 |
// variant |
| 1947 |
let mut options = CollatorOptions::default(); |
| 1948 |
options.strength = Some(Strength::Tertiary); |
| 1949 |
let collator = Collator::try_new(Default::default(), options).unwrap(); |
| 1950 |
assert_ne!(collator.compare("a", "á"), Ordering::Equal); |
| 1951 |
assert_ne!(collator.compare("a", "A"), Ordering::Equal); |
| 1952 |
} |
| 1953 |
} |
| 1954 |
|
| 1955 |
#[test] |
| 1956 |
fn test_prefs_overrides() { |
| 1957 |
let locale_prefs = "da-u-kn-kf-upper".parse::<Locale>().unwrap().into(); |
| 1958 |
|
| 1959 |
// preferences from locale are applied |
| 1960 |
{ |
| 1961 |
let prefs = locale_prefs; |
| 1962 |
let collator = Collator::try_new(prefs, CollatorOptions::default()).unwrap(); |
| 1963 |
let resolved = collator.resolved_options(); |
| 1964 |
assert_eq!(resolved.case_first, CollationCaseFirst::Upper); |
| 1965 |
assert_eq!(resolved.numeric, CollationNumericOrdering::True); |
| 1966 |
|
| 1967 |
assert_eq!(collator.compare("𝕒", "A"), Ordering::Greater); |
| 1968 |
assert_eq!(collator.compare("10", "2"), Ordering::Greater); |
| 1969 |
} |
| 1970 |
|
| 1971 |
// preferences from locale can be overridden by explicit preferences |
| 1972 |
{ |
| 1973 |
let mut prefs = locale_prefs; |
| 1974 |
let mut explicit_prefs = CollatorPreferences::default(); |
| 1975 |
explicit_prefs.case_first = Some(CollationCaseFirst::False); |
| 1976 |
explicit_prefs.numeric_ordering = Some(CollationNumericOrdering::False); |
| 1977 |
prefs.extend(explicit_prefs); |
| 1978 |
|
| 1979 |
let collator = Collator::try_new(prefs, CollatorOptions::default()).unwrap(); |
| 1980 |
let resolved = collator.resolved_options(); |
| 1981 |
assert_eq!(resolved.case_first, CollationCaseFirst::False); |
| 1982 |
assert_eq!(resolved.numeric, CollationNumericOrdering::False); |
| 1983 |
|
| 1984 |
assert_eq!(collator.compare("𝕒", "A"), Ordering::Less); |
| 1985 |
assert_eq!(collator.compare("10", "2"), Ordering::Less); |
| 1986 |
} |
| 1987 |
} |
| 1988 |
|
| 1989 |
// This test fails if the root data isn't ICU4X identical prefix-aware. |
| 1990 |
#[test] |
| 1991 |
fn test_identical_prefix_root_builder_0cc2() { |
| 1992 |
let mut options = CollatorOptions::default(); |
| 1993 |
options.strength = Some(Strength::Tertiary); |
| 1994 |
let collator = Collator::try_new(Default::default(), options).unwrap(); |
| 1995 |
assert_eq!( |
| 1996 |
collator.compare_utf16( |
| 1997 |
&[0x0CC8, 0x0CC6, 0x0CC2, 0x0CD6], |
| 1998 |
&[0x0CC8, 0x0CC6, 0x0CC2, 0x0CD5] |
| 1999 |
), |
| 2000 |
Ordering::Less |
| 2001 |
); |
| 2002 |
} |
| 2003 |
|
| 2004 |
// This test fails if the root data isn't ICU4X identical prefix-aware. |
| 2005 |
#[test] |
| 2006 |
fn test_identical_prefix_root_builder_0dcf() { |
| 2007 |
let mut options = CollatorOptions::default(); |
| 2008 |
options.strength = Some(Strength::Tertiary); |
| 2009 |
let collator = Collator::try_new(Default::default(), options).unwrap(); |
| 2010 |
assert_eq!( |
| 2011 |
collator.compare_utf16( |
| 2012 |
&[0x0DC6, 0x0DD9, 0x0DCF, 0xAAF2], |
| 2013 |
&[0x0DC6, 0x0DD9, 0x0DCF, 0x0DCA] |
| 2014 |
), |
| 2015 |
Ordering::Less |
| 2016 |
); |
| 2017 |
} |
| 2018 |
|
| 2019 |
// Not really for testing the outcome but seeing the internals in a debugger. |
| 2020 |
#[test] |
| 2021 |
fn test_middle_contraction_markers() { |
| 2022 |
let mut options = CollatorOptions::default(); |
| 2023 |
options.strength = Some(Strength::Tertiary); |
| 2024 |
let collator = Collator::try_new(Default::default(), options).unwrap(); |
| 2025 |
assert_eq!( |
| 2026 |
collator.compare_utf16(&[0x0CC2, 0xDC00], &[0x0DCF, 0xDC00]), |
| 2027 |
Ordering::Less |
| 2028 |
); |
| 2029 |
} |
| 2030 |
|
| 2031 |
#[test] |
| 2032 |
fn test_fffe_issue_6811() { |
| 2033 |
let mut options = CollatorOptions::default(); |
| 2034 |
options.strength = Some(Strength::Identical); |
| 2035 |
let collator = Collator::try_new(Default::default(), options).unwrap(); |
| 2036 |
// All of these control codes are completely-ignorable, so that |
| 2037 |
// their low code points are compared with the merge separator. |
| 2038 |
// The merge separator must compare less than any other character. |
| 2039 |
assert_eq!( |
| 2040 |
collator.compare_utf16(&[0xFFFE, 0x0001], &[0x0001, 0xFFFE]), |
| 2041 |
Ordering::Less |
| 2042 |
); |
| 2043 |
// The merge separator must even compare less than U+0000. |
| 2044 |
assert_eq!( |
| 2045 |
collator.compare_utf16(&[0xFFFE, 0x0000], &[0x0000, 0xFFFE]), |
| 2046 |
Ordering::Less |
| 2047 |
); |
| 2048 |
} |
| 2049 |
|
| 2050 |
#[test] |
| 2051 |
fn test_burmese() { |
| 2052 |
let mut options = CollatorOptions::default(); |
| 2053 |
options.strength = Some(Strength::Tertiary); |
| 2054 |
let collator = Collator::try_new(locale!("my").into(), options).unwrap(); |
| 2055 |
assert_eq!( |
| 2056 |
collator.compare("", "\u{102d}\u{102f}\u{1037}"), |
| 2057 |
Ordering::Less |
| 2058 |
); |
| 2059 |
} |
| 2060 |
|
| 2061 |
#[cfg(feature = "latin1")] |
| 2062 |
#[test] |
| 2063 |
fn test_latin1_root() { |
| 2064 |
let mut options = CollatorOptions::default(); |
| 2065 |
options.strength = Some(Strength::Primary); |
| 2066 |
let collator = Collator::try_new(Default::default(), options).unwrap(); |
| 2067 |
assert_eq!(collator.compare_latin1(b"ax", b"a\xE4"), Ordering::Greater); |
| 2068 |
} |
| 2069 |
|
| 2070 |
#[cfg(feature = "latin1")] |
| 2071 |
#[test] |
| 2072 |
fn test_latin1_utf16_root() { |
| 2073 |
let mut options = CollatorOptions::default(); |
| 2074 |
options.strength = Some(Strength::Primary); |
| 2075 |
let collator = Collator::try_new(Default::default(), options).unwrap(); |
| 2076 |
assert_eq!( |
| 2077 |
collator.compare_latin1_utf16(b"ax", &[0x0061, 0x00E4]), |
| 2078 |
Ordering::Greater |
| 2079 |
); |
| 2080 |
} |
| 2081 |
|
| 2082 |
#[cfg(feature = "latin1")] |
| 2083 |
#[test] |
| 2084 |
fn test_latin1_fi() { |
| 2085 |
let mut options = CollatorOptions::default(); |
| 2086 |
options.strength = Some(Strength::Primary); |
| 2087 |
let collator = Collator::try_new(locale!("fi").into(), options).unwrap(); |
| 2088 |
assert_eq!(collator.compare_latin1(b"ax", b"a\xE4"), Ordering::Less); |
| 2089 |
} |
| 2090 |
|
| 2091 |
#[cfg(feature = "latin1")] |
| 2092 |
#[test] |
| 2093 |
fn test_latin1_utf16_fi() { |
| 2094 |
let mut options = CollatorOptions::default(); |
| 2095 |
options.strength = Some(Strength::Primary); |
| 2096 |
let collator = Collator::try_new(locale!("fi").into(), options).unwrap(); |
| 2097 |
assert_eq!( |
| 2098 |
collator.compare_latin1_utf16(b"ax", &[0x0061, 0x00E4]), |
| 2099 |
Ordering::Less |
| 2100 |
); |
| 2101 |
} |
| 2102 |
|
| 2103 |
#[test] |
| 2104 |
fn test_numeric_zeros() { |
| 2105 |
let mut prefs = CollatorPreferences::default(); |
| 2106 |
prefs.numeric_ordering = Some(CollationNumericOrdering::True); |
| 2107 |
|
| 2108 |
let collator = Collator::try_new(prefs, CollatorOptions::default()).unwrap(); |
| 2109 |
let mut left = String::new(); |
| 2110 |
left.push('1'); |
| 2111 |
for _ in 0..510 { |
| 2112 |
left.push('0'); |
| 2113 |
} |
| 2114 |
let mut right = String::new(); |
| 2115 |
right.push('2'); |
| 2116 |
for _ in 0..509 { |
| 2117 |
right.push('0'); |
| 2118 |
} |
| 2119 |
// Should be `Greater`, but we do `Less` for now to replicate |
| 2120 |
// ICU4C behavior. Let's treat fixing both as a shared design bug |
| 2121 |
// to figure out. |
| 2122 |
assert_eq!(collator.compare(&left, &right), Ordering::Less); |
| 2123 |
} |
| 2124 |
|
| 2125 |
#[test] |
| 2126 |
fn emoji() { |
| 2127 |
let mut options = CollatorOptions::default(); |
| 2128 |
options.strength = Some(Strength::Secondary); |
| 2129 |
let collator = Collator::try_new(locale!("und-u-co-emoji").into(), options).unwrap(); |
| 2130 |
let grinning_face = "😀"; |
| 2131 |
let grinning_face_with_big_eyes = "😃"; |
| 2132 |
let red_circle = "🔴"; |
| 2133 |
assert_eq!( |
| 2134 |
collator.compare(grinning_face, grinning_face_with_big_eyes), |
| 2135 |
Ordering::Less |
| 2136 |
); |
| 2137 |
assert_eq!(collator.compare(grinning_face, red_circle), Ordering::Less); |
| 2138 |
} |
| 2139 |
|
| 2140 |
// TODO: Test languages that map to the root. |
| 2141 |
// The languages that map to root without script reordering are: |
| 2142 |
// ca (at least for now) |
| 2143 |
// de |
| 2144 |
// en |
| 2145 |
// fr |
| 2146 |
// ga |
| 2147 |
// id |
| 2148 |
// it |
| 2149 |
// lb |
| 2150 |
// ms |
| 2151 |
// nl |
| 2152 |
// pt |
| 2153 |
// sw |
| 2154 |
// xh |
| 2155 |
// zu |
| 2156 |
// |
| 2157 |
// A bit unclear: ff in Latin script? |
| 2158 |
// |
| 2159 |
// These are root plus script reordering: |
| 2160 |
// am |
| 2161 |
// be |
| 2162 |
// bg |
| 2163 |
// chr |
| 2164 |
// el |
| 2165 |
// ka |
| 2166 |
// lo |
| 2167 |
// mn |
| 2168 |
// ne |
| 2169 |
// ru |
| 2170 |
|
| 2171 |
// TODO: Test imports. These aren't aliases but should get deduplicated |
| 2172 |
// in the provider: |
| 2173 |
// bo: dz-u-co-standard (draft: unconfirmed) |
| 2174 |
// bs: hr |
| 2175 |
// bs-Cyrl: sr |
| 2176 |
// fa-AF: ps |
| 2177 |
// sr-Latn: hr |
| 2178 |
|
| 2179 |
// TODO: Test that nn and nb are aliases for no |
| 2180 |
|
| 2181 |
// TODO: Consider testing ff-Adlm for supplementary-plane tailoring, including contractions |
| 2182 |
|
| 2183 |
#[test] |
| 2184 |
// Regression test for a bug where setting `AlternateHandling::Shifted` and |
| 2185 |
// `MaxVariable::Currency` caused a panic during collation. |
| 2186 |
// The bug was due to an off-by-one error when validating special primaries, |
| 2187 |
// which truncated the `last_primaries` vector and removed the `Currency` element. |
| 2188 |
// This test verifies that the collator can be successfully constructed with these |
| 2189 |
// options and that it correctly compares an empty string and a space as equal |
| 2190 |
// (since space is shifted and ignored at the default Tertiary strength). |
| 2191 |
fn test_shifted_max_variable_currency() { |
| 2192 |
let prefs = CollatorPreferences::default(); |
| 2193 |
let mut options = CollatorOptions::default(); |
| 2194 |
options.alternate_handling = Some(AlternateHandling::Shifted); |
| 2195 |
options.max_variable = Some(MaxVariable::Currency); |
| 2196 |
let collator = Collator::try_new(prefs, options).unwrap(); |
| 2197 |
assert_eq!(collator.compare("", " "), Ordering::Equal); |
| 2198 |
} |