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