Debian Codegraph @

debian/rust-vendor/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::ComposingNormalizer;
6 use icu_normalizer::ComposingNormalizerBorrowed;
7 use icu_normalizer::DecomposingNormalizer;
8 use icu_normalizer::DecomposingNormalizerBorrowed;
9 use icu_normalizer::properties::CanonicalCombiningClassMap;
10 use icu_normalizer::properties::CanonicalCombiningClassMapBorrowed;
11 use icu_normalizer::properties::CanonicalComposition;
12 use icu_normalizer::properties::CanonicalCompositionBorrowed;
13 use icu_normalizer::properties::CanonicalDecomposition;
14 use icu_normalizer::properties::CanonicalDecompositionBorrowed;
15 use icu_normalizer::properties::Decomposed;
16 use icu_normalizer::uts46::Uts46Mapper;
17 use icu_normalizer::uts46::Uts46MapperBorrowed;
18
19 #[test]
20 fn test_nfd_basic() {
21 let normalizer = DecomposingNormalizerBorrowed::new_nfd();
22 assert_eq!(normalizer.normalize("ä"), "a\u{0308}");
23 assert_eq!(normalizer.normalize("Ä"), "A\u{0308}");
24 assert_eq!(normalizer.normalize("ệ"), "e\u{0323}\u{0302}");
25 assert_eq!(normalizer.normalize("Ệ"), "E\u{0323}\u{0302}");
26 assert_eq!(normalizer.normalize("𝅗𝅥"), "𝅗\u{1D165}");
27 assert_eq!(normalizer.normalize("\u{2126}"), "Ω"); // ohm sign
28 assert_eq!(normalizer.normalize("ベ"), "ベ"); // half-width unchanged
29 assert_eq!(normalizer.normalize("ペ"), "ペ"); // half-width unchanged
30 assert_eq!(normalizer.normalize("fi"), "fi"); // ligature unchanged
31 assert_eq!(normalizer.normalize("\u{FDFA}"), "\u{FDFA}"); // ligature unchanged
32 assert_eq!(normalizer.normalize("㈎"), "㈎"); // parenthetical unchanged
33 assert_eq!(normalizer.normalize("\u{0345}"), "\u{0345}"); // Iota subscript
34 }
35
36 #[test]
37 fn test_nfd_owned() {
38 let owned =
39 DecomposingNormalizer::try_new_nfd_unstable(&icu_normalizer::provider::Baked).unwrap();
40 let normalizer = owned.as_borrowed();
41 assert_eq!(normalizer.normalize("ä"), "a\u{0308}");
42 assert_eq!(normalizer.normalize("Ä"), "A\u{0308}");
43 assert_eq!(normalizer.normalize("ệ"), "e\u{0323}\u{0302}");
44 assert_eq!(normalizer.normalize("Ệ"), "E\u{0323}\u{0302}");
45 assert_eq!(normalizer.normalize("𝅗𝅥"), "𝅗\u{1D165}");
46 assert_eq!(normalizer.normalize("\u{2126}"), "Ω"); // ohm sign
47 assert_eq!(normalizer.normalize("ベ"), "ベ"); // half-width unchanged
48 assert_eq!(normalizer.normalize("ペ"), "ペ"); // half-width unchanged
49 assert_eq!(normalizer.normalize("fi"), "fi"); // ligature unchanged
50 assert_eq!(normalizer.normalize("\u{FDFA}"), "\u{FDFA}"); // ligature unchanged
51 assert_eq!(normalizer.normalize("㈎"), "㈎"); // parenthetical unchanged
52 assert_eq!(normalizer.normalize("\u{0345}"), "\u{0345}"); // Iota subscript
53 }
54
55 #[test]
56 fn test_nfkd_basic() {
57 let normalizer = DecomposingNormalizerBorrowed::new_nfkd();
58 assert_eq!(normalizer.normalize("ä"), "a\u{0308}");
59 assert_eq!(normalizer.normalize("Ä"), "A\u{0308}");
60 assert_eq!(normalizer.normalize("ệ"), "e\u{0323}\u{0302}");
61 assert_eq!(normalizer.normalize("Ệ"), "E\u{0323}\u{0302}");
62 assert_eq!(normalizer.normalize("𝅗𝅥"), "𝅗\u{1D165}");
63 assert_eq!(normalizer.normalize("\u{2126}"), "Ω"); // ohm sign
64 assert_eq!(normalizer.normalize("ベ"), "ヘ\u{3099}"); // half-width to full-width
65 assert_eq!(normalizer.normalize("ペ"), "ヘ\u{309A}"); // half-width to full-width
66 assert_eq!(normalizer.normalize("fi"), "fi"); // ligature expanded
67 assert_eq!(
68 normalizer.normalize("\u{FDFA}"),
69 "\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}"
70 );
71 // ligature expanded
72 assert_eq!(normalizer.normalize("㈎"), "(\u{1100}\u{1161})"); // parenthetical expanded
73 assert_eq!(normalizer.normalize("\u{0345}"), "\u{0345}"); // Iota subscript
74 }
75
76 #[test]
77 fn test_nfkd_owned() {
78 let owned =
79 DecomposingNormalizer::try_new_nfkd_unstable(&icu_normalizer::provider::Baked).unwrap();
80 let normalizer = owned.as_borrowed();
81 assert_eq!(normalizer.normalize("ä"), "a\u{0308}");
82 assert_eq!(normalizer.normalize("Ä"), "A\u{0308}");
83 assert_eq!(normalizer.normalize("ệ"), "e\u{0323}\u{0302}");
84 assert_eq!(normalizer.normalize("Ệ"), "E\u{0323}\u{0302}");
85 assert_eq!(normalizer.normalize("𝅗𝅥"), "𝅗\u{1D165}");
86 assert_eq!(normalizer.normalize("\u{2126}"), "Ω"); // ohm sign
87 assert_eq!(normalizer.normalize("ベ"), "ヘ\u{3099}"); // half-width to full-width
88 assert_eq!(normalizer.normalize("ペ"), "ヘ\u{309A}"); // half-width to full-width
89 assert_eq!(normalizer.normalize("fi"), "fi"); // ligature expanded
90 assert_eq!(
91 normalizer.normalize("\u{FDFA}"),
92 "\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}"
93 );
94 // ligature expanded
95 assert_eq!(normalizer.normalize("㈎"), "(\u{1100}\u{1161})"); // parenthetical expanded
96 assert_eq!(normalizer.normalize("\u{0345}"), "\u{0345}"); // Iota subscript
97 }
98
99 #[test]
100 fn test_nfc_basic() {
101 let normalizer = ComposingNormalizerBorrowed::new_nfc();
102 assert_eq!(normalizer.normalize("a\u{0308}"), "ä");
103 assert_eq!(normalizer.normalize("A\u{0308}"), "Ä");
104 assert_eq!(normalizer.normalize("e\u{0323}\u{0302}"), "ệ");
105 assert_eq!(normalizer.normalize("E\u{0323}\u{0302}"), "Ệ");
106 assert_eq!(normalizer.normalize("𝅗𝅥"), "𝅗\u{1D165}"); // Composition exclusion
107
108 assert_eq!(normalizer.normalize("\u{2126}"), "Ω"); // ohm sign
109 assert_eq!(normalizer.normalize("ベ"), "ベ"); // half-width unchanged
110 assert_eq!(normalizer.normalize("ペ"), "ペ"); // half-width unchanged
111 assert_eq!(normalizer.normalize("fi"), "fi"); // ligature unchanged
112 assert_eq!(normalizer.normalize("\u{FDFA}"), "\u{FDFA}"); // ligature unchanged
113 assert_eq!(normalizer.normalize("㈎"), "㈎"); // parenthetical unchanged
114 assert_eq!(normalizer.normalize("\u{0345}"), "\u{0345}"); // Iota subscript
115 }
116
117 #[test]
118 fn test_nfc_owned() {
119 let owned =
120 ComposingNormalizer::try_new_nfc_unstable(&icu_normalizer::provider::Baked).unwrap();
121 let normalizer = owned.as_borrowed();
122 assert_eq!(normalizer.normalize("a\u{0308}"), "ä");
123 assert_eq!(normalizer.normalize("A\u{0308}"), "Ä");
124 assert_eq!(normalizer.normalize("e\u{0323}\u{0302}"), "ệ");
125 assert_eq!(normalizer.normalize("E\u{0323}\u{0302}"), "Ệ");
126 assert_eq!(normalizer.normalize("𝅗𝅥"), "𝅗\u{1D165}"); // Composition exclusion
127
128 assert_eq!(normalizer.normalize("\u{2126}"), "Ω"); // ohm sign
129 assert_eq!(normalizer.normalize("ベ"), "ベ"); // half-width unchanged
130 assert_eq!(normalizer.normalize("ペ"), "ペ"); // half-width unchanged
131 assert_eq!(normalizer.normalize("fi"), "fi"); // ligature unchanged
132 assert_eq!(normalizer.normalize("\u{FDFA}"), "\u{FDFA}"); // ligature unchanged
133 assert_eq!(normalizer.normalize("㈎"), "㈎"); // parenthetical unchanged
134 assert_eq!(normalizer.normalize("\u{0345}"), "\u{0345}"); // Iota subscript
135 }
136
137 #[test]
138 fn test_nfkc_basic() {
139 let normalizer = ComposingNormalizerBorrowed::new_nfkc();
140 assert_eq!(normalizer.normalize("a\u{0308}"), "ä");
141 assert_eq!(normalizer.normalize("A\u{0308}"), "Ä");
142 assert_eq!(normalizer.normalize("e\u{0323}\u{0302}"), "ệ");
143 assert_eq!(normalizer.normalize("E\u{0323}\u{0302}"), "Ệ");
144 assert_eq!(normalizer.normalize("𝅗𝅥"), "𝅗\u{1D165}"); // Composition exclusion
145
146 assert_eq!(normalizer.normalize("\u{2126}"), "Ω"); // ohm sign
147 assert_eq!(normalizer.normalize("ベ"), "ベ"); // half-width to full-width, the compose
148 assert_eq!(normalizer.normalize("ペ"), "ペ"); // half-width to full-width, the compose
149 assert_eq!(normalizer.normalize("fi"), "fi"); // ligature expanded
150 assert_eq!(
151 normalizer.normalize("\u{FDFA}"),
152 "\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}"
153 );
154 // ligature expanded
155 assert_eq!(normalizer.normalize("㈎"), "(가)"); // parenthetical expanded and partially recomposed
156 assert_eq!(normalizer.normalize("\u{0345}"), "\u{0345}"); // Iota subscript
157 }
158
159 #[test]
160 fn test_nfkc_owned() {
161 let owned =
162 ComposingNormalizer::try_new_nfkc_unstable(&icu_normalizer::provider::Baked).unwrap();
163 let normalizer = owned.as_borrowed();
164 assert_eq!(normalizer.normalize("a\u{0308}"), "ä");
165 assert_eq!(normalizer.normalize("A\u{0308}"), "Ä");
166 assert_eq!(normalizer.normalize("e\u{0323}\u{0302}"), "ệ");
167 assert_eq!(normalizer.normalize("E\u{0323}\u{0302}"), "Ệ");
168 assert_eq!(normalizer.normalize("𝅗𝅥"), "𝅗\u{1D165}"); // Composition exclusion
169
170 assert_eq!(normalizer.normalize("\u{2126}"), "Ω"); // ohm sign
171 assert_eq!(normalizer.normalize("ベ"), "ベ"); // half-width to full-width, the compose
172 assert_eq!(normalizer.normalize("ペ"), "ペ"); // half-width to full-width, the compose
173 assert_eq!(normalizer.normalize("fi"), "fi"); // ligature expanded
174 assert_eq!(
175 normalizer.normalize("\u{FDFA}"),
176 "\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}"
177 );
178 // ligature expanded
179 assert_eq!(normalizer.normalize("㈎"), "(가)"); // parenthetical expanded and partially recomposed
180 assert_eq!(normalizer.normalize("\u{0345}"), "\u{0345}"); // Iota subscript
181 }
182
183 #[test]
184 fn test_uts46_map_normalize() {
185 let mapper = Uts46MapperBorrowed::new();
186 assert_eq!(
187 mapper
188 .map_normalize("a\u{0308}".chars())
189 .collect::<String>(),
190 "ä"
191 );
192 assert_eq!(
193 mapper
194 .map_normalize("A\u{0308}".chars())
195 .collect::<String>(),
196 "ä"
197 );
198 assert_eq!(
199 mapper
200 .map_normalize("e\u{0323}\u{0302}".chars())
201 .collect::<String>(),
202 "ệ"
203 );
204 assert_eq!(
205 mapper
206 .map_normalize("E\u{0323}\u{0302}".chars())
207 .collect::<String>(),
208 "ệ"
209 );
210 assert_eq!(
211 mapper.map_normalize("𝅗𝅥".chars()).collect::<String>(),
212 "𝅗\u{1D165}"
213 ); // Composition exclusion
214
215 assert_eq!(
216 mapper.map_normalize("\u{2126}".chars()).collect::<String>(),
217 "ω"
218 ); // ohm sign
219 assert_eq!(mapper.map_normalize("ベ".chars()).collect::<String>(), "ベ"); // half-width to full-width, the compose
220 assert_eq!(mapper.map_normalize("ペ".chars()).collect::<String>(), "ペ"); // half-width to full-width, the compose
221 assert_eq!(mapper.map_normalize("fi".chars()).collect::<String>(), "fi"); // ligature expanded
222 assert_eq!(
223 mapper.map_normalize("\u{FDFA}".chars()).collect::<String>(),
224 "\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}"
225 );
226 // ligature expanded
227 assert_eq!(
228 mapper.map_normalize("㈎".chars()).collect::<String>(),
229 "(가)"
230 ); // parenthetical expanded and partially recomposed
231
232 // Deviations (UTS 46, 6 Mapping Table Derivation, Step 4)
233 assert_eq!(
234 mapper.map_normalize("\u{200C}".chars()).collect::<String>(),
235 "\u{200C}"
236 );
237 assert_eq!(
238 mapper.map_normalize("\u{200D}".chars()).collect::<String>(),
239 "\u{200D}"
240 );
241 assert_eq!(mapper.map_normalize("ß".chars()).collect::<String>(), "ß");
242 assert_eq!(mapper.map_normalize("ς".chars()).collect::<String>(), "ς");
243
244 // Iota subscript
245 assert_eq!(
246 mapper.map_normalize("\u{0345}".chars()).collect::<String>(),
247 "ι"
248 );
249
250 // Disallowed
251 assert_eq!(
252 mapper.map_normalize("\u{061C}".chars()).collect::<String>(),
253 "\u{FFFD}"
254 );
255
256 // Ignored
257 assert_eq!(
258 mapper
259 .map_normalize("a\u{180B}b".chars())
260 .collect::<String>(),
261 "ab"
262 );
263 }
264
265 #[test]
266 fn test_uts46_owned() {
267 let owned = Uts46Mapper::try_new(&icu_normalizer::provider::Baked).unwrap();
268 let mapper = owned.as_borrowed();
269 assert_eq!(
270 mapper
271 .map_normalize("a\u{0308}".chars())
272 .collect::<String>(),
273 "ä"
274 );
275 assert_eq!(
276 mapper
277 .map_normalize("A\u{0308}".chars())
278 .collect::<String>(),
279 "ä"
280 );
281 assert_eq!(
282 mapper
283 .map_normalize("e\u{0323}\u{0302}".chars())
284 .collect::<String>(),
285 "ệ"
286 );
287 assert_eq!(
288 mapper
289 .map_normalize("E\u{0323}\u{0302}".chars())
290 .collect::<String>(),
291 "ệ"
292 );
293 assert_eq!(
294 mapper.map_normalize("𝅗𝅥".chars()).collect::<String>(),
295 "𝅗\u{1D165}"
296 ); // Composition exclusion
297
298 assert_eq!(
299 mapper.map_normalize("\u{2126}".chars()).collect::<String>(),
300 "ω"
301 ); // ohm sign
302 assert_eq!(mapper.map_normalize("ベ".chars()).collect::<String>(), "ベ"); // half-width to full-width, the compose
303 assert_eq!(mapper.map_normalize("ペ".chars()).collect::<String>(), "ペ"); // half-width to full-width, the compose
304 assert_eq!(mapper.map_normalize("fi".chars()).collect::<String>(), "fi"); // ligature expanded
305 assert_eq!(
306 mapper.map_normalize("\u{FDFA}".chars()).collect::<String>(),
307 "\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}"
308 );
309 // ligature expanded
310 assert_eq!(
311 mapper.map_normalize("㈎".chars()).collect::<String>(),
312 "(가)"
313 ); // parenthetical expanded and partially recomposed
314
315 // Deviations (UTS 46, 6 Mapping Table Derivation, Step 4)
316 assert_eq!(
317 mapper.map_normalize("\u{200C}".chars()).collect::<String>(),
318 "\u{200C}"
319 );
320 assert_eq!(
321 mapper.map_normalize("\u{200D}".chars()).collect::<String>(),
322 "\u{200D}"
323 );
324 assert_eq!(mapper.map_normalize("ß".chars()).collect::<String>(), "ß");
325 assert_eq!(mapper.map_normalize("ς".chars()).collect::<String>(), "ς");
326
327 // Iota subscript
328 assert_eq!(
329 mapper.map_normalize("\u{0345}".chars()).collect::<String>(),
330 "ι"
331 );
332
333 // Disallowed
334 assert_eq!(
335 mapper.map_normalize("\u{061C}".chars()).collect::<String>(),
336 "\u{FFFD}"
337 );
338
339 // Ignored
340 assert_eq!(
341 mapper
342 .map_normalize("a\u{180B}b".chars())
343 .collect::<String>(),
344 "ab"
345 );
346 }
347
348 #[test]
349 fn test_uts46_normalize_validate() {
350 let mapper = Uts46MapperBorrowed::new();
351 assert_eq!(
352 mapper
353 .normalize_validate("a\u{0308}".chars())
354 .collect::<String>(),
355 "ä"
356 );
357 assert_eq!(
358 mapper
359 .normalize_validate("A\u{0308}".chars())
360 .collect::<String>(),
361 "ä"
362 );
363 assert_eq!(
364 mapper
365 .normalize_validate("e\u{0323}\u{0302}".chars())
366 .collect::<String>(),
367 "ệ"
368 );
369 assert_eq!(
370 mapper
371 .normalize_validate("E\u{0323}\u{0302}".chars())
372 .collect::<String>(),
373 "ệ"
374 );
375 assert_eq!(
376 mapper.normalize_validate("𝅗𝅥".chars()).collect::<String>(),
377 "𝅗\u{1D165}"
378 ); // Composition exclusion
379
380 assert_eq!(
381 mapper
382 .normalize_validate("\u{2126}".chars())
383 .collect::<String>(),
384 "ω"
385 ); // ohm sign
386 assert_eq!(
387 mapper.normalize_validate("ベ".chars()).collect::<String>(),
388 "ベ"
389 ); // half-width to full-width, the compose
390 assert_eq!(
391 mapper.normalize_validate("ペ".chars()).collect::<String>(),
392 "ペ"
393 ); // half-width to full-width, the compose
394 assert_eq!(
395 mapper.normalize_validate("fi".chars()).collect::<String>(),
396 "fi"
397 ); // ligature expanded
398 assert_eq!(
399 mapper
400 .normalize_validate("\u{FDFA}".chars())
401 .collect::<String>(),
402 "\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}"
403 );
404 // ligature expanded
405 assert_eq!(
406 mapper.normalize_validate("㈎".chars()).collect::<String>(),
407 "(가)"
408 ); // parenthetical expanded and partially recomposed
409
410 // Deviations (UTS 46, 6 Mapping Table Derivation, Step 4)
411 assert_eq!(
412 mapper
413 .normalize_validate("\u{200C}".chars())
414 .collect::<String>(),
415 "\u{200C}"
416 );
417 assert_eq!(
418 mapper
419 .normalize_validate("\u{200D}".chars())
420 .collect::<String>(),
421 "\u{200D}"
422 );
423 assert_eq!(
424 mapper.normalize_validate("ß".chars()).collect::<String>(),
425 "ß"
426 );
427 assert_eq!(
428 mapper.normalize_validate("ς".chars()).collect::<String>(),
429 "ς"
430 );
431
432 // Iota subscript
433 assert_eq!(
434 mapper
435 .normalize_validate("\u{0345}".chars())
436 .collect::<String>(),
437 "ι"
438 );
439
440 // Disallowed
441 assert_eq!(
442 mapper
443 .normalize_validate("\u{061C}".chars())
444 .collect::<String>(),
445 "\u{FFFD}"
446 );
447
448 // Ignored
449 assert_eq!(
450 mapper
451 .normalize_validate("a\u{180B}b".chars())
452 .collect::<String>(),
453 "a\u{FFFD}b"
454 );
455 }
456
457 #[test]
458 fn test_nfd_str_to() {
459 let normalizer = DecomposingNormalizerBorrowed::new_nfd();
460
461 let mut buf = String::new();
462 assert!(normalizer.normalize_to("ä", &mut buf).is_ok());
463 assert_eq!(&buf, "a\u{0308}");
464
465 buf.clear();
466 assert!(normalizer.normalize_to("ệ", &mut buf).is_ok());
467 assert_eq!(&buf, "e\u{0323}\u{0302}");
468 }
469
470 #[test]
471 fn test_nfd_utf8_to() {
472 let normalizer = DecomposingNormalizerBorrowed::new_nfd();
473
474 let mut buf = String::new();
475 assert!(
476 normalizer
477 .normalize_utf8_to("ä".as_bytes(), &mut buf)
478 .is_ok()
479 );
480 assert_eq!(&buf, "a\u{0308}");
481
482 buf.clear();
483 assert!(
484 normalizer
485 .normalize_utf8_to("ệ".as_bytes(), &mut buf)
486 .is_ok()
487 );
488 assert_eq!(&buf, "e\u{0323}\u{0302}");
489 }
490
491 #[test]
492 fn test_nfd_utf16_to() {
493 let normalizer = DecomposingNormalizerBorrowed::new_nfd();
494
495 let mut buf = Vec::new();
496 assert!(
497 normalizer
498 .normalize_utf16_to([0x00E4u16].as_slice(), &mut buf)
499 .is_ok()
500 );
501 assert_eq!(&buf, [0x0061u16, 0x0308u16].as_slice());
502
503 buf.clear();
504 assert!(
505 normalizer
506 .normalize_utf16_to([0x1EC7u16].as_slice(), &mut buf)
507 .is_ok()
508 );
509 assert_eq!(&buf, [0x0065u16, 0x0323u16, 0x0302u16].as_slice());
510 }
511
512 #[test]
513 fn test_nfc_str_to() {
514 let normalizer = ComposingNormalizerBorrowed::new_nfc();
515
516 let mut buf = String::new();
517 assert!(normalizer.normalize_to("a\u{0308}", &mut buf).is_ok());
518 assert_eq!(&buf, "ä");
519
520 buf.clear();
521 assert!(
522 normalizer
523 .normalize_to("e\u{0323}\u{0302}", &mut buf)
524 .is_ok()
525 );
526 assert_eq!(&buf, "ệ");
527 }
528
529 #[test]
530 fn test_nfc_utf8_to() {
531 let normalizer = ComposingNormalizerBorrowed::new_nfc();
532
533 let mut buf = String::new();
534 assert!(
535 normalizer
536 .normalize_utf8_to("a\u{0308}".as_bytes(), &mut buf)
537 .is_ok()
538 );
539 assert_eq!(&buf, "ä");
540
541 buf.clear();
542 assert!(
543 normalizer
544 .normalize_utf8_to("e\u{0323}\u{0302}".as_bytes(), &mut buf)
545 .is_ok()
546 );
547 assert_eq!(&buf, "ệ");
548 }
549
550 #[test]
551 fn test_nfc_utf16_to() {
552 let normalizer = ComposingNormalizerBorrowed::new_nfc();
553
554 let mut buf = Vec::new();
555 assert!(
556 normalizer
557 .normalize_utf16_to([0x0061u16, 0x0308u16].as_slice(), &mut buf)
558 .is_ok()
559 );
560 assert_eq!(&buf, [0x00E4u16].as_slice());
561
562 buf.clear();
563 assert!(
564 normalizer
565 .normalize_utf16_to([0x0065u16, 0x0323u16, 0x0302u16].as_slice(), &mut buf)
566 .is_ok()
567 );
568 assert_eq!(&buf, [0x1EC7u16].as_slice());
569 }
570
571 #[test]
572 fn test_nfc_utf8_to_errors() {
573 let normalizer = ComposingNormalizerBorrowed::new_nfc();
574
575 let mut buf = String::new();
576 assert!(
577 normalizer
578 .normalize_utf8_to(b"\xFFa\xCC\x88\xFF", &mut buf)
579 .is_ok()
580 );
581 assert_eq!(&buf, "\u{FFFD}ä\u{FFFD}");
582
583 buf.clear();
584 assert!(
585 normalizer
586 .normalize_utf8_to(b"\x80e\xCC\xA3\xCC\x82\x80", &mut buf)
587 .is_ok()
588 );
589 assert_eq!(&buf, "\u{FFFD}ệ\u{FFFD}");
590
591 buf.clear();
592 assert!(
593 normalizer
594 .normalize_utf8_to(b"aaa\xFFaaa\xFFaaa", &mut buf)
595 .is_ok()
596 );
597 assert_eq!(&buf, "aaa\u{FFFD}aaa\u{FFFD}aaa");
598
599 buf.clear();
600 assert!(
601 normalizer
602 .normalize_utf8_to(b"aaa\xE2\x98aaa\xE2\x98aaa", &mut buf)
603 .is_ok()
604 );
605 assert_eq!(&buf, "aaa\u{FFFD}aaa\u{FFFD}aaa");
606 }
607
608 #[test]
609 fn test_nfd_utf8_to_errors() {
610 let normalizer = DecomposingNormalizerBorrowed::new_nfd();
611
612 let mut buf = String::new();
613 assert!(
614 normalizer
615 .normalize_utf8_to(b"\xFF\xC3\xA4\xFF", &mut buf)
616 .is_ok()
617 );
618 assert_eq!(&buf, "\u{FFFD}a\u{0308}\u{FFFD}");
619
620 buf.clear();
621 assert!(
622 normalizer
623 .normalize_utf8_to(b"\x80\xE1\xBB\x87\x80", &mut buf)
624 .is_ok()
625 );
626 assert_eq!(&buf, "\u{FFFD}e\u{0323}\u{0302}\u{FFFD}");
627
628 buf.clear();
629 assert!(
630 normalizer
631 .normalize_utf8_to(b"aaa\xFFaaa\xFFaaa", &mut buf)
632 .is_ok()
633 );
634 assert_eq!(&buf, "aaa\u{FFFD}aaa\u{FFFD}aaa");
635
636 buf.clear();
637 assert!(
638 normalizer
639 .normalize_utf8_to(b"aaa\xE2\x98aaa\xE2\x98aaa", &mut buf)
640 .is_ok()
641 );
642 assert_eq!(&buf, "aaa\u{FFFD}aaa\u{FFFD}aaa");
643 }
644
645 #[test]
646 fn test_nfc_utf16_to_errors() {
647 let normalizer = ComposingNormalizerBorrowed::new_nfc();
648
649 let mut buf = Vec::new();
650 assert!(
651 normalizer
652 .normalize_utf16_to([0xD800u16, 0x0061u16, 0x0308u16].as_slice(), &mut buf)
653 .is_ok()
654 );
655 assert_eq!(&buf, [0xFFFDu16, 0x00E4u16].as_slice());
656
657 buf.clear();
658 assert!(
659 normalizer
660 .normalize_utf16_to([0xDC00u16, 0x0061u16, 0x0308u16].as_slice(), &mut buf)
661 .is_ok()
662 );
663 assert_eq!(&buf, [0xFFFDu16, 0x00E4u16].as_slice());
664
665 buf.clear();
666 assert!(
667 normalizer
668 .normalize_utf16_to(
669 [0x0061u16, 0xD800u16, 0x0061u16, 0x0308u16].as_slice(),
670 &mut buf
671 )
672 .is_ok()
673 );
674 assert_eq!(&buf, [0x0061u16, 0xFFFDu16, 0x00E4u16].as_slice());
675
676 buf.clear();
677 assert!(
678 normalizer
679 .normalize_utf16_to(
680 [0x0061u16, 0xDC00u16, 0x0061u16, 0x0308u16].as_slice(),
681 &mut buf
682 )
683 .is_ok()
684 );
685 assert_eq!(&buf, [0x0061u16, 0xFFFDu16, 0x00E4u16].as_slice());
686
687 buf.clear();
688 assert!(
689 normalizer
690 .normalize_utf16_to(
691 [0x0061u16, 0xD800u16, 0x0061u16, 0x0308u16, 0xD800u16].as_slice(),
692 &mut buf
693 )
694 .is_ok()
695 );
696 assert_eq!(
697 &buf,
698 [0x0061u16, 0xFFFDu16, 0x00E4u16, 0xFFFDu16].as_slice()
699 );
700
701 buf.clear();
702 assert!(
703 normalizer
704 .normalize_utf16_to(
705 [0x0061u16, 0xDC00u16, 0x0061u16, 0x0308u16, 0xDC00u16].as_slice(),
706 &mut buf
707 )
708 .is_ok()
709 );
710 assert_eq!(
711 &buf,
712 [0x0061u16, 0xFFFDu16, 0x00E4u16, 0xFFFDu16].as_slice()
713 );
714
715 buf.clear();
716 assert!(
717 normalizer
718 .normalize_utf16_to(
719 [0x0061u16, 0xD800u16, 0x0061u16, 0x0061u16, 0xD800u16].as_slice(),
720 &mut buf
721 )
722 .is_ok()
723 );
724 assert_eq!(
725 &buf,
726 [0x0061u16, 0xFFFDu16, 0x0061u16, 0x0061u16, 0xFFFDu16].as_slice()
727 );
728
729 buf.clear();
730 assert!(
731 normalizer
732 .normalize_utf16_to(
733 [0x0061u16, 0xDC00u16, 0x0061u16, 0x0061u16, 0xDC00u16].as_slice(),
734 &mut buf
735 )
736 .is_ok()
737 );
738 assert_eq!(
739 &buf,
740 [0x0061u16, 0xFFFDu16, 0x0061u16, 0x0061u16, 0xFFFDu16].as_slice()
741 );
742
743 buf.clear();
744 assert!(
745 normalizer
746 .normalize_utf16_to(
747 [0x0061u16, 0xD800u16, 0x0308u16, 0xD800u16].as_slice(),
748 &mut buf
749 )
750 .is_ok()
751 );
752 assert_eq!(
753 &buf,
754 [0x0061u16, 0xFFFDu16, 0x0308u16, 0xFFFDu16].as_slice()
755 );
756
757 buf.clear();
758 assert!(
759 normalizer
760 .normalize_utf16_to(
761 [0x0061u16, 0xDC00u16, 0x0308u16, 0xDC00u16].as_slice(),
762 &mut buf
763 )
764 .is_ok()
765 );
766 assert_eq!(
767 &buf,
768 [0x0061u16, 0xFFFDu16, 0x0308u16, 0xFFFDu16].as_slice()
769 );
770 }
771
772 #[test]
773 fn test_nfd_utf16_to_errors() {
774 let normalizer = DecomposingNormalizerBorrowed::new_nfd();
775
776 let mut buf = Vec::new();
777 assert!(
778 normalizer
779 .normalize_utf16_to([0xD800u16, 0x00E4u16].as_slice(), &mut buf)
780 .is_ok()
781 );
782 assert_eq!(&buf, [0xFFFDu16, 0x0061u16, 0x0308u16].as_slice());
783
784 buf.clear();
785 assert!(
786 normalizer
787 .normalize_utf16_to([0xDC00u16, 0x00E4u16].as_slice(), &mut buf)
788 .is_ok()
789 );
790 assert_eq!(&buf, [0xFFFDu16, 0x0061u16, 0x0308u16].as_slice());
791
792 buf.clear();
793 assert!(
794 normalizer
795 .normalize_utf16_to([0x0061u16, 0xD800u16, 0x00E4u16].as_slice(), &mut buf)
796 .is_ok()
797 );
798 assert_eq!(
799 &buf,
800 [0x0061u16, 0xFFFDu16, 0x0061u16, 0x0308u16].as_slice()
801 );
802
803 buf.clear();
804 assert!(
805 normalizer
806 .normalize_utf16_to([0x0061u16, 0xDC00u16, 0x00E4u16].as_slice(), &mut buf)
807 .is_ok()
808 );
809 assert_eq!(
810 &buf,
811 [0x0061u16, 0xFFFDu16, 0x0061u16, 0x0308u16].as_slice()
812 );
813
814 buf.clear();
815 assert!(
816 normalizer
817 .normalize_utf16_to(
818 [0x0061u16, 0xD800u16, 0x00E4u16, 0xD800u16].as_slice(),
819 &mut buf
820 )
821 .is_ok()
822 );
823 assert_eq!(
824 &buf,
825 [0x0061u16, 0xFFFDu16, 0x0061u16, 0x0308u16, 0xFFFDu16].as_slice()
826 );
827
828 buf.clear();
829 assert!(
830 normalizer
831 .normalize_utf16_to(
832 [0x0061u16, 0xDC00u16, 0x00E4u16, 0xDC00u16].as_slice(),
833 &mut buf
834 )
835 .is_ok()
836 );
837 assert_eq!(
838 &buf,
839 [0x0061u16, 0xFFFDu16, 0x0061u16, 0x0308u16, 0xFFFDu16].as_slice()
840 );
841
842 buf.clear();
843 assert!(
844 normalizer
845 .normalize_utf16_to(
846 [0x0061u16, 0xD800u16, 0x0061u16, 0x0061u16, 0xD800u16].as_slice(),
847 &mut buf
848 )
849 .is_ok()
850 );
851 assert_eq!(
852 &buf,
853 [0x0061u16, 0xFFFDu16, 0x0061u16, 0x0061u16, 0xFFFDu16].as_slice()
854 );
855
856 buf.clear();
857 assert!(
858 normalizer
859 .normalize_utf16_to(
860 [0x0061u16, 0xDC00u16, 0x0061u16, 0x0061u16, 0xDC00u16].as_slice(),
861 &mut buf
862 )
863 .is_ok()
864 );
865 assert_eq!(
866 &buf,
867 [0x0061u16, 0xFFFDu16, 0x0061u16, 0x0061u16, 0xFFFDu16].as_slice()
868 );
869
870 buf.clear();
871 assert!(
872 normalizer
873 .normalize_utf16_to(
874 [0x0061u16, 0xD800u16, 0x0308u16, 0xD800u16].as_slice(),
875 &mut buf
876 )
877 .is_ok()
878 );
879 assert_eq!(
880 &buf,
881 [0x0061u16, 0xFFFDu16, 0x0308u16, 0xFFFDu16].as_slice()
882 );
883
884 buf.clear();
885 assert!(
886 normalizer
887 .normalize_utf16_to(
888 [0x0061u16, 0xDC00u16, 0x0308u16, 0xDC00u16].as_slice(),
889 &mut buf
890 )
891 .is_ok()
892 );
893 assert_eq!(
894 &buf,
895 [0x0061u16, 0xFFFDu16, 0x0308u16, 0xFFFDu16].as_slice()
896 );
897 }
898
899 use atoi::FromRadix16;
900 use icu_properties::props::CanonicalCombiningClass;
901
902 /// Parse five semicolon-terminated strings consisting of space-separated hexadecimal scalar values
903 fn parse_hex(mut hexes: &[u8]) -> [String; 5] {
904 let mut strings = [
905 String::new(),
906 String::new(),
907 String::new(),
908 String::new(),
909 String::new(),
910 ];
911 let mut current = 0;
912 loop {
913 let (scalar, mut offset) = u32::from_radix_16(hexes);
914 let c = char::from_u32(scalar).unwrap();
915 strings[current].push(c);
916 match hexes[offset] {
917 b';' => {
918 current += 1;
919 if current == strings.len() {
920 return strings;
921 }
922 offset += 1;
923 }
924 b' ' => {
925 offset += 1;
926 }
927 _ => {
928 panic!("Bad format: Garbage");
929 }
930 }
931 hexes = &hexes[offset..];
932 }
933 }
934
935 #[test]
936 fn test_conformance() {
937 let nfd = DecomposingNormalizerBorrowed::new_nfd();
938 let nfkd = DecomposingNormalizerBorrowed::new_nfkd();
939 let nfc = ComposingNormalizerBorrowed::new_nfc();
940 let nfkc = ComposingNormalizerBorrowed::new_nfkc();
941
942 let mut prev = 0u32;
943 let mut part = 0u8;
944 let data = include_bytes!("data/NormalizationTest.txt");
945 let lines = data.split(|b| b == &b'\n');
946 for line in lines {
947 if line.is_empty() {
948 continue;
949 }
950 if line.starts_with(b"#") {
951 continue;
952 }
953 if line.starts_with(&b"@Part"[..]) {
954 part = line[5] - b'0';
955 if part == 2 {
956 for u in prev + 1..=0x10FFFF {
957 if let Some(c) = char::from_u32(u) {
958 assert!(
959 nfd.normalize_iter(core::iter::once(c))
960 .eq(core::iter::once(c))
961 );
962 assert!(
963 nfkd.normalize_iter(core::iter::once(c))
964 .eq(core::iter::once(c))
965 );
966 assert!(
967 nfc.normalize_iter(core::iter::once(c))
968 .eq(core::iter::once(c))
969 );
970 assert!(
971 nfkc.normalize_iter(core::iter::once(c))
972 .eq(core::iter::once(c))
973 );
974 }
975 }
976 }
977 continue;
978 }
979 let strings = parse_hex(line);
980 // 0: source
981 // 1: NFC
982 // 2: NFD
983 // 3: NFKC
984 // 4: NFKD
985 if part == 1 {
986 let mut iter = strings[0].chars();
987 let current = iter.next().unwrap();
988 assert_eq!(iter.next(), None);
989 let current_u = u32::from(current);
990 for u in prev + 1..current_u {
991 if let Some(c) = char::from_u32(u) {
992 assert!(
993 nfd.normalize_iter(core::iter::once(c))
994 .eq(core::iter::once(c))
995 );
996 assert!(
997 nfkd.normalize_iter(core::iter::once(c))
998 .eq(core::iter::once(c))
999 );
1000 assert!(
1001 nfc.normalize_iter(core::iter::once(c))
1002 .eq(core::iter::once(c))
1003 );
1004 assert!(
1005 nfkc.normalize_iter(core::iter::once(c))
1006 .eq(core::iter::once(c))
1007 );
1008 }
1009 }
1010 prev = current_u;
1011 }
1012 // NFC
1013 assert!(
1014 nfc.normalize_iter(strings[0].chars())
1015 .eq(strings[1].chars())
1016 );
1017 assert!(
1018 nfc.normalize_iter(strings[1].chars())
1019 .eq(strings[1].chars())
1020 );
1021 assert!(
1022 nfc.normalize_iter(strings[2].chars())
1023 .eq(strings[1].chars())
1024 );
1025
1026 assert!(
1027 nfc.normalize_iter(strings[3].chars())
1028 .eq(strings[3].chars())
1029 );
1030 assert!(
1031 nfc.normalize_iter(strings[4].chars())
1032 .eq(strings[3].chars())
1033 );
1034
1035 // NFD
1036 assert!(
1037 nfd.normalize_iter(strings[0].chars())
1038 .eq(strings[2].chars())
1039 );
1040 assert!(
1041 nfd.normalize_iter(strings[1].chars())
1042 .eq(strings[2].chars())
1043 );
1044 assert!(
1045 nfd.normalize_iter(strings[2].chars())
1046 .eq(strings[2].chars())
1047 );
1048
1049 assert!(
1050 nfd.normalize_iter(strings[3].chars())
1051 .eq(strings[4].chars())
1052 );
1053 assert!(
1054 nfd.normalize_iter(strings[4].chars())
1055 .eq(strings[4].chars())
1056 );
1057
1058 // NFKC
1059 assert!(
1060 nfkc.normalize_iter(strings[0].chars())
1061 .eq(strings[3].chars())
1062 );
1063 assert!(
1064 nfkc.normalize_iter(strings[1].chars())
1065 .eq(strings[3].chars())
1066 );
1067 assert!(
1068 nfkc.normalize_iter(strings[2].chars())
1069 .eq(strings[3].chars())
1070 );
1071 assert!(
1072 nfkc.normalize_iter(strings[3].chars())
1073 .eq(strings[3].chars())
1074 );
1075 assert!(
1076 nfkc.normalize_iter(strings[4].chars())
1077 .eq(strings[3].chars())
1078 );
1079
1080 // NFKD
1081 assert!(
1082 nfkd.normalize_iter(strings[0].chars())
1083 .eq(strings[4].chars())
1084 );
1085 assert!(
1086 nfkd.normalize_iter(strings[1].chars())
1087 .eq(strings[4].chars())
1088 );
1089 assert!(
1090 nfkd.normalize_iter(strings[2].chars())
1091 .eq(strings[4].chars())
1092 );
1093 assert!(
1094 nfkd.normalize_iter(strings[3].chars())
1095 .eq(strings[4].chars())
1096 );
1097 assert!(
1098 nfkd.normalize_iter(strings[4].chars())
1099 .eq(strings[4].chars())
1100 );
1101 }
1102 }
1103
1104 // Commented out, because we don't currently have a way to force a no-op set for testing.
1105 // #[test]
1106 // fn test_hangul() {
1107 // use icu_collections::codepointinvlist::{CodePointSet, CodePointSetBuilder};
1108 // use zerofrom::ZeroFrom;
1109 // let builder = CodePointSetBuilder::new();
1110 // let set: CodePointSet = builder.build();
1111
1112 // let normalizer: ComposingNormalizer = ComposingNormalizerBorrowed::new_nfc();
1113 // {
1114 // let mut norm_iter = normalizer.normalize_iter("A\u{AC00}\u{11A7}".chars());
1115 // // Pessimize passthrough to avoid hiding bugs.
1116 // norm_iter
1117 // .decomposition
1118 // .potential_passthrough_and_not_backward_combining = Some(ZeroFrom::zero_from(&set));
1119 // assert!(norm_iter.eq("A\u{AC00}\u{11A7}".chars()));
1120 // }
1121 // {
1122 // let mut norm_iter = normalizer.normalize_iter("A\u{AC00}\u{11C2}".chars());
1123 // // Pessimize passthrough to avoid hiding bugs.
1124 // norm_iter
1125 // .decomposition
1126 // .potential_passthrough_and_not_backward_combining = Some(ZeroFrom::zero_from(&set));
1127 // assert!(norm_iter.eq("A\u{AC1B}".chars()));
1128 // }
1129 // }
1130
1131 fn str_to_utf16(s: &str, sink: &mut Vec<u16>) {
1132 sink.clear();
1133 sink.extend(s.encode_utf16());
1134 }
1135
1136 fn char_to_utf16(c: char, sink: &mut Vec<u16>) {
1137 sink.clear();
1138 sink.extend(c.encode_utf16(&mut [0; 2]).iter());
1139 }
1140
1141 fn str_to_str(s: &str, sink: &mut String) {
1142 sink.clear();
1143 sink.push_str(s);
1144 }
1145
1146 fn char_to_str(c: char, sink: &mut String) {
1147 sink.clear();
1148 sink.push(c);
1149 }
1150
1151 #[test]
1152 fn test_conformance_utf16() {
1153 let nfd = DecomposingNormalizerBorrowed::new_nfd();
1154 let nfkd = DecomposingNormalizerBorrowed::new_nfkd();
1155 let nfc = ComposingNormalizerBorrowed::new_nfc();
1156 let nfkc = ComposingNormalizerBorrowed::new_nfkc();
1157
1158 let mut input = Vec::new();
1159 let mut normalized = Vec::new();
1160 let mut expected = Vec::new();
1161
1162 let mut prev = 0u32;
1163 let mut part = 0u8;
1164 let data = include_bytes!("data/NormalizationTest.txt");
1165 let lines = data.split(|b| b == &b'\n');
1166 for line in lines {
1167 if line.is_empty() {
1168 continue;
1169 }
1170 if line.starts_with(b"#") {
1171 continue;
1172 }
1173 if line.starts_with(&b"@Part"[..]) {
1174 part = line[5] - b'0';
1175 if part == 2 {
1176 for u in prev + 1..=0x10FFFF {
1177 if let Some(c) = char::from_u32(u) {
1178 normalized.clear();
1179 char_to_utf16(c, &mut input);
1180 assert!(nfd.normalize_utf16_to(&input, &mut normalized).is_ok());
1181 assert_eq!(&normalized, &input);
1182
1183 normalized.clear();
1184 char_to_utf16(c, &mut input);
1185 assert!(nfkd.normalize_utf16_to(&input, &mut normalized).is_ok());
1186 assert_eq!(&normalized, &input);
1187
1188 normalized.clear();
1189 char_to_utf16(c, &mut input);
1190 assert!(nfc.normalize_utf16_to(&input, &mut normalized).is_ok());
1191 assert_eq!(&normalized, &input);
1192
1193 normalized.clear();
1194 char_to_utf16(c, &mut input);
1195 assert!(nfkc.normalize_utf16_to(&input, &mut normalized).is_ok());
1196 assert_eq!(&normalized, &input);
1197 }
1198 }
1199 }
1200 continue;
1201 }
1202 let strings = parse_hex(line);
1203 // 0: source
1204 // 1: NFC
1205 // 2: NFD
1206 // 3: NFKC
1207 // 4: NFKD
1208 if part == 1 {
1209 let mut iter = strings[0].chars();
1210 let current = iter.next().unwrap();
1211 assert_eq!(iter.next(), None);
1212 let current_u = u32::from(current);
1213 for u in prev + 1..current_u {
1214 if let Some(c) = char::from_u32(u) {
1215 normalized.clear();
1216 char_to_utf16(c, &mut input);
1217 assert!(nfd.normalize_utf16_to(&input, &mut normalized).is_ok());
1218 assert_eq!(&normalized, &input);
1219
1220 normalized.clear();
1221 char_to_utf16(c, &mut input);
1222 assert!(nfkd.normalize_utf16_to(&input, &mut normalized).is_ok());
1223 assert_eq!(&normalized, &input);
1224
1225 normalized.clear();
1226 char_to_utf16(c, &mut input);
1227 assert!(nfc.normalize_utf16_to(&input, &mut normalized).is_ok());
1228 assert_eq!(&normalized, &input);
1229
1230 normalized.clear();
1231 char_to_utf16(c, &mut input);
1232 assert!(nfkc.normalize_utf16_to(&input, &mut normalized).is_ok());
1233 assert_eq!(&normalized, &input);
1234 }
1235 }
1236 prev = current_u;
1237 }
1238 // NFC
1239 normalized.clear();
1240 str_to_utf16(&strings[0], &mut input);
1241 str_to_utf16(&strings[1], &mut expected);
1242 assert!(nfc.normalize_utf16_to(&input, &mut normalized).is_ok());
1243 assert_eq!(&normalized, &expected);
1244
1245 normalized.clear();
1246 str_to_utf16(&strings[1], &mut input);
1247 str_to_utf16(&strings[1], &mut expected);
1248 assert!(nfc.normalize_utf16_to(&input, &mut normalized).is_ok());
1249 assert_eq!(&normalized, &expected);
1250
1251 normalized.clear();
1252 str_to_utf16(&strings[2], &mut input);
1253 str_to_utf16(&strings[1], &mut expected);
1254 assert!(nfc.normalize_utf16_to(&input, &mut normalized).is_ok());
1255 assert_eq!(&normalized, &expected);
1256
1257 normalized.clear();
1258 str_to_utf16(&strings[3], &mut input);
1259 str_to_utf16(&strings[3], &mut expected);
1260 assert!(nfc.normalize_utf16_to(&input, &mut normalized).is_ok());
1261 assert_eq!(&normalized, &expected);
1262
1263 normalized.clear();
1264 str_to_utf16(&strings[4], &mut input);
1265 str_to_utf16(&strings[3], &mut expected);
1266 assert!(nfc.normalize_utf16_to(&input, &mut normalized).is_ok());
1267 assert_eq!(&normalized, &expected);
1268
1269 // NFD
1270 normalized.clear();
1271 str_to_utf16(&strings[0], &mut input);
1272 str_to_utf16(&strings[2], &mut expected);
1273 assert!(nfd.normalize_utf16_to(&input, &mut normalized).is_ok());
1274 assert_eq!(&normalized, &expected);
1275
1276 normalized.clear();
1277 str_to_utf16(&strings[1], &mut input);
1278 str_to_utf16(&strings[2], &mut expected);
1279 assert!(nfd.normalize_utf16_to(&input, &mut normalized).is_ok());
1280 assert_eq!(&normalized, &expected);
1281
1282 normalized.clear();
1283 str_to_utf16(&strings[2], &mut input);
1284 str_to_utf16(&strings[2], &mut expected);
1285 assert!(nfd.normalize_utf16_to(&input, &mut normalized).is_ok());
1286 assert_eq!(&normalized, &expected);
1287
1288 normalized.clear();
1289 str_to_utf16(&strings[3], &mut input);
1290 str_to_utf16(&strings[4], &mut expected);
1291 assert!(nfd.normalize_utf16_to(&input, &mut normalized).is_ok());
1292 assert_eq!(&normalized, &expected);
1293
1294 normalized.clear();
1295 str_to_utf16(&strings[4], &mut input);
1296 str_to_utf16(&strings[4], &mut expected);
1297 assert!(nfd.normalize_utf16_to(&input, &mut normalized).is_ok());
1298 assert_eq!(&normalized, &expected);
1299
1300 // NFKC
1301 normalized.clear();
1302 str_to_utf16(&strings[0], &mut input);
1303 str_to_utf16(&strings[3], &mut expected);
1304 assert!(nfkc.normalize_utf16_to(&input, &mut normalized).is_ok());
1305 assert_eq!(&normalized, &expected);
1306
1307 normalized.clear();
1308 str_to_utf16(&strings[1], &mut input);
1309 str_to_utf16(&strings[3], &mut expected);
1310 assert!(nfkc.normalize_utf16_to(&input, &mut normalized).is_ok());
1311 assert_eq!(&normalized, &expected);
1312
1313 normalized.clear();
1314 str_to_utf16(&strings[2], &mut input);
1315 str_to_utf16(&strings[3], &mut expected);
1316 assert!(nfkc.normalize_utf16_to(&input, &mut normalized).is_ok());
1317 assert_eq!(&normalized, &expected);
1318
1319 normalized.clear();
1320 str_to_utf16(&strings[3], &mut input);
1321 str_to_utf16(&strings[3], &mut expected);
1322 assert!(nfkc.normalize_utf16_to(&input, &mut normalized).is_ok());
1323 assert_eq!(&normalized, &expected);
1324
1325 normalized.clear();
1326 str_to_utf16(&strings[4], &mut input);
1327 str_to_utf16(&strings[3], &mut expected);
1328 assert!(nfkc.normalize_utf16_to(&input, &mut normalized).is_ok());
1329 assert_eq!(&normalized, &expected);
1330
1331 // NFKD
1332 normalized.clear();
1333 str_to_utf16(&strings[0], &mut input);
1334 str_to_utf16(&strings[4], &mut expected);
1335 assert!(nfkd.normalize_utf16_to(&input, &mut normalized).is_ok());
1336 assert_eq!(&normalized, &expected);
1337
1338 normalized.clear();
1339 str_to_utf16(&strings[1], &mut input);
1340 str_to_utf16(&strings[4], &mut expected);
1341 assert!(nfkd.normalize_utf16_to(&input, &mut normalized).is_ok());
1342 assert_eq!(&normalized, &expected);
1343
1344 normalized.clear();
1345 str_to_utf16(&strings[2], &mut input);
1346 str_to_utf16(&strings[4], &mut expected);
1347 assert!(nfkd.normalize_utf16_to(&input, &mut normalized).is_ok());
1348 assert_eq!(&normalized, &expected);
1349
1350 normalized.clear();
1351 str_to_utf16(&strings[3], &mut input);
1352 str_to_utf16(&strings[4], &mut expected);
1353 assert!(nfkd.normalize_utf16_to(&input, &mut normalized).is_ok());
1354 assert_eq!(&normalized, &expected);
1355
1356 normalized.clear();
1357 str_to_utf16(&strings[4], &mut input);
1358 str_to_utf16(&strings[4], &mut expected);
1359 assert!(nfkd.normalize_utf16_to(&input, &mut normalized).is_ok());
1360 assert_eq!(&normalized, &expected);
1361 }
1362 }
1363
1364 #[test]
1365 fn test_conformance_utf8() {
1366 let nfd = DecomposingNormalizerBorrowed::new_nfd();
1367 let nfkd = DecomposingNormalizerBorrowed::new_nfkd();
1368 let nfc = ComposingNormalizerBorrowed::new_nfc();
1369 let nfkc = ComposingNormalizerBorrowed::new_nfkc();
1370
1371 let mut input = String::new();
1372 let mut normalized = String::new();
1373 let mut expected = String::new();
1374
1375 let mut prev = 0u32;
1376 let mut part = 0u8;
1377 let data = include_bytes!("data/NormalizationTest.txt");
1378 let lines = data.split(|b| b == &b'\n');
1379 for line in lines {
1380 if line.is_empty() {
1381 continue;
1382 }
1383 if line.starts_with(b"#") {
1384 continue;
1385 }
1386 if line.starts_with(&b"@Part"[..]) {
1387 part = line[5] - b'0';
1388 if part == 2 {
1389 for u in prev + 1..=0x10FFFF {
1390 if let Some(c) = char::from_u32(u) {
1391 normalized.clear();
1392 char_to_str(c, &mut input);
1393 assert!(
1394 nfd.normalize_utf8_to(input.as_bytes(), &mut normalized)
1395 .is_ok()
1396 );
1397 assert_eq!(&normalized, &input);
1398
1399 normalized.clear();
1400 char_to_str(c, &mut input);
1401 assert!(
1402 nfkd.normalize_utf8_to(input.as_bytes(), &mut normalized)
1403 .is_ok()
1404 );
1405 assert_eq!(&normalized, &input);
1406
1407 normalized.clear();
1408 char_to_str(c, &mut input);
1409 assert!(
1410 nfc.normalize_utf8_to(input.as_bytes(), &mut normalized)
1411 .is_ok()
1412 );
1413 assert_eq!(&normalized, &input);
1414
1415 normalized.clear();
1416 char_to_str(c, &mut input);
1417 assert!(
1418 nfkc.normalize_utf8_to(input.as_bytes(), &mut normalized)
1419 .is_ok()
1420 );
1421 assert_eq!(&normalized, &input);
1422 }
1423 }
1424 }
1425 continue;
1426 }
1427 let strings = parse_hex(line);
1428 // 0: source
1429 // 1: NFC
1430 // 2: NFD
1431 // 3: NFKC
1432 // 4: NFKD
1433 if part == 1 {
1434 let mut iter = strings[0].chars();
1435 let current = iter.next().unwrap();
1436 assert_eq!(iter.next(), None);
1437 let current_u = u32::from(current);
1438 for u in prev + 1..current_u {
1439 if let Some(c) = char::from_u32(u) {
1440 normalized.clear();
1441 char_to_str(c, &mut input);
1442 assert!(
1443 nfd.normalize_utf8_to(input.as_bytes(), &mut normalized)
1444 .is_ok()
1445 );
1446 assert_eq!(&normalized, &input);
1447
1448 normalized.clear();
1449 char_to_str(c, &mut input);
1450 assert!(
1451 nfkd.normalize_utf8_to(input.as_bytes(), &mut normalized)
1452 .is_ok()
1453 );
1454 assert_eq!(&normalized, &input);
1455
1456 normalized.clear();
1457 char_to_str(c, &mut input);
1458 assert!(
1459 nfc.normalize_utf8_to(input.as_bytes(), &mut normalized)
1460 .is_ok()
1461 );
1462 assert_eq!(&normalized, &input);
1463
1464 normalized.clear();
1465 char_to_str(c, &mut input);
1466 assert!(
1467 nfkc.normalize_utf8_to(input.as_bytes(), &mut normalized)
1468 .is_ok()
1469 );
1470 assert_eq!(&normalized, &input);
1471 }
1472 }
1473 prev = current_u;
1474 }
1475 // NFC
1476 normalized.clear();
1477 str_to_str(&strings[0], &mut input);
1478 str_to_str(&strings[1], &mut expected);
1479 assert!(
1480 nfc.normalize_utf8_to(input.as_bytes(), &mut normalized)
1481 .is_ok()
1482 );
1483 assert_eq!(&normalized, &expected);
1484
1485 normalized.clear();
1486 str_to_str(&strings[1], &mut input);
1487 str_to_str(&strings[1], &mut expected);
1488 assert!(
1489 nfc.normalize_utf8_to(input.as_bytes(), &mut normalized)
1490 .is_ok()
1491 );
1492 assert_eq!(&normalized, &expected);
1493
1494 normalized.clear();
1495 str_to_str(&strings[2], &mut input);
1496 str_to_str(&strings[1], &mut expected);
1497 assert!(
1498 nfc.normalize_utf8_to(input.as_bytes(), &mut normalized)
1499 .is_ok()
1500 );
1501 assert_eq!(&normalized, &expected);
1502
1503 normalized.clear();
1504 str_to_str(&strings[3], &mut input);
1505 str_to_str(&strings[3], &mut expected);
1506 assert!(
1507 nfc.normalize_utf8_to(input.as_bytes(), &mut normalized)
1508 .is_ok()
1509 );
1510 assert_eq!(&normalized, &expected);
1511
1512 normalized.clear();
1513 str_to_str(&strings[4], &mut input);
1514 str_to_str(&strings[3], &mut expected);
1515 assert!(
1516 nfc.normalize_utf8_to(input.as_bytes(), &mut normalized)
1517 .is_ok()
1518 );
1519 assert_eq!(&normalized, &expected);
1520
1521 // NFD
1522 normalized.clear();
1523 str_to_str(&strings[0], &mut input);
1524 str_to_str(&strings[2], &mut expected);
1525 assert!(
1526 nfd.normalize_utf8_to(input.as_bytes(), &mut normalized)
1527 .is_ok()
1528 );
1529 assert_eq!(&normalized, &expected);
1530
1531 normalized.clear();
1532 str_to_str(&strings[1], &mut input);
1533 str_to_str(&strings[2], &mut expected);
1534 assert!(
1535 nfd.normalize_utf8_to(input.as_bytes(), &mut normalized)
1536 .is_ok()
1537 );
1538 assert_eq!(&normalized, &expected);
1539
1540 normalized.clear();
1541 str_to_str(&strings[2], &mut input);
1542 str_to_str(&strings[2], &mut expected);
1543 assert!(
1544 nfd.normalize_utf8_to(input.as_bytes(), &mut normalized)
1545 .is_ok()
1546 );
1547 assert_eq!(&normalized, &expected);
1548
1549 normalized.clear();
1550 str_to_str(&strings[3], &mut input);
1551 str_to_str(&strings[4], &mut expected);
1552 assert!(
1553 nfd.normalize_utf8_to(input.as_bytes(), &mut normalized)
1554 .is_ok()
1555 );
1556 assert_eq!(&normalized, &expected);
1557
1558 normalized.clear();
1559 str_to_str(&strings[4], &mut input);
1560 str_to_str(&strings[4], &mut expected);
1561 assert!(
1562 nfd.normalize_utf8_to(input.as_bytes(), &mut normalized)
1563 .is_ok()
1564 );
1565 assert_eq!(&normalized, &expected);
1566
1567 // NFKC
1568 normalized.clear();
1569 str_to_str(&strings[0], &mut input);
1570 str_to_str(&strings[3], &mut expected);
1571 assert!(
1572 nfkc.normalize_utf8_to(input.as_bytes(), &mut normalized)
1573 .is_ok()
1574 );
1575 assert_eq!(&normalized, &expected);
1576
1577 normalized.clear();
1578 str_to_str(&strings[1], &mut input);
1579 str_to_str(&strings[3], &mut expected);
1580 assert!(
1581 nfkc.normalize_utf8_to(input.as_bytes(), &mut normalized)
1582 .is_ok()
1583 );
1584 assert_eq!(&normalized, &expected);
1585
1586 normalized.clear();
1587 str_to_str(&strings[2], &mut input);
1588 str_to_str(&strings[3], &mut expected);
1589 assert!(
1590 nfkc.normalize_utf8_to(input.as_bytes(), &mut normalized)
1591 .is_ok()
1592 );
1593 assert_eq!(&normalized, &expected);
1594
1595 normalized.clear();
1596 str_to_str(&strings[3], &mut input);
1597 str_to_str(&strings[3], &mut expected);
1598 assert!(
1599 nfkc.normalize_utf8_to(input.as_bytes(), &mut normalized)
1600 .is_ok()
1601 );
1602 assert_eq!(&normalized, &expected);
1603
1604 normalized.clear();
1605 str_to_str(&strings[4], &mut input);
1606 str_to_str(&strings[3], &mut expected);
1607 assert!(
1608 nfkc.normalize_utf8_to(input.as_bytes(), &mut normalized)
1609 .is_ok()
1610 );
1611 assert_eq!(&normalized, &expected);
1612
1613 // NFKD
1614 normalized.clear();
1615 str_to_str(&strings[0], &mut input);
1616 str_to_str(&strings[4], &mut expected);
1617 assert!(
1618 nfkd.normalize_utf8_to(input.as_bytes(), &mut normalized)
1619 .is_ok()
1620 );
1621 assert_eq!(&normalized, &expected);
1622
1623 normalized.clear();
1624 str_to_str(&strings[1], &mut input);
1625 str_to_str(&strings[4], &mut expected);
1626 assert!(
1627 nfkd.normalize_utf8_to(input.as_bytes(), &mut normalized)
1628 .is_ok()
1629 );
1630 assert_eq!(&normalized, &expected);
1631
1632 normalized.clear();
1633 str_to_str(&strings[2], &mut input);
1634 str_to_str(&strings[4], &mut expected);
1635 assert!(
1636 nfkd.normalize_utf8_to(input.as_bytes(), &mut normalized)
1637 .is_ok()
1638 );
1639 assert_eq!(&normalized, &expected);
1640
1641 normalized.clear();
1642 str_to_str(&strings[3], &mut input);
1643 str_to_str(&strings[4], &mut expected);
1644 assert!(
1645 nfkd.normalize_utf8_to(input.as_bytes(), &mut normalized)
1646 .is_ok()
1647 );
1648 assert_eq!(&normalized, &expected);
1649
1650 normalized.clear();
1651 str_to_str(&strings[4], &mut input);
1652 str_to_str(&strings[4], &mut expected);
1653 assert!(
1654 nfkd.normalize_utf8_to(input.as_bytes(), &mut normalized)
1655 .is_ok()
1656 );
1657 assert_eq!(&normalized, &expected);
1658 }
1659 }
1660
1661 #[test]
1662 fn test_canonical_composition() {
1663 let comp = CanonicalCompositionBorrowed::new();
1664
1665 assert_eq!(comp.compose('a', 'b'), None); // Just two starters
1666
1667 assert_eq!(comp.compose('a', '\u{0308}'), Some('ä'));
1668 assert_eq!(comp.compose('A', '\u{0308}'), Some('Ä'));
1669 assert_eq!(comp.compose('ẹ', '\u{0302}'), Some('ệ'));
1670 assert_eq!(comp.compose('Ẹ', '\u{0302}'), Some('Ệ'));
1671 assert_eq!(comp.compose('\u{1D157}', '\u{1D165}'), None); // Composition exclusion
1672
1673 assert_eq!(comp.compose('ে', 'া'), Some('ো')); // Second is starter; BMP
1674 assert_eq!(comp.compose('𑄱', '𑄧'), Some('𑄮')); // Second is starter; non-BMP
1675
1676 assert_eq!(comp.compose('ᄀ', 'ᅡ'), Some('가')); // Hangul LV
1677 assert_eq!(comp.compose('가', 'ᆨ'), Some('각')); // Hangul LVT
1678 }
1679
1680 #[test]
1681 fn test_canonical_composition_owned() {
1682 let owned = CanonicalComposition::try_new_unstable(&icu_normalizer::provider::Baked).unwrap();
1683 let comp = owned.as_borrowed();
1684
1685 assert_eq!(comp.compose('a', 'b'), None); // Just two starters
1686
1687 assert_eq!(comp.compose('a', '\u{0308}'), Some('ä'));
1688 assert_eq!(comp.compose('A', '\u{0308}'), Some('Ä'));
1689 assert_eq!(comp.compose('ẹ', '\u{0302}'), Some('ệ'));
1690 assert_eq!(comp.compose('Ẹ', '\u{0302}'), Some('Ệ'));
1691 assert_eq!(comp.compose('\u{1D157}', '\u{1D165}'), None); // Composition exclusion
1692
1693 assert_eq!(comp.compose('ে', 'া'), Some('ো')); // Second is starter; BMP
1694 assert_eq!(comp.compose('𑄱', '𑄧'), Some('𑄮')); // Second is starter; non-BMP
1695
1696 assert_eq!(comp.compose('ᄀ', 'ᅡ'), Some('가')); // Hangul LV
1697 assert_eq!(comp.compose('가', 'ᆨ'), Some('각')); // Hangul LVT
1698 }
1699
1700 #[test]
1701 fn test_canonical_decomposition() {
1702 let decomp = CanonicalDecompositionBorrowed::new();
1703
1704 assert_eq!(
1705 decomp.decompose('ä'),
1706 Decomposed::Expansion('a', '\u{0308}')
1707 );
1708 assert_eq!(
1709 decomp.decompose('Ä'),
1710 Decomposed::Expansion('A', '\u{0308}')
1711 );
1712 assert_eq!(
1713 decomp.decompose('ệ'),
1714 Decomposed::Expansion('ẹ', '\u{0302}')
1715 );
1716 assert_eq!(
1717 decomp.decompose('Ệ'),
1718 Decomposed::Expansion('Ẹ', '\u{0302}')
1719 );
1720 assert_eq!(
1721 decomp.decompose('\u{1D15E}'),
1722 Decomposed::Expansion('\u{1D157}', '\u{1D165}')
1723 );
1724 assert_eq!(decomp.decompose('ো'), Decomposed::Expansion('ে', 'া'));
1725 assert_eq!(decomp.decompose('𑄮'), Decomposed::Expansion('𑄱', '𑄧'));
1726 assert_eq!(decomp.decompose('가'), Decomposed::Expansion('ᄀ', 'ᅡ'));
1727 assert_eq!(decomp.decompose('각'), Decomposed::Expansion('가', 'ᆨ'));
1728
1729 assert_eq!(decomp.decompose('\u{212B}'), Decomposed::Singleton('Å')); // ANGSTROM SIGN
1730 assert_eq!(decomp.decompose('\u{2126}'), Decomposed::Singleton('Ω')); // OHM SIGN
1731
1732 assert_eq!(decomp.decompose('\u{1F71}'), Decomposed::Singleton('ά')); // oxia
1733 assert_eq!(
1734 decomp.decompose('\u{1F72}'),
1735 Decomposed::Expansion('ε', '\u{0300}')
1736 ); // not oxia but in the oxia range
1737 assert_eq!(
1738 decomp.decompose('ά'),
1739 Decomposed::Expansion('α', '\u{0301}')
1740 ); // tonos
1741 }
1742
1743 #[test]
1744 fn test_canonical_decomposition_owned() {
1745 let owned = CanonicalDecomposition::try_new_unstable(&icu_normalizer::provider::Baked).unwrap();
1746 let decomp = owned.as_borrowed();
1747
1748 assert_eq!(
1749 decomp.decompose('ä'),
1750 Decomposed::Expansion('a', '\u{0308}')
1751 );
1752 assert_eq!(
1753 decomp.decompose('Ä'),
1754 Decomposed::Expansion('A', '\u{0308}')
1755 );
1756 assert_eq!(
1757 decomp.decompose('ệ'),
1758 Decomposed::Expansion('ẹ', '\u{0302}')
1759 );
1760 assert_eq!(
1761 decomp.decompose('Ệ'),
1762 Decomposed::Expansion('Ẹ', '\u{0302}')
1763 );
1764 assert_eq!(
1765 decomp.decompose('\u{1D15E}'),
1766 Decomposed::Expansion('\u{1D157}', '\u{1D165}')
1767 );
1768 assert_eq!(decomp.decompose('ো'), Decomposed::Expansion('ে', 'া'));
1769 assert_eq!(decomp.decompose('𑄮'), Decomposed::Expansion('𑄱', '𑄧'));
1770 assert_eq!(decomp.decompose('가'), Decomposed::Expansion('ᄀ', 'ᅡ'));
1771 assert_eq!(decomp.decompose('각'), Decomposed::Expansion('가', 'ᆨ'));
1772
1773 assert_eq!(decomp.decompose('\u{212B}'), Decomposed::Singleton('Å')); // ANGSTROM SIGN
1774 assert_eq!(decomp.decompose('\u{2126}'), Decomposed::Singleton('Ω')); // OHM SIGN
1775
1776 assert_eq!(decomp.decompose('\u{1F71}'), Decomposed::Singleton('ά')); // oxia
1777 assert_eq!(
1778 decomp.decompose('\u{1F72}'),
1779 Decomposed::Expansion('ε', '\u{0300}')
1780 ); // not oxia but in the oxia range
1781 assert_eq!(
1782 decomp.decompose('ά'),
1783 Decomposed::Expansion('α', '\u{0301}')
1784 ); // tonos
1785 }
1786
1787 #[test]
1788 fn test_ccc() {
1789 let map = CanonicalCombiningClassMapBorrowed::new();
1790 for u in 0..=0x10FFFF {
1791 assert_eq!(
1792 map.get32(u),
1793 icu_properties::CodePointMapData::<CanonicalCombiningClass>::new().get32(u)
1794 );
1795 }
1796 }
1797
1798 #[test]
1799 fn test_ccc_owned() {
1800 let owned =
1801 CanonicalCombiningClassMap::try_new_unstable(&icu_normalizer::provider::Baked).unwrap();
1802 let map = owned.as_borrowed();
1803 for u in 0..=0x10FFFF {
1804 assert_eq!(
1805 map.get32(u),
1806 icu_properties::CodePointMapData::<CanonicalCombiningClass>::new().get32(u)
1807 );
1808 }
1809 }
1810
1811 #[test]
1812 fn test_utf16_basic() {
1813 let normalizer = ComposingNormalizerBorrowed::new_nfc();
1814
1815 assert_eq!(
1816 normalizer.normalize_utf16(&[0x0061]).as_ref(),
1817 [0x0061].as_slice()
1818 );
1819 assert_eq!(
1820 normalizer.normalize_utf16(&[0x0300, 0x0323]).as_ref(),
1821 [0x0323, 0x0300].as_slice()
1822 );
1823 }
1824
1825 #[test]
1826 fn test_accented_digraph() {
1827 let normalizer = DecomposingNormalizerBorrowed::new_nfkd();
1828 assert_eq!(
1829 normalizer.normalize("\u{01C4}\u{0323}"),
1830 "DZ\u{0323}\u{030C}"
1831 );
1832 assert_eq!(
1833 normalizer.normalize("DZ\u{030C}\u{0323}"),
1834 "DZ\u{0323}\u{030C}"
1835 );
1836 }
1837
1838 #[test]
1839 fn test_ddd() {
1840 let normalizer = DecomposingNormalizerBorrowed::new_nfd();
1841 assert_eq!(
1842 normalizer.normalize("\u{0DDD}\u{0334}"),
1843 "\u{0DD9}\u{0DCF}\u{0334}\u{0DCA}"
1844 );
1845 }
1846
1847 #[test]
1848 fn test_is_normalized() {
1849 let nfd = DecomposingNormalizerBorrowed::new_nfd();
1850 let nfkd = DecomposingNormalizerBorrowed::new_nfkd();
1851 let nfc = ComposingNormalizerBorrowed::new_nfc();
1852 let nfkc = ComposingNormalizerBorrowed::new_nfkc();
1853
1854 let aaa = "aaa";
1855 assert!(nfd.is_normalized(aaa));
1856 assert!(nfkd.is_normalized(aaa));
1857 assert!(nfc.is_normalized(aaa));
1858 assert!(nfkc.is_normalized(aaa));
1859
1860 assert!(nfd.is_normalized_utf8(aaa.as_bytes()));
1861 assert!(nfkd.is_normalized_utf8(aaa.as_bytes()));
1862 assert!(nfc.is_normalized_utf8(aaa.as_bytes()));
1863 assert!(nfkc.is_normalized_utf8(aaa.as_bytes()));
1864
1865 let aaa16 = [0x0061u16, 0x0061u16, 0x0061u16].as_slice();
1866 assert!(nfd.is_normalized_utf16(aaa16));
1867 assert!(nfkd.is_normalized_utf16(aaa16));
1868 assert!(nfc.is_normalized_utf16(aaa16));
1869 assert!(nfkc.is_normalized_utf16(aaa16));
1870
1871 let affa = b"a\xFFa";
1872 assert!(nfd.is_normalized_utf8(affa));
1873 assert!(nfkd.is_normalized_utf8(affa));
1874 assert!(nfc.is_normalized_utf8(affa));
1875 assert!(nfkc.is_normalized_utf8(affa));
1876
1877 let a_surrogate_a = [0x0061u16, 0xD800u16, 0x0061u16].as_slice();
1878 assert!(nfd.is_normalized_utf16(a_surrogate_a));
1879 assert!(nfkd.is_normalized_utf16(a_surrogate_a));
1880 assert!(nfc.is_normalized_utf16(a_surrogate_a));
1881 assert!(nfkc.is_normalized_utf16(a_surrogate_a));
1882
1883 let note = "a𝅗\u{1D165}a";
1884 assert!(nfd.is_normalized(note));
1885 assert!(nfkd.is_normalized(note));
1886 assert!(nfc.is_normalized(note));
1887 assert!(nfkc.is_normalized(note));
1888
1889 assert!(nfd.is_normalized_utf8(note.as_bytes()));
1890 assert!(nfkd.is_normalized_utf8(note.as_bytes()));
1891 assert!(nfc.is_normalized_utf8(note.as_bytes()));
1892 assert!(nfkc.is_normalized_utf8(note.as_bytes()));
1893
1894 let note16 = [
1895 0x0061u16, 0xD834u16, 0xDD57u16, 0xD834u16, 0xDD65u16, 0x0061u16,
1896 ]
1897 .as_slice();
1898 assert!(nfd.is_normalized_utf16(note16));
1899 assert!(nfkd.is_normalized_utf16(note16));
1900 assert!(nfc.is_normalized_utf16(note16));
1901 assert!(nfkc.is_normalized_utf16(note16));
1902
1903 let umlaut = "aäa";
1904 assert!(!nfd.is_normalized(umlaut));
1905 assert!(!nfkd.is_normalized(umlaut));
1906 assert!(nfc.is_normalized(umlaut));
1907 assert!(nfkc.is_normalized(umlaut));
1908
1909 assert!(!nfd.is_normalized_utf8(umlaut.as_bytes()));
1910 assert!(!nfkd.is_normalized_utf8(umlaut.as_bytes()));
1911 assert!(nfc.is_normalized_utf8(umlaut.as_bytes()));
1912 assert!(nfkc.is_normalized_utf8(umlaut.as_bytes()));
1913
1914 let umlaut16 = [0x0061u16, 0x00E4u16, 0x0061u16].as_slice();
1915 assert!(!nfd.is_normalized_utf16(umlaut16));
1916 assert!(!nfkd.is_normalized_utf16(umlaut16));
1917 assert!(nfc.is_normalized_utf16(umlaut16));
1918 assert!(nfkc.is_normalized_utf16(umlaut16));
1919
1920 let fraction = "a½a";
1921 assert!(nfd.is_normalized(fraction));
1922 assert!(!nfkd.is_normalized(fraction));
1923 assert!(nfc.is_normalized(fraction));
1924 assert!(!nfkc.is_normalized(fraction));
1925
1926 assert!(nfd.is_normalized_utf8(fraction.as_bytes()));
1927 assert!(!nfkd.is_normalized_utf8(fraction.as_bytes()));
1928 assert!(nfc.is_normalized_utf8(fraction.as_bytes()));
1929 assert!(!nfkc.is_normalized_utf8(fraction.as_bytes()));
1930
1931 let fraction16 = [0x0061u16, 0x00BDu16, 0x0061u16].as_slice();
1932 assert!(nfd.is_normalized_utf16(fraction16));
1933 assert!(!nfkd.is_normalized_utf16(fraction16));
1934 assert!(nfc.is_normalized_utf16(fraction16));
1935 assert!(!nfkc.is_normalized_utf16(fraction16));
1936 }
1937
1938 #[test]
1939 fn test_is_normalized_up_to() {
1940 let nfd = DecomposingNormalizerBorrowed::new_nfd();
1941 let nfkd = DecomposingNormalizerBorrowed::new_nfkd();
1942 let nfc = ComposingNormalizerBorrowed::new_nfc();
1943 let nfkc = ComposingNormalizerBorrowed::new_nfkc();
1944
1945 // Check a string slice is normalized up to where is_normalized_up_to reports
1946 let check_str = |input: &str| {
1947 // Check nfd
1948 let (head, tail) = nfd.split_normalized(input);
1949 let mut normalized = String::from(head);
1950 let _ = nfd.normalize_to(tail, &mut normalized);
1951 assert!(nfd.is_normalized(&normalized));
1952
1953 // Check nfkd
1954 let (head, tail) = nfkd.split_normalized(input);
1955 let mut normalized = String::from(head);
1956 let _ = nfkd.normalize_to(tail, &mut normalized);
1957 assert!(nfkd.is_normalized(&normalized));
1958
1959 // Check nfc
1960 let (head, tail) = nfc.split_normalized(input);
1961 let mut normalized = String::from(head);
1962 let _ = nfc.normalize_to(tail, &mut normalized);
1963 assert!(nfc.is_normalized(&normalized));
1964
1965 // Check nfkc
1966 let (head, tail) = nfkc.split_normalized(input);
1967 let mut normalized = String::from(head);
1968 let _ = nfkc.normalize_to(tail, &mut normalized);
1969 assert!(nfkc.is_normalized(&normalized));
1970 };
1971
1972 // Check a string of UTF8 bytes is normalized up to where is_normalized_up_to reports
1973 // note: from_utf8 can panic with invalid UTF8 input
1974 let check_utf8 = |input: &[u8]| {
1975 // Check nfd
1976 let (head, tail) = nfd.split_normalized_utf8(input);
1977 let mut normalized = String::from(head);
1978 let _ = nfd.normalize_utf8_to(tail, &mut normalized);
1979 assert!(nfd.is_normalized(&normalized));
1980
1981 // Check nfkd
1982 let (head, tail) = nfkd.split_normalized_utf8(input);
1983 let mut normalized = String::from(head);
1984 let _ = nfkd.normalize_utf8_to(tail, &mut normalized);
1985 assert!(nfkd.is_normalized(&normalized));
1986
1987 // Check nfc
1988 let (head, tail) = nfc.split_normalized_utf8(input);
1989 let mut normalized = String::from(head);
1990 let _ = nfc.normalize_utf8_to(tail, &mut normalized);
1991 assert!(nfc.is_normalized(&normalized));
1992
1993 // Check nfkc
1994 let (head, tail) = nfkc.split_normalized_utf8(input);
1995 let mut normalized = String::from(head);
1996 let _ = nfkc.normalize_utf8_to(tail, &mut normalized);
1997 assert!(nfkc.is_normalized(&normalized));
1998 };
1999
2000 // Check a string of UTF-16 code units is normalized up to where is_normalized_up_to reports
2001 let check_utf16 = |input: &[u16]| {
2002 // Check nfd
2003 let (head, tail) = nfd.split_normalized_utf16(input);
2004 let mut normalized = head.to_vec();
2005 let _ = nfd.normalize_utf16_to(tail, &mut normalized);
2006 assert!(nfd.is_normalized_utf16(&normalized));
2007
2008 // Check nfkd
2009 let (head, tail) = nfkd.split_normalized_utf16(input);
2010 let mut normalized = head.to_vec();
2011 let _ = nfkd.normalize_utf16_to(tail, &mut normalized);
2012 assert!(nfkd.is_normalized_utf16(&normalized));
2013
2014 // Check nfc
2015 let (head, tail) = nfc.split_normalized_utf16(input);
2016 let mut normalized = head.to_vec();
2017 let _ = nfc.normalize_utf16_to(tail, &mut normalized);
2018 assert!(nfc.is_normalized_utf16(&normalized));
2019
2020 // Check nfkc
2021 let (head, tail) = nfkc.split_normalized_utf16(input);
2022 let mut normalized = head.to_vec();
2023 let _ = nfkc.normalize_utf16_to(tail, &mut normalized);
2024 assert!(nfkc.is_normalized_utf16(&normalized));
2025 };
2026
2027 let aaa = "aaa";
2028 check_str(aaa);
2029
2030 let aaa_utf8 = aaa.as_bytes();
2031 check_utf8(aaa_utf8);
2032
2033 let aaa_utf16: Vec<u16> = aaa.encode_utf16().collect();
2034 check_utf16(&aaa_utf16);
2035
2036 assert!(nfd.split_normalized(aaa).0.len() == aaa.len());
2037 assert!(nfkd.split_normalized(aaa).0.len() == aaa.len());
2038 assert!(nfc.split_normalized(aaa).0.len() == aaa.len());
2039 assert!(nfkc.split_normalized(aaa).0.len() == aaa.len());
2040 assert!(nfd.split_normalized_utf8(aaa_utf8).0.len() == aaa_utf8.len());
2041 assert!(nfkd.split_normalized_utf8(aaa_utf8).0.len() == aaa_utf8.len());
2042 assert!(nfc.split_normalized_utf8(aaa_utf8).0.len() == aaa_utf8.len());
2043 assert!(nfkc.split_normalized_utf8(aaa_utf8).0.len() == aaa_utf8.len());
2044 assert!(nfd.split_normalized_utf16(&aaa_utf16).0.len() == aaa_utf16.len());
2045 assert!(nfkd.split_normalized_utf16(&aaa_utf16).0.len() == aaa_utf16.len());
2046 assert!(nfc.split_normalized_utf16(&aaa_utf16).0.len() == aaa_utf16.len());
2047 assert!(nfkc.split_normalized_utf16(&aaa_utf16).0.len() == aaa_utf16.len());
2048
2049 let note = "a𝅗\u{1D165}a";
2050 check_str(note);
2051
2052 let note_utf8 = note.as_bytes();
2053 check_utf8(note_utf8);
2054
2055 let note_utf16: Vec<u16> = note.encode_utf16().collect();
2056 check_utf16(&note_utf16);
2057
2058 assert!(nfd.split_normalized(note).0.len() == note.len());
2059 assert!(nfkd.split_normalized(note).0.len() == note.len());
2060 assert!(nfc.split_normalized(note).0.len() == note.len());
2061 assert!(nfkc.split_normalized(note).0.len() == note.len());
2062 assert!(nfd.split_normalized_utf8(note_utf8).0.len() == note_utf8.len());
2063 assert!(nfkd.split_normalized_utf8(note_utf8).0.len() == note_utf8.len());
2064 assert!(nfc.split_normalized_utf8(note_utf8).0.len() == note_utf8.len());
2065 assert!(nfkc.split_normalized_utf8(note_utf8).0.len() == note_utf8.len());
2066 assert!(nfd.split_normalized_utf16(&note_utf16).0.len() == note_utf16.len());
2067 assert!(nfkd.split_normalized_utf16(&note_utf16).0.len() == note_utf16.len());
2068 assert!(nfc.split_normalized_utf16(&note_utf16).0.len() == note_utf16.len());
2069 assert!(nfkc.split_normalized_utf16(&note_utf16).0.len() == note_utf16.len());
2070
2071 let umlaut = "aäa";
2072 check_str(umlaut);
2073
2074 let umlaut_utf8 = umlaut.as_bytes();
2075 check_utf8(umlaut_utf8);
2076
2077 let umlaut_utf16: Vec<u16> = umlaut.encode_utf16().collect();
2078 check_utf16(&umlaut_utf16);
2079
2080 assert_eq!(nfd.split_normalized(umlaut).0.len(), 1);
2081 assert_eq!(nfkd.split_normalized(umlaut).0.len(), 1);
2082 assert_eq!(nfc.split_normalized(umlaut).0.len(), 4);
2083 assert_eq!(nfkc.split_normalized(umlaut).0.len(), 4);
2084 assert_eq!(nfd.split_normalized_utf8(umlaut_utf8).0.len(), 1);
2085 assert_eq!(nfkd.split_normalized_utf8(umlaut_utf8).0.len(), 1);
2086 assert_eq!(nfc.split_normalized_utf8(umlaut_utf8).0.len(), 4);
2087 assert_eq!(nfkc.split_normalized_utf8(umlaut_utf8).0.len(), 4);
2088 assert_eq!(nfd.split_normalized_utf16(&umlaut_utf16).0.len(), 1);
2089 assert_eq!(nfkd.split_normalized_utf16(&umlaut_utf16).0.len(), 1);
2090 assert_eq!(nfc.split_normalized_utf16(&umlaut_utf16).0.len(), 3);
2091 assert_eq!(nfkc.split_normalized_utf16(&umlaut_utf16).0.len(), 3);
2092
2093 let fraction = "a½a";
2094 check_str(fraction);
2095
2096 let fraction_utf8 = fraction.as_bytes();
2097 check_utf8(fraction_utf8);
2098
2099 let fraction_utf16: Vec<u16> = fraction.encode_utf16().collect();
2100 check_utf16(&fraction_utf16);
2101
2102 assert_eq!(nfd.split_normalized(fraction).0.len(), 4);
2103 assert_eq!(nfkd.split_normalized(fraction).0.len(), 1);
2104 assert_eq!(nfc.split_normalized(fraction).0.len(), 4);
2105 assert_eq!(nfkc.split_normalized(fraction).0.len(), 1);
2106 assert_eq!(nfd.split_normalized_utf8(fraction_utf8).0.len(), 4);
2107 assert_eq!(nfkd.split_normalized_utf8(fraction_utf8).0.len(), 1);
2108 assert_eq!(nfc.split_normalized_utf8(fraction_utf8).0.len(), 4);
2109 assert_eq!(nfkc.split_normalized_utf8(fraction_utf8).0.len(), 1);
2110 assert_eq!(nfd.split_normalized_utf16(&fraction_utf16).0.len(), 3);
2111 assert_eq!(nfkd.split_normalized_utf16(&fraction_utf16).0.len(), 1);
2112 assert_eq!(nfc.split_normalized_utf16(&fraction_utf16).0.len(), 3);
2113 assert_eq!(nfkc.split_normalized_utf16(&fraction_utf16).0.len(), 1);
2114
2115 let reversed_vietnamese = "e\u{0302}\u{0323}";
2116 check_str(reversed_vietnamese);
2117
2118 let reversed_vietnamese_utf8 = reversed_vietnamese.as_bytes();
2119 check_utf8(reversed_vietnamese_utf8);
2120
2121 let reversed_vietnamese_utf16: Vec<u16> = reversed_vietnamese.encode_utf16().collect();
2122 check_utf16(&reversed_vietnamese_utf16);
2123
2124 assert_eq!(nfd.split_normalized(reversed_vietnamese).0.len(), 1);
2125 assert_eq!(nfkd.split_normalized(reversed_vietnamese).0.len(), 1);
2126 assert_eq!(nfc.split_normalized(reversed_vietnamese).0.len(), 0);
2127 assert_eq!(nfkc.split_normalized(reversed_vietnamese).0.len(), 0);
2128 assert_eq!(
2129 nfd.split_normalized_utf8(reversed_vietnamese_utf8).0.len(),
2130 1
2131 );
2132 assert_eq!(
2133 nfkd.split_normalized_utf8(reversed_vietnamese_utf8).0.len(),
2134 1
2135 );
2136 assert_eq!(
2137 nfc.split_normalized_utf8(reversed_vietnamese_utf8).0.len(),
2138 0
2139 );
2140 assert_eq!(
2141 nfkc.split_normalized_utf8(reversed_vietnamese_utf8).0.len(),
2142 0
2143 );
2144 assert_eq!(
2145 nfd.split_normalized_utf16(&reversed_vietnamese_utf16)
2146 .0
2147 .len(),
2148 1
2149 );
2150 assert_eq!(
2151 nfkd.split_normalized_utf16(&reversed_vietnamese_utf16)
2152 .0
2153 .len(),
2154 1
2155 );
2156 assert_eq!(
2157 nfc.split_normalized_utf16(&reversed_vietnamese_utf16)
2158 .0
2159 .len(),
2160 0
2161 );
2162 assert_eq!(
2163 nfkc.split_normalized_utf16(&reversed_vietnamese_utf16)
2164 .0
2165 .len(),
2166 0
2167 );
2168
2169 let truncated_vietnamese = "e\u{0302}";
2170 check_str(truncated_vietnamese);
2171
2172 let truncated_vietnamese_utf8 = truncated_vietnamese.as_bytes();
2173 check_utf8(truncated_vietnamese_utf8);
2174
2175 let truncated_vietnamese_utf16: Vec<u16> = truncated_vietnamese.encode_utf16().collect();
2176 check_utf16(&truncated_vietnamese_utf16);
2177
2178 assert_eq!(nfd.split_normalized(truncated_vietnamese).0.len(), 3);
2179 assert_eq!(nfkd.split_normalized(truncated_vietnamese).0.len(), 3);
2180 assert_eq!(nfc.split_normalized(truncated_vietnamese).0.len(), 0);
2181 assert_eq!(nfkc.split_normalized(truncated_vietnamese).0.len(), 0);
2182 assert_eq!(
2183 nfd.split_normalized_utf8(truncated_vietnamese_utf8).0.len(),
2184 3
2185 );
2186 assert_eq!(
2187 nfkd.split_normalized_utf8(truncated_vietnamese_utf8)
2188 .0
2189 .len(),
2190 3
2191 );
2192 assert_eq!(
2193 nfc.split_normalized_utf8(truncated_vietnamese_utf8).0.len(),
2194 0
2195 );
2196 assert_eq!(
2197 nfkc.split_normalized_utf8(truncated_vietnamese_utf8)
2198 .0
2199 .len(),
2200 0
2201 );
2202 assert_eq!(
2203 nfd.split_normalized_utf16(&truncated_vietnamese_utf16)
2204 .0
2205 .len(),
2206 2
2207 );
2208 assert_eq!(
2209 nfkd.split_normalized_utf16(&truncated_vietnamese_utf16)
2210 .0
2211 .len(),
2212 2
2213 );
2214 assert_eq!(
2215 nfc.split_normalized_utf16(&truncated_vietnamese_utf16)
2216 .0
2217 .len(),
2218 0
2219 );
2220 assert_eq!(
2221 nfkc.split_normalized_utf16(&truncated_vietnamese_utf16)
2222 .0
2223 .len(),
2224 0
2225 );
2226 }