1 // ArrayListTest.cs - NUnit Test Cases for the System.Collections.ArrayList class
\r
3 // David Brandt (bucky@keystreams.com)
\r
5 // (C) Ximian, Inc. http://www.ximian.com
\r
9 using System.Collections;
\r
11 using NUnit.Framework;
\r
15 namespace MonoTests.System.Collections {
\r
18 /// <summary>ArrayList test.</summary>
\r
19 public class ArrayListTest : TestCase {
\r
20 public ArrayListTest() : base ("MonoTests.System.ArrayListTest testsuite") {}
\r
21 public ArrayListTest(string name) : base(name) {}
\r
23 protected override void SetUp()
\r
27 protected override void TearDown()
\r
31 public static ITest Suite {
\r
33 return new TestSuite(typeof(ArrayListTest));
\r
37 public void TestCtor() {
\r
39 ArrayList al1 = new ArrayList();
\r
40 AssertNotNull("no basic ArrayList", al1);
\r
43 bool errorThrown = false;
\r
45 ArrayList a = new ArrayList(null);
\r
46 } catch (ArgumentNullException) {
\r
49 Assert("null icollection error not thrown",
\r
53 // what can I say? I like chars. [--DB]
\r
54 char[] coll = {'a', 'b', 'c', 'd'};
\r
55 ArrayList al1 = new ArrayList(coll);
\r
56 AssertNotNull("no icollection ArrayList", al1);
\r
57 for (int i = 0; i < coll.Length; i++) {
\r
58 AssertEquals(i + " not ctor'ed properly.",
\r
62 // TODO - multi-dim ICollection ctor?
\r
64 bool errorThrown = false;
\r
66 ArrayList a = new ArrayList(-1);
\r
67 } catch (ArgumentOutOfRangeException) {
\r
70 Assert("negative capacity error not thrown",
\r
75 public void TestCapacity() {
\r
76 for (int i = 0; i < 100; i++) {
\r
77 ArrayList al1 = new ArrayList(i);
\r
78 AssertEquals("Bad capacity of " + i,
\r
82 ArrayList al1 = new ArrayList();
\r
83 AssertEquals("Bad default capacity",
\r
88 public void TestCount() {
\r
90 ArrayList al1 = new ArrayList();
\r
91 AssertEquals("Bad initial count",
\r
93 for (int i = 1; i <= 100; i++) {
\r
95 AssertEquals("Bad count " + i,
\r
99 for (int i = 0; i < 100; i++) {
\r
100 char[] coll = new Char[i];
\r
101 ArrayList al1 = new ArrayList(coll);
\r
102 AssertEquals("Bad count for " + i,
\r
107 public void TestIsFixed() {
\r
108 ArrayList al1 = new ArrayList();
\r
109 Assert("should not be fixed by default", !al1.IsFixedSize);
\r
110 ArrayList al2 = ArrayList.FixedSize(al1);
\r
111 Assert("fixed-size wrapper not working", al2.IsFixedSize);
\r
114 public void TestIsReadOnly() {
\r
115 ArrayList al1 = new ArrayList();
\r
116 Assert("should not be ReadOnly by default", !al1.IsReadOnly);
\r
117 ArrayList al2 = ArrayList.ReadOnly(al1);
\r
118 Assert("read-only wrapper not working", al2.IsReadOnly);
\r
121 public void TestIsSynchronized() {
\r
122 ArrayList al1 = new ArrayList();
\r
123 Assert("should not be synchronized by default",
\r
124 !al1.IsSynchronized);
\r
125 ArrayList al2 = ArrayList.Synchronized(al1);
\r
126 Assert("synchronized wrapper not working", al2.IsSynchronized);
\r
129 public void TestItem() {
\r
130 ArrayList al1 = new ArrayList();
\r
132 bool errorThrown = false;
\r
134 object o = al1[-1];
\r
135 } catch (ArgumentOutOfRangeException) {
\r
136 errorThrown = true;
\r
138 Assert("negative item error not thrown",
\r
142 bool errorThrown = false;
\r
145 } catch (ArgumentOutOfRangeException) {
\r
146 errorThrown = true;
\r
148 Assert("past-end item error not thrown",
\r
151 for (int i = 0; i <= 100; i++) {
\r
154 for (int i = 0; i <= 100; i++) {
\r
155 AssertEquals("item not fetched for " + i,
\r
160 public void TestAdapter() {
\r
162 bool errorThrown = false;
\r
164 ArrayList al1 = ArrayList.Adapter(null);
\r
165 } catch (ArgumentNullException) {
\r
166 errorThrown = true;
\r
168 Assert("null adapter error not thrown",
\r
172 char[] list = {'a', 'b', 'c', 'd'};
\r
173 ArrayList al1 = ArrayList.Adapter(list);
\r
174 AssertNotNull("Couldn't get an adapter", al1);
\r
175 for (int i = 0; i < list.Length; i++) {
\r
176 AssertEquals("adapter not adapting", list[i], al1[i]);
\r
179 for (int i = 0; i < list.Length; i++) {
\r
180 AssertEquals("adapter not adapting", list[i], al1[i]);
\r
183 // TODO - test other adapter types?
\r
186 public void TestAdd() {
\r
188 bool errorThrown = false;
\r
191 ArrayList.FixedSize(new ArrayList());
\r
193 } catch (NotSupportedException) {
\r
194 errorThrown = true;
\r
196 Assert("add to fixed size error not thrown",
\r
200 bool errorThrown = false;
\r
203 ArrayList.ReadOnly(new ArrayList());
\r
205 } catch (NotSupportedException) {
\r
206 errorThrown = true;
\r
208 Assert("add to read only error not thrown",
\r
212 ArrayList al1 = new ArrayList();
\r
213 for (int i = 1; i <= 100; i++) {
\r
215 AssertEquals("add failed " + i,
\r
217 AssertEquals("add failed " + i,
\r
224 public void TestAddRange() {
\r
226 bool errorThrown = false;
\r
229 ArrayList.FixedSize(new ArrayList());
\r
230 String[] s1 = {"Hi!"};
\r
232 } catch (NotSupportedException) {
\r
233 errorThrown = true;
\r
235 Assert("add to fixed size error not thrown",
\r
239 bool errorThrown = false;
\r
242 ArrayList.ReadOnly(new ArrayList());
\r
243 String[] s1 = {"Hi!"};
\r
245 } catch (NotSupportedException) {
\r
246 errorThrown = true;
\r
248 Assert("add to read only error not thrown",
\r
252 bool errorThrown = false;
\r
254 ArrayList al1 = new ArrayList();
\r
255 al1.AddRange(null);
\r
256 } catch (ArgumentNullException) {
\r
257 errorThrown = true;
\r
259 Assert("add to read only error not thrown",
\r
264 ArrayList a1 = new ArrayList();
\r
265 AssertEquals("ArrayList should start empty",
\r
267 char[] coll = {'a', 'b', 'c'};
\r
269 AssertEquals("ArrayList has wrong elements",
\r
272 AssertEquals("ArrayList has wrong elements",
\r
277 public void TestBinarySearch() {
\r
279 //bool errorThrown = false;
\r
281 // ArrayList al1 = new ArrayList();
\r
282 // String[] s1 = {"This", "is", "a", "test"};
\r
283 // al1.AddRange(s1);
\r
284 // al1.BinarySearch(42);
\r
285 //} catch (ArgumentException) {
\r
286 // errorThrown = true;
\r
288 //Assert("search-for-wrong-type error not thrown",
\r
293 char[] bad = {'d', 'a', 'd', 'a', 'c', 'a'};
\r
294 ArrayList al1 = new ArrayList(bad);
\r
295 AssertEquals("shouldn't find elem in badly-sorted array", -1, al1.BinarySearch('c'));
\r
298 char[] bad = {'a', 'd', 'a', 'd', 'a', 'c', 'a'};
\r
299 ArrayList al1 = new ArrayList(bad);
\r
300 AssertEquals("shouldn't find elem in badly-sorted array", -2, al1.BinarySearch('c'));
\r
303 char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
\r
304 ArrayList al1 = new ArrayList(arr);
\r
305 Assert("couldn't find elem",
\r
306 al1.BinarySearch('c') >= 3);
\r
307 Assert("couldn't find elem",
\r
308 al1.BinarySearch('c') < 6);
\r
311 char[] arr = {'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
\r
312 ArrayList al1 = new ArrayList(arr);
\r
313 AssertEquals("couldn't find next-higher elem",
\r
314 -4, al1.BinarySearch('c'));
\r
317 char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
\r
318 ArrayList al1 = new ArrayList(arr);
\r
319 AssertEquals("couldn't find end",
\r
320 -9, al1.BinarySearch('e'));
\r
325 // TODO - BinarySearch with IComparer
\r
327 public void TestClear() {
\r
329 bool errorThrown = false;
\r
332 ArrayList.FixedSize(new ArrayList());
\r
334 } catch (NotSupportedException) {
\r
335 errorThrown = true;
\r
337 Assert("add to fixed size error not thrown",
\r
341 bool errorThrown = false;
\r
344 ArrayList.ReadOnly(new ArrayList());
\r
346 } catch (NotSupportedException) {
\r
347 errorThrown = true;
\r
349 Assert("add to read only error not thrown",
\r
353 ArrayList al1 = new ArrayList();
\r
355 AssertEquals("should have one element",
\r
358 AssertEquals("should be empty",
\r
362 int[] i1 = {1,2,3,4};
\r
363 ArrayList al1 = new ArrayList(i1);
\r
364 AssertEquals("should have elements",
\r
365 i1.Length, al1.Count);
\r
367 AssertEquals("should be empty again",
\r
372 public void TestClone() {
\r
374 char[] c1 = {'a', 'b', 'c'};
\r
375 ArrayList al1 = new ArrayList(c1);
\r
376 ArrayList al2 = (ArrayList)al1.Clone();
\r
377 AssertEquals("ArrayList match", al1[0], al2[0]);
\r
378 AssertEquals("ArrayList match", al1[1], al2[1]);
\r
379 AssertEquals("ArrayList match", al1[2], al2[2]);
\r
382 char[] d10 = {'a', 'b'};
\r
383 char[] d11 = {'a', 'c'};
\r
384 char[] d12 = {'b', 'c'};
\r
385 char[][] d1 = {d10, d11, d12};
\r
386 ArrayList al1 = new ArrayList(d1);
\r
387 ArrayList al2 = (ArrayList)al1.Clone();
\r
388 AssertEquals("Array match", al1[0], al2[0]);
\r
389 AssertEquals("Array match", al1[1], al2[1]);
\r
390 AssertEquals("Array match", al1[2], al2[2]);
\r
392 ((char[])al1[0])[0] = 'z';
\r
393 AssertEquals("shallow copy", al1[0], al2[0]);
\r
397 public void TestContains() {
\r
398 char[] c1 = {'a', 'b', 'c'};
\r
399 ArrayList al1 = new ArrayList(c1);
\r
400 Assert("never find a null", !al1.Contains(null));
\r
401 Assert("can't find value", al1.Contains('b'));
\r
402 Assert("shouldn't find value", !al1.Contains('?'));
\r
405 public void TestCopyTo() {
\r
407 bool errorThrown = false;
\r
409 Char[] c1 = new Char[2];
\r
410 ArrayList al1 = new ArrayList(c1);
\r
411 al1.CopyTo(null, 2);
\r
412 } catch (ArgumentNullException) {
\r
413 errorThrown = true;
\r
415 Assert("error not thrown", errorThrown);
\r
418 bool errorThrown = false;
\r
420 Char[] c1 = new Char[2];
\r
421 ArrayList al1 = new ArrayList(c1);
\r
422 Char[,] c2 = new Char[2,2];
\r
424 } catch (ArgumentException) {
\r
425 errorThrown = true;
\r
427 Assert("error not thrown", errorThrown);
\r
430 bool errorThrown = false;
\r
432 Char[,] c1 = new Char[2,2];
\r
433 ArrayList al1 = new ArrayList(c1);
\r
434 Char[] c2 = new Char[2];
\r
436 } catch (RankException) {
\r
437 errorThrown = true;
\r
439 Assert("error not thrown", errorThrown);
\r
442 bool errorThrown = false;
\r
444 Char[] c1 = new Char[2];
\r
445 ArrayList al1 = new ArrayList(c1);
\r
446 Char[] c2 = new Char[2];
\r
447 al1.CopyTo(c2, -1);
\r
448 } catch (ArgumentOutOfRangeException) {
\r
449 errorThrown = true;
\r
451 Assert("error not thrown", errorThrown);
\r
454 bool errorThrown = false;
\r
456 Char[] c1 = new Char[2];
\r
457 ArrayList al1 = new ArrayList(c1);
\r
458 Char[] c2 = new Char[2];
\r
460 } catch (ArgumentException) {
\r
461 errorThrown = true;
\r
463 Assert("error not thrown", errorThrown);
\r
466 bool errorThrown = false;
\r
468 Char[] c1 = new Char[2];
\r
469 ArrayList al1 = new ArrayList(c1);
\r
470 Char[] c2 = new Char[2];
\r
472 } catch (ArgumentException) {
\r
473 errorThrown = true;
\r
475 Assert("error not thrown", errorThrown);
\r
478 bool errorThrown = false;
\r
480 String[] c1 = new String[2];
\r
481 ArrayList al1 = new ArrayList(c1);
\r
482 Char[] c2 = new Char[2];
\r
484 } catch (InvalidCastException) {
\r
485 errorThrown = true;
\r
487 Assert("error not thrown", errorThrown);
\r
490 Char[] orig = {'a', 'b', 'c', 'd'};
\r
491 ArrayList al = new ArrayList(orig);
\r
492 Char[] copy = new Char[10];
\r
493 Array.Clear(copy, 0, copy.Length);
\r
494 al.CopyTo(copy, 3);
\r
495 AssertEquals("Wrong CopyTo 0", (char)0, copy[0]);
\r
496 AssertEquals("Wrong CopyTo 1", (char)0, copy[1]);
\r
497 AssertEquals("Wrong CopyTo 2", (char)0, copy[2]);
\r
498 AssertEquals("Wrong CopyTo 3", orig[0], copy[3]);
\r
499 AssertEquals("Wrong CopyTo 4", orig[1], copy[4]);
\r
500 AssertEquals("Wrong CopyTo 5", orig[2], copy[5]);
\r
501 AssertEquals("Wrong CopyTo 6", orig[3], copy[6]);
\r
502 AssertEquals("Wrong CopyTo 7", (char)0, copy[7]);
\r
503 AssertEquals("Wrong CopyTo 8", (char)0, copy[8]);
\r
504 AssertEquals("Wrong CopyTo 9", (char)0, copy[9]);
\r
507 public void TestFixedSize() {
\r
509 bool errorThrown = false;
\r
511 ArrayList al1 = ArrayList.FixedSize(null);
\r
512 } catch (ArgumentNullException) {
\r
513 errorThrown = true;
\r
515 Assert("null arg error not thrown", errorThrown);
\r
518 ArrayList al1 = new ArrayList();
\r
519 AssertEquals("arrays start un-fixed.",
\r
520 false, al1.IsFixedSize);
\r
521 ArrayList al2 = ArrayList.FixedSize(al1);
\r
522 AssertEquals("should be fixed.",
\r
523 true, al2.IsFixedSize);
\r
527 public void TestGetEnumerator() {
\r
529 bool errorThrown = false;
\r
531 ArrayList a = new ArrayList();
\r
532 IEnumerator en = a.GetEnumerator(-1,1);
\r
533 } catch (ArgumentOutOfRangeException) {
\r
534 errorThrown = true;
\r
536 Assert("negative index error not thrown",
\r
540 bool errorThrown = false;
\r
542 ArrayList a = new ArrayList();
\r
543 IEnumerator en = a.GetEnumerator(1,-1);
\r
544 } catch (ArgumentOutOfRangeException) {
\r
545 errorThrown = true;
\r
547 Assert("negative index error not thrown",
\r
551 bool errorThrown = false;
\r
553 ArrayList a = new ArrayList();
\r
554 IEnumerator en = a.GetEnumerator(1,1);
\r
555 } catch (ArgumentException) {
\r
556 errorThrown = true;
\r
558 Assert("out-of-range index error not thrown",
\r
562 String[] s1 = {"this", "is", "a", "test"};
\r
563 ArrayList al1 = new ArrayList(s1);
\r
564 IEnumerator en = al1.GetEnumerator();
\r
565 AssertNotNull("No enumerator", en);
\r
567 for (int i = 0; i < s1.Length; i++) {
\r
569 AssertEquals("Not enumerating",
\r
570 al1[i], en.Current);
\r
574 String[] s1 = {"this", "is", "a", "test"};
\r
575 ArrayList al1 = new ArrayList(s1);
\r
576 IEnumerator en = al1.GetEnumerator(1,2);
\r
577 AssertNotNull("No enumerator", en);
\r
579 for (int i = 0; i < 2; i++) {
\r
581 AssertEquals("Not enumerating",
\r
582 al1[i+1], en.Current);
\r
587 public void TestGetRange() {
\r
589 bool errorThrown = false;
\r
591 ArrayList a = new ArrayList();
\r
592 ArrayList b = a.GetRange(-1,1);
\r
593 } catch (ArgumentOutOfRangeException) {
\r
594 errorThrown = true;
\r
596 Assert("negative index error not thrown",
\r
600 bool errorThrown = false;
\r
602 ArrayList a = new ArrayList();
\r
603 ArrayList b = a.GetRange(1,-1);
\r
604 } catch (ArgumentOutOfRangeException) {
\r
605 errorThrown = true;
\r
607 Assert("negative index error not thrown",
\r
611 bool errorThrown = false;
\r
613 ArrayList a = new ArrayList();
\r
614 ArrayList b = a.GetRange(1,1);
\r
615 } catch (ArgumentException) {
\r
616 errorThrown = true;
\r
618 Assert("out-of-range index error not thrown",
\r
622 char[] chars = {'a', 'b', 'c', 'd', 'e', 'f'};
\r
623 ArrayList a = new ArrayList(chars);
\r
624 ArrayList b = a.GetRange(1, 3);
\r
625 for (int i = 0; i < b.Count; i++) {
\r
626 AssertEquals("range didn't work",
\r
630 a[2] = '?'; // should screw up ArrayList b.
\r
631 bool errorThrown = false;
\r
634 } catch (InvalidOperationException) {
\r
635 errorThrown = true;
\r
637 AssertEquals("Munging 'a' should mess up 'b'",
\r
638 true, errorThrown);
\r
642 public void TestIndexOf() {
\r
644 bool errorThrown = false;
\r
646 ArrayList a = new ArrayList(1);
\r
647 int i = a.IndexOf('a', -1);
\r
648 } catch (ArgumentOutOfRangeException) {
\r
649 errorThrown = true;
\r
651 Assert("negative indexof error not thrown",
\r
655 bool errorThrown = false;
\r
657 ArrayList a = new ArrayList(1);
\r
658 int i = a.IndexOf('a', 2);
\r
659 } catch (ArgumentOutOfRangeException) {
\r
660 errorThrown = true;
\r
662 Assert("past-end indexof error not thrown",
\r
666 bool errorThrown = false;
\r
668 ArrayList a = new ArrayList(1);
\r
669 int i = a.IndexOf('a', 0, -1);
\r
670 } catch (ArgumentOutOfRangeException) {
\r
671 errorThrown = true;
\r
673 Assert("negative indexof error not thrown",
\r
677 bool errorThrown = false;
\r
679 ArrayList a = new ArrayList(1);
\r
680 int i = a.IndexOf('a', 0, 2);
\r
681 } catch (ArgumentOutOfRangeException) {
\r
682 errorThrown = true;
\r
684 Assert("past-end indexof error not thrown",
\r
688 bool errorThrown = false;
\r
690 ArrayList a = new ArrayList(2);
\r
691 int i = a.IndexOf('a', 1, 2);
\r
692 } catch (ArgumentOutOfRangeException) {
\r
693 errorThrown = true;
\r
695 Assert("past-end indexof error not thrown",
\r
699 char[] c = {'a', 'b', 'c', 'd', 'e'};
\r
700 ArrayList a = new ArrayList(c);
\r
701 AssertEquals("never find null",
\r
702 -1, a.IndexOf(null));
\r
703 AssertEquals("never find null",
\r
704 -1, a.IndexOf(null, 0));
\r
705 AssertEquals("never find null",
\r
706 -1, a.IndexOf(null, 0, 5));
\r
707 AssertEquals("can't find elem",
\r
708 2, a.IndexOf('c'));
\r
709 AssertEquals("can't find elem",
\r
710 2, a.IndexOf('c', 2));
\r
711 AssertEquals("can't find elem",
\r
712 2, a.IndexOf('c', 2, 2));
\r
713 AssertEquals("shouldn't find elem",
\r
714 -1, a.IndexOf('c', 3, 2));
\r
715 AssertEquals("shouldn't find", -1, a.IndexOf('?'));
\r
716 AssertEquals("shouldn't find", -1, a.IndexOf(3));
\r
720 public void TestInsert() {
\r
722 bool errorThrown = false;
\r
725 ArrayList.FixedSize(new ArrayList());
\r
726 al1.Insert(0, "Hi!");
\r
727 } catch (NotSupportedException) {
\r
728 errorThrown = true;
\r
730 Assert("insert to fixed size error not thrown",
\r
734 bool errorThrown = false;
\r
737 ArrayList.ReadOnly(new ArrayList());
\r
738 al1.Insert(0, "Hi!");
\r
739 } catch (NotSupportedException) {
\r
740 errorThrown = true;
\r
742 Assert("insert to read only error not thrown",
\r
746 bool errorThrown = false;
\r
748 ArrayList al1 = new ArrayList(3);
\r
749 al1.Insert(-1, "Hi!");
\r
750 } catch (ArgumentOutOfRangeException) {
\r
751 errorThrown = true;
\r
753 Assert("insert to read only error not thrown",
\r
757 bool errorThrown = false;
\r
759 ArrayList al1 = new ArrayList(3);
\r
760 al1.Insert(4, "Hi!");
\r
761 } catch (ArgumentOutOfRangeException) {
\r
762 errorThrown = true;
\r
764 Assert("insert to read only error not thrown",
\r
768 ArrayList al1 = new ArrayList();
\r
769 AssertEquals("arraylist starts empty", 0, al1.Count);
\r
770 al1.Insert(0, 'a');
\r
771 al1.Insert(1, 'b');
\r
772 al1.Insert(0, 'c');
\r
773 AssertEquals("arraylist needs stuff", 3, al1.Count);
\r
774 AssertEquals("arraylist got stuff", 'c', al1[0]);
\r
775 AssertEquals("arraylist got stuff", 'a', al1[1]);
\r
776 AssertEquals("arraylist got stuff", 'b', al1[2]);
\r
780 public void TestInsertRange() {
\r
782 bool errorThrown = false;
\r
785 ArrayList.FixedSize(new ArrayList());
\r
786 string[] s = {"Hi!"};
\r
787 al1.InsertRange(0, s);
\r
788 } catch (NotSupportedException) {
\r
789 errorThrown = true;
\r
791 Assert("insert to fixed size error not thrown",
\r
795 bool errorThrown = false;
\r
798 ArrayList.ReadOnly(new ArrayList());
\r
799 string[] s = {"Hi!"};
\r
800 al1.InsertRange(0, s);
\r
801 } catch (NotSupportedException) {
\r
802 errorThrown = true;
\r
804 Assert("insert to read only error not thrown",
\r
808 bool errorThrown = false;
\r
810 ArrayList al1 = new ArrayList(3);
\r
811 string[] s = {"Hi!"};
\r
812 al1.InsertRange(-1, s);
\r
813 } catch (ArgumentOutOfRangeException) {
\r
814 errorThrown = true;
\r
816 Assert("negative index insert error not thrown",
\r
820 bool errorThrown = false;
\r
822 ArrayList al1 = new ArrayList(3);
\r
823 string[] s = {"Hi!"};
\r
824 al1.InsertRange(4, s);
\r
825 } catch (ArgumentOutOfRangeException) {
\r
826 errorThrown = true;
\r
828 Assert("out-of-range insert error not thrown",
\r
832 bool errorThrown = false;
\r
834 ArrayList al1 = new ArrayList(3);
\r
835 al1.InsertRange(0, null);
\r
836 } catch (ArgumentNullException) {
\r
837 errorThrown = true;
\r
839 Assert("null insert error not thrown",
\r
843 char[] c = {'a', 'b', 'c'};
\r
844 ArrayList a = new ArrayList(c);
\r
845 a.InsertRange(1, c);
\r
846 AssertEquals("bad insert", 'a', a[0]);
\r
847 AssertEquals("bad insert", 'a', a[1]);
\r
848 AssertEquals("bad insert", 'b', a[2]);
\r
849 AssertEquals("bad insert", 'c', a[3]);
\r
850 AssertEquals("bad insert", 'b', a[4]);
\r
851 AssertEquals("bad insert", 'c', a[5]);
\r
855 public void TestLastIndexOf() {
\r
857 //bool errorThrown = false;
\r
859 //ArrayList a = new ArrayList(1);
\r
860 //int i = a.LastIndexOf('a', -1);
\r
861 //} catch (ArgumentOutOfRangeException) {
\r
862 //errorThrown = true;
\r
864 //Assert("first negative lastindexof error not thrown",
\r
868 bool errorThrown = false;
\r
870 ArrayList a = new ArrayList(1);
\r
871 int i = a.LastIndexOf('a', 2);
\r
872 } catch (ArgumentOutOfRangeException) {
\r
873 errorThrown = true;
\r
875 Assert("past-end lastindexof error not thrown",
\r
879 //bool errorThrown = false;
\r
881 //ArrayList a = new ArrayList(1);
\r
882 //int i = a.LastIndexOf('a', 0, -1);
\r
883 //} catch (ArgumentOutOfRangeException) {
\r
884 //errorThrown = true;
\r
886 //Assert("second negative lastindexof error not thrown",
\r
890 //bool errorThrown = false;
\r
892 //ArrayList a = new ArrayList(1);
\r
893 //int i = a.LastIndexOf('a', 0, 2);
\r
894 //} catch (ArgumentOutOfRangeException) {
\r
895 //errorThrown = true;
\r
897 //Assert("past-end lastindexof error not thrown",
\r
901 //bool errorThrown = false;
\r
903 //ArrayList a = new ArrayList(2);
\r
904 //int i = a.LastIndexOf('a', 0, 2);
\r
905 //} catch (ArgumentOutOfRangeException) {
\r
906 //errorThrown = true;
\r
908 //Assert("past-end lastindexof error not thrown",
\r
912 char[] c = {'a', 'b', 'c', 'd', 'e'};
\r
913 ArrayList a = new ArrayList(c);
\r
914 AssertEquals("never find null",
\r
915 -1, a.LastIndexOf(null));
\r
916 AssertEquals("never find null",
\r
917 -1, a.LastIndexOf(null, 4));
\r
918 AssertEquals("never find null",
\r
919 -1, a.LastIndexOf(null, 4, 5));
\r
920 AssertEquals("can't find elem",
\r
921 2, a.LastIndexOf('c'));
\r
922 AssertEquals("can't find elem",
\r
923 2, a.LastIndexOf('c', 4));
\r
924 AssertEquals("can't find elem",
\r
925 2, a.LastIndexOf('c', 3, 2));
\r
926 AssertEquals("shouldn't find elem",
\r
927 -1, a.LastIndexOf('c', 4, 2));
\r
928 AssertEquals("shouldn't find", -1, a.LastIndexOf('?'));
\r
929 AssertEquals("shouldn't find", -1, a.LastIndexOf(1));
\r
933 public void TestReadOnly() {
\r
935 bool errorThrown = false;
\r
937 ArrayList al1 = ArrayList.ReadOnly(null);
\r
938 } catch (ArgumentNullException) {
\r
939 errorThrown = true;
\r
941 Assert("null arg error not thrown", errorThrown);
\r
944 ArrayList al1 = new ArrayList();
\r
945 AssertEquals("arrays start writeable.",
\r
946 false, al1.IsReadOnly);
\r
947 ArrayList al2 = ArrayList.ReadOnly(al1);
\r
948 AssertEquals("should be readonly.",
\r
949 true, al2.IsReadOnly);
\r
953 public void TestRemove() {
\r
955 bool errorThrown = false;
\r
958 ArrayList.FixedSize(new ArrayList(3));
\r
960 } catch (NotSupportedException) {
\r
961 errorThrown = true;
\r
963 Assert("remove fixed size error not thrown",
\r
967 bool errorThrown = false;
\r
970 ArrayList.ReadOnly(new ArrayList(3));
\r
972 } catch (NotSupportedException) {
\r
973 errorThrown = true;
\r
975 Assert("remove read only error not thrown",
\r
979 char[] c = {'a','b','c'};
\r
980 ArrayList a = new ArrayList(c);
\r
983 AssertEquals("should be unchanged", c.Length, a.Count);
\r
985 AssertEquals("should be changed", 2, a.Count);
\r
986 AssertEquals("should have shifted", 'b', a[0]);
\r
987 AssertEquals("should have shifted", 'c', a[1]);
\r
991 public void TestRemoveAt() {
\r
993 bool errorThrown = false;
\r
996 ArrayList.FixedSize(new ArrayList(3));
\r
998 } catch (NotSupportedException) {
\r
999 errorThrown = true;
\r
1001 Assert("remove from fixed size error not thrown",
\r
1005 bool errorThrown = false;
\r
1008 ArrayList.ReadOnly(new ArrayList(3));
\r
1010 } catch (NotSupportedException) {
\r
1011 errorThrown = true;
\r
1013 Assert("remove from read only error not thrown",
\r
1017 bool errorThrown = false;
\r
1019 ArrayList al1 = new ArrayList(3);
\r
1021 } catch (ArgumentOutOfRangeException) {
\r
1022 errorThrown = true;
\r
1024 Assert("remove at negative index error not thrown",
\r
1028 bool errorThrown = false;
\r
1030 ArrayList al1 = new ArrayList(3);
\r
1032 } catch (ArgumentOutOfRangeException) {
\r
1033 errorThrown = true;
\r
1035 Assert("remove at out-of-range index error not thrown",
\r
1039 char[] c = {'a','b','c'};
\r
1040 ArrayList a = new ArrayList(c);
\r
1042 AssertEquals("should be changed", 2, a.Count);
\r
1043 AssertEquals("should have shifted", 'b', a[0]);
\r
1044 AssertEquals("should have shifted", 'c', a[1]);
\r
1048 public void TestRemoveRange() {
\r
1050 bool errorThrown = false;
\r
1053 ArrayList.FixedSize(new ArrayList(3));
\r
1054 al1.RemoveRange(0, 1);
\r
1055 } catch (NotSupportedException) {
\r
1056 errorThrown = true;
\r
1058 Assert("removerange from fixed size error not thrown",
\r
1062 bool errorThrown = false;
\r
1065 ArrayList.ReadOnly(new ArrayList(3));
\r
1066 al1.RemoveRange(0, 1);
\r
1067 } catch (NotSupportedException) {
\r
1068 errorThrown = true;
\r
1070 Assert("removerange from read only error not thrown",
\r
1074 bool errorThrown = false;
\r
1076 ArrayList al1 = new ArrayList(3);
\r
1077 al1.RemoveRange(-1, 1);
\r
1078 } catch (ArgumentOutOfRangeException) {
\r
1079 errorThrown = true;
\r
1081 Assert("removerange at negative index error not thrown",
\r
1085 bool errorThrown = false;
\r
1087 ArrayList al1 = new ArrayList(3);
\r
1088 al1.RemoveRange(0, -1);
\r
1089 } catch (ArgumentOutOfRangeException) {
\r
1090 errorThrown = true;
\r
1092 Assert("removerange at negative index error not thrown",
\r
1096 bool errorThrown = false;
\r
1098 ArrayList al1 = new ArrayList(3);
\r
1099 al1.RemoveRange(2, 3);
\r
1100 } catch (ArgumentException) {
\r
1101 errorThrown = true;
\r
1103 Assert("removerange at bad range error not thrown",
\r
1107 char[] c = {'a','b','c'};
\r
1108 ArrayList a = new ArrayList(c);
\r
1109 a.RemoveRange(1,2);
\r
1110 AssertEquals("should be changed", 1, a.Count);
\r
1111 AssertEquals("should have shifted", 'a', a[0]);
\r
1115 public void TestRepeat() {
\r
1117 bool errorThrown = false;
\r
1119 ArrayList al1 = ArrayList.Repeat('c', -1);
\r
1120 } catch (ArgumentOutOfRangeException) {
\r
1121 errorThrown = true;
\r
1123 Assert("repeat negative copies error not thrown",
\r
1127 ArrayList al1 = ArrayList.Repeat("huh?", 0);
\r
1128 AssertEquals("should be nothing in array",
\r
1132 ArrayList al1 = ArrayList.Repeat("huh?", 3);
\r
1133 AssertEquals("should be something in array",
\r
1135 AssertEquals("array elem doesn't check",
\r
1137 AssertEquals("array elem doesn't check",
\r
1139 AssertEquals("array elem doesn't check",
\r
1144 public void TestReverse() {
\r
1146 bool errorThrown = false;
\r
1149 ArrayList.ReadOnly(new ArrayList());
\r
1151 } catch (NotSupportedException) {
\r
1152 errorThrown = true;
\r
1154 Assert("reverse on read only error not thrown",
\r
1158 bool errorThrown = false;
\r
1160 char[] c = new Char[2];
\r
1161 ArrayList al1 = new ArrayList(c);
\r
1162 al1.Reverse(0, 3);
\r
1163 } catch (ArgumentException) {
\r
1164 errorThrown = true;
\r
1166 Assert("error not thrown", errorThrown);
\r
1169 bool errorThrown = false;
\r
1171 char[] c = new Char[2];
\r
1172 ArrayList al1 = new ArrayList(c);
\r
1173 al1.Reverse(3, 0);
\r
1174 } catch (ArgumentException) {
\r
1175 errorThrown = true;
\r
1177 Assert("error not thrown", errorThrown);
\r
1180 char[] c = {'a', 'b', 'c', 'd', 'e'};
\r
1181 ArrayList al1 = new ArrayList(c);
\r
1183 for (int i = 0; i < al1.Count; i++) {
\r
1184 AssertEquals("Should be no change yet",
\r
1188 for (int i = 0; i < al1.Count; i++) {
\r
1189 AssertEquals("Should be reversed",
\r
1193 for (int i = 0; i < al1.Count; i++) {
\r
1194 AssertEquals("Should be back to normal",
\r
1198 AssertEquals("Should be back to normal", c[0], al1[0]);
\r
1199 AssertEquals("Should be back to normal", c[3], al1[1]);
\r
1200 AssertEquals("Should be back to normal", c[2], al1[2]);
\r
1201 AssertEquals("Should be back to normal", c[1], al1[3]);
\r
1202 AssertEquals("Should be back to normal", c[4], al1[4]);
\r
1206 public void TestSetRange() {
\r
1208 bool errorThrown = false;
\r
1210 char[] c = {'a', 'b', 'c'};
\r
1212 ArrayList.ReadOnly(new ArrayList(3));
\r
1213 al1.SetRange(0, c);
\r
1214 } catch (NotSupportedException) {
\r
1215 errorThrown = true;
\r
1217 Assert("setrange on read only error not thrown",
\r
1221 bool errorThrown = false;
\r
1223 ArrayList al1 = new ArrayList(3);
\r
1224 al1.SetRange(0, null);
\r
1225 } catch (ArgumentNullException) {
\r
1226 errorThrown = true;
\r
1227 } catch (ArgumentOutOfRangeException) {
\r
1228 errorThrown = true;
\r
1230 Assert("setrange with null error not thrown",
\r
1234 bool errorThrown = false;
\r
1236 char[] c = {'a', 'b', 'c'};
\r
1237 ArrayList al1 = new ArrayList(3);
\r
1238 al1.SetRange(-1, c);
\r
1239 } catch (ArgumentOutOfRangeException) {
\r
1240 errorThrown = true;
\r
1242 Assert("setrange with negative index error not thrown",
\r
1246 bool errorThrown = false;
\r
1248 char[] c = {'a', 'b', 'c'};
\r
1249 ArrayList al1 = new ArrayList(3);
\r
1250 al1.SetRange(2, c);
\r
1251 } catch (ArgumentOutOfRangeException) {
\r
1252 errorThrown = true;
\r
1254 Assert("setrange with too much error not thrown",
\r
1258 char[] c = {'a', 'b', 'c'};
\r
1259 ArrayList al1 = ArrayList.Repeat('?', 3);
\r
1260 Assert("no match yet", c[0] != (char)al1[0]);
\r
1261 Assert("no match yet", c[1] != (char)al1[1]);
\r
1262 Assert("no match yet", c[2] != (char)al1[2]);
\r
1263 al1.SetRange(0, c);
\r
1264 AssertEquals("should match", c[0], al1[0]);
\r
1265 AssertEquals("should match", c[1], al1[1]);
\r
1266 AssertEquals("should match", c[2], al1[2]);
\r
1270 public void TestSort() {
\r
1272 bool errorThrown = false;
\r
1275 ArrayList.ReadOnly(new ArrayList());
\r
1277 } catch (NotSupportedException) {
\r
1278 errorThrown = true;
\r
1280 Assert("sort on read only error not thrown",
\r
1284 char[] starter = {'d', 'b', 'f', 'e', 'a', 'c'};
\r
1285 ArrayList al1 = new ArrayList(starter);
\r
1287 AssertEquals("Should be sorted", 'a', al1[0]);
\r
1288 AssertEquals("Should be sorted", 'b', al1[1]);
\r
1289 AssertEquals("Should be sorted", 'c', al1[2]);
\r
1290 AssertEquals("Should be sorted", 'd', al1[3]);
\r
1291 AssertEquals("Should be sorted", 'e', al1[4]);
\r
1292 AssertEquals("Should be sorted", 'f', al1[5]);
\r
1296 // TODO - Sort with IComparers
\r
1298 // TODO - Synchronize
\r
1300 public void TestToArray() {
\r
1302 bool errorThrown = false;
\r
1304 ArrayList al1 = new ArrayList(3);
\r
1305 al1.ToArray(null);
\r
1306 } catch (ArgumentNullException) {
\r
1307 errorThrown = true;
\r
1309 Assert("toarray with null error not thrown",
\r
1313 bool errorThrown = false;
\r
1315 char[] c = {'a', 'b', 'c'};
\r
1316 string s = "huh?";
\r
1317 ArrayList al1 = new ArrayList(c);
\r
1318 al1.ToArray(s.GetType());
\r
1319 } catch (InvalidCastException) {
\r
1320 errorThrown = true;
\r
1322 Assert("toarray with bad type error not thrown",
\r
1326 char[] c1 = {'a', 'b', 'c', 'd', 'e'};
\r
1327 ArrayList al1 = new ArrayList(c1);
\r
1328 object[] o2 = al1.ToArray();
\r
1329 for (int i = 0; i < c1.Length; i++) {
\r
1330 AssertEquals("should be copy", c1[i], o2[i]);
\r
1332 Array c2 = al1.ToArray(c1[0].GetType());
\r
1333 for (int i = 0; i < c1.Length; i++) {
\r
1334 AssertEquals("should be copy",
\r
1335 c1[i], c2.GetValue(i));
\r
1340 public void TestTrimToSize() {
\r
1342 bool errorThrown = false;
\r
1345 ArrayList.ReadOnly(new ArrayList());
\r
1347 } catch (NotSupportedException) {
\r
1348 errorThrown = true;
\r
1350 Assert("trim read only error not thrown",
\r
1354 ArrayList al1 = new ArrayList();
\r
1355 int capacity = al1.Capacity;
\r
1356 while (al1.Capacity <= capacity) {
\r
1360 AssertEquals("size not good", capacity, al1.Count);
\r
1361 Assert("capacity not good", al1.Capacity > capacity);
\r
1363 AssertEquals("no capacity match",
\r
1364 capacity, al1.Capacity);
\r
1368 AssertEquals("no default capacity",
\r
1369 capacity, al1.Capacity);
\r