simd-normalizer 0.1.1

SIMD-accelerated Unicode normalization (NFC, NFD, NFKC, NFKD)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
//! Supplementary plane edge case tests.
//!
//! Tests Unicode characters outside the BMP (U+10000 and above) that exercise
//! 4-byte UTF-8 encoding, supplementary decompositions, compatibility mappings,
//! case folding, and SIMD chunk boundary handling for multi-byte sequences.
//!
//! Categories:
//! 1. Musical symbols with canonical decompositions (composition exclusions)
//! 2. CJK Compatibility Ideographs Supplement (U+2F800 range)
//! 3. Mathematical Alphanumeric Symbols (U+1D400 range) -- NFKC/NFKD
//! 4. Supplementary character case folding (math bold, Deseret)
//! 5. 4-byte UTF-8 supplementary chars at every SIMD chunk offset position
//! 6. Cross-validation of all results against ICU4X

use icu_normalizer::{ComposingNormalizerBorrowed, DecomposingNormalizerBorrowed};
use simd_normalizer::matching::{MatchingOptions, normalize_for_matching};
use simd_normalizer::{CaseFoldMode, UnicodeNormalization, casefold};

// ---------------------------------------------------------------------------
// ICU4X reference helpers
// ---------------------------------------------------------------------------

fn icu_nfc(s: &str) -> String {
    ComposingNormalizerBorrowed::new_nfc()
        .normalize(s)
        .into_owned()
}

fn icu_nfd(s: &str) -> String {
    DecomposingNormalizerBorrowed::new_nfd()
        .normalize(s)
        .into_owned()
}

fn icu_nfkc(s: &str) -> String {
    ComposingNormalizerBorrowed::new_nfkc()
        .normalize(s)
        .into_owned()
}

fn icu_nfkd(s: &str) -> String {
    DecomposingNormalizerBorrowed::new_nfkd()
        .normalize(s)
        .into_owned()
}

// ---------------------------------------------------------------------------
// Cross-validation assertion helpers
// ---------------------------------------------------------------------------

/// Assert NFD output matches expected AND matches ICU4X.
fn assert_nfd(input: &str, expected: &str) {
    let simd_result = input.nfd();
    let icu_result = icu_nfd(input);
    assert_eq!(
        &*simd_result, expected,
        "NFD mismatch for {:?}: simd={:?}, expected={:?}",
        input, simd_result, expected
    );
    assert_eq!(
        &*simd_result, &*icu_result,
        "NFD cross-validation failed for {:?}: simd={:?}, icu={:?}",
        input, simd_result, icu_result
    );
}

/// Assert NFC output matches expected AND matches ICU4X.
fn assert_nfc(input: &str, expected: &str) {
    let simd_result = input.nfc();
    let icu_result = icu_nfc(input);
    assert_eq!(
        &*simd_result, expected,
        "NFC mismatch for {:?}: simd={:?}, expected={:?}",
        input, simd_result, expected
    );
    assert_eq!(
        &*simd_result, &*icu_result,
        "NFC cross-validation failed for {:?}: simd={:?}, icu={:?}",
        input, simd_result, icu_result
    );
}

/// Assert NFKD output matches expected AND matches ICU4X.
fn assert_nfkd(input: &str, expected: &str) {
    let simd_result = input.nfkd();
    let icu_result = icu_nfkd(input);
    assert_eq!(
        &*simd_result, expected,
        "NFKD mismatch for {:?}: simd={:?}, expected={:?}",
        input, simd_result, expected
    );
    assert_eq!(
        &*simd_result, &*icu_result,
        "NFKD cross-validation failed for {:?}: simd={:?}, icu={:?}",
        input, simd_result, icu_result
    );
}

/// Assert NFKC output matches expected AND matches ICU4X.
fn assert_nfkc(input: &str, expected: &str) {
    let simd_result = input.nfkc();
    let icu_result = icu_nfkc(input);
    assert_eq!(
        &*simd_result, expected,
        "NFKC mismatch for {:?}: simd={:?}, expected={:?}",
        input, simd_result, expected
    );
    assert_eq!(
        &*simd_result, &*icu_result,
        "NFKC cross-validation failed for {:?}: simd={:?}, icu={:?}",
        input, simd_result, icu_result
    );
}

/// Assert all four normalization forms match ICU4X for the given input.
fn assert_all_forms_match_icu(input: &str) {
    let nfc_simd = input.nfc();
    let nfd_simd = input.nfd();
    let nfkc_simd = input.nfkc();
    let nfkd_simd = input.nfkd();

    assert_eq!(
        &*nfc_simd,
        &*icu_nfc(input),
        "NFC cross-validation failed for input ({} bytes)",
        input.len()
    );
    assert_eq!(
        &*nfd_simd,
        &*icu_nfd(input),
        "NFD cross-validation failed for input ({} bytes)",
        input.len()
    );
    assert_eq!(
        &*nfkc_simd,
        &*icu_nfkc(input),
        "NFKC cross-validation failed for input ({} bytes)",
        input.len()
    );
    assert_eq!(
        &*nfkd_simd,
        &*icu_nfkd(input),
        "NFKD cross-validation failed for input ({} bytes)",
        input.len()
    );
}

/// Assert is_normalized checks match ICU4X for the given input.
fn assert_is_normalized_matches_icu(input: &str) {
    let icu_nfc = ComposingNormalizerBorrowed::new_nfc();
    let icu_nfd = DecomposingNormalizerBorrowed::new_nfd();
    let icu_nfkc = ComposingNormalizerBorrowed::new_nfkc();
    let icu_nfkd = DecomposingNormalizerBorrowed::new_nfkd();

    assert_eq!(
        input.is_nfc(),
        icu_nfc.is_normalized(input),
        "is_nfc mismatch for {:?}",
        input
    );
    assert_eq!(
        input.is_nfd(),
        icu_nfd.is_normalized(input),
        "is_nfd mismatch for {:?}",
        input
    );
    assert_eq!(
        input.is_nfkc(),
        icu_nfkc.is_normalized(input),
        "is_nfkc mismatch for {:?}",
        input
    );
    assert_eq!(
        input.is_nfkd(),
        icu_nfkd.is_normalized(input),
        "is_nfkd mismatch for {:?}",
        input
    );
}

/// Build a string of exactly `n` ASCII bytes using a repeating pattern.
fn ascii_pad(n: usize) -> String {
    let pattern = b"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_-";
    let mut s = String::with_capacity(n);
    for i in 0..n {
        s.push(pattern[i % pattern.len()] as char);
    }
    debug_assert_eq!(s.len(), n);
    s
}

// ===========================================================================
// 1. Musical symbols with canonical decompositions (composition exclusions)
// ===========================================================================

#[test]
fn musical_half_note_decomposition() {
    // U+1D15E (MUSICAL SYMBOL HALF NOTE) canonically decomposes to
    // U+1D157 (VOID NOTEHEAD) + U+1D165 (COMBINING STEM).
    // This is a composition exclusion, so NFC does NOT recompose it.
    let half_note = "\u{1D15E}";
    let expected_decomposed = "\u{1D157}\u{1D165}";

    assert_nfd(half_note, expected_decomposed);
    assert_nfkd(half_note, expected_decomposed);
    // Composition exclusion: NFC and NFKC produce the same decomposed form.
    assert_nfc(half_note, expected_decomposed);
    assert_nfkc(half_note, expected_decomposed);
}

#[test]
fn musical_quarter_note_decomposition() {
    // U+1D15F (MUSICAL SYMBOL QUARTER NOTE) canonically decomposes to
    // U+1D158 (NOTEPAD BLACK) + U+1D165 (COMBINING STEM).
    let quarter_note = "\u{1D15F}";
    let expected_decomposed = "\u{1D158}\u{1D165}";

    assert_nfd(quarter_note, expected_decomposed);
    assert_nfkd(quarter_note, expected_decomposed);
    assert_nfc(quarter_note, expected_decomposed);
    assert_nfkc(quarter_note, expected_decomposed);
}

#[test]
fn musical_eighth_note_multi_step_decomposition() {
    // U+1D160 (MUSICAL SYMBOL EIGHTH NOTE) decomposes to
    // U+1D15F (QUARTER NOTE) + U+1D16E (COMBINING FLAG-1),
    // and U+1D15F itself decomposes to U+1D158 + U+1D165.
    // Full decomposition: U+1D158 + U+1D165 + U+1D16E.
    let eighth_note = "\u{1D160}";
    let expected_decomposed = "\u{1D158}\u{1D165}\u{1D16E}";

    assert_nfd(eighth_note, expected_decomposed);
    assert_nfkd(eighth_note, expected_decomposed);
    // Composition exclusions: NFC/NFKC produce the same decomposed form.
    assert_nfc(eighth_note, expected_decomposed);
    assert_nfkc(eighth_note, expected_decomposed);
}

#[test]
fn musical_symbols_is_normalized_checks() {
    // The composed form (U+1D15E) is NOT normalized in any form because it
    // has a canonical decomposition.
    let half_note = "\u{1D15E}";
    assert!(!half_note.is_nfd(), "U+1D15E should not be NFD");
    assert!(!half_note.is_nfc(), "U+1D15E should not be NFC");
    assert!(!half_note.is_nfkd(), "U+1D15E should not be NFKD");
    assert!(!half_note.is_nfkc(), "U+1D15E should not be NFKC");
    assert_is_normalized_matches_icu(half_note);

    // The decomposed form IS normalized in all forms (composition exclusion).
    let decomposed = "\u{1D157}\u{1D165}";
    assert!(decomposed.is_nfd(), "decomposed half note should be NFD");
    assert!(
        decomposed.is_nfc(),
        "decomposed half note should be NFC (composition exclusion)"
    );
    assert!(decomposed.is_nfkd(), "decomposed half note should be NFKD");
    assert!(decomposed.is_nfkc(), "decomposed half note should be NFKC");
    assert_is_normalized_matches_icu(decomposed);
}

#[test]
fn musical_symbols_nfc_nfkc_agreement() {
    // For musical symbols with canonical (not compatibility) decompositions,
    // NFC and NFKC should agree, and NFD and NFKD should agree.
    let inputs = ["\u{1D15E}", "\u{1D15F}", "\u{1D160}"];
    for input in &inputs {
        let nfc_result = input.nfc();
        let nfkc_result = input.nfkc();
        assert_eq!(
            &*nfc_result, &*nfkc_result,
            "NFC and NFKC should agree for musical symbol {:?}",
            input
        );

        let nfd_result = input.nfd();
        let nfkd_result = input.nfkd();
        assert_eq!(
            &*nfd_result, &*nfkd_result,
            "NFD and NFKD should agree for musical symbol {:?}",
            input
        );
    }
}

#[test]
fn musical_symbol_with_surrounding_text() {
    // Musical symbol embedded in ASCII text.
    let input = "Music: \u{1D15E} is a half note";
    assert_all_forms_match_icu(input);

    // Multiple musical symbols in sequence.
    let input2 = "\u{1D15E}\u{1D15F}\u{1D160}";
    assert_all_forms_match_icu(input2);
}

// ===========================================================================
// 2. CJK Compatibility Ideographs Supplement (U+2F800-U+2FA1F)
// ===========================================================================

#[test]
fn cjk_compat_supplement_canonical_decomposition() {
    // These have canonical decompositions to CJK Unified Ideographs.
    // All four normalization forms should produce the unified ideograph.
    let cases: &[(&str, &str)] = &[
        ("\u{2F800}", "\u{4E3D}"), // CJK COMPATIBILITY IDEOGRAPH-2F800 -> U+4E3D
        ("\u{2F801}", "\u{4E38}"), // CJK COMPATIBILITY IDEOGRAPH-2F801 -> U+4E38
        ("\u{2F802}", "\u{4E41}"), // CJK COMPATIBILITY IDEOGRAPH-2F802 -> U+4E41
        ("\u{2F804}", "\u{4F60}"), // CJK COMPATIBILITY IDEOGRAPH-2F804 -> U+4F60
        ("\u{2F80A}", "\u{50E7}"), // CJK COMPATIBILITY IDEOGRAPH-2F80A -> U+50E7
    ];

    for &(input, expected) in cases {
        assert_nfd(input, expected);
        assert_nfc(input, expected);
        assert_nfkd(input, expected);
        assert_nfkc(input, expected);
    }
}

#[test]
fn cjk_compat_supplement_is_not_normalized() {
    // The compatibility ideograph forms are not normalized in any form.
    let compat_chars = [
        "\u{2F800}",
        "\u{2F801}",
        "\u{2F802}",
        "\u{2F804}",
        "\u{2F80A}",
    ];
    for input in &compat_chars {
        assert!(!input.is_nfd(), "{:?} should not be NFD", input);
        assert!(!input.is_nfc(), "{:?} should not be NFC", input);
        assert!(!input.is_nfkd(), "{:?} should not be NFKD", input);
        assert!(!input.is_nfkc(), "{:?} should not be NFKC", input);
        assert_is_normalized_matches_icu(input);
    }
}

#[test]
fn cjk_compat_supplement_unified_form_is_normalized() {
    // The unified ideograph targets should be normalized in all forms.
    let unified_chars = ["\u{4E3D}", "\u{4E38}", "\u{4E41}", "\u{4F60}", "\u{50E7}"];
    for input in &unified_chars {
        assert!(input.is_nfd(), "{:?} should be NFD", input);
        assert!(input.is_nfc(), "{:?} should be NFC", input);
        assert!(input.is_nfkd(), "{:?} should be NFKD", input);
        assert!(input.is_nfkc(), "{:?} should be NFKC", input);
        assert_is_normalized_matches_icu(input);
    }
}

#[test]
fn cjk_compat_supplement_in_text() {
    // CJK compat ideographs embedded in mixed text.
    let input = "CJK: \u{2F800}\u{2F801} test";
    let expected_nfd = "CJK: \u{4E3D}\u{4E38} test";
    assert_nfd(input, expected_nfd);
    assert_nfc(input, expected_nfd);
    assert_all_forms_match_icu(input);
}

// ===========================================================================
// 3. Mathematical Alphanumeric Symbols (U+1D400-U+1D7FF)
// ===========================================================================

#[test]
fn math_bold_nfkc_decomposition() {
    // Mathematical Bold letters have compatibility decompositions to basic Latin.
    // NFC/NFD leave them unchanged; NFKC/NFKD decompose them.
    let cases: &[(&str, &str, &str)] = &[
        ("\u{1D400}", "A", "MATH BOLD CAPITAL A"),
        ("\u{1D41A}", "a", "MATH BOLD SMALL A"),
        ("\u{1D401}", "B", "MATH BOLD CAPITAL B"),
        ("\u{1D41B}", "b", "MATH BOLD SMALL B"),
    ];

    for &(input, expected_compat, label) in cases {
        // NFC/NFD: unchanged (no canonical decomposition).
        assert_nfc(input, input);
        assert_nfd(input, input);
        // NFKC/NFKD: compatibility decomposition to basic Latin.
        assert_nfkc(input, expected_compat);
        assert_nfkd(input, expected_compat);
        // Verify is_normalized checks.
        assert!(input.is_nfc(), "{} should be NFC", label);
        assert!(input.is_nfd(), "{} should be NFD", label);
        assert!(!input.is_nfkc(), "{} should NOT be NFKC", label);
        assert!(!input.is_nfkd(), "{} should NOT be NFKD", label);
        assert_is_normalized_matches_icu(input);
    }
}

#[test]
fn math_italic_nfkc_decomposition() {
    let cases: &[(&str, &str)] = &[
        ("\u{1D434}", "A"), // MATHEMATICAL ITALIC CAPITAL A
        ("\u{1D44E}", "a"), // MATHEMATICAL ITALIC SMALL A
    ];
    for &(input, expected) in cases {
        assert_nfc(input, input);
        assert_nfd(input, input);
        assert_nfkc(input, expected);
        assert_nfkd(input, expected);
    }
}

#[test]
fn math_bold_italic_nfkc_decomposition() {
    let cases: &[(&str, &str)] = &[
        ("\u{1D468}", "A"), // MATHEMATICAL BOLD ITALIC CAPITAL A
        ("\u{1D482}", "a"), // MATHEMATICAL BOLD ITALIC SMALL A
    ];
    for &(input, expected) in cases {
        assert_nfc(input, input);
        assert_nfd(input, input);
        assert_nfkc(input, expected);
        assert_nfkd(input, expected);
    }
}

#[test]
fn math_script_nfkc_decomposition() {
    // MATHEMATICAL SCRIPT CAPITAL A -> A
    assert_nfkc("\u{1D49C}", "A");
    assert_nfkd("\u{1D49C}", "A");
    assert_nfc("\u{1D49C}", "\u{1D49C}");
    assert_nfd("\u{1D49C}", "\u{1D49C}");
}

#[test]
fn math_fraktur_nfkc_decomposition() {
    // MATHEMATICAL FRAKTUR CAPITAL A -> A
    assert_nfkc("\u{1D504}", "A");
    assert_nfkd("\u{1D504}", "A");
    assert_nfc("\u{1D504}", "\u{1D504}");
    assert_nfd("\u{1D504}", "\u{1D504}");
}

#[test]
fn math_doublestruck_nfkc_decomposition() {
    // MATHEMATICAL DOUBLE-STRUCK CAPITAL A -> A
    assert_nfkc("\u{1D538}", "A");
    assert_nfkd("\u{1D538}", "A");
    assert_nfc("\u{1D538}", "\u{1D538}");
    assert_nfd("\u{1D538}", "\u{1D538}");
}

#[test]
fn math_bold_digits_nfkc_decomposition() {
    // MATHEMATICAL BOLD DIGIT ZERO -> 0
    assert_nfkc("\u{1D7CE}", "0");
    assert_nfkd("\u{1D7CE}", "0");
    assert_nfc("\u{1D7CE}", "\u{1D7CE}");
    assert_nfd("\u{1D7CE}", "\u{1D7CE}");

    // MATHEMATICAL BOLD DIGIT NINE -> 9
    assert_nfkc("\u{1D7D7}", "9");
    assert_nfkd("\u{1D7D7}", "9");
}

#[test]
fn math_symbols_string_nfkc() {
    // A string of mixed math styles should all decompose under NFKC/NFKD.
    // Bold A + Italic a + Script A + Fraktur A
    let input = "\u{1D400}\u{1D44E}\u{1D49C}\u{1D504}";
    assert_nfkc(input, "AaAA");
    assert_nfkd(input, "AaAA");
    // NFC/NFD: unchanged.
    assert_nfc(input, input);
    assert_nfd(input, input);
    assert_all_forms_match_icu(input);
}

// ===========================================================================
// 4. Supplementary character case folding
// ===========================================================================

#[test]
fn math_bold_capital_a_casefold_via_nfkc() {
    // U+1D400 (MATH BOLD CAPITAL A) has no direct case fold mapping,
    // but NFKC maps it to "A", which then casefolds to "a".
    let nfkc_result = "\u{1D400}".nfkc();
    assert_eq!(&*nfkc_result, "A");
    let folded = casefold(&nfkc_result, CaseFoldMode::Standard);
    assert_eq!(&*folded, "a");
}

#[test]
fn math_bold_small_a_casefold_via_nfkc() {
    // U+1D41A (MATH BOLD SMALL A) -> NFKC -> "a" -> casefold -> "a" (unchanged).
    let nfkc_result = "\u{1D41A}".nfkc();
    assert_eq!(&*nfkc_result, "a");
    let folded = casefold(&nfkc_result, CaseFoldMode::Standard);
    assert_eq!(&*folded, "a");
}

#[test]
fn deseret_capital_casefold() {
    // Deseret letters (U+10400-U+1044F): capitals fold to lowercase.
    // U+10400 (DESERET CAPITAL LONG I) -> U+10428 (DESERET SMALL LONG I)
    let capital = "\u{10400}";
    let expected_lower = "\u{10428}";
    let folded = casefold(capital, CaseFoldMode::Standard);
    assert_eq!(
        &*folded, expected_lower,
        "Deseret capital U+10400 should casefold to U+10428"
    );
}

#[test]
fn deseret_lowercase_casefold_unchanged() {
    // U+10428 (DESERET SMALL LONG I) should be unchanged by casefold.
    let lower = "\u{10428}";
    let folded = casefold(lower, CaseFoldMode::Standard);
    assert_eq!(
        &*folded, lower,
        "Deseret lowercase U+10428 should be unchanged by casefold"
    );
}

#[test]
fn deseret_range_casefold() {
    // Test several Deseret capital-to-lowercase pairs.
    let pairs: &[(char, char)] = &[
        ('\u{10400}', '\u{10428}'), // LONG I
        ('\u{10401}', '\u{10429}'), // LONG E
        ('\u{10402}', '\u{1042A}'), // LONG A
        ('\u{10410}', '\u{10438}'), // BEE
        ('\u{1041F}', '\u{10447}'), // EW
    ];
    for &(upper, lower) in pairs {
        let upper_str: String = core::iter::once(upper).collect();
        let folded = casefold(&upper_str, CaseFoldMode::Standard);
        let expected: String = core::iter::once(lower).collect();
        assert_eq!(
            &*folded, &expected,
            "Deseret U+{:04X} should casefold to U+{:04X}",
            upper as u32, lower as u32
        );
    }
}

#[test]
fn deseret_normalization_unchanged() {
    // Deseret letters have no decomposition mappings; all forms leave them unchanged.
    let capital = "\u{10400}";
    let lower = "\u{10428}";
    for input in &[capital, lower] {
        assert_nfc(input, input);
        assert_nfd(input, input);
        assert_nfkc(input, input);
        assert_nfkd(input, input);
        assert_is_normalized_matches_icu(input);
    }
}

#[test]
fn matching_pipeline_math_bold() {
    // The matching pipeline (NFKC + casefold + skeleton) should equate
    // Math Bold A and plain "a".
    let opts = MatchingOptions::default();
    let bold_a = normalize_for_matching("\u{1D400}", &opts);
    let plain_a = normalize_for_matching("a", &opts);
    assert_eq!(
        bold_a, plain_a,
        "Math Bold A should match 'a' through the matching pipeline"
    );
}

#[test]
fn matching_pipeline_math_styles_equivalent() {
    // All mathematical styles of "A" should match plain "a" through the pipeline.
    let opts = MatchingOptions::default();
    let reference = normalize_for_matching("a", &opts);
    let math_capitals = [
        "\u{1D400}", // Bold A
        "\u{1D434}", // Italic A
        "\u{1D468}", // Bold Italic A
        "\u{1D49C}", // Script A
        "\u{1D504}", // Fraktur A
        "\u{1D538}", // Double-struck A
    ];
    for input in &math_capitals {
        let result = normalize_for_matching(input, &opts);
        assert_eq!(
            result, reference,
            "Math symbol {:?} should match 'a' through matching pipeline",
            input
        );
    }
}

#[test]
fn matching_pipeline_deseret() {
    // Deseret capital and lowercase should match through the matching pipeline.
    let opts = MatchingOptions::default();
    let upper = normalize_for_matching("\u{10400}", &opts);
    let lower = normalize_for_matching("\u{10428}", &opts);
    assert_eq!(
        upper, lower,
        "Deseret U+10400 and U+10428 should match through matching pipeline"
    );
}

// ===========================================================================
// 5. 4-byte UTF-8 supplementary chars at every SIMD chunk offset position
// ===========================================================================

#[test]
fn musical_symbol_at_every_offset_in_16byte_window() {
    // Place a musical symbol (U+1D15E, which decomposes) at offsets 0..=15
    // within a 16-byte window. The 4-byte UTF-8 encoding tests the SIMD
    // scanner's ability to detect non-ASCII bytes and fall back to scalar
    // processing at various alignments.
    for offset in 0..=15 {
        let prefix = ascii_pad(offset);
        let suffix_len = 15usize.saturating_sub(offset);
        let suffix = ascii_pad(suffix_len);
        let input = format!("{}\u{1D15E}{}", prefix, suffix);

        assert_all_forms_match_icu(&input);
    }
}

#[test]
fn cjk_compat_at_every_offset_in_16byte_window() {
    // U+2F800 (CJK compat, decomposes to U+4E3D) at various offsets.
    for offset in 0..=15 {
        let prefix = ascii_pad(offset);
        let suffix_len = 15usize.saturating_sub(offset);
        let suffix = ascii_pad(suffix_len);
        let input = format!("{}\u{2F800}{}", prefix, suffix);

        assert_all_forms_match_icu(&input);
    }
}

#[test]
fn math_bold_at_every_offset_in_16byte_window() {
    // U+1D400 (MATH BOLD A, compatibility decomposition) at various offsets.
    for offset in 0..=15 {
        let prefix = ascii_pad(offset);
        let suffix_len = 15usize.saturating_sub(offset);
        let suffix = ascii_pad(suffix_len);
        let input = format!("{}\u{1D400}{}", prefix, suffix);

        assert_all_forms_match_icu(&input);
    }
}

#[test]
fn supplementary_char_at_64byte_chunk_boundary_offsets() {
    // Test a decomposing supplementary char at various positions relative to
    // the 64-byte SIMD chunk boundary.
    for ascii_prefix_len in 56..=68 {
        let prefix = ascii_pad(ascii_prefix_len);
        let input = format!("{}\u{1D15E}", prefix);
        assert_all_forms_match_icu(&input);
    }
}

#[test]
fn supplementary_char_at_128byte_chunk_boundary_offsets() {
    // Same test at the 128-byte boundary (two-chunk boundary).
    for ascii_prefix_len in 120..=132 {
        let prefix = ascii_pad(ascii_prefix_len);
        let input = format!("{}\u{1D15E}", prefix);
        assert_all_forms_match_icu(&input);
    }
}

#[test]
fn multiple_supplementary_chars_across_chunk() {
    // Multiple 4-byte chars in sequence near a chunk boundary.
    for start_offset in 56..=64 {
        let prefix = ascii_pad(start_offset);
        // Three musical symbols in sequence (12 bytes of 4-byte chars).
        let input = format!("{}\u{1D15E}\u{1D15F}\u{1D160}", prefix);
        assert_all_forms_match_icu(&input);
    }
}

#[test]
fn supplementary_char_interleaved_with_ascii_near_boundary() {
    // Supplementary chars interleaved with ASCII near the chunk boundary.
    for offset in 58..=66 {
        let prefix = ascii_pad(offset);
        let input = format!("{}X\u{1D15E}Y\u{2F800}Z", prefix);
        assert_all_forms_match_icu(&input);
    }
}

// ===========================================================================
// 6. ICU4X cross-validation (comprehensive)
// ===========================================================================

#[test]
fn cross_validate_supplementary_normalization_roundtrips() {
    // For each supplementary test character, verify the full roundtrip:
    // input -> NFD -> NFC should equal input -> NFC
    // input -> NFKD -> NFKC should equal input -> NFKC
    let test_chars = [
        "\u{1D15E}", // Musical half note
        "\u{1D15F}", // Musical quarter note
        "\u{1D160}", // Musical eighth note
        "\u{2F800}", // CJK compat
        "\u{2F801}", // CJK compat
        "\u{1D400}", // Math bold A
        "\u{1D41A}", // Math bold a
        "\u{10400}", // Deseret capital
        "\u{10428}", // Deseret small
        "\u{1F600}", // Emoji (no decomposition, stable)
    ];

    for input in &test_chars {
        // NFD -> NFC roundtrip
        let nfd_result = input.nfd();
        let nfd_then_nfc = nfd_result.nfc();
        let direct_nfc = input.nfc();
        assert_eq!(
            &*nfd_then_nfc, &*direct_nfc,
            "NFD->NFC roundtrip mismatch for {:?}",
            input
        );
        // Cross-validate with ICU4X
        let icu_nfd_then_nfc = icu_nfc(&icu_nfd(input));
        assert_eq!(
            &*nfd_then_nfc, &*icu_nfd_then_nfc,
            "NFD->NFC roundtrip ICU4X mismatch for {:?}",
            input
        );

        // NFKD -> NFKC roundtrip
        let nfkd_result = input.nfkd();
        let nfkd_then_nfkc = nfkd_result.nfkc();
        let direct_nfkc = input.nfkc();
        assert_eq!(
            &*nfkd_then_nfkc, &*direct_nfkc,
            "NFKD->NFKC roundtrip mismatch for {:?}",
            input
        );
        let icu_nfkd_then_nfkc = icu_nfkc(&icu_nfkd(input));
        assert_eq!(
            &*nfkd_then_nfkc, &*icu_nfkd_then_nfkc,
            "NFKD->NFKC roundtrip ICU4X mismatch for {:?}",
            input
        );
    }
}

#[test]
fn cross_validate_supplementary_idempotence() {
    // Normalization should be idempotent: normalizing the output again
    // should return the same string.
    let test_chars = [
        "\u{1D15E}",
        "\u{1D15F}",
        "\u{1D160}",
        "\u{2F800}",
        "\u{2F801}",
        "\u{2F804}",
        "\u{1D400}",
        "\u{1D41A}",
        "\u{1D504}",
        "\u{10400}",
        "\u{10428}",
    ];

    for input in &test_chars {
        let nfc1 = input.nfc();
        let nfc2 = nfc1.nfc();
        assert_eq!(&*nfc1, &*nfc2, "NFC not idempotent for {:?}", input);

        let nfd1 = input.nfd();
        let nfd2 = nfd1.nfd();
        assert_eq!(&*nfd1, &*nfd2, "NFD not idempotent for {:?}", input);

        let nfkc1 = input.nfkc();
        let nfkc2 = nfkc1.nfkc();
        assert_eq!(&*nfkc1, &*nfkc2, "NFKC not idempotent for {:?}", input);

        let nfkd1 = input.nfkd();
        let nfkd2 = nfkd1.nfkd();
        assert_eq!(&*nfkd1, &*nfkd2, "NFKD not idempotent for {:?}", input);
    }
}

#[test]
fn cross_validate_nfc_subset_nfkc() {
    // For supplementary chars: NFKC(x) == NFKC(NFC(x))
    // i.e., NFC does not interfere with further NFKC normalization.
    let test_inputs = [
        "\u{1D15E}",
        "\u{2F800}",
        "\u{1D400}",
        "\u{10400}",
        "hello \u{1D15E} world",
        "\u{1D400}\u{1D41A}\u{1D504}",
    ];

    for input in &test_inputs {
        let nfkc_direct = input.nfkc();
        let nfc_result = input.nfc();
        let nfc_then_nfkc = nfc_result.nfkc();
        assert_eq!(
            &*nfkc_direct, &*nfc_then_nfkc,
            "NFKC(x) != NFKC(NFC(x)) for {:?}",
            input
        );

        // Cross-validate
        let icu_nfkc_direct = icu_nfkc(input);
        assert_eq!(
            &*nfkc_direct, &*icu_nfkc_direct,
            "NFKC ICU4X mismatch for {:?}",
            input
        );
    }
}

// ===========================================================================
// proptest: supplementary plane fuzz testing
// ===========================================================================

#[cfg(test)]
mod proptest_supplementary {
    use super::*;
    use proptest::prelude::*;

    /// Strategy that generates strings containing supplementary plane characters
    /// mixed with ASCII, to stress-test SIMD boundary handling.
    fn supplementary_string_strategy() -> impl Strategy<Value = String> {
        // Mix of: ASCII, musical symbols, CJK compat, math alphanumeric, Deseret, emoji
        let char_strategy = prop_oneof![
            4 => (0x20u32..=0x7Eu32).prop_map(|c| char::from_u32(c).unwrap()),
            1 => prop::sample::select(vec![
                '\u{1D15E}', '\u{1D15F}', '\u{1D160}',     // Musical
                '\u{2F800}', '\u{2F801}', '\u{2F804}',      // CJK compat
                '\u{1D400}', '\u{1D41A}', '\u{1D434}',      // Math bold/italic
                '\u{1D504}', '\u{1D538}', '\u{1D7CE}',      // Math fraktur/doublestruck/digit
                '\u{10400}', '\u{10428}',                     // Deseret
                '\u{1F600}', '\u{1F60A}',                     // Emoji (stable)
            ]),
        ];

        proptest::collection::vec(char_strategy, 1..=64)
            .prop_map(|chars| chars.into_iter().collect::<String>())
    }

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(500))]

        #[test]
        fn supplementary_nfc_matches_icu4x(input in supplementary_string_strategy()) {
            let simd_result = input.nfc();
            let icu_result = icu_nfc(&input);
            prop_assert_eq!(&*simd_result, &*icu_result, "NFC mismatch");
        }

        #[test]
        fn supplementary_nfd_matches_icu4x(input in supplementary_string_strategy()) {
            let simd_result = input.nfd();
            let icu_result = icu_nfd(&input);
            prop_assert_eq!(&*simd_result, &*icu_result, "NFD mismatch");
        }

        #[test]
        fn supplementary_nfkc_matches_icu4x(input in supplementary_string_strategy()) {
            let simd_result = input.nfkc();
            let icu_result = icu_nfkc(&input);
            prop_assert_eq!(&*simd_result, &*icu_result, "NFKC mismatch");
        }

        #[test]
        fn supplementary_nfkd_matches_icu4x(input in supplementary_string_strategy()) {
            let simd_result = input.nfkd();
            let icu_result = icu_nfkd(&input);
            prop_assert_eq!(&*simd_result, &*icu_result, "NFKD mismatch");
        }

        #[test]
        fn supplementary_idempotent(input in supplementary_string_strategy()) {
            let nfc1 = input.nfc();
            let nfc2 = nfc1.nfc();
            prop_assert_eq!(&*nfc1, &*nfc2, "NFC not idempotent");

            let nfd1 = input.nfd();
            let nfd2 = nfd1.nfd();
            prop_assert_eq!(&*nfd1, &*nfd2, "NFD not idempotent");

            let nfkc1 = input.nfkc();
            let nfkc2 = nfkc1.nfkc();
            prop_assert_eq!(&*nfkc1, &*nfkc2, "NFKC not idempotent");

            let nfkd1 = input.nfkd();
            let nfkd2 = nfkd1.nfkd();
            prop_assert_eq!(&*nfkd1, &*nfkd2, "NFKD not idempotent");
        }
    }
}