Debian Codegraph @

third_party/rust/icu_normalizer/tests/tests.rs

raw
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 icu_normalizer::properties::CanonicalCombiningClassMap;
6 use icu_normalizer::properties::CanonicalComposition;
7 use icu_normalizer::properties::CanonicalDecomposition;
8 use icu_normalizer::properties::Decomposed;
9 use icu_normalizer::uts46::Uts46Mapper;
10 use icu_normalizer::ComposingNormalizer;
11 use icu_normalizer::DecomposingNormalizer;
12
13 #[test]
14 fn test_nfd_basic() {
15 let normalizer: DecomposingNormalizer = DecomposingNormalizer::new_nfd();
16 assert_eq!(normalizer.normalize("ä"), "a\u{0308}");
17 assert_eq!(normalizer.normalize("Ä"), "A\u{0308}");
18 assert_eq!(normalizer.normalize("ệ"), "e\u{0323}\u{0302}");
19 assert_eq!(normalizer.normalize("Ệ"), "E\u{0323}\u{0302}");
20 assert_eq!(normalizer.normalize("𝅗𝅥"), "𝅗\u{1D165}");
21 assert_eq!(normalizer.normalize("\u{2126}"), "Ω"); // ohm sign
22 assert_eq!(normalizer.normalize("ベ"), "ベ"); // half-width unchanged
23 assert_eq!(normalizer.normalize("ペ"), "ペ"); // half-width unchanged
24 assert_eq!(normalizer.normalize("fi"), "fi"); // ligature unchanged
25 assert_eq!(normalizer.normalize("\u{FDFA}"), "\u{FDFA}"); // ligature unchanged
26 assert_eq!(normalizer.normalize("㈎"), "㈎"); // parenthetical unchanged
27 assert_eq!(normalizer.normalize("\u{0345}"), "\u{0345}"); // Iota subscript
28 }
29
30 #[test]
31 fn test_nfkd_basic() {
32 let normalizer: DecomposingNormalizer = DecomposingNormalizer::new_nfkd();
33 assert_eq!(normalizer.normalize("ä"), "a\u{0308}");
34 assert_eq!(normalizer.normalize("Ä"), "A\u{0308}");
35 assert_eq!(normalizer.normalize("ệ"), "e\u{0323}\u{0302}");
36 assert_eq!(normalizer.normalize("Ệ"), "E\u{0323}\u{0302}");
37 assert_eq!(normalizer.normalize("𝅗𝅥"), "𝅗\u{1D165}");
38 assert_eq!(normalizer.normalize("\u{2126}"), "Ω"); // ohm sign
39 assert_eq!(normalizer.normalize("ベ"), "ヘ\u{3099}"); // half-width to full-width
40 assert_eq!(normalizer.normalize("ペ"), "ヘ\u{309A}"); // half-width to full-width
41 assert_eq!(normalizer.normalize("fi"), "fi"); // ligature expanded
42 assert_eq!(normalizer.normalize("\u{FDFA}"), "\u{635}\u{644}\u{649} \u{627}\u{644}\u{644}\u{647} \u{639}\u{644}\u{64A}\u{647} \u{648}\u{633}\u{644}\u{645}");
43 // ligature expanded
44 assert_eq!(normalizer.normalize("㈎"), "(\u{1100}\u{1161})"); // parenthetical expanded
45 assert_eq!(normalizer.normalize("\u{0345}"), "\u{0345}"); // Iota subscript
46 }
47
48 #[test]
49 fn test_nfc_basic() {
50 let normalizer: ComposingNormalizer = ComposingNormalizer::new_nfc();
51 assert_eq!(normalizer.normalize("a\u{0308}"), "ä");
52 assert_eq!(normalizer.normalize("A\u{0308}"), "Ä");
53 assert_eq!(normalizer.normalize("e\u{0323}\u{0302}"), "ệ");
54 assert_eq!(normalizer.normalize("E\u{0323}\u{0302}"), "Ệ");
55 assert_eq!(normalizer.normalize("𝅗𝅥"), "𝅗\u{1D165}"); // Composition exclusion
56
57 assert_eq!(normalizer.normalize("\u{2126}"), "Ω"); // ohm sign
58 assert_eq!(normalizer.normalize("ベ"), "ベ"); // half-width unchanged
59 assert_eq!(normalizer.normalize("ペ"), "ペ"); // half-width unchanged
60 assert_eq!(normalizer.normalize("fi"), "fi"); // ligature unchanged
61 assert_eq!(normalizer.normalize("\u{FDFA}"), "\u{FDFA}"); // ligature unchanged
62 assert_eq!(normalizer.normalize("㈎"), "㈎"); // parenthetical unchanged
63 assert_eq!(normalizer.normalize("\u{0345}"), "\u{0345}"); // Iota subscript
64 }
65
66 #[test]
67 fn test_nfkc_basic() {
68 let normalizer: ComposingNormalizer = ComposingNormalizer::new_nfkc();
69 assert_eq!(normalizer.normalize("a\u{0308}"), "ä");
70 assert_eq!(normalizer.normalize("A\u{0308}"), "Ä");
71 assert_eq!(normalizer.normalize("e\u{0323}\u{0302}"), "ệ");
72 assert_eq!(normalizer.normalize("E\u{0323}\u{0302}"), "Ệ");
73 assert_eq!(normalizer.normalize("𝅗𝅥"), "𝅗\u{1D165}"); // Composition exclusion
74
75 assert_eq!(normalizer.normalize("\u{2126}"), "Ω"); // ohm sign
76 assert_eq!(normalizer.normalize("ベ"), "ベ"); // half-width to full-width, the compose
77 assert_eq!(normalizer.normalize("ペ"), "ペ"); // half-width to full-width, the compose
78 assert_eq!(normalizer.normalize("fi"), "fi"); // ligature expanded
79 assert_eq!(normalizer.normalize("\u{FDFA}"), "\u{0635}\u{0644}\u{0649} \u{0627}\u{0644}\u{0644}\u{0647} \u{0639}\u{0644}\u{064A}\u{0647} \u{0648}\u{0633}\u{0644}\u{0645}");
80 // ligature expanded
81 assert_eq!(normalizer.normalize("㈎"), "(가)"); // parenthetical expanded and partially recomposed
82 assert_eq!(normalizer.normalize("\u{0345}"), "\u{0345}"); // Iota subscript
83 }
84
85 #[test]
86 fn test_uts46_map_normalize() {
87 let mapper: Uts46Mapper = Uts46Mapper::new();
88 assert_eq!(
89 mapper
90 .map_normalize("a\u{0308}".chars())
91 .collect::<String>(),
92 "ä"
93 );
94 assert_eq!(
95 mapper
96 .map_normalize("A\u{0308}".chars())
97 .collect::<String>(),
98 "ä"
99 );
100 assert_eq!(
101 mapper
102 .map_normalize("e\u{0323}\u{0302}".chars())
103 .collect::<String>(),
104 "ệ"
105 );
106 assert_eq!(
107 mapper
108 .map_normalize("E\u{0323}\u{0302}".chars())
109 .collect::<String>(),
110 "ệ"
111 );
112 assert_eq!(
113 mapper.map_normalize("𝅗𝅥".chars()).collect::<String>(),
114 "𝅗\u{1D165}"
115 ); // Composition exclusion
116
117 assert_eq!(
118 mapper.map_normalize("\u{2126}".chars()).collect::<String>(),
119 "ω"
120 ); // ohm sign
121 assert_eq!(mapper.map_normalize("ベ".chars()).collect::<String>(), "ベ"); // half-width to full-width, the compose
122 assert_eq!(mapper.map_normalize("ペ".chars()).collect::<String>(), "ペ"); // half-width to full-width, the compose
123 assert_eq!(mapper.map_normalize("fi".chars()).collect::<String>(), "fi"); // ligature expanded
124 assert_eq!(mapper.map_normalize("\u{FDFA}".chars()).collect::<String>(), "\u{0635}\u{0644}\u{0649} \u{0627}\u{0644}\u{0644}\u{0647} \u{0639}\u{0644}\u{064A}\u{0647} \u{0648}\u{0633}\u{0644}\u{0645}");
125 // ligature expanded
126 assert_eq!(
127 mapper.map_normalize("㈎".chars()).collect::<String>(),
128 "(가)"
129 ); // parenthetical expanded and partially recomposed
130
131 // Deviations (UTS 46, 6 Mapping Table Derivation, Step 4)
132 assert_eq!(
133 mapper.map_normalize("\u{200C}".chars()).collect::<String>(),
134 "\u{200C}"
135 );
136 assert_eq!(
137 mapper.map_normalize("\u{200D}".chars()).collect::<String>(),
138 "\u{200D}"
139 );
140 assert_eq!(mapper.map_normalize("ß".chars()).collect::<String>(), "ß");
141 assert_eq!(mapper.map_normalize("ς".chars()).collect::<String>(), "ς");
142
143 // Iota subscript
144 assert_eq!(
145 mapper.map_normalize("\u{0345}".chars()).collect::<String>(),
146 "ι"
147 );
148
149 // Disallowed
150 assert_eq!(
151 mapper.map_normalize("\u{061C}".chars()).collect::<String>(),
152 "\u{FFFD}"
153 );
154
155 // Ignored
156 assert_eq!(
157 mapper
158 .map_normalize("a\u{180B}b".chars())
159 .collect::<String>(),
160 "ab"
161 );
162 }
163
164 #[test]
165 fn test_uts46_normalize_validate() {
166 let mapper: Uts46Mapper = Uts46Mapper::new();
167 assert_eq!(
168 mapper
169 .normalize_validate("a\u{0308}".chars())
170 .collect::<String>(),
171 "ä"
172 );
173 assert_eq!(
174 mapper
175 .normalize_validate("A\u{0308}".chars())
176 .collect::<String>(),
177 "ä"
178 );
179 assert_eq!(
180 mapper
181 .normalize_validate("e\u{0323}\u{0302}".chars())
182 .collect::<String>(),
183 "ệ"
184 );
185 assert_eq!(
186 mapper
187 .normalize_validate("E\u{0323}\u{0302}".chars())
188 .collect::<String>(),
189 "ệ"
190 );
191 assert_eq!(
192 mapper.normalize_validate("𝅗𝅥".chars()).collect::<String>(),
193 "𝅗\u{1D165}"
194 ); // Composition exclusion
195
196 assert_eq!(
197 mapper
198 .normalize_validate("\u{2126}".chars())
199 .collect::<String>(),
200 "ω"
201 ); // ohm sign
202 assert_eq!(
203 mapper.normalize_validate("ベ".chars()).collect::<String>(),
204 "ベ"
205 ); // half-width to full-width, the compose
206 assert_eq!(
207 mapper.normalize_validate("ペ".chars()).collect::<String>(),
208 "ペ"
209 ); // half-width to full-width, the compose
210 assert_eq!(
211 mapper.normalize_validate("fi".chars()).collect::<String>(),
212 "fi"
213 ); // ligature expanded
214 assert_eq!(mapper.normalize_validate("\u{FDFA}".chars()).collect::<String>(), "\u{0635}\u{0644}\u{0649} \u{0627}\u{0644}\u{0644}\u{0647} \u{0639}\u{0644}\u{064A}\u{0647} \u{0648}\u{0633}\u{0644}\u{0645}");
215 // ligature expanded
216 assert_eq!(
217 mapper.normalize_validate("㈎".chars()).collect::<String>(),
218 "(가)"
219 ); // parenthetical expanded and partially recomposed
220
221 // Deviations (UTS 46, 6 Mapping Table Derivation, Step 4)
222 assert_eq!(
223 mapper
224 .normalize_validate("\u{200C}".chars())
225 .collect::<String>(),
226 "\u{200C}"
227 );
228 assert_eq!(
229 mapper
230 .normalize_validate("\u{200D}".chars())
231 .collect::<String>(),
232 "\u{200D}"
233 );
234 assert_eq!(
235 mapper.normalize_validate("ß".chars()).collect::<String>(),
236 "ß"
237 );
238 assert_eq!(
239 mapper.normalize_validate("ς".chars()).collect::<String>(),
240 "ς"
241 );
242
243 // Iota subscript
244 assert_eq!(
245 mapper
246 .normalize_validate("\u{0345}".chars())
247 .collect::<String>(),
248 "ι"
249 );
250
251 // Disallowed
252 assert_eq!(
253 mapper
254 .normalize_validate("\u{061C}".chars())
255 .collect::<String>(),
256 "\u{FFFD}"
257 );
258
259 // Ignored
260 assert_eq!(
261 mapper
262 .normalize_validate("a\u{180B}b".chars())
263 .collect::<String>(),
264 "a\u{FFFD}b"
265 );
266 }
267
268 type StackString = arraystring::ArrayString<arraystring::typenum::U48>;
269
270 #[test]
271 fn test_nfd_str_to() {
272 let normalizer: DecomposingNormalizer = DecomposingNormalizer::new_nfd();
273
274 let mut buf = StackString::new();
275 assert!(normalizer.normalize_to("ä", &mut buf).is_ok());
276 assert_eq!(&buf, "a\u{0308}");
277
278 buf.clear();
279 assert!(normalizer.normalize_to("ệ", &mut buf).is_ok());
280 assert_eq!(&buf, "e\u{0323}\u{0302}");
281 }
282
283 #[test]
284 fn test_nfd_utf8_to() {
285 let normalizer: DecomposingNormalizer = DecomposingNormalizer::new_nfd();
286
287 let mut buf = StackString::new();
288 assert!(normalizer
289 .normalize_utf8_to("ä".as_bytes(), &mut buf)
290 .is_ok());
291 assert_eq!(&buf, "a\u{0308}");
292
293 buf.clear();
294 assert!(normalizer
295 .normalize_utf8_to("ệ".as_bytes(), &mut buf)
296 .is_ok());
297 assert_eq!(&buf, "e\u{0323}\u{0302}");
298 }
299
300 type StackVec = arrayvec::ArrayVec<u16, 32>;
301
302 #[test]
303 fn test_nfd_utf16_to() {
304 let normalizer: DecomposingNormalizer = DecomposingNormalizer::new_nfd();
305
306 let mut buf = StackVec::new();
307 assert!(normalizer
308 .normalize_utf16_to([0x00E4u16].as_slice(), &mut buf)
309 .is_ok());
310 assert_eq!(&buf, [0x0061u16, 0x0308u16].as_slice());
311
312 buf.clear();
313 assert!(normalizer
314 .normalize_utf16_to([0x1EC7u16].as_slice(), &mut buf)
315 .is_ok());
316 assert_eq!(&buf, [0x0065u16, 0x0323u16, 0x0302u16].as_slice());
317 }
318
319 #[test]
320 fn test_nfc_str_to() {
321 let normalizer: ComposingNormalizer = ComposingNormalizer::new_nfc();
322
323 let mut buf = StackString::new();
324 assert!(normalizer.normalize_to("a\u{0308}", &mut buf).is_ok());
325 assert_eq!(&buf, "ä");
326
327 buf.clear();
328 assert!(normalizer
329 .normalize_to("e\u{0323}\u{0302}", &mut buf)
330 .is_ok());
331 assert_eq!(&buf, "ệ");
332 }
333
334 #[test]
335 fn test_nfc_utf8_to() {
336 let normalizer: ComposingNormalizer = ComposingNormalizer::new_nfc();
337
338 let mut buf = StackString::new();
339 assert!(normalizer
340 .normalize_utf8_to("a\u{0308}".as_bytes(), &mut buf)
341 .is_ok());
342 assert_eq!(&buf, "ä");
343
344 buf.clear();
345 assert!(normalizer
346 .normalize_utf8_to("e\u{0323}\u{0302}".as_bytes(), &mut buf)
347 .is_ok());
348 assert_eq!(&buf, "ệ");
349 }
350
351 #[test]
352 fn test_nfc_utf16_to() {
353 let normalizer: ComposingNormalizer = ComposingNormalizer::new_nfc();
354
355 let mut buf = StackVec::new();
356 assert!(normalizer
357 .normalize_utf16_to([0x0061u16, 0x0308u16].as_slice(), &mut buf)
358 .is_ok());
359 assert_eq!(&buf, [0x00E4u16].as_slice());
360
361 buf.clear();
362 assert!(normalizer
363 .normalize_utf16_to([0x0065u16, 0x0323u16, 0x0302u16].as_slice(), &mut buf)
364 .is_ok());
365 assert_eq!(&buf, [0x1EC7u16].as_slice());
366 }
367
368 #[test]
369 fn test_nfc_utf8_to_errors() {
370 let normalizer: ComposingNormalizer = ComposingNormalizer::new_nfc();
371
372 let mut buf = StackString::new();
373 assert!(normalizer
374 .normalize_utf8_to(b"\xFFa\xCC\x88\xFF", &mut buf)
375 .is_ok());
376 assert_eq!(&buf, "\u{FFFD}ä\u{FFFD}");
377
378 buf.clear();
379 assert!(normalizer
380 .normalize_utf8_to(b"\x80e\xCC\xA3\xCC\x82\x80", &mut buf)
381 .is_ok());
382 assert_eq!(&buf, "\u{FFFD}ệ\u{FFFD}");
383
384 buf.clear();
385 assert!(normalizer
386 .normalize_utf8_to(b"aaa\xFFaaa\xFFaaa", &mut buf)
387 .is_ok());
388 assert_eq!(&buf, "aaa\u{FFFD}aaa\u{FFFD}aaa");
389
390 buf.clear();
391 assert!(normalizer
392 .normalize_utf8_to(b"aaa\xE2\x98aaa\xE2\x98aaa", &mut buf)
393 .is_ok());
394 assert_eq!(&buf, "aaa\u{FFFD}aaa\u{FFFD}aaa");
395 }
396
397 #[test]
398 fn test_nfd_utf8_to_errors() {
399 let normalizer: DecomposingNormalizer = DecomposingNormalizer::new_nfd();
400
401 let mut buf = StackString::new();
402 assert!(normalizer
403 .normalize_utf8_to(b"\xFF\xC3\xA4\xFF", &mut buf)
404 .is_ok());
405 assert_eq!(&buf, "\u{FFFD}a\u{0308}\u{FFFD}");
406
407 buf.clear();
408 assert!(normalizer
409 .normalize_utf8_to(b"\x80\xE1\xBB\x87\x80", &mut buf)
410 .is_ok());
411 assert_eq!(&buf, "\u{FFFD}e\u{0323}\u{0302}\u{FFFD}");
412
413 buf.clear();
414 assert!(normalizer
415 .normalize_utf8_to(b"aaa\xFFaaa\xFFaaa", &mut buf)
416 .is_ok());
417 assert_eq!(&buf, "aaa\u{FFFD}aaa\u{FFFD}aaa");
418
419 buf.clear();
420 assert!(normalizer
421 .normalize_utf8_to(b"aaa\xE2\x98aaa\xE2\x98aaa", &mut buf)
422 .is_ok());
423 assert_eq!(&buf, "aaa\u{FFFD}aaa\u{FFFD}aaa");
424 }
425
426 #[test]
427 fn test_nfc_utf16_to_errors() {
428 let normalizer: ComposingNormalizer = ComposingNormalizer::new_nfc();
429
430 let mut buf = StackVec::new();
431 assert!(normalizer
432 .normalize_utf16_to([0xD800u16, 0x0061u16, 0x0308u16].as_slice(), &mut buf)
433 .is_ok());
434 assert_eq!(&buf, [0xFFFDu16, 0x00E4u16].as_slice());
435
436 buf.clear();
437 assert!(normalizer
438 .normalize_utf16_to([0xDC00u16, 0x0061u16, 0x0308u16].as_slice(), &mut buf)
439 .is_ok());
440 assert_eq!(&buf, [0xFFFDu16, 0x00E4u16].as_slice());
441
442 buf.clear();
443 assert!(normalizer
444 .normalize_utf16_to(
445 [0x0061u16, 0xD800u16, 0x0061u16, 0x0308u16].as_slice(),
446 &mut buf
447 )
448 .is_ok());
449 assert_eq!(&buf, [0x0061u16, 0xFFFDu16, 0x00E4u16].as_slice());
450
451 buf.clear();
452 assert!(normalizer
453 .normalize_utf16_to(
454 [0x0061u16, 0xDC00u16, 0x0061u16, 0x0308u16].as_slice(),
455 &mut buf
456 )
457 .is_ok());
458 assert_eq!(&buf, [0x0061u16, 0xFFFDu16, 0x00E4u16].as_slice());
459
460 buf.clear();
461 assert!(normalizer
462 .normalize_utf16_to(
463 [0x0061u16, 0xD800u16, 0x0061u16, 0x0308u16, 0xD800u16].as_slice(),
464 &mut buf
465 )
466 .is_ok());
467 assert_eq!(
468 &buf,
469 [0x0061u16, 0xFFFDu16, 0x00E4u16, 0xFFFDu16].as_slice()
470 );
471
472 buf.clear();
473 assert!(normalizer
474 .normalize_utf16_to(
475 [0x0061u16, 0xDC00u16, 0x0061u16, 0x0308u16, 0xDC00u16].as_slice(),
476 &mut buf
477 )
478 .is_ok());
479 assert_eq!(
480 &buf,
481 [0x0061u16, 0xFFFDu16, 0x00E4u16, 0xFFFDu16].as_slice()
482 );
483
484 buf.clear();
485 assert!(normalizer
486 .normalize_utf16_to(
487 [0x0061u16, 0xD800u16, 0x0061u16, 0x0061u16, 0xD800u16].as_slice(),
488 &mut buf
489 )
490 .is_ok());
491 assert_eq!(
492 &buf,
493 [0x0061u16, 0xFFFDu16, 0x0061u16, 0x0061u16, 0xFFFDu16].as_slice()
494 );
495
496 buf.clear();
497 assert!(normalizer
498 .normalize_utf16_to(
499 [0x0061u16, 0xDC00u16, 0x0061u16, 0x0061u16, 0xDC00u16].as_slice(),
500 &mut buf
501 )
502 .is_ok());
503 assert_eq!(
504 &buf,
505 [0x0061u16, 0xFFFDu16, 0x0061u16, 0x0061u16, 0xFFFDu16].as_slice()
506 );
507
508 buf.clear();
509 assert!(normalizer
510 .normalize_utf16_to(
511 [0x0061u16, 0xD800u16, 0x0308u16, 0xD800u16].as_slice(),
512 &mut buf
513 )
514 .is_ok());
515 assert_eq!(
516 &buf,
517 [0x0061u16, 0xFFFDu16, 0x0308u16, 0xFFFDu16].as_slice()
518 );
519
520 buf.clear();
521 assert!(normalizer
522 .normalize_utf16_to(
523 [0x0061u16, 0xDC00u16, 0x0308u16, 0xDC00u16].as_slice(),
524 &mut buf
525 )
526 .is_ok());
527 assert_eq!(
528 &buf,
529 [0x0061u16, 0xFFFDu16, 0x0308u16, 0xFFFDu16].as_slice()
530 );
531 }
532
533 #[test]
534 fn test_nfd_utf16_to_errors() {
535 let normalizer: DecomposingNormalizer = DecomposingNormalizer::new_nfd();
536
537 let mut buf = StackVec::new();
538 assert!(normalizer
539 .normalize_utf16_to([0xD800u16, 0x00E4u16].as_slice(), &mut buf)
540 .is_ok());
541 assert_eq!(&buf, [0xFFFDu16, 0x0061u16, 0x0308u16].as_slice());
542
543 buf.clear();
544 assert!(normalizer
545 .normalize_utf16_to([0xDC00u16, 0x00E4u16].as_slice(), &mut buf)
546 .is_ok());
547 assert_eq!(&buf, [0xFFFDu16, 0x0061u16, 0x0308u16].as_slice());
548
549 buf.clear();
550 assert!(normalizer
551 .normalize_utf16_to([0x0061u16, 0xD800u16, 0x00E4u16].as_slice(), &mut buf)
552 .is_ok());
553 assert_eq!(
554 &buf,
555 [0x0061u16, 0xFFFDu16, 0x0061u16, 0x0308u16].as_slice()
556 );
557
558 buf.clear();
559 assert!(normalizer
560 .normalize_utf16_to([0x0061u16, 0xDC00u16, 0x00E4u16].as_slice(), &mut buf)
561 .is_ok());
562 assert_eq!(
563 &buf,
564 [0x0061u16, 0xFFFDu16, 0x0061u16, 0x0308u16].as_slice()
565 );
566
567 buf.clear();
568 assert!(normalizer
569 .normalize_utf16_to(
570 [0x0061u16, 0xD800u16, 0x00E4u16, 0xD800u16].as_slice(),
571 &mut buf
572 )
573 .is_ok());
574 assert_eq!(
575 &buf,
576 [0x0061u16, 0xFFFDu16, 0x0061u16, 0x0308u16, 0xFFFDu16].as_slice()
577 );
578
579 buf.clear();
580 assert!(normalizer
581 .normalize_utf16_to(
582 [0x0061u16, 0xDC00u16, 0x00E4u16, 0xDC00u16].as_slice(),
583 &mut buf
584 )
585 .is_ok());
586 assert_eq!(
587 &buf,
588 [0x0061u16, 0xFFFDu16, 0x0061u16, 0x0308u16, 0xFFFDu16].as_slice()
589 );
590
591 buf.clear();
592 assert!(normalizer
593 .normalize_utf16_to(
594 [0x0061u16, 0xD800u16, 0x0061u16, 0x0061u16, 0xD800u16].as_slice(),
595 &mut buf
596 )
597 .is_ok());
598 assert_eq!(
599 &buf,
600 [0x0061u16, 0xFFFDu16, 0x0061u16, 0x0061u16, 0xFFFDu16].as_slice()
601 );
602
603 buf.clear();
604 assert!(normalizer
605 .normalize_utf16_to(
606 [0x0061u16, 0xDC00u16, 0x0061u16, 0x0061u16, 0xDC00u16].as_slice(),
607 &mut buf
608 )
609 .is_ok());
610 assert_eq!(
611 &buf,
612 [0x0061u16, 0xFFFDu16, 0x0061u16, 0x0061u16, 0xFFFDu16].as_slice()
613 );
614
615 buf.clear();
616 assert!(normalizer
617 .normalize_utf16_to(
618 [0x0061u16, 0xD800u16, 0x0308u16, 0xD800u16].as_slice(),
619 &mut buf
620 )
621 .is_ok());
622 assert_eq!(
623 &buf,
624 [0x0061u16, 0xFFFDu16, 0x0308u16, 0xFFFDu16].as_slice()
625 );
626
627 buf.clear();
628 assert!(normalizer
629 .normalize_utf16_to(
630 [0x0061u16, 0xDC00u16, 0x0308u16, 0xDC00u16].as_slice(),
631 &mut buf
632 )
633 .is_ok());
634 assert_eq!(
635 &buf,
636 [0x0061u16, 0xFFFDu16, 0x0308u16, 0xFFFDu16].as_slice()
637 );
638 }
639
640 use atoi::FromRadix16;
641
642 /// Parse five semicolon-terminated strings consisting of space-separated hexadecimal scalar values
643 fn parse_hex(mut hexes: &[u8]) -> [StackString; 5] {
644 let mut strings = [
645 StackString::new(),
646 StackString::new(),
647 StackString::new(),
648 StackString::new(),
649 StackString::new(),
650 ];
651 let mut current = 0;
652 loop {
653 let (scalar, mut offset) = u32::from_radix_16(hexes);
654 let c = core::char::from_u32(scalar).unwrap();
655 strings[current].try_push(c).unwrap();
656 match hexes[offset] {
657 b';' => {
658 current += 1;
659 if current == strings.len() {
660 return strings;
661 }
662 offset += 1;
663 }
664 b' ' => {
665 offset += 1;
666 }
667 _ => {
668 panic!("Bad format: Garbage");
669 }
670 }
671 hexes = &hexes[offset..];
672 }
673 }
674
675 #[test]
676 fn test_conformance() {
677 let nfd: DecomposingNormalizer = DecomposingNormalizer::new_nfd();
678 let nfkd: DecomposingNormalizer = DecomposingNormalizer::new_nfkd();
679 let nfc: ComposingNormalizer = ComposingNormalizer::new_nfc();
680 let nfkc: ComposingNormalizer = ComposingNormalizer::new_nfkc();
681
682 let mut prev = 0u32;
683 let mut part = 0u8;
684 let data = include_bytes!("data/NormalizationTest.txt");
685 let lines = data.split(|b| b == &b'\n');
686 for line in lines {
687 if line.is_empty() {
688 continue;
689 }
690 if line.starts_with(b"#") {
691 continue;
692 }
693 if line.starts_with(&b"@Part"[..]) {
694 part = line[5] - b'0';
695 if part == 2 {
696 for u in prev + 1..=0x10FFFF {
697 if let Some(c) = char::from_u32(u) {
698 assert!(nfd
699 .normalize_iter(core::iter::once(c))
700 .eq(core::iter::once(c)));
701 assert!(nfkd
702 .normalize_iter(core::iter::once(c))
703 .eq(core::iter::once(c)));
704 assert!(nfc
705 .normalize_iter(core::iter::once(c))
706 .eq(core::iter::once(c)));
707 assert!(nfkc
708 .normalize_iter(core::iter::once(c))
709 .eq(core::iter::once(c)));
710 }
711 }
712 }
713 continue;
714 }
715 let strings = parse_hex(line);
716 // 0: source
717 // 1: NFC
718 // 2: NFD
719 // 3: NFKC
720 // 4: NFKD
721 if part == 1 {
722 let mut iter = strings[0].chars();
723 let current = iter.next().unwrap();
724 assert_eq!(iter.next(), None);
725 let current_u = u32::from(current);
726 for u in prev + 1..current_u {
727 if let Some(c) = char::from_u32(u) {
728 assert!(nfd
729 .normalize_iter(core::iter::once(c))
730 .eq(core::iter::once(c)));
731 assert!(nfkd
732 .normalize_iter(core::iter::once(c))
733 .eq(core::iter::once(c)));
734 assert!(nfc
735 .normalize_iter(core::iter::once(c))
736 .eq(core::iter::once(c)));
737 assert!(nfkc
738 .normalize_iter(core::iter::once(c))
739 .eq(core::iter::once(c)));
740 }
741 }
742 prev = current_u;
743 }
744 // NFC
745 assert!(nfc
746 .normalize_iter(strings[0].chars())
747 .eq(strings[1].chars()));
748 assert!(nfc
749 .normalize_iter(strings[1].chars())
750 .eq(strings[1].chars()));
751 assert!(nfc
752 .normalize_iter(strings[2].chars())
753 .eq(strings[1].chars()));
754
755 assert!(nfc
756 .normalize_iter(strings[3].chars())
757 .eq(strings[3].chars()));
758 assert!(nfc
759 .normalize_iter(strings[4].chars())
760 .eq(strings[3].chars()));
761
762 // NFD
763 assert!(nfd
764 .normalize_iter(strings[0].chars())
765 .eq(strings[2].chars()));
766 assert!(nfd
767 .normalize_iter(strings[1].chars())
768 .eq(strings[2].chars()));
769 assert!(nfd
770 .normalize_iter(strings[2].chars())
771 .eq(strings[2].chars()));
772
773 assert!(nfd
774 .normalize_iter(strings[3].chars())
775 .eq(strings[4].chars()));
776 assert!(nfd
777 .normalize_iter(strings[4].chars())
778 .eq(strings[4].chars()));
779
780 // NFKC
781 assert!(nfkc
782 .normalize_iter(strings[0].chars())
783 .eq(strings[3].chars()));
784 assert!(nfkc
785 .normalize_iter(strings[1].chars())
786 .eq(strings[3].chars()));
787 assert!(nfkc
788 .normalize_iter(strings[2].chars())
789 .eq(strings[3].chars()));
790 assert!(nfkc
791 .normalize_iter(strings[3].chars())
792 .eq(strings[3].chars()));
793 assert!(nfkc
794 .normalize_iter(strings[4].chars())
795 .eq(strings[3].chars()));
796
797 // NFKD
798 assert!(nfkd
799 .normalize_iter(strings[0].chars())
800 .eq(strings[4].chars()));
801 assert!(nfkd
802 .normalize_iter(strings[1].chars())
803 .eq(strings[4].chars()));
804 assert!(nfkd
805 .normalize_iter(strings[2].chars())
806 .eq(strings[4].chars()));
807 assert!(nfkd
808 .normalize_iter(strings[3].chars())
809 .eq(strings[4].chars()));
810 assert!(nfkd
811 .normalize_iter(strings[4].chars())
812 .eq(strings[4].chars()));
813 }
814 }
815
816 // Commented out, because we don't currently have a way to force a no-op set for testing.
817 // #[test]
818 // fn test_hangul() {
819 // use icu_collections::codepointinvlist::{CodePointSet, CodePointSetBuilder};
820 // use zerofrom::ZeroFrom;
821 // let builder = CodePointSetBuilder::new();
822 // let set: CodePointSet = builder.build();
823
824 // let normalizer: ComposingNormalizer = ComposingNormalizer::new_nfc();
825 // {
826 // let mut norm_iter = normalizer.normalize_iter("A\u{AC00}\u{11A7}".chars());
827 // // Pessimize passthrough to avoid hiding bugs.
828 // norm_iter
829 // .decomposition
830 // .potential_passthrough_and_not_backward_combining = Some(ZeroFrom::zero_from(&set));
831 // assert!(norm_iter.eq("A\u{AC00}\u{11A7}".chars()));
832 // }
833 // {
834 // let mut norm_iter = normalizer.normalize_iter("A\u{AC00}\u{11C2}".chars());
835 // // Pessimize passthrough to avoid hiding bugs.
836 // norm_iter
837 // .decomposition
838 // .potential_passthrough_and_not_backward_combining = Some(ZeroFrom::zero_from(&set));
839 // assert!(norm_iter.eq("A\u{AC1B}".chars()));
840 // }
841 // }
842
843 fn str_to_utf16(s: &str, sink: &mut StackVec) {
844 sink.clear();
845 let mut buf = [0u16; 2];
846 for c in s.chars() {
847 sink.try_extend_from_slice(c.encode_utf16(&mut buf))
848 .unwrap();
849 }
850 }
851
852 fn char_to_utf16(c: char, sink: &mut StackVec) {
853 sink.clear();
854 let mut buf = [0u16; 2];
855 sink.try_extend_from_slice(c.encode_utf16(&mut buf))
856 .unwrap();
857 }
858
859 fn str_to_str(s: &str, sink: &mut StackString) {
860 sink.clear();
861 sink.try_push_str(s).unwrap();
862 }
863
864 fn char_to_str(c: char, sink: &mut StackString) {
865 sink.clear();
866 sink.try_push(c).unwrap();
867 }
868
869 #[test]
870 fn test_conformance_utf16() {
871 let nfd: DecomposingNormalizer = DecomposingNormalizer::new_nfd();
872 let nfkd: DecomposingNormalizer = DecomposingNormalizer::new_nfkd();
873 let nfc: ComposingNormalizer = ComposingNormalizer::new_nfc();
874 let nfkc: ComposingNormalizer = ComposingNormalizer::new_nfkc();
875
876 let mut input = StackVec::new();
877 let mut normalized = StackVec::new();
878 let mut expected = StackVec::new();
879
880 let mut prev = 0u32;
881 let mut part = 0u8;
882 let data = include_bytes!("data/NormalizationTest.txt");
883 let lines = data.split(|b| b == &b'\n');
884 for line in lines {
885 if line.is_empty() {
886 continue;
887 }
888 if line.starts_with(b"#") {
889 continue;
890 }
891 if line.starts_with(&b"@Part"[..]) {
892 part = line[5] - b'0';
893 if part == 2 {
894 for u in prev + 1..=0x10FFFF {
895 if let Some(c) = char::from_u32(u) {
896 normalized.clear();
897 char_to_utf16(c, &mut input);
898 assert!(nfd.normalize_utf16_to(&input, &mut normalized).is_ok());
899 assert_eq!(&normalized, &input);
900
901 normalized.clear();
902 char_to_utf16(c, &mut input);
903 assert!(nfkd.normalize_utf16_to(&input, &mut normalized).is_ok());
904 assert_eq!(&normalized, &input);
905
906 normalized.clear();
907 char_to_utf16(c, &mut input);
908 assert!(nfc.normalize_utf16_to(&input, &mut normalized).is_ok());
909 assert_eq!(&normalized, &input);
910
911 normalized.clear();
912 char_to_utf16(c, &mut input);
913 assert!(nfkc.normalize_utf16_to(&input, &mut normalized).is_ok());
914 assert_eq!(&normalized, &input);
915 }
916 }
917 }
918 continue;
919 }
920 let strings = parse_hex(line);
921 // 0: source
922 // 1: NFC
923 // 2: NFD
924 // 3: NFKC
925 // 4: NFKD
926 if part == 1 {
927 let mut iter = strings[0].chars();
928 let current = iter.next().unwrap();
929 assert_eq!(iter.next(), None);
930 let current_u = u32::from(current);
931 for u in prev + 1..current_u {
932 if let Some(c) = char::from_u32(u) {
933 normalized.clear();
934 char_to_utf16(c, &mut input);
935 assert!(nfd.normalize_utf16_to(&input, &mut normalized).is_ok());
936 assert_eq!(&normalized, &input);
937
938 normalized.clear();
939 char_to_utf16(c, &mut input);
940 assert!(nfkd.normalize_utf16_to(&input, &mut normalized).is_ok());
941 assert_eq!(&normalized, &input);
942
943 normalized.clear();
944 char_to_utf16(c, &mut input);
945 assert!(nfc.normalize_utf16_to(&input, &mut normalized).is_ok());
946 assert_eq!(&normalized, &input);
947
948 normalized.clear();
949 char_to_utf16(c, &mut input);
950 assert!(nfkc.normalize_utf16_to(&input, &mut normalized).is_ok());
951 assert_eq!(&normalized, &input);
952 }
953 }
954 prev = current_u;
955 }
956 // NFC
957 normalized.clear();
958 str_to_utf16(&strings[0], &mut input);
959 str_to_utf16(&strings[1], &mut expected);
960 assert!(nfc.normalize_utf16_to(&input, &mut normalized).is_ok());
961 assert_eq!(&normalized, &expected);
962
963 normalized.clear();
964 str_to_utf16(&strings[1], &mut input);
965 str_to_utf16(&strings[1], &mut expected);
966 assert!(nfc.normalize_utf16_to(&input, &mut normalized).is_ok());
967 assert_eq!(&normalized, &expected);
968
969 normalized.clear();
970 str_to_utf16(&strings[2], &mut input);
971 str_to_utf16(&strings[1], &mut expected);
972 assert!(nfc.normalize_utf16_to(&input, &mut normalized).is_ok());
973 assert_eq!(&normalized, &expected);
974
975 normalized.clear();
976 str_to_utf16(&strings[3], &mut input);
977 str_to_utf16(&strings[3], &mut expected);
978 assert!(nfc.normalize_utf16_to(&input, &mut normalized).is_ok());
979 assert_eq!(&normalized, &expected);
980
981 normalized.clear();
982 str_to_utf16(&strings[4], &mut input);
983 str_to_utf16(&strings[3], &mut expected);
984 assert!(nfc.normalize_utf16_to(&input, &mut normalized).is_ok());
985 assert_eq!(&normalized, &expected);
986
987 // NFD
988 normalized.clear();
989 str_to_utf16(&strings[0], &mut input);
990 str_to_utf16(&strings[2], &mut expected);
991 assert!(nfd.normalize_utf16_to(&input, &mut normalized).is_ok());
992 assert_eq!(&normalized, &expected);
993
994 normalized.clear();
995 str_to_utf16(&strings[1], &mut input);
996 str_to_utf16(&strings[2], &mut expected);
997 assert!(nfd.normalize_utf16_to(&input, &mut normalized).is_ok());
998 assert_eq!(&normalized, &expected);
999
1000 normalized.clear();
1001 str_to_utf16(&strings[2], &mut input);
1002 str_to_utf16(&strings[2], &mut expected);
1003 assert!(nfd.normalize_utf16_to(&input, &mut normalized).is_ok());
1004 assert_eq!(&normalized, &expected);
1005
1006 normalized.clear();
1007 str_to_utf16(&strings[3], &mut input);
1008 str_to_utf16(&strings[4], &mut expected);
1009 assert!(nfd.normalize_utf16_to(&input, &mut normalized).is_ok());
1010 assert_eq!(&normalized, &expected);
1011
1012 normalized.clear();
1013 str_to_utf16(&strings[4], &mut input);
1014 str_to_utf16(&strings[4], &mut expected);
1015 assert!(nfd.normalize_utf16_to(&input, &mut normalized).is_ok());
1016 assert_eq!(&normalized, &expected);
1017
1018 // NFKC
1019 normalized.clear();
1020 str_to_utf16(&strings[0], &mut input);
1021 str_to_utf16(&strings[3], &mut expected);
1022 assert!(nfkc.normalize_utf16_to(&input, &mut normalized).is_ok());
1023 assert_eq!(&normalized, &expected);
1024
1025 normalized.clear();
1026 str_to_utf16(&strings[1], &mut input);
1027 str_to_utf16(&strings[3], &mut expected);
1028 assert!(nfkc.normalize_utf16_to(&input, &mut normalized).is_ok());
1029 assert_eq!(&normalized, &expected);
1030
1031 normalized.clear();
1032 str_to_utf16(&strings[2], &mut input);
1033 str_to_utf16(&strings[3], &mut expected);
1034 assert!(nfkc.normalize_utf16_to(&input, &mut normalized).is_ok());
1035 assert_eq!(&normalized, &expected);
1036
1037 normalized.clear();
1038 str_to_utf16(&strings[3], &mut input);
1039 str_to_utf16(&strings[3], &mut expected);
1040 assert!(nfkc.normalize_utf16_to(&input, &mut normalized).is_ok());
1041 assert_eq!(&normalized, &expected);
1042
1043 normalized.clear();
1044 str_to_utf16(&strings[4], &mut input);
1045 str_to_utf16(&strings[3], &mut expected);
1046 assert!(nfkc.normalize_utf16_to(&input, &mut normalized).is_ok());
1047 assert_eq!(&normalized, &expected);
1048
1049 // NFKD
1050 normalized.clear();
1051 str_to_utf16(&strings[0], &mut input);
1052 str_to_utf16(&strings[4], &mut expected);
1053 assert!(nfkd.normalize_utf16_to(&input, &mut normalized).is_ok());
1054 assert_eq!(&normalized, &expected);
1055
1056 normalized.clear();
1057 str_to_utf16(&strings[1], &mut input);
1058 str_to_utf16(&strings[4], &mut expected);
1059 assert!(nfkd.normalize_utf16_to(&input, &mut normalized).is_ok());
1060 assert_eq!(&normalized, &expected);
1061
1062 normalized.clear();
1063 str_to_utf16(&strings[2], &mut input);
1064 str_to_utf16(&strings[4], &mut expected);
1065 assert!(nfkd.normalize_utf16_to(&input, &mut normalized).is_ok());
1066 assert_eq!(&normalized, &expected);
1067
1068 normalized.clear();
1069 str_to_utf16(&strings[3], &mut input);
1070 str_to_utf16(&strings[4], &mut expected);
1071 assert!(nfkd.normalize_utf16_to(&input, &mut normalized).is_ok());
1072 assert_eq!(&normalized, &expected);
1073
1074 normalized.clear();
1075 str_to_utf16(&strings[4], &mut input);
1076 str_to_utf16(&strings[4], &mut expected);
1077 assert!(nfkd.normalize_utf16_to(&input, &mut normalized).is_ok());
1078 assert_eq!(&normalized, &expected);
1079 }
1080 }
1081
1082 #[test]
1083 fn test_conformance_utf8() {
1084 let nfd: DecomposingNormalizer = DecomposingNormalizer::new_nfd();
1085 let nfkd: DecomposingNormalizer = DecomposingNormalizer::new_nfkd();
1086 let nfc: ComposingNormalizer = ComposingNormalizer::new_nfc();
1087 let nfkc: ComposingNormalizer = ComposingNormalizer::new_nfkc();
1088
1089 let mut input = StackString::new();
1090 let mut normalized = StackString::new();
1091 let mut expected = StackString::new();
1092
1093 let mut prev = 0u32;
1094 let mut part = 0u8;
1095 let data = include_bytes!("data/NormalizationTest.txt");
1096 let lines = data.split(|b| b == &b'\n');
1097 for line in lines {
1098 if line.is_empty() {
1099 continue;
1100 }
1101 if line.starts_with(b"#") {
1102 continue;
1103 }
1104 if line.starts_with(&b"@Part"[..]) {
1105 part = line[5] - b'0';
1106 if part == 2 {
1107 for u in prev + 1..=0x10FFFF {
1108 if let Some(c) = char::from_u32(u) {
1109 normalized.clear();
1110 char_to_str(c, &mut input);
1111 assert!(nfd
1112 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1113 .is_ok());
1114 assert_eq!(&normalized, &input);
1115
1116 normalized.clear();
1117 char_to_str(c, &mut input);
1118 assert!(nfkd
1119 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1120 .is_ok());
1121 assert_eq!(&normalized, &input);
1122
1123 normalized.clear();
1124 char_to_str(c, &mut input);
1125 assert!(nfc
1126 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1127 .is_ok());
1128 assert_eq!(&normalized, &input);
1129
1130 normalized.clear();
1131 char_to_str(c, &mut input);
1132 assert!(nfkc
1133 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1134 .is_ok());
1135 assert_eq!(&normalized, &input);
1136 }
1137 }
1138 }
1139 continue;
1140 }
1141 let strings = parse_hex(line);
1142 // 0: source
1143 // 1: NFC
1144 // 2: NFD
1145 // 3: NFKC
1146 // 4: NFKD
1147 if part == 1 {
1148 let mut iter = strings[0].chars();
1149 let current = iter.next().unwrap();
1150 assert_eq!(iter.next(), None);
1151 let current_u = u32::from(current);
1152 for u in prev + 1..current_u {
1153 if let Some(c) = char::from_u32(u) {
1154 normalized.clear();
1155 char_to_str(c, &mut input);
1156 assert!(nfd
1157 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1158 .is_ok());
1159 assert_eq!(&normalized, &input);
1160
1161 normalized.clear();
1162 char_to_str(c, &mut input);
1163 assert!(nfkd
1164 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1165 .is_ok());
1166 assert_eq!(&normalized, &input);
1167
1168 normalized.clear();
1169 char_to_str(c, &mut input);
1170 assert!(nfc
1171 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1172 .is_ok());
1173 assert_eq!(&normalized, &input);
1174
1175 normalized.clear();
1176 char_to_str(c, &mut input);
1177 assert!(nfkc
1178 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1179 .is_ok());
1180 assert_eq!(&normalized, &input);
1181 }
1182 }
1183 prev = current_u;
1184 }
1185 // NFC
1186 normalized.clear();
1187 str_to_str(&strings[0], &mut input);
1188 str_to_str(&strings[1], &mut expected);
1189 assert!(nfc
1190 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1191 .is_ok());
1192 assert_eq!(&normalized, &expected);
1193
1194 normalized.clear();
1195 str_to_str(&strings[1], &mut input);
1196 str_to_str(&strings[1], &mut expected);
1197 assert!(nfc
1198 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1199 .is_ok());
1200 assert_eq!(&normalized, &expected);
1201
1202 normalized.clear();
1203 str_to_str(&strings[2], &mut input);
1204 str_to_str(&strings[1], &mut expected);
1205 assert!(nfc
1206 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1207 .is_ok());
1208 assert_eq!(&normalized, &expected);
1209
1210 normalized.clear();
1211 str_to_str(&strings[3], &mut input);
1212 str_to_str(&strings[3], &mut expected);
1213 assert!(nfc
1214 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1215 .is_ok());
1216 assert_eq!(&normalized, &expected);
1217
1218 normalized.clear();
1219 str_to_str(&strings[4], &mut input);
1220 str_to_str(&strings[3], &mut expected);
1221 assert!(nfc
1222 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1223 .is_ok());
1224 assert_eq!(&normalized, &expected);
1225
1226 // NFD
1227 normalized.clear();
1228 str_to_str(&strings[0], &mut input);
1229 str_to_str(&strings[2], &mut expected);
1230 assert!(nfd
1231 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1232 .is_ok());
1233 assert_eq!(&normalized, &expected);
1234
1235 normalized.clear();
1236 str_to_str(&strings[1], &mut input);
1237 str_to_str(&strings[2], &mut expected);
1238 assert!(nfd
1239 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1240 .is_ok());
1241 assert_eq!(&normalized, &expected);
1242
1243 normalized.clear();
1244 str_to_str(&strings[2], &mut input);
1245 str_to_str(&strings[2], &mut expected);
1246 assert!(nfd
1247 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1248 .is_ok());
1249 assert_eq!(&normalized, &expected);
1250
1251 normalized.clear();
1252 str_to_str(&strings[3], &mut input);
1253 str_to_str(&strings[4], &mut expected);
1254 assert!(nfd
1255 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1256 .is_ok());
1257 assert_eq!(&normalized, &expected);
1258
1259 normalized.clear();
1260 str_to_str(&strings[4], &mut input);
1261 str_to_str(&strings[4], &mut expected);
1262 assert!(nfd
1263 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1264 .is_ok());
1265 assert_eq!(&normalized, &expected);
1266
1267 // NFKC
1268 normalized.clear();
1269 str_to_str(&strings[0], &mut input);
1270 str_to_str(&strings[3], &mut expected);
1271 assert!(nfkc
1272 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1273 .is_ok());
1274 assert_eq!(&normalized, &expected);
1275
1276 normalized.clear();
1277 str_to_str(&strings[1], &mut input);
1278 str_to_str(&strings[3], &mut expected);
1279 assert!(nfkc
1280 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1281 .is_ok());
1282 assert_eq!(&normalized, &expected);
1283
1284 normalized.clear();
1285 str_to_str(&strings[2], &mut input);
1286 str_to_str(&strings[3], &mut expected);
1287 assert!(nfkc
1288 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1289 .is_ok());
1290 assert_eq!(&normalized, &expected);
1291
1292 normalized.clear();
1293 str_to_str(&strings[3], &mut input);
1294 str_to_str(&strings[3], &mut expected);
1295 assert!(nfkc
1296 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1297 .is_ok());
1298 assert_eq!(&normalized, &expected);
1299
1300 normalized.clear();
1301 str_to_str(&strings[4], &mut input);
1302 str_to_str(&strings[3], &mut expected);
1303 assert!(nfkc
1304 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1305 .is_ok());
1306 assert_eq!(&normalized, &expected);
1307
1308 // NFKD
1309 normalized.clear();
1310 str_to_str(&strings[0], &mut input);
1311 str_to_str(&strings[4], &mut expected);
1312 assert!(nfkd
1313 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1314 .is_ok());
1315 assert_eq!(&normalized, &expected);
1316
1317 normalized.clear();
1318 str_to_str(&strings[1], &mut input);
1319 str_to_str(&strings[4], &mut expected);
1320 assert!(nfkd
1321 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1322 .is_ok());
1323 assert_eq!(&normalized, &expected);
1324
1325 normalized.clear();
1326 str_to_str(&strings[2], &mut input);
1327 str_to_str(&strings[4], &mut expected);
1328 assert!(nfkd
1329 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1330 .is_ok());
1331 assert_eq!(&normalized, &expected);
1332
1333 normalized.clear();
1334 str_to_str(&strings[3], &mut input);
1335 str_to_str(&strings[4], &mut expected);
1336 assert!(nfkd
1337 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1338 .is_ok());
1339 assert_eq!(&normalized, &expected);
1340
1341 normalized.clear();
1342 str_to_str(&strings[4], &mut input);
1343 str_to_str(&strings[4], &mut expected);
1344 assert!(nfkd
1345 .normalize_utf8_to(input.as_bytes(), &mut normalized)
1346 .is_ok());
1347 assert_eq!(&normalized, &expected);
1348 }
1349 }
1350
1351 #[test]
1352 fn test_canonical_composition() {
1353 let comp = CanonicalComposition::new();
1354
1355 assert_eq!(comp.compose('a', 'b'), None); // Just two starters
1356
1357 assert_eq!(comp.compose('a', '\u{0308}'), Some('ä'));
1358 assert_eq!(comp.compose('A', '\u{0308}'), Some('Ä'));
1359 assert_eq!(comp.compose('ẹ', '\u{0302}'), Some('ệ'));
1360 assert_eq!(comp.compose('Ẹ', '\u{0302}'), Some('Ệ'));
1361 assert_eq!(comp.compose('\u{1D157}', '\u{1D165}'), None); // Composition exclusion
1362
1363 assert_eq!(comp.compose('ে', 'া'), Some('ো')); // Second is starter; BMP
1364 assert_eq!(comp.compose('𑄱', '𑄧'), Some('𑄮')); // Second is starter; non-BMP
1365
1366 assert_eq!(comp.compose('ᄀ', 'ᅡ'), Some('가')); // Hangul LV
1367 assert_eq!(comp.compose('가', 'ᆨ'), Some('각')); // Hangul LVT
1368 }
1369
1370 #[test]
1371 fn test_canonical_decomposition() {
1372 let decomp = CanonicalDecomposition::new();
1373
1374 assert_eq!(
1375 decomp.decompose('ä'),
1376 Decomposed::Expansion('a', '\u{0308}')
1377 );
1378 assert_eq!(
1379 decomp.decompose('Ä'),
1380 Decomposed::Expansion('A', '\u{0308}')
1381 );
1382 assert_eq!(
1383 decomp.decompose('ệ'),
1384 Decomposed::Expansion('ẹ', '\u{0302}')
1385 );
1386 assert_eq!(
1387 decomp.decompose('Ệ'),
1388 Decomposed::Expansion('Ẹ', '\u{0302}')
1389 );
1390 assert_eq!(
1391 decomp.decompose('\u{1D15E}'),
1392 Decomposed::Expansion('\u{1D157}', '\u{1D165}')
1393 );
1394 assert_eq!(decomp.decompose('ো'), Decomposed::Expansion('ে', 'া'));
1395 assert_eq!(decomp.decompose('𑄮'), Decomposed::Expansion('𑄱', '𑄧'));
1396 assert_eq!(decomp.decompose('가'), Decomposed::Expansion('ᄀ', 'ᅡ'));
1397 assert_eq!(decomp.decompose('각'), Decomposed::Expansion('가', 'ᆨ'));
1398
1399 assert_eq!(decomp.decompose('\u{212B}'), Decomposed::Singleton('Å')); // ANGSTROM SIGN
1400 assert_eq!(decomp.decompose('\u{2126}'), Decomposed::Singleton('Ω')); // OHM SIGN
1401
1402 assert_eq!(decomp.decompose('\u{1F71}'), Decomposed::Singleton('ά')); // oxia
1403 assert_eq!(
1404 decomp.decompose('\u{1F72}'),
1405 Decomposed::Expansion('ε', '\u{0300}')
1406 ); // not oxia but in the oxia range
1407 assert_eq!(
1408 decomp.decompose('ά'),
1409 Decomposed::Expansion('α', '\u{0301}')
1410 ); // tonos
1411 }
1412
1413 #[test]
1414 fn test_ccc() {
1415 let map = CanonicalCombiningClassMap::new();
1416 for u in 0..=0x10FFFF {
1417 assert_eq!(
1418 map.get32(u),
1419 icu_properties::maps::canonical_combining_class().get32(u)
1420 );
1421 }
1422 }
1423
1424 #[test]
1425 fn test_utf16_basic() {
1426 let normalizer: ComposingNormalizer = ComposingNormalizer::new_nfc();
1427
1428 assert_eq!(
1429 normalizer.normalize_utf16(&[0x0061]).as_slice(),
1430 [0x0061].as_slice()
1431 );
1432 assert_eq!(
1433 normalizer.normalize_utf16(&[0x0300, 0x0323]).as_slice(),
1434 [0x0323, 0x0300].as_slice()
1435 );
1436 }
1437
1438 #[test]
1439 fn test_accented_digraph() {
1440 let normalizer: DecomposingNormalizer = DecomposingNormalizer::new_nfkd();
1441 assert_eq!(
1442 normalizer.normalize("\u{01C4}\u{0323}"),
1443 "DZ\u{0323}\u{030C}"
1444 );
1445 assert_eq!(
1446 normalizer.normalize("DZ\u{030C}\u{0323}"),
1447 "DZ\u{0323}\u{030C}"
1448 );
1449 }
1450
1451 #[test]
1452 fn test_ddd() {
1453 let normalizer: DecomposingNormalizer = DecomposingNormalizer::new_nfd();
1454 assert_eq!(
1455 normalizer.normalize("\u{0DDD}\u{0334}"),
1456 "\u{0DD9}\u{0DCF}\u{0334}\u{0DCA}"
1457 );
1458 }
1459
1460 #[test]
1461 fn test_is_normalized() {
1462 let nfd: DecomposingNormalizer = DecomposingNormalizer::new_nfd();
1463 let nfkd: DecomposingNormalizer = DecomposingNormalizer::new_nfkd();
1464 let nfc: ComposingNormalizer = ComposingNormalizer::new_nfc();
1465 let nfkc: ComposingNormalizer = ComposingNormalizer::new_nfkc();
1466
1467 let aaa = "aaa";
1468 assert!(nfd.is_normalized(aaa));
1469 assert!(nfkd.is_normalized(aaa));
1470 assert!(nfc.is_normalized(aaa));
1471 assert!(nfkc.is_normalized(aaa));
1472
1473 assert!(nfd.is_normalized_utf8(aaa.as_bytes()));
1474 assert!(nfkd.is_normalized_utf8(aaa.as_bytes()));
1475 assert!(nfc.is_normalized_utf8(aaa.as_bytes()));
1476 assert!(nfkc.is_normalized_utf8(aaa.as_bytes()));
1477
1478 let aaa16 = [0x0061u16, 0x0061u16, 0x0061u16].as_slice();
1479 assert!(nfd.is_normalized_utf16(aaa16));
1480 assert!(nfkd.is_normalized_utf16(aaa16));
1481 assert!(nfc.is_normalized_utf16(aaa16));
1482 assert!(nfkc.is_normalized_utf16(aaa16));
1483
1484 let affa = b"a\xFFa";
1485 assert!(nfd.is_normalized_utf8(affa));
1486 assert!(nfkd.is_normalized_utf8(affa));
1487 assert!(nfc.is_normalized_utf8(affa));
1488 assert!(nfkc.is_normalized_utf8(affa));
1489
1490 let a_surrogate_a = [0x0061u16, 0xD800u16, 0x0061u16].as_slice();
1491 assert!(nfd.is_normalized_utf16(a_surrogate_a));
1492 assert!(nfkd.is_normalized_utf16(a_surrogate_a));
1493 assert!(nfc.is_normalized_utf16(a_surrogate_a));
1494 assert!(nfkc.is_normalized_utf16(a_surrogate_a));
1495
1496 let note = "a𝅗\u{1D165}a";
1497 assert!(nfd.is_normalized(note));
1498 assert!(nfkd.is_normalized(note));
1499 assert!(nfc.is_normalized(note));
1500 assert!(nfkc.is_normalized(note));
1501
1502 assert!(nfd.is_normalized_utf8(note.as_bytes()));
1503 assert!(nfkd.is_normalized_utf8(note.as_bytes()));
1504 assert!(nfc.is_normalized_utf8(note.as_bytes()));
1505 assert!(nfkc.is_normalized_utf8(note.as_bytes()));
1506
1507 let note16 = [
1508 0x0061u16, 0xD834u16, 0xDD57u16, 0xD834u16, 0xDD65u16, 0x0061u16,
1509 ]
1510 .as_slice();
1511 assert!(nfd.is_normalized_utf16(note16));
1512 assert!(nfkd.is_normalized_utf16(note16));
1513 assert!(nfc.is_normalized_utf16(note16));
1514 assert!(nfkc.is_normalized_utf16(note16));
1515
1516 let umlaut = "aäa";
1517 assert!(!nfd.is_normalized(umlaut));
1518 assert!(!nfkd.is_normalized(umlaut));
1519 assert!(nfc.is_normalized(umlaut));
1520 assert!(nfkc.is_normalized(umlaut));
1521
1522 assert!(!nfd.is_normalized_utf8(umlaut.as_bytes()));
1523 assert!(!nfkd.is_normalized_utf8(umlaut.as_bytes()));
1524 assert!(nfc.is_normalized_utf8(umlaut.as_bytes()));
1525 assert!(nfkc.is_normalized_utf8(umlaut.as_bytes()));
1526
1527 let umlaut16 = [0x0061u16, 0x00E4u16, 0x0061u16].as_slice();
1528 assert!(!nfd.is_normalized_utf16(umlaut16));
1529 assert!(!nfkd.is_normalized_utf16(umlaut16));
1530 assert!(nfc.is_normalized_utf16(umlaut16));
1531 assert!(nfkc.is_normalized_utf16(umlaut16));
1532
1533 let fraction = "a½a";
1534 assert!(nfd.is_normalized(fraction));
1535 assert!(!nfkd.is_normalized(fraction));
1536 assert!(nfc.is_normalized(fraction));
1537 assert!(!nfkc.is_normalized(fraction));
1538
1539 assert!(nfd.is_normalized_utf8(fraction.as_bytes()));
1540 assert!(!nfkd.is_normalized_utf8(fraction.as_bytes()));
1541 assert!(nfc.is_normalized_utf8(fraction.as_bytes()));
1542 assert!(!nfkc.is_normalized_utf8(fraction.as_bytes()));
1543
1544 let fraction16 = [0x0061u16, 0x00BDu16, 0x0061u16].as_slice();
1545 assert!(nfd.is_normalized_utf16(fraction16));
1546 assert!(!nfkd.is_normalized_utf16(fraction16));
1547 assert!(nfc.is_normalized_utf16(fraction16));
1548 assert!(!nfkc.is_normalized_utf16(fraction16));
1549 }