Merge pull request #1225 from strawd/bug22307
[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 TestGetEnumerator() {
784                 String[] s1 = {"this", "is", "a", "test"};
785                 IEnumerator en = s1.GetEnumerator ();
786                 Assert.IsNotNull (en, "#G01");
787
788                 Assert.IsTrue (en.MoveNext (), "#G02");
789                 Assert.AreEqual ("this", en.Current, "#G03");
790                 Assert.IsTrue (en.MoveNext (), "#G04");
791                 Assert.AreEqual ("is", en.Current, "#G05");
792                 Assert.IsTrue (en.MoveNext (), "#G06");
793                 Assert.AreEqual ("a", en.Current, "#G07");
794                 Assert.IsTrue (en.MoveNext (), "#G08");
795                 Assert.AreEqual ("test", en.Current, "#G09");
796                 Assert.IsTrue (!en.MoveNext (), "#G10");
797
798                 en.Reset ();
799                 Assert.IsTrue (en.MoveNext (), "#G11");
800                 Assert.AreEqual ("this", en.Current, "#G12");
801
802                 // mutation does not invalidate array enumerator!
803                 s1.SetValue ("change", 1);
804                 Assert.IsTrue (en.MoveNext (), "#G13");
805                 Assert.AreEqual ("change", en.Current, "#G14");
806         }
807
808         [Test]
809         public void TestGetEnumeratorMultipleDimension() {
810                 String[,] s1 = {{"this", "is"}, {"a", "test"}};
811                 IEnumerator en = s1.GetEnumerator ();
812                 Assert.IsNotNull (en, "#AA01");
813
814                 Assert.IsTrue (en.MoveNext (), "#AA02");
815                 Assert.AreEqual ("this", en.Current, "#AA03");
816                 Assert.IsTrue (en.MoveNext (), "#AA04");
817                 Assert.AreEqual ("is", en.Current, "#AA05");
818                 Assert.IsTrue (en.MoveNext (), "#AA06");
819                 Assert.AreEqual ("a", en.Current, "#AA07");
820                 Assert.IsTrue (en.MoveNext (), "#AA08");
821                 Assert.AreEqual ("test", en.Current, "#AA09");
822                 Assert.IsTrue (!en.MoveNext (), "#AA10");
823
824                 en.Reset ();
825                 Assert.IsTrue (en.MoveNext (), "#AA11");
826                 Assert.AreEqual ("this", en.Current, "#AA12");
827
828                 int[] idxs = {0,1};
829                 // mutation does not invalidate array enumerator!
830                 s1.SetValue ("change", idxs);
831                 Assert.IsTrue (en.MoveNext (), "#AA13");
832                 Assert.AreEqual ("change", en.Current, "#AA14");
833         }
834
835         [Test]
836         public void TestGetEnumeratorNonZeroLowerBounds() {
837                 int[] myLengthsArray = new int[2] { 3, 5 };
838                 int[] myBoundsArray = new int[2] { 2, 3 };
839
840                 Array myArray=Array.CreateInstance( typeof(String), myLengthsArray, myBoundsArray );
841                 for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
842                         for ( int j = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++ )  {
843                                 int[] myIndicesArray = new int[2] { i, j };
844                                 myArray.SetValue( Convert.ToString(i) + j, myIndicesArray );
845                         }
846                 IEnumerator en = myArray.GetEnumerator ();
847                 Assert.IsNotNull (en, "#AB01");
848
849                 // check the first couple of values
850                 Assert.IsTrue (en.MoveNext (), "#AB02");
851                 Assert.AreEqual ("23", en.Current, "#AB03");
852                 Assert.IsTrue (en.MoveNext (), "#AB04");
853                 Assert.AreEqual ("24", en.Current, "#AB05");
854
855                 // then check the last element's value
856                 string lastElement;
857                 do {  
858                         lastElement = (string)en.Current;
859                 } while (en.MoveNext());
860                 Assert.AreEqual ("47", lastElement, "#AB06");
861         }
862
863         [Test]
864         public void TestIList_Add () {
865                 int[] myLengthsArray = new int[2] { 3, 5 };
866                 int[] myBoundsArray = new int[2] { 2, 3 };
867
868                 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
869                 try {
870                         ((IList)myArray).Add ("can not");
871                         Assert.Fail ("IList.Add should throw");
872                 }
873                 catch (NotSupportedException) {
874                         return;
875                 }
876                 catch (Exception) {
877                         Assert.Fail ("IList.Add threw wrong exception type");
878                 }
879
880                 Assert.Fail ("IList.Add shouldn't get this far");
881         }
882
883         [Test]
884         public void TestIList_Insert () {
885                 int[] myLengthsArray = new int[2] { 3, 5 };
886                 int[] myBoundsArray = new int[2] { 2, 3 };
887
888                 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
889                 try {
890                         ((IList)myArray).Insert (0, "can not");
891                         Assert.Fail ("IList.Insert should throw");
892                 }
893                 catch (NotSupportedException) {
894                         return;
895                 }
896                 catch (Exception) {
897                         Assert.Fail ("IList.Insert threw wrong exception type");
898                 }
899
900                 Assert.Fail ("IList.Insert shouldn't get this far");
901         }
902
903         [Test]
904         public void TestIList_Remove () {
905                 int[] myLengthsArray = new int[2] { 3, 5 };
906                 int[] myBoundsArray = new int[2] { 2, 3 };
907
908                 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
909                 try {
910                         ((IList)myArray).Remove ("can not");
911                         Assert.Fail ("IList.Remove should throw");
912                 }
913                 catch (NotSupportedException) {
914                         return;
915                 }
916                 catch (Exception) {
917                         Assert.Fail ("IList.Remove threw wrong exception type");
918                 }
919
920                 Assert.Fail ("IList.Remove shouldn't get this far");
921         }
922
923         [Test]
924         public void TestIList_RemoveAt () {
925                 int[] myLengthsArray = new int[2] { 3, 5 };
926                 int[] myBoundsArray = new int[2] { 2, 3 };
927
928                 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
929                 try {
930                         ((IList)myArray).RemoveAt (0);
931                         Assert.Fail ("IList.RemoveAt should throw");
932                 }
933                 catch (NotSupportedException) {
934                         return;
935                 }
936                 catch (Exception) {
937                         Assert.Fail ("IList.RemoveAt threw wrong exception type");
938                 }
939
940                 Assert.Fail ("IList.RemoveAt shouldn't get this far");
941         }
942
943         [Test]
944         public void TestIList_Contains () {
945                 int[] myLengthsArray = new int[2] { 3, 5 };
946                 int[] myBoundsArray = new int[2] { 2, 3 };
947
948                 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
949
950                 try {
951                         bool b = ((IList)myArray).Contains ("23");
952                         Assert.Fail ("IList.Contains should throw with multi-dimensional arrays");
953                 }
954                 catch (RankException) {
955                         int[] iArr = new int[3] { 1, 2, 3};
956                         // check the first and last items
957                         Assert.IsTrue (((IList)iArr).Contains (1), "AC01");
958                         Assert.IsTrue (((IList)iArr).Contains (3), "AC02");
959
960                         // and one that is definately not there
961                         Assert.IsTrue (!((IList)iArr).Contains (42), "AC03");
962                         return;
963                 }
964
965                 Assert.Fail ("Should not get here");
966         }
967
968         [Test]
969         public void TestIList_IndexOf () {
970                 int[] myLengthsArray = new int[2] { 3, 5 };
971                 int[] myBoundsArray = new int[2] { 2, 3 };
972
973                 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
974
975                 try {
976                         bool b = ((IList)myArray).Contains ("23");
977                         Assert.Fail ("IList.Contains should throw with multi-dimensional arrays");
978                 }
979                 catch (RankException) {
980                         int[] iArr = new int[3] { 1, 2, 3};
981                         // check the first and last items
982                         Assert.AreEqual (0, ((IList)iArr).IndexOf (1), "AD01");
983                         Assert.AreEqual (2, ((IList)iArr).IndexOf (3), "AD02");
984
985                         // and one that is definately not there
986                         Assert.AreEqual (-1, ((IList)iArr).IndexOf (42), "AD03");
987                 }
988                 catch (Exception e) {
989                         Assert.Fail ("Unexpected exception: " + e.ToString());
990                 }
991
992                 // check that wierd case whem lowerbound is Int32.MinValue,
993                 // so that IndexOf() needs to return Int32.MaxValue when it cannot find the object
994                 int[] myLengthArray = new int[1] { 3 };
995                 int[] myBoundArray = new int[1] { Int32.MinValue };
996                 Array myExtremeArray=Array.CreateInstance ( typeof(String), myLengthArray, myBoundArray );
997                 Assert.AreEqual (Int32.MaxValue, ((IList)myExtremeArray).IndexOf (42), "AD04");
998
999         }
1000
1001         [Test]
1002         public void TestGetLength() {
1003                 {
1004                         bool errorThrown = false;
1005                         try {
1006                                 char[] c1 = {'a', 'b', 'c'};
1007                                 c1.GetLength(-1);
1008                         } catch (IndexOutOfRangeException) {
1009                                 errorThrown = true;
1010                         }
1011                         Assert.IsTrue (errorThrown, "#H01");
1012                 }
1013                 {
1014                         bool errorThrown = false;
1015                         try {
1016                                 char[] c1 = {'a', 'b', 'c'};
1017                                 c1.GetLength(1);
1018                         } catch (IndexOutOfRangeException) {
1019                                 errorThrown = true;
1020                         }
1021                         Assert.IsTrue (errorThrown, "#H02");
1022                 }
1023
1024                 char[] c2 = new Char[5];
1025                 Assert.AreEqual (5, c2.GetLength(0), "#H03");
1026
1027                 char[,] c3 = new Char[6,7];
1028                 Assert.AreEqual (6, c3.GetLength(0), "#H04");
1029                 Assert.AreEqual (7, c3.GetLength(1), "#H05");
1030         }
1031
1032         [Test]
1033         public void TestGetLowerBound() {
1034                 {
1035                         bool errorThrown = false;
1036                         try {
1037                                 char[] c = {'a', 'b', 'c'};
1038                                 c.GetLowerBound(-1);
1039                         } catch (IndexOutOfRangeException) {
1040                                 errorThrown = true;
1041                         }
1042                         Assert.IsTrue (errorThrown, "#H31");
1043                 }
1044                 {
1045                         bool errorThrown = false;
1046                         try {
1047                                 char[] c = {'a', 'b', 'c'};
1048                                 c.GetLowerBound(1);
1049                         } catch (IndexOutOfRangeException) {
1050                                 errorThrown = true;
1051                         }
1052                         Assert.IsTrue (errorThrown, "#H32");
1053                 }
1054
1055                 char[] c1 = new Char[5];
1056                 Assert.AreEqual (0, c1.GetLowerBound(0), "#H33");
1057
1058                 char[,] c2 = new Char[4,4];
1059                 Assert.AreEqual (0, c2.GetLowerBound(0), "#H34");
1060                 Assert.AreEqual (0, c2.GetLowerBound(1), "#H35");
1061         }
1062
1063         [Test]
1064         public void TestGetUpperBound() {
1065                 {
1066                         bool errorThrown = false;
1067                         try {
1068                                 char[] c = {'a', 'b', 'c'};
1069                                 c.GetUpperBound(-1);
1070                         } catch (IndexOutOfRangeException) {
1071                                 errorThrown = true;
1072                         }
1073                         Assert.IsTrue (errorThrown, "#H61");
1074                 }
1075                 {
1076                         bool errorThrown = false;
1077                         try {
1078                                 char[] c = {'a', 'b', 'c'};
1079                                 c.GetUpperBound(1);
1080                         } catch (IndexOutOfRangeException) {
1081                                 errorThrown = true;
1082                         }
1083                         Assert.IsTrue (errorThrown, "#H62");
1084                 }
1085
1086                 char[] c1 = new Char[5];
1087                 Assert.AreEqual (4, c1.GetUpperBound(0), "#H63");
1088
1089                 char[,] c2 = new Char[4,6];
1090                 Assert.AreEqual (3, c2.GetUpperBound(0), "#H64");
1091                 Assert.AreEqual (5, c2.GetUpperBound(1), "#H65");
1092         }
1093
1094         [Test]
1095         public void TestGetValue1() {
1096                 {
1097                         bool errorThrown = false;
1098                         try {
1099                                 char[,] c = new Char[2,2];
1100                                 c.GetValue(1);
1101                         } catch (ArgumentException) {
1102                                 errorThrown = true;
1103                         }
1104                         Assert.IsTrue (errorThrown, "#I01");
1105                 }
1106                 {
1107                         bool errorThrown = false;
1108                         try {
1109                                 char[] c = {'a', 'b', 'c'};
1110                                 c.GetValue(-1);
1111                         } catch (IndexOutOfRangeException) {
1112                                 errorThrown = true;
1113                         }
1114                         Assert.IsTrue (errorThrown, "#I02");
1115                 }
1116                 {
1117                         bool errorThrown = false;
1118                         try {
1119                                 char[] c = {'a', 'b', 'c'};
1120                                 c.GetValue(4);
1121                         } catch (IndexOutOfRangeException) {
1122                                 errorThrown = true;
1123                         }
1124                         Assert.IsTrue (errorThrown, "#I03");
1125                 }
1126
1127                 char[] c1 = {'a', 'b', 'c', 'd'};
1128                 for (int i = 0; i < c1.Length; i++) {
1129                         Assert.AreEqual (c1[i], c1.GetValue(i), "#I04(" + i + ")");
1130                 }
1131         }
1132
1133         [Test]
1134         public void TestGetValue2() {
1135                 {
1136                         bool errorThrown = false;
1137                         try {
1138                                 char[] c = new Char[2];
1139                                 c.GetValue(1,1);
1140                         } catch (ArgumentException) {
1141                                 errorThrown = true;
1142                         }
1143                         Assert.IsTrue (errorThrown, "#I21");
1144                 }
1145                 {
1146                         bool errorThrown = false;
1147                         try {
1148                                 char[,] c = new Char[2,2];
1149                                 c.GetValue(-1, 1);
1150                         } catch (IndexOutOfRangeException) {
1151                                 errorThrown = true;
1152                         }
1153                         Assert.IsTrue (errorThrown, "#I22");
1154                 }
1155                 {
1156                         bool errorThrown = false;
1157                         try {
1158                                 char[,] c = new Char[2,2];
1159                                 c.GetValue(4,1);
1160                         } catch (IndexOutOfRangeException) {
1161                                 errorThrown = true;
1162                         }
1163                         Assert.IsTrue (errorThrown, "#I23");
1164                 }
1165
1166                 char[,] c1 = new Char[4,6];
1167                 for (int i = 0; i < 24; i++) {
1168                         int first = i / 6;
1169                         int second = i % 6;
1170                         c1[first,second] = (char)(((int)'a')+i);
1171                 }
1172                 for (int i = 0; i < c1.GetLength(0); i++) {
1173                         for (int j = 0; j < c1.GetLength(1); j++) {
1174                                 Assert.AreEqual (c1[i, j], c1.GetValue(i, j), "#I24(" + i + "," + j + ")");
1175                         }
1176                 }
1177         }
1178
1179         [Test]
1180         public void TestGetValue3() {
1181                 {
1182                         bool errorThrown = false;
1183                         try {
1184                                 char[] c = new Char[2];
1185                                 c.GetValue(1,1,1);
1186                         } catch (ArgumentException) {
1187                                 errorThrown = true;
1188                         }
1189                         Assert.IsTrue (errorThrown, "#I41");
1190                 }
1191                 {
1192                         bool errorThrown = false;
1193                         try {
1194                                 char[,,] c = new Char[2,2,2];
1195                                 c.GetValue(-1, 1, 1);
1196                         } catch (IndexOutOfRangeException) {
1197                                 errorThrown = true;
1198                         }
1199                         Assert.IsTrue (errorThrown, "#I42");
1200                 }
1201                 {
1202                         bool errorThrown = false;
1203                         try {
1204                                 char[,,] c = new Char[2,2,2];
1205                                 c.GetValue(4,1,1);
1206                         } catch (IndexOutOfRangeException) {
1207                                 errorThrown = true;
1208                         }
1209                         Assert.IsTrue (errorThrown, "#I43");
1210                 }
1211
1212                 char[,,] c1 = new Char[4,2,3];
1213                 for (int i = 0; i < 24; i++) {
1214                         int first = i / 6;
1215                         int remains = i % 6;
1216                         int second = remains / 3;
1217                         int third = remains % 3;
1218                         c1[first,second, third] = (char)(((int)'a')+i);
1219                 }
1220                 for (int i = 0; i < c1.GetLength(0); i++) {
1221                         for (int j = 0; j < c1.GetLength(1); j++) {
1222                                 for (int k = 0; k < c1.GetLength(2); k++) {
1223                                         Assert.AreEqual (c1[i, j, k], c1.GetValue(i, j, k), "#I44(" + i + "," + j + ")");
1224                                 }
1225                         }
1226                 }
1227         }
1228
1229         [Test]
1230         [ExpectedException (typeof (ArgumentNullException))]
1231         public void TestGetValueLongArray ()
1232         {
1233                 char[] c = new Char[2];
1234                 c.GetValue((long [])null);
1235         }
1236
1237         [Test]
1238         public void TestGetValueN() {
1239                 {
1240                         bool errorThrown = false;
1241                         try {
1242                                 char[] c = new Char[2];
1243                                 c.GetValue((int [])null);
1244                         } catch (ArgumentNullException) {
1245                                 errorThrown = true;
1246                         }
1247                         Assert.IsTrue (errorThrown, "#I61a");
1248                 }
1249                 {
1250                         bool errorThrown = false;
1251                         try {
1252                                 char[] c = new Char[2];
1253                                 int[] coords = {1, 1};
1254                                 c.GetValue(coords);
1255                         } catch (ArgumentException) {
1256                                 errorThrown = true;
1257                         }
1258                         Assert.IsTrue (errorThrown, "#I62");
1259                 }
1260                 {
1261                         bool errorThrown = false;
1262                         try {
1263                                 char[,] c = new Char[2,2];
1264                                 int[] coords = {-1, 1};
1265                                 c.GetValue(coords);
1266                         } catch (IndexOutOfRangeException) {
1267                                 errorThrown = true;
1268                         }
1269                         Assert.IsTrue (errorThrown, "#I63");
1270                 }
1271                 {
1272                         bool errorThrown = false;
1273                         try {
1274                                 char[,] c = new Char[2,2];
1275                                 int[] coords = {4, 1};
1276                                 c.GetValue(coords);
1277                         } catch (IndexOutOfRangeException) {
1278                                 errorThrown = true;
1279                         }
1280                         Assert.IsTrue (errorThrown, "#I64");
1281                 }
1282
1283                 char[,] c1 = new Char[4,6];
1284                 for (int i = 0; i < 24; i++) {
1285                         int first = i / 6;
1286                         int second = i % 6;
1287                         c1[first,second] = (char)(((int)'a')+i);
1288                 }
1289                 for (int i = 0; i < c1.GetLength(0); i++) {
1290                         for (int j = 0; j < c1.GetLength(1); j++) {
1291                                 int[] coords = {i, j};
1292                                 Assert.AreEqual (c1[i, j], c1.GetValue(coords), "#I65(" + i + "," + j + ")");
1293                         }
1294                 }
1295         }
1296
1297         [Test]
1298         public void TestIndexOf1() {
1299                 {
1300                         bool errorThrown = false;
1301                         try {
1302                                 Array.IndexOf(null, "huh?");
1303                         } catch (ArgumentNullException) {
1304                                 errorThrown = true;
1305                         }
1306                         Assert.IsTrue (errorThrown, "#J01");
1307                 }
1308                 {
1309                         bool errorThrown = false;
1310                         try {
1311                                 char[,] c = new Char[2,2];
1312                                 Array.IndexOf(c, "huh?");
1313                         } catch (RankException) {
1314                                 errorThrown = true;
1315                         }
1316                         Assert.IsTrue (errorThrown, "#J02");
1317                 }
1318
1319                 String[] s1 = {"this", "is", "a", "test"};
1320                 Assert.AreEqual (-1, Array.IndexOf(s1, null), "#J03");
1321                 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing"), "#J04");
1322                 Assert.AreEqual (0, Array.IndexOf(s1, "this"), "#J05");
1323                 Assert.AreEqual (3, Array.IndexOf(s1, "test"), "#J06");
1324         }
1325
1326         [Test]
1327         public void TestIndexOf2() {
1328                 {
1329                         bool errorThrown = false;
1330                         try {
1331                                 Array.IndexOf(null, "huh?", 0);
1332                         } catch (ArgumentNullException) {
1333                                 errorThrown = true;
1334                         }
1335                         Assert.IsTrue (errorThrown, "#J21");
1336                 }
1337                 {
1338                         bool errorThrown = false;
1339                         try {
1340                                 char[,] c = new Char[2,2];
1341                                 Array.IndexOf(c, "huh?", 0);
1342                         } catch (RankException) {
1343                                 errorThrown = true;
1344                         }
1345                         Assert.IsTrue (errorThrown, "#J22");
1346                 }
1347                 {
1348                         bool errorThrown = false;
1349                         try {
1350                                 char[] c = new Char[2];
1351                                 Array.IndexOf(c, "huh?", 3);
1352                         } catch (ArgumentOutOfRangeException) {
1353                                 errorThrown = true;
1354                         }
1355                         Assert.IsTrue (errorThrown, "#J23");
1356                 }
1357
1358                 String[] s1 = {"this", "is", "really", "a", "test"};
1359                 Assert.AreEqual (-1, Array.IndexOf(s1, null, 1), "#J24");
1360                 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing", 1), "#J25");
1361                 Assert.AreEqual (-1, Array.IndexOf(s1, "this", 1), "#J26");
1362                 Assert.AreEqual (1, Array.IndexOf(s1, "is", 1), "#J27");
1363                 Assert.AreEqual (4, Array.IndexOf(s1, "test", 1), "#J28");
1364         }
1365
1366         [Test]
1367         public void TestIndexOf3() {
1368                 {
1369                         bool errorThrown = false;
1370                         try {
1371                                 Array.IndexOf(null, "huh?", 0, 1);
1372                         } catch (ArgumentNullException) {
1373                                 errorThrown = true;
1374                         }
1375                         Assert.IsTrue (errorThrown, "#J41");
1376                 }
1377                 {
1378                         bool errorThrown = false;
1379                         try {
1380                                 char[,] c = new Char[2,2];
1381                                 Array.IndexOf(c, "huh?", 0, 1);
1382                         } catch (RankException) {
1383                                 errorThrown = true;
1384                         }
1385                         Assert.IsTrue (errorThrown, "#J42");
1386                 }
1387                 {
1388                         bool errorThrown = false;
1389                         try {
1390                                 char[] c = new Char[2];
1391                                 Array.IndexOf(c, "huh?", 3, 1);
1392                         } catch (ArgumentOutOfRangeException) {
1393                                 errorThrown = true;
1394                         }
1395                         Assert.IsTrue (errorThrown, "#J43");
1396                 }
1397                 {
1398                         bool errorThrown = false;
1399                         try {
1400                                 char[] c = new Char[2];
1401                                 Array.IndexOf(c, "huh?", 0, 5);
1402                         } catch (ArgumentOutOfRangeException) {
1403                                 errorThrown = true;
1404                         }
1405                         Assert.IsTrue (errorThrown, "#J44");
1406                 }
1407
1408                 String[] s1 = {"this", "is", "really", "a", "test"};
1409                 Assert.AreEqual (-1, Array.IndexOf(s1, null, 1, 3), "#J45");
1410                 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing", 1, 3), "#J46");
1411                 Assert.AreEqual (-1, Array.IndexOf(s1, "this", 1, 3), "#J47");
1412                 Assert.AreEqual (1, Array.IndexOf(s1, "is", 1, 3), "#J48");
1413                 Assert.AreEqual (-1, Array.IndexOf(s1, "test", 1, 3), "#J49");
1414                 Assert.AreEqual (3, Array.IndexOf(s1, "a", 1, 3), "#J50");
1415         }
1416         
1417         [Test]
1418         public void TestIndexOf_CustomEqual ()
1419         {
1420                 DataEqual[] test = new DataEqual [] { new DataEqual () };
1421                 Assert.AreEqual (0, Array.IndexOf (test, "asdfas", 0));
1422                 
1423                 IList array = (IList)test;
1424                 Assert.AreEqual (0, array.IndexOf ("asdfas"));
1425         }
1426         
1427         [Test]
1428         public void TestLastIndexOf1() {
1429                 {
1430                         bool errorThrown = false;
1431                         try {
1432                                 Array.LastIndexOf(null, "huh?");
1433                         } catch (ArgumentNullException) {
1434                                 errorThrown = true;
1435                         }
1436                         Assert.IsTrue (errorThrown, "#K01");
1437                 }
1438                 {
1439                         bool errorThrown = false;
1440                         try {
1441                                 char[,] c = new Char[2,2];
1442                                 Array.LastIndexOf(c, "huh?");
1443                         } catch (RankException) {
1444                                 errorThrown = true;
1445                         }
1446                         Assert.IsTrue (errorThrown, "#K02");
1447                 }
1448
1449                 String[] s1 = {"this", "is", "a", "a", "test"};
1450                 Assert.AreEqual (-1, Array.LastIndexOf(s1, null), "#K03");
1451                 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing"), "#K04");
1452                 Assert.AreEqual (0, Array.LastIndexOf(s1, "this"), "#K05");
1453                 Assert.AreEqual (4, Array.LastIndexOf(s1, "test"), "#K06");
1454                 Assert.AreEqual (3, Array.LastIndexOf(s1, "a"), "#K07");
1455
1456                 Assert.AreEqual (-1, Array.LastIndexOf (new String [0], "foo"));
1457         }
1458
1459         [Test]
1460         public void TestLastIndexOf2() {
1461                 {
1462                         bool errorThrown = false;
1463                         try {
1464                                 Array.LastIndexOf(null, "huh?", 0);
1465                         } catch (ArgumentNullException) {
1466                                 errorThrown = true;
1467                         }
1468                         Assert.IsTrue (errorThrown, "#K21");
1469                 }
1470                 {
1471                         bool errorThrown = false;
1472                         try {
1473                                 char[,] c = new Char[2,2];
1474                                 Array.LastIndexOf(c, "huh?", 0);
1475                         } catch (RankException) {
1476                                 errorThrown = true;
1477                         }
1478                         Assert.IsTrue (errorThrown, "#K22");
1479                 }
1480                 {
1481                         bool errorThrown = false;
1482                         try {
1483                                 char[] c = new Char[2];
1484                                 Array.LastIndexOf(c, "huh?", 3);
1485                         } catch (ArgumentOutOfRangeException) {
1486                                 errorThrown = true;
1487                         }
1488                         Assert.IsTrue (errorThrown, "#K23");
1489                 }
1490
1491                 String[] s1 = {"this", "is", "really", "a", "test"};
1492                 Assert.AreEqual (-1, Array.LastIndexOf(s1, null, 3), "#K24");
1493                 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing", 3), "#K25");
1494                 Assert.AreEqual (-1, Array.LastIndexOf(s1, "test", 3), "#K26");
1495                 Assert.AreEqual (3, Array.LastIndexOf(s1, "a", 3), "#K27");
1496                 Assert.AreEqual (0, Array.LastIndexOf(s1, "this", 3), "#K28");
1497         }
1498
1499         [Test]
1500         public void TestLastIndexOf3() {
1501                 {
1502                         bool errorThrown = false;
1503                         try {
1504                                 Array.LastIndexOf(null, "huh?", 0, 1);
1505                         } catch (ArgumentNullException) {
1506                                 errorThrown = true;
1507                         }
1508                         Assert.IsTrue (errorThrown, "#K41");
1509                 }
1510                 {
1511                         bool errorThrown = false;
1512                         try {
1513                                 char[,] c = new Char[2,2];
1514                                 Array.LastIndexOf(c, "huh?", 0, 1);
1515                         } catch (RankException) {
1516                                 errorThrown = true;
1517                         }
1518                         Assert.IsTrue (errorThrown, "#K42");
1519                 }
1520                 {
1521                         bool errorThrown = false;
1522                         try {
1523                                 char[] c = new Char[2];
1524                                 Array.LastIndexOf(c, "huh?", 3, 1);
1525                         } catch (ArgumentOutOfRangeException) {
1526                                 errorThrown = true;
1527                         }
1528                         Assert.IsTrue (errorThrown, "#K43");
1529                 }
1530                 {
1531                         bool errorThrown = false;
1532                         try {
1533                                 char[] c = new Char[2];
1534                                 Array.LastIndexOf(c, "huh?", 0, 5);
1535                         } catch (ArgumentOutOfRangeException) {
1536                                 errorThrown = true;
1537                         }
1538                         Assert.IsTrue (errorThrown, "#K44");
1539                 }
1540
1541                 String[] s1 = {"this", "is", "really", "a", "test"};
1542                 Assert.AreEqual (-1, Array.LastIndexOf(s1, null, 3, 3), "#K45");
1543                 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing", 3, 3), "#K46");
1544                 Assert.AreEqual (-1, Array.LastIndexOf(s1, "this", 3, 3), "#K47");
1545                 Assert.AreEqual (1, Array.LastIndexOf(s1, "is", 3, 3), "#K48");
1546                 Assert.AreEqual (-1, Array.LastIndexOf(s1, "test", 3, 3), "#K49");
1547                 Assert.AreEqual (3, Array.LastIndexOf(s1, "a", 3, 3), "#K50");
1548         }
1549
1550         [Test]
1551         public void TestLastIndexOf4 ()
1552         {
1553                 short [] a = new short [] { 19, 238, 317, 6, 565, 0, -52, 60, -563, 753, 238, 238};
1554                 try {
1555                         Array.LastIndexOf (a, (object)16, -1);
1556                         NUnit.Framework.Assert.Fail ("#1");
1557                 } catch (ArgumentOutOfRangeException) { }
1558                 
1559                 try {
1560                         Array.LastIndexOf<short> (a, 16, -1);
1561                         NUnit.Framework.Assert.Fail ("#2");
1562                 } catch (ArgumentOutOfRangeException) { }
1563         }
1564
1565         [Test]
1566         public void TestLastIndexOf5 ()
1567         {
1568                 char [] a = new char [] {'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'j', 'i', 'h'};
1569                 string s;
1570                 int retval;
1571                 bool error = false;
1572
1573                 for (int i = a.Length - 1; i >= 0 ; i--) {
1574                         s = i.ToString ();
1575                         retval = Array.LastIndexOf(a, a [i], i, i + 1);
1576                         if (retval != i)
1577                                 error = true;
1578                 }
1579                 Assert.IsTrue (!error);
1580         }
1581
1582         [Test]
1583         [ExpectedException (typeof (ArgumentOutOfRangeException))]
1584         public void LastIndexOf_StartIndexOverflow ()
1585         {
1586                 // legal - no exception
1587                 byte[] array = new byte [16];
1588                 Array.LastIndexOf (array, this, Int32.MaxValue, 1);
1589         }
1590
1591         [Test]
1592         [ExpectedException (typeof (ArgumentOutOfRangeException))]
1593         public void LastIndexOf_CountOverflow ()
1594         {
1595                 // legal - no exception
1596                 byte[] array = new byte [16];
1597                 Array.LastIndexOf (array, this, 1, Int32.MaxValue);
1598         }
1599
1600         [Test]
1601         public void LastIndexOf_0LengthArray ()
1602         {
1603                 Array array = Array.CreateInstance (typeof (char), 0);
1604                 int idx = Array.LastIndexOf (array, (object) null, -1, 0);
1605                 Assert.IsTrue (idx == -1, "#01");
1606                 idx = Array.LastIndexOf (array, (object) null, -1, 10);
1607                 Assert.IsTrue (idx == -1, "#02");
1608                 idx = Array.LastIndexOf (array, (object) null, -100, 10);
1609                 Assert.IsTrue (idx == -1, "#02");
1610
1611                 array = Array.CreateInstance (typeof (char), 1);
1612                 try {
1613                         Array.LastIndexOf (array, (object) null, -1, 0);
1614                         Assert.Fail ("#04");
1615                 } catch (ArgumentOutOfRangeException e) {
1616                 }
1617                 try {
1618                         Array.LastIndexOf (array, (object) null, -1, 10);
1619                         Assert.Fail ("#05");
1620                 } catch (ArgumentOutOfRangeException e) {
1621                 }
1622                 try {
1623                         Array.LastIndexOf (array, (object) null, -100, 10);
1624                         Assert.Fail ("#06");
1625                 } catch (ArgumentOutOfRangeException e) {
1626                 }
1627         }
1628
1629
1630         [Test]
1631         public void FindIndexTest ()
1632         {
1633                 var a = new int[] { 2, 2, 2, 3, 2 };
1634                 Assert.AreEqual (2, Array.FindIndex (a, 2, 2, l => true));
1635         }
1636
1637         [Test]
1638         public void FindIndex_Invalid ()
1639         {
1640                 var array = new int [] { 1, 2, 3, 4, 5 };
1641
1642                 try {
1643                         Array.FindIndex (array, null);
1644                         Assert.Fail ("#1");
1645                 } catch (ArgumentNullException) {
1646                 }
1647
1648                 try {
1649                         Array.FindIndex (array, -1, l => true);
1650                         Assert.Fail ("#2");
1651                 } catch (ArgumentOutOfRangeException) {
1652                 }
1653
1654                 try {
1655                         Array.FindIndex (array, -1, 0, l => true);
1656                         Assert.Fail ("#2b");
1657                 } catch (ArgumentOutOfRangeException) {
1658                 }
1659
1660                 try {
1661                         Array.FindIndex (array, 0, -1, l => true);
1662                         Assert.Fail ("#3");
1663                 } catch (ArgumentOutOfRangeException) {
1664                 }
1665
1666                 try {
1667                         Array.FindIndex (array, 100, l => true);
1668                         Assert.Fail ("#4");
1669                 } catch (ArgumentOutOfRangeException) {
1670                 }
1671
1672                 try {
1673                         Array.FindIndex (array, 100, 0, l => true);
1674                         Assert.Fail ("#4b");
1675                 } catch (ArgumentOutOfRangeException) {
1676                 }
1677
1678                 try {
1679                         Array.FindIndex (array, 7, 2, l => true);
1680                         Assert.Fail ("#5");
1681                 } catch (ArgumentOutOfRangeException) {
1682                 }
1683         }
1684
1685         [Test, ExpectedException (typeof (ArgumentNullException))]
1686         public void FindLastNullTest ()
1687         {
1688                 var array = new int [] { 1, 2, 3, 4, 5 };               
1689                 Array.FindLast (array, null);
1690         }
1691
1692         [Test]
1693         public void FindLastIndexTest ()
1694         {
1695                 var array = new int [] { 1, 2, 3, 4, 5 };
1696
1697                 Assert.AreEqual (2, Array.FindLastIndex (array, 2, 3, l => true));
1698                 Assert.AreEqual (2, Array.FindLastIndex (array, 2, 2, l => true));
1699                 Assert.AreEqual (1, Array.FindLastIndex (array, 1, 2, l => true));
1700         }
1701
1702         [Test]
1703         public void FindLastIndex_Invalid ()
1704         {
1705                 var array = new int [] { 1, 2, 3, 4, 5 };
1706                 try {
1707                         Array.FindLastIndex (array, null);
1708                         Assert.Fail ("#1");
1709                 } catch (ArgumentNullException) {
1710                 }
1711
1712                 try {
1713                         Array.FindLastIndex (array, -1, l => true);
1714                         Assert.Fail ("#2");
1715                 } catch (ArgumentOutOfRangeException) {
1716                 }
1717
1718                 try {
1719                         Array.FindLastIndex (array, -1, 0, l => true);
1720                         Assert.Fail ("#2b");
1721                 } catch (ArgumentOutOfRangeException) {
1722                 }
1723
1724                 try {
1725                         Array.FindLastIndex (array, 0, -1, l => true);
1726                         Assert.Fail ("#3");
1727                 } catch (ArgumentOutOfRangeException) {
1728                 }
1729
1730                 try {
1731                         Array.FindLastIndex (array, 100, l => true);
1732                         Assert.Fail ("#4");
1733                 } catch (ArgumentOutOfRangeException) {
1734                 }
1735
1736                 try {
1737                         Array.FindLastIndex (array, 100, 0, l => true);
1738                         Assert.Fail ("#4b");
1739                 } catch (ArgumentOutOfRangeException) {
1740                 }
1741
1742                 try {
1743                         Array.FindLastIndex (array, 2, 4, l => true);
1744                         Assert.Fail ("#5");
1745                 } catch (ArgumentOutOfRangeException) {
1746                 }
1747         }
1748
1749         [Test]
1750         public void TestReverse() {
1751                 {
1752                         bool errorThrown = false;
1753                         try {
1754                                 Array.Reverse(null);
1755                         } catch (ArgumentNullException) {
1756                                 errorThrown = true;
1757                         }
1758                         Assert.IsTrue (errorThrown, "#L01");
1759                 }
1760                 {
1761                         bool errorThrown = false;
1762                         try {
1763                                 char[,] c = new Char[2,2];
1764                                 Array.Reverse(c);
1765                         } catch (RankException) {
1766                                 errorThrown = true;
1767                         }
1768                         Assert.IsTrue (errorThrown, "#L02");
1769                 }
1770                 
1771                 char[] c1 = {'a', 'b', 'c', 'd'};
1772                 Array.Reverse(c1);
1773                 Assert.AreEqual ('d', c1[0], "#L03");
1774                 Assert.AreEqual ('c', c1[1], "#L04");
1775                 Assert.AreEqual ('b', c1[2], "#L05");
1776                 Assert.AreEqual ('a', c1[3], "#L06");
1777
1778                 {
1779                         bool errorThrown = false;
1780                         try {
1781                                 Array.Reverse(null, 0, 0);
1782                         } catch (ArgumentNullException) {
1783                                 errorThrown = true;
1784                         }
1785                         Assert.IsTrue (errorThrown, "#L07");
1786                 }
1787                 {
1788                         bool errorThrown = false;
1789                         try {
1790                                 char[,] c = new Char[2,2];
1791                                 Array.Reverse(c, 0, 0);
1792                         } catch (RankException) {
1793                                 errorThrown = true;
1794                         }
1795                         Assert.IsTrue (errorThrown, "#L08");
1796                 }
1797                 //{
1798                 //bool errorThrown = false;
1799                 //try {
1800                 //      char[] c = new Char[2];
1801                 //      Array.Reverse(c, 0, 3);
1802                 //} catch (ArgumentOutOfRangeException) {
1803                 //      errorThrown = true;
1804                 //}
1805                 //Assert.IsTrue (errorThrown, "#L09");
1806                 //}
1807                 //{
1808                 //bool errorThrown = false;
1809                 //try {
1810                 //      char[] c = new Char[2];
1811                 //      Array.Reverse(c, 3, 0);
1812                 //} catch (ArgumentOutOfRangeException) {
1813                 //      errorThrown = true;
1814                 //}
1815                 //Assert.IsTrue (errorThrown, "#L10");
1816                 //}
1817
1818                 char[] c2 = { 'a', 'b', 'c', 'd'};
1819                 Array.Reverse(c2, 1, 2);
1820                 Assert.AreEqual ('a', c2[0], "#L11");
1821                 Assert.AreEqual ('c', c2[1], "#L12");
1822                 Assert.AreEqual ('b', c2[2], "#L13");
1823                 Assert.AreEqual ('d', c2[3], "#L14");
1824         }
1825
1826         [Test]
1827         // #8904
1828         public void ReverseStruct () {
1829                 BStruct[] c3 = new BStruct[2];
1830                 c3 [0] = new BStruct () { i1 = 1, i2 = 2, i3 = 3 };
1831                 c3 [1] = new BStruct () { i1 = 4, i2 = 5, i3 = 6 };
1832                 Array.Reverse (c3);
1833                 Assert.AreEqual (4, c3 [0].i1);
1834                 Assert.AreEqual (5, c3 [0].i2);
1835                 Assert.AreEqual (6, c3 [0].i3);
1836                 Assert.AreEqual (1, c3 [1].i1);
1837                 Assert.AreEqual (2, c3 [1].i2);
1838                 Assert.AreEqual (3, c3 [1].i3);
1839         }
1840
1841         struct BStruct {
1842                 public int i1, i2, i3;
1843         }
1844
1845         [Test]
1846         public void TestSetValue1() {
1847                 {
1848                         bool errorThrown = false;
1849                         try {
1850                                 char[,] c = new Char[2,2];
1851                                 c.SetValue("buh", 1);
1852                         } catch (ArgumentException) {
1853                                 errorThrown = true;
1854                         }
1855                         Assert.IsTrue (errorThrown, "#M01");
1856                 }
1857                 {
1858                         bool errorThrown = false;
1859                         try {
1860                                 char[] c = {'a', 'b', 'c'};
1861                                 c.SetValue("buh", -1);
1862                         } catch (IndexOutOfRangeException) {
1863                                 errorThrown = true;
1864                         }
1865                         Assert.IsTrue (errorThrown, "#M02");
1866                 }
1867                 {
1868                         bool errorThrown = false;
1869                         try {
1870                                 char[] c = {'a', 'b', 'c'};
1871                                 c.SetValue("buh", 4);
1872                         } catch (IndexOutOfRangeException) {
1873                                 errorThrown = true;
1874                         }
1875                         Assert.IsTrue (errorThrown, "#M03");
1876                 }
1877
1878                 char[] c1 = {'a', 'b', 'c', 'd'};
1879                 char[] c2 = new char[4];
1880                 for (int i = 0; i < c1.Length; i++) {
1881                         c2.SetValue(c1[i], i);
1882                 }
1883                 for (int i = 0; i < c1.Length; i++) {
1884                         Assert.AreEqual (c1[i], c2[i], "#M04(" + i + ")");
1885                 }
1886
1887                 int[] c3 = { 1, 2, 3 };
1888                 long[] c4 = new long [3];
1889
1890                 for (int i = 0; i < c3.Length; i++)
1891                         c4.SetValue (c3 [i], i);
1892
1893                 try {
1894                         c3.CopyTo (c4, 0);
1895                 } catch (Exception e) {
1896                         Assert.Fail ("c3.CopyTo(): e=" + e);
1897                 }
1898                 for (int i = 0; i < c3.Length; i++)
1899                         Assert.IsTrue (c3[i] == c4[i], "#M05(" + i + ")");
1900
1901                 Object[] c5 = new Object [3];
1902                 long[] c6 = new long [3];
1903
1904                 try {
1905                         c4.CopyTo (c5, 0);
1906                 } catch (Exception e) {
1907                         Assert.Fail ("c4.CopyTo(): e=" + e);
1908                 }
1909
1910                 try {
1911                         c5.CopyTo (c6, 0);
1912                 } catch (Exception e) {
1913                         Assert.Fail ("c5.CopyTo(): e=" + e);
1914                 }
1915                 // for (int i = 0; i < c5.Length; i++)
1916                 // Assert.IsTrue (c5[i] == c6[i], "#M06(" + i + ")");
1917         }
1918
1919         [Test]
1920         public void TestSetValue2() {
1921                 {
1922                         bool errorThrown = false;
1923                         try {
1924                                 char[] c = new Char[2];
1925                                 c.SetValue("buh", 1,1);
1926                         } catch (ArgumentException) {
1927                                 errorThrown = true;
1928                         }
1929                         Assert.IsTrue (errorThrown, "#M21");
1930                 }
1931                 {
1932                         bool errorThrown = false;
1933                         try {
1934                                 char[,] c = new Char[2,2];
1935                                 c.SetValue("buh", -1, 1);
1936                         } catch (IndexOutOfRangeException) {
1937                                 errorThrown = true;
1938                         }
1939                         Assert.IsTrue (errorThrown, "#M22");
1940                 }
1941                 {
1942                         bool errorThrown = false;
1943                         try {
1944                                 char[,] c = new Char[2,2];
1945                                 c.SetValue("buh", 4,1);
1946                         } catch (IndexOutOfRangeException) {
1947                                 errorThrown = true;
1948                         }
1949                         Assert.IsTrue (errorThrown, "#M23");
1950                 }
1951
1952                 char[,] c1 = new Char[4,6];
1953                 char[,] c2 = new Char[4,6];
1954                 for (int i = 0; i < 24; i++) {
1955                         int first = i / 6;
1956                         int second = i % 6;
1957                         c1[first,second] = (char)(((int)'a')+i);
1958                         c2.SetValue(c1[first,second], first, second);
1959                 }
1960                 for (int i = 0; i < c1.GetLength(0); i++) {
1961                         for (int j = 0; j < c1.GetLength(1); j++) {
1962                                 Assert.AreEqual (c1[i, j], c2[i, j], "#M24(" + i + "," + j + ")");
1963                         }
1964                 }
1965         }
1966
1967         [Test]
1968         public void TestSetValue3() {
1969                 {
1970                         bool errorThrown = false;
1971                         try {
1972                                 char[] c = new Char[2];
1973                                 c.SetValue("buh", 1,1,1);
1974                         } catch (ArgumentException) {
1975                                 errorThrown = true;
1976                         }
1977                         Assert.IsTrue (errorThrown, "#M41");
1978                 }
1979                 {
1980                         bool errorThrown = false;
1981                         try {
1982                                 char[,,] c = new Char[2,2,2];
1983                                 c.SetValue("buh", -1, 1, 1);
1984                         } catch (IndexOutOfRangeException) {
1985                                 errorThrown = true;
1986                         }
1987                         Assert.IsTrue (errorThrown, "#M42");
1988                 }
1989                 {
1990                         bool errorThrown = false;
1991                         try {
1992                                 char[,,] c = new Char[2,2,2];
1993                                 c.SetValue("buh", 4,1,1);
1994                         } catch (IndexOutOfRangeException) {
1995                                 errorThrown = true;
1996                         }
1997                         Assert.IsTrue (errorThrown, "#M43");
1998                 }
1999
2000                 char[,,] c1 = new Char[4,2,3];
2001                 char[,,] c2 = new Char[4,2,3];
2002                 for (int i = 0; i < 24; i++) {
2003                         int first = i / 6;
2004                         int remains = i % 6;
2005                         int second = remains / 3;
2006                         int third = remains % 3;
2007                         c1[first,second, third] = (char)(((int)'a')+i);
2008                         c2.SetValue(c1[first, second, third], first, second, third);
2009                 }
2010                 for (int i = 0; i < c1.GetLength(0); i++) {
2011                         for (int j = 0; j < c1.GetLength(1); j++) {
2012                                 for (int k = 0; k < c1.GetLength(2); k++) {
2013                                         Assert.AreEqual (c1[i, j, k], c2[i, j, k], "#M44(" + i + "," + j + " )");
2014                                 }
2015                         }
2016                 }
2017         }
2018
2019         [Test]
2020         [ExpectedException (typeof (ArgumentNullException))]
2021         public void TestSetValueLongArray ()
2022         {
2023                 char[] c = new Char[2];
2024                 c.SetValue("buh", (long [])null);
2025         }
2026
2027         [Test]
2028         public void TestSetValueN() {
2029                 {
2030                         bool errorThrown = false;
2031                         try {
2032                                 char[] c = new Char[2];
2033                                 c.SetValue("buh", (int [])null);
2034                         } catch (ArgumentNullException) {
2035                                 errorThrown = true;
2036                         }
2037                         Assert.IsTrue (errorThrown, "#M61a");
2038                 }
2039                 {
2040                         bool errorThrown = false;
2041                         try {
2042                                 char[] c = new Char[2];
2043                                 int[] coords = {1, 1};
2044                                 c.SetValue("buh", coords);
2045                         } catch (ArgumentException) {
2046                                 errorThrown = true;
2047                         }
2048                         Assert.IsTrue (errorThrown, "#M62");
2049                 }
2050                 {
2051                         bool errorThrown = false;
2052                         try {
2053                                 char[,] c = new Char[2,2];
2054                                 int[] coords = {-1, 1};
2055                                 c.SetValue("buh", coords);
2056                         } catch (IndexOutOfRangeException) {
2057                                 errorThrown = true;
2058                         }
2059                         Assert.IsTrue (errorThrown, "#M63");
2060                 }
2061                 {
2062                         bool errorThrown = false;
2063                         try {
2064                                 char[,] c = new Char[2,2];
2065                                 int[] coords = {4, 1};
2066                                 c.SetValue("buh", coords);
2067                         } catch (IndexOutOfRangeException) {
2068                                 errorThrown = true;
2069                         }
2070                         Assert.IsTrue (errorThrown, "#M64");
2071                 }
2072
2073                 char[,] c1 = new Char[4,6];
2074                 char[,] c2 = new Char[4,6];
2075                 for (int i = 0; i < 24; i++) {
2076                         int first = i / 6;
2077                         int second = i % 6;
2078                         c1[first,second] = (char)(((int)'a')+i);
2079                         int[] coords = {first, second};
2080                         c2.SetValue(c1[first,second], coords);
2081                 }
2082                 for (int i = 0; i < c1.GetLength(0); i++) {
2083                         for (int j = 0; j < c1.GetLength(1); j++) {
2084                                 Assert.AreEqual (c1[i, j], c2[i, j], "#M65(" + i + "," + j + ")");
2085                         }
2086                 }
2087         }
2088
2089         [Test]
2090         public void TestSetValue4() {
2091                 {
2092                         int[] c1 = { 1, 2, 3 };
2093                         long[] c2 = new long [3];
2094
2095                         for (int i = 0; i < c1.Length; i++)
2096                                 c2.SetValue (c1 [i], i);
2097
2098                         for (int i = 0; i < c1.Length; i++) {
2099                                 Assert.IsTrue (c1[i] == c2[i], "#M81(" + i + ")");
2100                                 Assert.AreEqual (typeof (long), c2[i].GetType (), "#M82(" + i + ")");
2101                         }
2102                 }
2103                 {
2104                         long[] c1 = { 1, 2, 3 };
2105                         int[] c2 = new int [3];
2106                         bool errorThrown = false;
2107                         try {
2108                                 c2.SetValue (c1 [0], 0);
2109                         } catch (ArgumentException) {
2110                                 errorThrown = true;
2111                         }
2112                         Assert.IsTrue (errorThrown, "#M83");
2113                 }
2114                 {
2115                         int[] c1 = { 1, 2, 3 };
2116                         Object[] c2 = new Object [3];
2117
2118                         for (int i = 0; i < c1.Length; i++)
2119                                 c2.SetValue (c1 [i], i);
2120
2121                         for (int i = 0; i < c1.Length; i++)
2122                                 Assert.AreEqual (c1[i], Convert.ToInt32 (c2[i]), "#M84(" + i + ")");
2123                 }
2124                 {
2125                         Object[] c1 = new Object [3];
2126                         Object[] c2 = new Object [3];
2127                         c1[0] = new Object ();
2128
2129                         for (int i = 0; i < c1.Length; i++)
2130                                 c2.SetValue (c1 [i], i);
2131
2132                         for (int i = 0; i < c1.Length; i++)
2133                                 Assert.AreEqual (c1[i], c2[i], "#M85(" + i + ")");
2134                 }
2135                 {
2136                         Object[] c1 = new Object [3];
2137                         string[] c2 = new String [3];
2138                         string test = "hello";
2139                         c1[0] = test;
2140
2141                         c2.SetValue (c1 [0], 0);
2142                         Assert.AreEqual (c1[0], c2[0], "#M86");
2143                         Assert.AreEqual ("hello", c2[0], "#M87");
2144                 }
2145                 {
2146                         char[] c1 = { 'a', 'b', 'c' };
2147                         string[] c2 = new string [3];
2148                         try {
2149                                 c2.SetValue (c1 [0], 0);
2150                                 Assert.Fail ("#M88");
2151                         } catch (InvalidCastException) {}
2152                 }
2153                 {
2154                         Single[] c1 = { 1.2F, 2.3F, 3.4F, 4.5F };
2155                         long[] c2 = new long [3];
2156                         try {
2157                                 c2.SetValue (c1 [0], 0);
2158                                 Assert.Fail ("#M89");
2159                         } catch (ArgumentException) {}
2160                 }
2161                 {
2162                         Type[] types = {
2163                                 typeof (Boolean),
2164                                 typeof (Byte),
2165                                 typeof (Char),
2166                                 typeof (Double),
2167                                 typeof (Int16),
2168                                 typeof (Int32),
2169                                 typeof (Int64),
2170                                 typeof (SByte),
2171                                 typeof (Single),
2172                                 typeof (UInt16),
2173                                 typeof (UInt32),
2174                                 typeof (UInt64)
2175                         };
2176
2177                         bool v1 = true;
2178                         Byte v2 = 1;
2179                         Char v3 = 'a';
2180                         Double v4 = -1.2;
2181                         Int16 v5 = -32;
2182                         Int32 v6 = -234;
2183                         Int64 v7 = -34523;
2184                         SByte v8 = -1;
2185                         Single v9 = -4.8F;
2186                         UInt16 v10 = 24234;
2187                         UInt32 v11 = 235354;
2188                         UInt64 v12 = 234552;
2189
2190                         Object[] va1 = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12 };
2191                         Object[] va2 = { "true", "1", "a", "-1.2", "-32", "-234", "-34523", "-1",
2192                                          "-4.8F", "24234", "235354", "234552" };
2193
2194                         Object[][] vt = { va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1 };
2195
2196                         int[] arg_ex = {
2197                                 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2198                                 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
2199                                 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
2200                                 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
2201                                 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1,
2202                                 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1,
2203                                 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,
2204                                 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1,
2205                                 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1,
2206                                 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
2207                                 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0,
2208                                 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0
2209                         };
2210
2211                         // SetValue
2212
2213                         for (int i = 0; i < types.Length; i++) {
2214                                 for (int j = 0; j < types.Length; j++) {
2215                                         Array array = Array.CreateInstance (types [j], 2);
2216
2217                                         Object value = vt[j][i];
2218
2219                                         bool errorThrown = false;
2220                                         try {
2221                                                 array.SetValue (value, 0);
2222                                         } catch (ArgumentException) {
2223                                                 errorThrown = true;
2224                                         }
2225
2226                                         int ex_index = (i * types.Length) + j;
2227
2228                                         Assert.AreEqual (errorThrown, arg_ex [ex_index] == 1, "#M90(" + types [i] + "," + types [j] + ")");
2229                                 }
2230                         }
2231
2232                         for (int i = 0; i < types.Length; i++) {
2233                                 String[] array = new String [2];
2234
2235                                 Object value = va1 [i];
2236
2237                                 bool errorThrown = false;
2238                                 try {
2239                                         array.SetValue (value, 0);
2240                                 } catch (InvalidCastException) {
2241                                         errorThrown = true;
2242                                 }
2243
2244                                 Assert.IsTrue (errorThrown, "#M91(" + types [i] + ")");
2245                         }
2246
2247                         for (int i = 0; i < types.Length; i++) {
2248                                 Array array = Array.CreateInstance (types [i], 2);
2249
2250                                 Object value = va2 [i];
2251
2252                                 bool errorThrown = false;
2253                                 try {
2254                                         array.SetValue (value, 0);
2255                                 } catch (InvalidCastException) {
2256                                         errorThrown = true;
2257                                 }
2258
2259                                 Assert.IsTrue (errorThrown, "#M92(" + types [i] + ")");
2260                         }
2261
2262                         for (int i = 0; i < types.Length; i++) {
2263                                 Array array = Array.CreateInstance (types [i], 2);
2264
2265                                 Object value = null;
2266
2267                                 bool errorThrown = false;
2268                                 try {
2269                                         array.SetValue (value, 0);
2270                                 } catch (InvalidCastException) {
2271                                         errorThrown = true;
2272                                 }
2273
2274                                 Assert.IsTrue (!errorThrown, "#M93(" + types [i] + ")");
2275                         }
2276
2277                         // Copy
2278
2279                         for (int i = 0; i < types.Length; i++) {
2280                                 for (int j = 0; j < types.Length; j++) {
2281                                         Array source = Array.CreateInstance (types [i], 2);
2282                                         Array array = Array.CreateInstance (types [j], 2);
2283
2284                                         source.SetValue (vt[j][i], 0);
2285                                         source.SetValue (vt[j][i], 1);
2286
2287                                         bool errorThrown = false;
2288                                         try {
2289                                                 Array.Copy (source, array, 2);
2290                                         } catch (ArrayTypeMismatchException) {
2291                                                 errorThrown = true;
2292                                         }
2293
2294                                         int ex_index = (i * types.Length) + j;
2295
2296                                         Assert.AreEqual (errorThrown, arg_ex [ex_index] == 1, "#M94(" + types [i] + "," + types [j] + ")");
2297                                 }
2298                         }
2299
2300                         for (int i = 0; i < types.Length; i++) {
2301                                 Array source = Array.CreateInstance (types [i], 2);
2302                                 String[] array = new String [2];
2303
2304                                 source.SetValue (va1 [i], 0);
2305                                 source.SetValue (va1 [i], 1);
2306
2307                                 bool errorThrown = false;
2308                                 try {
2309                                         Array.Copy (source, array, 2);
2310                                 } catch (ArrayTypeMismatchException) {
2311                                         errorThrown = true;
2312                                 }
2313
2314                                 Assert.IsTrue (errorThrown, "#M95(" + types [i] + ")");
2315                         }
2316
2317                         for (int i = 0; i < types.Length; i++) {
2318                                 String[] source = new String [2];
2319                                 Array array = Array.CreateInstance (types [i], 2);
2320
2321                                 source.SetValue (va2 [i], 0);
2322                                 source.SetValue (va2 [i], 1);
2323
2324                                 bool errorThrown = false;
2325                                 try {
2326                                         Array.Copy (source, array, 2);
2327                                 } catch (ArrayTypeMismatchException) {
2328                                         errorThrown = true;
2329                                 }
2330
2331                                 Assert.IsTrue (errorThrown, "#M96(" + types [i] + ")");
2332                         }
2333                 }
2334         }
2335
2336         [Test]
2337         public void TestSort() {
2338                 {
2339                         bool errorThrown = false;
2340                         try {
2341                                 Array.Sort(null);
2342                         } catch (ArgumentNullException) {
2343                                 errorThrown = true;
2344                         }
2345                         Assert.IsTrue (errorThrown, "#N01");
2346                 }
2347                 {
2348                         bool errorThrown = false;
2349                         try {
2350                                 Array.Sort(null, 0, 1);
2351                         } catch (ArgumentNullException) {
2352                                 errorThrown = true;
2353                         }
2354                         Assert.IsTrue (errorThrown, "#N02");
2355                 }
2356                 {
2357                         bool errorThrown = false;
2358                         try {
2359                                 char[] c1 = new Char[2];
2360                                 Array.Sort(null, c1);
2361                         } catch (ArgumentNullException) {
2362                                 errorThrown = true;
2363                         }
2364                         Assert.IsTrue (errorThrown, "#N03");
2365                 }
2366                 {
2367                         bool errorThrown = false;
2368                         try {
2369                                 char[] c1 = new Char[2];
2370                                 Array.Sort(null, c1, 0, 1);
2371                         } catch (ArgumentNullException) {
2372                                 errorThrown = true;
2373                         }
2374                         Assert.IsTrue (errorThrown, "#N04");
2375                 }
2376                 {
2377                         int tc = 5;
2378                         char[] arr = {'d', 'b', 'f', 'e', 'a', 'c'};
2379                         
2380                         try {
2381                                 Array.Sort (null, 0, 1);
2382                                 Assert.Fail ("#N" + tc.ToString ());
2383                         }
2384                         catch (ArgumentException) {}
2385                         catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2386                         tc++;
2387                         
2388                         try {
2389                                 Array.Sort (arr, -1, 3);
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, arr.Length, arr.Length + 2);
2406                                 Assert.Fail ("#N" + tc.ToString ());
2407                         }
2408                         catch (ArgumentException) {}
2409                         catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2410                 }
2411                 
2412                 // note: null second array => just sort first array
2413                 char[] starter = {'d', 'b', 'f', 'e', 'a', 'c'};
2414                 int[] starter1 = {1,2,3,4,5,6};
2415                 {
2416                         char[] c1 = (char[])starter.Clone();
2417                         Array.Sort(c1);
2418                         Assert.AreEqual ('a', c1[0], "#N21");
2419                         Assert.AreEqual ('b', c1[1], "#N22");
2420                         Assert.AreEqual ('c', c1[2], "#N23");
2421                         Assert.AreEqual ('d', c1[3], "#N24");
2422                         Assert.AreEqual ('e', c1[4], "#N25");
2423                         Assert.AreEqual ('f', c1[5], "#N26");
2424                 }
2425                 {
2426                         char[] c1 = (char[])starter.Clone();
2427                         int[] i1 = (int[])starter1.Clone();
2428                         Array.Sort(c1, i1);
2429                         Assert.AreEqual ('a', c1[0], "#N41");
2430                         Assert.AreEqual ('b', c1[1], "#N42");
2431                         Assert.AreEqual ('c', c1[2], "#N43");
2432                         Assert.AreEqual ('d', c1[3], "#N44");
2433                         Assert.AreEqual ('e', c1[4], "#N45");
2434                         Assert.AreEqual ('f', c1[5], "#N46");
2435                         Assert.AreEqual (5, i1[0], "#N47");
2436                         Assert.AreEqual (2, i1[1], "#N48");
2437                         Assert.AreEqual (6, i1[2], "#N49");
2438                         Assert.AreEqual (1, i1[3], "#N50");
2439                         Assert.AreEqual (4, i1[4], "#N51");
2440                         Assert.AreEqual (3, i1[5], "#N52");
2441                 }
2442                 {
2443                         char[] c1 = (char[])starter.Clone();
2444                         Array.Sort(c1, 1, 4);
2445                         Assert.AreEqual ('d', c1[0], "#N61");
2446                         Assert.AreEqual ('a', c1[1], "#N62");
2447                         Assert.AreEqual ('b', c1[2], "#N63");
2448                         Assert.AreEqual ('e', c1[3], "#N64");
2449                         Assert.AreEqual ('f', c1[4], "#N65");
2450                         Assert.AreEqual ('c', c1[5], "#N66");
2451                 }
2452                 {
2453                         char[] c1 = (char[])starter.Clone();
2454                         int[] i1 = (int[])starter1.Clone();
2455                         Array.Sort(c1, i1, 1, 4);
2456                         Assert.AreEqual ('d', c1[0], "#N81");
2457                         Assert.AreEqual ('a', c1[1], "#N82");
2458                         Assert.AreEqual ('b', c1[2], "#N83");
2459                         Assert.AreEqual ('e', c1[3], "#N84");
2460                         Assert.AreEqual ('f', c1[4], "#N85");
2461                         Assert.AreEqual ('c', c1[5], "#N86");
2462                         Assert.AreEqual (1, i1[0], "#N87");
2463                         Assert.AreEqual (5, i1[1], "#N88");
2464                         Assert.AreEqual (2, i1[2], "#N89");
2465                         Assert.AreEqual (4, i1[3], "#N90");
2466                         Assert.AreEqual (3, i1[4], "#N91");
2467                         Assert.AreEqual (6, i1[5], "#N92");
2468                 }
2469
2470                 {
2471                         // #648828
2472                         double[] a = new double[115];
2473                         int[] b = new int[256];
2474                         Array.Sort<double, int> (a, b, 0, 115);
2475                 }
2476
2477                 /* Check that ulong[] is not sorted as long[] */
2478                 {
2479                         string[] names = new string[] {
2480                                 "A", "B", "C", "D", "E"
2481                         };
2482
2483                         ulong[] arr = new ulong [] {
2484                                 5,
2485                                 unchecked((ulong)0xffffFFFF00000000),
2486                                         0,
2487                                                 0x7FFFFFFFffffffff,
2488                                                 100
2489                                                 };
2490
2491                         Array a = arr;
2492                         Array.Sort (a, names, null);
2493                         Assert.AreEqual (0, a.GetValue (0));
2494                 }
2495         }
2496
2497         [Test]
2498         public void Sort_NullValues ()
2499         {
2500                 var s = new [] { "a", null, "b", null };
2501                 Array.Sort (s, (a, b) => {
2502                         if (a == null) {
2503                                 return b == null ? 0 : 1;
2504                         }
2505
2506                         if (b == null)
2507                                 return -1;
2508
2509                         return a.CompareTo (b);
2510                 });
2511
2512                 Assert.AreEqual ("a", s [0], "#1");
2513                 Assert.AreEqual ("b", s [1], "#2");
2514                 Assert.IsNull (s [2], "#3");
2515                 Assert.IsNull (s [3], "#4");
2516         }
2517
2518         [Test] // #616416
2519         public void SortNonGenericDoubleItems () {
2520             double[] doubleValues = new double[11];
2521
2522                         doubleValues[0] = 0.221788066253601;
2523                         doubleValues[1] = 0.497278285809481;
2524                         doubleValues[2] = 0.100565033883643;
2525                         doubleValues[3] = 0.0433309347749905;
2526                         doubleValues[4] = 0.00476726438463812;
2527                         doubleValues[5] = 0.1354609735456;
2528                         doubleValues[6] = 0.57690356588135;
2529                         doubleValues[7] = 0.466239434334826;
2530                         doubleValues[8] = 0.409741461978934;
2531                         doubleValues[9] = 0.0112412763949565;
2532                         doubleValues[10] = 0.668704347674307;
2533
2534             int[] indices = new int[11];
2535             indices[0] = 0;
2536             indices[1] = 1;
2537             indices[2] = 2;
2538             indices[3] = 3;
2539             indices[4] = 4;
2540             indices[5] = 5;
2541             indices[6] = 6;
2542             indices[7] = 7;
2543             indices[8] = 8;
2544             indices[9] = 9;
2545             indices[10] = 10;
2546
2547                         Array.Sort ((Array)doubleValues, (Array)indices);
2548                         Assert.AreEqual (4, indices [0]);
2549         }
2550
2551         [Test]
2552         public void TestInitializeEmpty()
2553         {
2554                 bool catched=false;
2555                 int[] a = {};
2556                 try
2557                 {
2558                         a.Initialize();
2559                 }
2560                 catch(Exception)
2561                 {
2562                         catched=true;
2563                 }
2564                 Assert.IsTrue (!catched, "#TI01");
2565         }
2566
2567         [Test]
2568         public void TestInitializeInt()
2569         {
2570                 int[] a = {1,2,0};
2571                 a.Initialize();
2572                 int[] b = {1,2,0};
2573                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2574                 {
2575                         Assert.AreEqual (a[i], b[i], "#TI02 " + i);
2576                 }
2577         }
2578
2579         [Test]
2580         public void TestInitializeDouble()
2581         {
2582                 double[] a = {1.0,2.0,0.0};
2583                 a.Initialize();
2584                 double[] b = {1.0,2.0,0.0};
2585                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2586                 {
2587                         Assert.AreEqual (a[i], b[i], "#TI03 " + i);
2588                 }
2589         }
2590
2591         [Test]
2592         public void TestInitializeFloat()
2593         {
2594                 float[] a = {1.0F,2.0F,0.0F};
2595                 a.Initialize();
2596                 float[] b = {1.0F,2.0F,0.0F};
2597                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2598                 {
2599                         Assert.AreEqual (a[i], b[i], "#TI04 " + i);
2600                 }
2601         }
2602
2603         [Test]
2604         public void TestInitializeChar()
2605         {
2606                 char[] a = {'1','.','0','F','2','.','0','F'};
2607                 a.Initialize();
2608                 char[] b = {'1','.','0','F','2','.','0','F'};
2609                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2610                 {
2611                         Assert.AreEqual (a[i], b[i], "#TI05 " + i);
2612                 }
2613         }
2614
2615         [Test]
2616         public void TestInitializeString()
2617         {
2618                 string[] a = {"hola","adios","menos","mas"};
2619                 a.Initialize();
2620                 string[] b = {"hola","adios","menos","mas"};
2621                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2622                 {
2623                         Assert.AreEqual (a[i], b[i], "#TI06 " + i);
2624                 }
2625         }
2626
2627         [Test]
2628         public void TestInitializeEnum()
2629         {
2630                 enua[] a = {enua.hola,enua.adios,enua.menos,enua.mas};
2631                 a.Initialize();
2632                 enua[] b = {enua.hola,enua.adios,enua.menos,enua.mas};
2633                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2634                 {
2635                         Assert.AreEqual (a[i], b[i], "#TI07 " + i);
2636                 }
2637         }
2638         
2639         [Test]
2640         public void TestInitializeIntNI()
2641         {
2642                 int[] a = new int[20];
2643                 a.Initialize();
2644                 foreach(int b in a)
2645                 {
2646                         Assert.AreEqual (b, 0, "#TI08");
2647                 }
2648         }
2649         
2650         [Test]
2651         public void TestInitializeCharNI()
2652         {
2653                 char[] a = new char[20];
2654                 a.Initialize();
2655                 foreach(char b in a)
2656                 {
2657                         Assert.AreEqual (b, 0, "#TI09");
2658                 }
2659         }
2660         
2661         [Test]
2662         public void TestInitializeDoubleNI()
2663         {
2664                 double[] a = new double[20];
2665                 a.Initialize();
2666                 foreach(double b in a)
2667                 {
2668                         Assert.AreEqual (b, 0.0, "#TI09");
2669                 }
2670         }
2671         
2672         [Test]
2673         public void TestInitializeStringNI()
2674         {
2675                 string[] a = new string[20];
2676                 a.Initialize();
2677                 foreach(string b in a)
2678                 {
2679                         Assert.AreEqual (b, null, "#TI10");
2680                 }
2681         }
2682         
2683         [Test]
2684         public void TestInitializeObjectNI()
2685         {
2686                 object[] a = new object[20];
2687                 a.Initialize();
2688                 foreach(object b in a)
2689                 {
2690                         Assert.AreEqual (b, null, "#TI11");
2691                 }
2692         }
2693
2694         [Test]
2695         public void TestInitializeAClassNI()
2696         {
2697                 AClass[] a = new AClass[20];
2698                 a.Initialize();
2699                 foreach(AClass b in a)
2700                 {
2701                         Assert.AreEqual (b, null, "#TI12");
2702                 }
2703         }
2704
2705
2706         [Test]
2707         public void TestInitializeAStructNI()
2708         {
2709                 AStruct[] a = new AStruct[20];
2710                 a.Initialize();
2711                 foreach(AStruct b in a)
2712                 {
2713                         Assert.AreEqual (b, new AStruct(), "#TI14");
2714                 }
2715         }
2716
2717         [Test]
2718         public void TestInitializeAStruct()
2719         {
2720                 AStruct[] a = new AStruct[3];
2721                 a[1].a = "ADIOS";
2722                 a[1].s = "HOLA";
2723                 a.Initialize();
2724                 AStruct[] b = new AStruct[3];
2725                 b[1].a = "ADIOS";
2726                 b[1].s = "HOLA";
2727                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2728                 {
2729                         Assert.AreEqual (a[i], b[i], "#TI15 " + i);
2730                 }
2731         }
2732
2733         [Test]
2734         public void TestInitializeDateTimeNI()
2735         {
2736                 DateTime[] a = new DateTime[20];
2737                 a.Initialize();
2738                 foreach(DateTime b in a)
2739                 {
2740                         Assert.AreEqual (b, new DateTime(), "#TI16");
2741                 }
2742         }
2743         
2744         [Test]
2745         [ExpectedException (typeof (ArgumentNullException))]
2746         public void MoreSort1 ()
2747         {
2748                 Array.Sort (null, 0, 1);
2749         }
2750
2751         [Test]
2752         [ExpectedException (typeof (ArgumentOutOfRangeException))]
2753         public void MoreSort2 ()
2754         {
2755                 Array.Sort (arrsort, -1, 3);
2756         }
2757
2758         [Test]
2759         [ExpectedException (typeof (ArgumentOutOfRangeException))]
2760         public void MoreSort3 ()
2761         {
2762                 Array.Sort (arrsort, 1, -3);
2763         }
2764
2765         [Test]
2766         [ExpectedException (typeof (ArgumentException))]
2767         public void MoreSort4 ()
2768         {
2769                 Array.Sort (arrsort, arrsort.Length, arrsort.Length + 2);
2770         }
2771
2772         [Test]
2773         [ExpectedException (typeof (RankException))]
2774         public void MoreSort5 ()
2775         {
2776                 char [,] arr = new char [,] {{'a'}, {'b'}};
2777                 Array.Sort (arr, 0, 1);
2778         }
2779
2780         [Test]
2781         public void MoreSort6 ()
2782         {
2783                 Array.Sort (arrsort, 0, 0);
2784         }
2785
2786         [Test]
2787         [ExpectedException (typeof (ArgumentException))]
2788         public void MoreSort7 ()
2789         {
2790                 Array.Sort (arrsort, arrsort.Length - 1, 2);
2791         }
2792
2793         [Test]
2794         [ExpectedException (typeof (ArgumentException))]
2795         public void MoreSort8 ()
2796         {
2797                 Array.Sort (arrsort, 0, arrsort.Length + 1);
2798         }
2799
2800         [Test]
2801         public void MoreSort9 ()
2802         {
2803                 Array.Sort (arrsort, null, 0, arrsort.Length, null);
2804         }
2805
2806         [Test]
2807         [ExpectedException (typeof (InvalidOperationException))]
2808         public void MoreSort10 ()
2809         {
2810                 object [] array = {true, 'k', SByte.MinValue, Byte.MinValue, (short) 2, 634, (long) 436, (float) 1.1, 1.23, "Hello World"};
2811                 Array.Sort (array, (IComparer) null);
2812         }
2813
2814         [Test] // bug #81941
2815         public void Sort ()
2816         {
2817                 double [] a = new double [2] { 0.9, 0.3 };
2818                 uint [] b = new uint [2] { 4, 7 };
2819                 Array.Sort (a, b);
2820                 Assert.AreEqual (0.3, a [0], "#1");
2821                 Assert.AreEqual (0.9, a [1], "#2");
2822                 Assert.AreEqual (7, b [0], "#3");
2823                 Assert.AreEqual (4, b [1], "#4");
2824         }
2825
2826         [Test]
2827         public void ClearJaggedArray () 
2828         {
2829                 byte[][] matrix = new byte [8][];
2830                 for (int i=0; i < 8; i++) {
2831                         matrix [i] = new byte [8];
2832                         for (int j=0; j < 8; j++) {
2833                                 matrix [i][j] = 1;
2834                         }
2835                 }
2836                 Array.Clear (matrix, 0, 8);
2837                 for (int i=0; i < 8; i++) {
2838                         Assert.IsNull (matrix [i], i.ToString ());
2839                 }
2840         }
2841
2842         [Test]
2843         public void ClearMultidimentionalArray () 
2844         {
2845                 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2846                 Array.Clear (matrix, 0, 2);
2847                 Assert.AreEqual (0, matrix [0, 0], "0,0");
2848                 Assert.AreEqual (0, matrix [0, 1], "0,1");
2849                 Assert.AreEqual (2, matrix [1, 0], "1,0");
2850                 Assert.AreEqual (2, matrix [1, 1], "1,1");
2851         }
2852
2853         [Test]
2854         [ExpectedException (typeof (IndexOutOfRangeException))]
2855         public void ClearOutsideMultidimentionalArray () 
2856         {
2857                 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2858                 Array.Clear (matrix, 0, 5);
2859         }
2860
2861         [Test]
2862         [ExpectedException (typeof (IndexOutOfRangeException))]
2863         public void Clear_IndexOverflow () 
2864         {
2865                 byte[] array = new byte [16];
2866                 Array.Clear (array, 4, Int32.MaxValue);
2867         }
2868
2869         [Test]
2870         [ExpectedException (typeof (IndexOutOfRangeException))]
2871         public void Clear_LengthOverflow () 
2872         {
2873                 byte[] array = new byte [16];
2874                 Array.Clear (array, Int32.MaxValue, 4);
2875         }
2876
2877         [Test]
2878         [ExpectedException (typeof (ArgumentException))]
2879         public void Copy_SourceIndexOverflow () 
2880         {
2881                 byte[] array = new byte [16];
2882                 Array.Copy (array, Int32.MaxValue, array, 8, 8);
2883         }
2884
2885         [Test]
2886         [ExpectedException (typeof (ArgumentException))]
2887         public void Copy_DestinationIndexOverflow () 
2888         {
2889                 byte[] array = new byte [16];
2890                 Array.Copy (array, 8, array, Int32.MaxValue, 8);
2891         }
2892
2893         [Test]
2894         [ExpectedException (typeof (ArgumentException))]
2895         public void Copy_LengthOverflow () 
2896         {
2897                 byte[] array = new byte [16];
2898                 Array.Copy (array, 8, array, 8, Int32.MaxValue);
2899         }
2900
2901         [Test]
2902         [ExpectedException (typeof (ArgumentException))]
2903         public void Reverse_IndexOverflow () 
2904         {
2905                 byte[] array = new byte [16];
2906                 Array.Reverse (array, Int32.MaxValue, 8);
2907         }
2908
2909         [Test]
2910         [ExpectedException (typeof (ArgumentException))]
2911         public void Reverse_LengthOverflow () 
2912         {
2913                 byte[] array = new byte [16];
2914                 Array.Reverse (array, 8, Int32.MaxValue);
2915         }
2916         
2917         public struct CharX : IComparable {
2918                 public char c;
2919         
2920                 public CharX (char c)
2921                 {
2922                         this.c = c;
2923                 }
2924         
2925                 public int CompareTo (object obj)
2926                 {
2927                         if (obj is CharX)
2928                                 return c.CompareTo (((CharX) obj).c);
2929                         else
2930                                 return c.CompareTo (obj);
2931                 }
2932         }
2933
2934         [Test]
2935         public void BinarySearch_ArgPassingOrder ()
2936         {
2937                 //
2938                 // This tests that arguments are passed to the comprer in the correct
2939                 // order. The IComparable of the *array* elements must get called, not
2940                 // that of the search object.
2941                 //
2942                 CharX [] x = { new CharX ('a'), new CharX ('b'), new CharX ('c') };
2943                 Assert.AreEqual (1, Array.BinarySearch (x, 'b'));
2944         }
2945
2946         class Comparer: IComparer {
2947
2948                 private bool called = false;
2949
2950                 public bool Called {
2951                         get {
2952                                 bool result = called;
2953                                 called = false;
2954                                 return called;
2955                         }
2956                 }
2957
2958                 public int Compare (object x, object y)
2959                 {
2960                         called = true;
2961                         return 0;
2962                 }
2963         }
2964
2965         [Test]
2966         public void BinarySearch1_EmptyList ()
2967         {
2968                 int[] array = new int[0];
2969                 Assert.AreEqual (- 1, Array.BinarySearch (array, 0), "BinarySearch");
2970         }
2971
2972         [Test]
2973         public void BinarySearch2_EmptyList ()
2974         {
2975                 int[] array = new int[0];
2976                 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, 0), "BinarySearch");
2977         }
2978
2979         [Test]
2980         public void BinarySearch3_EmptyList ()
2981         {
2982                 Comparer comparer = new Comparer ();
2983                 int[] array = new int[0];
2984                 Assert.AreEqual (-1, Array.BinarySearch (array, 0, comparer), "BinarySearch");
2985                 // bug 77030 - the comparer isn't called for an empty array/list
2986                 Assert.IsTrue (!comparer.Called, "Called");
2987         }
2988
2989         [Test]
2990         public void BinarySearch4_EmptyList ()
2991         {
2992                 Comparer comparer = new Comparer ();
2993                 int[] array = new int[0];
2994                 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, comparer), "BinarySearch");
2995                 // bug 77030 - the comparer isn't called for an empty array/list
2996                 Assert.IsTrue (!comparer.Called, "Called");
2997         }
2998
2999         [Test]
3000         [ExpectedException (typeof (ArgumentNullException))]
3001         public void AsReadOnly_NullArray ()
3002         {
3003                 Array.AsReadOnly <int> (null);
3004         }
3005
3006         [Test]
3007         public void ReadOnly_Count ()
3008         {
3009                 Assert.AreEqual (10, Array.AsReadOnly (new int [10]).Count);
3010         }
3011
3012         [Test]
3013         public void ReadOnly_Contains ()
3014         {
3015                 int[] arr = new int [2];
3016                 arr [0] = 3;
3017                 arr [1] = 5;
3018                 IList<int> a = Array.AsReadOnly (arr);
3019
3020                 Assert.IsTrue (a.Contains (3));
3021                 Assert.IsTrue (!a.Contains (6));
3022         }
3023
3024         [Test]
3025         public void ReadOnly_IndexOf ()
3026         {
3027                 int[] arr = new int [2];
3028                 arr [0] = 3;
3029                 arr [1] = 5;
3030                 IList<int> a = Array.AsReadOnly (arr);
3031
3032                 Assert.AreEqual (0, a.IndexOf (3));
3033                 Assert.AreEqual (1, a.IndexOf (5));
3034                 Assert.AreEqual (-1, a.IndexOf (6));
3035         }
3036
3037         [Test]
3038         public void ReadOnly_Indexer ()
3039         {
3040                 int[] arr = new int [2];
3041                 arr [0] = 3;
3042                 arr [1] = 5;
3043                 IList<int> a = Array.AsReadOnly (arr);
3044
3045                 Assert.AreEqual (3, a [0]);
3046                 Assert.AreEqual (5, a [1]);
3047
3048                 /* Check that modifications to the original array are visible */
3049                 arr [0] = 6;
3050                 Assert.AreEqual (6, a [0]);
3051         }
3052
3053         [Test]
3054         public void ReadOnly_Enumerator ()
3055         {
3056                 int[] arr = new int [10];
3057
3058                 for (int i = 0; i < 10; ++i)
3059                         arr [i] = i;
3060
3061                 int sum = 0;
3062                 foreach (int i in Array.AsReadOnly (arr))
3063                         sum += i;
3064
3065                 Assert.AreEqual (45, sum);
3066         }
3067
3068         [Test]
3069         public void ReadOnly_CopyTo ()
3070         {
3071                 int[] arr = new int [2];
3072                 arr [0] = 3;
3073                 arr [1] = 5;
3074                 IList<int> a = Array.AsReadOnly (arr);
3075
3076                 int[] arr2 = new int [3];
3077                 a.CopyTo (arr2, 1);
3078
3079                 Assert.AreEqual (0, arr2 [0]);
3080                 Assert.AreEqual (3, arr2 [1]);
3081                 Assert.AreEqual (5, arr2 [2]);
3082         }
3083
3084         [Test]
3085         public void Resize ()
3086         {
3087                 int [] arr = new int [] { 1, 3, 5 };
3088                 Array.Resize <int> (ref arr, 3);
3089                 Assert.AreEqual (3, arr.Length, "#A1");
3090                 Assert.AreEqual (1, arr [0], "#A2");
3091                 Assert.AreEqual (3, arr [1], "#A3");
3092                 Assert.AreEqual (5, arr [2], "#A4");
3093
3094                 Array.Resize <int> (ref arr, 2);
3095                 Assert.AreEqual (2, arr.Length, "#B1");
3096                 Assert.AreEqual (1, arr [0], "#B2");
3097                 Assert.AreEqual (3, arr [1], "#B3");
3098
3099                 Array.Resize <int> (ref arr, 4);
3100                 Assert.AreEqual (4, arr.Length, "#C1");
3101                 Assert.AreEqual (1, arr [0], "#C2");
3102                 Assert.AreEqual (3, arr [1], "#C3");
3103                 Assert.AreEqual (0, arr [2], "#C4");
3104                 Assert.AreEqual (0, arr [3], "#C5");
3105         }
3106
3107         [Test]
3108         public void Resize_null ()
3109         {
3110                 int [] arr = null;
3111                 Array.Resize (ref arr, 10);
3112                 Assert.AreEqual (arr.Length, 10);
3113         }
3114
3115         [Test]
3116         public void Test_ContainsAndIndexOf_EquatableItem ()
3117         {
3118                 EquatableClass[] list = new EquatableClass[] {new EquatableClass (0), new EquatableClass (1), new EquatableClass (0)};
3119
3120                 Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, list[0]), "#0");
3121                 Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, new EquatableClass (0)), "#1");
3122                 Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, list[0]), "#2");
3123                 Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, new EquatableClass (0)), "#3");
3124         }
3125
3126         public class EquatableClass : IEquatable<EquatableClass>
3127         {
3128                 int _x;
3129                 public EquatableClass (int x)
3130                 {
3131                         _x = x;
3132                 }
3133
3134                 public bool Equals (EquatableClass other)
3135                 {
3136                         return this._x == other._x;
3137                 }
3138         }
3139
3140         [Test]
3141         public void AsIList ()
3142         {
3143                 IList<int> arr = new int [10];
3144                 arr [0] = 5;
3145                 Assert.AreEqual (5, arr [0]);
3146
3147                 IList<FooStruct> arr2 = new FooStruct [10];
3148                 FooStruct s = new FooStruct ();
3149                 s.i = 11;
3150                 s.j = 22;
3151                 arr2 [5] = s;
3152                 s = arr2 [5];
3153                 Assert.AreEqual (11, s.i);
3154                 Assert.AreEqual (22, s.j);
3155
3156                 IList<string> arr3 = new string [10];
3157                 arr3 [5] = "ABC";
3158                 Assert.AreEqual ("ABC", arr3 [5]);
3159         }
3160
3161         struct FooStruct {
3162                 public int i, j;
3163         }
3164
3165         [Test]
3166         // From bug #80563
3167         public void ICollectionNull ()
3168         {
3169                 ICollection<object> test;
3170                 
3171                 test = new List<object>();
3172                 Assert.AreEqual (test.Contains (null), false, "list<o>");
3173
3174                 test = new object[] {};
3175                 Assert.AreEqual (test.Contains (null), false, "empty array");
3176
3177                 test = new object[] {null};
3178                 Assert.AreEqual (test.Contains (null), true, "array with null");
3179
3180                 test = new object[] { 1, null};
3181                 Assert.IsTrue (test.Contains (null), "array with last null");
3182                 
3183                 test = new List<object>(test);
3184                 Assert.AreEqual (test.Contains (null), true, "List<object> with test");
3185                 
3186                 test = new object[] {new object()};
3187                 Assert.AreEqual (test.Contains (null), false, "array with object");
3188
3189                 test = new List<object>(test);
3190                 Assert.AreEqual (test.Contains (null), false, "array with test");
3191         }
3192         
3193         [Test]
3194         public void IListNull ()
3195         {
3196                 IList<object> test;
3197                 
3198                 test = new List<object>();
3199                 Assert.AreEqual (-1, test.IndexOf (null), "list<o>");
3200
3201                 test = new object[] {};
3202                 Assert.AreEqual (-1, test.IndexOf (null), "empty array");
3203
3204                 test = new object[] {null};
3205                 Assert.AreEqual (0, test.IndexOf (null), "array with null");
3206
3207                 test = new object[] { 1, null};
3208                 Assert.AreEqual (1, test.IndexOf (null), "array with last null");
3209                 
3210                 test = new List<object>(test);
3211                 Assert.AreEqual (1, test.IndexOf (null), "List<object> with test");
3212                 
3213                 test = new object[] {new object()};
3214                 Assert.AreEqual (-1, test.IndexOf (null), "array with object");
3215
3216                 test = new List<object>(test);
3217                 Assert.AreEqual (-1, test.IndexOf (null), "array with test");
3218         }
3219         
3220
3221         #region Bug 80299
3222
3223         enum ByteEnum : byte {}
3224         enum IntEnum : int {}
3225
3226         [Test]
3227         public void TestByteEnumArrayToByteArray ()
3228         {
3229                 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
3230                 byte[] b = new byte[a.Length];
3231                 a.CopyTo (b, 0);
3232         }
3233
3234         [Test]
3235         public void TestByteEnumArrayToIntArray ()
3236         {
3237                 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
3238                 int[] b = new int[a.Length];
3239                 a.CopyTo (b, 0);
3240         }
3241
3242         [Test]
3243         [ExpectedException (typeof (ArrayTypeMismatchException))]
3244         public void TestIntEnumArrayToByteArray ()
3245         {
3246                 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
3247                 byte[] b = new byte[a.Length];
3248                 a.CopyTo (b, 0);
3249         }
3250
3251         [Test]
3252         public void TestIntEnumArrayToIntArray ()
3253         {
3254                 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
3255                 int[] b = new int[a.Length];
3256                 a.CopyTo (b, 0);
3257         }
3258
3259         #endregion
3260
3261         [Test] // bug #322248
3262         public void IEnumerator_Reset ()
3263         {
3264                 int[] array = new int[] { 1, 2, 3};
3265                 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3266                 Assert.IsTrue (e.MoveNext (), "#A1");
3267                 Assert.AreEqual (1, e.Current, "#A2");
3268                 Assert.IsTrue (e.MoveNext (), "#A3");
3269                 Assert.AreEqual (2, e.Current, "#A4");
3270
3271                 e.Reset ();
3272
3273                 Assert.IsTrue (e.MoveNext (), "#C1");
3274                 Assert.AreEqual (1, e.Current, "#C2");
3275         }
3276
3277         [Test]
3278         public void IEnumerator_Current_Finished ()
3279         {
3280                 int[] array = new int[] { 1, 2, 3 };
3281                 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3282                 Assert.IsTrue (e.MoveNext (), "#A1");
3283                 Assert.AreEqual (1, e.Current, "#A2");
3284                 Assert.IsTrue (e.MoveNext (), "#A3");
3285                 Assert.AreEqual (2, e.Current, "#A4");
3286                 Assert.IsTrue (e.MoveNext (), "#A5");
3287                 Assert.AreEqual (3, e.Current, "#A6");
3288                 Assert.IsTrue (!e.MoveNext (), "#A6");
3289
3290                 try {
3291                         Assert.Fail ("#B1:" + e.Current);
3292                 } catch (InvalidOperationException ex) {
3293                         // Enumeration already finished
3294                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3295                         Assert.IsNull (ex.InnerException, "#B3");
3296                         Assert.IsNotNull (ex.Message, "#B4");
3297                 }
3298         }
3299
3300         [Test]
3301         public void IEnumerator_Current_NotStarted ()
3302         {
3303                 int[] array = new int[] { 1, 2, 3 };
3304                 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3305
3306                 try {
3307                         Assert.Fail ("#A1:" + e.Current);
3308                 } catch (InvalidOperationException ex) {
3309                         // Enumeration has not started. Call MoveNext
3310                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
3311                         Assert.IsNull (ex.InnerException, "#A3");
3312                         Assert.IsNotNull (ex.Message, "#A4");
3313                 }
3314         }
3315
3316         [Test]
3317         public void IEnumerator_Current_Reset ()
3318         {
3319                 int[] array = new int[] { 1, 2, 3 };
3320                 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3321                 e.MoveNext ();
3322                 e.Reset ();
3323
3324                 try {
3325                         Assert.Fail ("#B1:" + e.Current);
3326                 } catch (InvalidOperationException ex) {
3327                         // Enumeration has not started. Call MoveNext
3328                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3329                         Assert.IsNull (ex.InnerException, "#B3");
3330                         Assert.IsNotNull (ex.Message, "#B4");
3331                 }
3332         }
3333
3334         public void ICollection_IsReadOnly() {
3335                 ICollection<string> arr = new string [10];
3336
3337                 Assert.IsTrue (arr.IsReadOnly);
3338         }
3339
3340         [Test]
3341         [ExpectedException (typeof (NotSupportedException))]
3342         public void ArrayCreateInstanceOfVoid ()
3343         {
3344                 Array.CreateInstance (typeof (void), 42);
3345         }
3346
3347         class Foo<T> {}
3348
3349         [Test]
3350         [ExpectedException (typeof (NotSupportedException))]
3351         public void ArrayCreateInstanceOfOpenGenericType ()
3352         {
3353                 Array.CreateInstance (typeof (Foo<>), 42);
3354         }
3355
3356         [Test]
3357         [ExpectedException (typeof (IndexOutOfRangeException))]
3358         public void ClearNegativeLength ()
3359         {
3360                 Array.Clear (new int [] { 1, 2 }, 0, -1);
3361         }
3362
3363         [Test]
3364         [ExpectedException (typeof (ArgumentException))]
3365         public void MultiDimension_IList_setItem ()
3366         {
3367                 IList array = new int [1, 1];
3368                 array [0] = 2;
3369         }
3370
3371         [Test]
3372         [ExpectedException (typeof (ArgumentException))]
3373         public void MultiDimension_IList_getItem ()
3374         {
3375                 IList array = new int [1, 1];
3376                 int a = (int) array [0];
3377         }
3378
3379         [Test]
3380         public void SetValue_Nullable () {
3381                 Array array = Array.CreateInstance (typeof (int?), 7);
3382
3383                 object o = 42;
3384
3385                 array.SetValue (o, 0);
3386                 Assert.AreEqual (42, array.GetValue (0));
3387
3388                 array.SetValue (null, 0);
3389                 Assert.AreEqual (null, array.GetValue (0));
3390         }
3391
3392         [Test]
3393         public void SortNullsWithGenericVersion ()
3394         {
3395             string[] s1 = new string[6]{
3396                 "J",
3397                 "M",
3398                  null,
3399                 "P",
3400                 "T",
3401                 "A"};
3402
3403             string[] s2 = new string[]{null,
3404                 "A",
3405                 "J",
3406                 "M",
3407                 "P",
3408                 "T"};
3409
3410             Array.Sort<string> (s1);
3411             for (int i = 0; i < 6; i++) {
3412                     Assert.AreEqual (s1[i], s2[i], "At:" + i);
3413             }
3414         }
3415         
3416         //
3417         // This is a test case for the case that was broken by the code contributed
3418         // for bug  #351638.
3419         //
3420         // This tests the fix for: #622101
3421         //
3422         [Test]
3423         public void SortActuallyWorks ()
3424         {
3425                 string[] data = new string[9]{"Foo", "Bar", "Dingus", null, "Dingu4", "123", "Iam", null, "NotNull"};
3426                 IComparer comparer = new NullAtEndComparer ();
3427                 Array.Sort (data, comparer);
3428
3429                 Assert.AreEqual (data [7], null);
3430                 Assert.AreNotEqual (data [0], null);
3431         }
3432
3433         class NullAtEndComparer : IComparer {
3434                 public int Compare(object x, object y)
3435                 {
3436                         if (x == null) return 1;
3437                         if (y == null) return -1;
3438                         return ((string)x).CompareTo((string)y);
3439                 }
3440         }
3441
3442         [Test] //bxc #11184
3443         public void UnalignedArrayClear ()
3444         {
3445                 byte[] input = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
3446                 byte[] expected = new byte[] { 1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
3447                 Array.Clear (input, 5, 11);
3448                 
3449                 Assert.AreEqual (input, expected);
3450         }
3451
3452         [Test]
3453         [ExpectedException (typeof (ArgumentException))]
3454         public void CompareToWithJaggedArray () {
3455                 IStructuralComparable a = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }};
3456                 IStructuralComparable b = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }};
3457                 a.CompareTo (b, Comparer<object>.Default);
3458         }
3459
3460         [Test]
3461         [ExpectedException (typeof (ArgumentException))]
3462         public void CompareToWithArrayOfTheWrongKind () {
3463                 IStructuralComparable a = new int[] { 1, 2 };
3464                 IStructuralComparable b = new double[] { 1, 2 };
3465                 a.CompareTo (b, Comparer<object>.Default);
3466         }
3467
3468         [Test]
3469         [ExpectedException (typeof (ArgumentException))]
3470         public void CompareToWithNonArrayType () {
3471                 IStructuralComparable a = new int[] { 1, 2 };
3472                 a.CompareTo (99, Comparer<object>.Default);
3473         }
3474
3475         [Test]
3476         [ExpectedException (typeof (ArgumentException))]
3477         public void CompareToWithNonArrayOfDifferentSize () {
3478                 IStructuralComparable a = new int[] { 1, 2 };
3479                 IStructuralComparable b = new int[] { 1, 2, 3 };
3480                 a.CompareTo (b, Comparer<object>.Default);
3481         }
3482
3483         [Test]
3484         [ExpectedException (typeof (ArgumentException))]
3485         public void CompareToWithMultiDimArray1 () {
3486                 IStructuralComparable a = new int [2,2] { {10, 10 }, { 10, 10 } };
3487                 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3488                 a.CompareTo (b, Comparer<object>.Default);
3489         }
3490
3491         [Test]
3492         [ExpectedException (typeof (ArgumentException))]
3493         public void CompareToWithMultiDimArray2 () {
3494                 IStructuralComparable a = new int [2] { 10, 10 };
3495                 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3496                 a.CompareTo (b, Comparer<object>.Default);
3497         }
3498
3499         [Test]
3500         [ExpectedException (typeof (ArgumentException))]
3501         public void CompareToWithMultiDimArray3 () {
3502                 IStructuralComparable a = new int [4] { 10, 10, 10, 10 };
3503                 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3504                 a.CompareTo (b, Comparer<object>.Default);
3505         }
3506
3507         [Test]
3508         [ExpectedException (typeof (IndexOutOfRangeException))]
3509         public void CompareToWithBoundedArray1 () {
3510                 IStructuralComparable a = new int [2] { 10, 10 };
3511                 Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 });
3512                 IStructuralComparable b = ab;
3513                 ab.SetValue (10, 5);
3514                 ab.SetValue (10, 6);
3515
3516                 a.CompareTo (b, Comparer<object>.Default);
3517         }
3518
3519         [Test]
3520         [ExpectedException (typeof (IndexOutOfRangeException))]
3521         public void CompareToWithBoundedArray2 () {
3522                 IStructuralComparable a = new int [2] { 10, 10 };
3523                 Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 });
3524                 IStructuralComparable b = ab;
3525                 ab.SetValue (10, 5);
3526                 ab.SetValue (10, 6);
3527
3528                 //Yes, CompareTo simply doesn't work with bounded arrays!
3529                 b.CompareTo (b, Comparer<object>.Default);
3530         }
3531
3532         [Test]
3533         [ExpectedException (typeof (NullReferenceException))]
3534         public void CompareToWithNullComparer () {
3535                 IStructuralComparable a = new int[] { 1, 2 };
3536                 IStructuralComparable b = new int[] { 1, 2 };
3537                 a.CompareTo (b, null);
3538         }
3539
3540         [Test]
3541         public void CompareToWithNullArray () {
3542                 IStructuralComparable a = new int[] { 1, 2 };
3543                 Assert.AreEqual (1, a.CompareTo (null, Comparer<object>.Default));
3544         }
3545
3546         [Test]
3547         public void CompareToWithGoodArrays () {
3548                 IStructuralComparable a = new int[] { 10, 20 };
3549                 Assert.AreEqual (0, a.CompareTo (a, Comparer<object>.Default));
3550                 Assert.AreEqual (0, a.CompareTo (new int [] { 10, 20 }, Comparer<object>.Default));
3551                 Assert.AreEqual (-1, a.CompareTo (new int [] { 11, 20 }, Comparer<object>.Default));
3552                 Assert.AreEqual (-1, a.CompareTo (new int [] { 10, 21 }, Comparer<object>.Default));
3553                 Assert.AreEqual (1, a.CompareTo (new int [] { 9, 20 }, Comparer<object>.Default));
3554                 Assert.AreEqual (1, a.CompareTo (new int [] { 10, 19 }, Comparer<object>.Default));
3555         }
3556
3557         [Test]
3558         public void IStructuralEquatable_Equals ()
3559         {
3560                 IStructuralEquatable array = new int[] {1, 2, 3};
3561                 IStructuralEquatable array2 = new int[] {1, 2, 3};
3562                 Assert.AreEqual (false, array.Equals (null, null));
3563                 Assert.AreEqual (true, array.Equals (array, null));
3564                 Assert.AreEqual (true, array.Equals (array2, EqualityComparer<int>.Default));
3565         }
3566
3567         [Test]
3568         [ExpectedException (typeof (NullReferenceException))]
3569         public void IStructuralEquatable_Equals_NoComparer ()
3570         {
3571                 IStructuralEquatable array = new int[] {1, 2, 3};
3572                 IStructuralComparable array2 = new int[] {1, 2, 3};
3573                 array.Equals (array2, null);
3574         }
3575
3576         [Test]
3577         [ExpectedException (typeof (ArgumentException))]
3578         public void IStructuralEquatable_Equals_ComparerThrows ()
3579         {
3580                 IStructuralEquatable array = new int[] {1, 2, 3};
3581                 IStructuralComparable array2 = new int[] {1, 2, 3};
3582                 array.Equals (array2, EqualityComparer<long>.Default);
3583         }
3584
3585         [Test]
3586         [ExpectedException (typeof (ArgumentNullException))]    
3587         public void IStructuralEquatable_GetHashCode_NullComparer ()
3588         {
3589                 IStructuralEquatable a = new int[] { 1, 2 };
3590                 a.GetHashCode (null);
3591         }
3592
3593         class TestComparer_GetHashCode : IEqualityComparer
3594         {
3595                 public int Counter;
3596
3597                 bool IEqualityComparer.Equals (object x, object y)
3598                 {
3599                         throw new NotImplementedException ();
3600                 }
3601
3602                 public int GetHashCode (object obj)
3603                 {
3604                         return Counter++;
3605                 }
3606         }
3607
3608         [Test]
3609         public void IStructuralEquatable_GetHashCode ()
3610         {
3611                 IStructuralEquatable a = new int[] { 1, 2, 9 };
3612
3613                 var c = new TestComparer_GetHashCode ();
3614                 a.GetHashCode (c);
3615                 Assert.AreEqual (3, c.Counter);         
3616         }
3617
3618
3619         [Test]
3620         public void JaggedArrayCtor ()
3621         {
3622         var type = Type.GetType ("System.Object[][]");
3623
3624                 ConstructorInfo ctor = null;
3625         foreach (var c in type.GetConstructors ()) {
3626                         if (c.GetParameters ().Length == 2)
3627                                 ctor = c;
3628                 }
3629                 Assert.IsNotNull (ctor);
3630                 var arr = (object[])ctor.Invoke (new object [] { 4, 10 });
3631                 for (int i = 0; i < 4; ++i) {
3632                         Assert.IsNotNull (arr [i]);
3633                         Assert.AreEqual (10, ((object[])arr [i]).Length);
3634                 }
3635         }
3636 }
3637 }