-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathunicode_test.rs
More file actions
769 lines (665 loc) · 21.9 KB
/
unicode_test.rs
File metadata and controls
769 lines (665 loc) · 21.9 KB
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
use super::*;
// ---------------------------------------------------------------
// decode_utf8 tests
// ---------------------------------------------------------------
#[test]
fn decode_utf8_empty_slice() {
let (ch, len) = decode_utf8(&[]);
assert_eq!(ch, '\0');
assert_eq!(len, 0);
}
#[test]
fn decode_utf8_ascii_a() {
let (ch, len) = decode_utf8(b"A");
assert_eq!(ch, 'A');
assert_eq!(len, 1);
}
#[test]
fn decode_utf8_ascii_nul() {
let (ch, len) = decode_utf8(&[0x00]);
assert_eq!(ch, '\0');
assert_eq!(len, 1);
}
#[test]
fn decode_utf8_ascii_del() {
// 0x7F is the highest single-byte value (DEL)
let (ch, len) = decode_utf8(&[0x7F]);
assert_eq!(ch, '\x7F');
assert_eq!(len, 1);
}
#[test]
fn decode_utf8_two_byte_latin() {
// U+00E9 LATIN SMALL LETTER E WITH ACUTE = 0xC3 0xA9
let bytes = "é".as_bytes();
let (ch, len) = decode_utf8(bytes);
assert_eq!(ch, 'é');
assert_eq!(len, 2);
}
#[test]
fn decode_utf8_two_byte_boundary() {
// U+0080 is the first 2-byte code point = 0xC2 0x80
let bytes = "\u{0080}".as_bytes();
let (ch, len) = decode_utf8(bytes);
assert_eq!(ch, '\u{0080}');
assert_eq!(len, 2);
}
#[test]
fn decode_utf8_three_byte_cjk() {
// U+4E2D (中) = 0xE4 0xB8 0xAD
let bytes = "中".as_bytes();
let (ch, len) = decode_utf8(bytes);
assert_eq!(ch, '中');
assert_eq!(len, 3);
}
#[test]
fn decode_utf8_three_byte_euro_sign() {
// U+20AC EURO SIGN = 0xE2 0x82 0xAC
let bytes = "€".as_bytes();
let (ch, len) = decode_utf8(bytes);
assert_eq!(ch, '€');
assert_eq!(len, 3);
}
#[test]
fn decode_utf8_four_byte_emoji() {
// U+1F600 GRINNING FACE = 0xF0 0x9F 0x98 0x80
let bytes = "\u{1F600}".as_bytes();
let (ch, len) = decode_utf8(bytes);
assert_eq!(ch, '\u{1F600}');
assert_eq!(len, 4);
}
#[test]
fn decode_utf8_four_byte_max_codepoint() {
// U+10FFFF is the maximum Unicode code point
let bytes = "\u{10FFFF}".as_bytes();
let (ch, len) = decode_utf8(bytes);
assert_eq!(ch, '\u{10FFFF}');
assert_eq!(len, 4);
}
#[test]
fn decode_utf8_invalid_continuation_byte() {
// 0x80 is a bare continuation byte — invalid as a start byte
let (ch, len) = decode_utf8(&[0x80]);
assert_eq!(ch, '\u{FFFD}');
assert_eq!(len, 1);
}
#[test]
fn decode_utf8_invalid_continuation_0x_bf() {
let (ch, len) = decode_utf8(&[0xBF]);
assert_eq!(ch, '\u{FFFD}');
assert_eq!(len, 1);
}
#[test]
fn decode_utf8_truncated_two_byte() {
// 0xC3 expects a second byte but slice is too short
let (ch, len) = decode_utf8(&[0xC3]);
assert_eq!(ch, '\u{FFFD}');
assert_eq!(len, 1);
}
#[test]
fn decode_utf8_truncated_three_byte() {
// 0xE4 expects two more bytes but only one provided
let (ch, len) = decode_utf8(&[0xE4, 0xB8]);
assert_eq!(ch, '\u{FFFD}');
assert_eq!(len, 1);
}
#[test]
fn decode_utf8_truncated_four_byte() {
// 0xF0 expects three more bytes but only two provided
let (ch, len) = decode_utf8(&[0xF0, 0x9F, 0x98]);
assert_eq!(ch, '\u{FFFD}');
assert_eq!(len, 1);
}
#[test]
fn decode_utf8_consumes_only_first_char() {
// "AB" — should decode only 'A' and report 1 byte consumed
let (ch, len) = decode_utf8(b"AB");
assert_eq!(ch, 'A');
assert_eq!(len, 1);
}
#[test]
fn decode_utf8_multibyte_followed_by_ascii() {
// "中A" — should decode only '中' (3 bytes)
let bytes = "中A".as_bytes();
let (ch, len) = decode_utf8(bytes);
assert_eq!(ch, '中');
assert_eq!(len, 3);
}
// ---------------------------------------------------------------
// is_wide_char tests
// ---------------------------------------------------------------
#[test]
fn wide_char_ascii_not_wide() {
assert!(!is_wide_char('A'));
assert!(!is_wide_char(' '));
assert!(!is_wide_char('~'));
}
#[test]
fn wide_char_cjk_unified() {
// U+4E2D 中
assert!(is_wide_char('中'));
// U+9FFF last in CJK Unified Ideographs block
assert!(is_wide_char('\u{9FFF}'));
// U+4E00 first in CJK Unified Ideographs block
assert!(is_wide_char('\u{4E00}'));
}
#[test]
fn wide_char_cjk_extension_a() {
assert!(is_wide_char('\u{3400}'));
assert!(is_wide_char('\u{4DBF}'));
}
#[test]
fn wide_char_cjk_extension_b() {
assert!(is_wide_char('\u{20000}'));
assert!(is_wide_char('\u{2A6DF}'));
}
#[test]
fn wide_char_fullwidth_forms() {
// U+FF01 FULLWIDTH EXCLAMATION MARK
assert!(is_wide_char('\u{FF01}'));
// U+FF5A FULLWIDTH LATIN SMALL LETTER Z
assert!(is_wide_char('\u{FF5A}'));
// U+FFE0 FULLWIDTH CENT SIGN
assert!(is_wide_char('\u{FFE0}'));
// U+FFE6 FULLWIDTH WON SIGN
assert!(is_wide_char('\u{FFE6}'));
}
#[test]
fn wide_char_hangul_syllable() {
// U+AC00 first Hangul syllable (가)
assert!(is_wide_char('\u{AC00}'));
// U+D7AF last Hangul syllable
assert!(is_wide_char('\u{D7AF}'));
}
#[test]
fn wide_char_hiragana() {
// U+3042 HIRAGANA LETTER A (あ)
assert!(is_wide_char('\u{3042}'));
}
#[test]
fn wide_char_katakana() {
// U+30A2 KATAKANA LETTER A (ア)
assert!(is_wide_char('\u{30A2}'));
}
#[test]
fn wide_char_cjk_symbols_and_punctuation() {
// U+3000 IDEOGRAPHIC SPACE
assert!(is_wide_char('\u{3000}'));
// U+3001 IDEOGRAPHIC COMMA
assert!(is_wide_char('\u{3001}'));
}
#[test]
fn wide_char_cjk_radicals() {
assert!(is_wide_char('\u{2E80}'));
assert!(is_wide_char('\u{2FDF}'));
}
#[test]
fn wide_char_emoji_is_wide() {
// U+1F600 GRINNING FACE
assert!(is_wide_char('\u{1F600}'));
// U+1F4A9 PILE OF POO
assert!(is_wide_char('\u{1F4A9}'));
}
#[test]
fn wide_char_latin_extended_not_wide() {
// U+00E9 LATIN SMALL LETTER E WITH ACUTE
assert!(!is_wide_char('é'));
// U+00FC LATIN SMALL LETTER U WITH DIAERESIS
assert!(!is_wide_char('ü'));
}
#[test]
fn wide_char_cjk_compat_ideographs() {
assert!(is_wide_char('\u{F900}'));
assert!(is_wide_char('\u{FAFF}'));
}
#[test]
fn wide_char_katakana_phonetic_extensions() {
assert!(is_wide_char('\u{31F0}'));
assert!(is_wide_char('\u{31FF}'));
}
// ---------------------------------------------------------------
// is_emoji_presentation tests
// ---------------------------------------------------------------
#[test]
fn emoji_presentation_emoticons() {
assert!(is_emoji_presentation(0x1F600)); // grinning face
assert!(is_emoji_presentation(0x1F64F)); // last emoticon
}
#[test]
fn emoji_presentation_misc_symbols_pictographs() {
assert!(is_emoji_presentation(0x1F300)); // cyclone
assert!(is_emoji_presentation(0x1F5FF)); // moyai
}
#[test]
fn emoji_presentation_transport_map() {
assert!(is_emoji_presentation(0x1F680)); // rocket
assert!(is_emoji_presentation(0x1F6FF));
}
#[test]
fn emoji_presentation_supplemental_symbols() {
assert!(is_emoji_presentation(0x1F900));
assert!(is_emoji_presentation(0x1F9FF));
}
#[test]
fn emoji_presentation_extended_a() {
assert!(is_emoji_presentation(0x1FA00));
assert!(is_emoji_presentation(0x1FA6F));
assert!(is_emoji_presentation(0x1FA70));
assert!(is_emoji_presentation(0x1FAFF));
}
#[test]
fn emoji_presentation_dingbats() {
assert!(is_emoji_presentation(0x2702)); // black scissors
assert!(is_emoji_presentation(0x27B0)); // curly loop
}
#[test]
fn emoji_presentation_regional_indicators() {
assert!(is_emoji_presentation(0x1F1E0));
assert!(is_emoji_presentation(0x1F1FF));
}
#[test]
fn emoji_presentation_special_cards() {
assert!(is_emoji_presentation(0x1F004)); // mahjong red dragon
assert!(is_emoji_presentation(0x1F0CF)); // playing card black joker
}
#[test]
fn emoji_presentation_non_emoji_ascii() {
assert!(!is_emoji_presentation(0x41)); // 'A'
assert!(!is_emoji_presentation(0x20)); // space
}
#[test]
fn emoji_presentation_non_emoji_cjk() {
assert!(!is_emoji_presentation(0x4E2D)); // 中
}
// ---------------------------------------------------------------
// is_cluster_extender tests
// ---------------------------------------------------------------
#[test]
fn cluster_extender_combining_diacritical() {
// U+0300 COMBINING GRAVE ACCENT
assert!(is_cluster_extender('\u{0300}'));
// U+036F last in basic combining diacriticals
assert!(is_cluster_extender('\u{036F}'));
}
#[test]
fn cluster_extender_combining_diacritical_extended() {
assert!(is_cluster_extender('\u{1AB0}'));
assert!(is_cluster_extender('\u{1AFF}'));
}
#[test]
fn cluster_extender_combining_diacritical_supplement() {
assert!(is_cluster_extender('\u{1DC0}'));
assert!(is_cluster_extender('\u{1DFF}'));
}
#[test]
fn cluster_extender_variation_selectors() {
// U+FE00..U+FE0F are variation selectors
assert!(is_cluster_extender('\u{FE00}'));
assert!(is_cluster_extender('\u{FE0F}')); // VS16 (emoji presentation)
}
#[test]
fn cluster_extender_skin_tone_modifiers() {
assert!(is_cluster_extender('\u{1F3FB}')); // light skin
assert!(is_cluster_extender('\u{1F3FF}')); // dark skin
}
#[test]
fn cluster_extender_zero_width_joiner() {
assert!(is_cluster_extender('\u{200D}')); // ZWJ
}
#[test]
fn cluster_extender_zero_width_chars() {
assert!(is_cluster_extender('\u{200B}')); // ZWSP
assert!(is_cluster_extender('\u{200C}')); // ZWNJ
assert!(is_cluster_extender('\u{200E}')); // LRM
assert!(is_cluster_extender('\u{200F}')); // RLM
assert!(is_cluster_extender('\u{FEFF}')); // BOM
}
#[test]
fn cluster_extender_combining_enclosing_keycap() {
assert!(is_cluster_extender('\u{20E3}'));
}
#[test]
fn cluster_extender_hebrew_marks() {
assert!(is_cluster_extender('\u{0591}'));
assert!(is_cluster_extender('\u{05BD}'));
assert!(is_cluster_extender('\u{05BF}'));
assert!(is_cluster_extender('\u{05C1}'));
assert!(is_cluster_extender('\u{05C7}'));
}
#[test]
fn cluster_extender_arabic_marks() {
assert!(is_cluster_extender('\u{0610}'));
assert!(is_cluster_extender('\u{064B}'));
assert!(is_cluster_extender('\u{0670}'));
}
#[test]
fn cluster_extender_devanagari_marks() {
assert!(is_cluster_extender('\u{0901}'));
assert!(is_cluster_extender('\u{093A}'));
}
#[test]
fn cluster_extender_thai_marks() {
assert!(is_cluster_extender('\u{0E31}'));
assert!(is_cluster_extender('\u{0E34}'));
assert!(is_cluster_extender('\u{0E47}'));
}
#[test]
fn cluster_extender_emoji_tag_chars() {
assert!(is_cluster_extender('\u{E0020}'));
assert!(is_cluster_extender('\u{E007F}')); // CANCEL TAG
}
#[test]
fn cluster_extender_supplementary_variation_selectors() {
assert!(is_cluster_extender('\u{E0100}'));
assert!(is_cluster_extender('\u{E01EF}'));
}
#[test]
fn cluster_extender_ascii_not_extender() {
assert!(!is_cluster_extender('A'));
assert!(!is_cluster_extender(' '));
assert!(!is_cluster_extender('0'));
}
#[test]
fn cluster_extender_cjk_not_extender() {
assert!(!is_cluster_extender('中'));
}
// ---------------------------------------------------------------
// is_regional_indicator tests
// ---------------------------------------------------------------
#[test]
fn regional_indicator_valid() {
// U+1F1E6 Regional Indicator Symbol Letter A
assert!(is_regional_indicator(0x1F1E6));
// U+1F1FF Regional Indicator Symbol Letter Z
assert!(is_regional_indicator(0x1F1FF));
// U+1F1FA Regional Indicator Symbol Letter U
assert!(is_regional_indicator(0x1F1FA));
}
#[test]
fn regional_indicator_invalid_before_range() {
assert!(!is_regional_indicator(0x1F1E5));
}
#[test]
fn regional_indicator_invalid_after_range() {
assert!(!is_regional_indicator(0x1F200));
}
#[test]
fn regional_indicator_ascii() {
assert!(!is_regional_indicator(0x41)); // 'A'
}
// ---------------------------------------------------------------
// collect_grapheme_cluster tests
// ---------------------------------------------------------------
#[test]
fn cluster_single_char_no_extenders() {
let (cluster, extra_bytes, extra_chars) = collect_grapheme_cluster('A', b"BC");
assert!(cluster.is_none());
assert_eq!(extra_bytes, 0);
assert_eq!(extra_chars, 0);
}
#[test]
fn cluster_single_char_empty_remaining() {
let (cluster, extra_bytes, extra_chars) = collect_grapheme_cluster('A', &[]);
assert!(cluster.is_none());
assert_eq!(extra_bytes, 0);
assert_eq!(extra_chars, 0);
}
#[test]
fn cluster_combining_acute_accent() {
// 'e' followed by U+0301 COMBINING ACUTE ACCENT (0xCC 0x81)
let remaining = "\u{0301}".as_bytes();
let (cluster, extra_bytes, extra_chars) = collect_grapheme_cluster('e', remaining);
assert_eq!(cluster, Some("e\u{0301}".to_string()));
assert_eq!(extra_bytes, 2);
assert_eq!(extra_chars, 1);
}
#[test]
fn cluster_multiple_combining_marks() {
// 'a' + U+0300 (combining grave) + U+0301 (combining acute)
let remaining_str = "\u{0300}\u{0301}";
let remaining = remaining_str.as_bytes();
let (cluster, extra_bytes, extra_chars) = collect_grapheme_cluster('a', remaining);
assert_eq!(cluster, Some("a\u{0300}\u{0301}".to_string()));
assert_eq!(extra_bytes, 4); // 2 bytes for each combining mark
assert_eq!(extra_chars, 2);
}
#[test]
fn cluster_emoji_with_skin_tone() {
// U+1F44D THUMBS UP followed by U+1F3FD MEDIUM SKIN TONE
let remaining = "\u{1F3FD}".as_bytes();
let (cluster, extra_bytes, extra_chars) = collect_grapheme_cluster('\u{1F44D}', remaining);
assert_eq!(cluster, Some("\u{1F44D}\u{1F3FD}".to_string()));
assert_eq!(extra_bytes, 4); // skin tone is a 4-byte character
assert_eq!(extra_chars, 1);
}
#[test]
fn cluster_zwj_sequence_two_emoji() {
// Heart + ZWJ + Fire = "heart on fire" emoji
// U+2764 (heart) + U+200D (ZWJ) + U+1F525 (fire)
let remaining_str = "\u{200D}\u{1F525}";
let remaining = remaining_str.as_bytes();
let (cluster, extra_bytes, extra_chars) = collect_grapheme_cluster('\u{2764}', remaining);
assert_eq!(cluster, Some("\u{2764}\u{200D}\u{1F525}".to_string()));
// ZWJ is 3 bytes, fire emoji is 4 bytes
assert_eq!(extra_bytes, 7);
assert_eq!(extra_chars, 2);
}
#[test]
fn cluster_variation_selector() {
// U+2764 HEAVY BLACK HEART + U+FE0F VARIATION SELECTOR-16
let remaining = "\u{FE0F}".as_bytes();
let (cluster, extra_bytes, extra_chars) = collect_grapheme_cluster('\u{2764}', remaining);
assert_eq!(cluster, Some("\u{2764}\u{FE0F}".to_string()));
assert_eq!(extra_bytes, 3); // FE0F is 3 bytes in UTF-8
assert_eq!(extra_chars, 1);
}
#[test]
fn cluster_regional_indicator_flag_pair() {
// U+1F1FA (Regional Indicator U) + U+1F1F8 (Regional Indicator S) = US flag
let ri_u = '\u{1F1FA}';
let remaining = "\u{1F1F8}".as_bytes();
let (cluster, extra_bytes, extra_chars) = collect_grapheme_cluster(ri_u, remaining);
assert_eq!(cluster, Some("\u{1F1FA}\u{1F1F8}".to_string()));
assert_eq!(extra_bytes, 4); // each RI is 4 bytes
assert_eq!(extra_chars, 1);
}
#[test]
fn cluster_regional_indicator_stops_at_two() {
// Three RIs: should only consume two (one pair), then stop
let ri_u = '\u{1F1FA}';
let remaining_str = "\u{1F1F8}\u{1F1E6}";
let remaining = remaining_str.as_bytes();
let (cluster, extra_bytes, extra_chars) = collect_grapheme_cluster(ri_u, remaining);
assert_eq!(cluster, Some("\u{1F1FA}\u{1F1F8}".to_string()));
assert_eq!(extra_bytes, 4); // only one extra RI consumed
assert_eq!(extra_chars, 1);
}
#[test]
fn cluster_zwj_at_end_of_remaining() {
// ZWJ at end of buffer with nothing after it
let remaining = "\u{200D}".as_bytes();
let (cluster, extra_bytes, extra_chars) = collect_grapheme_cluster('\u{1F468}', remaining);
// ZWJ is consumed but no char after it
assert_eq!(cluster, Some("\u{1F468}\u{200D}".to_string()));
assert_eq!(extra_bytes, 3); // ZWJ is 3 bytes
assert_eq!(extra_chars, 1);
}
#[test]
fn cluster_skin_tone_then_ascii() {
// Emoji + skin tone + ASCII — should stop at ASCII
let remaining_str = "\u{1F3FB}A";
let remaining = remaining_str.as_bytes();
let (cluster, extra_bytes, extra_chars) = collect_grapheme_cluster('\u{1F44D}', remaining);
assert_eq!(cluster, Some("\u{1F44D}\u{1F3FB}".to_string()));
assert_eq!(extra_bytes, 4);
assert_eq!(extra_chars, 1);
}
#[test]
fn cluster_non_ri_base_with_ri_in_remaining() {
// Non-RI base followed by RI should NOT absorb the RI
let remaining = "\u{1F1FA}".as_bytes();
let (cluster, extra_bytes, extra_chars) = collect_grapheme_cluster('A', remaining);
assert!(cluster.is_none());
assert_eq!(extra_bytes, 0);
assert_eq!(extra_chars, 0);
}
#[test]
fn cluster_zwj_emoji_sequence_family() {
// Man + ZWJ + Woman + ZWJ + Boy
// U+1F468 + U+200D + U+1F469 + U+200D + U+1F466
let remaining_str = "\u{200D}\u{1F469}\u{200D}\u{1F466}";
let remaining = remaining_str.as_bytes();
let (cluster, extra_bytes, extra_chars) = collect_grapheme_cluster('\u{1F468}', remaining);
assert_eq!(
cluster,
Some("\u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F466}".to_string())
);
// ZWJ=3 + woman=4 + ZWJ=3 + boy=4 = 14
assert_eq!(extra_bytes, 14);
assert_eq!(extra_chars, 4);
}
#[test]
fn cluster_combining_on_cjk_base() {
// CJK character + combining mark
let remaining = "\u{0300}".as_bytes();
let (cluster, extra_bytes, extra_chars) = collect_grapheme_cluster('中', remaining);
assert_eq!(cluster, Some("中\u{0300}".to_string()));
assert_eq!(extra_bytes, 2);
assert_eq!(extra_chars, 1);
}
#[test]
fn cluster_keycap_sequence() {
// '3' + U+FE0F (VS16) + U+20E3 (combining enclosing keycap)
let remaining_str = "\u{FE0F}\u{20E3}";
let remaining = remaining_str.as_bytes();
let (cluster, extra_bytes, extra_chars) = collect_grapheme_cluster('3', remaining);
assert_eq!(cluster, Some("3\u{FE0F}\u{20E3}".to_string()));
// FE0F = 3 bytes, 20E3 = 3 bytes
assert_eq!(extra_bytes, 6);
assert_eq!(extra_chars, 2);
}
// ---------------------------------------------------------------
// is_potentially_glyphless tests
// ---------------------------------------------------------------
#[test]
fn glyphless_c1_control_chars() {
assert!(is_potentially_glyphless('\u{0080}'));
assert!(is_potentially_glyphless('\u{009F}'));
assert!(is_potentially_glyphless('\u{0085}')); // NEL
}
#[test]
fn glyphless_soft_hyphen() {
assert!(is_potentially_glyphless('\u{00AD}'));
}
#[test]
fn glyphless_zero_width_chars() {
assert!(is_potentially_glyphless('\u{200B}')); // ZWSP
assert!(is_potentially_glyphless('\u{200C}')); // ZWNJ
assert!(is_potentially_glyphless('\u{200D}')); // ZWJ
assert!(is_potentially_glyphless('\u{200E}')); // LRM
assert!(is_potentially_glyphless('\u{200F}')); // RLM
}
#[test]
fn glyphless_bidi_embedding() {
assert!(is_potentially_glyphless('\u{202A}')); // LRE
assert!(is_potentially_glyphless('\u{202E}')); // RLO
}
#[test]
fn glyphless_word_joiner_and_invisible_separators() {
assert!(is_potentially_glyphless('\u{2060}')); // word joiner
assert!(is_potentially_glyphless('\u{2069}'));
}
#[test]
fn glyphless_line_paragraph_separator() {
assert!(is_potentially_glyphless('\u{2028}')); // line separator
assert!(is_potentially_glyphless('\u{2029}')); // paragraph separator
}
#[test]
fn glyphless_bom() {
assert!(is_potentially_glyphless('\u{FEFF}'));
}
#[test]
fn glyphless_specials_block() {
assert!(is_potentially_glyphless('\u{FFF0}'));
assert!(is_potentially_glyphless('\u{FFFD}')); // replacement char
}
#[test]
fn glyphless_tags_block() {
assert!(is_potentially_glyphless('\u{E0000}'));
assert!(is_potentially_glyphless('\u{E007F}')); // CANCEL TAG
}
#[test]
fn glyphless_ascii_not_glyphless() {
// Normal ASCII characters should not be glyphless
assert!(!is_potentially_glyphless('A'));
assert!(!is_potentially_glyphless(' '));
assert!(!is_potentially_glyphless('0'));
assert!(!is_potentially_glyphless('\n'));
}
#[test]
fn glyphless_c0_control_not_glyphless() {
// C0 controls (0x00-0x1F) are NOT in the glyphless ranges
// (they are handled differently by Emacs)
assert!(!is_potentially_glyphless('\u{0001}')); // SOH
assert!(!is_potentially_glyphless('\u{001F}')); // US
}
#[test]
fn glyphless_normal_latin_not_glyphless() {
assert!(!is_potentially_glyphless('é'));
assert!(!is_potentially_glyphless('ü'));
}
#[test]
fn glyphless_cjk_not_glyphless() {
assert!(!is_potentially_glyphless('中'));
}
#[test]
fn glyphless_emoji_not_glyphless() {
assert!(!is_potentially_glyphless('\u{1F600}'));
}
#[test]
fn glyphless_just_outside_c1_range() {
// U+007F (DEL) is NOT in the C1 range
assert!(!is_potentially_glyphless('\u{007F}'));
// U+00A0 (NBSP) is NOT glyphless
assert!(!is_potentially_glyphless('\u{00A0}'));
}
// ---------------------------------------------------------------
// Integration / round-trip tests
// ---------------------------------------------------------------
#[test]
fn decode_utf8_sequential_walk() {
// Decode an entire multi-byte string character by character
let input = "Aé中\u{1F600}";
let bytes = input.as_bytes();
let mut pos = 0;
let mut chars = Vec::new();
while pos < bytes.len() {
let (ch, len) = decode_utf8(&bytes[pos..]);
assert!(len > 0, "must advance at least 1 byte");
chars.push(ch);
pos += len;
}
assert_eq!(chars, vec!['A', 'é', '中', '\u{1F600}']);
}
#[test]
fn wide_and_emoji_mutual_coverage() {
// Every emoji presentation char should also be wide via is_wide_char
let emoji_cps: Vec<u32> = vec![0x1F600, 0x1F4A9, 0x1F680, 0x1F1E6, 0x1F004, 0x1F0CF, 0x2702];
for cp in emoji_cps {
if let Some(ch) = char::from_u32(cp) {
assert!(is_wide_char(ch), "emoji 0x{:X} should be wide", cp);
}
}
}
#[test]
fn cluster_extender_and_glyphless_overlap() {
// Some chars are both cluster extenders and potentially glyphless
// ZWJ (U+200D) is both
assert!(is_cluster_extender('\u{200D}'));
assert!(is_potentially_glyphless('\u{200D}'));
// ZWSP (U+200B) is both
assert!(is_cluster_extender('\u{200B}'));
assert!(is_potentially_glyphless('\u{200B}'));
// BOM (U+FEFF) is both
assert!(is_cluster_extender('\u{FEFF}'));
assert!(is_potentially_glyphless('\u{FEFF}'));
}