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