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"));
1399 AssertEquals (-1, Array.LastIndexOf (new String [0], "foo"));
1403 public void TestLastIndexOf2() {
1405 bool errorThrown = false;
1407 Array.LastIndexOf(null, "huh?", 0);
1408 } catch (ArgumentNullException) {
1411 Assert("#K21", errorThrown);
1414 bool errorThrown = false;
1416 char[,] c = new Char[2,2];
1417 Array.LastIndexOf(c, "huh?", 0);
1418 } catch (RankException) {
1421 Assert("#K22", errorThrown);
1424 bool errorThrown = false;
1426 char[] c = new Char[2];
1427 Array.LastIndexOf(c, "huh?", 3);
1428 } catch (ArgumentOutOfRangeException) {
1431 Assert("#K23", errorThrown);
1434 String[] s1 = {"this", "is", "really", "a", "test"};
1435 AssertEquals("#K24", -1, Array.LastIndexOf(s1, null, 3));
1436 AssertEquals("#K25", -1, Array.LastIndexOf(s1, "nothing", 3));
1437 AssertEquals("#K26", -1, Array.LastIndexOf(s1, "test", 3));
1438 AssertEquals("#K27", 3, Array.LastIndexOf(s1, "a", 3));
1439 AssertEquals("#K28", 0, Array.LastIndexOf(s1, "this", 3));
1443 public void TestLastIndexOf3() {
1445 bool errorThrown = false;
1447 Array.LastIndexOf(null, "huh?", 0, 1);
1448 } catch (ArgumentNullException) {
1451 Assert("#K41", errorThrown);
1454 bool errorThrown = false;
1456 char[,] c = new Char[2,2];
1457 Array.LastIndexOf(c, "huh?", 0, 1);
1458 } catch (RankException) {
1461 Assert("#K42", errorThrown);
1464 bool errorThrown = false;
1466 char[] c = new Char[2];
1467 Array.LastIndexOf(c, "huh?", 3, 1);
1468 } catch (ArgumentOutOfRangeException) {
1471 Assert("#K43", errorThrown);
1474 bool errorThrown = false;
1476 char[] c = new Char[2];
1477 Array.LastIndexOf(c, "huh?", 0, 5);
1478 } catch (ArgumentOutOfRangeException) {
1481 Assert("#K44", errorThrown);
1484 String[] s1 = {"this", "is", "really", "a", "test"};
1485 AssertEquals("#K45", -1, Array.LastIndexOf(s1, null, 3, 3));
1486 AssertEquals("#K46", -1, Array.LastIndexOf(s1, "nothing", 3, 3));
1487 AssertEquals("#K47", -1, Array.LastIndexOf(s1, "this", 3, 3));
1488 AssertEquals("#K48", 1, Array.LastIndexOf(s1, "is", 3, 3));
1489 AssertEquals("#K49", -1, Array.LastIndexOf(s1, "test", 3, 3));
1490 AssertEquals("#K50", 3, Array.LastIndexOf(s1, "a", 3, 3));
1494 public void TestLastIndexOf4 ()
1496 short [] a = new short [] { 19, 238, 317, 6, 565, 0, -52, 60, -563, 753, 238, 238};
1498 Array.LastIndexOf (a, (object)16, -1);
1499 NUnit.Framework.Assert.Fail ("#1");
1500 } catch (ArgumentOutOfRangeException) { }
1504 Array.LastIndexOf<short> (a, 16, -1);
1505 NUnit.Framework.Assert.Fail ("#2");
1506 } catch (ArgumentOutOfRangeException) { }
1511 public void TestLastIndexOf5 ()
1513 char [] a = new char [] {'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'j', 'i', 'h'};
1518 for (int i = a.Length - 1; i >= 0 ; i--) {
1520 retval = Array.LastIndexOf(a, a [i], i, i + 1);
1528 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1529 public void LastIndexOf_StartIndexOverflow ()
1531 // legal - no exception
1532 byte[] array = new byte [16];
1533 Array.LastIndexOf (array, this, Int32.MaxValue, 1);
1537 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1538 public void LastIndexOf_CountOverflow ()
1540 // legal - no exception
1541 byte[] array = new byte [16];
1542 Array.LastIndexOf (array, this, 1, Int32.MaxValue);
1546 public void TestReverse() {
1548 bool errorThrown = false;
1550 Array.Reverse(null);
1551 } catch (ArgumentNullException) {
1554 Assert("#L01", errorThrown);
1557 bool errorThrown = false;
1559 char[,] c = new Char[2,2];
1561 } catch (RankException) {
1564 Assert("#L02", errorThrown);
1567 char[] c1 = {'a', 'b', 'c', 'd'};
1569 AssertEquals("#L03", 'd', c1[0]);
1570 AssertEquals("#L04", 'c', c1[1]);
1571 AssertEquals("#L05", 'b', c1[2]);
1572 AssertEquals("#L06", 'a', c1[3]);
1575 bool errorThrown = false;
1577 Array.Reverse(null, 0, 0);
1578 } catch (ArgumentNullException) {
1581 Assert("#L07", errorThrown);
1584 bool errorThrown = false;
1586 char[,] c = new Char[2,2];
1587 Array.Reverse(c, 0, 0);
1588 } catch (RankException) {
1591 Assert("#L08", errorThrown);
1594 //bool errorThrown = false;
1596 // char[] c = new Char[2];
1597 // Array.Reverse(c, 0, 3);
1598 //} catch (ArgumentOutOfRangeException) {
1599 // errorThrown = true;
1601 //Assert("#L09", errorThrown);
1604 //bool errorThrown = false;
1606 // char[] c = new Char[2];
1607 // Array.Reverse(c, 3, 0);
1608 //} catch (ArgumentOutOfRangeException) {
1609 // errorThrown = true;
1611 //Assert("#L10", errorThrown);
1614 char[] c2 = { 'a', 'b', 'c', 'd'};
1615 Array.Reverse(c2, 1, 2);
1616 AssertEquals("#L11", 'a', c2[0]);
1617 AssertEquals("#L12", 'c', c2[1]);
1618 AssertEquals("#L13", 'b', c2[2]);
1619 AssertEquals("#L14", 'd', c2[3]);
1623 public void TestSetValue1() {
1625 bool errorThrown = false;
1627 char[,] c = new Char[2,2];
1628 c.SetValue("buh", 1);
1629 } catch (ArgumentException) {
1632 Assert("#M01", errorThrown);
1635 bool errorThrown = false;
1637 char[] c = {'a', 'b', 'c'};
1638 c.SetValue("buh", -1);
1639 } catch (IndexOutOfRangeException) {
1642 Assert("#M02", errorThrown);
1645 bool errorThrown = false;
1647 char[] c = {'a', 'b', 'c'};
1648 c.SetValue("buh", 4);
1649 } catch (IndexOutOfRangeException) {
1652 Assert("#M03", errorThrown);
1655 char[] c1 = {'a', 'b', 'c', 'd'};
1656 char[] c2 = new char[4];
1657 for (int i = 0; i < c1.Length; i++) {
1658 c2.SetValue(c1[i], i);
1660 for (int i = 0; i < c1.Length; i++) {
1661 AssertEquals("#M04(" + i + ")", c1[i], c2[i]);
1664 int[] c3 = { 1, 2, 3 };
1665 long[] c4 = new long [3];
1667 for (int i = 0; i < c3.Length; i++)
1668 c4.SetValue (c3 [i], i);
1672 } catch (Exception e) {
1673 Fail ("c3.CopyTo(): e=" + e);
1675 for (int i = 0; i < c3.Length; i++)
1676 Assert ("#M05(" + i + ")", c3[i] == c4[i]);
1678 Object[] c5 = new Object [3];
1679 long[] c6 = new long [3];
1683 } catch (Exception e) {
1684 Fail ("c4.CopyTo(): e=" + e);
1689 } catch (Exception e) {
1690 Fail ("c5.CopyTo(): e=" + e);
1692 // for (int i = 0; i < c5.Length; i++)
1693 // Assert ("#M06(" + i + ")", c5[i] == c6[i]);
1697 public void TestSetValue2() {
1699 bool errorThrown = false;
1701 char[] c = new Char[2];
1702 c.SetValue("buh", 1,1);
1703 } catch (ArgumentException) {
1706 Assert("#M21", errorThrown);
1709 bool errorThrown = false;
1711 char[,] c = new Char[2,2];
1712 c.SetValue("buh", -1, 1);
1713 } catch (IndexOutOfRangeException) {
1716 Assert("#M22", errorThrown);
1719 bool errorThrown = false;
1721 char[,] c = new Char[2,2];
1722 c.SetValue("buh", 4,1);
1723 } catch (IndexOutOfRangeException) {
1726 Assert("#M23", errorThrown);
1729 char[,] c1 = new Char[4,6];
1730 char[,] c2 = new Char[4,6];
1731 for (int i = 0; i < 24; i++) {
1734 c1[first,second] = (char)(((int)'a')+i);
1735 c2.SetValue(c1[first,second], first, second);
1737 for (int i = 0; i < c1.GetLength(0); i++) {
1738 for (int j = 0; j < c1.GetLength(1); j++) {
1739 AssertEquals("#M24(" + i + "," + j + ")",
1746 public void TestSetValue3() {
1748 bool errorThrown = false;
1750 char[] c = new Char[2];
1751 c.SetValue("buh", 1,1,1);
1752 } catch (ArgumentException) {
1755 Assert("#M41", errorThrown);
1758 bool errorThrown = false;
1760 char[,,] c = new Char[2,2,2];
1761 c.SetValue("buh", -1, 1, 1);
1762 } catch (IndexOutOfRangeException) {
1765 Assert("#M42", errorThrown);
1768 bool errorThrown = false;
1770 char[,,] c = new Char[2,2,2];
1771 c.SetValue("buh", 4,1,1);
1772 } catch (IndexOutOfRangeException) {
1775 Assert("#M43", errorThrown);
1778 char[,,] c1 = new Char[4,2,3];
1779 char[,,] c2 = new Char[4,2,3];
1780 for (int i = 0; i < 24; i++) {
1782 int remains = i % 6;
1783 int second = remains / 3;
1784 int third = remains % 3;
1785 c1[first,second, third] = (char)(((int)'a')+i);
1786 c2.SetValue(c1[first, second, third], first, second, third);
1788 for (int i = 0; i < c1.GetLength(0); i++) {
1789 for (int j = 0; j < c1.GetLength(1); j++) {
1790 for (int k = 0; k < c1.GetLength(2); k++) {
1791 AssertEquals("#M44(" + i + "," + j + " )",
1792 c1[i,j,k], c2[i,j,k]);
1800 [ExpectedException (typeof (ArgumentNullException))]
1802 [ExpectedException (typeof (NullReferenceException))]
1804 public void TestSetValueLongArray ()
1806 char[] c = new Char[2];
1807 c.SetValue("buh", (long [])null);
1811 public void TestSetValueN() {
1813 bool errorThrown = false;
1815 char[] c = new Char[2];
1816 c.SetValue("buh", (int [])null);
1817 } catch (ArgumentNullException) {
1820 Assert("#M61a", errorThrown);
1823 bool errorThrown = false;
1825 char[] c = new Char[2];
1826 int[] coords = {1, 1};
1827 c.SetValue("buh", coords);
1828 } catch (ArgumentException) {
1831 Assert("#M62", errorThrown);
1834 bool errorThrown = false;
1836 char[,] c = new Char[2,2];
1837 int[] coords = {-1, 1};
1838 c.SetValue("buh", coords);
1839 } catch (IndexOutOfRangeException) {
1842 Assert("#M63", errorThrown);
1845 bool errorThrown = false;
1847 char[,] c = new Char[2,2];
1848 int[] coords = {4, 1};
1849 c.SetValue("buh", coords);
1850 } catch (IndexOutOfRangeException) {
1853 Assert("#M64", errorThrown);
1856 char[,] c1 = new Char[4,6];
1857 char[,] c2 = new Char[4,6];
1858 for (int i = 0; i < 24; i++) {
1861 c1[first,second] = (char)(((int)'a')+i);
1862 int[] coords = {first, second};
1863 c2.SetValue(c1[first,second], coords);
1865 for (int i = 0; i < c1.GetLength(0); i++) {
1866 for (int j = 0; j < c1.GetLength(1); j++) {
1867 AssertEquals("#M65(" + i + "," + j + ")",
1874 public void TestSetValue4() {
1876 int[] c1 = { 1, 2, 3 };
1877 long[] c2 = new long [3];
1879 for (int i = 0; i < c1.Length; i++)
1880 c2.SetValue (c1 [i], i);
1882 for (int i = 0; i < c1.Length; i++) {
1883 Assert ("#M81(" + i + ")", c1[i] == c2[i]);
1884 AssertEquals ("#M82(" + i + ")", typeof (long), c2[i].GetType ());
1888 long[] c1 = { 1, 2, 3 };
1889 int[] c2 = new int [3];
1890 bool errorThrown = false;
1892 c2.SetValue (c1 [0], 0);
1893 } catch (ArgumentException) {
1896 Assert("#M83", errorThrown);
1899 int[] c1 = { 1, 2, 3 };
1900 Object[] c2 = new Object [3];
1902 for (int i = 0; i < c1.Length; i++)
1903 c2.SetValue (c1 [i], i);
1905 for (int i = 0; i < c1.Length; i++)
1906 AssertEquals ("#M84(" + i + ")", c1[i], Convert.ToInt32 (c2[i]));
1909 Object[] c1 = new Object [3];
1910 Object[] c2 = new Object [3];
1911 c1[0] = new Object ();
1913 for (int i = 0; i < c1.Length; i++)
1914 c2.SetValue (c1 [i], i);
1916 for (int i = 0; i < c1.Length; i++)
1917 AssertEquals ("#M85(" + i + ")", c1[i], c2[i]);
1920 Object[] c1 = new Object [3];
1921 string[] c2 = new String [3];
1922 string test = "hello";
1925 c2.SetValue (c1 [0], 0);
1926 AssertEquals ("#M86", c1[0], c2[0]);
1927 AssertEquals ("#M87", "hello", c2[0]);
1930 char[] c1 = { 'a', 'b', 'c' };
1931 string[] c2 = new string [3];
1933 c2.SetValue (c1 [0], 0);
1935 } catch (InvalidCastException) {}
1938 Single[] c1 = { 1.2F, 2.3F, 3.4F, 4.5F };
1939 long[] c2 = new long [3];
1941 c2.SetValue (c1 [0], 0);
1943 } catch (ArgumentException) {}
1971 UInt32 v11 = 235354;
1972 UInt64 v12 = 234552;
1974 Object[] va1 = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12 };
1975 Object[] va2 = { "true", "1", "a", "-1.2", "-32", "-234", "-34523", "-1",
1976 "-4.8F", "24234", "235354", "234552" };
1978 Object[][] vt = { va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1 };
1981 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1982 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
1983 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
1984 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1985 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1,
1986 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1,
1987 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,
1988 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1,
1989 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1,
1990 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
1991 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0,
1992 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0
1997 for (int i = 0; i < types.Length; i++) {
1998 for (int j = 0; j < types.Length; j++) {
1999 Array array = Array.CreateInstance (types [j], 2);
2001 Object value = vt[j][i];
2003 bool errorThrown = false;
2005 array.SetValue (value, 0);
2006 } catch (ArgumentException) {
2010 int ex_index = (i * types.Length) + j;
2012 AssertEquals ("#M90(" + types [i] + "," + types [j] + ")",
2013 errorThrown, arg_ex [ex_index] == 1);
2017 for (int i = 0; i < types.Length; i++) {
2018 String[] array = new String [2];
2020 Object value = va1 [i];
2022 bool errorThrown = false;
2024 array.SetValue (value, 0);
2025 } catch (InvalidCastException) {
2029 Assert ("#M91(" + types [i] + ")", errorThrown);
2032 for (int i = 0; i < types.Length; i++) {
2033 Array array = Array.CreateInstance (types [i], 2);
2035 Object value = va2 [i];
2037 bool errorThrown = false;
2039 array.SetValue (value, 0);
2040 } catch (InvalidCastException) {
2044 Assert ("#M92(" + types [i] + ")", errorThrown);
2047 for (int i = 0; i < types.Length; i++) {
2048 Array array = Array.CreateInstance (types [i], 2);
2050 Object value = null;
2052 bool errorThrown = false;
2054 array.SetValue (value, 0);
2055 } catch (InvalidCastException) {
2059 Assert ("#M93(" + types [i] + ")", !errorThrown);
2064 for (int i = 0; i < types.Length; i++) {
2065 for (int j = 0; j < types.Length; j++) {
2066 Array source = Array.CreateInstance (types [i], 2);
2067 Array array = Array.CreateInstance (types [j], 2);
2069 source.SetValue (vt[j][i], 0);
2070 source.SetValue (vt[j][i], 1);
2072 bool errorThrown = false;
2074 Array.Copy (source, array, 2);
2075 } catch (ArrayTypeMismatchException) {
2079 int ex_index = (i * types.Length) + j;
2081 AssertEquals ("#M94(" + types [i] + "," + types [j] + ")",
2082 errorThrown, arg_ex [ex_index] == 1);
2086 for (int i = 0; i < types.Length; i++) {
2087 Array source = Array.CreateInstance (types [i], 2);
2088 String[] array = new String [2];
2090 source.SetValue (va1 [i], 0);
2091 source.SetValue (va1 [i], 1);
2093 bool errorThrown = false;
2095 Array.Copy (source, array, 2);
2096 } catch (ArrayTypeMismatchException) {
2100 Assert ("#M95(" + types [i] + ")", errorThrown);
2103 for (int i = 0; i < types.Length; i++) {
2104 String[] source = new String [2];
2105 Array array = Array.CreateInstance (types [i], 2);
2107 source.SetValue (va2 [i], 0);
2108 source.SetValue (va2 [i], 1);
2110 bool errorThrown = false;
2112 Array.Copy (source, array, 2);
2113 } catch (ArrayTypeMismatchException) {
2117 Assert ("#M96(" + types [i] + ")", errorThrown);
2123 public void TestSort() {
2125 bool errorThrown = false;
2128 } catch (ArgumentNullException) {
2131 Assert("#N01", errorThrown);
2134 bool errorThrown = false;
2136 Array.Sort(null, 0, 1);
2137 } catch (ArgumentNullException) {
2140 Assert("#N02", errorThrown);
2143 bool errorThrown = false;
2145 char[] c1 = new Char[2];
2146 Array.Sort(null, c1);
2147 } catch (ArgumentNullException) {
2150 Assert("#N03", errorThrown);
2153 bool errorThrown = false;
2155 char[] c1 = new Char[2];
2156 Array.Sort(null, c1, 0, 1);
2157 } catch (ArgumentNullException) {
2160 Assert("#N04", errorThrown);
2164 char[] arr = {'d', 'b', 'f', 'e', 'a', 'c'};
2167 Array.Sort (null, 0, 1);
2168 Fail ("#N" + tc.ToString ());
2170 catch (ArgumentException) {}
2171 catch (Exception) { Fail ("#N" + tc.ToString ()); }
2175 Array.Sort (arr, -1, 3);
2176 Fail ("#N" + tc.ToString ());
2178 catch (ArgumentException) {}
2179 catch (Exception) { Fail ("#N" + tc.ToString ()); }
2183 Array.Sort (arr, 1, -3);
2184 Fail ("#N" + tc.ToString ());
2186 catch (ArgumentException) {}
2187 catch (Exception) { Fail ("#N" + tc.ToString ()); }
2191 Array.Sort (arr, arr.Length, arr.Length + 2);
2192 Fail ("#N" + tc.ToString ());
2194 catch (ArgumentException) {}
2195 catch (Exception) { Fail ("#N" + tc.ToString ()); }
2198 // note: null second array => just sort first array
2199 char[] starter = {'d', 'b', 'f', 'e', 'a', 'c'};
2200 int[] starter1 = {1,2,3,4,5,6};
2202 char[] c1 = (char[])starter.Clone();
2204 AssertEquals("#N21", 'a', c1[0]);
2205 AssertEquals("#N22", 'b', c1[1]);
2206 AssertEquals("#N23", 'c', c1[2]);
2207 AssertEquals("#N24", 'd', c1[3]);
2208 AssertEquals("#N25", 'e', c1[4]);
2209 AssertEquals("#N26", 'f', c1[5]);
2212 char[] c1 = (char[])starter.Clone();
2213 int[] i1 = (int[])starter1.Clone();
2215 AssertEquals("#N41", 'a', c1[0]);
2216 AssertEquals("#N42", 'b', c1[1]);
2217 AssertEquals("#N43", 'c', c1[2]);
2218 AssertEquals("#N44", 'd', c1[3]);
2219 AssertEquals("#N45", 'e', c1[4]);
2220 AssertEquals("#N46", 'f', c1[5]);
2221 AssertEquals("#N47", 5, i1[0]);
2222 AssertEquals("#N48", 2, i1[1]);
2223 AssertEquals("#N49", 6, i1[2]);
2224 AssertEquals("#N50", 1, i1[3]);
2225 AssertEquals("#N51", 4, i1[4]);
2226 AssertEquals("#N52", 3, i1[5]);
2229 char[] c1 = (char[])starter.Clone();
2230 Array.Sort(c1, 1, 4);
2231 AssertEquals("#N61", 'd', c1[0]);
2232 AssertEquals("#N62", 'a', c1[1]);
2233 AssertEquals("#N63", 'b', c1[2]);
2234 AssertEquals("#N64", 'e', c1[3]);
2235 AssertEquals("#N65", 'f', c1[4]);
2236 AssertEquals("#N66", 'c', c1[5]);
2239 char[] c1 = (char[])starter.Clone();
2240 int[] i1 = (int[])starter1.Clone();
2241 Array.Sort(c1, i1, 1, 4);
2242 AssertEquals("#N81", 'd', c1[0]);
2243 AssertEquals("#N82", 'a', c1[1]);
2244 AssertEquals("#N83", 'b', c1[2]);
2245 AssertEquals("#N84", 'e', c1[3]);
2246 AssertEquals("#N85", 'f', c1[4]);
2247 AssertEquals("#N86", 'c', c1[5]);
2248 AssertEquals("#N87", 1, i1[0]);
2249 AssertEquals("#N88", 5, i1[1]);
2250 AssertEquals("#N89", 2, i1[2]);
2251 AssertEquals("#N90", 4, i1[3]);
2252 AssertEquals("#N91", 3, i1[4]);
2253 AssertEquals("#N92", 6, i1[5]);
2258 public void TestInitializeEmpty()
2270 Assert("#TI01",!catched);
2274 public void TestInitializeInt()
2279 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2281 AssertEquals("#TI02 " + i ,a[i],b[i]);
2286 public void TestInitializeDouble()
2288 double[] a = {1.0,2.0,0.0};
2290 double[] b = {1.0,2.0,0.0};
2291 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2293 AssertEquals("#TI03 " + i ,a[i],b[i]);
2298 public void TestInitializeFloat()
2300 float[] a = {1.0F,2.0F,0.0F};
2302 float[] b = {1.0F,2.0F,0.0F};
2303 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2305 AssertEquals("#TI04 " + i ,a[i],b[i]);
2310 public void TestInitializeChar()
2312 char[] a = {'1','.','0','F','2','.','0','F'};
2314 char[] b = {'1','.','0','F','2','.','0','F'};
2315 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2317 AssertEquals("#TI05 " + i ,a[i],b[i]);
2322 public void TestInitializeString()
2324 string[] a = {"hola","adios","menos","mas"};
2326 string[] b = {"hola","adios","menos","mas"};
2327 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2329 AssertEquals("#TI06 " + i ,a[i],b[i]);
2334 public void TestInitializeEnum()
2336 enua[] a = {enua.hola,enua.adios,enua.menos,enua.mas};
2338 enua[] b = {enua.hola,enua.adios,enua.menos,enua.mas};
2339 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2341 AssertEquals("#TI07 " + i ,a[i],b[i]);
2346 public void TestInitializeIntNI()
2348 int[] a = new int[20];
2352 AssertEquals("#TI08",b,0);
2357 public void TestInitializeCharNI()
2359 char[] a = new char[20];
2361 foreach(char b in a)
2363 AssertEquals("#TI09",b,0);
2368 public void TestInitializeDoubleNI()
2370 double[] a = new double[20];
2372 foreach(double b in a)
2374 AssertEquals("#TI09",b,0.0);
2379 public void TestInitializeStringNI()
2381 string[] a = new string[20];
2383 foreach(string b in a)
2385 AssertEquals("#TI10",b,null);
2390 public void TestInitializeObjectNI()
2392 object[] a = new object[20];
2394 foreach(object b in a)
2396 AssertEquals("#TI11",b,null);
2401 public void TestInitializeAClassNI()
2403 AClass[] a = new AClass[20];
2405 foreach(AClass b in a)
2407 AssertEquals("#TI12",b,null);
2413 public void TestInitializeAStructNI()
2415 AStruct[] a = new AStruct[20];
2417 foreach(AStruct b in a)
2419 AssertEquals("#TI14",b,new AStruct());
2424 public void TestInitializeAStruct()
2426 AStruct[] a = new AStruct[3];
2430 AStruct[] b = new AStruct[3];
2433 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2435 AssertEquals("#TI15 " + i ,a[i],b[i]);
2440 public void TestInitializeDateTimeNI()
2442 DateTime[] a = new DateTime[20];
2444 foreach(DateTime b in a)
2446 AssertEquals("#TI16",b,new DateTime());
2451 [ExpectedException (typeof (ArgumentNullException))]
2452 public void MoreSort1 ()
2454 Array.Sort (null, 0, 1);
2458 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2459 public void MoreSort2 ()
2461 Array.Sort (arrsort, -1, 3);
2465 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2466 public void MoreSort3 ()
2468 Array.Sort (arrsort, 1, -3);
2472 [ExpectedException (typeof (ArgumentException))]
2473 public void MoreSort4 ()
2475 Array.Sort (arrsort, arrsort.Length, arrsort.Length + 2);
2479 [ExpectedException (typeof (RankException))]
2480 public void MoreSort5 ()
2482 char [,] arr = new char [,] {{'a'}, {'b'}};
2483 Array.Sort (arr, 0, 1);
2487 public void MoreSort6 ()
2489 Array.Sort (arrsort, 0, 0);
2493 [ExpectedException (typeof (ArgumentException))]
2494 public void MoreSort7 ()
2496 Array.Sort (arrsort, arrsort.Length - 1, 2);
2500 [ExpectedException (typeof (ArgumentException))]
2501 public void MoreSort8 ()
2503 Array.Sort (arrsort, 0, arrsort.Length + 1);
2507 public void MoreSort9 ()
2509 Array.Sort (arrsort, null, 0, arrsort.Length, null);
2513 [ExpectedException (typeof (InvalidOperationException))]
2514 public void MoreSort10 ()
2516 object [] array = {true, 'k', SByte.MinValue, Byte.MinValue, (short) 2, 634, (long) 436, (float) 1.1, 1.23, "Hello World"};
2517 Array.Sort (array, (IComparer) null);
2520 [Test] // bug #81941
2523 double [] a = new double [2] { 0.9, 0.3 };
2524 uint [] b = new uint [2] { 4, 7 };
2526 AssertEquals ("#1", 0.3, a [0]);
2527 AssertEquals ("#2", 0.9, a [1]);
2528 AssertEquals ("#3", 7, b [0]);
2529 AssertEquals ("#4", 4, b [1]);
2533 public void ClearJaggedArray ()
2535 byte[][] matrix = new byte [8][];
2536 for (int i=0; i < 8; i++) {
2537 matrix [i] = new byte [8];
2538 for (int j=0; j < 8; j++) {
2542 Array.Clear (matrix, 0, 8);
2543 for (int i=0; i < 8; i++) {
2544 AssertNull (i.ToString (), matrix [i]);
2549 public void ClearMultidimentionalArray ()
2551 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2552 Array.Clear (matrix, 0, 2);
2553 AssertEquals ("0,0", 0, matrix [0,0]);
2554 AssertEquals ("0,1", 0, matrix [0,1]);
2555 AssertEquals ("1,0", 2, matrix [1,0]);
2556 AssertEquals ("1,1", 2, matrix [1,1]);
2560 [ExpectedException (typeof (IndexOutOfRangeException))]
2561 public void ClearOutsideMultidimentionalArray ()
2563 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2564 Array.Clear (matrix, 0, 5);
2568 [ExpectedException (typeof (IndexOutOfRangeException))]
2569 public void Clear_IndexOverflow ()
2571 byte[] array = new byte [16];
2572 Array.Clear (array, 4, Int32.MaxValue);
2576 [ExpectedException (typeof (IndexOutOfRangeException))]
2577 public void Clear_LengthOverflow ()
2579 byte[] array = new byte [16];
2580 Array.Clear (array, Int32.MaxValue, 4);
2584 [ExpectedException (typeof (ArgumentException))]
2585 public void Copy_SourceIndexOverflow ()
2587 byte[] array = new byte [16];
2588 Array.Copy (array, Int32.MaxValue, array, 8, 8);
2592 [ExpectedException (typeof (ArgumentException))]
2593 public void Copy_DestinationIndexOverflow ()
2595 byte[] array = new byte [16];
2596 Array.Copy (array, 8, array, Int32.MaxValue, 8);
2600 [ExpectedException (typeof (ArgumentException))]
2601 public void Copy_LengthOverflow ()
2603 byte[] array = new byte [16];
2604 Array.Copy (array, 8, array, 8, Int32.MaxValue);
2608 [ExpectedException (typeof (ArgumentException))]
2609 public void Reverse_IndexOverflow ()
2611 byte[] array = new byte [16];
2612 Array.Reverse (array, Int32.MaxValue, 8);
2616 [ExpectedException (typeof (ArgumentException))]
2617 public void Reverse_LengthOverflow ()
2619 byte[] array = new byte [16];
2620 Array.Reverse (array, 8, Int32.MaxValue);
2623 public struct CharX : IComparable {
2626 public CharX (char c)
2631 public int CompareTo (object obj)
2634 return c.CompareTo (((CharX) obj).c);
2636 return c.CompareTo (obj);
2641 public void BinarySearch_ArgPassingOrder ()
2644 // This tests that arguments are passed to the comprer in the correct
2645 // order. The IComparable of the *array* elements must get called, not
2646 // that of the search object.
2648 CharX [] x = { new CharX ('a'), new CharX ('b'), new CharX ('c') };
2649 AssertEquals (1, Array.BinarySearch (x, 'b'));
2652 class Comparer: IComparer {
2654 private bool called = false;
2656 public bool Called {
2658 bool result = called;
2664 public int Compare (object x, object y)
2672 public void BinarySearch1_EmptyList ()
2674 int[] array = new int[0];
2675 AssertEquals ("BinarySearch", - 1, Array.BinarySearch (array, 0));
2679 public void BinarySearch2_EmptyList ()
2681 int[] array = new int[0];
2682 AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, 0, 0));
2686 public void BinarySearch3_EmptyList ()
2688 Comparer comparer = new Comparer ();
2689 int[] array = new int[0];
2690 AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, comparer));
2691 // bug 77030 - the comparer isn't called for an empty array/list
2692 Assert ("Called", !comparer.Called);
2696 public void BinarySearch4_EmptyList ()
2698 Comparer comparer = new Comparer ();
2699 int[] array = new int[0];
2700 AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, 0, comparer));
2701 // bug 77030 - the comparer isn't called for an empty array/list
2702 Assert ("Called", !comparer.Called);
2707 [ExpectedException (typeof (ArgumentNullException))]
2708 public void AsReadOnly_NullArray ()
2710 Array.AsReadOnly <int> (null);
2714 public void ReadOnly_Count ()
2716 AssertEquals (10, Array.AsReadOnly (new int [10]).Count);
2720 public void ReadOnly_Contains ()
2722 int[] arr = new int [2];
2725 IList<int> a = Array.AsReadOnly (arr);
2727 Assert (a.Contains (3));
2728 Assert (!a.Contains (6));
2732 public void ReadOnly_IndexOf ()
2734 int[] arr = new int [2];
2737 IList<int> a = Array.AsReadOnly (arr);
2739 AssertEquals (0, a.IndexOf (3));
2740 AssertEquals (1, a.IndexOf (5));
2741 AssertEquals (-1, a.IndexOf (6));
2745 public void ReadOnly_Indexer ()
2747 int[] arr = new int [2];
2750 IList<int> a = Array.AsReadOnly (arr);
2752 AssertEquals (3, a [0]);
2753 AssertEquals (5, a [1]);
2755 /* Check that modifications to the original array are visible */
2757 AssertEquals (6, a [0]);
2761 public void ReadOnly_Enumerator ()
2763 int[] arr = new int [10];
2765 for (int i = 0; i < 10; ++i)
2769 foreach (int i in Array.AsReadOnly (arr))
2772 AssertEquals (45, sum);
2776 public void Resize ()
2778 int [] arr = new int [] { 1, 3, 5 };
2779 Array.Resize <int> (ref arr, 3);
2780 AssertEquals ("#A1", 3, arr.Length);
2781 AssertEquals ("#A2", 1, arr [0]);
2782 AssertEquals ("#A3", 3, arr [1]);
2783 AssertEquals ("#A4", 5, arr [2]);
2785 Array.Resize <int> (ref arr, 2);
2786 AssertEquals ("#B1", 2, arr.Length);
2787 AssertEquals ("#B2", 1, arr [0]);
2788 AssertEquals ("#B3", 3, arr [1]);
2790 Array.Resize <int> (ref arr, 4);
2791 AssertEquals ("#C1", 4, arr.Length);
2792 AssertEquals ("#C2", 1, arr [0]);
2793 AssertEquals ("#C3", 3, arr [1]);
2794 AssertEquals ("#C4", 0, arr [2]);
2795 AssertEquals ("#C5", 0, arr [3]);
2799 public void Resize_null ()
2802 Array.Resize (ref arr, 10);
2803 AssertEquals (arr.Length, 10);
2807 public void Test_ContainsAndIndexOf_EquatableItem ()
2809 EquatableClass[] list = new EquatableClass[] {new EquatableClass (0), new EquatableClass (1), new EquatableClass (0)};
2811 AssertEquals ("#0", 0, Array.IndexOf<EquatableClass> (list, list[0]));
2812 AssertEquals ("#1", 0, Array.IndexOf<EquatableClass> (list, new EquatableClass (0)));
2813 AssertEquals ("#2", 2, Array.LastIndexOf<EquatableClass> (list, list[0]));
2814 AssertEquals ("#3", 2, Array.LastIndexOf<EquatableClass> (list, new EquatableClass (0)));
2817 public class EquatableClass : IEquatable<EquatableClass>
2820 public EquatableClass (int x)
2825 public bool Equals (EquatableClass other)
2827 return this._x == other._x;
2832 public void AsIList ()
2834 IList<int> arr = new int [10];
2836 AssertEquals (5, arr [0]);
2838 IList<FooStruct> arr2 = new FooStruct [10];
2839 FooStruct s = new FooStruct ();
2844 AssertEquals (11, s.i);
2845 AssertEquals (22, s.j);
2847 IList<string> arr3 = new string [10];
2849 AssertEquals ("ABC", arr3 [5]);
2856 #if !TARGET_JVM // BugBUG: T[] is not yet ICollection<T> under TARGET_JVM
2859 public void ICollectionNull ()
2861 ICollection<object> test;
2863 test = new List<object>();
2864 AssertEquals ("list<o>", test.Contains (null), false);
2866 test = new object[] {};
2867 AssertEquals ("empty array", test.Contains (null), false);
2869 test = new object[] {null};
2870 AssertEquals ("array with null", test.Contains (null), true);
2872 test = new List<object>(test);
2873 AssertEquals ("List<object> with test", test.Contains (null), true);
2875 test = new object[] {new object()};
2876 AssertEquals ("array with object", test.Contains (null), false);
2878 test = new List<object>(test);
2879 AssertEquals ("array with test", test.Contains (null), false);
2881 #endif // TARGET_JVM
2886 enum ByteEnum : byte {}
2887 enum IntEnum : int {}
2890 public void TestByteEnumArrayToByteArray ()
2892 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
2893 byte[] b = new byte[a.Length];
2898 public void TestByteEnumArrayToIntArray ()
2900 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
2901 int[] b = new int[a.Length];
2906 [ExpectedException (typeof (ArrayTypeMismatchException))]
2907 public void TestIntEnumArrayToByteArray ()
2909 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
2910 byte[] b = new byte[a.Length];
2915 public void TestIntEnumArrayToIntArray ()
2917 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
2918 int[] b = new int[a.Length];
2925 [Test] // bug #322248
2926 public void IEnumerator_Reset ()
2928 int[] array = new int[] { 1, 2, 3};
2929 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
2930 Assert ("#A1", e.MoveNext ());
2931 AssertEquals ("#A2", 1, e.Current);
2932 Assert ("#A3", e.MoveNext ());
2933 AssertEquals ("#A4", 2, e.Current);
2937 Assert ("#C1", e.MoveNext ());
2938 AssertEquals ("#C2", 1, e.Current);
2942 public void IEnumerator_Current_Finished ()
2944 int[] array = new int[] { 1, 2, 3 };
2945 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
2946 Assert ("#A1", e.MoveNext ());
2947 AssertEquals ("#A2", 1, e.Current);
2948 Assert ("#A3", e.MoveNext ());
2949 AssertEquals ("#A4", 2, e.Current);
2950 Assert ("#A5", e.MoveNext ());
2951 AssertEquals ("#A6", 3, e.Current);
2952 Assert ("#A6", !e.MoveNext ());
2955 Fail ("#B1:" + e.Current);
2956 } catch (InvalidOperationException ex) {
2957 // Enumeration already finished
2958 AssertEquals ("#B2", typeof (InvalidOperationException), ex.GetType ());
2959 AssertNull ("#B3", ex.InnerException);
2960 AssertNotNull ("#B4", ex.Message);
2965 public void IEnumerator_Current_NotStarted ()
2967 int[] array = new int[] { 1, 2, 3 };
2968 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
2971 Fail ("#A1:" + e.Current);
2972 } catch (InvalidOperationException ex) {
2973 // Enumeration has not started. Call MoveNext
2974 AssertEquals ("#A2", typeof (InvalidOperationException), ex.GetType ());
2975 AssertNull ("#A3", ex.InnerException);
2976 AssertNotNull ("#A4", ex.Message);
2981 public void IEnumerator_Current_Reset ()
2983 int[] array = new int[] { 1, 2, 3 };
2984 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
2989 Fail ("#B1:" + e.Current);
2990 } catch (InvalidOperationException ex) {
2991 // Enumeration has not started. Call MoveNext
2992 AssertEquals ("#B2", typeof (InvalidOperationException), ex.GetType ());
2993 AssertNull ("#B3", ex.InnerException);
2994 AssertNotNull ("#B4", ex.Message);