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
6 // Copyright (C) 2005 Novell (http://www.novell.com)
10 using System.Collections;
12 using NUnit.Framework;
14 namespace MonoTests.System.Collections
17 public class ArrayListTest : Assertion
20 public void TestCtor ()
23 ArrayList al1 = new ArrayList ();
24 AssertNotNull ("no basic ArrayList", al1);
27 bool errorThrown = false;
29 ArrayList a = new ArrayList (null);
30 } catch (ArgumentNullException) {
33 Assert ("null icollection error not thrown",
37 // what can I say? I like chars. [--DB]
38 char [] coll = { 'a', 'b', 'c', 'd' };
39 ArrayList al1 = new ArrayList (coll);
40 AssertNotNull ("no icollection ArrayList", al1);
41 for (int i = 0; i < coll.Length; i++) {
42 AssertEquals (i + " not ctor'ed properly.",
48 Char [,] c1 = new Char [2, 2];
49 ArrayList al1 = new ArrayList (c1);
50 Fail ("Should fail with multi-dimensional array in constructor.");
51 } catch (RankException) {
56 bool errorThrown = false;
58 ArrayList a = new ArrayList (-1);
59 } catch (ArgumentOutOfRangeException) {
62 Assert ("negative capacity error not thrown",
68 public void TestCapacity ()
71 int default_capacity = 4;
72 int unspecified_capacity = 0;
74 int default_capacity = 16;
75 int unspecified_capacity = 16;
77 for (int i = 1; i < 100; i++) {
78 ArrayList al1 = new ArrayList (i);
79 AssertEquals ("Bad capacity of " + i,
83 ArrayList al1 = new ArrayList (0);
85 // AssertEquals("Bad capacity when set to 0",
88 AssertEquals ("Bad capacity when set to 0",
89 default_capacity, al1.Capacity);
92 ArrayList al1 = new ArrayList ();
93 AssertEquals ("Bad default capacity",
94 unspecified_capacity, al1.Capacity);
99 public void TestCount ()
102 ArrayList al1 = new ArrayList ();
103 AssertEquals ("Bad initial count",
105 for (int i = 1; i <= 100; i++) {
107 AssertEquals ("Bad count " + i,
111 for (int i = 0; i < 100; i++) {
112 char [] coll = new Char [i];
113 ArrayList al1 = new ArrayList (coll);
114 AssertEquals ("Bad count for " + i,
120 public void TestIsFixed ()
122 ArrayList al1 = new ArrayList ();
123 Assert ("should not be fixed by default", !al1.IsFixedSize);
124 ArrayList al2 = ArrayList.FixedSize (al1);
125 Assert ("fixed-size wrapper not working", al2.IsFixedSize);
129 public void TestIsReadOnly ()
131 ArrayList al1 = new ArrayList ();
132 Assert ("should not be ReadOnly by default", !al1.IsReadOnly);
133 ArrayList al2 = ArrayList.ReadOnly (al1);
134 Assert ("read-only wrapper not working", al2.IsReadOnly);
138 public void TestIsSynchronized ()
140 ArrayList al1 = new ArrayList ();
141 Assert ("should not be synchronized by default",
142 !al1.IsSynchronized);
143 ArrayList al2 = ArrayList.Synchronized (al1);
144 Assert ("synchronized wrapper not working", al2.IsSynchronized);
148 public void TestItem ()
150 ArrayList al1 = new ArrayList ();
152 bool errorThrown = false;
155 } catch (ArgumentOutOfRangeException) {
158 Assert ("negative item error not thrown",
162 bool errorThrown = false;
165 } catch (ArgumentOutOfRangeException) {
168 Assert ("past-end item error not thrown",
171 for (int i = 0; i <= 100; i++) {
174 for (int i = 0; i <= 100; i++) {
175 AssertEquals ("item not fetched for " + i,
181 public void TestAdapter ()
184 bool errorThrown = false;
186 ArrayList al1 = ArrayList.Adapter (null);
187 } catch (ArgumentNullException) {
189 } catch (Exception e) {
190 Fail ("Incorrect exception thrown at 1: " + e.ToString ());
192 Assert ("null adapter error not thrown",
196 char [] list = { 'a', 'b', 'c', 'd' };
197 ArrayList al1 = ArrayList.Adapter (list);
198 AssertNotNull ("Couldn't get an adapter", al1);
199 for (int i = 0; i < list.Length; i++) {
200 AssertEquals ("adapter not adapting", list [i], al1 [i]);
203 for (int i = 0; i < list.Length; i++) {
204 AssertEquals ("adapter not adapting", list [i], al1 [i]);
207 // Test Binary Search
209 bool errorThrown = false;
212 String [] s1 = { "This", "is", "a", "test" };
213 ArrayList al1 = ArrayList.Adapter (s1);
214 al1.BinarySearch (42);
215 } catch (InvalidOperationException) {
216 // this is what .NET throws
218 } catch (ArgumentException) {
219 // this is what the docs say it should throw
221 } catch (Exception e) {
222 Fail ("Incorrect exception thrown at 1: " + e.ToString ());
224 Assert ("search-for-wrong-type error not thrown",
229 char [] arr = { 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd' };
230 ArrayList al1 = ArrayList.Adapter (arr);
231 Assert ("couldn't find elem #1",
232 al1.BinarySearch ('c') >= 3);
233 Assert ("couldn't find elem #2",
234 al1.BinarySearch ('c') < 6);
237 char [] arr = { 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e' };
238 ArrayList al1 = ArrayList.Adapter (arr);
239 AssertEquals ("couldn't find next-higher elem",
240 -4, al1.BinarySearch ('c'));
243 char [] arr = { 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd' };
244 ArrayList al1 = ArrayList.Adapter (arr);
245 AssertEquals ("couldn't find end",
246 -9, al1.BinarySearch ('e'));
250 char [] starter = { 'd', 'b', 'f', 'e', 'a', 'c' };
251 ArrayList al1 = ArrayList.Adapter (starter);
253 AssertEquals ("Should be sorted", 'a', al1 [0]);
254 AssertEquals ("Should be sorted", 'b', al1 [1]);
255 AssertEquals ("Should be sorted", 'c', al1 [2]);
256 AssertEquals ("Should be sorted", 'd', al1 [3]);
257 AssertEquals ("Should be sorted", 'e', al1 [4]);
258 AssertEquals ("Should be sorted", 'f', al1 [5]);
261 // TODO - test other adapter types?
265 public void TestAdd ()
268 bool errorThrown = false;
271 ArrayList.FixedSize (new ArrayList ());
273 } catch (NotSupportedException) {
275 } catch (Exception e) {
276 Fail ("Incorrect exception thrown at 1: " + e.ToString ());
278 Assert ("add to fixed size error not thrown",
282 bool errorThrown = false;
285 ArrayList.ReadOnly (new ArrayList ());
287 } catch (NotSupportedException) {
289 } catch (Exception e) {
290 Fail ("Incorrect exception thrown at 2: " + e.ToString ());
292 Assert ("add to read only error not thrown",
296 ArrayList al1 = new ArrayList ();
297 for (int i = 1; i <= 100; i++) {
299 AssertEquals ("add failed " + i,
301 AssertEquals ("add failed " + i,
307 string [] strArray = new string [] { };
308 ArrayList al1 = new ArrayList (strArray);
311 AssertEquals ("add failed", 2, al1.Count);
316 public void TestAddRange ()
319 bool errorThrown = false;
322 ArrayList.FixedSize (new ArrayList ());
323 String [] s1 = { "Hi!" };
325 } catch (NotSupportedException) {
327 } catch (Exception e) {
328 Fail ("Incorrect exception thrown at 1: " + e.ToString ());
330 Assert ("add to fixed size error not thrown",
334 bool errorThrown = false;
337 ArrayList.ReadOnly (new ArrayList ());
338 String [] s1 = { "Hi!" };
340 } catch (NotSupportedException) {
342 } catch (Exception e) {
343 Fail ("Incorrect exception thrown at 2: " + e.ToString ());
345 Assert ("add to read only error not thrown",
349 bool errorThrown = false;
351 ArrayList al1 = new ArrayList ();
353 } catch (ArgumentNullException) {
355 } catch (Exception e) {
356 Fail ("Incorrect exception thrown at 3: " + e.ToString ());
358 Assert ("add to read only error not thrown",
363 ArrayList a1 = new ArrayList ();
364 AssertEquals ("ArrayList should start empty",
366 char [] coll = { 'a', 'b', 'c' };
368 AssertEquals ("ArrayList has wrong elements",
371 AssertEquals ("ArrayList has wrong elements",
376 ArrayList list = new ArrayList ();
378 for (int i = 0; i < 100; i++) {
382 AssertEquals ("BinarySearch off-by-one bug",
383 49, list.BinarySearch (1));
388 public void TestBinarySearch ()
391 bool errorThrown = false;
393 ArrayList al1 = new ArrayList ();
394 String [] s1 = { "This", "is", "a", "test" };
396 al1.BinarySearch (42);
397 } catch (InvalidOperationException) {
398 // this is what .NET throws
400 } catch (ArgumentException) {
401 // this is what the docs say it should throw
403 } catch (Exception e) {
404 Fail ("Incorrect exception thrown at 1: " + e.ToString ());
406 Assert ("search-for-wrong-type error not thrown",
411 char [] arr = { 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd' };
412 ArrayList al1 = new ArrayList (arr);
413 Assert ("couldn't find elem #1",
414 al1.BinarySearch ('c') >= 3);
415 Assert ("couldn't find elem #2",
416 al1.BinarySearch ('c') < 6);
419 char [] arr = { 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e' };
420 ArrayList al1 = new ArrayList (arr);
421 AssertEquals ("couldn't find next-higher elem",
422 -4, al1.BinarySearch ('c'));
425 char [] arr = { 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd' };
426 ArrayList al1 = new ArrayList (arr);
427 AssertEquals ("couldn't find end",
428 -9, al1.BinarySearch ('e'));
434 [ExpectedException (typeof (ArgumentException))]
435 public void BinarySearch_IndexOverflow ()
437 ArrayList al = new ArrayList ();
439 al.BinarySearch (Int32.MaxValue, 1, this, null);
443 [ExpectedException (typeof (ArgumentException))]
444 public void BinarySearch_CountOverflow ()
446 ArrayList al = new ArrayList ();
448 al.BinarySearch (1, Int32.MaxValue, this, null);
452 public void BinarySearch_Null ()
454 ArrayList al = new ArrayList ();
456 AssertEquals ("null", -1, al.BinarySearch (null));
459 // TODO - BinarySearch with IComparer
462 public void TestClear ()
465 bool errorThrown = false;
468 ArrayList.FixedSize (new ArrayList ());
470 } catch (NotSupportedException) {
473 Assert ("add to fixed size error not thrown",
477 bool errorThrown = false;
480 ArrayList.ReadOnly (new ArrayList ());
482 } catch (NotSupportedException) {
485 Assert ("add to read only error not thrown",
489 ArrayList al1 = new ArrayList ();
491 AssertEquals ("should have one element",
494 AssertEquals ("should be empty",
498 int [] i1 = { 1, 2, 3, 4 };
499 ArrayList al1 = new ArrayList (i1);
500 AssertEquals ("should have elements",
501 i1.Length, al1.Count);
502 int capacity = al1.Capacity;
504 AssertEquals ("should be empty again",
506 AssertEquals ("capacity shouldn't have changed",
507 capacity, al1.Capacity);
512 public void TestClone ()
515 char [] c1 = { 'a', 'b', 'c' };
516 ArrayList al1 = new ArrayList (c1);
517 ArrayList al2 = (ArrayList) al1.Clone ();
518 AssertEquals ("ArrayList match", al1 [0], al2 [0]);
519 AssertEquals ("ArrayList match", al1 [1], al2 [1]);
520 AssertEquals ("ArrayList match", al1 [2], al2 [2]);
523 char [] d10 = { 'a', 'b' };
524 char [] d11 = { 'a', 'c' };
525 char [] d12 = { 'b', 'c' };
526 char [] [] d1 = { d10, d11, d12 };
527 ArrayList al1 = new ArrayList (d1);
528 ArrayList al2 = (ArrayList) al1.Clone ();
529 AssertEquals ("Array match", al1 [0], al2 [0]);
530 AssertEquals ("Array match", al1 [1], al2 [1]);
531 AssertEquals ("Array match", al1 [2], al2 [2]);
533 ((char []) al1 [0]) [0] = 'z';
534 AssertEquals ("shallow copy", al1 [0], al2 [0]);
539 public void TestContains ()
541 char [] c1 = { 'a', 'b', 'c' };
542 ArrayList al1 = new ArrayList (c1);
543 Assert ("never find a null", !al1.Contains (null));
544 Assert ("can't find value", al1.Contains ('b'));
545 Assert ("shouldn't find value", !al1.Contains ('?'));
549 public void TestCopyTo ()
552 bool errorThrown = false;
554 Char [] c1 = new Char [2];
555 ArrayList al1 = new ArrayList (c1);
556 al1.CopyTo (null, 2);
557 } catch (ArgumentNullException) {
559 } catch (Exception e) {
560 Fail ("Incorrect exception thrown at 1: " + e.ToString ());
562 Assert ("error not thrown 1", errorThrown);
565 bool errorThrown = false;
567 Char [] c1 = new Char [2];
568 ArrayList al1 = new ArrayList (c1);
569 Char [,] c2 = new Char [2, 2];
571 } catch (ArgumentException) {
573 } catch (Exception e) {
574 Fail ("Incorrect exception thrown at 2: " + e.ToString ());
576 Assert ("error not thrown 2", errorThrown);
579 bool errorThrown = false;
581 // This appears to be a bug in the ArrayList Constructor.
582 // It throws a RankException if a multidimensional Array
583 // is passed. The docs imply that an IEnumerator is used
584 // to retrieve the items from the collection, so this should
585 // work. In anycase this test is for CopyTo, so use what
586 // works on both platforms.
587 //Char[,] c1 = new Char[2,2];
588 Char [] c1 = new Char [2];
589 ArrayList al1 = new ArrayList (c1);
590 Char [] c2 = new Char [2];
592 } catch (ArgumentException) {
594 } catch (Exception e) {
595 Fail ("Incorrect exception thrown at 3: " + e.ToString ());
597 Assert ("error not thrown 3", errorThrown);
600 bool errorThrown = false;
602 Char [] c1 = new Char [2];
603 ArrayList al1 = new ArrayList (c1);
604 Char [] c2 = new Char [2];
606 } catch (ArgumentOutOfRangeException) {
608 } catch (Exception e) {
609 Fail ("Incorrect exception thrown at 4: " + e.ToString ());
611 Assert ("error not thrown 4", errorThrown);
614 bool errorThrown = false;
616 Char [] c1 = new Char [2];
617 ArrayList al1 = new ArrayList (c1);
618 Char [] c2 = new Char [2];
620 } catch (ArgumentException) {
622 } catch (Exception e) {
623 Fail ("Incorrect exception thrown at 5: " + e.ToString ());
625 Assert ("error not thrown 5", errorThrown);
628 bool errorThrown = false;
630 Char [] c1 = new Char [2];
631 ArrayList al1 = new ArrayList (c1);
632 Char [] c2 = new Char [2];
634 } catch (ArgumentException) {
636 } catch (Exception e) {
637 Fail ("Incorrect exception thrown at 6: " + e.ToString ());
639 Assert ("error not thrown 6", errorThrown);
642 bool errorThrown = false;
644 String [] c1 = { "String", "array" };
645 ArrayList al1 = new ArrayList (c1);
646 Char [] c2 = new Char [2];
648 } catch (InvalidCastException) {
650 } catch (Exception e) {
651 Fail ("Incorrect exception thrown at 7: " + e.ToString ());
653 Assert ("error not thrown 7", errorThrown);
656 Char [] orig = { 'a', 'b', 'c', 'd' };
657 ArrayList al = new ArrayList (orig);
658 Char [] copy = new Char [10];
659 Array.Clear (copy, 0, copy.Length);
661 AssertEquals ("Wrong CopyTo 0", (char) 0, copy [0]);
662 AssertEquals ("Wrong CopyTo 1", (char) 0, copy [1]);
663 AssertEquals ("Wrong CopyTo 2", (char) 0, copy [2]);
664 AssertEquals ("Wrong CopyTo 3", orig [0], copy [3]);
665 AssertEquals ("Wrong CopyTo 4", orig [1], copy [4]);
666 AssertEquals ("Wrong CopyTo 5", orig [2], copy [5]);
667 AssertEquals ("Wrong CopyTo 6", orig [3], copy [6]);
668 AssertEquals ("Wrong CopyTo 7", (char) 0, copy [7]);
669 AssertEquals ("Wrong CopyTo 8", (char) 0, copy [8]);
670 AssertEquals ("Wrong CopyTo 9", (char) 0, copy [9]);
674 [ExpectedException (typeof (ArgumentException))]
675 public void CopyTo_IndexOverflow ()
677 ArrayList al = new ArrayList ();
679 al.CopyTo (Int32.MaxValue, new byte [2], 0, 0);
683 [ExpectedException (typeof (ArgumentException))]
684 public void CopyTo_ArrayIndexOverflow ()
686 ArrayList al = new ArrayList ();
688 al.CopyTo (0, new byte [2], Int32.MaxValue, 0);
692 [ExpectedException (typeof (ArgumentException))]
693 public void CopyTo_CountOverflow ()
695 ArrayList al = new ArrayList ();
697 al.CopyTo (0, new byte [2], 0, Int32.MaxValue);
701 public void TestFixedSize ()
704 bool errorThrown = false;
706 ArrayList al1 = ArrayList.FixedSize (null);
707 } catch (ArgumentNullException) {
710 Assert ("null arg error not thrown", errorThrown);
713 ArrayList al1 = new ArrayList ();
714 AssertEquals ("arrays start un-fixed.",
715 false, al1.IsFixedSize);
716 ArrayList al2 = ArrayList.FixedSize (al1);
717 AssertEquals ("should be fixed.",
718 true, al2.IsFixedSize);
723 public void TestEnumerator ()
725 String [] s1 = { "this", "is", "a", "test" };
726 ArrayList al1 = new ArrayList (s1);
727 IEnumerator en = al1.GetEnumerator ();
729 al1.Add ("something");
732 Fail ("Add() didn't invalidate the enumerator");
733 } catch (InvalidOperationException) {
734 // do nothing...this is what we expect
737 en = al1.GetEnumerator ();
742 Fail ("AddRange() didn't invalidate the enumerator");
743 } catch (InvalidOperationException) {
744 // do nothing...this is what we expect
747 en = al1.GetEnumerator ();
752 Fail ("Clear() didn't invalidate the enumerator");
753 } catch (InvalidOperationException) {
754 // do nothing...this is what we expect
757 al1 = new ArrayList (s1);
758 en = al1.GetEnumerator ();
760 al1.Insert (0, "new first");
763 Fail ("Insert() didn't invalidate the enumerator");
764 } catch (InvalidOperationException) {
765 // do nothing...this is what we expect
768 en = al1.GetEnumerator ();
770 al1.InsertRange (0, al1);
773 Fail ("InsertRange() didn't invalidate the enumerator");
774 } catch (InvalidOperationException) {
775 // do nothing...this is what we expect
778 en = al1.GetEnumerator ();
783 Fail ("Remove() didn't invalidate the enumerator");
784 } catch (InvalidOperationException) {
785 // do nothing...this is what we expect
788 en = al1.GetEnumerator ();
793 Fail ("RemoveAt() didn't invalidate the enumerator");
794 } catch (InvalidOperationException) {
795 // do nothing...this is what we expect
798 en = al1.GetEnumerator ();
800 al1.RemoveRange (1, 1);
803 Fail ("RemoveRange() didn't invalidate the enumerator");
804 } catch (InvalidOperationException) {
805 // do nothing...this is what we expect
808 en = al1.GetEnumerator ();
813 Fail ("Reverse() didn't invalidate the enumerator");
814 } catch (InvalidOperationException) {
815 // do nothing...this is what we expect
818 en = al1.GetEnumerator ();
823 Fail ("Sort() didn't invalidate the enumerator");
824 } catch (InvalidOperationException) {
825 // do nothing...this is what we expect
830 public void TestGetEnumerator ()
833 bool errorThrown = false;
835 ArrayList a = new ArrayList ();
836 IEnumerator en = a.GetEnumerator (-1, 1);
837 } catch (ArgumentOutOfRangeException) {
840 Assert ("negative index error not thrown",
844 bool errorThrown = false;
846 ArrayList a = new ArrayList ();
847 IEnumerator en = a.GetEnumerator (1, -1);
848 } catch (ArgumentOutOfRangeException) {
851 Assert ("negative index error not thrown",
855 bool errorThrown = false;
857 ArrayList a = new ArrayList ();
858 IEnumerator en = a.GetEnumerator (1, 1);
859 } catch (ArgumentException) {
862 Assert ("out-of-range index error not thrown",
866 String [] s1 = { "this", "is", "a", "test" };
867 ArrayList al1 = new ArrayList (s1);
868 IEnumerator en = al1.GetEnumerator ();
869 AssertNotNull ("No enumerator", en);
871 for (int i = 0; i < s1.Length; i++) {
873 AssertEquals ("Not enumerating",
874 al1 [i], en.Current);
878 String [] s1 = { "this", "is", "a", "test" };
879 ArrayList al1 = new ArrayList (s1);
880 IEnumerator en = al1.GetEnumerator (1, 2);
881 AssertNotNull ("No enumerator", en);
883 for (int i = 0; i < 2; i++) {
885 AssertEquals ("Not enumerating",
886 al1 [i + 1], en.Current);
892 [ExpectedException (typeof (ArgumentException))]
893 public void GetEnumerator_IndexOverflow ()
895 ArrayList al = new ArrayList ();
897 al.GetEnumerator (Int32.MaxValue, 0);
901 [ExpectedException (typeof (ArgumentException))]
902 public void GetEnumerator_CountOverflow ()
904 ArrayList al = new ArrayList ();
906 al.GetEnumerator (0, Int32.MaxValue);
910 public void TestGetRange ()
913 bool errorThrown = false;
915 ArrayList a = new ArrayList ();
916 ArrayList b = a.GetRange (-1, 1);
917 } catch (ArgumentOutOfRangeException) {
920 Assert ("negative index error not thrown",
924 bool errorThrown = false;
926 ArrayList a = new ArrayList ();
927 ArrayList b = a.GetRange (1, -1);
928 } catch (ArgumentOutOfRangeException) {
931 Assert ("negative index error not thrown",
935 bool errorThrown = false;
937 ArrayList a = new ArrayList ();
938 ArrayList b = a.GetRange (1, 1);
939 } catch (ArgumentException) {
942 Assert ("out-of-range index error not thrown",
946 char [] chars = { 'a', 'b', 'c', 'd', 'e', 'f' };
947 ArrayList a = new ArrayList (chars);
948 ArrayList b = a.GetRange (1, 3);
949 AssertEquals ("GetRange returned wrong size ArrayList", 3, b.Count);
950 for (int i = 0; i < b.Count; i++) {
951 AssertEquals ("range didn't work",
952 chars [i + 1], b [i]);
955 a [2] = '?'; // should screw up ArrayList b.
956 bool errorThrown = false;
959 } catch (InvalidOperationException) {
962 AssertEquals ("Munging 'a' should mess up 'b'",
966 char [] chars = { 'a', 'b', 'c', 'd', 'e', 'f' };
967 ArrayList a = new ArrayList (chars);
968 ArrayList b = a.GetRange (3, 3);
969 object [] obj_chars = b.ToArray ();
970 for (int i = 0; i < 3; i++) {
971 char c = (char) obj_chars [i];
972 AssertEquals ("range.ToArray didn't work",
975 char [] new_chars = (char []) b.ToArray (typeof (char));
976 for (int i = 0; i < 3; i++) {
977 AssertEquals ("range.ToArray with type didn't work",
978 chars [i + 3], new_chars [i]);
984 [ExpectedException (typeof (ArgumentException))]
985 public void GetRange_IndexOverflow ()
987 ArrayList al = new ArrayList ();
989 al.GetRange (Int32.MaxValue, 0);
993 [ExpectedException (typeof (ArgumentException))]
994 public void GetRange_CountOverflow ()
996 ArrayList al = new ArrayList ();
998 al.GetRange (0, Int32.MaxValue);
1002 public void TestIndexOf ()
1005 bool errorThrown = false;
1007 ArrayList a = new ArrayList (1);
1008 int i = a.IndexOf ('a', -1);
1009 } catch (ArgumentOutOfRangeException) {
1012 Assert ("negative indexof error not thrown",
1016 bool errorThrown = false;
1018 ArrayList a = new ArrayList (1);
1019 int i = a.IndexOf ('a', 2);
1020 } catch (ArgumentOutOfRangeException) {
1023 Assert ("past-end indexof error not thrown",
1027 bool errorThrown = false;
1029 ArrayList a = new ArrayList (1);
1030 int i = a.IndexOf ('a', 0, -1);
1031 } catch (ArgumentOutOfRangeException) {
1034 Assert ("negative indexof error not thrown",
1038 bool errorThrown = false;
1040 ArrayList a = new ArrayList (1);
1041 int i = a.IndexOf ('a', 0, 2);
1042 } catch (ArgumentOutOfRangeException) {
1045 Assert ("past-end indexof error not thrown",
1049 bool errorThrown = false;
1051 ArrayList a = new ArrayList (2);
1052 int i = a.IndexOf ('a', 1, 2);
1053 } catch (ArgumentOutOfRangeException) {
1056 Assert ("past-end indexof error not thrown",
1060 char [] c = { 'a', 'b', 'c', 'd', 'e' };
1061 ArrayList a = new ArrayList (c);
1062 AssertEquals ("never find null",
1063 -1, a.IndexOf (null));
1064 AssertEquals ("never find null",
1065 -1, a.IndexOf (null, 0));
1066 AssertEquals ("never find null",
1067 -1, a.IndexOf (null, 0, 5));
1068 AssertEquals ("can't find elem",
1069 2, a.IndexOf ('c'));
1070 AssertEquals ("can't find elem",
1071 2, a.IndexOf ('c', 2));
1072 AssertEquals ("can't find elem",
1073 2, a.IndexOf ('c', 2, 2));
1074 AssertEquals ("shouldn't find elem",
1075 -1, a.IndexOf ('c', 3, 2));
1076 AssertEquals ("shouldn't find", -1, a.IndexOf ('?'));
1077 AssertEquals ("shouldn't find", -1, a.IndexOf (3));
1082 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1083 public void IndexOf_StartIndexOverflow ()
1085 ArrayList al = new ArrayList ();
1087 al.IndexOf ('a', Int32.MaxValue, 1);
1091 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1092 public void IndexOf_CountOverflow ()
1094 ArrayList al = new ArrayList ();
1096 al.IndexOf ('a', 1, Int32.MaxValue);
1100 public void TestInsert ()
1103 bool errorThrown = false;
1106 ArrayList.FixedSize (new ArrayList ());
1107 al1.Insert (0, "Hi!");
1108 } catch (NotSupportedException) {
1111 Assert ("insert to fixed size error not thrown",
1115 bool errorThrown = false;
1118 ArrayList.ReadOnly (new ArrayList ());
1119 al1.Insert (0, "Hi!");
1120 } catch (NotSupportedException) {
1123 Assert ("insert to read only error not thrown",
1127 bool errorThrown = false;
1129 ArrayList al1 = new ArrayList (3);
1130 al1.Insert (-1, "Hi!");
1131 } catch (ArgumentOutOfRangeException) {
1134 Assert ("insert to read only error not thrown",
1138 bool errorThrown = false;
1140 ArrayList al1 = new ArrayList (3);
1141 al1.Insert (4, "Hi!");
1142 } catch (ArgumentOutOfRangeException) {
1145 Assert ("insert to read only error not thrown",
1149 ArrayList al1 = new ArrayList ();
1150 AssertEquals ("arraylist starts empty", 0, al1.Count);
1151 al1.Insert (0, 'a');
1152 al1.Insert (1, 'b');
1153 al1.Insert (0, 'c');
1154 AssertEquals ("arraylist needs stuff", 3, al1.Count);
1155 AssertEquals ("arraylist got stuff", 'c', al1 [0]);
1156 AssertEquals ("arraylist got stuff", 'a', al1 [1]);
1157 AssertEquals ("arraylist got stuff", 'b', al1 [2]);
1162 public void TestInsertRange ()
1165 bool errorThrown = false;
1168 ArrayList.FixedSize (new ArrayList ());
1169 string [] s = { "Hi!" };
1170 al1.InsertRange (0, s);
1171 } catch (NotSupportedException) {
1174 Assert ("insert to fixed size error not thrown",
1178 bool errorThrown = false;
1181 ArrayList.ReadOnly (new ArrayList ());
1182 string [] s = { "Hi!" };
1183 al1.InsertRange (0, s);
1184 } catch (NotSupportedException) {
1187 Assert ("insert to read only error not thrown",
1191 bool errorThrown = false;
1193 ArrayList al1 = new ArrayList (3);
1194 string [] s = { "Hi!" };
1195 al1.InsertRange (-1, s);
1196 } catch (ArgumentOutOfRangeException) {
1199 Assert ("negative index insert error not thrown",
1203 bool errorThrown = false;
1205 ArrayList al1 = new ArrayList (3);
1206 string [] s = { "Hi!" };
1207 al1.InsertRange (4, s);
1208 } catch (ArgumentOutOfRangeException) {
1211 Assert ("out-of-range insert error not thrown",
1215 bool errorThrown = false;
1217 ArrayList al1 = new ArrayList (3);
1218 al1.InsertRange (0, null);
1219 } catch (ArgumentNullException) {
1222 Assert ("null insert error not thrown",
1226 char [] c = { 'a', 'b', 'c' };
1227 ArrayList a = new ArrayList (c);
1228 a.InsertRange (1, c);
1229 AssertEquals ("bad insert 1", 'a', a [0]);
1230 AssertEquals ("bad insert 2", 'a', a [1]);
1231 AssertEquals ("bad insert 3", 'b', a [2]);
1232 AssertEquals ("bad insert 4", 'c', a [3]);
1233 AssertEquals ("bad insert 5", 'b', a [4]);
1234 AssertEquals ("bad insert 6", 'c', a [5]);
1239 public void TestLastIndexOf ()
1242 //bool errorThrown = false;
1244 //ArrayList a = new ArrayList(1);
1245 //int i = a.LastIndexOf('a', -1);
1246 //} catch (ArgumentOutOfRangeException) {
1247 //errorThrown = true;
1249 //Assert("first negative lastindexof error not thrown",
1253 bool errorThrown = false;
1255 ArrayList a = new ArrayList (1);
1256 int i = a.LastIndexOf ('a', 2);
1257 } catch (ArgumentOutOfRangeException) {
1260 Assert ("past-end lastindexof error not thrown",
1264 //bool errorThrown = false;
1266 //ArrayList a = new ArrayList(1);
1267 //int i = a.LastIndexOf('a', 0, -1);
1268 //} catch (ArgumentOutOfRangeException) {
1269 //errorThrown = true;
1271 //Assert("second negative lastindexof error not thrown",
1275 //bool errorThrown = false;
1277 //ArrayList a = new ArrayList(1);
1278 //int i = a.LastIndexOf('a', 0, 2);
1279 //} catch (ArgumentOutOfRangeException) {
1280 //errorThrown = true;
1282 //Assert("past-end lastindexof error not thrown",
1286 //bool errorThrown = false;
1288 //ArrayList a = new ArrayList(2);
1289 //int i = a.LastIndexOf('a', 0, 2);
1290 //} catch (ArgumentOutOfRangeException) {
1291 //errorThrown = true;
1293 //Assert("past-end lastindexof error not thrown",
1298 char [] c = { 'a', 'b', 'c', 'd', 'e' };
1299 ArrayList a = new ArrayList (c);
1300 AssertEquals ("never find null",
1301 -1, a.LastIndexOf (null));
1303 AssertEquals ("never find null",
1304 -1, a.LastIndexOf (null, 4));
1306 AssertEquals ("never find null",
1307 -1, a.LastIndexOf (null, 4, 5));
1309 AssertEquals ("can't find elem",
1310 2, a.LastIndexOf ('c'));
1312 AssertEquals ("can't find elem",
1313 2, a.LastIndexOf ('c', 4));
1315 AssertEquals ("can't find elem",
1316 2, a.LastIndexOf ('c', 3, 2));
1318 AssertEquals ("shouldn't find elem",
1319 -1, a.LastIndexOf ('c', 4, 2));
1321 AssertEquals ("shouldn't find", -1, a.LastIndexOf ('?'));
1323 AssertEquals ("shouldn't find", -1, a.LastIndexOf (1));
1324 } catch (Exception e) {
1325 Fail ("Unexpected exception caught when iTest=" + iTest + ". e=" + e);
1330 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1331 public void LastIndexOf_StartIndexOverflow ()
1333 ArrayList al = new ArrayList ();
1335 al.LastIndexOf ('a', Int32.MaxValue, 1);
1339 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1340 public void LastIndexOf_CountOverflow ()
1342 ArrayList al = new ArrayList ();
1344 al.LastIndexOf ('a', 1, Int32.MaxValue);
1348 public void TestReadOnly ()
1351 bool errorThrown = false;
1353 ArrayList al1 = ArrayList.ReadOnly (null);
1354 } catch (ArgumentNullException) {
1357 Assert ("null arg error not thrown", errorThrown);
1360 ArrayList al1 = new ArrayList ();
1361 AssertEquals ("arrays start writeable.",
1362 false, al1.IsReadOnly);
1363 ArrayList al2 = ArrayList.ReadOnly (al1);
1364 AssertEquals ("should be readonly.",
1365 true, al2.IsReadOnly);
1370 public void TestRemove ()
1373 bool errorThrown = false;
1376 ArrayList.FixedSize (new ArrayList (3));
1378 } catch (NotSupportedException) {
1381 Assert ("remove fixed size error not thrown",
1385 bool errorThrown = false;
1388 ArrayList.ReadOnly (new ArrayList (3));
1390 } catch (NotSupportedException) {
1393 Assert ("remove read only error not thrown",
1397 char [] c = { 'a', 'b', 'c' };
1398 ArrayList a = new ArrayList (c);
1401 AssertEquals ("should be unchanged", c.Length, a.Count);
1403 AssertEquals ("should be changed", 2, a.Count);
1404 AssertEquals ("should have shifted", 'b', a [0]);
1405 AssertEquals ("should have shifted", 'c', a [1]);
1410 public void TestRemoveAt ()
1413 bool errorThrown = false;
1416 ArrayList.FixedSize (new ArrayList (3));
1418 } catch (NotSupportedException) {
1421 Assert ("remove from fixed size error not thrown",
1425 bool errorThrown = false;
1428 ArrayList.ReadOnly (new ArrayList (3));
1430 } catch (NotSupportedException) {
1433 Assert ("remove from read only error not thrown",
1437 bool errorThrown = false;
1439 ArrayList al1 = new ArrayList (3);
1441 } catch (ArgumentOutOfRangeException) {
1444 Assert ("remove at negative index error not thrown",
1448 bool errorThrown = false;
1450 ArrayList al1 = new ArrayList (3);
1452 } catch (ArgumentOutOfRangeException) {
1455 Assert ("remove at out-of-range index error not thrown",
1459 char [] c = { 'a', 'b', 'c' };
1460 ArrayList a = new ArrayList (c);
1462 AssertEquals ("should be changed", 2, a.Count);
1463 AssertEquals ("should have shifted", 'b', a [0]);
1464 AssertEquals ("should have shifted", 'c', a [1]);
1469 public void TestRemoveRange ()
1472 bool errorThrown = false;
1475 ArrayList.FixedSize (new ArrayList (3));
1476 al1.RemoveRange (0, 1);
1477 } catch (NotSupportedException) {
1480 Assert ("removerange from fixed size error not thrown",
1484 bool errorThrown = false;
1487 ArrayList.ReadOnly (new ArrayList (3));
1488 al1.RemoveRange (0, 1);
1489 } catch (NotSupportedException) {
1492 Assert ("removerange from read only error not thrown",
1496 bool errorThrown = false;
1498 ArrayList al1 = new ArrayList (3);
1499 al1.RemoveRange (-1, 1);
1500 } catch (ArgumentOutOfRangeException) {
1503 Assert ("removerange at negative index error not thrown",
1507 bool errorThrown = false;
1509 ArrayList al1 = new ArrayList (3);
1510 al1.RemoveRange (0, -1);
1511 } catch (ArgumentOutOfRangeException) {
1514 Assert ("removerange at negative index error not thrown",
1518 bool errorThrown = false;
1520 ArrayList al1 = new ArrayList (3);
1521 al1.RemoveRange (2, 3);
1522 } catch (ArgumentException) {
1525 Assert ("removerange at bad range error not thrown",
1529 char [] c = { 'a', 'b', 'c' };
1530 ArrayList a = new ArrayList (c);
1531 a.RemoveRange (1, 2);
1532 AssertEquals ("should be changed", 1, a.Count);
1533 AssertEquals ("should have shifted", 'a', a [0]);
1538 [ExpectedException (typeof (ArgumentException))]
1539 public void RemoveRange_IndexOverflow ()
1541 ArrayList al = new ArrayList ();
1543 al.RemoveRange (Int32.MaxValue, 1);
1547 [ExpectedException (typeof (ArgumentException))]
1548 public void RemoveRange_CountOverflow ()
1550 ArrayList al = new ArrayList ();
1552 al.RemoveRange (1, Int32.MaxValue);
1556 public void TestRepeat ()
1559 bool errorThrown = false;
1561 ArrayList al1 = ArrayList.Repeat ('c', -1);
1562 } catch (ArgumentOutOfRangeException) {
1565 Assert ("repeat negative copies error not thrown",
1569 ArrayList al1 = ArrayList.Repeat ("huh?", 0);
1570 AssertEquals ("should be nothing in array",
1574 ArrayList al1 = ArrayList.Repeat ("huh?", 3);
1575 AssertEquals ("should be something in array",
1577 AssertEquals ("array elem doesn't check",
1579 AssertEquals ("array elem doesn't check",
1581 AssertEquals ("array elem doesn't check",
1587 public void TestReverse ()
1590 bool errorThrown = false;
1593 ArrayList.ReadOnly (new ArrayList ());
1595 } catch (NotSupportedException) {
1598 Assert ("reverse on read only error not thrown",
1602 bool errorThrown = false;
1604 char [] c = new Char [2];
1605 ArrayList al1 = new ArrayList (c);
1607 } catch (ArgumentException) {
1610 Assert ("error not thrown", errorThrown);
1613 bool errorThrown = false;
1615 char [] c = new Char [2];
1616 ArrayList al1 = new ArrayList (c);
1618 } catch (ArgumentException) {
1621 Assert ("error not thrown", errorThrown);
1624 char [] c = { 'a', 'b', 'c', 'd', 'e' };
1625 ArrayList al1 = new ArrayList (c);
1627 for (int i = 0; i < al1.Count; i++) {
1628 AssertEquals ("Should be no change yet",
1632 for (int i = 0; i < al1.Count; i++) {
1633 AssertEquals ("Should be reversed",
1634 c [i], al1 [4 - i]);
1637 for (int i = 0; i < al1.Count; i++) {
1638 AssertEquals ("Should be back to normal",
1642 AssertEquals ("Should be back to normal", c [0], al1 [0]);
1643 AssertEquals ("Should be back to normal", c [3], al1 [1]);
1644 AssertEquals ("Should be back to normal", c [2], al1 [2]);
1645 AssertEquals ("Should be back to normal", c [1], al1 [3]);
1646 AssertEquals ("Should be back to normal", c [4], al1 [4]);
1651 [ExpectedException (typeof (ArgumentException))]
1652 public void Reverse_IndexOverflow ()
1654 ArrayList al = new ArrayList ();
1656 al.Reverse (Int32.MaxValue, 1);
1660 [ExpectedException (typeof (ArgumentException))]
1661 public void Reverse_CountOverflow ()
1663 ArrayList al = new ArrayList ();
1665 al.Reverse (1, Int32.MaxValue);
1669 public void TestSetRange ()
1672 bool errorThrown = false;
1674 char [] c = { 'a', 'b', 'c' };
1676 ArrayList.ReadOnly (new ArrayList (3));
1677 al1.SetRange (0, c);
1678 } catch (NotSupportedException) {
1680 } catch (Exception e) {
1681 Fail ("Incorrect exception thrown at 1: " + e.ToString ());
1683 Assert ("setrange on read only error not thrown",
1687 bool errorThrown = false;
1689 ArrayList al1 = new ArrayList (3);
1690 al1.SetRange (0, null);
1691 } catch (ArgumentNullException) {
1693 } catch (ArgumentOutOfRangeException) {
1695 } catch (Exception e) {
1696 Fail ("Incorrect exception thrown at 2: " + e.ToString ());
1698 Assert ("setrange with null error not thrown",
1702 bool errorThrown = false;
1704 char [] c = { 'a', 'b', 'c' };
1705 ArrayList al1 = new ArrayList (3);
1706 al1.SetRange (-1, c);
1707 } catch (ArgumentOutOfRangeException) {
1709 } catch (Exception e) {
1710 Fail ("Incorrect exception thrown at 3: " + e.ToString ());
1712 Assert ("setrange with negative index error not thrown",
1716 bool errorThrown = false;
1718 char [] c = { 'a', 'b', 'c' };
1719 ArrayList al1 = new ArrayList (3);
1720 al1.SetRange (2, c);
1721 } catch (ArgumentOutOfRangeException) {
1723 } catch (Exception e) {
1724 Fail ("Incorrect exception thrown at 4: " + e.ToString ());
1726 Assert ("setrange with too much error not thrown",
1731 char [] c = { 'a', 'b', 'c' };
1732 ArrayList al1 = ArrayList.Repeat ('?', 3);
1733 Assert ("no match yet", c [0] != (char) al1 [0]);
1734 Assert ("no match yet", c [1] != (char) al1 [1]);
1735 Assert ("no match yet", c [2] != (char) al1 [2]);
1736 al1.SetRange (0, c);
1737 AssertEquals ("should match", c [0], al1 [0]);
1738 AssertEquals ("should match", c [1], al1 [1]);
1739 AssertEquals ("should match", c [2], al1 [2]);
1744 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1745 public void SetRange_Overflow ()
1747 ArrayList al = new ArrayList ();
1749 al.SetRange (Int32.MaxValue, new ArrayList ());
1753 public void TestInsertRange_this ()
1755 String [] s1 = { "this", "is", "a", "test" };
1756 ArrayList al = new ArrayList (s1);
1757 al.InsertRange (2, al);
1758 String [] s2 = { "this", "is", "this", "is", "a", "test", "a", "test" };
1759 for (int i = 0; i < al.Count; i++) {
1760 AssertEquals ("at i=" + i, s2 [i], al [i]);
1765 public void TestSort ()
1768 bool errorThrown = false;
1771 ArrayList.ReadOnly (new ArrayList ());
1773 } catch (NotSupportedException) {
1776 Assert ("sort on read only error not thrown",
1780 char [] starter = { 'd', 'b', 'f', 'e', 'a', 'c' };
1781 ArrayList al1 = new ArrayList (starter);
1783 AssertEquals ("Should be sorted", 'a', al1 [0]);
1784 AssertEquals ("Should be sorted", 'b', al1 [1]);
1785 AssertEquals ("Should be sorted", 'c', al1 [2]);
1786 AssertEquals ("Should be sorted", 'd', al1 [3]);
1787 AssertEquals ("Should be sorted", 'e', al1 [4]);
1788 AssertEquals ("Should be sorted", 'f', al1 [5]);
1791 ArrayList al1 = new ArrayList ();
1800 AssertEquals ("Should be null", null, al1 [0]);
1801 AssertEquals ("Should be 2. null", null, al1 [1]);
1802 AssertEquals ("Should be 3. null", null, al1 [2]);
1803 AssertEquals ("Should be 4. null", null, al1 [3]);
1804 AssertEquals ("Should be 32", 32, al1 [4]);
1805 AssertEquals ("Should be 33", 33, al1 [5]);
1810 [ExpectedException (typeof (ArgumentException))]
1811 public void Sort_IndexOverflow ()
1813 ArrayList al = new ArrayList ();
1815 al.Sort (Int32.MaxValue, 1, null);
1819 [ExpectedException (typeof (ArgumentException))]
1820 public void Sort_CountOverflow ()
1822 ArrayList al = new ArrayList ();
1824 al.Sort (1, Int32.MaxValue, null);
1827 // TODO - Sort with IComparers
1829 // TODO - Synchronize
1832 public void TestToArray ()
1835 bool errorThrown = false;
1837 ArrayList al1 = new ArrayList (3);
1839 } catch (ArgumentNullException) {
1842 Assert ("toarray with null error not thrown",
1846 bool errorThrown = false;
1848 char [] c = { 'a', 'b', 'c' };
1850 ArrayList al1 = new ArrayList (c);
1851 al1.ToArray (s.GetType ());
1852 } catch (InvalidCastException) {
1855 Assert ("toarray with bad type error not thrown",
1859 char [] c1 = { 'a', 'b', 'c', 'd', 'e' };
1860 ArrayList al1 = new ArrayList (c1);
1861 object [] o2 = al1.ToArray ();
1862 for (int i = 0; i < c1.Length; i++) {
1863 AssertEquals ("should be copy", c1 [i], o2 [i]);
1865 Array c2 = al1.ToArray (c1 [0].GetType ());
1866 for (int i = 0; i < c1.Length; i++) {
1867 AssertEquals ("should be copy",
1868 c1 [i], c2.GetValue (i));
1874 [ExpectedException (typeof (NotSupportedException))]
1875 public void TrimToSize_ReadOnly ()
1877 ArrayList al1 = ArrayList.ReadOnly (new ArrayList ());
1882 public void TrimToSize ()
1884 ArrayList al1 = new ArrayList ();
1886 // Capacity is 0 under 2.0
1889 int capacity = al1.Capacity;
1891 int size = capacity / 2;
1892 for (int i = 1; i <= size; i++) {
1897 AssertEquals ("no capacity match", size - 1, al1.Capacity);
1901 AssertEquals ("no default capacity", capacity, al1.Capacity);
1904 class Comparer : IComparer
1907 private bool called = false;
1913 bool result = called;
1919 public int Compare (object x, object y)
1927 public void BinarySearch1_EmptyList ()
1929 ArrayList list = new ArrayList ();
1930 AssertEquals ("BinarySearch", -1, list.BinarySearch (0));
1934 public void BinarySearch2_EmptyList ()
1936 Comparer comparer = new Comparer ();
1937 ArrayList list = new ArrayList ();
1938 AssertEquals ("BinarySearch", -1, list.BinarySearch (0, comparer));
1939 // bug 77030 - the comparer isn't called for an empty array/list
1940 Assert ("Called", !comparer.Called);
1944 public void BinarySearch3_EmptyList ()
1946 Comparer comparer = new Comparer ();
1947 ArrayList list = new ArrayList ();
1948 AssertEquals ("BinarySearch", -1, list.BinarySearch (0, 0, 0, comparer));
1949 // bug 77030 - the comparer isn't called for an empty array/list
1950 Assert ("Called", !comparer.Called);
1955 [Category ("NotDotNet")] // MS bug
1957 public void AddRange_GetRange ()
1959 ArrayList source = ArrayList.Adapter (new object [] { "1", "2" });
1960 AssertEquals ("#1", 2, source.Count);
1961 AssertEquals ("#2", "1", source [0]);
1962 AssertEquals ("#3", "2", source [1]);
1963 ArrayList range = source.GetRange (1, 1);
1964 AssertEquals ("#4", 1, range.Count);
1965 AssertEquals ("#5", "2", range [0]);
1966 ArrayList target = new ArrayList ();
1967 target.AddRange (range);
1968 AssertEquals ("#6", 1, target.Count);
1969 AssertEquals ("#7", "2", target [0]);
1974 [Category ("NotDotNet")] // MS bug
1976 public void IterateSelf ()
1978 ArrayList list = new ArrayList ();
1980 IEnumerator enumerator = list.GetEnumerator ();
1981 Assert ("#1", enumerator.MoveNext ());
1982 Assert ("#2", object.ReferenceEquals (list, enumerator.Current));
1983 Assert ("#3", !enumerator.MoveNext ());