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};
48 public override bool Equals (object obj)
54 //End Auxiliary Things
57 public class ArrayTest : Assertion
59 char [] arrsort = {'d', 'b', 'f', 'e', 'a', 'c'};
64 public void TestIsFixedSize() {
66 Assert("All arrays are fixed", a1.IsFixedSize);
70 public void TestIsReadOnly() {
72 Assert("No array is readonly", !a1.IsReadOnly);
76 public void TestIsSynchronized() {
78 Assert("No array is synchronized", !a1.IsSynchronized);
82 public void TestLength() {
85 AssertEquals("Zero length array", 0, a1.Length);
89 AssertEquals("One-length array", 1, a1.Length);
92 char[] a1 = {'c', 'c'};
93 AssertEquals("Two-length array", 2, a1.Length);
98 public void TestRank() {
99 char[] a1 = { 'c', 'd', 'e' };
100 AssertEquals("Rank one", 1, a1.Rank);
102 char[,] a2 = new Char[3,3];
103 AssertEquals("Rank two", 2, a2.Rank);
105 char[,,] a3 = new Char[3,3,3];
106 AssertEquals("Rank three", 3, a3.Rank);
110 public void TestBinarySearch1() {
111 bool errorThrown = false;
113 Array.BinarySearch(null, "blue");
114 } catch (ArgumentNullException) {
117 Assert("#B01", errorThrown);
120 char[,] c1 = new Char[2,2];
121 Array.BinarySearch(c1, "needle");
122 } catch (RankException) {
125 Assert("#B02", errorThrown);
128 char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
129 Assert("#B05", Array.BinarySearch(arr, 'c') >= 3);
130 Assert("#B06", Array.BinarySearch(arr, 'c') < 6);
133 char[] arr = {'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
134 AssertEquals("#B07", -4, Array.BinarySearch(arr, 'c'));
137 char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
138 AssertEquals("#B08", -9, Array.BinarySearch(arr, 'e'));
143 public void TestBinarySearch2() {
144 bool errorThrown = false;
146 Array.BinarySearch(null, 0, 1, "blue");
147 } catch (ArgumentNullException) {
150 Assert("#B20", errorThrown);
153 char[,] c1 = new Char[2,2];
154 Array.BinarySearch(c1, 0, 1, "needle");
155 } catch (RankException) {
158 Assert("#B21", errorThrown);
162 Array.BinarySearch(c1, -1, 1, 'a');
163 } catch (ArgumentOutOfRangeException) {
166 Assert("#B22", errorThrown);
170 Array.BinarySearch(c1, 0, -1, 'a');
171 } catch (ArgumentOutOfRangeException) {
174 Assert("#B23", errorThrown);
178 Array.BinarySearch(c1, 0, 4, 'a');
179 } catch (ArgumentException) {
182 Assert("#B24", errorThrown);
185 char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
186 Assert("#B26", Array.BinarySearch(arr, 2, 8, 'c') >= 5);
187 Assert("#B27", Array.BinarySearch(arr, 2, 8, 'c') < 8);
190 char[] arr = {'z', 'z', 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
191 AssertEquals("#B28", -6, Array.BinarySearch(arr, 2, 8, 'c'));
194 char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
195 AssertEquals("#B29", -11, Array.BinarySearch(arr, 2, 8, 'e'));
199 public void TestBinarySearch3()
201 int[] array = new int[100];
203 for (int i = 0; i < 100; i++)
206 AssertEquals("#B30", 49, Array.BinarySearch(array, 10));
210 public void BinarySearch_NullValue ()
212 int[] array = new int[1];
213 AssertEquals ("I=a,o", -1, Array.BinarySearch (array, null));
214 AssertEquals ("I=a,o,c", -1, Array.BinarySearch (array, null, null));
215 AssertEquals ("I=a,i,i,o", -1, Array.BinarySearch (array, 0, 1, null));
216 AssertEquals ("I=a,i,i,o,c", -1, Array.BinarySearch (array, 0, 1, null,null));
218 object[] o = new object [3] { this, this, null };
219 AssertEquals ("O=a,o", -1, Array.BinarySearch (o, null));
220 AssertEquals ("O=a,o,c", -1, Array.BinarySearch (o, null, null));
221 AssertEquals ("O=a,i,i,o", -1, Array.BinarySearch (o, 0, 3, null));
222 AssertEquals ("O=a,i,i,o,c", -1, Array.BinarySearch (o, 0, 3, null, null));
225 // TODO - testBinarySearch with explicit IComparer args
228 public void TestClear() {
229 bool errorThrown = false;
231 Array.Clear(null, 0, 1);
232 } catch (ArgumentNullException) {
235 Assert("#C01", errorThrown);
237 int[] i1 = { 1, 2, 3, 4 };
239 int[] compare = {1,2,3,4};
240 AssertEquals("#C02", compare[0], i1[0]);
241 AssertEquals("#C03", compare[1], i1[1]);
242 AssertEquals("#C04", compare[2], i1[2]);
243 AssertEquals("#C05", compare[3], i1[3]);
245 Array.Clear(i1, 3, 1);
247 int[] compare = {1,2,3,0};
248 AssertEquals("#C06", compare[0], i1[0]);
249 AssertEquals("#C07", compare[1], i1[1]);
250 AssertEquals("#C08", compare[2], i1[2]);
251 AssertEquals("#C09", compare[3], i1[3]);
253 Array.Clear(i1, 1, 1);
255 int[] compare = {1,0,3,0};
256 AssertEquals("#C10", compare[0], i1[0]);
257 AssertEquals("#C11", compare[1], i1[1]);
258 AssertEquals("#C12", compare[2], i1[2]);
259 AssertEquals("#C13", compare[3], i1[3]);
261 Array.Clear(i1, 1, 3);
263 int[] compare = {1,0,0,0};
264 AssertEquals("#C14", compare[0], i1[0]);
265 AssertEquals("#C15", compare[1], i1[1]);
266 AssertEquals("#C16", compare[2], i1[2]);
267 AssertEquals("#C17", compare[3], i1[3]);
270 string[] s1 = { "red", "green", "blue" };
271 Array.Clear(s1, 0, 3);
273 string[] compare = {null, null, null};
274 AssertEquals("#C18", compare[0], s1[0]);
275 AssertEquals("#C19", compare[1], s1[1]);
276 AssertEquals("#C20", compare[2], s1[2]);
281 public void TestClone() {
282 char[] c1 = {'a', 'b', 'c'};
283 char[] c2 = (char[])c1.Clone();
284 AssertEquals("#D01", c1[0], c2[0]);
285 AssertEquals("#D02", c1[1], c2[1]);
286 AssertEquals("#D03", c1[2], c2[2]);
288 char[] d10 = {'a', 'b'};
289 char[] d11 = {'a', 'c'};
290 char[] d12 = {'b', 'c'};
291 char[][] d1 = {d10, d11, d12};
292 char[][] d2 = (char[][])d1.Clone();
293 AssertEquals("#D04", d1[0], d2[0]);
294 AssertEquals("#D05", d1[1], d2[1]);
295 AssertEquals("#D06", d1[2], d2[2]);
298 AssertEquals("#D07", d1[0], d2[0]);
301 [Test] public void TestIndexer ()
303 int [] a = new int [10];
307 Fail ("IList.this [-1] should throw");
308 } catch (IndexOutOfRangeException) {
311 Fail ("Should have thrown an IndexOutOfRangeException");
316 public void TestCopy() {
318 bool errorThrown = false;
321 Array.Copy(c1, null, 1);
322 } catch (ArgumentNullException) {
325 Assert("#E01", errorThrown);
328 bool errorThrown = false;
331 Array.Copy(null, c1, 1);
332 } catch (ArgumentNullException) {
335 Assert("#E02", errorThrown);
338 bool errorThrown = false;
340 Char[] c1 = new Char[1];
341 Char[,] c2 = new Char[1,1];
342 Array.Copy(c1, c2, 1);
343 } catch (RankException) {
346 Assert("#E03", errorThrown);
349 bool errorThrown = false;
351 Char[] c1 = new Char[1];
352 string[] s1 = new String[1];
353 Array.Copy(c1, s1, 1);
354 } catch (ArrayTypeMismatchException) {
357 Assert("#E04", errorThrown);
360 bool errorThrown = false;
362 Char[] c1 = new Char[1];
363 Object[] o1 = new Object[1];
365 Array.Copy(o1, c1, 1);
366 } catch (InvalidCastException) {
369 Assert("#E05", errorThrown);
372 bool errorThrown = false;
374 Char[] c1 = new Char[1];
375 Char[] c2 = new Char[1];
376 Array.Copy(c1, c2, -1);
377 } catch (ArgumentOutOfRangeException) {
380 Assert("#E06", errorThrown);
383 bool errorThrown = false;
385 Char[] c1 = new Char[1];
386 Char[] c2 = new Char[2];
387 Array.Copy(c1, c2, 2);
388 } catch (ArgumentException) {
391 Assert("#E07", errorThrown);
394 bool errorThrown = false;
396 Char[] c1 = new Char[1];
397 Char[] c2 = new Char[2];
398 Array.Copy(c2, c1, 2);
399 } catch (ArgumentException) {
402 Assert("#E08", errorThrown);
405 char[] orig = {'a', 'b', 'd', 'a'};
406 char[] copy = new Char[4];
407 Array.Copy(orig, copy, 4);
408 for (int i = 0; i < orig.Length; i++) {
409 AssertEquals("#E09(" + i + ")", orig[i], copy[i]);
411 Array.Clear(copy, 0, copy.Length);
412 for (int i = 0; i < orig.Length; i++) {
413 AssertEquals("#E10(" + i + ")", (char)0, copy[i]);
415 Array.Copy(orig, copy, 2);
416 AssertEquals("#E11", orig[0], copy[0]);
417 AssertEquals("#E12", orig[1], copy[1]);
418 Assert("#E13", orig[2] != copy[2]);
419 Assert("#E14", orig[3] != copy[3]);
423 public void TestCopy2() {
425 bool errorThrown = false;
427 Char[] c1 = new Char[2];
428 Char[] c2 = new Char[2];
429 Array.Copy(c2, 1, c1, 0, 2);
430 } catch (ArgumentException) {
433 Assert("#E31", errorThrown);
436 bool errorThrown = false;
438 Char[] c1 = new Char[2];
439 Char[] c2 = new Char[2];
440 Array.Copy(c2, 0, c1, 1, 2);
441 } catch (ArgumentException) {
444 Assert("#E32", errorThrown);
447 char[] orig = {'a', 'b', 'd', 'a'};
448 char[] copy = new Char[4];
449 Array.Copy(orig, 1, copy, 1, 3);
450 Assert("#E33", copy[0] != orig[0]);
451 for (int i = 1; i < orig.Length; i++) {
452 AssertEquals("#E34(" + i + ")", orig[i], copy[i]);
454 Array.Clear(copy, 0, copy.Length);
455 Array.Copy(orig, 1, copy, 0, 2);
456 AssertEquals("#E35", orig[1], copy[0]);
457 AssertEquals("#E36", orig[2], copy[1]);
458 Assert("#E37", copy[2] != orig[2]);
459 Assert("#E38", copy[3] != orig[3]);
463 [ExpectedException (typeof (InvalidCastException))]
464 public void Copy_InvalidCast () {
465 object[] arr1 = new object [10];
466 Type[] arr2 = new Type [10];
468 arr1 [0] = new object ();
470 Array.Copy (arr1, 0, arr2, 0, 10);
474 public void TestCopyTo() {
476 bool errorThrown = false;
478 Char[] c1 = new Char[2];
480 } catch (ArgumentNullException) {
483 Assert("#E61", errorThrown);
486 bool errorThrown = false;
488 Char[] c1 = new Char[2];
489 Char[,] c2 = new Char[2,2];
491 } catch (ArgumentException) {
494 #if TARGET_JVM // This is really implementation dependent behaviour.
495 catch (RankException) {
499 Assert("#E62", errorThrown);
502 bool errorThrown = false;
504 Char[,] c1 = new Char[2,2];
505 Char[] c2 = new Char[2];
507 } catch (RankException) {
510 Assert("#E63", errorThrown);
513 bool errorThrown = false;
515 Char[,] c1 = new Char[2,2];
516 Char[] c2 = new Char[2];
518 } catch (RankException) {
521 Assert("#E64", errorThrown);
524 bool errorThrown = false;
526 Char[] c1 = new Char[2];
527 Char[] c2 = new Char[2];
529 } catch (ArgumentOutOfRangeException) {
532 Assert("#E65", errorThrown);
535 bool errorThrown = false;
537 Char[] c1 = new Char[2];
538 Char[] c2 = new Char[2];
540 } catch (ArgumentException) {
543 Assert("#E66", errorThrown);
546 bool errorThrown = false;
548 Char[] c1 = new Char[2];
549 Char[] c2 = new Char[2];
551 } catch (ArgumentException) {
554 Assert("#E67", errorThrown);
558 bool errorThrown = false;
560 String[] c1 = new String[2];
561 // TODO: this crashes mono if there are null
562 // values in the array.
565 Char[] c2 = new Char[2];
569 } catch (ArrayTypeMismatchException) {
572 Assert("#E68", errorThrown);
575 Char[] orig = {'a', 'b', 'c', 'd'};
576 Char[] copy = new Char[10];
577 Array.Clear(copy, 0, copy.Length);
578 orig.CopyTo(copy, 3);
579 AssertEquals("#E69", (char)0, copy[0]);
580 AssertEquals("#E70", (char)0, copy[1]);
581 AssertEquals("#E71", (char)0, copy[2]);
582 AssertEquals("#E72", orig[0], copy[3]);
583 AssertEquals("#E73", orig[1], copy[4]);
584 AssertEquals("#E74", orig[2], copy[5]);
585 AssertEquals("#E75", orig[3], copy[6]);
586 AssertEquals("#E76", (char)0, copy[7]);
587 AssertEquals("#E77", (char)0, copy[8]);
588 AssertEquals("#E78", (char)0, copy[9]);
591 // The following is valid and must not throw an exception.
592 bool errorThrown = false;
594 int[] src = new int [0];
595 int[] dest = new int [0];
596 src.CopyTo (dest, 0);
597 } catch (ArgumentException) {
600 Assert("#E79", !errorThrown);
605 bool errorThrown = false;
607 CClass[] src = new CClass [] { new CClass () };
608 BClass[] dest = new BClass [1];
610 src.CopyTo (dest, 0);
612 } catch (ArrayTypeMismatchException) {
615 Assert("#E80", errorThrown);
620 public void TestCreateInstance() {
622 bool errorThrown = false;
624 Array.CreateInstance(null, 12);
625 } catch (ArgumentNullException) {
628 Assert("#F01", errorThrown);
631 bool errorThrown = false;
633 Array.CreateInstance(Type.GetType("System.Char"), -3);
634 } catch (ArgumentOutOfRangeException) {
637 Assert("#F02", errorThrown);
640 bool errorThrown = false;
642 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
643 } catch (ArgumentNullException) {
646 Assert("#F03a", errorThrown);
650 bool errorThrown = false;
652 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
653 } catch (ArgumentNullException) {
656 Assert("#F03b", errorThrown);
659 #if !TARGET_JVM // Arrays lower bounds are not supported for TARGET_JVM
661 bool errorThrown = false;
663 Array.CreateInstance(Type.GetType("System.Char"), null, null);
664 } catch (ArgumentNullException) {
667 Assert("#F04", errorThrown);
671 bool errorThrown = false;
673 int[] lengths = new int [0];
674 Array.CreateInstance(Type.GetType("System.Char"), lengths);
675 } catch (ArgumentException) {
678 Assert("#F05", errorThrown);
680 #if !TARGET_JVM // CreateInstance with lower bounds not supported for TARGET_JVM
682 bool errorThrown = false;
684 int[] lengths = new int [1];
685 int[] bounds = new int [2];
686 Array.CreateInstance(Type.GetType("System.Char"), lengths, bounds);
688 } catch (ArgumentException) {
691 Assert("#F06", errorThrown);
694 char[] c1 = (char[])Array.CreateInstance(Type.GetType("System.Char"), 12);
695 AssertEquals("#F07", 12, c1.Length);
697 Array c2 = Array.CreateInstance(Type.GetType("System.Char"), 12, 5);
698 AssertEquals("#F08", 2, c2.Rank);
699 AssertEquals("#F09", 60, c2.Length);
703 int[] lengths = { 3 };
704 int[] bounds = { 5 };
705 int[] src = { 512, 718, 912 };
706 Array array = Array.CreateInstance(typeof(int), lengths, bounds);
708 AssertEquals("#F10", 3, array.Length);
709 AssertEquals("#F11", 5, array.GetLowerBound(0));
710 AssertEquals("#F12", 7, array.GetUpperBound(0));
712 src.CopyTo (array, 5);
714 for (int i = 0; i < src.Length; i++)
715 AssertEquals("#F13(" + i + ")", src[i], array.GetValue(i+5));
718 // Test that a 1 dimensional array with 0 lower bound is the
719 // same as an szarray
720 Type szarrayType = new int [10].GetType ();
721 Assert (szarrayType == (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0})).GetType ());
722 Assert (szarrayType != (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {1})).GetType ());
727 [ExpectedException (typeof (ArgumentNullException))]
728 public void TestCreateInstance2 ()
730 Array.CreateInstance (typeof (Int32), (int[])null);
735 [ExpectedException (typeof (ArgumentNullException))]
737 [ExpectedException (typeof (NullReferenceException))]
739 public void TestCreateInstance2b ()
741 Array.CreateInstance (typeof (Int32), (long[])null);
745 public void TestGetEnumerator() {
746 String[] s1 = {"this", "is", "a", "test"};
747 IEnumerator en = s1.GetEnumerator ();
748 AssertNotNull ("#G01", en);
750 Assert ("#G02", en.MoveNext ());
751 AssertEquals ("#G03", "this", en.Current);
752 Assert ("#G04", en.MoveNext ());
753 AssertEquals ("#G05", "is", en.Current);
754 Assert ("#G06", en.MoveNext ());
755 AssertEquals ("#G07", "a", en.Current);
756 Assert ("#G08", en.MoveNext ());
757 AssertEquals ("#G09", "test", en.Current);
758 Assert ("#G10", !en.MoveNext ());
761 Assert("#G11", en.MoveNext ());
762 AssertEquals ("#G12", "this", en.Current);
764 // mutation does not invalidate array enumerator!
765 s1.SetValue ("change", 1);
766 Assert ("#G13", en.MoveNext ());
767 AssertEquals ("#G14", "change", en.Current);
771 public void TestGetEnumeratorMultipleDimension() {
772 String[,] s1 = {{"this", "is"}, {"a", "test"}};
773 IEnumerator en = s1.GetEnumerator ();
774 AssertNotNull ("#AA01", en);
776 Assert ("#AA02", en.MoveNext ());
777 AssertEquals ("#AA03", "this", en.Current);
778 Assert ("#AA04", en.MoveNext ());
779 AssertEquals ("#AA05", "is", en.Current);
780 Assert ("#AA06", en.MoveNext ());
781 AssertEquals ("#AA07", "a", en.Current);
782 Assert ("#AA08", en.MoveNext ());
783 AssertEquals ("#AA09", "test", en.Current);
784 Assert ("#AA10", !en.MoveNext ());
787 Assert("#AA11", en.MoveNext ());
788 AssertEquals ("#AA12", "this", en.Current);
791 // mutation does not invalidate array enumerator!
792 s1.SetValue ("change", idxs);
793 Assert ("#AA13", en.MoveNext ());
794 AssertEquals ("#AA14", "change", en.Current);
798 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
799 public void TestGetEnumeratorNonZeroLowerBounds() {
800 int[] myLengthsArray = new int[2] { 3, 5 };
801 int[] myBoundsArray = new int[2] { 2, 3 };
803 Array myArray=Array.CreateInstance( typeof(String), myLengthsArray, myBoundsArray );
804 for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
805 for ( int j = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++ ) {
806 int[] myIndicesArray = new int[2] { i, j };
807 myArray.SetValue( Convert.ToString(i) + j, myIndicesArray );
809 IEnumerator en = myArray.GetEnumerator ();
810 AssertNotNull ("#AB01", en);
812 // check the first couple of values
813 Assert ("#AB02", en.MoveNext ());
814 AssertEquals ("#AB03", "23", en.Current);
815 Assert ("#AB04", en.MoveNext ());
816 AssertEquals ("#AB05", "24", en.Current);
818 // then check the last element's value
821 lastElement = (string)en.Current;
822 } while (en.MoveNext());
823 AssertEquals ("#AB06", "47", lastElement);
827 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
828 public void TestIList_Add () {
829 int[] myLengthsArray = new int[2] { 3, 5 };
830 int[] myBoundsArray = new int[2] { 2, 3 };
832 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
834 ((IList)myArray).Add ("can not");
835 Fail ("IList.Add should throw");
837 catch (NotSupportedException) {
841 Fail ("IList.Add threw wrong exception type");
844 Fail("IList.Add shouldn't get this far");
848 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
849 public void TestIList_Insert () {
850 int[] myLengthsArray = new int[2] { 3, 5 };
851 int[] myBoundsArray = new int[2] { 2, 3 };
853 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
855 ((IList)myArray).Insert (0, "can not");
856 Fail ("IList.Insert should throw");
858 catch (NotSupportedException) {
862 Fail ("IList.Insert threw wrong exception type");
865 Fail("IList.Insert shouldn't get this far");
869 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
870 public void TestIList_Remove () {
871 int[] myLengthsArray = new int[2] { 3, 5 };
872 int[] myBoundsArray = new int[2] { 2, 3 };
874 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
876 ((IList)myArray).Remove ("can not");
877 Fail ("IList.Remove should throw");
879 catch (NotSupportedException) {
883 Fail ("IList.Remove threw wrong exception type");
886 Fail("IList.Remove shouldn't get this far");
890 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
891 public void TestIList_RemoveAt () {
892 int[] myLengthsArray = new int[2] { 3, 5 };
893 int[] myBoundsArray = new int[2] { 2, 3 };
895 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
897 ((IList)myArray).RemoveAt (0);
898 Fail ("IList.RemoveAt should throw");
900 catch (NotSupportedException) {
904 Fail ("IList.RemoveAt threw wrong exception type");
907 Fail("IList.RemoveAt shouldn't get this far");
911 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
912 public void TestIList_Contains () {
913 int[] myLengthsArray = new int[2] { 3, 5 };
914 int[] myBoundsArray = new int[2] { 2, 3 };
916 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
919 bool b = ((IList)myArray).Contains ("23");
920 Fail("IList.Contains should throw with multi-dimensional arrays");
922 catch (RankException) {
923 int[] iArr = new int[3] { 1, 2, 3};
924 // check the first and last items
925 Assert("AC01", ((IList)iArr).Contains (1));
926 Assert("AC02", ((IList)iArr).Contains (3));
928 // and one that is definately not there
929 Assert("AC03", !((IList)iArr).Contains (42));
933 Fail("Should not get here");
937 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
938 public void TestIList_IndexOf () {
939 int[] myLengthsArray = new int[2] { 3, 5 };
940 int[] myBoundsArray = new int[2] { 2, 3 };
942 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
945 bool b = ((IList)myArray).Contains ("23");
946 Fail("IList.Contains should throw with multi-dimensional arrays");
948 catch (RankException) {
949 int[] iArr = new int[3] { 1, 2, 3};
950 // check the first and last items
951 AssertEquals("AD01", 0, ((IList)iArr).IndexOf (1));
952 AssertEquals("AD02", 2, ((IList)iArr).IndexOf (3));
954 // and one that is definately not there
955 AssertEquals("AD03", -1, ((IList)iArr).IndexOf (42));
957 catch (Exception e) {
958 Fail("Unexpected exception: " + e.ToString());
961 // check that wierd case whem lowerbound is Int32.MinValue,
962 // so that IndexOf() needs to return Int32.MaxValue when it cannot find the object
963 int[] myLengthArray = new int[1] { 3 };
964 int[] myBoundArray = new int[1] { Int32.MinValue };
965 Array myExtremeArray=Array.CreateInstance ( typeof(String), myLengthArray, myBoundArray );
966 AssertEquals("AD04", Int32.MaxValue, ((IList)myExtremeArray).IndexOf (42));
971 public void TestGetLength() {
973 bool errorThrown = false;
975 char[] c1 = {'a', 'b', 'c'};
977 } catch (IndexOutOfRangeException) {
980 Assert("#H01", errorThrown);
983 bool errorThrown = false;
985 char[] c1 = {'a', 'b', 'c'};
987 } catch (IndexOutOfRangeException) {
990 Assert("#H02", errorThrown);
993 char[] c2 = new Char[5];
994 AssertEquals("#H03", 5, c2.GetLength(0));
996 char[,] c3 = new Char[6,7];
997 AssertEquals("#H04", 6, c3.GetLength(0));
998 AssertEquals("#H05", 7, c3.GetLength(1));
1002 public void TestGetLowerBound() {
1004 bool errorThrown = false;
1006 char[] c = {'a', 'b', 'c'};
1007 c.GetLowerBound(-1);
1008 } catch (IndexOutOfRangeException) {
1011 Assert("#H31", errorThrown);
1014 bool errorThrown = false;
1016 char[] c = {'a', 'b', 'c'};
1018 } catch (IndexOutOfRangeException) {
1021 Assert("#H32", errorThrown);
1024 char[] c1 = new Char[5];
1025 AssertEquals("#H33", 0, c1.GetLowerBound(0));
1027 char[,] c2 = new Char[4,4];
1028 AssertEquals("#H34", 0, c2.GetLowerBound(0));
1029 AssertEquals("#H35", 0, c2.GetLowerBound(1));
1033 public void TestGetUpperBound() {
1035 bool errorThrown = false;
1037 char[] c = {'a', 'b', 'c'};
1038 c.GetUpperBound(-1);
1039 } catch (IndexOutOfRangeException) {
1042 Assert("#H61", errorThrown);
1045 bool errorThrown = false;
1047 char[] c = {'a', 'b', 'c'};
1049 } catch (IndexOutOfRangeException) {
1052 Assert("#H62", errorThrown);
1055 char[] c1 = new Char[5];
1056 AssertEquals("#H63", 4, c1.GetUpperBound(0));
1058 char[,] c2 = new Char[4,6];
1059 AssertEquals("#H64", 3, c2.GetUpperBound(0));
1060 AssertEquals("#H65", 5, c2.GetUpperBound(1));
1064 public void TestGetValue1() {
1066 bool errorThrown = false;
1068 char[,] c = new Char[2,2];
1070 } catch (ArgumentException) {
1073 Assert("#I01", errorThrown);
1076 bool errorThrown = false;
1078 char[] c = {'a', 'b', 'c'};
1080 } catch (IndexOutOfRangeException) {
1083 Assert("#I02", errorThrown);
1086 bool errorThrown = false;
1088 char[] c = {'a', 'b', 'c'};
1090 } catch (IndexOutOfRangeException) {
1093 Assert("#I03", errorThrown);
1096 char[] c1 = {'a', 'b', 'c', 'd'};
1097 for (int i = 0; i < c1.Length; i++) {
1098 AssertEquals("#I04(" + i + ")", c1[i], c1.GetValue(i));
1103 public void TestGetValue2() {
1105 bool errorThrown = false;
1107 char[] c = new Char[2];
1109 } catch (ArgumentException) {
1112 Assert("#I21", errorThrown);
1115 bool errorThrown = false;
1117 char[,] c = new Char[2,2];
1119 } catch (IndexOutOfRangeException) {
1122 Assert("#I22", errorThrown);
1125 bool errorThrown = false;
1127 char[,] c = new Char[2,2];
1129 } catch (IndexOutOfRangeException) {
1132 Assert("#I23", errorThrown);
1135 char[,] c1 = new Char[4,6];
1136 for (int i = 0; i < 24; i++) {
1139 c1[first,second] = (char)(((int)'a')+i);
1141 for (int i = 0; i < c1.GetLength(0); i++) {
1142 for (int j = 0; j < c1.GetLength(1); j++) {
1143 AssertEquals("#I24(" + i + "," + j + ")",
1144 c1[i,j], c1.GetValue(i, j));
1150 public void TestGetValue3() {
1152 bool errorThrown = false;
1154 char[] c = new Char[2];
1156 } catch (ArgumentException) {
1159 Assert("#I41", errorThrown);
1162 bool errorThrown = false;
1164 char[,,] c = new Char[2,2,2];
1165 c.GetValue(-1, 1, 1);
1166 } catch (IndexOutOfRangeException) {
1169 Assert("#I42", errorThrown);
1172 bool errorThrown = false;
1174 char[,,] c = new Char[2,2,2];
1176 } catch (IndexOutOfRangeException) {
1179 Assert("#I43", errorThrown);
1182 char[,,] c1 = new Char[4,2,3];
1183 for (int i = 0; i < 24; i++) {
1185 int remains = i % 6;
1186 int second = remains / 3;
1187 int third = remains % 3;
1188 c1[first,second, third] = (char)(((int)'a')+i);
1190 for (int i = 0; i < c1.GetLength(0); i++) {
1191 for (int j = 0; j < c1.GetLength(1); j++) {
1192 for (int k = 0; k < c1.GetLength(2); k++) {
1193 AssertEquals("#I44(" + i + "," + j + ")",
1194 c1[i,j,k], c1.GetValue(i,j,k));
1202 [ExpectedException (typeof (ArgumentNullException))]
1204 [ExpectedException (typeof (NullReferenceException))]
1206 public void TestGetValueLongArray ()
1208 char[] c = new Char[2];
1209 c.GetValue((long [])null);
1213 public void TestGetValueN() {
1215 bool errorThrown = false;
1217 char[] c = new Char[2];
1218 c.GetValue((int [])null);
1219 } catch (ArgumentNullException) {
1222 Assert("#I61a", errorThrown);
1225 bool errorThrown = false;
1227 char[] c = new Char[2];
1228 int[] coords = {1, 1};
1230 } catch (ArgumentException) {
1233 Assert("#I62", errorThrown);
1236 bool errorThrown = false;
1238 char[,] c = new Char[2,2];
1239 int[] coords = {-1, 1};
1241 } catch (IndexOutOfRangeException) {
1244 Assert("#I63", errorThrown);
1247 bool errorThrown = false;
1249 char[,] c = new Char[2,2];
1250 int[] coords = {4, 1};
1252 } catch (IndexOutOfRangeException) {
1255 Assert("#I64", errorThrown);
1258 char[,] c1 = new Char[4,6];
1259 for (int i = 0; i < 24; i++) {
1262 c1[first,second] = (char)(((int)'a')+i);
1264 for (int i = 0; i < c1.GetLength(0); i++) {
1265 for (int j = 0; j < c1.GetLength(1); j++) {
1266 int[] coords = {i, j};
1267 AssertEquals("#I65(" + i + "," + j + ")",
1268 c1[i,j], c1.GetValue(coords));
1274 public void TestIndexOf1() {
1276 bool errorThrown = false;
1278 Array.IndexOf(null, "huh?");
1279 } catch (ArgumentNullException) {
1282 Assert("#J01", errorThrown);
1285 bool errorThrown = false;
1287 char[,] c = new Char[2,2];
1288 Array.IndexOf(c, "huh?");
1289 } catch (RankException) {
1292 Assert("#J02", errorThrown);
1295 String[] s1 = {"this", "is", "a", "test"};
1296 AssertEquals("#J03", -1, Array.IndexOf(s1, null));
1297 AssertEquals("#J04", -1, Array.IndexOf(s1, "nothing"));
1298 AssertEquals("#J05", 0, Array.IndexOf(s1, "this"));
1299 AssertEquals("#J06", 3, Array.IndexOf(s1, "test"));
1303 public void TestIndexOf2() {
1305 bool errorThrown = false;
1307 Array.IndexOf(null, "huh?", 0);
1308 } catch (ArgumentNullException) {
1311 Assert("#J21", errorThrown);
1314 bool errorThrown = false;
1316 char[,] c = new Char[2,2];
1317 Array.IndexOf(c, "huh?", 0);
1318 } catch (RankException) {
1321 Assert("#J22", errorThrown);
1324 bool errorThrown = false;
1326 char[] c = new Char[2];
1327 Array.IndexOf(c, "huh?", 3);
1328 } catch (ArgumentOutOfRangeException) {
1331 Assert("#J23", errorThrown);
1334 String[] s1 = {"this", "is", "really", "a", "test"};
1335 AssertEquals("#J24", -1, Array.IndexOf(s1, null, 1));
1336 AssertEquals("#J25", -1, Array.IndexOf(s1, "nothing", 1));
1337 AssertEquals("#J26", -1, Array.IndexOf(s1, "this", 1));
1338 AssertEquals("#J27", 1, Array.IndexOf(s1, "is", 1));
1339 AssertEquals("#J28", 4, Array.IndexOf(s1, "test", 1));
1343 public void TestIndexOf3() {
1345 bool errorThrown = false;
1347 Array.IndexOf(null, "huh?", 0, 1);
1348 } catch (ArgumentNullException) {
1351 Assert("#J41", errorThrown);
1354 bool errorThrown = false;
1356 char[,] c = new Char[2,2];
1357 Array.IndexOf(c, "huh?", 0, 1);
1358 } catch (RankException) {
1361 Assert("#J42", errorThrown);
1364 bool errorThrown = false;
1366 char[] c = new Char[2];
1367 Array.IndexOf(c, "huh?", 3, 1);
1368 } catch (ArgumentOutOfRangeException) {
1371 Assert("#J43", errorThrown);
1374 bool errorThrown = false;
1376 char[] c = new Char[2];
1377 Array.IndexOf(c, "huh?", 0, 5);
1378 } catch (ArgumentOutOfRangeException) {
1381 Assert("#J44", errorThrown);
1384 String[] s1 = {"this", "is", "really", "a", "test"};
1385 AssertEquals("#J45", -1, Array.IndexOf(s1, null, 1, 3));
1386 AssertEquals("#J46", -1, Array.IndexOf(s1, "nothing", 1, 3));
1387 AssertEquals("#J47", -1, Array.IndexOf(s1, "this", 1, 3));
1388 AssertEquals("#J48", 1, Array.IndexOf(s1, "is", 1, 3));
1389 AssertEquals("#J49", -1, Array.IndexOf(s1, "test", 1, 3));
1390 AssertEquals("#J50", 3, Array.IndexOf(s1, "a", 1, 3));
1394 public void TestIndexOf_CustomEqual ()
1396 DataEqual[] test = new DataEqual [] { new DataEqual () };
1397 AssertEquals (0, Array.IndexOf (test, "asdfas", 0));
1399 IList array = (IList)test;
1400 AssertEquals (0, array.IndexOf ("asdfas"));
1404 public void TestLastIndexOf1() {
1406 bool errorThrown = false;
1408 Array.LastIndexOf(null, "huh?");
1409 } catch (ArgumentNullException) {
1412 Assert("#K01", errorThrown);
1415 bool errorThrown = false;
1417 char[,] c = new Char[2,2];
1418 Array.LastIndexOf(c, "huh?");
1419 } catch (RankException) {
1422 Assert("#K02", errorThrown);
1425 String[] s1 = {"this", "is", "a", "a", "test"};
1426 AssertEquals("#K03", -1, Array.LastIndexOf(s1, null));
1427 AssertEquals("#K04", -1, Array.LastIndexOf(s1, "nothing"));
1428 AssertEquals("#K05", 0, Array.LastIndexOf(s1, "this"));
1429 AssertEquals("#K06", 4, Array.LastIndexOf(s1, "test"));
1430 AssertEquals("#K07", 3, Array.LastIndexOf(s1, "a"));
1432 AssertEquals (-1, Array.LastIndexOf (new String [0], "foo"));
1436 public void TestLastIndexOf2() {
1438 bool errorThrown = false;
1440 Array.LastIndexOf(null, "huh?", 0);
1441 } catch (ArgumentNullException) {
1444 Assert("#K21", errorThrown);
1447 bool errorThrown = false;
1449 char[,] c = new Char[2,2];
1450 Array.LastIndexOf(c, "huh?", 0);
1451 } catch (RankException) {
1454 Assert("#K22", errorThrown);
1457 bool errorThrown = false;
1459 char[] c = new Char[2];
1460 Array.LastIndexOf(c, "huh?", 3);
1461 } catch (ArgumentOutOfRangeException) {
1464 Assert("#K23", errorThrown);
1467 String[] s1 = {"this", "is", "really", "a", "test"};
1468 AssertEquals("#K24", -1, Array.LastIndexOf(s1, null, 3));
1469 AssertEquals("#K25", -1, Array.LastIndexOf(s1, "nothing", 3));
1470 AssertEquals("#K26", -1, Array.LastIndexOf(s1, "test", 3));
1471 AssertEquals("#K27", 3, Array.LastIndexOf(s1, "a", 3));
1472 AssertEquals("#K28", 0, Array.LastIndexOf(s1, "this", 3));
1476 public void TestLastIndexOf3() {
1478 bool errorThrown = false;
1480 Array.LastIndexOf(null, "huh?", 0, 1);
1481 } catch (ArgumentNullException) {
1484 Assert("#K41", errorThrown);
1487 bool errorThrown = false;
1489 char[,] c = new Char[2,2];
1490 Array.LastIndexOf(c, "huh?", 0, 1);
1491 } catch (RankException) {
1494 Assert("#K42", errorThrown);
1497 bool errorThrown = false;
1499 char[] c = new Char[2];
1500 Array.LastIndexOf(c, "huh?", 3, 1);
1501 } catch (ArgumentOutOfRangeException) {
1504 Assert("#K43", errorThrown);
1507 bool errorThrown = false;
1509 char[] c = new Char[2];
1510 Array.LastIndexOf(c, "huh?", 0, 5);
1511 } catch (ArgumentOutOfRangeException) {
1514 Assert("#K44", errorThrown);
1517 String[] s1 = {"this", "is", "really", "a", "test"};
1518 AssertEquals("#K45", -1, Array.LastIndexOf(s1, null, 3, 3));
1519 AssertEquals("#K46", -1, Array.LastIndexOf(s1, "nothing", 3, 3));
1520 AssertEquals("#K47", -1, Array.LastIndexOf(s1, "this", 3, 3));
1521 AssertEquals("#K48", 1, Array.LastIndexOf(s1, "is", 3, 3));
1522 AssertEquals("#K49", -1, Array.LastIndexOf(s1, "test", 3, 3));
1523 AssertEquals("#K50", 3, Array.LastIndexOf(s1, "a", 3, 3));
1527 public void TestLastIndexOf4 ()
1529 short [] a = new short [] { 19, 238, 317, 6, 565, 0, -52, 60, -563, 753, 238, 238};
1531 Array.LastIndexOf (a, (object)16, -1);
1532 NUnit.Framework.Assert.Fail ("#1");
1533 } catch (ArgumentOutOfRangeException) { }
1537 Array.LastIndexOf<short> (a, 16, -1);
1538 NUnit.Framework.Assert.Fail ("#2");
1539 } catch (ArgumentOutOfRangeException) { }
1544 public void TestLastIndexOf5 ()
1546 char [] a = new char [] {'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'j', 'i', 'h'};
1551 for (int i = a.Length - 1; i >= 0 ; i--) {
1553 retval = Array.LastIndexOf(a, a [i], i, i + 1);
1561 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1562 public void LastIndexOf_StartIndexOverflow ()
1564 // legal - no exception
1565 byte[] array = new byte [16];
1566 Array.LastIndexOf (array, this, Int32.MaxValue, 1);
1570 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1571 public void LastIndexOf_CountOverflow ()
1573 // legal - no exception
1574 byte[] array = new byte [16];
1575 Array.LastIndexOf (array, this, 1, Int32.MaxValue);
1579 public void TestReverse() {
1581 bool errorThrown = false;
1583 Array.Reverse(null);
1584 } catch (ArgumentNullException) {
1587 Assert("#L01", errorThrown);
1590 bool errorThrown = false;
1592 char[,] c = new Char[2,2];
1594 } catch (RankException) {
1597 Assert("#L02", errorThrown);
1600 char[] c1 = {'a', 'b', 'c', 'd'};
1602 AssertEquals("#L03", 'd', c1[0]);
1603 AssertEquals("#L04", 'c', c1[1]);
1604 AssertEquals("#L05", 'b', c1[2]);
1605 AssertEquals("#L06", 'a', c1[3]);
1608 bool errorThrown = false;
1610 Array.Reverse(null, 0, 0);
1611 } catch (ArgumentNullException) {
1614 Assert("#L07", errorThrown);
1617 bool errorThrown = false;
1619 char[,] c = new Char[2,2];
1620 Array.Reverse(c, 0, 0);
1621 } catch (RankException) {
1624 Assert("#L08", errorThrown);
1627 //bool errorThrown = false;
1629 // char[] c = new Char[2];
1630 // Array.Reverse(c, 0, 3);
1631 //} catch (ArgumentOutOfRangeException) {
1632 // errorThrown = true;
1634 //Assert("#L09", errorThrown);
1637 //bool errorThrown = false;
1639 // char[] c = new Char[2];
1640 // Array.Reverse(c, 3, 0);
1641 //} catch (ArgumentOutOfRangeException) {
1642 // errorThrown = true;
1644 //Assert("#L10", errorThrown);
1647 char[] c2 = { 'a', 'b', 'c', 'd'};
1648 Array.Reverse(c2, 1, 2);
1649 AssertEquals("#L11", 'a', c2[0]);
1650 AssertEquals("#L12", 'c', c2[1]);
1651 AssertEquals("#L13", 'b', c2[2]);
1652 AssertEquals("#L14", 'd', c2[3]);
1656 public void TestSetValue1() {
1658 bool errorThrown = false;
1660 char[,] c = new Char[2,2];
1661 c.SetValue("buh", 1);
1662 } catch (ArgumentException) {
1665 Assert("#M01", errorThrown);
1668 bool errorThrown = false;
1670 char[] c = {'a', 'b', 'c'};
1671 c.SetValue("buh", -1);
1672 } catch (IndexOutOfRangeException) {
1675 Assert("#M02", errorThrown);
1678 bool errorThrown = false;
1680 char[] c = {'a', 'b', 'c'};
1681 c.SetValue("buh", 4);
1682 } catch (IndexOutOfRangeException) {
1685 Assert("#M03", errorThrown);
1688 char[] c1 = {'a', 'b', 'c', 'd'};
1689 char[] c2 = new char[4];
1690 for (int i = 0; i < c1.Length; i++) {
1691 c2.SetValue(c1[i], i);
1693 for (int i = 0; i < c1.Length; i++) {
1694 AssertEquals("#M04(" + i + ")", c1[i], c2[i]);
1697 int[] c3 = { 1, 2, 3 };
1698 long[] c4 = new long [3];
1700 for (int i = 0; i < c3.Length; i++)
1701 c4.SetValue (c3 [i], i);
1705 } catch (Exception e) {
1706 Fail ("c3.CopyTo(): e=" + e);
1708 for (int i = 0; i < c3.Length; i++)
1709 Assert ("#M05(" + i + ")", c3[i] == c4[i]);
1711 Object[] c5 = new Object [3];
1712 long[] c6 = new long [3];
1716 } catch (Exception e) {
1717 Fail ("c4.CopyTo(): e=" + e);
1722 } catch (Exception e) {
1723 Fail ("c5.CopyTo(): e=" + e);
1725 // for (int i = 0; i < c5.Length; i++)
1726 // Assert ("#M06(" + i + ")", c5[i] == c6[i]);
1730 public void TestSetValue2() {
1732 bool errorThrown = false;
1734 char[] c = new Char[2];
1735 c.SetValue("buh", 1,1);
1736 } catch (ArgumentException) {
1739 Assert("#M21", errorThrown);
1742 bool errorThrown = false;
1744 char[,] c = new Char[2,2];
1745 c.SetValue("buh", -1, 1);
1746 } catch (IndexOutOfRangeException) {
1749 Assert("#M22", errorThrown);
1752 bool errorThrown = false;
1754 char[,] c = new Char[2,2];
1755 c.SetValue("buh", 4,1);
1756 } catch (IndexOutOfRangeException) {
1759 Assert("#M23", errorThrown);
1762 char[,] c1 = new Char[4,6];
1763 char[,] c2 = new Char[4,6];
1764 for (int i = 0; i < 24; i++) {
1767 c1[first,second] = (char)(((int)'a')+i);
1768 c2.SetValue(c1[first,second], first, second);
1770 for (int i = 0; i < c1.GetLength(0); i++) {
1771 for (int j = 0; j < c1.GetLength(1); j++) {
1772 AssertEquals("#M24(" + i + "," + j + ")",
1779 public void TestSetValue3() {
1781 bool errorThrown = false;
1783 char[] c = new Char[2];
1784 c.SetValue("buh", 1,1,1);
1785 } catch (ArgumentException) {
1788 Assert("#M41", errorThrown);
1791 bool errorThrown = false;
1793 char[,,] c = new Char[2,2,2];
1794 c.SetValue("buh", -1, 1, 1);
1795 } catch (IndexOutOfRangeException) {
1798 Assert("#M42", errorThrown);
1801 bool errorThrown = false;
1803 char[,,] c = new Char[2,2,2];
1804 c.SetValue("buh", 4,1,1);
1805 } catch (IndexOutOfRangeException) {
1808 Assert("#M43", errorThrown);
1811 char[,,] c1 = new Char[4,2,3];
1812 char[,,] c2 = new Char[4,2,3];
1813 for (int i = 0; i < 24; i++) {
1815 int remains = i % 6;
1816 int second = remains / 3;
1817 int third = remains % 3;
1818 c1[first,second, third] = (char)(((int)'a')+i);
1819 c2.SetValue(c1[first, second, third], first, second, third);
1821 for (int i = 0; i < c1.GetLength(0); i++) {
1822 for (int j = 0; j < c1.GetLength(1); j++) {
1823 for (int k = 0; k < c1.GetLength(2); k++) {
1824 AssertEquals("#M44(" + i + "," + j + " )",
1825 c1[i,j,k], c2[i,j,k]);
1833 [ExpectedException (typeof (ArgumentNullException))]
1835 [ExpectedException (typeof (NullReferenceException))]
1837 public void TestSetValueLongArray ()
1839 char[] c = new Char[2];
1840 c.SetValue("buh", (long [])null);
1844 public void TestSetValueN() {
1846 bool errorThrown = false;
1848 char[] c = new Char[2];
1849 c.SetValue("buh", (int [])null);
1850 } catch (ArgumentNullException) {
1853 Assert("#M61a", errorThrown);
1856 bool errorThrown = false;
1858 char[] c = new Char[2];
1859 int[] coords = {1, 1};
1860 c.SetValue("buh", coords);
1861 } catch (ArgumentException) {
1864 Assert("#M62", errorThrown);
1867 bool errorThrown = false;
1869 char[,] c = new Char[2,2];
1870 int[] coords = {-1, 1};
1871 c.SetValue("buh", coords);
1872 } catch (IndexOutOfRangeException) {
1875 Assert("#M63", errorThrown);
1878 bool errorThrown = false;
1880 char[,] c = new Char[2,2];
1881 int[] coords = {4, 1};
1882 c.SetValue("buh", coords);
1883 } catch (IndexOutOfRangeException) {
1886 Assert("#M64", errorThrown);
1889 char[,] c1 = new Char[4,6];
1890 char[,] c2 = new Char[4,6];
1891 for (int i = 0; i < 24; i++) {
1894 c1[first,second] = (char)(((int)'a')+i);
1895 int[] coords = {first, second};
1896 c2.SetValue(c1[first,second], coords);
1898 for (int i = 0; i < c1.GetLength(0); i++) {
1899 for (int j = 0; j < c1.GetLength(1); j++) {
1900 AssertEquals("#M65(" + i + "," + j + ")",
1907 public void TestSetValue4() {
1909 int[] c1 = { 1, 2, 3 };
1910 long[] c2 = new long [3];
1912 for (int i = 0; i < c1.Length; i++)
1913 c2.SetValue (c1 [i], i);
1915 for (int i = 0; i < c1.Length; i++) {
1916 Assert ("#M81(" + i + ")", c1[i] == c2[i]);
1917 AssertEquals ("#M82(" + i + ")", typeof (long), c2[i].GetType ());
1921 long[] c1 = { 1, 2, 3 };
1922 int[] c2 = new int [3];
1923 bool errorThrown = false;
1925 c2.SetValue (c1 [0], 0);
1926 } catch (ArgumentException) {
1929 Assert("#M83", errorThrown);
1932 int[] c1 = { 1, 2, 3 };
1933 Object[] c2 = new Object [3];
1935 for (int i = 0; i < c1.Length; i++)
1936 c2.SetValue (c1 [i], i);
1938 for (int i = 0; i < c1.Length; i++)
1939 AssertEquals ("#M84(" + i + ")", c1[i], Convert.ToInt32 (c2[i]));
1942 Object[] c1 = new Object [3];
1943 Object[] c2 = new Object [3];
1944 c1[0] = new Object ();
1946 for (int i = 0; i < c1.Length; i++)
1947 c2.SetValue (c1 [i], i);
1949 for (int i = 0; i < c1.Length; i++)
1950 AssertEquals ("#M85(" + i + ")", c1[i], c2[i]);
1953 Object[] c1 = new Object [3];
1954 string[] c2 = new String [3];
1955 string test = "hello";
1958 c2.SetValue (c1 [0], 0);
1959 AssertEquals ("#M86", c1[0], c2[0]);
1960 AssertEquals ("#M87", "hello", c2[0]);
1963 char[] c1 = { 'a', 'b', 'c' };
1964 string[] c2 = new string [3];
1966 c2.SetValue (c1 [0], 0);
1968 } catch (InvalidCastException) {}
1971 Single[] c1 = { 1.2F, 2.3F, 3.4F, 4.5F };
1972 long[] c2 = new long [3];
1974 c2.SetValue (c1 [0], 0);
1976 } catch (ArgumentException) {}
2004 UInt32 v11 = 235354;
2005 UInt64 v12 = 234552;
2007 Object[] va1 = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12 };
2008 Object[] va2 = { "true", "1", "a", "-1.2", "-32", "-234", "-34523", "-1",
2009 "-4.8F", "24234", "235354", "234552" };
2011 Object[][] vt = { va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1 };
2014 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2015 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
2016 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
2017 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
2018 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1,
2019 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1,
2020 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,
2021 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1,
2022 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1,
2023 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
2024 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0,
2025 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0
2030 for (int i = 0; i < types.Length; i++) {
2031 for (int j = 0; j < types.Length; j++) {
2032 Array array = Array.CreateInstance (types [j], 2);
2034 Object value = vt[j][i];
2036 bool errorThrown = false;
2038 array.SetValue (value, 0);
2039 } catch (ArgumentException) {
2043 int ex_index = (i * types.Length) + j;
2045 AssertEquals ("#M90(" + types [i] + "," + types [j] + ")",
2046 errorThrown, arg_ex [ex_index] == 1);
2050 for (int i = 0; i < types.Length; i++) {
2051 String[] array = new String [2];
2053 Object value = va1 [i];
2055 bool errorThrown = false;
2057 array.SetValue (value, 0);
2058 } catch (InvalidCastException) {
2062 Assert ("#M91(" + types [i] + ")", errorThrown);
2065 for (int i = 0; i < types.Length; i++) {
2066 Array array = Array.CreateInstance (types [i], 2);
2068 Object value = va2 [i];
2070 bool errorThrown = false;
2072 array.SetValue (value, 0);
2073 } catch (InvalidCastException) {
2077 Assert ("#M92(" + types [i] + ")", errorThrown);
2080 for (int i = 0; i < types.Length; i++) {
2081 Array array = Array.CreateInstance (types [i], 2);
2083 Object value = null;
2085 bool errorThrown = false;
2087 array.SetValue (value, 0);
2088 } catch (InvalidCastException) {
2092 Assert ("#M93(" + types [i] + ")", !errorThrown);
2097 for (int i = 0; i < types.Length; i++) {
2098 for (int j = 0; j < types.Length; j++) {
2099 Array source = Array.CreateInstance (types [i], 2);
2100 Array array = Array.CreateInstance (types [j], 2);
2102 source.SetValue (vt[j][i], 0);
2103 source.SetValue (vt[j][i], 1);
2105 bool errorThrown = false;
2107 Array.Copy (source, array, 2);
2108 } catch (ArrayTypeMismatchException) {
2112 int ex_index = (i * types.Length) + j;
2114 AssertEquals ("#M94(" + types [i] + "," + types [j] + ")",
2115 errorThrown, arg_ex [ex_index] == 1);
2119 for (int i = 0; i < types.Length; i++) {
2120 Array source = Array.CreateInstance (types [i], 2);
2121 String[] array = new String [2];
2123 source.SetValue (va1 [i], 0);
2124 source.SetValue (va1 [i], 1);
2126 bool errorThrown = false;
2128 Array.Copy (source, array, 2);
2129 } catch (ArrayTypeMismatchException) {
2133 Assert ("#M95(" + types [i] + ")", errorThrown);
2136 for (int i = 0; i < types.Length; i++) {
2137 String[] source = new String [2];
2138 Array array = Array.CreateInstance (types [i], 2);
2140 source.SetValue (va2 [i], 0);
2141 source.SetValue (va2 [i], 1);
2143 bool errorThrown = false;
2145 Array.Copy (source, array, 2);
2146 } catch (ArrayTypeMismatchException) {
2150 Assert ("#M96(" + types [i] + ")", errorThrown);
2156 public void TestSort() {
2158 bool errorThrown = false;
2161 } catch (ArgumentNullException) {
2164 Assert("#N01", errorThrown);
2167 bool errorThrown = false;
2169 Array.Sort(null, 0, 1);
2170 } catch (ArgumentNullException) {
2173 Assert("#N02", errorThrown);
2176 bool errorThrown = false;
2178 char[] c1 = new Char[2];
2179 Array.Sort(null, c1);
2180 } catch (ArgumentNullException) {
2183 Assert("#N03", errorThrown);
2186 bool errorThrown = false;
2188 char[] c1 = new Char[2];
2189 Array.Sort(null, c1, 0, 1);
2190 } catch (ArgumentNullException) {
2193 Assert("#N04", errorThrown);
2197 char[] arr = {'d', 'b', 'f', 'e', 'a', 'c'};
2200 Array.Sort (null, 0, 1);
2201 Fail ("#N" + tc.ToString ());
2203 catch (ArgumentException) {}
2204 catch (Exception) { Fail ("#N" + tc.ToString ()); }
2208 Array.Sort (arr, -1, 3);
2209 Fail ("#N" + tc.ToString ());
2211 catch (ArgumentException) {}
2212 catch (Exception) { Fail ("#N" + tc.ToString ()); }
2216 Array.Sort (arr, 1, -3);
2217 Fail ("#N" + tc.ToString ());
2219 catch (ArgumentException) {}
2220 catch (Exception) { Fail ("#N" + tc.ToString ()); }
2224 Array.Sort (arr, arr.Length, arr.Length + 2);
2225 Fail ("#N" + tc.ToString ());
2227 catch (ArgumentException) {}
2228 catch (Exception) { Fail ("#N" + tc.ToString ()); }
2231 // note: null second array => just sort first array
2232 char[] starter = {'d', 'b', 'f', 'e', 'a', 'c'};
2233 int[] starter1 = {1,2,3,4,5,6};
2235 char[] c1 = (char[])starter.Clone();
2237 AssertEquals("#N21", 'a', c1[0]);
2238 AssertEquals("#N22", 'b', c1[1]);
2239 AssertEquals("#N23", 'c', c1[2]);
2240 AssertEquals("#N24", 'd', c1[3]);
2241 AssertEquals("#N25", 'e', c1[4]);
2242 AssertEquals("#N26", 'f', c1[5]);
2245 char[] c1 = (char[])starter.Clone();
2246 int[] i1 = (int[])starter1.Clone();
2248 AssertEquals("#N41", 'a', c1[0]);
2249 AssertEquals("#N42", 'b', c1[1]);
2250 AssertEquals("#N43", 'c', c1[2]);
2251 AssertEquals("#N44", 'd', c1[3]);
2252 AssertEquals("#N45", 'e', c1[4]);
2253 AssertEquals("#N46", 'f', c1[5]);
2254 AssertEquals("#N47", 5, i1[0]);
2255 AssertEquals("#N48", 2, i1[1]);
2256 AssertEquals("#N49", 6, i1[2]);
2257 AssertEquals("#N50", 1, i1[3]);
2258 AssertEquals("#N51", 4, i1[4]);
2259 AssertEquals("#N52", 3, i1[5]);
2262 char[] c1 = (char[])starter.Clone();
2263 Array.Sort(c1, 1, 4);
2264 AssertEquals("#N61", 'd', c1[0]);
2265 AssertEquals("#N62", 'a', c1[1]);
2266 AssertEquals("#N63", 'b', c1[2]);
2267 AssertEquals("#N64", 'e', c1[3]);
2268 AssertEquals("#N65", 'f', c1[4]);
2269 AssertEquals("#N66", 'c', c1[5]);
2272 char[] c1 = (char[])starter.Clone();
2273 int[] i1 = (int[])starter1.Clone();
2274 Array.Sort(c1, i1, 1, 4);
2275 AssertEquals("#N81", 'd', c1[0]);
2276 AssertEquals("#N82", 'a', c1[1]);
2277 AssertEquals("#N83", 'b', c1[2]);
2278 AssertEquals("#N84", 'e', c1[3]);
2279 AssertEquals("#N85", 'f', c1[4]);
2280 AssertEquals("#N86", 'c', c1[5]);
2281 AssertEquals("#N87", 1, i1[0]);
2282 AssertEquals("#N88", 5, i1[1]);
2283 AssertEquals("#N89", 2, i1[2]);
2284 AssertEquals("#N90", 4, i1[3]);
2285 AssertEquals("#N91", 3, i1[4]);
2286 AssertEquals("#N92", 6, i1[5]);
2291 public void TestInitializeEmpty()
2303 Assert("#TI01",!catched);
2307 public void TestInitializeInt()
2312 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2314 AssertEquals("#TI02 " + i ,a[i],b[i]);
2319 public void TestInitializeDouble()
2321 double[] a = {1.0,2.0,0.0};
2323 double[] b = {1.0,2.0,0.0};
2324 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2326 AssertEquals("#TI03 " + i ,a[i],b[i]);
2331 public void TestInitializeFloat()
2333 float[] a = {1.0F,2.0F,0.0F};
2335 float[] b = {1.0F,2.0F,0.0F};
2336 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2338 AssertEquals("#TI04 " + i ,a[i],b[i]);
2343 public void TestInitializeChar()
2345 char[] a = {'1','.','0','F','2','.','0','F'};
2347 char[] b = {'1','.','0','F','2','.','0','F'};
2348 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2350 AssertEquals("#TI05 " + i ,a[i],b[i]);
2355 public void TestInitializeString()
2357 string[] a = {"hola","adios","menos","mas"};
2359 string[] b = {"hola","adios","menos","mas"};
2360 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2362 AssertEquals("#TI06 " + i ,a[i],b[i]);
2367 public void TestInitializeEnum()
2369 enua[] a = {enua.hola,enua.adios,enua.menos,enua.mas};
2371 enua[] b = {enua.hola,enua.adios,enua.menos,enua.mas};
2372 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2374 AssertEquals("#TI07 " + i ,a[i],b[i]);
2379 public void TestInitializeIntNI()
2381 int[] a = new int[20];
2385 AssertEquals("#TI08",b,0);
2390 public void TestInitializeCharNI()
2392 char[] a = new char[20];
2394 foreach(char b in a)
2396 AssertEquals("#TI09",b,0);
2401 public void TestInitializeDoubleNI()
2403 double[] a = new double[20];
2405 foreach(double b in a)
2407 AssertEquals("#TI09",b,0.0);
2412 public void TestInitializeStringNI()
2414 string[] a = new string[20];
2416 foreach(string b in a)
2418 AssertEquals("#TI10",b,null);
2423 public void TestInitializeObjectNI()
2425 object[] a = new object[20];
2427 foreach(object b in a)
2429 AssertEquals("#TI11",b,null);
2434 public void TestInitializeAClassNI()
2436 AClass[] a = new AClass[20];
2438 foreach(AClass b in a)
2440 AssertEquals("#TI12",b,null);
2446 public void TestInitializeAStructNI()
2448 AStruct[] a = new AStruct[20];
2450 foreach(AStruct b in a)
2452 AssertEquals("#TI14",b,new AStruct());
2457 public void TestInitializeAStruct()
2459 AStruct[] a = new AStruct[3];
2463 AStruct[] b = new AStruct[3];
2466 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2468 AssertEquals("#TI15 " + i ,a[i],b[i]);
2473 public void TestInitializeDateTimeNI()
2475 DateTime[] a = new DateTime[20];
2477 foreach(DateTime b in a)
2479 AssertEquals("#TI16",b,new DateTime());
2484 [ExpectedException (typeof (ArgumentNullException))]
2485 public void MoreSort1 ()
2487 Array.Sort (null, 0, 1);
2491 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2492 public void MoreSort2 ()
2494 Array.Sort (arrsort, -1, 3);
2498 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2499 public void MoreSort3 ()
2501 Array.Sort (arrsort, 1, -3);
2505 [ExpectedException (typeof (ArgumentException))]
2506 public void MoreSort4 ()
2508 Array.Sort (arrsort, arrsort.Length, arrsort.Length + 2);
2512 [ExpectedException (typeof (RankException))]
2513 public void MoreSort5 ()
2515 char [,] arr = new char [,] {{'a'}, {'b'}};
2516 Array.Sort (arr, 0, 1);
2520 public void MoreSort6 ()
2522 Array.Sort (arrsort, 0, 0);
2526 [ExpectedException (typeof (ArgumentException))]
2527 public void MoreSort7 ()
2529 Array.Sort (arrsort, arrsort.Length - 1, 2);
2533 [ExpectedException (typeof (ArgumentException))]
2534 public void MoreSort8 ()
2536 Array.Sort (arrsort, 0, arrsort.Length + 1);
2540 public void MoreSort9 ()
2542 Array.Sort (arrsort, null, 0, arrsort.Length, null);
2546 [ExpectedException (typeof (InvalidOperationException))]
2547 public void MoreSort10 ()
2549 object [] array = {true, 'k', SByte.MinValue, Byte.MinValue, (short) 2, 634, (long) 436, (float) 1.1, 1.23, "Hello World"};
2550 Array.Sort (array, (IComparer) null);
2553 [Test] // bug #81941
2556 double [] a = new double [2] { 0.9, 0.3 };
2557 uint [] b = new uint [2] { 4, 7 };
2559 AssertEquals ("#1", 0.3, a [0]);
2560 AssertEquals ("#2", 0.9, a [1]);
2561 AssertEquals ("#3", 7, b [0]);
2562 AssertEquals ("#4", 4, b [1]);
2566 public void ClearJaggedArray ()
2568 byte[][] matrix = new byte [8][];
2569 for (int i=0; i < 8; i++) {
2570 matrix [i] = new byte [8];
2571 for (int j=0; j < 8; j++) {
2575 Array.Clear (matrix, 0, 8);
2576 for (int i=0; i < 8; i++) {
2577 AssertNull (i.ToString (), matrix [i]);
2582 public void ClearMultidimentionalArray ()
2584 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2585 Array.Clear (matrix, 0, 2);
2586 AssertEquals ("0,0", 0, matrix [0,0]);
2587 AssertEquals ("0,1", 0, matrix [0,1]);
2588 AssertEquals ("1,0", 2, matrix [1,0]);
2589 AssertEquals ("1,1", 2, matrix [1,1]);
2593 [ExpectedException (typeof (IndexOutOfRangeException))]
2594 public void ClearOutsideMultidimentionalArray ()
2596 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2597 Array.Clear (matrix, 0, 5);
2601 [ExpectedException (typeof (IndexOutOfRangeException))]
2602 public void Clear_IndexOverflow ()
2604 byte[] array = new byte [16];
2605 Array.Clear (array, 4, Int32.MaxValue);
2609 [ExpectedException (typeof (IndexOutOfRangeException))]
2610 public void Clear_LengthOverflow ()
2612 byte[] array = new byte [16];
2613 Array.Clear (array, Int32.MaxValue, 4);
2617 [ExpectedException (typeof (ArgumentException))]
2618 public void Copy_SourceIndexOverflow ()
2620 byte[] array = new byte [16];
2621 Array.Copy (array, Int32.MaxValue, array, 8, 8);
2625 [ExpectedException (typeof (ArgumentException))]
2626 public void Copy_DestinationIndexOverflow ()
2628 byte[] array = new byte [16];
2629 Array.Copy (array, 8, array, Int32.MaxValue, 8);
2633 [ExpectedException (typeof (ArgumentException))]
2634 public void Copy_LengthOverflow ()
2636 byte[] array = new byte [16];
2637 Array.Copy (array, 8, array, 8, Int32.MaxValue);
2641 [ExpectedException (typeof (ArgumentException))]
2642 public void Reverse_IndexOverflow ()
2644 byte[] array = new byte [16];
2645 Array.Reverse (array, Int32.MaxValue, 8);
2649 [ExpectedException (typeof (ArgumentException))]
2650 public void Reverse_LengthOverflow ()
2652 byte[] array = new byte [16];
2653 Array.Reverse (array, 8, Int32.MaxValue);
2656 public struct CharX : IComparable {
2659 public CharX (char c)
2664 public int CompareTo (object obj)
2667 return c.CompareTo (((CharX) obj).c);
2669 return c.CompareTo (obj);
2674 public void BinarySearch_ArgPassingOrder ()
2677 // This tests that arguments are passed to the comprer in the correct
2678 // order. The IComparable of the *array* elements must get called, not
2679 // that of the search object.
2681 CharX [] x = { new CharX ('a'), new CharX ('b'), new CharX ('c') };
2682 AssertEquals (1, Array.BinarySearch (x, 'b'));
2685 class Comparer: IComparer {
2687 private bool called = false;
2689 public bool Called {
2691 bool result = called;
2697 public int Compare (object x, object y)
2705 public void BinarySearch1_EmptyList ()
2707 int[] array = new int[0];
2708 AssertEquals ("BinarySearch", - 1, Array.BinarySearch (array, 0));
2712 public void BinarySearch2_EmptyList ()
2714 int[] array = new int[0];
2715 AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, 0, 0));
2719 public void BinarySearch3_EmptyList ()
2721 Comparer comparer = new Comparer ();
2722 int[] array = new int[0];
2723 AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, comparer));
2724 // bug 77030 - the comparer isn't called for an empty array/list
2725 Assert ("Called", !comparer.Called);
2729 public void BinarySearch4_EmptyList ()
2731 Comparer comparer = new Comparer ();
2732 int[] array = new int[0];
2733 AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, 0, comparer));
2734 // bug 77030 - the comparer isn't called for an empty array/list
2735 Assert ("Called", !comparer.Called);
2740 [ExpectedException (typeof (ArgumentNullException))]
2741 public void AsReadOnly_NullArray ()
2743 Array.AsReadOnly <int> (null);
2747 public void ReadOnly_Count ()
2749 AssertEquals (10, Array.AsReadOnly (new int [10]).Count);
2753 public void ReadOnly_Contains ()
2755 int[] arr = new int [2];
2758 IList<int> a = Array.AsReadOnly (arr);
2760 Assert (a.Contains (3));
2761 Assert (!a.Contains (6));
2765 public void ReadOnly_IndexOf ()
2767 int[] arr = new int [2];
2770 IList<int> a = Array.AsReadOnly (arr);
2772 AssertEquals (0, a.IndexOf (3));
2773 AssertEquals (1, a.IndexOf (5));
2774 AssertEquals (-1, a.IndexOf (6));
2778 public void ReadOnly_Indexer ()
2780 int[] arr = new int [2];
2783 IList<int> a = Array.AsReadOnly (arr);
2785 AssertEquals (3, a [0]);
2786 AssertEquals (5, a [1]);
2788 /* Check that modifications to the original array are visible */
2790 AssertEquals (6, a [0]);
2794 public void ReadOnly_Enumerator ()
2796 int[] arr = new int [10];
2798 for (int i = 0; i < 10; ++i)
2802 foreach (int i in Array.AsReadOnly (arr))
2805 AssertEquals (45, sum);
2809 public void Resize ()
2811 int [] arr = new int [] { 1, 3, 5 };
2812 Array.Resize <int> (ref arr, 3);
2813 AssertEquals ("#A1", 3, arr.Length);
2814 AssertEquals ("#A2", 1, arr [0]);
2815 AssertEquals ("#A3", 3, arr [1]);
2816 AssertEquals ("#A4", 5, arr [2]);
2818 Array.Resize <int> (ref arr, 2);
2819 AssertEquals ("#B1", 2, arr.Length);
2820 AssertEquals ("#B2", 1, arr [0]);
2821 AssertEquals ("#B3", 3, arr [1]);
2823 Array.Resize <int> (ref arr, 4);
2824 AssertEquals ("#C1", 4, arr.Length);
2825 AssertEquals ("#C2", 1, arr [0]);
2826 AssertEquals ("#C3", 3, arr [1]);
2827 AssertEquals ("#C4", 0, arr [2]);
2828 AssertEquals ("#C5", 0, arr [3]);
2832 public void Resize_null ()
2835 Array.Resize (ref arr, 10);
2836 AssertEquals (arr.Length, 10);
2840 public void Test_ContainsAndIndexOf_EquatableItem ()
2842 EquatableClass[] list = new EquatableClass[] {new EquatableClass (0), new EquatableClass (1), new EquatableClass (0)};
2844 AssertEquals ("#0", 0, Array.IndexOf<EquatableClass> (list, list[0]));
2845 AssertEquals ("#1", 0, Array.IndexOf<EquatableClass> (list, new EquatableClass (0)));
2846 AssertEquals ("#2", 2, Array.LastIndexOf<EquatableClass> (list, list[0]));
2847 AssertEquals ("#3", 2, Array.LastIndexOf<EquatableClass> (list, new EquatableClass (0)));
2850 public class EquatableClass : IEquatable<EquatableClass>
2853 public EquatableClass (int x)
2858 public bool Equals (EquatableClass other)
2860 return this._x == other._x;
2865 public void AsIList ()
2867 IList<int> arr = new int [10];
2869 AssertEquals (5, arr [0]);
2871 IList<FooStruct> arr2 = new FooStruct [10];
2872 FooStruct s = new FooStruct ();
2877 AssertEquals (11, s.i);
2878 AssertEquals (22, s.j);
2880 IList<string> arr3 = new string [10];
2882 AssertEquals ("ABC", arr3 [5]);
2889 #if !TARGET_JVM // BugBUG: T[] is not yet ICollection<T> under TARGET_JVM
2892 public void ICollectionNull ()
2894 ICollection<object> test;
2896 test = new List<object>();
2897 AssertEquals ("list<o>", test.Contains (null), false);
2899 test = new object[] {};
2900 AssertEquals ("empty array", test.Contains (null), false);
2902 test = new object[] {null};
2903 AssertEquals ("array with null", test.Contains (null), true);
2905 test = new List<object>(test);
2906 AssertEquals ("List<object> with test", test.Contains (null), true);
2908 test = new object[] {new object()};
2909 AssertEquals ("array with object", test.Contains (null), false);
2911 test = new List<object>(test);
2912 AssertEquals ("array with test", test.Contains (null), false);
2914 #endif // TARGET_JVM
2919 enum ByteEnum : byte {}
2920 enum IntEnum : int {}
2923 public void TestByteEnumArrayToByteArray ()
2925 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
2926 byte[] b = new byte[a.Length];
2931 public void TestByteEnumArrayToIntArray ()
2933 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
2934 int[] b = new int[a.Length];
2939 [ExpectedException (typeof (ArrayTypeMismatchException))]
2940 public void TestIntEnumArrayToByteArray ()
2942 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
2943 byte[] b = new byte[a.Length];
2948 public void TestIntEnumArrayToIntArray ()
2950 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
2951 int[] b = new int[a.Length];
2958 [Test] // bug #322248
2959 public void IEnumerator_Reset ()
2961 int[] array = new int[] { 1, 2, 3};
2962 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
2963 Assert ("#A1", e.MoveNext ());
2964 AssertEquals ("#A2", 1, e.Current);
2965 Assert ("#A3", e.MoveNext ());
2966 AssertEquals ("#A4", 2, e.Current);
2970 Assert ("#C1", e.MoveNext ());
2971 AssertEquals ("#C2", 1, e.Current);
2975 public void IEnumerator_Current_Finished ()
2977 int[] array = new int[] { 1, 2, 3 };
2978 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
2979 Assert ("#A1", e.MoveNext ());
2980 AssertEquals ("#A2", 1, e.Current);
2981 Assert ("#A3", e.MoveNext ());
2982 AssertEquals ("#A4", 2, e.Current);
2983 Assert ("#A5", e.MoveNext ());
2984 AssertEquals ("#A6", 3, e.Current);
2985 Assert ("#A6", !e.MoveNext ());
2988 Fail ("#B1:" + e.Current);
2989 } catch (InvalidOperationException ex) {
2990 // Enumeration already finished
2991 AssertEquals ("#B2", typeof (InvalidOperationException), ex.GetType ());
2992 AssertNull ("#B3", ex.InnerException);
2993 AssertNotNull ("#B4", ex.Message);
2998 public void IEnumerator_Current_NotStarted ()
3000 int[] array = new int[] { 1, 2, 3 };
3001 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3004 Fail ("#A1:" + e.Current);
3005 } catch (InvalidOperationException ex) {
3006 // Enumeration has not started. Call MoveNext
3007 AssertEquals ("#A2", typeof (InvalidOperationException), ex.GetType ());
3008 AssertNull ("#A3", ex.InnerException);
3009 AssertNotNull ("#A4", ex.Message);
3014 public void IEnumerator_Current_Reset ()
3016 int[] array = new int[] { 1, 2, 3 };
3017 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3022 Fail ("#B1:" + e.Current);
3023 } catch (InvalidOperationException ex) {
3024 // Enumeration has not started. Call MoveNext
3025 AssertEquals ("#B2", typeof (InvalidOperationException), ex.GetType ());
3026 AssertNull ("#B3", ex.InnerException);
3027 AssertNotNull ("#B4", ex.Message);
3031 public void ICollection_IsReadOnly() {
3032 ICollection<string> arr = new string [10];
3034 Assert (arr.IsReadOnly);