(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / Test / System.Collections / ArrayListTest.cs
blob7174634ad5e248b497b3222bf8e8bb920ee806f7
1 // ArrayListTest.cs - NUnit Test Cases for the System.Collections.ArrayList class
2 //
3 // David Brandt (bucky@keystreams.com)
4 //
5 // (C) Ximian, Inc. http://www.ximian.com
6 //
8 using System;
9 using System.Collections;
11 using NUnit.Framework;
13 namespace MonoTests.System.Collections {
15 [TestFixture]
16 public class ArrayListTest : Assertion {
18 public void TestCtor() {
20 ArrayList al1 = new ArrayList();
21 AssertNotNull("no basic ArrayList", al1);
24 bool errorThrown = false;
25 try {
26 ArrayList a = new ArrayList(null);
27 } catch (ArgumentNullException) {
28 errorThrown = true;
30 Assert("null icollection error not thrown",
31 errorThrown);
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.",
40 coll[i], al1[i]);
44 try {
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;
54 try {
55 ArrayList a = new ArrayList(-1);
56 } catch (ArgumentOutOfRangeException) {
57 errorThrown = true;
59 Assert("negative capacity error not thrown",
60 errorThrown);
64 public void TestCapacity() {
65 for (int i = 1; i < 100; i++) {
66 ArrayList al1 = new ArrayList(i);
67 AssertEquals("Bad capacity of " + i,
68 i, al1.Capacity);
71 ArrayList al1 = new ArrayList(0);
72 // LAMESPEC:
73 // AssertEquals("Bad capacity when set to 0",
74 // 16, al1.Capacity);
75 al1.Add ("?");
76 AssertEquals("Bad capacity when set to 0",
77 16, al1.Capacity);
80 ArrayList al1 = new ArrayList();
81 AssertEquals("Bad default capacity",
82 16, al1.Capacity);
86 public void TestCount() {
88 ArrayList al1 = new ArrayList();
89 AssertEquals("Bad initial count",
90 0, al1.Count);
91 for (int i = 1; i <= 100; i++) {
92 al1.Add(i);
93 AssertEquals("Bad count " + i,
94 i, al1.Count);
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,
101 i, al1.Count);
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;
131 try {
132 object o = al1[-1];
133 } catch (ArgumentOutOfRangeException) {
134 errorThrown = true;
136 Assert("negative item error not thrown",
137 errorThrown);
140 bool errorThrown = false;
141 try {
142 object o = al1[1];
143 } catch (ArgumentOutOfRangeException) {
144 errorThrown = true;
146 Assert("past-end item error not thrown",
147 errorThrown);
149 for (int i = 0; i <= 100; i++) {
150 al1.Add(i);
152 for (int i = 0; i <= 100; i++) {
153 AssertEquals("item not fetched for " + i,
154 i, al1[i]);
158 public void TestAdapter() {
160 bool errorThrown = false;
161 try {
162 ArrayList al1 = ArrayList.Adapter(null);
163 } catch (ArgumentNullException) {
164 errorThrown = true;
165 } catch (Exception e) {
166 Fail ("Incorrect exception thrown at 1: " + e.ToString());
168 Assert("null adapter error not thrown",
169 errorThrown);
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]);
178 list[0] = 'z';
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;
186 try {
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
193 errorThrown = true;
194 } catch (ArgumentException) {
195 // this is what the docs say it should throw
196 errorThrown = true;
197 } catch (Exception e) {
198 Fail ("Incorrect exception thrown at 1: " + e.ToString());
200 Assert("search-for-wrong-type error not thrown",
201 errorThrown);
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'));
224 // Sort
226 char[] starter = {'d', 'b', 'f', 'e', 'a', 'c'};
227 ArrayList al1 = ArrayList.Adapter (starter);
228 al1.Sort();
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;
243 try {
244 ArrayList al1 =
245 ArrayList.FixedSize(new ArrayList());
246 al1.Add("Hi!");
247 } catch (NotSupportedException) {
248 errorThrown = true;
249 } catch (Exception e) {
250 Fail ("Incorrect exception thrown at 1: " + e.ToString());
252 Assert("add to fixed size error not thrown",
253 errorThrown);
256 bool errorThrown = false;
257 try {
258 ArrayList al1 =
259 ArrayList.ReadOnly(new ArrayList());
260 al1.Add("Hi!");
261 } catch (NotSupportedException) {
262 errorThrown = true;
263 } catch (Exception e) {
264 Fail ("Incorrect exception thrown at 2: " + e.ToString());
266 Assert("add to read only error not thrown",
267 errorThrown);
270 ArrayList al1 = new ArrayList();
271 for (int i = 1; i <= 100; i++) {
272 al1.Add(i);
273 AssertEquals("add failed " + i,
274 i, al1.Count);
275 AssertEquals("add failed " + i,
276 i, al1[i-1]);
281 string [] strArray = new string [] {};
282 ArrayList al1 = new ArrayList (strArray);
283 al1.Add ("Hi!");
284 al1.Add ("Hi!");
285 AssertEquals ("add failed", 2, al1.Count);
289 public void TestAddRange() {
291 bool errorThrown = false;
292 try {
293 ArrayList al1 =
294 ArrayList.FixedSize(new ArrayList());
295 String[] s1 = {"Hi!"};
296 al1.AddRange(s1);
297 } catch (NotSupportedException) {
298 errorThrown = true;
299 } catch (Exception e) {
300 Fail ("Incorrect exception thrown at 1: " + e.ToString());
302 Assert("add to fixed size error not thrown",
303 errorThrown);
306 bool errorThrown = false;
307 try {
308 ArrayList al1 =
309 ArrayList.ReadOnly(new ArrayList());
310 String[] s1 = {"Hi!"};
311 al1.AddRange(s1);
312 } catch (NotSupportedException) {
313 errorThrown = true;
314 } catch (Exception e) {
315 Fail ("Incorrect exception thrown at 2: " + e.ToString());
317 Assert("add to read only error not thrown",
318 errorThrown);
321 bool errorThrown = false;
322 try {
323 ArrayList al1 = new ArrayList();
324 al1.AddRange(null);
325 } catch (ArgumentNullException) {
326 errorThrown = true;
327 } catch (Exception e) {
328 Fail ("Incorrect exception thrown at 3: " + e.ToString());
330 Assert("add to read only error not thrown",
331 errorThrown);
335 ArrayList a1 = new ArrayList();
336 AssertEquals("ArrayList should start empty",
337 0, a1.Count);
338 char[] coll = {'a', 'b', 'c'};
339 a1.AddRange(coll);
340 AssertEquals("ArrayList has wrong elements",
341 3, a1.Count);
342 a1.AddRange(coll);
343 AssertEquals("ArrayList has wrong elements",
344 6, a1.Count);
348 ArrayList list = new ArrayList ();
350 for (int i = 0; i < 100; i ++) {
351 list.Add (1);
354 AssertEquals ("BinarySearch off-by-one bug",
355 49, list.BinarySearch (1));
359 public void TestBinarySearch() {
361 bool errorThrown = false;
362 try {
363 ArrayList al1 = new ArrayList();
364 String[] s1 = {"This", "is", "a", "test"};
365 al1.AddRange(s1);
366 al1.BinarySearch(42);
367 } catch (InvalidOperationException) {
368 // this is what .NET throws
369 errorThrown = true;
370 } catch (ArgumentException) {
371 // this is what the docs say it should throw
372 errorThrown = true;
373 } catch (Exception e) {
374 Fail ("Incorrect exception thrown at 1: " + e.ToString());
376 Assert("search-for-wrong-type error not thrown",
377 errorThrown);
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'));
403 [Test]
404 [ExpectedException (typeof (ArgumentException))]
405 public void BinarySearch_IndexOverflow ()
407 ArrayList al = new ArrayList ();
408 al.Add (this);
409 al.BinarySearch (Int32.MaxValue, 1, this, null);
412 [Test]
413 [ExpectedException (typeof (ArgumentException))]
414 public void BinarySearch_CountOverflow ()
416 ArrayList al = new ArrayList ();
417 al.Add (this);
418 al.BinarySearch (1, Int32.MaxValue, this, null);
421 [Test]
422 public void BinarySearch_Null ()
424 ArrayList al = new ArrayList ();
425 al.Add (this);
426 AssertEquals ("null", -1, al.BinarySearch (null));
429 // TODO - BinarySearch with IComparer
431 public void TestClear() {
433 bool errorThrown = false;
434 try {
435 ArrayList al1 =
436 ArrayList.FixedSize(new ArrayList());
437 al1.Clear();
438 } catch (NotSupportedException) {
439 errorThrown = true;
441 Assert("add to fixed size error not thrown",
442 errorThrown);
445 bool errorThrown = false;
446 try {
447 ArrayList al1 =
448 ArrayList.ReadOnly(new ArrayList());
449 al1.Clear();
450 } catch (NotSupportedException) {
451 errorThrown = true;
453 Assert("add to read only error not thrown",
454 errorThrown);
457 ArrayList al1 = new ArrayList();
458 al1.Add('c');
459 AssertEquals("should have one element",
460 1, al1.Count);
461 al1.Clear();
462 AssertEquals("should be empty",
463 0, al1.Count);
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;
471 al1.Clear();
472 AssertEquals("should be empty again",
473 0, al1.Count);
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;
515 try {
516 Char[] c1 = new Char[2];
517 ArrayList al1 = new ArrayList(c1);
518 al1.CopyTo(null, 2);
519 } catch (ArgumentNullException) {
520 errorThrown = true;
521 } catch (Exception e) {
522 Fail ("Incorrect exception thrown at 1: " + e.ToString());
524 Assert("error not thrown 1", errorThrown);
527 bool errorThrown = false;
528 try {
529 Char[] c1 = new Char[2];
530 ArrayList al1 = new ArrayList(c1);
531 Char[,] c2 = new Char[2,2];
532 al1.CopyTo(c2, 2);
533 } catch (ArgumentException) {
534 errorThrown = true;
535 } catch (Exception e) {
536 Fail ("Incorrect exception thrown at 2: " + e.ToString());
538 Assert("error not thrown 2", errorThrown);
541 bool errorThrown = false;
542 try {
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];
553 al1.CopyTo(c2, 2);
554 } catch (ArgumentException) {
555 errorThrown = true;
556 } catch (Exception e) {
557 Fail ("Incorrect exception thrown at 3: " + e.ToString());
559 Assert("error not thrown 3", errorThrown);
562 bool errorThrown = false;
563 try {
564 Char[] c1 = new Char[2];
565 ArrayList al1 = new ArrayList(c1);
566 Char[] c2 = new Char[2];
567 al1.CopyTo(c2, -1);
568 } catch (ArgumentOutOfRangeException) {
569 errorThrown = true;
570 } catch (Exception e) {
571 Fail ("Incorrect exception thrown at 4: " + e.ToString());
573 Assert("error not thrown 4", errorThrown);
576 bool errorThrown = false;
577 try {
578 Char[] c1 = new Char[2];
579 ArrayList al1 = new ArrayList(c1);
580 Char[] c2 = new Char[2];
581 al1.CopyTo(c2, 3);
582 } catch (ArgumentException) {
583 errorThrown = true;
584 } catch (Exception e) {
585 Fail ("Incorrect exception thrown at 5: " + e.ToString());
587 Assert("error not thrown 5", errorThrown);
590 bool errorThrown = false;
591 try {
592 Char[] c1 = new Char[2];
593 ArrayList al1 = new ArrayList(c1);
594 Char[] c2 = new Char[2];
595 al1.CopyTo(c2, 1);
596 } catch (ArgumentException) {
597 errorThrown = true;
598 } catch (Exception e) {
599 Fail ("Incorrect exception thrown at 6: " + e.ToString());
601 Assert("error not thrown 6", errorThrown);
604 bool errorThrown = false;
605 try {
606 String[] c1 = {"String", "array"};
607 ArrayList al1 = new ArrayList(c1);
608 Char[] c2 = new Char[2];
609 al1.CopyTo(c2, 0);
610 } catch (InvalidCastException) {
611 errorThrown = true;
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);
622 al.CopyTo(copy, 3);
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]);
635 [Test]
636 [ExpectedException (typeof (ArgumentException))]
637 public void CopyTo_IndexOverflow ()
639 ArrayList al = new ArrayList ();
640 al.Add (this);
641 al.CopyTo (Int32.MaxValue, new byte [2], 0, 0);
644 [Test]
645 [ExpectedException (typeof (ArgumentException))]
646 public void CopyTo_ArrayIndexOverflow ()
648 ArrayList al = new ArrayList ();
649 al.Add (this);
650 al.CopyTo (0, new byte [2], Int32.MaxValue, 0);
653 [Test]
654 [ExpectedException (typeof (ArgumentException))]
655 public void CopyTo_CountOverflow ()
657 ArrayList al = new ArrayList ();
658 al.Add (this);
659 al.CopyTo (0, new byte [2], 0, Int32.MaxValue);
662 public void TestFixedSize() {
664 bool errorThrown = false;
665 try {
666 ArrayList al1 = ArrayList.FixedSize(null);
667 } catch (ArgumentNullException) {
668 errorThrown = true;
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();
686 en.MoveNext();
687 al1.Add("something");
688 try {
689 en.MoveNext();
690 Fail("Add() didn't invalidate the enumerator");
691 } catch (InvalidOperationException) {
692 // do nothing...this is what we expect
695 en = al1.GetEnumerator();
696 en.MoveNext();
697 al1.AddRange(al1);
698 try {
699 en.MoveNext();
700 Fail("AddRange() didn't invalidate the enumerator");
701 } catch (InvalidOperationException) {
702 // do nothing...this is what we expect
705 en = al1.GetEnumerator();
706 en.MoveNext();
707 al1.Clear();
708 try {
709 en.MoveNext();
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();
717 en.MoveNext();
718 al1.Insert(0, "new first");
719 try {
720 en.MoveNext();
721 Fail("Insert() didn't invalidate the enumerator");
722 } catch (InvalidOperationException) {
723 // do nothing...this is what we expect
726 en = al1.GetEnumerator();
727 en.MoveNext();
728 al1.InsertRange(0, al1);
729 try {
730 en.MoveNext();
731 Fail("InsertRange() didn't invalidate the enumerator");
732 } catch (InvalidOperationException) {
733 // do nothing...this is what we expect
736 en = al1.GetEnumerator();
737 en.MoveNext();
738 al1.Remove("this");
739 try {
740 en.MoveNext();
741 Fail("Remove() didn't invalidate the enumerator");
742 } catch (InvalidOperationException) {
743 // do nothing...this is what we expect
746 en = al1.GetEnumerator();
747 en.MoveNext();
748 al1.RemoveAt(2);
749 try {
750 en.MoveNext();
751 Fail("RemoveAt() didn't invalidate the enumerator");
752 } catch (InvalidOperationException) {
753 // do nothing...this is what we expect
756 en = al1.GetEnumerator();
757 en.MoveNext();
758 al1.RemoveRange(1, 1);
759 try {
760 en.MoveNext();
761 Fail("RemoveRange() didn't invalidate the enumerator");
762 } catch (InvalidOperationException) {
763 // do nothing...this is what we expect
766 en = al1.GetEnumerator();
767 en.MoveNext();
768 al1.Reverse();
769 try {
770 en.MoveNext();
771 Fail("Reverse() didn't invalidate the enumerator");
772 } catch (InvalidOperationException) {
773 // do nothing...this is what we expect
776 en = al1.GetEnumerator();
777 en.MoveNext();
778 al1.Sort();
779 try {
780 en.MoveNext();
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;
790 try {
791 ArrayList a = new ArrayList();
792 IEnumerator en = a.GetEnumerator(-1,1);
793 } catch (ArgumentOutOfRangeException) {
794 errorThrown = true;
796 Assert("negative index error not thrown",
797 errorThrown);
800 bool errorThrown = false;
801 try {
802 ArrayList a = new ArrayList();
803 IEnumerator en = a.GetEnumerator(1,-1);
804 } catch (ArgumentOutOfRangeException) {
805 errorThrown = true;
807 Assert("negative index error not thrown",
808 errorThrown);
811 bool errorThrown = false;
812 try {
813 ArrayList a = new ArrayList();
814 IEnumerator en = a.GetEnumerator(1,1);
815 } catch (ArgumentException) {
816 errorThrown = true;
818 Assert("out-of-range index error not thrown",
819 errorThrown);
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++) {
828 en.MoveNext();
829 AssertEquals("Not enumerating",
830 al1[i], en.Current);
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++) {
840 en.MoveNext();
841 AssertEquals("Not enumerating",
842 al1[i+1], en.Current);
847 [Test]
848 [ExpectedException (typeof (ArgumentException))]
849 public void GetEnumerator_IndexOverflow ()
851 ArrayList al = new ArrayList ();
852 al.Add (this);
853 al.GetEnumerator (Int32.MaxValue, 0);
856 [Test]
857 [ExpectedException (typeof (ArgumentException))]
858 public void GetEnumerator_CountOverflow ()
860 ArrayList al = new ArrayList ();
861 al.Add (this);
862 al.GetEnumerator (0, Int32.MaxValue);
865 public void TestGetRange() {
867 bool errorThrown = false;
868 try {
869 ArrayList a = new ArrayList();
870 ArrayList b = a.GetRange(-1,1);
871 } catch (ArgumentOutOfRangeException) {
872 errorThrown = true;
874 Assert("negative index error not thrown",
875 errorThrown);
878 bool errorThrown = false;
879 try {
880 ArrayList a = new ArrayList();
881 ArrayList b = a.GetRange(1,-1);
882 } catch (ArgumentOutOfRangeException) {
883 errorThrown = true;
885 Assert("negative index error not thrown",
886 errorThrown);
889 bool errorThrown = false;
890 try {
891 ArrayList a = new ArrayList();
892 ArrayList b = a.GetRange(1,1);
893 } catch (ArgumentException) {
894 errorThrown = true;
896 Assert("out-of-range index error not thrown",
897 errorThrown);
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",
906 chars[i+1], b[i]);
909 a[2] = '?'; // should screw up ArrayList b.
910 bool errorThrown = false;
911 try {
912 int i = b.Count;
913 } catch (InvalidOperationException) {
914 errorThrown = true;
916 AssertEquals("Munging 'a' should mess up 'b'",
917 true, errorThrown);
921 [Test]
922 [ExpectedException (typeof (ArgumentException))]
923 public void GetRange_IndexOverflow ()
925 ArrayList al = new ArrayList ();
926 al.Add (this);
927 al.GetRange (Int32.MaxValue, 0);
930 [Test]
931 [ExpectedException (typeof (ArgumentException))]
932 public void GetRange_CountOverflow ()
934 ArrayList al = new ArrayList ();
935 al.Add (this);
936 al.GetRange (0, Int32.MaxValue);
939 public void TestIndexOf() {
941 bool errorThrown = false;
942 try {
943 ArrayList a = new ArrayList(1);
944 int i = a.IndexOf('a', -1);
945 } catch (ArgumentOutOfRangeException) {
946 errorThrown = true;
948 Assert("negative indexof error not thrown",
949 errorThrown);
952 bool errorThrown = false;
953 try {
954 ArrayList a = new ArrayList(1);
955 int i = a.IndexOf('a', 2);
956 } catch (ArgumentOutOfRangeException) {
957 errorThrown = true;
959 Assert("past-end indexof error not thrown",
960 errorThrown);
963 bool errorThrown = false;
964 try {
965 ArrayList a = new ArrayList(1);
966 int i = a.IndexOf('a', 0, -1);
967 } catch (ArgumentOutOfRangeException) {
968 errorThrown = true;
970 Assert("negative indexof error not thrown",
971 errorThrown);
974 bool errorThrown = false;
975 try {
976 ArrayList a = new ArrayList(1);
977 int i = a.IndexOf('a', 0, 2);
978 } catch (ArgumentOutOfRangeException) {
979 errorThrown = true;
981 Assert("past-end indexof error not thrown",
982 errorThrown);
985 bool errorThrown = false;
986 try {
987 ArrayList a = new ArrayList(2);
988 int i = a.IndexOf('a', 1, 2);
989 } catch (ArgumentOutOfRangeException) {
990 errorThrown = true;
992 Assert("past-end indexof error not thrown",
993 errorThrown);
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",
1005 2, a.IndexOf('c'));
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));
1017 [Test]
1018 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1019 public void IndexOf_StartIndexOverflow ()
1021 ArrayList al = new ArrayList ();
1022 al.Add (this);
1023 al.IndexOf ('a', Int32.MaxValue, 1);
1026 [Test]
1027 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1028 public void IndexOf_CountOverflow ()
1030 ArrayList al = new ArrayList ();
1031 al.Add (this);
1032 al.IndexOf ('a', 1, Int32.MaxValue);
1035 public void TestInsert() {
1037 bool errorThrown = false;
1038 try {
1039 ArrayList al1 =
1040 ArrayList.FixedSize(new ArrayList());
1041 al1.Insert(0, "Hi!");
1042 } catch (NotSupportedException) {
1043 errorThrown = true;
1045 Assert("insert to fixed size error not thrown",
1046 errorThrown);
1049 bool errorThrown = false;
1050 try {
1051 ArrayList al1 =
1052 ArrayList.ReadOnly(new ArrayList());
1053 al1.Insert(0, "Hi!");
1054 } catch (NotSupportedException) {
1055 errorThrown = true;
1057 Assert("insert to read only error not thrown",
1058 errorThrown);
1061 bool errorThrown = false;
1062 try {
1063 ArrayList al1 = new ArrayList(3);
1064 al1.Insert(-1, "Hi!");
1065 } catch (ArgumentOutOfRangeException) {
1066 errorThrown = true;
1068 Assert("insert to read only error not thrown",
1069 errorThrown);
1072 bool errorThrown = false;
1073 try {
1074 ArrayList al1 = new ArrayList(3);
1075 al1.Insert(4, "Hi!");
1076 } catch (ArgumentOutOfRangeException) {
1077 errorThrown = true;
1079 Assert("insert to read only error not thrown",
1080 errorThrown);
1083 ArrayList al1 = new ArrayList();
1084 AssertEquals("arraylist starts empty", 0, al1.Count);
1085 al1.Insert(0, 'a');
1086 al1.Insert(1, 'b');
1087 al1.Insert(0, 'c');
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;
1098 try {
1099 ArrayList al1 =
1100 ArrayList.FixedSize(new ArrayList());
1101 string[] s = {"Hi!"};
1102 al1.InsertRange(0, s);
1103 } catch (NotSupportedException) {
1104 errorThrown = true;
1106 Assert("insert to fixed size error not thrown",
1107 errorThrown);
1110 bool errorThrown = false;
1111 try {
1112 ArrayList al1 =
1113 ArrayList.ReadOnly(new ArrayList());
1114 string[] s = {"Hi!"};
1115 al1.InsertRange(0, s);
1116 } catch (NotSupportedException) {
1117 errorThrown = true;
1119 Assert("insert to read only error not thrown",
1120 errorThrown);
1123 bool errorThrown = false;
1124 try {
1125 ArrayList al1 = new ArrayList(3);
1126 string[] s = {"Hi!"};
1127 al1.InsertRange(-1, s);
1128 } catch (ArgumentOutOfRangeException) {
1129 errorThrown = true;
1131 Assert("negative index insert error not thrown",
1132 errorThrown);
1135 bool errorThrown = false;
1136 try {
1137 ArrayList al1 = new ArrayList(3);
1138 string[] s = {"Hi!"};
1139 al1.InsertRange(4, s);
1140 } catch (ArgumentOutOfRangeException) {
1141 errorThrown = true;
1143 Assert("out-of-range insert error not thrown",
1144 errorThrown);
1147 bool errorThrown = false;
1148 try {
1149 ArrayList al1 = new ArrayList(3);
1150 al1.InsertRange(0, null);
1151 } catch (ArgumentNullException) {
1152 errorThrown = true;
1154 Assert("null insert error not thrown",
1155 errorThrown);
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;
1173 //try {
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",
1180 //errorThrown);
1183 bool errorThrown = false;
1184 try {
1185 ArrayList a = new ArrayList(1);
1186 int i = a.LastIndexOf('a', 2);
1187 } catch (ArgumentOutOfRangeException) {
1188 errorThrown = true;
1190 Assert("past-end lastindexof error not thrown",
1191 errorThrown);
1194 //bool errorThrown = false;
1195 //try {
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",
1202 //errorThrown);
1205 //bool errorThrown = false;
1206 //try {
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",
1213 //errorThrown);
1216 //bool errorThrown = false;
1217 //try {
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",
1224 //errorThrown);
1226 int iTest = 0;
1227 try {
1228 char[] c = {'a', 'b', 'c', 'd', 'e'};
1229 ArrayList a = new ArrayList(c);
1230 AssertEquals("never find null",
1231 -1, a.LastIndexOf(null));
1232 iTest++;
1233 AssertEquals("never find null",
1234 -1, a.LastIndexOf(null, 4));
1235 iTest++;
1236 AssertEquals("never find null",
1237 -1, a.LastIndexOf(null, 4, 5));
1238 iTest++;
1239 AssertEquals("can't find elem",
1240 2, a.LastIndexOf('c'));
1241 iTest++;
1242 AssertEquals("can't find elem",
1243 2, a.LastIndexOf('c', 4));
1244 iTest++;
1245 AssertEquals("can't find elem",
1246 2, a.LastIndexOf('c', 3, 2));
1247 iTest++;
1248 AssertEquals("shouldn't find elem",
1249 -1, a.LastIndexOf('c', 4, 2));
1250 iTest++;
1251 AssertEquals("shouldn't find", -1, a.LastIndexOf('?'));
1252 iTest++;
1253 AssertEquals("shouldn't find", -1, a.LastIndexOf(1));
1254 } catch (Exception e) {
1255 Fail ("Unexpected exception caught when iTest=" + iTest + ". e=" + e);
1259 [Test]
1260 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1261 public void LastIndexOf_StartIndexOverflow ()
1263 ArrayList al = new ArrayList ();
1264 al.Add (this);
1265 al.LastIndexOf ('a', Int32.MaxValue, 1);
1268 [Test]
1269 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1270 public void LastIndexOf_CountOverflow ()
1272 ArrayList al = new ArrayList ();
1273 al.Add (this);
1274 al.LastIndexOf ('a', 1, Int32.MaxValue);
1277 public void TestReadOnly() {
1279 bool errorThrown = false;
1280 try {
1281 ArrayList al1 = ArrayList.ReadOnly(null);
1282 } catch (ArgumentNullException) {
1283 errorThrown = true;
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;
1300 try {
1301 ArrayList al1 =
1302 ArrayList.FixedSize(new ArrayList(3));
1303 al1.Remove(1);
1304 } catch (NotSupportedException) {
1305 errorThrown = true;
1307 Assert("remove fixed size error not thrown",
1308 errorThrown);
1311 bool errorThrown = false;
1312 try {
1313 ArrayList al1 =
1314 ArrayList.ReadOnly(new ArrayList(3));
1315 al1.Remove(1);
1316 } catch (NotSupportedException) {
1317 errorThrown = true;
1319 Assert("remove read only error not thrown",
1320 errorThrown);
1323 char[] c = {'a','b','c'};
1324 ArrayList a = new ArrayList(c);
1325 a.Remove(1);
1326 a.Remove('?');
1327 AssertEquals("should be unchanged", c.Length, a.Count);
1328 a.Remove('a');
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;
1338 try {
1339 ArrayList al1 =
1340 ArrayList.FixedSize(new ArrayList(3));
1341 al1.RemoveAt(1);
1342 } catch (NotSupportedException) {
1343 errorThrown = true;
1345 Assert("remove from fixed size error not thrown",
1346 errorThrown);
1349 bool errorThrown = false;
1350 try {
1351 ArrayList al1 =
1352 ArrayList.ReadOnly(new ArrayList(3));
1353 al1.RemoveAt(1);
1354 } catch (NotSupportedException) {
1355 errorThrown = true;
1357 Assert("remove from read only error not thrown",
1358 errorThrown);
1361 bool errorThrown = false;
1362 try {
1363 ArrayList al1 = new ArrayList(3);
1364 al1.RemoveAt(-1);
1365 } catch (ArgumentOutOfRangeException) {
1366 errorThrown = true;
1368 Assert("remove at negative index error not thrown",
1369 errorThrown);
1372 bool errorThrown = false;
1373 try {
1374 ArrayList al1 = new ArrayList(3);
1375 al1.RemoveAt(4);
1376 } catch (ArgumentOutOfRangeException) {
1377 errorThrown = true;
1379 Assert("remove at out-of-range index error not thrown",
1380 errorThrown);
1383 char[] c = {'a','b','c'};
1384 ArrayList a = new ArrayList(c);
1385 a.RemoveAt(0);
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;
1395 try {
1396 ArrayList al1 =
1397 ArrayList.FixedSize(new ArrayList(3));
1398 al1.RemoveRange(0, 1);
1399 } catch (NotSupportedException) {
1400 errorThrown = true;
1402 Assert("removerange from fixed size error not thrown",
1403 errorThrown);
1406 bool errorThrown = false;
1407 try {
1408 ArrayList al1 =
1409 ArrayList.ReadOnly(new ArrayList(3));
1410 al1.RemoveRange(0, 1);
1411 } catch (NotSupportedException) {
1412 errorThrown = true;
1414 Assert("removerange from read only error not thrown",
1415 errorThrown);
1418 bool errorThrown = false;
1419 try {
1420 ArrayList al1 = new ArrayList(3);
1421 al1.RemoveRange(-1, 1);
1422 } catch (ArgumentOutOfRangeException) {
1423 errorThrown = true;
1425 Assert("removerange at negative index error not thrown",
1426 errorThrown);
1429 bool errorThrown = false;
1430 try {
1431 ArrayList al1 = new ArrayList(3);
1432 al1.RemoveRange(0, -1);
1433 } catch (ArgumentOutOfRangeException) {
1434 errorThrown = true;
1436 Assert("removerange at negative index error not thrown",
1437 errorThrown);
1440 bool errorThrown = false;
1441 try {
1442 ArrayList al1 = new ArrayList(3);
1443 al1.RemoveRange(2, 3);
1444 } catch (ArgumentException) {
1445 errorThrown = true;
1447 Assert("removerange at bad range error not thrown",
1448 errorThrown);
1451 char[] c = {'a','b','c'};
1452 ArrayList a = new ArrayList(c);
1453 a.RemoveRange(1,2);
1454 AssertEquals("should be changed", 1, a.Count);
1455 AssertEquals("should have shifted", 'a', a[0]);
1459 [Test]
1460 [ExpectedException (typeof (ArgumentException))]
1461 public void RemoveRange_IndexOverflow ()
1463 ArrayList al = new ArrayList ();
1464 al.Add (this);
1465 al.RemoveRange (Int32.MaxValue, 1);
1468 [Test]
1469 [ExpectedException (typeof (ArgumentException))]
1470 public void RemoveRange_CountOverflow ()
1472 ArrayList al = new ArrayList ();
1473 al.Add (this);
1474 al.RemoveRange (1, Int32.MaxValue);
1477 public void TestRepeat() {
1479 bool errorThrown = false;
1480 try {
1481 ArrayList al1 = ArrayList.Repeat('c', -1);
1482 } catch (ArgumentOutOfRangeException) {
1483 errorThrown = true;
1485 Assert("repeat negative copies error not thrown",
1486 errorThrown);
1489 ArrayList al1 = ArrayList.Repeat("huh?", 0);
1490 AssertEquals("should be nothing in array",
1491 0, al1.Count);
1494 ArrayList al1 = ArrayList.Repeat("huh?", 3);
1495 AssertEquals("should be something in array",
1496 3, al1.Count);
1497 AssertEquals("array elem doesn't check",
1498 "huh?", al1[0]);
1499 AssertEquals("array elem doesn't check",
1500 "huh?", al1[1]);
1501 AssertEquals("array elem doesn't check",
1502 "huh?", al1[2]);
1506 public void TestReverse() {
1508 bool errorThrown = false;
1509 try {
1510 ArrayList al1 =
1511 ArrayList.ReadOnly(new ArrayList());
1512 al1.Reverse();
1513 } catch (NotSupportedException) {
1514 errorThrown = true;
1516 Assert("reverse on read only error not thrown",
1517 errorThrown);
1520 bool errorThrown = false;
1521 try {
1522 char[] c = new Char[2];
1523 ArrayList al1 = new ArrayList(c);
1524 al1.Reverse(0, 3);
1525 } catch (ArgumentException) {
1526 errorThrown = true;
1528 Assert("error not thrown", errorThrown);
1531 bool errorThrown = false;
1532 try {
1533 char[] c = new Char[2];
1534 ArrayList al1 = new ArrayList(c);
1535 al1.Reverse(3, 0);
1536 } catch (ArgumentException) {
1537 errorThrown = true;
1539 Assert("error not thrown", errorThrown);
1542 char[] c = {'a', 'b', 'c', 'd', 'e'};
1543 ArrayList al1 = new ArrayList(c);
1544 al1.Reverse(2,1);
1545 for (int i = 0; i < al1.Count; i++) {
1546 AssertEquals("Should be no change yet",
1547 c[i], al1[i]);
1549 al1.Reverse();
1550 for (int i = 0; i < al1.Count; i++) {
1551 AssertEquals("Should be reversed",
1552 c[i], al1[4-i]);
1554 al1.Reverse();
1555 for (int i = 0; i < al1.Count; i++) {
1556 AssertEquals("Should be back to normal",
1557 c[i], al1[i]);
1559 al1.Reverse(1,3);
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]);
1568 [Test]
1569 [ExpectedException (typeof (ArgumentException))]
1570 public void Reverse_IndexOverflow ()
1572 ArrayList al = new ArrayList ();
1573 al.Add (this);
1574 al.Reverse (Int32.MaxValue, 1);
1577 [Test]
1578 [ExpectedException (typeof (ArgumentException))]
1579 public void Reverse_CountOverflow ()
1581 ArrayList al = new ArrayList ();
1582 al.Add (this);
1583 al.Reverse (1, Int32.MaxValue);
1586 public void TestSetRange() {
1588 bool errorThrown = false;
1589 try {
1590 char[] c = {'a', 'b', 'c'};
1591 ArrayList al1 =
1592 ArrayList.ReadOnly(new ArrayList(3));
1593 al1.SetRange(0, c);
1594 } catch (NotSupportedException) {
1595 errorThrown = true;
1596 } catch (Exception e) {
1597 Fail ("Incorrect exception thrown at 1: " + e.ToString());
1599 Assert("setrange on read only error not thrown",
1600 errorThrown);
1603 bool errorThrown = false;
1604 try {
1605 ArrayList al1 = new ArrayList(3);
1606 al1.SetRange(0, null);
1607 } catch (ArgumentNullException) {
1608 errorThrown = true;
1609 } catch (ArgumentOutOfRangeException) {
1610 errorThrown = true;
1611 } catch (Exception e) {
1612 Fail ("Incorrect exception thrown at 2: " + e.ToString());
1614 Assert("setrange with null error not thrown",
1615 errorThrown);
1618 bool errorThrown = false;
1619 try {
1620 char[] c = {'a', 'b', 'c'};
1621 ArrayList al1 = new ArrayList(3);
1622 al1.SetRange(-1, c);
1623 } catch (ArgumentOutOfRangeException) {
1624 errorThrown = true;
1625 } catch (Exception e) {
1626 Fail ("Incorrect exception thrown at 3: " + e.ToString());
1628 Assert("setrange with negative index error not thrown",
1629 errorThrown);
1632 bool errorThrown = false;
1633 try {
1634 char[] c = {'a', 'b', 'c'};
1635 ArrayList al1 = new ArrayList(3);
1636 al1.SetRange(2, c);
1637 } catch (ArgumentOutOfRangeException) {
1638 errorThrown = true;
1639 } catch (Exception e) {
1640 Fail ("Incorrect exception thrown at 4: " + e.ToString());
1642 Assert("setrange with too much error not thrown",
1643 errorThrown);
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]);
1652 al1.SetRange(0, c);
1653 AssertEquals("should match", c[0], al1[0]);
1654 AssertEquals("should match", c[1], al1[1]);
1655 AssertEquals("should match", c[2], al1[2]);
1659 [Test]
1660 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1661 public void SetRange_Overflow ()
1663 ArrayList al = new ArrayList ();
1664 al.Add (this);
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;
1681 try {
1682 ArrayList al1 =
1683 ArrayList.ReadOnly(new ArrayList());
1684 al1.Sort();
1685 } catch (NotSupportedException) {
1686 errorThrown = true;
1688 Assert("sort on read only error not thrown",
1689 errorThrown);
1692 char[] starter = {'d', 'b', 'f', 'e', 'a', 'c'};
1693 ArrayList al1 = new ArrayList(starter);
1694 al1.Sort();
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 ();
1704 al1.Add (null);
1705 al1.Add (null);
1706 al1.Add (32);
1707 al1.Add (33);
1708 al1.Add (null);
1709 al1.Add (null);
1711 al1.Sort ();
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]);
1721 [Test]
1722 [ExpectedException (typeof (ArgumentException))]
1723 public void Sort_IndexOverflow ()
1725 ArrayList al = new ArrayList ();
1726 al.Add (this);
1727 al.Sort (Int32.MaxValue, 1, null);
1730 [Test]
1731 [ExpectedException (typeof (ArgumentException))]
1732 public void Sort_CountOverflow ()
1734 ArrayList al = new ArrayList ();
1735 al.Add (this);
1736 al.Sort (1, Int32.MaxValue, null);
1739 // TODO - Sort with IComparers
1741 // TODO - Synchronize
1743 public void TestToArray() {
1745 bool errorThrown = false;
1746 try {
1747 ArrayList al1 = new ArrayList(3);
1748 al1.ToArray(null);
1749 } catch (ArgumentNullException) {
1750 errorThrown = true;
1752 Assert("toarray with null error not thrown",
1753 errorThrown);
1756 bool errorThrown = false;
1757 try {
1758 char[] c = {'a', 'b', 'c'};
1759 string s = "huh?";
1760 ArrayList al1 = new ArrayList(c);
1761 al1.ToArray(s.GetType());
1762 } catch (InvalidCastException) {
1763 errorThrown = true;
1765 Assert("toarray with bad type error not thrown",
1766 errorThrown);
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;
1786 try {
1787 ArrayList al1 =
1788 ArrayList.ReadOnly(new ArrayList());
1789 al1.TrimToSize();
1790 } catch (NotSupportedException) {
1791 errorThrown = true;
1793 Assert("trim read only error not thrown",
1794 errorThrown);
1797 ArrayList al1 = new ArrayList();
1798 int capacity = al1.Capacity;
1799 int size = capacity / 2;
1800 for (int i = 1; i <=size; i++) {
1801 al1.Add('?');
1803 al1.RemoveAt(0);
1804 al1.TrimToSize();
1805 AssertEquals("no capacity match",
1806 size - 1, al1.Capacity);
1808 al1.Clear();
1809 al1.TrimToSize();
1810 AssertEquals("no default capacity",
1811 capacity, al1.Capacity);