1 // ArrayListTest.cs - NUnit Test Cases for the System.Collections.ArrayList class
3 // David Brandt (bucky@keystreams.com)
5 // (C) Ximian, Inc. http://www.ximian.com
9 using System
.Collections
;
11 using NUnit
.Framework
;
13 namespace MonoTests
.System
.Collections
{
16 public class ArrayListTest
: Assertion
{
18 public void TestCtor() {
20 ArrayList al1
= new ArrayList();
21 AssertNotNull("no basic ArrayList", al1
);
24 bool errorThrown
= false;
26 ArrayList a
= new ArrayList(null);
27 } catch (ArgumentNullException
) {
30 Assert("null icollection error not thrown",
34 // what can I say? I like chars. [--DB]
35 char[] coll
= {'a', 'b', 'c', 'd'}
;
36 ArrayList al1
= new ArrayList(coll
);
37 AssertNotNull("no icollection ArrayList", al1
);
38 for (int i
= 0; i
< coll
.Length
; i
++) {
39 AssertEquals(i
+ " not ctor'ed properly.",
45 Char
[,] c1
= new Char
[2,2];
46 ArrayList al1
= new ArrayList(c1
);
47 Fail ("Should fail with multi-dimensional array in constructor.");
48 } catch (RankException
) {
53 bool errorThrown
= false;
55 ArrayList a
= new ArrayList(-1);
56 } catch (ArgumentOutOfRangeException
) {
59 Assert("negative capacity error not thrown",
64 public void TestCapacity() {
65 for (int i
= 1; i
< 100; i
++) {
66 ArrayList al1
= new ArrayList(i
);
67 AssertEquals("Bad capacity of " + i
,
71 ArrayList al1
= new ArrayList(0);
73 // AssertEquals("Bad capacity when set to 0",
76 AssertEquals("Bad capacity when set to 0",
80 ArrayList al1
= new ArrayList();
81 AssertEquals("Bad default capacity",
86 public void TestCount() {
88 ArrayList al1
= new ArrayList();
89 AssertEquals("Bad initial count",
91 for (int i
= 1; i
<= 100; i
++) {
93 AssertEquals("Bad count " + i
,
97 for (int i
= 0; i
< 100; i
++) {
98 char[] coll
= new Char
[i
];
99 ArrayList al1
= new ArrayList(coll
);
100 AssertEquals("Bad count for " + i
,
105 public void TestIsFixed() {
106 ArrayList al1
= new ArrayList();
107 Assert("should not be fixed by default", !al1
.IsFixedSize
);
108 ArrayList al2
= ArrayList
.FixedSize(al1
);
109 Assert("fixed-size wrapper not working", al2
.IsFixedSize
);
112 public void TestIsReadOnly() {
113 ArrayList al1
= new ArrayList();
114 Assert("should not be ReadOnly by default", !al1
.IsReadOnly
);
115 ArrayList al2
= ArrayList
.ReadOnly(al1
);
116 Assert("read-only wrapper not working", al2
.IsReadOnly
);
119 public void TestIsSynchronized() {
120 ArrayList al1
= new ArrayList();
121 Assert("should not be synchronized by default",
122 !al1
.IsSynchronized
);
123 ArrayList al2
= ArrayList
.Synchronized(al1
);
124 Assert("synchronized wrapper not working", al2
.IsSynchronized
);
127 public void TestItem() {
128 ArrayList al1
= new ArrayList();
130 bool errorThrown
= false;
133 } catch (ArgumentOutOfRangeException
) {
136 Assert("negative item error not thrown",
140 bool errorThrown
= false;
143 } catch (ArgumentOutOfRangeException
) {
146 Assert("past-end item error not thrown",
149 for (int i
= 0; i
<= 100; i
++) {
152 for (int i
= 0; i
<= 100; i
++) {
153 AssertEquals("item not fetched for " + i
,
158 public void TestAdapter() {
160 bool errorThrown
= false;
162 ArrayList al1
= ArrayList
.Adapter(null);
163 } catch (ArgumentNullException
) {
165 } catch (Exception e
) {
166 Fail ("Incorrect exception thrown at 1: " + e
.ToString());
168 Assert("null adapter error not thrown",
172 char[] list
= {'a', 'b', 'c', 'd'}
;
173 ArrayList al1
= ArrayList
.Adapter(list
);
174 AssertNotNull("Couldn't get an adapter", al1
);
175 for (int i
= 0; i
< list
.Length
; i
++) {
176 AssertEquals("adapter not adapting", list
[i
], al1
[i
]);
179 for (int i
= 0; i
< list
.Length
; i
++) {
180 AssertEquals("adapter not adapting", list
[i
], al1
[i
]);
183 // Test Binary Search
185 bool errorThrown
= false;
188 String
[] s1
= {"This", "is", "a", "test"}
;
189 ArrayList al1
= ArrayList
.Adapter (s1
);
190 al1
.BinarySearch(42);
191 } catch (InvalidOperationException
) {
192 // this is what .NET throws
194 } catch (ArgumentException
) {
195 // this is what the docs say it should throw
197 } catch (Exception e
) {
198 Fail ("Incorrect exception thrown at 1: " + e
.ToString());
200 Assert("search-for-wrong-type error not thrown",
205 char[] arr
= {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'}
;
206 ArrayList al1
= ArrayList
.Adapter (arr
);
207 Assert("couldn't find elem #1",
208 al1
.BinarySearch('c') >= 3);
209 Assert("couldn't find elem #2",
210 al1
.BinarySearch('c') < 6);
213 char[] arr
= {'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'}
;
214 ArrayList al1
= ArrayList
.Adapter (arr
);
215 AssertEquals("couldn't find next-higher elem",
216 -4, al1
.BinarySearch('c'));
219 char[] arr
= {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'}
;
220 ArrayList al1
= ArrayList
.Adapter (arr
);
221 AssertEquals("couldn't find end",
222 -9, al1
.BinarySearch('e'));
226 char[] starter
= {'d', 'b', 'f', 'e', 'a', 'c'}
;
227 ArrayList al1
= ArrayList
.Adapter (starter
);
229 AssertEquals("Should be sorted", 'a', al1
[0]);
230 AssertEquals("Should be sorted", 'b', al1
[1]);
231 AssertEquals("Should be sorted", 'c', al1
[2]);
232 AssertEquals("Should be sorted", 'd', al1
[3]);
233 AssertEquals("Should be sorted", 'e', al1
[4]);
234 AssertEquals("Should be sorted", 'f', al1
[5]);
237 // TODO - test other adapter types?
240 public void TestAdd() {
242 bool errorThrown
= false;
245 ArrayList
.FixedSize(new ArrayList());
247 } catch (NotSupportedException
) {
249 } catch (Exception e
) {
250 Fail ("Incorrect exception thrown at 1: " + e
.ToString());
252 Assert("add to fixed size error not thrown",
256 bool errorThrown
= false;
259 ArrayList
.ReadOnly(new ArrayList());
261 } catch (NotSupportedException
) {
263 } catch (Exception e
) {
264 Fail ("Incorrect exception thrown at 2: " + e
.ToString());
266 Assert("add to read only error not thrown",
270 ArrayList al1
= new ArrayList();
271 for (int i
= 1; i
<= 100; i
++) {
273 AssertEquals("add failed " + i
,
275 AssertEquals("add failed " + i
,
281 string [] strArray
= new string [] {};
282 ArrayList al1
= new ArrayList (strArray
);
285 AssertEquals ("add failed", 2, al1
.Count
);
289 public void TestAddRange() {
291 bool errorThrown
= false;
294 ArrayList
.FixedSize(new ArrayList());
295 String
[] s1
= {"Hi!"}
;
297 } catch (NotSupportedException
) {
299 } catch (Exception e
) {
300 Fail ("Incorrect exception thrown at 1: " + e
.ToString());
302 Assert("add to fixed size error not thrown",
306 bool errorThrown
= false;
309 ArrayList
.ReadOnly(new ArrayList());
310 String
[] s1
= {"Hi!"}
;
312 } catch (NotSupportedException
) {
314 } catch (Exception e
) {
315 Fail ("Incorrect exception thrown at 2: " + e
.ToString());
317 Assert("add to read only error not thrown",
321 bool errorThrown
= false;
323 ArrayList al1
= new ArrayList();
325 } catch (ArgumentNullException
) {
327 } catch (Exception e
) {
328 Fail ("Incorrect exception thrown at 3: " + e
.ToString());
330 Assert("add to read only error not thrown",
335 ArrayList a1
= new ArrayList();
336 AssertEquals("ArrayList should start empty",
338 char[] coll
= {'a', 'b', 'c'}
;
340 AssertEquals("ArrayList has wrong elements",
343 AssertEquals("ArrayList has wrong elements",
348 ArrayList list
= new ArrayList ();
350 for (int i
= 0; i
< 100; i
++) {
354 AssertEquals ("BinarySearch off-by-one bug",
355 49, list
.BinarySearch (1));
359 public void TestBinarySearch() {
361 bool errorThrown
= false;
363 ArrayList al1
= new ArrayList();
364 String
[] s1
= {"This", "is", "a", "test"}
;
366 al1
.BinarySearch(42);
367 } catch (InvalidOperationException
) {
368 // this is what .NET throws
370 } catch (ArgumentException
) {
371 // this is what the docs say it should throw
373 } catch (Exception e
) {
374 Fail ("Incorrect exception thrown at 1: " + e
.ToString());
376 Assert("search-for-wrong-type error not thrown",
381 char[] arr
= {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'}
;
382 ArrayList al1
= new ArrayList(arr
);
383 Assert("couldn't find elem #1",
384 al1
.BinarySearch('c') >= 3);
385 Assert("couldn't find elem #2",
386 al1
.BinarySearch('c') < 6);
389 char[] arr
= {'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'}
;
390 ArrayList al1
= new ArrayList(arr
);
391 AssertEquals("couldn't find next-higher elem",
392 -4, al1
.BinarySearch('c'));
395 char[] arr
= {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'}
;
396 ArrayList al1
= new ArrayList(arr
);
397 AssertEquals("couldn't find end",
398 -9, al1
.BinarySearch('e'));
404 [ExpectedException (typeof (ArgumentException
))]
405 public void BinarySearch_IndexOverflow ()
407 ArrayList al
= new ArrayList ();
409 al
.BinarySearch (Int32
.MaxValue
, 1, this, null);
413 [ExpectedException (typeof (ArgumentException
))]
414 public void BinarySearch_CountOverflow ()
416 ArrayList al
= new ArrayList ();
418 al
.BinarySearch (1, Int32
.MaxValue
, this, null);
422 public void BinarySearch_Null ()
424 ArrayList al
= new ArrayList ();
426 AssertEquals ("null", -1, al
.BinarySearch (null));
429 // TODO - BinarySearch with IComparer
431 public void TestClear() {
433 bool errorThrown
= false;
436 ArrayList
.FixedSize(new ArrayList());
438 } catch (NotSupportedException
) {
441 Assert("add to fixed size error not thrown",
445 bool errorThrown
= false;
448 ArrayList
.ReadOnly(new ArrayList());
450 } catch (NotSupportedException
) {
453 Assert("add to read only error not thrown",
457 ArrayList al1
= new ArrayList();
459 AssertEquals("should have one element",
462 AssertEquals("should be empty",
466 int[] i1
= {1,2,3,4}
;
467 ArrayList al1
= new ArrayList(i1
);
468 AssertEquals("should have elements",
469 i1
.Length
, al1
.Count
);
470 int capacity
= al1
.Capacity
;
472 AssertEquals("should be empty again",
474 AssertEquals("capacity shouldn't have changed",
475 capacity
, al1
.Capacity
);
479 public void TestClone() {
481 char[] c1
= {'a', 'b', 'c'}
;
482 ArrayList al1
= new ArrayList(c1
);
483 ArrayList al2
= (ArrayList
)al1
.Clone();
484 AssertEquals("ArrayList match", al1
[0], al2
[0]);
485 AssertEquals("ArrayList match", al1
[1], al2
[1]);
486 AssertEquals("ArrayList match", al1
[2], al2
[2]);
489 char[] d10
= {'a', 'b'}
;
490 char[] d11
= {'a', 'c'}
;
491 char[] d12
= {'b', 'c'}
;
492 char[][] d1
= {d10, d11, d12}
;
493 ArrayList al1
= new ArrayList(d1
);
494 ArrayList al2
= (ArrayList
)al1
.Clone();
495 AssertEquals("Array match", al1
[0], al2
[0]);
496 AssertEquals("Array match", al1
[1], al2
[1]);
497 AssertEquals("Array match", al1
[2], al2
[2]);
499 ((char[])al1
[0])[0] = 'z';
500 AssertEquals("shallow copy", al1
[0], al2
[0]);
504 public void TestContains() {
505 char[] c1
= {'a', 'b', 'c'}
;
506 ArrayList al1
= new ArrayList(c1
);
507 Assert("never find a null", !al1
.Contains(null));
508 Assert("can't find value", al1
.Contains('b'));
509 Assert("shouldn't find value", !al1
.Contains('?'));
512 public void TestCopyTo() {
514 bool errorThrown
= false;
516 Char
[] c1
= new Char
[2];
517 ArrayList al1
= new ArrayList(c1
);
519 } catch (ArgumentNullException
) {
521 } catch (Exception e
) {
522 Fail ("Incorrect exception thrown at 1: " + e
.ToString());
524 Assert("error not thrown 1", errorThrown
);
527 bool errorThrown
= false;
529 Char
[] c1
= new Char
[2];
530 ArrayList al1
= new ArrayList(c1
);
531 Char
[,] c2
= new Char
[2,2];
533 } catch (ArgumentException
) {
535 } catch (Exception e
) {
536 Fail ("Incorrect exception thrown at 2: " + e
.ToString());
538 Assert("error not thrown 2", errorThrown
);
541 bool errorThrown
= false;
543 // This appears to be a bug in the ArrayList Constructor.
544 // It throws a RankException if a multidimensional Array
545 // is passed. The docs imply that an IEnumerator is used
546 // to retrieve the items from the collection, so this should
547 // work. In anycase this test is for CopyTo, so use what
548 // works on both platforms.
549 //Char[,] c1 = new Char[2,2];
550 Char
[] c1
= new Char
[2];
551 ArrayList al1
= new ArrayList(c1
);
552 Char
[] c2
= new Char
[2];
554 } catch (ArgumentException
) {
556 } catch (Exception e
) {
557 Fail ("Incorrect exception thrown at 3: " + e
.ToString());
559 Assert("error not thrown 3", errorThrown
);
562 bool errorThrown
= false;
564 Char
[] c1
= new Char
[2];
565 ArrayList al1
= new ArrayList(c1
);
566 Char
[] c2
= new Char
[2];
568 } catch (ArgumentOutOfRangeException
) {
570 } catch (Exception e
) {
571 Fail ("Incorrect exception thrown at 4: " + e
.ToString());
573 Assert("error not thrown 4", errorThrown
);
576 bool errorThrown
= false;
578 Char
[] c1
= new Char
[2];
579 ArrayList al1
= new ArrayList(c1
);
580 Char
[] c2
= new Char
[2];
582 } catch (ArgumentException
) {
584 } catch (Exception e
) {
585 Fail ("Incorrect exception thrown at 5: " + e
.ToString());
587 Assert("error not thrown 5", errorThrown
);
590 bool errorThrown
= false;
592 Char
[] c1
= new Char
[2];
593 ArrayList al1
= new ArrayList(c1
);
594 Char
[] c2
= new Char
[2];
596 } catch (ArgumentException
) {
598 } catch (Exception e
) {
599 Fail ("Incorrect exception thrown at 6: " + e
.ToString());
601 Assert("error not thrown 6", errorThrown
);
604 bool errorThrown
= false;
606 String
[] c1
= {"String", "array"}
;
607 ArrayList al1
= new ArrayList(c1
);
608 Char
[] c2
= new Char
[2];
610 } catch (InvalidCastException
) {
612 } catch (Exception e
) {
613 Fail ("Incorrect exception thrown at 7: " + e
.ToString());
615 Assert("error not thrown 7", errorThrown
);
618 Char
[] orig
= {'a', 'b', 'c', 'd'}
;
619 ArrayList al
= new ArrayList(orig
);
620 Char
[] copy
= new Char
[10];
621 Array
.Clear(copy
, 0, copy
.Length
);
623 AssertEquals("Wrong CopyTo 0", (char)0, copy
[0]);
624 AssertEquals("Wrong CopyTo 1", (char)0, copy
[1]);
625 AssertEquals("Wrong CopyTo 2", (char)0, copy
[2]);
626 AssertEquals("Wrong CopyTo 3", orig
[0], copy
[3]);
627 AssertEquals("Wrong CopyTo 4", orig
[1], copy
[4]);
628 AssertEquals("Wrong CopyTo 5", orig
[2], copy
[5]);
629 AssertEquals("Wrong CopyTo 6", orig
[3], copy
[6]);
630 AssertEquals("Wrong CopyTo 7", (char)0, copy
[7]);
631 AssertEquals("Wrong CopyTo 8", (char)0, copy
[8]);
632 AssertEquals("Wrong CopyTo 9", (char)0, copy
[9]);
636 [ExpectedException (typeof (ArgumentException
))]
637 public void CopyTo_IndexOverflow ()
639 ArrayList al
= new ArrayList ();
641 al
.CopyTo (Int32
.MaxValue
, new byte [2], 0, 0);
645 [ExpectedException (typeof (ArgumentException
))]
646 public void CopyTo_ArrayIndexOverflow ()
648 ArrayList al
= new ArrayList ();
650 al
.CopyTo (0, new byte [2], Int32
.MaxValue
, 0);
654 [ExpectedException (typeof (ArgumentException
))]
655 public void CopyTo_CountOverflow ()
657 ArrayList al
= new ArrayList ();
659 al
.CopyTo (0, new byte [2], 0, Int32
.MaxValue
);
662 public void TestFixedSize() {
664 bool errorThrown
= false;
666 ArrayList al1
= ArrayList
.FixedSize(null);
667 } catch (ArgumentNullException
) {
670 Assert("null arg error not thrown", errorThrown
);
673 ArrayList al1
= new ArrayList();
674 AssertEquals("arrays start un-fixed.",
675 false, al1
.IsFixedSize
);
676 ArrayList al2
= ArrayList
.FixedSize(al1
);
677 AssertEquals("should be fixed.",
678 true, al2
.IsFixedSize
);
682 public void TestEnumerator() {
683 String
[] s1
= {"this", "is", "a", "test"}
;
684 ArrayList al1
= new ArrayList(s1
);
685 IEnumerator en
= al1
.GetEnumerator();
687 al1
.Add("something");
690 Fail("Add() didn't invalidate the enumerator");
691 } catch (InvalidOperationException
) {
692 // do nothing...this is what we expect
695 en
= al1
.GetEnumerator();
700 Fail("AddRange() didn't invalidate the enumerator");
701 } catch (InvalidOperationException
) {
702 // do nothing...this is what we expect
705 en
= al1
.GetEnumerator();
710 Fail("Clear() didn't invalidate the enumerator");
711 } catch (InvalidOperationException
) {
712 // do nothing...this is what we expect
715 al1
= new ArrayList(s1
);
716 en
= al1
.GetEnumerator();
718 al1
.Insert(0, "new first");
721 Fail("Insert() didn't invalidate the enumerator");
722 } catch (InvalidOperationException
) {
723 // do nothing...this is what we expect
726 en
= al1
.GetEnumerator();
728 al1
.InsertRange(0, al1
);
731 Fail("InsertRange() didn't invalidate the enumerator");
732 } catch (InvalidOperationException
) {
733 // do nothing...this is what we expect
736 en
= al1
.GetEnumerator();
741 Fail("Remove() didn't invalidate the enumerator");
742 } catch (InvalidOperationException
) {
743 // do nothing...this is what we expect
746 en
= al1
.GetEnumerator();
751 Fail("RemoveAt() didn't invalidate the enumerator");
752 } catch (InvalidOperationException
) {
753 // do nothing...this is what we expect
756 en
= al1
.GetEnumerator();
758 al1
.RemoveRange(1, 1);
761 Fail("RemoveRange() didn't invalidate the enumerator");
762 } catch (InvalidOperationException
) {
763 // do nothing...this is what we expect
766 en
= al1
.GetEnumerator();
771 Fail("Reverse() didn't invalidate the enumerator");
772 } catch (InvalidOperationException
) {
773 // do nothing...this is what we expect
776 en
= al1
.GetEnumerator();
781 Fail("Sort() didn't invalidate the enumerator");
782 } catch (InvalidOperationException
) {
783 // do nothing...this is what we expect
787 public void TestGetEnumerator() {
789 bool errorThrown
= false;
791 ArrayList a
= new ArrayList();
792 IEnumerator en
= a
.GetEnumerator(-1,1);
793 } catch (ArgumentOutOfRangeException
) {
796 Assert("negative index error not thrown",
800 bool errorThrown
= false;
802 ArrayList a
= new ArrayList();
803 IEnumerator en
= a
.GetEnumerator(1,-1);
804 } catch (ArgumentOutOfRangeException
) {
807 Assert("negative index error not thrown",
811 bool errorThrown
= false;
813 ArrayList a
= new ArrayList();
814 IEnumerator en
= a
.GetEnumerator(1,1);
815 } catch (ArgumentException
) {
818 Assert("out-of-range index error not thrown",
822 String
[] s1
= {"this", "is", "a", "test"}
;
823 ArrayList al1
= new ArrayList(s1
);
824 IEnumerator en
= al1
.GetEnumerator();
825 AssertNotNull("No enumerator", en
);
827 for (int i
= 0; i
< s1
.Length
; i
++) {
829 AssertEquals("Not enumerating",
834 String
[] s1
= {"this", "is", "a", "test"}
;
835 ArrayList al1
= new ArrayList(s1
);
836 IEnumerator en
= al1
.GetEnumerator(1,2);
837 AssertNotNull("No enumerator", en
);
839 for (int i
= 0; i
< 2; i
++) {
841 AssertEquals("Not enumerating",
842 al1
[i
+1], en
.Current
);
848 [ExpectedException (typeof (ArgumentException
))]
849 public void GetEnumerator_IndexOverflow ()
851 ArrayList al
= new ArrayList ();
853 al
.GetEnumerator (Int32
.MaxValue
, 0);
857 [ExpectedException (typeof (ArgumentException
))]
858 public void GetEnumerator_CountOverflow ()
860 ArrayList al
= new ArrayList ();
862 al
.GetEnumerator (0, Int32
.MaxValue
);
865 public void TestGetRange() {
867 bool errorThrown
= false;
869 ArrayList a
= new ArrayList();
870 ArrayList b
= a
.GetRange(-1,1);
871 } catch (ArgumentOutOfRangeException
) {
874 Assert("negative index error not thrown",
878 bool errorThrown
= false;
880 ArrayList a
= new ArrayList();
881 ArrayList b
= a
.GetRange(1,-1);
882 } catch (ArgumentOutOfRangeException
) {
885 Assert("negative index error not thrown",
889 bool errorThrown
= false;
891 ArrayList a
= new ArrayList();
892 ArrayList b
= a
.GetRange(1,1);
893 } catch (ArgumentException
) {
896 Assert("out-of-range index error not thrown",
900 char[] chars
= {'a', 'b', 'c', 'd', 'e', 'f'}
;
901 ArrayList a
= new ArrayList(chars
);
902 ArrayList b
= a
.GetRange(1, 3);
903 AssertEquals("GetRange returned wrong size ArrayList", 3, b
.Count
);
904 for (int i
= 0; i
< b
.Count
; i
++) {
905 AssertEquals("range didn't work",
909 a
[2] = '?'; // should screw up ArrayList b.
910 bool errorThrown
= false;
913 } catch (InvalidOperationException
) {
916 AssertEquals("Munging 'a' should mess up 'b'",
922 [ExpectedException (typeof (ArgumentException
))]
923 public void GetRange_IndexOverflow ()
925 ArrayList al
= new ArrayList ();
927 al
.GetRange (Int32
.MaxValue
, 0);
931 [ExpectedException (typeof (ArgumentException
))]
932 public void GetRange_CountOverflow ()
934 ArrayList al
= new ArrayList ();
936 al
.GetRange (0, Int32
.MaxValue
);
939 public void TestIndexOf() {
941 bool errorThrown
= false;
943 ArrayList a
= new ArrayList(1);
944 int i
= a
.IndexOf('a', -1);
945 } catch (ArgumentOutOfRangeException
) {
948 Assert("negative indexof error not thrown",
952 bool errorThrown
= false;
954 ArrayList a
= new ArrayList(1);
955 int i
= a
.IndexOf('a', 2);
956 } catch (ArgumentOutOfRangeException
) {
959 Assert("past-end indexof error not thrown",
963 bool errorThrown
= false;
965 ArrayList a
= new ArrayList(1);
966 int i
= a
.IndexOf('a', 0, -1);
967 } catch (ArgumentOutOfRangeException
) {
970 Assert("negative indexof error not thrown",
974 bool errorThrown
= false;
976 ArrayList a
= new ArrayList(1);
977 int i
= a
.IndexOf('a', 0, 2);
978 } catch (ArgumentOutOfRangeException
) {
981 Assert("past-end indexof error not thrown",
985 bool errorThrown
= false;
987 ArrayList a
= new ArrayList(2);
988 int i
= a
.IndexOf('a', 1, 2);
989 } catch (ArgumentOutOfRangeException
) {
992 Assert("past-end indexof error not thrown",
996 char[] c
= {'a', 'b', 'c', 'd', 'e'}
;
997 ArrayList a
= new ArrayList(c
);
998 AssertEquals("never find null",
999 -1, a
.IndexOf(null));
1000 AssertEquals("never find null",
1001 -1, a
.IndexOf(null, 0));
1002 AssertEquals("never find null",
1003 -1, a
.IndexOf(null, 0, 5));
1004 AssertEquals("can't find elem",
1006 AssertEquals("can't find elem",
1007 2, a
.IndexOf('c', 2));
1008 AssertEquals("can't find elem",
1009 2, a
.IndexOf('c', 2, 2));
1010 AssertEquals("shouldn't find elem",
1011 -1, a
.IndexOf('c', 3, 2));
1012 AssertEquals("shouldn't find", -1, a
.IndexOf('?'));
1013 AssertEquals("shouldn't find", -1, a
.IndexOf(3));
1018 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
1019 public void IndexOf_StartIndexOverflow ()
1021 ArrayList al
= new ArrayList ();
1023 al
.IndexOf ('a', Int32
.MaxValue
, 1);
1027 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
1028 public void IndexOf_CountOverflow ()
1030 ArrayList al
= new ArrayList ();
1032 al
.IndexOf ('a', 1, Int32
.MaxValue
);
1035 public void TestInsert() {
1037 bool errorThrown
= false;
1040 ArrayList
.FixedSize(new ArrayList());
1041 al1
.Insert(0, "Hi!");
1042 } catch (NotSupportedException
) {
1045 Assert("insert to fixed size error not thrown",
1049 bool errorThrown
= false;
1052 ArrayList
.ReadOnly(new ArrayList());
1053 al1
.Insert(0, "Hi!");
1054 } catch (NotSupportedException
) {
1057 Assert("insert to read only error not thrown",
1061 bool errorThrown
= false;
1063 ArrayList al1
= new ArrayList(3);
1064 al1
.Insert(-1, "Hi!");
1065 } catch (ArgumentOutOfRangeException
) {
1068 Assert("insert to read only error not thrown",
1072 bool errorThrown
= false;
1074 ArrayList al1
= new ArrayList(3);
1075 al1
.Insert(4, "Hi!");
1076 } catch (ArgumentOutOfRangeException
) {
1079 Assert("insert to read only error not thrown",
1083 ArrayList al1
= new ArrayList();
1084 AssertEquals("arraylist starts empty", 0, al1
.Count
);
1088 AssertEquals("arraylist needs stuff", 3, al1
.Count
);
1089 AssertEquals("arraylist got stuff", 'c', al1
[0]);
1090 AssertEquals("arraylist got stuff", 'a', al1
[1]);
1091 AssertEquals("arraylist got stuff", 'b', al1
[2]);
1095 public void TestInsertRange() {
1097 bool errorThrown
= false;
1100 ArrayList
.FixedSize(new ArrayList());
1101 string[] s
= {"Hi!"}
;
1102 al1
.InsertRange(0, s
);
1103 } catch (NotSupportedException
) {
1106 Assert("insert to fixed size error not thrown",
1110 bool errorThrown
= false;
1113 ArrayList
.ReadOnly(new ArrayList());
1114 string[] s
= {"Hi!"}
;
1115 al1
.InsertRange(0, s
);
1116 } catch (NotSupportedException
) {
1119 Assert("insert to read only error not thrown",
1123 bool errorThrown
= false;
1125 ArrayList al1
= new ArrayList(3);
1126 string[] s
= {"Hi!"}
;
1127 al1
.InsertRange(-1, s
);
1128 } catch (ArgumentOutOfRangeException
) {
1131 Assert("negative index insert error not thrown",
1135 bool errorThrown
= false;
1137 ArrayList al1
= new ArrayList(3);
1138 string[] s
= {"Hi!"}
;
1139 al1
.InsertRange(4, s
);
1140 } catch (ArgumentOutOfRangeException
) {
1143 Assert("out-of-range insert error not thrown",
1147 bool errorThrown
= false;
1149 ArrayList al1
= new ArrayList(3);
1150 al1
.InsertRange(0, null);
1151 } catch (ArgumentNullException
) {
1154 Assert("null insert error not thrown",
1158 char[] c
= {'a', 'b', 'c'}
;
1159 ArrayList a
= new ArrayList(c
);
1160 a
.InsertRange(1, c
);
1161 AssertEquals("bad insert 1", 'a', a
[0]);
1162 AssertEquals("bad insert 2", 'a', a
[1]);
1163 AssertEquals("bad insert 3", 'b', a
[2]);
1164 AssertEquals("bad insert 4", 'c', a
[3]);
1165 AssertEquals("bad insert 5", 'b', a
[4]);
1166 AssertEquals("bad insert 6", 'c', a
[5]);
1170 public void TestLastIndexOf() {
1172 //bool errorThrown = false;
1174 //ArrayList a = new ArrayList(1);
1175 //int i = a.LastIndexOf('a', -1);
1176 //} catch (ArgumentOutOfRangeException) {
1177 //errorThrown = true;
1179 //Assert("first negative lastindexof error not thrown",
1183 bool errorThrown
= false;
1185 ArrayList a
= new ArrayList(1);
1186 int i
= a
.LastIndexOf('a', 2);
1187 } catch (ArgumentOutOfRangeException
) {
1190 Assert("past-end lastindexof error not thrown",
1194 //bool errorThrown = false;
1196 //ArrayList a = new ArrayList(1);
1197 //int i = a.LastIndexOf('a', 0, -1);
1198 //} catch (ArgumentOutOfRangeException) {
1199 //errorThrown = true;
1201 //Assert("second negative lastindexof error not thrown",
1205 //bool errorThrown = false;
1207 //ArrayList a = new ArrayList(1);
1208 //int i = a.LastIndexOf('a', 0, 2);
1209 //} catch (ArgumentOutOfRangeException) {
1210 //errorThrown = true;
1212 //Assert("past-end lastindexof error not thrown",
1216 //bool errorThrown = false;
1218 //ArrayList a = new ArrayList(2);
1219 //int i = a.LastIndexOf('a', 0, 2);
1220 //} catch (ArgumentOutOfRangeException) {
1221 //errorThrown = true;
1223 //Assert("past-end lastindexof error not thrown",
1228 char[] c
= {'a', 'b', 'c', 'd', 'e'}
;
1229 ArrayList a
= new ArrayList(c
);
1230 AssertEquals("never find null",
1231 -1, a
.LastIndexOf(null));
1233 AssertEquals("never find null",
1234 -1, a
.LastIndexOf(null, 4));
1236 AssertEquals("never find null",
1237 -1, a
.LastIndexOf(null, 4, 5));
1239 AssertEquals("can't find elem",
1240 2, a
.LastIndexOf('c'));
1242 AssertEquals("can't find elem",
1243 2, a
.LastIndexOf('c', 4));
1245 AssertEquals("can't find elem",
1246 2, a
.LastIndexOf('c', 3, 2));
1248 AssertEquals("shouldn't find elem",
1249 -1, a
.LastIndexOf('c', 4, 2));
1251 AssertEquals("shouldn't find", -1, a
.LastIndexOf('?'));
1253 AssertEquals("shouldn't find", -1, a
.LastIndexOf(1));
1254 } catch (Exception e
) {
1255 Fail ("Unexpected exception caught when iTest=" + iTest
+ ". e=" + e
);
1260 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
1261 public void LastIndexOf_StartIndexOverflow ()
1263 ArrayList al
= new ArrayList ();
1265 al
.LastIndexOf ('a', Int32
.MaxValue
, 1);
1269 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
1270 public void LastIndexOf_CountOverflow ()
1272 ArrayList al
= new ArrayList ();
1274 al
.LastIndexOf ('a', 1, Int32
.MaxValue
);
1277 public void TestReadOnly() {
1279 bool errorThrown
= false;
1281 ArrayList al1
= ArrayList
.ReadOnly(null);
1282 } catch (ArgumentNullException
) {
1285 Assert("null arg error not thrown", errorThrown
);
1288 ArrayList al1
= new ArrayList();
1289 AssertEquals("arrays start writeable.",
1290 false, al1
.IsReadOnly
);
1291 ArrayList al2
= ArrayList
.ReadOnly(al1
);
1292 AssertEquals("should be readonly.",
1293 true, al2
.IsReadOnly
);
1297 public void TestRemove() {
1299 bool errorThrown
= false;
1302 ArrayList
.FixedSize(new ArrayList(3));
1304 } catch (NotSupportedException
) {
1307 Assert("remove fixed size error not thrown",
1311 bool errorThrown
= false;
1314 ArrayList
.ReadOnly(new ArrayList(3));
1316 } catch (NotSupportedException
) {
1319 Assert("remove read only error not thrown",
1323 char[] c
= {'a','b','c'}
;
1324 ArrayList a
= new ArrayList(c
);
1327 AssertEquals("should be unchanged", c
.Length
, a
.Count
);
1329 AssertEquals("should be changed", 2, a
.Count
);
1330 AssertEquals("should have shifted", 'b', a
[0]);
1331 AssertEquals("should have shifted", 'c', a
[1]);
1335 public void TestRemoveAt() {
1337 bool errorThrown
= false;
1340 ArrayList
.FixedSize(new ArrayList(3));
1342 } catch (NotSupportedException
) {
1345 Assert("remove from fixed size error not thrown",
1349 bool errorThrown
= false;
1352 ArrayList
.ReadOnly(new ArrayList(3));
1354 } catch (NotSupportedException
) {
1357 Assert("remove from read only error not thrown",
1361 bool errorThrown
= false;
1363 ArrayList al1
= new ArrayList(3);
1365 } catch (ArgumentOutOfRangeException
) {
1368 Assert("remove at negative index error not thrown",
1372 bool errorThrown
= false;
1374 ArrayList al1
= new ArrayList(3);
1376 } catch (ArgumentOutOfRangeException
) {
1379 Assert("remove at out-of-range index error not thrown",
1383 char[] c
= {'a','b','c'}
;
1384 ArrayList a
= new ArrayList(c
);
1386 AssertEquals("should be changed", 2, a
.Count
);
1387 AssertEquals("should have shifted", 'b', a
[0]);
1388 AssertEquals("should have shifted", 'c', a
[1]);
1392 public void TestRemoveRange() {
1394 bool errorThrown
= false;
1397 ArrayList
.FixedSize(new ArrayList(3));
1398 al1
.RemoveRange(0, 1);
1399 } catch (NotSupportedException
) {
1402 Assert("removerange from fixed size error not thrown",
1406 bool errorThrown
= false;
1409 ArrayList
.ReadOnly(new ArrayList(3));
1410 al1
.RemoveRange(0, 1);
1411 } catch (NotSupportedException
) {
1414 Assert("removerange from read only error not thrown",
1418 bool errorThrown
= false;
1420 ArrayList al1
= new ArrayList(3);
1421 al1
.RemoveRange(-1, 1);
1422 } catch (ArgumentOutOfRangeException
) {
1425 Assert("removerange at negative index error not thrown",
1429 bool errorThrown
= false;
1431 ArrayList al1
= new ArrayList(3);
1432 al1
.RemoveRange(0, -1);
1433 } catch (ArgumentOutOfRangeException
) {
1436 Assert("removerange at negative index error not thrown",
1440 bool errorThrown
= false;
1442 ArrayList al1
= new ArrayList(3);
1443 al1
.RemoveRange(2, 3);
1444 } catch (ArgumentException
) {
1447 Assert("removerange at bad range error not thrown",
1451 char[] c
= {'a','b','c'}
;
1452 ArrayList a
= new ArrayList(c
);
1454 AssertEquals("should be changed", 1, a
.Count
);
1455 AssertEquals("should have shifted", 'a', a
[0]);
1460 [ExpectedException (typeof (ArgumentException
))]
1461 public void RemoveRange_IndexOverflow ()
1463 ArrayList al
= new ArrayList ();
1465 al
.RemoveRange (Int32
.MaxValue
, 1);
1469 [ExpectedException (typeof (ArgumentException
))]
1470 public void RemoveRange_CountOverflow ()
1472 ArrayList al
= new ArrayList ();
1474 al
.RemoveRange (1, Int32
.MaxValue
);
1477 public void TestRepeat() {
1479 bool errorThrown
= false;
1481 ArrayList al1
= ArrayList
.Repeat('c', -1);
1482 } catch (ArgumentOutOfRangeException
) {
1485 Assert("repeat negative copies error not thrown",
1489 ArrayList al1
= ArrayList
.Repeat("huh?", 0);
1490 AssertEquals("should be nothing in array",
1494 ArrayList al1
= ArrayList
.Repeat("huh?", 3);
1495 AssertEquals("should be something in array",
1497 AssertEquals("array elem doesn't check",
1499 AssertEquals("array elem doesn't check",
1501 AssertEquals("array elem doesn't check",
1506 public void TestReverse() {
1508 bool errorThrown
= false;
1511 ArrayList
.ReadOnly(new ArrayList());
1513 } catch (NotSupportedException
) {
1516 Assert("reverse on read only error not thrown",
1520 bool errorThrown
= false;
1522 char[] c
= new Char
[2];
1523 ArrayList al1
= new ArrayList(c
);
1525 } catch (ArgumentException
) {
1528 Assert("error not thrown", errorThrown
);
1531 bool errorThrown
= false;
1533 char[] c
= new Char
[2];
1534 ArrayList al1
= new ArrayList(c
);
1536 } catch (ArgumentException
) {
1539 Assert("error not thrown", errorThrown
);
1542 char[] c
= {'a', 'b', 'c', 'd', 'e'}
;
1543 ArrayList al1
= new ArrayList(c
);
1545 for (int i
= 0; i
< al1
.Count
; i
++) {
1546 AssertEquals("Should be no change yet",
1550 for (int i
= 0; i
< al1
.Count
; i
++) {
1551 AssertEquals("Should be reversed",
1555 for (int i
= 0; i
< al1
.Count
; i
++) {
1556 AssertEquals("Should be back to normal",
1560 AssertEquals("Should be back to normal", c
[0], al1
[0]);
1561 AssertEquals("Should be back to normal", c
[3], al1
[1]);
1562 AssertEquals("Should be back to normal", c
[2], al1
[2]);
1563 AssertEquals("Should be back to normal", c
[1], al1
[3]);
1564 AssertEquals("Should be back to normal", c
[4], al1
[4]);
1569 [ExpectedException (typeof (ArgumentException
))]
1570 public void Reverse_IndexOverflow ()
1572 ArrayList al
= new ArrayList ();
1574 al
.Reverse (Int32
.MaxValue
, 1);
1578 [ExpectedException (typeof (ArgumentException
))]
1579 public void Reverse_CountOverflow ()
1581 ArrayList al
= new ArrayList ();
1583 al
.Reverse (1, Int32
.MaxValue
);
1586 public void TestSetRange() {
1588 bool errorThrown
= false;
1590 char[] c
= {'a', 'b', 'c'}
;
1592 ArrayList
.ReadOnly(new ArrayList(3));
1594 } catch (NotSupportedException
) {
1596 } catch (Exception e
) {
1597 Fail ("Incorrect exception thrown at 1: " + e
.ToString());
1599 Assert("setrange on read only error not thrown",
1603 bool errorThrown
= false;
1605 ArrayList al1
= new ArrayList(3);
1606 al1
.SetRange(0, null);
1607 } catch (ArgumentNullException
) {
1609 } catch (ArgumentOutOfRangeException
) {
1611 } catch (Exception e
) {
1612 Fail ("Incorrect exception thrown at 2: " + e
.ToString());
1614 Assert("setrange with null error not thrown",
1618 bool errorThrown
= false;
1620 char[] c
= {'a', 'b', 'c'}
;
1621 ArrayList al1
= new ArrayList(3);
1622 al1
.SetRange(-1, c
);
1623 } catch (ArgumentOutOfRangeException
) {
1625 } catch (Exception e
) {
1626 Fail ("Incorrect exception thrown at 3: " + e
.ToString());
1628 Assert("setrange with negative index error not thrown",
1632 bool errorThrown
= false;
1634 char[] c
= {'a', 'b', 'c'}
;
1635 ArrayList al1
= new ArrayList(3);
1637 } catch (ArgumentOutOfRangeException
) {
1639 } catch (Exception e
) {
1640 Fail ("Incorrect exception thrown at 4: " + e
.ToString());
1642 Assert("setrange with too much error not thrown",
1647 char[] c
= {'a', 'b', 'c'}
;
1648 ArrayList al1
= ArrayList
.Repeat('?', 3);
1649 Assert("no match yet", c
[0] != (char)al1
[0]);
1650 Assert("no match yet", c
[1] != (char)al1
[1]);
1651 Assert("no match yet", c
[2] != (char)al1
[2]);
1653 AssertEquals("should match", c
[0], al1
[0]);
1654 AssertEquals("should match", c
[1], al1
[1]);
1655 AssertEquals("should match", c
[2], al1
[2]);
1660 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
1661 public void SetRange_Overflow ()
1663 ArrayList al
= new ArrayList ();
1665 al
.SetRange (Int32
.MaxValue
, new ArrayList ());
1668 public void TestInsertRange_this() {
1669 String
[] s1
= {"this", "is", "a", "test"}
;
1670 ArrayList al
= new ArrayList(s1
);
1671 al
.InsertRange(2, al
);
1672 String
[] s2
= {"this", "is", "this", "is", "a", "test", "a", "test"}
;
1673 for (int i
=0; i
< al
.Count
; i
++) {
1674 AssertEquals("at i=" + i
, s2
[i
], al
[i
]);
1678 public void TestSort() {
1680 bool errorThrown
= false;
1683 ArrayList
.ReadOnly(new ArrayList());
1685 } catch (NotSupportedException
) {
1688 Assert("sort on read only error not thrown",
1692 char[] starter
= {'d', 'b', 'f', 'e', 'a', 'c'}
;
1693 ArrayList al1
= new ArrayList(starter
);
1695 AssertEquals("Should be sorted", 'a', al1
[0]);
1696 AssertEquals("Should be sorted", 'b', al1
[1]);
1697 AssertEquals("Should be sorted", 'c', al1
[2]);
1698 AssertEquals("Should be sorted", 'd', al1
[3]);
1699 AssertEquals("Should be sorted", 'e', al1
[4]);
1700 AssertEquals("Should be sorted", 'f', al1
[5]);
1703 ArrayList al1
= new ArrayList ();
1712 AssertEquals ("Should be null", null, al1
[0]);
1713 AssertEquals ("Should be 2. null", null, al1
[1]);
1714 AssertEquals ("Should be 3. null", null, al1
[2]);
1715 AssertEquals ("Should be 4. null", null, al1
[3]);
1716 AssertEquals ("Should be 32", 32, al1
[4]);
1717 AssertEquals ("Should be 33", 33, al1
[5]);
1722 [ExpectedException (typeof (ArgumentException
))]
1723 public void Sort_IndexOverflow ()
1725 ArrayList al
= new ArrayList ();
1727 al
.Sort (Int32
.MaxValue
, 1, null);
1731 [ExpectedException (typeof (ArgumentException
))]
1732 public void Sort_CountOverflow ()
1734 ArrayList al
= new ArrayList ();
1736 al
.Sort (1, Int32
.MaxValue
, null);
1739 // TODO - Sort with IComparers
1741 // TODO - Synchronize
1743 public void TestToArray() {
1745 bool errorThrown
= false;
1747 ArrayList al1
= new ArrayList(3);
1749 } catch (ArgumentNullException
) {
1752 Assert("toarray with null error not thrown",
1756 bool errorThrown
= false;
1758 char[] c
= {'a', 'b', 'c'}
;
1760 ArrayList al1
= new ArrayList(c
);
1761 al1
.ToArray(s
.GetType());
1762 } catch (InvalidCastException
) {
1765 Assert("toarray with bad type error not thrown",
1769 char[] c1
= {'a', 'b', 'c', 'd', 'e'}
;
1770 ArrayList al1
= new ArrayList(c1
);
1771 object[] o2
= al1
.ToArray();
1772 for (int i
= 0; i
< c1
.Length
; i
++) {
1773 AssertEquals("should be copy", c1
[i
], o2
[i
]);
1775 Array c2
= al1
.ToArray(c1
[0].GetType());
1776 for (int i
= 0; i
< c1
.Length
; i
++) {
1777 AssertEquals("should be copy",
1778 c1
[i
], c2
.GetValue(i
));
1783 public void TestTrimToSize() {
1785 bool errorThrown
= false;
1788 ArrayList
.ReadOnly(new ArrayList());
1790 } catch (NotSupportedException
) {
1793 Assert("trim read only error not thrown",
1797 ArrayList al1
= new ArrayList();
1798 int capacity
= al1
.Capacity
;
1799 int size
= capacity
/ 2;
1800 for (int i
= 1; i
<=size
; i
++) {
1805 AssertEquals("no capacity match",
1806 size
- 1, al1
.Capacity
);
1810 AssertEquals("no default capacity",
1811 capacity
, al1
.Capacity
);