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
// tests/unicode_norm_compat.rs
//! Compatibility tests ported from the `unicode-normalization` crate.
//!
//! These tests validate that `simd-normalizer` produces the same results as
//! `unicode-normalization` for specific test vectors, CJK compatibility
//! ideographs, quick-check cross-form consistency, and a full differential
//! pass over the official conformance data.
//!
//! This file does NOT duplicate:
//! - conformance.rs: UAX#15 20-invariant normalization tests
//! - exhaustive.rs: per-codepoint differential tests vs icu_normalizer
//! - properties.rs: proptest property-based tests

mod data;
use data::normalization_tests::NORMALIZATION_TESTS;
use simd_normalizer::UnicodeNormalization;

// ============================================================================
// Section 1: Specific test vectors from unicode-normalization src/test.rs
// ============================================================================

#[test]
fn test_nfd_vectors() {
    let cases: &[(&str, &str)] = &[
        ("abc", "abc"),
        ("\u{1e0b}\u{1c4}", "d\u{307}\u{1c4}"),
        ("\u{2026}", "\u{2026}"),
        ("\u{2126}", "\u{3a9}"),
        ("\u{1e0b}\u{323}", "d\u{323}\u{307}"),
        ("\u{1e0d}\u{307}", "d\u{323}\u{307}"),
        ("a\u{301}", "a\u{301}"),
        ("\u{301}a", "\u{301}a"),
        ("\u{d4db}", "\u{1111}\u{1171}\u{11b6}"),
        ("\u{ac1c}", "\u{1100}\u{1162}"),
    ];

    for (input, expected) in cases {
        let result = input.nfd();
        assert_eq!(
            &*result, *expected,
            "NFD({:?}): got {:?}, expected {:?}",
            input, &*result, expected
        );
    }
}

#[test]
fn test_nfkd_vectors() {
    let cases: &[(&str, &str)] = &[
        ("abc", "abc"),
        ("\u{1e0b}\u{1c4}", "d\u{307}DZ\u{30c}"),
        ("\u{2026}", "..."),
        ("\u{2126}", "\u{3a9}"),
        ("\u{1e0b}\u{323}", "d\u{323}\u{307}"),
        ("\u{1e0d}\u{307}", "d\u{323}\u{307}"),
        ("a\u{301}", "a\u{301}"),
        ("\u{301}a", "\u{301}a"),
        ("\u{d4db}", "\u{1111}\u{1171}\u{11b6}"),
        ("\u{ac1c}", "\u{1100}\u{1162}"),
    ];

    for (input, expected) in cases {
        let result = input.nfkd();
        assert_eq!(
            &*result, *expected,
            "NFKD({:?}): got {:?}, expected {:?}",
            input, &*result, expected
        );
    }
}

#[test]
fn test_nfc_vectors() {
    let cases: &[(&str, &str)] = &[
        ("abc", "abc"),
        ("\u{1e0b}\u{1c4}", "\u{1e0b}\u{1c4}"),
        ("\u{2026}", "\u{2026}"),
        ("\u{2126}", "\u{3a9}"),
        ("\u{1e0b}\u{323}", "\u{1e0d}\u{307}"),
        ("\u{1e0d}\u{307}", "\u{1e0d}\u{307}"),
        ("a\u{301}", "\u{e1}"),
        ("\u{301}a", "\u{301}a"),
        ("\u{d4db}", "\u{d4db}"),
        ("\u{ac1c}", "\u{ac1c}"),
        (
            "a\u{300}\u{305}\u{315}\u{5ae}b",
            "\u{e0}\u{5ae}\u{305}\u{315}b",
        ),
    ];

    for (input, expected) in cases {
        let result = input.nfc();
        assert_eq!(
            &*result, *expected,
            "NFC({:?}): got {:?}, expected {:?}",
            input, &*result, expected
        );
    }
}

#[test]
fn test_nfkc_vectors() {
    let cases: &[(&str, &str)] = &[
        ("abc", "abc"),
        ("\u{1e0b}\u{1c4}", "\u{1e0b}D\u{17d}"),
        ("\u{2026}", "..."),
        ("\u{2126}", "\u{3a9}"),
        ("\u{1e0b}\u{323}", "\u{1e0d}\u{307}"),
        ("\u{1e0d}\u{307}", "\u{1e0d}\u{307}"),
        ("a\u{301}", "\u{e1}"),
        ("\u{301}a", "\u{301}a"),
        ("\u{d4db}", "\u{d4db}"),
        ("\u{ac1c}", "\u{ac1c}"),
        (
            "a\u{300}\u{305}\u{315}\u{5ae}b",
            "\u{e0}\u{5ae}\u{305}\u{315}b",
        ),
    ];

    for (input, expected) in cases {
        let result = input.nfkc();
        assert_eq!(
            &*result, *expected,
            "NFKC({:?}): got {:?}, expected {:?}",
            input, &*result, expected
        );
    }
}

// ============================================================================
// Section 2: CJK Compatibility Ideograph Decomposition
// ============================================================================

#[test]
fn test_cjk_compat_decomposition() {
    // U+2F999 (CJK compat) -> U+831D (singleton canonical decomposition)
    // U+2F8A6 (CJK compat) -> U+6148 (singleton canonical decomposition)
    let s = "\u{2f999}\u{2f8a6}";
    let expected = "\u{831d}\u{6148}";

    assert_eq!(&*s.nfd(), expected, "NFD of CJK compat ideographs");
    assert_eq!(&*s.nfkd(), expected, "NFKD of CJK compat ideographs");
    assert_eq!(&*s.nfc(), expected, "NFC of CJK compat ideographs");
    assert_eq!(&*s.nfkc(), expected, "NFKC of CJK compat ideographs");
}

// ============================================================================
// Section 3: Quick-Check Cross-Form Consistency
//
// Ported from unicode-normalization tests/tests.rs test_quick_check.
// This tests relationships BETWEEN normalization forms that are not covered
// by conformance.rs (which only checks is_X(X_column)==true).
// ============================================================================

#[test]
fn test_quick_check_cross_form_consistency() {
    let mut failures = Vec::new();

    for (i, t) in NORMALIZATION_TESTS.iter().enumerate() {
        // Basic: normalized forms must pass their own quick-check.
        // (These are also in conformance.rs, but included here for completeness
        // of the cross-form logic below which depends on them.)
        if !t.nfc.is_nfc() {
            failures.push(format!(
                "case {i}: is_nfc(nfc) should be true, nfc={:?}",
                t.nfc
            ));
        }
        if !t.nfd.is_nfd() {
            failures.push(format!(
                "case {i}: is_nfd(nfd) should be true, nfd={:?}",
                t.nfd
            ));
        }
        if !t.nfkc.is_nfkc() {
            failures.push(format!(
                "case {i}: is_nfkc(nfkc) should be true, nfkc={:?}",
                t.nfkc
            ));
        }
        if !t.nfkd.is_nfkd() {
            failures.push(format!(
                "case {i}: is_nfkd(nfkd) should be true, nfkd={:?}",
                t.nfkd
            ));
        }

        // Cross-form: if NFC != NFD, then NFD should not be NFC and vice versa.
        if t.nfc != t.nfd {
            if t.nfd.is_nfc() {
                failures.push(format!(
                    "case {i}: nfc!=nfd but is_nfc(nfd) is true; nfc={:?} nfd={:?}",
                    t.nfc, t.nfd
                ));
            }
            if t.nfc.is_nfd() {
                failures.push(format!(
                    "case {i}: nfc!=nfd but is_nfd(nfc) is true; nfc={:?} nfd={:?}",
                    t.nfc, t.nfd
                ));
            }
        }

        // Cross-form: if NFKC != NFC, then NFC is not NFKC, but NFKC is NFC.
        if t.nfkc != t.nfc {
            if t.nfc.is_nfkc() {
                failures.push(format!(
                    "case {i}: nfkc!=nfc but is_nfkc(nfc) is true; nfkc={:?} nfc={:?}",
                    t.nfkc, t.nfc
                ));
            }
            if !t.nfkc.is_nfc() {
                failures.push(format!(
                    "case {i}: nfkc!=nfc but is_nfc(nfkc) is false; nfkc={:?} nfc={:?}",
                    t.nfkc, t.nfc
                ));
            }
        }

        // Cross-form: if NFKD != NFD, then NFD is not NFKD, but NFKD is NFD.
        if t.nfkd != t.nfd {
            if t.nfd.is_nfkd() {
                failures.push(format!(
                    "case {i}: nfkd!=nfd but is_nfkd(nfd) is true; nfkd={:?} nfd={:?}",
                    t.nfkd, t.nfd
                ));
            }
            if !t.nfkd.is_nfd() {
                failures.push(format!(
                    "case {i}: nfkd!=nfd but is_nfd(nfkd) is false; nfkd={:?} nfd={:?}",
                    t.nfkd, t.nfd
                ));
            }
        }

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

    assert!(
        failures.is_empty(),
        "Quick-check cross-form consistency failures ({} failures):\n{}",
        failures.len(),
        failures.join("\n")
    );
}

// ============================================================================
// Section 4: Differential Validation Against unicode-normalization
//
// Cross-validates simd-normalizer against the unicode-normalization crate
// (a second reference implementation, separate from icu_normalizer used in
// exhaustive.rs). Uses the trait-based API with iterators from
// unicode-normalization.
// ============================================================================

/// Helper: normalize via unicode-normalization crate (iterator-based API).
mod un_reference {
    use unicode_normalization::UnicodeNormalization as UNUnicodeNormalization;

    pub fn nfc(s: &str) -> String {
        s.nfc().collect()
    }

    pub fn nfd(s: &str) -> String {
        s.nfd().collect()
    }

    pub fn nfkc(s: &str) -> String {
        s.nfkc().collect()
    }

    pub fn nfkd(s: &str) -> String {
        s.nfkd().collect()
    }
}

#[test]
fn test_differential_vs_unicode_normalization() {
    let mut failures = Vec::new();

    for (i, t) in NORMALIZATION_TESTS.iter().enumerate() {
        // Test all four forms on all five columns (source, nfc, nfd, nfkc, nfkd)
        let columns: &[(&str, &str)] = &[
            ("source", t.source),
            ("nfc", t.nfc),
            ("nfd", t.nfd),
            ("nfkc", t.nfkc),
            ("nfkd", t.nfkd),
        ];

        for &(col_name, input) in columns {
            // NFC
            let ours = input.nfc();
            let theirs = un_reference::nfc(input);
            if *ours != theirs {
                failures.push(format!(
                    "case {i} NFC({col_name}): simd={:?} un={:?} (input={:?})",
                    &*ours, theirs, input
                ));
            }

            // NFD
            let ours = input.nfd();
            let theirs = un_reference::nfd(input);
            if *ours != theirs {
                failures.push(format!(
                    "case {i} NFD({col_name}): simd={:?} un={:?} (input={:?})",
                    &*ours, theirs, input
                ));
            }

            // NFKC
            let ours = input.nfkc();
            let theirs = un_reference::nfkc(input);
            if *ours != theirs {
                failures.push(format!(
                    "case {i} NFKC({col_name}): simd={:?} un={:?} (input={:?})",
                    &*ours, theirs, input
                ));
            }

            // NFKD
            let ours = input.nfkd();
            let theirs = un_reference::nfkd(input);
            if *ours != theirs {
                failures.push(format!(
                    "case {i} NFKD({col_name}): simd={:?} un={:?} (input={:?})",
                    &*ours, theirs, input
                ));
            }
        }

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

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

/// Also cross-validate the specific test vectors from Section 1 against
/// unicode-normalization, ensuring both libraries agree on these cases.
#[test]
fn test_specific_vectors_differential() {
    // All inputs used in the Section 1 vectors
    let inputs: &[&str] = &[
        "abc",
        "\u{1e0b}\u{1c4}",
        "\u{2026}",
        "\u{2126}",
        "\u{1e0b}\u{323}",
        "\u{1e0d}\u{307}",
        "a\u{301}",
        "\u{301}a",
        "\u{d4db}",
        "\u{ac1c}",
        "a\u{300}\u{305}\u{315}\u{5ae}b",
        "\u{2f999}\u{2f8a6}", // CJK compat
    ];

    for input in inputs {
        // NFC
        let ours = input.nfc();
        let theirs = un_reference::nfc(input);
        assert_eq!(
            &*ours, &*theirs,
            "NFC({:?}): simd={:?} un={:?}",
            input, &*ours, theirs
        );

        // NFD
        let ours = input.nfd();
        let theirs = un_reference::nfd(input);
        assert_eq!(
            &*ours, &*theirs,
            "NFD({:?}): simd={:?} un={:?}",
            input, &*ours, theirs
        );

        // NFKC
        let ours = input.nfkc();
        let theirs = un_reference::nfkc(input);
        assert_eq!(
            &*ours, &*theirs,
            "NFKC({:?}): simd={:?} un={:?}",
            input, &*ours, theirs
        );

        // NFKD
        let ours = input.nfkd();
        let theirs = un_reference::nfkd(input);
        assert_eq!(
            &*ours, &*theirs,
            "NFKD({:?}): simd={:?} un={:?}",
            input, &*ours, theirs
        );
    }
}