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