-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
ArrayListTests.cs
2980 lines (2570 loc) · 103 KB
/
ArrayListTests.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.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Threading.Tasks;
using Xunit;
namespace System.Collections.Tests
{
public static class ArrayListTests
{
[Fact]
public static void Ctor_Empty()
{
var arrList = new ArrayList();
Assert.Equal(0, arrList.Count);
Assert.Equal(0, arrList.Capacity);
Assert.False(arrList.IsFixedSize);
Assert.False(arrList.IsReadOnly);
Assert.False(arrList.IsSynchronized);
}
[Theory]
[InlineData(16)]
[InlineData(0)]
public static void Ctor_Int(int capacity)
{
var arrList = new ArrayList(capacity);
Assert.Equal(capacity, arrList.Capacity);
Assert.False(arrList.IsFixedSize);
Assert.False(arrList.IsReadOnly);
Assert.False(arrList.IsSynchronized);
}
[Fact]
public static void Ctor_Int_NegativeCapacity_ThrowsArgumentOutOfRangeException()
{
AssertExtensions.Throws<ArgumentOutOfRangeException>("capacity", () => new ArrayList(-1)); // Capacity < 0
}
[Fact]
public static void Ctor_ICollection()
{
ArrayList sourceList = Helpers.CreateIntArrayList(100);
var arrList = new ArrayList(sourceList);
Assert.Equal(sourceList.Count, arrList.Count);
for (int i = 0; i < arrList.Count; i++)
{
Assert.Equal(sourceList[i], arrList[i]);
}
Assert.False(arrList.IsFixedSize);
Assert.False(arrList.IsReadOnly);
Assert.False(arrList.IsSynchronized);
}
[Fact]
public static void Ctor_ICollection_Empty()
{
ICollection arrListCollection = new ArrayList();
ArrayList arrList = new ArrayList(arrListCollection);
Assert.Equal(0, arrList.Count);
Assert.False(arrList.IsFixedSize);
Assert.False(arrList.IsReadOnly);
Assert.False(arrList.IsSynchronized);
}
[Fact]
public static void Ctor_ICollection_NullCollection_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("c", () => new ArrayList(null)); // Collection is null
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsDebuggerTypeProxyAttributeSupported))]
public static void DebuggerAttribute()
{
DebuggerAttributes.ValidateDebuggerDisplayReferences(new ArrayList());
DebuggerAttributes.ValidateDebuggerTypeProxyProperties(new ArrayList() { "a", 1, "b", 2 });
bool threwNull = false;
try
{
DebuggerAttributes.ValidateDebuggerTypeProxyProperties(typeof(ArrayList), null);
}
catch (TargetInvocationException ex)
{
threwNull = ex.InnerException is ArgumentNullException;
}
Assert.True(threwNull);
}
[Fact]
public static void Adapter_ArrayList()
{
ArrayList arrList = ArrayList.Adapter(new ArrayList());
Assert.False(arrList.IsFixedSize);
Assert.False(arrList.IsReadOnly);
Assert.False(arrList.IsSynchronized);
}
[Fact]
public static void Adapter_FixedSizeArrayList()
{
ArrayList arrList = ArrayList.Adapter(ArrayList.FixedSize(new ArrayList()));
Assert.True(arrList.IsFixedSize);
Assert.False(arrList.IsReadOnly);
Assert.False(arrList.IsSynchronized);
}
[Fact]
public static void Adapter_ReadOnlyArrayList()
{
ArrayList arrList = ArrayList.Adapter(ArrayList.ReadOnly(new ArrayList()));
Assert.True(arrList.IsFixedSize);
Assert.True(arrList.IsReadOnly);
Assert.False(arrList.IsSynchronized);
}
[Fact]
public static void Adapter_SynchronizedArrayList()
{
ArrayList arrList = ArrayList.Adapter(ArrayList.Synchronized(new ArrayList()));
Assert.False(arrList.IsFixedSize);
Assert.False(arrList.IsReadOnly);
Assert.True(arrList.IsSynchronized);
}
[Fact]
public static void Adapter_PopulateChangesToList()
{
const string FromBefore = " from before";
// Make sure changes through listAdapter show up in list
ArrayList arrList = Helpers.CreateStringArrayList(count: 10, optionalString: FromBefore);
ArrayList adapter = ArrayList.Adapter(arrList);
adapter.Reverse(0, adapter.Count);
int j = 9;
for (int i = 0; i < adapter.Count; i++)
{
Assert.Equal(j.ToString() + FromBefore, adapter[i]);
j--;
}
}
[Fact]
public static void Adapter_ClearList()
{
// Make sure changes through list show up in listAdapter
ArrayList arrList = Helpers.CreateIntArrayList(100);
ArrayList adapter = ArrayList.Adapter(arrList);
arrList.Clear();
Assert.Equal(0, adapter.Count);
}
[Fact]
public static void Adapter_Enumerators()
{
// Test to see if enumerators are correctly enumerate through elements
ArrayList arrList = Helpers.CreateIntArrayList(10);
IEnumerator enumeratorBasic = arrList.GetEnumerator();
ArrayList adapter = ArrayList.Adapter(arrList);
IEnumerator enumeratorWrapped = arrList.GetEnumerator();
int j = 0;
while (enumeratorBasic.MoveNext())
{
Assert.Equal(j, enumeratorBasic.Current);
j++;
}
j = 0;
while (enumeratorWrapped.MoveNext())
{
Assert.Equal(j, enumeratorWrapped.Current);
j++;
}
}
[Fact]
public static void Adapter_EnumeratorsModifiedList()
{
// Test to see if enumerators are correctly getting invalidated with list modified through list
ArrayList arrList = Helpers.CreateIntArrayList(10);
IEnumerator enumeratorBasic = arrList.GetEnumerator();
ArrayList adapter = ArrayList.Adapter(arrList);
IEnumerator numeratorWrapped = arrList.GetEnumerator();
enumeratorBasic.MoveNext();
numeratorWrapped.MoveNext();
// Now modify list through arrList
arrList.Add(100);
// Make sure accessing enumeratorBasic and enuemratorWrapped throws
Assert.Throws<InvalidOperationException>(() => enumeratorBasic.MoveNext());
Assert.Throws<InvalidOperationException>(() => numeratorWrapped.MoveNext());
}
[Fact]
public static void Adapter_EnumeratorsModifiedAdapter()
{
// Test to see if enumerators are correctly getting invalidated with list modified through listAdapter
ArrayList arrList = Helpers.CreateStringArrayList(10);
IEnumerator enumeratorBasic = arrList.GetEnumerator();
ArrayList adapter = ArrayList.Adapter(arrList);
IEnumerator enumeratorWrapped = arrList.GetEnumerator();
enumeratorBasic.MoveNext();
enumeratorWrapped.MoveNext();
// Now modify list through adapter
adapter.Add("Hey this is new element");
// Make sure accessing enumeratorBasic and enuemratorWrapped throws
Assert.Throws<InvalidOperationException>(() => enumeratorBasic.MoveNext());
Assert.Throws<InvalidOperationException>(() => enumeratorWrapped.MoveNext());
}
[Fact]
public static void Adapter_InsertRange()
{
// Test to see if listAdaptor modified using InsertRange works
// Populate the list
ArrayList arrList = Helpers.CreateIntArrayList(10);
ArrayList adapter = ArrayList.Adapter(arrList);
// Now add a few more elements using InsertRange
ArrayList arrListSecond = Helpers.CreateIntArrayList(10);
adapter.InsertRange(adapter.Count, arrListSecond);
Assert.Equal(20, adapter.Count);
}
[Fact]
public static void Adapter_Capacity_Set()
{
ArrayList arrList = Helpers.CreateIntArrayList(10);
ArrayList adapter = ArrayList.Adapter(arrList);
adapter.Capacity = 10;
Assert.Equal(10, adapter.Capacity);
}
[Fact]
public static void Adapter_NullList_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("list", () => ArrayList.Adapter(null)); // List is null
}
[Fact]
public static void AddRange_Basic()
{
ArrayList arrList1 = Helpers.CreateIntArrayList(10);
ArrayList arrList2 = Helpers.CreateIntArrayList(20, 10);
VerifyAddRange(arrList1, arrList2);
}
[Fact]
public static void AddRange_DifferentCollection()
{
ArrayList arrList = Helpers.CreateIntArrayList(10);
Queue queue = new Queue();
for (int i = 10; i < 20; i++)
{
queue.Enqueue(i);
}
VerifyAddRange(arrList, queue);
}
[Fact]
public static void AddRange_Self()
{
ArrayList arrList1 = Helpers.CreateIntArrayList(10);
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
if (arrList2.IsFixedSize)
{
return;
}
arrList2.AddRange(arrList2);
for (int i = 0; i < arrList2.Count / 2; i++)
{
Assert.Equal(i, arrList2[i]);
}
for (int i = arrList2.Count / 2; i < arrList2.Count; i++)
{
Assert.Equal(i - arrList2.Count / 2, arrList2[i]);
}
});
}
private static void VerifyAddRange(ArrayList arrList1, ICollection c)
{
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
if (arrList2.IsFixedSize)
{
return;
}
int expectedCount = arrList2.Count + c.Count;
arrList2.AddRange(c);
Assert.Equal(expectedCount, arrList2.Count);
for (int i = 0; i < arrList2.Count; i++)
{
Assert.Equal(i, arrList2[i]);
}
// Assumes that the array list and collection contain integer types
// and the first item in the collection is the count of the array list
for (int i = arrList2.Count; i < c.Count; i++)
{
Assert.Equal(i, arrList2[i]);
}
});
}
[Fact]
public static void AddRange_DifferentObjectTypes()
{
// Add an ICollection with different type objects
ArrayList arrList1 = Helpers.CreateIntArrayList(10); // Array list contains only integers currently
var queue = new Queue(); // Queue contains strings
for (int i = 10; i < 20; i++)
queue.Enqueue("String_" + i);
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
if (arrList2.IsFixedSize)
{
return;
}
arrList2.AddRange(queue);
for (int i = 0; i < 10; i++)
{
Assert.Equal(i, arrList2[i]);
}
for (int i = 10; i < 20; i++)
{
Assert.Equal("String_" + i, arrList2[i]);
}
});
}
[Fact]
public static void AddRange_EmptyCollection()
{
var emptyCollection = new Queue();
ArrayList arrList1 = Helpers.CreateIntArrayList(100);
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
if (arrList2.IsFixedSize)
{
return;
}
arrList2.AddRange(emptyCollection);
Assert.Equal(100, arrList2.Count);
});
}
[Fact]
public static void AddRange_NullCollection_ThrowsArgumentNullException()
{
var arrList1 = new ArrayList();
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
if (arrList2.IsFixedSize)
{
return;
}
AssertExtensions.Throws<ArgumentNullException>("c", () => arrList2.AddRange(null)); // Collection is null
});
}
[Theory]
[InlineData(1)]
[InlineData(10)]
[InlineData(100)]
public static void Add_SmallCapacity(int count)
{
ArrayList arrList1 = new ArrayList(1);
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
if (arrList2.IsFixedSize)
{
return;
}
for (int i = 0; i < count; i++)
{
arrList2.Add(i);
Assert.Equal(i, arrList2[i]);
Assert.Equal(i + 1, arrList2.Count);
Assert.True(arrList2.Capacity >= arrList2.Count);
}
Assert.Equal(count, arrList2.Count);
for (int i = 0; i < count; i++)
{
arrList2.RemoveAt(0);
}
Assert.Equal(0, arrList2.Count);
});
}
[Fact]
public static void BinarySearch_Basic()
{
ArrayList arrList1 = Helpers.CreateIntArrayList(10);
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
foreach (object value in arrList2)
{
int ndx = arrList2.BinarySearch(value);
Assert.Equal(arrList2[ndx], value);
}
});
}
[Fact]
public static void BinarySearch_Basic_NotFoundReturnsNextElementIndex()
{
// The zero-based index of the value in the sorted ArrayList, if value is found; otherwise, a negative number,
// which is the bitwise complement of the index of the next element.
ArrayList arrList1 = Helpers.CreateIntArrayList(100);
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
Assert.Equal(100, ~arrList2.BinarySearch(150));
// Searching for null items should return -1.
Assert.Equal(-1, arrList2.BinarySearch(null));
});
}
[Fact]
public static void BinarySearch_Basic_NullObject()
{
ArrayList arrList1 = Helpers.CreateIntArrayList(100);
arrList1.Add(null);
arrList1.Sort();
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
Assert.Equal(0, arrList2.BinarySearch(null));
});
}
[Fact]
public static void BinarySearch_Basic_DuplicateResults()
{
// If we have duplicate results, return the first.
var arrList1 = new ArrayList();
for (int i = 0; i < 100; i++)
arrList1.Add(5);
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
// Remember, this is BinarySearch.
Assert.Equal(49, arrList2.BinarySearch(5));
});
}
[Fact]
public static void BinarySearch_IComparer()
{
ArrayList arrList1 = Helpers.CreateIntArrayList(10);
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
foreach (object value in arrList1)
{
int ndx = arrList2.BinarySearch(value, new BinarySearchComparer());
Assert.Equal(arrList2[ndx], value);
}
});
}
[Fact]
public static void BinarySearch_IComparer_NotFoundReturnsNextElementIndex()
{
ArrayList arrList1 = Helpers.CreateIntArrayList(100);
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
Assert.Equal(100, ~arrList2.BinarySearch(150, new BinarySearchComparer()));
// Searching for null items should return -1.
Assert.Equal(-1, arrList2.BinarySearch(null, new BinarySearchComparer()));
});
}
[Fact]
public static void BinarySearch_IComparer_NullObject()
{
ArrayList arrList1 = Helpers.CreateIntArrayList(100);
arrList1.Add(null);
arrList1.Sort();
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
Assert.Equal(0, arrList2.BinarySearch(null));
});
}
[Fact]
public static void BinarySearch_IComparer_DuplicateResults()
{
// If we have duplicate results, return the first.
var arrList1 = new ArrayList();
for (int i = 0; i < 100; i++)
arrList1.Add(5);
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
// Remember, this is BinarySearch.
Assert.Equal(49, arrList2.BinarySearch(5, new BinarySearchComparer()));
});
}
[Fact]
public static void BinarySearch_Int_Int_IComparer()
{
ArrayList arrList1 = Helpers.CreateIntArrayList(10);
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
foreach (object value in arrList2)
{
int ndx = arrList2.BinarySearch(0, arrList2.Count, value, new BinarySearchComparer());
Assert.Equal(arrList2[ndx], value);
}
});
}
[Fact]
public static void BinarySearch_Int_Int_IComparer_ObjectOutsideIndex()
{
ArrayList arrList1 = Helpers.CreateIntArrayList(10);
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
// Index > list.IndexOf(object)
int ndx = arrList2.BinarySearch(1, arrList2.Count - 1, 0, new BinarySearchComparer());
Assert.Equal(-2, ndx);
// Index + count < list.IndexOf(object)
ndx = arrList2.BinarySearch(0, arrList2.Count - 2, 9, new BinarySearchComparer());
Assert.Equal(-9, ndx);
});
}
[Fact]
public static void BinarySearch_Int_Int_IComparer_NullComparer()
{
ArrayList arrList1 = Helpers.CreateIntArrayList(10);
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
// Locating item in list using a null comparer uses default comparer.
int ndx1 = arrList2.BinarySearch(0, arrList2.Count, 5, null);
int ndx2 = arrList2.BinarySearch(5, null);
int ndx3 = arrList2.BinarySearch(5);
Assert.Equal(ndx1, ndx2);
Assert.Equal(ndx1, ndx3);
Assert.Equal(5, ndx1);
});
}
[Fact]
public static void BinarySearch_Int_Int_IComparer_Invalid()
{
ArrayList arrList1 = Helpers.CreateIntArrayList(10);
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
IComparer comparer = new BinarySearchComparer();
AssertExtensions.Throws<ArgumentOutOfRangeException>("index", () => arrList2.BinarySearch(-1, 1000, arrList2.Count, comparer)); // Index < 0
AssertExtensions.Throws<ArgumentOutOfRangeException>("index", () => arrList2.BinarySearch(-1, 1000, 1, comparer)); // Index < 0
AssertExtensions.Throws<ArgumentOutOfRangeException>("index", () => arrList2.BinarySearch(-1, arrList2.Count, 1, comparer)); // Index < 0
AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => arrList2.BinarySearch(0, -1, 1, comparer)); // Count < 0
AssertExtensions.Throws<ArgumentException>(null, () => arrList2.BinarySearch(1, arrList2.Count, 1, comparer)); // Index + Count >= list.Count
AssertExtensions.Throws<ArgumentException>(null, () => arrList2.BinarySearch(3, arrList2.Count - 2, 1, comparer)); // Index + Count >= list.Count
});
}
[Fact]
public static void Capacity_Get()
{
var arrList = Helpers.CreateIntArrayList(10);
Helpers.PerformActionOnAllArrayListWrappers(arrList, arrList2 =>
{
Assert.True(arrList2.Capacity >= arrList2.Count);
});
}
[Fact]
public static void Capacity_Set()
{
var arrList = Helpers.CreateIntArrayList(10);
int nCapacity = 2 * arrList.Capacity;
arrList.Capacity = nCapacity;
Assert.Equal(nCapacity, arrList.Capacity);
// Synchronized
arrList = ArrayList.Synchronized(new ArrayList(Helpers.CreateIntArray(10)));
arrList.Capacity = 1000;
Assert.Equal(1000, arrList.Capacity);
// Range ignores setter
arrList = new ArrayList(Helpers.CreateIntArray(10)).GetRange(0, arrList.Count);
arrList.Capacity = 1000;
Assert.NotEqual(1000, arrList.Capacity);
}
[Fact]
public static void Capacity_Set_Zero()
{
var arrList = new ArrayList(1);
arrList.Capacity = 0;
Assert.Equal(4, arrList.Capacity);
for (int i = 0; i < 32; i++)
arrList.Add(i);
for (int i = 0; i < 32; i++)
Assert.Equal(i, arrList[i]);
}
[Fact]
public static void Capacity_Set_One()
{
var arrList = new ArrayList(4);
arrList.Capacity = 1;
Assert.Equal(1, arrList.Capacity);
for (int i = 0; i < 32; i++)
arrList.Add(i);
for (int i = 0; i < 32; i++)
Assert.Equal(i, arrList[i]);
}
[Fact]
public static void Capacity_Set_InvalidValue_ThrowsArgumentOutOfRangeException()
{
ArrayList arrList1 = Helpers.CreateIntArrayList(10);
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
if (arrList2.IsFixedSize)
{
return;
}
AssertExtensions.Throws<ArgumentOutOfRangeException>("value", () => arrList2.Capacity = -1); // Capacity < 0
AssertExtensions.Throws<ArgumentOutOfRangeException>("value", () => arrList2.Capacity = arrList1.Count - 1); // Capacity < list.Count
});
}
[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(10)]
[InlineData(100)]
public static void Clone(int count)
{
// Clone should exactly replicate a collection to another object reference
// afterwards these 2 should not hold the same object references
ArrayList arrList1 = Helpers.CreateIntArrayList(count);
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
ArrayList clone = (ArrayList)arrList2.Clone();
Assert.Equal(arrList2.Count, clone.Count);
Assert.Equal(arrList2.IsReadOnly, clone.IsReadOnly);
Assert.Equal(arrList2.IsSynchronized, clone.IsSynchronized);
Assert.Equal(arrList2.IsFixedSize, clone.IsFixedSize);
for (int i = 0; i < arrList2.Count; i++)
{
Assert.Equal(arrList2[i], clone[i]);
}
});
}
[Fact]
public static void Clone_IsShallowCopy()
{
var arrList = new ArrayList();
for (int i = 0; i < 10; i++)
{
arrList.Add(new Foo());
}
ArrayList clone = (ArrayList)arrList.Clone();
string stringValue = "Hello World";
for (int i = 0; i < 10; i++)
{
Assert.Equal(stringValue, ((Foo)clone[i]).StringValue);
}
// Now we remove an object from the original list, but this should still be present in the clone
arrList.RemoveAt(9);
Assert.Equal(stringValue, ((Foo)clone[9]).StringValue);
stringValue = "Good Bye";
((Foo)arrList[0]).StringValue = stringValue;
Assert.Equal(stringValue, ((Foo)arrList[0]).StringValue);
Assert.Equal(stringValue, ((Foo)clone[0]).StringValue);
// If we change the object, of course, the previous should not happen
clone[0] = new Foo();
stringValue = "Good Bye";
Assert.Equal(stringValue, ((Foo)arrList[0]).StringValue);
stringValue = "Hello World";
Assert.Equal(stringValue, ((Foo)clone[0]).StringValue);
}
[Fact]
public static void CopyTo_Int()
{
int index = 1;
ArrayList arrList1 = Helpers.CreateIntArrayList(10);
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
var arrCopy = new int[arrList2.Count + index];
arrCopy.SetValue(400, 0);
arrList2.CopyTo(arrCopy, index);
Assert.Equal(arrList2.Count + index, arrCopy.Length);
for (int i = 0; i < arrCopy.Length; i++)
{
if (i == 0)
{
Assert.Equal(400, arrCopy.GetValue(i));
}
else
{
Assert.Equal(arrList2[i - 1], arrCopy.GetValue(i));
}
}
});
}
[Fact]
public static void CopyTo_Int_EqualToLength()
{
var arrList1 = new ArrayList();
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
var arrCopy = new string[2];
arrList2.CopyTo(arrCopy, arrCopy.Length); // Should not throw
});
}
[Fact]
public static void CopyTo_Int_EmptyArrayListToFilledArray()
{
var arrList1 = new ArrayList();
var arrCopy = new string[10];
for (int i = 0; i < arrCopy.Length; i++)
{
arrCopy[i] = "a";
}
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
arrList2.CopyTo(arrCopy, 3);
// Make sure sentinels stay the same
for (int i = 0; i < arrCopy.Length; i++)
{
Assert.Equal("a", arrCopy[i]);
}
});
}
[Fact]
public static void CopyTo_Int_EmptyArray()
{
var arrList1 = new ArrayList();
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
var arrCopy = new string[0];
arrList2.CopyTo(arrCopy, 0);
Assert.Equal(0, arrCopy.Length);
});
}
[Fact]
public static void CopyTo_Int_Invalid()
{
ArrayList arrList1 = Helpers.CreateIntArrayList(10);
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
var arrCopy = new int[arrList2.Count];
Assert.Throws<ArgumentNullException>(() => arrList2.CopyTo(null)); // Array is null
AssertExtensions.Throws<ArgumentException>("array", null, () => arrList2.CopyTo(new object[10, 10])); // Array is multidimensional
Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.CopyTo(arrCopy, -1)); // Index < 0
bool hasParamName = arrList2.GetType().Name != "Range";
AssertExtensions.Throws<ArgumentException>(hasParamName ? "destinationArray" : null, hasParamName ? "" : null, () => arrList2.CopyTo(new object[11], 2)); // Invalid index and length
});
}
[Fact]
public static void CopyTo_Int_Int()
{
int index = 3;
int count = 3;
ArrayList arrList1 = Helpers.CreateIntArrayList(10);
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
var arrCopy = new int[100];
arrList2.CopyTo(index, arrCopy, index, count);
Assert.Equal(100, arrCopy.Length);
for (int i = index; i < index + count; i++)
{
Assert.Equal(arrList2[i], arrCopy[i]);
}
});
}
[Fact]
public static void CopyTo_Int_Int_EmptyArrayListToFilledArray()
{
var arrList1 = new ArrayList();
var arrCopy = new string[10];
for (int i = 0; i < arrCopy.Length; i++)
{
arrCopy[i] = "a";
}
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
arrList2.CopyTo(0, arrCopy, 3, 0);
// Make sure sentinels stay the same
for (int i = 0; i < arrCopy.Length; i++)
{
Assert.Equal("a", arrCopy[i]);
}
});
}
[Fact]
public static void CopyTo_Int_Int_Invalid()
{
ArrayList arrList1 = Helpers.CreateIntArrayList(10);
Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 =>
{
var arrCopy = new string[10];
Assert.ThrowsAny<ArgumentException>(() => arrList2.CopyTo(0, arrCopy, -1, 1000)); // Array index < 0 (should throw ArgumentOutOfRangeException)
Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.CopyTo(-1, arrCopy, 0, 1)); // Index < 0
Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.CopyTo(0, arrCopy, 0, -1)); // Count < 0
AssertExtensions.Throws<ArgumentException>(null, () =>
{
arrCopy = new string[100];
arrList2.CopyTo(arrList2.Count - 1, arrCopy, 0, 24);
});
Assert.Throws<ArgumentNullException>(() => arrList2.CopyTo(0, null, 3, 3)); // Array is null
AssertExtensions.Throws<ArgumentException>("array", null, () => arrList2.CopyTo(0, new object[arrList2.Count, arrList2.Count], 0, arrList2.Count)); // Array is multidimensional
// Array index and count is out of bounds
AssertExtensions.Throws<ArgumentException>(null, () =>
{
arrCopy = new string[1];
arrList2.CopyTo(0, arrCopy, 3, 15);
});
Assert.ThrowsAny<ArgumentException>(() => arrList2.CopyTo(0, new object[arrList2.Count, arrList2.Count], 0, -1)); // Should throw ArgumentOutOfRangeException
});
}
[Fact]
public static void FixedSize_ArrayList()
{
ArrayList arrList1 = Helpers.CreateIntArrayList(10);
ArrayList arrList2 = ArrayList.FixedSize(arrList1);
Assert.True(arrList2.IsFixedSize);
Assert.False(arrList2.IsReadOnly);
Assert.False(arrList2.IsSynchronized);
Assert.Equal(arrList1.Count, arrList2.Count);
for (int i = 0; i < arrList1.Count; i++)
{
Assert.Equal(arrList1[i], arrList2[i]);
}
// Remove an object from the original list and verify the object underneath has been cut
arrList1.RemoveAt(9);
AssertExtensions.Throws<ArgumentOutOfRangeException>("index", () => arrList2[9]);
// We cant remove or add to the fixed list
Assert.Throws<NotSupportedException>(() => arrList2.RemoveRange(0, 1));
Assert.Throws<NotSupportedException>(() => arrList2.AddRange(new ArrayList()));
Assert.Throws<NotSupportedException>(() => arrList2.InsertRange(0, new ArrayList()));
Assert.Throws<NotSupportedException>(() => arrList2.TrimToSize());
Assert.Throws<NotSupportedException>(() => arrList2.Capacity = 10);
}
[Fact]
public static void FixedSize_ReadOnlyArrayList()
{
ArrayList arrList = ArrayList.FixedSize(ArrayList.ReadOnly(new ArrayList()));
Assert.True(arrList.IsFixedSize);
Assert.True(arrList.IsReadOnly);
Assert.False(arrList.IsSynchronized);
}
[Fact]
public static void FixedSize_SynchronizedArrayList()
{
ArrayList arrList = ArrayList.FixedSize(ArrayList.Synchronized(new ArrayList()));
Assert.True(arrList.IsFixedSize);
Assert.False(arrList.IsReadOnly);
Assert.True(arrList.IsSynchronized);
}
[Fact]
public static void FixedSize_RangeArrayList()
{
ArrayList arrList = ArrayList.FixedSize(new ArrayList()).GetRange(0, 0);
Assert.True(arrList.IsFixedSize);
}
[Fact]
public static void FixedSize_ArrayList_CanChangeExistingItems()
{
ArrayList arrList1 = Helpers.CreateIntArrayList(10);
ArrayList arrList2 = ArrayList.FixedSize(arrList1);
arrList2[0] = 10;
Assert.Equal(10, arrList2[0]);
}
[Fact]
public static void FixedSize_ArrayList_NullCollection_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("list", () => ArrayList.FixedSize(null)); // List is null
}
[Fact]
public static void FixedSize_IList()
{
ArrayList arrList = Helpers.CreateIntArrayList(10);
IList iList = ArrayList.FixedSize((IList)arrList);
Assert.True(iList.IsFixedSize);
Assert.False(iList.IsReadOnly);
Assert.False(iList.IsSynchronized);
Assert.Equal(arrList.Count, iList.Count);
for (int i = 0; i < arrList.Count; i++)
{
Assert.Equal(arrList[i], iList[i]);
}
}
[Fact]
public static void FixedSize_SynchronizedIList()
{
IList iList = ArrayList.FixedSize((IList)ArrayList.Synchronized(new ArrayList()));
Assert.True(iList.IsFixedSize);
Assert.False(iList.IsReadOnly);
Assert.True(iList.IsSynchronized);
}
[Fact]
public static void FixedSize_IList_ModifyingUnderlyingCollection_CutsFacade()
{
ArrayList arrList = Helpers.CreateIntArrayList(10);
IList iList = ArrayList.FixedSize((IList)arrList);
// Remove an object from the original list. Verify the object underneath has been cut
arrList.RemoveAt(9);
AssertExtensions.Throws<ArgumentOutOfRangeException>("index", () => iList[9]);
}