2008-01-08 Marek Safar <marek.safar@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
1400         [Test]
1401         public void TestLastIndexOf2() {
1402                 {
1403                         bool errorThrown = false;
1404                         try {
1405                                 Array.LastIndexOf(null, "huh?", 0);
1406                         } catch (ArgumentNullException) {
1407                                 errorThrown = true;
1408                         }
1409                         Assert("#K21", errorThrown);
1410                 }
1411                 {
1412                         bool errorThrown = false;
1413                         try {
1414                                 char[,] c = new Char[2,2];
1415                                 Array.LastIndexOf(c, "huh?", 0);
1416                         } catch (RankException) {
1417                                 errorThrown = true;
1418                         }
1419                         Assert("#K22", errorThrown);
1420                 }
1421                 {
1422                         bool errorThrown = false;
1423                         try {
1424                                 char[] c = new Char[2];
1425                                 Array.LastIndexOf(c, "huh?", 3);
1426                         } catch (ArgumentOutOfRangeException) {
1427                                 errorThrown = true;
1428                         }
1429                         Assert("#K23", errorThrown);
1430                 }
1431
1432                 String[] s1 = {"this", "is", "really", "a", "test"};
1433                 AssertEquals("#K24", -1, Array.LastIndexOf(s1, null, 3));
1434                 AssertEquals("#K25", -1, Array.LastIndexOf(s1, "nothing", 3));
1435                 AssertEquals("#K26", -1, Array.LastIndexOf(s1, "test", 3));
1436                 AssertEquals("#K27", 3, Array.LastIndexOf(s1, "a", 3));
1437                 AssertEquals("#K28", 0, Array.LastIndexOf(s1, "this", 3));
1438         }
1439
1440         [Test]
1441         public void TestLastIndexOf3() {
1442                 {
1443                         bool errorThrown = false;
1444                         try {
1445                                 Array.LastIndexOf(null, "huh?", 0, 1);
1446                         } catch (ArgumentNullException) {
1447                                 errorThrown = true;
1448                         }
1449                         Assert("#K41", errorThrown);
1450                 }
1451                 {
1452                         bool errorThrown = false;
1453                         try {
1454                                 char[,] c = new Char[2,2];
1455                                 Array.LastIndexOf(c, "huh?", 0, 1);
1456                         } catch (RankException) {
1457                                 errorThrown = true;
1458                         }
1459                         Assert("#K42", errorThrown);
1460                 }
1461                 {
1462                         bool errorThrown = false;
1463                         try {
1464                                 char[] c = new Char[2];
1465                                 Array.LastIndexOf(c, "huh?", 3, 1);
1466                         } catch (ArgumentOutOfRangeException) {
1467                                 errorThrown = true;
1468                         }
1469                         Assert("#K43", errorThrown);
1470                 }
1471                 {
1472                         bool errorThrown = false;
1473                         try {
1474                                 char[] c = new Char[2];
1475                                 Array.LastIndexOf(c, "huh?", 0, 5);
1476                         } catch (ArgumentOutOfRangeException) {
1477                                 errorThrown = true;
1478                         }
1479                         Assert("#K44", errorThrown);
1480                 }
1481
1482                 String[] s1 = {"this", "is", "really", "a", "test"};
1483                 AssertEquals("#K45", -1, Array.LastIndexOf(s1, null, 3, 3));
1484                 AssertEquals("#K46", -1, Array.LastIndexOf(s1, "nothing", 3, 3));
1485                 AssertEquals("#K47", -1, Array.LastIndexOf(s1, "this", 3, 3));
1486                 AssertEquals("#K48", 1, Array.LastIndexOf(s1, "is", 3, 3));
1487                 AssertEquals("#K49", -1, Array.LastIndexOf(s1, "test", 3, 3));
1488                 AssertEquals("#K50", 3, Array.LastIndexOf(s1, "a", 3, 3));
1489         }
1490
1491         [Test]
1492         public void TestLastIndexOf4 ()
1493         {
1494                 short [] a = new short [] { 19, 238, 317, 6, 565, 0, -52, 60, -563, 753, 238, 238};
1495                 try {
1496                         Array.LastIndexOf (a, (object)16, -1);
1497                         NUnit.Framework.Assert.Fail ("#1");
1498                 } catch (ArgumentOutOfRangeException) { }
1499                 
1500 #if NET_2_0             
1501                 try {
1502                         Array.LastIndexOf<short> (a, 16, -1);
1503                         NUnit.Framework.Assert.Fail ("#2");
1504                 } catch (ArgumentOutOfRangeException) { }
1505 #endif          
1506         }
1507
1508         [Test]
1509         public void TestLastIndexOf5 ()
1510         {
1511                 char [] a = new char [] {'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'j', 'i', 'h'};
1512                 string s;
1513                 int retval;
1514                 bool error = false;
1515
1516                 for (int i = a.Length - 1; i >= 0 ; i--) {
1517                         s = i.ToString ();
1518                         retval = Array.LastIndexOf(a, a [i], i, i + 1);
1519                         if (retval != i)
1520                                 error = true;
1521                 }
1522                 Assert (!error);
1523         }
1524
1525         [Test]
1526         [ExpectedException (typeof (ArgumentOutOfRangeException))]
1527         public void LastIndexOf_StartIndexOverflow ()
1528         {
1529                 // legal - no exception
1530                 byte[] array = new byte [16];
1531                 Array.LastIndexOf (array, this, Int32.MaxValue, 1);
1532         }
1533
1534         [Test]
1535         [ExpectedException (typeof (ArgumentOutOfRangeException))]
1536         public void LastIndexOf_CountOverflow ()
1537         {
1538                 // legal - no exception
1539                 byte[] array = new byte [16];
1540                 Array.LastIndexOf (array, this, 1, Int32.MaxValue);
1541         }
1542
1543         [Test]
1544         public void TestReverse() {
1545                 {
1546                         bool errorThrown = false;
1547                         try {
1548                                 Array.Reverse(null);
1549                         } catch (ArgumentNullException) {
1550                                 errorThrown = true;
1551                         }
1552                         Assert("#L01", errorThrown);
1553                 }
1554                 {
1555                         bool errorThrown = false;
1556                         try {
1557                                 char[,] c = new Char[2,2];
1558                                 Array.Reverse(c);
1559                         } catch (RankException) {
1560                                 errorThrown = true;
1561                         }
1562                         Assert("#L02", errorThrown);
1563                 }
1564                 
1565                 char[] c1 = {'a', 'b', 'c', 'd'};
1566                 Array.Reverse(c1);
1567                 AssertEquals("#L03", 'd', c1[0]);
1568                 AssertEquals("#L04", 'c', c1[1]);
1569                 AssertEquals("#L05", 'b', c1[2]);
1570                 AssertEquals("#L06", 'a', c1[3]);
1571
1572                 {
1573                         bool errorThrown = false;
1574                         try {
1575                                 Array.Reverse(null, 0, 0);
1576                         } catch (ArgumentNullException) {
1577                                 errorThrown = true;
1578                         }
1579                         Assert("#L07", errorThrown);
1580                 }
1581                 {
1582                         bool errorThrown = false;
1583                         try {
1584                                 char[,] c = new Char[2,2];
1585                                 Array.Reverse(c, 0, 0);
1586                         } catch (RankException) {
1587                                 errorThrown = true;
1588                         }
1589                         Assert("#L08", errorThrown);
1590                 }
1591                 //{
1592                 //bool errorThrown = false;
1593                 //try {
1594                 //      char[] c = new Char[2];
1595                 //      Array.Reverse(c, 0, 3);
1596                 //} catch (ArgumentOutOfRangeException) {
1597                 //      errorThrown = true;
1598                 //}
1599                 //Assert("#L09", errorThrown);
1600                 //}
1601                 //{
1602                 //bool errorThrown = false;
1603                 //try {
1604                 //      char[] c = new Char[2];
1605                 //      Array.Reverse(c, 3, 0);
1606                 //} catch (ArgumentOutOfRangeException) {
1607                 //      errorThrown = true;
1608                 //}
1609                 //Assert("#L10", errorThrown);
1610                 //}
1611
1612                 char[] c2 = { 'a', 'b', 'c', 'd'};
1613                 Array.Reverse(c2, 1, 2);
1614                 AssertEquals("#L11", 'a', c2[0]);
1615                 AssertEquals("#L12", 'c', c2[1]);
1616                 AssertEquals("#L13", 'b', c2[2]);
1617                 AssertEquals("#L14", 'd', c2[3]);
1618         }
1619
1620         [Test]
1621         public void TestSetValue1() {
1622                 {
1623                         bool errorThrown = false;
1624                         try {
1625                                 char[,] c = new Char[2,2];
1626                                 c.SetValue("buh", 1);
1627                         } catch (ArgumentException) {
1628                                 errorThrown = true;
1629                         }
1630                         Assert("#M01", errorThrown);
1631                 }
1632                 {
1633                         bool errorThrown = false;
1634                         try {
1635                                 char[] c = {'a', 'b', 'c'};
1636                                 c.SetValue("buh", -1);
1637                         } catch (IndexOutOfRangeException) {
1638                                 errorThrown = true;
1639                         }
1640                         Assert("#M02", errorThrown);
1641                 }
1642                 {
1643                         bool errorThrown = false;
1644                         try {
1645                                 char[] c = {'a', 'b', 'c'};
1646                                 c.SetValue("buh", 4);
1647                         } catch (IndexOutOfRangeException) {
1648                                 errorThrown = true;
1649                         }
1650                         Assert("#M03", errorThrown);
1651                 }
1652
1653                 char[] c1 = {'a', 'b', 'c', 'd'};
1654                 char[] c2 = new char[4];
1655                 for (int i = 0; i < c1.Length; i++) {
1656                         c2.SetValue(c1[i], i);
1657                 }
1658                 for (int i = 0; i < c1.Length; i++) {
1659                         AssertEquals("#M04(" + i + ")", c1[i], c2[i]);
1660                 }
1661
1662                 int[] c3 = { 1, 2, 3 };
1663                 long[] c4 = new long [3];
1664
1665                 for (int i = 0; i < c3.Length; i++)
1666                         c4.SetValue (c3 [i], i);
1667
1668                 try {
1669                         c3.CopyTo (c4, 0);
1670                 } catch (Exception e) {
1671                         Fail ("c3.CopyTo(): e=" + e);
1672                 }
1673                 for (int i = 0; i < c3.Length; i++)
1674                         Assert ("#M05(" + i + ")", c3[i] == c4[i]);
1675
1676                 Object[] c5 = new Object [3];
1677                 long[] c6 = new long [3];
1678
1679                 try {
1680                         c4.CopyTo (c5, 0);
1681                 } catch (Exception e) {
1682                         Fail ("c4.CopyTo(): e=" + e);
1683                 }
1684
1685                 try {
1686                         c5.CopyTo (c6, 0);
1687                 } catch (Exception e) {
1688                         Fail ("c5.CopyTo(): e=" + e);
1689                 }
1690                 // for (int i = 0; i < c5.Length; i++)
1691                 // Assert ("#M06(" + i + ")", c5[i] == c6[i]);
1692         }
1693
1694         [Test]
1695         public void TestSetValue2() {
1696                 {
1697                         bool errorThrown = false;
1698                         try {
1699                                 char[] c = new Char[2];
1700                                 c.SetValue("buh", 1,1);
1701                         } catch (ArgumentException) {
1702                                 errorThrown = true;
1703                         }
1704                         Assert("#M21", errorThrown);
1705                 }
1706                 {
1707                         bool errorThrown = false;
1708                         try {
1709                                 char[,] c = new Char[2,2];
1710                                 c.SetValue("buh", -1, 1);
1711                         } catch (IndexOutOfRangeException) {
1712                                 errorThrown = true;
1713                         }
1714                         Assert("#M22", errorThrown);
1715                 }
1716                 {
1717                         bool errorThrown = false;
1718                         try {
1719                                 char[,] c = new Char[2,2];
1720                                 c.SetValue("buh", 4,1);
1721                         } catch (IndexOutOfRangeException) {
1722                                 errorThrown = true;
1723                         }
1724                         Assert("#M23", errorThrown);
1725                 }
1726
1727                 char[,] c1 = new Char[4,6];
1728                 char[,] c2 = new Char[4,6];
1729                 for (int i = 0; i < 24; i++) {
1730                         int first = i / 6;
1731                         int second = i % 6;
1732                         c1[first,second] = (char)(((int)'a')+i);
1733                         c2.SetValue(c1[first,second], first, second);
1734                 }
1735                 for (int i = 0; i < c1.GetLength(0); i++) {
1736                         for (int j = 0; j < c1.GetLength(1); j++) {
1737                                 AssertEquals("#M24(" + i + "," + j + ")",
1738                                         c1[i,j], c2[i, j]);
1739                         }
1740                 }
1741         }
1742
1743         [Test]
1744         public void TestSetValue3() {
1745                 {
1746                         bool errorThrown = false;
1747                         try {
1748                                 char[] c = new Char[2];
1749                                 c.SetValue("buh", 1,1,1);
1750                         } catch (ArgumentException) {
1751                                 errorThrown = true;
1752                         }
1753                         Assert("#M41", errorThrown);
1754                 }
1755                 {
1756                         bool errorThrown = false;
1757                         try {
1758                                 char[,,] c = new Char[2,2,2];
1759                                 c.SetValue("buh", -1, 1, 1);
1760                         } catch (IndexOutOfRangeException) {
1761                                 errorThrown = true;
1762                         }
1763                         Assert("#M42", errorThrown);
1764                 }
1765                 {
1766                         bool errorThrown = false;
1767                         try {
1768                                 char[,,] c = new Char[2,2,2];
1769                                 c.SetValue("buh", 4,1,1);
1770                         } catch (IndexOutOfRangeException) {
1771                                 errorThrown = true;
1772                         }
1773                         Assert("#M43", errorThrown);
1774                 }
1775
1776                 char[,,] c1 = new Char[4,2,3];
1777                 char[,,] c2 = new Char[4,2,3];
1778                 for (int i = 0; i < 24; i++) {
1779                         int first = i / 6;
1780                         int remains = i % 6;
1781                         int second = remains / 3;
1782                         int third = remains % 3;
1783                         c1[first,second, third] = (char)(((int)'a')+i);
1784                         c2.SetValue(c1[first, second, third], first, second, third);
1785                 }
1786                 for (int i = 0; i < c1.GetLength(0); i++) {
1787                         for (int j = 0; j < c1.GetLength(1); j++) {
1788                                 for (int k = 0; k < c1.GetLength(2); k++) {
1789                                         AssertEquals("#M44(" + i + "," + j + " )",
1790                                                 c1[i,j,k], c2[i,j,k]);
1791                                 }
1792                         }
1793                 }
1794         }
1795
1796         [Test]
1797 #if NET_2_0
1798         [ExpectedException (typeof (ArgumentNullException))]
1799 #else
1800         [ExpectedException (typeof (NullReferenceException))]
1801 #endif
1802         public void TestSetValueLongArray ()
1803         {
1804                 char[] c = new Char[2];
1805                 c.SetValue("buh", (long [])null);
1806         }
1807
1808         [Test]
1809         public void TestSetValueN() {
1810                 {
1811                         bool errorThrown = false;
1812                         try {
1813                                 char[] c = new Char[2];
1814                                 c.SetValue("buh", (int [])null);
1815                         } catch (ArgumentNullException) {
1816                                 errorThrown = true;
1817                         }
1818                         Assert("#M61a", errorThrown);
1819                 }
1820                 {
1821                         bool errorThrown = false;
1822                         try {
1823                                 char[] c = new Char[2];
1824                                 int[] coords = {1, 1};
1825                                 c.SetValue("buh", coords);
1826                         } catch (ArgumentException) {
1827                                 errorThrown = true;
1828                         }
1829                         Assert("#M62", errorThrown);
1830                 }
1831                 {
1832                         bool errorThrown = false;
1833                         try {
1834                                 char[,] c = new Char[2,2];
1835                                 int[] coords = {-1, 1};
1836                                 c.SetValue("buh", coords);
1837                         } catch (IndexOutOfRangeException) {
1838                                 errorThrown = true;
1839                         }
1840                         Assert("#M63", errorThrown);
1841                 }
1842                 {
1843                         bool errorThrown = false;
1844                         try {
1845                                 char[,] c = new Char[2,2];
1846                                 int[] coords = {4, 1};
1847                                 c.SetValue("buh", coords);
1848                         } catch (IndexOutOfRangeException) {
1849                                 errorThrown = true;
1850                         }
1851                         Assert("#M64", errorThrown);
1852                 }
1853
1854                 char[,] c1 = new Char[4,6];
1855                 char[,] c2 = new Char[4,6];
1856                 for (int i = 0; i < 24; i++) {
1857                         int first = i / 6;
1858                         int second = i % 6;
1859                         c1[first,second] = (char)(((int)'a')+i);
1860                         int[] coords = {first, second};
1861                         c2.SetValue(c1[first,second], coords);
1862                 }
1863                 for (int i = 0; i < c1.GetLength(0); i++) {
1864                         for (int j = 0; j < c1.GetLength(1); j++) {
1865                                 AssertEquals("#M65(" + i + "," + j + ")",
1866                                         c1[i,j], c2[i,j]);
1867                         }
1868                 }
1869         }
1870
1871         [Test]
1872         public void TestSetValue4() {
1873                 {
1874                         int[] c1 = { 1, 2, 3 };
1875                         long[] c2 = new long [3];
1876
1877                         for (int i = 0; i < c1.Length; i++)
1878                                 c2.SetValue (c1 [i], i);
1879
1880                         for (int i = 0; i < c1.Length; i++) {
1881                                 Assert ("#M81(" + i + ")", c1[i] == c2[i]);
1882                                 AssertEquals ("#M82(" + i + ")", typeof (long), c2[i].GetType ());
1883                         }
1884                 }
1885                 {
1886                         long[] c1 = { 1, 2, 3 };
1887                         int[] c2 = new int [3];
1888                         bool errorThrown = false;
1889                         try {
1890                                 c2.SetValue (c1 [0], 0);
1891                         } catch (ArgumentException) {
1892                                 errorThrown = true;
1893                         }
1894                         Assert("#M83", errorThrown);
1895                 }
1896                 {
1897                         int[] c1 = { 1, 2, 3 };
1898                         Object[] c2 = new Object [3];
1899
1900                         for (int i = 0; i < c1.Length; i++)
1901                                 c2.SetValue (c1 [i], i);
1902
1903                         for (int i = 0; i < c1.Length; i++)
1904                                 AssertEquals ("#M84(" + i + ")", c1[i], Convert.ToInt32 (c2[i]));
1905                 }
1906                 {
1907                         Object[] c1 = new Object [3];
1908                         Object[] c2 = new Object [3];
1909                         c1[0] = new Object ();
1910
1911                         for (int i = 0; i < c1.Length; i++)
1912                                 c2.SetValue (c1 [i], i);
1913
1914                         for (int i = 0; i < c1.Length; i++)
1915                                 AssertEquals ("#M85(" + i + ")", c1[i], c2[i]);
1916                 }
1917                 {
1918                         Object[] c1 = new Object [3];
1919                         string[] c2 = new String [3];
1920                         string test = "hello";
1921                         c1[0] = test;
1922
1923                         c2.SetValue (c1 [0], 0);
1924                         AssertEquals ("#M86", c1[0], c2[0]);
1925                         AssertEquals ("#M87", "hello", c2[0]);
1926                 }
1927                 {
1928                         char[] c1 = { 'a', 'b', 'c' };
1929                         string[] c2 = new string [3];
1930                         try {
1931                                 c2.SetValue (c1 [0], 0);
1932                                 Fail ("#M88");
1933                         } catch (InvalidCastException) {}
1934                 }
1935                 {
1936                         Single[] c1 = { 1.2F, 2.3F, 3.4F, 4.5F };
1937                         long[] c2 = new long [3];
1938                         try {
1939                                 c2.SetValue (c1 [0], 0);
1940                                 Fail ("#M89");
1941                         } catch (ArgumentException) {}
1942                 }
1943                 {
1944                         Type[] types = {
1945                                 typeof (Boolean),
1946                                 typeof (Byte),
1947                                 typeof (Char),
1948                                 typeof (Double),
1949                                 typeof (Int16),
1950                                 typeof (Int32),
1951                                 typeof (Int64),
1952                                 typeof (SByte),
1953                                 typeof (Single),
1954                                 typeof (UInt16),
1955                                 typeof (UInt32),
1956                                 typeof (UInt64)
1957                         };
1958
1959                         bool v1 = true;
1960                         Byte v2 = 1;
1961                         Char v3 = 'a';
1962                         Double v4 = -1.2;
1963                         Int16 v5 = -32;
1964                         Int32 v6 = -234;
1965                         Int64 v7 = -34523;
1966                         SByte v8 = -1;
1967                         Single v9 = -4.8F;
1968                         UInt16 v10 = 24234;
1969                         UInt32 v11 = 235354;
1970                         UInt64 v12 = 234552;
1971
1972                         Object[] va1 = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12 };
1973                         Object[] va2 = { "true", "1", "a", "-1.2", "-32", "-234", "-34523", "-1",
1974                                          "-4.8F", "24234", "235354", "234552" };
1975
1976                         Object[][] vt = { va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1 };
1977
1978                         int[] arg_ex = {
1979                                 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1980                                 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
1981                                 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
1982                                 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1983                                 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1,
1984                                 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1,
1985                                 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,
1986                                 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1,
1987                                 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1,
1988                                 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
1989                                 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0,
1990                                 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0
1991                         };
1992
1993                         // SetValue
1994
1995                         for (int i = 0; i < types.Length; i++) {
1996                                 for (int j = 0; j < types.Length; j++) {
1997                                         Array array = Array.CreateInstance (types [j], 2);
1998
1999                                         Object value = vt[j][i];
2000
2001                                         bool errorThrown = false;
2002                                         try {
2003                                                 array.SetValue (value, 0);
2004                                         } catch (ArgumentException) {
2005                                                 errorThrown = true;
2006                                         }
2007
2008                                         int ex_index = (i * types.Length) + j;
2009
2010                                         AssertEquals ("#M90(" + types [i] + "," + types [j] + ")",
2011                                                 errorThrown, arg_ex [ex_index] == 1);
2012                                 }
2013                         }
2014
2015                         for (int i = 0; i < types.Length; i++) {
2016                                 String[] array = new String [2];
2017
2018                                 Object value = va1 [i];
2019
2020                                 bool errorThrown = false;
2021                                 try {
2022                                         array.SetValue (value, 0);
2023                                 } catch (InvalidCastException) {
2024                                         errorThrown = true;
2025                                 }
2026
2027                                 Assert ("#M91(" + types [i] + ")", errorThrown);
2028                         }
2029
2030                         for (int i = 0; i < types.Length; i++) {
2031                                 Array array = Array.CreateInstance (types [i], 2);
2032
2033                                 Object value = va2 [i];
2034
2035                                 bool errorThrown = false;
2036                                 try {
2037                                         array.SetValue (value, 0);
2038                                 } catch (InvalidCastException) {
2039                                         errorThrown = true;
2040                                 }
2041
2042                                 Assert ("#M92(" + types [i] + ")", errorThrown);
2043                         }
2044
2045                         for (int i = 0; i < types.Length; i++) {
2046                                 Array array = Array.CreateInstance (types [i], 2);
2047
2048                                 Object value = null;
2049
2050                                 bool errorThrown = false;
2051                                 try {
2052                                         array.SetValue (value, 0);
2053                                 } catch (InvalidCastException) {
2054                                         errorThrown = true;
2055                                 }
2056
2057                                 Assert ("#M93(" + types [i] + ")", !errorThrown);
2058                         }
2059
2060                         // Copy
2061
2062                         for (int i = 0; i < types.Length; i++) {
2063                                 for (int j = 0; j < types.Length; j++) {
2064                                         Array source = Array.CreateInstance (types [i], 2);
2065                                         Array array = Array.CreateInstance (types [j], 2);
2066
2067                                         source.SetValue (vt[j][i], 0);
2068                                         source.SetValue (vt[j][i], 1);
2069
2070                                         bool errorThrown = false;
2071                                         try {
2072                                                 Array.Copy (source, array, 2);
2073                                         } catch (ArrayTypeMismatchException) {
2074                                                 errorThrown = true;
2075                                         }
2076
2077                                         int ex_index = (i * types.Length) + j;
2078
2079                                         AssertEquals ("#M94(" + types [i] + "," + types [j] + ")",
2080                                                 errorThrown, arg_ex [ex_index] == 1);
2081                                 }
2082                         }
2083
2084                         for (int i = 0; i < types.Length; i++) {
2085                                 Array source = Array.CreateInstance (types [i], 2);
2086                                 String[] array = new String [2];
2087
2088                                 source.SetValue (va1 [i], 0);
2089                                 source.SetValue (va1 [i], 1);
2090
2091                                 bool errorThrown = false;
2092                                 try {
2093                                         Array.Copy (source, array, 2);
2094                                 } catch (ArrayTypeMismatchException) {
2095                                         errorThrown = true;
2096                                 }
2097
2098                                 Assert ("#M95(" + types [i] + ")", errorThrown);
2099                         }
2100
2101                         for (int i = 0; i < types.Length; i++) {
2102                                 String[] source = new String [2];
2103                                 Array array = Array.CreateInstance (types [i], 2);
2104
2105                                 source.SetValue (va2 [i], 0);
2106                                 source.SetValue (va2 [i], 1);
2107
2108                                 bool errorThrown = false;
2109                                 try {
2110                                         Array.Copy (source, array, 2);
2111                                 } catch (ArrayTypeMismatchException) {
2112                                         errorThrown = true;
2113                                 }
2114
2115                                 Assert ("#M96(" + types [i] + ")", errorThrown);
2116                         }
2117                 }
2118         }
2119
2120         [Test]
2121         public void TestSort() {
2122                 {
2123                         bool errorThrown = false;
2124                         try {
2125                                 Array.Sort(null);
2126                         } catch (ArgumentNullException) {
2127                                 errorThrown = true;
2128                         }
2129                         Assert("#N01", errorThrown);
2130                 }
2131                 {
2132                         bool errorThrown = false;
2133                         try {
2134                                 Array.Sort(null, 0, 1);
2135                         } catch (ArgumentNullException) {
2136                                 errorThrown = true;
2137                         }
2138                         Assert("#N02", errorThrown);
2139                 }
2140                 {
2141                         bool errorThrown = false;
2142                         try {
2143                                 char[] c1 = new Char[2];
2144                                 Array.Sort(null, c1);
2145                         } catch (ArgumentNullException) {
2146                                 errorThrown = true;
2147                         }
2148                         Assert("#N03", errorThrown);
2149                 }
2150                 {
2151                         bool errorThrown = false;
2152                         try {
2153                                 char[] c1 = new Char[2];
2154                                 Array.Sort(null, c1, 0, 1);
2155                         } catch (ArgumentNullException) {
2156                                 errorThrown = true;
2157                         }
2158                         Assert("#N04", errorThrown);
2159                 }
2160                 {
2161                         int tc = 5;
2162                         char[] arr = {'d', 'b', 'f', 'e', 'a', 'c'};
2163                         
2164                         try {
2165                                 Array.Sort (null, 0, 1);
2166                                 Fail ("#N" + tc.ToString ());
2167                         }
2168                         catch (ArgumentException) {}
2169                         catch (Exception) { Fail ("#N" + tc.ToString ()); }
2170                         tc++;
2171                         
2172                         try {
2173                                 Array.Sort (arr, -1, 3);
2174                                 Fail ("#N" + tc.ToString ());
2175                         }
2176                         catch (ArgumentException) {}
2177                         catch (Exception) { Fail ("#N" + tc.ToString ()); }
2178                         tc++;
2179                         
2180                         try {
2181                                 Array.Sort (arr, 1, -3);
2182                                 Fail ("#N" + tc.ToString ());
2183                         }
2184                         catch (ArgumentException) {}
2185                         catch (Exception) { Fail ("#N" + tc.ToString ()); }
2186                         tc++;
2187                         
2188                         try {
2189                                 Array.Sort (arr, arr.Length, arr.Length + 2);
2190                                 Fail ("#N" + tc.ToString ());
2191                         }
2192                         catch (ArgumentException) {}
2193                         catch (Exception) { Fail ("#N" + tc.ToString ()); }
2194                 }
2195                 
2196                 // note: null second array => just sort first array
2197                 char[] starter = {'d', 'b', 'f', 'e', 'a', 'c'};
2198                 int[] starter1 = {1,2,3,4,5,6};
2199                 {
2200                         char[] c1 = (char[])starter.Clone();
2201                         Array.Sort(c1);
2202                         AssertEquals("#N21", 'a', c1[0]);
2203                         AssertEquals("#N22", 'b', c1[1]);
2204                         AssertEquals("#N23", 'c', c1[2]);
2205                         AssertEquals("#N24", 'd', c1[3]);
2206                         AssertEquals("#N25", 'e', c1[4]);
2207                         AssertEquals("#N26", 'f', c1[5]);
2208                 }
2209                 {
2210                         char[] c1 = (char[])starter.Clone();
2211                         int[] i1 = (int[])starter1.Clone();
2212                         Array.Sort(c1, i1);
2213                         AssertEquals("#N41", 'a', c1[0]);
2214                         AssertEquals("#N42", 'b', c1[1]);
2215                         AssertEquals("#N43", 'c', c1[2]);
2216                         AssertEquals("#N44", 'd', c1[3]);
2217                         AssertEquals("#N45", 'e', c1[4]);
2218                         AssertEquals("#N46", 'f', c1[5]);
2219                         AssertEquals("#N47", 5, i1[0]);
2220                         AssertEquals("#N48", 2, i1[1]);
2221                         AssertEquals("#N49", 6, i1[2]);
2222                         AssertEquals("#N50", 1, i1[3]);
2223                         AssertEquals("#N51", 4, i1[4]);
2224                         AssertEquals("#N52", 3, i1[5]);
2225                 }
2226                 {
2227                         char[] c1 = (char[])starter.Clone();
2228                         Array.Sort(c1, 1, 4);
2229                         AssertEquals("#N61", 'd', c1[0]);
2230                         AssertEquals("#N62", 'a', c1[1]);
2231                         AssertEquals("#N63", 'b', c1[2]);
2232                         AssertEquals("#N64", 'e', c1[3]);
2233                         AssertEquals("#N65", 'f', c1[4]);
2234                         AssertEquals("#N66", 'c', c1[5]);
2235                 }
2236                 {
2237                         char[] c1 = (char[])starter.Clone();
2238                         int[] i1 = (int[])starter1.Clone();
2239                         Array.Sort(c1, i1, 1, 4);
2240                         AssertEquals("#N81", 'd', c1[0]);
2241                         AssertEquals("#N82", 'a', c1[1]);
2242                         AssertEquals("#N83", 'b', c1[2]);
2243                         AssertEquals("#N84", 'e', c1[3]);
2244                         AssertEquals("#N85", 'f', c1[4]);
2245                         AssertEquals("#N86", 'c', c1[5]);
2246                         AssertEquals("#N87", 1, i1[0]);
2247                         AssertEquals("#N88", 5, i1[1]);
2248                         AssertEquals("#N89", 2, i1[2]);
2249                         AssertEquals("#N90", 4, i1[3]);
2250                         AssertEquals("#N91", 3, i1[4]);
2251                         AssertEquals("#N92", 6, i1[5]);
2252                 }
2253         }
2254
2255         [Test]
2256         public void TestInitializeEmpty()
2257         {
2258                 bool catched=false;
2259                 int[] a = {};
2260                 try
2261                 {
2262                         a.Initialize();
2263                 }
2264                 catch(Exception)
2265                 {
2266                         catched=true;
2267                 }
2268                 Assert("#TI01",!catched);
2269         }
2270
2271         [Test]
2272         public void TestInitializeInt()
2273         {
2274                 int[] a = {1,2,0};
2275                 a.Initialize();
2276                 int[] b = {1,2,0};
2277                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2278                 {
2279                         AssertEquals("#TI02 " + i ,a[i],b[i]);
2280                 }
2281         }
2282
2283         [Test]
2284         public void TestInitializeDouble()
2285         {
2286                 double[] a = {1.0,2.0,0.0};
2287                 a.Initialize();
2288                 double[] b = {1.0,2.0,0.0};
2289                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2290                 {
2291                         AssertEquals("#TI03 " + i ,a[i],b[i]);
2292                 }
2293         }
2294
2295         [Test]
2296         public void TestInitializeFloat()
2297         {
2298                 float[] a = {1.0F,2.0F,0.0F};
2299                 a.Initialize();
2300                 float[] b = {1.0F,2.0F,0.0F};
2301                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2302                 {
2303                         AssertEquals("#TI04 " + i ,a[i],b[i]);
2304                 }
2305         }
2306
2307         [Test]
2308         public void TestInitializeChar()
2309         {
2310                 char[] a = {'1','.','0','F','2','.','0','F'};
2311                 a.Initialize();
2312                 char[] b = {'1','.','0','F','2','.','0','F'};
2313                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2314                 {
2315                         AssertEquals("#TI05 " + i ,a[i],b[i]);
2316                 }
2317         }
2318
2319         [Test]
2320         public void TestInitializeString()
2321         {
2322                 string[] a = {"hola","adios","menos","mas"};
2323                 a.Initialize();
2324                 string[] b = {"hola","adios","menos","mas"};
2325                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2326                 {
2327                         AssertEquals("#TI06 " + i ,a[i],b[i]);
2328                 }
2329         }
2330
2331         [Test]
2332         public void TestInitializeEnum()
2333         {
2334                 enua[] a = {enua.hola,enua.adios,enua.menos,enua.mas};
2335                 a.Initialize();
2336                 enua[] b = {enua.hola,enua.adios,enua.menos,enua.mas};
2337                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2338                 {
2339                         AssertEquals("#TI07 " + i ,a[i],b[i]);
2340                 }
2341         }
2342         
2343         [Test]
2344         public void TestInitializeIntNI()
2345         {
2346                 int[] a = new int[20];
2347                 a.Initialize();
2348                 foreach(int b in a)
2349                 {
2350                         AssertEquals("#TI08",b,0);
2351                 }
2352         }
2353         
2354         [Test]
2355         public void TestInitializeCharNI()
2356         {
2357                 char[] a = new char[20];
2358                 a.Initialize();
2359                 foreach(char b in a)
2360                 {
2361                         AssertEquals("#TI09",b,0);
2362                 }
2363         }
2364         
2365         [Test]
2366         public void TestInitializeDoubleNI()
2367         {
2368                 double[] a = new double[20];
2369                 a.Initialize();
2370                 foreach(double b in a)
2371                 {
2372                         AssertEquals("#TI09",b,0.0);
2373                 }
2374         }
2375         
2376         [Test]
2377         public void TestInitializeStringNI()
2378         {
2379                 string[] a = new string[20];
2380                 a.Initialize();
2381                 foreach(string b in a)
2382                 {
2383                         AssertEquals("#TI10",b,null);
2384                 }
2385         }
2386         
2387         [Test]
2388         public void TestInitializeObjectNI()
2389         {
2390                 object[] a = new object[20];
2391                 a.Initialize();
2392                 foreach(object b in a)
2393                 {
2394                         AssertEquals("#TI11",b,null);
2395                 }
2396         }
2397
2398         [Test]
2399         public void TestInitializeAClassNI()
2400         {
2401                 AClass[] a = new AClass[20];
2402                 a.Initialize();
2403                 foreach(AClass b in a)
2404                 {
2405                         AssertEquals("#TI12",b,null);
2406                 }
2407         }
2408
2409
2410         [Test]
2411         public void TestInitializeAStructNI()
2412         {
2413                 AStruct[] a = new AStruct[20];
2414                 a.Initialize();
2415                 foreach(AStruct b in a)
2416                 {
2417                         AssertEquals("#TI14",b,new AStruct());
2418                 }
2419         }
2420
2421         [Test]
2422         public void TestInitializeAStruct()
2423         {
2424                 AStruct[] a = new AStruct[3];
2425                 a[1].a = "ADIOS";
2426                 a[1].s = "HOLA";
2427                 a.Initialize();
2428                 AStruct[] b = new AStruct[3];
2429                 b[1].a = "ADIOS";
2430                 b[1].s = "HOLA";
2431                 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2432                 {
2433                         AssertEquals("#TI15 " + i ,a[i],b[i]);
2434                 }
2435         }
2436
2437         [Test]
2438         public void TestInitializeDateTimeNI()
2439         {
2440                 DateTime[] a = new DateTime[20];
2441                 a.Initialize();
2442                 foreach(DateTime b in a)
2443                 {
2444                         AssertEquals("#TI16",b,new DateTime());
2445                 }
2446         }
2447         
2448         [Test]
2449         [ExpectedException (typeof (ArgumentNullException))]
2450         public void MoreSort1 ()
2451         {
2452                 Array.Sort (null, 0, 1);
2453         }
2454
2455         [Test]
2456         [ExpectedException (typeof (ArgumentOutOfRangeException))]
2457         public void MoreSort2 ()
2458         {
2459                 Array.Sort (arrsort, -1, 3);
2460         }
2461
2462         [Test]
2463         [ExpectedException (typeof (ArgumentOutOfRangeException))]
2464         public void MoreSort3 ()
2465         {
2466                 Array.Sort (arrsort, 1, -3);
2467         }
2468
2469         [Test]
2470         [ExpectedException (typeof (ArgumentException))]
2471         public void MoreSort4 ()
2472         {
2473                 Array.Sort (arrsort, arrsort.Length, arrsort.Length + 2);
2474         }
2475
2476         [Test]
2477         [ExpectedException (typeof (RankException))]
2478         public void MoreSort5 ()
2479         {
2480                 char [,] arr = new char [,] {{'a'}, {'b'}};
2481                 Array.Sort (arr, 0, 1);
2482         }
2483
2484         [Test]
2485         public void MoreSort6 ()
2486         {
2487                 Array.Sort (arrsort, 0, 0);
2488         }
2489
2490         [Test]
2491         [ExpectedException (typeof (ArgumentException))]
2492         public void MoreSort7 ()
2493         {
2494                 Array.Sort (arrsort, arrsort.Length - 1, 2);
2495         }
2496
2497         [Test]
2498         [ExpectedException (typeof (ArgumentException))]
2499         public void MoreSort8 ()
2500         {
2501                 Array.Sort (arrsort, 0, arrsort.Length + 1);
2502         }
2503
2504         [Test]
2505         public void MoreSort9 ()
2506         {
2507                 Array.Sort (arrsort, null, 0, arrsort.Length, null);
2508         }
2509
2510         [Test]
2511         [ExpectedException (typeof (InvalidOperationException))]
2512         public void MoreSort10 ()
2513         {
2514                 object [] array = {true, 'k', SByte.MinValue, Byte.MinValue, (short) 2, 634, (long) 436, (float) 1.1, 1.23, "Hello World"};
2515                 Array.Sort (array, (IComparer) null);
2516         }
2517
2518         [Test] // bug #81941
2519         public void Sort ()
2520         {
2521                 double [] a = new double [2] { 0.9, 0.3 };
2522                 uint [] b = new uint [2] { 4, 7 };
2523                 Array.Sort (a, b);
2524                 AssertEquals ("#1", 0.3, a [0]);
2525                 AssertEquals ("#2", 0.9, a [1]);
2526                 AssertEquals ("#3", 7, b [0]);
2527                 AssertEquals ("#4", 4, b [1]);
2528         }
2529
2530         [Test]
2531         public void ClearJaggedArray () 
2532         {
2533                 byte[][] matrix = new byte [8][];
2534                 for (int i=0; i < 8; i++) {
2535                         matrix [i] = new byte [8];
2536                         for (int j=0; j < 8; j++) {
2537                                 matrix [i][j] = 1;
2538                         }
2539                 }
2540                 Array.Clear (matrix, 0, 8);
2541                 for (int i=0; i < 8; i++) {
2542                         AssertNull (i.ToString (), matrix [i]);
2543                 }
2544         }
2545
2546         [Test]
2547         public void ClearMultidimentionalArray () 
2548         {
2549                 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2550                 Array.Clear (matrix, 0, 2);
2551                 AssertEquals ("0,0", 0, matrix [0,0]);
2552                 AssertEquals ("0,1", 0, matrix [0,1]);
2553                 AssertEquals ("1,0", 2, matrix [1,0]);
2554                 AssertEquals ("1,1", 2, matrix [1,1]);
2555         }
2556
2557         [Test]
2558         [ExpectedException (typeof (IndexOutOfRangeException))]
2559         public void ClearOutsideMultidimentionalArray () 
2560         {
2561                 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2562                 Array.Clear (matrix, 0, 5);
2563         }
2564
2565         [Test]
2566         [ExpectedException (typeof (IndexOutOfRangeException))]
2567         public void Clear_IndexOverflow () 
2568         {
2569                 byte[] array = new byte [16];
2570                 Array.Clear (array, 4, Int32.MaxValue);
2571         }
2572
2573         [Test]
2574         [ExpectedException (typeof (IndexOutOfRangeException))]
2575         public void Clear_LengthOverflow () 
2576         {
2577                 byte[] array = new byte [16];
2578                 Array.Clear (array, Int32.MaxValue, 4);
2579         }
2580
2581         [Test]
2582         [ExpectedException (typeof (ArgumentException))]
2583         public void Copy_SourceIndexOverflow () 
2584         {
2585                 byte[] array = new byte [16];
2586                 Array.Copy (array, Int32.MaxValue, array, 8, 8);
2587         }
2588
2589         [Test]
2590         [ExpectedException (typeof (ArgumentException))]
2591         public void Copy_DestinationIndexOverflow () 
2592         {
2593                 byte[] array = new byte [16];
2594                 Array.Copy (array, 8, array, Int32.MaxValue, 8);
2595         }
2596
2597         [Test]
2598         [ExpectedException (typeof (ArgumentException))]
2599         public void Copy_LengthOverflow () 
2600         {
2601                 byte[] array = new byte [16];
2602                 Array.Copy (array, 8, array, 8, Int32.MaxValue);
2603         }
2604
2605         [Test]
2606         [ExpectedException (typeof (ArgumentException))]
2607         public void Reverse_IndexOverflow () 
2608         {
2609                 byte[] array = new byte [16];
2610                 Array.Reverse (array, Int32.MaxValue, 8);
2611         }
2612
2613         [Test]
2614         [ExpectedException (typeof (ArgumentException))]
2615         public void Reverse_LengthOverflow () 
2616         {
2617                 byte[] array = new byte [16];
2618                 Array.Reverse (array, 8, Int32.MaxValue);
2619         }
2620         
2621         public struct CharX : IComparable {
2622                 public char c;
2623         
2624                 public CharX (char c)
2625                 {
2626                         this.c = c;
2627                 }
2628         
2629                 public int CompareTo (object obj)
2630                 {
2631                         if (obj is CharX)
2632                                 return c.CompareTo (((CharX) obj).c);
2633                         else
2634                                 return c.CompareTo (obj);
2635                 }
2636         }
2637
2638         [Test]
2639         public void BinarySearch_ArgPassingOrder ()
2640         {
2641                 //
2642                 // This tests that arguments are passed to the comprer in the correct
2643                 // order. The IComparable of the *array* elements must get called, not
2644                 // that of the search object.
2645                 //
2646                 CharX [] x = { new CharX ('a'), new CharX ('b'), new CharX ('c') };
2647                 AssertEquals (1, Array.BinarySearch (x, 'b'));
2648         }
2649
2650         class Comparer: IComparer {
2651
2652                 private bool called = false;
2653
2654                 public bool Called {
2655                         get {
2656                                 bool result = called;
2657                                 called = false;
2658                                 return called;
2659                         }
2660                 }
2661
2662                 public int Compare (object x, object y)
2663                 {
2664                         called = true;
2665                         return 0;
2666                 }
2667         }
2668
2669         [Test]
2670         public void BinarySearch1_EmptyList ()
2671         {
2672                 int[] array = new int[0];
2673                 AssertEquals ("BinarySearch", - 1, Array.BinarySearch (array, 0));
2674         }
2675
2676         [Test]
2677         public void BinarySearch2_EmptyList ()
2678         {
2679                 int[] array = new int[0];
2680                 AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, 0, 0));
2681         }
2682
2683         [Test]
2684         public void BinarySearch3_EmptyList ()
2685         {
2686                 Comparer comparer = new Comparer ();
2687                 int[] array = new int[0];
2688                 AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, comparer));
2689                 // bug 77030 - the comparer isn't called for an empty array/list
2690                 Assert ("Called", !comparer.Called);
2691         }
2692
2693         [Test]
2694         public void BinarySearch4_EmptyList ()
2695         {
2696                 Comparer comparer = new Comparer ();
2697                 int[] array = new int[0];
2698                 AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, 0, comparer));
2699                 // bug 77030 - the comparer isn't called for an empty array/list
2700                 Assert ("Called", !comparer.Called);
2701         }
2702
2703 #if NET_2_0
2704         [Test]
2705         [ExpectedException (typeof (ArgumentNullException))]
2706         public void AsReadOnly_NullArray ()
2707         {
2708                 Array.AsReadOnly <int> (null);
2709         }
2710
2711         [Test]
2712         public void ReadOnly_Count ()
2713         {
2714                 AssertEquals (10, Array.AsReadOnly (new int [10]).Count);
2715         }
2716
2717         [Test]
2718         public void ReadOnly_Contains ()
2719         {
2720                 int[] arr = new int [2];
2721                 arr [0] = 3;
2722                 arr [1] = 5;
2723                 IList<int> a = Array.AsReadOnly (arr);
2724
2725                 Assert (a.Contains (3));
2726                 Assert (!a.Contains (6));
2727         }
2728
2729         [Test]
2730         public void ReadOnly_IndexOf ()
2731         {
2732                 int[] arr = new int [2];
2733                 arr [0] = 3;
2734                 arr [1] = 5;
2735                 IList<int> a = Array.AsReadOnly (arr);
2736
2737                 AssertEquals (0, a.IndexOf (3));
2738                 AssertEquals (1, a.IndexOf (5));
2739                 AssertEquals (-1, a.IndexOf (6));
2740         }
2741
2742         [Test]
2743         public void ReadOnly_Indexer ()
2744         {
2745                 int[] arr = new int [2];
2746                 arr [0] = 3;
2747                 arr [1] = 5;
2748                 IList<int> a = Array.AsReadOnly (arr);
2749
2750                 AssertEquals (3, a [0]);
2751                 AssertEquals (5, a [1]);
2752
2753                 /* Check that modifications to the original array are visible */
2754                 arr [0] = 6;
2755                 AssertEquals (6, a [0]);
2756         }
2757
2758         [Test]
2759         public void ReadOnly_Enumerator ()
2760         {
2761                 int[] arr = new int [10];
2762
2763                 for (int i = 0; i < 10; ++i)
2764                         arr [i] = i;
2765
2766                 int sum = 0;
2767                 foreach (int i in Array.AsReadOnly (arr))
2768                         sum += i;
2769
2770                 AssertEquals (45, sum);
2771         }
2772
2773         [Test]
2774         public void Resize ()
2775         {
2776                 int [] arr = new int [] { 1, 3, 5 };
2777                 Array.Resize <int> (ref arr, 3);
2778                 AssertEquals ("#A1", 3, arr.Length);
2779                 AssertEquals ("#A2", 1, arr [0]);
2780                 AssertEquals ("#A3", 3, arr [1]);
2781                 AssertEquals ("#A4", 5, arr [2]);
2782
2783                 Array.Resize <int> (ref arr, 2);
2784                 AssertEquals ("#B1", 2, arr.Length);
2785                 AssertEquals ("#B2", 1, arr [0]);
2786                 AssertEquals ("#B3", 3, arr [1]);
2787
2788                 Array.Resize <int> (ref arr, 4);
2789                 AssertEquals ("#C1", 4, arr.Length);
2790                 AssertEquals ("#C2", 1, arr [0]);
2791                 AssertEquals ("#C3", 3, arr [1]);
2792                 AssertEquals ("#C4", 0, arr [2]);
2793                 AssertEquals ("#C5", 0, arr [3]);
2794         }
2795
2796         [Test]
2797         public void Resize_null ()
2798         {
2799                 int [] arr = null;
2800                 Array.Resize (ref arr, 10);
2801                 AssertEquals (arr.Length, 10);
2802         }
2803
2804         [Test]
2805         public void Test_ContainsAndIndexOf_EquatableItem ()
2806         {
2807                 EquatableClass[] list = new EquatableClass[] {new EquatableClass (0), new EquatableClass (1), new EquatableClass (0)};
2808
2809                 AssertEquals ("#0", 0, Array.IndexOf<EquatableClass> (list, list[0]));
2810                 AssertEquals ("#1", 0, Array.IndexOf<EquatableClass> (list, new EquatableClass (0)));
2811                 AssertEquals ("#2", 2, Array.LastIndexOf<EquatableClass> (list, list[0]));
2812                 AssertEquals ("#3", 2, Array.LastIndexOf<EquatableClass> (list, new EquatableClass (0)));
2813         }
2814
2815         public class EquatableClass : IEquatable<EquatableClass>
2816         {
2817                 int _x;
2818                 public EquatableClass (int x)
2819                 {
2820                         _x = x;
2821                 }
2822
2823                 public bool Equals (EquatableClass other)
2824                 {
2825                         return this._x == other._x;
2826                 }
2827         }
2828
2829         [Test]
2830         public void AsIList ()
2831         {
2832                 IList<int> arr = new int [10];
2833                 arr [0] = 5;
2834                 AssertEquals (5, arr [0]);
2835
2836                 IList<FooStruct> arr2 = new FooStruct [10];
2837                 FooStruct s = new FooStruct ();
2838                 s.i = 11;
2839                 s.j = 22;
2840                 arr2 [5] = s;
2841                 s = arr2 [5];
2842                 AssertEquals (11, s.i);
2843                 AssertEquals (22, s.j);
2844
2845                 IList<string> arr3 = new string [10];
2846                 arr3 [5] = "ABC";
2847                 AssertEquals ("ABC", arr3 [5]);
2848         }
2849
2850         struct FooStruct {
2851                 public int i, j;
2852         }
2853
2854 #if !TARGET_JVM // BugBUG: T[] is not yet ICollection<T> under TARGET_JVM
2855         [Test]
2856         // From bug #80563
2857         public void ICollectionNull ()
2858         {
2859                 ICollection<object> test;
2860                 
2861                 test = new List<object>();
2862                 AssertEquals ("list<o>", test.Contains (null), false);
2863
2864                 test = new object[] {};
2865                 AssertEquals ("empty array", test.Contains (null), false);
2866
2867                 test = new object[] {null};
2868                 AssertEquals ("array with null", test.Contains (null), true);
2869
2870                 test = new List<object>(test);
2871                 AssertEquals ("List<object> with test", test.Contains (null), true);
2872                 
2873                 test = new object[] {new object()};
2874                 AssertEquals ("array with object", test.Contains (null), false);
2875
2876                 test = new List<object>(test);
2877                 AssertEquals ("array with test", test.Contains (null), false);
2878         }
2879 #endif // TARGET_JVM
2880 #endif
2881
2882         #region Bug 80299
2883
2884         enum ByteEnum : byte {}
2885         enum IntEnum : int {}
2886
2887         [Test]
2888         public void TestByteEnumArrayToByteArray ()
2889         {
2890                 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
2891                 byte[] b = new byte[a.Length];
2892                 a.CopyTo (b, 0);
2893         }
2894
2895         [Test]
2896         public void TestByteEnumArrayToIntArray ()
2897         {
2898                 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
2899                 int[] b = new int[a.Length];
2900                 a.CopyTo (b, 0);
2901         }
2902
2903         [Test]
2904         [ExpectedException (typeof (ArrayTypeMismatchException))]
2905         public void TestIntEnumArrayToByteArray ()
2906         {
2907                 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
2908                 byte[] b = new byte[a.Length];
2909                 a.CopyTo (b, 0);
2910         }
2911
2912         [Test]
2913         public void TestIntEnumArrayToIntArray ()
2914         {
2915                 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
2916                 int[] b = new int[a.Length];
2917                 a.CopyTo (b, 0);
2918         }
2919
2920         #endregion
2921
2922 #if NET_2_0
2923         [Test] // bug #322248
2924         public void IEnumerator_Reset ()
2925         {
2926                 int[] array = new int[] { 1, 2, 3};
2927                 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
2928                 Assert ("#A1", e.MoveNext ());
2929                 AssertEquals ("#A2", 1, e.Current);
2930                 Assert ("#A3", e.MoveNext ());
2931                 AssertEquals ("#A4", 2, e.Current);
2932
2933                 e.Reset ();
2934
2935                 Assert ("#C1", e.MoveNext ());
2936                 AssertEquals ("#C2", 1, e.Current);
2937         }
2938
2939         [Test]
2940         public void IEnumerator_Current_Finished ()
2941         {
2942                 int[] array = new int[] { 1, 2, 3 };
2943                 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
2944                 Assert ("#A1", e.MoveNext ());
2945                 AssertEquals ("#A2", 1, e.Current);
2946                 Assert ("#A3", e.MoveNext ());
2947                 AssertEquals ("#A4", 2, e.Current);
2948                 Assert ("#A5", e.MoveNext ());
2949                 AssertEquals ("#A6", 3, e.Current);
2950                 Assert ("#A6", !e.MoveNext ());
2951
2952                 try {
2953                         Fail ("#B1:" + e.Current);
2954                 } catch (InvalidOperationException ex) {
2955                         // Enumeration already finished
2956                         AssertEquals ("#B2", typeof (InvalidOperationException), ex.GetType ());
2957                         AssertNull ("#B3", ex.InnerException);
2958                         AssertNotNull ("#B4", ex.Message);
2959                 }
2960         }
2961
2962         [Test]
2963         public void IEnumerator_Current_NotStarted ()
2964         {
2965                 int[] array = new int[] { 1, 2, 3 };
2966                 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
2967
2968                 try {
2969                         Fail ("#A1:" + e.Current);
2970                 } catch (InvalidOperationException ex) {
2971                         // Enumeration has not started. Call MoveNext
2972                         AssertEquals ("#A2", typeof (InvalidOperationException), ex.GetType ());
2973                         AssertNull ("#A3", ex.InnerException);
2974                         AssertNotNull ("#A4", ex.Message);
2975                 }
2976         }
2977
2978         [Test]
2979         public void IEnumerator_Current_Reset ()
2980         {
2981                 int[] array = new int[] { 1, 2, 3 };
2982                 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
2983                 e.MoveNext ();
2984                 e.Reset ();
2985
2986                 try {
2987                         Fail ("#B1:" + e.Current);
2988                 } catch (InvalidOperationException ex) {
2989                         // Enumeration has not started. Call MoveNext
2990                         AssertEquals ("#B2", typeof (InvalidOperationException), ex.GetType ());
2991                         AssertNull ("#B3", ex.InnerException);
2992                         AssertNotNull ("#B4", ex.Message);
2993                 }
2994         }
2995 #endif
2996 }
2997 }