-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathStringTests.cs
7844 lines (6737 loc) · 400 KB
/
StringTests.cs
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.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.DotNet.RemoteExecutor;
using Microsoft.DotNet.XUnitExtensions;
using Xunit;
using static System.Text.Tests.StringBuilderTests;
#pragma warning disable xUnit2009 // these are the tests for String and so should be using the explicit methods on String
namespace System.Tests
{
//When add new tests make sure to add checks for both string and span APIs where relevant.
public partial class StringTests
{
private const string SoftHyphen = "\u00AD";
private const string ZeroWidthJoiner = "\u200D"; // weightless in both ICU and NLS
private static readonly char[] s_whiteSpaceCharacters = { '\u0009', '\u000a', '\u000b', '\u000c', '\u000d', '\u0020', '\u0085', '\u00a0', '\u1680' };
[Theory]
[InlineData(new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', '\0' }, "abcdefgh")]
[InlineData(new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', '\0', 'i', 'j' }, "abcdefgh")]
[InlineData(new char[] { 'a', '\0' }, "a")]
[InlineData(new char[] { '\0' }, "")]
[InlineData(new char[] { '?', '@', ' ', '\0' }, "?@ ")] // ? and @ don't have overlapping bits
[InlineData(new char[] { '\u8001', '\u8002', '\ufffd', '\u1234', '\ud800', '\udfff', '\0' }, "\u8001\u8002\ufffd\u1234\ud800\udfff")] // chars with high bits set
public static unsafe void Ctor_CharPtr(char[] valueArray, string expected)
{
fixed (char* value = valueArray)
{
Assert.Equal(expected, new string(value));
}
fixed (char* value = &MemoryMarshal.GetReference<char>(valueArray))
{
Assert.Equal(expected, new string(value));
}
}
[Fact]
public static unsafe void Ctor_CharPtr_Empty()
{
Assert.Same(string.Empty, new string((char*)null));
}
[Fact]
public static unsafe void Ctor_CharPtr_OddAddressShouldStillWork()
{
// We need to get an odd address, so allocate a byte[] and
// take the address of the second element
byte[] bytes = { 0xff, 0x12, 0x34, 0x00, 0x00 };
fixed (byte* pBytes = bytes)
{
// The address of a fixed byte[] should always be even
Debug.Assert(unchecked((int)pBytes) % 2 == 0);
char* pCh = (char*)(pBytes + 1);
// This should handle the odd address when trying to get
// the length of the string to allocate
string actual = new string(pCh);
// Since we're casting between pointers of types with different sizes,
// the result will vary on little/big endian platforms
string expected = BitConverter.IsLittleEndian ? "\u3412" : "\u1234";
Assert.Equal(expected, actual);
}
}
[Theory]
[InlineData(new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', '\0' }, 0, 8, "abcdefgh")]
[InlineData(new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', '\0' }, 0, 9, "abcdefgh\0")]
[InlineData(new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', '\0', 'i', 'j', 'k' }, 0, 12, "abcdefgh\0ijk")]
[InlineData(new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', '\0' }, 2, 3, "cde")]
[InlineData(new char[] { '\0' }, 0, 1, "\0")]
[InlineData(new char[] { 'a', 'b', 'c' }, 0, 0, "")]
[InlineData(new char[] { 'a', 'b', 'c' }, 1, 0, "")]
public static unsafe void Ctor_CharPtr_Int_Int(char[] valueArray, int startIndex, int length, string expected)
{
_ = valueArray; // xunit analyzer bug: https://github.com/xunit/xunit/issues/1969
fixed (char* value = valueArray)
{
Assert.Equal(expected, new string(value, startIndex, length));
}
}
[Fact]
public static unsafe void Ctor_CharPtr_Int_Int_Empty()
{
Assert.Same(string.Empty, new string((char*)null, 0, 0));
}
[Fact]
public static unsafe void Ctor_CharPtr_Int_Int_Invalid()
{
var valueArray = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', '\0' };
AssertExtensions.Throws<ArgumentOutOfRangeException>("startIndex", () =>
{
fixed (char* value = valueArray) { new string(value, -1, 8); } // Start index < 0
});
AssertExtensions.Throws<ArgumentOutOfRangeException>("length", () =>
{
fixed (char* value = valueArray) { new string(value, 0, -1); } // Length < 0
});
AssertExtensions.Throws<ArgumentOutOfRangeException>("ptr", () => new string((char*)null, 0, 1)); // null ptr with non-zero length
AssertExtensions.Throws<ArgumentOutOfRangeException>("startIndex", () => new string(UIntPtr.Size == 4 ? (char*)uint.MaxValue : (char*)ulong.MaxValue, 42, 0)); // overflowing ptr + startIndex
}
[Theory]
[InlineData('a', 0, "")]
[InlineData('a', 1, "a")]
[InlineData('a', 2, "aa")]
[InlineData('a', 3, "aaa")]
[InlineData('a', 4, "aaaa")]
[InlineData('a', 5, "aaaaa")]
[InlineData('a', 6, "aaaaaa")]
[InlineData('a', 7, "aaaaaaa")]
[InlineData('a', 8, "aaaaaaaa")]
[InlineData('a', 9, "aaaaaaaaa")]
[InlineData('\0', 1, "\0")]
[InlineData('\0', 2, "\0\0")]
public static void Ctor_Char_Int(char c, int count, string expected)
{
Assert.Equal(expected, new string(c, count));
}
[Fact]
public static void Ctor_Char_Int_Negative_Count_ThrowsArgumentOutOfRangeException()
{
AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => new string('a', -1)); // Count < 0
}
[Theory]
[InlineData(new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' }, 0, 0, "")]
[InlineData(new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' }, 0, 3, "abc")]
[InlineData(new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' }, 2, 3, "cde")]
[InlineData(new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' }, 2, 6, "cdefgh")]
[InlineData(new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' }, 0, 8, "abcdefgh")]
[InlineData(new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', '\0', 'i', 'j' }, 0, 11, "abcdefgh\0ij")]
[InlineData(new char[] { '\u041F', '\u0420', '\u0418', '\u0412', '\u0415', '\u0422' }, 0, 6, "\u041F\u0420\u0418\u0412\u0415\u0422")]
[InlineData(new char[0], 0, 0, "")]
[InlineData(null, 0, 0, "")]
public static void Ctor_CharArray(char[] value, int startIndex, int length, string expected)
{
if (value == null)
{
Assert.Equal(expected, new string(value));
return;
}
if (startIndex == 0 && length == value.Length)
{
Assert.Equal(expected, new string(value));
}
Assert.Equal(expected, new string(value, startIndex, length));
}
[Fact]
public static void Ctor_CharArray_Invalid()
{
var value = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' };
AssertExtensions.Throws<ArgumentNullException>("value", () => new string((char[])null, 0, 0));
AssertExtensions.Throws<ArgumentOutOfRangeException>("startIndex", () => new string(value, 0, 9)); // Length > array length
AssertExtensions.Throws<ArgumentOutOfRangeException>("length", () => new string(value, 5, -1)); // Length < 0
AssertExtensions.Throws<ArgumentOutOfRangeException>("startIndex", () => new string(value, -1, 1)); // Start Index < 0
AssertExtensions.Throws<ArgumentOutOfRangeException>("startIndex", () => new string(value, 6, 5)); // Walks off array
}
[Theory]
[InlineData("Hello", 0, 'H')]
[InlineData("Hello", 1, 'e')]
[InlineData("Hello", 2, 'l')]
[InlineData("Hello", 3, 'l')]
[InlineData("Hello", 4, 'o')]
[InlineData("\0", 0, '\0')]
public static void Item_Get(string s, int index, char expected)
{
Assert.Equal(expected, s[index]);
Assert.Equal(expected, s.AsSpan()[index]);
}
[Fact]
public static void Item_Get_InvalidIndex_ThrowsIndexOutOfRangeException()
{
Assert.Throws<IndexOutOfRangeException>(() => "Hello"[-1]); // Index < 0
Assert.Throws<IndexOutOfRangeException>(() => "Hello"[5]); // Index >= string.Length
Assert.Throws<IndexOutOfRangeException>(() => ""[0]); // Index >= string.Length
Assert.Throws<IndexOutOfRangeException>(() => "Hello".AsSpan()[-1]); // Index < 0
Assert.Throws<IndexOutOfRangeException>(() => "Hello".AsSpan()[5]); // Index >= string.Length
Assert.Throws<IndexOutOfRangeException>(() => "".AsSpan()[0]); // Index >= string.Length
}
[Theory]
[InlineData("", 0)]
[InlineData("\0", 1)]
[InlineData("abc", 3)]
[InlineData("hello", 5)]
public static void Length(string s, int expected)
{
Assert.Equal(expected, s.Length);
Assert.Equal(expected, s.AsSpan().Length);
}
public static IEnumerable<object[]> Concat_Strings_LessThan2_GreaterThan4_TestData()
{
// 0
yield return new object[] { new string[0], "" };
// 1
yield return new object[] { new string[] { "1" }, "1" };
yield return new object[] { new string[] { null }, "" };
yield return new object[] { new string[] { "" }, "" };
// 5
yield return new object[] { new string[] { "1", "2", "3", "4", "5" }, "12345" };
yield return new object[] { new string[] { null, "1", "2", "3", "4" }, "1234" };
yield return new object[] { new string[] { "", "1", "2", "3", "4" }, "1234" };
yield return new object[] { new string[] { "1", null, "2", "3", "4" }, "1234" };
yield return new object[] { new string[] { "1", "", "2", "3", "4" }, "1234" };
yield return new object[] { new string[] { "1", "2", null, "3", "4" }, "1234" };
yield return new object[] { new string[] { "1", "2", "", "3", "4" }, "1234" };
yield return new object[] { new string[] { "1", "2", "3", null, "4" }, "1234" };
yield return new object[] { new string[] { "1", "2", "3", "", "4" }, "1234" };
yield return new object[] { new string[] { "1", "2", "3", "4", null }, "1234" };
yield return new object[] { new string[] { "1", "2", "3", "4", "" }, "1234" };
yield return new object[] { new string[] { "1", null, "3", null, "5" }, "135" };
yield return new object[] { new string[] { "1", "", "3", "", "5" }, "135" };
yield return new object[] { new string[] { null, null, null, null, null }, "" };
yield return new object[] { new string[] { "", "", "", "", "" }, "" };
// 7
yield return new object[] { new string[] { "abcd", "efgh", "ijkl", "mnop", "qrst", "uvwx", "yz" }, "abcdefghijklmnopqrstuvwxyz" };
}
public static IEnumerable<object[]> Concat_Strings_2_3_4_TestData()
{
// 2
yield return new object[] { new string[] { "1", "2" }, "12" };
yield return new object[] { new string[] { null, "1" }, "1" };
yield return new object[] { new string[] { "", "1" }, "1" };
yield return new object[] { new string[] { "1", null }, "1" };
yield return new object[] { new string[] { "1", "" }, "1" };
yield return new object[] { new string[] { null, null }, "" };
yield return new object[] { new string[] { "", "" }, "" };
// 3
yield return new object[] { new string[] { "1", "2", "3" }, "123" };
yield return new object[] { new string[] { null, "1", "2" }, "12" };
yield return new object[] { new string[] { "", "1", "2" }, "12" };
yield return new object[] { new string[] { "1", null, "2" }, "12" };
yield return new object[] { new string[] { "1", "", "2" }, "12" };
yield return new object[] { new string[] { "1", "2", null }, "12" };
yield return new object[] { new string[] { "1", "2", "" }, "12" };
yield return new object[] { new string[] { null, "2", null }, "2" };
yield return new object[] { new string[] { "", "2", "" }, "2" };
yield return new object[] { new string[] { null, null, null }, "" };
yield return new object[] { new string[] { "", "", "" }, "" };
// 4
yield return new object[] { new string[] { "1", "2", "3", "4" }, "1234" };
yield return new object[] { new string[] { null, "1", "2", "3" }, "123" };
yield return new object[] { new string[] { "", "1", "2", "3" }, "123" };
yield return new object[] { new string[] { "1", null, "2", "3" }, "123" };
yield return new object[] { new string[] { "1", "", "2", "3" }, "123" };
yield return new object[] { new string[] { "1", "2", null, "3" }, "123" };
yield return new object[] { new string[] { "1", "2", "", "3" }, "123" };
yield return new object[] { new string[] { "1", "2", "3", null }, "123" };
yield return new object[] { new string[] { "1", "2", "3", "" }, "123" };
yield return new object[] { new string[] { "1", null, null, null }, "1" };
yield return new object[] { new string[] { "1", "", "", "" }, "1" };
yield return new object[] { new string[] { null, "1", null, "2" }, "12" };
yield return new object[] { new string[] { "", "1", "", "2" }, "12" };
yield return new object[] { new string[] { null, null, null, null }, "" };
yield return new object[] { new string[] { "", "", "", "" }, "" };
}
[Theory]
[MemberData(nameof(Concat_Strings_2_3_4_TestData))]
[MemberData(nameof(Concat_Strings_LessThan2_GreaterThan4_TestData))]
public static void Concat_String(string[] values, string expected)
{
void Validate(string result)
{
Assert.Equal(expected, result);
if (result.Length == 0)
{
Assert.Same(string.Empty, result);
}
}
if (values.Length == 2)
{
Validate(string.Concat(values[0], values[1]));
}
else if (values.Length == 3)
{
Validate(string.Concat(values[0], values[1], values[2]));
}
else if (values.Length == 4)
{
Validate(string.Concat(values[0], values[1], values[2], values[3]));
}
Validate(string.Concat(values));
Validate(string.Concat((ReadOnlySpan<string?>)values));
Validate(string.Concat((IEnumerable<string>)values));
Validate(string.Concat<string>((IEnumerable<string>)values)); // Call the generic IEnumerable<T>-based overload
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[OuterLoop] // mini-stress test that likely runs for several seconds
public static void Concat_String_ConcurrencySafe()
{
var inputs = new string[2] { "abc", "def" };
var cts = new CancellationTokenSource();
using (var b = new Barrier(2))
{
// String.Concat(string[]) has a slow path that handles the case where the
// input array is mutated concurrently. Queue two tasks, one that repeatedly
// does concats and the other that mutates the array concurrently. This isn't
// guaranteed to trigger the special case, but it typically does.
Task.WaitAll(
Task.Run(() =>
{
b.SignalAndWait();
while (!cts.IsCancellationRequested)
{
string result = string.Concat(inputs);
Assert.True(result == "abcdef" || result == "abc" || result == "def" || result == "", $"result == {result}");
}
}),
Task.Run(() =>
{
b.SignalAndWait();
try
{
for (int iter = 0; iter < 100000000; iter++)
{
Volatile.Write(ref inputs[0], null);
Volatile.Write(ref inputs[1], null);
Volatile.Write(ref inputs[0], "abc");
Volatile.Write(ref inputs[1], "def");
}
}
finally
{
cts.Cancel();
}
}));
}
}
public static IEnumerable<object[]> Concat_Objects_TestData()
{
yield return new object[] { new object[] { }, "" };
yield return new object[] { new object[] { 1 }, "1" };
yield return new object[] { new object[] { null }, "" };
yield return new object[] { new object[] { 1, 2 }, "12" };
yield return new object[] { new object[] { null, 1 }, "1" };
yield return new object[] { new object[] { 1, null }, "1" };
yield return new object[] { new object[] { null, null }, "" };
yield return new object[] { new object[] { 1, 2, 3 }, "123" };
yield return new object[] { new object[] { null, 1, 2 }, "12" };
yield return new object[] { new object[] { 1, null, 2 }, "12" };
yield return new object[] { new object[] { 1, 2, null }, "12" };
yield return new object[] { new object[] { null, null, null }, "" };
yield return new object[] { new object[] { 1, 2, 3, 4 }, "1234" };
yield return new object[] { new object[] { null, 1, 2, 3 }, "123" };
yield return new object[] { new object[] { 1, null, 2, 3 }, "123" };
yield return new object[] { new object[] { 1, 2, 3, null }, "123" };
yield return new object[] { new object[] { null, null, null, null }, "" };
yield return new object[] { new object[] { 1, 2, 3, 4, 5 }, "12345" };
yield return new object[] { new object[] { null, 1, 2, 3, 4 }, "1234" };
yield return new object[] { new object[] { 1, null, 2, 3, 4 }, "1234" };
yield return new object[] { new object[] { 1, 2, 3, 4, null }, "1234" };
yield return new object[] { new object[] { null, null, null, null, null }, "" };
// Concat should ignore objects that have a null ToString() value
yield return new object[] { new object[] { new ObjectWithNullToString(), "Foo", new ObjectWithNullToString(), "Bar", new ObjectWithNullToString() }, "FooBar" };
yield return new object[] { new object[] { new ObjectWithNullToString() }, "" };
}
[Theory]
[MemberData(nameof(Concat_Objects_TestData))]
public static void Concat_Objects(object[] values, string expected)
{
if (values.Length == 1)
{
Assert.Equal(expected, string.Concat(values[0]));
}
else if (values.Length == 2)
{
Assert.Equal(expected, string.Concat(values[0], values[1]));
}
else if (values.Length == 3)
{
Assert.Equal(expected, string.Concat(values[0], values[1], values[2]));
}
else if (values.Length == 4)
{
Assert.Equal(expected, string.Concat(values[0], values[1], values[2], values[3]));
}
Assert.Equal(expected, string.Concat(values));
Assert.Equal(expected, string.Concat((ReadOnlySpan<object?>)values));
Assert.Equal(expected, string.Concat((IEnumerable<object>)values));
}
[Theory]
[InlineData(new char[0], "")]
[InlineData(new char[] { 'a' }, "a")]
[InlineData(new char[] { 'a', 'b' }, "ab")]
[InlineData(new char[] { 'a', '\0', 'b' }, "a\0b")]
[InlineData(new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g' }, "abcdefg")]
public static void Concat_CharEnumerable(char[] values, string expected)
{
Assert.Equal(expected, string.Concat(values.Select(c => c)));
}
[Fact]
public static void Concat_Invalid()
{
AssertExtensions.Throws<ArgumentNullException>("values", () => string.Concat((IEnumerable<string>)null)); // Values is null
AssertExtensions.Throws<ArgumentNullException>("values", () => string.Concat<string>((IEnumerable<string>)null)); // Generic overload
AssertExtensions.Throws<ArgumentNullException>("values", () => string.Concat(null)); // Values is null
AssertExtensions.Throws<ArgumentNullException>("args", () => string.Concat((object[])null)); // Values is null
AssertExtensions.Throws<ArgumentNullException>("values", () => string.Concat<string>(null)); // Values is null
AssertExtensions.Throws<ArgumentNullException>("values", () => string.Concat<object>(null)); // Values is null
}
[Theory]
[InlineData("Hello", 0, 0, 5, new char[] { 'H', 'e', 'l', 'l', 'o' })]
[InlineData("Hello", 1, 5, 3, new char[] { '\0', '\0', '\0', '\0', '\0', 'e', 'l', 'l', '\0', '\0' })]
[InlineData("Hello", 2, 0, 3, new char[] { 'l', 'l', 'o', '\0', '\0', '\0', '\0', '\0', '\0', '\0' })]
[InlineData("Hello", 0, 7, 3, new char[] { '\0', '\0', '\0', '\0', '\0', '\0', '\0', 'H', 'e', 'l' })]
[InlineData("Hello", 5, 10, 0, new char[] { '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' })]
[InlineData("H" + SoftHyphen + "ello", 0, 0, 3, new char[] { 'H', '\u00AD', 'e' })]
public static void CopyTo(string s, int sourceIndex, int destinationIndex, int count, char[] expected)
{
char[] dst = new char[expected.Length];
s.CopyTo(sourceIndex, dst, destinationIndex, count);
Assert.Equal(expected, dst);
Span<char> dstSpan = new char[expected.Length];
s.AsSpan(sourceIndex, count).CopyTo(dstSpan.Slice(destinationIndex, count));
Assert.Equal(expected, dstSpan.ToArray());
}
[Fact]
public static void CopyTo_Invalid()
{
string s = "Hello";
char[] dst = new char[10];
AssertExtensions.Throws<ArgumentNullException>("destination", () => s.CopyTo(0, null, 0, 0)); // Dst is null
AssertExtensions.Throws<ArgumentOutOfRangeException>("sourceIndex", () => s.CopyTo(-1, dst, 0, 0)); // Source index < 0
AssertExtensions.Throws<ArgumentOutOfRangeException>("destinationIndex", () => s.CopyTo(0, dst, -1, 0)); // Destination index < 0
AssertExtensions.Throws<ArgumentOutOfRangeException>("destinationIndex", () => s.CopyTo(0, dst, dst.Length, 1)); // Destination index > dst.Length
AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => s.CopyTo(0, dst, 0, -1)); // Count < 0
// Source index + count > string.Length
AssertExtensions.Throws<ArgumentOutOfRangeException>("sourceIndex", () => s.CopyTo(s.Length, dst, 0, 1));
AssertExtensions.Throws<ArgumentOutOfRangeException>("sourceIndex", () => s.CopyTo(s.Length - 1, dst, 0, 2));
AssertExtensions.Throws<ArgumentOutOfRangeException>("sourceIndex", () => s.CopyTo(0, dst, 0, 6));
}
[Theory]
[InlineData("", 0)]
[InlineData("", 1)]
[InlineData("a", 1)]
[InlineData("a", 0)]
[InlineData("a", 2)]
[InlineData("abc", 2)]
[InlineData("abc", 3)]
[InlineData("abc", 4)]
[InlineData("Hello world", 20)]
public static void CopyTo_Span(string s, int destinationLength)
{
char[] destination = new char[destinationLength];
if (s.Length > destinationLength)
{
AssertExtensions.Throws<ArgumentException>("destination", () => s.CopyTo(destination));
Assert.All(destination, c => Assert.Equal(0, c));
Assert.False(s.TryCopyTo(destination));
Assert.All(destination, c => Assert.Equal(0, c));
}
else
{
s.CopyTo(destination);
Assert.Equal(s, new Span<char>(destination, 0, s.Length).ToString());
Assert.All(destination.AsSpan(s.Length).ToArray(), c => Assert.Equal(0, c));
Array.Clear(destination);
Assert.True(s.TryCopyTo(destination));
Assert.Equal(s, new Span<char>(destination, 0, s.Length).ToString());
Assert.All(destination.AsSpan(s.Length).ToArray(), c => Assert.Equal(0, c));
}
}
public static IEnumerable<object[]> Compare_TestData()
{
// CurrentCulture
yield return new object[] { "", 0, "", 0, 0, StringComparison.CurrentCulture, 0 };
yield return new object[] { "Hello", 0, "Hello", 0, 5, StringComparison.CurrentCulture, 0 };
yield return new object[] { "Hello", 2, "Hello", 3, 1, StringComparison.CurrentCulture, 0 };
yield return new object[] { "Hello", 0, "Goodbye", 0, 5, StringComparison.CurrentCulture, 1 };
yield return new object[] { "Goodbye", 0, "Hello", 0, 5, StringComparison.CurrentCulture, -1 };
yield return new object[] { "HELLO", 2, "hello", 2, 3, StringComparison.CurrentCulture, PlatformDetection.IsInvariantGlobalization ? -1 : 1 };
yield return new object[] { "hello", 2, "HELLO", 2, 3, StringComparison.CurrentCulture, PlatformDetection.IsInvariantGlobalization ? 1 : -1 };
yield return new object[] { "Hello", 2, "Hello", 2, 3, StringComparison.CurrentCulture, 0 };
yield return new object[] { "Hello", 2, "Goodbye", 2, 3, StringComparison.CurrentCulture, -1 };
yield return new object[] { "A", 0, "B", 0, 1, StringComparison.CurrentCulture, -1 };
yield return new object[] { "B", 0, "A", 0, 1, StringComparison.CurrentCulture, 1 };
yield return new object[] { null, 0, null, 0, 0, StringComparison.CurrentCulture, 0 };
yield return new object[] { "Hello", 0, null, 0, 0, StringComparison.CurrentCulture, 1 };
yield return new object[] { null, 0, "Hello", 0, 0, StringComparison.CurrentCulture, -1 };
yield return new object[] { null, -1, null, -1, -1, StringComparison.CurrentCulture, 0 };
yield return new object[] { "foo", -1, null, -1, -1, StringComparison.CurrentCulture, 1 };
yield return new object[] { null, -1, "foo", -1, -1, StringComparison.CurrentCulture, -1 };
// CurrentCultureIgnoreCase
yield return new object[] { "", 0, "", 0, 0, StringComparison.CurrentCultureIgnoreCase, 0 };
yield return new object[] { "HELLO", 0, "hello", 0, 5, StringComparison.CurrentCultureIgnoreCase, 0 };
yield return new object[] { "Hello", 2, "Hello", 3, 1, StringComparison.CurrentCultureIgnoreCase, 0 };
yield return new object[] { "Hello", 0, "Hello", 0, 5, StringComparison.CurrentCultureIgnoreCase, 0 };
yield return new object[] { "Hello", 2, "Hello", 2, 3, StringComparison.CurrentCultureIgnoreCase, 0 };
yield return new object[] { "Hello", 2, "Yellow", 2, 3, StringComparison.CurrentCultureIgnoreCase, 0 };
yield return new object[] { "Hello", 0, "Goodbye", 0, 5, StringComparison.CurrentCultureIgnoreCase, 1 };
yield return new object[] { "Goodbye", 0, "Hello", 0, 5, StringComparison.CurrentCultureIgnoreCase, -1 };
yield return new object[] { "HELLO", 2, "hello", 2, 3, StringComparison.CurrentCultureIgnoreCase, 0 };
yield return new object[] { "Hello", 2, "Goodbye", 2, 3, StringComparison.CurrentCultureIgnoreCase, -1 };
yield return new object[] { null, 0, null, 0, 0, StringComparison.CurrentCultureIgnoreCase, 0 };
yield return new object[] { "Hello", 0, null, 0, 0, StringComparison.CurrentCultureIgnoreCase, 1 };
yield return new object[] { null, 0, "Hello", 0, 0, StringComparison.CurrentCultureIgnoreCase, -1 };
yield return new object[] { null, -1, null, -1, -1, StringComparison.CurrentCultureIgnoreCase, 0 };
yield return new object[] { "foo", -1, null, -1, -1, StringComparison.CurrentCultureIgnoreCase, 1 };
yield return new object[] { null, -1, "foo", -1, -1, StringComparison.CurrentCultureIgnoreCase, -1 };
// InvariantCulture
yield return new object[] { "", 0, "", 0, 0, StringComparison.InvariantCulture, 0 };
yield return new object[] { "Hello", 0, "Hello", 0, 5, StringComparison.InvariantCulture, 0 };
yield return new object[] { "Hello", 2, "Hello", 3, 1, StringComparison.InvariantCulture, 0 };
yield return new object[] { "Hello", 0, "Goodbye", 0, 5, StringComparison.InvariantCulture, 1 };
yield return new object[] { "Goodbye", 0, "Hello", 0, 5, StringComparison.InvariantCulture, -1 };
yield return new object[] { "HELLO", 2, "hello", 2, 3, StringComparison.InvariantCulture, PlatformDetection.IsInvariantGlobalization ? -1 : 1 };
yield return new object[] { "hello", 2, "HELLO", 2, 3, StringComparison.InvariantCulture, PlatformDetection.IsInvariantGlobalization ? 1 : -1 };
yield return new object[] { null, 0, null, 0, 0, StringComparison.InvariantCulture, 0 };
yield return new object[] { "Hello", 0, null, 0, 5, StringComparison.InvariantCulture, 1 };
yield return new object[] { null, 0, "Hello", 0, 5, StringComparison.InvariantCulture, -1 };
// InvariantCultureIgnoreCase
yield return new object[] { "", 0, "", 0, 0, StringComparison.InvariantCultureIgnoreCase, 0 };
yield return new object[] { "HELLO", 0, "hello", 0, 5, StringComparison.InvariantCultureIgnoreCase, 0 };
yield return new object[] { "Hello", 0, "Hello", 0, 5, StringComparison.InvariantCultureIgnoreCase, 0 };
yield return new object[] { "Hello", 2, "Hello", 3, 1, StringComparison.InvariantCultureIgnoreCase, 0 };
yield return new object[] { "Hello", 2, "Hello", 2, 3, StringComparison.InvariantCultureIgnoreCase, 0 };
yield return new object[] { "Hello", 2, "Yellow", 2, 3, StringComparison.InvariantCultureIgnoreCase, 0 };
yield return new object[] { "Hello", 0, "Goodbye", 0, 5, StringComparison.InvariantCultureIgnoreCase, 1 };
yield return new object[] { "Goodbye", 0, "Hello", 0, 5, StringComparison.InvariantCultureIgnoreCase, -1 };
yield return new object[] { "HELLO", 2, "hello", 2, 3, StringComparison.InvariantCultureIgnoreCase, 0 };
yield return new object[] { "Hello", 2, "Goodbye", 2, 3, StringComparison.InvariantCultureIgnoreCase, -1 };
yield return new object[] { null, 0, null, 0, 0, StringComparison.InvariantCultureIgnoreCase, 0 };
yield return new object[] { "Hello", 0, null, 0, 5, StringComparison.InvariantCultureIgnoreCase, 1 };
yield return new object[] { null, 0, "Hello", 0, 5, StringComparison.InvariantCultureIgnoreCase, -1 };
// Ordinal
yield return new object[] { "", 0, "", 0, 0, StringComparison.Ordinal, 0 };
yield return new object[] { "Hello", 0, "Hello", 0, 5, StringComparison.Ordinal, 0 };
yield return new object[] { "Hello", 2, "Hello", 3, 1, StringComparison.Ordinal, 0 };
yield return new object[] { "Hello", 0, "Goodbye", 0, 5, StringComparison.Ordinal, 1 };
yield return new object[] { "Goodbye", 0, "Hello", 0, 5, StringComparison.Ordinal, -1 };
yield return new object[] { "Hello", 2, "Hello", 2, 3, StringComparison.Ordinal, 0 };
yield return new object[] { "HELLO", 2, "hello", 2, 3, StringComparison.Ordinal, -1 };
yield return new object[] { "Hello", 2, "Goodbye", 2, 3, StringComparison.Ordinal, -1 };
yield return new object[] { "Hello", 0, "Hello", 0, 0, StringComparison.Ordinal, 0 };
yield return new object[] { "Hello", 0, "Hello", 0, 3, StringComparison.Ordinal, 0 };
yield return new object[] { "Hello", 0, "He" + SoftHyphen + "llo", 0, 5, StringComparison.Ordinal, -1 };
yield return new object[] { "Hello", 0, "-=<Hello>=-", 3, 5, StringComparison.Ordinal, 0 };
yield return new object[] { "\uD83D\uDD53Hello\uD83D\uDD50", 1, "\uD83D\uDD53Hello\uD83D\uDD54", 1, 7, StringComparison.Ordinal, 0 }; // Surrogate split
yield return new object[] { "Hello", 0, "Hello123", 0, int.MaxValue, StringComparison.Ordinal, -1 }; // Recalculated length, second string longer
yield return new object[] { "Hello123", 0, "Hello", 0, int.MaxValue, StringComparison.Ordinal, 1 }; // Recalculated length, first string longer
yield return new object[] { "---aaaaaaaaaaa", 3, "+++aaaaaaaaaaa", 3, 100, StringComparison.Ordinal, 0 }; // Equal long alignment 2, equal compare
yield return new object[] { "aaaaaaaaaaaaaa", 3, "aaaxaaaaaaaaaa", 3, 100, StringComparison.Ordinal, -1 }; // Equal long alignment 2, different compare at n=1
yield return new object[] { "-aaaaaaaaaaaaa", 1, "+aaaaaaaaaaaaa", 1, 100, StringComparison.Ordinal, 0 }; // Equal long alignment 6, equal compare
yield return new object[] { "aaaaaaaaaaaaaa", 1, "axaaaaaaaaaaaa", 1, 100, StringComparison.Ordinal, -1 }; // Equal long alignment 6, different compare at n=1
yield return new object[] { "aaaaaaaaaaaaaa", 0, "aaaaaaaaaaaaaa", 0, 100, StringComparison.Ordinal, 0 }; // Equal long alignment 4, equal compare
yield return new object[] { "aaaaaaaaaaaaaa", 0, "xaaaaaaaaaaaaa", 0, 100, StringComparison.Ordinal, -1 }; // Equal long alignment 4, different compare at n=1
yield return new object[] { "aaaaaaaaaaaaaa", 0, "axaaaaaaaaaaaa", 0, 100, StringComparison.Ordinal, -1 }; // Equal long alignment 4, different compare at n=2
yield return new object[] { "--aaaaaaaaaaaa", 2, "++aaaaaaaaaaaa", 2, 100, StringComparison.Ordinal, 0 }; // Equal long alignment 0, equal compare
yield return new object[] { "aaaaaaaaaaaaaa", 2, "aaxaaaaaaaaaaa", 2, 100, StringComparison.Ordinal, -1 }; // Equal long alignment 0, different compare at n=1
yield return new object[] { "aaaaaaaaaaaaaa", 2, "aaaxaaaaaaaaaa", 2, 100, StringComparison.Ordinal, -1 }; // Equal long alignment 0, different compare at n=2
yield return new object[] { "aaaaaaaaaaaaaa", 2, "aaaaxaaaaaaaaa", 2, 100, StringComparison.Ordinal, -1 }; // Equal long alignment 0, different compare at n=3
yield return new object[] { "aaaaaaaaaaaaaa", 2, "aaaaaxaaaaaaaa", 2, 100, StringComparison.Ordinal, -1 }; // Equal long alignment 0, different compare at n=4
yield return new object[] { "aaaaaaaaaaaaaa", 2, "aaaaaaxaaaaaaa", 2, 100, StringComparison.Ordinal, -1 }; // Equal long alignment 0, different compare at n=5
yield return new object[] { "aaaaaaaaaaaaaa", 0, "+aaaaaaaaaaaaa", 1, 13, StringComparison.Ordinal, 0 }; // Different int alignment, equal compare
yield return new object[] { "aaaaaaaaaaaaaa", 0, "aaaaaaaaaaaaax", 1, 100, StringComparison.Ordinal, -1 }; // Different int alignment
yield return new object[] { "aaaaaaaaaaaaaa", 1, "aaaxaaaaaaaaaa", 3, 100, StringComparison.Ordinal, -1 }; // Different long alignment, abs of 4, one of them is 2, different at n=1
yield return new object[] { "-aaaaaaaaaaaaa", 1, "++++aaaaaaaaaa", 4, 10, StringComparison.Ordinal, 0 }; // Different long alignment, equal compare
yield return new object[] { "aaaaaaaaaaaaaa", 1, "aaaaaaaaaaaaax", 4, 100, StringComparison.Ordinal, -1 }; // Different long alignment
yield return new object[] { "\0", 0, "", 0, 1, StringComparison.Ordinal, 1 }; // Same memory layout, except for m_stringLength (m_firstChars are both 0)
yield return new object[] { "\0\0", 0, "", 0, 2, StringComparison.Ordinal, 1 }; // Same as above, except m_stringLength for one is 2
yield return new object[] { "", 0, "\0b", 0, 2, StringComparison.Ordinal, -1 }; // strA's second char != strB's second char codepath
yield return new object[] { "", 0, "b", 0, 1, StringComparison.Ordinal, -1 }; // Should hit strA.m_firstChar != strB.m_firstChar codepath
yield return new object[] { "abcxxxxxxxxxxxxxxxxxxxxxx", 0, "abdxxxxxxxxxxxxxxx", 0, int.MaxValue, StringComparison.Ordinal, -1 }; // 64-bit: first long compare is different
yield return new object[] { "abcdefgxxxxxxxxxxxxxxxxxx", 0, "abcdefhxxxxxxxxxxx", 0, int.MaxValue, StringComparison.Ordinal, -1 }; // 64-bit: second long compare is different
yield return new object[] { "abcdefghijkxxxxxxxxxxxxxx", 0, "abcdefghijlxxxxxxx", 0, int.MaxValue, StringComparison.Ordinal, -1 }; // 64-bit: third long compare is different
yield return new object[] { "abcdexxxxxxxxxxxxxxxxxxxx", 0, "abcdfxxxxxxxxxxxxx", 0, int.MaxValue, StringComparison.Ordinal, -1 }; // 32-bit: second int compare is different
yield return new object[] { "abcdefghixxxxxxxxxxxxxxxx", 0, "abcdefghjxxxxxxxxx", 0, int.MaxValue, StringComparison.Ordinal, -1 }; // 32-bit: fourth int compare is different
yield return new object[] { null, 0, null, 0, 0, StringComparison.Ordinal, 0 };
yield return new object[] { "Hello", 0, null, 0, 5, StringComparison.Ordinal, 1 };
yield return new object[] { null, 0, "Hello", 0, 5, StringComparison.Ordinal, -1 };
yield return new object[] { null, -1, null, -1, -1, StringComparison.Ordinal, 0 };
yield return new object[] { "foo", -1, null, -1, -1, StringComparison.Ordinal, 1 };
yield return new object[] { null, -1, "foo", -1, -1, StringComparison.Ordinal, -1 };
// OrdinalIgnoreCase
yield return new object[] { "", 0, "", 0, 0, StringComparison.OrdinalIgnoreCase, 0 };
yield return new object[] { "HELLO", 0, "hello", 0, 5, StringComparison.OrdinalIgnoreCase, 0 };
yield return new object[] { "Hello", 0, "Hello", 0, 5, StringComparison.OrdinalIgnoreCase, 0 };
yield return new object[] { "Hello", 2, "Hello", 3, 1, StringComparison.OrdinalIgnoreCase, 0 };
yield return new object[] { "Hello", 2, "Hello", 2, 3, StringComparison.OrdinalIgnoreCase, 0 };
yield return new object[] { "Hello", 2, "Yellow", 2, 3, StringComparison.OrdinalIgnoreCase, 0 };
yield return new object[] { "Hello", 0, "Goodbye", 0, 5, StringComparison.OrdinalIgnoreCase, 1 };
yield return new object[] { "Goodbye", 0, "Hello", 0, 5, StringComparison.OrdinalIgnoreCase, -1 };
yield return new object[] { "HELLO", 2, "hello", 2, 3, StringComparison.OrdinalIgnoreCase, 0 };
yield return new object[] { "Hello", 2, "Goodbye", 2, 3, StringComparison.OrdinalIgnoreCase, -1 };
yield return new object[] { "A", 0, "x", 0, 1, StringComparison.OrdinalIgnoreCase, -1 };
yield return new object[] { "a", 0, "X", 0, 1, StringComparison.OrdinalIgnoreCase, -1 };
yield return new object[] { "[", 0, "A", 0, 1, StringComparison.OrdinalIgnoreCase, 1 };
yield return new object[] { "[", 0, "a", 0, 1, StringComparison.OrdinalIgnoreCase, 1 };
yield return new object[] { "\\", 0, "A", 0, 1, StringComparison.OrdinalIgnoreCase, 1 };
yield return new object[] { "\\", 0, "a", 0, 1, StringComparison.OrdinalIgnoreCase, 1 };
yield return new object[] { "]", 0, "A", 0, 1, StringComparison.OrdinalIgnoreCase, 1 };
yield return new object[] { "]", 0, "a", 0, 1, StringComparison.OrdinalIgnoreCase, 1 };
yield return new object[] { "^", 0, "A", 0, 1, StringComparison.OrdinalIgnoreCase, 1 };
yield return new object[] { "^", 0, "a", 0, 1, StringComparison.OrdinalIgnoreCase, 1 };
yield return new object[] { "_", 0, "A", 0, 1, StringComparison.OrdinalIgnoreCase, 1 };
yield return new object[] { "_", 0, "a", 0, 1, StringComparison.OrdinalIgnoreCase, 1 };
yield return new object[] { "`", 0, "A", 0, 1, StringComparison.OrdinalIgnoreCase, 1 };
yield return new object[] { "`", 0, "a", 0, 1, StringComparison.OrdinalIgnoreCase, 1 };
yield return new object[] { null, 0, null, 0, 0, StringComparison.OrdinalIgnoreCase, 0 };
yield return new object[] { "Hello", 0, null, 0, 5, StringComparison.OrdinalIgnoreCase, 1 };
yield return new object[] { null, 0, "Hello", 0, 5, StringComparison.OrdinalIgnoreCase, -1 };
}
[Theory]
[MemberData(nameof(Compare_TestData))]
public static void Compare(string strA, int indexA, string strB, int indexB, int length, StringComparison comparisonType, int expected)
{
bool hasNullInputs = (strA == null || strB == null);
bool indicesReferToEntireString = (strA != null && strB != null && indexA == 0 && indexB == 0 && (length == strB.Length || length == strA.Length));
bool skipNonComparisonOverloads = length != 0 && ((strA == null && indexA != 0) || (strB == null && indexB != 0));
if (hasNullInputs || indicesReferToEntireString)
{
if (comparisonType == StringComparison.CurrentCulture)
{
// Use Compare(string, string) or Compare(string, string, false) or CompareTo(string)
Assert.Equal(expected, Math.Sign(string.Compare(strA, strB)));
Assert.Equal(expected, Math.Sign(string.Compare(strA, strB, ignoreCase: false)));
if (strA != null)
{
Assert.Equal(expected, Math.Sign(strA.CompareTo(strB)));
IComparable iComparable = strA;
Assert.Equal(expected, Math.Sign(iComparable.CompareTo(strB)));
}
if (strB != null)
{
Assert.Equal(expected, -Math.Sign(strB.CompareTo(strA)));
IComparable iComparable = strB;
Assert.Equal(expected, -Math.Sign(iComparable.CompareTo(strA)));
}
}
else if (comparisonType == StringComparison.CurrentCultureIgnoreCase)
{
// Use Compare(string, string, true)
Assert.Equal(expected, Math.Sign(string.Compare(strA, strB, ignoreCase: true)));
}
else if (comparisonType == StringComparison.Ordinal)
{
// Use CompareOrdinal(string, string)
Assert.Equal(expected, Math.Sign(string.CompareOrdinal(strA, strB)));
}
// Use CompareOrdinal(string, string, StringComparison)
Assert.Equal(expected, Math.Sign(string.Compare(strA, strB, comparisonType)));
}
if (comparisonType == StringComparison.CurrentCulture)
{
// This may have different behavior than the overload accepting a StringComparison
// for a combination of null/invalid inputs; see notes in Compare_Invalid for more
if (!skipNonComparisonOverloads)
{
// Use Compare(string, int, string, int, int) or Compare(string, int, string, int, int, false)
Assert.Equal(expected, Math.Sign(string.Compare(strA, indexA, strB, indexB, length)));
Assert.Equal(expected, Math.Sign(string.Compare(strA, indexA, strB, indexB, length, ignoreCase: false)));
}
}
else if (comparisonType == StringComparison.CurrentCultureIgnoreCase)
{
// This may have different behavior than the overload accepting a StringComparison
// for a combination of null/invalid inputs; see notes in Compare_Invalid for more
if (!skipNonComparisonOverloads)
{
// Use Compare(string, int, string, int, int, true)
Assert.Equal(expected, Math.Sign(string.Compare(strA, indexA, strB, indexB, length, ignoreCase: true)));
}
}
else if (comparisonType == StringComparison.Ordinal)
{
// Use CompareOrdinal(string, int, string, int, int)
Assert.Equal(expected, Math.Sign(string.CompareOrdinal(strA, indexA, strB, indexB, length)));
}
// Use Compare(string, int, string, int, int, StringComparison)
Assert.Equal(expected, Math.Sign(string.Compare(strA, indexA, strB, indexB, length, comparisonType)));
if (indexA >= 0 && indexB >= 0 && length >= 0)
{
// Comparing spans from null strings gives different results since span doesn't special case null and treats it the same as an empty string.
if (strA == null && strB != null)
expected = -1;
if (strA != null && strB == null)
expected = 1;
if (length == 0)
expected = 0;
ReadOnlySpan<char> span = length <= (strA.AsSpan().Length - indexA) ? strA.AsSpan(indexA, length) : strA.AsSpan(indexA);
ReadOnlySpan<char> value = length <= (strB.AsSpan().Length - indexB) ? strB.AsSpan(indexB, length) : strB.AsSpan(indexB);
Assert.Equal(expected, Math.Sign(span.CompareTo(value, comparisonType)));
}
}
[Fact]
public static void Compare_LongString()
{
string veryLongString =
"<NamedPermissionSets><PermissionSet class=\u0022System.Security.NamedPermissionS" +
"et\u0022version=\u00221\u0022 Unrestricted=\u0022true\u0022 Name=\u0022FullTrust" +
"\u0022 Description=\u0022{Policy_PS_FullTrust}\u0022/><PermissionSet class=\u0022" +
"System.Security.NamedPermissionSet\u0022version=\u00221\u0022 Name=\u0022Everyth" +
"ing\u0022 Description=\u0022{Policy_PS_Everything}\u0022><Permission class=\u0022" +
"System.Security.Permissions.IsolatedStorageFilePermission, mscorlib, Version={VE" +
"RSION}, Culture=neutral, PublicKeyToken=b77a5c561934e089\u0022version=\u00221\u0022" +
" Unrestricted=\u0022true\u0022/><Permission class=\u0022System.Security.Permissi" +
"ons.EnvironmentPermission, mscorlib, Version={VERSION}, Culture=neutral, PublicK" +
"eyToken=b77a5c561934e089\u0022version=\u00221\u0022 Unrestricted=\u0022true\u0022" +
"/><Permission class=\u0022System.Security.Permissions.FileIOPermission, mscorlib" +
", Version={VERSION}, Culture=neutral, PublicKeyToken=b77a5c561934e089\u0022versi" +
"on=\u00221\u0022 Unrestricted=\u0022true\u0022/><Permission class=\u0022System.S" +
"ecurity.Permissions.FileDialogPermission, mscorlib, Version={VERSION}, Culture=n" +
"eutral, PublicKeyToken=b77a5c561934e089\u0022version=\u00221\u0022 Unrestricted=" +
"\u0022true\u0022/><Permission class=\u0022System.Security.Permissions.Reflection" +
"Permission, mscorlib, Version={VERSION}, Culture=neutral, PublicKeyToken=b77a5c5" +
"61934e089\u0022version=\u00221\u0022 Unrestricted=\u0022true\u0022/><Permission " +
"class=\u0022System.Security.Permissions.SecurityPermission, mscorlib, Version={V" +
"ERSION}, Culture=neutral, PublicKeyToken=b77a5c561934e089\u0022version=\u00221\u0022" +
" Flags=\u0022Assertion, UnmanagedCode, Execution, ControlThread, ControlEvidence" +
", ControlPolicy, ControlAppDomain, SerializationFormatter, ControlDomainPolicy, " +
"ControlPrincipal, RemotingConfiguration, Infrastructure, BindingRedirects\u0022/" +
"><Permission class=\u0022System.Security.Permissions.UIPermission, mscorlib, Ver" +
"sion={VERSION}, Culture=neutral, PublicKeyToken=b77a5c561934e089\u0022version=\u0022" +
"1\u0022 Unrestricted=\u0022true\u0022/><IPermission class=\u0022System.Net.Socke" +
"tPermission, System, Version={VERSION}, Culture=neutral, PublicKeyToken=b77a5c56" +
"1934e089\u0022version=\u00221\u0022 Unrestricted=\u0022true\u0022/><IPermission " +
"class=\u0022System.Net.WebPermission, System, Version={VERSION}, Culture=neutral" +
", PublicKeyToken=b77a5c561934e089\u0022version=\u00221\u0022 Unrestricted=\u0022" +
"true\u0022/><IPermission class=\u0022System.Net.DnsPermission, System, Version={" +
"VERSION}, Culture=neutral, PublicKeyToken=b77a5c561934e089\u0022version=\u00221\u0022" +
" Unrestricted=\u0022true\u0022/><IPermission class=\u0022System.Security.Permiss" +
"ions.KeyContainerPermission, mscorlib, Version={VERSION}, Culture=neutral, Publi" +
"cKeyToken=b77a5c561934e089\u0022version=\u00221\u0022 Unrestricted=\u0022true\u0022" +
"/><Permission class=\u0022System.Security.Permissions.RegistryPermission, mscorl" +
"ib, Version={VERSION}, Culture=neutral, PublicKeyToken=b77a5c561934e089\u0022ver" +
"sion=\u00221\u0022 Unrestricted=\u0022true\u0022/><IPermission class=\u0022Syste" +
"m.Drawing.Printing.PrintingPermission, System.Drawing, Version={VERSION}, Cultur" +
"e=neutral, PublicKeyToken=b03f5f7f11d50a3a\u0022version=\u00221\u0022 Unrestrict" +
"ed=\u0022true\u0022/><IPermission class=\u0022System.Diagnostics.EventLogPermiss" +
"ion, System, Version={VERSION}, Culture=neutral, PublicKeyToken=b77a5c561934e089" +
"\u0022version=\u00221\u0022 Unrestricted=\u0022true\u0022/><IPermission class=\u0022" +
"System.Security.Permissions.StorePermission, System, Version={VERSION}, Culture=" +
"neutral, PublicKeyToken=b77a5c561934e089\u0022 version=\u00221\u0022 Unrestricte" +
"d=\u0022true\u0022/><IPermission class=\u0022System.Diagnostics.PerformanceCount" +
"erPermission, System, Version={VERSION}, Culture=neutral, PublicKeyToken=b77a5c5" +
"61934e089\u0022version=\u00221\u0022 Unrestricted=\u0022true\u0022/><IPermission" +
" class=\u0022System.Data.OleDb.OleDbPermission, System.Data, Version={VERSION}, " +
"Culture=neutral, PublicKeyToken=b77a5c561934e089\u0022 version=\u00221\u0022 Unr" +
"estricted=\u0022true\u0022/><IPermission class=\u0022System.Data.SqlClient.SqlCl" +
"ientPermission, System.Data, Version={VERSION}, Culture=neutral, PublicKeyToken=" +
"b77a5c561934e089\u0022 version=\u00221\u0022 Unrestricted=\u0022true\u0022/><IPe" +
"rmission class=\u0022System.Security.Permissions.DataProtectionPermission, Syste" +
"m.Security, Version={VERSION}, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\u0022" +
" version=\u00221\u0022 Unrestricted=\u0022true\u0022/></PermissionSet><Permissio" +
"nSet class=\u0022System.Security.NamedPermissionSet\u0022version=\u00221\u0022 N" +
"ame=\u0022Nothing\u0022 Description=\u0022{Policy_PS_Nothing}\u0022/><Permission" +
"Set class=\u0022System.Security.NamedPermissionSet\u0022version=\u00221\u0022 Na" +
"me=\u0022Execution\u0022 Description=\u0022{Policy_PS_Execution}\u0022><Permissi" +
"on class=\u0022System.Security.Permissions.SecurityPermission, mscorlib, Version" +
"={VERSION}, Culture=neutral, PublicKeyToken=b77a5c561934e089\u0022version=\u0022" +
"1\u0022 Flags=\u0022Execution\u0022/></PermissionSet><PermissionSet class=\u0022" +
"System.Security.NamedPermissionSet\u0022version=\u00221\u0022 Name=\u0022SkipVer" +
"ification\u0022 Description=\u0022{Policy_PS_SkipVerification}\u0022><Permission" +
" class=\u0022System.Security.Permissions.SecurityPermission, mscorlib, Version={" +
"VERSION}, Culture=neutral, PublicKeyToken=b77a5c561934e089\u0022version=\u00221\u0022" +
" Flags=\u0022SkipVerification\u0022/></PermissionSet></NamedPermissionSets>";
int result = string.Compare("{Policy_PS_Nothing}", 0, veryLongString, 4380, 19, StringComparison.Ordinal);
Assert.True(result < 0);
result = "{Policy_PS_Nothing}".AsSpan().CompareTo(veryLongString.AsSpan(4380, 19), StringComparison.Ordinal);
Assert.True(result < 0);
}
[Fact]
public static void ZeroLengthCompareTo_StringComparison()
{
string value = "456";
string s = value.Substring(2, 0);
Assert.True(0 < string.Compare(value, s, StringComparison.Ordinal));
Assert.True(0 < string.Compare(value, s, StringComparison.CurrentCulture));
Assert.True(0 < string.Compare(value, s, StringComparison.CurrentCultureIgnoreCase));
Assert.True(0 < string.Compare(value, s, StringComparison.InvariantCulture));
Assert.True(0 < string.Compare(value, s, StringComparison.InvariantCultureIgnoreCase));
Assert.True(0 < string.Compare(value, s, StringComparison.OrdinalIgnoreCase));
string emptyValue = value.Substring(1, 0);
Assert.Equal(0, string.Compare(emptyValue, s, StringComparison.Ordinal));
Assert.Equal(0, string.Compare(emptyValue, s, StringComparison.CurrentCulture));
Assert.Equal(0, string.Compare(emptyValue, s, StringComparison.CurrentCultureIgnoreCase));
Assert.Equal(0, string.Compare(emptyValue, s, StringComparison.InvariantCulture));
Assert.Equal(0, string.Compare(emptyValue, s, StringComparison.InvariantCultureIgnoreCase));
Assert.Equal(0, string.Compare(emptyValue, s, StringComparison.OrdinalIgnoreCase));
ReadOnlySpan<char> span = value.AsSpan();
ReadOnlySpan<char> emptySlice = value.AsSpan(2, 0);
Assert.True(0 < span.CompareTo(emptySlice, StringComparison.Ordinal));
Assert.True(0 < span.CompareTo(emptySlice, StringComparison.CurrentCulture));
Assert.True(0 < span.CompareTo(emptySlice, StringComparison.CurrentCultureIgnoreCase));
Assert.True(0 < span.CompareTo(emptySlice, StringComparison.InvariantCulture));
Assert.True(0 < span.CompareTo(emptySlice, StringComparison.InvariantCultureIgnoreCase));
Assert.True(0 < span.CompareTo(emptySlice, StringComparison.OrdinalIgnoreCase));
span = value.AsSpan(1, 0);
Assert.Equal(0, span.CompareTo(emptySlice, StringComparison.Ordinal));
Assert.Equal(0, span.CompareTo(emptySlice, StringComparison.CurrentCulture));
Assert.Equal(0, span.CompareTo(emptySlice, StringComparison.CurrentCultureIgnoreCase));
Assert.Equal(0, span.CompareTo(emptySlice, StringComparison.InvariantCulture));
Assert.Equal(0, span.CompareTo(emptySlice, StringComparison.InvariantCultureIgnoreCase));
Assert.Equal(0, span.CompareTo(emptySlice, StringComparison.OrdinalIgnoreCase));
}
[Fact]
public static void SameValueCompareTo_StringComparison()
{
string value = "456";
Assert.Equal(0, string.Compare(value, value, StringComparison.Ordinal));
Assert.Equal(0, string.Compare(value, value, StringComparison.CurrentCulture));
Assert.Equal(0, string.Compare(value, value, StringComparison.CurrentCultureIgnoreCase));
Assert.Equal(0, string.Compare(value, value, StringComparison.InvariantCulture));
Assert.Equal(0, string.Compare(value, value, StringComparison.InvariantCultureIgnoreCase));
Assert.Equal(0, string.Compare(value, value, StringComparison.OrdinalIgnoreCase));
ReadOnlySpan<char> span = value.AsSpan();
Assert.Equal(0, span.CompareTo(span, StringComparison.Ordinal));
Assert.Equal(0, span.CompareTo(span, StringComparison.CurrentCulture));
Assert.Equal(0, span.CompareTo(span, StringComparison.CurrentCultureIgnoreCase));
Assert.Equal(0, span.CompareTo(span, StringComparison.InvariantCulture));
Assert.Equal(0, span.CompareTo(span, StringComparison.InvariantCultureIgnoreCase));
Assert.Equal(0, span.CompareTo(span, StringComparison.OrdinalIgnoreCase));
}
[Fact]
public static void LengthMismatchCompareTo_StringComparison()
{
string value = "456";
string s1 = value.Substring(0, 2);
string s2 = value.Substring(0, 3);
Assert.True(0 > string.Compare(s1, s2, StringComparison.Ordinal));
Assert.True(0 > string.Compare(s1, s2, StringComparison.CurrentCulture));
Assert.True(0 > string.Compare(s1, s2, StringComparison.CurrentCultureIgnoreCase));
Assert.True(0 > string.Compare(s1, s2, StringComparison.InvariantCulture));
Assert.True(0 > string.Compare(s1, s2, StringComparison.InvariantCultureIgnoreCase));
Assert.True(0 > string.Compare(s1, s2, StringComparison.OrdinalIgnoreCase));
ReadOnlySpan<char> span = value.AsSpan(0, 2);
ReadOnlySpan<char> slice = value.AsSpan(0, 3);
Assert.True(0 > span.CompareTo(slice, StringComparison.Ordinal));
Assert.True(0 > span.CompareTo(slice, StringComparison.CurrentCulture));
Assert.True(0 > span.CompareTo(slice, StringComparison.CurrentCultureIgnoreCase));
Assert.True(0 > span.CompareTo(slice, StringComparison.InvariantCulture));
Assert.True(0 > span.CompareTo(slice, StringComparison.InvariantCultureIgnoreCase));
Assert.True(0 > span.CompareTo(slice, StringComparison.OrdinalIgnoreCase));
}
[Fact]
public static void CompareToOverlappingMatch_StringComparison()
{
string value = "456565";
string s1 = value.Substring(1, 3);
string s2 = value.Substring(3, 3);
Assert.Equal(0, string.Compare(s1, s2, StringComparison.Ordinal));
Assert.Equal(0, string.Compare(s1, s2, StringComparison.CurrentCulture));
Assert.Equal(0, string.Compare(s1, s2, StringComparison.CurrentCultureIgnoreCase));
Assert.Equal(0, string.Compare(s1, s2, StringComparison.InvariantCulture));
Assert.Equal(0, string.Compare(s1, s2, StringComparison.InvariantCultureIgnoreCase));
Assert.Equal(0, string.Compare(s1, s2, StringComparison.OrdinalIgnoreCase));
ReadOnlySpan<char> span = value.AsSpan(1, 3);
ReadOnlySpan<char> slice = value.AsSpan(3, 3);
Assert.Equal(0, span.CompareTo(slice, StringComparison.Ordinal));
Assert.Equal(0, span.CompareTo(slice, StringComparison.CurrentCulture));
Assert.Equal(0, span.CompareTo(slice, StringComparison.CurrentCultureIgnoreCase));
Assert.Equal(0, span.CompareTo(slice, StringComparison.InvariantCulture));
Assert.Equal(0, span.CompareTo(slice, StringComparison.InvariantCultureIgnoreCase));
Assert.Equal(0, span.CompareTo(slice, StringComparison.OrdinalIgnoreCase));
}
[Fact]
public static void CompareToMatchDifferentInstances_StringComparison()
{
string sa = "4567";
string sb = "456";
string s1 = sa.Substring(0, 3);
string s2 = sb.Substring(0, 3);
Assert.Equal(0, string.Compare(s1, s2, StringComparison.Ordinal));
Assert.Equal(0, string.Compare(s1, s2, StringComparison.CurrentCulture));
Assert.Equal(0, string.Compare(s1, s2, StringComparison.CurrentCultureIgnoreCase));
Assert.Equal(0, string.Compare(s1, s2, StringComparison.InvariantCulture));
Assert.Equal(0, string.Compare(s1, s2, StringComparison.InvariantCultureIgnoreCase));
Assert.Equal(0, string.Compare(s1, s2, StringComparison.OrdinalIgnoreCase));
ReadOnlySpan<char> span = sa.AsSpan(0, 3);
ReadOnlySpan<char> slice = sb.AsSpan(0, 3);
Assert.Equal(0, span.CompareTo(slice, StringComparison.Ordinal));
Assert.Equal(0, span.CompareTo(slice, StringComparison.CurrentCulture));
Assert.Equal(0, span.CompareTo(slice, StringComparison.CurrentCultureIgnoreCase));
Assert.Equal(0, span.CompareTo(slice, StringComparison.InvariantCulture));
Assert.Equal(0, span.CompareTo(slice, StringComparison.InvariantCultureIgnoreCase));
Assert.Equal(0, span.CompareTo(slice, StringComparison.OrdinalIgnoreCase));
}
[Fact]
public static void MakeSureNoCompareToChecksGoOutOfRange_StringComparison()
{
for (int length = 0; length < 100; length++)
{
var first = new char[length + 2];
first[0] = (char)99;
first[length + 1] = (char)99;
var second = new char[length + 2];
second[0] = (char)100;
second[length + 1] = (char)100;
var s1 = new string(first, 1, length);
var s2 = new string(second, 1, length);
Assert.Equal(0, string.Compare(s1, s2, StringComparison.Ordinal));
Assert.Equal(0, string.Compare(s1, s2, StringComparison.CurrentCulture));
Assert.Equal(0, string.Compare(s1, s2, StringComparison.CurrentCultureIgnoreCase));
Assert.Equal(0, string.Compare(s1, s2, StringComparison.InvariantCulture));
Assert.Equal(0, string.Compare(s1, s2, StringComparison.InvariantCultureIgnoreCase));
Assert.Equal(0, string.Compare(s1, s2, StringComparison.OrdinalIgnoreCase));