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)
53 public override int GetHashCode ()
59 //End Auxiliary Things
62 public class ArrayTest
64 char [] arrsort = {'d', 'b', 'f', 'e', 'a', 'c'};
69 public void TestIsFixedSize() {
71 Assert.IsTrue (a1.IsFixedSize, "All arrays are fixed");
75 public void TestIsReadOnly() {
77 Assert.IsTrue (!a1.IsReadOnly, "No array is readonly");
81 public void TestIsSynchronized() {
83 Assert.IsTrue (!a1.IsSynchronized, "No array is synchronized");
87 public void TestLength() {
90 Assert.AreEqual (0, a1.Length, "Zero length array");
94 Assert.AreEqual (1, a1.Length, "One-length array");
97 char[] a1 = {'c', 'c'};
98 Assert.AreEqual (2, a1.Length, "Two-length array");
103 public void TestRank() {
104 char[] a1 = { 'c', 'd', 'e' };
105 Assert.AreEqual (1, a1.Rank, "Rank one");
107 char[,] a2 = new Char[3,3];
108 Assert.AreEqual (2, a2.Rank, "Rank two");
110 char[,,] a3 = new Char[3,3,3];
111 Assert.AreEqual (3, a3.Rank, "Rank three");
115 public void TestBinarySearch1() {
116 bool errorThrown = false;
118 Array.BinarySearch(null, "blue");
119 } catch (ArgumentNullException) {
122 Assert.IsTrue (errorThrown, "#B01");
125 char[,] c1 = new Char[2,2];
126 Array.BinarySearch(c1, "needle");
127 } catch (RankException) {
130 Assert.IsTrue (errorThrown, "#B02");
133 char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
134 Assert.IsTrue (Array.BinarySearch(arr, 'c') >= 3, "#B05");
135 Assert.IsTrue (Array.BinarySearch(arr, 'c') < 6, "#B06");
138 char[] arr = {'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
139 Assert.AreEqual (-4, Array.BinarySearch(arr, 'c'), "#B07");
142 char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
143 Assert.AreEqual (-9, Array.BinarySearch(arr, 'e'), "#B08");
148 public void TestBinarySearch2() {
149 bool errorThrown = false;
151 Array.BinarySearch(null, 0, 1, "blue");
152 } catch (ArgumentNullException) {
155 Assert.IsTrue (errorThrown, "#B20");
158 char[,] c1 = new Char[2,2];
159 Array.BinarySearch(c1, 0, 1, "needle");
160 } catch (RankException) {
163 Assert.IsTrue (errorThrown, "#B21");
167 Array.BinarySearch(c1, -1, 1, 'a');
168 } catch (ArgumentOutOfRangeException) {
171 Assert.IsTrue (errorThrown, "#B22");
175 Array.BinarySearch(c1, 0, -1, 'a');
176 } catch (ArgumentOutOfRangeException) {
179 Assert.IsTrue (errorThrown, "#B23");
183 Array.BinarySearch(c1, 0, 4, 'a');
184 } catch (ArgumentException) {
187 Assert.IsTrue (errorThrown, "#B24");
190 char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
191 Assert.IsTrue (Array.BinarySearch(arr, 2, 8, 'c') >= 5, "#B26");
192 Assert.IsTrue (Array.BinarySearch(arr, 2, 8, 'c') < 8, "#B27");
195 char[] arr = {'z', 'z', 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
196 Assert.AreEqual (-6, Array.BinarySearch(arr, 2, 8, 'c'), "#B28");
199 char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
200 Assert.AreEqual (-11, Array.BinarySearch(arr, 2, 8, 'e'), "#B29");
204 public void TestBinarySearch3()
206 int[] array = new int[100];
208 for (int i = 0; i < 100; i++)
211 Assert.AreEqual (49, Array.BinarySearch(array, 10), "#B30");
215 public void BinarySearch_NullValue ()
217 int[] array = new int[1];
218 Assert.AreEqual (-1, Array.BinarySearch (array, null), "I=a,o");
219 Assert.AreEqual (-1, Array.BinarySearch (array, null, null), "I=a,o,c");
220 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 1, null), "I=a,i,i,o");
221 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 1, null, null), "I=a,i,i,o,c");
223 object[] o = new object [3] { this, this, null };
224 Assert.AreEqual (-1, Array.BinarySearch (o, null), "O=a,o");
225 Assert.AreEqual (-1, Array.BinarySearch (o, null, null), "O=a,o,c");
226 Assert.AreEqual (-1, Array.BinarySearch (o, 0, 3, null), "O=a,i,i,o");
227 Assert.AreEqual (-1, Array.BinarySearch (o, 0, 3, null, null), "O=a,i,i,o,c");
230 // TODO - testBinarySearch with explicit IComparer args
233 public void TestClear() {
234 bool errorThrown = false;
236 Array.Clear(null, 0, 1);
237 } catch (ArgumentNullException) {
240 Assert.IsTrue (errorThrown, "#C01");
242 int[] i1 = { 1, 2, 3, 4 };
244 int[] compare = {1,2,3,4};
245 Assert.AreEqual (compare[0], i1[0], "#C02");
246 Assert.AreEqual (compare[1], i1[1], "#C03");
247 Assert.AreEqual (compare[2], i1[2], "#C04");
248 Assert.AreEqual (compare[3], i1[3], "#C05");
250 Array.Clear(i1, 3, 1);
252 int[] compare = {1,2,3,0};
253 Assert.AreEqual (compare[0], i1[0], "#C06");
254 Assert.AreEqual (compare[1], i1[1], "#C07");
255 Assert.AreEqual (compare[2], i1[2], "#C08");
256 Assert.AreEqual (compare[3], i1[3], "#C09");
258 Array.Clear(i1, 1, 1);
260 int[] compare = {1,0,3,0};
261 Assert.AreEqual (compare[0], i1[0], "#C10");
262 Assert.AreEqual (compare[1], i1[1], "#C11");
263 Assert.AreEqual (compare[2], i1[2], "#C12");
264 Assert.AreEqual (compare[3], i1[3], "#C13");
266 Array.Clear(i1, 1, 3);
268 int[] compare = {1,0,0,0};
269 Assert.AreEqual (compare[0], i1[0], "#C14");
270 Assert.AreEqual (compare[1], i1[1], "#C15");
271 Assert.AreEqual (compare[2], i1[2], "#C16");
272 Assert.AreEqual (compare[3], i1[3], "#C17");
275 string[] s1 = { "red", "green", "blue" };
276 Array.Clear(s1, 0, 3);
278 string[] compare = {null, null, null};
279 Assert.AreEqual (compare[0], s1[0], "#C18");
280 Assert.AreEqual (compare[1], s1[1], "#C19");
281 Assert.AreEqual (compare[2], s1[2], "#C20");
286 public void TestClone() {
287 char[] c1 = {'a', 'b', 'c'};
288 char[] c2 = (char[])c1.Clone();
289 Assert.AreEqual (c1[0], c2[0], "#D01");
290 Assert.AreEqual (c1[1], c2[1], "#D02");
291 Assert.AreEqual (c1[2], c2[2], "#D03");
293 char[] d10 = {'a', 'b'};
294 char[] d11 = {'a', 'c'};
295 char[] d12 = {'b', 'c'};
296 char[][] d1 = {d10, d11, d12};
297 char[][] d2 = (char[][])d1.Clone();
298 Assert.AreEqual (d1[0], d2[0], "#D04");
299 Assert.AreEqual (d1[1], d2[1], "#D05");
300 Assert.AreEqual (d1[2], d2[2], "#D06");
303 Assert.AreEqual (d1[0], d2[0], "#D07");
306 [Test] public void TestIndexer ()
308 int [] a = new int [10];
312 Assert.Fail ("IList.this [-1] should throw");
313 } catch (IndexOutOfRangeException) {
316 Assert.Fail ("Should have thrown an IndexOutOfRangeException");
321 public void TestCopy() {
323 bool errorThrown = false;
326 Array.Copy(c1, null, 1);
327 } catch (ArgumentNullException) {
330 Assert.IsTrue (errorThrown, "#E01");
333 bool errorThrown = false;
336 Array.Copy(null, c1, 1);
337 } catch (ArgumentNullException) {
340 Assert.IsTrue (errorThrown, "#E02");
343 bool errorThrown = false;
345 Char[] c1 = new Char[1];
346 Char[,] c2 = new Char[1,1];
347 Array.Copy(c1, c2, 1);
348 } catch (RankException) {
351 Assert.IsTrue (errorThrown, "#E03");
354 bool errorThrown = false;
356 Char[] c1 = new Char[1];
357 string[] s1 = new String[1];
358 Array.Copy(c1, s1, 1);
359 } catch (ArrayTypeMismatchException) {
362 Assert.IsTrue (errorThrown, "#E04");
365 bool errorThrown = false;
367 Char[] c1 = new Char[1];
368 Object[] o1 = new Object[1];
370 Array.Copy(o1, c1, 1);
371 } catch (InvalidCastException) {
374 Assert.IsTrue (errorThrown, "#E05");
377 bool errorThrown = false;
379 Char[] c1 = new Char[1];
380 Char[] c2 = new Char[1];
381 Array.Copy(c1, c2, -1);
382 } catch (ArgumentOutOfRangeException) {
385 Assert.IsTrue (errorThrown, "#E06");
388 bool errorThrown = false;
390 Char[] c1 = new Char[1];
391 Char[] c2 = new Char[2];
392 Array.Copy(c1, c2, 2);
393 } catch (ArgumentException) {
396 Assert.IsTrue (errorThrown, "#E07");
399 bool errorThrown = false;
401 Char[] c1 = new Char[1];
402 Char[] c2 = new Char[2];
403 Array.Copy(c2, c1, 2);
404 } catch (ArgumentException) {
407 Assert.IsTrue (errorThrown, "#E08");
410 char[] orig = {'a', 'b', 'd', 'a'};
411 char[] copy = new Char[4];
412 Array.Copy(orig, copy, 4);
413 for (int i = 0; i < orig.Length; i++) {
414 Assert.AreEqual (orig[i], copy[i], "#E09(" + i + ")");
416 Array.Clear(copy, 0, copy.Length);
417 for (int i = 0; i < orig.Length; i++) {
418 Assert.AreEqual ((char)0, copy[i], "#E10(" + i + ")");
420 Array.Copy(orig, copy, 2);
421 Assert.AreEqual (orig[0], copy[0], "#E11");
422 Assert.AreEqual (orig[1], copy[1], "#E12");
423 Assert.IsTrue (orig[2] != copy[2], "#E13");
424 Assert.IsTrue (orig[3] != copy[3], "#E14");
428 public void TestCopy2() {
430 bool errorThrown = false;
432 Char[] c1 = new Char[2];
433 Char[] c2 = new Char[2];
434 Array.Copy(c2, 1, c1, 0, 2);
435 } catch (ArgumentException) {
438 Assert.IsTrue (errorThrown, "#E31");
441 bool errorThrown = false;
443 Char[] c1 = new Char[2];
444 Char[] c2 = new Char[2];
445 Array.Copy(c2, 0, c1, 1, 2);
446 } catch (ArgumentException) {
449 Assert.IsTrue (errorThrown, "#E32");
452 char[] orig = {'a', 'b', 'd', 'a'};
453 char[] copy = new Char[4];
454 Array.Copy(orig, 1, copy, 1, 3);
455 Assert.IsTrue (copy[0] != orig[0], "#E33");
456 for (int i = 1; i < orig.Length; i++) {
457 Assert.AreEqual (orig[i], copy[i], "#E34(" + i + ")");
459 Array.Clear(copy, 0, copy.Length);
460 Array.Copy(orig, 1, copy, 0, 2);
461 Assert.AreEqual (orig[1], copy[0], "#E35");
462 Assert.AreEqual (orig[2], copy[1], "#E36");
463 Assert.IsTrue (copy[2] != orig[2], "#E37");
464 Assert.IsTrue (copy[3] != orig[3], "#E38");
468 [ExpectedException (typeof (InvalidCastException))]
469 public void Copy_InvalidCast () {
470 object[] arr1 = new object [10];
471 Type[] arr2 = new Type [10];
473 arr1 [0] = new object ();
475 Array.Copy (arr1, 0, arr2, 0, 10);
479 public void TestCopyTo() {
481 bool errorThrown = false;
483 Char[] c1 = new Char[2];
485 } catch (ArgumentNullException) {
488 Assert.IsTrue (errorThrown, "#E61");
491 bool errorThrown = false;
493 Char[] c1 = new Char[2];
494 Char[,] c2 = new Char[2,2];
496 } catch (ArgumentException) {
499 #if TARGET_JVM // This is really implementation dependent behaviour.
500 catch (RankException) {
504 Assert.IsTrue (errorThrown, "#E62");
507 bool errorThrown = false;
509 Char[,] c1 = new Char[2,2];
510 Char[] c2 = new Char[2];
512 } catch (RankException) {
515 Assert.IsTrue (errorThrown, "#E63");
518 bool errorThrown = false;
520 Char[,] c1 = new Char[2,2];
521 Char[] c2 = new Char[2];
523 } catch (RankException) {
526 Assert.IsTrue (errorThrown, "#E64");
529 bool errorThrown = false;
531 Char[] c1 = new Char[2];
532 Char[] c2 = new Char[2];
534 } catch (ArgumentOutOfRangeException) {
537 Assert.IsTrue (errorThrown, "#E65");
540 bool errorThrown = false;
542 Char[] c1 = new Char[2];
543 Char[] c2 = new Char[2];
545 } catch (ArgumentException) {
548 Assert.IsTrue (errorThrown, "#E66");
551 bool errorThrown = false;
553 Char[] c1 = new Char[2];
554 Char[] c2 = new Char[2];
556 } catch (ArgumentException) {
559 Assert.IsTrue (errorThrown, "#E67");
563 bool errorThrown = false;
565 String[] c1 = new String[2];
566 // TODO: this crashes mono if there are null
567 // values in the array.
570 Char[] c2 = new Char[2];
574 } catch (ArrayTypeMismatchException) {
577 Assert.IsTrue (errorThrown, "#E68");
580 Char[] orig = {'a', 'b', 'c', 'd'};
581 Char[] copy = new Char[10];
582 Array.Clear(copy, 0, copy.Length);
583 orig.CopyTo(copy, 3);
584 Assert.AreEqual ((char)0, copy[0], "#E69");
585 Assert.AreEqual ((char)0, copy[1], "#E70");
586 Assert.AreEqual ((char)0, copy[2], "#E71");
587 Assert.AreEqual (orig[0], copy[3], "#E72");
588 Assert.AreEqual (orig[1], copy[4], "#E73");
589 Assert.AreEqual (orig[2], copy[5], "#E74");
590 Assert.AreEqual (orig[3], copy[6], "#E75");
591 Assert.AreEqual ((char)0, copy[7], "#E76");
592 Assert.AreEqual ((char)0, copy[8], "#E77");
593 Assert.AreEqual ((char)0, copy[9], "#E78");
596 // The following is valid and must not throw an exception.
597 bool errorThrown = false;
599 int[] src = new int [0];
600 int[] dest = new int [0];
601 src.CopyTo (dest, 0);
602 } catch (ArgumentException) {
605 Assert.IsTrue (!errorThrown, "#E79");
610 bool errorThrown = false;
612 CClass[] src = new CClass [] { new CClass () };
613 BClass[] dest = new BClass [1];
615 src.CopyTo (dest, 0);
617 } catch (ArrayTypeMismatchException) {
620 Assert.IsTrue (errorThrown, "#E80");
625 public void TestCreateInstance() {
627 bool errorThrown = false;
629 Array.CreateInstance(null, 12);
630 } catch (ArgumentNullException) {
633 Assert.IsTrue (errorThrown, "#F01");
636 bool errorThrown = false;
638 Array.CreateInstance(Type.GetType("System.Char"), -3);
639 } catch (ArgumentOutOfRangeException) {
642 Assert.IsTrue (errorThrown, "#F02");
645 bool errorThrown = false;
647 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
648 } catch (ArgumentNullException) {
651 Assert.IsTrue (errorThrown, "#F03a");
655 bool errorThrown = false;
657 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
658 } catch (ArgumentNullException) {
661 Assert.IsTrue (errorThrown, "#F03b");
664 #if !TARGET_JVM // Arrays lower bounds are not supported for TARGET_JVM
666 bool errorThrown = false;
668 Array.CreateInstance(Type.GetType("System.Char"), null, null);
669 } catch (ArgumentNullException) {
672 Assert.IsTrue (errorThrown, "#F04");
676 bool errorThrown = false;
678 int[] lengths = new int [0];
679 Array.CreateInstance(Type.GetType("System.Char"), lengths);
680 } catch (ArgumentException) {
683 Assert.IsTrue (errorThrown, "#F05");
685 #if !TARGET_JVM // CreateInstance with lower bounds not supported for TARGET_JVM
687 bool errorThrown = false;
689 int[] lengths = new int [1];
690 int[] bounds = new int [2];
691 Array.CreateInstance(Type.GetType("System.Char"), lengths, bounds);
693 } catch (ArgumentException) {
696 Assert.IsTrue (errorThrown, "#F06");
699 char[] c1 = (char[])Array.CreateInstance(Type.GetType("System.Char"), 12);
700 Assert.AreEqual (12, c1.Length, "#F07");
702 Array c2 = Array.CreateInstance(Type.GetType("System.Char"), 12, 5);
703 Assert.AreEqual (2, c2.Rank, "#F08");
704 Assert.AreEqual (60, c2.Length, "#F09");
708 int[] lengths = { 3 };
709 int[] bounds = { 5 };
710 int[] src = { 512, 718, 912 };
711 Array array = Array.CreateInstance(typeof(int), lengths, bounds);
713 Assert.AreEqual (3, array.Length, "#F10");
714 Assert.AreEqual (5, array.GetLowerBound(0), "#F11");
715 Assert.AreEqual (7, array.GetUpperBound(0), "#F12");
717 src.CopyTo (array, 5);
719 for (int i = 0; i < src.Length; i++)
720 Assert.AreEqual (src[i], array.GetValue(i+5), "#F13(" + i + ")");
723 // Test that a 1 dimensional array with 0 lower bound is the
724 // same as an szarray
725 Type szarrayType = new int [10].GetType ();
726 Assert.IsTrue (szarrayType == (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0})).GetType ());
727 Assert.IsTrue (szarrayType != (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {1})).GetType ());
732 [ExpectedException (typeof (ArgumentNullException))]
733 public void TestCreateInstance2 ()
735 Array.CreateInstance (typeof (Int32), (int[])null);
740 [ExpectedException (typeof (ArgumentNullException))]
742 [ExpectedException (typeof (NullReferenceException))]
744 public void TestCreateInstance2b ()
746 Array.CreateInstance (typeof (Int32), (long[])null);
750 public void TestGetEnumerator() {
751 String[] s1 = {"this", "is", "a", "test"};
752 IEnumerator en = s1.GetEnumerator ();
753 Assert.IsNotNull (en, "#G01");
755 Assert.IsTrue (en.MoveNext (), "#G02");
756 Assert.AreEqual ("this", en.Current, "#G03");
757 Assert.IsTrue (en.MoveNext (), "#G04");
758 Assert.AreEqual ("is", en.Current, "#G05");
759 Assert.IsTrue (en.MoveNext (), "#G06");
760 Assert.AreEqual ("a", en.Current, "#G07");
761 Assert.IsTrue (en.MoveNext (), "#G08");
762 Assert.AreEqual ("test", en.Current, "#G09");
763 Assert.IsTrue (!en.MoveNext (), "#G10");
766 Assert.IsTrue (en.MoveNext (), "#G11");
767 Assert.AreEqual ("this", en.Current, "#G12");
769 // mutation does not invalidate array enumerator!
770 s1.SetValue ("change", 1);
771 Assert.IsTrue (en.MoveNext (), "#G13");
772 Assert.AreEqual ("change", en.Current, "#G14");
776 public void TestGetEnumeratorMultipleDimension() {
777 String[,] s1 = {{"this", "is"}, {"a", "test"}};
778 IEnumerator en = s1.GetEnumerator ();
779 Assert.IsNotNull (en, "#AA01");
781 Assert.IsTrue (en.MoveNext (), "#AA02");
782 Assert.AreEqual ("this", en.Current, "#AA03");
783 Assert.IsTrue (en.MoveNext (), "#AA04");
784 Assert.AreEqual ("is", en.Current, "#AA05");
785 Assert.IsTrue (en.MoveNext (), "#AA06");
786 Assert.AreEqual ("a", en.Current, "#AA07");
787 Assert.IsTrue (en.MoveNext (), "#AA08");
788 Assert.AreEqual ("test", en.Current, "#AA09");
789 Assert.IsTrue (!en.MoveNext (), "#AA10");
792 Assert.IsTrue (en.MoveNext (), "#AA11");
793 Assert.AreEqual ("this", en.Current, "#AA12");
796 // mutation does not invalidate array enumerator!
797 s1.SetValue ("change", idxs);
798 Assert.IsTrue (en.MoveNext (), "#AA13");
799 Assert.AreEqual ("change", en.Current, "#AA14");
803 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
804 public void TestGetEnumeratorNonZeroLowerBounds() {
805 int[] myLengthsArray = new int[2] { 3, 5 };
806 int[] myBoundsArray = new int[2] { 2, 3 };
808 Array myArray=Array.CreateInstance( typeof(String), myLengthsArray, myBoundsArray );
809 for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
810 for ( int j = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++ ) {
811 int[] myIndicesArray = new int[2] { i, j };
812 myArray.SetValue( Convert.ToString(i) + j, myIndicesArray );
814 IEnumerator en = myArray.GetEnumerator ();
815 Assert.IsNotNull (en, "#AB01");
817 // check the first couple of values
818 Assert.IsTrue (en.MoveNext (), "#AB02");
819 Assert.AreEqual ("23", en.Current, "#AB03");
820 Assert.IsTrue (en.MoveNext (), "#AB04");
821 Assert.AreEqual ("24", en.Current, "#AB05");
823 // then check the last element's value
826 lastElement = (string)en.Current;
827 } while (en.MoveNext());
828 Assert.AreEqual ("47", lastElement, "#AB06");
832 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
833 public void TestIList_Add () {
834 int[] myLengthsArray = new int[2] { 3, 5 };
835 int[] myBoundsArray = new int[2] { 2, 3 };
837 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
839 ((IList)myArray).Add ("can not");
840 Assert.Fail ("IList.Add should throw");
842 catch (NotSupportedException) {
846 Assert.Fail ("IList.Add threw wrong exception type");
849 Assert.Fail ("IList.Add shouldn't get this far");
853 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
854 public void TestIList_Insert () {
855 int[] myLengthsArray = new int[2] { 3, 5 };
856 int[] myBoundsArray = new int[2] { 2, 3 };
858 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
860 ((IList)myArray).Insert (0, "can not");
861 Assert.Fail ("IList.Insert should throw");
863 catch (NotSupportedException) {
867 Assert.Fail ("IList.Insert threw wrong exception type");
870 Assert.Fail ("IList.Insert shouldn't get this far");
874 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
875 public void TestIList_Remove () {
876 int[] myLengthsArray = new int[2] { 3, 5 };
877 int[] myBoundsArray = new int[2] { 2, 3 };
879 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
881 ((IList)myArray).Remove ("can not");
882 Assert.Fail ("IList.Remove should throw");
884 catch (NotSupportedException) {
888 Assert.Fail ("IList.Remove threw wrong exception type");
891 Assert.Fail ("IList.Remove shouldn't get this far");
895 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
896 public void TestIList_RemoveAt () {
897 int[] myLengthsArray = new int[2] { 3, 5 };
898 int[] myBoundsArray = new int[2] { 2, 3 };
900 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
902 ((IList)myArray).RemoveAt (0);
903 Assert.Fail ("IList.RemoveAt should throw");
905 catch (NotSupportedException) {
909 Assert.Fail ("IList.RemoveAt threw wrong exception type");
912 Assert.Fail ("IList.RemoveAt shouldn't get this far");
916 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
917 public void TestIList_Contains () {
918 int[] myLengthsArray = new int[2] { 3, 5 };
919 int[] myBoundsArray = new int[2] { 2, 3 };
921 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
924 bool b = ((IList)myArray).Contains ("23");
925 Assert.Fail ("IList.Contains should throw with multi-dimensional arrays");
927 catch (RankException) {
928 int[] iArr = new int[3] { 1, 2, 3};
929 // check the first and last items
930 Assert.IsTrue (((IList)iArr).Contains (1), "AC01");
931 Assert.IsTrue (((IList)iArr).Contains (3), "AC02");
933 // and one that is definately not there
934 Assert.IsTrue (!((IList)iArr).Contains (42), "AC03");
938 Assert.Fail ("Should not get here");
942 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
943 public void TestIList_IndexOf () {
944 int[] myLengthsArray = new int[2] { 3, 5 };
945 int[] myBoundsArray = new int[2] { 2, 3 };
947 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
950 bool b = ((IList)myArray).Contains ("23");
951 Assert.Fail ("IList.Contains should throw with multi-dimensional arrays");
953 catch (RankException) {
954 int[] iArr = new int[3] { 1, 2, 3};
955 // check the first and last items
956 Assert.AreEqual (0, ((IList)iArr).IndexOf (1), "AD01");
957 Assert.AreEqual (2, ((IList)iArr).IndexOf (3), "AD02");
959 // and one that is definately not there
960 Assert.AreEqual (-1, ((IList)iArr).IndexOf (42), "AD03");
962 catch (Exception e) {
963 Assert.Fail ("Unexpected exception: " + e.ToString());
966 // check that wierd case whem lowerbound is Int32.MinValue,
967 // so that IndexOf() needs to return Int32.MaxValue when it cannot find the object
968 int[] myLengthArray = new int[1] { 3 };
969 int[] myBoundArray = new int[1] { Int32.MinValue };
970 Array myExtremeArray=Array.CreateInstance ( typeof(String), myLengthArray, myBoundArray );
971 Assert.AreEqual (Int32.MaxValue, ((IList)myExtremeArray).IndexOf (42), "AD04");
976 public void TestGetLength() {
978 bool errorThrown = false;
980 char[] c1 = {'a', 'b', 'c'};
982 } catch (IndexOutOfRangeException) {
985 Assert.IsTrue (errorThrown, "#H01");
988 bool errorThrown = false;
990 char[] c1 = {'a', 'b', 'c'};
992 } catch (IndexOutOfRangeException) {
995 Assert.IsTrue (errorThrown, "#H02");
998 char[] c2 = new Char[5];
999 Assert.AreEqual (5, c2.GetLength(0), "#H03");
1001 char[,] c3 = new Char[6,7];
1002 Assert.AreEqual (6, c3.GetLength(0), "#H04");
1003 Assert.AreEqual (7, c3.GetLength(1), "#H05");
1007 public void TestGetLowerBound() {
1009 bool errorThrown = false;
1011 char[] c = {'a', 'b', 'c'};
1012 c.GetLowerBound(-1);
1013 } catch (IndexOutOfRangeException) {
1016 Assert.IsTrue (errorThrown, "#H31");
1019 bool errorThrown = false;
1021 char[] c = {'a', 'b', 'c'};
1023 } catch (IndexOutOfRangeException) {
1026 Assert.IsTrue (errorThrown, "#H32");
1029 char[] c1 = new Char[5];
1030 Assert.AreEqual (0, c1.GetLowerBound(0), "#H33");
1032 char[,] c2 = new Char[4,4];
1033 Assert.AreEqual (0, c2.GetLowerBound(0), "#H34");
1034 Assert.AreEqual (0, c2.GetLowerBound(1), "#H35");
1038 public void TestGetUpperBound() {
1040 bool errorThrown = false;
1042 char[] c = {'a', 'b', 'c'};
1043 c.GetUpperBound(-1);
1044 } catch (IndexOutOfRangeException) {
1047 Assert.IsTrue (errorThrown, "#H61");
1050 bool errorThrown = false;
1052 char[] c = {'a', 'b', 'c'};
1054 } catch (IndexOutOfRangeException) {
1057 Assert.IsTrue (errorThrown, "#H62");
1060 char[] c1 = new Char[5];
1061 Assert.AreEqual (4, c1.GetUpperBound(0), "#H63");
1063 char[,] c2 = new Char[4,6];
1064 Assert.AreEqual (3, c2.GetUpperBound(0), "#H64");
1065 Assert.AreEqual (5, c2.GetUpperBound(1), "#H65");
1069 public void TestGetValue1() {
1071 bool errorThrown = false;
1073 char[,] c = new Char[2,2];
1075 } catch (ArgumentException) {
1078 Assert.IsTrue (errorThrown, "#I01");
1081 bool errorThrown = false;
1083 char[] c = {'a', 'b', 'c'};
1085 } catch (IndexOutOfRangeException) {
1088 Assert.IsTrue (errorThrown, "#I02");
1091 bool errorThrown = false;
1093 char[] c = {'a', 'b', 'c'};
1095 } catch (IndexOutOfRangeException) {
1098 Assert.IsTrue (errorThrown, "#I03");
1101 char[] c1 = {'a', 'b', 'c', 'd'};
1102 for (int i = 0; i < c1.Length; i++) {
1103 Assert.AreEqual (c1[i], c1.GetValue(i), "#I04(" + i + ")");
1108 public void TestGetValue2() {
1110 bool errorThrown = false;
1112 char[] c = new Char[2];
1114 } catch (ArgumentException) {
1117 Assert.IsTrue (errorThrown, "#I21");
1120 bool errorThrown = false;
1122 char[,] c = new Char[2,2];
1124 } catch (IndexOutOfRangeException) {
1127 Assert.IsTrue (errorThrown, "#I22");
1130 bool errorThrown = false;
1132 char[,] c = new Char[2,2];
1134 } catch (IndexOutOfRangeException) {
1137 Assert.IsTrue (errorThrown, "#I23");
1140 char[,] c1 = new Char[4,6];
1141 for (int i = 0; i < 24; i++) {
1144 c1[first,second] = (char)(((int)'a')+i);
1146 for (int i = 0; i < c1.GetLength(0); i++) {
1147 for (int j = 0; j < c1.GetLength(1); j++) {
1148 Assert.AreEqual (c1[i, j], c1.GetValue(i, j), "#I24(" + i + "," + j + ")");
1154 public void TestGetValue3() {
1156 bool errorThrown = false;
1158 char[] c = new Char[2];
1160 } catch (ArgumentException) {
1163 Assert.IsTrue (errorThrown, "#I41");
1166 bool errorThrown = false;
1168 char[,,] c = new Char[2,2,2];
1169 c.GetValue(-1, 1, 1);
1170 } catch (IndexOutOfRangeException) {
1173 Assert.IsTrue (errorThrown, "#I42");
1176 bool errorThrown = false;
1178 char[,,] c = new Char[2,2,2];
1180 } catch (IndexOutOfRangeException) {
1183 Assert.IsTrue (errorThrown, "#I43");
1186 char[,,] c1 = new Char[4,2,3];
1187 for (int i = 0; i < 24; i++) {
1189 int remains = i % 6;
1190 int second = remains / 3;
1191 int third = remains % 3;
1192 c1[first,second, third] = (char)(((int)'a')+i);
1194 for (int i = 0; i < c1.GetLength(0); i++) {
1195 for (int j = 0; j < c1.GetLength(1); j++) {
1196 for (int k = 0; k < c1.GetLength(2); k++) {
1197 Assert.AreEqual (c1[i, j, k], c1.GetValue(i, j, k), "#I44(" + i + "," + j + ")");
1205 [ExpectedException (typeof (ArgumentNullException))]
1207 [ExpectedException (typeof (NullReferenceException))]
1209 public void TestGetValueLongArray ()
1211 char[] c = new Char[2];
1212 c.GetValue((long [])null);
1216 public void TestGetValueN() {
1218 bool errorThrown = false;
1220 char[] c = new Char[2];
1221 c.GetValue((int [])null);
1222 } catch (ArgumentNullException) {
1225 Assert.IsTrue (errorThrown, "#I61a");
1228 bool errorThrown = false;
1230 char[] c = new Char[2];
1231 int[] coords = {1, 1};
1233 } catch (ArgumentException) {
1236 Assert.IsTrue (errorThrown, "#I62");
1239 bool errorThrown = false;
1241 char[,] c = new Char[2,2];
1242 int[] coords = {-1, 1};
1244 } catch (IndexOutOfRangeException) {
1247 Assert.IsTrue (errorThrown, "#I63");
1250 bool errorThrown = false;
1252 char[,] c = new Char[2,2];
1253 int[] coords = {4, 1};
1255 } catch (IndexOutOfRangeException) {
1258 Assert.IsTrue (errorThrown, "#I64");
1261 char[,] c1 = new Char[4,6];
1262 for (int i = 0; i < 24; i++) {
1265 c1[first,second] = (char)(((int)'a')+i);
1267 for (int i = 0; i < c1.GetLength(0); i++) {
1268 for (int j = 0; j < c1.GetLength(1); j++) {
1269 int[] coords = {i, j};
1270 Assert.AreEqual (c1[i, j], c1.GetValue(coords), "#I65(" + i + "," + j + ")");
1276 public void TestIndexOf1() {
1278 bool errorThrown = false;
1280 Array.IndexOf(null, "huh?");
1281 } catch (ArgumentNullException) {
1284 Assert.IsTrue (errorThrown, "#J01");
1287 bool errorThrown = false;
1289 char[,] c = new Char[2,2];
1290 Array.IndexOf(c, "huh?");
1291 } catch (RankException) {
1294 Assert.IsTrue (errorThrown, "#J02");
1297 String[] s1 = {"this", "is", "a", "test"};
1298 Assert.AreEqual (-1, Array.IndexOf(s1, null), "#J03");
1299 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing"), "#J04");
1300 Assert.AreEqual (0, Array.IndexOf(s1, "this"), "#J05");
1301 Assert.AreEqual (3, Array.IndexOf(s1, "test"), "#J06");
1305 public void TestIndexOf2() {
1307 bool errorThrown = false;
1309 Array.IndexOf(null, "huh?", 0);
1310 } catch (ArgumentNullException) {
1313 Assert.IsTrue (errorThrown, "#J21");
1316 bool errorThrown = false;
1318 char[,] c = new Char[2,2];
1319 Array.IndexOf(c, "huh?", 0);
1320 } catch (RankException) {
1323 Assert.IsTrue (errorThrown, "#J22");
1326 bool errorThrown = false;
1328 char[] c = new Char[2];
1329 Array.IndexOf(c, "huh?", 3);
1330 } catch (ArgumentOutOfRangeException) {
1333 Assert.IsTrue (errorThrown, "#J23");
1336 String[] s1 = {"this", "is", "really", "a", "test"};
1337 Assert.AreEqual (-1, Array.IndexOf(s1, null, 1), "#J24");
1338 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing", 1), "#J25");
1339 Assert.AreEqual (-1, Array.IndexOf(s1, "this", 1), "#J26");
1340 Assert.AreEqual (1, Array.IndexOf(s1, "is", 1), "#J27");
1341 Assert.AreEqual (4, Array.IndexOf(s1, "test", 1), "#J28");
1345 public void TestIndexOf3() {
1347 bool errorThrown = false;
1349 Array.IndexOf(null, "huh?", 0, 1);
1350 } catch (ArgumentNullException) {
1353 Assert.IsTrue (errorThrown, "#J41");
1356 bool errorThrown = false;
1358 char[,] c = new Char[2,2];
1359 Array.IndexOf(c, "huh?", 0, 1);
1360 } catch (RankException) {
1363 Assert.IsTrue (errorThrown, "#J42");
1366 bool errorThrown = false;
1368 char[] c = new Char[2];
1369 Array.IndexOf(c, "huh?", 3, 1);
1370 } catch (ArgumentOutOfRangeException) {
1373 Assert.IsTrue (errorThrown, "#J43");
1376 bool errorThrown = false;
1378 char[] c = new Char[2];
1379 Array.IndexOf(c, "huh?", 0, 5);
1380 } catch (ArgumentOutOfRangeException) {
1383 Assert.IsTrue (errorThrown, "#J44");
1386 String[] s1 = {"this", "is", "really", "a", "test"};
1387 Assert.AreEqual (-1, Array.IndexOf(s1, null, 1, 3), "#J45");
1388 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing", 1, 3), "#J46");
1389 Assert.AreEqual (-1, Array.IndexOf(s1, "this", 1, 3), "#J47");
1390 Assert.AreEqual (1, Array.IndexOf(s1, "is", 1, 3), "#J48");
1391 Assert.AreEqual (-1, Array.IndexOf(s1, "test", 1, 3), "#J49");
1392 Assert.AreEqual (3, Array.IndexOf(s1, "a", 1, 3), "#J50");
1396 public void TestIndexOf_CustomEqual ()
1398 DataEqual[] test = new DataEqual [] { new DataEqual () };
1399 Assert.AreEqual (0, Array.IndexOf (test, "asdfas", 0));
1401 IList array = (IList)test;
1402 Assert.AreEqual (0, array.IndexOf ("asdfas"));
1406 public void TestLastIndexOf1() {
1408 bool errorThrown = false;
1410 Array.LastIndexOf(null, "huh?");
1411 } catch (ArgumentNullException) {
1414 Assert.IsTrue (errorThrown, "#K01");
1417 bool errorThrown = false;
1419 char[,] c = new Char[2,2];
1420 Array.LastIndexOf(c, "huh?");
1421 } catch (RankException) {
1424 Assert.IsTrue (errorThrown, "#K02");
1427 String[] s1 = {"this", "is", "a", "a", "test"};
1428 Assert.AreEqual (-1, Array.LastIndexOf(s1, null), "#K03");
1429 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing"), "#K04");
1430 Assert.AreEqual (0, Array.LastIndexOf(s1, "this"), "#K05");
1431 Assert.AreEqual (4, Array.LastIndexOf(s1, "test"), "#K06");
1432 Assert.AreEqual (3, Array.LastIndexOf(s1, "a"), "#K07");
1434 Assert.AreEqual (-1, Array.LastIndexOf (new String [0], "foo"));
1438 public void TestLastIndexOf2() {
1440 bool errorThrown = false;
1442 Array.LastIndexOf(null, "huh?", 0);
1443 } catch (ArgumentNullException) {
1446 Assert.IsTrue (errorThrown, "#K21");
1449 bool errorThrown = false;
1451 char[,] c = new Char[2,2];
1452 Array.LastIndexOf(c, "huh?", 0);
1453 } catch (RankException) {
1456 Assert.IsTrue (errorThrown, "#K22");
1459 bool errorThrown = false;
1461 char[] c = new Char[2];
1462 Array.LastIndexOf(c, "huh?", 3);
1463 } catch (ArgumentOutOfRangeException) {
1466 Assert.IsTrue (errorThrown, "#K23");
1469 String[] s1 = {"this", "is", "really", "a", "test"};
1470 Assert.AreEqual (-1, Array.LastIndexOf(s1, null, 3), "#K24");
1471 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing", 3), "#K25");
1472 Assert.AreEqual (-1, Array.LastIndexOf(s1, "test", 3), "#K26");
1473 Assert.AreEqual (3, Array.LastIndexOf(s1, "a", 3), "#K27");
1474 Assert.AreEqual (0, Array.LastIndexOf(s1, "this", 3), "#K28");
1478 public void TestLastIndexOf3() {
1480 bool errorThrown = false;
1482 Array.LastIndexOf(null, "huh?", 0, 1);
1483 } catch (ArgumentNullException) {
1486 Assert.IsTrue (errorThrown, "#K41");
1489 bool errorThrown = false;
1491 char[,] c = new Char[2,2];
1492 Array.LastIndexOf(c, "huh?", 0, 1);
1493 } catch (RankException) {
1496 Assert.IsTrue (errorThrown, "#K42");
1499 bool errorThrown = false;
1501 char[] c = new Char[2];
1502 Array.LastIndexOf(c, "huh?", 3, 1);
1503 } catch (ArgumentOutOfRangeException) {
1506 Assert.IsTrue (errorThrown, "#K43");
1509 bool errorThrown = false;
1511 char[] c = new Char[2];
1512 Array.LastIndexOf(c, "huh?", 0, 5);
1513 } catch (ArgumentOutOfRangeException) {
1516 Assert.IsTrue (errorThrown, "#K44");
1519 String[] s1 = {"this", "is", "really", "a", "test"};
1520 Assert.AreEqual (-1, Array.LastIndexOf(s1, null, 3, 3), "#K45");
1521 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing", 3, 3), "#K46");
1522 Assert.AreEqual (-1, Array.LastIndexOf(s1, "this", 3, 3), "#K47");
1523 Assert.AreEqual (1, Array.LastIndexOf(s1, "is", 3, 3), "#K48");
1524 Assert.AreEqual (-1, Array.LastIndexOf(s1, "test", 3, 3), "#K49");
1525 Assert.AreEqual (3, Array.LastIndexOf(s1, "a", 3, 3), "#K50");
1529 public void TestLastIndexOf4 ()
1531 short [] a = new short [] { 19, 238, 317, 6, 565, 0, -52, 60, -563, 753, 238, 238};
1533 Array.LastIndexOf (a, (object)16, -1);
1534 NUnit.Framework.Assert.Fail ("#1");
1535 } catch (ArgumentOutOfRangeException) { }
1539 Array.LastIndexOf<short> (a, 16, -1);
1540 NUnit.Framework.Assert.Fail ("#2");
1541 } catch (ArgumentOutOfRangeException) { }
1546 public void TestLastIndexOf5 ()
1548 char [] a = new char [] {'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'j', 'i', 'h'};
1553 for (int i = a.Length - 1; i >= 0 ; i--) {
1555 retval = Array.LastIndexOf(a, a [i], i, i + 1);
1559 Assert.IsTrue (!error);
1563 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1564 public void LastIndexOf_StartIndexOverflow ()
1566 // legal - no exception
1567 byte[] array = new byte [16];
1568 Array.LastIndexOf (array, this, Int32.MaxValue, 1);
1572 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1573 public void LastIndexOf_CountOverflow ()
1575 // legal - no exception
1576 byte[] array = new byte [16];
1577 Array.LastIndexOf (array, this, 1, Int32.MaxValue);
1581 public void LastIndexOf_0LengthArray ()
1583 Array array = Array.CreateInstance (typeof (char), 0);
1584 int idx = Array.LastIndexOf (array, (object) null, -1, 0);
1585 Assert.IsTrue (idx == -1, "#01");
1586 idx = Array.LastIndexOf (array, (object) null, -1, 10);
1587 Assert.IsTrue (idx == -1, "#02");
1588 idx = Array.LastIndexOf (array, (object) null, -100, 10);
1589 Assert.IsTrue (idx == -1, "#02");
1591 array = Array.CreateInstance (typeof (char), 1);
1593 Array.LastIndexOf (array, (object) null, -1, 0);
1594 Assert.Fail ("#04");
1595 } catch (ArgumentOutOfRangeException e) {
1598 Array.LastIndexOf (array, (object) null, -1, 10);
1599 Assert.Fail ("#05");
1600 } catch (ArgumentOutOfRangeException e) {
1603 Array.LastIndexOf (array, (object) null, -100, 10);
1604 Assert.Fail ("#06");
1605 } catch (ArgumentOutOfRangeException e) {
1610 public void TestReverse() {
1612 bool errorThrown = false;
1614 Array.Reverse(null);
1615 } catch (ArgumentNullException) {
1618 Assert.IsTrue (errorThrown, "#L01");
1621 bool errorThrown = false;
1623 char[,] c = new Char[2,2];
1625 } catch (RankException) {
1628 Assert.IsTrue (errorThrown, "#L02");
1631 char[] c1 = {'a', 'b', 'c', 'd'};
1633 Assert.AreEqual ('d', c1[0], "#L03");
1634 Assert.AreEqual ('c', c1[1], "#L04");
1635 Assert.AreEqual ('b', c1[2], "#L05");
1636 Assert.AreEqual ('a', c1[3], "#L06");
1639 bool errorThrown = false;
1641 Array.Reverse(null, 0, 0);
1642 } catch (ArgumentNullException) {
1645 Assert.IsTrue (errorThrown, "#L07");
1648 bool errorThrown = false;
1650 char[,] c = new Char[2,2];
1651 Array.Reverse(c, 0, 0);
1652 } catch (RankException) {
1655 Assert.IsTrue (errorThrown, "#L08");
1658 //bool errorThrown = false;
1660 // char[] c = new Char[2];
1661 // Array.Reverse(c, 0, 3);
1662 //} catch (ArgumentOutOfRangeException) {
1663 // errorThrown = true;
1665 //Assert.IsTrue (errorThrown, "#L09");
1668 //bool errorThrown = false;
1670 // char[] c = new Char[2];
1671 // Array.Reverse(c, 3, 0);
1672 //} catch (ArgumentOutOfRangeException) {
1673 // errorThrown = true;
1675 //Assert.IsTrue (errorThrown, "#L10");
1678 char[] c2 = { 'a', 'b', 'c', 'd'};
1679 Array.Reverse(c2, 1, 2);
1680 Assert.AreEqual ('a', c2[0], "#L11");
1681 Assert.AreEqual ('c', c2[1], "#L12");
1682 Assert.AreEqual ('b', c2[2], "#L13");
1683 Assert.AreEqual ('d', c2[3], "#L14");
1687 public void TestSetValue1() {
1689 bool errorThrown = false;
1691 char[,] c = new Char[2,2];
1692 c.SetValue("buh", 1);
1693 } catch (ArgumentException) {
1696 Assert.IsTrue (errorThrown, "#M01");
1699 bool errorThrown = false;
1701 char[] c = {'a', 'b', 'c'};
1702 c.SetValue("buh", -1);
1703 } catch (IndexOutOfRangeException) {
1706 Assert.IsTrue (errorThrown, "#M02");
1709 bool errorThrown = false;
1711 char[] c = {'a', 'b', 'c'};
1712 c.SetValue("buh", 4);
1713 } catch (IndexOutOfRangeException) {
1716 Assert.IsTrue (errorThrown, "#M03");
1719 char[] c1 = {'a', 'b', 'c', 'd'};
1720 char[] c2 = new char[4];
1721 for (int i = 0; i < c1.Length; i++) {
1722 c2.SetValue(c1[i], i);
1724 for (int i = 0; i < c1.Length; i++) {
1725 Assert.AreEqual (c1[i], c2[i], "#M04(" + i + ")");
1728 int[] c3 = { 1, 2, 3 };
1729 long[] c4 = new long [3];
1731 for (int i = 0; i < c3.Length; i++)
1732 c4.SetValue (c3 [i], i);
1736 } catch (Exception e) {
1737 Assert.Fail ("c3.CopyTo(): e=" + e);
1739 for (int i = 0; i < c3.Length; i++)
1740 Assert.IsTrue (c3[i] == c4[i], "#M05(" + i + ")");
1742 Object[] c5 = new Object [3];
1743 long[] c6 = new long [3];
1747 } catch (Exception e) {
1748 Assert.Fail ("c4.CopyTo(): e=" + e);
1753 } catch (Exception e) {
1754 Assert.Fail ("c5.CopyTo(): e=" + e);
1756 // for (int i = 0; i < c5.Length; i++)
1757 // Assert.IsTrue (c5[i] == c6[i], "#M06(" + i + ")");
1761 public void TestSetValue2() {
1763 bool errorThrown = false;
1765 char[] c = new Char[2];
1766 c.SetValue("buh", 1,1);
1767 } catch (ArgumentException) {
1770 Assert.IsTrue (errorThrown, "#M21");
1773 bool errorThrown = false;
1775 char[,] c = new Char[2,2];
1776 c.SetValue("buh", -1, 1);
1777 } catch (IndexOutOfRangeException) {
1780 Assert.IsTrue (errorThrown, "#M22");
1783 bool errorThrown = false;
1785 char[,] c = new Char[2,2];
1786 c.SetValue("buh", 4,1);
1787 } catch (IndexOutOfRangeException) {
1790 Assert.IsTrue (errorThrown, "#M23");
1793 char[,] c1 = new Char[4,6];
1794 char[,] c2 = new Char[4,6];
1795 for (int i = 0; i < 24; i++) {
1798 c1[first,second] = (char)(((int)'a')+i);
1799 c2.SetValue(c1[first,second], first, second);
1801 for (int i = 0; i < c1.GetLength(0); i++) {
1802 for (int j = 0; j < c1.GetLength(1); j++) {
1803 Assert.AreEqual (c1[i, j], c2[i, j], "#M24(" + i + "," + j + ")");
1809 public void TestSetValue3() {
1811 bool errorThrown = false;
1813 char[] c = new Char[2];
1814 c.SetValue("buh", 1,1,1);
1815 } catch (ArgumentException) {
1818 Assert.IsTrue (errorThrown, "#M41");
1821 bool errorThrown = false;
1823 char[,,] c = new Char[2,2,2];
1824 c.SetValue("buh", -1, 1, 1);
1825 } catch (IndexOutOfRangeException) {
1828 Assert.IsTrue (errorThrown, "#M42");
1831 bool errorThrown = false;
1833 char[,,] c = new Char[2,2,2];
1834 c.SetValue("buh", 4,1,1);
1835 } catch (IndexOutOfRangeException) {
1838 Assert.IsTrue (errorThrown, "#M43");
1841 char[,,] c1 = new Char[4,2,3];
1842 char[,,] c2 = new Char[4,2,3];
1843 for (int i = 0; i < 24; i++) {
1845 int remains = i % 6;
1846 int second = remains / 3;
1847 int third = remains % 3;
1848 c1[first,second, third] = (char)(((int)'a')+i);
1849 c2.SetValue(c1[first, second, third], first, second, third);
1851 for (int i = 0; i < c1.GetLength(0); i++) {
1852 for (int j = 0; j < c1.GetLength(1); j++) {
1853 for (int k = 0; k < c1.GetLength(2); k++) {
1854 Assert.AreEqual (c1[i, j, k], c2[i, j, k], "#M44(" + i + "," + j + " )");
1862 [ExpectedException (typeof (ArgumentNullException))]
1864 [ExpectedException (typeof (NullReferenceException))]
1866 public void TestSetValueLongArray ()
1868 char[] c = new Char[2];
1869 c.SetValue("buh", (long [])null);
1873 public void TestSetValueN() {
1875 bool errorThrown = false;
1877 char[] c = new Char[2];
1878 c.SetValue("buh", (int [])null);
1879 } catch (ArgumentNullException) {
1882 Assert.IsTrue (errorThrown, "#M61a");
1885 bool errorThrown = false;
1887 char[] c = new Char[2];
1888 int[] coords = {1, 1};
1889 c.SetValue("buh", coords);
1890 } catch (ArgumentException) {
1893 Assert.IsTrue (errorThrown, "#M62");
1896 bool errorThrown = false;
1898 char[,] c = new Char[2,2];
1899 int[] coords = {-1, 1};
1900 c.SetValue("buh", coords);
1901 } catch (IndexOutOfRangeException) {
1904 Assert.IsTrue (errorThrown, "#M63");
1907 bool errorThrown = false;
1909 char[,] c = new Char[2,2];
1910 int[] coords = {4, 1};
1911 c.SetValue("buh", coords);
1912 } catch (IndexOutOfRangeException) {
1915 Assert.IsTrue (errorThrown, "#M64");
1918 char[,] c1 = new Char[4,6];
1919 char[,] c2 = new Char[4,6];
1920 for (int i = 0; i < 24; i++) {
1923 c1[first,second] = (char)(((int)'a')+i);
1924 int[] coords = {first, second};
1925 c2.SetValue(c1[first,second], coords);
1927 for (int i = 0; i < c1.GetLength(0); i++) {
1928 for (int j = 0; j < c1.GetLength(1); j++) {
1929 Assert.AreEqual (c1[i, j], c2[i, j], "#M65(" + i + "," + j + ")");
1935 public void TestSetValue4() {
1937 int[] c1 = { 1, 2, 3 };
1938 long[] c2 = new long [3];
1940 for (int i = 0; i < c1.Length; i++)
1941 c2.SetValue (c1 [i], i);
1943 for (int i = 0; i < c1.Length; i++) {
1944 Assert.IsTrue (c1[i] == c2[i], "#M81(" + i + ")");
1945 Assert.AreEqual (typeof (long), c2[i].GetType (), "#M82(" + i + ")");
1949 long[] c1 = { 1, 2, 3 };
1950 int[] c2 = new int [3];
1951 bool errorThrown = false;
1953 c2.SetValue (c1 [0], 0);
1954 } catch (ArgumentException) {
1957 Assert.IsTrue (errorThrown, "#M83");
1960 int[] c1 = { 1, 2, 3 };
1961 Object[] c2 = new Object [3];
1963 for (int i = 0; i < c1.Length; i++)
1964 c2.SetValue (c1 [i], i);
1966 for (int i = 0; i < c1.Length; i++)
1967 Assert.AreEqual (c1[i], Convert.ToInt32 (c2[i]), "#M84(" + i + ")");
1970 Object[] c1 = new Object [3];
1971 Object[] c2 = new Object [3];
1972 c1[0] = new Object ();
1974 for (int i = 0; i < c1.Length; i++)
1975 c2.SetValue (c1 [i], i);
1977 for (int i = 0; i < c1.Length; i++)
1978 Assert.AreEqual (c1[i], c2[i], "#M85(" + i + ")");
1981 Object[] c1 = new Object [3];
1982 string[] c2 = new String [3];
1983 string test = "hello";
1986 c2.SetValue (c1 [0], 0);
1987 Assert.AreEqual (c1[0], c2[0], "#M86");
1988 Assert.AreEqual ("hello", c2[0], "#M87");
1991 char[] c1 = { 'a', 'b', 'c' };
1992 string[] c2 = new string [3];
1994 c2.SetValue (c1 [0], 0);
1995 Assert.Fail ("#M88");
1996 } catch (InvalidCastException) {}
1999 Single[] c1 = { 1.2F, 2.3F, 3.4F, 4.5F };
2000 long[] c2 = new long [3];
2002 c2.SetValue (c1 [0], 0);
2003 Assert.Fail ("#M89");
2004 } catch (ArgumentException) {}
2032 UInt32 v11 = 235354;
2033 UInt64 v12 = 234552;
2035 Object[] va1 = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12 };
2036 Object[] va2 = { "true", "1", "a", "-1.2", "-32", "-234", "-34523", "-1",
2037 "-4.8F", "24234", "235354", "234552" };
2039 Object[][] vt = { va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1 };
2042 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2043 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
2044 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
2045 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
2046 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1,
2047 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1,
2048 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,
2049 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1,
2050 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1,
2051 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
2052 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0,
2053 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0
2058 for (int i = 0; i < types.Length; i++) {
2059 for (int j = 0; j < types.Length; j++) {
2060 Array array = Array.CreateInstance (types [j], 2);
2062 Object value = vt[j][i];
2064 bool errorThrown = false;
2066 array.SetValue (value, 0);
2067 } catch (ArgumentException) {
2071 int ex_index = (i * types.Length) + j;
2073 Assert.AreEqual (errorThrown, arg_ex [ex_index] == 1, "#M90(" + types [i] + "," + types [j] + ")");
2077 for (int i = 0; i < types.Length; i++) {
2078 String[] array = new String [2];
2080 Object value = va1 [i];
2082 bool errorThrown = false;
2084 array.SetValue (value, 0);
2085 } catch (InvalidCastException) {
2089 Assert.IsTrue (errorThrown, "#M91(" + types [i] + ")");
2092 for (int i = 0; i < types.Length; i++) {
2093 Array array = Array.CreateInstance (types [i], 2);
2095 Object value = va2 [i];
2097 bool errorThrown = false;
2099 array.SetValue (value, 0);
2100 } catch (InvalidCastException) {
2104 Assert.IsTrue (errorThrown, "#M92(" + types [i] + ")");
2107 for (int i = 0; i < types.Length; i++) {
2108 Array array = Array.CreateInstance (types [i], 2);
2110 Object value = null;
2112 bool errorThrown = false;
2114 array.SetValue (value, 0);
2115 } catch (InvalidCastException) {
2119 Assert.IsTrue (!errorThrown, "#M93(" + types [i] + ")");
2124 for (int i = 0; i < types.Length; i++) {
2125 for (int j = 0; j < types.Length; j++) {
2126 Array source = Array.CreateInstance (types [i], 2);
2127 Array array = Array.CreateInstance (types [j], 2);
2129 source.SetValue (vt[j][i], 0);
2130 source.SetValue (vt[j][i], 1);
2132 bool errorThrown = false;
2134 Array.Copy (source, array, 2);
2135 } catch (ArrayTypeMismatchException) {
2139 int ex_index = (i * types.Length) + j;
2141 Assert.AreEqual (errorThrown, arg_ex [ex_index] == 1, "#M94(" + types [i] + "," + types [j] + ")");
2145 for (int i = 0; i < types.Length; i++) {
2146 Array source = Array.CreateInstance (types [i], 2);
2147 String[] array = new String [2];
2149 source.SetValue (va1 [i], 0);
2150 source.SetValue (va1 [i], 1);
2152 bool errorThrown = false;
2154 Array.Copy (source, array, 2);
2155 } catch (ArrayTypeMismatchException) {
2159 Assert.IsTrue (errorThrown, "#M95(" + types [i] + ")");
2162 for (int i = 0; i < types.Length; i++) {
2163 String[] source = new String [2];
2164 Array array = Array.CreateInstance (types [i], 2);
2166 source.SetValue (va2 [i], 0);
2167 source.SetValue (va2 [i], 1);
2169 bool errorThrown = false;
2171 Array.Copy (source, array, 2);
2172 } catch (ArrayTypeMismatchException) {
2176 Assert.IsTrue (errorThrown, "#M96(" + types [i] + ")");
2182 public void TestSort() {
2184 bool errorThrown = false;
2187 } catch (ArgumentNullException) {
2190 Assert.IsTrue (errorThrown, "#N01");
2193 bool errorThrown = false;
2195 Array.Sort(null, 0, 1);
2196 } catch (ArgumentNullException) {
2199 Assert.IsTrue (errorThrown, "#N02");
2202 bool errorThrown = false;
2204 char[] c1 = new Char[2];
2205 Array.Sort(null, c1);
2206 } catch (ArgumentNullException) {
2209 Assert.IsTrue (errorThrown, "#N03");
2212 bool errorThrown = false;
2214 char[] c1 = new Char[2];
2215 Array.Sort(null, c1, 0, 1);
2216 } catch (ArgumentNullException) {
2219 Assert.IsTrue (errorThrown, "#N04");
2223 char[] arr = {'d', 'b', 'f', 'e', 'a', 'c'};
2226 Array.Sort (null, 0, 1);
2227 Assert.Fail ("#N" + tc.ToString ());
2229 catch (ArgumentException) {}
2230 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2234 Array.Sort (arr, -1, 3);
2235 Assert.Fail ("#N" + tc.ToString ());
2237 catch (ArgumentException) {}
2238 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2242 Array.Sort (arr, 1, -3);
2243 Assert.Fail ("#N" + tc.ToString ());
2245 catch (ArgumentException) {}
2246 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2250 Array.Sort (arr, arr.Length, arr.Length + 2);
2251 Assert.Fail ("#N" + tc.ToString ());
2253 catch (ArgumentException) {}
2254 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2257 // note: null second array => just sort first array
2258 char[] starter = {'d', 'b', 'f', 'e', 'a', 'c'};
2259 int[] starter1 = {1,2,3,4,5,6};
2261 char[] c1 = (char[])starter.Clone();
2263 Assert.AreEqual ('a', c1[0], "#N21");
2264 Assert.AreEqual ('b', c1[1], "#N22");
2265 Assert.AreEqual ('c', c1[2], "#N23");
2266 Assert.AreEqual ('d', c1[3], "#N24");
2267 Assert.AreEqual ('e', c1[4], "#N25");
2268 Assert.AreEqual ('f', c1[5], "#N26");
2271 char[] c1 = (char[])starter.Clone();
2272 int[] i1 = (int[])starter1.Clone();
2274 Assert.AreEqual ('a', c1[0], "#N41");
2275 Assert.AreEqual ('b', c1[1], "#N42");
2276 Assert.AreEqual ('c', c1[2], "#N43");
2277 Assert.AreEqual ('d', c1[3], "#N44");
2278 Assert.AreEqual ('e', c1[4], "#N45");
2279 Assert.AreEqual ('f', c1[5], "#N46");
2280 Assert.AreEqual (5, i1[0], "#N47");
2281 Assert.AreEqual (2, i1[1], "#N48");
2282 Assert.AreEqual (6, i1[2], "#N49");
2283 Assert.AreEqual (1, i1[3], "#N50");
2284 Assert.AreEqual (4, i1[4], "#N51");
2285 Assert.AreEqual (3, i1[5], "#N52");
2288 char[] c1 = (char[])starter.Clone();
2289 Array.Sort(c1, 1, 4);
2290 Assert.AreEqual ('d', c1[0], "#N61");
2291 Assert.AreEqual ('a', c1[1], "#N62");
2292 Assert.AreEqual ('b', c1[2], "#N63");
2293 Assert.AreEqual ('e', c1[3], "#N64");
2294 Assert.AreEqual ('f', c1[4], "#N65");
2295 Assert.AreEqual ('c', c1[5], "#N66");
2298 char[] c1 = (char[])starter.Clone();
2299 int[] i1 = (int[])starter1.Clone();
2300 Array.Sort(c1, i1, 1, 4);
2301 Assert.AreEqual ('d', c1[0], "#N81");
2302 Assert.AreEqual ('a', c1[1], "#N82");
2303 Assert.AreEqual ('b', c1[2], "#N83");
2304 Assert.AreEqual ('e', c1[3], "#N84");
2305 Assert.AreEqual ('f', c1[4], "#N85");
2306 Assert.AreEqual ('c', c1[5], "#N86");
2307 Assert.AreEqual (1, i1[0], "#N87");
2308 Assert.AreEqual (5, i1[1], "#N88");
2309 Assert.AreEqual (2, i1[2], "#N89");
2310 Assert.AreEqual (4, i1[3], "#N90");
2311 Assert.AreEqual (3, i1[4], "#N91");
2312 Assert.AreEqual (6, i1[5], "#N92");
2317 public void SortNonGenericDoubleItems () {
2318 double[] doubleValues = new double[11];
2320 doubleValues[0] = 0.221788066253601;
2321 doubleValues[1] = 0.497278285809481;
2322 doubleValues[2] = 0.100565033883643;
2323 doubleValues[3] = 0.0433309347749905;
2324 doubleValues[4] = 0.00476726438463812;
2325 doubleValues[5] = 0.1354609735456;
2326 doubleValues[6] = 0.57690356588135;
2327 doubleValues[7] = 0.466239434334826;
2328 doubleValues[8] = 0.409741461978934;
2329 doubleValues[9] = 0.0112412763949565;
2330 doubleValues[10] = 0.668704347674307;
2332 int[] indices = new int[11];
2345 Array.Sort ((Array)doubleValues, (Array)indices);
2346 Assert.AreEqual (4, indices [0]);
2350 public void TestInitializeEmpty()
2362 Assert.IsTrue (!catched, "#TI01");
2366 public void TestInitializeInt()
2371 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2373 Assert.AreEqual (a[i], b[i], "#TI02 " + i);
2378 public void TestInitializeDouble()
2380 double[] a = {1.0,2.0,0.0};
2382 double[] b = {1.0,2.0,0.0};
2383 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2385 Assert.AreEqual (a[i], b[i], "#TI03 " + i);
2390 public void TestInitializeFloat()
2392 float[] a = {1.0F,2.0F,0.0F};
2394 float[] b = {1.0F,2.0F,0.0F};
2395 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2397 Assert.AreEqual (a[i], b[i], "#TI04 " + i);
2402 public void TestInitializeChar()
2404 char[] a = {'1','.','0','F','2','.','0','F'};
2406 char[] b = {'1','.','0','F','2','.','0','F'};
2407 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2409 Assert.AreEqual (a[i], b[i], "#TI05 " + i);
2414 public void TestInitializeString()
2416 string[] a = {"hola","adios","menos","mas"};
2418 string[] b = {"hola","adios","menos","mas"};
2419 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2421 Assert.AreEqual (a[i], b[i], "#TI06 " + i);
2426 public void TestInitializeEnum()
2428 enua[] a = {enua.hola,enua.adios,enua.menos,enua.mas};
2430 enua[] b = {enua.hola,enua.adios,enua.menos,enua.mas};
2431 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2433 Assert.AreEqual (a[i], b[i], "#TI07 " + i);
2438 public void TestInitializeIntNI()
2440 int[] a = new int[20];
2444 Assert.AreEqual (b, 0, "#TI08");
2449 public void TestInitializeCharNI()
2451 char[] a = new char[20];
2453 foreach(char b in a)
2455 Assert.AreEqual (b, 0, "#TI09");
2460 public void TestInitializeDoubleNI()
2462 double[] a = new double[20];
2464 foreach(double b in a)
2466 Assert.AreEqual (b, 0.0, "#TI09");
2471 public void TestInitializeStringNI()
2473 string[] a = new string[20];
2475 foreach(string b in a)
2477 Assert.AreEqual (b, null, "#TI10");
2482 public void TestInitializeObjectNI()
2484 object[] a = new object[20];
2486 foreach(object b in a)
2488 Assert.AreEqual (b, null, "#TI11");
2493 public void TestInitializeAClassNI()
2495 AClass[] a = new AClass[20];
2497 foreach(AClass b in a)
2499 Assert.AreEqual (b, null, "#TI12");
2505 public void TestInitializeAStructNI()
2507 AStruct[] a = new AStruct[20];
2509 foreach(AStruct b in a)
2511 Assert.AreEqual (b, new AStruct(), "#TI14");
2516 public void TestInitializeAStruct()
2518 AStruct[] a = new AStruct[3];
2522 AStruct[] b = new AStruct[3];
2525 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2527 Assert.AreEqual (a[i], b[i], "#TI15 " + i);
2532 public void TestInitializeDateTimeNI()
2534 DateTime[] a = new DateTime[20];
2536 foreach(DateTime b in a)
2538 Assert.AreEqual (b, new DateTime(), "#TI16");
2543 [ExpectedException (typeof (ArgumentNullException))]
2544 public void MoreSort1 ()
2546 Array.Sort (null, 0, 1);
2550 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2551 public void MoreSort2 ()
2553 Array.Sort (arrsort, -1, 3);
2557 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2558 public void MoreSort3 ()
2560 Array.Sort (arrsort, 1, -3);
2564 [ExpectedException (typeof (ArgumentException))]
2565 public void MoreSort4 ()
2567 Array.Sort (arrsort, arrsort.Length, arrsort.Length + 2);
2571 [ExpectedException (typeof (RankException))]
2572 public void MoreSort5 ()
2574 char [,] arr = new char [,] {{'a'}, {'b'}};
2575 Array.Sort (arr, 0, 1);
2579 public void MoreSort6 ()
2581 Array.Sort (arrsort, 0, 0);
2585 [ExpectedException (typeof (ArgumentException))]
2586 public void MoreSort7 ()
2588 Array.Sort (arrsort, arrsort.Length - 1, 2);
2592 [ExpectedException (typeof (ArgumentException))]
2593 public void MoreSort8 ()
2595 Array.Sort (arrsort, 0, arrsort.Length + 1);
2599 public void MoreSort9 ()
2601 Array.Sort (arrsort, null, 0, arrsort.Length, null);
2605 [ExpectedException (typeof (InvalidOperationException))]
2606 public void MoreSort10 ()
2608 object [] array = {true, 'k', SByte.MinValue, Byte.MinValue, (short) 2, 634, (long) 436, (float) 1.1, 1.23, "Hello World"};
2609 Array.Sort (array, (IComparer) null);
2612 [Test] // bug #81941
2615 double [] a = new double [2] { 0.9, 0.3 };
2616 uint [] b = new uint [2] { 4, 7 };
2618 Assert.AreEqual (0.3, a [0], "#1");
2619 Assert.AreEqual (0.9, a [1], "#2");
2620 Assert.AreEqual (7, b [0], "#3");
2621 Assert.AreEqual (4, b [1], "#4");
2625 public void ClearJaggedArray ()
2627 byte[][] matrix = new byte [8][];
2628 for (int i=0; i < 8; i++) {
2629 matrix [i] = new byte [8];
2630 for (int j=0; j < 8; j++) {
2634 Array.Clear (matrix, 0, 8);
2635 for (int i=0; i < 8; i++) {
2636 Assert.IsNull (matrix [i], i.ToString ());
2641 public void ClearMultidimentionalArray ()
2643 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2644 Array.Clear (matrix, 0, 2);
2645 Assert.AreEqual (0, matrix [0, 0], "0,0");
2646 Assert.AreEqual (0, matrix [0, 1], "0,1");
2647 Assert.AreEqual (2, matrix [1, 0], "1,0");
2648 Assert.AreEqual (2, matrix [1, 1], "1,1");
2652 [ExpectedException (typeof (IndexOutOfRangeException))]
2653 public void ClearOutsideMultidimentionalArray ()
2655 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2656 Array.Clear (matrix, 0, 5);
2660 [ExpectedException (typeof (IndexOutOfRangeException))]
2661 public void Clear_IndexOverflow ()
2663 byte[] array = new byte [16];
2664 Array.Clear (array, 4, Int32.MaxValue);
2668 [ExpectedException (typeof (IndexOutOfRangeException))]
2669 public void Clear_LengthOverflow ()
2671 byte[] array = new byte [16];
2672 Array.Clear (array, Int32.MaxValue, 4);
2676 [ExpectedException (typeof (ArgumentException))]
2677 public void Copy_SourceIndexOverflow ()
2679 byte[] array = new byte [16];
2680 Array.Copy (array, Int32.MaxValue, array, 8, 8);
2684 [ExpectedException (typeof (ArgumentException))]
2685 public void Copy_DestinationIndexOverflow ()
2687 byte[] array = new byte [16];
2688 Array.Copy (array, 8, array, Int32.MaxValue, 8);
2692 [ExpectedException (typeof (ArgumentException))]
2693 public void Copy_LengthOverflow ()
2695 byte[] array = new byte [16];
2696 Array.Copy (array, 8, array, 8, Int32.MaxValue);
2700 [ExpectedException (typeof (ArgumentException))]
2701 public void Reverse_IndexOverflow ()
2703 byte[] array = new byte [16];
2704 Array.Reverse (array, Int32.MaxValue, 8);
2708 [ExpectedException (typeof (ArgumentException))]
2709 public void Reverse_LengthOverflow ()
2711 byte[] array = new byte [16];
2712 Array.Reverse (array, 8, Int32.MaxValue);
2715 public struct CharX : IComparable {
2718 public CharX (char c)
2723 public int CompareTo (object obj)
2726 return c.CompareTo (((CharX) obj).c);
2728 return c.CompareTo (obj);
2733 public void BinarySearch_ArgPassingOrder ()
2736 // This tests that arguments are passed to the comprer in the correct
2737 // order. The IComparable of the *array* elements must get called, not
2738 // that of the search object.
2740 CharX [] x = { new CharX ('a'), new CharX ('b'), new CharX ('c') };
2741 Assert.AreEqual (1, Array.BinarySearch (x, 'b'));
2744 class Comparer: IComparer {
2746 private bool called = false;
2748 public bool Called {
2750 bool result = called;
2756 public int Compare (object x, object y)
2764 public void BinarySearch1_EmptyList ()
2766 int[] array = new int[0];
2767 Assert.AreEqual (- 1, Array.BinarySearch (array, 0), "BinarySearch");
2771 public void BinarySearch2_EmptyList ()
2773 int[] array = new int[0];
2774 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, 0), "BinarySearch");
2778 public void BinarySearch3_EmptyList ()
2780 Comparer comparer = new Comparer ();
2781 int[] array = new int[0];
2782 Assert.AreEqual (-1, Array.BinarySearch (array, 0, comparer), "BinarySearch");
2783 // bug 77030 - the comparer isn't called for an empty array/list
2784 Assert.IsTrue (!comparer.Called, "Called");
2788 public void BinarySearch4_EmptyList ()
2790 Comparer comparer = new Comparer ();
2791 int[] array = new int[0];
2792 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, comparer), "BinarySearch");
2793 // bug 77030 - the comparer isn't called for an empty array/list
2794 Assert.IsTrue (!comparer.Called, "Called");
2799 [ExpectedException (typeof (ArgumentNullException))]
2800 public void AsReadOnly_NullArray ()
2802 Array.AsReadOnly <int> (null);
2806 public void ReadOnly_Count ()
2808 Assert.AreEqual (10, Array.AsReadOnly (new int [10]).Count);
2812 public void ReadOnly_Contains ()
2814 int[] arr = new int [2];
2817 IList<int> a = Array.AsReadOnly (arr);
2819 Assert.IsTrue (a.Contains (3));
2820 Assert.IsTrue (!a.Contains (6));
2824 public void ReadOnly_IndexOf ()
2826 int[] arr = new int [2];
2829 IList<int> a = Array.AsReadOnly (arr);
2831 Assert.AreEqual (0, a.IndexOf (3));
2832 Assert.AreEqual (1, a.IndexOf (5));
2833 Assert.AreEqual (-1, a.IndexOf (6));
2837 public void ReadOnly_Indexer ()
2839 int[] arr = new int [2];
2842 IList<int> a = Array.AsReadOnly (arr);
2844 Assert.AreEqual (3, a [0]);
2845 Assert.AreEqual (5, a [1]);
2847 /* Check that modifications to the original array are visible */
2849 Assert.AreEqual (6, a [0]);
2853 public void ReadOnly_Enumerator ()
2855 int[] arr = new int [10];
2857 for (int i = 0; i < 10; ++i)
2861 foreach (int i in Array.AsReadOnly (arr))
2864 Assert.AreEqual (45, sum);
2868 public void ReadOnly_CopyTo ()
2870 int[] arr = new int [2];
2873 IList<int> a = Array.AsReadOnly (arr);
2875 int[] arr2 = new int [3];
2878 Assert.AreEqual (0, arr2 [0]);
2879 Assert.AreEqual (3, arr2 [1]);
2880 Assert.AreEqual (5, arr2 [2]);
2884 public void Resize ()
2886 int [] arr = new int [] { 1, 3, 5 };
2887 Array.Resize <int> (ref arr, 3);
2888 Assert.AreEqual (3, arr.Length, "#A1");
2889 Assert.AreEqual (1, arr [0], "#A2");
2890 Assert.AreEqual (3, arr [1], "#A3");
2891 Assert.AreEqual (5, arr [2], "#A4");
2893 Array.Resize <int> (ref arr, 2);
2894 Assert.AreEqual (2, arr.Length, "#B1");
2895 Assert.AreEqual (1, arr [0], "#B2");
2896 Assert.AreEqual (3, arr [1], "#B3");
2898 Array.Resize <int> (ref arr, 4);
2899 Assert.AreEqual (4, arr.Length, "#C1");
2900 Assert.AreEqual (1, arr [0], "#C2");
2901 Assert.AreEqual (3, arr [1], "#C3");
2902 Assert.AreEqual (0, arr [2], "#C4");
2903 Assert.AreEqual (0, arr [3], "#C5");
2907 public void Resize_null ()
2910 Array.Resize (ref arr, 10);
2911 Assert.AreEqual (arr.Length, 10);
2915 public void Test_ContainsAndIndexOf_EquatableItem ()
2917 EquatableClass[] list = new EquatableClass[] {new EquatableClass (0), new EquatableClass (1), new EquatableClass (0)};
2919 Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, list[0]), "#0");
2920 Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, new EquatableClass (0)), "#1");
2921 Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, list[0]), "#2");
2922 Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, new EquatableClass (0)), "#3");
2925 public class EquatableClass : IEquatable<EquatableClass>
2928 public EquatableClass (int x)
2933 public bool Equals (EquatableClass other)
2935 return this._x == other._x;
2940 public void AsIList ()
2942 IList<int> arr = new int [10];
2944 Assert.AreEqual (5, arr [0]);
2946 IList<FooStruct> arr2 = new FooStruct [10];
2947 FooStruct s = new FooStruct ();
2952 Assert.AreEqual (11, s.i);
2953 Assert.AreEqual (22, s.j);
2955 IList<string> arr3 = new string [10];
2957 Assert.AreEqual ("ABC", arr3 [5]);
2964 #if !TARGET_JVM // BugBUG: T[] is not yet ICollection<T> under TARGET_JVM
2967 public void ICollectionNull ()
2969 ICollection<object> test;
2971 test = new List<object>();
2972 Assert.AreEqual (test.Contains (null), false, "list<o>");
2974 test = new object[] {};
2975 Assert.AreEqual (test.Contains (null), false, "empty array");
2977 test = new object[] {null};
2978 Assert.AreEqual (test.Contains (null), true, "array with null");
2980 test = new List<object>(test);
2981 Assert.AreEqual (test.Contains (null), true, "List<object> with test");
2983 test = new object[] {new object()};
2984 Assert.AreEqual (test.Contains (null), false, "array with object");
2986 test = new List<object>(test);
2987 Assert.AreEqual (test.Contains (null), false, "array with test");
2989 #endif // TARGET_JVM
2994 enum ByteEnum : byte {}
2995 enum IntEnum : int {}
2998 public void TestByteEnumArrayToByteArray ()
3000 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
3001 byte[] b = new byte[a.Length];
3006 public void TestByteEnumArrayToIntArray ()
3008 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
3009 int[] b = new int[a.Length];
3014 [ExpectedException (typeof (ArrayTypeMismatchException))]
3015 public void TestIntEnumArrayToByteArray ()
3017 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
3018 byte[] b = new byte[a.Length];
3023 public void TestIntEnumArrayToIntArray ()
3025 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
3026 int[] b = new int[a.Length];
3033 [Test] // bug #322248
3034 public void IEnumerator_Reset ()
3036 int[] array = new int[] { 1, 2, 3};
3037 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3038 Assert.IsTrue (e.MoveNext (), "#A1");
3039 Assert.AreEqual (1, e.Current, "#A2");
3040 Assert.IsTrue (e.MoveNext (), "#A3");
3041 Assert.AreEqual (2, e.Current, "#A4");
3045 Assert.IsTrue (e.MoveNext (), "#C1");
3046 Assert.AreEqual (1, e.Current, "#C2");
3050 public void IEnumerator_Current_Finished ()
3052 int[] array = new int[] { 1, 2, 3 };
3053 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3054 Assert.IsTrue (e.MoveNext (), "#A1");
3055 Assert.AreEqual (1, e.Current, "#A2");
3056 Assert.IsTrue (e.MoveNext (), "#A3");
3057 Assert.AreEqual (2, e.Current, "#A4");
3058 Assert.IsTrue (e.MoveNext (), "#A5");
3059 Assert.AreEqual (3, e.Current, "#A6");
3060 Assert.IsTrue (!e.MoveNext (), "#A6");
3063 Assert.Fail ("#B1:" + e.Current);
3064 } catch (InvalidOperationException ex) {
3065 // Enumeration already finished
3066 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3067 Assert.IsNull (ex.InnerException, "#B3");
3068 Assert.IsNotNull (ex.Message, "#B4");
3073 public void IEnumerator_Current_NotStarted ()
3075 int[] array = new int[] { 1, 2, 3 };
3076 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3079 Assert.Fail ("#A1:" + e.Current);
3080 } catch (InvalidOperationException ex) {
3081 // Enumeration has not started. Call MoveNext
3082 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
3083 Assert.IsNull (ex.InnerException, "#A3");
3084 Assert.IsNotNull (ex.Message, "#A4");
3089 public void IEnumerator_Current_Reset ()
3091 int[] array = new int[] { 1, 2, 3 };
3092 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3097 Assert.Fail ("#B1:" + e.Current);
3098 } catch (InvalidOperationException ex) {
3099 // Enumeration has not started. Call MoveNext
3100 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3101 Assert.IsNull (ex.InnerException, "#B3");
3102 Assert.IsNotNull (ex.Message, "#B4");
3106 public void ICollection_IsReadOnly() {
3107 ICollection<string> arr = new string [10];
3109 Assert.IsTrue (arr.IsReadOnly);
3114 [ExpectedException (typeof (NotSupportedException))]
3115 public void ArrayCreateInstanceOfVoid ()
3117 Array.CreateInstance (typeof (void), 42);
3123 [ExpectedException (typeof (NotSupportedException))]
3124 public void ArrayCreateInstanceOfOpenGenericType ()
3126 Array.CreateInstance (typeof (Foo<>), 42);
3130 [ExpectedException (typeof (IndexOutOfRangeException))]
3131 public void ClearNegativeLength ()
3133 Array.Clear (new int [] { 1, 2 }, 0, -1);
3137 [ExpectedException (typeof (ArgumentException))]
3138 public void MultiDimension_IList_setItem ()
3140 IList array = new int [1, 1];
3145 [ExpectedException (typeof (ArgumentException))]
3146 public void MultiDimension_IList_getItem ()
3148 IList array = new int [1, 1];
3149 int a = (int) array [0];
3153 public void SetValue_Nullable () {
3154 Array array = Array.CreateInstance (typeof (int?), 7);
3158 array.SetValue (o, 0);
3159 Assert.AreEqual (42, array.GetValue (0));
3161 array.SetValue (null, 0);
3162 Assert.AreEqual (null, array.GetValue (0));
3166 public void SortNullsWithGenericVersion ()
3168 string[] s1 = new string[6]{
3176 string[] s2 = new string[]{null,
3183 Array.Sort<string> (s1);
3184 for (int i = 0; i < 6; i++) {
3185 Assert.AreEqual (s1[i], s2[i], "At:" + i);
3190 // This is a test case for the case that was broken by the code contributed
3193 // This tests the fix for: #622101
3196 public void SortActuallyWorks ()
3198 string[] data = new string[9]{"Foo", "Bar", "Dingus", null, "Dingu4", "123", "Iam", null, "NotNull"};
3199 IComparer comparer = new NullAtEndComparer ();
3200 Array.Sort (data, comparer);
3202 Assert.AreEqual (data [7], null);
3203 Assert.AreNotEqual (data [0], null);
3206 class NullAtEndComparer : IComparer {
3207 public int Compare(object x, object y)
3209 if (x == null) return 1;
3210 if (y == null) return -1;
3211 return ((string)x).CompareTo((string)y);
3217 [ExpectedException (typeof (ArgumentException))]
3218 public void CompareToWithJaggedArray () {
3219 IStructuralComparable a = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }};
3220 IStructuralComparable b = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }};
3221 a.CompareTo (b, Comparer<object>.Default);
3225 [ExpectedException (typeof (ArgumentException))]
3226 public void CompareToWithArrayOfTheWrongKind () {
3227 IStructuralComparable a = new int[] { 1, 2 };
3228 IStructuralComparable b = new double[] { 1, 2 };
3229 a.CompareTo (b, Comparer<object>.Default);
3233 [ExpectedException (typeof (ArgumentException))]
3234 public void CompareToWithNonArrayType () {
3235 IStructuralComparable a = new int[] { 1, 2 };
3236 a.CompareTo (99, Comparer<object>.Default);
3240 [ExpectedException (typeof (ArgumentException))]
3241 public void CompareToWithNonArrayOfDifferentSize () {
3242 IStructuralComparable a = new int[] { 1, 2 };
3243 IStructuralComparable b = new int[] { 1, 2, 3 };
3244 a.CompareTo (b, Comparer<object>.Default);
3248 [ExpectedException (typeof (ArgumentException))]
3249 public void CompareToWithMultiDimArray1 () {
3250 IStructuralComparable a = new int [2,2] { {10, 10 }, { 10, 10 } };
3251 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3252 a.CompareTo (b, Comparer<object>.Default);
3256 [ExpectedException (typeof (ArgumentException))]
3257 public void CompareToWithMultiDimArray2 () {
3258 IStructuralComparable a = new int [2] { 10, 10 };
3259 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3260 a.CompareTo (b, Comparer<object>.Default);
3264 [ExpectedException (typeof (ArgumentException))]
3265 public void CompareToWithMultiDimArray3 () {
3266 IStructuralComparable a = new int [4] { 10, 10, 10, 10 };
3267 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3268 a.CompareTo (b, Comparer<object>.Default);
3272 [ExpectedException (typeof (IndexOutOfRangeException))]
3273 public void CompareToWithBoundedArray1 () {
3274 IStructuralComparable a = new int [2] { 10, 10 };
3275 Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 });
3276 IStructuralComparable b = ab;
3277 ab.SetValue (10, 5);
3278 ab.SetValue (10, 6);
3280 a.CompareTo (b, Comparer<object>.Default);
3284 [ExpectedException (typeof (IndexOutOfRangeException))]
3285 public void CompareToWithBoundedArray2 () {
3286 IStructuralComparable a = new int [2] { 10, 10 };
3287 Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 });
3288 IStructuralComparable b = ab;
3289 ab.SetValue (10, 5);
3290 ab.SetValue (10, 6);
3292 //Yes, CompareTo simply doesn't work with bounded arrays!
3293 b.CompareTo (b, Comparer<object>.Default);
3297 [ExpectedException (typeof (NullReferenceException))]
3298 public void CompareToWithNullComparer () {
3299 IStructuralComparable a = new int[] { 1, 2 };
3300 IStructuralComparable b = new int[] { 1, 2 };
3301 a.CompareTo (b, null);
3305 public void CompareToWithNullArray () {
3306 IStructuralComparable a = new int[] { 1, 2 };
3307 Assert.AreEqual (1, a.CompareTo (null, Comparer<object>.Default));
3311 public void CompareToWithGoodArrays () {
3312 IStructuralComparable a = new int[] { 10, 20 };
3313 Assert.AreEqual (0, a.CompareTo (a, Comparer<object>.Default));
3314 Assert.AreEqual (0, a.CompareTo (new int [] { 10, 20 }, Comparer<object>.Default));
3315 Assert.AreEqual (-1, a.CompareTo (new int [] { 11, 20 }, Comparer<object>.Default));
3316 Assert.AreEqual (-1, a.CompareTo (new int [] { 10, 21 }, Comparer<object>.Default));
3317 Assert.AreEqual (1, a.CompareTo (new int [] { 9, 20 }, Comparer<object>.Default));
3318 Assert.AreEqual (1, a.CompareTo (new int [] { 10, 19 }, Comparer<object>.Default));
3322 public void IStructuralEquatable_Equals ()
3324 IStructuralEquatable array = new int[] {1, 2, 3};
3325 IStructuralEquatable array2 = new int[] {1, 2, 3};
3326 Assert.AreEqual (false, array.Equals (null, null));
3327 Assert.AreEqual (true, array.Equals (array, null));
3328 Assert.AreEqual (true, array.Equals (array2, EqualityComparer<int>.Default));
3332 [ExpectedException (typeof (NullReferenceException))]
3333 public void IStructuralEquatable_Equals_NoComparer ()
3335 IStructuralEquatable array = new int[] {1, 2, 3};
3336 IStructuralComparable array2 = new int[] {1, 2, 3};
3337 array.Equals (array2, null);
3341 [ExpectedException (typeof (ArgumentException))]
3342 public void IStructuralEquatable_Equals_ComparerThrows ()
3344 IStructuralEquatable array = new int[] {1, 2, 3};
3345 IStructuralComparable array2 = new int[] {1, 2, 3};
3346 array.Equals (array2, EqualityComparer<long>.Default);