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