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
22 enum enua {hola,adios,mas,menos};
45 //End Auxiliary Things
48 public class ArrayTest : Assertion
50 char [] arrsort = {'d', 'b', 'f', 'e', 'a', 'c'};
55 public void TestIsFixedSize() {
57 Assert("All arrays are fixed", a1.IsFixedSize);
61 public void TestIsReadOnly() {
63 Assert("No array is readonly", !a1.IsReadOnly);
67 public void TestIsSynchronized() {
69 Assert("No array is synchronized", !a1.IsSynchronized);
73 public void TestLength() {
76 AssertEquals("Zero length array", 0, a1.Length);
80 AssertEquals("One-length array", 1, a1.Length);
83 char[] a1 = {'c', 'c'};
84 AssertEquals("Two-length array", 2, a1.Length);
89 public void TestRank() {
90 char[] a1 = { 'c', 'd', 'e' };
91 AssertEquals("Rank one", 1, a1.Rank);
93 char[,] a2 = new Char[3,3];
94 AssertEquals("Rank two", 2, a2.Rank);
96 char[,,] a3 = new Char[3,3,3];
97 AssertEquals("Rank three", 3, a3.Rank);
101 public void TestBinarySearch1() {
102 bool errorThrown = false;
104 Array.BinarySearch(null, "blue");
105 } catch (ArgumentNullException) {
108 Assert("#B01", errorThrown);
111 char[,] c1 = new Char[2,2];
112 Array.BinarySearch(c1, "needle");
113 } catch (RankException) {
116 Assert("#B02", errorThrown);
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);
124 char[] arr = {'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
125 AssertEquals("#B07", -4, Array.BinarySearch(arr, 'c'));
128 char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
129 AssertEquals("#B08", -9, Array.BinarySearch(arr, 'e'));
134 public void TestBinarySearch2() {
135 bool errorThrown = false;
137 Array.BinarySearch(null, 0, 1, "blue");
138 } catch (ArgumentNullException) {
141 Assert("#B20", errorThrown);
144 char[,] c1 = new Char[2,2];
145 Array.BinarySearch(c1, 0, 1, "needle");
146 } catch (RankException) {
149 Assert("#B21", errorThrown);
153 Array.BinarySearch(c1, -1, 1, 'a');
154 } catch (ArgumentOutOfRangeException) {
157 Assert("#B22", errorThrown);
161 Array.BinarySearch(c1, 0, -1, 'a');
162 } catch (ArgumentOutOfRangeException) {
165 Assert("#B23", errorThrown);
169 Array.BinarySearch(c1, 0, 4, 'a');
170 } catch (ArgumentException) {
173 Assert("#B24", errorThrown);
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);
181 char[] arr = {'z', 'z', 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
182 AssertEquals("#B28", -6, Array.BinarySearch(arr, 2, 8, 'c'));
185 char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
186 AssertEquals("#B29", -11, Array.BinarySearch(arr, 2, 8, 'e'));
190 public void TestBinarySearch3()
192 int[] array = new int[100];
194 for (int i = 0; i < 100; i++)
197 AssertEquals("#B30", 49, Array.BinarySearch(array, 10));
201 public void BinarySearch_NullValue ()
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));
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));
216 // TODO - testBinarySearch with explicit IComparer args
219 public void TestClear() {
220 bool errorThrown = false;
222 Array.Clear(null, 0, 1);
223 } catch (ArgumentNullException) {
226 Assert("#C01", errorThrown);
228 int[] i1 = { 1, 2, 3, 4 };
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]);
236 Array.Clear(i1, 3, 1);
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]);
244 Array.Clear(i1, 1, 1);
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]);
252 Array.Clear(i1, 1, 3);
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]);
261 string[] s1 = { "red", "green", "blue" };
262 Array.Clear(s1, 0, 3);
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]);
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]);
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]);
289 AssertEquals("#D07", d1[0], d2[0]);
293 public void TestCopy() {
295 bool errorThrown = false;
298 Array.Copy(c1, null, 1);
299 } catch (ArgumentNullException) {
302 Assert("#E01", errorThrown);
305 bool errorThrown = false;
308 Array.Copy(null, c1, 1);
309 } catch (ArgumentNullException) {
312 Assert("#E02", errorThrown);
315 bool errorThrown = false;
317 Char[] c1 = new Char[1];
318 Char[,] c2 = new Char[1,1];
319 Array.Copy(c1, c2, 1);
320 } catch (RankException) {
323 Assert("#E03", errorThrown);
326 bool errorThrown = false;
328 Char[] c1 = new Char[1];
329 string[] s1 = new String[1];
330 Array.Copy(c1, s1, 1);
331 } catch (ArrayTypeMismatchException) {
334 Assert("#E04", errorThrown);
337 bool errorThrown = false;
339 Char[] c1 = new Char[1];
340 Object[] o1 = new Object[1];
342 Array.Copy(o1, c1, 1);
343 } catch (InvalidCastException) {
346 Assert("#E05", errorThrown);
349 bool errorThrown = false;
351 Char[] c1 = new Char[1];
352 Char[] c2 = new Char[1];
353 Array.Copy(c1, c2, -1);
354 } catch (ArgumentOutOfRangeException) {
357 Assert("#E06", errorThrown);
360 bool errorThrown = false;
362 Char[] c1 = new Char[1];
363 Char[] c2 = new Char[2];
364 Array.Copy(c1, c2, 2);
365 } catch (ArgumentException) {
368 Assert("#E07", errorThrown);
371 bool errorThrown = false;
373 Char[] c1 = new Char[1];
374 Char[] c2 = new Char[2];
375 Array.Copy(c2, c1, 2);
376 } catch (ArgumentException) {
379 Assert("#E08", errorThrown);
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]);
388 Array.Clear(copy, 0, copy.Length);
389 for (int i = 0; i < orig.Length; i++) {
390 AssertEquals("#E10(" + i + ")", (char)0, copy[i]);
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]);
400 public void TestCopy2() {
402 bool errorThrown = false;
404 Char[] c1 = new Char[2];
405 Char[] c2 = new Char[2];
406 Array.Copy(c2, 1, c1, 0, 2);
407 } catch (ArgumentException) {
410 Assert("#E31", errorThrown);
413 bool errorThrown = false;
415 Char[] c1 = new Char[2];
416 Char[] c2 = new Char[2];
417 Array.Copy(c2, 0, c1, 1, 2);
418 } catch (ArgumentException) {
421 Assert("#E32", errorThrown);
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]);
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]);
440 [ExpectedException (typeof (InvalidCastException))]
441 public void Copy_InvalidCast () {
442 object[] arr1 = new object [10];
443 Type[] arr2 = new Type [10];
445 arr1 [0] = new object ();
447 Array.Copy (arr1, 0, arr2, 0, 10);
451 public void TestCopyTo() {
453 bool errorThrown = false;
455 Char[] c1 = new Char[2];
457 } catch (ArgumentNullException) {
460 Assert("#E61", errorThrown);
463 bool errorThrown = false;
465 Char[] c1 = new Char[2];
466 Char[,] c2 = new Char[2,2];
468 } catch (ArgumentException) {
471 #if TARGET_JVM // This is really implementation dependent behaviour.
472 catch (RankException) {
476 Assert("#E62", errorThrown);
479 bool errorThrown = false;
481 Char[,] c1 = new Char[2,2];
482 Char[] c2 = new Char[2];
484 } catch (RankException) {
487 Assert("#E63", errorThrown);
490 bool errorThrown = false;
492 Char[,] c1 = new Char[2,2];
493 Char[] c2 = new Char[2];
495 } catch (RankException) {
498 Assert("#E64", errorThrown);
501 bool errorThrown = false;
503 Char[] c1 = new Char[2];
504 Char[] c2 = new Char[2];
506 } catch (ArgumentOutOfRangeException) {
509 Assert("#E65", errorThrown);
512 bool errorThrown = false;
514 Char[] c1 = new Char[2];
515 Char[] c2 = new Char[2];
517 } catch (ArgumentException) {
520 Assert("#E66", errorThrown);
523 bool errorThrown = false;
525 Char[] c1 = new Char[2];
526 Char[] c2 = new Char[2];
528 } catch (ArgumentException) {
531 Assert("#E67", errorThrown);
535 bool errorThrown = false;
537 String[] c1 = new String[2];
538 // TODO: this crashes mono if there are null
539 // values in the array.
542 Char[] c2 = new Char[2];
546 } catch (ArrayTypeMismatchException) {
549 Assert("#E68", errorThrown);
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]);
568 // The following is valid and must not throw an exception.
569 bool errorThrown = false;
571 int[] src = new int [0];
572 int[] dest = new int [0];
573 src.CopyTo (dest, 0);
574 } catch (ArgumentException) {
577 Assert("#E79", !errorThrown);
582 bool errorThrown = false;
584 CClass[] src = new CClass [] { new CClass () };
585 BClass[] dest = new BClass [1];
587 src.CopyTo (dest, 0);
589 } catch (ArrayTypeMismatchException) {
592 Assert("#E80", errorThrown);
597 public void TestCreateInstance() {
599 bool errorThrown = false;
601 Array.CreateInstance(null, 12);
602 } catch (ArgumentNullException) {
605 Assert("#F01", errorThrown);
608 bool errorThrown = false;
610 Array.CreateInstance(Type.GetType("System.Char"), -3);
611 } catch (ArgumentOutOfRangeException) {
614 Assert("#F02", errorThrown);
617 bool errorThrown = false;
619 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
620 } catch (ArgumentNullException) {
623 Assert("#F03a", errorThrown);
627 bool errorThrown = false;
629 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
630 } catch (ArgumentNullException) {
633 Assert("#F03b", errorThrown);
636 #if !TARGET_JVM // Arrays lower bounds are not supported for TARGET_JVM
638 bool errorThrown = false;
640 Array.CreateInstance(Type.GetType("System.Char"), null, null);
641 } catch (ArgumentNullException) {
644 Assert("#F04", errorThrown);
648 bool errorThrown = false;
650 int[] lengths = new int [0];
651 Array.CreateInstance(Type.GetType("System.Char"), lengths);
652 } catch (ArgumentException) {
655 Assert("#F05", errorThrown);
657 #if !TARGET_JVM // CreateInstance with lower bounds not supported for TARGET_JVM
659 bool errorThrown = false;
661 int[] lengths = new int [1];
662 int[] bounds = new int [2];
663 Array.CreateInstance(Type.GetType("System.Char"), lengths, bounds);
665 } catch (ArgumentException) {
668 Assert("#F06", errorThrown);
671 char[] c1 = (char[])Array.CreateInstance(Type.GetType("System.Char"), 12);
672 AssertEquals("#F07", 12, c1.Length);
674 Array c2 = Array.CreateInstance(Type.GetType("System.Char"), 12, 5);
675 AssertEquals("#F08", 2, c2.Rank);
676 AssertEquals("#F09", 60, c2.Length);
680 int[] lengths = { 3 };
681 int[] bounds = { 5 };
682 int[] src = { 512, 718, 912 };
683 Array array = Array.CreateInstance(typeof(int), lengths, bounds);
685 AssertEquals("#F10", 3, array.Length);
686 AssertEquals("#F11", 5, array.GetLowerBound(0));
687 AssertEquals("#F12", 7, array.GetUpperBound(0));
689 src.CopyTo (array, 5);
691 for (int i = 0; i < src.Length; i++)
692 AssertEquals("#F13(" + i + ")", src[i], array.GetValue(i+5));
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 ());
704 [ExpectedException (typeof (ArgumentNullException))]
705 public void TestCreateInstance2 ()
707 Array.CreateInstance (typeof (Int32), (int[])null);
712 [ExpectedException (typeof (ArgumentNullException))]
714 [ExpectedException (typeof (NullReferenceException))]
716 public void TestCreateInstance2b ()
718 Array.CreateInstance (typeof (Int32), (long[])null);
722 public void TestGetEnumerator() {
723 String[] s1 = {"this", "is", "a", "test"};
724 IEnumerator en = s1.GetEnumerator ();
725 AssertNotNull ("#G01", en);
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 ());
738 Assert("#G11", en.MoveNext ());
739 AssertEquals ("#G12", "this", en.Current);
741 // mutation does not invalidate array enumerator!
742 s1.SetValue ("change", 1);
743 Assert ("#G13", en.MoveNext ());
744 AssertEquals ("#G14", "change", en.Current);
748 public void TestGetEnumeratorMultipleDimension() {
749 String[,] s1 = {{"this", "is"}, {"a", "test"}};
750 IEnumerator en = s1.GetEnumerator ();
751 AssertNotNull ("#AA01", en);
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 ());
764 Assert("#AA11", en.MoveNext ());
765 AssertEquals ("#AA12", "this", en.Current);
768 // mutation does not invalidate array enumerator!
769 s1.SetValue ("change", idxs);
770 Assert ("#AA13", en.MoveNext ());
771 AssertEquals ("#AA14", "change", en.Current);
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 };
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 );
786 IEnumerator en = myArray.GetEnumerator ();
787 AssertNotNull ("#AB01", en);
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);
795 // then check the last element's value
798 lastElement = (string)en.Current;
799 } while (en.MoveNext());
800 AssertEquals ("#AB06", "47", lastElement);
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 };
809 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
811 ((IList)myArray).Add ("can not");
812 Fail ("IList.Add should throw");
814 catch (NotSupportedException) {
818 Fail ("IList.Add threw wrong exception type");
821 Fail("IList.Add shouldn't get this far");
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 };
830 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
832 ((IList)myArray).Insert (0, "can not");
833 Fail ("IList.Insert should throw");
835 catch (NotSupportedException) {
839 Fail ("IList.Insert threw wrong exception type");
842 Fail("IList.Insert shouldn't get this far");
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 };
851 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
853 ((IList)myArray).Remove ("can not");
854 Fail ("IList.Remove should throw");
856 catch (NotSupportedException) {
860 Fail ("IList.Remove threw wrong exception type");
863 Fail("IList.Remove shouldn't get this far");
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 };
872 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
874 ((IList)myArray).RemoveAt (0);
875 Fail ("IList.RemoveAt should throw");
877 catch (NotSupportedException) {
881 Fail ("IList.RemoveAt threw wrong exception type");
884 Fail("IList.RemoveAt shouldn't get this far");
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 };
893 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
896 bool b = ((IList)myArray).Contains ("23");
897 Fail("IList.Contains should throw with multi-dimensional arrays");
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));
905 // and one that is definately not there
906 Assert("AC03", !((IList)iArr).Contains (42));
910 Fail("Should not get here");
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 };
919 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
922 bool b = ((IList)myArray).Contains ("23");
923 Fail("IList.Contains should throw with multi-dimensional arrays");
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));
931 // and one that is definately not there
932 AssertEquals("AD03", -1, ((IList)iArr).IndexOf (42));
934 catch (Exception e) {
935 Fail("Unexpected exception: " + e.ToString());
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));
948 public void TestGetLength() {
950 bool errorThrown = false;
952 char[] c1 = {'a', 'b', 'c'};
954 } catch (IndexOutOfRangeException) {
957 Assert("#H01", errorThrown);
960 bool errorThrown = false;
962 char[] c1 = {'a', 'b', 'c'};
964 } catch (IndexOutOfRangeException) {
967 Assert("#H02", errorThrown);
970 char[] c2 = new Char[5];
971 AssertEquals("#H03", 5, c2.GetLength(0));
973 char[,] c3 = new Char[6,7];
974 AssertEquals("#H04", 6, c3.GetLength(0));
975 AssertEquals("#H05", 7, c3.GetLength(1));
979 public void TestGetLowerBound() {
981 bool errorThrown = false;
983 char[] c = {'a', 'b', 'c'};
985 } catch (IndexOutOfRangeException) {
988 Assert("#H31", errorThrown);
991 bool errorThrown = false;
993 char[] c = {'a', 'b', 'c'};
995 } catch (IndexOutOfRangeException) {
998 Assert("#H32", errorThrown);
1001 char[] c1 = new Char[5];
1002 AssertEquals("#H33", 0, c1.GetLowerBound(0));
1004 char[,] c2 = new Char[4,4];
1005 AssertEquals("#H34", 0, c2.GetLowerBound(0));
1006 AssertEquals("#H35", 0, c2.GetLowerBound(1));
1010 public void TestGetUpperBound() {
1012 bool errorThrown = false;
1014 char[] c = {'a', 'b', 'c'};
1015 c.GetUpperBound(-1);
1016 } catch (IndexOutOfRangeException) {
1019 Assert("#H61", errorThrown);
1022 bool errorThrown = false;
1024 char[] c = {'a', 'b', 'c'};
1026 } catch (IndexOutOfRangeException) {
1029 Assert("#H62", errorThrown);
1032 char[] c1 = new Char[5];
1033 AssertEquals("#H63", 4, c1.GetUpperBound(0));
1035 char[,] c2 = new Char[4,6];
1036 AssertEquals("#H64", 3, c2.GetUpperBound(0));
1037 AssertEquals("#H65", 5, c2.GetUpperBound(1));
1041 public void TestGetValue1() {
1043 bool errorThrown = false;
1045 char[,] c = new Char[2,2];
1047 } catch (ArgumentException) {
1050 Assert("#I01", errorThrown);
1053 bool errorThrown = false;
1055 char[] c = {'a', 'b', 'c'};
1057 } catch (IndexOutOfRangeException) {
1060 Assert("#I02", errorThrown);
1063 bool errorThrown = false;
1065 char[] c = {'a', 'b', 'c'};
1067 } catch (IndexOutOfRangeException) {
1070 Assert("#I03", errorThrown);
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));
1080 public void TestGetValue2() {
1082 bool errorThrown = false;
1084 char[] c = new Char[2];
1086 } catch (ArgumentException) {
1089 Assert("#I21", errorThrown);
1092 bool errorThrown = false;
1094 char[,] c = new Char[2,2];
1096 } catch (IndexOutOfRangeException) {
1099 Assert("#I22", errorThrown);
1102 bool errorThrown = false;
1104 char[,] c = new Char[2,2];
1106 } catch (IndexOutOfRangeException) {
1109 Assert("#I23", errorThrown);
1112 char[,] c1 = new Char[4,6];
1113 for (int i = 0; i < 24; i++) {
1116 c1[first,second] = (char)(((int)'a')+i);
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));
1127 public void TestGetValue3() {
1129 bool errorThrown = false;
1131 char[] c = new Char[2];
1133 } catch (ArgumentException) {
1136 Assert("#I41", errorThrown);
1139 bool errorThrown = false;
1141 char[,,] c = new Char[2,2,2];
1142 c.GetValue(-1, 1, 1);
1143 } catch (IndexOutOfRangeException) {
1146 Assert("#I42", errorThrown);
1149 bool errorThrown = false;
1151 char[,,] c = new Char[2,2,2];
1153 } catch (IndexOutOfRangeException) {
1156 Assert("#I43", errorThrown);
1159 char[,,] c1 = new Char[4,2,3];
1160 for (int i = 0; i < 24; i++) {
1162 int remains = i % 6;
1163 int second = remains / 3;
1164 int third = remains % 3;
1165 c1[first,second, third] = (char)(((int)'a')+i);
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));
1179 [ExpectedException (typeof (ArgumentNullException))]
1181 [ExpectedException (typeof (NullReferenceException))]
1183 public void TestGetValueLongArray ()
1185 char[] c = new Char[2];
1186 c.GetValue((long [])null);
1190 public void TestGetValueN() {
1192 bool errorThrown = false;
1194 char[] c = new Char[2];
1195 c.GetValue((int [])null);
1196 } catch (ArgumentNullException) {
1199 Assert("#I61a", errorThrown);
1202 bool errorThrown = false;
1204 char[] c = new Char[2];
1205 int[] coords = {1, 1};
1207 } catch (ArgumentException) {
1210 Assert("#I62", errorThrown);
1213 bool errorThrown = false;
1215 char[,] c = new Char[2,2];
1216 int[] coords = {-1, 1};
1218 } catch (IndexOutOfRangeException) {
1221 Assert("#I63", errorThrown);
1224 bool errorThrown = false;
1226 char[,] c = new Char[2,2];
1227 int[] coords = {4, 1};
1229 } catch (IndexOutOfRangeException) {
1232 Assert("#I64", errorThrown);
1235 char[,] c1 = new Char[4,6];
1236 for (int i = 0; i < 24; i++) {
1239 c1[first,second] = (char)(((int)'a')+i);
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));
1251 public void TestIndexOf1() {
1253 bool errorThrown = false;
1255 Array.IndexOf(null, "huh?");
1256 } catch (ArgumentNullException) {
1259 Assert("#J01", errorThrown);
1262 bool errorThrown = false;
1264 char[,] c = new Char[2,2];
1265 Array.IndexOf(c, "huh?");
1266 } catch (RankException) {
1269 Assert("#J02", errorThrown);
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"));
1280 public void TestIndexOf2() {
1282 bool errorThrown = false;
1284 Array.IndexOf(null, "huh?", 0);
1285 } catch (ArgumentNullException) {
1288 Assert("#J21", errorThrown);
1291 bool errorThrown = false;
1293 char[,] c = new Char[2,2];
1294 Array.IndexOf(c, "huh?", 0);
1295 } catch (RankException) {
1298 Assert("#J22", errorThrown);
1301 bool errorThrown = false;
1303 char[] c = new Char[2];
1304 Array.IndexOf(c, "huh?", 3);
1305 } catch (ArgumentOutOfRangeException) {
1308 Assert("#J23", errorThrown);
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));
1320 public void TestIndexOf3() {
1322 bool errorThrown = false;
1324 Array.IndexOf(null, "huh?", 0, 1);
1325 } catch (ArgumentNullException) {
1328 Assert("#J41", errorThrown);
1331 bool errorThrown = false;
1333 char[,] c = new Char[2,2];
1334 Array.IndexOf(c, "huh?", 0, 1);
1335 } catch (RankException) {
1338 Assert("#J42", errorThrown);
1341 bool errorThrown = false;
1343 char[] c = new Char[2];
1344 Array.IndexOf(c, "huh?", 3, 1);
1345 } catch (ArgumentOutOfRangeException) {
1348 Assert("#J43", errorThrown);
1351 bool errorThrown = false;
1353 char[] c = new Char[2];
1354 Array.IndexOf(c, "huh?", 0, 5);
1355 } catch (ArgumentOutOfRangeException) {
1358 Assert("#J44", errorThrown);
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));
1371 public void TestLastIndexOf1() {
1373 bool errorThrown = false;
1375 Array.LastIndexOf(null, "huh?");
1376 } catch (ArgumentNullException) {
1379 Assert("#K01", errorThrown);
1382 bool errorThrown = false;
1384 char[,] c = new Char[2,2];
1385 Array.LastIndexOf(c, "huh?");
1386 } catch (RankException) {
1389 Assert("#K02", errorThrown);
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"));
1401 public void TestLastIndexOf2() {
1403 bool errorThrown = false;
1405 Array.LastIndexOf(null, "huh?", 0);
1406 } catch (ArgumentNullException) {
1409 Assert("#K21", errorThrown);
1412 bool errorThrown = false;
1414 char[,] c = new Char[2,2];
1415 Array.LastIndexOf(c, "huh?", 0);
1416 } catch (RankException) {
1419 Assert("#K22", errorThrown);
1422 bool errorThrown = false;
1424 char[] c = new Char[2];
1425 Array.LastIndexOf(c, "huh?", 3);
1426 } catch (ArgumentOutOfRangeException) {
1429 Assert("#K23", errorThrown);
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));
1441 public void TestLastIndexOf3() {
1443 bool errorThrown = false;
1445 Array.LastIndexOf(null, "huh?", 0, 1);
1446 } catch (ArgumentNullException) {
1449 Assert("#K41", errorThrown);
1452 bool errorThrown = false;
1454 char[,] c = new Char[2,2];
1455 Array.LastIndexOf(c, "huh?", 0, 1);
1456 } catch (RankException) {
1459 Assert("#K42", errorThrown);
1462 bool errorThrown = false;
1464 char[] c = new Char[2];
1465 Array.LastIndexOf(c, "huh?", 3, 1);
1466 } catch (ArgumentOutOfRangeException) {
1469 Assert("#K43", errorThrown);
1472 bool errorThrown = false;
1474 char[] c = new Char[2];
1475 Array.LastIndexOf(c, "huh?", 0, 5);
1476 } catch (ArgumentOutOfRangeException) {
1479 Assert("#K44", errorThrown);
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));
1492 public void TestLastIndexOf4 ()
1494 short [] a = new short [] { 19, 238, 317, 6, 565, 0, -52, 60, -563, 753, 238, 238};
1496 Array.LastIndexOf (a, (object)16, -1);
1497 NUnit.Framework.Assert.Fail ("#1");
1498 } catch (ArgumentOutOfRangeException) { }
1502 Array.LastIndexOf<short> (a, 16, -1);
1503 NUnit.Framework.Assert.Fail ("#2");
1504 } catch (ArgumentOutOfRangeException) { }
1509 public void TestLastIndexOf5 ()
1511 char [] a = new char [] {'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'j', 'i', 'h'};
1516 for (int i = a.Length - 1; i >= 0 ; i--) {
1518 retval = Array.LastIndexOf(a, a [i], i, i + 1);
1526 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1527 public void LastIndexOf_StartIndexOverflow ()
1529 // legal - no exception
1530 byte[] array = new byte [16];
1531 Array.LastIndexOf (array, this, Int32.MaxValue, 1);
1535 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1536 public void LastIndexOf_CountOverflow ()
1538 // legal - no exception
1539 byte[] array = new byte [16];
1540 Array.LastIndexOf (array, this, 1, Int32.MaxValue);
1544 public void TestReverse() {
1546 bool errorThrown = false;
1548 Array.Reverse(null);
1549 } catch (ArgumentNullException) {
1552 Assert("#L01", errorThrown);
1555 bool errorThrown = false;
1557 char[,] c = new Char[2,2];
1559 } catch (RankException) {
1562 Assert("#L02", errorThrown);
1565 char[] c1 = {'a', 'b', 'c', 'd'};
1567 AssertEquals("#L03", 'd', c1[0]);
1568 AssertEquals("#L04", 'c', c1[1]);
1569 AssertEquals("#L05", 'b', c1[2]);
1570 AssertEquals("#L06", 'a', c1[3]);
1573 bool errorThrown = false;
1575 Array.Reverse(null, 0, 0);
1576 } catch (ArgumentNullException) {
1579 Assert("#L07", errorThrown);
1582 bool errorThrown = false;
1584 char[,] c = new Char[2,2];
1585 Array.Reverse(c, 0, 0);
1586 } catch (RankException) {
1589 Assert("#L08", errorThrown);
1592 //bool errorThrown = false;
1594 // char[] c = new Char[2];
1595 // Array.Reverse(c, 0, 3);
1596 //} catch (ArgumentOutOfRangeException) {
1597 // errorThrown = true;
1599 //Assert("#L09", errorThrown);
1602 //bool errorThrown = false;
1604 // char[] c = new Char[2];
1605 // Array.Reverse(c, 3, 0);
1606 //} catch (ArgumentOutOfRangeException) {
1607 // errorThrown = true;
1609 //Assert("#L10", errorThrown);
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]);
1621 public void TestSetValue1() {
1623 bool errorThrown = false;
1625 char[,] c = new Char[2,2];
1626 c.SetValue("buh", 1);
1627 } catch (ArgumentException) {
1630 Assert("#M01", errorThrown);
1633 bool errorThrown = false;
1635 char[] c = {'a', 'b', 'c'};
1636 c.SetValue("buh", -1);
1637 } catch (IndexOutOfRangeException) {
1640 Assert("#M02", errorThrown);
1643 bool errorThrown = false;
1645 char[] c = {'a', 'b', 'c'};
1646 c.SetValue("buh", 4);
1647 } catch (IndexOutOfRangeException) {
1650 Assert("#M03", errorThrown);
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);
1658 for (int i = 0; i < c1.Length; i++) {
1659 AssertEquals("#M04(" + i + ")", c1[i], c2[i]);
1662 int[] c3 = { 1, 2, 3 };
1663 long[] c4 = new long [3];
1665 for (int i = 0; i < c3.Length; i++)
1666 c4.SetValue (c3 [i], i);
1670 } catch (Exception e) {
1671 Fail ("c3.CopyTo(): e=" + e);
1673 for (int i = 0; i < c3.Length; i++)
1674 Assert ("#M05(" + i + ")", c3[i] == c4[i]);
1676 Object[] c5 = new Object [3];
1677 long[] c6 = new long [3];
1681 } catch (Exception e) {
1682 Fail ("c4.CopyTo(): e=" + e);
1687 } catch (Exception e) {
1688 Fail ("c5.CopyTo(): e=" + e);
1690 // for (int i = 0; i < c5.Length; i++)
1691 // Assert ("#M06(" + i + ")", c5[i] == c6[i]);
1695 public void TestSetValue2() {
1697 bool errorThrown = false;
1699 char[] c = new Char[2];
1700 c.SetValue("buh", 1,1);
1701 } catch (ArgumentException) {
1704 Assert("#M21", errorThrown);
1707 bool errorThrown = false;
1709 char[,] c = new Char[2,2];
1710 c.SetValue("buh", -1, 1);
1711 } catch (IndexOutOfRangeException) {
1714 Assert("#M22", errorThrown);
1717 bool errorThrown = false;
1719 char[,] c = new Char[2,2];
1720 c.SetValue("buh", 4,1);
1721 } catch (IndexOutOfRangeException) {
1724 Assert("#M23", errorThrown);
1727 char[,] c1 = new Char[4,6];
1728 char[,] c2 = new Char[4,6];
1729 for (int i = 0; i < 24; i++) {
1732 c1[first,second] = (char)(((int)'a')+i);
1733 c2.SetValue(c1[first,second], first, second);
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 + ")",
1744 public void TestSetValue3() {
1746 bool errorThrown = false;
1748 char[] c = new Char[2];
1749 c.SetValue("buh", 1,1,1);
1750 } catch (ArgumentException) {
1753 Assert("#M41", errorThrown);
1756 bool errorThrown = false;
1758 char[,,] c = new Char[2,2,2];
1759 c.SetValue("buh", -1, 1, 1);
1760 } catch (IndexOutOfRangeException) {
1763 Assert("#M42", errorThrown);
1766 bool errorThrown = false;
1768 char[,,] c = new Char[2,2,2];
1769 c.SetValue("buh", 4,1,1);
1770 } catch (IndexOutOfRangeException) {
1773 Assert("#M43", errorThrown);
1776 char[,,] c1 = new Char[4,2,3];
1777 char[,,] c2 = new Char[4,2,3];
1778 for (int i = 0; i < 24; i++) {
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);
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]);
1798 [ExpectedException (typeof (ArgumentNullException))]
1800 [ExpectedException (typeof (NullReferenceException))]
1802 public void TestSetValueLongArray ()
1804 char[] c = new Char[2];
1805 c.SetValue("buh", (long [])null);
1809 public void TestSetValueN() {
1811 bool errorThrown = false;
1813 char[] c = new Char[2];
1814 c.SetValue("buh", (int [])null);
1815 } catch (ArgumentNullException) {
1818 Assert("#M61a", errorThrown);
1821 bool errorThrown = false;
1823 char[] c = new Char[2];
1824 int[] coords = {1, 1};
1825 c.SetValue("buh", coords);
1826 } catch (ArgumentException) {
1829 Assert("#M62", errorThrown);
1832 bool errorThrown = false;
1834 char[,] c = new Char[2,2];
1835 int[] coords = {-1, 1};
1836 c.SetValue("buh", coords);
1837 } catch (IndexOutOfRangeException) {
1840 Assert("#M63", errorThrown);
1843 bool errorThrown = false;
1845 char[,] c = new Char[2,2];
1846 int[] coords = {4, 1};
1847 c.SetValue("buh", coords);
1848 } catch (IndexOutOfRangeException) {
1851 Assert("#M64", errorThrown);
1854 char[,] c1 = new Char[4,6];
1855 char[,] c2 = new Char[4,6];
1856 for (int i = 0; i < 24; i++) {
1859 c1[first,second] = (char)(((int)'a')+i);
1860 int[] coords = {first, second};
1861 c2.SetValue(c1[first,second], coords);
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 + ")",
1872 public void TestSetValue4() {
1874 int[] c1 = { 1, 2, 3 };
1875 long[] c2 = new long [3];
1877 for (int i = 0; i < c1.Length; i++)
1878 c2.SetValue (c1 [i], i);
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 ());
1886 long[] c1 = { 1, 2, 3 };
1887 int[] c2 = new int [3];
1888 bool errorThrown = false;
1890 c2.SetValue (c1 [0], 0);
1891 } catch (ArgumentException) {
1894 Assert("#M83", errorThrown);
1897 int[] c1 = { 1, 2, 3 };
1898 Object[] c2 = new Object [3];
1900 for (int i = 0; i < c1.Length; i++)
1901 c2.SetValue (c1 [i], i);
1903 for (int i = 0; i < c1.Length; i++)
1904 AssertEquals ("#M84(" + i + ")", c1[i], Convert.ToInt32 (c2[i]));
1907 Object[] c1 = new Object [3];
1908 Object[] c2 = new Object [3];
1909 c1[0] = new Object ();
1911 for (int i = 0; i < c1.Length; i++)
1912 c2.SetValue (c1 [i], i);
1914 for (int i = 0; i < c1.Length; i++)
1915 AssertEquals ("#M85(" + i + ")", c1[i], c2[i]);
1918 Object[] c1 = new Object [3];
1919 string[] c2 = new String [3];
1920 string test = "hello";
1923 c2.SetValue (c1 [0], 0);
1924 AssertEquals ("#M86", c1[0], c2[0]);
1925 AssertEquals ("#M87", "hello", c2[0]);
1928 char[] c1 = { 'a', 'b', 'c' };
1929 string[] c2 = new string [3];
1931 c2.SetValue (c1 [0], 0);
1933 } catch (InvalidCastException) {}
1936 Single[] c1 = { 1.2F, 2.3F, 3.4F, 4.5F };
1937 long[] c2 = new long [3];
1939 c2.SetValue (c1 [0], 0);
1941 } catch (ArgumentException) {}
1969 UInt32 v11 = 235354;
1970 UInt64 v12 = 234552;
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" };
1976 Object[][] vt = { va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1 };
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
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);
1999 Object value = vt[j][i];
2001 bool errorThrown = false;
2003 array.SetValue (value, 0);
2004 } catch (ArgumentException) {
2008 int ex_index = (i * types.Length) + j;
2010 AssertEquals ("#M90(" + types [i] + "," + types [j] + ")",
2011 errorThrown, arg_ex [ex_index] == 1);
2015 for (int i = 0; i < types.Length; i++) {
2016 String[] array = new String [2];
2018 Object value = va1 [i];
2020 bool errorThrown = false;
2022 array.SetValue (value, 0);
2023 } catch (InvalidCastException) {
2027 Assert ("#M91(" + types [i] + ")", errorThrown);
2030 for (int i = 0; i < types.Length; i++) {
2031 Array array = Array.CreateInstance (types [i], 2);
2033 Object value = va2 [i];
2035 bool errorThrown = false;
2037 array.SetValue (value, 0);
2038 } catch (InvalidCastException) {
2042 Assert ("#M92(" + types [i] + ")", errorThrown);
2045 for (int i = 0; i < types.Length; i++) {
2046 Array array = Array.CreateInstance (types [i], 2);
2048 Object value = null;
2050 bool errorThrown = false;
2052 array.SetValue (value, 0);
2053 } catch (InvalidCastException) {
2057 Assert ("#M93(" + types [i] + ")", !errorThrown);
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);
2067 source.SetValue (vt[j][i], 0);
2068 source.SetValue (vt[j][i], 1);
2070 bool errorThrown = false;
2072 Array.Copy (source, array, 2);
2073 } catch (ArrayTypeMismatchException) {
2077 int ex_index = (i * types.Length) + j;
2079 AssertEquals ("#M94(" + types [i] + "," + types [j] + ")",
2080 errorThrown, arg_ex [ex_index] == 1);
2084 for (int i = 0; i < types.Length; i++) {
2085 Array source = Array.CreateInstance (types [i], 2);
2086 String[] array = new String [2];
2088 source.SetValue (va1 [i], 0);
2089 source.SetValue (va1 [i], 1);
2091 bool errorThrown = false;
2093 Array.Copy (source, array, 2);
2094 } catch (ArrayTypeMismatchException) {
2098 Assert ("#M95(" + types [i] + ")", errorThrown);
2101 for (int i = 0; i < types.Length; i++) {
2102 String[] source = new String [2];
2103 Array array = Array.CreateInstance (types [i], 2);
2105 source.SetValue (va2 [i], 0);
2106 source.SetValue (va2 [i], 1);
2108 bool errorThrown = false;
2110 Array.Copy (source, array, 2);
2111 } catch (ArrayTypeMismatchException) {
2115 Assert ("#M96(" + types [i] + ")", errorThrown);
2121 public void TestSort() {
2123 bool errorThrown = false;
2126 } catch (ArgumentNullException) {
2129 Assert("#N01", errorThrown);
2132 bool errorThrown = false;
2134 Array.Sort(null, 0, 1);
2135 } catch (ArgumentNullException) {
2138 Assert("#N02", errorThrown);
2141 bool errorThrown = false;
2143 char[] c1 = new Char[2];
2144 Array.Sort(null, c1);
2145 } catch (ArgumentNullException) {
2148 Assert("#N03", errorThrown);
2151 bool errorThrown = false;
2153 char[] c1 = new Char[2];
2154 Array.Sort(null, c1, 0, 1);
2155 } catch (ArgumentNullException) {
2158 Assert("#N04", errorThrown);
2162 char[] arr = {'d', 'b', 'f', 'e', 'a', 'c'};
2165 Array.Sort (null, 0, 1);
2166 Fail ("#N" + tc.ToString ());
2168 catch (ArgumentException) {}
2169 catch (Exception) { Fail ("#N" + tc.ToString ()); }
2173 Array.Sort (arr, -1, 3);
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, arr.Length, arr.Length + 2);
2190 Fail ("#N" + tc.ToString ());
2192 catch (ArgumentException) {}
2193 catch (Exception) { Fail ("#N" + tc.ToString ()); }
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};
2200 char[] c1 = (char[])starter.Clone();
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]);
2210 char[] c1 = (char[])starter.Clone();
2211 int[] i1 = (int[])starter1.Clone();
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]);
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]);
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]);
2256 public void TestInitializeEmpty()
2268 Assert("#TI01",!catched);
2272 public void TestInitializeInt()
2277 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2279 AssertEquals("#TI02 " + i ,a[i],b[i]);
2284 public void TestInitializeDouble()
2286 double[] a = {1.0,2.0,0.0};
2288 double[] b = {1.0,2.0,0.0};
2289 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2291 AssertEquals("#TI03 " + i ,a[i],b[i]);
2296 public void TestInitializeFloat()
2298 float[] a = {1.0F,2.0F,0.0F};
2300 float[] b = {1.0F,2.0F,0.0F};
2301 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2303 AssertEquals("#TI04 " + i ,a[i],b[i]);
2308 public void TestInitializeChar()
2310 char[] a = {'1','.','0','F','2','.','0','F'};
2312 char[] b = {'1','.','0','F','2','.','0','F'};
2313 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2315 AssertEquals("#TI05 " + i ,a[i],b[i]);
2320 public void TestInitializeString()
2322 string[] a = {"hola","adios","menos","mas"};
2324 string[] b = {"hola","adios","menos","mas"};
2325 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2327 AssertEquals("#TI06 " + i ,a[i],b[i]);
2332 public void TestInitializeEnum()
2334 enua[] a = {enua.hola,enua.adios,enua.menos,enua.mas};
2336 enua[] b = {enua.hola,enua.adios,enua.menos,enua.mas};
2337 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2339 AssertEquals("#TI07 " + i ,a[i],b[i]);
2344 public void TestInitializeIntNI()
2346 int[] a = new int[20];
2350 AssertEquals("#TI08",b,0);
2355 public void TestInitializeCharNI()
2357 char[] a = new char[20];
2359 foreach(char b in a)
2361 AssertEquals("#TI09",b,0);
2366 public void TestInitializeDoubleNI()
2368 double[] a = new double[20];
2370 foreach(double b in a)
2372 AssertEquals("#TI09",b,0.0);
2377 public void TestInitializeStringNI()
2379 string[] a = new string[20];
2381 foreach(string b in a)
2383 AssertEquals("#TI10",b,null);
2388 public void TestInitializeObjectNI()
2390 object[] a = new object[20];
2392 foreach(object b in a)
2394 AssertEquals("#TI11",b,null);
2399 public void TestInitializeAClassNI()
2401 AClass[] a = new AClass[20];
2403 foreach(AClass b in a)
2405 AssertEquals("#TI12",b,null);
2411 public void TestInitializeAStructNI()
2413 AStruct[] a = new AStruct[20];
2415 foreach(AStruct b in a)
2417 AssertEquals("#TI14",b,new AStruct());
2422 public void TestInitializeAStruct()
2424 AStruct[] a = new AStruct[3];
2428 AStruct[] b = new AStruct[3];
2431 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2433 AssertEquals("#TI15 " + i ,a[i],b[i]);
2438 public void TestInitializeDateTimeNI()
2440 DateTime[] a = new DateTime[20];
2442 foreach(DateTime b in a)
2444 AssertEquals("#TI16",b,new DateTime());
2449 [ExpectedException (typeof (ArgumentNullException))]
2450 public void MoreSort1 ()
2452 Array.Sort (null, 0, 1);
2456 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2457 public void MoreSort2 ()
2459 Array.Sort (arrsort, -1, 3);
2463 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2464 public void MoreSort3 ()
2466 Array.Sort (arrsort, 1, -3);
2470 [ExpectedException (typeof (ArgumentException))]
2471 public void MoreSort4 ()
2473 Array.Sort (arrsort, arrsort.Length, arrsort.Length + 2);
2477 [ExpectedException (typeof (RankException))]
2478 public void MoreSort5 ()
2480 char [,] arr = new char [,] {{'a'}, {'b'}};
2481 Array.Sort (arr, 0, 1);
2485 public void MoreSort6 ()
2487 Array.Sort (arrsort, 0, 0);
2491 [ExpectedException (typeof (ArgumentException))]
2492 public void MoreSort7 ()
2494 Array.Sort (arrsort, arrsort.Length - 1, 2);
2498 [ExpectedException (typeof (ArgumentException))]
2499 public void MoreSort8 ()
2501 Array.Sort (arrsort, 0, arrsort.Length + 1);
2505 public void MoreSort9 ()
2507 Array.Sort (arrsort, null, 0, arrsort.Length, null);
2511 [ExpectedException (typeof (InvalidOperationException))]
2512 public void MoreSort10 ()
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);
2518 [Test] // bug #81941
2521 double [] a = new double [2] { 0.9, 0.3 };
2522 uint [] b = new uint [2] { 4, 7 };
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]);
2531 public void ClearJaggedArray ()
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++) {
2540 Array.Clear (matrix, 0, 8);
2541 for (int i=0; i < 8; i++) {
2542 AssertNull (i.ToString (), matrix [i]);
2547 public void ClearMultidimentionalArray ()
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]);
2558 [ExpectedException (typeof (IndexOutOfRangeException))]
2559 public void ClearOutsideMultidimentionalArray ()
2561 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2562 Array.Clear (matrix, 0, 5);
2566 [ExpectedException (typeof (IndexOutOfRangeException))]
2567 public void Clear_IndexOverflow ()
2569 byte[] array = new byte [16];
2570 Array.Clear (array, 4, Int32.MaxValue);
2574 [ExpectedException (typeof (IndexOutOfRangeException))]
2575 public void Clear_LengthOverflow ()
2577 byte[] array = new byte [16];
2578 Array.Clear (array, Int32.MaxValue, 4);
2582 [ExpectedException (typeof (ArgumentException))]
2583 public void Copy_SourceIndexOverflow ()
2585 byte[] array = new byte [16];
2586 Array.Copy (array, Int32.MaxValue, array, 8, 8);
2590 [ExpectedException (typeof (ArgumentException))]
2591 public void Copy_DestinationIndexOverflow ()
2593 byte[] array = new byte [16];
2594 Array.Copy (array, 8, array, Int32.MaxValue, 8);
2598 [ExpectedException (typeof (ArgumentException))]
2599 public void Copy_LengthOverflow ()
2601 byte[] array = new byte [16];
2602 Array.Copy (array, 8, array, 8, Int32.MaxValue);
2606 [ExpectedException (typeof (ArgumentException))]
2607 public void Reverse_IndexOverflow ()
2609 byte[] array = new byte [16];
2610 Array.Reverse (array, Int32.MaxValue, 8);
2614 [ExpectedException (typeof (ArgumentException))]
2615 public void Reverse_LengthOverflow ()
2617 byte[] array = new byte [16];
2618 Array.Reverse (array, 8, Int32.MaxValue);
2621 public struct CharX : IComparable {
2624 public CharX (char c)
2629 public int CompareTo (object obj)
2632 return c.CompareTo (((CharX) obj).c);
2634 return c.CompareTo (obj);
2639 public void BinarySearch_ArgPassingOrder ()
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.
2646 CharX [] x = { new CharX ('a'), new CharX ('b'), new CharX ('c') };
2647 AssertEquals (1, Array.BinarySearch (x, 'b'));
2650 class Comparer: IComparer {
2652 private bool called = false;
2654 public bool Called {
2656 bool result = called;
2662 public int Compare (object x, object y)
2670 public void BinarySearch1_EmptyList ()
2672 int[] array = new int[0];
2673 AssertEquals ("BinarySearch", - 1, Array.BinarySearch (array, 0));
2677 public void BinarySearch2_EmptyList ()
2679 int[] array = new int[0];
2680 AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, 0, 0));
2684 public void BinarySearch3_EmptyList ()
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);
2694 public void BinarySearch4_EmptyList ()
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);
2705 [ExpectedException (typeof (ArgumentNullException))]
2706 public void AsReadOnly_NullArray ()
2708 Array.AsReadOnly <int> (null);
2712 public void ReadOnly_Count ()
2714 AssertEquals (10, Array.AsReadOnly (new int [10]).Count);
2718 public void ReadOnly_Contains ()
2720 int[] arr = new int [2];
2723 IList<int> a = Array.AsReadOnly (arr);
2725 Assert (a.Contains (3));
2726 Assert (!a.Contains (6));
2730 public void ReadOnly_IndexOf ()
2732 int[] arr = new int [2];
2735 IList<int> a = Array.AsReadOnly (arr);
2737 AssertEquals (0, a.IndexOf (3));
2738 AssertEquals (1, a.IndexOf (5));
2739 AssertEquals (-1, a.IndexOf (6));
2743 public void ReadOnly_Indexer ()
2745 int[] arr = new int [2];
2748 IList<int> a = Array.AsReadOnly (arr);
2750 AssertEquals (3, a [0]);
2751 AssertEquals (5, a [1]);
2753 /* Check that modifications to the original array are visible */
2755 AssertEquals (6, a [0]);
2759 public void ReadOnly_Enumerator ()
2761 int[] arr = new int [10];
2763 for (int i = 0; i < 10; ++i)
2767 foreach (int i in Array.AsReadOnly (arr))
2770 AssertEquals (45, sum);
2774 public void Resize ()
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]);
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]);
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]);
2797 public void Resize_null ()
2800 Array.Resize (ref arr, 10);
2801 AssertEquals (arr.Length, 10);
2805 public void Test_ContainsAndIndexOf_EquatableItem ()
2807 EquatableClass[] list = new EquatableClass[] {new EquatableClass (0), new EquatableClass (1), new EquatableClass (0)};
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)));
2815 public class EquatableClass : IEquatable<EquatableClass>
2818 public EquatableClass (int x)
2823 public bool Equals (EquatableClass other)
2825 return this._x == other._x;
2830 public void AsIList ()
2832 IList<int> arr = new int [10];
2834 AssertEquals (5, arr [0]);
2836 IList<FooStruct> arr2 = new FooStruct [10];
2837 FooStruct s = new FooStruct ();
2842 AssertEquals (11, s.i);
2843 AssertEquals (22, s.j);
2845 IList<string> arr3 = new string [10];
2847 AssertEquals ("ABC", arr3 [5]);
2854 #if !TARGET_JVM // BugBUG: T[] is not yet ICollection<T> under TARGET_JVM
2857 public void ICollectionNull ()
2859 ICollection<object> test;
2861 test = new List<object>();
2862 AssertEquals ("list<o>", test.Contains (null), false);
2864 test = new object[] {};
2865 AssertEquals ("empty array", test.Contains (null), false);
2867 test = new object[] {null};
2868 AssertEquals ("array with null", test.Contains (null), true);
2870 test = new List<object>(test);
2871 AssertEquals ("List<object> with test", test.Contains (null), true);
2873 test = new object[] {new object()};
2874 AssertEquals ("array with object", test.Contains (null), false);
2876 test = new List<object>(test);
2877 AssertEquals ("array with test", test.Contains (null), false);
2879 #endif // TARGET_JVM
2884 enum ByteEnum : byte {}
2885 enum IntEnum : int {}
2888 public void TestByteEnumArrayToByteArray ()
2890 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
2891 byte[] b = new byte[a.Length];
2896 public void TestByteEnumArrayToIntArray ()
2898 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
2899 int[] b = new int[a.Length];
2904 [ExpectedException (typeof (ArrayTypeMismatchException))]
2905 public void TestIntEnumArrayToByteArray ()
2907 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
2908 byte[] b = new byte[a.Length];
2913 public void TestIntEnumArrayToIntArray ()
2915 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
2916 int[] b = new int[a.Length];
2923 [Test] // bug #322248
2924 public void IEnumerator_Reset ()
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);
2935 Assert ("#C1", e.MoveNext ());
2936 AssertEquals ("#C2", 1, e.Current);
2940 public void IEnumerator_Current_Finished ()
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 ());
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);
2963 public void IEnumerator_Current_NotStarted ()
2965 int[] array = new int[] { 1, 2, 3 };
2966 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
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);
2979 public void IEnumerator_Current_Reset ()
2981 int[] array = new int[] { 1, 2, 3 };
2982 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
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);