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