Merge pull request #2311 from mlancione/master
[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 TestSortComparable()
2553         {
2554                 int[] source = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
2555                 int[] expected = { 6, 5, 4, 3, 2, 1, 7, 8, 9 };
2556                 Comp[] c = { new Comp (100), new Comp (16), new Comp (11), new Comp (9), new Comp (0), new Comp (-100) };
2557                 IComparer<Comp> comp = null;
2558                 Array.Sort<Comp, int> (c, source, comp);
2559
2560                 Assert.AreEqual (expected, source);
2561         }
2562
2563         class Comp : IComparable
2564         {
2565                 readonly int val;
2566
2567                 public Comp (int a)
2568                 {
2569                         val = a;
2570                 }
2571
2572                 int IComparable.CompareTo (object obj)
2573                 {
2574                         return val.CompareTo ((obj as Comp).val);
2575                 }
2576         }
2577
2578         [Test]
2579         public void TestInitializeEmpty()
2580         {
2581                 bool catched=false;
2582                 int[] a = {};
2583                 try
2584                 {
2585                         a.Initialize();
2586                 }
2587                 catch(Exception)
2588                 {
2589                         catched=true;
2590                 }
2591                 Assert.IsTrue (!catched, "#TI01");
2592         }
2593
2594         [Test]
2595         public void TestInitializeInt()
2596         {
2597                 int[] a = {1,2,0};
2598                 a.Initialize();
2599                 int[] b = {1,2,0};
2600                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2601                 {
2602                         Assert.AreEqual (a[i], b[i], "#TI02 " + i);
2603                 }
2604         }
2605
2606         [Test]
2607         public void TestInitializeDouble()
2608         {
2609                 double[] a = {1.0,2.0,0.0};
2610                 a.Initialize();
2611                 double[] b = {1.0,2.0,0.0};
2612                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2613                 {
2614                         Assert.AreEqual (a[i], b[i], "#TI03 " + i);
2615                 }
2616         }
2617
2618         [Test]
2619         public void TestInitializeFloat()
2620         {
2621                 float[] a = {1.0F,2.0F,0.0F};
2622                 a.Initialize();
2623                 float[] b = {1.0F,2.0F,0.0F};
2624                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2625                 {
2626                         Assert.AreEqual (a[i], b[i], "#TI04 " + i);
2627                 }
2628         }
2629
2630         [Test]
2631         public void TestInitializeChar()
2632         {
2633                 char[] a = {'1','.','0','F','2','.','0','F'};
2634                 a.Initialize();
2635                 char[] b = {'1','.','0','F','2','.','0','F'};
2636                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2637                 {
2638                         Assert.AreEqual (a[i], b[i], "#TI05 " + i);
2639                 }
2640         }
2641
2642         [Test]
2643         public void TestInitializeString()
2644         {
2645                 string[] a = {"hola","adios","menos","mas"};
2646                 a.Initialize();
2647                 string[] b = {"hola","adios","menos","mas"};
2648                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2649                 {
2650                         Assert.AreEqual (a[i], b[i], "#TI06 " + i);
2651                 }
2652         }
2653
2654         [Test]
2655         public void TestInitializeEnum()
2656         {
2657                 enua[] a = {enua.hola,enua.adios,enua.menos,enua.mas};
2658                 a.Initialize();
2659                 enua[] b = {enua.hola,enua.adios,enua.menos,enua.mas};
2660                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2661                 {
2662                         Assert.AreEqual (a[i], b[i], "#TI07 " + i);
2663                 }
2664         }
2665         
2666         [Test]
2667         public void TestInitializeIntNI()
2668         {
2669                 int[] a = new int[20];
2670                 a.Initialize();
2671                 foreach(int b in a)
2672                 {
2673                         Assert.AreEqual (b, 0, "#TI08");
2674                 }
2675         }
2676         
2677         [Test]
2678         public void TestInitializeCharNI()
2679         {
2680                 char[] a = new char[20];
2681                 a.Initialize();
2682                 foreach(char b in a)
2683                 {
2684                         Assert.AreEqual (b, 0, "#TI09");
2685                 }
2686         }
2687         
2688         [Test]
2689         public void TestInitializeDoubleNI()
2690         {
2691                 double[] a = new double[20];
2692                 a.Initialize();
2693                 foreach(double b in a)
2694                 {
2695                         Assert.AreEqual (b, 0.0, "#TI09");
2696                 }
2697         }
2698         
2699         [Test]
2700         public void TestInitializeStringNI()
2701         {
2702                 string[] a = new string[20];
2703                 a.Initialize();
2704                 foreach(string b in a)
2705                 {
2706                         Assert.AreEqual (b, null, "#TI10");
2707                 }
2708         }
2709         
2710         [Test]
2711         public void TestInitializeObjectNI()
2712         {
2713                 object[] a = new object[20];
2714                 a.Initialize();
2715                 foreach(object b in a)
2716                 {
2717                         Assert.AreEqual (b, null, "#TI11");
2718                 }
2719         }
2720
2721         [Test]
2722         public void TestInitializeAClassNI()
2723         {
2724                 AClass[] a = new AClass[20];
2725                 a.Initialize();
2726                 foreach(AClass b in a)
2727                 {
2728                         Assert.AreEqual (b, null, "#TI12");
2729                 }
2730         }
2731
2732
2733         [Test]
2734         public void TestInitializeAStructNI()
2735         {
2736                 AStruct[] a = new AStruct[20];
2737                 a.Initialize();
2738                 foreach(AStruct b in a)
2739                 {
2740                         Assert.AreEqual (b, new AStruct(), "#TI14");
2741                 }
2742         }
2743
2744         [Test]
2745         public void TestInitializeAStruct()
2746         {
2747                 AStruct[] a = new AStruct[3];
2748                 a[1].a = "ADIOS";
2749                 a[1].s = "HOLA";
2750                 a.Initialize();
2751                 AStruct[] b = new AStruct[3];
2752                 b[1].a = "ADIOS";
2753                 b[1].s = "HOLA";
2754                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2755                 {
2756                         Assert.AreEqual (a[i], b[i], "#TI15 " + i);
2757                 }
2758         }
2759
2760         [Test]
2761         public void TestInitializeDateTimeNI()
2762         {
2763                 DateTime[] a = new DateTime[20];
2764                 a.Initialize();
2765                 foreach(DateTime b in a)
2766                 {
2767                         Assert.AreEqual (b, new DateTime(), "#TI16");
2768                 }
2769         }
2770         
2771         [Test]
2772         [ExpectedException (typeof (ArgumentNullException))]
2773         public void MoreSort1 ()
2774         {
2775                 Array.Sort (null, 0, 1);
2776         }
2777
2778         [Test]
2779         [ExpectedException (typeof (ArgumentOutOfRangeException))]
2780         public void MoreSort2 ()
2781         {
2782                 Array.Sort (arrsort, -1, 3);
2783         }
2784
2785         [Test]
2786         [ExpectedException (typeof (ArgumentOutOfRangeException))]
2787         public void MoreSort3 ()
2788         {
2789                 Array.Sort (arrsort, 1, -3);
2790         }
2791
2792         [Test]
2793         [ExpectedException (typeof (ArgumentException))]
2794         public void MoreSort4 ()
2795         {
2796                 Array.Sort (arrsort, arrsort.Length, arrsort.Length + 2);
2797         }
2798
2799         [Test]
2800         [ExpectedException (typeof (RankException))]
2801         public void MoreSort5 ()
2802         {
2803                 char [,] arr = new char [,] {{'a'}, {'b'}};
2804                 Array.Sort (arr, 0, 1);
2805         }
2806
2807         [Test]
2808         public void MoreSort6 ()
2809         {
2810                 Array.Sort (arrsort, 0, 0);
2811         }
2812
2813         [Test]
2814         [ExpectedException (typeof (ArgumentException))]
2815         public void MoreSort7 ()
2816         {
2817                 Array.Sort (arrsort, arrsort.Length - 1, 2);
2818         }
2819
2820         [Test]
2821         [ExpectedException (typeof (ArgumentException))]
2822         public void MoreSort8 ()
2823         {
2824                 Array.Sort (arrsort, 0, arrsort.Length + 1);
2825         }
2826
2827         [Test]
2828         public void MoreSort9 ()
2829         {
2830                 Array.Sort (arrsort, null, 0, arrsort.Length, null);
2831         }
2832
2833         [Test]
2834         [ExpectedException (typeof (InvalidOperationException))]
2835         public void MoreSort10 ()
2836         {
2837                 object [] array = {true, 'k', SByte.MinValue, Byte.MinValue, (short) 2, 634, (long) 436, (float) 1.1, 1.23, "Hello World"};
2838                 Array.Sort (array, (IComparer) null);
2839         }
2840
2841         [Test] // bug #81941
2842         public void Sort ()
2843         {
2844                 double [] a = new double [2] { 0.9, 0.3 };
2845                 uint [] b = new uint [2] { 4, 7 };
2846                 Array.Sort (a, b);
2847                 Assert.AreEqual (0.3, a [0], "#1");
2848                 Assert.AreEqual (0.9, a [1], "#2");
2849                 Assert.AreEqual (7, b [0], "#3");
2850                 Assert.AreEqual (4, b [1], "#4");
2851         }
2852
2853         [Test]
2854         public void ClearJaggedArray () 
2855         {
2856                 byte[][] matrix = new byte [8][];
2857                 for (int i=0; i < 8; i++) {
2858                         matrix [i] = new byte [8];
2859                         for (int j=0; j < 8; j++) {
2860                                 matrix [i][j] = 1;
2861                         }
2862                 }
2863                 Array.Clear (matrix, 0, 8);
2864                 for (int i=0; i < 8; i++) {
2865                         Assert.IsNull (matrix [i], i.ToString ());
2866                 }
2867         }
2868
2869         [Test]
2870         public void ClearMultidimentionalArray () 
2871         {
2872                 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2873                 Array.Clear (matrix, 0, 2);
2874                 Assert.AreEqual (0, matrix [0, 0], "0,0");
2875                 Assert.AreEqual (0, matrix [0, 1], "0,1");
2876                 Assert.AreEqual (2, matrix [1, 0], "1,0");
2877                 Assert.AreEqual (2, matrix [1, 1], "1,1");
2878         }
2879
2880         [Test]
2881         [ExpectedException (typeof (IndexOutOfRangeException))]
2882         public void ClearOutsideMultidimentionalArray () 
2883         {
2884                 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2885                 Array.Clear (matrix, 0, 5);
2886         }
2887
2888         [Test]
2889         [ExpectedException (typeof (IndexOutOfRangeException))]
2890         public void Clear_IndexOverflow () 
2891         {
2892                 byte[] array = new byte [16];
2893                 Array.Clear (array, 4, Int32.MaxValue);
2894         }
2895
2896         [Test]
2897         [ExpectedException (typeof (IndexOutOfRangeException))]
2898         public void Clear_LengthOverflow () 
2899         {
2900                 byte[] array = new byte [16];
2901                 Array.Clear (array, Int32.MaxValue, 4);
2902         }
2903
2904         [Test]
2905         [ExpectedException (typeof (ArgumentException))]
2906         public void Copy_SourceIndexOverflow () 
2907         {
2908                 byte[] array = new byte [16];
2909                 Array.Copy (array, Int32.MaxValue, array, 8, 8);
2910         }
2911
2912         [Test]
2913         [ExpectedException (typeof (ArgumentException))]
2914         public void Copy_DestinationIndexOverflow () 
2915         {
2916                 byte[] array = new byte [16];
2917                 Array.Copy (array, 8, array, Int32.MaxValue, 8);
2918         }
2919
2920         [Test]
2921         [ExpectedException (typeof (ArgumentException))]
2922         public void Copy_LengthOverflow () 
2923         {
2924                 byte[] array = new byte [16];
2925                 Array.Copy (array, 8, array, 8, Int32.MaxValue);
2926         }
2927
2928         [Test]
2929         [ExpectedException (typeof (ArgumentException))]
2930         public void Reverse_IndexOverflow () 
2931         {
2932                 byte[] array = new byte [16];
2933                 Array.Reverse (array, Int32.MaxValue, 8);
2934         }
2935
2936         [Test]
2937         [ExpectedException (typeof (ArgumentException))]
2938         public void Reverse_LengthOverflow () 
2939         {
2940                 byte[] array = new byte [16];
2941                 Array.Reverse (array, 8, Int32.MaxValue);
2942         }
2943         
2944         public struct CharX : IComparable {
2945                 public char c;
2946         
2947                 public CharX (char c)
2948                 {
2949                         this.c = c;
2950                 }
2951         
2952                 public int CompareTo (object obj)
2953                 {
2954                         if (obj is CharX)
2955                                 return c.CompareTo (((CharX) obj).c);
2956                         else
2957                                 return c.CompareTo (obj);
2958                 }
2959         }
2960
2961         [Test]
2962         public void BinarySearch_ArgPassingOrder ()
2963         {
2964                 //
2965                 // This tests that arguments are passed to the comprer in the correct
2966                 // order. The IComparable of the *array* elements must get called, not
2967                 // that of the search object.
2968                 //
2969                 CharX [] x = { new CharX ('a'), new CharX ('b'), new CharX ('c') };
2970                 Assert.AreEqual (1, Array.BinarySearch (x, 'b'));
2971         }
2972
2973         class Comparer: IComparer {
2974
2975                 private bool called = false;
2976
2977                 public bool Called {
2978                         get {
2979                                 bool result = called;
2980                                 called = false;
2981                                 return called;
2982                         }
2983                 }
2984
2985                 public int Compare (object x, object y)
2986                 {
2987                         called = true;
2988                         return 0;
2989                 }
2990         }
2991
2992         [Test]
2993         public void BinarySearch1_EmptyList ()
2994         {
2995                 int[] array = new int[0];
2996                 Assert.AreEqual (- 1, Array.BinarySearch (array, 0), "BinarySearch");
2997         }
2998
2999         [Test]
3000         public void BinarySearch2_EmptyList ()
3001         {
3002                 int[] array = new int[0];
3003                 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, 0), "BinarySearch");
3004         }
3005
3006         [Test]
3007         public void BinarySearch3_EmptyList ()
3008         {
3009                 Comparer comparer = new Comparer ();
3010                 int[] array = new int[0];
3011                 Assert.AreEqual (-1, Array.BinarySearch (array, 0, comparer), "BinarySearch");
3012                 // bug 77030 - the comparer isn't called for an empty array/list
3013                 Assert.IsTrue (!comparer.Called, "Called");
3014         }
3015
3016         [Test]
3017         public void BinarySearch4_EmptyList ()
3018         {
3019                 Comparer comparer = new Comparer ();
3020                 int[] array = new int[0];
3021                 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, comparer), "BinarySearch");
3022                 // bug 77030 - the comparer isn't called for an empty array/list
3023                 Assert.IsTrue (!comparer.Called, "Called");
3024         }
3025
3026         [Test]
3027         [ExpectedException (typeof (ArgumentNullException))]
3028         public void AsReadOnly_NullArray ()
3029         {
3030                 Array.AsReadOnly <int> (null);
3031         }
3032
3033         [Test]
3034         public void ReadOnly_Count ()
3035         {
3036                 Assert.AreEqual (10, Array.AsReadOnly (new int [10]).Count);
3037         }
3038
3039         [Test]
3040         public void ReadOnly_Contains ()
3041         {
3042                 int[] arr = new int [2];
3043                 arr [0] = 3;
3044                 arr [1] = 5;
3045                 IList<int> a = Array.AsReadOnly (arr);
3046
3047                 Assert.IsTrue (a.Contains (3));
3048                 Assert.IsTrue (!a.Contains (6));
3049         }
3050
3051         [Test]
3052         public void ReadOnly_IndexOf ()
3053         {
3054                 int[] arr = new int [2];
3055                 arr [0] = 3;
3056                 arr [1] = 5;
3057                 IList<int> a = Array.AsReadOnly (arr);
3058
3059                 Assert.AreEqual (0, a.IndexOf (3));
3060                 Assert.AreEqual (1, a.IndexOf (5));
3061                 Assert.AreEqual (-1, a.IndexOf (6));
3062         }
3063
3064         [Test]
3065         public void ReadOnly_Indexer ()
3066         {
3067                 int[] arr = new int [2];
3068                 arr [0] = 3;
3069                 arr [1] = 5;
3070                 IList<int> a = Array.AsReadOnly (arr);
3071
3072                 Assert.AreEqual (3, a [0]);
3073                 Assert.AreEqual (5, a [1]);
3074
3075                 /* Check that modifications to the original array are visible */
3076                 arr [0] = 6;
3077                 Assert.AreEqual (6, a [0]);
3078         }
3079
3080         [Test]
3081         public void ReadOnly_Enumerator ()
3082         {
3083                 int[] arr = new int [10];
3084
3085                 for (int i = 0; i < 10; ++i)
3086                         arr [i] = i;
3087
3088                 int sum = 0;
3089                 foreach (int i in Array.AsReadOnly (arr))
3090                         sum += i;
3091
3092                 Assert.AreEqual (45, sum);
3093         }
3094
3095         [Test]
3096         public void ReadOnly_CopyTo ()
3097         {
3098                 int[] arr = new int [2];
3099                 arr [0] = 3;
3100                 arr [1] = 5;
3101                 IList<int> a = Array.AsReadOnly (arr);
3102
3103                 int[] arr2 = new int [3];
3104                 a.CopyTo (arr2, 1);
3105
3106                 Assert.AreEqual (0, arr2 [0]);
3107                 Assert.AreEqual (3, arr2 [1]);
3108                 Assert.AreEqual (5, arr2 [2]);
3109         }
3110
3111         [Test]
3112         public void Resize ()
3113         {
3114                 int [] arr = new int [] { 1, 3, 5 };
3115                 Array.Resize <int> (ref arr, 3);
3116                 Assert.AreEqual (3, arr.Length, "#A1");
3117                 Assert.AreEqual (1, arr [0], "#A2");
3118                 Assert.AreEqual (3, arr [1], "#A3");
3119                 Assert.AreEqual (5, arr [2], "#A4");
3120
3121                 Array.Resize <int> (ref arr, 2);
3122                 Assert.AreEqual (2, arr.Length, "#B1");
3123                 Assert.AreEqual (1, arr [0], "#B2");
3124                 Assert.AreEqual (3, arr [1], "#B3");
3125
3126                 Array.Resize <int> (ref arr, 4);
3127                 Assert.AreEqual (4, arr.Length, "#C1");
3128                 Assert.AreEqual (1, arr [0], "#C2");
3129                 Assert.AreEqual (3, arr [1], "#C3");
3130                 Assert.AreEqual (0, arr [2], "#C4");
3131                 Assert.AreEqual (0, arr [3], "#C5");
3132         }
3133
3134         [Test]
3135         public void Resize_null ()
3136         {
3137                 int [] arr = null;
3138                 Array.Resize (ref arr, 10);
3139                 Assert.AreEqual (arr.Length, 10);
3140         }
3141
3142         [Test]
3143         public void Test_ContainsAndIndexOf_EquatableItem ()
3144         {
3145                 EquatableClass[] list = new EquatableClass[] {new EquatableClass (0), new EquatableClass (1), new EquatableClass (0)};
3146
3147                 Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, list[0]), "#0");
3148                 Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, new EquatableClass (0)), "#1");
3149                 Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, list[0]), "#2");
3150                 Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, new EquatableClass (0)), "#3");
3151         }
3152
3153         public class EquatableClass : IEquatable<EquatableClass>
3154         {
3155                 int _x;
3156                 public EquatableClass (int x)
3157                 {
3158                         _x = x;
3159                 }
3160
3161                 public bool Equals (EquatableClass other)
3162                 {
3163                         return this._x == other._x;
3164                 }
3165         }
3166
3167         [Test]
3168         public void AsIList ()
3169         {
3170                 IList<int> arr = new int [10];
3171                 arr [0] = 5;
3172                 Assert.AreEqual (5, arr [0]);
3173
3174                 IList<FooStruct> arr2 = new FooStruct [10];
3175                 FooStruct s = new FooStruct ();
3176                 s.i = 11;
3177                 s.j = 22;
3178                 arr2 [5] = s;
3179                 s = arr2 [5];
3180                 Assert.AreEqual (11, s.i);
3181                 Assert.AreEqual (22, s.j);
3182
3183                 IList<string> arr3 = new string [10];
3184                 arr3 [5] = "ABC";
3185                 Assert.AreEqual ("ABC", arr3 [5]);
3186         }
3187
3188         struct FooStruct {
3189                 public int i, j;
3190         }
3191
3192         [Test]
3193         // From bug #80563
3194         public void ICollectionNull ()
3195         {
3196                 ICollection<object> test;
3197                 
3198                 test = new List<object>();
3199                 Assert.AreEqual (test.Contains (null), false, "list<o>");
3200
3201                 test = new object[] {};
3202                 Assert.AreEqual (test.Contains (null), false, "empty array");
3203
3204                 test = new object[] {null};
3205                 Assert.AreEqual (test.Contains (null), true, "array with null");
3206
3207                 test = new object[] { 1, null};
3208                 Assert.IsTrue (test.Contains (null), "array with last null");
3209                 
3210                 test = new List<object>(test);
3211                 Assert.AreEqual (test.Contains (null), true, "List<object> with test");
3212                 
3213                 test = new object[] {new object()};
3214                 Assert.AreEqual (test.Contains (null), false, "array with object");
3215
3216                 test = new List<object>(test);
3217                 Assert.AreEqual (test.Contains (null), false, "array with test");
3218         }
3219         
3220         [Test]
3221         public void IListNull ()
3222         {
3223                 IList<object> test;
3224                 
3225                 test = new List<object>();
3226                 Assert.AreEqual (-1, test.IndexOf (null), "list<o>");
3227
3228                 test = new object[] {};
3229                 Assert.AreEqual (-1, test.IndexOf (null), "empty array");
3230
3231                 test = new object[] {null};
3232                 Assert.AreEqual (0, test.IndexOf (null), "array with null");
3233
3234                 test = new object[] { 1, null};
3235                 Assert.AreEqual (1, test.IndexOf (null), "array with last null");
3236                 
3237                 test = new List<object>(test);
3238                 Assert.AreEqual (1, test.IndexOf (null), "List<object> with test");
3239                 
3240                 test = new object[] {new object()};
3241                 Assert.AreEqual (-1, test.IndexOf (null), "array with object");
3242
3243                 test = new List<object>(test);
3244                 Assert.AreEqual (-1, test.IndexOf (null), "array with test");
3245         }
3246         
3247
3248         #region Bug 80299
3249
3250         enum ByteEnum : byte {}
3251         enum IntEnum : int {}
3252
3253         [Test]
3254         public void TestByteEnumArrayToByteArray ()
3255         {
3256                 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
3257                 byte[] b = new byte[a.Length];
3258                 a.CopyTo (b, 0);
3259         }
3260
3261         [Test]
3262         public void TestByteEnumArrayToIntArray ()
3263         {
3264                 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
3265                 int[] b = new int[a.Length];
3266                 a.CopyTo (b, 0);
3267         }
3268
3269         [Test]
3270         [ExpectedException (typeof (ArrayTypeMismatchException))]
3271         public void TestIntEnumArrayToByteArray ()
3272         {
3273                 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
3274                 byte[] b = new byte[a.Length];
3275                 a.CopyTo (b, 0);
3276         }
3277
3278         [Test]
3279         public void TestIntEnumArrayToIntArray ()
3280         {
3281                 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
3282                 int[] b = new int[a.Length];
3283                 a.CopyTo (b, 0);
3284         }
3285
3286         #endregion
3287
3288         [Test] // bug #322248
3289         public void IEnumerator_Reset ()
3290         {
3291                 int[] array = new int[] { 1, 2, 3};
3292                 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3293                 Assert.IsTrue (e.MoveNext (), "#A1");
3294                 Assert.AreEqual (1, e.Current, "#A2");
3295                 Assert.IsTrue (e.MoveNext (), "#A3");
3296                 Assert.AreEqual (2, e.Current, "#A4");
3297
3298                 e.Reset ();
3299
3300                 Assert.IsTrue (e.MoveNext (), "#C1");
3301                 Assert.AreEqual (1, e.Current, "#C2");
3302         }
3303
3304         [Test]
3305         public void IEnumerator_Current_Finished ()
3306         {
3307                 int[] array = new int[] { 1, 2, 3 };
3308                 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3309                 Assert.IsTrue (e.MoveNext (), "#A1");
3310                 Assert.AreEqual (1, e.Current, "#A2");
3311                 Assert.IsTrue (e.MoveNext (), "#A3");
3312                 Assert.AreEqual (2, e.Current, "#A4");
3313                 Assert.IsTrue (e.MoveNext (), "#A5");
3314                 Assert.AreEqual (3, e.Current, "#A6");
3315                 Assert.IsTrue (!e.MoveNext (), "#A6");
3316
3317                 try {
3318                         Assert.Fail ("#B1:" + e.Current);
3319                 } catch (InvalidOperationException ex) {
3320                         // Enumeration already finished
3321                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3322                         Assert.IsNull (ex.InnerException, "#B3");
3323                         Assert.IsNotNull (ex.Message, "#B4");
3324                 }
3325         }
3326
3327         [Test]
3328         public void IEnumerator_Current_NotStarted ()
3329         {
3330                 int[] array = new int[] { 1, 2, 3 };
3331                 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3332
3333                 try {
3334                         Assert.Fail ("#A1:" + e.Current);
3335                 } catch (InvalidOperationException ex) {
3336                         // Enumeration has not started. Call MoveNext
3337                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
3338                         Assert.IsNull (ex.InnerException, "#A3");
3339                         Assert.IsNotNull (ex.Message, "#A4");
3340                 }
3341         }
3342
3343         [Test]
3344         public void IEnumerator_Current_Reset ()
3345         {
3346                 int[] array = new int[] { 1, 2, 3 };
3347                 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3348                 e.MoveNext ();
3349                 e.Reset ();
3350
3351                 try {
3352                         Assert.Fail ("#B1:" + e.Current);
3353                 } catch (InvalidOperationException ex) {
3354                         // Enumeration has not started. Call MoveNext
3355                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3356                         Assert.IsNull (ex.InnerException, "#B3");
3357                         Assert.IsNotNull (ex.Message, "#B4");
3358                 }
3359         }
3360
3361         public void ICollection_IsReadOnly() {
3362                 ICollection<string> arr = new string [10];
3363
3364                 Assert.IsTrue (arr.IsReadOnly);
3365         }
3366
3367         [Test]
3368         [ExpectedException (typeof (NotSupportedException))]
3369         public void ArrayCreateInstanceOfVoid ()
3370         {
3371                 Array.CreateInstance (typeof (void), 42);
3372         }
3373
3374         class Foo<T> {}
3375
3376         [Test]
3377         [ExpectedException (typeof (NotSupportedException))]
3378         public void ArrayCreateInstanceOfOpenGenericType ()
3379         {
3380                 Array.CreateInstance (typeof (Foo<>), 42);
3381         }
3382
3383         [Test]
3384         [ExpectedException (typeof (IndexOutOfRangeException))]
3385         public void ClearNegativeLength ()
3386         {
3387                 Array.Clear (new int [] { 1, 2 }, 0, -1);
3388         }
3389
3390         [Test]
3391         [ExpectedException (typeof (ArgumentException))]
3392         public void MultiDimension_IList_setItem ()
3393         {
3394                 IList array = new int [1, 1];
3395                 array [0] = 2;
3396         }
3397
3398         [Test]
3399         [ExpectedException (typeof (ArgumentException))]
3400         public void MultiDimension_IList_getItem ()
3401         {
3402                 IList array = new int [1, 1];
3403                 int a = (int) array [0];
3404         }
3405
3406         [Test]
3407         public void SetValue_Nullable () {
3408                 Array array = Array.CreateInstance (typeof (int?), 7);
3409
3410                 object o = 42;
3411
3412                 array.SetValue (o, 0);
3413                 Assert.AreEqual (42, array.GetValue (0));
3414
3415                 array.SetValue (null, 0);
3416                 Assert.AreEqual (null, array.GetValue (0));
3417         }
3418
3419         [Test]
3420         public void SortNullsWithGenericVersion ()
3421         {
3422             string[] s1 = new string[6]{
3423                 "J",
3424                 "M",
3425                  null,
3426                 "P",
3427                 "T",
3428                 "A"};
3429
3430             string[] s2 = new string[]{null,
3431                 "A",
3432                 "J",
3433                 "M",
3434                 "P",
3435                 "T"};
3436
3437             Array.Sort<string> (s1);
3438             for (int i = 0; i < 6; i++) {
3439                     Assert.AreEqual (s1[i], s2[i], "At:" + i);
3440             }
3441         }
3442         
3443         //
3444         // This is a test case for the case that was broken by the code contributed
3445         // for bug  #351638.
3446         //
3447         // This tests the fix for: #622101
3448         //
3449         [Test]
3450         public void SortActuallyWorks ()
3451         {
3452                 string[] data = new string[9]{"Foo", "Bar", "Dingus", null, "Dingu4", "123", "Iam", null, "NotNull"};
3453                 IComparer comparer = new NullAtEndComparer ();
3454                 Array.Sort (data, comparer);
3455
3456                 Assert.AreEqual (data [7], null);
3457                 Assert.AreNotEqual (data [0], null);
3458         }
3459
3460         class NullAtEndComparer : IComparer {
3461                 public int Compare(object x, object y)
3462                 {
3463                         if (x == null) return 1;
3464                         if (y == null) return -1;
3465                         return ((string)x).CompareTo((string)y);
3466                 }
3467         }
3468
3469         [Test] //bxc #11184
3470         public void UnalignedArrayClear ()
3471         {
3472                 byte[] input = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
3473                 byte[] expected = new byte[] { 1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
3474                 Array.Clear (input, 5, 11);
3475                 
3476                 Assert.AreEqual (input, expected);
3477         }
3478
3479         [Test]
3480         [ExpectedException (typeof (ArgumentException))]
3481         public void CompareToWithJaggedArray () {
3482                 IStructuralComparable a = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }};
3483                 IStructuralComparable b = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }};
3484                 a.CompareTo (b, Comparer<object>.Default);
3485         }
3486
3487         [Test]
3488         [ExpectedException (typeof (ArgumentException))]
3489         public void CompareToWithArrayOfTheWrongKind () {
3490                 IStructuralComparable a = new int[] { 1, 2 };
3491                 IStructuralComparable b = new double[] { 1, 2 };
3492                 a.CompareTo (b, Comparer<object>.Default);
3493         }
3494
3495         [Test]
3496         [ExpectedException (typeof (ArgumentException))]
3497         public void CompareToWithNonArrayType () {
3498                 IStructuralComparable a = new int[] { 1, 2 };
3499                 a.CompareTo (99, Comparer<object>.Default);
3500         }
3501
3502         [Test]
3503         [ExpectedException (typeof (ArgumentException))]
3504         public void CompareToWithNonArrayOfDifferentSize () {
3505                 IStructuralComparable a = new int[] { 1, 2 };
3506                 IStructuralComparable b = new int[] { 1, 2, 3 };
3507                 a.CompareTo (b, Comparer<object>.Default);
3508         }
3509
3510         [Test]
3511         [ExpectedException (typeof (ArgumentException))]
3512         public void CompareToWithMultiDimArray1 () {
3513                 IStructuralComparable a = new int [2,2] { {10, 10 }, { 10, 10 } };
3514                 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3515                 a.CompareTo (b, Comparer<object>.Default);
3516         }
3517
3518         [Test]
3519         [ExpectedException (typeof (ArgumentException))]
3520         public void CompareToWithMultiDimArray2 () {
3521                 IStructuralComparable a = new int [2] { 10, 10 };
3522                 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3523                 a.CompareTo (b, Comparer<object>.Default);
3524         }
3525
3526         [Test]
3527         [ExpectedException (typeof (ArgumentException))]
3528         public void CompareToWithMultiDimArray3 () {
3529                 IStructuralComparable a = new int [4] { 10, 10, 10, 10 };
3530                 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3531                 a.CompareTo (b, Comparer<object>.Default);
3532         }
3533
3534         [Test]
3535         [ExpectedException (typeof (IndexOutOfRangeException))]
3536         public void CompareToWithBoundedArray1 () {
3537                 IStructuralComparable a = new int [2] { 10, 10 };
3538                 Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 });
3539                 IStructuralComparable b = ab;
3540                 ab.SetValue (10, 5);
3541                 ab.SetValue (10, 6);
3542
3543                 a.CompareTo (b, Comparer<object>.Default);
3544         }
3545
3546         [Test]
3547         [ExpectedException (typeof (IndexOutOfRangeException))]
3548         public void CompareToWithBoundedArray2 () {
3549                 IStructuralComparable a = new int [2] { 10, 10 };
3550                 Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 });
3551                 IStructuralComparable b = ab;
3552                 ab.SetValue (10, 5);
3553                 ab.SetValue (10, 6);
3554
3555                 //Yes, CompareTo simply doesn't work with bounded arrays!
3556                 b.CompareTo (b, Comparer<object>.Default);
3557         }
3558
3559         [Test]
3560         [ExpectedException (typeof (NullReferenceException))]
3561         public void CompareToWithNullComparer () {
3562                 IStructuralComparable a = new int[] { 1, 2 };
3563                 IStructuralComparable b = new int[] { 1, 2 };
3564                 a.CompareTo (b, null);
3565         }
3566
3567         [Test]
3568         public void CompareToWithNullArray () {
3569                 IStructuralComparable a = new int[] { 1, 2 };
3570                 Assert.AreEqual (1, a.CompareTo (null, Comparer<object>.Default));
3571         }
3572
3573         [Test]
3574         public void CompareToWithGoodArrays () {
3575                 IStructuralComparable a = new int[] { 10, 20 };
3576                 Assert.AreEqual (0, a.CompareTo (a, Comparer<object>.Default));
3577                 Assert.AreEqual (0, a.CompareTo (new int [] { 10, 20 }, Comparer<object>.Default));
3578                 Assert.AreEqual (-1, a.CompareTo (new int [] { 11, 20 }, Comparer<object>.Default));
3579                 Assert.AreEqual (-1, a.CompareTo (new int [] { 10, 21 }, Comparer<object>.Default));
3580                 Assert.AreEqual (1, a.CompareTo (new int [] { 9, 20 }, Comparer<object>.Default));
3581                 Assert.AreEqual (1, a.CompareTo (new int [] { 10, 19 }, Comparer<object>.Default));
3582         }
3583
3584         [Test]
3585         public void IStructuralEquatable_Equals ()
3586         {
3587                 IStructuralEquatable array = new int[] {1, 2, 3};
3588                 IStructuralEquatable array2 = new int[] {1, 2, 3};
3589                 Assert.AreEqual (false, array.Equals (null, null));
3590                 Assert.AreEqual (true, array.Equals (array, null));
3591                 Assert.AreEqual (true, array.Equals (array2, EqualityComparer<int>.Default));
3592         }
3593
3594         [Test]
3595         [ExpectedException (typeof (NullReferenceException))]
3596         public void IStructuralEquatable_Equals_NoComparer ()
3597         {
3598                 IStructuralEquatable array = new int[] {1, 2, 3};
3599                 IStructuralComparable array2 = new int[] {1, 2, 3};
3600                 array.Equals (array2, null);
3601         }
3602
3603         [Test]
3604         [ExpectedException (typeof (ArgumentException))]
3605         public void IStructuralEquatable_Equals_ComparerThrows ()
3606         {
3607                 IStructuralEquatable array = new int[] {1, 2, 3};
3608                 IStructuralComparable array2 = new int[] {1, 2, 3};
3609                 array.Equals (array2, EqualityComparer<long>.Default);
3610         }
3611
3612         [Test]
3613         [ExpectedException (typeof (ArgumentNullException))]    
3614         public void IStructuralEquatable_GetHashCode_NullComparer ()
3615         {
3616                 IStructuralEquatable a = new int[] { 1, 2 };
3617                 a.GetHashCode (null);
3618         }
3619
3620         class TestComparer_GetHashCode : IEqualityComparer
3621         {
3622                 public int Counter;
3623
3624                 bool IEqualityComparer.Equals (object x, object y)
3625                 {
3626                         throw new NotImplementedException ();
3627                 }
3628
3629                 public int GetHashCode (object obj)
3630                 {
3631                         return Counter++;
3632                 }
3633         }
3634
3635         [Test]
3636         public void IStructuralEquatable_GetHashCode ()
3637         {
3638                 IStructuralEquatable a = new int[] { 1, 2, 9 };
3639
3640                 var c = new TestComparer_GetHashCode ();
3641                 a.GetHashCode (c);
3642                 Assert.AreEqual (3, c.Counter);         
3643         }
3644
3645
3646         [Test]
3647         public void JaggedArrayCtor ()
3648         {
3649         var type = Type.GetType ("System.Object[][]");
3650
3651                 ConstructorInfo ctor = null;
3652         foreach (var c in type.GetConstructors ()) {
3653                         if (c.GetParameters ().Length == 2)
3654                                 ctor = c;
3655                 }
3656                 Assert.IsNotNull (ctor);
3657                 var arr = (object[])ctor.Invoke (new object [] { 4, 10 });
3658                 for (int i = 0; i < 4; ++i) {
3659                         Assert.IsNotNull (arr [i]);
3660                         Assert.AreEqual (10, ((object[])arr [i]).Length);
3661                 }
3662         }
3663 }
3664 }