Debian Codegraph @

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