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