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