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