-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
ArrayTests.cs
4811 lines (4098 loc) · 282 KB
/
ArrayTests.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.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using Microsoft.DotNet.XUnitExtensions;
using Xunit;
using Xunit.Abstractions;
namespace System.Tests
{
public class ArrayTests
{
[Fact]
public static void IList_GetSetItem()
{
var intArray = new int[] { 7, 8, 9, 10, 11, 12, 13 };
IList<int> iList = intArray;
Assert.Equal(intArray.Length, iList.Count);
for (int i = 0; i < iList.Count; i++)
{
Assert.Equal(intArray[i], iList[i]);
iList[i] = 99;
Assert.Equal(99, iList[i]);
Assert.Equal(99, intArray[i]);
}
}
[Fact]
public static void IList_GetSetItem_Invalid()
{
IList iList = new int[] { 7, 8, 9, 10, 11, 12, 13 };
Assert.Throws<IndexOutOfRangeException>(() => iList[-1]); // Index < 0
Assert.Throws<IndexOutOfRangeException>(() => iList[iList.Count]); // Index >= list.Count
Assert.Throws<IndexOutOfRangeException>(() => iList[-1] = 0); // Index < 0
Assert.Throws<IndexOutOfRangeException>(() => iList[iList.Count] = 0); // Index >= list.Count
iList = new int[,] { { 1 }, { 2 } };
AssertExtensions.Throws<ArgumentException>(null, () => iList[0]); // Array is multidimensional
AssertExtensions.Throws<ArgumentException>(null, () => iList[0] = 0); // Array is multidimensional
}
[Fact]
public static void IList_ModifyingArray_ThrowsNotSupportedException()
{
var array = new int[] { 7, 8, 9, 10, 11, 12, 13 };
IList<int> iList = array;
Assert.True(iList.IsReadOnly);
Assert.True(((IList)array).IsFixedSize);
Assert.Throws<NotSupportedException>(() => iList.Add(2));
Assert.Throws<NotSupportedException>(() => iList.Insert(0, 0));
Assert.Throws<NotSupportedException>(() => iList.Clear());
Assert.Throws<NotSupportedException>(() => iList.Remove(2));
Assert.Throws<NotSupportedException>(() => iList.RemoveAt(2));
}
[Fact]
public static void CastAs_IListOfT()
{
var sa = new string[] { "Hello", "There" };
Assert.True(sa is IList<string>);
IList<string> ils = sa;
Assert.Equal(2, ils.Count);
ils[0] = "50";
Assert.Equal("50", sa[0]);
Assert.Equal(sa[1], ils[1]);
Assert.Equal("There", sa[1]);
}
[Fact]
public static void Construction()
{
// Check a number of the simple APIs on Array for dimensions up to 4.
Array array = new int[] { 1, 2, 3 };
VerifyArray(array, typeof(int), new int[] { 3 }, new int[1]);
array = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
VerifyArray(array, typeof(int), new int[] { 2, 3 }, new int[2]);
array = new int[2, 3, 4];
VerifyArray(array, typeof(int), new int[] { 2, 3, 4 }, new int[3]);
array = new int[2, 3, 4, 5];
VerifyArray(array, typeof(int), new int[] { 2, 3, 4, 5 }, new int[4]);
}
[Fact]
public static void Construction_MultiDimensionalArray()
{
// This C# initialization syntax generates some peculiar looking IL.
// Initializations of this form are handled specially on Desktop and in .NET Native by UTC.
var array = new int[,,,] { { { { 1, 2, 3 }, { 1, 2, 3 } }, { { 1, 2, 3 }, { 1, 2, 3 } } }, { { { 1, 2, 3 }, { 1, 2, 3 } }, { { 1, 2, 3 }, { 1, 2, 3 } } } };
Assert.NotNull(array);
Assert.Equal(1, array.GetValue(0, 0, 0, 0));
Assert.Equal(2, array.GetValue(0, 0, 0, 1));
Assert.Equal(3, array.GetValue(0, 0, 0, 2));
}
[Fact]
public void AsReadOnly_ValidArray_ReturnsExpected()
{
var array = new string[] { "a", "b" };
ReadOnlyCollection<string> readOnlyCollection = Array.AsReadOnly(array);
Assert.Equal(array, readOnlyCollection);
Assert.Equal(new ReadOnlyCollection<string>(array), readOnlyCollection);
}
[Fact]
public void AsReadOnly_NullArray_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("array", () => Array.AsReadOnly<int>(null));
}
public static IEnumerable<object[]> BinarySearch_SZArray_TestData()
{
string[] stringArray = new string[] { null, "aa", "bb", "bb", "cc", "dd", "ee" };
yield return new object[] { stringArray, 0, 7, "bb", null, 3 };
yield return new object[] { stringArray, 0, 7, "ee", null, 6 };
yield return new object[] { stringArray, 3, 4, "bb", null, 3 };
yield return new object[] { stringArray, 4, 3, "bb", null, -5 };
yield return new object[] { stringArray, 4, 0, "bb", null, -5 };
yield return new object[] { stringArray, 0, 7, "bb", new StringComparer(), 3 };
yield return new object[] { stringArray, 0, 7, "ee", new StringComparer(), 6 };
yield return new object[] { stringArray, 0, 7, "no-such-object", new StringComparer(), -8 };
yield return new object[] { new string[0], 0, 0, "", null, -1 };
// SByte
sbyte[] sbyteArray = new sbyte[] { sbyte.MinValue, 0, 0, sbyte.MaxValue };
yield return new object[] { sbyteArray, 0, 4, sbyte.MinValue, null, 0 };
yield return new object[] { sbyteArray, 0, 4, (sbyte)0, null, 1 };
yield return new object[] { sbyteArray, 0, 4, sbyte.MaxValue, null, 3 };
yield return new object[] { sbyteArray, 0, 4, (sbyte)1, null, -4 };
yield return new object[] { sbyteArray, 0, 1, sbyte.MinValue, null, 0 };
yield return new object[] { sbyteArray, 1, 3, sbyte.MaxValue, null, 3 };
yield return new object[] { sbyteArray, 1, 3, sbyte.MinValue, null, -2 };
yield return new object[] { sbyteArray, 1, 0, (sbyte)0, null, -2 };
yield return new object[] { new sbyte[0], 0, 0, (sbyte)0, null, -1 };
// Byte
byte[] byteArray = new byte[] { byte.MinValue, 5, 5, byte.MaxValue };
yield return new object[] { byteArray, 0, 4, byte.MinValue, null, 0 };
yield return new object[] { byteArray, 0, 4, (byte)5, null, 1 };
yield return new object[] { byteArray, 0, 4, byte.MaxValue, null, 3 };
yield return new object[] { byteArray, 0, 4, (byte)1, null, -2 };
yield return new object[] { byteArray, 0, 1, byte.MinValue, null, 0 };
yield return new object[] { byteArray, 1, 3, byte.MaxValue, null, 3 };
yield return new object[] { byteArray, 1, 3, byte.MinValue, null, -2 };
yield return new object[] { byteArray, 1, 0, (byte)5, null, -2 };
yield return new object[] { new byte[0], 0, 0, (byte)0, null, -1 };
// Int16
short[] shortArray = new short[] { short.MinValue, 0, 0, short.MaxValue };
yield return new object[] { shortArray, 0, 4, short.MinValue, null, 0 };
yield return new object[] { shortArray, 0, 4, (short)0, null, 1 };
yield return new object[] { shortArray, 0, 4, short.MaxValue, null, 3 };
yield return new object[] { shortArray, 0, 4, (short)1, null, -4 };
yield return new object[] { shortArray, 0, 1, short.MinValue, null, 0 };
yield return new object[] { shortArray, 1, 3, short.MaxValue, null, 3 };
yield return new object[] { shortArray, 1, 3, short.MinValue, null, -2 };
yield return new object[] { shortArray, 1, 0, (short)0, null, -2 };
yield return new object[] { new short[0], 0, 0, (short)0, null, -1 };
// UInt16
ushort[] ushortArray = new ushort[] { ushort.MinValue, 5, 5, ushort.MaxValue };
yield return new object[] { ushortArray, 0, 4, ushort.MinValue, null, 0 };
yield return new object[] { ushortArray, 0, 4, (ushort)5, null, 1 };
yield return new object[] { ushortArray, 0, 4, ushort.MaxValue, null, 3 };
yield return new object[] { ushortArray, 0, 4, (ushort)1, null, -2 };
yield return new object[] { ushortArray, 0, 1, ushort.MinValue, null, 0 };
yield return new object[] { ushortArray, 1, 3, ushort.MaxValue, null, 3 };
yield return new object[] { ushortArray, 1, 3, ushort.MinValue, null, -2 };
yield return new object[] { ushortArray, 1, 0, (ushort)5, null, -2 };
yield return new object[] { new ushort[0], 0, 0, (ushort)0, null, -1 };
// Int32
int[] intArray = new int[] { int.MinValue, 0, 0, int.MaxValue };
yield return new object[] { intArray, 0, 4, int.MinValue, null, 0 };
yield return new object[] { intArray, 0, 4, 0, null, 1 };
yield return new object[] { intArray, 0, 4, int.MaxValue, null, 3 };
yield return new object[] { intArray, 0, 4, 1, null, -4 };
yield return new object[] { intArray, 0, 1, int.MinValue, null, 0 };
yield return new object[] { intArray, 1, 3, int.MaxValue, null, 3 };
yield return new object[] { intArray, 1, 3, int.MinValue, null, -2 };
int[] intArray2 = new int[] { 1, 3, 6, 6, 8, 10, 12, 16 };
yield return new object[] { intArray2, 0, 8, 8, new IntegerComparer(), 4 };
yield return new object[] { intArray2, 0, 8, 6, new IntegerComparer(), 3 };
yield return new object[] { intArray2, 0, 8, 0, new IntegerComparer(), -1 };
yield return new object[] { new int[0], 0, 0, 0, null, -1 };
// UInt32
uint[] uintArray = new uint[] { uint.MinValue, 5, 5, uint.MaxValue };
yield return new object[] { uintArray, 0, 4, uint.MinValue, null, 0 };
yield return new object[] { uintArray, 0, 4, (uint)5, null, 1 };
yield return new object[] { uintArray, 0, 4, uint.MaxValue, null, 3 };
yield return new object[] { uintArray, 0, 4, (uint)1, null, -2 };
yield return new object[] { uintArray, 0, 1, uint.MinValue, null, 0 };
yield return new object[] { uintArray, 1, 3, uint.MaxValue, null, 3 };
yield return new object[] { uintArray, 1, 3, uint.MinValue, null, -2 };
yield return new object[] { uintArray, 1, 0, (uint)5, null, -2 };
yield return new object[] { new uint[0], 0, 0, (uint)0, null, -1 };
// Int64
long[] longArray = new long[] { long.MinValue, 0, 0, long.MaxValue };
yield return new object[] { longArray, 0, 4, long.MinValue, null, 0 };
yield return new object[] { longArray, 0, 4, (long)0, null, 1 };
yield return new object[] { longArray, 0, 4, long.MaxValue, null, 3 };
yield return new object[] { longArray, 0, 4, (long)1, null, -4 };
yield return new object[] { longArray, 0, 1, long.MinValue, null, 0 };
yield return new object[] { longArray, 1, 3, long.MaxValue, null, 3 };
yield return new object[] { longArray, 1, 3, long.MinValue, null, -2 };
yield return new object[] { longArray, 1, 0, (long)0, null, -2 };
yield return new object[] { new long[0], 0, 0, (long)0, null, -1 };
// UInt64
ulong[] ulongArray = new ulong[] { ulong.MinValue, 5, 5, ulong.MaxValue };
yield return new object[] { ulongArray, 0, 4, ulong.MinValue, null, 0 };
yield return new object[] { ulongArray, 0, 4, (ulong)5, null, 1 };
yield return new object[] { ulongArray, 0, 4, ulong.MaxValue, null, 3 };
yield return new object[] { ulongArray, 0, 4, (ulong)1, null, -2 };
yield return new object[] { ulongArray, 0, 1, ulong.MinValue, null, 0 };
yield return new object[] { ulongArray, 1, 3, ulong.MaxValue, null, 3 };
yield return new object[] { ulongArray, 1, 3, ulong.MinValue, null, -2 };
yield return new object[] { ulongArray, 1, 0, (ulong)5, null, -2 };
yield return new object[] { new ulong[0], 0, 0, (ulong)0, null, -1 };
// // [ActiveIssue("https://github.com/xunit/xunit/issues/1771")]
// // IntPtr
// IntPtr[] intPtrArray = new IntPtr[] { IntPtr.MinValue, (IntPtr)0, (IntPtr)0, IntPtr.MaxValue };
// yield return new object[] { intPtrArray, 0, 4, IntPtr.MinValue, null, 0 };
// yield return new object[] { intPtrArray, 0, 4, (IntPtr)0, null, 1 };
// yield return new object[] { intPtrArray, 0, 4, IntPtr.MaxValue, null, 3 };
// yield return new object[] { intPtrArray, 0, 4, (IntPtr)1, null, -4 };
// yield return new object[] { intPtrArray, 0, 1, IntPtr.MinValue, null, 0 };
// yield return new object[] { intPtrArray, 1, 3, IntPtr.MaxValue, null, 3 };
// yield return new object[] { intPtrArray, 1, 3, IntPtr.MinValue, null, -2 };
// yield return new object[] { intPtrArray, 1, 0, (IntPtr)0, null, -2 };
// yield return new object[] { new IntPtr[0], 0, 0, (IntPtr)0, null, -1 };
// // UIntPtr
// UIntPtr[] uintPtrArray = new UIntPtr[] { UIntPtr.MinValue, (UIntPtr)5, (UIntPtr)5, UIntPtr.MaxValue };
// yield return new object[] { uintPtrArray, 0, 4, UIntPtr.MinValue, null, 0 };
// yield return new object[] { uintPtrArray, 0, 4, (UIntPtr)5, null, 1 };
// yield return new object[] { uintPtrArray, 0, 4, UIntPtr.MaxValue, null, 3 };
// yield return new object[] { uintPtrArray, 0, 4, (UIntPtr)1, null, -2 };
// yield return new object[] { uintPtrArray, 0, 1, UIntPtr.MinValue, null, 0 };
// yield return new object[] { uintPtrArray, 1, 3, UIntPtr.MaxValue, null, 3 };
// yield return new object[] { uintPtrArray, 1, 3, UIntPtr.MinValue, null, -2 };
// yield return new object[] { uintPtrArray, 1, 0, (UIntPtr)5, null, -2 };
// yield return new object[] { new UIntPtr[0], 0, 0, (UIntPtr)0, null, -1 };
// Char
char[] charArray = new char[] { char.MinValue, (char)5, (char)5, char.MaxValue };
yield return new object[] { charArray, 0, 4, char.MinValue, null, 0 };
yield return new object[] { charArray, 0, 4, (char)5, null, 1 };
yield return new object[] { charArray, 0, 4, char.MaxValue, null, 3 };
yield return new object[] { charArray, 0, 4, (char)1, null, -2 };
yield return new object[] { charArray, 0, 1, char.MinValue, null, 0 };
yield return new object[] { charArray, 1, 3, char.MaxValue, null, 3 };
yield return new object[] { charArray, 1, 3, char.MinValue, null, -2 };
yield return new object[] { charArray, 1, 0, (char)5, null, -2 };
yield return new object[] { new char[0], 0, 0, '\0', null, -1 };
// Bool
bool[] boolArray = new bool[] { false, false, true };
yield return new object[] { boolArray, 0, 3, false, null, 1 };
yield return new object[] { boolArray, 0, 3, true, null, 2 };
yield return new object[] { new bool[] { false }, 0, 1, true, null, -2 };
yield return new object[] { boolArray, 0, 1, false, null, 0 };
yield return new object[] { boolArray, 2, 1, true, null, 2 };
yield return new object[] { boolArray, 2, 1, false, null, -3 };
yield return new object[] { boolArray, 1, 0, false, null, -2 };
yield return new object[] { new bool[0], 0, 0, false, null, -1 };
// Single
float[] floatArray = new float[] { float.MinValue, 0, 0, float.MaxValue };
yield return new object[] { floatArray, 0, 4, float.MinValue, null, 0 };
yield return new object[] { floatArray, 0, 4, 0f, null, 1 };
yield return new object[] { floatArray, 0, 4, float.MaxValue, null, 3 };
yield return new object[] { floatArray, 0, 4, (float)1, null, -4 };
yield return new object[] { floatArray, 0, 1, float.MinValue, null, 0 };
yield return new object[] { floatArray, 1, 3, float.MaxValue, null, 3 };
yield return new object[] { floatArray, 1, 3, float.MinValue, null, -2 };
yield return new object[] { floatArray, 1, 0, 0f, null, -2 };
yield return new object[] { new float[0], 0, 0, 0f, null, -1 };
// Double
double[] doubleArray = new double[] { double.MinValue, 0, 0, double.MaxValue };
yield return new object[] { doubleArray, 0, 4, double.MinValue, null, 0 };
yield return new object[] { doubleArray, 0, 4, 0d, null, 1 };
yield return new object[] { doubleArray, 0, 4, double.MaxValue, null, 3 };
yield return new object[] { doubleArray, 0, 4, (double)1, null, -4 };
yield return new object[] { doubleArray, 0, 1, double.MinValue, null, 0 };
yield return new object[] { doubleArray, 1, 3, double.MaxValue, null, 3 };
yield return new object[] { doubleArray, 1, 3, double.MinValue, null, -2 };
yield return new object[] { doubleArray, 1, 0, 0d, null, -2 };
yield return new object[] { new double[0], 0, 0, 0d, null, -1 };
}
[Theory]
// Workaround: Move these tests to BinarySearch_SZArray_TestData if/ when https://github.com/xunit/xunit/pull/965 is available
[InlineData(new sbyte[] { 0 }, 0, 1, null, null, -1)]
[InlineData(new byte[] { 0 }, 0, 1, null, null, -1)]
[InlineData(new short[] { 0 }, 0, 1, null, null, -1)]
[InlineData(new ushort[] { 0 }, 0, 1, null, null, -1)]
[InlineData(new int[] { 0 }, 0, 1, null, null, -1)]
[InlineData(new uint[] { 0 }, 0, 1, null, null, -1)]
[InlineData(new long[] { 0 }, 0, 1, null, null, -1)]
[InlineData(new ulong[] { 0 }, 0, 1, null, null, -1)]
[InlineData(new bool[] { false }, 0, 1, null, null, -1)]
[InlineData(new char[] { '\0' }, 0, 1, null, null, -1)]
[InlineData(new float[] { 0 }, 0, 1, null, null, -1)]
[InlineData(new double[] { 0 }, 0, 1, null, null, -1)]
public static void BinarySearch_Array(Array array, int index, int length, object value, IComparer comparer, int expected)
{
bool isDefaultComparer = comparer == null || comparer == Comparer.Default;
if (index == array.GetLowerBound(0) && length == array.Length)
{
if (isDefaultComparer)
{
// Use BinarySearch(Array, object)
Assert.Equal(expected, Array.BinarySearch(array, value));
Assert.Equal(expected, Array.BinarySearch(array, value, Comparer.Default));
}
// Use BinarySearch(Array, object, IComparer)
Assert.Equal(expected, Array.BinarySearch(array, value, comparer));
}
if (isDefaultComparer)
{
// Use BinarySearch(Array, int, int, object)
Assert.Equal(expected, Array.BinarySearch(array, index, length, value));
}
// Use BinarySearch(Array, int, int, object, IComparer)
Assert.Equal(expected, Array.BinarySearch(array, index, length, value, comparer));
}
[Theory]
[MemberData(nameof(BinarySearch_SZArray_TestData))]
public static void BinarySearch_SZArray<T>(T[] array, int index, int length, T value, IComparer<T> comparer, int expected)
{
// Forward to the non-generic overload if we can.
bool isDefaultComparer = comparer == null || comparer == Comparer<T>.Default;
if (isDefaultComparer || comparer is IComparer)
{
// Basic: forward SZArray
BinarySearch_Array(array, index, length, value, (IComparer)comparer, expected);
if (PlatformDetection.IsNonZeroLowerBoundArraySupported)
{
// Advanced: convert SZArray to an array with non-zero lower bound
const int lowerBound = 5;
Array nonZeroLowerBoundArray = NonZeroLowerBoundArray(array, lowerBound);
int lowerBoundExpected = expected < 0 ? expected - lowerBound : expected + lowerBound;
BinarySearch_Array(nonZeroLowerBoundArray, index + lowerBound, length, value, (IComparer)comparer, lowerBoundExpected);
}
}
if (index == 0 && length == array.Length)
{
if (isDefaultComparer)
{
// Use BinarySearch<T>(T[], T)
Assert.Equal(expected, Array.BinarySearch(array, value));
Assert.Equal(expected, Array.BinarySearch(array, value, Comparer<T>.Default));
}
// Use BinarySearch<T>(T[], T, IComparer)
Assert.Equal(expected, Array.BinarySearch(array, value, comparer));
}
if (isDefaultComparer)
{
// Use BinarySearch<T>(T, int, int, T)
Assert.Equal(expected, Array.BinarySearch(array, index, length, value));
}
// Use BinarySearch<T>(T[], int, int, T, IComparer<T>)
Assert.Equal(expected, Array.BinarySearch(array, index, length, value, comparer));
}
[Fact]
public static void BinarySearch_SZArray_NonInferrableEntries()
{
// Workaround: Move these values to BinarySearch_SZArray_TestData if/ when https://github.com/xunit/xunit/pull/965 is available
BinarySearch_SZArray(new string[] { null, "a", "b", "c" }, 0, 4, null, null, 0);
BinarySearch_SZArray(new string[] { null, "a", "b", "c" }, 0, 4, null, new StringComparer(), 0);
}
[Fact]
public static void BinarySearch_NullArray_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("array", () => Array.BinarySearch((int[])null, ""));
AssertExtensions.Throws<ArgumentNullException>("array", () => Array.BinarySearch(null, ""));
AssertExtensions.Throws<ArgumentNullException>("array", () => Array.BinarySearch((int[])null, "", null));
AssertExtensions.Throws<ArgumentNullException>("array", () => Array.BinarySearch(null, "", null));
AssertExtensions.Throws<ArgumentNullException>("array", () => Array.BinarySearch((int[])null, 0, 0, ""));
AssertExtensions.Throws<ArgumentNullException>("array", () => Array.BinarySearch(null, 0, 0, ""));
AssertExtensions.Throws<ArgumentNullException>("array", () => Array.BinarySearch((int[])null, 0, 0, "", null));
AssertExtensions.Throws<ArgumentNullException>("array", () => Array.BinarySearch(null, 0, 0, "", null));
}
[Fact]
public static void BinarySearch_MultiDimensionalArray_ThrowsRankException()
{
Assert.Throws<RankException>(() => Array.BinarySearch(new string[0, 0], ""));
Assert.Throws<RankException>(() => Array.BinarySearch(new string[0, 0], "", null));
Assert.Throws<RankException>(() => Array.BinarySearch(new string[0, 0], 0, 0, ""));
Assert.Throws<RankException>(() => Array.BinarySearch(new string[0, 0], 0, 0, "", null));
}
public static IEnumerable<object[]> BinarySearch_TypesNotComparable_TestData()
{
// Different types
yield return new object[] { new int[] { 0 }, "" };
// Type does not implement IComparable
yield return new object[] { new object[] { new object() }, new object() };
// Conversion between primitives is not allowed
yield return new object[] { new sbyte[] { 0 }, 0 };
yield return new object[] { new char[] { '\0' }, (ushort)0 };
}
[Theory]
[MemberData(nameof(BinarySearch_TypesNotComparable_TestData))]
public static void BinarySearch_TypesNotIComparable_ThrowsInvalidOperationException<T>(T[] array, object value)
{
Assert.Throws<InvalidOperationException>(() => Array.BinarySearch(array, value));
Assert.Throws<InvalidOperationException>(() => Array.BinarySearch(array, value, null));
Assert.Throws<InvalidOperationException>(() => Array.BinarySearch(array, 0, array.Length, value));
Assert.Throws<InvalidOperationException>(() => Array.BinarySearch(array, 0, array.Length, value, null));
if (value is T)
{
Assert.Throws<InvalidOperationException>(() => Array.BinarySearch(array, (T)value));
Assert.Throws<InvalidOperationException>(() => Array.BinarySearch(array, 0, array.Length, (T)value));
Assert.Throws<InvalidOperationException>(() => Array.BinarySearch(array, 0, array.Length, (T)value, null));
}
}
[Fact]
public static void BinarySearch_IndexLessThanZero_ThrowsArgumentOutOfRangeException()
{
AssertExtensions.Throws<ArgumentOutOfRangeException>("index", () => Array.BinarySearch(new int[3], -1, 0, ""));
AssertExtensions.Throws<ArgumentOutOfRangeException>("index", () => Array.BinarySearch(new string[3], -1, 0, ""));
AssertExtensions.Throws<ArgumentOutOfRangeException>("index", () => Array.BinarySearch(new int[3], -1, 0, "", null));
AssertExtensions.Throws<ArgumentOutOfRangeException>("index", () => Array.BinarySearch(new string[3], -1, 0, "", null));
}
[Fact]
public static void BinarySearch_LengthLessThanZero_ThrowsArgumentOutOfRangeException()
{
AssertExtensions.Throws<ArgumentOutOfRangeException>("length", () => Array.BinarySearch(new int[3], 0, -1, ""));
AssertExtensions.Throws<ArgumentOutOfRangeException>("length", () => Array.BinarySearch(new string[3], 0, -1, ""));
AssertExtensions.Throws<ArgumentOutOfRangeException>("length", () => Array.BinarySearch(new int[3], 0, -1, "", null));
AssertExtensions.Throws<ArgumentOutOfRangeException>("length", () => Array.BinarySearch(new string[3], 0, -1, "", null));
}
[Theory]
[InlineData(0, 0, 1)]
[InlineData(0, 1, 0)]
[InlineData(3, 0, 4)]
[InlineData(3, 1, 3)]
[InlineData(3, 3, 1)]
public static void BinarySearch_IndexPlusLengthInvalid_ThrowsArgumentException(int count, int index, int length)
{
AssertExtensions.Throws<ArgumentException>(null, () => Array.BinarySearch(new int[count], index, length, ""));
AssertExtensions.Throws<ArgumentException>(null, () => Array.BinarySearch(new string[count], index, length, ""));
AssertExtensions.Throws<ArgumentException>(null, () => Array.BinarySearch(new int[count], index, length, "", null));
AssertExtensions.Throws<ArgumentException>(null, () => Array.BinarySearch(new string[count], index, length, "", null));
}
[Theory]
[InlineData(typeof(object), 0)]
[InlineData(typeof(object), 2)]
[InlineData(typeof(int), 0)]
[InlineData(typeof(int), 2)]
[InlineData(typeof(IntPtr), 0)]
[InlineData(typeof(IntPtr), 2)]
[InlineData(typeof(UIntPtr), 0)]
[InlineData(typeof(UIntPtr), 2)]
public static void BinarySearch_CountZero_ValueInvalidType_DoesNotThrow(Type elementType, int length)
{
Array array = Array.CreateInstance(elementType, length);
Assert.Equal(-1, Array.BinarySearch(array, 0, 0, new object()));
}
[Fact]
public static void GetValue_RankOneInt_SetValue()
{
var intArray = new int[] { 7, 8, 9 };
Array array = intArray;
Assert.Equal(7, array.GetValue(0));
array.SetValue(41, 0);
Assert.Equal(41, intArray[0]);
Assert.Equal(8, array.GetValue(1));
array.SetValue(42, 1);
Assert.Equal(42, intArray[1]);
Assert.Equal(9, array.GetValue(2));
array.SetValue(43, 2);
Assert.Equal(43, intArray[2]);
}
[Fact]
public static void GetValue_RankOneLong_SetValue()
{
int[] array = new int[] { 7, 8, 9 };
Assert.Equal(7, array.GetValue((long)0));
array.SetValue(41, (long)0);
Assert.Equal(41, array[0]);
Assert.Equal(8, array.GetValue((long)1));
array.SetValue(42, (long)1);
Assert.Equal(42, array[1]);
Assert.Equal(9, array.GetValue((long)2));
array.SetValue(43, (long)2);
Assert.Equal(43, array[2]);
}
[Fact]
public static void GetValue_RankTwoInt_SetValue()
{
int[,] array = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
Assert.Equal(1, array.GetValue(0, 0));
Assert.Equal(6, array.GetValue(1, 2));
array.SetValue(42, 1, 2);
Assert.Equal(42, array.GetValue(1, 2));
}
[Fact]
public static void GetValue_RankTwoLong_SetValue()
{
int[,] array = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
Assert.Equal(1, array.GetValue((long)0, 0));
Assert.Equal(6, array.GetValue((long)1, 2));
array.SetValue(42, (long)1, 2);
Assert.Equal(42, array.GetValue((long)1, 2));
}
[Fact]
public static void GetValue_RankThreeInt_SetValue()
{
Array array = Array.CreateInstance(typeof(int), 2, 3, 4);
array.SetValue(42, 1, 2, 3);
Assert.Equal(42, array.GetValue(1, 2, 3));
}
[Fact]
public static void GetValue_RankThreeLong_SetValue()
{
Array array = Array.CreateInstance(typeof(int), 2, 3, 4);
array.SetValue(42, (long)1, 2, 3);
Assert.Equal(42, array.GetValue((long)1, 2, 3));
}
[Fact]
public static void GetValue_RankFourInt_SetValue()
{
Array array = Array.CreateInstance(typeof(int), 2, 3, 4, 5);
array.SetValue(42, 1, 2, 3, 4);
Assert.Equal(42, array.GetValue(1, 2, 3, 4));
}
[Fact]
public static void GetValue_RankFourLong_SetValue()
{
Array array = Array.CreateInstance(typeof(int), 2, 3, 4, 5);
array.SetValue(42, (long)1, 2, 3, 4);
Assert.Equal(42, array.GetValue((long)1, 2, 3, 4));
}
[Theory]
[InlineData(-1)]
[InlineData(10)]
public void GetValue_OutOfRangeIntIndex1_ThrowsIndexOutOfRangeException(int index)
{
Assert.Throws<IndexOutOfRangeException>(() => new int[10].GetValue(index));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10].GetValue(index, 0));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10, 10].GetValue(index, 0, 0));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10, 10].GetValue(new int[] { index, 0, 0 }));
Assert.Throws<IndexOutOfRangeException>(() => new int[10].GetValue((long)index));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10].GetValue((long)index, 0));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10, 10].GetValue((long)index, 0, 0));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10, 10].GetValue(new long[] { (long)index, 0, 0 }));
}
[Theory]
[InlineData((long)int.MaxValue + 1)]
[InlineData((long)int.MinValue - 1)]
public void GetValue_OutOfRangeLongIndex1_ThrowsArgumentOutOfRangeException(long index)
{
Assert.Throws<ArgumentOutOfRangeException>("index", () => new int[10].GetValue(index));
Assert.Throws<ArgumentOutOfRangeException>("index1", () => new int[10, 10].GetValue(index, 0));
Assert.Throws<ArgumentOutOfRangeException>("index1", () => new int[10, 10, 10].GetValue(index, 0, 0));
Assert.Throws<ArgumentOutOfRangeException>("index", () => new int[10, 10, 10].GetValue(new long[] { index, 0, 0 }));
}
[Theory]
[InlineData(-1)]
[InlineData(10)]
public void GetValue_OutOfRangeIntIndex2_ThrowsIndexOutOfRangeException(int index)
{
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10].GetValue(0, index));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10, 10].GetValue(0, index, 0));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10, 10].GetValue(new int[] { 0, index, 0 }));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10].GetValue((long)0, index));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10, 10].GetValue((long)0, index, 0));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10, 10].GetValue(new long[] { 0, index, 0 }));
}
[Theory]
[InlineData((long)int.MaxValue + 1)]
[InlineData((long)int.MinValue - 1)]
public void GetValue_OutOfRangeLongIndex2_ThrowsArgumentOutOfRangeException(long index)
{
Assert.Throws<ArgumentOutOfRangeException>("index2", () => new int[10, 10].GetValue(0, index));
Assert.Throws<ArgumentOutOfRangeException>("index2", () => new int[10, 10, 10].GetValue(0, index, 0));
Assert.Throws<ArgumentOutOfRangeException>("index", () => new int[10, 10, 10].GetValue(new long[] { 0, index, 0 }));
}
[Theory]
[InlineData(-1)]
[InlineData(10)]
public void GetValue_OutOfRangeIntIndex3_ThrowsIndexOutOfRangeException(int index)
{
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10, 10].GetValue(0, 0, index));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10, 10].GetValue(new int[] { 0, 0, index }));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10, 10].GetValue((long)0, 0, index));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10, 10].GetValue(new long[] { 0, 0, index }));
}
[Theory]
[InlineData((long)int.MaxValue + 1)]
[InlineData((long)int.MinValue - 1)]
public void GetValue_OutOfRangeLongIndex3_ThrowsArgumentOutOfRangeException(long index)
{
Assert.Throws<ArgumentOutOfRangeException>("index3", () => new int[10, 10, 10].GetValue(0, 0, index));
Assert.Throws<ArgumentOutOfRangeException>("index", () => new int[10, 10, 10].GetValue(new long[] { 0, 0, index }));
}
[Fact]
public void GetValue_InvalidRank_ThrowsArgumentException()
{
AssertExtensions.Throws<ArgumentException>(null, () => new int[10].GetValue(0, 1));
AssertExtensions.Throws<ArgumentException>(null, () => new int[10].GetValue(0, 1, 2));
AssertExtensions.Throws<ArgumentException>(null, () => new int[10].GetValue(new int[] { 0, 1, 2 }));
AssertExtensions.Throws<ArgumentException>(null, () => new int[10].GetValue((long)0, 1));
AssertExtensions.Throws<ArgumentException>(null, () => new int[10].GetValue((long)0, 1, 2));
AssertExtensions.Throws<ArgumentException>(null, () => new int[10].GetValue(new long[] { 0, 1, 2 }));
AssertExtensions.Throws<ArgumentException>(null, () => new int[10, 10].GetValue(0));
AssertExtensions.Throws<ArgumentException>(null, () => new int[10, 10].GetValue(0, 1, 2));
AssertExtensions.Throws<ArgumentException>(null, () => new int[10, 10].GetValue(new int[] { 0, 1, 2 }));
AssertExtensions.Throws<ArgumentException>(null, () => new int[10, 10].GetValue((long)0));
AssertExtensions.Throws<ArgumentException>(null, () => new int[10, 10].GetValue((long)0, 1, 2));
AssertExtensions.Throws<ArgumentException>(null, () => new int[10, 10].GetValue(new long[] { 0, 1, 2 }));
}
[Fact]
public static void GetValue_NullIndices_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("indices", () => new int[10].GetValue((int[])null));
AssertExtensions.Throws<ArgumentNullException>("indices", () => new int[10].GetValue((long[])null));
}
[Theory]
[InlineData(-1)]
[InlineData(10)]
public void SetValue_OutOfRangeIntIndex1_ThrowsIndexOutOfRangeException(int index)
{
Assert.Throws<IndexOutOfRangeException>(() => new int[10].SetValue(1, index));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10].SetValue(1, index, 0));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10, 10].SetValue(1, index, 0, 0));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10, 10].SetValue(1, new int[] { index, 0, 0 }));
Assert.Throws<IndexOutOfRangeException>(() => new int[10].SetValue(1, (long)index));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10].SetValue(1, (long)index, 0));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10, 10].SetValue(1, (long)index, 0, 0));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10, 10].SetValue(1, new long[] { (long)index, 0, 0 }));
}
[Theory]
[InlineData((long)int.MaxValue + 1)]
[InlineData((long)int.MinValue - 1)]
public void SetValue_OutOfRangeLongIndex1_ThrowsArgumentOutOfRangeException(long index)
{
Assert.Throws<ArgumentOutOfRangeException>("index", () => new int[10].SetValue(1, index));
Assert.Throws<ArgumentOutOfRangeException>("index1", () => new int[10, 10].SetValue(1, index, 0));
Assert.Throws<ArgumentOutOfRangeException>("index1", () => new int[10, 10, 10].SetValue(1, index, 0, 0));
Assert.Throws<ArgumentOutOfRangeException>("index", () => new int[10, 10, 10].SetValue(1, new long[] { index, 0, 0 }));
}
[Theory]
[InlineData(-1)]
[InlineData(10)]
public void SetValue_OutOfRangeIntIndex2_ThrowsIndexOutOfRangeException(int index)
{
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10].SetValue(1, 0, index));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10, 10].SetValue(1, 0, index, 0));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10, 10].SetValue(1, new int[] { 0, index, 0 }));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10].SetValue(1, (long)0, index));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10, 10].SetValue(1, (long)0, index, 0));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10, 10].SetValue(1, new long[] { 0, index, 0 }));
}
[Theory]
[InlineData((long)int.MaxValue + 1)]
[InlineData((long)int.MinValue - 1)]
public void SetValue_OutOfRangeLongIndex2_ThrowsArgumentOutOfRangeException(long index)
{
Assert.Throws<ArgumentOutOfRangeException>("index2", () => new int[10, 10].SetValue(1, 0, index));
Assert.Throws<ArgumentOutOfRangeException>("index2", () => new int[10, 10, 10].SetValue(1, 0, index, 0));
Assert.Throws<ArgumentOutOfRangeException>("index", () => new int[10, 10, 10].SetValue(1, new long[] { 0, index, 0 }));
}
[Theory]
[InlineData(-1)]
[InlineData(10)]
public void SetValue_OutOfRangeIntIndex3_ThrowsIndexOutOfRangeException(int index)
{
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10, 10].SetValue(1, 0, 0, index));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10, 10].SetValue(1, new int[] { 0, 0, index }));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10, 10].SetValue(1, (long)0, 0, index));
Assert.Throws<IndexOutOfRangeException>(() => new int[10, 10, 10].SetValue(1, new long[] { 0, 0, index }));
}
[Theory]
[InlineData((long)int.MaxValue + 1)]
[InlineData((long)int.MinValue - 1)]
public void SetValue_OutOfRangeLongIndex3_ThrowsArgumentOutOfRangeException(long index)
{
Assert.Throws<ArgumentOutOfRangeException>("index3", () => new int[10, 10, 10].SetValue(1, 0, 0, index));
Assert.Throws<ArgumentOutOfRangeException>("index", () => new int[10, 10, 10].SetValue(1, new long[] { 0, 0, index }));
}
[Fact]
public void SetValue_InvalidRank_ThrowsArgumentException()
{
AssertExtensions.Throws<ArgumentException>(null, () => new int[10].SetValue(1, 0, 1));
AssertExtensions.Throws<ArgumentException>(null, () => new int[10].SetValue(1, 0, 1, 2));
AssertExtensions.Throws<ArgumentException>(null, () => new int[10].SetValue(1, new int[] { 0, 1, 2 }));
AssertExtensions.Throws<ArgumentException>(null, () => new int[10].SetValue(1, (long)0, 1));
AssertExtensions.Throws<ArgumentException>(null, () => new int[10].SetValue(1, (long)0, 1, 2));
AssertExtensions.Throws<ArgumentException>(null, () => new int[10].SetValue(1, new long[] { 0, 1, 2 }));
AssertExtensions.Throws<ArgumentException>(null, () => new int[10, 10].SetValue(1, 0));
AssertExtensions.Throws<ArgumentException>(null, () => new int[10, 10].SetValue(1, 0, 1, 2));
AssertExtensions.Throws<ArgumentException>(null, () => new int[10, 10].SetValue(1, new int[] { 0, 1, 2 }));
AssertExtensions.Throws<ArgumentException>(null, () => new int[10, 10].SetValue(1, (long)0));
AssertExtensions.Throws<ArgumentException>(null, () => new int[10, 10].SetValue(1, (long)0, 1, 2));
AssertExtensions.Throws<ArgumentException>(null, () => new int[10, 10].SetValue(1, new long[] { 0, 1, 2 }));
}
[Fact]
public static void SetValue_NullIndices_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("indices", () => new int[10].SetValue(1, (int[])null));
AssertExtensions.Throws<ArgumentNullException>("indices", () => new int[10].SetValue(1, (long[])null));
}
[Theory]
[InlineData(new int[] { 7, 8, 9 }, 0, 3, new int[] { 0, 0, 0 })]
[InlineData(new int[] { 0x1234567, 0x789abcde, 0x22334455, 0x66778899, 0x11335577, 0x22446688 }, 0, 6, new int[] { 0, 0, 0, 0, 0, 0 })]
[InlineData(new int[] { 0x1234567, 0x789abcde, 0x22334455, 0x66778899, 0x11335577, 0x22446688 }, 2, 3, new int[] { 0x1234567, 0x789abcde, 0, 0, 0, 0x22446688 })]
[InlineData(new int[] { 0x1234567, 0x789abcde, 0x22334455, 0x66778899, 0x11335577, 0x22446688 }, 6, 0, new int[] { 0x1234567, 0x789abcde, 0x22334455, 0x66778899, 0x11335577, 0x22446688 })]
[InlineData(new int[] { 0x1234567, 0x789abcde, 0x22334455, 0x66778899, 0x11335577, 0x22446688 }, 0, 0, new int[] { 0x1234567, 0x789abcde, 0x22334455, 0x66778899, 0x11335577, 0x22446688 })]
[InlineData(new string[] { "7", "8", "9" }, 0, 3, new string[] { null, null, null })]
[InlineData(new string[] { "0x1234567", "0x789abcde", "0x22334455", "0x66778899", "0x11335577", "0x22446688" }, 0, 6, new string[] { null, null, null, null, null, null })]
[InlineData(new string[] { "0x1234567", "0x789abcde", "0x22334455", "0x66778899", "0x11335577", "0x22446688" }, 2, 3, new string[] { "0x1234567", "0x789abcde", null, null, null, "0x22446688" })]
[InlineData(new string[] { "0x1234567", "0x789abcde", "0x22334455", "0x66778899", "0x11335577", "0x22446688" }, 6, 0, new string[] { "0x1234567", "0x789abcde", "0x22334455", "0x66778899", "0x11335577", "0x22446688" })]
[InlineData(new string[] { "0x1234567", "0x789abcde", "0x22334455", "0x66778899", "0x11335577", "0x22446688" }, 0, 0, new string[] { "0x1234567", "0x789abcde", "0x22334455", "0x66778899", "0x11335577", "0x22446688" })]
public static void Clear(Array array, int index, int length, Array expected)
{
if (index == 0 && length == array.Length)
{
// Use Array.Clear()
Array arrayClone1 = (Array)array.Clone();
Array.Clear(arrayClone1);
Assert.Equal(expected, arrayClone1);
// Use IList.Clear()
Array arrayClone2 = (Array)array.Clone();
((IList)arrayClone2).Clear();
Assert.Equal(expected, arrayClone2);
}
Array arrayClone3 = (Array)array.Clone();
Array.Clear(arrayClone3, index, length);
Assert.Equal(expected, arrayClone3);
}
[Fact]
public static void Clear_Struct_WithReferenceAndValueTypeFields_Array()
{
var array = new NonGenericStruct[]
{
new NonGenericStruct { x = 1, s = "Hello", z = 2 },
new NonGenericStruct { x = 2, s = "Hello", z = 3 },
new NonGenericStruct { x = 3, s = "Hello", z = 4 },
new NonGenericStruct { x = 4, s = "Hello", z = 5 },
new NonGenericStruct { x = 5, s = "Hello", z = 6 }
};
Array.Clear(array);
for (int i = 0; i < array.Length; i++)
{
Assert.Equal(0, array[i].x);
Assert.Null(array[i].s);
Assert.Equal(0, array[i].z);
}
array = new NonGenericStruct[]
{
new NonGenericStruct { x = 1, s = "Hello", z = 2 },
new NonGenericStruct { x = 2, s = "Hello", z = 3 },
new NonGenericStruct { x = 3, s = "Hello", z = 4 },
new NonGenericStruct { x = 4, s = "Hello", z = 5 },
new NonGenericStruct { x = 5, s = "Hello", z = 6 }
};
Array.Clear(array, 0, 5);
for (int i = 0; i < array.Length; i++)
{
Assert.Equal(0, array[i].x);
Assert.Null(array[i].s);
Assert.Equal(0, array[i].z);
}
array = new NonGenericStruct[]
{
new NonGenericStruct { x = 1, s = "Hello", z = 2 },
new NonGenericStruct { x = 2, s = "Hello", z = 3 },
new NonGenericStruct { x = 3, s = "Hello", z = 4 },
new NonGenericStruct { x = 4, s = "Hello", z = 5 },
new NonGenericStruct { x = 5, s = "Hello", z = 6 }
};
Array.Clear(array, 2, 3);
Assert.Equal(1, array[0].x);
Assert.Equal("Hello", array[0].s);
Assert.Equal(2, array[0].z);
Assert.Equal(2, array[1].x);
Assert.Equal("Hello", array[1].s);
Assert.Equal(3, array[1].z);
for (int i = 2; i < 2 + 3; i++)
{
Assert.Equal(0, array[i].x);
Assert.Null(array[i].s);
Assert.Equal(0, array[i].z);
}
}
[Fact]
public static void Clear_Invalid()
{
AssertExtensions.Throws<ArgumentNullException>("array", () => Array.Clear(null)); // Array is null
AssertExtensions.Throws<ArgumentNullException>("array", () => Array.Clear(null, 0, 0)); // Array is null
Assert.Throws<IndexOutOfRangeException>(() => Array.Clear(new int[10], -1, 0)); // Index < 0
Assert.Throws<IndexOutOfRangeException>(() => Array.Clear(new int[10], 0, -1)); // Length < 0
// Index + length > array.Length
Assert.Throws<IndexOutOfRangeException>(() => Array.Clear(new int[10], 0, 11));
Assert.Throws<IndexOutOfRangeException>(() => Array.Clear(new int[10], 10, 1));
Assert.Throws<IndexOutOfRangeException>(() => Array.Clear(new int[10], 9, 2));
Assert.Throws<IndexOutOfRangeException>(() => Array.Clear(new int[10], 6, 0x7fffffff));
}
public static IEnumerable<object[]> Clone_TestData()
{
yield return new object[] { new int[0] };
yield return new object[] { new char[] { '1', '2', '3' } };
yield return new object[] { new int[0, 0] };
yield return new object[] { new int[0, 1] };
yield return new object[] { new int[1, 1] };
yield return new object[] { new int[2, 3, 4, 5] };
if (PlatformDetection.IsNonZeroLowerBoundArraySupported)
{
yield return new object[] { Array.CreateInstance(typeof(int), new int[] { 1 }, new int[] { -100 }) };
yield return new object[] { Array.CreateInstance(typeof(int), new int[] { 1, 2, 3, 4, 5 }, new int[] { 1, 2, 3, 4, 5 }) };
}
}
[Theory]
[MemberData(nameof(Clone_TestData))]
public void Clone_Array_ReturnsExpected(Array array)
{
Array clone = Assert.IsAssignableFrom<Array>(array.Clone());
Assert.NotSame(array, clone);
Assert.Equal(array, clone);
Assert.Equal(array.Rank, clone.Rank);
Assert.Equal(array.GetType().GetElementType(), clone.GetType().GetElementType());
for (int i = 0; i < clone.Rank; i++)
{
Assert.Equal(array.GetLength(i), clone.GetLength(i));
Assert.Equal(array.GetLowerBound(i), clone.GetLowerBound(i));
}
}
[Fact]
public void Clone_SingleDimensionalArray_ModifyingOriginalDoesNotAffectClone()
{
var array = new int[] { 1, 2, 3 };
int[] clone = Assert.IsType<int[]>(array.Clone());
array[0] = 10;
Assert.Equal(1, clone[0]);
}
[Fact]
public void Clone_MultiDimensionalArray_ModifyingOriginalDoesNotAffectClone()
{
var array = new int[1, 1];
int[,] clone = Assert.IsType<int[,]>(array.Clone());
array[0, 0] = 10;
Assert.Equal(0, clone[0, 0]);
}
[Fact]
public void ConvertAll()
{
int[] result = Array.ConvertAll(new int[] { }, new Converter<int, int>(i => { throw new InvalidOperationException(); }));
Assert.Equal(new int[] { }, result);
string[] result2 = Array.ConvertAll(new int[] { 1 }, new Converter<int, string>(i => (i + 1).ToString()));
Assert.Equal(new string[] { "2" }, result2);
result2 = Array.ConvertAll(new int[] { 1, 2 }, new Converter<int, string>(i => (i + 1).ToString()));
Assert.Equal(new string[] { "2", "3" }, result2);
result2 = Array.ConvertAll(new int[] { 1 }, new Converter<int, string>(i => null));
Assert.Equal(new string[] { null }, result2);
}
[Fact]
public void ConvertAll_NullArray_ThrowsArgumentNullException()