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