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
// tests/exhaustive.rs
//! Exhaustive codepoint tests ported from the ICU4X test suite.
//!
//! These tests validate EVERY Unicode scalar value (0..=0x10FFFF, skipping
//! surrogates) for normalization invariance and correctness. They cover two
//! key gaps not addressed by the conformance tests:
//!
//! 1. **Unlisted-codepoint invariant** (UAX#15 requirement): every codepoint
//!    NOT listed in NormalizationTest.txt must be invariant under all four
//!    normalization forms (NFC, NFD, NFKC, NFKD).
//!
//! 2. **Differential validation against icu_normalizer**: for every single-
//!    character string, verify that simd_normalizer produces the same output
//!    as icu_normalizer for all four forms. This implicitly validates CCC
//!    (Canonical Combining Class) correctness since decomposition ordering
//!    depends on CCC values.
//!
//! The full-range tests are marked `#[ignore]` because they iterate ~1.1M
//! codepoints x 4 forms and take several minutes. Run them with:
//!
//!     cargo test --test exhaustive -- --ignored
//!
//! Non-ignored "spot check" variants test a representative subset (every
//! 100th codepoint plus key boundary ranges) and run in seconds.

mod data;
use data::normalization_tests::NORMALIZATION_TESTS;
use std::collections::HashSet;

// ---------------------------------------------------------------------------
// Helpers: simd_normalizer (our crate)
// ---------------------------------------------------------------------------

fn our_nfc(s: &str) -> String {
    simd_normalizer::nfc().normalize(s).into_owned()
}

fn our_nfd(s: &str) -> String {
    simd_normalizer::nfd().normalize(s).into_owned()
}

fn our_nfkc(s: &str) -> String {
    simd_normalizer::nfkc().normalize(s).into_owned()
}

fn our_nfkd(s: &str) -> String {
    simd_normalizer::nfkd().normalize(s).into_owned()
}

// ---------------------------------------------------------------------------
// Helpers: icu_normalizer (reference)
// ---------------------------------------------------------------------------

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

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

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

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

// ---------------------------------------------------------------------------
// Helpers: formatting
// ---------------------------------------------------------------------------

fn codepoints(s: &str) -> String {
    s.chars()
        .map(|c| format!("U+{:04X}", c as u32))
        .collect::<Vec<_>>()
        .join(" ")
}

// ---------------------------------------------------------------------------
// Helper: collect all codepoints listed in NormalizationTest.txt (Part 1)
//
// The official test data's Part 1 contains single-codepoint test cases.
// The first character of the `source` field is the codepoint under test.
// We collect ALL first-chars to build the "listed" set.
// ---------------------------------------------------------------------------

fn listed_codepoints() -> HashSet<u32> {
    let mut set = HashSet::with_capacity(NORMALIZATION_TESTS.len());
    for t in NORMALIZATION_TESTS.iter() {
        // Each Part 1 entry has a single codepoint as `source`.
        // Multi-codepoint entries (Parts 2-4) are also included; we extract
        // the first char. This is a superset of the Part 1 codepoints, which
        // is safe: we only SKIP invariance checks for listed codepoints, so
        // including extras means we check MORE, not fewer.
        if let Some(c) = t.source.chars().next() {
            set.insert(c as u32);
        }
    }
    set
}

/// Iterator over all valid Unicode scalar values, skipping surrogates.
fn all_scalar_values() -> impl Iterator<Item = char> {
    (0u32..=0x10FFFF).filter_map(char::from_u32)
}

/// Iterator over a sampled subset: every `step`-th codepoint plus boundary
/// ranges that are known to be interesting for normalization.
fn sampled_scalar_values(step: u32) -> impl Iterator<Item = char> {
    // Interesting boundary ranges to always include
    let boundary_ranges: Vec<std::ops::RangeInclusive<u32>> = vec![
        0x0000..=0x00FF,       // Basic Latin + Latin-1 Supplement
        0x0300..=0x036F,       // Combining Diacritical Marks
        0x0590..=0x05FF,       // Hebrew
        0x0600..=0x06FF,       // Arabic
        0x0900..=0x097F,       // Devanagari
        0x1100..=0x11FF,       // Hangul Jamo
        0x2000..=0x206F,       // General Punctuation
        0x2100..=0x214F,       // Letterlike Symbols
        0x2150..=0x218F,       // Number Forms
        0x2460..=0x24FF,       // Enclosed Alphanumerics
        0x3040..=0x30FF,       // Hiragana + Katakana
        0x3300..=0x33FF,       // CJK Compatibility
        0xAC00..=0xAC00 + 100, // Hangul Syllables (first 100)
        0xD7A0..=0xD7FF,       // Hangul Jamo Extended-B (near surrogates)
        0xF900..=0xFAFF,       // CJK Compatibility Ideographs
        0xFB00..=0xFB06,       // Latin ligatures
        0xFE00..=0xFE0F,       // Variation Selectors
        0xFF00..=0xFFEF,       // Halfwidth and Fullwidth Forms
        0x1D100..=0x1D1FF,     // Musical Symbols
        0x1F600..=0x1F64F,     // Emoticons
        0x10FF00..=0x10FFFF,   // Last valid range (plane 16 tail)
    ];

    let mut seen = HashSet::new();
    let mut result = Vec::new();

    // Add all boundary codepoints
    for range in boundary_ranges {
        for u in range {
            if let Some(c) = char::from_u32(u)
                && seen.insert(u)
            {
                result.push(c);
            }
        }
    }

    // Add every step-th codepoint
    let mut u = 0u32;
    while u <= 0x10FFFF {
        if let Some(c) = char::from_u32(u)
            && seen.insert(u)
        {
            result.push(c);
        }
        u += step;
    }

    result.into_iter()
}

// ===========================================================================
// Gap 1: Unlisted-Codepoint Invariant (from ICU4X `test_conformance`)
// ===========================================================================

/// Full exhaustive test: every codepoint NOT in NormalizationTest.txt must be
/// invariant under all four normalization forms.
///
/// Run with: `cargo test --test exhaustive -- --ignored unlisted_codepoint_invariant_full`
#[test]
#[ignore]
fn unlisted_codepoint_invariant_full() {
    let listed = listed_codepoints();
    let mut failures = Vec::new();

    for c in all_scalar_values() {
        if listed.contains(&(c as u32)) {
            continue;
        }

        let s: String = c.to_string();

        let nfc = our_nfc(&s);
        if nfc != s {
            failures.push(format!(
                "U+{:04X}: NFC({}) = {} (expected invariant)",
                c as u32,
                codepoints(&s),
                codepoints(&nfc)
            ));
        }

        let nfd = our_nfd(&s);
        if nfd != s {
            failures.push(format!(
                "U+{:04X}: NFD({}) = {} (expected invariant)",
                c as u32,
                codepoints(&s),
                codepoints(&nfd)
            ));
        }

        let nfkc = our_nfkc(&s);
        if nfkc != s {
            failures.push(format!(
                "U+{:04X}: NFKC({}) = {} (expected invariant)",
                c as u32,
                codepoints(&s),
                codepoints(&nfkc)
            ));
        }

        let nfkd = our_nfkd(&s);
        if nfkd != s {
            failures.push(format!(
                "U+{:04X}: NFKD({}) = {} (expected invariant)",
                c as u32,
                codepoints(&s),
                codepoints(&nfkd)
            ));
        }

        // Bail early if too many failures to avoid overwhelming output
        if failures.len() > 100 {
            failures.push("... (truncated after 100 failures)".to_string());
            break;
        }
    }

    assert!(
        failures.is_empty(),
        "Unlisted codepoint invariant violations ({} failures):\n{}",
        failures.len(),
        failures.join("\n")
    );
}

/// Spot-check: sampled subset of unlisted codepoints (every 100th + boundaries).
#[test]
fn unlisted_codepoint_invariant_spot_check() {
    let listed = listed_codepoints();
    let mut failures = Vec::new();

    for c in sampled_scalar_values(100) {
        if listed.contains(&(c as u32)) {
            continue;
        }

        let s: String = c.to_string();

        let nfc = our_nfc(&s);
        if nfc != s {
            failures.push(format!(
                "U+{:04X}: NFC({}) = {} (expected invariant)",
                c as u32,
                codepoints(&s),
                codepoints(&nfc)
            ));
        }

        let nfd = our_nfd(&s);
        if nfd != s {
            failures.push(format!(
                "U+{:04X}: NFD({}) = {} (expected invariant)",
                c as u32,
                codepoints(&s),
                codepoints(&nfd)
            ));
        }

        let nfkc = our_nfkc(&s);
        if nfkc != s {
            failures.push(format!(
                "U+{:04X}: NFKC({}) = {} (expected invariant)",
                c as u32,
                codepoints(&s),
                codepoints(&nfkc)
            ));
        }

        let nfkd = our_nfkd(&s);
        if nfkd != s {
            failures.push(format!(
                "U+{:04X}: NFKD({}) = {} (expected invariant)",
                c as u32,
                codepoints(&s),
                codepoints(&nfkd)
            ));
        }
    }

    assert!(
        failures.is_empty(),
        "Unlisted codepoint invariant violations ({} failures):\n{}",
        failures.len(),
        failures.join("\n")
    );
}

// ===========================================================================
// Gap 2: Differential test against icu_normalizer for ALL codepoints
// ===========================================================================

/// Full exhaustive differential: for every Unicode scalar value (as a single-
/// char string), compare NFC/NFD/NFKC/NFKD output of simd_normalizer against
/// icu_normalizer. This implicitly validates CCC correctness since
/// decomposition ordering depends on CCC values.
///
/// Run with: `cargo test --test exhaustive -- --ignored differential_all_codepoints_full`
#[test]
#[ignore]
fn differential_all_codepoints_full() {
    let mut failures = Vec::new();

    for c in all_scalar_values() {
        let s: String = c.to_string();

        // NFD
        let our = our_nfd(&s);
        let reference = icu_nfd(&s);
        if our != reference {
            failures.push(format!(
                "U+{:04X} NFD: ours=[{}] ref=[{}]",
                c as u32,
                codepoints(&our),
                codepoints(&reference)
            ));
        }

        // NFC
        let our = our_nfc(&s);
        let reference = icu_nfc(&s);
        if our != reference {
            failures.push(format!(
                "U+{:04X} NFC: ours=[{}] ref=[{}]",
                c as u32,
                codepoints(&our),
                codepoints(&reference)
            ));
        }

        // NFKD
        let our = our_nfkd(&s);
        let reference = icu_nfkd(&s);
        if our != reference {
            failures.push(format!(
                "U+{:04X} NFKD: ours=[{}] ref=[{}]",
                c as u32,
                codepoints(&our),
                codepoints(&reference)
            ));
        }

        // NFKC
        let our = our_nfkc(&s);
        let reference = icu_nfkc(&s);
        if our != reference {
            failures.push(format!(
                "U+{:04X} NFKC: ours=[{}] ref=[{}]",
                c as u32,
                codepoints(&our),
                codepoints(&reference)
            ));
        }

        if failures.len() > 100 {
            failures.push("... (truncated after 100 failures)".to_string());
            break;
        }
    }

    assert!(
        failures.is_empty(),
        "Differential failures against icu_normalizer ({} failures):\n{}",
        failures.len(),
        failures.join("\n")
    );
}

/// Spot-check differential: sampled subset (every 100th + boundaries).
#[test]
fn differential_all_codepoints_spot_check() {
    let mut failures = Vec::new();

    for c in sampled_scalar_values(100) {
        let s: String = c.to_string();

        // NFD
        let our = our_nfd(&s);
        let reference = icu_nfd(&s);
        if our != reference {
            failures.push(format!(
                "U+{:04X} NFD: ours=[{}] ref=[{}]",
                c as u32,
                codepoints(&our),
                codepoints(&reference)
            ));
        }

        // NFC
        let our = our_nfc(&s);
        let reference = icu_nfc(&s);
        if our != reference {
            failures.push(format!(
                "U+{:04X} NFC: ours=[{}] ref=[{}]",
                c as u32,
                codepoints(&our),
                codepoints(&reference)
            ));
        }

        // NFKD
        let our = our_nfkd(&s);
        let reference = icu_nfkd(&s);
        if our != reference {
            failures.push(format!(
                "U+{:04X} NFKD: ours=[{}] ref=[{}]",
                c as u32,
                codepoints(&our),
                codepoints(&reference)
            ));
        }

        // NFKC
        let our = our_nfkc(&s);
        let reference = icu_nfkc(&s);
        if our != reference {
            failures.push(format!(
                "U+{:04X} NFKC: ours=[{}] ref=[{}]",
                c as u32,
                codepoints(&our),
                codepoints(&reference)
            ));
        }
    }

    assert!(
        failures.is_empty(),
        "Differential failures against icu_normalizer ({} failures):\n{}",
        failures.len(),
        failures.join("\n")
    );
}

// ===========================================================================
// Gap 2b: CCC exhaustive validation via NFD differential
//
// Since simd_normalizer's CCC lookup is `pub(crate)`, we validate CCC
// indirectly: if NFD decomposition matches between simd_normalizer and
// icu_normalizer for every codepoint, then the CCC values must be correct
// (since NFD ordering depends entirely on CCC). This is the same approach
// used by the full differential test above, but isolated here for clarity
// and to match the ICU4X `test_ccc` pattern.
// ===========================================================================

/// Full exhaustive CCC validation: compare NFD output for every codepoint.
///
/// Run with: `cargo test --test exhaustive -- --ignored ccc_nfd_differential_full`
#[test]
#[ignore]
fn ccc_nfd_differential_full() {
    let mut failures = Vec::new();

    for c in all_scalar_values() {
        let s: String = c.to_string();
        let our = our_nfd(&s);
        let reference = icu_nfd(&s);

        if our != reference {
            failures.push(format!(
                "U+{:04X}: our NFD=[{}] icu NFD=[{}]",
                c as u32,
                codepoints(&our),
                codepoints(&reference)
            ));
        }

        if failures.len() > 100 {
            failures.push("... (truncated after 100 failures)".to_string());
            break;
        }
    }

    assert!(
        failures.is_empty(),
        "CCC/NFD differential failures ({} failures):\n{}",
        failures.len(),
        failures.join("\n")
    );
}

/// Spot-check CCC/NFD: sampled subset.
#[test]
fn ccc_nfd_differential_spot_check() {
    let mut failures = Vec::new();

    for c in sampled_scalar_values(100) {
        let s: String = c.to_string();
        let our = our_nfd(&s);
        let reference = icu_nfd(&s);

        if our != reference {
            failures.push(format!(
                "U+{:04X}: our NFD=[{}] icu NFD=[{}]",
                c as u32,
                codepoints(&our),
                codepoints(&reference)
            ));
        }
    }

    assert!(
        failures.is_empty(),
        "CCC/NFD differential failures ({} failures):\n{}",
        failures.len(),
        failures.join("\n")
    );
}

// ===========================================================================
// Bonus: is_normalized consistency for all codepoints
// ===========================================================================

/// Full exhaustive: for every codepoint, if normalizing produces the same
/// string, then is_normalized must return true. (The converse is not always
/// true due to quick-check MAYBE results.)
///
/// Run with: `cargo test --test exhaustive -- --ignored is_normalized_consistency_full`
#[test]
#[ignore]
fn is_normalized_consistency_full() {
    use simd_normalizer::UnicodeNormalization;
    let mut failures = Vec::new();

    for c in all_scalar_values() {
        let s: String = c.to_string();

        // NFC
        let normalized = our_nfc(&s);
        if normalized == s && !s.is_nfc() {
            failures.push(format!(
                "U+{:04X}: NFC is invariant but is_nfc()=false",
                c as u32
            ));
        }

        // NFD
        let normalized = our_nfd(&s);
        if normalized == s && !s.is_nfd() {
            failures.push(format!(
                "U+{:04X}: NFD is invariant but is_nfd()=false",
                c as u32
            ));
        }

        // NFKC
        let normalized = our_nfkc(&s);
        if normalized == s && !s.is_nfkc() {
            failures.push(format!(
                "U+{:04X}: NFKC is invariant but is_nfkc()=false",
                c as u32
            ));
        }

        // NFKD
        let normalized = our_nfkd(&s);
        if normalized == s && !s.is_nfkd() {
            failures.push(format!(
                "U+{:04X}: NFKD is invariant but is_nfkd()=false",
                c as u32
            ));
        }

        if failures.len() > 100 {
            failures.push("... (truncated after 100 failures)".to_string());
            break;
        }
    }

    assert!(
        failures.is_empty(),
        "is_normalized consistency failures ({} failures):\n{}",
        failures.len(),
        failures.join("\n")
    );
}

/// Spot-check is_normalized consistency.
#[test]
fn is_normalized_consistency_spot_check() {
    use simd_normalizer::UnicodeNormalization;
    let mut failures = Vec::new();

    for c in sampled_scalar_values(100) {
        let s: String = c.to_string();

        let normalized = our_nfc(&s);
        if normalized == s && !s.is_nfc() {
            failures.push(format!(
                "U+{:04X}: NFC is invariant but is_nfc()=false",
                c as u32
            ));
        }

        let normalized = our_nfd(&s);
        if normalized == s && !s.is_nfd() {
            failures.push(format!(
                "U+{:04X}: NFD is invariant but is_nfd()=false",
                c as u32
            ));
        }

        let normalized = our_nfkc(&s);
        if normalized == s && !s.is_nfkc() {
            failures.push(format!(
                "U+{:04X}: NFKC is invariant but is_nfkc()=false",
                c as u32
            ));
        }

        let normalized = our_nfkd(&s);
        if normalized == s && !s.is_nfkd() {
            failures.push(format!(
                "U+{:04X}: NFKD is invariant but is_nfkd()=false",
                c as u32
            ));
        }
    }

    assert!(
        failures.is_empty(),
        "is_normalized consistency failures ({} failures):\n{}",
        failures.len(),
        failures.join("\n")
    );
}