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
20 public void TestCtor ()
23 ArrayList al1 = new ArrayList ();
24 Assert.IsNotNull (al1, "no basic ArrayList");
27 bool errorThrown = false;
29 ArrayList a = new ArrayList (null);
30 } catch (ArgumentNullException) {
33 Assert.IsTrue (errorThrown, "null icollection error not thrown");
36 // what can I say? I like chars. [--DB]
37 char [] coll = { 'a', 'b', 'c', 'd' };
38 ArrayList al1 = new ArrayList (coll);
39 Assert.IsNotNull (al1, "no icollection ArrayList");
40 for (int i = 0; i < coll.Length; i++) {
41 Assert.AreEqual (coll [i], al1 [i], i + " not ctor'ed properly.");
46 Char [,] c1 = new Char [2, 2];
47 ArrayList al1 = new ArrayList (c1);
48 Assert.Fail ("Should fail with multi-dimensional array in constructor.");
49 } catch (RankException) {
54 bool errorThrown = false;
56 ArrayList a = new ArrayList (-1);
57 } catch (ArgumentOutOfRangeException) {
60 Assert.IsTrue (errorThrown, "negative capacity error not thrown");
65 public void TestCapacity ()
68 int default_capacity = 4;
69 int unspecified_capacity = 0;
71 int default_capacity = 16;
72 int unspecified_capacity = 16;
74 for (int i = 1; i < 100; i++) {
75 ArrayList al1 = new ArrayList (i);
76 Assert.AreEqual (i, al1.Capacity, "Bad capacity of " + i);
79 ArrayList al1 = new ArrayList (0);
81 // Assert.AreEqual (// 16, al1.Capacity, "Bad capacity when set to 0");
83 Assert.AreEqual (default_capacity, al1.Capacity, "Bad capacity when set to 0");
86 ArrayList al1 = new ArrayList ();
87 Assert.AreEqual (unspecified_capacity, al1.Capacity, "Bad default capacity");
92 public void TestCount ()
95 ArrayList al1 = new ArrayList ();
96 Assert.AreEqual (0, al1.Count, "Bad initial count");
97 for (int i = 1; i <= 100; i++) {
99 Assert.AreEqual (i, al1.Count, "Bad count " + i);
102 for (int i = 0; i < 100; i++) {
103 char [] coll = new Char [i];
104 ArrayList al1 = new ArrayList (coll);
105 Assert.AreEqual (i, al1.Count, "Bad count for " + i);
110 public void TestIsFixed ()
112 ArrayList al1 = new ArrayList ();
113 Assert.IsTrue (!al1.IsFixedSize, "should not be fixed by default");
114 ArrayList al2 = ArrayList.FixedSize (al1);
115 Assert.IsTrue (al2.IsFixedSize, "fixed-size wrapper not working");
119 public void TestIsReadOnly ()
121 ArrayList al1 = new ArrayList ();
122 Assert.IsTrue (!al1.IsReadOnly, "should not be ReadOnly by default");
123 ArrayList al2 = ArrayList.ReadOnly (al1);
124 Assert.IsTrue (al2.IsReadOnly, "read-only wrapper not working");
128 public void TestIsSynchronized ()
130 ArrayList al1 = new ArrayList ();
131 Assert.IsTrue (!al1.IsSynchronized, "should not be synchronized by default");
132 ArrayList al2 = ArrayList.Synchronized (al1);
133 Assert.IsTrue (al2.IsSynchronized, "synchronized wrapper not working");
137 public void TestItem ()
139 ArrayList al1 = new ArrayList ();
141 bool errorThrown = false;
144 } catch (ArgumentOutOfRangeException) {
147 Assert.IsTrue (errorThrown, "negative item error not thrown");
150 bool errorThrown = false;
153 } catch (ArgumentOutOfRangeException) {
156 Assert.IsTrue (errorThrown, "past-end item error not thrown");
158 for (int i = 0; i <= 100; i++) {
161 for (int i = 0; i <= 100; i++) {
162 Assert.AreEqual (i, al1 [i], "item not fetched for " + i);
167 public void TestAdapter ()
170 bool errorThrown = false;
172 ArrayList al1 = ArrayList.Adapter (null);
173 } catch (ArgumentNullException) {
175 } catch (Exception e) {
176 Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString ());
178 Assert.IsTrue (errorThrown, "null adapter error not thrown");
181 char [] list = { 'a', 'b', 'c', 'd' };
182 ArrayList al1 = ArrayList.Adapter (list);
183 Assert.IsNotNull (al1, "Couldn't get an adapter");
184 for (int i = 0; i < list.Length; i++) {
185 Assert.AreEqual (list [i], al1 [i], "adapter not adapting");
188 for (int i = 0; i < list.Length; i++) {
189 Assert.AreEqual (list [i], al1 [i], "adapter not adapting");
192 // Test Binary Search
194 bool errorThrown = false;
197 String [] s1 = { "This", "is", "a", "test" };
198 ArrayList al1 = ArrayList.Adapter (s1);
199 al1.BinarySearch (42);
200 } catch (InvalidOperationException) {
201 // this is what .NET throws
203 } catch (ArgumentException) {
204 // this is what the docs say it should throw
206 } catch (Exception e) {
207 Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString ());
209 Assert.IsTrue (errorThrown, "search-for-wrong-type error not thrown");
213 char [] arr = { 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd' };
214 ArrayList al1 = ArrayList.Adapter (arr);
215 Assert.IsTrue (al1.BinarySearch ('c') >= 3, "couldn't find elem #1");
216 Assert.IsTrue (al1.BinarySearch ('c') < 6, "couldn't find elem #2");
219 char [] arr = { 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e' };
220 ArrayList al1 = ArrayList.Adapter (arr);
221 Assert.AreEqual (-4, al1.BinarySearch ('c'), "couldn't find next-higher elem");
224 char [] arr = { 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd' };
225 ArrayList al1 = ArrayList.Adapter (arr);
226 Assert.AreEqual (-9, al1.BinarySearch ('e'), "couldn't find end");
230 char [] starter = { 'd', 'b', 'f', 'e', 'a', 'c' };
231 ArrayList al1 = ArrayList.Adapter (starter);
233 Assert.AreEqual ('a', al1 [0], "Should be sorted");
234 Assert.AreEqual ('b', al1 [1], "Should be sorted");
235 Assert.AreEqual ('c', al1 [2], "Should be sorted");
236 Assert.AreEqual ('d', al1 [3], "Should be sorted");
237 Assert.AreEqual ('e', al1 [4], "Should be sorted");
238 Assert.AreEqual ('f', al1 [5], "Should be sorted");
241 // TODO - test other adapter types?
245 public void TestAdd ()
248 bool errorThrown = false;
251 ArrayList.FixedSize (new ArrayList ());
253 } catch (NotSupportedException) {
255 } catch (Exception e) {
256 Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString ());
258 Assert.IsTrue (errorThrown, "add to fixed size error not thrown");
261 bool errorThrown = false;
264 ArrayList.ReadOnly (new ArrayList ());
266 } catch (NotSupportedException) {
268 } catch (Exception e) {
269 Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString ());
271 Assert.IsTrue (errorThrown, "add to read only error not thrown");
274 ArrayList al1 = new ArrayList ();
275 for (int i = 1; i <= 100; i++) {
277 Assert.AreEqual (i, al1.Count, "add failed " + i);
278 Assert.AreEqual (i, al1 [i - 1], "add failed " + i);
283 string [] strArray = new string [] { };
284 ArrayList al1 = new ArrayList (strArray);
287 Assert.AreEqual (2, al1.Count, "add failed");
292 public void TestAddRange ()
295 bool errorThrown = false;
298 ArrayList.FixedSize (new ArrayList ());
299 String [] s1 = { "Hi!" };
301 } catch (NotSupportedException) {
303 } catch (Exception e) {
304 Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString ());
306 Assert.IsTrue (errorThrown, "add to fixed size error not thrown");
309 bool errorThrown = false;
312 ArrayList.ReadOnly (new ArrayList ());
313 String [] s1 = { "Hi!" };
315 } catch (NotSupportedException) {
317 } catch (Exception e) {
318 Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString ());
320 Assert.IsTrue (errorThrown, "add to read only error not thrown");
323 bool errorThrown = false;
325 ArrayList al1 = new ArrayList ();
327 } catch (ArgumentNullException) {
329 } catch (Exception e) {
330 Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString ());
332 Assert.IsTrue (errorThrown, "add to read only error not thrown");
336 ArrayList a1 = new ArrayList ();
337 Assert.AreEqual (0, a1.Count, "ArrayList should start empty");
338 char [] coll = { 'a', 'b', 'c' };
340 Assert.AreEqual (3, a1.Count, "ArrayList has wrong elements");
342 Assert.AreEqual (6, a1.Count, "ArrayList has wrong elements");
346 ArrayList list = new ArrayList ();
348 for (int i = 0; i < 100; i++) {
352 Assert.AreEqual (49, list.BinarySearch (1), "BinarySearch off-by-one bug");
357 public void TestBinarySearch ()
360 bool errorThrown = false;
362 ArrayList al1 = new ArrayList ();
363 String [] s1 = { "This", "is", "a", "test" };
365 al1.BinarySearch (42);
366 } catch (InvalidOperationException) {
367 // this is what .NET throws
369 } catch (ArgumentException) {
370 // this is what the docs say it should throw
372 } catch (Exception e) {
373 Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString ());
375 Assert.IsTrue (errorThrown, "search-for-wrong-type error not thrown");
379 char [] arr = { 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd' };
380 ArrayList al1 = new ArrayList (arr);
381 Assert.IsTrue (al1.BinarySearch ('c') >= 3, "couldn't find elem #1");
382 Assert.IsTrue (al1.BinarySearch ('c') < 6, "couldn't find elem #2");
385 char [] arr = { 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e' };
386 ArrayList al1 = new ArrayList (arr);
387 Assert.AreEqual (-4, al1.BinarySearch ('c'), "couldn't find next-higher elem");
390 char [] arr = { 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd' };
391 ArrayList al1 = new ArrayList (arr);
392 Assert.AreEqual (-9, al1.BinarySearch ('e'), "couldn't find end");
398 [ExpectedException (typeof (ArgumentException))]
399 public void BinarySearch_IndexOverflow ()
401 ArrayList al = new ArrayList ();
403 al.BinarySearch (Int32.MaxValue, 1, this, null);
407 [ExpectedException (typeof (ArgumentException))]
408 public void BinarySearch_CountOverflow ()
410 ArrayList al = new ArrayList ();
412 al.BinarySearch (1, Int32.MaxValue, this, null);
416 public void BinarySearch_Null ()
418 ArrayList al = new ArrayList ();
420 Assert.AreEqual (-1, al.BinarySearch (null), "null");
423 // TODO - BinarySearch with IComparer
426 public void TestClear ()
429 bool errorThrown = false;
432 ArrayList.FixedSize (new ArrayList ());
434 } catch (NotSupportedException) {
437 Assert.IsTrue (errorThrown, "add to fixed size error not thrown");
440 bool errorThrown = false;
443 ArrayList.ReadOnly (new ArrayList ());
445 } catch (NotSupportedException) {
448 Assert.IsTrue (errorThrown, "add to read only error not thrown");
451 ArrayList al1 = new ArrayList ();
453 Assert.AreEqual (1, al1.Count, "should have one element");
455 Assert.AreEqual (0, al1.Count, "should be empty");
458 int [] i1 = { 1, 2, 3, 4 };
459 ArrayList al1 = new ArrayList (i1);
460 Assert.AreEqual (i1.Length, al1.Count, "should have elements");
461 int capacity = al1.Capacity;
463 Assert.AreEqual (0, al1.Count, "should be empty again");
464 Assert.AreEqual (capacity, al1.Capacity, "capacity shouldn't have changed");
469 public void TestClone ()
472 char [] c1 = { 'a', 'b', 'c' };
473 ArrayList al1 = new ArrayList (c1);
474 ArrayList al2 = (ArrayList) al1.Clone ();
475 Assert.AreEqual (al1 [0], al2 [0], "ArrayList match");
476 Assert.AreEqual (al1 [1], al2 [1], "ArrayList match");
477 Assert.AreEqual (al1 [2], al2 [2], "ArrayList match");
480 char [] d10 = { 'a', 'b' };
481 char [] d11 = { 'a', 'c' };
482 char [] d12 = { 'b', 'c' };
483 char [] [] d1 = { d10, d11, d12 };
484 ArrayList al1 = new ArrayList (d1);
485 ArrayList al2 = (ArrayList) al1.Clone ();
486 Assert.AreEqual (al1 [0], al2 [0], "Array match");
487 Assert.AreEqual (al1 [1], al2 [1], "Array match");
488 Assert.AreEqual (al1 [2], al2 [2], "Array match");
490 ((char []) al1 [0]) [0] = 'z';
491 Assert.AreEqual (al1 [0], al2 [0], "shallow copy");
496 public void TestContains ()
498 char [] c1 = { 'a', 'b', 'c' };
499 ArrayList al1 = new ArrayList (c1);
500 Assert.IsTrue (!al1.Contains (null), "never find a null");
501 Assert.IsTrue (al1.Contains ('b'), "can't find value");
502 Assert.IsTrue (!al1.Contains ('?'), "shouldn't find value");
506 public void TestCopyTo ()
509 bool errorThrown = false;
511 Char [] c1 = new Char [2];
512 ArrayList al1 = new ArrayList (c1);
513 al1.CopyTo (null, 2);
514 } catch (ArgumentNullException) {
516 } catch (Exception e) {
517 Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString ());
519 Assert.IsTrue (errorThrown, "error not thrown 1");
522 bool errorThrown = false;
524 Char [] c1 = new Char [2];
525 ArrayList al1 = new ArrayList (c1);
526 Char [,] c2 = new Char [2, 2];
528 } catch (ArgumentException) {
530 } catch (Exception e) {
531 Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString ());
533 Assert.IsTrue (errorThrown, "error not thrown 2");
536 bool errorThrown = false;
538 // This appears to be a bug in the ArrayList Constructor.
539 // It throws a RankException if a multidimensional Array
540 // is passed. The docs imply that an IEnumerator is used
541 // to retrieve the items from the collection, so this should
542 // work. In anycase this test is for CopyTo, so use what
543 // works on both platforms.
544 //Char[,] c1 = new Char[2,2];
545 Char [] c1 = new Char [2];
546 ArrayList al1 = new ArrayList (c1);
547 Char [] c2 = new Char [2];
549 } catch (ArgumentException) {
551 } catch (Exception e) {
552 Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString ());
554 Assert.IsTrue (errorThrown, "error not thrown 3");
557 bool errorThrown = false;
559 Char [] c1 = new Char [2];
560 ArrayList al1 = new ArrayList (c1);
561 Char [] c2 = new Char [2];
563 } catch (ArgumentOutOfRangeException) {
565 } catch (Exception e) {
566 Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString ());
568 Assert.IsTrue (errorThrown, "error not thrown 4");
571 bool errorThrown = false;
573 Char [] c1 = new Char [2];
574 ArrayList al1 = new ArrayList (c1);
575 Char [] c2 = new Char [2];
577 } catch (ArgumentException) {
579 } catch (Exception e) {
580 Assert.Fail ("Incorrect exception thrown at 5: " + e.ToString ());
582 Assert.IsTrue (errorThrown, "error not thrown 5");
585 bool errorThrown = false;
587 Char [] c1 = new Char [2];
588 ArrayList al1 = new ArrayList (c1);
589 Char [] c2 = new Char [2];
591 } catch (ArgumentException) {
593 } catch (Exception e) {
594 Assert.Fail ("Incorrect exception thrown at 6: " + e.ToString ());
596 Assert.IsTrue (errorThrown, "error not thrown 6");
599 bool errorThrown = false;
601 String [] c1 = { "String", "array" };
602 ArrayList al1 = new ArrayList (c1);
603 Char [] c2 = new Char [2];
605 } catch (InvalidCastException) {
607 } catch (Exception e) {
608 Assert.Fail ("Incorrect exception thrown at 7: " + e.ToString ());
610 Assert.IsTrue (errorThrown, "error not thrown 7");
613 Char [] orig = { 'a', 'b', 'c', 'd' };
614 ArrayList al = new ArrayList (orig);
615 Char [] copy = new Char [10];
616 Array.Clear (copy, 0, copy.Length);
618 Assert.AreEqual ((char) 0, copy [0], "Wrong CopyTo 0");
619 Assert.AreEqual ((char) 0, copy [1], "Wrong CopyTo 1");
620 Assert.AreEqual ((char) 0, copy [2], "Wrong CopyTo 2");
621 Assert.AreEqual (orig [0], copy [3], "Wrong CopyTo 3");
622 Assert.AreEqual (orig [1], copy [4], "Wrong CopyTo 4");
623 Assert.AreEqual (orig [2], copy [5], "Wrong CopyTo 5");
624 Assert.AreEqual (orig [3], copy [6], "Wrong CopyTo 6");
625 Assert.AreEqual ((char) 0, copy [7], "Wrong CopyTo 7");
626 Assert.AreEqual ((char) 0, copy [8], "Wrong CopyTo 8");
627 Assert.AreEqual ((char) 0, copy [9], "Wrong CopyTo 9");
631 [ExpectedException (typeof (ArgumentException))]
632 public void CopyTo_IndexOverflow ()
634 ArrayList al = new ArrayList ();
636 al.CopyTo (Int32.MaxValue, new byte [2], 0, 0);
640 [ExpectedException (typeof (ArgumentException))]
641 public void CopyTo_ArrayIndexOverflow ()
643 ArrayList al = new ArrayList ();
645 al.CopyTo (0, new byte [2], Int32.MaxValue, 0);
649 [ExpectedException (typeof (ArgumentException))]
650 public void CopyTo_CountOverflow ()
652 ArrayList al = new ArrayList ();
654 al.CopyTo (0, new byte [2], 0, Int32.MaxValue);
658 public void TestFixedSize ()
661 bool errorThrown = false;
663 ArrayList al1 = ArrayList.FixedSize (null);
664 } catch (ArgumentNullException) {
667 Assert.IsTrue (errorThrown, "null arg error not thrown");
670 ArrayList al1 = new ArrayList ();
671 Assert.AreEqual (false, al1.IsFixedSize, "arrays start un-fixed.");
672 ArrayList al2 = ArrayList.FixedSize (al1);
673 Assert.AreEqual (true, al2.IsFixedSize, "should be fixed.");
678 public void TestEnumerator ()
680 String [] s1 = { "this", "is", "a", "test" };
681 ArrayList al1 = new ArrayList (s1);
682 IEnumerator en = al1.GetEnumerator ();
684 al1.Add ("something");
687 Assert.Fail ("Add() didn't invalidate the enumerator");
688 } catch (InvalidOperationException) {
689 // do nothing...this is what we expect
692 en = al1.GetEnumerator ();
697 Assert.Fail ("AddRange() didn't invalidate the enumerator");
698 } catch (InvalidOperationException) {
699 // do nothing...this is what we expect
702 en = al1.GetEnumerator ();
707 Assert.Fail ("Clear() didn't invalidate the enumerator");
708 } catch (InvalidOperationException) {
709 // do nothing...this is what we expect
712 al1 = new ArrayList (s1);
713 en = al1.GetEnumerator ();
715 al1.Insert (0, "new first");
718 Assert.Fail ("Insert() didn't invalidate the enumerator");
719 } catch (InvalidOperationException) {
720 // do nothing...this is what we expect
723 en = al1.GetEnumerator ();
725 al1.InsertRange (0, al1);
728 Assert.Fail ("InsertRange() didn't invalidate the enumerator");
729 } catch (InvalidOperationException) {
730 // do nothing...this is what we expect
733 en = al1.GetEnumerator ();
738 Assert.Fail ("Remove() didn't invalidate the enumerator");
739 } catch (InvalidOperationException) {
740 // do nothing...this is what we expect
743 en = al1.GetEnumerator ();
748 Assert.Fail ("RemoveAt() didn't invalidate the enumerator");
749 } catch (InvalidOperationException) {
750 // do nothing...this is what we expect
753 en = al1.GetEnumerator ();
755 al1.RemoveRange (1, 1);
758 Assert.Fail ("RemoveRange() didn't invalidate the enumerator");
759 } catch (InvalidOperationException) {
760 // do nothing...this is what we expect
763 en = al1.GetEnumerator ();
768 Assert.Fail ("Reverse() didn't invalidate the enumerator");
769 } catch (InvalidOperationException) {
770 // do nothing...this is what we expect
773 en = al1.GetEnumerator ();
778 Assert.Fail ("Sort() didn't invalidate the enumerator");
779 } catch (InvalidOperationException) {
780 // do nothing...this is what we expect
785 public void TestGetEnumerator ()
788 bool errorThrown = false;
790 ArrayList a = new ArrayList ();
791 IEnumerator en = a.GetEnumerator (-1, 1);
792 } catch (ArgumentOutOfRangeException) {
795 Assert.IsTrue (errorThrown, "negative index error not thrown");
798 bool errorThrown = false;
800 ArrayList a = new ArrayList ();
801 IEnumerator en = a.GetEnumerator (1, -1);
802 } catch (ArgumentOutOfRangeException) {
805 Assert.IsTrue (errorThrown, "negative index error not thrown");
808 bool errorThrown = false;
810 ArrayList a = new ArrayList ();
811 IEnumerator en = a.GetEnumerator (1, 1);
812 } catch (ArgumentException) {
815 Assert.IsTrue (errorThrown, "out-of-range index error not thrown");
818 String [] s1 = { "this", "is", "a", "test" };
819 ArrayList al1 = new ArrayList (s1);
820 IEnumerator en = al1.GetEnumerator ();
821 Assert.IsNotNull (en, "No enumerator");
823 for (int i = 0; i < s1.Length; i++) {
825 Assert.AreEqual (al1 [i], en.Current, "Not enumerating");
829 String [] s1 = { "this", "is", "a", "test" };
830 ArrayList al1 = new ArrayList (s1);
831 IEnumerator en = al1.GetEnumerator (1, 2);
832 Assert.IsNotNull (en, "No enumerator");
834 for (int i = 0; i < 2; i++) {
836 Assert.AreEqual (al1 [i + 1], en.Current, "Not enumerating");
842 [ExpectedException (typeof (ArgumentException))]
843 public void GetEnumerator_IndexOverflow ()
845 ArrayList al = new ArrayList ();
847 al.GetEnumerator (Int32.MaxValue, 0);
851 [ExpectedException (typeof (ArgumentException))]
852 public void GetEnumerator_CountOverflow ()
854 ArrayList al = new ArrayList ();
856 al.GetEnumerator (0, Int32.MaxValue);
860 public void TestGetRange ()
863 bool errorThrown = false;
865 ArrayList a = new ArrayList ();
866 ArrayList b = a.GetRange (-1, 1);
867 } catch (ArgumentOutOfRangeException) {
870 Assert.IsTrue (errorThrown, "negative index error not thrown");
873 bool errorThrown = false;
875 ArrayList a = new ArrayList ();
876 ArrayList b = a.GetRange (1, -1);
877 } catch (ArgumentOutOfRangeException) {
880 Assert.IsTrue (errorThrown, "negative index error not thrown");
883 bool errorThrown = false;
885 ArrayList a = new ArrayList ();
886 ArrayList b = a.GetRange (1, 1);
887 } catch (ArgumentException) {
890 Assert.IsTrue (errorThrown, "out-of-range index error not thrown");
893 char [] chars = { 'a', 'b', 'c', 'd', 'e', 'f' };
894 ArrayList a = new ArrayList (chars);
895 ArrayList b = a.GetRange (1, 3);
896 Assert.AreEqual (3, b.Count, "GetRange returned wrong size ArrayList");
897 for (int i = 0; i < b.Count; i++) {
898 Assert.AreEqual (chars [i + 1], b [i], "range didn't work");
901 a [2] = '?'; // should screw up ArrayList b.
902 bool errorThrown = false;
905 } catch (InvalidOperationException) {
908 Assert.AreEqual (true, errorThrown, "Munging 'a' should mess up 'b'");
911 char [] chars = { 'a', 'b', 'c', 'd', 'e', 'f' };
912 ArrayList a = new ArrayList (chars);
913 ArrayList b = a.GetRange (3, 3);
914 object [] obj_chars = b.ToArray ();
915 for (int i = 0; i < 3; i++) {
916 char c = (char) obj_chars [i];
917 Assert.AreEqual (chars [i + 3], c, "range.ToArray didn't work");
919 char [] new_chars = (char []) b.ToArray (typeof (char));
920 for (int i = 0; i < 3; i++) {
921 Assert.AreEqual (chars [i + 3], new_chars [i], "range.ToArray with type didn't work");
927 [ExpectedException (typeof (ArgumentException))]
928 public void GetRange_IndexOverflow ()
930 ArrayList al = new ArrayList ();
932 al.GetRange (Int32.MaxValue, 0);
936 [ExpectedException (typeof (ArgumentException))]
937 public void GetRange_CountOverflow ()
939 ArrayList al = new ArrayList ();
941 al.GetRange (0, Int32.MaxValue);
945 public void TestIndexOf ()
948 bool errorThrown = false;
950 ArrayList a = new ArrayList (1);
951 int i = a.IndexOf ('a', -1);
952 } catch (ArgumentOutOfRangeException) {
955 Assert.IsTrue (errorThrown, "negative indexof error not thrown");
958 bool errorThrown = false;
960 ArrayList a = new ArrayList (1);
961 int i = a.IndexOf ('a', 2);
962 } catch (ArgumentOutOfRangeException) {
965 Assert.IsTrue (errorThrown, "past-end indexof error not thrown");
968 bool errorThrown = false;
970 ArrayList a = new ArrayList (1);
971 int i = a.IndexOf ('a', 0, -1);
972 } catch (ArgumentOutOfRangeException) {
975 Assert.IsTrue (errorThrown, "negative indexof error not thrown");
978 bool errorThrown = false;
980 ArrayList a = new ArrayList (1);
981 int i = a.IndexOf ('a', 0, 2);
982 } catch (ArgumentOutOfRangeException) {
985 Assert.IsTrue (errorThrown, "past-end indexof error not thrown");
988 bool errorThrown = false;
990 ArrayList a = new ArrayList (2);
991 int i = a.IndexOf ('a', 1, 2);
992 } catch (ArgumentOutOfRangeException) {
995 Assert.IsTrue (errorThrown, "past-end indexof error not thrown");
998 char [] c = { 'a', 'b', 'c', 'd', 'e' };
999 ArrayList a = new ArrayList (c);
1000 Assert.AreEqual (-1, a.IndexOf (null), "never find null");
1001 Assert.AreEqual (-1, a.IndexOf (null, 0), "never find null");
1002 Assert.AreEqual (-1, a.IndexOf (null, 0, 5), "never find null");
1003 Assert.AreEqual (2, a.IndexOf ('c'), "can't find elem");
1004 Assert.AreEqual (2, a.IndexOf ('c', 2), "can't find elem");
1005 Assert.AreEqual (2, a.IndexOf ('c', 2, 2), "can't find elem");
1006 Assert.AreEqual (-1, a.IndexOf ('c', 3, 2), "shouldn't find elem");
1007 Assert.AreEqual (-1, a.IndexOf ('?'), "shouldn't find");
1008 Assert.AreEqual (-1, a.IndexOf (3), "shouldn't find");
1013 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1014 public void IndexOf_StartIndexOverflow ()
1016 ArrayList al = new ArrayList ();
1018 al.IndexOf ('a', Int32.MaxValue, 1);
1022 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1023 public void IndexOf_CountOverflow ()
1025 ArrayList al = new ArrayList ();
1027 al.IndexOf ('a', 1, Int32.MaxValue);
1031 public void TestInsert ()
1034 bool errorThrown = false;
1037 ArrayList.FixedSize (new ArrayList ());
1038 al1.Insert (0, "Hi!");
1039 } catch (NotSupportedException) {
1042 Assert.IsTrue (errorThrown, "insert to fixed size error not thrown");
1045 bool errorThrown = false;
1048 ArrayList.ReadOnly (new ArrayList ());
1049 al1.Insert (0, "Hi!");
1050 } catch (NotSupportedException) {
1053 Assert.IsTrue (errorThrown, "insert to read only error not thrown");
1056 bool errorThrown = false;
1058 ArrayList al1 = new ArrayList (3);
1059 al1.Insert (-1, "Hi!");
1060 } catch (ArgumentOutOfRangeException) {
1063 Assert.IsTrue (errorThrown, "insert to read only error not thrown");
1066 bool errorThrown = false;
1068 ArrayList al1 = new ArrayList (3);
1069 al1.Insert (4, "Hi!");
1070 } catch (ArgumentOutOfRangeException) {
1073 Assert.IsTrue (errorThrown, "insert to read only error not thrown");
1076 ArrayList al1 = new ArrayList ();
1077 Assert.AreEqual (0, al1.Count, "arraylist starts empty");
1078 al1.Insert (0, 'a');
1079 al1.Insert (1, 'b');
1080 al1.Insert (0, 'c');
1081 Assert.AreEqual (3, al1.Count, "arraylist needs stuff");
1082 Assert.AreEqual ('c', al1 [0], "arraylist got stuff");
1083 Assert.AreEqual ('a', al1 [1], "arraylist got stuff");
1084 Assert.AreEqual ('b', al1 [2], "arraylist got stuff");
1089 public void TestInsertRange ()
1092 bool errorThrown = false;
1095 ArrayList.FixedSize (new ArrayList ());
1096 string [] s = { "Hi!" };
1097 al1.InsertRange (0, s);
1098 } catch (NotSupportedException) {
1101 Assert.IsTrue (errorThrown, "insert to fixed size error not thrown");
1104 bool errorThrown = false;
1107 ArrayList.ReadOnly (new ArrayList ());
1108 string [] s = { "Hi!" };
1109 al1.InsertRange (0, s);
1110 } catch (NotSupportedException) {
1113 Assert.IsTrue (errorThrown, "insert to read only error not thrown");
1116 bool errorThrown = false;
1118 ArrayList al1 = new ArrayList (3);
1119 string [] s = { "Hi!" };
1120 al1.InsertRange (-1, s);
1121 } catch (ArgumentOutOfRangeException) {
1124 Assert.IsTrue (errorThrown, "negative index insert error not thrown");
1127 bool errorThrown = false;
1129 ArrayList al1 = new ArrayList (3);
1130 string [] s = { "Hi!" };
1131 al1.InsertRange (4, s);
1132 } catch (ArgumentOutOfRangeException) {
1135 Assert.IsTrue (errorThrown, "out-of-range insert error not thrown");
1138 bool errorThrown = false;
1140 ArrayList al1 = new ArrayList (3);
1141 al1.InsertRange (0, null);
1142 } catch (ArgumentNullException) {
1145 Assert.IsTrue (errorThrown, "null insert error not thrown");
1148 char [] c = { 'a', 'b', 'c' };
1149 ArrayList a = new ArrayList (c);
1150 a.InsertRange (1, c);
1151 Assert.AreEqual ('a', a [0], "bad insert 1");
1152 Assert.AreEqual ('a', a [1], "bad insert 2");
1153 Assert.AreEqual ('b', a [2], "bad insert 3");
1154 Assert.AreEqual ('c', a [3], "bad insert 4");
1155 Assert.AreEqual ('b', a [4], "bad insert 5");
1156 Assert.AreEqual ('c', a [5], "bad insert 6");
1161 public void TestLastIndexOf ()
1164 //bool errorThrown = false;
1166 //ArrayList a = new ArrayList(1);
1167 //int i = a.LastIndexOf('a', -1);
1168 //} catch (ArgumentOutOfRangeException) {
1169 //errorThrown = true;
1171 //Assert.IsTrue (//errorThrown, "first negative lastindexof error not thrown");
1174 bool errorThrown = false;
1176 ArrayList a = new ArrayList (1);
1177 int i = a.LastIndexOf ('a', 2);
1178 } catch (ArgumentOutOfRangeException) {
1181 Assert.IsTrue (errorThrown, "past-end lastindexof error not thrown");
1184 //bool errorThrown = false;
1186 //ArrayList a = new ArrayList(1);
1187 //int i = a.LastIndexOf('a', 0, -1);
1188 //} catch (ArgumentOutOfRangeException) {
1189 //errorThrown = true;
1191 //Assert.IsTrue (//errorThrown, "second negative lastindexof error not thrown");
1194 //bool errorThrown = false;
1196 //ArrayList a = new ArrayList(1);
1197 //int i = a.LastIndexOf('a', 0, 2);
1198 //} catch (ArgumentOutOfRangeException) {
1199 //errorThrown = true;
1201 //Assert.IsTrue (//errorThrown, "past-end lastindexof error not thrown");
1204 //bool errorThrown = false;
1206 //ArrayList a = new ArrayList(2);
1207 //int i = a.LastIndexOf('a', 0, 2);
1208 //} catch (ArgumentOutOfRangeException) {
1209 //errorThrown = true;
1211 //Assert.IsTrue (//errorThrown, "past-end lastindexof error not thrown");
1215 char [] c = { 'a', 'b', 'c', 'd', 'e' };
1216 ArrayList a = new ArrayList (c);
1217 Assert.AreEqual (-1, a.LastIndexOf (null), "never find null");
1219 Assert.AreEqual (-1, a.LastIndexOf (null, 4), "never find null");
1221 Assert.AreEqual (-1, a.LastIndexOf (null, 4, 5), "never find null");
1223 Assert.AreEqual (2, a.LastIndexOf ('c'), "can't find elem");
1225 Assert.AreEqual (2, a.LastIndexOf ('c', 4), "can't find elem");
1227 Assert.AreEqual (2, a.LastIndexOf ('c', 3, 2), "can't find elem");
1229 Assert.AreEqual (-1, a.LastIndexOf ('c', 4, 2), "shouldn't find elem");
1231 Assert.AreEqual (-1, a.LastIndexOf ('?'), "shouldn't find");
1233 Assert.AreEqual (-1, a.LastIndexOf (1), "shouldn't find");
1234 } catch (Exception e) {
1235 Assert.Fail ("Unexpected exception caught when iTest=" + iTest + ". e=" + e);
1240 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1241 public void LastIndexOf_StartIndexOverflow ()
1243 ArrayList al = new ArrayList ();
1245 al.LastIndexOf ('a', Int32.MaxValue, 1);
1249 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1250 public void LastIndexOf_CountOverflow ()
1252 ArrayList al = new ArrayList ();
1254 al.LastIndexOf ('a', 1, Int32.MaxValue);
1258 public void TestReadOnly ()
1261 bool errorThrown = false;
1263 ArrayList al1 = ArrayList.ReadOnly (null);
1264 } catch (ArgumentNullException) {
1267 Assert.IsTrue (errorThrown, "null arg error not thrown");
1270 ArrayList al1 = new ArrayList ();
1271 Assert.AreEqual (false, al1.IsReadOnly, "arrays start writeable.");
1272 ArrayList al2 = ArrayList.ReadOnly (al1);
1273 Assert.AreEqual (true, al2.IsReadOnly, "should be readonly.");
1278 public void TestRemove ()
1281 bool errorThrown = false;
1284 ArrayList.FixedSize (new ArrayList (3));
1286 } catch (NotSupportedException) {
1289 Assert.IsTrue (errorThrown, "remove fixed size error not thrown");
1292 bool errorThrown = false;
1295 ArrayList.ReadOnly (new ArrayList (3));
1297 } catch (NotSupportedException) {
1300 Assert.IsTrue (errorThrown, "remove read only error not thrown");
1303 char [] c = { 'a', 'b', 'c' };
1304 ArrayList a = new ArrayList (c);
1307 Assert.AreEqual (c.Length, a.Count, "should be unchanged");
1309 Assert.AreEqual (2, a.Count, "should be changed");
1310 Assert.AreEqual ('b', a [0], "should have shifted");
1311 Assert.AreEqual ('c', a [1], "should have shifted");
1316 public void TestRemoveAt ()
1319 bool errorThrown = false;
1322 ArrayList.FixedSize (new ArrayList (3));
1324 } catch (NotSupportedException) {
1327 Assert.IsTrue (errorThrown, "remove from fixed size error not thrown");
1330 bool errorThrown = false;
1333 ArrayList.ReadOnly (new ArrayList (3));
1335 } catch (NotSupportedException) {
1338 Assert.IsTrue (errorThrown, "remove from read only error not thrown");
1341 bool errorThrown = false;
1343 ArrayList al1 = new ArrayList (3);
1345 } catch (ArgumentOutOfRangeException) {
1348 Assert.IsTrue (errorThrown, "remove at negative index error not thrown");
1351 bool errorThrown = false;
1353 ArrayList al1 = new ArrayList (3);
1355 } catch (ArgumentOutOfRangeException) {
1358 Assert.IsTrue (errorThrown, "remove at out-of-range index error not thrown");
1361 char [] c = { 'a', 'b', 'c' };
1362 ArrayList a = new ArrayList (c);
1364 Assert.AreEqual (2, a.Count, "should be changed");
1365 Assert.AreEqual ('b', a [0], "should have shifted");
1366 Assert.AreEqual ('c', a [1], "should have shifted");
1371 public void TestRemoveRange ()
1374 bool errorThrown = false;
1377 ArrayList.FixedSize (new ArrayList (3));
1378 al1.RemoveRange (0, 1);
1379 } catch (NotSupportedException) {
1382 Assert.IsTrue (errorThrown, "removerange from fixed size error not thrown");
1385 bool errorThrown = false;
1388 ArrayList.ReadOnly (new ArrayList (3));
1389 al1.RemoveRange (0, 1);
1390 } catch (NotSupportedException) {
1393 Assert.IsTrue (errorThrown, "removerange from read only error not thrown");
1396 bool errorThrown = false;
1398 ArrayList al1 = new ArrayList (3);
1399 al1.RemoveRange (-1, 1);
1400 } catch (ArgumentOutOfRangeException) {
1403 Assert.IsTrue (errorThrown, "removerange at negative index error not thrown");
1406 bool errorThrown = false;
1408 ArrayList al1 = new ArrayList (3);
1409 al1.RemoveRange (0, -1);
1410 } catch (ArgumentOutOfRangeException) {
1413 Assert.IsTrue (errorThrown, "removerange at negative index error not thrown");
1416 bool errorThrown = false;
1418 ArrayList al1 = new ArrayList (3);
1419 al1.RemoveRange (2, 3);
1420 } catch (ArgumentException) {
1423 Assert.IsTrue (errorThrown, "removerange at bad range error not thrown");
1426 char [] c = { 'a', 'b', 'c' };
1427 ArrayList a = new ArrayList (c);
1428 a.RemoveRange (1, 2);
1429 Assert.AreEqual (1, a.Count, "should be changed");
1430 Assert.AreEqual ('a', a [0], "should have shifted");
1435 [ExpectedException (typeof (ArgumentException))]
1436 public void RemoveRange_IndexOverflow ()
1438 ArrayList al = new ArrayList ();
1440 al.RemoveRange (Int32.MaxValue, 1);
1444 [ExpectedException (typeof (ArgumentException))]
1445 public void RemoveRange_CountOverflow ()
1447 ArrayList al = new ArrayList ();
1449 al.RemoveRange (1, Int32.MaxValue);
1453 public void TestRepeat ()
1456 bool errorThrown = false;
1458 ArrayList al1 = ArrayList.Repeat ('c', -1);
1459 } catch (ArgumentOutOfRangeException) {
1462 Assert.IsTrue (errorThrown, "repeat negative copies error not thrown");
1465 ArrayList al1 = ArrayList.Repeat ("huh?", 0);
1466 Assert.AreEqual (0, al1.Count, "should be nothing in array");
1469 ArrayList al1 = ArrayList.Repeat ("huh?", 3);
1470 Assert.AreEqual (3, al1.Count, "should be something in array");
1471 Assert.AreEqual ("huh?", al1 [0], "array elem doesn't check");
1472 Assert.AreEqual ("huh?", al1 [1], "array elem doesn't check");
1473 Assert.AreEqual ("huh?", al1 [2], "array elem doesn't check");
1478 public void TestReverse ()
1481 bool errorThrown = false;
1484 ArrayList.ReadOnly (new ArrayList ());
1486 } catch (NotSupportedException) {
1489 Assert.IsTrue (errorThrown, "reverse on read only error not thrown");
1492 bool errorThrown = false;
1494 char [] c = new Char [2];
1495 ArrayList al1 = new ArrayList (c);
1497 } catch (ArgumentException) {
1500 Assert.IsTrue (errorThrown, "error not thrown");
1503 bool errorThrown = false;
1505 char [] c = new Char [2];
1506 ArrayList al1 = new ArrayList (c);
1508 } catch (ArgumentException) {
1511 Assert.IsTrue (errorThrown, "error not thrown");
1514 char [] c = { 'a', 'b', 'c', 'd', 'e' };
1515 ArrayList al1 = new ArrayList (c);
1517 for (int i = 0; i < al1.Count; i++) {
1518 Assert.AreEqual (c [i], al1 [i], "Should be no change yet");
1521 for (int i = 0; i < al1.Count; i++) {
1522 Assert.AreEqual (c [i], al1 [4 - i], "Should be reversed");
1525 for (int i = 0; i < al1.Count; i++) {
1526 Assert.AreEqual (c [i], al1 [i], "Should be back to normal");
1529 Assert.AreEqual (c [0], al1 [0], "Should be back to normal");
1530 Assert.AreEqual (c [3], al1 [1], "Should be back to normal");
1531 Assert.AreEqual (c [2], al1 [2], "Should be back to normal");
1532 Assert.AreEqual (c [1], al1 [3], "Should be back to normal");
1533 Assert.AreEqual (c [4], al1 [4], "Should be back to normal");
1538 [ExpectedException (typeof (ArgumentException))]
1539 public void Reverse_IndexOverflow ()
1541 ArrayList al = new ArrayList ();
1543 al.Reverse (Int32.MaxValue, 1);
1547 [ExpectedException (typeof (ArgumentException))]
1548 public void Reverse_CountOverflow ()
1550 ArrayList al = new ArrayList ();
1552 al.Reverse (1, Int32.MaxValue);
1556 public void TestSetRange ()
1559 bool errorThrown = false;
1561 char [] c = { 'a', 'b', 'c' };
1563 ArrayList.ReadOnly (new ArrayList (3));
1564 al1.SetRange (0, c);
1565 } catch (NotSupportedException) {
1567 } catch (Exception e) {
1568 Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString ());
1570 Assert.IsTrue (errorThrown, "setrange on read only error not thrown");
1573 bool errorThrown = false;
1575 ArrayList al1 = new ArrayList (3);
1576 al1.SetRange (0, null);
1577 } catch (ArgumentNullException) {
1579 } catch (ArgumentOutOfRangeException) {
1581 } catch (Exception e) {
1582 Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString ());
1584 Assert.IsTrue (errorThrown, "setrange with null error not thrown");
1587 bool errorThrown = false;
1589 char [] c = { 'a', 'b', 'c' };
1590 ArrayList al1 = new ArrayList (3);
1591 al1.SetRange (-1, c);
1592 } catch (ArgumentOutOfRangeException) {
1594 } catch (Exception e) {
1595 Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString ());
1597 Assert.IsTrue (errorThrown, "setrange with negative index error not thrown");
1600 bool errorThrown = false;
1602 char [] c = { 'a', 'b', 'c' };
1603 ArrayList al1 = new ArrayList (3);
1604 al1.SetRange (2, c);
1605 } catch (ArgumentOutOfRangeException) {
1607 } catch (Exception e) {
1608 Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString ());
1610 Assert.IsTrue (errorThrown, "setrange with too much error not thrown");
1614 char [] c = { 'a', 'b', 'c' };
1615 ArrayList al1 = ArrayList.Repeat ('?', 3);
1616 Assert.IsTrue (c [0] != (char) al1 [0], "no match yet");
1617 Assert.IsTrue (c [1] != (char) al1 [1], "no match yet");
1618 Assert.IsTrue (c [2] != (char) al1 [2], "no match yet");
1619 al1.SetRange (0, c);
1620 Assert.AreEqual (c [0], al1 [0], "should match");
1621 Assert.AreEqual (c [1], al1 [1], "should match");
1622 Assert.AreEqual (c [2], al1 [2], "should match");
1627 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1628 public void SetRange_Overflow ()
1630 ArrayList al = new ArrayList ();
1632 al.SetRange (Int32.MaxValue, new ArrayList ());
1636 public void TestInsertRange_this ()
1638 String [] s1 = { "this", "is", "a", "test" };
1639 ArrayList al = new ArrayList (s1);
1640 al.InsertRange (2, al);
1641 String [] s2 = { "this", "is", "this", "is", "a", "test", "a", "test" };
1642 for (int i = 0; i < al.Count; i++) {
1643 Assert.AreEqual (s2 [i], al [i], "at i=" + i);
1648 public void TestSort ()
1651 bool errorThrown = false;
1654 ArrayList.ReadOnly (new ArrayList ());
1656 } catch (NotSupportedException) {
1659 Assert.IsTrue (errorThrown, "sort on read only error not thrown");
1662 char [] starter = { 'd', 'b', 'f', 'e', 'a', 'c' };
1663 ArrayList al1 = new ArrayList (starter);
1665 Assert.AreEqual ('a', al1 [0], "Should be sorted");
1666 Assert.AreEqual ('b', al1 [1], "Should be sorted");
1667 Assert.AreEqual ('c', al1 [2], "Should be sorted");
1668 Assert.AreEqual ('d', al1 [3], "Should be sorted");
1669 Assert.AreEqual ('e', al1 [4], "Should be sorted");
1670 Assert.AreEqual ('f', al1 [5], "Should be sorted");
1673 ArrayList al1 = new ArrayList ();
1682 Assert.AreEqual (null, al1 [0], "Should be null");
1683 Assert.AreEqual (null, al1 [1], "Should be 2. null");
1684 Assert.AreEqual (null, al1 [2], "Should be 3. null");
1685 Assert.AreEqual (null, al1 [3], "Should be 4. null");
1686 Assert.AreEqual (32, al1 [4], "Should be 32");
1687 Assert.AreEqual (33, al1 [5], "Should be 33");
1692 [ExpectedException (typeof (ArgumentException))]
1693 public void Sort_IndexOverflow ()
1695 ArrayList al = new ArrayList ();
1697 al.Sort (Int32.MaxValue, 1, null);
1701 [ExpectedException (typeof (ArgumentException))]
1702 public void Sort_CountOverflow ()
1704 ArrayList al = new ArrayList ();
1706 al.Sort (1, Int32.MaxValue, null);
1709 // TODO - Sort with IComparers
1711 // TODO - Synchronize
1714 public void TestToArray ()
1717 bool errorThrown = false;
1719 ArrayList al1 = new ArrayList (3);
1721 } catch (ArgumentNullException) {
1724 Assert.IsTrue (errorThrown, "toarray with null error not thrown");
1727 bool errorThrown = false;
1729 char [] c = { 'a', 'b', 'c' };
1731 ArrayList al1 = new ArrayList (c);
1732 al1.ToArray (s.GetType ());
1733 } catch (InvalidCastException) {
1736 Assert.IsTrue (errorThrown, "toarray with bad type error not thrown");
1739 char [] c1 = { 'a', 'b', 'c', 'd', 'e' };
1740 ArrayList al1 = new ArrayList (c1);
1741 object [] o2 = al1.ToArray ();
1742 for (int i = 0; i < c1.Length; i++) {
1743 Assert.AreEqual (c1 [i], o2 [i], "should be copy");
1745 Array c2 = al1.ToArray (c1 [0].GetType ());
1746 for (int i = 0; i < c1.Length; i++) {
1747 Assert.AreEqual (c1 [i], c2.GetValue (i), "should be copy");
1753 [ExpectedException (typeof (NotSupportedException))]
1754 public void TrimToSize_ReadOnly ()
1756 ArrayList al1 = ArrayList.ReadOnly (new ArrayList ());
1761 public void TrimToSize ()
1763 ArrayList al1 = new ArrayList ();
1765 // Capacity is 0 under 2.0
1768 int capacity = al1.Capacity;
1770 int size = capacity / 2;
1771 for (int i = 1; i <= size; i++) {
1776 Assert.AreEqual (size - 1, al1.Capacity, "no capacity match");
1780 Assert.AreEqual (capacity, al1.Capacity, "no default capacity");
1783 class Comparer : IComparer
1786 private bool called = false;
1792 bool result = called;
1798 public int Compare (object x, object y)
1806 public void BinarySearch1_EmptyList ()
1808 ArrayList list = new ArrayList ();
1809 Assert.AreEqual (-1, list.BinarySearch (0), "BinarySearch");
1813 public void BinarySearch2_EmptyList ()
1815 Comparer comparer = new Comparer ();
1816 ArrayList list = new ArrayList ();
1817 Assert.AreEqual (-1, list.BinarySearch (0, comparer), "BinarySearch");
1818 // bug 77030 - the comparer isn't called for an empty array/list
1819 Assert.IsTrue (!comparer.Called, "Called");
1823 public void BinarySearch3_EmptyList ()
1825 Comparer comparer = new Comparer ();
1826 ArrayList list = new ArrayList ();
1827 Assert.AreEqual (-1, list.BinarySearch (0, 0, 0, comparer), "BinarySearch");
1828 // bug 77030 - the comparer isn't called for an empty array/list
1829 Assert.IsTrue (!comparer.Called, "Called");
1834 [Category ("NotDotNet")] // MS bug
1836 public void AddRange_GetRange ()
1838 ArrayList source = ArrayList.Adapter (new object [] { "1", "2" });
1839 Assert.AreEqual (2, source.Count, "#1");
1840 Assert.AreEqual ("1", source [0], "#2");
1841 Assert.AreEqual ("2", source [1], "#3");
1842 ArrayList range = source.GetRange (1, 1);
1843 Assert.AreEqual (1, range.Count, "#4");
1844 Assert.AreEqual ("2", range [0], "#5");
1845 ArrayList target = new ArrayList ();
1846 target.AddRange (range);
1847 Assert.AreEqual (1, target.Count, "#6");
1848 Assert.AreEqual ("2", target [0], "#7");
1853 [Category ("NotDotNet")] // MS bug
1855 public void IterateSelf ()
1857 ArrayList list = new ArrayList ();
1859 IEnumerator enumerator = list.GetEnumerator ();
1860 Assert.IsTrue (enumerator.MoveNext (), "#1");
1861 Assert.IsTrue (object.ReferenceEquals (list, enumerator.Current), "#2");
1862 Assert.IsTrue (!enumerator.MoveNext (), "#3");