Fix sorting of ulong[] arrays among others when using the non-generic Sort() overloads.
[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                 /* Check that ulong[] is not sorted as long[] */
2333                 {
2334                         string[] names = new string[] {
2335                                 "A", "B", "C", "D", "E"
2336                         };
2337
2338                         ulong[] arr = new ulong [] {
2339                                 5,
2340                                 unchecked((ulong)0xffffFFFF00000000),
2341                                         0,
2342                                                 0x7FFFFFFFffffffff,
2343                                                 100
2344                                                 };
2345
2346                         Array a = arr;
2347                         Array.Sort (a, names, null);
2348                         Assert.AreEqual (0, a.GetValue (0));
2349                 }
2350         }
2351
2352         [Test] // #616416
2353         public void SortNonGenericDoubleItems () {
2354             double[] doubleValues = new double[11];
2355
2356                         doubleValues[0] = 0.221788066253601;
2357                         doubleValues[1] = 0.497278285809481;
2358                         doubleValues[2] = 0.100565033883643;
2359                         doubleValues[3] = 0.0433309347749905;
2360                         doubleValues[4] = 0.00476726438463812;
2361                         doubleValues[5] = 0.1354609735456;
2362                         doubleValues[6] = 0.57690356588135;
2363                         doubleValues[7] = 0.466239434334826;
2364                         doubleValues[8] = 0.409741461978934;
2365                         doubleValues[9] = 0.0112412763949565;
2366                         doubleValues[10] = 0.668704347674307;
2367
2368             int[] indices = new int[11];
2369             indices[0] = 0;
2370             indices[1] = 1;
2371             indices[2] = 2;
2372             indices[3] = 3;
2373             indices[4] = 4;
2374             indices[5] = 5;
2375             indices[6] = 6;
2376             indices[7] = 7;
2377             indices[8] = 8;
2378             indices[9] = 9;
2379             indices[10] = 10;
2380
2381                         Array.Sort ((Array)doubleValues, (Array)indices);
2382                         Assert.AreEqual (4, indices [0]);
2383         }
2384
2385         [Test]
2386         public void TestInitializeEmpty()
2387         {
2388                 bool catched=false;
2389                 int[] a = {};
2390                 try
2391                 {
2392                         a.Initialize();
2393                 }
2394                 catch(Exception)
2395                 {
2396                         catched=true;
2397                 }
2398                 Assert.IsTrue (!catched, "#TI01");
2399         }
2400
2401         [Test]
2402         public void TestInitializeInt()
2403         {
2404                 int[] a = {1,2,0};
2405                 a.Initialize();
2406                 int[] b = {1,2,0};
2407                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2408                 {
2409                         Assert.AreEqual (a[i], b[i], "#TI02 " + i);
2410                 }
2411         }
2412
2413         [Test]
2414         public void TestInitializeDouble()
2415         {
2416                 double[] a = {1.0,2.0,0.0};
2417                 a.Initialize();
2418                 double[] b = {1.0,2.0,0.0};
2419                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2420                 {
2421                         Assert.AreEqual (a[i], b[i], "#TI03 " + i);
2422                 }
2423         }
2424
2425         [Test]
2426         public void TestInitializeFloat()
2427         {
2428                 float[] a = {1.0F,2.0F,0.0F};
2429                 a.Initialize();
2430                 float[] b = {1.0F,2.0F,0.0F};
2431                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2432                 {
2433                         Assert.AreEqual (a[i], b[i], "#TI04 " + i);
2434                 }
2435         }
2436
2437         [Test]
2438         public void TestInitializeChar()
2439         {
2440                 char[] a = {'1','.','0','F','2','.','0','F'};
2441                 a.Initialize();
2442                 char[] b = {'1','.','0','F','2','.','0','F'};
2443                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2444                 {
2445                         Assert.AreEqual (a[i], b[i], "#TI05 " + i);
2446                 }
2447         }
2448
2449         [Test]
2450         public void TestInitializeString()
2451         {
2452                 string[] a = {"hola","adios","menos","mas"};
2453                 a.Initialize();
2454                 string[] b = {"hola","adios","menos","mas"};
2455                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2456                 {
2457                         Assert.AreEqual (a[i], b[i], "#TI06 " + i);
2458                 }
2459         }
2460
2461         [Test]
2462         public void TestInitializeEnum()
2463         {
2464                 enua[] a = {enua.hola,enua.adios,enua.menos,enua.mas};
2465                 a.Initialize();
2466                 enua[] b = {enua.hola,enua.adios,enua.menos,enua.mas};
2467                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2468                 {
2469                         Assert.AreEqual (a[i], b[i], "#TI07 " + i);
2470                 }
2471         }
2472         
2473         [Test]
2474         public void TestInitializeIntNI()
2475         {
2476                 int[] a = new int[20];
2477                 a.Initialize();
2478                 foreach(int b in a)
2479                 {
2480                         Assert.AreEqual (b, 0, "#TI08");
2481                 }
2482         }
2483         
2484         [Test]
2485         public void TestInitializeCharNI()
2486         {
2487                 char[] a = new char[20];
2488                 a.Initialize();
2489                 foreach(char b in a)
2490                 {
2491                         Assert.AreEqual (b, 0, "#TI09");
2492                 }
2493         }
2494         
2495         [Test]
2496         public void TestInitializeDoubleNI()
2497         {
2498                 double[] a = new double[20];
2499                 a.Initialize();
2500                 foreach(double b in a)
2501                 {
2502                         Assert.AreEqual (b, 0.0, "#TI09");
2503                 }
2504         }
2505         
2506         [Test]
2507         public void TestInitializeStringNI()
2508         {
2509                 string[] a = new string[20];
2510                 a.Initialize();
2511                 foreach(string b in a)
2512                 {
2513                         Assert.AreEqual (b, null, "#TI10");
2514                 }
2515         }
2516         
2517         [Test]
2518         public void TestInitializeObjectNI()
2519         {
2520                 object[] a = new object[20];
2521                 a.Initialize();
2522                 foreach(object b in a)
2523                 {
2524                         Assert.AreEqual (b, null, "#TI11");
2525                 }
2526         }
2527
2528         [Test]
2529         public void TestInitializeAClassNI()
2530         {
2531                 AClass[] a = new AClass[20];
2532                 a.Initialize();
2533                 foreach(AClass b in a)
2534                 {
2535                         Assert.AreEqual (b, null, "#TI12");
2536                 }
2537         }
2538
2539
2540         [Test]
2541         public void TestInitializeAStructNI()
2542         {
2543                 AStruct[] a = new AStruct[20];
2544                 a.Initialize();
2545                 foreach(AStruct b in a)
2546                 {
2547                         Assert.AreEqual (b, new AStruct(), "#TI14");
2548                 }
2549         }
2550
2551         [Test]
2552         public void TestInitializeAStruct()
2553         {
2554                 AStruct[] a = new AStruct[3];
2555                 a[1].a = "ADIOS";
2556                 a[1].s = "HOLA";
2557                 a.Initialize();
2558                 AStruct[] b = new AStruct[3];
2559                 b[1].a = "ADIOS";
2560                 b[1].s = "HOLA";
2561                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2562                 {
2563                         Assert.AreEqual (a[i], b[i], "#TI15 " + i);
2564                 }
2565         }
2566
2567         [Test]
2568         public void TestInitializeDateTimeNI()
2569         {
2570                 DateTime[] a = new DateTime[20];
2571                 a.Initialize();
2572                 foreach(DateTime b in a)
2573                 {
2574                         Assert.AreEqual (b, new DateTime(), "#TI16");
2575                 }
2576         }
2577         
2578         [Test]
2579         [ExpectedException (typeof (ArgumentNullException))]
2580         public void MoreSort1 ()
2581         {
2582                 Array.Sort (null, 0, 1);
2583         }
2584
2585         [Test]
2586         [ExpectedException (typeof (ArgumentOutOfRangeException))]
2587         public void MoreSort2 ()
2588         {
2589                 Array.Sort (arrsort, -1, 3);
2590         }
2591
2592         [Test]
2593         [ExpectedException (typeof (ArgumentOutOfRangeException))]
2594         public void MoreSort3 ()
2595         {
2596                 Array.Sort (arrsort, 1, -3);
2597         }
2598
2599         [Test]
2600         [ExpectedException (typeof (ArgumentException))]
2601         public void MoreSort4 ()
2602         {
2603                 Array.Sort (arrsort, arrsort.Length, arrsort.Length + 2);
2604         }
2605
2606         [Test]
2607         [ExpectedException (typeof (RankException))]
2608         public void MoreSort5 ()
2609         {
2610                 char [,] arr = new char [,] {{'a'}, {'b'}};
2611                 Array.Sort (arr, 0, 1);
2612         }
2613
2614         [Test]
2615         public void MoreSort6 ()
2616         {
2617                 Array.Sort (arrsort, 0, 0);
2618         }
2619
2620         [Test]
2621         [ExpectedException (typeof (ArgumentException))]
2622         public void MoreSort7 ()
2623         {
2624                 Array.Sort (arrsort, arrsort.Length - 1, 2);
2625         }
2626
2627         [Test]
2628         [ExpectedException (typeof (ArgumentException))]
2629         public void MoreSort8 ()
2630         {
2631                 Array.Sort (arrsort, 0, arrsort.Length + 1);
2632         }
2633
2634         [Test]
2635         public void MoreSort9 ()
2636         {
2637                 Array.Sort (arrsort, null, 0, arrsort.Length, null);
2638         }
2639
2640         [Test]
2641         [ExpectedException (typeof (InvalidOperationException))]
2642         public void MoreSort10 ()
2643         {
2644                 object [] array = {true, 'k', SByte.MinValue, Byte.MinValue, (short) 2, 634, (long) 436, (float) 1.1, 1.23, "Hello World"};
2645                 Array.Sort (array, (IComparer) null);
2646         }
2647
2648         [Test] // bug #81941
2649         public void Sort ()
2650         {
2651                 double [] a = new double [2] { 0.9, 0.3 };
2652                 uint [] b = new uint [2] { 4, 7 };
2653                 Array.Sort (a, b);
2654                 Assert.AreEqual (0.3, a [0], "#1");
2655                 Assert.AreEqual (0.9, a [1], "#2");
2656                 Assert.AreEqual (7, b [0], "#3");
2657                 Assert.AreEqual (4, b [1], "#4");
2658         }
2659
2660         [Test]
2661         public void ClearJaggedArray () 
2662         {
2663                 byte[][] matrix = new byte [8][];
2664                 for (int i=0; i < 8; i++) {
2665                         matrix [i] = new byte [8];
2666                         for (int j=0; j < 8; j++) {
2667                                 matrix [i][j] = 1;
2668                         }
2669                 }
2670                 Array.Clear (matrix, 0, 8);
2671                 for (int i=0; i < 8; i++) {
2672                         Assert.IsNull (matrix [i], i.ToString ());
2673                 }
2674         }
2675
2676         [Test]
2677         public void ClearMultidimentionalArray () 
2678         {
2679                 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2680                 Array.Clear (matrix, 0, 2);
2681                 Assert.AreEqual (0, matrix [0, 0], "0,0");
2682                 Assert.AreEqual (0, matrix [0, 1], "0,1");
2683                 Assert.AreEqual (2, matrix [1, 0], "1,0");
2684                 Assert.AreEqual (2, matrix [1, 1], "1,1");
2685         }
2686
2687         [Test]
2688         [ExpectedException (typeof (IndexOutOfRangeException))]
2689         public void ClearOutsideMultidimentionalArray () 
2690         {
2691                 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2692                 Array.Clear (matrix, 0, 5);
2693         }
2694
2695         [Test]
2696         [ExpectedException (typeof (IndexOutOfRangeException))]
2697         public void Clear_IndexOverflow () 
2698         {
2699                 byte[] array = new byte [16];
2700                 Array.Clear (array, 4, Int32.MaxValue);
2701         }
2702
2703         [Test]
2704         [ExpectedException (typeof (IndexOutOfRangeException))]
2705         public void Clear_LengthOverflow () 
2706         {
2707                 byte[] array = new byte [16];
2708                 Array.Clear (array, Int32.MaxValue, 4);
2709         }
2710
2711         [Test]
2712         [ExpectedException (typeof (ArgumentException))]
2713         public void Copy_SourceIndexOverflow () 
2714         {
2715                 byte[] array = new byte [16];
2716                 Array.Copy (array, Int32.MaxValue, array, 8, 8);
2717         }
2718
2719         [Test]
2720         [ExpectedException (typeof (ArgumentException))]
2721         public void Copy_DestinationIndexOverflow () 
2722         {
2723                 byte[] array = new byte [16];
2724                 Array.Copy (array, 8, array, Int32.MaxValue, 8);
2725         }
2726
2727         [Test]
2728         [ExpectedException (typeof (ArgumentException))]
2729         public void Copy_LengthOverflow () 
2730         {
2731                 byte[] array = new byte [16];
2732                 Array.Copy (array, 8, array, 8, Int32.MaxValue);
2733         }
2734
2735         [Test]
2736         [ExpectedException (typeof (ArgumentException))]
2737         public void Reverse_IndexOverflow () 
2738         {
2739                 byte[] array = new byte [16];
2740                 Array.Reverse (array, Int32.MaxValue, 8);
2741         }
2742
2743         [Test]
2744         [ExpectedException (typeof (ArgumentException))]
2745         public void Reverse_LengthOverflow () 
2746         {
2747                 byte[] array = new byte [16];
2748                 Array.Reverse (array, 8, Int32.MaxValue);
2749         }
2750         
2751         public struct CharX : IComparable {
2752                 public char c;
2753         
2754                 public CharX (char c)
2755                 {
2756                         this.c = c;
2757                 }
2758         
2759                 public int CompareTo (object obj)
2760                 {
2761                         if (obj is CharX)
2762                                 return c.CompareTo (((CharX) obj).c);
2763                         else
2764                                 return c.CompareTo (obj);
2765                 }
2766         }
2767
2768         [Test]
2769         public void BinarySearch_ArgPassingOrder ()
2770         {
2771                 //
2772                 // This tests that arguments are passed to the comprer in the correct
2773                 // order. The IComparable of the *array* elements must get called, not
2774                 // that of the search object.
2775                 //
2776                 CharX [] x = { new CharX ('a'), new CharX ('b'), new CharX ('c') };
2777                 Assert.AreEqual (1, Array.BinarySearch (x, 'b'));
2778         }
2779
2780         class Comparer: IComparer {
2781
2782                 private bool called = false;
2783
2784                 public bool Called {
2785                         get {
2786                                 bool result = called;
2787                                 called = false;
2788                                 return called;
2789                         }
2790                 }
2791
2792                 public int Compare (object x, object y)
2793                 {
2794                         called = true;
2795                         return 0;
2796                 }
2797         }
2798
2799         [Test]
2800         public void BinarySearch1_EmptyList ()
2801         {
2802                 int[] array = new int[0];
2803                 Assert.AreEqual (- 1, Array.BinarySearch (array, 0), "BinarySearch");
2804         }
2805
2806         [Test]
2807         public void BinarySearch2_EmptyList ()
2808         {
2809                 int[] array = new int[0];
2810                 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, 0), "BinarySearch");
2811         }
2812
2813         [Test]
2814         public void BinarySearch3_EmptyList ()
2815         {
2816                 Comparer comparer = new Comparer ();
2817                 int[] array = new int[0];
2818                 Assert.AreEqual (-1, Array.BinarySearch (array, 0, comparer), "BinarySearch");
2819                 // bug 77030 - the comparer isn't called for an empty array/list
2820                 Assert.IsTrue (!comparer.Called, "Called");
2821         }
2822
2823         [Test]
2824         public void BinarySearch4_EmptyList ()
2825         {
2826                 Comparer comparer = new Comparer ();
2827                 int[] array = new int[0];
2828                 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, comparer), "BinarySearch");
2829                 // bug 77030 - the comparer isn't called for an empty array/list
2830                 Assert.IsTrue (!comparer.Called, "Called");
2831         }
2832
2833 #if NET_2_0
2834         [Test]
2835         [ExpectedException (typeof (ArgumentNullException))]
2836         public void AsReadOnly_NullArray ()
2837         {
2838                 Array.AsReadOnly <int> (null);
2839         }
2840
2841         [Test]
2842         public void ReadOnly_Count ()
2843         {
2844                 Assert.AreEqual (10, Array.AsReadOnly (new int [10]).Count);
2845         }
2846
2847         [Test]
2848         public void ReadOnly_Contains ()
2849         {
2850                 int[] arr = new int [2];
2851                 arr [0] = 3;
2852                 arr [1] = 5;
2853                 IList<int> a = Array.AsReadOnly (arr);
2854
2855                 Assert.IsTrue (a.Contains (3));
2856                 Assert.IsTrue (!a.Contains (6));
2857         }
2858
2859         [Test]
2860         public void ReadOnly_IndexOf ()
2861         {
2862                 int[] arr = new int [2];
2863                 arr [0] = 3;
2864                 arr [1] = 5;
2865                 IList<int> a = Array.AsReadOnly (arr);
2866
2867                 Assert.AreEqual (0, a.IndexOf (3));
2868                 Assert.AreEqual (1, a.IndexOf (5));
2869                 Assert.AreEqual (-1, a.IndexOf (6));
2870         }
2871
2872         [Test]
2873         public void ReadOnly_Indexer ()
2874         {
2875                 int[] arr = new int [2];
2876                 arr [0] = 3;
2877                 arr [1] = 5;
2878                 IList<int> a = Array.AsReadOnly (arr);
2879
2880                 Assert.AreEqual (3, a [0]);
2881                 Assert.AreEqual (5, a [1]);
2882
2883                 /* Check that modifications to the original array are visible */
2884                 arr [0] = 6;
2885                 Assert.AreEqual (6, a [0]);
2886         }
2887
2888         [Test]
2889         public void ReadOnly_Enumerator ()
2890         {
2891                 int[] arr = new int [10];
2892
2893                 for (int i = 0; i < 10; ++i)
2894                         arr [i] = i;
2895
2896                 int sum = 0;
2897                 foreach (int i in Array.AsReadOnly (arr))
2898                         sum += i;
2899
2900                 Assert.AreEqual (45, sum);
2901         }
2902
2903         [Test]
2904         public void ReadOnly_CopyTo ()
2905         {
2906                 int[] arr = new int [2];
2907                 arr [0] = 3;
2908                 arr [1] = 5;
2909                 IList<int> a = Array.AsReadOnly (arr);
2910
2911                 int[] arr2 = new int [3];
2912                 a.CopyTo (arr2, 1);
2913
2914                 Assert.AreEqual (0, arr2 [0]);
2915                 Assert.AreEqual (3, arr2 [1]);
2916                 Assert.AreEqual (5, arr2 [2]);
2917         }
2918
2919         [Test]
2920         public void Resize ()
2921         {
2922                 int [] arr = new int [] { 1, 3, 5 };
2923                 Array.Resize <int> (ref arr, 3);
2924                 Assert.AreEqual (3, arr.Length, "#A1");
2925                 Assert.AreEqual (1, arr [0], "#A2");
2926                 Assert.AreEqual (3, arr [1], "#A3");
2927                 Assert.AreEqual (5, arr [2], "#A4");
2928
2929                 Array.Resize <int> (ref arr, 2);
2930                 Assert.AreEqual (2, arr.Length, "#B1");
2931                 Assert.AreEqual (1, arr [0], "#B2");
2932                 Assert.AreEqual (3, arr [1], "#B3");
2933
2934                 Array.Resize <int> (ref arr, 4);
2935                 Assert.AreEqual (4, arr.Length, "#C1");
2936                 Assert.AreEqual (1, arr [0], "#C2");
2937                 Assert.AreEqual (3, arr [1], "#C3");
2938                 Assert.AreEqual (0, arr [2], "#C4");
2939                 Assert.AreEqual (0, arr [3], "#C5");
2940         }
2941
2942         [Test]
2943         public void Resize_null ()
2944         {
2945                 int [] arr = null;
2946                 Array.Resize (ref arr, 10);
2947                 Assert.AreEqual (arr.Length, 10);
2948         }
2949
2950         [Test]
2951         public void Test_ContainsAndIndexOf_EquatableItem ()
2952         {
2953                 EquatableClass[] list = new EquatableClass[] {new EquatableClass (0), new EquatableClass (1), new EquatableClass (0)};
2954
2955                 Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, list[0]), "#0");
2956                 Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, new EquatableClass (0)), "#1");
2957                 Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, list[0]), "#2");
2958                 Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, new EquatableClass (0)), "#3");
2959         }
2960
2961         public class EquatableClass : IEquatable<EquatableClass>
2962         {
2963                 int _x;
2964                 public EquatableClass (int x)
2965                 {
2966                         _x = x;
2967                 }
2968
2969                 public bool Equals (EquatableClass other)
2970                 {
2971                         return this._x == other._x;
2972                 }
2973         }
2974
2975         [Test]
2976         public void AsIList ()
2977         {
2978                 IList<int> arr = new int [10];
2979                 arr [0] = 5;
2980                 Assert.AreEqual (5, arr [0]);
2981
2982                 IList<FooStruct> arr2 = new FooStruct [10];
2983                 FooStruct s = new FooStruct ();
2984                 s.i = 11;
2985                 s.j = 22;
2986                 arr2 [5] = s;
2987                 s = arr2 [5];
2988                 Assert.AreEqual (11, s.i);
2989                 Assert.AreEqual (22, s.j);
2990
2991                 IList<string> arr3 = new string [10];
2992                 arr3 [5] = "ABC";
2993                 Assert.AreEqual ("ABC", arr3 [5]);
2994         }
2995
2996         struct FooStruct {
2997                 public int i, j;
2998         }
2999
3000 #if !TARGET_JVM // BugBUG: T[] is not yet ICollection<T> under TARGET_JVM
3001         [Test]
3002         // From bug #80563
3003         public void ICollectionNull ()
3004         {
3005                 ICollection<object> test;
3006                 
3007                 test = new List<object>();
3008                 Assert.AreEqual (test.Contains (null), false, "list<o>");
3009
3010                 test = new object[] {};
3011                 Assert.AreEqual (test.Contains (null), false, "empty array");
3012
3013                 test = new object[] {null};
3014                 Assert.AreEqual (test.Contains (null), true, "array with null");
3015
3016                 test = new List<object>(test);
3017                 Assert.AreEqual (test.Contains (null), true, "List<object> with test");
3018                 
3019                 test = new object[] {new object()};
3020                 Assert.AreEqual (test.Contains (null), false, "array with object");
3021
3022                 test = new List<object>(test);
3023                 Assert.AreEqual (test.Contains (null), false, "array with test");
3024         }
3025 #endif // TARGET_JVM
3026 #endif
3027
3028         #region Bug 80299
3029
3030         enum ByteEnum : byte {}
3031         enum IntEnum : int {}
3032
3033         [Test]
3034         public void TestByteEnumArrayToByteArray ()
3035         {
3036                 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
3037                 byte[] b = new byte[a.Length];
3038                 a.CopyTo (b, 0);
3039         }
3040
3041         [Test]
3042         public void TestByteEnumArrayToIntArray ()
3043         {
3044                 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
3045                 int[] b = new int[a.Length];
3046                 a.CopyTo (b, 0);
3047         }
3048
3049         [Test]
3050         [ExpectedException (typeof (ArrayTypeMismatchException))]
3051         public void TestIntEnumArrayToByteArray ()
3052         {
3053                 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
3054                 byte[] b = new byte[a.Length];
3055                 a.CopyTo (b, 0);
3056         }
3057
3058         [Test]
3059         public void TestIntEnumArrayToIntArray ()
3060         {
3061                 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
3062                 int[] b = new int[a.Length];
3063                 a.CopyTo (b, 0);
3064         }
3065
3066         #endregion
3067
3068 #if NET_2_0
3069         [Test] // bug #322248
3070         public void IEnumerator_Reset ()
3071         {
3072                 int[] array = new int[] { 1, 2, 3};
3073                 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3074                 Assert.IsTrue (e.MoveNext (), "#A1");
3075                 Assert.AreEqual (1, e.Current, "#A2");
3076                 Assert.IsTrue (e.MoveNext (), "#A3");
3077                 Assert.AreEqual (2, e.Current, "#A4");
3078
3079                 e.Reset ();
3080
3081                 Assert.IsTrue (e.MoveNext (), "#C1");
3082                 Assert.AreEqual (1, e.Current, "#C2");
3083         }
3084
3085         [Test]
3086         public void IEnumerator_Current_Finished ()
3087         {
3088                 int[] array = new int[] { 1, 2, 3 };
3089                 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3090                 Assert.IsTrue (e.MoveNext (), "#A1");
3091                 Assert.AreEqual (1, e.Current, "#A2");
3092                 Assert.IsTrue (e.MoveNext (), "#A3");
3093                 Assert.AreEqual (2, e.Current, "#A4");
3094                 Assert.IsTrue (e.MoveNext (), "#A5");
3095                 Assert.AreEqual (3, e.Current, "#A6");
3096                 Assert.IsTrue (!e.MoveNext (), "#A6");
3097
3098                 try {
3099                         Assert.Fail ("#B1:" + e.Current);
3100                 } catch (InvalidOperationException ex) {
3101                         // Enumeration already finished
3102                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3103                         Assert.IsNull (ex.InnerException, "#B3");
3104                         Assert.IsNotNull (ex.Message, "#B4");
3105                 }
3106         }
3107
3108         [Test]
3109         public void IEnumerator_Current_NotStarted ()
3110         {
3111                 int[] array = new int[] { 1, 2, 3 };
3112                 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3113
3114                 try {
3115                         Assert.Fail ("#A1:" + e.Current);
3116                 } catch (InvalidOperationException ex) {
3117                         // Enumeration has not started. Call MoveNext
3118                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
3119                         Assert.IsNull (ex.InnerException, "#A3");
3120                         Assert.IsNotNull (ex.Message, "#A4");
3121                 }
3122         }
3123
3124         [Test]
3125         public void IEnumerator_Current_Reset ()
3126         {
3127                 int[] array = new int[] { 1, 2, 3 };
3128                 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3129                 e.MoveNext ();
3130                 e.Reset ();
3131
3132                 try {
3133                         Assert.Fail ("#B1:" + e.Current);
3134                 } catch (InvalidOperationException ex) {
3135                         // Enumeration has not started. Call MoveNext
3136                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3137                         Assert.IsNull (ex.InnerException, "#B3");
3138                         Assert.IsNotNull (ex.Message, "#B4");
3139                 }
3140         }
3141
3142         public void ICollection_IsReadOnly() {
3143                 ICollection<string> arr = new string [10];
3144
3145                 Assert.IsTrue (arr.IsReadOnly);
3146         }
3147 #endif
3148
3149         [Test]
3150         [ExpectedException (typeof (NotSupportedException))]
3151         public void ArrayCreateInstanceOfVoid ()
3152         {
3153                 Array.CreateInstance (typeof (void), 42);
3154         }
3155
3156         class Foo<T> {}
3157
3158         [Test]
3159         [ExpectedException (typeof (NotSupportedException))]
3160         public void ArrayCreateInstanceOfOpenGenericType ()
3161         {
3162                 Array.CreateInstance (typeof (Foo<>), 42);
3163         }
3164
3165         [Test]
3166         [ExpectedException (typeof (IndexOutOfRangeException))]
3167         public void ClearNegativeLength ()
3168         {
3169                 Array.Clear (new int [] { 1, 2 }, 0, -1);
3170         }
3171
3172         [Test]
3173         [ExpectedException (typeof (ArgumentException))]
3174         public void MultiDimension_IList_setItem ()
3175         {
3176                 IList array = new int [1, 1];
3177                 array [0] = 2;
3178         }
3179
3180         [Test]
3181         [ExpectedException (typeof (ArgumentException))]
3182         public void MultiDimension_IList_getItem ()
3183         {
3184                 IList array = new int [1, 1];
3185                 int a = (int) array [0];
3186         }
3187
3188         [Test]
3189         public void SetValue_Nullable () {
3190                 Array array = Array.CreateInstance (typeof (int?), 7);
3191
3192                 object o = 42;
3193
3194                 array.SetValue (o, 0);
3195                 Assert.AreEqual (42, array.GetValue (0));
3196
3197                 array.SetValue (null, 0);
3198                 Assert.AreEqual (null, array.GetValue (0));
3199         }
3200
3201         [Test]
3202         public void SortNullsWithGenericVersion ()
3203         {
3204             string[] s1 = new string[6]{
3205                 "J",
3206                 "M",
3207                  null,
3208                 "P",
3209                 "T",
3210                 "A"};
3211
3212             string[] s2 = new string[]{null,
3213                 "A",
3214                 "J",
3215                 "M",
3216                 "P",
3217                 "T"};
3218
3219             Array.Sort<string> (s1);
3220             for (int i = 0; i < 6; i++) {
3221                     Assert.AreEqual (s1[i], s2[i], "At:" + i);
3222             }
3223         }
3224         
3225         //
3226         // This is a test case for the case that was broken by the code contributed
3227         // for bug  #351638.
3228         //
3229         // This tests the fix for: #622101
3230         //
3231         [Test]
3232         public void SortActuallyWorks ()
3233         {
3234                 string[] data = new string[9]{"Foo", "Bar", "Dingus", null, "Dingu4", "123", "Iam", null, "NotNull"};
3235                 IComparer comparer = new NullAtEndComparer ();
3236                 Array.Sort (data, comparer);
3237
3238                 Assert.AreEqual (data [7], null);
3239                 Assert.AreNotEqual (data [0], null);
3240         }
3241
3242         class NullAtEndComparer : IComparer {
3243                 public int Compare(object x, object y)
3244                 {
3245                         if (x == null) return 1;
3246                         if (y == null) return -1;
3247                         return ((string)x).CompareTo((string)y);
3248                 }
3249         }
3250
3251 #if NET_4_0
3252         [Test]
3253         [ExpectedException (typeof (ArgumentException))]
3254         public void CompareToWithJaggedArray () {
3255                 IStructuralComparable a = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }};
3256                 IStructuralComparable b = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }};
3257                 a.CompareTo (b, Comparer<object>.Default);
3258         }
3259
3260         [Test]
3261         [ExpectedException (typeof (ArgumentException))]
3262         public void CompareToWithArrayOfTheWrongKind () {
3263                 IStructuralComparable a = new int[] { 1, 2 };
3264                 IStructuralComparable b = new double[] { 1, 2 };
3265                 a.CompareTo (b, Comparer<object>.Default);
3266         }
3267
3268         [Test]
3269         [ExpectedException (typeof (ArgumentException))]
3270         public void CompareToWithNonArrayType () {
3271                 IStructuralComparable a = new int[] { 1, 2 };
3272                 a.CompareTo (99, Comparer<object>.Default);
3273         }
3274
3275         [Test]
3276         [ExpectedException (typeof (ArgumentException))]
3277         public void CompareToWithNonArrayOfDifferentSize () {
3278                 IStructuralComparable a = new int[] { 1, 2 };
3279                 IStructuralComparable b = new int[] { 1, 2, 3 };
3280                 a.CompareTo (b, Comparer<object>.Default);
3281         }
3282
3283         [Test]
3284         [ExpectedException (typeof (ArgumentException))]
3285         public void CompareToWithMultiDimArray1 () {
3286                 IStructuralComparable a = new int [2,2] { {10, 10 }, { 10, 10 } };
3287                 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3288                 a.CompareTo (b, Comparer<object>.Default);
3289         }
3290
3291         [Test]
3292         [ExpectedException (typeof (ArgumentException))]
3293         public void CompareToWithMultiDimArray2 () {
3294                 IStructuralComparable a = new int [2] { 10, 10 };
3295                 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3296                 a.CompareTo (b, Comparer<object>.Default);
3297         }
3298
3299         [Test]
3300         [ExpectedException (typeof (ArgumentException))]
3301         public void CompareToWithMultiDimArray3 () {
3302                 IStructuralComparable a = new int [4] { 10, 10, 10, 10 };
3303                 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3304                 a.CompareTo (b, Comparer<object>.Default);
3305         }
3306
3307         [Test]
3308         [ExpectedException (typeof (IndexOutOfRangeException))]
3309         public void CompareToWithBoundedArray1 () {
3310                 IStructuralComparable a = new int [2] { 10, 10 };
3311                 Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 });
3312                 IStructuralComparable b = ab;
3313                 ab.SetValue (10, 5);
3314                 ab.SetValue (10, 6);
3315
3316                 a.CompareTo (b, Comparer<object>.Default);
3317         }
3318
3319         [Test]
3320         [ExpectedException (typeof (IndexOutOfRangeException))]
3321         public void CompareToWithBoundedArray2 () {
3322                 IStructuralComparable a = new int [2] { 10, 10 };
3323                 Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 });
3324                 IStructuralComparable b = ab;
3325                 ab.SetValue (10, 5);
3326                 ab.SetValue (10, 6);
3327
3328                 //Yes, CompareTo simply doesn't work with bounded arrays!
3329                 b.CompareTo (b, Comparer<object>.Default);
3330         }
3331
3332         [Test]
3333         [ExpectedException (typeof (NullReferenceException))]
3334         public void CompareToWithNullComparer () {
3335                 IStructuralComparable a = new int[] { 1, 2 };
3336                 IStructuralComparable b = new int[] { 1, 2 };
3337                 a.CompareTo (b, null);
3338         }
3339
3340         [Test]
3341         public void CompareToWithNullArray () {
3342                 IStructuralComparable a = new int[] { 1, 2 };
3343                 Assert.AreEqual (1, a.CompareTo (null, Comparer<object>.Default));
3344         }
3345
3346         [Test]
3347         public void CompareToWithGoodArrays () {
3348                 IStructuralComparable a = new int[] { 10, 20 };
3349                 Assert.AreEqual (0, a.CompareTo (a, Comparer<object>.Default));
3350                 Assert.AreEqual (0, a.CompareTo (new int [] { 10, 20 }, Comparer<object>.Default));
3351                 Assert.AreEqual (-1, a.CompareTo (new int [] { 11, 20 }, Comparer<object>.Default));
3352                 Assert.AreEqual (-1, a.CompareTo (new int [] { 10, 21 }, Comparer<object>.Default));
3353                 Assert.AreEqual (1, a.CompareTo (new int [] { 9, 20 }, Comparer<object>.Default));
3354                 Assert.AreEqual (1, a.CompareTo (new int [] { 10, 19 }, Comparer<object>.Default));
3355         }
3356
3357         [Test]
3358         public void IStructuralEquatable_Equals ()
3359         {
3360                 IStructuralEquatable array = new int[] {1, 2, 3};
3361                 IStructuralEquatable array2 = new int[] {1, 2, 3};
3362                 Assert.AreEqual (false, array.Equals (null, null));
3363                 Assert.AreEqual (true, array.Equals (array, null));
3364                 Assert.AreEqual (true, array.Equals (array2, EqualityComparer<int>.Default));
3365         }
3366
3367         [Test]
3368         [ExpectedException (typeof (NullReferenceException))]
3369         public void IStructuralEquatable_Equals_NoComparer ()
3370         {
3371                 IStructuralEquatable array = new int[] {1, 2, 3};
3372                 IStructuralComparable array2 = new int[] {1, 2, 3};
3373                 array.Equals (array2, null);
3374         }
3375
3376         [Test]
3377         [ExpectedException (typeof (ArgumentException))]
3378         public void IStructuralEquatable_Equals_ComparerThrows ()
3379         {
3380                 IStructuralEquatable array = new int[] {1, 2, 3};
3381                 IStructuralComparable array2 = new int[] {1, 2, 3};
3382                 array.Equals (array2, EqualityComparer<long>.Default);
3383         }
3384
3385 #endif
3386
3387 }
3388 }