2009-06-10 Gert Driesen <drieseng@users.sourceforge.net>
[mono.git] / mcs / class / corlib / Test / System / ArrayTest.cs
1 // ArrayTest.cs - NUnit Test Cases for the System.Array class
2 //
3 // David Brandt (bucky@keystreams.com)
4 // Eduardo Garcia (kiwnix@yahoo.es)
5 // 
6 // (C) Ximian, Inc.  http://www.ximian.com
7 // Copyright (C) 2004 Novell (http://www.novell.com)
8 // 
9
10 using NUnit.Framework;
11 using System;
12 using System.Collections;
13 using System.Globalization;
14
15 #if NET_2_0
16 using System.Collections.Generic;
17 #endif
18
19 namespace MonoTests.System
20 {
21         //Auxiliary Things
22         enum enua  {hola,adios,mas,menos};
23
24         class AClass
25         {
26                 public AClass()
27                 {
28
29                 }
30         }
31
32         class BClass : AClass
33         {
34         }
35
36         class CClass : AClass
37         {
38         }
39
40         struct AStruct
41         {
42                 public string s;
43                 public string a;
44         }
45         
46         class DataEqual
47         {
48                 public override bool Equals (object obj)
49                 {
50                         return true;
51                 }
52         }
53                 
54         //End Auxiliary Things
55
56 [TestFixture]
57 public class ArrayTest : Assertion
58 {
59         char [] arrsort = {'d', 'b', 'f', 'e', 'a', 'c'};
60
61         public ArrayTest() {}
62
63         [Test]
64         public void TestIsFixedSize() {
65                 char[] a1 = {'a'};
66                 Assert("All arrays are fixed", a1.IsFixedSize);
67         }
68         
69         [Test]
70         public void TestIsReadOnly() {
71                 char[] a1 = {'a'};
72                 Assert("No array is readonly", !a1.IsReadOnly);
73         }
74
75         [Test]
76         public void TestIsSynchronized() {
77                 char[] a1 = {'a'};
78                 Assert("No array is synchronized", !a1.IsSynchronized);
79         }
80
81         [Test]
82         public void TestLength() {
83                 {
84                         char[] a1 = { };
85                         AssertEquals("Zero length array", 0, a1.Length);
86                 }
87                 {
88                         char[] a1 = {'c'};
89                         AssertEquals("One-length array", 1, a1.Length);
90                 }
91                 {
92                         char[] a1 = {'c', 'c'};
93                         AssertEquals("Two-length array", 2, a1.Length);
94                 }
95         }
96
97         [Test]
98         public void TestRank() {
99                 char[] a1 = { 'c', 'd', 'e' };
100                 AssertEquals("Rank one", 1, a1.Rank);
101
102                 char[,] a2 = new Char[3,3];
103                 AssertEquals("Rank two", 2, a2.Rank);
104
105                 char[,,] a3 = new Char[3,3,3];
106                 AssertEquals("Rank three", 3, a3.Rank);
107         }
108
109         [Test]
110         public void TestBinarySearch1() {
111                 bool errorThrown = false;
112                 try {
113                         Array.BinarySearch(null, "blue");
114                 } catch (ArgumentNullException) {
115                         errorThrown = true;
116                 }
117                 Assert("#B01", errorThrown);
118                 errorThrown = false;
119                 try {
120                         char[,] c1 = new Char[2,2];
121                         Array.BinarySearch(c1, "needle");
122                 } catch (RankException) {
123                         errorThrown = true;
124                 }
125                 Assert("#B02", errorThrown);
126
127                 {
128                         char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
129                         Assert("#B05", Array.BinarySearch(arr, 'c') >= 3);
130                         Assert("#B06", Array.BinarySearch(arr, 'c') < 6);
131                 }
132                 {
133                         char[] arr = {'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
134                         AssertEquals("#B07", -4, Array.BinarySearch(arr, 'c'));
135                 }
136                 {
137                         char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
138                         AssertEquals("#B08", -9, Array.BinarySearch(arr, 'e'));
139                 }
140         }
141
142         [Test]
143         public void TestBinarySearch2() {
144                 bool errorThrown = false;
145                 try {
146                         Array.BinarySearch(null, 0, 1, "blue");
147                 } catch (ArgumentNullException) {
148                         errorThrown = true;
149                 }
150                 Assert("#B20", errorThrown);
151                 errorThrown = false;
152                 try {
153                         char[,] c1 = new Char[2,2];
154                         Array.BinarySearch(c1, 0, 1, "needle");
155                 } catch (RankException) {
156                         errorThrown = true;
157                 }
158                 Assert("#B21", errorThrown);
159                 errorThrown = false;
160                 try {
161                         char[] c1 = {'a'};
162                         Array.BinarySearch(c1, -1, 1, 'a');
163                 } catch (ArgumentOutOfRangeException) {
164                         errorThrown = true;
165                 }
166                 Assert("#B22", errorThrown);
167                 errorThrown = false;
168                 try {
169                         char[] c1 = {'a'};
170                         Array.BinarySearch(c1, 0, -1, 'a');
171                 } catch (ArgumentOutOfRangeException) {
172                         errorThrown = true;
173                 }
174                 Assert("#B23", errorThrown);
175                 errorThrown = false;
176                 try {
177                         char[] c1 = {'a'};
178                         Array.BinarySearch(c1, 0, 4, 'a');
179                 } catch (ArgumentException) {
180                         errorThrown = true;
181                 }
182                 Assert("#B24", errorThrown);
183
184                 {
185                         char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
186                         Assert("#B26", Array.BinarySearch(arr, 2, 8, 'c') >= 5);
187                         Assert("#B27", Array.BinarySearch(arr, 2, 8, 'c') < 8);
188                 }
189                 {
190                         char[] arr = {'z', 'z', 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
191                         AssertEquals("#B28", -6, Array.BinarySearch(arr, 2, 8, 'c'));
192                 }
193                 {
194                         char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
195                         AssertEquals("#B29", -11, Array.BinarySearch(arr, 2, 8, 'e'));
196                 }
197         }
198
199         public void TestBinarySearch3()
200         {
201                 int[] array = new int[100];
202
203                 for (int i = 0; i < 100; i++)
204                         array[i] = 10;
205
206                 AssertEquals("#B30", 49, Array.BinarySearch(array, 10));
207         }
208
209         [Test]
210         public void BinarySearch_NullValue () 
211         {
212                 int[] array = new int[1];
213                 AssertEquals ("I=a,o", -1, Array.BinarySearch (array, null));
214                 AssertEquals ("I=a,o,c", -1, Array.BinarySearch (array, null, null));
215                 AssertEquals ("I=a,i,i,o", -1, Array.BinarySearch (array, 0, 1, null));
216                 AssertEquals ("I=a,i,i,o,c", -1, Array.BinarySearch (array, 0, 1, null,null));
217
218                 object[] o = new object [3] { this, this, null };
219                 AssertEquals ("O=a,o", -1, Array.BinarySearch (o, null));
220                 AssertEquals ("O=a,o,c", -1, Array.BinarySearch (o, null, null));
221                 AssertEquals ("O=a,i,i,o", -1, Array.BinarySearch (o, 0, 3, null));
222                 AssertEquals ("O=a,i,i,o,c", -1, Array.BinarySearch (o, 0, 3, null, null));
223         }
224
225         // TODO - testBinarySearch with explicit IComparer args
226
227         [Test]
228         public void TestClear() {
229                 bool errorThrown = false;
230                 try {
231                         Array.Clear(null, 0, 1);
232                 } catch (ArgumentNullException) {
233                         errorThrown = true;
234                 }
235                 Assert("#C01", errorThrown);
236
237                 int[] i1 = { 1, 2, 3, 4 };
238                 {
239                         int[] compare = {1,2,3,4};
240                         AssertEquals("#C02", compare[0], i1[0]);
241                         AssertEquals("#C03", compare[1], i1[1]);
242                         AssertEquals("#C04", compare[2], i1[2]);
243                         AssertEquals("#C05", compare[3], i1[3]);
244                 }
245                 Array.Clear(i1, 3, 1);
246                 {
247                         int[] compare = {1,2,3,0};
248                         AssertEquals("#C06", compare[0], i1[0]);
249                         AssertEquals("#C07", compare[1], i1[1]);
250                         AssertEquals("#C08", compare[2], i1[2]);
251                         AssertEquals("#C09", compare[3], i1[3]);
252                 }
253                 Array.Clear(i1, 1, 1);
254                 {
255                         int[] compare = {1,0,3,0};
256                         AssertEquals("#C10", compare[0], i1[0]);
257                         AssertEquals("#C11", compare[1], i1[1]);
258                         AssertEquals("#C12", compare[2], i1[2]);
259                         AssertEquals("#C13", compare[3], i1[3]);
260                 }
261                 Array.Clear(i1, 1, 3);
262                 {
263                         int[] compare = {1,0,0,0};
264                         AssertEquals("#C14", compare[0], i1[0]);
265                         AssertEquals("#C15", compare[1], i1[1]);
266                         AssertEquals("#C16", compare[2], i1[2]);
267                         AssertEquals("#C17", compare[3], i1[3]);
268                 }
269
270                 string[] s1 = { "red", "green", "blue" };
271                 Array.Clear(s1, 0, 3);
272                 {
273                         string[] compare = {null, null, null};
274                         AssertEquals("#C18", compare[0], s1[0]);
275                         AssertEquals("#C19", compare[1], s1[1]);
276                         AssertEquals("#C20", compare[2], s1[2]);
277                 }
278         }
279
280         [Test]
281         public void TestClone() {
282                 char[] c1 = {'a', 'b', 'c'};
283                 char[] c2 = (char[])c1.Clone();
284                 AssertEquals("#D01", c1[0], c2[0]);
285                 AssertEquals("#D02", c1[1], c2[1]);
286                 AssertEquals("#D03", c1[2], c2[2]);
287
288                 char[] d10 = {'a', 'b'};
289                 char[] d11 = {'a', 'c'};
290                 char[] d12 = {'b', 'c'};
291                 char[][] d1 = {d10, d11, d12};
292                 char[][] d2 = (char[][])d1.Clone();
293                 AssertEquals("#D04", d1[0], d2[0]);
294                 AssertEquals("#D05", d1[1], d2[1]);
295                 AssertEquals("#D06", d1[2], d2[2]);
296
297                 d1[0][0] = 'z';
298                 AssertEquals("#D07", d1[0], d2[0]);
299         }
300
301         [Test] public void TestIndexer ()
302         {
303                 int [] a = new int [10];
304                 IList b = a;
305                 try {
306                         object c = b [-1];
307                         Fail ("IList.this [-1] should throw");
308                 } catch (IndexOutOfRangeException) {
309                         // Good
310                 } catch (Exception){
311                         Fail ("Should have thrown an IndexOutOfRangeException");
312                 }
313         }
314                 
315         [Test]
316         public void TestCopy() {
317                 {
318                         bool errorThrown = false;
319                         try {
320                                 Char[] c1 = {};
321                                 Array.Copy(c1, null, 1);
322                         } catch (ArgumentNullException) {
323                                 errorThrown = true;
324                         }
325                         Assert("#E01", errorThrown);
326                 }
327                 {
328                         bool errorThrown = false;
329                         try {
330                                 Char[] c1 = {};
331                                 Array.Copy(null, c1, 1);
332                         } catch (ArgumentNullException) {
333                                 errorThrown = true;
334                         }
335                         Assert("#E02", errorThrown);
336                 }
337                 {
338                         bool errorThrown = false;
339                         try {
340                                 Char[] c1 = new Char[1];
341                                 Char[,] c2 = new Char[1,1];
342                                 Array.Copy(c1, c2, 1);
343                         } catch (RankException) {
344                                 errorThrown = true;
345                         }
346                         Assert("#E03", errorThrown);
347                 }
348                 {
349                         bool errorThrown = false;
350                         try {
351                                 Char[] c1 = new Char[1];
352                                 string[] s1 = new String[1];
353                                 Array.Copy(c1, s1, 1);
354                         } catch (ArrayTypeMismatchException) {
355                                 errorThrown = true;
356                         }
357                         Assert("#E04", errorThrown);
358                 }
359                 {
360                         bool errorThrown = false;
361                         try {
362                                 Char[] c1 = new Char[1];
363                                 Object[] o1 = new Object[1];
364                                 o1[0] = "hello";
365                                 Array.Copy(o1, c1, 1);
366                         } catch (InvalidCastException) {
367                                 errorThrown = true;
368                         }
369                         Assert("#E05", errorThrown);
370                 }
371                 {
372                         bool errorThrown = false;
373                         try {
374                                 Char[] c1 = new Char[1];
375                                 Char[] c2 = new Char[1];
376                                 Array.Copy(c1, c2, -1);
377                         } catch (ArgumentOutOfRangeException) {
378                                 errorThrown = true;
379                         }
380                         Assert("#E06", errorThrown);
381                 }
382                 {
383                         bool errorThrown = false;
384                         try {
385                                 Char[] c1 = new Char[1];
386                                 Char[] c2 = new Char[2];
387                                 Array.Copy(c1, c2, 2);
388                         } catch (ArgumentException) {
389                                 errorThrown = true;
390                         }
391                         Assert("#E07", errorThrown);
392                 }
393                 {
394                         bool errorThrown = false;
395                         try {
396                                 Char[] c1 = new Char[1];
397                                 Char[] c2 = new Char[2];
398                                 Array.Copy(c2, c1, 2);
399                         } catch (ArgumentException) {
400                                 errorThrown = true;
401                         }
402                         Assert("#E08", errorThrown);
403                 }
404
405                 char[] orig = {'a', 'b', 'd', 'a'};
406                 char[] copy = new Char[4];
407                 Array.Copy(orig, copy, 4);
408                 for (int i = 0; i < orig.Length; i++) {
409                         AssertEquals("#E09(" + i + ")", orig[i], copy[i]);
410                 }
411                 Array.Clear(copy, 0, copy.Length);
412                 for (int i = 0; i < orig.Length; i++) {
413                         AssertEquals("#E10(" + i + ")", (char)0, copy[i]);
414                 }
415                 Array.Copy(orig, copy, 2);
416                 AssertEquals("#E11", orig[0], copy[0]);
417                 AssertEquals("#E12", orig[1], copy[1]);
418                 Assert("#E13", orig[2] != copy[2]);
419                 Assert("#E14", orig[3] != copy[3]);
420         }
421
422         [Test]
423         public void TestCopy2() {
424                 {
425                         bool errorThrown = false;
426                         try {
427                                 Char[] c1 = new Char[2];
428                                 Char[] c2 = new Char[2];
429                                 Array.Copy(c2, 1, c1, 0, 2);
430                         } catch (ArgumentException) {
431                                 errorThrown = true;
432                         }
433                         Assert("#E31", errorThrown);
434                 }
435                 {
436                         bool errorThrown = false;
437                         try {
438                                 Char[] c1 = new Char[2];
439                                 Char[] c2 = new Char[2];
440                                 Array.Copy(c2, 0, c1, 1, 2);
441                         } catch (ArgumentException) {
442                                 errorThrown = true;
443                         }
444                         Assert("#E32", errorThrown);
445                 }
446                 
447                 char[] orig = {'a', 'b', 'd', 'a'};
448                 char[] copy = new Char[4];
449                 Array.Copy(orig, 1, copy, 1, 3);
450                 Assert("#E33", copy[0] != orig[0]);
451                 for (int i = 1; i < orig.Length; i++) {
452                         AssertEquals("#E34(" + i + ")", orig[i], copy[i]);
453                 }
454                 Array.Clear(copy, 0, copy.Length);
455                 Array.Copy(orig, 1, copy, 0, 2);
456                 AssertEquals("#E35", orig[1], copy[0]);
457                 AssertEquals("#E36", orig[2], copy[1]);
458                 Assert("#E37", copy[2] != orig[2]);
459                 Assert("#E38", copy[3] != orig[3]);
460         }
461
462         [Test]
463         [ExpectedException (typeof (InvalidCastException))]
464         public void Copy_InvalidCast () {
465                 object[] arr1 = new object [10];
466                 Type[] arr2 = new Type [10];
467
468                 arr1 [0] = new object ();
469
470                 Array.Copy (arr1, 0, arr2, 0, 10);
471         }
472
473         [Test]
474         public void TestCopyTo() {
475                 {
476                         bool errorThrown = false;
477                         try {
478                                 Char[] c1 = new Char[2];
479                                 c1.CopyTo(null, 2);
480                         } catch (ArgumentNullException) {
481                                 errorThrown = true;
482                         }
483                         Assert("#E61", errorThrown);
484                 }
485                 {
486                         bool errorThrown = false;
487                         try {
488                                 Char[] c1 = new Char[2];
489                                 Char[,] c2 = new Char[2,2];
490                                 c1.CopyTo(c2, 2);
491                         } catch (ArgumentException) {
492                                 errorThrown = true;
493                         }
494 #if TARGET_JVM // This is really implementation dependent behaviour.
495                         catch (RankException) {
496                                 errorThrown = true;
497                         }
498 #endif // TARGET_JVM
499                         Assert("#E62", errorThrown);
500                 }
501                 {
502                         bool errorThrown = false;
503                         try {
504                                 Char[,] c1 = new Char[2,2];
505                                 Char[] c2 = new Char[2];
506                                 c1.CopyTo(c2, -1);
507                         } catch (RankException) {
508                                 errorThrown = true;
509                         }
510                         Assert("#E63", errorThrown);
511                 }
512                 {
513                         bool errorThrown = false;
514                         try {
515                                 Char[,] c1 = new Char[2,2];
516                                 Char[] c2 = new Char[2];
517                                 c1.CopyTo(c2, 2);
518                         } catch (RankException) {
519                                 errorThrown = true;
520                         }
521                         Assert("#E64", errorThrown);
522                 }
523                 {
524                         bool errorThrown = false;
525                         try {
526                                 Char[] c1 = new Char[2];
527                                 Char[] c2 = new Char[2];
528                                 c1.CopyTo(c2, -1);
529                         } catch (ArgumentOutOfRangeException) {
530                                 errorThrown = true;
531                         }
532                         Assert("#E65", errorThrown);
533                 }
534                 {
535                         bool errorThrown = false;
536                         try {
537                                 Char[] c1 = new Char[2];
538                                 Char[] c2 = new Char[2];
539                                 c1.CopyTo(c2, 3);
540                         } catch (ArgumentException) {
541                                 errorThrown = true;
542                         }
543                         Assert("#E66", errorThrown);
544                 }
545                 {
546                         bool errorThrown = false;
547                         try {
548                                 Char[] c1 = new Char[2];
549                                 Char[] c2 = new Char[2];
550                                 c1.CopyTo(c2, 1);
551                         } catch (ArgumentException) {
552                                 errorThrown = true;
553                         }
554                         Assert("#E67", errorThrown);
555                 }
556
557                 {
558                         bool errorThrown = false;
559                         try {
560                                 String[] c1 = new String[2];
561                                 // TODO: this crashes mono if there are null
562                                 // values in the array.
563                                 c1[1] = "hey";
564                                 c1[0] = "you";
565                                 Char[] c2 = new Char[2];
566                                 c2[1] = 'a';
567                                 c2[0] = 'z';
568                                 c1.CopyTo(c2, 0);
569                         } catch (ArrayTypeMismatchException) {
570                                 errorThrown = true;
571                         }
572                         Assert("#E68", errorThrown);
573                 }
574
575                 Char[] orig = {'a', 'b', 'c', 'd'};
576                 Char[] copy = new Char[10];
577                 Array.Clear(copy, 0, copy.Length);
578                 orig.CopyTo(copy, 3);
579                 AssertEquals("#E69", (char)0, copy[0]);
580                 AssertEquals("#E70", (char)0, copy[1]);
581                 AssertEquals("#E71", (char)0, copy[2]);
582                 AssertEquals("#E72", orig[0], copy[3]);
583                 AssertEquals("#E73", orig[1], copy[4]);
584                 AssertEquals("#E74", orig[2], copy[5]);
585                 AssertEquals("#E75", orig[3], copy[6]);
586                 AssertEquals("#E76", (char)0, copy[7]);
587                 AssertEquals("#E77", (char)0, copy[8]);
588                 AssertEquals("#E78", (char)0, copy[9]);
589
590                 {
591                         // The following is valid and must not throw an exception.
592                         bool errorThrown = false;
593                         try {
594                                 int[] src = new int [0];
595                                 int[] dest = new int [0];
596                                 src.CopyTo (dest, 0);
597                         } catch (ArgumentException) {
598                                 errorThrown = true;
599                         }
600                         Assert("#E79", !errorThrown);
601                 }
602
603                 {
604                         // bug #38812
605                         bool errorThrown = false;
606                         try {
607                                 CClass[] src = new CClass [] { new CClass () };
608                                 BClass[] dest = new BClass [1];
609
610                                 src.CopyTo (dest, 0);
611
612                         } catch (ArrayTypeMismatchException) {
613                                 errorThrown = true;
614                         }
615                         Assert("#E80", errorThrown);
616                 }
617         }
618
619         [Test]
620         public void TestCreateInstance() {
621                 {
622                         bool errorThrown = false;
623                         try {
624                                 Array.CreateInstance(null, 12);
625                         } catch (ArgumentNullException) {
626                                 errorThrown = true;
627                         }
628                         Assert("#F01", errorThrown);
629                 }
630                 {
631                         bool errorThrown = false;
632                         try {
633                                 Array.CreateInstance(Type.GetType("System.Char"), -3);
634                         } catch (ArgumentOutOfRangeException) {
635                                 errorThrown = true;
636                         }
637                         Assert("#F02", errorThrown);
638                 }
639                 {
640                         bool errorThrown = false;
641                         try {
642                                 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
643                         } catch (ArgumentNullException) {
644                                 errorThrown = true;
645                         }
646                         Assert("#F03a", errorThrown);
647                 }
648 #if NET_1_1
649                 {
650                         bool errorThrown = false;
651                         try {
652                                 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
653                         } catch (ArgumentNullException) {
654                                 errorThrown = true;
655                         }
656                         Assert("#F03b", errorThrown);
657                 }
658 #endif
659 #if !TARGET_JVM // Arrays lower bounds are not supported for TARGET_JVM
660                 {
661                         bool errorThrown = false;
662                         try {
663                                 Array.CreateInstance(Type.GetType("System.Char"), null, null);
664                         } catch (ArgumentNullException) {
665                                 errorThrown = true;
666                         }
667                         Assert("#F04", errorThrown);
668                 }
669 #endif // TARGET_JVM
670                 {
671                         bool errorThrown = false;
672                         try {
673                                 int[] lengths = new int [0];
674                                 Array.CreateInstance(Type.GetType("System.Char"), lengths);
675                         } catch (ArgumentException) {
676                                 errorThrown = true;
677                         }
678                         Assert("#F05", errorThrown);
679                 }
680 #if !TARGET_JVM // CreateInstance with lower bounds not supported for TARGET_JVM
681                 {
682                         bool errorThrown = false;
683                         try {
684                                 int[] lengths = new int [1];
685                                 int[] bounds = new int [2];
686                                 Array.CreateInstance(Type.GetType("System.Char"), lengths, bounds);
687                                 errorThrown = true;
688                         } catch (ArgumentException) {
689                                 errorThrown = true;
690                         }
691                         Assert("#F06", errorThrown);
692                 }
693
694                 char[] c1 = (char[])Array.CreateInstance(Type.GetType("System.Char"), 12);
695                 AssertEquals("#F07", 12, c1.Length);
696
697                 Array c2 = Array.CreateInstance(Type.GetType("System.Char"), 12, 5);
698                 AssertEquals("#F08", 2, c2.Rank);
699                 AssertEquals("#F09", 60, c2.Length);
700
701
702                 {
703                         int[] lengths = { 3 };
704                         int[] bounds = { 5 };
705                         int[] src = { 512, 718, 912 };
706                         Array array = Array.CreateInstance(typeof(int), lengths, bounds);
707
708                         AssertEquals("#F10", 3, array.Length);
709                         AssertEquals("#F11", 5, array.GetLowerBound(0));
710                         AssertEquals("#F12", 7, array.GetUpperBound(0));
711
712                         src.CopyTo (array, 5);
713
714                         for (int i = 0; i < src.Length; i++)
715                                 AssertEquals("#F13(" + i + ")", src[i], array.GetValue(i+5));
716                 }
717
718                 // Test that a 1 dimensional array with 0 lower bound is the
719                 // same as an szarray
720                 Type szarrayType = new int [10].GetType ();
721                 Assert (szarrayType == (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0})).GetType ());
722                 Assert (szarrayType != (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {1})).GetType ());
723 #endif // TARGET_JVM
724         }
725         
726         [Test]
727         [ExpectedException (typeof (ArgumentNullException))]
728         public void TestCreateInstance2 ()
729         {
730                 Array.CreateInstance (typeof (Int32), (int[])null);
731         }
732
733         [Test]
734 #if NET_2_0
735         [ExpectedException (typeof (ArgumentNullException))]
736 #else
737         [ExpectedException (typeof (NullReferenceException))]
738 #endif
739         public void TestCreateInstance2b ()
740         {
741                 Array.CreateInstance (typeof (Int32), (long[])null);
742         }
743
744         [Test]
745         public void TestGetEnumerator() {
746                 String[] s1 = {"this", "is", "a", "test"};
747                 IEnumerator en = s1.GetEnumerator ();
748                 AssertNotNull ("#G01", en);
749
750                 Assert ("#G02", en.MoveNext ());
751                 AssertEquals ("#G03", "this", en.Current);
752                 Assert ("#G04", en.MoveNext ());
753                 AssertEquals ("#G05", "is", en.Current);
754                 Assert ("#G06", en.MoveNext ());
755                 AssertEquals ("#G07", "a", en.Current);
756                 Assert ("#G08", en.MoveNext ());
757                 AssertEquals ("#G09", "test", en.Current);
758                 Assert ("#G10", !en.MoveNext ());
759
760                 en.Reset ();
761                 Assert("#G11", en.MoveNext ());
762                 AssertEquals ("#G12", "this", en.Current);
763
764                 // mutation does not invalidate array enumerator!
765                 s1.SetValue ("change", 1);
766                 Assert ("#G13", en.MoveNext ());
767                 AssertEquals ("#G14", "change", en.Current);
768         }
769
770         [Test]
771         public void TestGetEnumeratorMultipleDimension() {
772                 String[,] s1 = {{"this", "is"}, {"a", "test"}};
773                 IEnumerator en = s1.GetEnumerator ();
774                 AssertNotNull ("#AA01", en);
775
776                 Assert ("#AA02", en.MoveNext ());
777                 AssertEquals ("#AA03", "this", en.Current);
778                 Assert ("#AA04", en.MoveNext ());
779                 AssertEquals ("#AA05", "is", en.Current);
780                 Assert ("#AA06", en.MoveNext ());
781                 AssertEquals ("#AA07", "a", en.Current);
782                 Assert ("#AA08", en.MoveNext ());
783                 AssertEquals ("#AA09", "test", en.Current);
784                 Assert ("#AA10", !en.MoveNext ());
785
786                 en.Reset ();
787                 Assert("#AA11", en.MoveNext ());
788                 AssertEquals ("#AA12", "this", en.Current);
789
790                 int[] idxs = {0,1};
791                 // mutation does not invalidate array enumerator!
792                 s1.SetValue ("change", idxs);
793                 Assert ("#AA13", en.MoveNext ());
794                 AssertEquals ("#AA14", "change", en.Current);
795         }
796
797         [Test]
798         [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
799         public void TestGetEnumeratorNonZeroLowerBounds() {
800                 int[] myLengthsArray = new int[2] { 3, 5 };
801                 int[] myBoundsArray = new int[2] { 2, 3 };
802
803                 Array myArray=Array.CreateInstance( typeof(String), myLengthsArray, myBoundsArray );
804                 for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
805                         for ( int j = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++ )  {
806                                 int[] myIndicesArray = new int[2] { i, j };
807                                 myArray.SetValue( Convert.ToString(i) + j, myIndicesArray );
808                         }
809                 IEnumerator en = myArray.GetEnumerator ();
810                 AssertNotNull ("#AB01", en);
811
812                 // check the first couple of values
813                 Assert ("#AB02", en.MoveNext ());
814                 AssertEquals ("#AB03", "23", en.Current);
815                 Assert ("#AB04", en.MoveNext ());
816                 AssertEquals ("#AB05", "24", en.Current);
817
818                 // then check the last element's value
819                 string lastElement;
820                 do {  
821                         lastElement = (string)en.Current;
822                 } while (en.MoveNext());
823                 AssertEquals ("#AB06", "47", lastElement);
824         }
825
826         [Test]
827         [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
828         public void TestIList_Add () {
829                 int[] myLengthsArray = new int[2] { 3, 5 };
830                 int[] myBoundsArray = new int[2] { 2, 3 };
831
832                 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
833                 try {
834                         ((IList)myArray).Add ("can not");
835                         Fail ("IList.Add should throw");
836                 }
837                 catch (NotSupportedException) {
838                         return;
839                 }
840                 catch (Exception) {
841                         Fail ("IList.Add threw wrong exception type");
842                 }
843
844                 Fail("IList.Add shouldn't get this far");
845         }
846
847         [Test]
848         [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
849         public void TestIList_Insert () {
850                 int[] myLengthsArray = new int[2] { 3, 5 };
851                 int[] myBoundsArray = new int[2] { 2, 3 };
852
853                 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
854                 try {
855                         ((IList)myArray).Insert (0, "can not");
856                         Fail ("IList.Insert should throw");
857                 }
858                 catch (NotSupportedException) {
859                         return;
860                 }
861                 catch (Exception) {
862                         Fail ("IList.Insert threw wrong exception type");
863                 }
864
865                 Fail("IList.Insert shouldn't get this far");
866         }
867
868         [Test]
869         [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
870         public void TestIList_Remove () {
871                 int[] myLengthsArray = new int[2] { 3, 5 };
872                 int[] myBoundsArray = new int[2] { 2, 3 };
873
874                 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
875                 try {
876                         ((IList)myArray).Remove ("can not");
877                         Fail ("IList.Remove should throw");
878                 }
879                 catch (NotSupportedException) {
880                         return;
881                 }
882                 catch (Exception) {
883                         Fail ("IList.Remove threw wrong exception type");
884                 }
885
886                 Fail("IList.Remove shouldn't get this far");
887         }
888
889         [Test]
890         [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
891         public void TestIList_RemoveAt () {
892                 int[] myLengthsArray = new int[2] { 3, 5 };
893                 int[] myBoundsArray = new int[2] { 2, 3 };
894
895                 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
896                 try {
897                         ((IList)myArray).RemoveAt (0);
898                         Fail ("IList.RemoveAt should throw");
899                 }
900                 catch (NotSupportedException) {
901                         return;
902                 }
903                 catch (Exception) {
904                         Fail ("IList.RemoveAt threw wrong exception type");
905                 }
906
907                 Fail("IList.RemoveAt shouldn't get this far");
908         }
909
910         [Test]
911         [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
912         public void TestIList_Contains () {
913                 int[] myLengthsArray = new int[2] { 3, 5 };
914                 int[] myBoundsArray = new int[2] { 2, 3 };
915
916                 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
917
918                 try {
919                         bool b = ((IList)myArray).Contains ("23");
920                         Fail("IList.Contains should throw with multi-dimensional arrays");
921                 }
922                 catch (RankException) {
923                         int[] iArr = new int[3] { 1, 2, 3};
924                         // check the first and last items
925                         Assert("AC01", ((IList)iArr).Contains (1));
926                         Assert("AC02", ((IList)iArr).Contains (3));
927
928                         // and one that is definately not there
929                         Assert("AC03", !((IList)iArr).Contains (42));
930                         return;
931                 }
932
933                 Fail("Should not get here");
934         }
935
936         [Test]
937         [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
938         public void TestIList_IndexOf () {
939                 int[] myLengthsArray = new int[2] { 3, 5 };
940                 int[] myBoundsArray = new int[2] { 2, 3 };
941
942                 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
943
944                 try {
945                         bool b = ((IList)myArray).Contains ("23");
946                         Fail("IList.Contains should throw with multi-dimensional arrays");
947                 }
948                 catch (RankException) {
949                         int[] iArr = new int[3] { 1, 2, 3};
950                         // check the first and last items
951                         AssertEquals("AD01", 0, ((IList)iArr).IndexOf (1));
952                         AssertEquals("AD02", 2, ((IList)iArr).IndexOf (3));
953
954                         // and one that is definately not there
955                         AssertEquals("AD03", -1, ((IList)iArr).IndexOf (42));
956                 }
957                 catch (Exception e) {
958                         Fail("Unexpected exception: " + e.ToString());
959                 }
960
961                 // check that wierd case whem lowerbound is Int32.MinValue,
962                 // so that IndexOf() needs to return Int32.MaxValue when it cannot find the object
963                 int[] myLengthArray = new int[1] { 3 };
964                 int[] myBoundArray = new int[1] { Int32.MinValue };
965                 Array myExtremeArray=Array.CreateInstance ( typeof(String), myLengthArray, myBoundArray );
966                 AssertEquals("AD04", Int32.MaxValue, ((IList)myExtremeArray).IndexOf (42));
967
968         }
969
970         [Test]
971         public void TestGetLength() {
972                 {
973                         bool errorThrown = false;
974                         try {
975                                 char[] c1 = {'a', 'b', 'c'};
976                                 c1.GetLength(-1);
977                         } catch (IndexOutOfRangeException) {
978                                 errorThrown = true;
979                         }
980                         Assert("#H01", errorThrown);
981                 }
982                 {
983                         bool errorThrown = false;
984                         try {
985                                 char[] c1 = {'a', 'b', 'c'};
986                                 c1.GetLength(1);
987                         } catch (IndexOutOfRangeException) {
988                                 errorThrown = true;
989                         }
990                         Assert("#H02", errorThrown);
991                 }
992
993                 char[] c2 = new Char[5];
994                 AssertEquals("#H03", 5, c2.GetLength(0));
995
996                 char[,] c3 = new Char[6,7];
997                 AssertEquals("#H04", 6, c3.GetLength(0));
998                 AssertEquals("#H05", 7, c3.GetLength(1));
999         }
1000
1001         [Test]
1002         public void TestGetLowerBound() {
1003                 {
1004                         bool errorThrown = false;
1005                         try {
1006                                 char[] c = {'a', 'b', 'c'};
1007                                 c.GetLowerBound(-1);
1008                         } catch (IndexOutOfRangeException) {
1009                                 errorThrown = true;
1010                         }
1011                         Assert("#H31", errorThrown);
1012                 }
1013                 {
1014                         bool errorThrown = false;
1015                         try {
1016                                 char[] c = {'a', 'b', 'c'};
1017                                 c.GetLowerBound(1);
1018                         } catch (IndexOutOfRangeException) {
1019                                 errorThrown = true;
1020                         }
1021                         Assert("#H32", errorThrown);
1022                 }
1023
1024                 char[] c1 = new Char[5];
1025                 AssertEquals("#H33", 0, c1.GetLowerBound(0));
1026
1027                 char[,] c2 = new Char[4,4];
1028                 AssertEquals("#H34", 0, c2.GetLowerBound(0));
1029                 AssertEquals("#H35", 0, c2.GetLowerBound(1));
1030         }
1031
1032         [Test]
1033         public void TestGetUpperBound() {
1034                 {
1035                         bool errorThrown = false;
1036                         try {
1037                                 char[] c = {'a', 'b', 'c'};
1038                                 c.GetUpperBound(-1);
1039                         } catch (IndexOutOfRangeException) {
1040                                 errorThrown = true;
1041                         }
1042                         Assert("#H61", errorThrown);
1043                 }
1044                 {
1045                         bool errorThrown = false;
1046                         try {
1047                                 char[] c = {'a', 'b', 'c'};
1048                                 c.GetUpperBound(1);
1049                         } catch (IndexOutOfRangeException) {
1050                                 errorThrown = true;
1051                         }
1052                         Assert("#H62", errorThrown);
1053                 }
1054
1055                 char[] c1 = new Char[5];
1056                 AssertEquals("#H63", 4, c1.GetUpperBound(0));
1057
1058                 char[,] c2 = new Char[4,6];
1059                 AssertEquals("#H64", 3, c2.GetUpperBound(0));
1060                 AssertEquals("#H65", 5, c2.GetUpperBound(1));
1061         }
1062
1063         [Test]
1064         public void TestGetValue1() {
1065                 {
1066                         bool errorThrown = false;
1067                         try {
1068                                 char[,] c = new Char[2,2];
1069                                 c.GetValue(1);
1070                         } catch (ArgumentException) {
1071                                 errorThrown = true;
1072                         }
1073                         Assert("#I01", errorThrown);
1074                 }
1075                 {
1076                         bool errorThrown = false;
1077                         try {
1078                                 char[] c = {'a', 'b', 'c'};
1079                                 c.GetValue(-1);
1080                         } catch (IndexOutOfRangeException) {
1081                                 errorThrown = true;
1082                         }
1083                         Assert("#I02", errorThrown);
1084                 }
1085                 {
1086                         bool errorThrown = false;
1087                         try {
1088                                 char[] c = {'a', 'b', 'c'};
1089                                 c.GetValue(4);
1090                         } catch (IndexOutOfRangeException) {
1091                                 errorThrown = true;
1092                         }
1093                         Assert("#I03", errorThrown);
1094                 }
1095
1096                 char[] c1 = {'a', 'b', 'c', 'd'};
1097                 for (int i = 0; i < c1.Length; i++) {
1098                         AssertEquals("#I04(" + i + ")", c1[i], c1.GetValue(i));
1099                 }
1100         }
1101
1102         [Test]
1103         public void TestGetValue2() {
1104                 {
1105                         bool errorThrown = false;
1106                         try {
1107                                 char[] c = new Char[2];
1108                                 c.GetValue(1,1);
1109                         } catch (ArgumentException) {
1110                                 errorThrown = true;
1111                         }
1112                         Assert("#I21", errorThrown);
1113                 }
1114                 {
1115                         bool errorThrown = false;
1116                         try {
1117                                 char[,] c = new Char[2,2];
1118                                 c.GetValue(-1, 1);
1119                         } catch (IndexOutOfRangeException) {
1120                                 errorThrown = true;
1121                         }
1122                         Assert("#I22", errorThrown);
1123                 }
1124                 {
1125                         bool errorThrown = false;
1126                         try {
1127                                 char[,] c = new Char[2,2];
1128                                 c.GetValue(4,1);
1129                         } catch (IndexOutOfRangeException) {
1130                                 errorThrown = true;
1131                         }
1132                         Assert("#I23", errorThrown);
1133                 }
1134
1135                 char[,] c1 = new Char[4,6];
1136                 for (int i = 0; i < 24; i++) {
1137                         int first = i / 6;
1138                         int second = i % 6;
1139                         c1[first,second] = (char)(((int)'a')+i);
1140                 }
1141                 for (int i = 0; i < c1.GetLength(0); i++) {
1142                         for (int j = 0; j < c1.GetLength(1); j++) {
1143                                 AssertEquals("#I24(" + i + "," + j + ")",
1144                                         c1[i,j], c1.GetValue(i, j));
1145                         }
1146                 }
1147         }
1148
1149         [Test]
1150         public void TestGetValue3() {
1151                 {
1152                         bool errorThrown = false;
1153                         try {
1154                                 char[] c = new Char[2];
1155                                 c.GetValue(1,1,1);
1156                         } catch (ArgumentException) {
1157                                 errorThrown = true;
1158                         }
1159                         Assert("#I41", errorThrown);
1160                 }
1161                 {
1162                         bool errorThrown = false;
1163                         try {
1164                                 char[,,] c = new Char[2,2,2];
1165                                 c.GetValue(-1, 1, 1);
1166                         } catch (IndexOutOfRangeException) {
1167                                 errorThrown = true;
1168                         }
1169                         Assert("#I42", errorThrown);
1170                 }
1171                 {
1172                         bool errorThrown = false;
1173                         try {
1174                                 char[,,] c = new Char[2,2,2];
1175                                 c.GetValue(4,1,1);
1176                         } catch (IndexOutOfRangeException) {
1177                                 errorThrown = true;
1178                         }
1179                         Assert("#I43", errorThrown);
1180                 }
1181
1182                 char[,,] c1 = new Char[4,2,3];
1183                 for (int i = 0; i < 24; i++) {
1184                         int first = i / 6;
1185                         int remains = i % 6;
1186                         int second = remains / 3;
1187                         int third = remains % 3;
1188                         c1[first,second, third] = (char)(((int)'a')+i);
1189                 }
1190                 for (int i = 0; i < c1.GetLength(0); i++) {
1191                         for (int j = 0; j < c1.GetLength(1); j++) {
1192                                 for (int k = 0; k < c1.GetLength(2); k++) {
1193                                         AssertEquals("#I44(" + i + "," + j + ")",
1194                                                 c1[i,j,k], c1.GetValue(i,j,k));
1195                                 }
1196                         }
1197                 }
1198         }
1199
1200         [Test]
1201 #if NET_2_0
1202         [ExpectedException (typeof (ArgumentNullException))]
1203 #else
1204         [ExpectedException (typeof (NullReferenceException))]
1205 #endif
1206         public void TestGetValueLongArray ()
1207         {
1208                 char[] c = new Char[2];
1209                 c.GetValue((long [])null);
1210         }
1211
1212         [Test]
1213         public void TestGetValueN() {
1214                 {
1215                         bool errorThrown = false;
1216                         try {
1217                                 char[] c = new Char[2];
1218                                 c.GetValue((int [])null);
1219                         } catch (ArgumentNullException) {
1220                                 errorThrown = true;
1221                         }
1222                         Assert("#I61a", errorThrown);
1223                 }
1224                 {
1225                         bool errorThrown = false;
1226                         try {
1227                                 char[] c = new Char[2];
1228                                 int[] coords = {1, 1};
1229                                 c.GetValue(coords);
1230                         } catch (ArgumentException) {
1231                                 errorThrown = true;
1232                         }
1233                         Assert("#I62", errorThrown);
1234                 }
1235                 {
1236                         bool errorThrown = false;
1237                         try {
1238                                 char[,] c = new Char[2,2];
1239                                 int[] coords = {-1, 1};
1240                                 c.GetValue(coords);
1241                         } catch (IndexOutOfRangeException) {
1242                                 errorThrown = true;
1243                         }
1244                         Assert("#I63", errorThrown);
1245                 }
1246                 {
1247                         bool errorThrown = false;
1248                         try {
1249                                 char[,] c = new Char[2,2];
1250                                 int[] coords = {4, 1};
1251                                 c.GetValue(coords);
1252                         } catch (IndexOutOfRangeException) {
1253                                 errorThrown = true;
1254                         }
1255                         Assert("#I64", errorThrown);
1256                 }
1257
1258                 char[,] c1 = new Char[4,6];
1259                 for (int i = 0; i < 24; i++) {
1260                         int first = i / 6;
1261                         int second = i % 6;
1262                         c1[first,second] = (char)(((int)'a')+i);
1263                 }
1264                 for (int i = 0; i < c1.GetLength(0); i++) {
1265                         for (int j = 0; j < c1.GetLength(1); j++) {
1266                                 int[] coords = {i, j};
1267                                 AssertEquals("#I65(" + i + "," + j + ")",
1268                                         c1[i,j], c1.GetValue(coords));
1269                         }
1270                 }
1271         }
1272
1273         [Test]
1274         public void TestIndexOf1() {
1275                 {
1276                         bool errorThrown = false;
1277                         try {
1278                                 Array.IndexOf(null, "huh?");
1279                         } catch (ArgumentNullException) {
1280                                 errorThrown = true;
1281                         }
1282                         Assert("#J01", errorThrown);
1283                 }
1284                 {
1285                         bool errorThrown = false;
1286                         try {
1287                                 char[,] c = new Char[2,2];
1288                                 Array.IndexOf(c, "huh?");
1289                         } catch (RankException) {
1290                                 errorThrown = true;
1291                         }
1292                         Assert("#J02", errorThrown);
1293                 }
1294
1295                 String[] s1 = {"this", "is", "a", "test"};
1296                 AssertEquals("#J03", -1, Array.IndexOf(s1, null));
1297                 AssertEquals("#J04", -1, Array.IndexOf(s1, "nothing"));
1298                 AssertEquals("#J05", 0, Array.IndexOf(s1, "this"));
1299                 AssertEquals("#J06", 3, Array.IndexOf(s1, "test"));
1300         }
1301
1302         [Test]
1303         public void TestIndexOf2() {
1304                 {
1305                         bool errorThrown = false;
1306                         try {
1307                                 Array.IndexOf(null, "huh?", 0);
1308                         } catch (ArgumentNullException) {
1309                                 errorThrown = true;
1310                         }
1311                         Assert("#J21", errorThrown);
1312                 }
1313                 {
1314                         bool errorThrown = false;
1315                         try {
1316                                 char[,] c = new Char[2,2];
1317                                 Array.IndexOf(c, "huh?", 0);
1318                         } catch (RankException) {
1319                                 errorThrown = true;
1320                         }
1321                         Assert("#J22", errorThrown);
1322                 }
1323                 {
1324                         bool errorThrown = false;
1325                         try {
1326                                 char[] c = new Char[2];
1327                                 Array.IndexOf(c, "huh?", 3);
1328                         } catch (ArgumentOutOfRangeException) {
1329                                 errorThrown = true;
1330                         }
1331                         Assert("#J23", errorThrown);
1332                 }
1333
1334                 String[] s1 = {"this", "is", "really", "a", "test"};
1335                 AssertEquals("#J24", -1, Array.IndexOf(s1, null, 1));
1336                 AssertEquals("#J25", -1, Array.IndexOf(s1, "nothing", 1));
1337                 AssertEquals("#J26", -1, Array.IndexOf(s1, "this", 1));
1338                 AssertEquals("#J27", 1, Array.IndexOf(s1, "is", 1));
1339                 AssertEquals("#J28", 4, Array.IndexOf(s1, "test", 1));
1340         }
1341
1342         [Test]
1343         public void TestIndexOf3() {
1344                 {
1345                         bool errorThrown = false;
1346                         try {
1347                                 Array.IndexOf(null, "huh?", 0, 1);
1348                         } catch (ArgumentNullException) {
1349                                 errorThrown = true;
1350                         }
1351                         Assert("#J41", errorThrown);
1352                 }
1353                 {
1354                         bool errorThrown = false;
1355                         try {
1356                                 char[,] c = new Char[2,2];
1357                                 Array.IndexOf(c, "huh?", 0, 1);
1358                         } catch (RankException) {
1359                                 errorThrown = true;
1360                         }
1361                         Assert("#J42", errorThrown);
1362                 }
1363                 {
1364                         bool errorThrown = false;
1365                         try {
1366                                 char[] c = new Char[2];
1367                                 Array.IndexOf(c, "huh?", 3, 1);
1368                         } catch (ArgumentOutOfRangeException) {
1369                                 errorThrown = true;
1370                         }
1371                         Assert("#J43", errorThrown);
1372                 }
1373                 {
1374                         bool errorThrown = false;
1375                         try {
1376                                 char[] c = new Char[2];
1377                                 Array.IndexOf(c, "huh?", 0, 5);
1378                         } catch (ArgumentOutOfRangeException) {
1379                                 errorThrown = true;
1380                         }
1381                         Assert("#J44", errorThrown);
1382                 }
1383
1384                 String[] s1 = {"this", "is", "really", "a", "test"};
1385                 AssertEquals("#J45", -1, Array.IndexOf(s1, null, 1, 3));
1386                 AssertEquals("#J46", -1, Array.IndexOf(s1, "nothing", 1, 3));
1387                 AssertEquals("#J47", -1, Array.IndexOf(s1, "this", 1, 3));
1388                 AssertEquals("#J48", 1, Array.IndexOf(s1, "is", 1, 3));
1389                 AssertEquals("#J49", -1, Array.IndexOf(s1, "test", 1, 3));
1390                 AssertEquals("#J50", 3, Array.IndexOf(s1, "a", 1, 3));
1391         }
1392         
1393         [Test]
1394         public void TestIndexOf_CustomEqual ()
1395         {
1396                 DataEqual[] test = new DataEqual [] { new DataEqual () };
1397                 AssertEquals (0, Array.IndexOf (test, "asdfas", 0));
1398                 
1399                 IList array = (IList)test;
1400                 AssertEquals (0, array.IndexOf ("asdfas"));
1401         }
1402         
1403         [Test]
1404         public void TestLastIndexOf1() {
1405                 {
1406                         bool errorThrown = false;
1407                         try {
1408                                 Array.LastIndexOf(null, "huh?");
1409                         } catch (ArgumentNullException) {
1410                                 errorThrown = true;
1411                         }
1412                         Assert("#K01", errorThrown);
1413                 }
1414                 {
1415                         bool errorThrown = false;
1416                         try {
1417                                 char[,] c = new Char[2,2];
1418                                 Array.LastIndexOf(c, "huh?");
1419                         } catch (RankException) {
1420                                 errorThrown = true;
1421                         }
1422                         Assert("#K02", errorThrown);
1423                 }
1424
1425                 String[] s1 = {"this", "is", "a", "a", "test"};
1426                 AssertEquals("#K03", -1, Array.LastIndexOf(s1, null));
1427                 AssertEquals("#K04", -1, Array.LastIndexOf(s1, "nothing"));
1428                 AssertEquals("#K05", 0, Array.LastIndexOf(s1, "this"));
1429                 AssertEquals("#K06", 4, Array.LastIndexOf(s1, "test"));
1430                 AssertEquals("#K07", 3, Array.LastIndexOf(s1, "a"));
1431
1432                 AssertEquals (-1, Array.LastIndexOf (new String [0], "foo"));
1433         }
1434
1435         [Test]
1436         public void TestLastIndexOf2() {
1437                 {
1438                         bool errorThrown = false;
1439                         try {
1440                                 Array.LastIndexOf(null, "huh?", 0);
1441                         } catch (ArgumentNullException) {
1442                                 errorThrown = true;
1443                         }
1444                         Assert("#K21", errorThrown);
1445                 }
1446                 {
1447                         bool errorThrown = false;
1448                         try {
1449                                 char[,] c = new Char[2,2];
1450                                 Array.LastIndexOf(c, "huh?", 0);
1451                         } catch (RankException) {
1452                                 errorThrown = true;
1453                         }
1454                         Assert("#K22", errorThrown);
1455                 }
1456                 {
1457                         bool errorThrown = false;
1458                         try {
1459                                 char[] c = new Char[2];
1460                                 Array.LastIndexOf(c, "huh?", 3);
1461                         } catch (ArgumentOutOfRangeException) {
1462                                 errorThrown = true;
1463                         }
1464                         Assert("#K23", errorThrown);
1465                 }
1466
1467                 String[] s1 = {"this", "is", "really", "a", "test"};
1468                 AssertEquals("#K24", -1, Array.LastIndexOf(s1, null, 3));
1469                 AssertEquals("#K25", -1, Array.LastIndexOf(s1, "nothing", 3));
1470                 AssertEquals("#K26", -1, Array.LastIndexOf(s1, "test", 3));
1471                 AssertEquals("#K27", 3, Array.LastIndexOf(s1, "a", 3));
1472                 AssertEquals("#K28", 0, Array.LastIndexOf(s1, "this", 3));
1473         }
1474
1475         [Test]
1476         public void TestLastIndexOf3() {
1477                 {
1478                         bool errorThrown = false;
1479                         try {
1480                                 Array.LastIndexOf(null, "huh?", 0, 1);
1481                         } catch (ArgumentNullException) {
1482                                 errorThrown = true;
1483                         }
1484                         Assert("#K41", errorThrown);
1485                 }
1486                 {
1487                         bool errorThrown = false;
1488                         try {
1489                                 char[,] c = new Char[2,2];
1490                                 Array.LastIndexOf(c, "huh?", 0, 1);
1491                         } catch (RankException) {
1492                                 errorThrown = true;
1493                         }
1494                         Assert("#K42", errorThrown);
1495                 }
1496                 {
1497                         bool errorThrown = false;
1498                         try {
1499                                 char[] c = new Char[2];
1500                                 Array.LastIndexOf(c, "huh?", 3, 1);
1501                         } catch (ArgumentOutOfRangeException) {
1502                                 errorThrown = true;
1503                         }
1504                         Assert("#K43", errorThrown);
1505                 }
1506                 {
1507                         bool errorThrown = false;
1508                         try {
1509                                 char[] c = new Char[2];
1510                                 Array.LastIndexOf(c, "huh?", 0, 5);
1511                         } catch (ArgumentOutOfRangeException) {
1512                                 errorThrown = true;
1513                         }
1514                         Assert("#K44", errorThrown);
1515                 }
1516
1517                 String[] s1 = {"this", "is", "really", "a", "test"};
1518                 AssertEquals("#K45", -1, Array.LastIndexOf(s1, null, 3, 3));
1519                 AssertEquals("#K46", -1, Array.LastIndexOf(s1, "nothing", 3, 3));
1520                 AssertEquals("#K47", -1, Array.LastIndexOf(s1, "this", 3, 3));
1521                 AssertEquals("#K48", 1, Array.LastIndexOf(s1, "is", 3, 3));
1522                 AssertEquals("#K49", -1, Array.LastIndexOf(s1, "test", 3, 3));
1523                 AssertEquals("#K50", 3, Array.LastIndexOf(s1, "a", 3, 3));
1524         }
1525
1526         [Test]
1527         public void TestLastIndexOf4 ()
1528         {
1529                 short [] a = new short [] { 19, 238, 317, 6, 565, 0, -52, 60, -563, 753, 238, 238};
1530                 try {
1531                         Array.LastIndexOf (a, (object)16, -1);
1532                         NUnit.Framework.Assert.Fail ("#1");
1533                 } catch (ArgumentOutOfRangeException) { }
1534                 
1535 #if NET_2_0             
1536                 try {
1537                         Array.LastIndexOf<short> (a, 16, -1);
1538                         NUnit.Framework.Assert.Fail ("#2");
1539                 } catch (ArgumentOutOfRangeException) { }
1540 #endif          
1541         }
1542
1543         [Test]
1544         public void TestLastIndexOf5 ()
1545         {
1546                 char [] a = new char [] {'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'j', 'i', 'h'};
1547                 string s;
1548                 int retval;
1549                 bool error = false;
1550
1551                 for (int i = a.Length - 1; i >= 0 ; i--) {
1552                         s = i.ToString ();
1553                         retval = Array.LastIndexOf(a, a [i], i, i + 1);
1554                         if (retval != i)
1555                                 error = true;
1556                 }
1557                 Assert (!error);
1558         }
1559
1560         [Test]
1561         [ExpectedException (typeof (ArgumentOutOfRangeException))]
1562         public void LastIndexOf_StartIndexOverflow ()
1563         {
1564                 // legal - no exception
1565                 byte[] array = new byte [16];
1566                 Array.LastIndexOf (array, this, Int32.MaxValue, 1);
1567         }
1568
1569         [Test]
1570         [ExpectedException (typeof (ArgumentOutOfRangeException))]
1571         public void LastIndexOf_CountOverflow ()
1572         {
1573                 // legal - no exception
1574                 byte[] array = new byte [16];
1575                 Array.LastIndexOf (array, this, 1, Int32.MaxValue);
1576         }
1577
1578         [Test]
1579         public void TestReverse() {
1580                 {
1581                         bool errorThrown = false;
1582                         try {
1583                                 Array.Reverse(null);
1584                         } catch (ArgumentNullException) {
1585                                 errorThrown = true;
1586                         }
1587                         Assert("#L01", errorThrown);
1588                 }
1589                 {
1590                         bool errorThrown = false;
1591                         try {
1592                                 char[,] c = new Char[2,2];
1593                                 Array.Reverse(c);
1594                         } catch (RankException) {
1595                                 errorThrown = true;
1596                         }
1597                         Assert("#L02", errorThrown);
1598                 }
1599                 
1600                 char[] c1 = {'a', 'b', 'c', 'd'};
1601                 Array.Reverse(c1);
1602                 AssertEquals("#L03", 'd', c1[0]);
1603                 AssertEquals("#L04", 'c', c1[1]);
1604                 AssertEquals("#L05", 'b', c1[2]);
1605                 AssertEquals("#L06", 'a', c1[3]);
1606
1607                 {
1608                         bool errorThrown = false;
1609                         try {
1610                                 Array.Reverse(null, 0, 0);
1611                         } catch (ArgumentNullException) {
1612                                 errorThrown = true;
1613                         }
1614                         Assert("#L07", errorThrown);
1615                 }
1616                 {
1617                         bool errorThrown = false;
1618                         try {
1619                                 char[,] c = new Char[2,2];
1620                                 Array.Reverse(c, 0, 0);
1621                         } catch (RankException) {
1622                                 errorThrown = true;
1623                         }
1624                         Assert("#L08", errorThrown);
1625                 }
1626                 //{
1627                 //bool errorThrown = false;
1628                 //try {
1629                 //      char[] c = new Char[2];
1630                 //      Array.Reverse(c, 0, 3);
1631                 //} catch (ArgumentOutOfRangeException) {
1632                 //      errorThrown = true;
1633                 //}
1634                 //Assert("#L09", errorThrown);
1635                 //}
1636                 //{
1637                 //bool errorThrown = false;
1638                 //try {
1639                 //      char[] c = new Char[2];
1640                 //      Array.Reverse(c, 3, 0);
1641                 //} catch (ArgumentOutOfRangeException) {
1642                 //      errorThrown = true;
1643                 //}
1644                 //Assert("#L10", errorThrown);
1645                 //}
1646
1647                 char[] c2 = { 'a', 'b', 'c', 'd'};
1648                 Array.Reverse(c2, 1, 2);
1649                 AssertEquals("#L11", 'a', c2[0]);
1650                 AssertEquals("#L12", 'c', c2[1]);
1651                 AssertEquals("#L13", 'b', c2[2]);
1652                 AssertEquals("#L14", 'd', c2[3]);
1653         }
1654
1655         [Test]
1656         public void TestSetValue1() {
1657                 {
1658                         bool errorThrown = false;
1659                         try {
1660                                 char[,] c = new Char[2,2];
1661                                 c.SetValue("buh", 1);
1662                         } catch (ArgumentException) {
1663                                 errorThrown = true;
1664                         }
1665                         Assert("#M01", errorThrown);
1666                 }
1667                 {
1668                         bool errorThrown = false;
1669                         try {
1670                                 char[] c = {'a', 'b', 'c'};
1671                                 c.SetValue("buh", -1);
1672                         } catch (IndexOutOfRangeException) {
1673                                 errorThrown = true;
1674                         }
1675                         Assert("#M02", errorThrown);
1676                 }
1677                 {
1678                         bool errorThrown = false;
1679                         try {
1680                                 char[] c = {'a', 'b', 'c'};
1681                                 c.SetValue("buh", 4);
1682                         } catch (IndexOutOfRangeException) {
1683                                 errorThrown = true;
1684                         }
1685                         Assert("#M03", errorThrown);
1686                 }
1687
1688                 char[] c1 = {'a', 'b', 'c', 'd'};
1689                 char[] c2 = new char[4];
1690                 for (int i = 0; i < c1.Length; i++) {
1691                         c2.SetValue(c1[i], i);
1692                 }
1693                 for (int i = 0; i < c1.Length; i++) {
1694                         AssertEquals("#M04(" + i + ")", c1[i], c2[i]);
1695                 }
1696
1697                 int[] c3 = { 1, 2, 3 };
1698                 long[] c4 = new long [3];
1699
1700                 for (int i = 0; i < c3.Length; i++)
1701                         c4.SetValue (c3 [i], i);
1702
1703                 try {
1704                         c3.CopyTo (c4, 0);
1705                 } catch (Exception e) {
1706                         Fail ("c3.CopyTo(): e=" + e);
1707                 }
1708                 for (int i = 0; i < c3.Length; i++)
1709                         Assert ("#M05(" + i + ")", c3[i] == c4[i]);
1710
1711                 Object[] c5 = new Object [3];
1712                 long[] c6 = new long [3];
1713
1714                 try {
1715                         c4.CopyTo (c5, 0);
1716                 } catch (Exception e) {
1717                         Fail ("c4.CopyTo(): e=" + e);
1718                 }
1719
1720                 try {
1721                         c5.CopyTo (c6, 0);
1722                 } catch (Exception e) {
1723                         Fail ("c5.CopyTo(): e=" + e);
1724                 }
1725                 // for (int i = 0; i < c5.Length; i++)
1726                 // Assert ("#M06(" + i + ")", c5[i] == c6[i]);
1727         }
1728
1729         [Test]
1730         public void TestSetValue2() {
1731                 {
1732                         bool errorThrown = false;
1733                         try {
1734                                 char[] c = new Char[2];
1735                                 c.SetValue("buh", 1,1);
1736                         } catch (ArgumentException) {
1737                                 errorThrown = true;
1738                         }
1739                         Assert("#M21", errorThrown);
1740                 }
1741                 {
1742                         bool errorThrown = false;
1743                         try {
1744                                 char[,] c = new Char[2,2];
1745                                 c.SetValue("buh", -1, 1);
1746                         } catch (IndexOutOfRangeException) {
1747                                 errorThrown = true;
1748                         }
1749                         Assert("#M22", errorThrown);
1750                 }
1751                 {
1752                         bool errorThrown = false;
1753                         try {
1754                                 char[,] c = new Char[2,2];
1755                                 c.SetValue("buh", 4,1);
1756                         } catch (IndexOutOfRangeException) {
1757                                 errorThrown = true;
1758                         }
1759                         Assert("#M23", errorThrown);
1760                 }
1761
1762                 char[,] c1 = new Char[4,6];
1763                 char[,] c2 = new Char[4,6];
1764                 for (int i = 0; i < 24; i++) {
1765                         int first = i / 6;
1766                         int second = i % 6;
1767                         c1[first,second] = (char)(((int)'a')+i);
1768                         c2.SetValue(c1[first,second], first, second);
1769                 }
1770                 for (int i = 0; i < c1.GetLength(0); i++) {
1771                         for (int j = 0; j < c1.GetLength(1); j++) {
1772                                 AssertEquals("#M24(" + i + "," + j + ")",
1773                                         c1[i,j], c2[i, j]);
1774                         }
1775                 }
1776         }
1777
1778         [Test]
1779         public void TestSetValue3() {
1780                 {
1781                         bool errorThrown = false;
1782                         try {
1783                                 char[] c = new Char[2];
1784                                 c.SetValue("buh", 1,1,1);
1785                         } catch (ArgumentException) {
1786                                 errorThrown = true;
1787                         }
1788                         Assert("#M41", errorThrown);
1789                 }
1790                 {
1791                         bool errorThrown = false;
1792                         try {
1793                                 char[,,] c = new Char[2,2,2];
1794                                 c.SetValue("buh", -1, 1, 1);
1795                         } catch (IndexOutOfRangeException) {
1796                                 errorThrown = true;
1797                         }
1798                         Assert("#M42", errorThrown);
1799                 }
1800                 {
1801                         bool errorThrown = false;
1802                         try {
1803                                 char[,,] c = new Char[2,2,2];
1804                                 c.SetValue("buh", 4,1,1);
1805                         } catch (IndexOutOfRangeException) {
1806                                 errorThrown = true;
1807                         }
1808                         Assert("#M43", errorThrown);
1809                 }
1810
1811                 char[,,] c1 = new Char[4,2,3];
1812                 char[,,] c2 = new Char[4,2,3];
1813                 for (int i = 0; i < 24; i++) {
1814                         int first = i / 6;
1815                         int remains = i % 6;
1816                         int second = remains / 3;
1817                         int third = remains % 3;
1818                         c1[first,second, third] = (char)(((int)'a')+i);
1819                         c2.SetValue(c1[first, second, third], first, second, third);
1820                 }
1821                 for (int i = 0; i < c1.GetLength(0); i++) {
1822                         for (int j = 0; j < c1.GetLength(1); j++) {
1823                                 for (int k = 0; k < c1.GetLength(2); k++) {
1824                                         AssertEquals("#M44(" + i + "," + j + " )",
1825                                                 c1[i,j,k], c2[i,j,k]);
1826                                 }
1827                         }
1828                 }
1829         }
1830
1831         [Test]
1832 #if NET_2_0
1833         [ExpectedException (typeof (ArgumentNullException))]
1834 #else
1835         [ExpectedException (typeof (NullReferenceException))]
1836 #endif
1837         public void TestSetValueLongArray ()
1838         {
1839                 char[] c = new Char[2];
1840                 c.SetValue("buh", (long [])null);
1841         }
1842
1843         [Test]
1844         public void TestSetValueN() {
1845                 {
1846                         bool errorThrown = false;
1847                         try {
1848                                 char[] c = new Char[2];
1849                                 c.SetValue("buh", (int [])null);
1850                         } catch (ArgumentNullException) {
1851                                 errorThrown = true;
1852                         }
1853                         Assert("#M61a", errorThrown);
1854                 }
1855                 {
1856                         bool errorThrown = false;
1857                         try {
1858                                 char[] c = new Char[2];
1859                                 int[] coords = {1, 1};
1860                                 c.SetValue("buh", coords);
1861                         } catch (ArgumentException) {
1862                                 errorThrown = true;
1863                         }
1864                         Assert("#M62", errorThrown);
1865                 }
1866                 {
1867                         bool errorThrown = false;
1868                         try {
1869                                 char[,] c = new Char[2,2];
1870                                 int[] coords = {-1, 1};
1871                                 c.SetValue("buh", coords);
1872                         } catch (IndexOutOfRangeException) {
1873                                 errorThrown = true;
1874                         }
1875                         Assert("#M63", errorThrown);
1876                 }
1877                 {
1878                         bool errorThrown = false;
1879                         try {
1880                                 char[,] c = new Char[2,2];
1881                                 int[] coords = {4, 1};
1882                                 c.SetValue("buh", coords);
1883                         } catch (IndexOutOfRangeException) {
1884                                 errorThrown = true;
1885                         }
1886                         Assert("#M64", errorThrown);
1887                 }
1888
1889                 char[,] c1 = new Char[4,6];
1890                 char[,] c2 = new Char[4,6];
1891                 for (int i = 0; i < 24; i++) {
1892                         int first = i / 6;
1893                         int second = i % 6;
1894                         c1[first,second] = (char)(((int)'a')+i);
1895                         int[] coords = {first, second};
1896                         c2.SetValue(c1[first,second], coords);
1897                 }
1898                 for (int i = 0; i < c1.GetLength(0); i++) {
1899                         for (int j = 0; j < c1.GetLength(1); j++) {
1900                                 AssertEquals("#M65(" + i + "," + j + ")",
1901                                         c1[i,j], c2[i,j]);
1902                         }
1903                 }
1904         }
1905
1906         [Test]
1907         public void TestSetValue4() {
1908                 {
1909                         int[] c1 = { 1, 2, 3 };
1910                         long[] c2 = new long [3];
1911
1912                         for (int i = 0; i < c1.Length; i++)
1913                                 c2.SetValue (c1 [i], i);
1914
1915                         for (int i = 0; i < c1.Length; i++) {
1916                                 Assert ("#M81(" + i + ")", c1[i] == c2[i]);
1917                                 AssertEquals ("#M82(" + i + ")", typeof (long), c2[i].GetType ());
1918                         }
1919                 }
1920                 {
1921                         long[] c1 = { 1, 2, 3 };
1922                         int[] c2 = new int [3];
1923                         bool errorThrown = false;
1924                         try {
1925                                 c2.SetValue (c1 [0], 0);
1926                         } catch (ArgumentException) {
1927                                 errorThrown = true;
1928                         }
1929                         Assert("#M83", errorThrown);
1930                 }
1931                 {
1932                         int[] c1 = { 1, 2, 3 };
1933                         Object[] c2 = new Object [3];
1934
1935                         for (int i = 0; i < c1.Length; i++)
1936                                 c2.SetValue (c1 [i], i);
1937
1938                         for (int i = 0; i < c1.Length; i++)
1939                                 AssertEquals ("#M84(" + i + ")", c1[i], Convert.ToInt32 (c2[i]));
1940                 }
1941                 {
1942                         Object[] c1 = new Object [3];
1943                         Object[] c2 = new Object [3];
1944                         c1[0] = new Object ();
1945
1946                         for (int i = 0; i < c1.Length; i++)
1947                                 c2.SetValue (c1 [i], i);
1948
1949                         for (int i = 0; i < c1.Length; i++)
1950                                 AssertEquals ("#M85(" + i + ")", c1[i], c2[i]);
1951                 }
1952                 {
1953                         Object[] c1 = new Object [3];
1954                         string[] c2 = new String [3];
1955                         string test = "hello";
1956                         c1[0] = test;
1957
1958                         c2.SetValue (c1 [0], 0);
1959                         AssertEquals ("#M86", c1[0], c2[0]);
1960                         AssertEquals ("#M87", "hello", c2[0]);
1961                 }
1962                 {
1963                         char[] c1 = { 'a', 'b', 'c' };
1964                         string[] c2 = new string [3];
1965                         try {
1966                                 c2.SetValue (c1 [0], 0);
1967                                 Fail ("#M88");
1968                         } catch (InvalidCastException) {}
1969                 }
1970                 {
1971                         Single[] c1 = { 1.2F, 2.3F, 3.4F, 4.5F };
1972                         long[] c2 = new long [3];
1973                         try {
1974                                 c2.SetValue (c1 [0], 0);
1975                                 Fail ("#M89");
1976                         } catch (ArgumentException) {}
1977                 }
1978                 {
1979                         Type[] types = {
1980                                 typeof (Boolean),
1981                                 typeof (Byte),
1982                                 typeof (Char),
1983                                 typeof (Double),
1984                                 typeof (Int16),
1985                                 typeof (Int32),
1986                                 typeof (Int64),
1987                                 typeof (SByte),
1988                                 typeof (Single),
1989                                 typeof (UInt16),
1990                                 typeof (UInt32),
1991                                 typeof (UInt64)
1992                         };
1993
1994                         bool v1 = true;
1995                         Byte v2 = 1;
1996                         Char v3 = 'a';
1997                         Double v4 = -1.2;
1998                         Int16 v5 = -32;
1999                         Int32 v6 = -234;
2000                         Int64 v7 = -34523;
2001                         SByte v8 = -1;
2002                         Single v9 = -4.8F;
2003                         UInt16 v10 = 24234;
2004                         UInt32 v11 = 235354;
2005                         UInt64 v12 = 234552;
2006
2007                         Object[] va1 = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12 };
2008                         Object[] va2 = { "true", "1", "a", "-1.2", "-32", "-234", "-34523", "-1",
2009                                          "-4.8F", "24234", "235354", "234552" };
2010
2011                         Object[][] vt = { va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1 };
2012
2013                         int[] arg_ex = {
2014                                 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2015                                 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
2016                                 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
2017                                 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
2018                                 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1,
2019                                 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1,
2020                                 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,
2021                                 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1,
2022                                 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1,
2023                                 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
2024                                 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0,
2025                                 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0
2026                         };
2027
2028                         // SetValue
2029
2030                         for (int i = 0; i < types.Length; i++) {
2031                                 for (int j = 0; j < types.Length; j++) {
2032                                         Array array = Array.CreateInstance (types [j], 2);
2033
2034                                         Object value = vt[j][i];
2035
2036                                         bool errorThrown = false;
2037                                         try {
2038                                                 array.SetValue (value, 0);
2039                                         } catch (ArgumentException) {
2040                                                 errorThrown = true;
2041                                         }
2042
2043                                         int ex_index = (i * types.Length) + j;
2044
2045                                         AssertEquals ("#M90(" + types [i] + "," + types [j] + ")",
2046                                                 errorThrown, arg_ex [ex_index] == 1);
2047                                 }
2048                         }
2049
2050                         for (int i = 0; i < types.Length; i++) {
2051                                 String[] array = new String [2];
2052
2053                                 Object value = va1 [i];
2054
2055                                 bool errorThrown = false;
2056                                 try {
2057                                         array.SetValue (value, 0);
2058                                 } catch (InvalidCastException) {
2059                                         errorThrown = true;
2060                                 }
2061
2062                                 Assert ("#M91(" + types [i] + ")", errorThrown);
2063                         }
2064
2065                         for (int i = 0; i < types.Length; i++) {
2066                                 Array array = Array.CreateInstance (types [i], 2);
2067
2068                                 Object value = va2 [i];
2069
2070                                 bool errorThrown = false;
2071                                 try {
2072                                         array.SetValue (value, 0);
2073                                 } catch (InvalidCastException) {
2074                                         errorThrown = true;
2075                                 }
2076
2077                                 Assert ("#M92(" + types [i] + ")", errorThrown);
2078                         }
2079
2080                         for (int i = 0; i < types.Length; i++) {
2081                                 Array array = Array.CreateInstance (types [i], 2);
2082
2083                                 Object value = null;
2084
2085                                 bool errorThrown = false;
2086                                 try {
2087                                         array.SetValue (value, 0);
2088                                 } catch (InvalidCastException) {
2089                                         errorThrown = true;
2090                                 }
2091
2092                                 Assert ("#M93(" + types [i] + ")", !errorThrown);
2093                         }
2094
2095                         // Copy
2096
2097                         for (int i = 0; i < types.Length; i++) {
2098                                 for (int j = 0; j < types.Length; j++) {
2099                                         Array source = Array.CreateInstance (types [i], 2);
2100                                         Array array = Array.CreateInstance (types [j], 2);
2101
2102                                         source.SetValue (vt[j][i], 0);
2103                                         source.SetValue (vt[j][i], 1);
2104
2105                                         bool errorThrown = false;
2106                                         try {
2107                                                 Array.Copy (source, array, 2);
2108                                         } catch (ArrayTypeMismatchException) {
2109                                                 errorThrown = true;
2110                                         }
2111
2112                                         int ex_index = (i * types.Length) + j;
2113
2114                                         AssertEquals ("#M94(" + types [i] + "," + types [j] + ")",
2115                                                 errorThrown, arg_ex [ex_index] == 1);
2116                                 }
2117                         }
2118
2119                         for (int i = 0; i < types.Length; i++) {
2120                                 Array source = Array.CreateInstance (types [i], 2);
2121                                 String[] array = new String [2];
2122
2123                                 source.SetValue (va1 [i], 0);
2124                                 source.SetValue (va1 [i], 1);
2125
2126                                 bool errorThrown = false;
2127                                 try {
2128                                         Array.Copy (source, array, 2);
2129                                 } catch (ArrayTypeMismatchException) {
2130                                         errorThrown = true;
2131                                 }
2132
2133                                 Assert ("#M95(" + types [i] + ")", errorThrown);
2134                         }
2135
2136                         for (int i = 0; i < types.Length; i++) {
2137                                 String[] source = new String [2];
2138                                 Array array = Array.CreateInstance (types [i], 2);
2139
2140                                 source.SetValue (va2 [i], 0);
2141                                 source.SetValue (va2 [i], 1);
2142
2143                                 bool errorThrown = false;
2144                                 try {
2145                                         Array.Copy (source, array, 2);
2146                                 } catch (ArrayTypeMismatchException) {
2147                                         errorThrown = true;
2148                                 }
2149
2150                                 Assert ("#M96(" + types [i] + ")", errorThrown);
2151                         }
2152                 }
2153         }
2154
2155         [Test]
2156         public void TestSort() {
2157                 {
2158                         bool errorThrown = false;
2159                         try {
2160                                 Array.Sort(null);
2161                         } catch (ArgumentNullException) {
2162                                 errorThrown = true;
2163                         }
2164                         Assert("#N01", errorThrown);
2165                 }
2166                 {
2167                         bool errorThrown = false;
2168                         try {
2169                                 Array.Sort(null, 0, 1);
2170                         } catch (ArgumentNullException) {
2171                                 errorThrown = true;
2172                         }
2173                         Assert("#N02", errorThrown);
2174                 }
2175                 {
2176                         bool errorThrown = false;
2177                         try {
2178                                 char[] c1 = new Char[2];
2179                                 Array.Sort(null, c1);
2180                         } catch (ArgumentNullException) {
2181                                 errorThrown = true;
2182                         }
2183                         Assert("#N03", errorThrown);
2184                 }
2185                 {
2186                         bool errorThrown = false;
2187                         try {
2188                                 char[] c1 = new Char[2];
2189                                 Array.Sort(null, c1, 0, 1);
2190                         } catch (ArgumentNullException) {
2191                                 errorThrown = true;
2192                         }
2193                         Assert("#N04", errorThrown);
2194                 }
2195                 {
2196                         int tc = 5;
2197                         char[] arr = {'d', 'b', 'f', 'e', 'a', 'c'};
2198                         
2199                         try {
2200                                 Array.Sort (null, 0, 1);
2201                                 Fail ("#N" + tc.ToString ());
2202                         }
2203                         catch (ArgumentException) {}
2204                         catch (Exception) { Fail ("#N" + tc.ToString ()); }
2205                         tc++;
2206                         
2207                         try {
2208                                 Array.Sort (arr, -1, 3);
2209                                 Fail ("#N" + tc.ToString ());
2210                         }
2211                         catch (ArgumentException) {}
2212                         catch (Exception) { Fail ("#N" + tc.ToString ()); }
2213                         tc++;
2214                         
2215                         try {
2216                                 Array.Sort (arr, 1, -3);
2217                                 Fail ("#N" + tc.ToString ());
2218                         }
2219                         catch (ArgumentException) {}
2220                         catch (Exception) { Fail ("#N" + tc.ToString ()); }
2221                         tc++;
2222                         
2223                         try {
2224                                 Array.Sort (arr, arr.Length, arr.Length + 2);
2225                                 Fail ("#N" + tc.ToString ());
2226                         }
2227                         catch (ArgumentException) {}
2228                         catch (Exception) { Fail ("#N" + tc.ToString ()); }
2229                 }
2230                 
2231                 // note: null second array => just sort first array
2232                 char[] starter = {'d', 'b', 'f', 'e', 'a', 'c'};
2233                 int[] starter1 = {1,2,3,4,5,6};
2234                 {
2235                         char[] c1 = (char[])starter.Clone();
2236                         Array.Sort(c1);
2237                         AssertEquals("#N21", 'a', c1[0]);
2238                         AssertEquals("#N22", 'b', c1[1]);
2239                         AssertEquals("#N23", 'c', c1[2]);
2240                         AssertEquals("#N24", 'd', c1[3]);
2241                         AssertEquals("#N25", 'e', c1[4]);
2242                         AssertEquals("#N26", 'f', c1[5]);
2243                 }
2244                 {
2245                         char[] c1 = (char[])starter.Clone();
2246                         int[] i1 = (int[])starter1.Clone();
2247                         Array.Sort(c1, i1);
2248                         AssertEquals("#N41", 'a', c1[0]);
2249                         AssertEquals("#N42", 'b', c1[1]);
2250                         AssertEquals("#N43", 'c', c1[2]);
2251                         AssertEquals("#N44", 'd', c1[3]);
2252                         AssertEquals("#N45", 'e', c1[4]);
2253                         AssertEquals("#N46", 'f', c1[5]);
2254                         AssertEquals("#N47", 5, i1[0]);
2255                         AssertEquals("#N48", 2, i1[1]);
2256                         AssertEquals("#N49", 6, i1[2]);
2257                         AssertEquals("#N50", 1, i1[3]);
2258                         AssertEquals("#N51", 4, i1[4]);
2259                         AssertEquals("#N52", 3, i1[5]);
2260                 }
2261                 {
2262                         char[] c1 = (char[])starter.Clone();
2263                         Array.Sort(c1, 1, 4);
2264                         AssertEquals("#N61", 'd', c1[0]);
2265                         AssertEquals("#N62", 'a', c1[1]);
2266                         AssertEquals("#N63", 'b', c1[2]);
2267                         AssertEquals("#N64", 'e', c1[3]);
2268                         AssertEquals("#N65", 'f', c1[4]);
2269                         AssertEquals("#N66", 'c', c1[5]);
2270                 }
2271                 {
2272                         char[] c1 = (char[])starter.Clone();
2273                         int[] i1 = (int[])starter1.Clone();
2274                         Array.Sort(c1, i1, 1, 4);
2275                         AssertEquals("#N81", 'd', c1[0]);
2276                         AssertEquals("#N82", 'a', c1[1]);
2277                         AssertEquals("#N83", 'b', c1[2]);
2278                         AssertEquals("#N84", 'e', c1[3]);
2279                         AssertEquals("#N85", 'f', c1[4]);
2280                         AssertEquals("#N86", 'c', c1[5]);
2281                         AssertEquals("#N87", 1, i1[0]);
2282                         AssertEquals("#N88", 5, i1[1]);
2283                         AssertEquals("#N89", 2, i1[2]);
2284                         AssertEquals("#N90", 4, i1[3]);
2285                         AssertEquals("#N91", 3, i1[4]);
2286                         AssertEquals("#N92", 6, i1[5]);
2287                 }
2288         }
2289
2290         [Test]
2291         public void TestInitializeEmpty()
2292         {
2293                 bool catched=false;
2294                 int[] a = {};
2295                 try
2296                 {
2297                         a.Initialize();
2298                 }
2299                 catch(Exception)
2300                 {
2301                         catched=true;
2302                 }
2303                 Assert("#TI01",!catched);
2304         }
2305
2306         [Test]
2307         public void TestInitializeInt()
2308         {
2309                 int[] a = {1,2,0};
2310                 a.Initialize();
2311                 int[] b = {1,2,0};
2312                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2313                 {
2314                         AssertEquals("#TI02 " + i ,a[i],b[i]);
2315                 }
2316         }
2317
2318         [Test]
2319         public void TestInitializeDouble()
2320         {
2321                 double[] a = {1.0,2.0,0.0};
2322                 a.Initialize();
2323                 double[] b = {1.0,2.0,0.0};
2324                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2325                 {
2326                         AssertEquals("#TI03 " + i ,a[i],b[i]);
2327                 }
2328         }
2329
2330         [Test]
2331         public void TestInitializeFloat()
2332         {
2333                 float[] a = {1.0F,2.0F,0.0F};
2334                 a.Initialize();
2335                 float[] b = {1.0F,2.0F,0.0F};
2336                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2337                 {
2338                         AssertEquals("#TI04 " + i ,a[i],b[i]);
2339                 }
2340         }
2341
2342         [Test]
2343         public void TestInitializeChar()
2344         {
2345                 char[] a = {'1','.','0','F','2','.','0','F'};
2346                 a.Initialize();
2347                 char[] b = {'1','.','0','F','2','.','0','F'};
2348                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2349                 {
2350                         AssertEquals("#TI05 " + i ,a[i],b[i]);
2351                 }
2352         }
2353
2354         [Test]
2355         public void TestInitializeString()
2356         {
2357                 string[] a = {"hola","adios","menos","mas"};
2358                 a.Initialize();
2359                 string[] b = {"hola","adios","menos","mas"};
2360                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2361                 {
2362                         AssertEquals("#TI06 " + i ,a[i],b[i]);
2363                 }
2364         }
2365
2366         [Test]
2367         public void TestInitializeEnum()
2368         {
2369                 enua[] a = {enua.hola,enua.adios,enua.menos,enua.mas};
2370                 a.Initialize();
2371                 enua[] b = {enua.hola,enua.adios,enua.menos,enua.mas};
2372                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2373                 {
2374                         AssertEquals("#TI07 " + i ,a[i],b[i]);
2375                 }
2376         }
2377         
2378         [Test]
2379         public void TestInitializeIntNI()
2380         {
2381                 int[] a = new int[20];
2382                 a.Initialize();
2383                 foreach(int b in a)
2384                 {
2385                         AssertEquals("#TI08",b,0);
2386                 }
2387         }
2388         
2389         [Test]
2390         public void TestInitializeCharNI()
2391         {
2392                 char[] a = new char[20];
2393                 a.Initialize();
2394                 foreach(char b in a)
2395                 {
2396                         AssertEquals("#TI09",b,0);
2397                 }
2398         }
2399         
2400         [Test]
2401         public void TestInitializeDoubleNI()
2402         {
2403                 double[] a = new double[20];
2404                 a.Initialize();
2405                 foreach(double b in a)
2406                 {
2407                         AssertEquals("#TI09",b,0.0);
2408                 }
2409         }
2410         
2411         [Test]
2412         public void TestInitializeStringNI()
2413         {
2414                 string[] a = new string[20];
2415                 a.Initialize();
2416                 foreach(string b in a)
2417                 {
2418                         AssertEquals("#TI10",b,null);
2419                 }
2420         }
2421         
2422         [Test]
2423         public void TestInitializeObjectNI()
2424         {
2425                 object[] a = new object[20];
2426                 a.Initialize();
2427                 foreach(object b in a)
2428                 {
2429                         AssertEquals("#TI11",b,null);
2430                 }
2431         }
2432
2433         [Test]
2434         public void TestInitializeAClassNI()
2435         {
2436                 AClass[] a = new AClass[20];
2437                 a.Initialize();
2438                 foreach(AClass b in a)
2439                 {
2440                         AssertEquals("#TI12",b,null);
2441                 }
2442         }
2443
2444
2445         [Test]
2446         public void TestInitializeAStructNI()
2447         {
2448                 AStruct[] a = new AStruct[20];
2449                 a.Initialize();
2450                 foreach(AStruct b in a)
2451                 {
2452                         AssertEquals("#TI14",b,new AStruct());
2453                 }
2454         }
2455
2456         [Test]
2457         public void TestInitializeAStruct()
2458         {
2459                 AStruct[] a = new AStruct[3];
2460                 a[1].a = "ADIOS";
2461                 a[1].s = "HOLA";
2462                 a.Initialize();
2463                 AStruct[] b = new AStruct[3];
2464                 b[1].a = "ADIOS";
2465                 b[1].s = "HOLA";
2466                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2467                 {
2468                         AssertEquals("#TI15 " + i ,a[i],b[i]);
2469                 }
2470         }
2471
2472         [Test]
2473         public void TestInitializeDateTimeNI()
2474         {
2475                 DateTime[] a = new DateTime[20];
2476                 a.Initialize();
2477                 foreach(DateTime b in a)
2478                 {
2479                         AssertEquals("#TI16",b,new DateTime());
2480                 }
2481         }
2482         
2483         [Test]
2484         [ExpectedException (typeof (ArgumentNullException))]
2485         public void MoreSort1 ()
2486         {
2487                 Array.Sort (null, 0, 1);
2488         }
2489
2490         [Test]
2491         [ExpectedException (typeof (ArgumentOutOfRangeException))]
2492         public void MoreSort2 ()
2493         {
2494                 Array.Sort (arrsort, -1, 3);
2495         }
2496
2497         [Test]
2498         [ExpectedException (typeof (ArgumentOutOfRangeException))]
2499         public void MoreSort3 ()
2500         {
2501                 Array.Sort (arrsort, 1, -3);
2502         }
2503
2504         [Test]
2505         [ExpectedException (typeof (ArgumentException))]
2506         public void MoreSort4 ()
2507         {
2508                 Array.Sort (arrsort, arrsort.Length, arrsort.Length + 2);
2509         }
2510
2511         [Test]
2512         [ExpectedException (typeof (RankException))]
2513         public void MoreSort5 ()
2514         {
2515                 char [,] arr = new char [,] {{'a'}, {'b'}};
2516                 Array.Sort (arr, 0, 1);
2517         }
2518
2519         [Test]
2520         public void MoreSort6 ()
2521         {
2522                 Array.Sort (arrsort, 0, 0);
2523         }
2524
2525         [Test]
2526         [ExpectedException (typeof (ArgumentException))]
2527         public void MoreSort7 ()
2528         {
2529                 Array.Sort (arrsort, arrsort.Length - 1, 2);
2530         }
2531
2532         [Test]
2533         [ExpectedException (typeof (ArgumentException))]
2534         public void MoreSort8 ()
2535         {
2536                 Array.Sort (arrsort, 0, arrsort.Length + 1);
2537         }
2538
2539         [Test]
2540         public void MoreSort9 ()
2541         {
2542                 Array.Sort (arrsort, null, 0, arrsort.Length, null);
2543         }
2544
2545         [Test]
2546         [ExpectedException (typeof (InvalidOperationException))]
2547         public void MoreSort10 ()
2548         {
2549                 object [] array = {true, 'k', SByte.MinValue, Byte.MinValue, (short) 2, 634, (long) 436, (float) 1.1, 1.23, "Hello World"};
2550                 Array.Sort (array, (IComparer) null);
2551         }
2552
2553         [Test] // bug #81941
2554         public void Sort ()
2555         {
2556                 double [] a = new double [2] { 0.9, 0.3 };
2557                 uint [] b = new uint [2] { 4, 7 };
2558                 Array.Sort (a, b);
2559                 AssertEquals ("#1", 0.3, a [0]);
2560                 AssertEquals ("#2", 0.9, a [1]);
2561                 AssertEquals ("#3", 7, b [0]);
2562                 AssertEquals ("#4", 4, b [1]);
2563         }
2564
2565         [Test]
2566         public void ClearJaggedArray () 
2567         {
2568                 byte[][] matrix = new byte [8][];
2569                 for (int i=0; i < 8; i++) {
2570                         matrix [i] = new byte [8];
2571                         for (int j=0; j < 8; j++) {
2572                                 matrix [i][j] = 1;
2573                         }
2574                 }
2575                 Array.Clear (matrix, 0, 8);
2576                 for (int i=0; i < 8; i++) {
2577                         AssertNull (i.ToString (), matrix [i]);
2578                 }
2579         }
2580
2581         [Test]
2582         public void ClearMultidimentionalArray () 
2583         {
2584                 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2585                 Array.Clear (matrix, 0, 2);
2586                 AssertEquals ("0,0", 0, matrix [0,0]);
2587                 AssertEquals ("0,1", 0, matrix [0,1]);
2588                 AssertEquals ("1,0", 2, matrix [1,0]);
2589                 AssertEquals ("1,1", 2, matrix [1,1]);
2590         }
2591
2592         [Test]
2593         [ExpectedException (typeof (IndexOutOfRangeException))]
2594         public void ClearOutsideMultidimentionalArray () 
2595         {
2596                 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2597                 Array.Clear (matrix, 0, 5);
2598         }
2599
2600         [Test]
2601         [ExpectedException (typeof (IndexOutOfRangeException))]
2602         public void Clear_IndexOverflow () 
2603         {
2604                 byte[] array = new byte [16];
2605                 Array.Clear (array, 4, Int32.MaxValue);
2606         }
2607
2608         [Test]
2609         [ExpectedException (typeof (IndexOutOfRangeException))]
2610         public void Clear_LengthOverflow () 
2611         {
2612                 byte[] array = new byte [16];
2613                 Array.Clear (array, Int32.MaxValue, 4);
2614         }
2615
2616         [Test]
2617         [ExpectedException (typeof (ArgumentException))]
2618         public void Copy_SourceIndexOverflow () 
2619         {
2620                 byte[] array = new byte [16];
2621                 Array.Copy (array, Int32.MaxValue, array, 8, 8);
2622         }
2623
2624         [Test]
2625         [ExpectedException (typeof (ArgumentException))]
2626         public void Copy_DestinationIndexOverflow () 
2627         {
2628                 byte[] array = new byte [16];
2629                 Array.Copy (array, 8, array, Int32.MaxValue, 8);
2630         }
2631
2632         [Test]
2633         [ExpectedException (typeof (ArgumentException))]
2634         public void Copy_LengthOverflow () 
2635         {
2636                 byte[] array = new byte [16];
2637                 Array.Copy (array, 8, array, 8, Int32.MaxValue);
2638         }
2639
2640         [Test]
2641         [ExpectedException (typeof (ArgumentException))]
2642         public void Reverse_IndexOverflow () 
2643         {
2644                 byte[] array = new byte [16];
2645                 Array.Reverse (array, Int32.MaxValue, 8);
2646         }
2647
2648         [Test]
2649         [ExpectedException (typeof (ArgumentException))]
2650         public void Reverse_LengthOverflow () 
2651         {
2652                 byte[] array = new byte [16];
2653                 Array.Reverse (array, 8, Int32.MaxValue);
2654         }
2655         
2656         public struct CharX : IComparable {
2657                 public char c;
2658         
2659                 public CharX (char c)
2660                 {
2661                         this.c = c;
2662                 }
2663         
2664                 public int CompareTo (object obj)
2665                 {
2666                         if (obj is CharX)
2667                                 return c.CompareTo (((CharX) obj).c);
2668                         else
2669                                 return c.CompareTo (obj);
2670                 }
2671         }
2672
2673         [Test]
2674         public void BinarySearch_ArgPassingOrder ()
2675         {
2676                 //
2677                 // This tests that arguments are passed to the comprer in the correct
2678                 // order. The IComparable of the *array* elements must get called, not
2679                 // that of the search object.
2680                 //
2681                 CharX [] x = { new CharX ('a'), new CharX ('b'), new CharX ('c') };
2682                 AssertEquals (1, Array.BinarySearch (x, 'b'));
2683         }
2684
2685         class Comparer: IComparer {
2686
2687                 private bool called = false;
2688
2689                 public bool Called {
2690                         get {
2691                                 bool result = called;
2692                                 called = false;
2693                                 return called;
2694                         }
2695                 }
2696
2697                 public int Compare (object x, object y)
2698                 {
2699                         called = true;
2700                         return 0;
2701                 }
2702         }
2703
2704         [Test]
2705         public void BinarySearch1_EmptyList ()
2706         {
2707                 int[] array = new int[0];
2708                 AssertEquals ("BinarySearch", - 1, Array.BinarySearch (array, 0));
2709         }
2710
2711         [Test]
2712         public void BinarySearch2_EmptyList ()
2713         {
2714                 int[] array = new int[0];
2715                 AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, 0, 0));
2716         }
2717
2718         [Test]
2719         public void BinarySearch3_EmptyList ()
2720         {
2721                 Comparer comparer = new Comparer ();
2722                 int[] array = new int[0];
2723                 AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, comparer));
2724                 // bug 77030 - the comparer isn't called for an empty array/list
2725                 Assert ("Called", !comparer.Called);
2726         }
2727
2728         [Test]
2729         public void BinarySearch4_EmptyList ()
2730         {
2731                 Comparer comparer = new Comparer ();
2732                 int[] array = new int[0];
2733                 AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, 0, comparer));
2734                 // bug 77030 - the comparer isn't called for an empty array/list
2735                 Assert ("Called", !comparer.Called);
2736         }
2737
2738 #if NET_2_0
2739         [Test]
2740         [ExpectedException (typeof (ArgumentNullException))]
2741         public void AsReadOnly_NullArray ()
2742         {
2743                 Array.AsReadOnly <int> (null);
2744         }
2745
2746         [Test]
2747         public void ReadOnly_Count ()
2748         {
2749                 AssertEquals (10, Array.AsReadOnly (new int [10]).Count);
2750         }
2751
2752         [Test]
2753         public void ReadOnly_Contains ()
2754         {
2755                 int[] arr = new int [2];
2756                 arr [0] = 3;
2757                 arr [1] = 5;
2758                 IList<int> a = Array.AsReadOnly (arr);
2759
2760                 Assert (a.Contains (3));
2761                 Assert (!a.Contains (6));
2762         }
2763
2764         [Test]
2765         public void ReadOnly_IndexOf ()
2766         {
2767                 int[] arr = new int [2];
2768                 arr [0] = 3;
2769                 arr [1] = 5;
2770                 IList<int> a = Array.AsReadOnly (arr);
2771
2772                 AssertEquals (0, a.IndexOf (3));
2773                 AssertEquals (1, a.IndexOf (5));
2774                 AssertEquals (-1, a.IndexOf (6));
2775         }
2776
2777         [Test]
2778         public void ReadOnly_Indexer ()
2779         {
2780                 int[] arr = new int [2];
2781                 arr [0] = 3;
2782                 arr [1] = 5;
2783                 IList<int> a = Array.AsReadOnly (arr);
2784
2785                 AssertEquals (3, a [0]);
2786                 AssertEquals (5, a [1]);
2787
2788                 /* Check that modifications to the original array are visible */
2789                 arr [0] = 6;
2790                 AssertEquals (6, a [0]);
2791         }
2792
2793         [Test]
2794         public void ReadOnly_Enumerator ()
2795         {
2796                 int[] arr = new int [10];
2797
2798                 for (int i = 0; i < 10; ++i)
2799                         arr [i] = i;
2800
2801                 int sum = 0;
2802                 foreach (int i in Array.AsReadOnly (arr))
2803                         sum += i;
2804
2805                 AssertEquals (45, sum);
2806         }
2807
2808         [Test]
2809         public void Resize ()
2810         {
2811                 int [] arr = new int [] { 1, 3, 5 };
2812                 Array.Resize <int> (ref arr, 3);
2813                 AssertEquals ("#A1", 3, arr.Length);
2814                 AssertEquals ("#A2", 1, arr [0]);
2815                 AssertEquals ("#A3", 3, arr [1]);
2816                 AssertEquals ("#A4", 5, arr [2]);
2817
2818                 Array.Resize <int> (ref arr, 2);
2819                 AssertEquals ("#B1", 2, arr.Length);
2820                 AssertEquals ("#B2", 1, arr [0]);
2821                 AssertEquals ("#B3", 3, arr [1]);
2822
2823                 Array.Resize <int> (ref arr, 4);
2824                 AssertEquals ("#C1", 4, arr.Length);
2825                 AssertEquals ("#C2", 1, arr [0]);
2826                 AssertEquals ("#C3", 3, arr [1]);
2827                 AssertEquals ("#C4", 0, arr [2]);
2828                 AssertEquals ("#C5", 0, arr [3]);
2829         }
2830
2831         [Test]
2832         public void Resize_null ()
2833         {
2834                 int [] arr = null;
2835                 Array.Resize (ref arr, 10);
2836                 AssertEquals (arr.Length, 10);
2837         }
2838
2839         [Test]
2840         public void Test_ContainsAndIndexOf_EquatableItem ()
2841         {
2842                 EquatableClass[] list = new EquatableClass[] {new EquatableClass (0), new EquatableClass (1), new EquatableClass (0)};
2843
2844                 AssertEquals ("#0", 0, Array.IndexOf<EquatableClass> (list, list[0]));
2845                 AssertEquals ("#1", 0, Array.IndexOf<EquatableClass> (list, new EquatableClass (0)));
2846                 AssertEquals ("#2", 2, Array.LastIndexOf<EquatableClass> (list, list[0]));
2847                 AssertEquals ("#3", 2, Array.LastIndexOf<EquatableClass> (list, new EquatableClass (0)));
2848         }
2849
2850         public class EquatableClass : IEquatable<EquatableClass>
2851         {
2852                 int _x;
2853                 public EquatableClass (int x)
2854                 {
2855                         _x = x;
2856                 }
2857
2858                 public bool Equals (EquatableClass other)
2859                 {
2860                         return this._x == other._x;
2861                 }
2862         }
2863
2864         [Test]
2865         public void AsIList ()
2866         {
2867                 IList<int> arr = new int [10];
2868                 arr [0] = 5;
2869                 AssertEquals (5, arr [0]);
2870
2871                 IList<FooStruct> arr2 = new FooStruct [10];
2872                 FooStruct s = new FooStruct ();
2873                 s.i = 11;
2874                 s.j = 22;
2875                 arr2 [5] = s;
2876                 s = arr2 [5];
2877                 AssertEquals (11, s.i);
2878                 AssertEquals (22, s.j);
2879
2880                 IList<string> arr3 = new string [10];
2881                 arr3 [5] = "ABC";
2882                 AssertEquals ("ABC", arr3 [5]);
2883         }
2884
2885         struct FooStruct {
2886                 public int i, j;
2887         }
2888
2889 #if !TARGET_JVM // BugBUG: T[] is not yet ICollection<T> under TARGET_JVM
2890         [Test]
2891         // From bug #80563
2892         public void ICollectionNull ()
2893         {
2894                 ICollection<object> test;
2895                 
2896                 test = new List<object>();
2897                 AssertEquals ("list<o>", test.Contains (null), false);
2898
2899                 test = new object[] {};
2900                 AssertEquals ("empty array", test.Contains (null), false);
2901
2902                 test = new object[] {null};
2903                 AssertEquals ("array with null", test.Contains (null), true);
2904
2905                 test = new List<object>(test);
2906                 AssertEquals ("List<object> with test", test.Contains (null), true);
2907                 
2908                 test = new object[] {new object()};
2909                 AssertEquals ("array with object", test.Contains (null), false);
2910
2911                 test = new List<object>(test);
2912                 AssertEquals ("array with test", test.Contains (null), false);
2913         }
2914 #endif // TARGET_JVM
2915 #endif
2916
2917         #region Bug 80299
2918
2919         enum ByteEnum : byte {}
2920         enum IntEnum : int {}
2921
2922         [Test]
2923         public void TestByteEnumArrayToByteArray ()
2924         {
2925                 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
2926                 byte[] b = new byte[a.Length];
2927                 a.CopyTo (b, 0);
2928         }
2929
2930         [Test]
2931         public void TestByteEnumArrayToIntArray ()
2932         {
2933                 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
2934                 int[] b = new int[a.Length];
2935                 a.CopyTo (b, 0);
2936         }
2937
2938         [Test]
2939         [ExpectedException (typeof (ArrayTypeMismatchException))]
2940         public void TestIntEnumArrayToByteArray ()
2941         {
2942                 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
2943                 byte[] b = new byte[a.Length];
2944                 a.CopyTo (b, 0);
2945         }
2946
2947         [Test]
2948         public void TestIntEnumArrayToIntArray ()
2949         {
2950                 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
2951                 int[] b = new int[a.Length];
2952                 a.CopyTo (b, 0);
2953         }
2954
2955         #endregion
2956
2957 #if NET_2_0
2958         [Test] // bug #322248
2959         public void IEnumerator_Reset ()
2960         {
2961                 int[] array = new int[] { 1, 2, 3};
2962                 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
2963                 Assert ("#A1", e.MoveNext ());
2964                 AssertEquals ("#A2", 1, e.Current);
2965                 Assert ("#A3", e.MoveNext ());
2966                 AssertEquals ("#A4", 2, e.Current);
2967
2968                 e.Reset ();
2969
2970                 Assert ("#C1", e.MoveNext ());
2971                 AssertEquals ("#C2", 1, e.Current);
2972         }
2973
2974         [Test]
2975         public void IEnumerator_Current_Finished ()
2976         {
2977                 int[] array = new int[] { 1, 2, 3 };
2978                 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
2979                 Assert ("#A1", e.MoveNext ());
2980                 AssertEquals ("#A2", 1, e.Current);
2981                 Assert ("#A3", e.MoveNext ());
2982                 AssertEquals ("#A4", 2, e.Current);
2983                 Assert ("#A5", e.MoveNext ());
2984                 AssertEquals ("#A6", 3, e.Current);
2985                 Assert ("#A6", !e.MoveNext ());
2986
2987                 try {
2988                         Fail ("#B1:" + e.Current);
2989                 } catch (InvalidOperationException ex) {
2990                         // Enumeration already finished
2991                         AssertEquals ("#B2", typeof (InvalidOperationException), ex.GetType ());
2992                         AssertNull ("#B3", ex.InnerException);
2993                         AssertNotNull ("#B4", ex.Message);
2994                 }
2995         }
2996
2997         [Test]
2998         public void IEnumerator_Current_NotStarted ()
2999         {
3000                 int[] array = new int[] { 1, 2, 3 };
3001                 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3002
3003                 try {
3004                         Fail ("#A1:" + e.Current);
3005                 } catch (InvalidOperationException ex) {
3006                         // Enumeration has not started. Call MoveNext
3007                         AssertEquals ("#A2", typeof (InvalidOperationException), ex.GetType ());
3008                         AssertNull ("#A3", ex.InnerException);
3009                         AssertNotNull ("#A4", ex.Message);
3010                 }
3011         }
3012
3013         [Test]
3014         public void IEnumerator_Current_Reset ()
3015         {
3016                 int[] array = new int[] { 1, 2, 3 };
3017                 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3018                 e.MoveNext ();
3019                 e.Reset ();
3020
3021                 try {
3022                         Fail ("#B1:" + e.Current);
3023                 } catch (InvalidOperationException ex) {
3024                         // Enumeration has not started. Call MoveNext
3025                         AssertEquals ("#B2", typeof (InvalidOperationException), ex.GetType ());
3026                         AssertNull ("#B3", ex.InnerException);
3027                         AssertNotNull ("#B4", ex.Message);
3028                 }
3029         }
3030
3031         public void ICollection_IsReadOnly() {
3032                 ICollection<string> arr = new string [10];
3033
3034                 Assert (arr.IsReadOnly);
3035         }
3036 #endif
3037 }
3038 }