-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathRuneTests.cs
More file actions
1017 lines (884 loc) · 52.2 KB
/
Copy pathRuneTests.cs
File metadata and controls
1017 lines (884 loc) · 52.2 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
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
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Buffers;
using System.Collections.Generic;
using System.Globalization;
using System.Text.Unicode;
using Xunit;
using Xunit.Sdk;
namespace System.Text.Tests
{
public static partial class RuneTests
{
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsWindows), nameof(PlatformDetection.IsNlsGlobalization))] // the localization tables used by our test data only exist on Win8+
[PlatformSpecific(TestPlatforms.Windows)]
[InlineData('0', '0', '0', "en-US")]
[InlineData('a', 'A', 'a', "en-US")]
[InlineData('i', 'I', 'i', "en-US")]
[InlineData('i', '\u0130', 'i', "tr-TR")]
[InlineData('z', 'Z', 'z', "en-US")]
[InlineData('A', 'A', 'a', "en-US")]
[InlineData('I', 'I', 'i', "en-US")]
[InlineData('I', 'I', '\u0131', "tr-TR")]
[InlineData('Z', 'Z', 'z', "en-US")]
[InlineData('\u00DF', '\u00DF', '\u00DF', "de-DE")] // U+00DF LATIN SMALL LETTER SHARP S -- n.b. ToUpper doesn't create the majuscule form
[InlineData('\u0130', '\u0130', 'i', "tr-TR")] // U+0130 LATIN CAPITAL LETTER I WITH DOT ABOVE
[InlineData('\u0131', 'I', '\u0131', "tr-TR")] // U+0131 LATIN SMALL LETTER DOTLESS I
[InlineData('\u1E9E', '\u1E9E', '\u00DF', "de-DE")] // U+1E9E LATIN CAPITAL LETTER SHARP S
[InlineData(0x10400, 0x10400, 0x10428, "en-US")] // U+10400 DESERET CAPITAL LETTER LONG I
[InlineData(0x10428, 0x10400, 0x10428, "en-US")] // U+10428 DESERET SMALL LETTER LONG I
public static void Casing_CultureAware(int original, int upper, int lower, string culture)
{
var rune = new Rune(original);
var cultureInfo = CultureInfo.GetCultureInfo(culture);
Assert.Equal(new Rune(upper), Rune.ToUpper(rune, cultureInfo));
Assert.Equal(new Rune(lower), Rune.ToLower(rune, cultureInfo));
}
// Invariant ToUpper / ToLower doesn't modify Turkish I or majuscule Eszett
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsWindows), nameof(PlatformDetection.IsNlsGlobalization))] // the localization tables used by our test data only exist on Win8+
[PlatformSpecific(TestPlatforms.Windows)]
[InlineData('0', '0', '0')]
[InlineData('a', 'A', 'a')]
[InlineData('i', 'I', 'i')]
[InlineData('z', 'Z', 'z')]
[InlineData('A', 'A', 'a')]
[InlineData('I', 'I', 'i')]
[InlineData('Z', 'Z', 'z')]
[InlineData('\u00DF', '\u00DF', '\u00DF')] // U+00DF LATIN SMALL LETTER SHARP S
[InlineData('\u0130', '\u0130', '\u0130')] // U+0130 LATIN CAPITAL LETTER I WITH DOT ABOVE
[InlineData('\u0131', '\u0131', '\u0131')] // U+0131 LATIN SMALL LETTER DOTLESS I
[InlineData('\u1E9E', '\u1E9E', '\u1E9E')] // U+1E9E LATIN CAPITAL LETTER SHARP S
public static void Casing_Invariant(int original, int upper, int lower)
{
var rune = new Rune(original);
Assert.Equal(new Rune(upper), Rune.ToUpperInvariant(rune));
Assert.Equal(new Rune(lower), Rune.ToLowerInvariant(rune));
}
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsIcuGlobalization), nameof(PlatformDetection.IsNotHybridGlobalizationOnApplePlatform))]
// HybridGlobalization on Apple mobile platforms has issues with casing dotless I
[InlineData('0', '0', '0')]
[InlineData('a', 'A', 'a')]
[InlineData('i', 'I', 'i')]
[InlineData('z', 'Z', 'z')]
[InlineData('A', 'A', 'a')]
[InlineData('I', 'I', 'i')]
[InlineData('Z', 'Z', 'z')]
[InlineData('\u00DF', '\u00DF', '\u00DF')] // U+00DF LATIN SMALL LETTER SHARP S
[InlineData('\u0130', '\u0130', '\u0130')] // U+0130 LATIN CAPITAL LETTER I WITH DOT ABOVE
[InlineData('\u0131', '\u0131', '\u0131')] // U+0131 LATIN SMALL LETTER DOTLESS I
[InlineData(0x10400, 0x10400, 0x10428)] // U+10400 DESERET CAPITAL LETTER LONG I
[InlineData(0x10428, 0x10400, 0x10428)] // U+10428 DESERET SMALL LETTER LONG I
public static void ICU_Casing_Invariant(int original, int upper, int lower)
{
var rune = new Rune(original);
Assert.Equal(new Rune(upper), Rune.ToUpperInvariant(rune));
Assert.Equal(new Rune(lower), Rune.ToLowerInvariant(rune));
}
[Theory]
[MemberData(nameof(GeneralTestData_BmpCodePoints_NoSurrogates))]
public static void Ctor_Cast_Char_Valid(GeneralTestData testData)
{
Rune rune = new Rune(checked((char)testData.ScalarValue));
Rune runeFromCast = (Rune)(char)testData.ScalarValue;
Assert.Equal(rune, runeFromCast);
Assert.Equal(testData.ScalarValue, rune.Value);
Assert.Equal(testData.IsAscii, rune.IsAscii);
Assert.Equal(testData.IsBmp, rune.IsBmp);
Assert.Equal(testData.Plane, rune.Plane);
}
[Theory]
[MemberData(nameof(BmpCodePoints_SurrogatesOnly))]
public static void Ctor_Cast_Char_Invalid_Throws(char ch)
{
Assert.Throws<ArgumentOutOfRangeException>(nameof(ch), () => new Rune(ch));
Assert.Throws<ArgumentOutOfRangeException>(nameof(ch), () => (Rune)ch);
}
[Theory]
[MemberData(nameof(GeneralTestData_BmpCodePoints_NoSurrogates))]
[MemberData(nameof(GeneralTestData_SupplementaryCodePoints_ValidOnly))]
public static void Ctor_Cast_Int32_Valid(GeneralTestData testData)
{
Rune rune = new Rune((int)testData.ScalarValue);
Rune runeFromCast = (Rune)(int)testData.ScalarValue;
Assert.Equal(rune, runeFromCast);
Assert.Equal(testData.ScalarValue, rune.Value);
Assert.Equal(testData.IsAscii, rune.IsAscii);
Assert.Equal(testData.IsBmp, rune.IsBmp);
Assert.Equal(testData.Plane, rune.Plane);
}
[Theory]
[MemberData(nameof(BmpCodePoints_SurrogatesOnly))]
[MemberData(nameof(SupplementaryCodePoints_InvalidOnly))]
public static void Ctor_Cast_Int32_Invalid_Throws(int value)
{
Assert.Throws<ArgumentOutOfRangeException>(nameof(value), () => new Rune(value));
Assert.Throws<ArgumentOutOfRangeException>(nameof(value), () => (Rune)value);
}
[Theory]
[MemberData(nameof(GeneralTestData_BmpCodePoints_NoSurrogates))]
[MemberData(nameof(GeneralTestData_SupplementaryCodePoints_ValidOnly))]
public static void Ctor_Cast_UInt32_Valid(GeneralTestData testData)
{
Rune rune = new Rune((uint)testData.ScalarValue);
Rune runeFromCast = (Rune)(uint)testData.ScalarValue;
Assert.Equal(rune, runeFromCast);
Assert.Equal(testData.ScalarValue, rune.Value);
Assert.Equal(testData.IsAscii, rune.IsAscii);
Assert.Equal(testData.IsBmp, rune.IsBmp);
Assert.Equal(testData.Plane, rune.Plane);
}
[Theory]
[MemberData(nameof(BmpCodePoints_SurrogatesOnly))]
[MemberData(nameof(SupplementaryCodePoints_InvalidOnly))]
public static void Ctor_Cast_UInt32_Invalid_Throws(int value)
{
Assert.Throws<ArgumentOutOfRangeException>(nameof(value), () => new Rune((uint)value));
Assert.Throws<ArgumentOutOfRangeException>(nameof(value), () => (Rune)(uint)value);
}
[Theory]
[MemberData(nameof(SurrogatePairTestData_ValidOnly))]
public static void Ctor_SurrogatePair_Valid(char highSurrogate, char lowSurrogate, int expectedValue)
{
Assert.Equal(expectedValue, new Rune(highSurrogate, lowSurrogate).Value);
}
[Theory]
[MemberData(nameof(SurrogatePairTestData_InvalidOnly))]
public static void Ctor_SurrogatePair_Invalid(char highSurrogate, char lowSurrogate)
{
string expectedParamName = !char.IsHighSurrogate(highSurrogate) ? nameof(highSurrogate) : nameof(lowSurrogate);
Assert.Throws<ArgumentOutOfRangeException>(expectedParamName, () => new Rune(highSurrogate, lowSurrogate));
}
[Theory]
[InlineData('A', 'a', -1)]
[InlineData('A', 'A', 0)]
[InlineData('a', 'A', 1)]
[InlineData(0x10000, 0x10000, 0)]
[InlineData('\uFFFD', 0x10000, -1)]
[InlineData(0x10FFFF, 0x10000, 1)]
public static void CompareTo_And_ComparisonOperators(int first, int other, int expectedSign)
{
Rune a = new Rune(first);
Rune b = new Rune(other);
Assert.Equal(expectedSign, Math.Sign(a.CompareTo(b)));
Assert.Equal(expectedSign, Math.Sign(((IComparable)a).CompareTo(b)));
Assert.Equal(expectedSign < 0, a < b);
Assert.Equal(expectedSign <= 0, a <= b);
Assert.Equal(expectedSign > 0, a > b);
Assert.Equal(expectedSign >= 0, a >= b);
}
[Theory]
[InlineData(new char[0], OperationStatus.NeedMoreData, 0xFFFD, 0)] // empty buffer
[InlineData(new char[] { '\u1234' }, OperationStatus.Done, 0x1234, 1)] // BMP char
[InlineData(new char[] { '\u1234', '\ud800' }, OperationStatus.Done, 0x1234, 1)] // BMP char
[InlineData(new char[] { '\ud83d', '\ude32' }, OperationStatus.Done, 0x1F632, 2)] // supplementary value (U+1F632 ASTONISHED FACE)
[InlineData(new char[] { '\udc00' }, OperationStatus.InvalidData, 0xFFFD, 1)] // standalone low surrogate
[InlineData(new char[] { '\udc00', '\udc00' }, OperationStatus.InvalidData, 0xFFFD, 1)] // standalone low surrogate
[InlineData(new char[] { '\ud800' }, OperationStatus.NeedMoreData, 0xFFFD, 1)] // high surrogate at end of buffer
[InlineData(new char[] { '\ud800', '\ud800' }, OperationStatus.InvalidData, 0xFFFD, 1)] // standalone high surrogate
[InlineData(new char[] { '\ud800', '\u1234' }, OperationStatus.InvalidData, 0xFFFD, 1)] // standalone high surrogate
public static void DecodeFromUtf16(char[] data, OperationStatus expectedOperationStatus, int expectedRuneValue, int expectedCharsConsumed)
{
Assert.Equal(expectedOperationStatus, Rune.DecodeFromUtf16(data, out Rune actualRune, out int actualCharsConsumed));
Assert.Equal(expectedRuneValue, actualRune.Value);
Assert.Equal(expectedCharsConsumed, actualCharsConsumed);
string s = new string(data);
// Parse / TryParse succeed iff the entire input decodes to a single scalar.
if (expectedOperationStatus == OperationStatus.Done && expectedCharsConsumed == data.Length)
{
Assert.Equal(expectedRuneValue, ParsableHelper<Rune>.Parse(s, null).Value);
Assert.True(ParsableHelper<Rune>.TryParse(s, null, out Rune parsedFromString));
Assert.Equal(expectedRuneValue, parsedFromString.Value);
Assert.Equal(expectedRuneValue, SpanParsableHelper<Rune>.Parse(data, null).Value);
Assert.True(SpanParsableHelper<Rune>.TryParse(data, null, out Rune parsedFromSpan));
Assert.Equal(expectedRuneValue, parsedFromSpan.Value);
}
else
{
Assert.Throws<FormatException>(() => ParsableHelper<Rune>.Parse(s, null));
Assert.False(ParsableHelper<Rune>.TryParse(s, null, out Rune parsedFromString));
Assert.Equal(Rune.ReplacementChar, parsedFromString);
Assert.Throws<FormatException>(() => SpanParsableHelper<Rune>.Parse(data, null));
Assert.False(SpanParsableHelper<Rune>.TryParse(data, null, out Rune parsedFromSpan));
Assert.Equal(Rune.ReplacementChar, parsedFromSpan);
}
}
[Theory]
[InlineData(new char[0], OperationStatus.NeedMoreData, 0xFFFD, 0)] // empty buffer
[InlineData(new char[] { '\u1234', '\u5678' }, OperationStatus.Done, 0x5678, 1)] // BMP char
[InlineData(new char[] { '\udc00', '\ud800' }, OperationStatus.NeedMoreData, 0xFFFD, 1)] // high surrogate at end of buffer
[InlineData(new char[] { '\ud83d', '\ude32' }, OperationStatus.Done, 0x1F632, 2)] // supplementary value (U+1F632 ASTONISHED FACE)
[InlineData(new char[] { '\u1234', '\udc00' }, OperationStatus.InvalidData, 0xFFFD, 1)] // standalone low surrogate
[InlineData(new char[] { '\udc00' }, OperationStatus.InvalidData, 0xFFFD, 1)] // standalone low surrogate
public static void DecodeLastFromUtf16(char[] data, OperationStatus expectedOperationStatus, int expectedRuneValue, int expectedCharsConsumed)
{
Assert.Equal(expectedOperationStatus, Rune.DecodeLastFromUtf16(data, out Rune actualRune, out int actualCharsConsumed));
Assert.Equal(expectedRuneValue, actualRune.Value);
Assert.Equal(expectedCharsConsumed, actualCharsConsumed);
}
[Theory]
[InlineData(new byte[0], OperationStatus.NeedMoreData, 0xFFFD, 0)] // empty buffer
[InlineData(new byte[] { 0x30 }, OperationStatus.Done, 0x0030, 1)] // ASCII byte
[InlineData(new byte[] { 0x30, 0x40, 0x50 }, OperationStatus.Done, 0x0030, 1)] // ASCII byte
[InlineData(new byte[] { 0x80 }, OperationStatus.InvalidData, 0xFFFD, 1)] // standalone continuation byte
[InlineData(new byte[] { 0x80, 0x80, 0x80 }, OperationStatus.InvalidData, 0xFFFD, 1)] // standalone continuation byte
[InlineData(new byte[] { 0xC1 }, OperationStatus.InvalidData, 0xFFFD, 1)] // C1 is never a valid UTF-8 byte
[InlineData(new byte[] { 0xF5 }, OperationStatus.InvalidData, 0xFFFD, 1)] // F5 is never a valid UTF-8 byte
[InlineData(new byte[] { 0xC2 }, OperationStatus.NeedMoreData, 0xFFFD, 1)] // C2 is a valid byte; expecting it to be followed by a continuation byte
[InlineData(new byte[] { 0xED }, OperationStatus.NeedMoreData, 0xFFFD, 1)] // ED is a valid byte; expecting it to be followed by a continuation byte
[InlineData(new byte[] { 0xF4 }, OperationStatus.NeedMoreData, 0xFFFD, 1)] // F4 is a valid byte; expecting it to be followed by a continuation byte
[InlineData(new byte[] { 0xC2, 0xC2 }, OperationStatus.InvalidData, 0xFFFD, 1)] // C2 not followed by continuation byte
[InlineData(new byte[] { 0xC3, 0x90 }, OperationStatus.Done, 0x00D0, 2)] // [ C3 90 ] is U+00D0 LATIN CAPITAL LETTER ETH
[InlineData(new byte[] { 0xC1, 0xBF }, OperationStatus.InvalidData, 0xFFFD, 1)] // [ C1 BF ] is overlong 2-byte sequence, all overlong sequences have maximal invalid subsequence length 1
[InlineData(new byte[] { 0xE0, 0x9F }, OperationStatus.InvalidData, 0xFFFD, 1)] // [ E0 9F ] is overlong 3-byte sequence, all overlong sequences have maximal invalid subsequence length 1
[InlineData(new byte[] { 0xE0, 0xA0 }, OperationStatus.NeedMoreData, 0xFFFD, 2)] // [ E0 A0 ] is valid 2-byte start of 3-byte sequence
[InlineData(new byte[] { 0xED, 0x9F }, OperationStatus.NeedMoreData, 0xFFFD, 2)] // [ ED 9F ] is valid 2-byte start of 3-byte sequence
[InlineData(new byte[] { 0xED, 0xBF }, OperationStatus.InvalidData, 0xFFFD, 1)] // [ ED BF ] would place us in UTF-16 surrogate range, all surrogate sequences have maximal invalid subsequence length 1
[InlineData(new byte[] { 0xEE, 0x80 }, OperationStatus.NeedMoreData, 0xFFFD, 2)] // [ EE 80 ] is valid 2-byte start of 3-byte sequence
[InlineData(new byte[] { 0xF0, 0x8F }, OperationStatus.InvalidData, 0xFFFD, 1)] // [ F0 8F ] is overlong 4-byte sequence, all overlong sequences have maximal invalid subsequence length 1
[InlineData(new byte[] { 0xF0, 0x90 }, OperationStatus.NeedMoreData, 0xFFFD, 2)] // [ F0 90 ] is valid 2-byte start of 4-byte sequence
[InlineData(new byte[] { 0xF4, 0x90 }, OperationStatus.InvalidData, 0xFFFD, 1)] // [ F4 90 ] would place us beyond U+10FFFF, all such sequences have maximal invalid subsequence length 1
[InlineData(new byte[] { 0xE2, 0x88, 0xB4 }, OperationStatus.Done, 0x2234, 3)] // [ E2 88 B4 ] is U+2234 THEREFORE
[InlineData(new byte[] { 0xE2, 0x88, 0xC0 }, OperationStatus.InvalidData, 0xFFFD, 2)] // [ E2 88 ] followed by non-continuation byte, maximal invalid subsequence length 2
[InlineData(new byte[] { 0xF0, 0x9F, 0x98 }, OperationStatus.NeedMoreData, 0xFFFD, 3)] // [ F0 9F 98 ] is valid 3-byte start of 4-byte sequence
[InlineData(new byte[] { 0xF0, 0x9F, 0x98, 0x20 }, OperationStatus.InvalidData, 0xFFFD, 3)] // [ F0 9F 98 ] followed by non-continuation byte, maximal invalid subsequence length 3
[InlineData(new byte[] { 0xF0, 0x9F, 0x98, 0xB2 }, OperationStatus.Done, 0x1F632, 4)] // [ F0 9F 98 B2 ] is U+1F632 ASTONISHED FACE
public static void DecodeFromUtf8(byte[] data, OperationStatus expectedOperationStatus, int expectedRuneValue, int expectedBytesConsumed)
{
Assert.Equal(expectedOperationStatus, Rune.DecodeFromUtf8(data, out Rune actualRune, out int actualBytesConsumed));
Assert.Equal(expectedRuneValue, actualRune.Value);
Assert.Equal(expectedBytesConsumed, actualBytesConsumed);
// Parse / TryParse succeed iff the entire input decodes to a single scalar.
if (expectedOperationStatus == OperationStatus.Done && expectedBytesConsumed == data.Length)
{
Assert.Equal(expectedRuneValue, Utf8SpanParsableHelper<Rune>.Parse(data, null).Value);
Assert.True(Utf8SpanParsableHelper<Rune>.TryParse(data, null, out Rune parsedFromUtf8));
Assert.Equal(expectedRuneValue, parsedFromUtf8.Value);
}
else
{
Assert.Throws<FormatException>(() => Utf8SpanParsableHelper<Rune>.Parse(data, null));
Assert.False(Utf8SpanParsableHelper<Rune>.TryParse(data, null, out Rune parsedFromUtf8));
Assert.Equal(Rune.ReplacementChar, parsedFromUtf8);
}
}
[Fact]
public static void Parse_String_Null()
{
Assert.Throws<ArgumentNullException>(() => ParsableHelper<Rune>.Parse(null, null));
Assert.False(ParsableHelper<Rune>.TryParse(null, null, out Rune actualRune));
Assert.Equal(Rune.ReplacementChar, actualRune);
}
[Theory]
[InlineData(new byte[0], OperationStatus.NeedMoreData, 0xFFFD, 0)] // empty buffer
[InlineData(new byte[] { 0x30 }, OperationStatus.Done, 0x0030, 1)] // ASCII byte
[InlineData(new byte[] { 0x30, 0x40, 0x50 }, OperationStatus.Done, 0x0050, 1)] // ASCII byte
[InlineData(new byte[] { 0x80 }, OperationStatus.InvalidData, 0xFFFD, 1)] // standalone continuation byte
[InlineData(new byte[] { 0x80, 0x80, 0x80 }, OperationStatus.InvalidData, 0xFFFD, 1)] // standalone continuation byte
[InlineData(new byte[] { 0x80, 0x80, 0x80, 0x80 }, OperationStatus.InvalidData, 0xFFFD, 1)] // standalone continuation byte
[InlineData(new byte[] { 0x80, 0x80, 0x80, 0xC2 }, OperationStatus.NeedMoreData, 0xFFFD, 1)] // [ C2 ] at end of buffer, valid 1-byte start of 2-byte sequence
[InlineData(new byte[] { 0xC1 }, OperationStatus.InvalidData, 0xFFFD, 1)] // [ C1 ] is never a valid byte
[InlineData(new byte[] { 0x80, 0xE2, 0x88, 0xB4 }, OperationStatus.Done, 0x2234, 3)] // [ E2 88 B4 ] is U+2234 THEREFORE
[InlineData(new byte[] { 0xF0, 0x9F, 0x98, 0xB2 }, OperationStatus.Done, 0x1F632, 4)] // [ F0 9F 98 B2 ] is U+1F632 ASTONISHED FACE
[InlineData(new byte[] { 0xE2, 0x88, 0xB4, 0xB2 }, OperationStatus.InvalidData, 0xFFFD, 1)] // [ B2 ] is standalone continuation byte
[InlineData(new byte[] { 0x80, 0x62, 0x80, 0x80 }, OperationStatus.InvalidData, 0xFFFD, 1)] // [ 80 ] is standalone continuation byte
[InlineData(new byte[] { 0xF0, 0x9F, 0x98, }, OperationStatus.NeedMoreData, 0xFFFD, 3)] // [ F0 9F 98 ] is valid 3-byte start of 4-byte sequence
public static void DecodeLastFromUtf8(byte[] data, OperationStatus expectedOperationStatus, int expectedRuneValue, int expectedBytesConsumed)
{
Assert.Equal(expectedOperationStatus, Rune.DecodeLastFromUtf8(data, out Rune actualRune, out int actualBytesConsumed));
Assert.Equal(expectedRuneValue, actualRune.Value);
Assert.Equal(expectedBytesConsumed, actualBytesConsumed);
}
[Theory]
[InlineData(0, 0, true)]
[InlineData(0x10FFFF, 0x10FFFF, true)]
[InlineData(0xFFFD, 0xFFFD, true)]
[InlineData(0xFFFD, 0xFFFF, false)]
[InlineData('a', 'a', true)]
[InlineData('a', 'A', false)]
[InlineData('a', 'b', false)]
public static void Equals_OperatorEqual_OperatorNotEqual(int first, int other, bool expected)
{
Rune a = new Rune(first);
Rune b = new Rune(other);
Assert.Equal(expected, Object.Equals(a, b));
Assert.Equal(expected, a.Equals(b));
Assert.Equal(expected, a.Equals((object)b));
Assert.Equal(expected, a == b);
Assert.NotEqual(expected, a != b);
}
[Theory]
[InlineData('a', 'a', StringComparison.CurrentCulture, true)]
[InlineData('a', 'A', StringComparison.CurrentCulture, false)]
[InlineData(0x1F600, 0x1F600, StringComparison.CurrentCulture, true)]
[InlineData(0x1F601, 0x1F600, StringComparison.CurrentCulture, false)]
[InlineData('a', 'a', StringComparison.CurrentCultureIgnoreCase, true)]
[InlineData('a', 'A', StringComparison.CurrentCultureIgnoreCase, true)]
[InlineData(0x1F600, 0x1F600, StringComparison.CurrentCultureIgnoreCase, true)]
[InlineData(0x1F601, 0x1F600, StringComparison.CurrentCultureIgnoreCase, false)]
[InlineData('a', 'a', StringComparison.InvariantCulture, true)]
[InlineData('a', 'A', StringComparison.InvariantCulture, false)]
[InlineData(0x1F600, 0x1F600, StringComparison.InvariantCulture, true)]
[InlineData(0x1F601, 0x1F600, StringComparison.InvariantCulture, false)]
[InlineData('a', 'a', StringComparison.InvariantCultureIgnoreCase, true)]
[InlineData('a', 'A', StringComparison.InvariantCultureIgnoreCase, true)]
[InlineData(0x1F600, 0x1F600, StringComparison.InvariantCultureIgnoreCase, true)]
[InlineData(0x1F601, 0x1F600, StringComparison.InvariantCultureIgnoreCase, false)]
[InlineData('a', 'a', StringComparison.Ordinal, true)]
[InlineData('a', 'A', StringComparison.Ordinal, false)]
[InlineData(0x1F600, 0x1F600, StringComparison.Ordinal, true)]
[InlineData(0x1F601, 0x1F600, StringComparison.Ordinal, false)]
[InlineData('a', 'a', StringComparison.OrdinalIgnoreCase, true)]
[InlineData('a', 'A', StringComparison.OrdinalIgnoreCase, true)]
[InlineData(0x1F600, 0x1F600, StringComparison.OrdinalIgnoreCase, true)]
[InlineData(0x1F601, 0x1F600, StringComparison.OrdinalIgnoreCase, false)]
public static void Equals_StringComparison(int first, int other, StringComparison comparisonType, bool expected)
{
Rune a = new Rune(first);
Rune b = new Rune(other);
Assert.Equal(expected, a.Equals(b, comparisonType));
}
[Theory]
[InlineData(0)]
[InlineData('a')]
[InlineData('\uFFFD')]
[InlineData(0x10FFFF)]
public static void GetHashCodeTests(int scalarValue)
{
Assert.Equal(scalarValue, new Rune(scalarValue).GetHashCode());
}
[Theory]
[InlineData("a", 0, (int)'a')]
[InlineData("ab", 1, (int)'b')]
[InlineData("x\U0001F46Ey", 3, (int)'y')]
[InlineData("x\U0001F46Ey", 1, 0x1F46E)] // U+1F46E POLICE OFFICER
public static void GetRuneAt_TryGetRuneAt_Utf16_Success(string inputString, int index, int expectedScalarValue)
{
// GetRuneAt
Assert.Equal(expectedScalarValue, Rune.GetRuneAt(inputString, index).Value);
// TryGetRuneAt
Assert.True(Rune.TryGetRuneAt(inputString, index, out Rune rune));
Assert.Equal(expectedScalarValue, rune.Value);
}
// Our unit test runner doesn't deal well with malformed literal strings, so
// we smuggle it as a char[] and turn it into a string within the test itself.
[Theory]
[InlineData(new char[] { 'x', '\uD83D', '\uDC6E', 'y' }, 2)] // attempt to index into the middle of a UTF-16 surrogate pair
[InlineData(new char[] { 'x', '\uD800', 'y' }, 1)] // high surrogate not followed by low surrogate
[InlineData(new char[] { 'x', '\uDFFF', '\uDFFF' }, 1)] // attempt to start at a low surrogate
[InlineData(new char[] { 'x', '\uD800' }, 1)] // end of string reached before could complete surrogate pair
public static void GetRuneAt_TryGetRuneAt_Utf16_InvalidData(char[] inputCharArray, int index)
{
string inputString = new string(inputCharArray);
// GetRuneAt
Assert.Throws<ArgumentException>("index", () => Rune.GetRuneAt(inputString, index));
// TryGetRuneAt
Assert.False(Rune.TryGetRuneAt(inputString, index, out Rune rune));
Assert.Equal(0, rune.Value);
}
[Fact]
public static void GetRuneAt_TryGetRuneAt_Utf16_BadArgs()
{
// null input
Assert.Throws<ArgumentNullException>("input", () => Rune.GetRuneAt(null, 0));
// negative index specified
Assert.Throws<ArgumentOutOfRangeException>("index", () => Rune.GetRuneAt("hello", -1));
// index goes past end of string
Assert.Throws<ArgumentOutOfRangeException>("index", () => Rune.GetRuneAt(string.Empty, 0));
}
[Theory]
[MemberData(nameof(UnicodeInfoTestData_Latin1AndSelectOthers))]
public static void GetNumericValue(UnicodeInfoTestData testData)
{
Assert.Equal(testData.NumericValue, Rune.GetNumericValue(testData.ScalarValue));
}
[Theory]
[MemberData(nameof(UnicodeInfoTestData_Latin1AndSelectOthers))]
public static void GetUnicodeCategory(UnicodeInfoTestData testData)
{
Assert.Equal(testData.UnicodeCategory, Rune.GetUnicodeCategory(testData.ScalarValue));
}
[OuterLoop]
[Fact]
public static void GetUnicodeCategory_AllInputs()
{
// This tests calls Rune.GetUnicodeCategory for every possible input, ensuring that
// the runtime agrees with the data in the core Unicode files.
foreach (Rune rune in AllRunes())
{
if (UnicodeData.GetUnicodeCategory(rune.Value) != Rune.GetUnicodeCategory(rune))
{
// We'll build up the exception message ourselves so the dev knows what code point failed.
throw EqualException.ForMismatchedValues(
expected: UnicodeData.GetUnicodeCategory(rune.Value).ToString(),
actual: Rune.GetUnicodeCategory(rune).ToString(),
banner: FormattableString.Invariant($@"Rune.GetUnicodeCategory(U+{rune.Value:X4}) returned wrong value."));
}
}
}
[Theory]
[MemberData(nameof(UnicodeInfoTestData_Latin1AndSelectOthers))]
public static void IsControl(UnicodeInfoTestData testData)
{
Assert.Equal(testData.IsControl, Rune.IsControl(testData.ScalarValue));
}
[Theory]
[MemberData(nameof(UnicodeInfoTestData_Latin1AndSelectOthers))]
public static void IsDigit(UnicodeInfoTestData testData)
{
Assert.Equal(testData.IsDigit, Rune.IsDigit(testData.ScalarValue));
}
[Theory]
[MemberData(nameof(UnicodeInfoTestData_Latin1AndSelectOthers))]
public static void IsLetter(UnicodeInfoTestData testData)
{
Assert.Equal(testData.IsLetter, Rune.IsLetter(testData.ScalarValue));
}
[Theory]
[MemberData(nameof(UnicodeInfoTestData_Latin1AndSelectOthers))]
public static void IsLetterOrDigit(UnicodeInfoTestData testData)
{
Assert.Equal(testData.IsLetterOrDigit, Rune.IsLetterOrDigit(testData.ScalarValue));
}
[Theory]
[MemberData(nameof(UnicodeInfoTestData_Latin1AndSelectOthers))]
public static void IsLower(UnicodeInfoTestData testData)
{
Assert.Equal(testData.IsLower, Rune.IsLower(testData.ScalarValue));
}
[Theory]
[MemberData(nameof(UnicodeInfoTestData_Latin1AndSelectOthers))]
public static void IsNumber(UnicodeInfoTestData testData)
{
Assert.Equal(testData.IsNumber, Rune.IsNumber(testData.ScalarValue));
}
[Theory]
[MemberData(nameof(UnicodeInfoTestData_Latin1AndSelectOthers))]
public static void IsPunctuation(UnicodeInfoTestData testData)
{
Assert.Equal(testData.IsPunctuation, Rune.IsPunctuation(testData.ScalarValue));
}
[Theory]
[MemberData(nameof(UnicodeInfoTestData_Latin1AndSelectOthers))]
public static void IsSeparator(UnicodeInfoTestData testData)
{
Assert.Equal(testData.IsSeparator, Rune.IsSeparator(testData.ScalarValue));
}
[Theory]
[MemberData(nameof(UnicodeInfoTestData_Latin1AndSelectOthers))]
public static void IsSymbol(UnicodeInfoTestData testData)
{
Assert.Equal(testData.IsSymbol, Rune.IsSymbol(testData.ScalarValue));
}
[Theory]
[MemberData(nameof(UnicodeInfoTestData_Latin1AndSelectOthers))]
public static void IsUpper(UnicodeInfoTestData testData)
{
Assert.Equal(testData.IsUpper, Rune.IsUpper(testData.ScalarValue));
}
[Theory]
[MemberData(nameof(IsValidTestData))]
public static void IsValid(int scalarValue, bool expectedIsValid)
{
Assert.Equal(expectedIsValid, Rune.IsValid(scalarValue));
Assert.Equal(expectedIsValid, Rune.IsValid((uint)scalarValue));
}
[Theory]
[MemberData(nameof(UnicodeInfoTestData_Latin1AndSelectOthers))]
public static void IsWhiteSpace(UnicodeInfoTestData testData)
{
Assert.Equal(testData.IsWhiteSpace, Rune.IsWhiteSpace(testData.ScalarValue));
}
[OuterLoop]
[Fact]
public static void IsWhiteSpace_AllInputs()
{
// This tests calls Rune.IsWhiteSpace for every possible input, ensuring that
// the runtime agrees with the data in the core Unicode files.
foreach (Rune rune in AllRunes())
{
Assert.Equal(UnicodeData.IsWhiteSpace(rune.Value), Rune.IsWhiteSpace(rune));
}
}
[Theory]
[InlineData(0, 0)]
[InlineData(0x80, 0x80)]
[InlineData(0x80, 0x100)]
[InlineData(0x100, 0x80)]
public static void Operators_And_CompareTo(uint scalarValueLeft, uint scalarValueRight)
{
Rune left = new Rune(scalarValueLeft);
Rune right = new Rune(scalarValueRight);
Assert.Equal(scalarValueLeft == scalarValueRight, left == right);
Assert.Equal(scalarValueLeft != scalarValueRight, left != right);
Assert.Equal(scalarValueLeft < scalarValueRight, left < right);
Assert.Equal(scalarValueLeft <= scalarValueRight, left <= right);
Assert.Equal(scalarValueLeft > scalarValueRight, left > right);
Assert.Equal(scalarValueLeft >= scalarValueRight, left >= right);
Assert.Equal(Math.Sign(scalarValueLeft.CompareTo(scalarValueRight)), Math.Sign(left.CompareTo(right)));
Assert.Equal(Math.Sign(((IComparable)scalarValueLeft).CompareTo(scalarValueRight)), Math.Sign(((IComparable)left).CompareTo(right)));
}
[Theory]
[InlineData(0)]
[InlineData(0x10FFFF)]
public static void NonGenericCompareTo_NonNullAlwaysGreaterThanNull(uint scalarValue)
{
Assert.Equal(1, Math.Sign(((IComparable)new Rune(scalarValue)).CompareTo(null)));
}
[Fact]
public static void NonGenericCompareTo_GivenNonRuneArgument_ThrowsArgumentException()
{
IComparable rune = new Rune(0);
Assert.Throws<ArgumentException>(() => rune.CompareTo(0 /* int32 */));
}
[Fact]
public static void ReplacementChar()
{
Assert.Equal(0xFFFD, Rune.ReplacementChar.Value);
}
[Theory]
[MemberData(nameof(GeneralTestData_BmpCodePoints_NoSurrogates))]
public static void TryCreate_Char_Valid(GeneralTestData testData)
{
Assert.True(Rune.TryCreate((char)testData.ScalarValue, out Rune result));
Assert.Equal(testData.ScalarValue, result.Value);
}
[Theory]
[MemberData(nameof(BmpCodePoints_SurrogatesOnly))]
public static void TryCreate_Char_Invalid(int scalarValue)
{
Assert.False(Rune.TryCreate((char)scalarValue, out Rune result));
Assert.Equal(0, result.Value);
}
[Theory]
[MemberData(nameof(SurrogatePairTestData_InvalidOnly))]
public static void TryCreate_SurrogateChars_Invalid(char highSurrogate, char lowSurrogate)
{
Assert.False(Rune.TryCreate(highSurrogate, lowSurrogate, out Rune result));
Assert.Equal(0, result.Value);
}
[Theory]
[MemberData(nameof(SurrogatePairTestData_ValidOnly))]
public static void TryCreate_SurrogateChars_Valid(char highSurrogate, char lowSurrogate, int expectedValue)
{
Assert.True(Rune.TryCreate(highSurrogate, lowSurrogate, out Rune result));
Assert.Equal(expectedValue, result.Value);
}
[Theory]
[MemberData(nameof(GeneralTestData_BmpCodePoints_NoSurrogates))]
[MemberData(nameof(GeneralTestData_SupplementaryCodePoints_ValidOnly))]
public static void TryCreate_Int32_Valid(GeneralTestData testData)
{
Assert.True(Rune.TryCreate((int)testData.ScalarValue, out Rune result));
Assert.Equal(testData.ScalarValue, result.Value);
}
[Theory]
[MemberData(nameof(BmpCodePoints_SurrogatesOnly))]
[MemberData(nameof(SupplementaryCodePoints_InvalidOnly))]
public static void TryCreate_Int32_Invalid(int scalarValue)
{
Assert.False(Rune.TryCreate((int)scalarValue, out Rune result));
Assert.Equal(0, result.Value);
}
[Theory]
[MemberData(nameof(GeneralTestData_BmpCodePoints_NoSurrogates))]
[MemberData(nameof(GeneralTestData_SupplementaryCodePoints_ValidOnly))]
public static void TryCreate_UInt32_Valid(GeneralTestData testData)
{
Assert.True(Rune.TryCreate((uint)testData.ScalarValue, out Rune result));
Assert.Equal(testData.ScalarValue, result.Value);
}
[Theory]
[MemberData(nameof(BmpCodePoints_SurrogatesOnly))]
[MemberData(nameof(SupplementaryCodePoints_InvalidOnly))]
public static void TryCreate_UInt32_Invalid(int scalarValue)
{
Assert.False(Rune.TryCreate((uint)scalarValue, out Rune result));
Assert.Equal(0, result.Value);
}
[Theory]
[MemberData(nameof(GeneralTestData_BmpCodePoints_NoSurrogates))]
[MemberData(nameof(GeneralTestData_SupplementaryCodePoints_ValidOnly))]
public static void ToStringTests(GeneralTestData testData)
{
Assert.Equal(new string(testData.Utf16Sequence), new Rune(testData.ScalarValue).ToString());
}
[Theory]
[MemberData(nameof(GeneralTestData_BmpCodePoints_NoSurrogates))]
[MemberData(nameof(GeneralTestData_SupplementaryCodePoints_ValidOnly))]
public static void TryEncodeToUtf16(GeneralTestData testData)
{
Rune rune = new Rune(testData.ScalarValue);
Assert.Equal(testData.Utf16Sequence.Length, rune.Utf16SequenceLength);
// First, try with a buffer that's too short
Span<char> utf16Buffer = stackalloc char[rune.Utf16SequenceLength - 1];
bool success = rune.TryEncodeToUtf16(utf16Buffer, out int charsWritten);
Assert.False(success);
Assert.Equal(0, charsWritten);
Assert.Throws<ArgumentException>("destination", () => rune.EncodeToUtf16(new char[rune.Utf16SequenceLength - 1]));
// Then, try with a buffer that's appropriately sized
utf16Buffer = stackalloc char[rune.Utf16SequenceLength];
success = rune.TryEncodeToUtf16(utf16Buffer, out charsWritten);
Assert.True(success);
Assert.Equal(testData.Utf16Sequence.Length, charsWritten);
Assert.True(utf16Buffer.SequenceEqual(testData.Utf16Sequence));
utf16Buffer.Clear();
Assert.Equal(testData.Utf16Sequence.Length, rune.EncodeToUtf16(utf16Buffer));
Assert.True(utf16Buffer.SequenceEqual(testData.Utf16Sequence));
// Finally, try with a buffer that's too long (should succeed)
utf16Buffer = stackalloc char[rune.Utf16SequenceLength + 1];
success = rune.TryEncodeToUtf16(utf16Buffer, out charsWritten);
Assert.True(success);
Assert.Equal(testData.Utf16Sequence.Length, charsWritten);
Assert.True(utf16Buffer.Slice(0, testData.Utf16Sequence.Length).SequenceEqual(testData.Utf16Sequence));
utf16Buffer.Clear();
Assert.Equal(testData.Utf16Sequence.Length, rune.EncodeToUtf16(utf16Buffer));
Assert.True(utf16Buffer.Slice(0, testData.Utf16Sequence.Length).SequenceEqual(testData.Utf16Sequence));
}
[Theory]
[MemberData(nameof(GeneralTestData_BmpCodePoints_NoSurrogates))]
[MemberData(nameof(GeneralTestData_SupplementaryCodePoints_ValidOnly))]
public static void TryEncodeToUtf8(GeneralTestData testData)
{
Rune rune = new Rune(testData.ScalarValue);
Assert.Equal(testData.Utf8Sequence.Length, actual: rune.Utf8SequenceLength);
// First, try with a buffer that's too short
Span<byte> utf8Buffer = stackalloc byte[rune.Utf8SequenceLength - 1];
bool success = rune.TryEncodeToUtf8(utf8Buffer, out int bytesWritten);
Assert.False(success);
Assert.Equal(0, bytesWritten);
Assert.Throws<ArgumentException>("destination", () => rune.EncodeToUtf8(new byte[rune.Utf8SequenceLength - 1]));
// Then, try with a buffer that's appropriately sized
utf8Buffer = stackalloc byte[rune.Utf8SequenceLength];
success = rune.TryEncodeToUtf8(utf8Buffer, out bytesWritten);
Assert.True(success);
Assert.Equal(testData.Utf8Sequence.Length, bytesWritten);
Assert.True(utf8Buffer.SequenceEqual(testData.Utf8Sequence));
utf8Buffer.Clear();
Assert.Equal(testData.Utf8Sequence.Length, rune.EncodeToUtf8(utf8Buffer));
Assert.True(utf8Buffer.SequenceEqual(testData.Utf8Sequence));
// Finally, try with a buffer that's too long (should succeed)
utf8Buffer = stackalloc byte[rune.Utf8SequenceLength + 1];
success = rune.TryEncodeToUtf8(utf8Buffer, out bytesWritten);
Assert.True(success);
Assert.Equal(testData.Utf8Sequence.Length, bytesWritten);
Assert.True(utf8Buffer.Slice(0, testData.Utf8Sequence.Length).SequenceEqual(testData.Utf8Sequence));
utf8Buffer.Clear();
Assert.Equal(testData.Utf8Sequence.Length, rune.EncodeToUtf8(utf8Buffer));
Assert.True(utf8Buffer.Slice(0, testData.Utf8Sequence.Length).SequenceEqual(testData.Utf8Sequence));
}
//
// RunePosition tests
//
private static void RunePosition_TestProps(Rune rune, int startIndex, int length, bool wasReplaced, RunePosition runePosition)
{
Assert.Equal(rune, runePosition.Rune);
Assert.Equal(startIndex, runePosition.StartIndex);
Assert.Equal(length, runePosition.Length);
Assert.Equal(wasReplaced, runePosition.WasReplaced);
Assert.Equal(new RunePosition(rune, startIndex, length, wasReplaced), runePosition);
}
private static void RunePosition_TestEquals(RunePosition expected, RunePosition runePosition)
{
if (expected.Rune == runePosition.Rune && expected.StartIndex == runePosition.StartIndex &&
expected.Length == runePosition.Length && expected.WasReplaced == runePosition.WasReplaced)
{
Assert.Equal(expected, runePosition);
Assert.Equal(runePosition, expected);
Assert.True(expected.Equals(runePosition));
Assert.True(runePosition.Equals(expected));
Assert.True(((object)expected).Equals(runePosition));
Assert.True(((object)runePosition).Equals(expected));
Assert.True(expected == runePosition);
Assert.True(runePosition == expected);
Assert.False(expected != runePosition);
Assert.False(runePosition != expected);
Assert.Equal(expected.GetHashCode(), runePosition.GetHashCode());
}
else
{
Assert.NotEqual(expected, runePosition);
Assert.NotEqual(runePosition, expected);
Assert.False(expected.Equals(runePosition));
Assert.False(runePosition.Equals(expected));
Assert.False(((object)expected).Equals(runePosition));
Assert.False(((object)runePosition).Equals(expected));
Assert.False(expected == runePosition);
Assert.False(runePosition == expected);
Assert.True(expected != runePosition);
Assert.True(runePosition != expected);
}
}
private static void RunePosition_TestDeconstruct(RunePosition runePosition)
{
{
(Rune rune, int startIndex) = runePosition;
Assert.Equal(runePosition.Rune, rune);
Assert.Equal(runePosition.StartIndex, startIndex);
}
{
(Rune rune, int startIndex, int length) = runePosition;
Assert.Equal(runePosition.Rune, rune);
Assert.Equal(runePosition.StartIndex, startIndex);
Assert.Equal(runePosition.Length, length);
}
}
[Fact]
public static void RunePosition_DefaultTest()
{
RunePosition runePosition = default;
RunePosition_TestProps(default, 0, 0, false, runePosition);
RunePosition_TestEquals(default, runePosition);
RunePosition_TestDeconstruct(runePosition);
runePosition = new RunePosition();
RunePosition_TestProps(default, 0, 0, false, runePosition);
RunePosition_TestEquals(default, runePosition);
RunePosition_TestDeconstruct(runePosition);
}
[Fact]
public static void EnumerateRunePositions_Empty()
{
{
RunePosition.Utf16Enumerator enumerator = RunePosition.EnumerateUtf16([]).GetEnumerator();
Assert.False(enumerator.MoveNext());
}
{
RunePosition.Utf8Enumerator enumerator = RunePosition.EnumerateUtf8([]).GetEnumerator();
Assert.False(enumerator.MoveNext());
}
}
[Theory]
[InlineData(new char[0])] // empty
[InlineData(new char[] { 'x', 'y', 'z' })]
[InlineData(new char[] { 'x', '\uD86D', '\uDF54', 'y' })] // valid surrogate pair
[InlineData(new char[] { 'x', '\uD86D', 'y' })] // standalone high surrogate
[InlineData(new char[] { 'x', '\uDF54', 'y' })] // standalone low surrogate
[InlineData(new char[] { 'x', '\uD86D' })] // standalone high surrogate at end of string
[InlineData(new char[] { 'x', '\uDF54' })] // standalone low surrogate at end of string
[InlineData(new char[] { 'x', '\uD86D', '\uD86D', 'y' })] // two high surrogates should be two replacement chars
[InlineData(new char[] { 'x', '\uFFFD', 'y' })] // literal U+FFFD
public static void EnumerateRunePositions_Battery16(char[] chars)
{
// Test data is smuggled as char[] instead of straight-up string since the test framework
// doesn't like invalid UTF-16 literals.
RunePosition.Utf16Enumerator enumerator = RunePosition.EnumerateUtf16(chars).GetEnumerator();
int expectedIndex = 0;
while (enumerator.MoveNext())
{
bool wasReplaced = Rune.DecodeFromUtf16(chars.AsSpan(expectedIndex), out Rune expectedRune, out int charsConsumed) != OperationStatus.Done;
RunePosition runePosition = enumerator.Current;
RunePosition_TestProps(expectedRune, expectedIndex, charsConsumed, wasReplaced, runePosition);
expectedIndex += charsConsumed;
}
Assert.Equal(chars.Length, expectedIndex);
}
[Theory]
[InlineData(new byte[0])] // empty
[InlineData(new byte[] { 0x30, 0x40, 0x50 })]
[InlineData(new byte[] { 0x31, 0x80, 0x41 })] // standalone continuation byte
[InlineData(new byte[] { 0x32, 0xC1, 0x42 })] // C1 is never a valid UTF-8 byte
[InlineData(new byte[] { 0x33, 0xF5, 0x43 })] // F5 is never a valid UTF-8 byte
[InlineData(new byte[] { 0x34, 0xC2, 0x44 })] // C2 is a valid byte; expecting it to be followed by a continuation byte
[InlineData(new byte[] { 0x35, 0xED, 0x45 })] // ED is a valid byte; expecting it to be followed by a continuation byte
[InlineData(new byte[] { 0x36, 0xF4, 0x46 })] // F4 is a valid byte; expecting it to be followed by a continuation byte
[InlineData(new byte[] { 0x37, 0xC2, 0xC2, 0x47 })] // C2 not followed by continuation byte
[InlineData(new byte[] { 0x38, 0xC3, 0x90, 0x48 })] // [ C3 90 ] is U+00D0 LATIN CAPITAL LETTER ETH
[InlineData(new byte[] { 0x39, 0xC1, 0xBF, 0x49 })] // [ C1 BF ] is overlong 2-byte sequence, all overlong sequences have maximal invalid subsequence length 1
[InlineData(new byte[] { 0x40, 0xE0, 0x9F, 0x50 })] // [ E0 9F ] is overlong 3-byte sequence, all overlong sequences have maximal invalid subsequence length 1
[InlineData(new byte[] { 0x41, 0xE0, 0xA0, 0x51 })] // [ E0 A0 ] is valid 2-byte start of 3-byte sequence
[InlineData(new byte[] { 0x42, 0xED, 0x9F, 0x52 })] // [ ED 9F ] is valid 2-byte start of 3-byte sequence
[InlineData(new byte[] { 0x43, 0xED, 0xBF, 0x53 })] // [ ED BF ] would place us in UTF-16 surrogate range, all surrogate sequences have maximal invalid subsequence length 1
[InlineData(new byte[] { 0x44, 0xEE, 0x80, 0x54 })] // [ EE 80 ] is valid 2-byte start of 3-byte sequence
[InlineData(new byte[] { 0x45, 0xF0, 0x8F, 0x55 })] // [ F0 8F ] is overlong 4-byte sequence, all overlong sequences have maximal invalid subsequence length 1
[InlineData(new byte[] { 0x46, 0xF0, 0x90, 0x56 })] // [ F0 90 ] is valid 2-byte start of 4-byte sequence
[InlineData(new byte[] { 0x47, 0xF4, 0x90, 0x57 })] // [ F4 90 ] would place us beyond U+10FFFF, all such sequences have maximal invalid subsequence length 1
[InlineData(new byte[] { 0x48, 0xE2, 0x88, 0xB4, 0x58 })] // [ E2 88 B4 ] is U+2234 THEREFORE
[InlineData(new byte[] { 0x49, 0xE2, 0x88, 0xC0, 0x59 })] // [ E2 88 ] followed by non-continuation byte, maximal invalid subsequence length 2
[InlineData(new byte[] { 0x50, 0xF0, 0x9F, 0x98, 0x60 })] // [ F0 9F 98 ] is valid 3-byte start of 4-byte sequence
[InlineData(new byte[] { 0x51, 0xF0, 0x9F, 0x98, 0x20, 0x61 })] // [ F0 9F 98 ] followed by non-continuation byte, maximal invalid subsequence length 3
[InlineData(new byte[] { 0x52, 0xF0, 0x9F, 0x98, 0xB2, 0x62 })] // [ F0 9F 98 B2 ] is U+1F632 ASTONISHED FACE
public static void EnumerateRunePositions_Battery8(byte[] bytes)
{
RunePosition.Utf8Enumerator enumerator = RunePosition.EnumerateUtf8(bytes).GetEnumerator();
int expectedIndex = 0;
while (enumerator.MoveNext())
{
bool wasReplaced = Rune.DecodeFromUtf8(bytes.AsSpan(expectedIndex), out Rune expectedRune, out int charsConsumed) != OperationStatus.Done;
RunePosition runePosition = enumerator.Current;
RunePosition_TestProps(expectedRune, expectedIndex, charsConsumed, wasReplaced, runePosition);
expectedIndex += charsConsumed;
}
Assert.Equal(bytes.Length, expectedIndex);
}
[Fact]
public static void EnumerateRunePositions_DoesNotReadPastEndOfSpan()
{
// As an optimization, reading scalars from a string *may* read past the end of the string
// to the terminating null. This optimization is invalid for arbitrary spans, so this test
// ensures that we're not performing this optimization here.
{
ReadOnlySpan<char> span = "xy\U0002B754z".AsSpan(1, 2); // well-formed string, but span splits surrogate pair
List<int> enumeratedValues = new List<int>();
foreach (RunePosition runePosition in RunePosition.EnumerateUtf16(span))
{
enumeratedValues.Add(runePosition.Rune.Value);
}
Assert.Equal(new int[] { 'y', '\uFFFD' }, enumeratedValues.ToArray());
}
{
ReadOnlySpan<byte> span = "xy\U0002B754z"u8.Slice(1, 2); // well-formed string, but span splits surrogate pair
List<int> enumeratedValues = new List<int>();
foreach (RunePosition runePosition in RunePosition.EnumerateUtf8(span))
{
enumeratedValues.Add(runePosition.Rune.Value);
}
Assert.Equal(new int[] { 'y', '\uFFFD' }, enumeratedValues.ToArray());
}
}
[Fact]
public static void EnumerateRunePositions_ResetEnumeration()
{
string text = "AB\U0002B754CD";
byte[] utf8Bytes = Encoding.UTF8.GetBytes(text);
{
RunePosition.Utf16Enumerator enumerator = RunePosition.EnumerateUtf16(text).GetEnumerator();
Assert.True(enumerator.MoveNext());
Assert.Equal('A', enumerator.Current.Rune.Value);
enumerator.Reset();
Assert.True(enumerator.MoveNext());
Assert.Equal('A', enumerator.Current.Rune.Value);
Assert.True(enumerator.MoveNext());
Assert.Equal('B', enumerator.Current.Rune.Value);
Assert.True(enumerator.MoveNext());
Assert.Equal(0x2B754, enumerator.Current.Rune.Value);
Assert.True(enumerator.MoveNext());
Assert.Equal('C', enumerator.Current.Rune.Value);
Assert.True(enumerator.MoveNext());
Assert.Equal('D', enumerator.Current.Rune.Value);
Assert.False(enumerator.MoveNext());
enumerator.Reset();
Assert.True(enumerator.MoveNext());
Assert.Equal('A', enumerator.Current.Rune.Value);
}
{
RunePosition.Utf8Enumerator enumerator = RunePosition.EnumerateUtf8(utf8Bytes).GetEnumerator();
Assert.True(enumerator.MoveNext());
Assert.Equal('A', enumerator.Current.Rune.Value);
enumerator.Reset();
Assert.True(enumerator.MoveNext());
Assert.Equal('A', enumerator.Current.Rune.Value);