1 // ArrayTest.cs - NUnit Test Cases for the System.Array class
3 // David Brandt (bucky@keystreams.com)
4 // Eduardo Garcia (kiwnix@yahoo.es)
6 // (C) Ximian, Inc. http://www.ximian.com
7 // Copyright (C) 2004 Novell (http://www.novell.com)
10 using NUnit.Framework;
12 using System.Collections;
13 using System.Globalization;
16 using System.Collections.Generic;
19 namespace MonoTests.System
24 enum enua {hola,adios,mas,menos};
47 //End Auxiliary Things
50 public class ArrayTest : Assertion
52 char [] arrsort = {'d', 'b', 'f', 'e', 'a', 'c'};
57 public void TestIsFixedSize() {
59 Assert("All arrays are fixed", a1.IsFixedSize);
63 public void TestIsReadOnly() {
65 Assert("No array is readonly", !a1.IsReadOnly);
69 public void TestIsSynchronized() {
71 Assert("No array is synchronized", !a1.IsSynchronized);
75 public void TestLength() {
78 AssertEquals("Zero length array", 0, a1.Length);
82 AssertEquals("One-length array", 1, a1.Length);
85 char[] a1 = {'c', 'c'};
86 AssertEquals("Two-length array", 2, a1.Length);
91 public void TestRank() {
92 char[] a1 = { 'c', 'd', 'e' };
93 AssertEquals("Rank one", 1, a1.Rank);
95 char[,] a2 = new Char[3,3];
96 AssertEquals("Rank two", 2, a2.Rank);
98 char[,,] a3 = new Char[3,3,3];
99 AssertEquals("Rank three", 3, a3.Rank);
103 public void TestBinarySearch1() {
104 bool errorThrown = false;
106 Array.BinarySearch(null, "blue");
107 } catch (ArgumentNullException) {
110 Assert("#B01", errorThrown);
113 char[,] c1 = new Char[2,2];
114 Array.BinarySearch(c1, "needle");
115 } catch (RankException) {
118 Assert("#B02", errorThrown);
121 char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
123 Array.BinarySearch(arr, 'c') >= 3);
125 Array.BinarySearch(arr, 'c') < 6);
128 char[] arr = {'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
130 -4, Array.BinarySearch(arr, 'c'));
133 char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
135 -9, Array.BinarySearch(arr, 'e'));
140 public void TestBinarySearch2() {
141 bool errorThrown = false;
143 Array.BinarySearch(null, 0, 1, "blue");
144 } catch (ArgumentNullException) {
147 Assert("#B20", errorThrown);
150 char[,] c1 = new Char[2,2];
151 Array.BinarySearch(c1, 0, 1, "needle");
152 } catch (RankException) {
155 Assert("#B21", errorThrown);
159 Array.BinarySearch(c1, -1, 1, 'a');
160 } catch (ArgumentOutOfRangeException) {
163 Assert("#B22", errorThrown);
167 Array.BinarySearch(c1, 0, -1, 'a');
168 } catch (ArgumentOutOfRangeException) {
171 Assert("#B23", errorThrown);
175 Array.BinarySearch(c1, 0, 4, 'a');
176 } catch (ArgumentException) {
179 Assert("#B24", errorThrown);
182 char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
184 Array.BinarySearch(arr, 2, 8, 'c') >= 5);
186 Array.BinarySearch(arr, 2, 8, 'c') < 8);
189 char[] arr = {'z', 'z', 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
191 -6, Array.BinarySearch(arr, 2, 8, 'c'));
194 char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
196 -11, Array.BinarySearch(arr, 2, 8, 'e'));
200 public void TestBinarySearch3()
202 int[] array = new int[100];
204 for (int i = 0; i < 100; i++)
207 AssertEquals("#B30", 49, Array.BinarySearch(array, 10));
211 public void BinarySearch_NullValue ()
213 int[] array = new int[1];
214 AssertEquals ("I=a,o", -1, Array.BinarySearch (array, null));
215 AssertEquals ("I=a,o,c", -1, Array.BinarySearch (array, null, null));
216 AssertEquals ("I=a,i,i,o", -1, Array.BinarySearch (array, 0, 1, null));
217 AssertEquals ("I=a,i,i,o,c", -1, Array.BinarySearch (array, 0, 1, null,null));
219 object[] o = new object [3] { this, this, null };
220 AssertEquals ("O=a,o", -1, Array.BinarySearch (o, null));
221 AssertEquals ("O=a,o,c", -1, Array.BinarySearch (o, null, null));
222 AssertEquals ("O=a,i,i,o", -1, Array.BinarySearch (o, 0, 3, null));
223 AssertEquals ("O=a,i,i,o,c", -1, Array.BinarySearch (o, 0, 3, null, null));
226 // TODO - testBinarySearch with explicit IComparer args
229 public void TestClear() {
230 bool errorThrown = false;
232 Array.Clear(null, 0, 1);
233 } catch (ArgumentNullException) {
236 Assert("#C01", errorThrown);
238 int[] i1 = { 1, 2, 3, 4 };
240 int[] compare = {1,2,3,4};
241 AssertEquals("#C02", compare[0], i1[0]);
242 AssertEquals("#C03", compare[1], i1[1]);
243 AssertEquals("#C04", compare[2], i1[2]);
244 AssertEquals("#C05", compare[3], i1[3]);
246 Array.Clear(i1, 3, 1);
248 int[] compare = {1,2,3,0};
249 AssertEquals("#C06", compare[0], i1[0]);
250 AssertEquals("#C07", compare[1], i1[1]);
251 AssertEquals("#C08", compare[2], i1[2]);
252 AssertEquals("#C09", compare[3], i1[3]);
254 Array.Clear(i1, 1, 1);
256 int[] compare = {1,0,3,0};
257 AssertEquals("#C10", compare[0], i1[0]);
258 AssertEquals("#C11", compare[1], i1[1]);
259 AssertEquals("#C12", compare[2], i1[2]);
260 AssertEquals("#C13", compare[3], i1[3]);
262 Array.Clear(i1, 1, 3);
264 int[] compare = {1,0,0,0};
265 AssertEquals("#C14", compare[0], i1[0]);
266 AssertEquals("#C15", compare[1], i1[1]);
267 AssertEquals("#C16", compare[2], i1[2]);
268 AssertEquals("#C17", compare[3], i1[3]);
271 string[] s1 = { "red", "green", "blue" };
272 Array.Clear(s1, 0, 3);
274 string[] compare = {null, null, null};
275 AssertEquals("#C18", compare[0], s1[0]);
276 AssertEquals("#C19", compare[1], s1[1]);
277 AssertEquals("#C20", compare[2], s1[2]);
282 public void TestClone() {
283 char[] c1 = {'a', 'b', 'c'};
284 char[] c2 = (char[])c1.Clone();
285 AssertEquals("#D01", c1[0], c2[0]);
286 AssertEquals("#D02", c1[1], c2[1]);
287 AssertEquals("#D03", c1[2], c2[2]);
289 char[] d10 = {'a', 'b'};
290 char[] d11 = {'a', 'c'};
291 char[] d12 = {'b', 'c'};
292 char[][] d1 = {d10, d11, d12};
293 char[][] d2 = (char[][])d1.Clone();
294 AssertEquals("#D04", d1[0], d2[0]);
295 AssertEquals("#D05", d1[1], d2[1]);
296 AssertEquals("#D06", d1[2], d2[2]);
299 AssertEquals("#D07", d1[0], d2[0]);
303 public void TestCopy() {
305 bool errorThrown = false;
308 Array.Copy(c1, null, 1);
309 } catch (ArgumentNullException) {
312 Assert("#E01", errorThrown);
315 bool errorThrown = false;
318 Array.Copy(null, c1, 1);
319 } catch (ArgumentNullException) {
322 Assert("#E02", errorThrown);
325 bool errorThrown = false;
327 Char[] c1 = new Char[1];
328 Char[,] c2 = new Char[1,1];
329 Array.Copy(c1, c2, 1);
330 } catch (RankException) {
333 Assert("#E03", errorThrown);
336 bool errorThrown = false;
338 Char[] c1 = new Char[1];
339 string[] s1 = new String[1];
340 Array.Copy(c1, s1, 1);
341 } catch (ArrayTypeMismatchException) {
344 Assert("#E04", errorThrown);
347 bool errorThrown = false;
349 Char[] c1 = new Char[1];
350 Object[] o1 = new Object[1];
352 Array.Copy(o1, c1, 1);
353 } catch (InvalidCastException) {
356 Assert("#E05", errorThrown);
359 bool errorThrown = false;
361 Char[] c1 = new Char[1];
362 Char[] c2 = new Char[1];
363 Array.Copy(c1, c2, -1);
364 } catch (ArgumentOutOfRangeException) {
367 Assert("#E06", errorThrown);
370 bool errorThrown = false;
372 Char[] c1 = new Char[1];
373 Char[] c2 = new Char[2];
374 Array.Copy(c1, c2, 2);
375 } catch (ArgumentException) {
378 Assert("#E07", errorThrown);
381 bool errorThrown = false;
383 Char[] c1 = new Char[1];
384 Char[] c2 = new Char[2];
385 Array.Copy(c2, c1, 2);
386 } catch (ArgumentException) {
389 Assert("#E08", errorThrown);
392 char[] orig = {'a', 'b', 'd', 'a'};
393 char[] copy = new Char[4];
394 Array.Copy(orig, copy, 4);
395 for (int i = 0; i < orig.Length; i++) {
396 AssertEquals("#E09(" + i + ")",
399 Array.Clear(copy, 0, copy.Length);
400 for (int i = 0; i < orig.Length; i++) {
401 AssertEquals("#E10(" + i + ")",
404 Array.Copy(orig, copy, 2);
405 AssertEquals("#E11", orig[0], copy[0]);
406 AssertEquals("#E12", orig[1], copy[1]);
407 Assert("#E13", orig[2] != copy[2]);
408 Assert("#E14", orig[3] != copy[3]);
412 public void TestCopy2() {
414 bool errorThrown = false;
416 Char[] c1 = new Char[2];
417 Char[] c2 = new Char[2];
418 Array.Copy(c2, 1, c1, 0, 2);
419 } catch (ArgumentException) {
422 Assert("#E31", errorThrown);
425 bool errorThrown = false;
427 Char[] c1 = new Char[2];
428 Char[] c2 = new Char[2];
429 Array.Copy(c2, 0, c1, 1, 2);
430 } catch (ArgumentException) {
433 Assert("#E32", errorThrown);
436 char[] orig = {'a', 'b', 'd', 'a'};
437 char[] copy = new Char[4];
438 Array.Copy(orig, 1, copy, 1, 3);
439 Assert("#E33", copy[0] != orig[0]);
440 for (int i = 1; i < orig.Length; i++) {
441 AssertEquals("#E34(" + i + ")",
444 Array.Clear(copy, 0, copy.Length);
445 Array.Copy(orig, 1, copy, 0, 2);
446 AssertEquals("#E35", orig[1], copy[0]);
447 AssertEquals("#E36", orig[2], copy[1]);
448 Assert("#E37", copy[2] != orig[2]);
449 Assert("#E38", copy[3] != orig[3]);
453 public void TestCopyTo() {
455 bool errorThrown = false;
457 Char[] c1 = new Char[2];
459 } catch (ArgumentNullException) {
462 Assert("#E61", errorThrown);
465 bool errorThrown = false;
467 Char[] c1 = new Char[2];
468 Char[,] c2 = new Char[2,2];
470 } catch (ArgumentException) {
473 #if TARGET_JVM // This is really implementation dependent behaviour.
474 catch (RankException) {
478 Assert("#E62", errorThrown);
481 bool errorThrown = false;
483 Char[,] c1 = new Char[2,2];
484 Char[] c2 = new Char[2];
486 } catch (RankException) {
489 Assert("#E63", errorThrown);
492 bool errorThrown = false;
494 Char[,] c1 = new Char[2,2];
495 Char[] c2 = new Char[2];
497 } catch (RankException) {
500 Assert("#E64", errorThrown);
503 bool errorThrown = false;
505 Char[] c1 = new Char[2];
506 Char[] c2 = new Char[2];
508 } catch (ArgumentOutOfRangeException) {
511 Assert("#E65", errorThrown);
514 bool errorThrown = false;
516 Char[] c1 = new Char[2];
517 Char[] c2 = new Char[2];
519 } catch (ArgumentException) {
522 Assert("#E66", errorThrown);
525 bool errorThrown = false;
527 Char[] c1 = new Char[2];
528 Char[] c2 = new Char[2];
530 } catch (ArgumentException) {
533 Assert("#E67", errorThrown);
537 bool errorThrown = false;
539 String[] c1 = new String[2];
540 // TODO: this crashes mono if there are null
541 // values in the array.
544 Char[] c2 = new Char[2];
548 } catch (ArrayTypeMismatchException) {
551 Assert("#E68", errorThrown);
554 Char[] orig = {'a', 'b', 'c', 'd'};
555 Char[] copy = new Char[10];
556 Array.Clear(copy, 0, copy.Length);
557 orig.CopyTo(copy, 3);
558 AssertEquals("#E69", (char)0, copy[0]);
559 AssertEquals("#E70", (char)0, copy[1]);
560 AssertEquals("#E71", (char)0, copy[2]);
561 AssertEquals("#E72", orig[0], copy[3]);
562 AssertEquals("#E73", orig[1], copy[4]);
563 AssertEquals("#E74", orig[2], copy[5]);
564 AssertEquals("#E75", orig[3], copy[6]);
565 AssertEquals("#E76", (char)0, copy[7]);
566 AssertEquals("#E77", (char)0, copy[8]);
567 AssertEquals("#E78", (char)0, copy[9]);
570 // The following is valid and must not throw an exception.
571 bool errorThrown = false;
573 int[] src = new int [0];
574 int[] dest = new int [0];
575 src.CopyTo (dest, 0);
576 } catch (ArgumentException) {
579 Assert("#E79", !errorThrown);
584 bool errorThrown = false;
586 CClass[] src = new CClass [] { new CClass () };
587 BClass[] dest = new BClass [1];
589 src.CopyTo (dest, 0);
591 } catch (ArrayTypeMismatchException) {
594 Assert("#E80", errorThrown);
599 public void TestCreateInstance() {
601 bool errorThrown = false;
603 Array.CreateInstance(null, 12);
604 } catch (ArgumentNullException) {
607 Assert("#F01", errorThrown);
610 bool errorThrown = false;
612 Array.CreateInstance(Type.GetType("System.Char"), -3);
613 } catch (ArgumentOutOfRangeException) {
616 Assert("#F02", errorThrown);
619 bool errorThrown = false;
621 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
622 } catch (ArgumentNullException) {
625 Assert("#F03a", errorThrown);
629 bool errorThrown = false;
631 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
632 } catch (ArgumentNullException) {
635 Assert("#F03b", errorThrown);
638 #if !TARGET_JVM // Arrays lower bounds are not supported for TARGET_JVM
640 bool errorThrown = false;
642 Array.CreateInstance(Type.GetType("System.Char"), null, null);
643 } catch (ArgumentNullException) {
646 Assert("#F04", errorThrown);
650 bool errorThrown = false;
652 int[] lengths = new int [0];
653 Array.CreateInstance(Type.GetType("System.Char"), lengths);
654 } catch (ArgumentException) {
657 Assert("#F05", errorThrown);
659 #if !TARGET_JVM // CreateInstance with lower bounds not supported for TARGET_JVM
661 bool errorThrown = false;
663 int[] lengths = new int [1];
664 int[] bounds = new int [2];
665 Array.CreateInstance(Type.GetType("System.Char"), lengths, bounds);
667 } catch (ArgumentException) {
670 Assert("#F06", errorThrown);
673 char[] c1 = (char[])Array.CreateInstance(Type.GetType("System.Char"), 12);
674 AssertEquals("#F07", 12, c1.Length);
676 Array c2 = Array.CreateInstance(Type.GetType("System.Char"), 12, 5);
677 AssertEquals("#F08", 2, c2.Rank);
678 AssertEquals("#F09", 60, c2.Length);
682 int[] lengths = { 3 };
683 int[] bounds = { 5 };
684 int[] src = { 512, 718, 912 };
685 Array array = Array.CreateInstance(typeof(int), lengths, bounds);
687 AssertEquals("#F10", 3, array.Length);
688 AssertEquals("#F11", 5, array.GetLowerBound(0));
689 AssertEquals("#F12", 7, array.GetUpperBound(0));
691 src.CopyTo (array, 5);
693 for (int i = 0; i < src.Length; i++)
694 AssertEquals("#F13(" + i + ")", src[i], array.GetValue(i+5));
697 // Test that a 1 dimensional array with 0 lower bound is the
698 // same as an szarray
699 Type szarrayType = new int [10].GetType ();
700 Assert (szarrayType == (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0})).GetType ());
701 Assert (szarrayType != (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {1})).GetType ());
706 [ExpectedException (typeof (ArgumentNullException))]
707 public void TestCreateInstance2 ()
709 Array a = Array.CreateInstance (typeof (Int32), (int[])null);
714 [ExpectedException (typeof (ArgumentNullException))]
716 [ExpectedException (typeof (NullReferenceException))]
718 public void TestCreateInstance2b ()
720 Array a = Array.CreateInstance (typeof (Int32), (long[])null);
724 public void TestGetEnumerator() {
725 String[] s1 = {"this", "is", "a", "test"};
726 IEnumerator en = s1.GetEnumerator ();
727 AssertNotNull ("#G01", en);
729 Assert ("#G02", en.MoveNext ());
730 AssertEquals ("#G03", "this", en.Current);
731 Assert ("#G04", en.MoveNext ());
732 AssertEquals ("#G05", "is", en.Current);
733 Assert ("#G06", en.MoveNext ());
734 AssertEquals ("#G07", "a", en.Current);
735 Assert ("#G08", en.MoveNext ());
736 AssertEquals ("#G09", "test", en.Current);
737 Assert ("#G10", !en.MoveNext ());
740 Assert("#G11", en.MoveNext ());
741 AssertEquals ("#G12", "this", en.Current);
743 // mutation does not invalidate array enumerator!
744 s1.SetValue ("change", 1);
745 Assert ("#G13", en.MoveNext ());
746 AssertEquals ("#G14", "change", en.Current);
750 public void TestGetEnumeratorMultipleDimension() {
751 String[,] s1 = {{"this", "is"}, {"a", "test"}};
752 IEnumerator en = s1.GetEnumerator ();
753 AssertNotNull ("#AA01", en);
755 Assert ("#AA02", en.MoveNext ());
756 AssertEquals ("#AA03", "this", en.Current);
757 Assert ("#AA04", en.MoveNext ());
758 AssertEquals ("#AA05", "is", en.Current);
759 Assert ("#AA06", en.MoveNext ());
760 AssertEquals ("#AA07", "a", en.Current);
761 Assert ("#AA08", en.MoveNext ());
762 AssertEquals ("#AA09", "test", en.Current);
763 Assert ("#AA10", !en.MoveNext ());
766 Assert("#AA11", en.MoveNext ());
767 AssertEquals ("#AA12", "this", en.Current);
770 // mutation does not invalidate array enumerator!
771 s1.SetValue ("change", idxs);
772 Assert ("#AA13", en.MoveNext ());
773 AssertEquals ("#AA14", "change", en.Current);
777 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
778 public void TestGetEnumeratorNonZeroLowerBounds() {
779 int[] myLengthsArray = new int[2] { 3, 5 };
780 int[] myBoundsArray = new int[2] { 2, 3 };
782 Array myArray=Array.CreateInstance( typeof(String), myLengthsArray, myBoundsArray );
783 for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
784 for ( int j = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++ ) {
785 int[] myIndicesArray = new int[2] { i, j };
786 myArray.SetValue( Convert.ToString(i) + j, myIndicesArray );
788 IEnumerator en = myArray.GetEnumerator ();
789 AssertNotNull ("#AB01", en);
791 // check the first couple of values
792 Assert ("#AB02", en.MoveNext ());
793 AssertEquals ("#AB03", "23", en.Current);
794 Assert ("#AB04", en.MoveNext ());
795 AssertEquals ("#AB05", "24", en.Current);
797 // then check the last element's value
800 lastElement = (string)en.Current;
801 } while (en.MoveNext());
802 AssertEquals ("#AB06", "47", lastElement);
806 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
807 public void TestIList_Add () {
808 int[] myLengthsArray = new int[2] { 3, 5 };
809 int[] myBoundsArray = new int[2] { 2, 3 };
811 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
813 ((IList)myArray).Add ("can not");
814 Fail ("IList.Add should throw");
816 catch (NotSupportedException) {
820 Fail ("IList.Add threw wrong exception type");
823 Fail("IList.Add shouldn't get this far");
827 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
828 public void TestIList_Insert () {
829 int[] myLengthsArray = new int[2] { 3, 5 };
830 int[] myBoundsArray = new int[2] { 2, 3 };
832 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
834 ((IList)myArray).Insert (0, "can not");
835 Fail ("IList.Insert should throw");
837 catch (NotSupportedException) {
841 Fail ("IList.Insert threw wrong exception type");
844 Fail("IList.Insert shouldn't get this far");
848 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
849 public void TestIList_Remove () {
850 int[] myLengthsArray = new int[2] { 3, 5 };
851 int[] myBoundsArray = new int[2] { 2, 3 };
853 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
855 ((IList)myArray).Remove ("can not");
856 Fail ("IList.Remove should throw");
858 catch (NotSupportedException) {
862 Fail ("IList.Remove threw wrong exception type");
865 Fail("IList.Remove shouldn't get this far");
869 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
870 public void TestIList_RemoveAt () {
871 int[] myLengthsArray = new int[2] { 3, 5 };
872 int[] myBoundsArray = new int[2] { 2, 3 };
874 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
876 ((IList)myArray).RemoveAt (0);
877 Fail ("IList.RemoveAt should throw");
879 catch (NotSupportedException) {
883 Fail ("IList.RemoveAt threw wrong exception type");
886 Fail("IList.RemoveAt shouldn't get this far");
890 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
891 public void TestIList_Contains () {
892 int[] myLengthsArray = new int[2] { 3, 5 };
893 int[] myBoundsArray = new int[2] { 2, 3 };
895 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
898 bool b = ((IList)myArray).Contains ("23");
899 Fail("IList.Contains should throw with multi-dimensional arrays");
901 catch (RankException) {
902 int[] iArr = new int[3] { 1, 2, 3};
903 // check the first and last items
904 Assert("AC01", ((IList)iArr).Contains (1));
905 Assert("AC02", ((IList)iArr).Contains (3));
907 // and one that is definately not there
908 Assert("AC03", !((IList)iArr).Contains (42));
912 Fail("Should not get here");
916 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
917 public void TestIList_IndexOf () {
918 int[] myLengthsArray = new int[2] { 3, 5 };
919 int[] myBoundsArray = new int[2] { 2, 3 };
921 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
924 bool b = ((IList)myArray).Contains ("23");
925 Fail("IList.Contains should throw with multi-dimensional arrays");
927 catch (RankException) {
928 int[] iArr = new int[3] { 1, 2, 3};
929 // check the first and last items
930 AssertEquals("AD01", 0, ((IList)iArr).IndexOf (1));
931 AssertEquals("AD02", 2, ((IList)iArr).IndexOf (3));
933 // and one that is definately not there
934 AssertEquals("AD03", -1, ((IList)iArr).IndexOf (42));
936 catch (Exception e) {
937 Fail("Unexpected exception: " + e.ToString());
940 // check that wierd case whem lowerbound is Int32.MinValue,
941 // so that IndexOf() needs to return Int32.MaxValue when it cannot find the object
942 int[] myLengthArray = new int[1] { 3 };
943 int[] myBoundArray = new int[1] { Int32.MinValue };
944 Array myExtremeArray=Array.CreateInstance ( typeof(String), myLengthArray, myBoundArray );
945 AssertEquals("AD04", Int32.MaxValue, ((IList)myExtremeArray).IndexOf (42));
950 public void TestGetLength() {
952 bool errorThrown = false;
954 char[] c1 = {'a', 'b', 'c'};
956 } catch (IndexOutOfRangeException) {
959 Assert("#H01", errorThrown);
962 bool errorThrown = false;
964 char[] c1 = {'a', 'b', 'c'};
966 } catch (IndexOutOfRangeException) {
969 Assert("#H02", errorThrown);
972 char[] c2 = new Char[5];
976 char[,] c3 = new Char[6,7];
984 public void TestGetLowerBound() {
986 bool errorThrown = false;
988 char[] c = {'a', 'b', 'c'};
990 } catch (IndexOutOfRangeException) {
993 Assert("#H31", errorThrown);
996 bool errorThrown = false;
998 char[] c = {'a', 'b', 'c'};
1000 } catch (IndexOutOfRangeException) {
1003 Assert("#H32", errorThrown);
1006 char[] c1 = new Char[5];
1007 AssertEquals("#H33",
1008 0, c1.GetLowerBound(0));
1010 char[,] c2 = new Char[4,4];
1011 AssertEquals("#H34",
1012 0, c2.GetLowerBound(0));
1013 AssertEquals("#H35",
1014 0, c2.GetLowerBound(1));
1018 public void TestGetUpperBound() {
1020 bool errorThrown = false;
1022 char[] c = {'a', 'b', 'c'};
1023 c.GetUpperBound(-1);
1024 } catch (IndexOutOfRangeException) {
1027 Assert("#H61", errorThrown);
1030 bool errorThrown = false;
1032 char[] c = {'a', 'b', 'c'};
1034 } catch (IndexOutOfRangeException) {
1037 Assert("#H62", errorThrown);
1040 char[] c1 = new Char[5];
1041 AssertEquals("#H63",
1042 4, c1.GetUpperBound(0));
1044 char[,] c2 = new Char[4,6];
1045 AssertEquals("#H64",
1046 3, c2.GetUpperBound(0));
1047 AssertEquals("#H65",
1048 5, c2.GetUpperBound(1));
1052 public void TestGetValue1() {
1054 bool errorThrown = false;
1056 char[,] c = new Char[2,2];
1058 } catch (ArgumentException) {
1061 Assert("#I01", errorThrown);
1064 bool errorThrown = false;
1066 char[] c = {'a', 'b', 'c'};
1068 } catch (IndexOutOfRangeException) {
1071 Assert("#I02", errorThrown);
1074 bool errorThrown = false;
1076 char[] c = {'a', 'b', 'c'};
1078 } catch (IndexOutOfRangeException) {
1081 Assert("#I03", errorThrown);
1084 char[] c1 = {'a', 'b', 'c', 'd'};
1085 for (int i = 0; i < c1.Length; i++) {
1086 AssertEquals("#I04(" + i + ")", c1[i], c1.GetValue(i));
1091 public void TestGetValue2() {
1093 bool errorThrown = false;
1095 char[] c = new Char[2];
1097 } catch (ArgumentException) {
1100 Assert("#I21", errorThrown);
1103 bool errorThrown = false;
1105 char[,] c = new Char[2,2];
1107 } catch (IndexOutOfRangeException) {
1110 Assert("#I22", errorThrown);
1113 bool errorThrown = false;
1115 char[,] c = new Char[2,2];
1117 } catch (IndexOutOfRangeException) {
1120 Assert("#I23", errorThrown);
1123 char[,] c1 = new Char[4,6];
1124 for (int i = 0; i < 24; i++) {
1127 c1[first,second] = (char)(((int)'a')+i);
1129 for (int i = 0; i < c1.GetLength(0); i++) {
1130 for (int j = 0; j < c1.GetLength(1); j++) {
1131 AssertEquals("#I24(" + i + "," + j + ")",
1132 c1[i,j], c1.GetValue(i, j));
1138 public void TestGetValue3() {
1140 bool errorThrown = false;
1142 char[] c = new Char[2];
1144 } catch (ArgumentException) {
1147 Assert("#I41", errorThrown);
1150 bool errorThrown = false;
1152 char[,,] c = new Char[2,2,2];
1153 c.GetValue(-1, 1, 1);
1154 } catch (IndexOutOfRangeException) {
1157 Assert("#I42", errorThrown);
1160 bool errorThrown = false;
1162 char[,,] c = new Char[2,2,2];
1164 } catch (IndexOutOfRangeException) {
1167 Assert("#I43", errorThrown);
1170 char[,,] c1 = new Char[4,2,3];
1171 for (int i = 0; i < 24; i++) {
1173 int remains = i % 6;
1174 int second = remains / 3;
1175 int third = remains % 3;
1176 c1[first,second, third] = (char)(((int)'a')+i);
1178 for (int i = 0; i < c1.GetLength(0); i++) {
1179 for (int j = 0; j < c1.GetLength(1); j++) {
1180 for (int k = 0; k < c1.GetLength(2); k++) {
1181 AssertEquals("#I44(" + i + "," + j + ")",
1182 c1[i,j,k], c1.GetValue(i,j,k));
1190 [ExpectedException (typeof (ArgumentNullException))]
1192 [ExpectedException (typeof (NullReferenceException))]
1194 public void TestGetValueLongArray ()
1196 char[] c = new Char[2];
1197 c.GetValue((long [])null);
1201 public void TestGetValueN() {
1203 bool errorThrown = false;
1205 char[] c = new Char[2];
1206 c.GetValue((int [])null);
1207 } catch (ArgumentNullException) {
1210 Assert("#I61a", errorThrown);
1213 bool errorThrown = false;
1215 char[] c = new Char[2];
1216 int[] coords = {1, 1};
1218 } catch (ArgumentException) {
1221 Assert("#I62", errorThrown);
1224 bool errorThrown = false;
1226 char[,] c = new Char[2,2];
1227 int[] coords = {-1, 1};
1229 } catch (IndexOutOfRangeException) {
1232 Assert("#I63", errorThrown);
1235 bool errorThrown = false;
1237 char[,] c = new Char[2,2];
1238 int[] coords = {4, 1};
1240 } catch (IndexOutOfRangeException) {
1243 Assert("#I64", errorThrown);
1246 char[,] c1 = new Char[4,6];
1247 for (int i = 0; i < 24; i++) {
1250 c1[first,second] = (char)(((int)'a')+i);
1252 for (int i = 0; i < c1.GetLength(0); i++) {
1253 for (int j = 0; j < c1.GetLength(1); j++) {
1254 int[] coords = {i, j};
1255 AssertEquals("#I65(" + i + "," + j + ")",
1256 c1[i,j], c1.GetValue(coords));
1262 public void TestIndexOf1() {
1264 bool errorThrown = false;
1266 Array.IndexOf(null, "huh?");
1267 } catch (ArgumentNullException) {
1270 Assert("#J01", errorThrown);
1273 bool errorThrown = false;
1275 char[,] c = new Char[2,2];
1276 Array.IndexOf(c, "huh?");
1277 } catch (RankException) {
1280 Assert("#J02", errorThrown);
1283 String[] s1 = {"this", "is", "a", "test"};
1284 AssertEquals("#J03", -1, Array.IndexOf(s1, null));
1285 AssertEquals("#J04", -1, Array.IndexOf(s1, "nothing"));
1286 AssertEquals("#J05", 0, Array.IndexOf(s1, "this"));
1287 AssertEquals("#J06", 3, Array.IndexOf(s1, "test"));
1291 public void TestIndexOf2() {
1293 bool errorThrown = false;
1295 Array.IndexOf(null, "huh?", 0);
1296 } catch (ArgumentNullException) {
1299 Assert("#J21", errorThrown);
1302 bool errorThrown = false;
1304 char[,] c = new Char[2,2];
1305 Array.IndexOf(c, "huh?", 0);
1306 } catch (RankException) {
1309 Assert("#J22", errorThrown);
1312 bool errorThrown = false;
1314 char[] c = new Char[2];
1315 Array.IndexOf(c, "huh?", 3);
1316 } catch (ArgumentOutOfRangeException) {
1319 Assert("#J23", errorThrown);
1322 String[] s1 = {"this", "is", "really", "a", "test"};
1323 AssertEquals("#J24", -1, Array.IndexOf(s1, null, 1));
1324 AssertEquals("#J25", -1, Array.IndexOf(s1, "nothing", 1));
1325 AssertEquals("#J26", -1, Array.IndexOf(s1, "this", 1));
1326 AssertEquals("#J27", 1, Array.IndexOf(s1, "is", 1));
1327 AssertEquals("#J28", 4, Array.IndexOf(s1, "test", 1));
1331 public void TestIndexOf3() {
1333 bool errorThrown = false;
1335 Array.IndexOf(null, "huh?", 0, 1);
1336 } catch (ArgumentNullException) {
1339 Assert("#J41", errorThrown);
1342 bool errorThrown = false;
1344 char[,] c = new Char[2,2];
1345 Array.IndexOf(c, "huh?", 0, 1);
1346 } catch (RankException) {
1349 Assert("#J42", errorThrown);
1352 bool errorThrown = false;
1354 char[] c = new Char[2];
1355 Array.IndexOf(c, "huh?", 3, 1);
1356 } catch (ArgumentOutOfRangeException) {
1359 Assert("#J43", errorThrown);
1362 bool errorThrown = false;
1364 char[] c = new Char[2];
1365 Array.IndexOf(c, "huh?", 0, 5);
1366 } catch (ArgumentOutOfRangeException) {
1369 Assert("#J44", errorThrown);
1372 String[] s1 = {"this", "is", "really", "a", "test"};
1373 AssertEquals("#J45", -1, Array.IndexOf(s1, null, 1, 3));
1374 AssertEquals("#J46", -1, Array.IndexOf(s1, "nothing", 1, 3));
1375 AssertEquals("#J47", -1, Array.IndexOf(s1, "this", 1, 3));
1376 AssertEquals("#J48", 1, Array.IndexOf(s1, "is", 1, 3));
1377 AssertEquals("#J49", -1, Array.IndexOf(s1, "test", 1, 3));
1378 AssertEquals("#J50", 3, Array.IndexOf(s1, "a", 1, 3));
1382 public void TestLastIndexOf1() {
1384 bool errorThrown = false;
1386 Array.LastIndexOf(null, "huh?");
1387 } catch (ArgumentNullException) {
1390 Assert("#K01", errorThrown);
1393 bool errorThrown = false;
1395 char[,] c = new Char[2,2];
1396 Array.LastIndexOf(c, "huh?");
1397 } catch (RankException) {
1400 Assert("#K02", errorThrown);
1403 String[] s1 = {"this", "is", "a", "a", "test"};
1404 AssertEquals("#K03", -1, Array.LastIndexOf(s1, null));
1405 AssertEquals("#K04", -1, Array.LastIndexOf(s1, "nothing"));
1406 AssertEquals("#K05", 0, Array.LastIndexOf(s1, "this"));
1407 AssertEquals("#K06", 4, Array.LastIndexOf(s1, "test"));
1408 AssertEquals("#K07", 3, Array.LastIndexOf(s1, "a"));
1412 public void TestLastIndexOf2() {
1414 bool errorThrown = false;
1416 Array.LastIndexOf(null, "huh?", 0);
1417 } catch (ArgumentNullException) {
1420 Assert("#K21", errorThrown);
1423 bool errorThrown = false;
1425 char[,] c = new Char[2,2];
1426 Array.LastIndexOf(c, "huh?", 0);
1427 } catch (RankException) {
1430 Assert("#K22", errorThrown);
1433 bool errorThrown = false;
1435 char[] c = new Char[2];
1436 Array.LastIndexOf(c, "huh?", 3);
1437 } catch (ArgumentOutOfRangeException) {
1440 Assert("#K23", errorThrown);
1443 String[] s1 = {"this", "is", "really", "a", "test"};
1444 AssertEquals("#K24", -1, Array.LastIndexOf(s1, null, 3));
1445 AssertEquals("#K25", -1, Array.LastIndexOf(s1, "nothing", 3));
1446 AssertEquals("#K26", -1, Array.LastIndexOf(s1, "test", 3));
1447 AssertEquals("#K27", 3, Array.LastIndexOf(s1, "a", 3));
1448 AssertEquals("#K28", 0, Array.LastIndexOf(s1, "this", 3));
1452 public void TestLastIndexOf3() {
1454 bool errorThrown = false;
1456 Array.LastIndexOf(null, "huh?", 0, 1);
1457 } catch (ArgumentNullException) {
1460 Assert("#K41", errorThrown);
1463 bool errorThrown = false;
1465 char[,] c = new Char[2,2];
1466 Array.LastIndexOf(c, "huh?", 0, 1);
1467 } catch (RankException) {
1470 Assert("#K42", errorThrown);
1473 bool errorThrown = false;
1475 char[] c = new Char[2];
1476 Array.LastIndexOf(c, "huh?", 3, 1);
1477 } catch (ArgumentOutOfRangeException) {
1480 Assert("#K43", errorThrown);
1483 bool errorThrown = false;
1485 char[] c = new Char[2];
1486 Array.LastIndexOf(c, "huh?", 0, 5);
1487 } catch (ArgumentOutOfRangeException) {
1490 Assert("#K44", errorThrown);
1493 String[] s1 = {"this", "is", "really", "a", "test"};
1494 AssertEquals("#K45",
1495 -1, Array.LastIndexOf(s1, null, 3, 3));
1496 AssertEquals("#K46",
1497 -1, Array.LastIndexOf(s1, "nothing", 3, 3));
1498 AssertEquals("#K47",
1499 -1, Array.LastIndexOf(s1, "this", 3, 3));
1500 AssertEquals("#K48",
1501 1, Array.LastIndexOf(s1, "is", 3, 3));
1502 AssertEquals("#K49",
1503 -1, Array.LastIndexOf(s1, "test", 3, 3));
1504 AssertEquals("#K50",
1505 3, Array.LastIndexOf(s1, "a", 3, 3));
1509 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1510 public void TestLastIndexOf4 ()
1512 short [] a = new short [] { 19, 238, 317, 6, 565, 0, -52, 60, -563, 753, 238, 238};
1513 Array.LastIndexOf (a, 16, -1);
1517 public void TestLastIndexOf5 ()
1519 char [] a = new char [] {'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'j', 'i', 'h'};
1524 for (int i = a.Length - 1; i >= 0 ; i--) {
1526 retval = Array.LastIndexOf(a, a [i], i, i + 1);
1534 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1535 public void LastIndexOf_StartIndexOverflow ()
1537 // legal - no exception
1538 byte[] array = new byte [16];
1539 Array.LastIndexOf (array, this, Int32.MaxValue, 1);
1543 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1544 public void LastIndexOf_CountOverflow ()
1546 // legal - no exception
1547 byte[] array = new byte [16];
1548 Array.LastIndexOf (array, this, 1, Int32.MaxValue);
1552 public void TestReverse() {
1554 bool errorThrown = false;
1556 Array.Reverse(null);
1557 } catch (ArgumentNullException) {
1560 Assert("#L01", errorThrown);
1563 bool errorThrown = false;
1565 char[,] c = new Char[2,2];
1567 } catch (RankException) {
1570 Assert("#L02", errorThrown);
1573 char[] c1 = {'a', 'b', 'c', 'd'};
1575 AssertEquals("#L03", 'd', c1[0]);
1576 AssertEquals("#L04", 'c', c1[1]);
1577 AssertEquals("#L05", 'b', c1[2]);
1578 AssertEquals("#L06", 'a', c1[3]);
1581 bool errorThrown = false;
1583 Array.Reverse(null, 0, 0);
1584 } catch (ArgumentNullException) {
1587 Assert("#L07", errorThrown);
1590 bool errorThrown = false;
1592 char[,] c = new Char[2,2];
1593 Array.Reverse(c, 0, 0);
1594 } catch (RankException) {
1597 Assert("#L08", errorThrown);
1600 //bool errorThrown = false;
1602 // char[] c = new Char[2];
1603 // Array.Reverse(c, 0, 3);
1604 //} catch (ArgumentOutOfRangeException) {
1605 // errorThrown = true;
1607 //Assert("#L09", errorThrown);
1610 //bool errorThrown = false;
1612 // char[] c = new Char[2];
1613 // Array.Reverse(c, 3, 0);
1614 //} catch (ArgumentOutOfRangeException) {
1615 // errorThrown = true;
1617 //Assert("#L10", errorThrown);
1620 char[] c2 = { 'a', 'b', 'c', 'd'};
1621 Array.Reverse(c2, 1, 2);
1622 AssertEquals("#L11", 'a', c2[0]);
1623 AssertEquals("#L12", 'c', c2[1]);
1624 AssertEquals("#L13", 'b', c2[2]);
1625 AssertEquals("#L14", 'd', c2[3]);
1629 public void TestSetValue1() {
1631 bool errorThrown = false;
1633 char[,] c = new Char[2,2];
1634 c.SetValue("buh", 1);
1635 } catch (ArgumentException) {
1638 Assert("#M01", errorThrown);
1641 bool errorThrown = false;
1643 char[] c = {'a', 'b', 'c'};
1644 c.SetValue("buh", -1);
1645 } catch (IndexOutOfRangeException) {
1648 Assert("#M02", errorThrown);
1651 bool errorThrown = false;
1653 char[] c = {'a', 'b', 'c'};
1654 c.SetValue("buh", 4);
1655 } catch (IndexOutOfRangeException) {
1658 Assert("#M03", errorThrown);
1661 char[] c1 = {'a', 'b', 'c', 'd'};
1662 char[] c2 = new char[4];
1663 for (int i = 0; i < c1.Length; i++) {
1664 c2.SetValue(c1[i], i);
1666 for (int i = 0; i < c1.Length; i++) {
1667 AssertEquals("#M04(" + i + ")", c1[i], c2[i]);
1670 int[] c3 = { 1, 2, 3 };
1671 long[] c4 = new long [3];
1673 for (int i = 0; i < c3.Length; i++)
1674 c4.SetValue (c3 [i], i);
1678 } catch (Exception e) {
1679 Fail ("c3.CopyTo(): e=" + e);
1681 for (int i = 0; i < c3.Length; i++)
1682 Assert ("#M05(" + i + ")", c3[i] == c4[i]);
1684 Object[] c5 = new Object [3];
1685 long[] c6 = new long [3];
1689 } catch (Exception e) {
1690 Fail ("c4.CopyTo(): e=" + e);
1695 } catch (Exception e) {
1696 Fail ("c5.CopyTo(): e=" + e);
1698 // for (int i = 0; i < c5.Length; i++)
1699 // Assert ("#M06(" + i + ")", c5[i] == c6[i]);
1703 public void TestSetValue2() {
1705 bool errorThrown = false;
1707 char[] c = new Char[2];
1708 c.SetValue("buh", 1,1);
1709 } catch (ArgumentException) {
1712 Assert("#M21", errorThrown);
1715 bool errorThrown = false;
1717 char[,] c = new Char[2,2];
1718 c.SetValue("buh", -1, 1);
1719 } catch (IndexOutOfRangeException) {
1722 Assert("#M22", errorThrown);
1725 bool errorThrown = false;
1727 char[,] c = new Char[2,2];
1728 c.SetValue("buh", 4,1);
1729 } catch (IndexOutOfRangeException) {
1732 Assert("#M23", errorThrown);
1735 char[,] c1 = new Char[4,6];
1736 char[,] c2 = new Char[4,6];
1737 for (int i = 0; i < 24; i++) {
1740 c1[first,second] = (char)(((int)'a')+i);
1741 c2.SetValue(c1[first,second], first, second);
1743 for (int i = 0; i < c1.GetLength(0); i++) {
1744 for (int j = 0; j < c1.GetLength(1); j++) {
1745 AssertEquals("#M24(" + i + "," + j + ")",
1752 public void TestSetValue3() {
1754 bool errorThrown = false;
1756 char[] c = new Char[2];
1757 c.SetValue("buh", 1,1,1);
1758 } catch (ArgumentException) {
1761 Assert("#M41", errorThrown);
1764 bool errorThrown = false;
1766 char[,,] c = new Char[2,2,2];
1767 c.SetValue("buh", -1, 1, 1);
1768 } catch (IndexOutOfRangeException) {
1771 Assert("#M42", errorThrown);
1774 bool errorThrown = false;
1776 char[,,] c = new Char[2,2,2];
1777 c.SetValue("buh", 4,1,1);
1778 } catch (IndexOutOfRangeException) {
1781 Assert("#M43", errorThrown);
1784 char[,,] c1 = new Char[4,2,3];
1785 char[,,] c2 = new Char[4,2,3];
1786 for (int i = 0; i < 24; i++) {
1788 int remains = i % 6;
1789 int second = remains / 3;
1790 int third = remains % 3;
1791 c1[first,second, third] = (char)(((int)'a')+i);
1792 c2.SetValue(c1[first, second, third], first, second, third);
1794 for (int i = 0; i < c1.GetLength(0); i++) {
1795 for (int j = 0; j < c1.GetLength(1); j++) {
1796 for (int k = 0; k < c1.GetLength(2); k++) {
1797 AssertEquals("#M44(" + i + "," + j + " )",
1798 c1[i,j,k], c2[i,j,k]);
1806 [ExpectedException (typeof (ArgumentNullException))]
1808 [ExpectedException (typeof (NullReferenceException))]
1810 public void TestSetValueLongArray ()
1812 char[] c = new Char[2];
1813 c.SetValue("buh", (long [])null);
1817 public void TestSetValueN() {
1819 bool errorThrown = false;
1821 char[] c = new Char[2];
1822 c.SetValue("buh", (int [])null);
1823 } catch (ArgumentNullException) {
1826 Assert("#M61a", errorThrown);
1829 bool errorThrown = false;
1831 char[] c = new Char[2];
1832 int[] coords = {1, 1};
1833 c.SetValue("buh", coords);
1834 } catch (ArgumentException) {
1837 Assert("#M62", errorThrown);
1840 bool errorThrown = false;
1842 char[,] c = new Char[2,2];
1843 int[] coords = {-1, 1};
1844 c.SetValue("buh", coords);
1845 } catch (IndexOutOfRangeException) {
1848 Assert("#M63", errorThrown);
1851 bool errorThrown = false;
1853 char[,] c = new Char[2,2];
1854 int[] coords = {4, 1};
1855 c.SetValue("buh", coords);
1856 } catch (IndexOutOfRangeException) {
1859 Assert("#M64", errorThrown);
1862 char[,] c1 = new Char[4,6];
1863 char[,] c2 = new Char[4,6];
1864 for (int i = 0; i < 24; i++) {
1867 c1[first,second] = (char)(((int)'a')+i);
1868 int[] coords = {first, second};
1869 c2.SetValue(c1[first,second], coords);
1871 for (int i = 0; i < c1.GetLength(0); i++) {
1872 for (int j = 0; j < c1.GetLength(1); j++) {
1873 AssertEquals("#M65(" + i + "," + j + ")",
1880 public void TestSetValue4() {
1882 int[] c1 = { 1, 2, 3 };
1883 long[] c2 = new long [3];
1885 for (int i = 0; i < c1.Length; i++)
1886 c2.SetValue (c1 [i], i);
1888 for (int i = 0; i < c1.Length; i++) {
1889 Assert ("#M81(" + i + ")", c1[i] == c2[i]);
1890 AssertEquals ("#M82(" + i + ")", typeof (long), c2[i].GetType ());
1894 long[] c1 = { 1, 2, 3 };
1895 int[] c2 = new int [3];
1896 bool errorThrown = false;
1898 c2.SetValue (c1 [0], 0);
1899 } catch (ArgumentException) {
1902 Assert("#M83", errorThrown);
1905 int[] c1 = { 1, 2, 3 };
1906 Object[] c2 = new Object [3];
1908 for (int i = 0; i < c1.Length; i++)
1909 c2.SetValue (c1 [i], i);
1911 for (int i = 0; i < c1.Length; i++)
1912 AssertEquals ("#M84(" + i + ")", c1[i], Convert.ToInt32 (c2[i]));
1915 Object[] c1 = new Object [3];
1916 Object[] c2 = new Object [3];
1917 c1[0] = new Object ();
1919 for (int i = 0; i < c1.Length; i++)
1920 c2.SetValue (c1 [i], i);
1922 for (int i = 0; i < c1.Length; i++)
1923 AssertEquals ("#M85(" + i + ")", c1[i], c2[i]);
1926 Object[] c1 = new Object [3];
1927 string[] c2 = new String [3];
1928 string test = "hello";
1931 c2.SetValue (c1 [0], 0);
1932 AssertEquals ("#M86", c1[0], c2[0]);
1933 AssertEquals ("#M87", "hello", c2[0]);
1936 char[] c1 = { 'a', 'b', 'c' };
1937 string[] c2 = new string [3];
1939 c2.SetValue (c1 [0], 0);
1941 } catch (InvalidCastException) {}
1944 Single[] c1 = { 1.2F, 2.3F, 3.4F, 4.5F };
1945 long[] c2 = new long [3];
1947 c2.SetValue (c1 [0], 0);
1949 } catch (ArgumentException) {}
1977 UInt32 v11 = 235354;
1978 UInt64 v12 = 234552;
1980 Object[] va1 = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12 };
1981 Object[] va2 = { "true", "1", "a", "-1.2", "-32", "-234", "-34523", "-1",
1982 "-4.8F", "24234", "235354", "234552" };
1984 Object[][] vt = { va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1 };
1987 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1988 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
1989 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
1990 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1991 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1,
1992 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1,
1993 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,
1994 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1,
1995 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1,
1996 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
1997 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0,
1998 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0
2003 for (int i = 0; i < types.Length; i++) {
2004 for (int j = 0; j < types.Length; j++) {
2005 Array array = Array.CreateInstance (types [j], 2);
2007 Object value = vt[j][i];
2009 bool errorThrown = false;
2011 array.SetValue (value, 0);
2012 } catch (ArgumentException) {
2016 int ex_index = (i * types.Length) + j;
2018 AssertEquals ("#M90(" + types [i] + "," + types [j] + ")",
2019 errorThrown, arg_ex [ex_index] == 1);
2023 for (int i = 0; i < types.Length; i++) {
2024 String[] array = new String [2];
2026 Object value = va1 [i];
2028 bool errorThrown = false;
2030 array.SetValue (value, 0);
2031 } catch (InvalidCastException) {
2035 Assert ("#M91(" + types [i] + ")", errorThrown);
2038 for (int i = 0; i < types.Length; i++) {
2039 Array array = Array.CreateInstance (types [i], 2);
2041 Object value = va2 [i];
2043 bool errorThrown = false;
2045 array.SetValue (value, 0);
2046 } catch (InvalidCastException) {
2050 Assert ("#M92(" + types [i] + ")", errorThrown);
2053 for (int i = 0; i < types.Length; i++) {
2054 Array array = Array.CreateInstance (types [i], 2);
2056 Object value = null;
2058 bool errorThrown = false;
2060 array.SetValue (value, 0);
2061 } catch (InvalidCastException) {
2065 Assert ("#M93(" + types [i] + ")", !errorThrown);
2070 for (int i = 0; i < types.Length; i++) {
2071 for (int j = 0; j < types.Length; j++) {
2072 Array source = Array.CreateInstance (types [i], 2);
2073 Array array = Array.CreateInstance (types [j], 2);
2075 source.SetValue (vt[j][i], 0);
2076 source.SetValue (vt[j][i], 1);
2078 bool errorThrown = false;
2080 Array.Copy (source, array, 2);
2081 } catch (ArrayTypeMismatchException) {
2085 int ex_index = (i * types.Length) + j;
2087 AssertEquals ("#M94(" + types [i] + "," + types [j] + ")",
2088 errorThrown, arg_ex [ex_index] == 1);
2092 for (int i = 0; i < types.Length; i++) {
2093 Array source = Array.CreateInstance (types [i], 2);
2094 String[] array = new String [2];
2096 source.SetValue (va1 [i], 0);
2097 source.SetValue (va1 [i], 1);
2099 bool errorThrown = false;
2101 Array.Copy (source, array, 2);
2102 } catch (ArrayTypeMismatchException) {
2106 Assert ("#M95(" + types [i] + ")", errorThrown);
2109 for (int i = 0; i < types.Length; i++) {
2110 String[] source = new String [2];
2111 Array array = Array.CreateInstance (types [i], 2);
2113 source.SetValue (va2 [i], 0);
2114 source.SetValue (va2 [i], 1);
2116 bool errorThrown = false;
2118 Array.Copy (source, array, 2);
2119 } catch (ArrayTypeMismatchException) {
2123 Assert ("#M96(" + types [i] + ")", errorThrown);
2129 public void TestSort() {
2131 bool errorThrown = false;
2134 } catch (ArgumentNullException) {
2137 Assert("#N01", errorThrown);
2140 bool errorThrown = false;
2142 Array.Sort(null, 0, 1);
2143 } catch (ArgumentNullException) {
2146 Assert("#N02", errorThrown);
2149 bool errorThrown = false;
2151 char[] c1 = new Char[2];
2152 Array.Sort(null, c1);
2153 } catch (ArgumentNullException) {
2156 Assert("#N03", errorThrown);
2159 bool errorThrown = false;
2161 char[] c1 = new Char[2];
2162 Array.Sort(null, c1, 0, 1);
2163 } catch (ArgumentNullException) {
2166 Assert("#N04", errorThrown);
2170 char[] arr = {'d', 'b', 'f', 'e', 'a', 'c'};
2173 Array.Sort (null, 0, 1);
2174 Fail ("#N" + tc.ToString ());
2176 catch (ArgumentException) {}
2177 catch (Exception) { Fail ("#N" + tc.ToString ()); }
2181 Array.Sort (arr, -1, 3);
2182 Fail ("#N" + tc.ToString ());
2184 catch (ArgumentException) {}
2185 catch (Exception) { Fail ("#N" + tc.ToString ()); }
2189 Array.Sort (arr, 1, -3);
2190 Fail ("#N" + tc.ToString ());
2192 catch (ArgumentException) {}
2193 catch (Exception) { Fail ("#N" + tc.ToString ()); }
2197 Array.Sort (arr, arr.Length, arr.Length + 2);
2198 Fail ("#N" + tc.ToString ());
2200 catch (ArgumentException) {}
2201 catch (Exception) { Fail ("#N" + tc.ToString ()); }
2204 // note: null second array => just sort first array
2205 char[] starter = {'d', 'b', 'f', 'e', 'a', 'c'};
2206 int[] starter1 = {1,2,3,4,5,6};
2208 char[] c1 = (char[])starter.Clone();
2210 AssertEquals("#N21", 'a', c1[0]);
2211 AssertEquals("#N22", 'b', c1[1]);
2212 AssertEquals("#N23", 'c', c1[2]);
2213 AssertEquals("#N24", 'd', c1[3]);
2214 AssertEquals("#N25", 'e', c1[4]);
2215 AssertEquals("#N26", 'f', c1[5]);
2218 char[] c1 = (char[])starter.Clone();
2219 int[] i1 = (int[])starter1.Clone();
2221 AssertEquals("#N41", 'a', c1[0]);
2222 AssertEquals("#N42", 'b', c1[1]);
2223 AssertEquals("#N43", 'c', c1[2]);
2224 AssertEquals("#N44", 'd', c1[3]);
2225 AssertEquals("#N45", 'e', c1[4]);
2226 AssertEquals("#N46", 'f', c1[5]);
2227 AssertEquals("#N47", 5, i1[0]);
2228 AssertEquals("#N48", 2, i1[1]);
2229 AssertEquals("#N49", 6, i1[2]);
2230 AssertEquals("#N50", 1, i1[3]);
2231 AssertEquals("#N51", 4, i1[4]);
2232 AssertEquals("#N52", 3, i1[5]);
2235 char[] c1 = (char[])starter.Clone();
2236 Array.Sort(c1, 1, 4);
2237 AssertEquals("#N61", 'd', c1[0]);
2238 AssertEquals("#N62", 'a', c1[1]);
2239 AssertEquals("#N63", 'b', c1[2]);
2240 AssertEquals("#N64", 'e', c1[3]);
2241 AssertEquals("#N65", 'f', c1[4]);
2242 AssertEquals("#N66", 'c', c1[5]);
2245 char[] c1 = (char[])starter.Clone();
2246 int[] i1 = (int[])starter1.Clone();
2247 Array.Sort(c1, i1, 1, 4);
2248 AssertEquals("#N81", 'd', c1[0]);
2249 AssertEquals("#N82", 'a', c1[1]);
2250 AssertEquals("#N83", 'b', c1[2]);
2251 AssertEquals("#N84", 'e', c1[3]);
2252 AssertEquals("#N85", 'f', c1[4]);
2253 AssertEquals("#N86", 'c', c1[5]);
2254 AssertEquals("#N87", 1, i1[0]);
2255 AssertEquals("#N88", 5, i1[1]);
2256 AssertEquals("#N89", 2, i1[2]);
2257 AssertEquals("#N90", 4, i1[3]);
2258 AssertEquals("#N91", 3, i1[4]);
2259 AssertEquals("#N92", 6, i1[5]);
2264 public void TestInitializeEmpty()
2276 Assert("#TI01",!catched);
2280 public void TestInitializeInt()
2285 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2287 AssertEquals("#TI02 " + i ,a[i],b[i]);
2292 public void TestInitializeDouble()
2294 double[] a = {1.0,2.0,0.0};
2296 double[] b = {1.0,2.0,0.0};
2297 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2299 AssertEquals("#TI03 " + i ,a[i],b[i]);
2304 public void TestInitializeFloat()
2306 float[] a = {1.0F,2.0F,0.0F};
2308 float[] b = {1.0F,2.0F,0.0F};
2309 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2311 AssertEquals("#TI04 " + i ,a[i],b[i]);
2316 public void TestInitializeChar()
2318 char[] a = {'1','.','0','F','2','.','0','F'};
2320 char[] b = {'1','.','0','F','2','.','0','F'};
2321 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2323 AssertEquals("#TI05 " + i ,a[i],b[i]);
2328 public void TestInitializeString()
2330 string[] a = {"hola","adios","menos","mas"};
2332 string[] b = {"hola","adios","menos","mas"};
2333 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2335 AssertEquals("#TI06 " + i ,a[i],b[i]);
2340 public void TestInitializeEnum()
2342 enua[] a = {enua.hola,enua.adios,enua.menos,enua.mas};
2344 enua[] b = {enua.hola,enua.adios,enua.menos,enua.mas};
2345 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2347 AssertEquals("#TI07 " + i ,a[i],b[i]);
2352 public void TestInitializeIntNI()
2354 int[] a = new int[20];
2358 AssertEquals("#TI08",b,0);
2363 public void TestInitializeCharNI()
2365 char[] a = new char[20];
2367 foreach(char b in a)
2369 AssertEquals("#TI09",b,0);
2374 public void TestInitializeDoubleNI()
2376 double[] a = new double[20];
2378 foreach(double b in a)
2380 AssertEquals("#TI09",b,0.0);
2385 public void TestInitializeStringNI()
2387 string[] a = new string[20];
2389 foreach(string b in a)
2391 AssertEquals("#TI10",b,null);
2396 public void TestInitializeObjectNI()
2398 object[] a = new object[20];
2400 foreach(object b in a)
2402 AssertEquals("#TI11",b,null);
2407 public void TestInitializeAClassNI()
2409 AClass[] a = new AClass[20];
2411 foreach(AClass b in a)
2413 AssertEquals("#TI12",b,null);
2419 public void TestInitializeAStructNI()
2421 AStruct[] a = new AStruct[20];
2423 foreach(AStruct b in a)
2425 AssertEquals("#TI14",b,new AStruct());
2430 public void TestInitializeAStruct()
2432 AStruct[] a = new AStruct[3];
2436 AStruct[] b = new AStruct[3];
2439 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2441 AssertEquals("#TI15 " + i ,a[i],b[i]);
2446 public void TestInitializeDateTimeNI()
2448 DateTime[] a = new DateTime[20];
2450 foreach(DateTime b in a)
2452 AssertEquals("#TI16",b,new DateTime());
2457 [ExpectedException (typeof (ArgumentNullException))]
2458 public void MoreSort1 ()
2460 Array.Sort (null, 0, 1);
2464 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2465 public void MoreSort2 ()
2467 Array.Sort (arrsort, -1, 3);
2471 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2472 public void MoreSort3 ()
2474 Array.Sort (arrsort, 1, -3);
2478 [ExpectedException (typeof (ArgumentException))]
2479 public void MoreSort4 ()
2481 Array.Sort (arrsort, arrsort.Length, arrsort.Length + 2);
2485 [ExpectedException (typeof (RankException))]
2486 public void MoreSort5 ()
2488 char [,] arr = new char [,] {{'a'}, {'b'}};
2489 Array.Sort (arr, 0, 1);
2493 public void MoreSort6 ()
2495 Array.Sort (arrsort, 0, 0);
2499 [ExpectedException (typeof (ArgumentException))]
2500 public void MoreSort7 ()
2502 Array.Sort (arrsort, arrsort.Length - 1, 2);
2506 [ExpectedException (typeof (ArgumentException))]
2507 public void MoreSort8 ()
2509 Array.Sort (arrsort, 0, arrsort.Length + 1);
2513 public void MoreSort9 ()
2515 Array.Sort (arrsort, null, 0, arrsort.Length, null);
2519 [ExpectedException (typeof (InvalidOperationException))]
2520 public void MoreSort10 ()
2522 object [] array = {true, 'k', SByte.MinValue, Byte.MinValue, (short) 2, 634, (long) 436, (float) 1.1, 1.23, "Hello World"};
2523 Array.Sort (array, (IComparer) null);
2527 public void ClearJaggedArray ()
2529 byte[][] matrix = new byte [8][];
2530 for (int i=0; i < 8; i++) {
2531 matrix [i] = new byte [8];
2532 for (int j=0; j < 8; j++) {
2536 Array.Clear (matrix, 0, 8);
2537 for (int i=0; i < 8; i++) {
2538 AssertNull (i.ToString (), matrix [i]);
2543 public void ClearMultidimentionalArray ()
2545 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2546 Array.Clear (matrix, 0, 2);
2547 AssertEquals ("0,0", 0, matrix [0,0]);
2548 AssertEquals ("0,1", 0, matrix [0,1]);
2549 AssertEquals ("1,0", 2, matrix [1,0]);
2550 AssertEquals ("1,1", 2, matrix [1,1]);
2554 [ExpectedException (typeof (IndexOutOfRangeException))]
2555 public void ClearOutsideMultidimentionalArray ()
2557 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2558 Array.Clear (matrix, 0, 5);
2562 [ExpectedException (typeof (IndexOutOfRangeException))]
2563 public void Clear_IndexOverflow ()
2565 byte[] array = new byte [16];
2566 Array.Clear (array, 4, Int32.MaxValue);
2570 [ExpectedException (typeof (IndexOutOfRangeException))]
2571 public void Clear_LengthOverflow ()
2573 byte[] array = new byte [16];
2574 Array.Clear (array, Int32.MaxValue, 4);
2578 [ExpectedException (typeof (ArgumentException))]
2579 public void Copy_SourceIndexOverflow ()
2581 byte[] array = new byte [16];
2582 Array.Copy (array, Int32.MaxValue, array, 8, 8);
2586 [ExpectedException (typeof (ArgumentException))]
2587 public void Copy_DestinationIndexOverflow ()
2589 byte[] array = new byte [16];
2590 Array.Copy (array, 8, array, Int32.MaxValue, 8);
2594 [ExpectedException (typeof (ArgumentException))]
2595 public void Copy_LengthOverflow ()
2597 byte[] array = new byte [16];
2598 Array.Copy (array, 8, array, 8, Int32.MaxValue);
2602 [ExpectedException (typeof (ArgumentException))]
2603 public void Reverse_IndexOverflow ()
2605 byte[] array = new byte [16];
2606 Array.Reverse (array, Int32.MaxValue, 8);
2610 [ExpectedException (typeof (ArgumentException))]
2611 public void Reverse_LengthOverflow ()
2613 byte[] array = new byte [16];
2614 Array.Reverse (array, 8, Int32.MaxValue);
2617 public struct CharX : IComparable {
2620 public CharX (char c)
2625 public int CompareTo (object obj)
2628 return c.CompareTo (((CharX) obj).c);
2630 return c.CompareTo (obj);
2635 public void BinarySearch_ArgPassingOrder ()
2638 // This tests that arguments are passed to the comprer in the correct
2639 // order. The IComparable of the *array* elements must get called, not
2640 // that of the search object.
2642 CharX [] x = { new CharX ('a'), new CharX ('b'), new CharX ('c') };
2643 AssertEquals (1, Array.BinarySearch (x, 'b'));
2646 class Comparer: IComparer {
2648 private bool called = false;
2650 public bool Called {
2652 bool result = called;
2658 public int Compare (object x, object y)
2666 public void BinarySearch1_EmptyList ()
2668 int[] array = new int[0];
2669 AssertEquals ("BinarySearch", - 1, Array.BinarySearch (array, 0));
2673 public void BinarySearch2_EmptyList ()
2675 int[] array = new int[0];
2676 AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, 0, 0));
2680 public void BinarySearch3_EmptyList ()
2682 Comparer comparer = new Comparer ();
2683 int[] array = new int[0];
2684 AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, comparer));
2685 // bug 77030 - the comparer isn't called for an empty array/list
2686 Assert ("Called", !comparer.Called);
2690 public void BinarySearch4_EmptyList ()
2692 Comparer comparer = new Comparer ();
2693 int[] array = new int[0];
2694 AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, 0, comparer));
2695 // bug 77030 - the comparer isn't called for an empty array/list
2696 Assert ("Called", !comparer.Called);
2701 [ExpectedException (typeof (ArgumentNullException))]
2702 public void AsReadOnly_NullArray ()
2704 Array.AsReadOnly <int> (null);
2708 public void ReadOnly_Count ()
2710 AssertEquals (10, Array.AsReadOnly (new int [10]).Count);
2714 public void ReadOnly_Contains ()
2716 int[] arr = new int [2];
2719 IList<int> a = Array.AsReadOnly (arr);
2721 Assert (a.Contains (3));
2722 Assert (!a.Contains (6));
2726 public void ReadOnly_IndexOf ()
2728 int[] arr = new int [2];
2731 IList<int> a = Array.AsReadOnly (arr);
2733 AssertEquals (0, a.IndexOf (3));
2734 AssertEquals (1, a.IndexOf (5));
2735 AssertEquals (-1, a.IndexOf (6));
2739 public void ReadOnly_Indexer ()
2741 int[] arr = new int [2];
2744 IList<int> a = Array.AsReadOnly (arr);
2746 AssertEquals (3, a [0]);
2747 AssertEquals (5, a [1]);
2749 /* Check that modifications to the original array are visible */
2751 AssertEquals (6, a [0]);
2755 public void ReadOnly_Enumerator ()
2757 int[] arr = new int [10];
2759 for (int i = 0; i < 10; ++i)
2763 foreach (int i in Array.AsReadOnly (arr))
2766 AssertEquals (45, sum);
2770 public void Resize_null ()
2773 Array.Resize (ref arr, 10);
2774 AssertEquals (arr.Length, 10);
2778 public void Test_ContainsAndIndexOf_EquatableItem ()
2780 EquatableClass[] list = new EquatableClass[] {new EquatableClass (0), new EquatableClass (1), new EquatableClass (0)};
2782 AssertEquals ("#0", 0, Array.IndexOf<EquatableClass> (list, list[0]));
2783 AssertEquals ("#1", 0, Array.IndexOf<EquatableClass> (list, new EquatableClass (0)));
2784 AssertEquals ("#2", 2, Array.LastIndexOf<EquatableClass> (list, list[0]));
2785 AssertEquals ("#3", 2, Array.LastIndexOf<EquatableClass> (list, new EquatableClass (0)));
2788 public class EquatableClass : IEquatable<EquatableClass>
2791 public EquatableClass (int x)
2796 public bool Equals (EquatableClass other)
2798 return this._x == other._x;
2803 #if !TARGET_JVM // BugBUG: T[] is not yet ICollection<T> under TARGET_JVM
2806 public void ICollectionNull ()
2808 ICollection<object> test;
2810 test = new List<object>();
2811 AssertEquals ("list<o>", test.Contains (null), false);
2813 test = new object[] {};
2814 AssertEquals ("empty array", test.Contains (null), false);
2816 test = new object[] {null};
2817 AssertEquals ("array with null", test.Contains (null), true);
2819 test = new List<object>(test);
2820 AssertEquals ("List<object> with test", test.Contains (null), true);
2822 test = new object[] {new object()};
2823 AssertEquals ("array with object", test.Contains (null), false);
2825 test = new List<object>(test);
2826 AssertEquals ("array with test", test.Contains (null), false);
2828 #endif // TARGET_JVM
2833 enum ByteEnum : byte {}
2834 enum IntEnum : int {}
2837 public void TestByteEnumArrayToByteArray ()
2839 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
2840 byte[] b = new byte[a.Length];
2845 public void TestByteEnumArrayToIntArray ()
2847 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
2848 int[] b = new int[a.Length];
2853 [ExpectedException (typeof (ArrayTypeMismatchException))]
2854 public void TestIntEnumArrayToByteArray ()
2856 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
2857 byte[] b = new byte[a.Length];
2862 public void TestIntEnumArrayToIntArray ()
2864 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
2865 int[] b = new int[a.Length];