New tests, update
[mono.git] / mcs / class / corlib / Test / System.Collections / ArrayListTest.cs
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 // Copyright (C) 2005 Novell (http://www.novell.com)
7 // 
8
9 using System;
10 using System.Collections;
11
12 using NUnit.Framework;
13
14 namespace MonoTests.System.Collections
15 {
16         [TestFixture]
17         public class ArrayListTest : Assertion
18         {
19                 [Test]
20                 public void TestCtor ()
21                 {
22                         {
23                                 ArrayList al1 = new ArrayList ();
24                                 AssertNotNull ("no basic ArrayList", al1);
25                         }
26                         {
27                                 bool errorThrown = false;
28                                 try {
29                                         ArrayList a = new ArrayList (null);
30                                 } catch (ArgumentNullException) {
31                                         errorThrown = true;
32                                 }
33                                 Assert ("null icollection error not thrown",
34                                            errorThrown);
35                         }
36                         {
37                                 // what can I say?  I like chars.  [--DB]
38                                 char [] coll = { 'a', 'b', 'c', 'd' };
39                                 ArrayList al1 = new ArrayList (coll);
40                                 AssertNotNull ("no icollection ArrayList", al1);
41                                 for (int i = 0; i < coll.Length; i++) {
42                                         AssertEquals (i + " not ctor'ed properly.",
43                                                          coll [i], al1 [i]);
44                                 }
45                         }
46                         {
47                                 try {
48                                         Char [,] c1 = new Char [2, 2];
49                                         ArrayList al1 = new ArrayList (c1);
50                                         Fail ("Should fail with multi-dimensional array in constructor.");
51                                 } catch (RankException) {
52                                 }
53                         }
54
55                         {
56                                 bool errorThrown = false;
57                                 try {
58                                         ArrayList a = new ArrayList (-1);
59                                 } catch (ArgumentOutOfRangeException) {
60                                         errorThrown = true;
61                                 }
62                                 Assert ("negative capacity error not thrown",
63                                            errorThrown);
64                         }
65                 }
66
67                 [Test]
68                 public void TestCapacity ()
69                 {
70 #if NET_2_0
71                 int default_capacity = 4;
72                 int unspecified_capacity = 0;
73 #else
74                         int default_capacity = 16;
75                         int unspecified_capacity = 16;
76 #endif
77                         for (int i = 1; i < 100; i++) {
78                                 ArrayList al1 = new ArrayList (i);
79                                 AssertEquals ("Bad capacity of " + i,
80                                                  i, al1.Capacity);
81                         }
82                         {
83                                 ArrayList al1 = new ArrayList (0);
84                                 // LAMESPEC: 
85                                 // AssertEquals("Bad capacity when set to 0",
86                                 //           16, al1.Capacity);
87                                 al1.Add ("?");
88                                 AssertEquals ("Bad capacity when set to 0",
89                                                  default_capacity, al1.Capacity);
90                         }
91                         {
92                                 ArrayList al1 = new ArrayList ();
93                                 AssertEquals ("Bad default capacity",
94                                                  unspecified_capacity, al1.Capacity);
95                         }
96                 }
97
98                 [Test]
99                 public void TestCount ()
100                 {
101                         {
102                                 ArrayList al1 = new ArrayList ();
103                                 AssertEquals ("Bad initial count",
104                                                  0, al1.Count);
105                                 for (int i = 1; i <= 100; i++) {
106                                         al1.Add (i);
107                                         AssertEquals ("Bad count " + i,
108                                                          i, al1.Count);
109                                 }
110                         }
111                         for (int i = 0; i < 100; i++) {
112                                 char [] coll = new Char [i];
113                                 ArrayList al1 = new ArrayList (coll);
114                                 AssertEquals ("Bad count for " + i,
115                                                  i, al1.Count);
116                         }
117                 }
118
119                 [Test]
120                 public void TestIsFixed ()
121                 {
122                         ArrayList al1 = new ArrayList ();
123                         Assert ("should not be fixed by default", !al1.IsFixedSize);
124                         ArrayList al2 = ArrayList.FixedSize (al1);
125                         Assert ("fixed-size wrapper not working", al2.IsFixedSize);
126                 }
127
128                 [Test]
129                 public void TestIsReadOnly ()
130                 {
131                         ArrayList al1 = new ArrayList ();
132                         Assert ("should not be ReadOnly by default", !al1.IsReadOnly);
133                         ArrayList al2 = ArrayList.ReadOnly (al1);
134                         Assert ("read-only wrapper not working", al2.IsReadOnly);
135                 }
136
137                 [Test]
138                 public void TestIsSynchronized ()
139                 {
140                         ArrayList al1 = new ArrayList ();
141                         Assert ("should not be synchronized by default",
142                                    !al1.IsSynchronized);
143                         ArrayList al2 = ArrayList.Synchronized (al1);
144                         Assert ("synchronized wrapper not working", al2.IsSynchronized);
145                 }
146
147                 [Test]
148                 public void TestItem ()
149                 {
150                         ArrayList al1 = new ArrayList ();
151                         {
152                                 bool errorThrown = false;
153                                 try {
154                                         object o = al1 [-1];
155                                 } catch (ArgumentOutOfRangeException) {
156                                         errorThrown = true;
157                                 }
158                                 Assert ("negative item error not thrown",
159                                            errorThrown);
160                         }
161                         {
162                                 bool errorThrown = false;
163                                 try {
164                                         object o = al1 [1];
165                                 } catch (ArgumentOutOfRangeException) {
166                                         errorThrown = true;
167                                 }
168                                 Assert ("past-end item error not thrown",
169                                            errorThrown);
170                         }
171                         for (int i = 0; i <= 100; i++) {
172                                 al1.Add (i);
173                         }
174                         for (int i = 0; i <= 100; i++) {
175                                 AssertEquals ("item not fetched for " + i,
176                                                  i, al1 [i]);
177                         }
178                 }
179
180                 [Test]
181                 public void TestAdapter ()
182                 {
183                         {
184                                 bool errorThrown = false;
185                                 try {
186                                         ArrayList al1 = ArrayList.Adapter (null);
187                                 } catch (ArgumentNullException) {
188                                         errorThrown = true;
189                                 } catch (Exception e) {
190                                         Fail ("Incorrect exception thrown at 1: " + e.ToString ());
191                                 }
192                                 Assert ("null adapter error not thrown",
193                                            errorThrown);
194                         }
195                         {
196                                 char [] list = { 'a', 'b', 'c', 'd' };
197                                 ArrayList al1 = ArrayList.Adapter (list);
198                                 AssertNotNull ("Couldn't get an adapter", al1);
199                                 for (int i = 0; i < list.Length; i++) {
200                                         AssertEquals ("adapter not adapting", list [i], al1 [i]);
201                                 }
202                                 list [0] = 'z';
203                                 for (int i = 0; i < list.Length; i++) {
204                                         AssertEquals ("adapter not adapting", list [i], al1 [i]);
205                                 }
206                         }
207                         // Test Binary Search
208                         {
209                                 bool errorThrown = false;
210                                 try {
211
212                                         String [] s1 = { "This", "is", "a", "test" };
213                                         ArrayList al1 = ArrayList.Adapter (s1);
214                                         al1.BinarySearch (42);
215                                 } catch (InvalidOperationException) {
216                                         // this is what .NET throws
217                                         errorThrown = true;
218                                 } catch (ArgumentException) {
219                                         // this is what the docs say it should throw
220                                         errorThrown = true;
221                                 } catch (Exception e) {
222                                         Fail ("Incorrect exception thrown at 1: " + e.ToString ());
223                                 }
224                                 Assert ("search-for-wrong-type error not thrown",
225                                            errorThrown);
226                         }
227
228                         {
229                                 char [] arr = { 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd' };
230                                 ArrayList al1 = ArrayList.Adapter (arr);
231                                 Assert ("couldn't find elem #1",
232                                            al1.BinarySearch ('c') >= 3);
233                                 Assert ("couldn't find elem #2",
234                                            al1.BinarySearch ('c') < 6);
235                         }
236                         {
237                                 char [] arr = { 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e' };
238                                 ArrayList al1 = ArrayList.Adapter (arr);
239                                 AssertEquals ("couldn't find next-higher elem",
240                                                  -4, al1.BinarySearch ('c'));
241                         }
242                         {
243                                 char [] arr = { 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd' };
244                                 ArrayList al1 = ArrayList.Adapter (arr);
245                                 AssertEquals ("couldn't find end",
246                                                  -9, al1.BinarySearch ('e'));
247                         }
248                         // Sort
249                         {
250                                 char [] starter = { 'd', 'b', 'f', 'e', 'a', 'c' };
251                                 ArrayList al1 = ArrayList.Adapter (starter);
252                                 al1.Sort ();
253                                 AssertEquals ("Should be sorted", 'a', al1 [0]);
254                                 AssertEquals ("Should be sorted", 'b', al1 [1]);
255                                 AssertEquals ("Should be sorted", 'c', al1 [2]);
256                                 AssertEquals ("Should be sorted", 'd', al1 [3]);
257                                 AssertEquals ("Should be sorted", 'e', al1 [4]);
258                                 AssertEquals ("Should be sorted", 'f', al1 [5]);
259                         }
260
261                         // TODO - test other adapter types?
262                 }
263
264                 [Test]
265                 public void TestAdd ()
266                 {
267                         {
268                                 bool errorThrown = false;
269                                 try {
270                                         ArrayList al1 =
271                                                 ArrayList.FixedSize (new ArrayList ());
272                                         al1.Add ("Hi!");
273                                 } catch (NotSupportedException) {
274                                         errorThrown = true;
275                                 } catch (Exception e) {
276                                         Fail ("Incorrect exception thrown at 1: " + e.ToString ());
277                                 }
278                                 Assert ("add to fixed size error not thrown",
279                                            errorThrown);
280                         }
281                         {
282                                 bool errorThrown = false;
283                                 try {
284                                         ArrayList al1 =
285                                                 ArrayList.ReadOnly (new ArrayList ());
286                                         al1.Add ("Hi!");
287                                 } catch (NotSupportedException) {
288                                         errorThrown = true;
289                                 } catch (Exception e) {
290                                         Fail ("Incorrect exception thrown at 2: " + e.ToString ());
291                                 }
292                                 Assert ("add to read only error not thrown",
293                                            errorThrown);
294                         }
295                         {
296                                 ArrayList al1 = new ArrayList ();
297                                 for (int i = 1; i <= 100; i++) {
298                                         al1.Add (i);
299                                         AssertEquals ("add failed " + i,
300                                                          i, al1.Count);
301                                         AssertEquals ("add failed " + i,
302                                                          i, al1 [i - 1]);
303
304                                 }
305                         }
306                         {
307                                 string [] strArray = new string [] { };
308                                 ArrayList al1 = new ArrayList (strArray);
309                                 al1.Add ("Hi!");
310                                 al1.Add ("Hi!");
311                                 AssertEquals ("add failed", 2, al1.Count);
312                         }
313                 }
314
315                 [Test]
316                 public void TestAddRange ()
317                 {
318                         {
319                                 bool errorThrown = false;
320                                 try {
321                                         ArrayList al1 =
322                                                 ArrayList.FixedSize (new ArrayList ());
323                                         String [] s1 = { "Hi!" };
324                                         al1.AddRange (s1);
325                                 } catch (NotSupportedException) {
326                                         errorThrown = true;
327                                 } catch (Exception e) {
328                                         Fail ("Incorrect exception thrown at 1: " + e.ToString ());
329                                 }
330                                 Assert ("add to fixed size error not thrown",
331                                            errorThrown);
332                         }
333                         {
334                                 bool errorThrown = false;
335                                 try {
336                                         ArrayList al1 =
337                                                 ArrayList.ReadOnly (new ArrayList ());
338                                         String [] s1 = { "Hi!" };
339                                         al1.AddRange (s1);
340                                 } catch (NotSupportedException) {
341                                         errorThrown = true;
342                                 } catch (Exception e) {
343                                         Fail ("Incorrect exception thrown at 2: " + e.ToString ());
344                                 }
345                                 Assert ("add to read only error not thrown",
346                                            errorThrown);
347                         }
348                         {
349                                 bool errorThrown = false;
350                                 try {
351                                         ArrayList al1 = new ArrayList ();
352                                         al1.AddRange (null);
353                                 } catch (ArgumentNullException) {
354                                         errorThrown = true;
355                                 } catch (Exception e) {
356                                         Fail ("Incorrect exception thrown at 3: " + e.ToString ());
357                                 }
358                                 Assert ("add to read only error not thrown",
359                                            errorThrown);
360                         }
361
362                         {
363                                 ArrayList a1 = new ArrayList ();
364                                 AssertEquals ("ArrayList should start empty",
365                                                  0, a1.Count);
366                                 char [] coll = { 'a', 'b', 'c' };
367                                 a1.AddRange (coll);
368                                 AssertEquals ("ArrayList has wrong elements",
369                                                  3, a1.Count);
370                                 a1.AddRange (coll);
371                                 AssertEquals ("ArrayList has wrong elements",
372                                                  6, a1.Count);
373                         }
374
375                         {
376                                 ArrayList list = new ArrayList ();
377
378                                 for (int i = 0; i < 100; i++) {
379                                         list.Add (1);
380                                 }
381
382                                 AssertEquals ("BinarySearch off-by-one bug",
383                                                 49, list.BinarySearch (1));
384                         }
385                 }
386
387                 [Test]
388                 public void TestBinarySearch ()
389                 {
390                         {
391                                 bool errorThrown = false;
392                                 try {
393                                         ArrayList al1 = new ArrayList ();
394                                         String [] s1 = { "This", "is", "a", "test" };
395                                         al1.AddRange (s1);
396                                         al1.BinarySearch (42);
397                                 } catch (InvalidOperationException) {
398                                         // this is what .NET throws
399                                         errorThrown = true;
400                                 } catch (ArgumentException) {
401                                         // this is what the docs say it should throw
402                                         errorThrown = true;
403                                 } catch (Exception e) {
404                                         Fail ("Incorrect exception thrown at 1: " + e.ToString ());
405                                 }
406                                 Assert ("search-for-wrong-type error not thrown",
407                                            errorThrown);
408                         }
409
410                         {
411                                 char [] arr = { 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd' };
412                                 ArrayList al1 = new ArrayList (arr);
413                                 Assert ("couldn't find elem #1",
414                                            al1.BinarySearch ('c') >= 3);
415                                 Assert ("couldn't find elem #2",
416                                            al1.BinarySearch ('c') < 6);
417                         }
418                         {
419                                 char [] arr = { 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e' };
420                                 ArrayList al1 = new ArrayList (arr);
421                                 AssertEquals ("couldn't find next-higher elem",
422                                                  -4, al1.BinarySearch ('c'));
423                         }
424                         {
425                                 char [] arr = { 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd' };
426                                 ArrayList al1 = new ArrayList (arr);
427                                 AssertEquals ("couldn't find end",
428                                                  -9, al1.BinarySearch ('e'));
429                         }
430
431                 }
432
433                 [Test]
434                 [ExpectedException (typeof (ArgumentException))]
435                 public void BinarySearch_IndexOverflow ()
436                 {
437                         ArrayList al = new ArrayList ();
438                         al.Add (this);
439                         al.BinarySearch (Int32.MaxValue, 1, this, null);
440                 }
441
442                 [Test]
443                 [ExpectedException (typeof (ArgumentException))]
444                 public void BinarySearch_CountOverflow ()
445                 {
446                         ArrayList al = new ArrayList ();
447                         al.Add (this);
448                         al.BinarySearch (1, Int32.MaxValue, this, null);
449                 }
450
451                 [Test]
452                 public void BinarySearch_Null ()
453                 {
454                         ArrayList al = new ArrayList ();
455                         al.Add (this);
456                         AssertEquals ("null", -1, al.BinarySearch (null));
457                 }
458
459                 // TODO - BinarySearch with IComparer
460
461                 [Test]
462                 public void TestClear ()
463                 {
464                         {
465                                 bool errorThrown = false;
466                                 try {
467                                         ArrayList al1 =
468                                                 ArrayList.FixedSize (new ArrayList ());
469                                         al1.Clear ();
470                                 } catch (NotSupportedException) {
471                                         errorThrown = true;
472                                 }
473                                 Assert ("add to fixed size error not thrown",
474                                            errorThrown);
475                         }
476                         {
477                                 bool errorThrown = false;
478                                 try {
479                                         ArrayList al1 =
480                                                 ArrayList.ReadOnly (new ArrayList ());
481                                         al1.Clear ();
482                                 } catch (NotSupportedException) {
483                                         errorThrown = true;
484                                 }
485                                 Assert ("add to read only error not thrown",
486                                            errorThrown);
487                         }
488                         {
489                                 ArrayList al1 = new ArrayList ();
490                                 al1.Add ('c');
491                                 AssertEquals ("should have one element",
492                                                  1, al1.Count);
493                                 al1.Clear ();
494                                 AssertEquals ("should be empty",
495                                                  0, al1.Count);
496                         }
497                         {
498                                 int [] i1 = { 1, 2, 3, 4 };
499                                 ArrayList al1 = new ArrayList (i1);
500                                 AssertEquals ("should have elements",
501                                                  i1.Length, al1.Count);
502                                 int capacity = al1.Capacity;
503                                 al1.Clear ();
504                                 AssertEquals ("should be empty again",
505                                                  0, al1.Count);
506                                 AssertEquals ("capacity shouldn't have changed",
507                                                  capacity, al1.Capacity);
508                         }
509                 }
510
511                 [Test]
512                 public void TestClone ()
513                 {
514                         {
515                                 char [] c1 = { 'a', 'b', 'c' };
516                                 ArrayList al1 = new ArrayList (c1);
517                                 ArrayList al2 = (ArrayList) al1.Clone ();
518                                 AssertEquals ("ArrayList match", al1 [0], al2 [0]);
519                                 AssertEquals ("ArrayList match", al1 [1], al2 [1]);
520                                 AssertEquals ("ArrayList match", al1 [2], al2 [2]);
521                         }
522                         {
523                                 char [] d10 = { 'a', 'b' };
524                                 char [] d11 = { 'a', 'c' };
525                                 char [] d12 = { 'b', 'c' };
526                                 char [] [] d1 = { d10, d11, d12 };
527                                 ArrayList al1 = new ArrayList (d1);
528                                 ArrayList al2 = (ArrayList) al1.Clone ();
529                                 AssertEquals ("Array match", al1 [0], al2 [0]);
530                                 AssertEquals ("Array match", al1 [1], al2 [1]);
531                                 AssertEquals ("Array match", al1 [2], al2 [2]);
532
533                                 ((char []) al1 [0]) [0] = 'z';
534                                 AssertEquals ("shallow copy", al1 [0], al2 [0]);
535                         }
536                 }
537
538                 [Test]
539                 public void TestContains ()
540                 {
541                         char [] c1 = { 'a', 'b', 'c' };
542                         ArrayList al1 = new ArrayList (c1);
543                         Assert ("never find a null", !al1.Contains (null));
544                         Assert ("can't find value", al1.Contains ('b'));
545                         Assert ("shouldn't find value", !al1.Contains ('?'));
546                 }
547
548                 [Test]
549                 public void TestCopyTo ()
550                 {
551                         {
552                                 bool errorThrown = false;
553                                 try {
554                                         Char [] c1 = new Char [2];
555                                         ArrayList al1 = new ArrayList (c1);
556                                         al1.CopyTo (null, 2);
557                                 } catch (ArgumentNullException) {
558                                         errorThrown = true;
559                                 } catch (Exception e) {
560                                         Fail ("Incorrect exception thrown at 1: " + e.ToString ());
561                                 }
562                                 Assert ("error not thrown 1", errorThrown);
563                         }
564                         {
565                                 bool errorThrown = false;
566                                 try {
567                                         Char [] c1 = new Char [2];
568                                         ArrayList al1 = new ArrayList (c1);
569                                         Char [,] c2 = new Char [2, 2];
570                                         al1.CopyTo (c2, 2);
571                                 } catch (ArgumentException) {
572                                         errorThrown = true;
573                                 } catch (Exception e) {
574                                         Fail ("Incorrect exception thrown at 2: " + e.ToString ());
575                                 }
576                                 Assert ("error not thrown 2", errorThrown);
577                         }
578                         {
579                                 bool errorThrown = false;
580                                 try {
581                                         // This appears to be a bug in the ArrayList Constructor.
582                                         // It throws a RankException if a multidimensional Array
583                                         // is passed. The docs imply that an IEnumerator is used
584                                         // to retrieve the items from the collection, so this should
585                                         // work.  In anycase this test is for CopyTo, so use what
586                                         // works on both platforms.
587                                         //Char[,] c1 = new Char[2,2];
588                                         Char [] c1 = new Char [2];
589                                         ArrayList al1 = new ArrayList (c1);
590                                         Char [] c2 = new Char [2];
591                                         al1.CopyTo (c2, 2);
592                                 } catch (ArgumentException) {
593                                         errorThrown = true;
594                                 } catch (Exception e) {
595                                         Fail ("Incorrect exception thrown at 3: " + e.ToString ());
596                                 }
597                                 Assert ("error not thrown 3", errorThrown);
598                         }
599                         {
600                                 bool errorThrown = false;
601                                 try {
602                                         Char [] c1 = new Char [2];
603                                         ArrayList al1 = new ArrayList (c1);
604                                         Char [] c2 = new Char [2];
605                                         al1.CopyTo (c2, -1);
606                                 } catch (ArgumentOutOfRangeException) {
607                                         errorThrown = true;
608                                 } catch (Exception e) {
609                                         Fail ("Incorrect exception thrown at 4: " + e.ToString ());
610                                 }
611                                 Assert ("error not thrown 4", errorThrown);
612                         }
613                         {
614                                 bool errorThrown = false;
615                                 try {
616                                         Char [] c1 = new Char [2];
617                                         ArrayList al1 = new ArrayList (c1);
618                                         Char [] c2 = new Char [2];
619                                         al1.CopyTo (c2, 3);
620                                 } catch (ArgumentException) {
621                                         errorThrown = true;
622                                 } catch (Exception e) {
623                                         Fail ("Incorrect exception thrown at 5: " + e.ToString ());
624                                 }
625                                 Assert ("error not thrown 5", errorThrown);
626                         }
627                         {
628                                 bool errorThrown = false;
629                                 try {
630                                         Char [] c1 = new Char [2];
631                                         ArrayList al1 = new ArrayList (c1);
632                                         Char [] c2 = new Char [2];
633                                         al1.CopyTo (c2, 1);
634                                 } catch (ArgumentException) {
635                                         errorThrown = true;
636                                 } catch (Exception e) {
637                                         Fail ("Incorrect exception thrown at 6: " + e.ToString ());
638                                 }
639                                 Assert ("error not thrown 6", errorThrown);
640                         }
641                         {
642                                 bool errorThrown = false;
643                                 try {
644                                         String [] c1 = { "String", "array" };
645                                         ArrayList al1 = new ArrayList (c1);
646                                         Char [] c2 = new Char [2];
647                                         al1.CopyTo (c2, 0);
648                                 } catch (InvalidCastException) {
649                                         errorThrown = true;
650                                 } catch (Exception e) {
651                                         Fail ("Incorrect exception thrown at 7: " + e.ToString ());
652                                 }
653                                 Assert ("error not thrown 7", errorThrown);
654                         }
655
656                         Char [] orig = { 'a', 'b', 'c', 'd' };
657                         ArrayList al = new ArrayList (orig);
658                         Char [] copy = new Char [10];
659                         Array.Clear (copy, 0, copy.Length);
660                         al.CopyTo (copy, 3);
661                         AssertEquals ("Wrong CopyTo 0", (char) 0, copy [0]);
662                         AssertEquals ("Wrong CopyTo 1", (char) 0, copy [1]);
663                         AssertEquals ("Wrong CopyTo 2", (char) 0, copy [2]);
664                         AssertEquals ("Wrong CopyTo 3", orig [0], copy [3]);
665                         AssertEquals ("Wrong CopyTo 4", orig [1], copy [4]);
666                         AssertEquals ("Wrong CopyTo 5", orig [2], copy [5]);
667                         AssertEquals ("Wrong CopyTo 6", orig [3], copy [6]);
668                         AssertEquals ("Wrong CopyTo 7", (char) 0, copy [7]);
669                         AssertEquals ("Wrong CopyTo 8", (char) 0, copy [8]);
670                         AssertEquals ("Wrong CopyTo 9", (char) 0, copy [9]);
671                 }
672
673                 [Test]
674                 [ExpectedException (typeof (ArgumentException))]
675                 public void CopyTo_IndexOverflow ()
676                 {
677                         ArrayList al = new ArrayList ();
678                         al.Add (this);
679                         al.CopyTo (Int32.MaxValue, new byte [2], 0, 0);
680                 }
681
682                 [Test]
683                 [ExpectedException (typeof (ArgumentException))]
684                 public void CopyTo_ArrayIndexOverflow ()
685                 {
686                         ArrayList al = new ArrayList ();
687                         al.Add (this);
688                         al.CopyTo (0, new byte [2], Int32.MaxValue, 0);
689                 }
690
691                 [Test]
692                 [ExpectedException (typeof (ArgumentException))]
693                 public void CopyTo_CountOverflow ()
694                 {
695                         ArrayList al = new ArrayList ();
696                         al.Add (this);
697                         al.CopyTo (0, new byte [2], 0, Int32.MaxValue);
698                 }
699
700                 [Test]
701                 public void TestFixedSize ()
702                 {
703                         {
704                                 bool errorThrown = false;
705                                 try {
706                                         ArrayList al1 = ArrayList.FixedSize (null);
707                                 } catch (ArgumentNullException) {
708                                         errorThrown = true;
709                                 }
710                                 Assert ("null arg error not thrown", errorThrown);
711                         }
712                         {
713                                 ArrayList al1 = new ArrayList ();
714                                 AssertEquals ("arrays start un-fixed.",
715                                                  false, al1.IsFixedSize);
716                                 ArrayList al2 = ArrayList.FixedSize (al1);
717                                 AssertEquals ("should be fixed.",
718                                                  true, al2.IsFixedSize);
719                         }
720                 }
721
722                 [Test]
723                 public void TestEnumerator ()
724                 {
725                         String [] s1 = { "this", "is", "a", "test" };
726                         ArrayList al1 = new ArrayList (s1);
727                         IEnumerator en = al1.GetEnumerator ();
728                         en.MoveNext ();
729                         al1.Add ("something");
730                         try {
731                                 en.MoveNext ();
732                                 Fail ("Add() didn't invalidate the enumerator");
733                         } catch (InvalidOperationException) {
734                                 // do nothing...this is what we expect
735                         }
736
737                         en = al1.GetEnumerator ();
738                         en.MoveNext ();
739                         al1.AddRange (al1);
740                         try {
741                                 en.MoveNext ();
742                                 Fail ("AddRange() didn't invalidate the enumerator");
743                         } catch (InvalidOperationException) {
744                                 // do nothing...this is what we expect
745                         }
746
747                         en = al1.GetEnumerator ();
748                         en.MoveNext ();
749                         al1.Clear ();
750                         try {
751                                 en.MoveNext ();
752                                 Fail ("Clear() didn't invalidate the enumerator");
753                         } catch (InvalidOperationException) {
754                                 // do nothing...this is what we expect
755                         }
756
757                         al1 = new ArrayList (s1);
758                         en = al1.GetEnumerator ();
759                         en.MoveNext ();
760                         al1.Insert (0, "new first");
761                         try {
762                                 en.MoveNext ();
763                                 Fail ("Insert() didn't invalidate the enumerator");
764                         } catch (InvalidOperationException) {
765                                 // do nothing...this is what we expect
766                         }
767
768                         en = al1.GetEnumerator ();
769                         en.MoveNext ();
770                         al1.InsertRange (0, al1);
771                         try {
772                                 en.MoveNext ();
773                                 Fail ("InsertRange() didn't invalidate the enumerator");
774                         } catch (InvalidOperationException) {
775                                 // do nothing...this is what we expect
776                         }
777
778                         en = al1.GetEnumerator ();
779                         en.MoveNext ();
780                         al1.Remove ("this");
781                         try {
782                                 en.MoveNext ();
783                                 Fail ("Remove() didn't invalidate the enumerator");
784                         } catch (InvalidOperationException) {
785                                 // do nothing...this is what we expect
786                         }
787
788                         en = al1.GetEnumerator ();
789                         en.MoveNext ();
790                         al1.RemoveAt (2);
791                         try {
792                                 en.MoveNext ();
793                                 Fail ("RemoveAt() didn't invalidate the enumerator");
794                         } catch (InvalidOperationException) {
795                                 // do nothing...this is what we expect
796                         }
797
798                         en = al1.GetEnumerator ();
799                         en.MoveNext ();
800                         al1.RemoveRange (1, 1);
801                         try {
802                                 en.MoveNext ();
803                                 Fail ("RemoveRange() didn't invalidate the enumerator");
804                         } catch (InvalidOperationException) {
805                                 // do nothing...this is what we expect
806                         }
807
808                         en = al1.GetEnumerator ();
809                         en.MoveNext ();
810                         al1.Reverse ();
811                         try {
812                                 en.MoveNext ();
813                                 Fail ("Reverse() didn't invalidate the enumerator");
814                         } catch (InvalidOperationException) {
815                                 // do nothing...this is what we expect
816                         }
817
818                         en = al1.GetEnumerator ();
819                         en.MoveNext ();
820                         al1.Sort ();
821                         try {
822                                 en.MoveNext ();
823                                 Fail ("Sort() didn't invalidate the enumerator");
824                         } catch (InvalidOperationException) {
825                                 // do nothing...this is what we expect
826                         }
827                 }
828
829                 [Test]
830                 public void TestGetEnumerator ()
831                 {
832                         {
833                                 bool errorThrown = false;
834                                 try {
835                                         ArrayList a = new ArrayList ();
836                                         IEnumerator en = a.GetEnumerator (-1, 1);
837                                 } catch (ArgumentOutOfRangeException) {
838                                         errorThrown = true;
839                                 }
840                                 Assert ("negative index error not thrown",
841                                            errorThrown);
842                         }
843                         {
844                                 bool errorThrown = false;
845                                 try {
846                                         ArrayList a = new ArrayList ();
847                                         IEnumerator en = a.GetEnumerator (1, -1);
848                                 } catch (ArgumentOutOfRangeException) {
849                                         errorThrown = true;
850                                 }
851                                 Assert ("negative index error not thrown",
852                                            errorThrown);
853                         }
854                         {
855                                 bool errorThrown = false;
856                                 try {
857                                         ArrayList a = new ArrayList ();
858                                         IEnumerator en = a.GetEnumerator (1, 1);
859                                 } catch (ArgumentException) {
860                                         errorThrown = true;
861                                 }
862                                 Assert ("out-of-range index error not thrown",
863                                            errorThrown);
864                         }
865                         {
866                                 String [] s1 = { "this", "is", "a", "test" };
867                                 ArrayList al1 = new ArrayList (s1);
868                                 IEnumerator en = al1.GetEnumerator ();
869                                 AssertNotNull ("No enumerator", en);
870
871                                 for (int i = 0; i < s1.Length; i++) {
872                                         en.MoveNext ();
873                                         AssertEquals ("Not enumerating",
874                                                          al1 [i], en.Current);
875                                 }
876                         }
877                         {
878                                 String [] s1 = { "this", "is", "a", "test" };
879                                 ArrayList al1 = new ArrayList (s1);
880                                 IEnumerator en = al1.GetEnumerator (1, 2);
881                                 AssertNotNull ("No enumerator", en);
882
883                                 for (int i = 0; i < 2; i++) {
884                                         en.MoveNext ();
885                                         AssertEquals ("Not enumerating",
886                                                          al1 [i + 1], en.Current);
887                                 }
888                         }
889                 }
890
891                 [Test]
892                 [ExpectedException (typeof (ArgumentException))]
893                 public void GetEnumerator_IndexOverflow ()
894                 {
895                         ArrayList al = new ArrayList ();
896                         al.Add (this);
897                         al.GetEnumerator (Int32.MaxValue, 0);
898                 }
899
900                 [Test]
901                 [ExpectedException (typeof (ArgumentException))]
902                 public void GetEnumerator_CountOverflow ()
903                 {
904                         ArrayList al = new ArrayList ();
905                         al.Add (this);
906                         al.GetEnumerator (0, Int32.MaxValue);
907                 }
908
909                 [Test]
910                 public void TestGetRange ()
911                 {
912                         {
913                                 bool errorThrown = false;
914                                 try {
915                                         ArrayList a = new ArrayList ();
916                                         ArrayList b = a.GetRange (-1, 1);
917                                 } catch (ArgumentOutOfRangeException) {
918                                         errorThrown = true;
919                                 }
920                                 Assert ("negative index error not thrown",
921                                            errorThrown);
922                         }
923                         {
924                                 bool errorThrown = false;
925                                 try {
926                                         ArrayList a = new ArrayList ();
927                                         ArrayList b = a.GetRange (1, -1);
928                                 } catch (ArgumentOutOfRangeException) {
929                                         errorThrown = true;
930                                 }
931                                 Assert ("negative index error not thrown",
932                                            errorThrown);
933                         }
934                         {
935                                 bool errorThrown = false;
936                                 try {
937                                         ArrayList a = new ArrayList ();
938                                         ArrayList b = a.GetRange (1, 1);
939                                 } catch (ArgumentException) {
940                                         errorThrown = true;
941                                 }
942                                 Assert ("out-of-range index error not thrown",
943                                            errorThrown);
944                         }
945                         {
946                                 char [] chars = { 'a', 'b', 'c', 'd', 'e', 'f' };
947                                 ArrayList a = new ArrayList (chars);
948                                 ArrayList b = a.GetRange (1, 3);
949                                 AssertEquals ("GetRange returned wrong size ArrayList", 3, b.Count);
950                                 for (int i = 0; i < b.Count; i++) {
951                                         AssertEquals ("range didn't work",
952                                                          chars [i + 1], b [i]);
953                                 }
954
955                                 a [2] = '?'; // should screw up ArrayList b.
956                                 bool errorThrown = false;
957                                 try {
958                                         int i = b.Count;
959                                 } catch (InvalidOperationException) {
960                                         errorThrown = true;
961                                 }
962                                 AssertEquals ("Munging 'a' should mess up 'b'",
963                                                  true, errorThrown);
964                         }
965                         {
966                                 char [] chars = { 'a', 'b', 'c', 'd', 'e', 'f' };
967                                 ArrayList a = new ArrayList (chars);
968                                 ArrayList b = a.GetRange (3, 3);
969                                 object [] obj_chars = b.ToArray ();
970                                 for (int i = 0; i < 3; i++) {
971                                         char c = (char) obj_chars [i];
972                                         AssertEquals ("range.ToArray didn't work",
973                                                          chars [i + 3], c);
974                                 }
975                                 char [] new_chars = (char []) b.ToArray (typeof (char));
976                                 for (int i = 0; i < 3; i++) {
977                                         AssertEquals ("range.ToArray with type didn't work",
978                                                          chars [i + 3], new_chars [i]);
979                                 }
980                         }
981                 }
982
983                 [Test]
984                 [ExpectedException (typeof (ArgumentException))]
985                 public void GetRange_IndexOverflow ()
986                 {
987                         ArrayList al = new ArrayList ();
988                         al.Add (this);
989                         al.GetRange (Int32.MaxValue, 0);
990                 }
991
992                 [Test]
993                 [ExpectedException (typeof (ArgumentException))]
994                 public void GetRange_CountOverflow ()
995                 {
996                         ArrayList al = new ArrayList ();
997                         al.Add (this);
998                         al.GetRange (0, Int32.MaxValue);
999                 }
1000
1001                 [Test]
1002                 public void TestIndexOf ()
1003                 {
1004                         {
1005                                 bool errorThrown = false;
1006                                 try {
1007                                         ArrayList a = new ArrayList (1);
1008                                         int i = a.IndexOf ('a', -1);
1009                                 } catch (ArgumentOutOfRangeException) {
1010                                         errorThrown = true;
1011                                 }
1012                                 Assert ("negative indexof error not thrown",
1013                                            errorThrown);
1014                         }
1015                         {
1016                                 bool errorThrown = false;
1017                                 try {
1018                                         ArrayList a = new ArrayList (1);
1019                                         int i = a.IndexOf ('a', 2);
1020                                 } catch (ArgumentOutOfRangeException) {
1021                                         errorThrown = true;
1022                                 }
1023                                 Assert ("past-end indexof error not thrown",
1024                                            errorThrown);
1025                         }
1026                         {
1027                                 bool errorThrown = false;
1028                                 try {
1029                                         ArrayList a = new ArrayList (1);
1030                                         int i = a.IndexOf ('a', 0, -1);
1031                                 } catch (ArgumentOutOfRangeException) {
1032                                         errorThrown = true;
1033                                 }
1034                                 Assert ("negative indexof error not thrown",
1035                                            errorThrown);
1036                         }
1037                         {
1038                                 bool errorThrown = false;
1039                                 try {
1040                                         ArrayList a = new ArrayList (1);
1041                                         int i = a.IndexOf ('a', 0, 2);
1042                                 } catch (ArgumentOutOfRangeException) {
1043                                         errorThrown = true;
1044                                 }
1045                                 Assert ("past-end indexof error not thrown",
1046                                            errorThrown);
1047                         }
1048                         {
1049                                 bool errorThrown = false;
1050                                 try {
1051                                         ArrayList a = new ArrayList (2);
1052                                         int i = a.IndexOf ('a', 1, 2);
1053                                 } catch (ArgumentOutOfRangeException) {
1054                                         errorThrown = true;
1055                                 }
1056                                 Assert ("past-end indexof error not thrown",
1057                                            errorThrown);
1058                         }
1059                         {
1060                                 char [] c = { 'a', 'b', 'c', 'd', 'e' };
1061                                 ArrayList a = new ArrayList (c);
1062                                 AssertEquals ("never find null",
1063                                                  -1, a.IndexOf (null));
1064                                 AssertEquals ("never find null",
1065                                                  -1, a.IndexOf (null, 0));
1066                                 AssertEquals ("never find null",
1067                                                  -1, a.IndexOf (null, 0, 5));
1068                                 AssertEquals ("can't find elem",
1069                                                  2, a.IndexOf ('c'));
1070                                 AssertEquals ("can't find elem",
1071                                                  2, a.IndexOf ('c', 2));
1072                                 AssertEquals ("can't find elem",
1073                                                  2, a.IndexOf ('c', 2, 2));
1074                                 AssertEquals ("shouldn't find elem",
1075                                                  -1, a.IndexOf ('c', 3, 2));
1076                                 AssertEquals ("shouldn't find", -1, a.IndexOf ('?'));
1077                                 AssertEquals ("shouldn't find", -1, a.IndexOf (3));
1078                         }
1079                 }
1080
1081                 [Test]
1082                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1083                 public void IndexOf_StartIndexOverflow ()
1084                 {
1085                         ArrayList al = new ArrayList ();
1086                         al.Add (this);
1087                         al.IndexOf ('a', Int32.MaxValue, 1);
1088                 }
1089
1090                 [Test]
1091                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1092                 public void IndexOf_CountOverflow ()
1093                 {
1094                         ArrayList al = new ArrayList ();
1095                         al.Add (this);
1096                         al.IndexOf ('a', 1, Int32.MaxValue);
1097                 }
1098
1099                 [Test]
1100                 public void TestInsert ()
1101                 {
1102                         {
1103                                 bool errorThrown = false;
1104                                 try {
1105                                         ArrayList al1 =
1106                                                 ArrayList.FixedSize (new ArrayList ());
1107                                         al1.Insert (0, "Hi!");
1108                                 } catch (NotSupportedException) {
1109                                         errorThrown = true;
1110                                 }
1111                                 Assert ("insert to fixed size error not thrown",
1112                                            errorThrown);
1113                         }
1114                         {
1115                                 bool errorThrown = false;
1116                                 try {
1117                                         ArrayList al1 =
1118                                                 ArrayList.ReadOnly (new ArrayList ());
1119                                         al1.Insert (0, "Hi!");
1120                                 } catch (NotSupportedException) {
1121                                         errorThrown = true;
1122                                 }
1123                                 Assert ("insert to read only error not thrown",
1124                                            errorThrown);
1125                         }
1126                         {
1127                                 bool errorThrown = false;
1128                                 try {
1129                                         ArrayList al1 = new ArrayList (3);
1130                                         al1.Insert (-1, "Hi!");
1131                                 } catch (ArgumentOutOfRangeException) {
1132                                         errorThrown = true;
1133                                 }
1134                                 Assert ("insert to read only error not thrown",
1135                                            errorThrown);
1136                         }
1137                         {
1138                                 bool errorThrown = false;
1139                                 try {
1140                                         ArrayList al1 = new ArrayList (3);
1141                                         al1.Insert (4, "Hi!");
1142                                 } catch (ArgumentOutOfRangeException) {
1143                                         errorThrown = true;
1144                                 }
1145                                 Assert ("insert to read only error not thrown",
1146                                            errorThrown);
1147                         }
1148                         {
1149                                 ArrayList al1 = new ArrayList ();
1150                                 AssertEquals ("arraylist starts empty", 0, al1.Count);
1151                                 al1.Insert (0, 'a');
1152                                 al1.Insert (1, 'b');
1153                                 al1.Insert (0, 'c');
1154                                 AssertEquals ("arraylist needs stuff", 3, al1.Count);
1155                                 AssertEquals ("arraylist got stuff", 'c', al1 [0]);
1156                                 AssertEquals ("arraylist got stuff", 'a', al1 [1]);
1157                                 AssertEquals ("arraylist got stuff", 'b', al1 [2]);
1158                         }
1159                 }
1160
1161                 [Test]
1162                 public void TestInsertRange ()
1163                 {
1164                         {
1165                                 bool errorThrown = false;
1166                                 try {
1167                                         ArrayList al1 =
1168                                                 ArrayList.FixedSize (new ArrayList ());
1169                                         string [] s = { "Hi!" };
1170                                         al1.InsertRange (0, s);
1171                                 } catch (NotSupportedException) {
1172                                         errorThrown = true;
1173                                 }
1174                                 Assert ("insert to fixed size error not thrown",
1175                                            errorThrown);
1176                         }
1177                         {
1178                                 bool errorThrown = false;
1179                                 try {
1180                                         ArrayList al1 =
1181                                                 ArrayList.ReadOnly (new ArrayList ());
1182                                         string [] s = { "Hi!" };
1183                                         al1.InsertRange (0, s);
1184                                 } catch (NotSupportedException) {
1185                                         errorThrown = true;
1186                                 }
1187                                 Assert ("insert to read only error not thrown",
1188                                            errorThrown);
1189                         }
1190                         {
1191                                 bool errorThrown = false;
1192                                 try {
1193                                         ArrayList al1 = new ArrayList (3);
1194                                         string [] s = { "Hi!" };
1195                                         al1.InsertRange (-1, s);
1196                                 } catch (ArgumentOutOfRangeException) {
1197                                         errorThrown = true;
1198                                 }
1199                                 Assert ("negative index insert error not thrown",
1200                                            errorThrown);
1201                         }
1202                         {
1203                                 bool errorThrown = false;
1204                                 try {
1205                                         ArrayList al1 = new ArrayList (3);
1206                                         string [] s = { "Hi!" };
1207                                         al1.InsertRange (4, s);
1208                                 } catch (ArgumentOutOfRangeException) {
1209                                         errorThrown = true;
1210                                 }
1211                                 Assert ("out-of-range insert error not thrown",
1212                                            errorThrown);
1213                         }
1214                         {
1215                                 bool errorThrown = false;
1216                                 try {
1217                                         ArrayList al1 = new ArrayList (3);
1218                                         al1.InsertRange (0, null);
1219                                 } catch (ArgumentNullException) {
1220                                         errorThrown = true;
1221                                 }
1222                                 Assert ("null insert error not thrown",
1223                                            errorThrown);
1224                         }
1225                         {
1226                                 char [] c = { 'a', 'b', 'c' };
1227                                 ArrayList a = new ArrayList (c);
1228                                 a.InsertRange (1, c);
1229                                 AssertEquals ("bad insert 1", 'a', a [0]);
1230                                 AssertEquals ("bad insert 2", 'a', a [1]);
1231                                 AssertEquals ("bad insert 3", 'b', a [2]);
1232                                 AssertEquals ("bad insert 4", 'c', a [3]);
1233                                 AssertEquals ("bad insert 5", 'b', a [4]);
1234                                 AssertEquals ("bad insert 6", 'c', a [5]);
1235                         }
1236                 }
1237
1238                 [Test]
1239                 public void TestLastIndexOf ()
1240                 {
1241                         //{
1242                         //bool errorThrown = false;
1243                         //try {
1244                         //ArrayList a = new ArrayList(1);
1245                         //int i = a.LastIndexOf('a', -1);
1246                         //} catch (ArgumentOutOfRangeException) {
1247                         //errorThrown = true;
1248                         //}
1249                         //Assert("first negative lastindexof error not thrown", 
1250                         //errorThrown);
1251                         //}
1252                         {
1253                                 bool errorThrown = false;
1254                                 try {
1255                                         ArrayList a = new ArrayList (1);
1256                                         int i = a.LastIndexOf ('a', 2);
1257                                 } catch (ArgumentOutOfRangeException) {
1258                                         errorThrown = true;
1259                                 }
1260                                 Assert ("past-end lastindexof error not thrown",
1261                                            errorThrown);
1262                         }
1263                         //{
1264                         //bool errorThrown = false;
1265                         //try {
1266                         //ArrayList a = new ArrayList(1);
1267                         //int i = a.LastIndexOf('a', 0, -1);
1268                         //} catch (ArgumentOutOfRangeException) {
1269                         //errorThrown = true;
1270                         //}
1271                         //Assert("second negative lastindexof error not thrown", 
1272                         //errorThrown);
1273                         //}
1274                         //{
1275                         //bool errorThrown = false;
1276                         //try {
1277                         //ArrayList a = new ArrayList(1);
1278                         //int i = a.LastIndexOf('a', 0, 2);
1279                         //} catch (ArgumentOutOfRangeException) {
1280                         //errorThrown = true;
1281                         //}
1282                         //Assert("past-end lastindexof error not thrown", 
1283                         //errorThrown);
1284                         //}
1285                         //{
1286                         //bool errorThrown = false;
1287                         //try {
1288                         //ArrayList a = new ArrayList(2);
1289                         //int i = a.LastIndexOf('a', 0, 2);
1290                         //} catch (ArgumentOutOfRangeException) {
1291                         //errorThrown = true;
1292                         //}
1293                         //Assert("past-end lastindexof error not thrown", 
1294                         //errorThrown);
1295                         //}
1296                         int iTest = 0;
1297                         try {
1298                                 char [] c = { 'a', 'b', 'c', 'd', 'e' };
1299                                 ArrayList a = new ArrayList (c);
1300                                 AssertEquals ("never find null",
1301                                                  -1, a.LastIndexOf (null));
1302                                 iTest++;
1303                                 AssertEquals ("never find null",
1304                                                  -1, a.LastIndexOf (null, 4));
1305                                 iTest++;
1306                                 AssertEquals ("never find null",
1307                                                  -1, a.LastIndexOf (null, 4, 5));
1308                                 iTest++;
1309                                 AssertEquals ("can't find elem",
1310                                                  2, a.LastIndexOf ('c'));
1311                                 iTest++;
1312                                 AssertEquals ("can't find elem",
1313                                                  2, a.LastIndexOf ('c', 4));
1314                                 iTest++;
1315                                 AssertEquals ("can't find elem",
1316                                                  2, a.LastIndexOf ('c', 3, 2));
1317                                 iTest++;
1318                                 AssertEquals ("shouldn't find elem",
1319                                                  -1, a.LastIndexOf ('c', 4, 2));
1320                                 iTest++;
1321                                 AssertEquals ("shouldn't find", -1, a.LastIndexOf ('?'));
1322                                 iTest++;
1323                                 AssertEquals ("shouldn't find", -1, a.LastIndexOf (1));
1324                         } catch (Exception e) {
1325                                 Fail ("Unexpected exception caught when iTest=" + iTest + ". e=" + e);
1326                         }
1327                 }
1328
1329                 [Test]
1330                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1331                 public void LastIndexOf_StartIndexOverflow ()
1332                 {
1333                         ArrayList al = new ArrayList ();
1334                         al.Add (this);
1335                         al.LastIndexOf ('a', Int32.MaxValue, 1);
1336                 }
1337
1338                 [Test]
1339                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1340                 public void LastIndexOf_CountOverflow ()
1341                 {
1342                         ArrayList al = new ArrayList ();
1343                         al.Add (this);
1344                         al.LastIndexOf ('a', 1, Int32.MaxValue);
1345                 }
1346
1347                 [Test]
1348                 public void TestReadOnly ()
1349                 {
1350                         {
1351                                 bool errorThrown = false;
1352                                 try {
1353                                         ArrayList al1 = ArrayList.ReadOnly (null);
1354                                 } catch (ArgumentNullException) {
1355                                         errorThrown = true;
1356                                 }
1357                                 Assert ("null arg error not thrown", errorThrown);
1358                         }
1359                         {
1360                                 ArrayList al1 = new ArrayList ();
1361                                 AssertEquals ("arrays start writeable.",
1362                                                  false, al1.IsReadOnly);
1363                                 ArrayList al2 = ArrayList.ReadOnly (al1);
1364                                 AssertEquals ("should be readonly.",
1365                                                  true, al2.IsReadOnly);
1366                         }
1367                 }
1368
1369                 [Test]
1370                 public void TestRemove ()
1371                 {
1372                         {
1373                                 bool errorThrown = false;
1374                                 try {
1375                                         ArrayList al1 =
1376                                                 ArrayList.FixedSize (new ArrayList (3));
1377                                         al1.Remove (1);
1378                                 } catch (NotSupportedException) {
1379                                         errorThrown = true;
1380                                 }
1381                                 Assert ("remove fixed size error not thrown",
1382                                            errorThrown);
1383                         }
1384                         {
1385                                 bool errorThrown = false;
1386                                 try {
1387                                         ArrayList al1 =
1388                                                 ArrayList.ReadOnly (new ArrayList (3));
1389                                         al1.Remove (1);
1390                                 } catch (NotSupportedException) {
1391                                         errorThrown = true;
1392                                 }
1393                                 Assert ("remove read only error not thrown",
1394                                            errorThrown);
1395                         }
1396                         {
1397                                 char [] c = { 'a', 'b', 'c' };
1398                                 ArrayList a = new ArrayList (c);
1399                                 a.Remove (1);
1400                                 a.Remove ('?');
1401                                 AssertEquals ("should be unchanged", c.Length, a.Count);
1402                                 a.Remove ('a');
1403                                 AssertEquals ("should be changed", 2, a.Count);
1404                                 AssertEquals ("should have shifted", 'b', a [0]);
1405                                 AssertEquals ("should have shifted", 'c', a [1]);
1406                         }
1407                 }
1408
1409                 [Test]
1410                 public void TestRemoveAt ()
1411                 {
1412                         {
1413                                 bool errorThrown = false;
1414                                 try {
1415                                         ArrayList al1 =
1416                                                 ArrayList.FixedSize (new ArrayList (3));
1417                                         al1.RemoveAt (1);
1418                                 } catch (NotSupportedException) {
1419                                         errorThrown = true;
1420                                 }
1421                                 Assert ("remove from fixed size error not thrown",
1422                                            errorThrown);
1423                         }
1424                         {
1425                                 bool errorThrown = false;
1426                                 try {
1427                                         ArrayList al1 =
1428                                                 ArrayList.ReadOnly (new ArrayList (3));
1429                                         al1.RemoveAt (1);
1430                                 } catch (NotSupportedException) {
1431                                         errorThrown = true;
1432                                 }
1433                                 Assert ("remove from read only error not thrown",
1434                                            errorThrown);
1435                         }
1436                         {
1437                                 bool errorThrown = false;
1438                                 try {
1439                                         ArrayList al1 = new ArrayList (3);
1440                                         al1.RemoveAt (-1);
1441                                 } catch (ArgumentOutOfRangeException) {
1442                                         errorThrown = true;
1443                                 }
1444                                 Assert ("remove at negative index error not thrown",
1445                                            errorThrown);
1446                         }
1447                         {
1448                                 bool errorThrown = false;
1449                                 try {
1450                                         ArrayList al1 = new ArrayList (3);
1451                                         al1.RemoveAt (4);
1452                                 } catch (ArgumentOutOfRangeException) {
1453                                         errorThrown = true;
1454                                 }
1455                                 Assert ("remove at out-of-range index error not thrown",
1456                                            errorThrown);
1457                         }
1458                         {
1459                                 char [] c = { 'a', 'b', 'c' };
1460                                 ArrayList a = new ArrayList (c);
1461                                 a.RemoveAt (0);
1462                                 AssertEquals ("should be changed", 2, a.Count);
1463                                 AssertEquals ("should have shifted", 'b', a [0]);
1464                                 AssertEquals ("should have shifted", 'c', a [1]);
1465                         }
1466                 }
1467
1468                 [Test]
1469                 public void TestRemoveRange ()
1470                 {
1471                         {
1472                                 bool errorThrown = false;
1473                                 try {
1474                                         ArrayList al1 =
1475                                                 ArrayList.FixedSize (new ArrayList (3));
1476                                         al1.RemoveRange (0, 1);
1477                                 } catch (NotSupportedException) {
1478                                         errorThrown = true;
1479                                 }
1480                                 Assert ("removerange from fixed size error not thrown",
1481                                            errorThrown);
1482                         }
1483                         {
1484                                 bool errorThrown = false;
1485                                 try {
1486                                         ArrayList al1 =
1487                                                 ArrayList.ReadOnly (new ArrayList (3));
1488                                         al1.RemoveRange (0, 1);
1489                                 } catch (NotSupportedException) {
1490                                         errorThrown = true;
1491                                 }
1492                                 Assert ("removerange from read only error not thrown",
1493                                            errorThrown);
1494                         }
1495                         {
1496                                 bool errorThrown = false;
1497                                 try {
1498                                         ArrayList al1 = new ArrayList (3);
1499                                         al1.RemoveRange (-1, 1);
1500                                 } catch (ArgumentOutOfRangeException) {
1501                                         errorThrown = true;
1502                                 }
1503                                 Assert ("removerange at negative index error not thrown",
1504                                            errorThrown);
1505                         }
1506                         {
1507                                 bool errorThrown = false;
1508                                 try {
1509                                         ArrayList al1 = new ArrayList (3);
1510                                         al1.RemoveRange (0, -1);
1511                                 } catch (ArgumentOutOfRangeException) {
1512                                         errorThrown = true;
1513                                 }
1514                                 Assert ("removerange at negative index error not thrown",
1515                                            errorThrown);
1516                         }
1517                         {
1518                                 bool errorThrown = false;
1519                                 try {
1520                                         ArrayList al1 = new ArrayList (3);
1521                                         al1.RemoveRange (2, 3);
1522                                 } catch (ArgumentException) {
1523                                         errorThrown = true;
1524                                 }
1525                                 Assert ("removerange at bad range error not thrown",
1526                                            errorThrown);
1527                         }
1528                         {
1529                                 char [] c = { 'a', 'b', 'c' };
1530                                 ArrayList a = new ArrayList (c);
1531                                 a.RemoveRange (1, 2);
1532                                 AssertEquals ("should be changed", 1, a.Count);
1533                                 AssertEquals ("should have shifted", 'a', a [0]);
1534                         }
1535                 }
1536
1537                 [Test]
1538                 [ExpectedException (typeof (ArgumentException))]
1539                 public void RemoveRange_IndexOverflow ()
1540                 {
1541                         ArrayList al = new ArrayList ();
1542                         al.Add (this);
1543                         al.RemoveRange (Int32.MaxValue, 1);
1544                 }
1545
1546                 [Test]
1547                 [ExpectedException (typeof (ArgumentException))]
1548                 public void RemoveRange_CountOverflow ()
1549                 {
1550                         ArrayList al = new ArrayList ();
1551                         al.Add (this);
1552                         al.RemoveRange (1, Int32.MaxValue);
1553                 }
1554
1555                 [Test]
1556                 public void TestRepeat ()
1557                 {
1558                         {
1559                                 bool errorThrown = false;
1560                                 try {
1561                                         ArrayList al1 = ArrayList.Repeat ('c', -1);
1562                                 } catch (ArgumentOutOfRangeException) {
1563                                         errorThrown = true;
1564                                 }
1565                                 Assert ("repeat negative copies error not thrown",
1566                                            errorThrown);
1567                         }
1568                         {
1569                                 ArrayList al1 = ArrayList.Repeat ("huh?", 0);
1570                                 AssertEquals ("should be nothing in array",
1571                                                  0, al1.Count);
1572                         }
1573                         {
1574                                 ArrayList al1 = ArrayList.Repeat ("huh?", 3);
1575                                 AssertEquals ("should be something in array",
1576                                                  3, al1.Count);
1577                                 AssertEquals ("array elem doesn't check",
1578                                                  "huh?", al1 [0]);
1579                                 AssertEquals ("array elem doesn't check",
1580                                                  "huh?", al1 [1]);
1581                                 AssertEquals ("array elem doesn't check",
1582                                                  "huh?", al1 [2]);
1583                         }
1584                 }
1585
1586                 [Test]
1587                 public void TestReverse ()
1588                 {
1589                         {
1590                                 bool errorThrown = false;
1591                                 try {
1592                                         ArrayList al1 =
1593                                                 ArrayList.ReadOnly (new ArrayList ());
1594                                         al1.Reverse ();
1595                                 } catch (NotSupportedException) {
1596                                         errorThrown = true;
1597                                 }
1598                                 Assert ("reverse on read only error not thrown",
1599                                            errorThrown);
1600                         }
1601                         {
1602                                 bool errorThrown = false;
1603                                 try {
1604                                         char [] c = new Char [2];
1605                                         ArrayList al1 = new ArrayList (c);
1606                                         al1.Reverse (0, 3);
1607                                 } catch (ArgumentException) {
1608                                         errorThrown = true;
1609                                 }
1610                                 Assert ("error not thrown", errorThrown);
1611                         }
1612                         {
1613                                 bool errorThrown = false;
1614                                 try {
1615                                         char [] c = new Char [2];
1616                                         ArrayList al1 = new ArrayList (c);
1617                                         al1.Reverse (3, 0);
1618                                 } catch (ArgumentException) {
1619                                         errorThrown = true;
1620                                 }
1621                                 Assert ("error not thrown", errorThrown);
1622                         }
1623                         {
1624                                 char [] c = { 'a', 'b', 'c', 'd', 'e' };
1625                                 ArrayList al1 = new ArrayList (c);
1626                                 al1.Reverse (2, 1);
1627                                 for (int i = 0; i < al1.Count; i++) {
1628                                         AssertEquals ("Should be no change yet",
1629                                                          c [i], al1 [i]);
1630                                 }
1631                                 al1.Reverse ();
1632                                 for (int i = 0; i < al1.Count; i++) {
1633                                         AssertEquals ("Should be reversed",
1634                                                          c [i], al1 [4 - i]);
1635                                 }
1636                                 al1.Reverse ();
1637                                 for (int i = 0; i < al1.Count; i++) {
1638                                         AssertEquals ("Should be back to normal",
1639                                                          c [i], al1 [i]);
1640                                 }
1641                                 al1.Reverse (1, 3);
1642                                 AssertEquals ("Should be back to normal", c [0], al1 [0]);
1643                                 AssertEquals ("Should be back to normal", c [3], al1 [1]);
1644                                 AssertEquals ("Should be back to normal", c [2], al1 [2]);
1645                                 AssertEquals ("Should be back to normal", c [1], al1 [3]);
1646                                 AssertEquals ("Should be back to normal", c [4], al1 [4]);
1647                         }
1648                 }
1649
1650                 [Test]
1651                 [ExpectedException (typeof (ArgumentException))]
1652                 public void Reverse_IndexOverflow ()
1653                 {
1654                         ArrayList al = new ArrayList ();
1655                         al.Add (this);
1656                         al.Reverse (Int32.MaxValue, 1);
1657                 }
1658
1659                 [Test]
1660                 [ExpectedException (typeof (ArgumentException))]
1661                 public void Reverse_CountOverflow ()
1662                 {
1663                         ArrayList al = new ArrayList ();
1664                         al.Add (this);
1665                         al.Reverse (1, Int32.MaxValue);
1666                 }
1667
1668                 [Test]
1669                 public void TestSetRange ()
1670                 {
1671                         {
1672                                 bool errorThrown = false;
1673                                 try {
1674                                         char [] c = { 'a', 'b', 'c' };
1675                                         ArrayList al1 =
1676                                                 ArrayList.ReadOnly (new ArrayList (3));
1677                                         al1.SetRange (0, c);
1678                                 } catch (NotSupportedException) {
1679                                         errorThrown = true;
1680                                 } catch (Exception e) {
1681                                         Fail ("Incorrect exception thrown at 1: " + e.ToString ());
1682                                 }
1683                                 Assert ("setrange on read only error not thrown",
1684                                            errorThrown);
1685                         }
1686                         {
1687                                 bool errorThrown = false;
1688                                 try {
1689                                         ArrayList al1 = new ArrayList (3);
1690                                         al1.SetRange (0, null);
1691                                 } catch (ArgumentNullException) {
1692                                         errorThrown = true;
1693                                 } catch (ArgumentOutOfRangeException) {
1694                                         errorThrown = true;
1695                                 } catch (Exception e) {
1696                                         Fail ("Incorrect exception thrown at 2: " + e.ToString ());
1697                                 }
1698                                 Assert ("setrange with null error not thrown",
1699                                            errorThrown);
1700                         }
1701                         {
1702                                 bool errorThrown = false;
1703                                 try {
1704                                         char [] c = { 'a', 'b', 'c' };
1705                                         ArrayList al1 = new ArrayList (3);
1706                                         al1.SetRange (-1, c);
1707                                 } catch (ArgumentOutOfRangeException) {
1708                                         errorThrown = true;
1709                                 } catch (Exception e) {
1710                                         Fail ("Incorrect exception thrown at 3: " + e.ToString ());
1711                                 }
1712                                 Assert ("setrange with negative index error not thrown",
1713                                            errorThrown);
1714                         }
1715                         {
1716                                 bool errorThrown = false;
1717                                 try {
1718                                         char [] c = { 'a', 'b', 'c' };
1719                                         ArrayList al1 = new ArrayList (3);
1720                                         al1.SetRange (2, c);
1721                                 } catch (ArgumentOutOfRangeException) {
1722                                         errorThrown = true;
1723                                 } catch (Exception e) {
1724                                         Fail ("Incorrect exception thrown at 4: " + e.ToString ());
1725                                 }
1726                                 Assert ("setrange with too much error not thrown",
1727                                            errorThrown);
1728                         }
1729
1730                         {
1731                                 char [] c = { 'a', 'b', 'c' };
1732                                 ArrayList al1 = ArrayList.Repeat ('?', 3);
1733                                 Assert ("no match yet", c [0] != (char) al1 [0]);
1734                                 Assert ("no match yet", c [1] != (char) al1 [1]);
1735                                 Assert ("no match yet", c [2] != (char) al1 [2]);
1736                                 al1.SetRange (0, c);
1737                                 AssertEquals ("should match", c [0], al1 [0]);
1738                                 AssertEquals ("should match", c [1], al1 [1]);
1739                                 AssertEquals ("should match", c [2], al1 [2]);
1740                         }
1741                 }
1742
1743                 [Test]
1744                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1745                 public void SetRange_Overflow ()
1746                 {
1747                         ArrayList al = new ArrayList ();
1748                         al.Add (this);
1749                         al.SetRange (Int32.MaxValue, new ArrayList ());
1750                 }
1751
1752                 [Test]
1753                 public void TestInsertRange_this ()
1754                 {
1755                         String [] s1 = { "this", "is", "a", "test" };
1756                         ArrayList al = new ArrayList (s1);
1757                         al.InsertRange (2, al);
1758                         String [] s2 = { "this", "is", "this", "is", "a", "test", "a", "test" };
1759                         for (int i = 0; i < al.Count; i++) {
1760                                 AssertEquals ("at i=" + i, s2 [i], al [i]);
1761                         }
1762                 }
1763
1764                 [Test]
1765                 public void TestSort ()
1766                 {
1767                         {
1768                                 bool errorThrown = false;
1769                                 try {
1770                                         ArrayList al1 =
1771                                                 ArrayList.ReadOnly (new ArrayList ());
1772                                         al1.Sort ();
1773                                 } catch (NotSupportedException) {
1774                                         errorThrown = true;
1775                                 }
1776                                 Assert ("sort on read only error not thrown",
1777                                            errorThrown);
1778                         }
1779                         {
1780                                 char [] starter = { 'd', 'b', 'f', 'e', 'a', 'c' };
1781                                 ArrayList al1 = new ArrayList (starter);
1782                                 al1.Sort ();
1783                                 AssertEquals ("Should be sorted", 'a', al1 [0]);
1784                                 AssertEquals ("Should be sorted", 'b', al1 [1]);
1785                                 AssertEquals ("Should be sorted", 'c', al1 [2]);
1786                                 AssertEquals ("Should be sorted", 'd', al1 [3]);
1787                                 AssertEquals ("Should be sorted", 'e', al1 [4]);
1788                                 AssertEquals ("Should be sorted", 'f', al1 [5]);
1789                         }
1790                         {
1791                                 ArrayList al1 = new ArrayList ();
1792                                 al1.Add (null);
1793                                 al1.Add (null);
1794                                 al1.Add (32);
1795                                 al1.Add (33);
1796                                 al1.Add (null);
1797                                 al1.Add (null);
1798
1799                                 al1.Sort ();
1800                                 AssertEquals ("Should be null", null, al1 [0]);
1801                                 AssertEquals ("Should be 2. null", null, al1 [1]);
1802                                 AssertEquals ("Should be 3. null", null, al1 [2]);
1803                                 AssertEquals ("Should be 4. null", null, al1 [3]);
1804                                 AssertEquals ("Should be 32", 32, al1 [4]);
1805                                 AssertEquals ("Should be 33", 33, al1 [5]);
1806                         }
1807                 }
1808
1809                 [Test]
1810                 [ExpectedException (typeof (ArgumentException))]
1811                 public void Sort_IndexOverflow ()
1812                 {
1813                         ArrayList al = new ArrayList ();
1814                         al.Add (this);
1815                         al.Sort (Int32.MaxValue, 1, null);
1816                 }
1817
1818                 [Test]
1819                 [ExpectedException (typeof (ArgumentException))]
1820                 public void Sort_CountOverflow ()
1821                 {
1822                         ArrayList al = new ArrayList ();
1823                         al.Add (this);
1824                         al.Sort (1, Int32.MaxValue, null);
1825                 }
1826
1827                 // TODO - Sort with IComparers
1828
1829                 // TODO - Synchronize
1830
1831                 [Test]
1832                 public void TestToArray ()
1833                 {
1834                         {
1835                                 bool errorThrown = false;
1836                                 try {
1837                                         ArrayList al1 = new ArrayList (3);
1838                                         al1.ToArray (null);
1839                                 } catch (ArgumentNullException) {
1840                                         errorThrown = true;
1841                                 }
1842                                 Assert ("toarray with null error not thrown",
1843                                            errorThrown);
1844                         }
1845                         {
1846                                 bool errorThrown = false;
1847                                 try {
1848                                         char [] c = { 'a', 'b', 'c' };
1849                                         string s = "huh?";
1850                                         ArrayList al1 = new ArrayList (c);
1851                                         al1.ToArray (s.GetType ());
1852                                 } catch (InvalidCastException) {
1853                                         errorThrown = true;
1854                                 }
1855                                 Assert ("toarray with bad type error not thrown",
1856                                            errorThrown);
1857                         }
1858                         {
1859                                 char [] c1 = { 'a', 'b', 'c', 'd', 'e' };
1860                                 ArrayList al1 = new ArrayList (c1);
1861                                 object [] o2 = al1.ToArray ();
1862                                 for (int i = 0; i < c1.Length; i++) {
1863                                         AssertEquals ("should be copy", c1 [i], o2 [i]);
1864                                 }
1865                                 Array c2 = al1.ToArray (c1 [0].GetType ());
1866                                 for (int i = 0; i < c1.Length; i++) {
1867                                         AssertEquals ("should be copy",
1868                                                          c1 [i], c2.GetValue (i));
1869                                 }
1870                         }
1871                 }
1872
1873                 [Test]
1874                 [ExpectedException (typeof (NotSupportedException))]
1875                 public void TrimToSize_ReadOnly ()
1876                 {
1877                         ArrayList al1 = ArrayList.ReadOnly (new ArrayList ());
1878                         al1.TrimToSize ();
1879                 }
1880
1881                 [Test]
1882                 public void TrimToSize ()
1883                 {
1884                         ArrayList al1 = new ArrayList ();
1885 #if NET_2_0
1886                 // Capacity is 0 under 2.0
1887                 int capacity = 4;
1888 #else
1889                         int capacity = al1.Capacity;
1890 #endif
1891                         int size = capacity / 2;
1892                         for (int i = 1; i <= size; i++) {
1893                                 al1.Add ('?');
1894                         }
1895                         al1.RemoveAt (0);
1896                         al1.TrimToSize ();
1897                         AssertEquals ("no capacity match", size - 1, al1.Capacity);
1898
1899                         al1.Clear ();
1900                         al1.TrimToSize ();
1901                         AssertEquals ("no default capacity", capacity, al1.Capacity);
1902                 }
1903
1904                 class Comparer : IComparer
1905                 {
1906
1907                         private bool called = false;
1908
1909                         public bool Called
1910                         {
1911                                 get
1912                                 {
1913                                         bool result = called;
1914                                         called = false;
1915                                         return called;
1916                                 }
1917                         }
1918
1919                         public int Compare (object x, object y)
1920                         {
1921                                 called = true;
1922                                 return 0;
1923                         }
1924                 }
1925
1926                 [Test]
1927                 public void BinarySearch1_EmptyList ()
1928                 {
1929                         ArrayList list = new ArrayList ();
1930                         AssertEquals ("BinarySearch", -1, list.BinarySearch (0));
1931                 }
1932
1933                 [Test]
1934                 public void BinarySearch2_EmptyList ()
1935                 {
1936                         Comparer comparer = new Comparer ();
1937                         ArrayList list = new ArrayList ();
1938                         AssertEquals ("BinarySearch", -1, list.BinarySearch (0, comparer));
1939                         // bug 77030 - the comparer isn't called for an empty array/list
1940                         Assert ("Called", !comparer.Called);
1941                 }
1942
1943                 [Test]
1944                 public void BinarySearch3_EmptyList ()
1945                 {
1946                         Comparer comparer = new Comparer ();
1947                         ArrayList list = new ArrayList ();
1948                         AssertEquals ("BinarySearch", -1, list.BinarySearch (0, 0, 0, comparer));
1949                         // bug 77030 - the comparer isn't called for an empty array/list
1950                         Assert ("Called", !comparer.Called);
1951                 }
1952
1953                 [Test]
1954 #if ONLY_1_1
1955         [Category ("NotDotNet")] // MS bug
1956 #endif
1957                 public void AddRange_GetRange ()
1958                 {
1959                         ArrayList source = ArrayList.Adapter (new object [] { "1", "2" });
1960                         AssertEquals ("#1", 2, source.Count);
1961                         AssertEquals ("#2", "1", source [0]);
1962                         AssertEquals ("#3", "2", source [1]);
1963                         ArrayList range = source.GetRange (1, 1);
1964                         AssertEquals ("#4", 1, range.Count);
1965                         AssertEquals ("#5", "2", range [0]);
1966                         ArrayList target = new ArrayList ();
1967                         target.AddRange (range);
1968                         AssertEquals ("#6", 1, target.Count);
1969                         AssertEquals ("#7", "2", target [0]);
1970                 }
1971
1972                 [Test]
1973 #if ONLY_1_1
1974         [Category ("NotDotNet")] // MS bug
1975 #endif
1976                 public void IterateSelf ()
1977                 {
1978                         ArrayList list = new ArrayList ();
1979                         list.Add (list);
1980                         IEnumerator enumerator = list.GetEnumerator ();
1981                         Assert ("#1", enumerator.MoveNext ());
1982                         Assert ("#2", object.ReferenceEquals (list, enumerator.Current));
1983                         Assert ("#3", !enumerator.MoveNext ());
1984                 }
1985         }
1986 }