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