2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / corlib / Test / System.Collections / NewArrayListTest.cs
1 // NewArrayListTest.cs
2 // 
3 // Unit tests for System.Collections.ArrayList
4 //
5 // Copyright (c) 2003 Thong (Tum) Nguyen [tum@veridicus.com]
6 //
7 // Released under the MIT License:
8 //
9 // http://www.opensource.org/licenses/mit-license.html
10 // 
11 // Permission is hereby granted, free of charge, to any person obtaining a copy of this
12 // software and associated documentation files (the "Software"), to deal in the 
13 // Software without restriction, including without limitation the rights to use, copy,
14 // modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
15 // and to permit persons to whom the Software is furnished to do so, subject to the
16 // following conditions:
17 //
18 // The above copyright notice and this permission notice shall be included in all copies
19 // or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
25 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
29 //
30 // Author's comment: Source code formatting has been changed by request to match 
31 // Mono's formatting style.  I personally use BSD-style formatting.
32 // 
33
34 using System;
35 using System.Collections;
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Collections 
39 {
40         /// <summary>
41         /// Some test cases for the new ArrayList implementation.
42         /// </summary>
43         [TestFixture]
44         public class NewArrayListTest
45                 : TestCase 
46         {
47                 private object[] c_TestData = new Object[] {0,1,2,3,4,5,6,7,8,9};
48
49                 private void VerifyContains(IList list, IList values, string message) 
50                 {
51                         if (values.Count != list.Count) 
52                         {
53                                 Assertion.Assert(message, false);
54                         }
55
56                         for (int i = 0; i < list.Count; i++) 
57                         {
58                                 if (list[i] == null && values[i] == null) 
59                                 {
60                                         continue;
61                                 }
62
63                                 if ((list[i] == null || values[i] == null) || !list[i].Equals(values[i])) 
64                                 {
65                                         Assertion.Assert(message, false);
66                                 }
67                         }
68                 }
69
70                 private void PrivateTestSort(ArrayList arrayList) 
71                 {       
72                         Random random = new Random(1027);
73
74                         // Sort arrays of lengths up to 200
75
76                         for (int i = 1; i < 200; i++) 
77                         {
78                                 for (int j = 0; j < i; j++) 
79                                 {
80                                         arrayList.Add(random.Next(0, 1000));
81                                 }
82
83                                 arrayList.Sort();
84
85                                 for (int j = 1; j < i; j++) 
86                                 {
87                                         if ((int)arrayList[j] < (int)arrayList[j - 1]) 
88                                         {
89                                                 Assertion.Fail("ArrayList.Sort()");
90
91                                                 return;
92                                         }
93                                 }
94
95                                 arrayList.Clear();
96                         }
97                 }
98
99                 [Test]
100                 public void TestSortStandard() 
101                 {
102                         PrivateTestSort(new ArrayList());
103                 }
104
105                 [Test]
106                 public void TestSortSynchronized() 
107                 {
108                         PrivateTestSort(ArrayList.Synchronized(new ArrayList()));
109                 }
110
111                 [Test]
112                 public void TestSortAdapter() 
113                 {
114                         PrivateTestSort(ArrayList.Adapter(new ArrayList()));
115                 }
116
117                 [Test]
118                 public void TestSortGetRange() 
119                 {
120                         PrivateTestSort(new ArrayList().GetRange(0, 0));
121                 }
122
123                 private void PrivateTestIndexOf(ArrayList arrayList) 
124                 {
125                         int x;
126                         
127                         arrayList.AddRange(c_TestData);
128
129                         for (int i = 0; i < 10; i++) 
130                         {                       
131                                 x = arrayList.IndexOf(i);
132                                 Assertion.Assert("ArrayList.IndexOf(" + i + ")", x == i);
133                         }
134
135                         try 
136                         {
137                                 arrayList.IndexOf(0, 10, 1);
138                                 Assertion.Fail("ArrayList.IndexOf(0, 10, 1)");
139                         }
140                         catch (ArgumentOutOfRangeException) 
141                         {
142                         }
143
144                         try 
145                         {
146                                 arrayList.IndexOf(0, 0, -1);
147                                 Assertion.Fail("ArrayList.IndexOf(0, 10, 1)");
148                         }
149                         catch (ArgumentOutOfRangeException) 
150                         {
151                         }
152
153                         try 
154                         {
155                                 arrayList.IndexOf(0, -1, -1);
156                                 Assertion.Fail("ArrayList.IndexOf(0, 10, 1)");
157                         }
158                         catch (ArgumentOutOfRangeException) 
159                         {
160                         }
161
162                         try 
163                         {
164                                 arrayList.IndexOf(0, 9, 10);                            
165                                 Assertion.Fail("ArrayList.IndexOf(0, 10, 1)");
166                         }
167                         catch (ArgumentOutOfRangeException) 
168                         {                               
169                         }
170
171                         try 
172                         {
173                                 arrayList.IndexOf(0, 0, 10);                            
174                         }
175                         catch (ArgumentOutOfRangeException) 
176                         {
177                                 Assertion.Fail("ArrayList.IndexOf(0, 10, 1)");
178                         }
179
180                         try 
181                         {
182                                 arrayList.IndexOf(0, 0, 11);
183                                 Assertion.Fail("ArrayList.IndexOf(0, 10, 1)");
184                         }
185                         catch (ArgumentOutOfRangeException) 
186                         {                               
187                         }
188
189                         // LastIndexOf
190
191                         for (int i = 0; i < 10; i++) 
192                         {
193                                 x = arrayList.LastIndexOf(i);
194
195                                 Assertion.Assert("ArrayList.LastIndexOf(" + i + ")", x == i);
196                         }                       
197
198                         try 
199                         {
200                                 arrayList.IndexOf(0, 10, 1);
201
202                                 Assertion.Fail("ArrayList.LastIndexOf(0, 10, 1)");
203                         }
204                         catch (ArgumentOutOfRangeException) 
205                         {
206                         }
207
208                         try 
209                         {
210                                 arrayList.IndexOf(0, 0, -1);
211
212                                 Assertion.Fail("ArrayList.LastIndexOf(0, 10, 1)");
213                         }
214                         catch (ArgumentOutOfRangeException) 
215                         {
216                         }
217
218                         try 
219                         {
220                                 arrayList.LastIndexOf(0, -1, -1);
221
222                                 Assertion.Fail("ArrayList.LastIndexOf(0, 10, 1)");
223                         }
224                         catch (ArgumentOutOfRangeException) 
225                         {
226                         }
227
228                         try 
229                         {
230                                 arrayList.LastIndexOf(0, 9, 10);                                
231                         }
232                         catch (ArgumentOutOfRangeException) 
233                         {
234                                 Assertion.Fail("ArrayList.LastIndexOf(0, 10, 1)");
235                         }
236
237                         try 
238                         {
239                                 arrayList.LastIndexOf(0, 0, 10);
240                                 Assertion.Fail("ArrayList.LastIndexOf(0, 10, 1)");
241                         }
242                         catch (ArgumentOutOfRangeException) 
243                         {                               
244                         }
245
246                         try 
247                         {
248                                 arrayList.LastIndexOf(0, 0, 11);
249                                 Assertion.Fail("ArrayList.LastIndexOf(0, 10, 1)");
250                         }
251                         catch (ArgumentOutOfRangeException) 
252                         {                               
253                         }
254                 }
255
256                 private void PrivateTestAddRange(ArrayList arrayList) 
257                 {
258                         arrayList.AddRange(c_TestData);
259                         arrayList.AddRange(c_TestData);
260
261                         VerifyContains(arrayList, new object[] {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9}, "VerifyContains");
262                 }
263
264                 [Test]
265                 public void TestAddRangeStandard() 
266                 {
267                         PrivateTestAddRange(new ArrayList());
268                 }
269
270                 [Test]
271                 public void TestAddRangeSynchronized() 
272                 {
273                         PrivateTestAddRange(ArrayList.Synchronized(new ArrayList()));
274                 }
275
276                 [Test]
277                 public void TestAddRangeAdapter() 
278                 {
279                         PrivateTestAddRange(ArrayList.Adapter(new ArrayList()));
280                 }
281
282                 [Test]
283                 public void TestAddRangeGetRange() 
284                 {
285                         PrivateTestAddRange(new ArrayList().GetRange(0, 0));
286                 }
287                 
288                 [Test]
289                 public void TestIndexOfStandard() 
290                 {
291                         PrivateTestIndexOf(new ArrayList());
292                 }
293
294                 [Test]
295                 public void TestIndexOfSynchronized() 
296                 {
297                         PrivateTestIndexOf(ArrayList.Synchronized(new ArrayList()));
298                 }
299
300                 [Test]
301                 public void TestIndexOfAdapter() 
302                 {
303                         PrivateTestIndexOf(ArrayList.Adapter(new ArrayList()));
304                 }
305
306                 [Test]
307                 public void TestIndexOfGetRange() 
308                 {
309                         PrivateTestIndexOf(new ArrayList().GetRange(0, 0));
310                 }
311
312                 [Test]
313                 public void TestReadOnly() 
314                 {
315                         ArrayList arrayList, readOnlyList;
316                         
317                         arrayList = new ArrayList();
318                         readOnlyList = ArrayList.ReadOnly(arrayList);
319
320                         arrayList.AddRange(c_TestData);
321
322                         // Make sure the readOnlyList is a wrapper and not a clone.
323
324                         arrayList.Add(10);
325                         Assertion.Assert("readOnlyList.Count == 11", readOnlyList.Count == 11);
326
327                         try 
328                         {
329                                 readOnlyList.Add(0);
330                                 Assertion.Fail("readOnlyList.Add(0)");
331                         }
332                         catch (NotSupportedException) 
333                         {
334                         }
335
336                         try 
337                         {
338                                 readOnlyList.AddRange(c_TestData);
339
340                                 Assertion.Fail("readOnlyList.AddRange(c_TestData)");
341                         }
342                         catch (NotSupportedException) 
343                         {
344                         }
345                         
346                         try 
347                         {
348                                 readOnlyList.BinarySearch(1);                           
349                         }
350                         catch (NotSupportedException) 
351                         {
352                                 Assertion.Fail("readOnlyList.BinarySearch(1)");
353                         }                       
354
355                         try 
356                         {
357                                 int x = readOnlyList.Capacity;
358                         }
359                         catch (NotSupportedException) 
360                         {
361                                 Assertion.Fail("readOnlyList.Capacity");
362                         }
363
364                         try 
365                         {
366                                 readOnlyList.Clear();
367                                 Assertion.Fail("readOnlyList.Clear()");
368                         }
369                         catch (NotSupportedException) 
370                         {                               
371                         }
372                         
373                         try 
374                         {
375                                 readOnlyList.Clone();                           
376                         }
377                         catch (NotSupportedException) 
378                         {
379                                 Assertion.Fail("readOnlyList.Clone()");
380                         }                       
381
382                         try 
383                         {
384                                 readOnlyList.Contains(1);
385                         }
386                         catch (NotSupportedException) 
387                         {
388                                 Assertion.Fail("readOnlyList.Contains");
389                         }
390
391                         try 
392                         {
393                                 readOnlyList.CopyTo(new object[readOnlyList.Count]);
394                         }
395                         catch (NotSupportedException) 
396                         {
397                                 Assertion.Fail("readOnlyList.CopyTo(new Array(readOnlyList.Count))");
398                         }
399
400                         try 
401                         {
402                                 foreach (object o in readOnlyList) 
403                                 {
404                                         o.ToString();
405                                 }
406                         }
407                         catch (NotSupportedException) 
408                         {
409                                 Assertion.Fail("readOnlyList.GetEnumerator()");
410                         }
411
412                         try 
413                         {
414                                 readOnlyList.GetRange(0, 1);                            
415                         }
416                         catch (NotSupportedException) 
417                         {
418                                 Assertion.Fail("readOnlyList.GetRange(0, 1)");
419                         }
420
421                         try 
422                         {
423                                 readOnlyList.IndexOf(1);                                
424                         }
425                         catch (NotSupportedException) 
426                         {
427                                 Assertion.Fail("readOnlyList.readOnlyList.IndexOf(1)");
428                         }
429
430                         try 
431                         {
432                                 readOnlyList[0] = 0;
433                                 Assertion.Fail("readOnlyList[0] = 0");
434                         }
435                         catch (NotSupportedException) 
436                         {
437                         }
438
439                         try 
440                         {
441                                 readOnlyList.IndexOf(0);
442                         }
443                         catch (NotSupportedException) 
444                         {
445                                 Assertion.Fail("readOnlyList.IndexOf(0)");
446                         }
447
448                         try 
449                         {
450                                 readOnlyList.InsertRange(0, new object[] {1,2});
451
452                                 Assertion.Fail("readOnlyList.InsertRange(0, new object[] {1,2})");
453                         }
454                         catch (NotSupportedException) 
455                         {
456                         }
457
458                         try 
459                         {
460                                 readOnlyList.LastIndexOf(1111);
461                         }
462                         catch (NotSupportedException) 
463                         {
464                                 Assertion.Fail("readOnlyList.LastIndexOf(1)");
465                         }
466
467                         try 
468                         {
469                                 readOnlyList.Remove(1);
470
471                                 Assertion.Fail("readOnlyList.Remove(1)");
472                         }
473                         catch (NotSupportedException) 
474                         {
475                         }
476
477                         try 
478                         {
479                                 readOnlyList.RemoveAt(1);
480
481                                 Assertion.Fail("readOnlyList.RemoveAt(1)");
482                         }
483                         catch (NotSupportedException) 
484                         {
485                         }
486
487                         try 
488                         {
489                                 readOnlyList.RemoveRange(0, 1);
490
491                                 Assertion.Fail("readOnlyList.RemoveRange(0, 1)");
492                         }
493                         catch (NotSupportedException) 
494                         {
495                         }
496
497                         try 
498                         {
499                                 readOnlyList.Reverse();
500
501                                 Assertion.Fail("readOnlyList.Reverse()");
502                         }
503                         catch (NotSupportedException) 
504                         {                               
505                         }
506
507                         try 
508                         {
509                                 readOnlyList.SetRange(0, new Object[] {0, 1});
510
511                                 Assertion.Fail("readOnlyList.SetRange(0, new Object[] {0, 1})");
512                         }
513                         catch (NotSupportedException) 
514                         {                               
515                         }
516
517                         try 
518                         {
519                                 readOnlyList.Sort();
520
521                                 Assertion.Fail("readOnlyList.Sort()");
522                         }
523                         catch (NotSupportedException) 
524                         {
525                         }
526
527                         try 
528                         {
529                                 readOnlyList.ToArray();                         
530                         }
531                         catch (NotSupportedException) 
532                         {
533                                 Assertion.Fail("readOnlyList.ToArray()");
534                         }
535
536                         try 
537                         {
538                                 readOnlyList.TrimToSize();
539
540                                 Assertion.Fail("readOnlyList.TrimToSize()");
541                         }
542                         catch (NotSupportedException) 
543                         {
544                         }                       
545                 }
546
547                 [Test]
548                 public void TestFixedSize() 
549                 {
550                         ArrayList arrayList, fixedSizeList;
551                         
552                         arrayList = new ArrayList();
553                         fixedSizeList = ArrayList.FixedSize(arrayList);
554
555                         arrayList.AddRange(c_TestData);
556
557                         // Make sure the fixedSizeList is a wrapper and not a clone.
558
559                         arrayList.Add(10);
560                         Assertion.Assert("fixedSizeList.Count == 11", fixedSizeList.Count == 11);
561
562                         try 
563                         {
564                                 fixedSizeList.Add(0);
565                                 Assertion.Fail("fixedSizeList.Add(0)");
566                         }
567                         catch (NotSupportedException) 
568                         {
569                         }
570
571                         try 
572                         {
573                                 fixedSizeList.Remove(0);
574                                 Assertion.Fail("fixedSizeList.Remove(0)");
575                         }
576                         catch (NotSupportedException) 
577                         {                               
578                         }
579
580                         try 
581                         {
582                                 fixedSizeList.RemoveAt(0);
583                                 Assertion.Fail("fixedSizeList.RemoveAt(0)");
584                         }
585                         catch (NotSupportedException) 
586                         {
587                         }
588
589                         try 
590                         {
591                                 fixedSizeList.Clear();
592                                 Assertion.Fail("fixedSizeList.Clear()");
593                         }
594                         catch (NotSupportedException) 
595                         {                               
596                         }
597
598                         try 
599                         {
600                                 fixedSizeList[0] = 0;                           
601                         }
602                         catch (NotSupportedException) 
603                         {
604                                 Assertion.Fail("fixedSizeList[0] = 0");
605                         }
606
607                         try 
608                         {
609                                 fixedSizeList.Clear();
610                                 Assertion.Fail("fixedSizeList.Clear()");
611                         }
612                         catch (NotSupportedException) 
613                         {
614                         }
615
616                         try 
617                         {
618                                 fixedSizeList.Contains(1);
619                         }
620                         catch (NotSupportedException) 
621                         {
622                                 Assertion.Fail("fixedSizeList.Contains");
623                         }
624
625                         try 
626                         {
627                                 int x = fixedSizeList.Count;
628                         }
629                         catch (NotSupportedException) 
630                         {
631                                 Assertion.Fail("fixedSizeList.Count");
632                         }
633
634                         try 
635                         {
636                                 fixedSizeList.GetRange(0, 1);
637                         }
638                         catch (NotSupportedException) 
639                         {
640                                 Assertion.Fail("fixedSizeList.GetRange(0, 1)");
641                         }
642
643                         try 
644                         {
645                                 fixedSizeList.IndexOf(0);
646                         }
647                         catch (NotSupportedException) 
648                         {
649                                 Assertion.Fail("fixedSizeList.IndexOf(0)");
650                         }
651
652                         try 
653                         {
654                                 fixedSizeList.InsertRange(0, new object[] {1,2});
655
656                                 Assertion.Fail("fixedSizeList.InsertRange(0, new object[] {1,2})");
657                         }
658                         catch (NotSupportedException) 
659                         {                               
660                         }
661
662                         try 
663                         {
664                                 fixedSizeList.Reverse();                                
665                         }
666                         catch (NotSupportedException) 
667                         {
668                                 Assertion.Fail("fixedSizeList.Reverse()");
669                         }
670
671                         try 
672                         {
673                                 fixedSizeList.SetRange(0, new Object[] {0, 1});
674                         }
675                         catch (NotSupportedException) 
676                         {                               
677                                 Assertion.Fail("fixedSizeList.SetRange(0, new Object[] {0, 1})");
678                         }
679
680                         try 
681                         {
682                                 fixedSizeList.Sort();
683                         }
684                         catch (NotSupportedException) 
685                         {
686                                 Assertion.Fail("fixedSizeList.Sort()");
687                         }
688
689                         try 
690                         {
691                                 fixedSizeList.ToArray();                                
692                         }
693                         catch (NotSupportedException) 
694                         {
695                                 Assertion.Fail("fixedSizeList.ToArray()");
696                         }
697
698                         try 
699                         {
700                                 fixedSizeList.TrimToSize();
701
702                                 Assertion.Fail("fixedSizeList.TrimToSize()");
703                         }
704                         catch (NotSupportedException) 
705                         {                               
706                         }
707
708                         try 
709                         {
710                                 fixedSizeList.Clone();                          
711                         }
712                         catch (NotSupportedException) 
713                         {
714                                 Assertion.Fail("fixedSizeList.Clone()");
715                         }
716                         
717                         try 
718                         {
719                                 fixedSizeList.AddRange(c_TestData);
720
721                                 Assertion.Fail("fixedSizeList.AddRange(c_TestData)");
722                         }
723                         catch (NotSupportedException) 
724                         {                               
725                         }                       
726                 }
727
728                 private void PrivateTestClone(ArrayList arrayList) 
729                 {                       
730                         ArrayList arrayList2;
731                                                 
732                         arrayList.AddRange(c_TestData);
733
734                         arrayList2 = (ArrayList)arrayList.Clone();
735
736                         VerifyContains(arrayList2, c_TestData, "arrayList.Clone()");
737                 }
738
739                 [Test]
740                 public void TestCloneStandard() 
741                 {
742                         PrivateTestClone(new ArrayList());
743                 }
744
745                 [Test]
746                 public void TestCloneSynchronized() 
747                 {
748                         PrivateTestClone(ArrayList.Synchronized(new ArrayList()));
749                 }
750
751                 [Test]
752                 public void TestCloneAdapter() 
753                 {
754                         PrivateTestClone(ArrayList.Adapter(new ArrayList()));
755                 }
756
757                 [Test]
758                 public void TestCloneGetRange() 
759                 {
760                         PrivateTestClone(new ArrayList().GetRange(0, 0));
761                 }
762
763                 private void PrivateTestCopyTo(ArrayList arrayList) 
764                 {
765                         object[] array;
766                         
767                         arrayList.AddRange(c_TestData);
768
769                         array = new Object[arrayList.Count];
770
771                         arrayList.CopyTo(array);
772                         
773                         VerifyContains(array, new object[] {0,1,2,3,4,5,6,7,8,9}, "ArrayList.CopyTo(array)");
774
775                         array = new Object[3];
776
777                         arrayList.CopyTo(0, array, 0, 3);
778                         
779                         VerifyContains(array, new object[] {0,1,2}, "ArrayList.CopyTo(0, array, 0, 3)");
780
781                         array = new Object[4];
782
783                         arrayList.CopyTo(0, array, 1, 3);
784                         
785                         VerifyContains(array, new object[] {null,0, 1, 2}, "ArrayList.CopyTo(0, array, 1, 3)");
786
787                         array = new object[10];
788
789                         arrayList.CopyTo(3, array, 3, 5);
790
791                         VerifyContains(array, new object[] {null, null, null, 3, 4, 5, 6, 7, null, null}, "VerifyContains(array, ...)");
792                 }
793
794                 [Test]
795                 public void TestCopyToStandard() 
796                 {
797                         PrivateTestCopyTo(new ArrayList());
798                 }
799
800                 [Test]
801                 public void TestCopyToSynchronized() 
802                 {
803                         PrivateTestCopyTo(ArrayList.Synchronized(new ArrayList()));
804                 }
805
806                 [Test]
807                 public void TestCopyToAdapter() 
808                 {
809                         PrivateTestCopyTo(ArrayList.Adapter(new ArrayList()));
810                 }
811
812                 [Test]
813                 public void TestCopyToGetRange() 
814                 {
815                         PrivateTestCopyTo(new ArrayList().GetRange(0, 0));
816                 }
817                 
818                 private void PrivateTestSetCapacity(ArrayList arrayList) 
819                 {
820                         int x;
821
822                         arrayList.AddRange(c_TestData);
823
824                         x = arrayList.Capacity;
825
826                         arrayList.Capacity = x * 2;
827
828                         Assert("arrayList.Capacity == x * 2", arrayList.Capacity == x * 2);
829
830                         VerifyContains(arrayList, c_TestData, "VerifyContains(arrayList, c_TestData)");
831                 }
832
833                 [Test]
834                 public void TestSetCapacity() 
835                 {
836                         PrivateTestSetCapacity(new ArrayList());
837                 }
838
839                 [Test]
840                 public void TestSetCapacitySynchronized() 
841                 {
842                         PrivateTestSetCapacity(ArrayList.Synchronized(new ArrayList()));
843                 }
844                 
845                 [Test]
846                 public void TestCapacityExpands() 
847                 {
848                         ArrayList arrayList = new ArrayList(10);
849
850                         arrayList.AddRange(c_TestData);
851
852                         Assertion.Assert("arrayList.Capacity == 10", arrayList.Capacity == 10);
853
854                         arrayList.Add(10);
855
856                         Assertion.Assert("arrayList.Capacity == 20", arrayList.Capacity == 20);
857
858                         VerifyContains(arrayList, new object[] {0,1,2,3,4,5,6,7,8,9,10}, "VerifyContains");
859                 }
860                 
861                 private void PrivateTestBinarySearch(ArrayList arrayList) 
862                 {
863                         // Try searching with different size lists...
864
865                         for (int x = 0; x < 10; x++) 
866                         {
867                                 for (int i = 0; i < x; i++) 
868                                 {
869                                         arrayList.Add(i);
870                                 }
871
872                                 for (int i = 0; i < x; i++) 
873                                 {
874                                         int y;
875
876                                         y = arrayList.BinarySearch(i);
877
878                                         Assertion.Equals(y, i);
879                                 }
880                         }
881
882                         arrayList.Clear();
883                         arrayList.Add(new object());
884
885                         try 
886                         {
887                                 arrayList.BinarySearch(new object());
888
889                                 Assertion.Fail("1: Binary search on object that doesn't support IComparable.");
890                         }
891                         catch (ArgumentException) 
892                         {                               
893                         }
894                         catch (InvalidOperationException) 
895                         {
896                                 // LAMESPEC: ArrayList.BinarySearch() on MS.NET throws InvalidOperationException
897                         }
898                         
899                         try 
900                         {
901                                 arrayList.BinarySearch(1);
902
903                                 Assertion.Fail("2: Binary search on incompatible object.");
904                         }
905                         catch (ArgumentException) 
906                         {                               
907                         }
908                         catch (InvalidOperationException) 
909                         {
910                                 // LAMESPEC: ArrayList.BinarySearch() on MS.NET throws InvalidOperationException
911                         }
912
913                         arrayList.Clear();
914
915                         for (int i = 0; i < 100; i++) 
916                         {
917                                 arrayList.Add(1);
918                         }
919
920                         Assertion.Assert("BinarySearch should start in middle.", arrayList.BinarySearch(1) == 49);                      
921                         Assertion.Assert("arrayList.BinarySearch(0, 0, 0, Comparer.Default)", arrayList.BinarySearch(0, 0, 0, Comparer.Default) == -1);
922                 }
923
924                 [Test]
925                 public void TestBinarySearchStandard() 
926                 {
927                         PrivateTestBinarySearch(new ArrayList());
928                 }
929
930                 [Test]
931                 public void TestBinarySearchSynchronized() 
932                 {
933                         PrivateTestBinarySearch(ArrayList.Synchronized(new ArrayList()));
934                 }
935
936                 [Test]
937                 public void TestBinarySearchAdapter() 
938                 {
939                         PrivateTestBinarySearch(ArrayList.Adapter(new ArrayList()));
940                 }
941
942                 [Test]
943                 public void TestBinarySearchGetRange() 
944                 {
945                         PrivateTestBinarySearch(new ArrayList().GetRange(0, 0));
946                 }
947                 
948                 private void PrivateTestRemoveAt(ArrayList arrayList) 
949                 {
950                         arrayList.Add(1);
951                         arrayList.Add(2);
952                         arrayList.Add(3);
953                         arrayList.Add(4);
954                         arrayList.Add(5);
955
956                         arrayList.Remove(2);
957
958                         VerifyContains(arrayList, new object[] {1, 3, 4, 5},
959                                 "Remove element failed.");
960
961                         arrayList.RemoveAt(0);
962
963                         VerifyContains(arrayList, new object[] {3, 4, 5},
964                                 "RemoveAt at start failed.");
965
966                         arrayList.RemoveAt(2);
967
968                         VerifyContains(arrayList, new object[] {3, 4},
969                                 "RemoveAt at end failed.");                     
970                 }
971
972                 [Test]
973                 public void TestRemoveAtStandard() 
974                 {
975                         PrivateTestRemoveAt(new ArrayList());
976                 }
977
978                 [Test]
979                 public void TestRemoveAtSynchronized() 
980                 {
981                         PrivateTestRemoveAt(ArrayList.Synchronized(new ArrayList()));
982                 }
983
984                 [Test]
985                 public void TestRemoveAtAdapter() 
986                 {
987                         PrivateTestRemoveAt(ArrayList.Adapter(new ArrayList()));
988                 }
989
990                 [Test]
991                 public void TestRemoveAtGetRange() 
992                 {
993                         PrivateTestRemoveAt(new ArrayList().GetRange(0, 0));
994                 }
995
996                 private void PrivateTestRemoveRange(ArrayList arrayList) 
997                 {
998                         arrayList.AddRange(c_TestData);
999
1000                         arrayList.RemoveRange(0, 3);
1001
1002                         VerifyContains(arrayList, new object[] { 3, 4, 5, 6, 7, 8, 9 },
1003                                 "RemoveRange at start failed.");
1004
1005                         arrayList.RemoveRange(4, 3);
1006
1007                         VerifyContains(arrayList, new object[] { 3, 4, 5, 6 },
1008                                 "RemoveRange at start failed.");
1009
1010                         arrayList.RemoveRange(2, 1);
1011
1012                         VerifyContains(arrayList, new object[] { 3, 4, 6 },
1013                                 "RemoveRange in middle failed.");
1014                 }
1015
1016                 [Test]
1017                 public void TestRemoveRangeStandard() 
1018                 {
1019                         PrivateTestRemoveRange(new ArrayList());
1020                 }
1021
1022                 [Test]
1023                 public void TestRemoveRangeSynchronized() 
1024                 {
1025                         PrivateTestRemoveRange(ArrayList.Synchronized(new ArrayList()));
1026                 }
1027
1028                 [Test]
1029                 public void TestRemoveRangeAdapter() 
1030                 {
1031                         PrivateTestRemoveRange(ArrayList.Adapter(new ArrayList()));
1032                 }
1033
1034                 [Test]
1035                 public void TestRemoveRangeGetRange() 
1036                 {
1037                         PrivateTestRemoveRange(new ArrayList().GetRange(0, 0));
1038                 }
1039
1040                 private void PrivateTestInsert(ArrayList arrayList) 
1041                 {
1042                         arrayList.Add(1);
1043                         arrayList.Add(2);
1044                         arrayList.Add(3);
1045                         arrayList.Add(4);
1046                         arrayList.Insert(0, 1);
1047
1048                         VerifyContains(arrayList, new object[] {1, 1, 2, 3, 4}, "Insert at beginning failed.");
1049
1050                         arrayList.Insert(5, 5);
1051
1052                         VerifyContains(arrayList, new object[] {1, 1, 2, 3, 4, 5}, "Insert at end failed.");
1053
1054                         arrayList.Insert(3, 7);
1055
1056                         VerifyContains(arrayList, new object[] {1, 1, 2, 7, 3, 4, 5}, "Insert in middle failed.");
1057                 }
1058
1059                 [Test]
1060                 public void TestInsertStandard() 
1061                 {
1062                         PrivateTestInsert(new ArrayList());
1063                 }
1064
1065                 [Test]
1066                 public void TestInsertAdapter() 
1067                 {
1068                         PrivateTestInsert(ArrayList.Adapter(new ArrayList()));
1069                 }
1070
1071                 [Test]
1072                 public void TestInsertSynchronized() 
1073                 {
1074                         PrivateTestInsert(ArrayList.Synchronized(new ArrayList()));
1075                 }
1076
1077                 [Test]
1078                 public void TestInsertGetRange() 
1079                 {
1080                         PrivateTestInsert(new ArrayList().GetRange(0, 0));
1081                 }
1082
1083                 private void PrivateTestGetRange(ArrayList arrayList) 
1084                 {
1085                         ArrayList rangeList;
1086
1087                         arrayList.AddRange(c_TestData);
1088
1089                         rangeList = arrayList.GetRange(3, 5);
1090
1091                         Assertion.Assert("rangeList.Count == 5", rangeList.Count == 5);
1092
1093                         this.VerifyContains(rangeList, new object[] {3,4,5,6,7}, "1: VerifyContains(rangeList)");
1094                         
1095 //FIXME: If items are removed from the Range, one may not iterate over it on .NET
1096 /*
1097                         rangeList.Remove(7);
1098                         
1099                         this.VerifyContains(a2, new object[] {3,4,5,6}, "2: VerifyContains(rangeList)");
1100
1101                         rangeList.RemoveAt(0);
1102
1103                         this.VerifyContains(a3, new object[] {4,5,6}, "3: VerifyContains(rangeList)");
1104
1105                         rangeList.Add(7);
1106                         rangeList.Add(6);
1107                         rangeList.Add(3);
1108                         rangeList.Add(11);
1109                         
1110                         Assertion.Assert("rangeList.LastIndexOf(6) == 4", rangeList.LastIndexOf(6) == 4);
1111
1112                         rangeList.Sort();
1113
1114                         this.VerifyContains(arrayList, new object[] {0, 1, 2, 3, 4, 5, 6, 6, 7, 11, 8, 9}, "4: VerifyContains(rangeList)");
1115 */
1116                 }
1117
1118                 [Test]
1119                 public void TestGetRangeStandard() 
1120                 {
1121                         PrivateTestGetRange(new ArrayList());
1122                 }
1123
1124                 [Test]
1125                 public void TestGetRangeAdapter() 
1126                 {
1127                         PrivateTestGetRange(ArrayList.Adapter(new ArrayList()));
1128                 }
1129
1130                 [Test]
1131                 public void TestGetRangeSynchronized() 
1132                 {
1133                         PrivateTestGetRange(ArrayList.Synchronized(new ArrayList()));
1134                 }
1135
1136                 [Test]
1137                 public void TestGetRangeGetRange() 
1138                 {
1139                         PrivateTestGetRange(new ArrayList().GetRange(0, 0));
1140                 }
1141
1142                 private void PrivateTestEnumeratorWithRange(ArrayList arrayList) 
1143                 {                       
1144                         IEnumerator enumerator;
1145
1146                         arrayList.AddRange(c_TestData);
1147
1148                         int x;
1149
1150                         // Test with the range 1 - 3
1151
1152                         enumerator = arrayList.GetEnumerator(1, 3);
1153                         
1154                         x = 1;
1155
1156                         while (enumerator.MoveNext()) 
1157                         {
1158                                 Assertion.Assert("enumerator.Current == x", (int)enumerator.Current == x);
1159
1160                                 x++;
1161                         }
1162
1163                         enumerator.Reset();
1164
1165                         x = 1;
1166
1167                         while (enumerator.MoveNext()) 
1168                         {
1169                                 Assertion.Assert("enumerator.Current == x", (int)enumerator.Current == x);
1170
1171                                 x++;
1172                         }
1173
1174
1175                         // Test with a range covering the whole list.
1176
1177                         enumerator = arrayList.GetEnumerator(0, arrayList.Count);
1178                         
1179                         x = 0;
1180
1181                         while (enumerator.MoveNext()) 
1182                         {
1183                                 Assertion.Assert("enumerator.Current == x", (int)enumerator.Current == x);
1184
1185                                 x++;
1186                         }
1187
1188                         enumerator.Reset();
1189
1190                         x = 0;
1191
1192                         while (enumerator.MoveNext()) 
1193                         {
1194                                 Assertion.Assert("enumerator.Current == x", (int)enumerator.Current == x);
1195
1196                                 x++;
1197                         }
1198
1199                         // Test with a range covering nothing.
1200
1201                         enumerator = arrayList.GetEnumerator(arrayList.Count, 0);
1202
1203                         Assertion.Assert("!enumerator.MoveNext()", !enumerator.MoveNext());
1204
1205                         enumerator.Reset();
1206                         
1207                         Assertion.Assert("!enumerator.MoveNext()", !enumerator.MoveNext());
1208                 }
1209
1210                 [Test]
1211                 public void TestEnumeratorWithRangeStandard() 
1212                 {
1213                         PrivateTestEnumeratorWithRange(new ArrayList());
1214                 }
1215
1216                 [Test]
1217                 public void TestEnumeratorWithRangeSynchronized() 
1218                 {
1219                         PrivateTestEnumeratorWithRange(ArrayList.Synchronized(new ArrayList()));
1220                 }
1221
1222                 [Test]
1223                 public void TestEnumeratorWithRangeAdapter() 
1224                 {
1225                         PrivateTestEnumeratorWithRange(ArrayList.Adapter(new ArrayList()));
1226                 }
1227
1228                 [Test]
1229                 public void TestEnumeratorWithRangeGetRange() 
1230                 {
1231                         PrivateTestEnumeratorWithRange(new ArrayList().GetRange(0, 0));
1232                 }
1233
1234                 private void PrivateTestEnumerator(ArrayList arrayList) 
1235                 {
1236                         int x = 0;
1237
1238                         arrayList.AddRange(c_TestData);
1239
1240                         x = 0;
1241
1242                         foreach (object o in arrayList) 
1243                         {
1244                                 if (!o.Equals(x)) 
1245                                 {
1246                                         Assertion.Fail("Arraylist.GetEnumerator()");
1247
1248                                         break;
1249                                 }
1250
1251                                 x++;
1252                         }
1253
1254                         IEnumerator enumerator;
1255
1256                         enumerator = arrayList.GetEnumerator();
1257
1258                         enumerator.MoveNext();
1259
1260                         Assert("enumerator.Current == 0", (int)enumerator.Current == 0);
1261
1262                         // Invalidate the enumerator.
1263
1264                         arrayList.Add(10);
1265                         
1266                         try 
1267                         {
1268                                 // According to the spec this should still work even though the enumerator is invalid.
1269
1270                                 Assert("enumerator.Current == 0", (int)enumerator.Current == 0);
1271                         }
1272                         catch (InvalidOperationException) 
1273                         {
1274                                 Assert("enumerator.Current should not fail.", false);
1275                         }
1276
1277                         try 
1278                         {
1279                                 // This should throw an InvalidOperationException.
1280
1281                                 enumerator.MoveNext();
1282
1283                                 Assert("enumerator.Current should fail.", false);
1284                         }
1285                         catch (InvalidOperationException) 
1286                         {
1287                         }
1288                 }
1289
1290                 [Test]
1291                 public void TestEnumeratorStandard() 
1292                 {
1293                         PrivateTestEnumerator(new ArrayList());
1294                 }
1295
1296                 [Test]
1297                 public void TestEnumeratorSynchronized() 
1298                 {
1299                         PrivateTestEnumerator(ArrayList.Synchronized(new ArrayList()));
1300                 }
1301
1302                 [Test]
1303                 public void TestEnumeratorAdapter() 
1304                 {
1305                         PrivateTestEnumerator(ArrayList.Adapter(new ArrayList()));
1306                 }
1307
1308                 [Test]
1309                 public void TestEnumeratorGetRange() 
1310                 {
1311                         PrivateTestEnumerator(new ArrayList().GetRange(0, 0));
1312                 }
1313                 
1314                 private void PrivateTestReverse(ArrayList arrayList) 
1315                 {                       
1316                         ArrayList arrayList2;
1317                         
1318                         for (int x = 1; x < 100; x ++) 
1319                         {
1320                                 arrayList2 = (ArrayList)arrayList.Clone();
1321                                 
1322                                 for (int i = 0; i < x; i++) 
1323                                 {
1324                                         arrayList2.Add(i);
1325                                 }
1326
1327                                 arrayList2.Reverse();
1328
1329                                 bool ok = true;
1330
1331                                 // Check that reverse did reverse the adapter.
1332
1333                                 for (int i = 0; i < x; i++) 
1334                                 {
1335                                         if ((int)arrayList2[i] != x - i - 1) 
1336                                         {
1337                                                 ok = false;
1338
1339                                                 break;
1340                                         }                               
1341                                 }
1342
1343                                 Assertion.Assert
1344                                         (
1345                                         String.Format("Reverse on arrayList failed on list with {0} items.", x),
1346                                         ok
1347                                         );
1348                         }
1349                 }
1350
1351                 [Test]
1352                 public void TestReverseStandard() 
1353                 {
1354                         PrivateTestReverse(new ArrayList());
1355                 }
1356
1357                 [Test]
1358                 public void TestReverseAdapter() 
1359                 {
1360                         ArrayList arrayList = new ArrayList();
1361                         ArrayList adapter = ArrayList.Adapter(arrayList);
1362
1363                         PrivateTestReverse(adapter);
1364
1365                         VerifyContains(adapter, arrayList, "Changing adapter didn't change ArrayList.");
1366                 }
1367
1368                 [Test]
1369                 public void TestReverseSynchronized() 
1370                 {
1371                         PrivateTestReverse(ArrayList.Synchronized(new ArrayList()));
1372                 }
1373
1374                 [Test]
1375                 public void TestReverseGetRange() 
1376                 {
1377                         PrivateTestReverse(new ArrayList().GetRange(0,0));
1378                 }
1379
1380                 [Test]
1381                 public void TestIterator ()
1382                 {
1383                         ArrayList a = new ArrayList ();
1384                         a.Add (1);
1385                         a.Add (2);
1386                         a.Add (3);
1387
1388                         int total = 0;
1389                         foreach (int b in a)
1390                                 total += b;
1391                         Assert ("Count should be 6", total == 6);
1392                 }
1393
1394                 [Test]
1395                 public void TestIteratorObjects ()
1396                 {
1397                         ArrayList a = new ArrayList ();
1398                         a.Add (1);
1399                         a.Add (null);
1400                         a.Add (3);
1401
1402                         int total = 0;
1403                         int count = 0;
1404                         bool found_null = false;
1405                         foreach (object b in a){
1406                                 count++;
1407                                 if (b == null){
1408                                         if (found_null)
1409                                                 Assert ("Should only find one null", false);
1410                                         found_null = true;
1411                                 } else {
1412                                         total += (int) b;
1413                                 }
1414                         }
1415                         
1416                         Assert ("Should fine one null", found_null);
1417                         Assert ("Total should be 4", total == 4);
1418                         Assert ("Count should be 3", count == 3);
1419                 }
1420         }
1421 }