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;
14 using System.Reflection;
15 using System.Collections.Generic;
17 namespace MonoTests.System
20 enum enua {hola,adios,mas,menos};
46 public override bool Equals (object obj)
51 public override int GetHashCode ()
57 //End Auxiliary Things
60 public class ArrayTest
62 char [] arrsort = {'d', 'b', 'f', 'e', 'a', 'c'};
67 public void TestIsFixedSize() {
69 Assert.IsTrue (a1.IsFixedSize, "All arrays are fixed");
73 public void TestIsReadOnly() {
75 Assert.IsTrue (!a1.IsReadOnly, "No array is readonly");
79 public void TestIsSynchronized() {
81 Assert.IsTrue (!a1.IsSynchronized, "No array is synchronized");
85 public void TestLength() {
88 Assert.AreEqual (0, a1.Length, "Zero length array");
92 Assert.AreEqual (1, a1.Length, "One-length array");
95 char[] a1 = {'c', 'c'};
96 Assert.AreEqual (2, a1.Length, "Two-length array");
101 public void TestRank() {
102 char[] a1 = { 'c', 'd', 'e' };
103 Assert.AreEqual (1, a1.Rank, "Rank one");
105 char[,] a2 = new Char[3,3];
106 Assert.AreEqual (2, a2.Rank, "Rank two");
108 char[,,] a3 = new Char[3,3,3];
109 Assert.AreEqual (3, a3.Rank, "Rank three");
113 public void TestBinarySearch1() {
114 bool errorThrown = false;
116 Array.BinarySearch(null, "blue");
117 } catch (ArgumentNullException) {
120 Assert.IsTrue (errorThrown, "#B01");
123 char[,] c1 = new Char[2,2];
124 Array.BinarySearch(c1, "needle");
125 } catch (RankException) {
128 Assert.IsTrue (errorThrown, "#B02");
131 char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
132 Assert.IsTrue (Array.BinarySearch(arr, 'c') >= 3, "#B05");
133 Assert.IsTrue (Array.BinarySearch(arr, 'c') < 6, "#B06");
136 char[] arr = {'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
137 Assert.AreEqual (-4, Array.BinarySearch(arr, 'c'), "#B07");
140 char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
141 Assert.AreEqual (-9, Array.BinarySearch(arr, 'e'), "#B08");
146 public void TestBinarySearch2() {
147 bool errorThrown = false;
149 Array.BinarySearch(null, 0, 1, "blue");
150 } catch (ArgumentNullException) {
153 Assert.IsTrue (errorThrown, "#B20");
156 char[,] c1 = new Char[2,2];
157 Array.BinarySearch(c1, 0, 1, "needle");
158 } catch (RankException) {
161 Assert.IsTrue (errorThrown, "#B21");
165 Array.BinarySearch(c1, -1, 1, 'a');
166 } catch (ArgumentOutOfRangeException) {
169 Assert.IsTrue (errorThrown, "#B22");
173 Array.BinarySearch(c1, 0, -1, 'a');
174 } catch (ArgumentOutOfRangeException) {
177 Assert.IsTrue (errorThrown, "#B23");
181 Array.BinarySearch(c1, 0, 4, 'a');
182 } catch (ArgumentException) {
185 Assert.IsTrue (errorThrown, "#B24");
188 char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
189 Assert.IsTrue (Array.BinarySearch(arr, 2, 8, 'c') >= 5, "#B26");
190 Assert.IsTrue (Array.BinarySearch(arr, 2, 8, 'c') < 8, "#B27");
193 char[] arr = {'z', 'z', 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
194 Assert.AreEqual (-6, Array.BinarySearch(arr, 2, 8, 'c'), "#B28");
197 char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
198 Assert.AreEqual (-11, Array.BinarySearch(arr, 2, 8, 'e'), "#B29");
202 public void TestBinarySearch3()
204 int[] array = new int[100];
206 for (int i = 0; i < 100; i++)
209 Assert.AreEqual (49, Array.BinarySearch(array, 10), "#B30");
213 public void BinarySearch_NullValue ()
215 int[] array = new int[1];
216 Assert.AreEqual (-1, Array.BinarySearch (array, null), "I=a,o");
217 Assert.AreEqual (-1, Array.BinarySearch (array, null, null), "I=a,o,c");
218 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 1, null), "I=a,i,i,o");
219 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 1, null, null), "I=a,i,i,o,c");
221 object[] o = new object [3] { this, this, null };
222 Assert.AreEqual (-1, Array.BinarySearch (o, null), "O=a,o");
223 Assert.AreEqual (-1, Array.BinarySearch (o, null, null), "O=a,o,c");
224 Assert.AreEqual (-1, Array.BinarySearch (o, 0, 3, null), "O=a,i,i,o");
225 Assert.AreEqual (-1, Array.BinarySearch (o, 0, 3, null, null), "O=a,i,i,o,c");
228 // TODO - testBinarySearch with explicit IComparer args
231 public void TestClear() {
232 bool errorThrown = false;
234 Array.Clear(null, 0, 1);
235 } catch (ArgumentNullException) {
238 Assert.IsTrue (errorThrown, "#C01");
240 int[] i1 = { 1, 2, 3, 4 };
242 int[] compare = {1,2,3,4};
243 Assert.AreEqual (compare[0], i1[0], "#C02");
244 Assert.AreEqual (compare[1], i1[1], "#C03");
245 Assert.AreEqual (compare[2], i1[2], "#C04");
246 Assert.AreEqual (compare[3], i1[3], "#C05");
248 Array.Clear(i1, 3, 1);
250 int[] compare = {1,2,3,0};
251 Assert.AreEqual (compare[0], i1[0], "#C06");
252 Assert.AreEqual (compare[1], i1[1], "#C07");
253 Assert.AreEqual (compare[2], i1[2], "#C08");
254 Assert.AreEqual (compare[3], i1[3], "#C09");
256 Array.Clear(i1, 1, 1);
258 int[] compare = {1,0,3,0};
259 Assert.AreEqual (compare[0], i1[0], "#C10");
260 Assert.AreEqual (compare[1], i1[1], "#C11");
261 Assert.AreEqual (compare[2], i1[2], "#C12");
262 Assert.AreEqual (compare[3], i1[3], "#C13");
264 Array.Clear(i1, 1, 3);
266 int[] compare = {1,0,0,0};
267 Assert.AreEqual (compare[0], i1[0], "#C14");
268 Assert.AreEqual (compare[1], i1[1], "#C15");
269 Assert.AreEqual (compare[2], i1[2], "#C16");
270 Assert.AreEqual (compare[3], i1[3], "#C17");
273 string[] s1 = { "red", "green", "blue" };
274 Array.Clear(s1, 0, 3);
276 string[] compare = {null, null, null};
277 Assert.AreEqual (compare[0], s1[0], "#C18");
278 Assert.AreEqual (compare[1], s1[1], "#C19");
279 Assert.AreEqual (compare[2], s1[2], "#C20");
284 public void TestClone() {
285 char[] c1 = {'a', 'b', 'c'};
286 char[] c2 = (char[])c1.Clone();
287 Assert.AreEqual (c1[0], c2[0], "#D01");
288 Assert.AreEqual (c1[1], c2[1], "#D02");
289 Assert.AreEqual (c1[2], c2[2], "#D03");
291 char[] d10 = {'a', 'b'};
292 char[] d11 = {'a', 'c'};
293 char[] d12 = {'b', 'c'};
294 char[][] d1 = {d10, d11, d12};
295 char[][] d2 = (char[][])d1.Clone();
296 Assert.AreEqual (d1[0], d2[0], "#D04");
297 Assert.AreEqual (d1[1], d2[1], "#D05");
298 Assert.AreEqual (d1[2], d2[2], "#D06");
301 Assert.AreEqual (d1[0], d2[0], "#D07");
305 public void TestMemberwiseClone () {
306 int[] array = new int[] { 1, 2, 3 };
307 MethodBase mi = array.GetType ().GetMethod("MemberwiseClone",
308 BindingFlags.Instance | BindingFlags.NonPublic);
309 int[] res = (int[])mi.Invoke (array, null);
310 Assert.AreEqual (3, res.Length);
313 [Test] public void TestIndexer ()
315 int [] a = new int [10];
319 Assert.Fail ("IList.this [-1] should throw");
320 } catch (IndexOutOfRangeException) {
323 Assert.Fail ("Should have thrown an IndexOutOfRangeException");
328 public void TestCopy() {
330 bool errorThrown = false;
333 Array.Copy(c1, null, 1);
334 } catch (ArgumentNullException) {
337 Assert.IsTrue (errorThrown, "#E01");
340 bool errorThrown = false;
343 Array.Copy(null, c1, 1);
344 } catch (ArgumentNullException) {
347 Assert.IsTrue (errorThrown, "#E02");
350 bool errorThrown = false;
352 Char[] c1 = new Char[1];
353 Char[,] c2 = new Char[1,1];
354 Array.Copy(c1, c2, 1);
355 } catch (RankException) {
358 Assert.IsTrue (errorThrown, "#E03");
361 bool errorThrown = false;
363 Char[] c1 = new Char[1];
364 string[] s1 = new String[1];
365 Array.Copy(c1, s1, 1);
366 } catch (ArrayTypeMismatchException) {
369 Assert.IsTrue (errorThrown, "#E04");
372 bool errorThrown = false;
374 Char[] c1 = new Char[1];
375 Object[] o1 = new Object[1];
377 Array.Copy(o1, c1, 1);
378 } catch (InvalidCastException) {
381 Assert.IsTrue (errorThrown, "#E05");
384 bool errorThrown = false;
386 Char[] c1 = new Char[1];
387 Char[] c2 = new Char[1];
388 Array.Copy(c1, c2, -1);
389 } catch (ArgumentOutOfRangeException) {
392 Assert.IsTrue (errorThrown, "#E06");
395 bool errorThrown = false;
397 Char[] c1 = new Char[1];
398 Char[] c2 = new Char[2];
399 Array.Copy(c1, c2, 2);
400 } catch (ArgumentException) {
403 Assert.IsTrue (errorThrown, "#E07");
406 bool errorThrown = false;
408 Char[] c1 = new Char[1];
409 Char[] c2 = new Char[2];
410 Array.Copy(c2, c1, 2);
411 } catch (ArgumentException) {
414 Assert.IsTrue (errorThrown, "#E08");
417 char[] orig = {'a', 'b', 'd', 'a'};
418 char[] copy = new Char[4];
419 Array.Copy(orig, copy, 4);
420 for (int i = 0; i < orig.Length; i++) {
421 Assert.AreEqual (orig[i], copy[i], "#E09(" + i + ")");
423 Array.Clear(copy, 0, copy.Length);
424 for (int i = 0; i < orig.Length; i++) {
425 Assert.AreEqual ((char)0, copy[i], "#E10(" + i + ")");
427 Array.Copy(orig, copy, 2);
428 Assert.AreEqual (orig[0], copy[0], "#E11");
429 Assert.AreEqual (orig[1], copy[1], "#E12");
430 Assert.IsTrue (orig[2] != copy[2], "#E13");
431 Assert.IsTrue (orig[3] != copy[3], "#E14");
435 public void TestCopy2() {
437 bool errorThrown = false;
439 Char[] c1 = new Char[2];
440 Char[] c2 = new Char[2];
441 Array.Copy(c2, 1, c1, 0, 2);
442 } catch (ArgumentException) {
445 Assert.IsTrue (errorThrown, "#E31");
448 bool errorThrown = false;
450 Char[] c1 = new Char[2];
451 Char[] c2 = new Char[2];
452 Array.Copy(c2, 0, c1, 1, 2);
453 } catch (ArgumentException) {
456 Assert.IsTrue (errorThrown, "#E32");
459 char[] orig = {'a', 'b', 'd', 'a'};
460 char[] copy = new Char[4];
461 Array.Copy(orig, 1, copy, 1, 3);
462 Assert.IsTrue (copy[0] != orig[0], "#E33");
463 for (int i = 1; i < orig.Length; i++) {
464 Assert.AreEqual (orig[i], copy[i], "#E34(" + i + ")");
466 Array.Clear(copy, 0, copy.Length);
467 Array.Copy(orig, 1, copy, 0, 2);
468 Assert.AreEqual (orig[1], copy[0], "#E35");
469 Assert.AreEqual (orig[2], copy[1], "#E36");
470 Assert.IsTrue (copy[2] != orig[2], "#E37");
471 Assert.IsTrue (copy[3] != orig[3], "#E38");
475 [ExpectedException (typeof (InvalidCastException))]
476 public void Copy_InvalidCast () {
477 object[] arr1 = new object [10];
478 Type[] arr2 = new Type [10];
480 arr1 [0] = new object ();
482 Array.Copy (arr1, 0, arr2, 0, 10);
486 public void TestCopyTo() {
488 bool errorThrown = false;
490 Char[] c1 = new Char[2];
492 } catch (ArgumentNullException) {
495 Assert.IsTrue (errorThrown, "#E61");
498 bool errorThrown = false;
500 Char[] c1 = new Char[2];
501 Char[,] c2 = new Char[2,2];
503 } catch (ArgumentException) {
506 #if TARGET_JVM // This is really implementation dependent behaviour.
507 catch (RankException) {
511 Assert.IsTrue (errorThrown, "#E62");
514 bool errorThrown = false;
516 Char[,] c1 = new Char[2,2];
517 Char[] c2 = new Char[2];
519 } catch (RankException) {
522 Assert.IsTrue (errorThrown, "#E63");
525 bool errorThrown = false;
527 Char[,] c1 = new Char[2,2];
528 Char[] c2 = new Char[2];
530 } catch (RankException) {
533 Assert.IsTrue (errorThrown, "#E64");
536 bool errorThrown = false;
538 Char[] c1 = new Char[2];
539 Char[] c2 = new Char[2];
541 } catch (ArgumentOutOfRangeException) {
544 Assert.IsTrue (errorThrown, "#E65");
547 bool errorThrown = false;
549 Char[] c1 = new Char[2];
550 Char[] c2 = new Char[2];
552 } catch (ArgumentException) {
555 Assert.IsTrue (errorThrown, "#E66");
558 bool errorThrown = false;
560 Char[] c1 = new Char[2];
561 Char[] c2 = new Char[2];
563 } catch (ArgumentException) {
566 Assert.IsTrue (errorThrown, "#E67");
570 bool errorThrown = false;
572 String[] c1 = new String[2];
573 // TODO: this crashes mono if there are null
574 // values in the array.
577 Char[] c2 = new Char[2];
581 } catch (ArrayTypeMismatchException) {
584 Assert.IsTrue (errorThrown, "#E68");
587 Char[] orig = {'a', 'b', 'c', 'd'};
588 Char[] copy = new Char[10];
589 Array.Clear(copy, 0, copy.Length);
590 orig.CopyTo(copy, 3);
591 Assert.AreEqual ((char)0, copy[0], "#E69");
592 Assert.AreEqual ((char)0, copy[1], "#E70");
593 Assert.AreEqual ((char)0, copy[2], "#E71");
594 Assert.AreEqual (orig[0], copy[3], "#E72");
595 Assert.AreEqual (orig[1], copy[4], "#E73");
596 Assert.AreEqual (orig[2], copy[5], "#E74");
597 Assert.AreEqual (orig[3], copy[6], "#E75");
598 Assert.AreEqual ((char)0, copy[7], "#E76");
599 Assert.AreEqual ((char)0, copy[8], "#E77");
600 Assert.AreEqual ((char)0, copy[9], "#E78");
603 // The following is valid and must not throw an exception.
604 bool errorThrown = false;
606 int[] src = new int [0];
607 int[] dest = new int [0];
608 src.CopyTo (dest, 0);
609 } catch (ArgumentException) {
612 Assert.IsTrue (!errorThrown, "#E79");
617 bool errorThrown = false;
619 CClass[] src = new CClass [] { new CClass () };
620 BClass[] dest = new BClass [1];
622 src.CopyTo (dest, 0);
624 } catch (ArrayTypeMismatchException) {
627 Assert.IsTrue (errorThrown, "#E80");
632 public void TestCreateInstance() {
634 bool errorThrown = false;
636 Array.CreateInstance(null, 12);
637 } catch (ArgumentNullException) {
640 Assert.IsTrue (errorThrown, "#F01");
643 bool errorThrown = false;
645 Array.CreateInstance(Type.GetType("System.Char"), -3);
646 } catch (ArgumentOutOfRangeException) {
649 Assert.IsTrue (errorThrown, "#F02");
652 bool errorThrown = false;
654 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
655 } catch (ArgumentNullException) {
658 Assert.IsTrue (errorThrown, "#F03a");
662 bool errorThrown = false;
664 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
665 } catch (ArgumentNullException) {
668 Assert.IsTrue (errorThrown, "#F03b");
671 #if !TARGET_JVM // Arrays lower bounds are not supported for TARGET_JVM
673 bool errorThrown = false;
675 Array.CreateInstance(Type.GetType("System.Char"), null, null);
676 } catch (ArgumentNullException) {
679 Assert.IsTrue (errorThrown, "#F04");
683 bool errorThrown = false;
685 int[] lengths = new int [0];
686 Array.CreateInstance(Type.GetType("System.Char"), lengths);
687 } catch (ArgumentException) {
690 Assert.IsTrue (errorThrown, "#F05");
692 #if !TARGET_JVM // CreateInstance with lower bounds not supported for TARGET_JVM
694 bool errorThrown = false;
696 int[] lengths = new int [1];
697 int[] bounds = new int [2];
698 Array.CreateInstance(Type.GetType("System.Char"), lengths, bounds);
700 } catch (ArgumentException) {
703 Assert.IsTrue (errorThrown, "#F06");
706 char[] c1 = (char[])Array.CreateInstance(Type.GetType("System.Char"), 12);
707 Assert.AreEqual (12, c1.Length, "#F07");
709 Array c2 = Array.CreateInstance(Type.GetType("System.Char"), 12, 5);
710 Assert.AreEqual (2, c2.Rank, "#F08");
711 Assert.AreEqual (60, c2.Length, "#F09");
715 int[] lengths = { 3 };
716 int[] bounds = { 5 };
717 int[] src = { 512, 718, 912 };
718 Array array = Array.CreateInstance(typeof(int), lengths, bounds);
720 Assert.AreEqual (3, array.Length, "#F10");
721 Assert.AreEqual (5, array.GetLowerBound(0), "#F11");
722 Assert.AreEqual (7, array.GetUpperBound(0), "#F12");
724 src.CopyTo (array, 5);
726 for (int i = 0; i < src.Length; i++)
727 Assert.AreEqual (src[i], array.GetValue(i+5), "#F13(" + i + ")");
730 // Test that a 1 dimensional array with 0 lower bound is the
731 // same as an szarray
732 Type szarrayType = new int [10].GetType ();
733 Assert.IsTrue (szarrayType == (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0})).GetType ());
734 Assert.IsTrue (szarrayType != (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {1})).GetType ());
739 [ExpectedException (typeof (ArgumentNullException))]
740 public void TestCreateInstance2 ()
742 Array.CreateInstance (typeof (Int32), (int[])null);
746 [ExpectedException (typeof (ArgumentNullException))]
747 public void TestCreateInstance2b ()
749 Array.CreateInstance (typeof (Int32), (long[])null);
753 public void TestGetEnumerator() {
754 String[] s1 = {"this", "is", "a", "test"};
755 IEnumerator en = s1.GetEnumerator ();
756 Assert.IsNotNull (en, "#G01");
758 Assert.IsTrue (en.MoveNext (), "#G02");
759 Assert.AreEqual ("this", en.Current, "#G03");
760 Assert.IsTrue (en.MoveNext (), "#G04");
761 Assert.AreEqual ("is", en.Current, "#G05");
762 Assert.IsTrue (en.MoveNext (), "#G06");
763 Assert.AreEqual ("a", en.Current, "#G07");
764 Assert.IsTrue (en.MoveNext (), "#G08");
765 Assert.AreEqual ("test", en.Current, "#G09");
766 Assert.IsTrue (!en.MoveNext (), "#G10");
769 Assert.IsTrue (en.MoveNext (), "#G11");
770 Assert.AreEqual ("this", en.Current, "#G12");
772 // mutation does not invalidate array enumerator!
773 s1.SetValue ("change", 1);
774 Assert.IsTrue (en.MoveNext (), "#G13");
775 Assert.AreEqual ("change", en.Current, "#G14");
779 public void TestGetEnumeratorMultipleDimension() {
780 String[,] s1 = {{"this", "is"}, {"a", "test"}};
781 IEnumerator en = s1.GetEnumerator ();
782 Assert.IsNotNull (en, "#AA01");
784 Assert.IsTrue (en.MoveNext (), "#AA02");
785 Assert.AreEqual ("this", en.Current, "#AA03");
786 Assert.IsTrue (en.MoveNext (), "#AA04");
787 Assert.AreEqual ("is", en.Current, "#AA05");
788 Assert.IsTrue (en.MoveNext (), "#AA06");
789 Assert.AreEqual ("a", en.Current, "#AA07");
790 Assert.IsTrue (en.MoveNext (), "#AA08");
791 Assert.AreEqual ("test", en.Current, "#AA09");
792 Assert.IsTrue (!en.MoveNext (), "#AA10");
795 Assert.IsTrue (en.MoveNext (), "#AA11");
796 Assert.AreEqual ("this", en.Current, "#AA12");
799 // mutation does not invalidate array enumerator!
800 s1.SetValue ("change", idxs);
801 Assert.IsTrue (en.MoveNext (), "#AA13");
802 Assert.AreEqual ("change", en.Current, "#AA14");
806 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
807 public void TestGetEnumeratorNonZeroLowerBounds() {
808 int[] myLengthsArray = new int[2] { 3, 5 };
809 int[] myBoundsArray = new int[2] { 2, 3 };
811 Array myArray=Array.CreateInstance( typeof(String), myLengthsArray, myBoundsArray );
812 for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
813 for ( int j = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++ ) {
814 int[] myIndicesArray = new int[2] { i, j };
815 myArray.SetValue( Convert.ToString(i) + j, myIndicesArray );
817 IEnumerator en = myArray.GetEnumerator ();
818 Assert.IsNotNull (en, "#AB01");
820 // check the first couple of values
821 Assert.IsTrue (en.MoveNext (), "#AB02");
822 Assert.AreEqual ("23", en.Current, "#AB03");
823 Assert.IsTrue (en.MoveNext (), "#AB04");
824 Assert.AreEqual ("24", en.Current, "#AB05");
826 // then check the last element's value
829 lastElement = (string)en.Current;
830 } while (en.MoveNext());
831 Assert.AreEqual ("47", lastElement, "#AB06");
835 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
836 public void TestIList_Add () {
837 int[] myLengthsArray = new int[2] { 3, 5 };
838 int[] myBoundsArray = new int[2] { 2, 3 };
840 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
842 ((IList)myArray).Add ("can not");
843 Assert.Fail ("IList.Add should throw");
845 catch (NotSupportedException) {
849 Assert.Fail ("IList.Add threw wrong exception type");
852 Assert.Fail ("IList.Add shouldn't get this far");
856 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
857 public void TestIList_Insert () {
858 int[] myLengthsArray = new int[2] { 3, 5 };
859 int[] myBoundsArray = new int[2] { 2, 3 };
861 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
863 ((IList)myArray).Insert (0, "can not");
864 Assert.Fail ("IList.Insert should throw");
866 catch (NotSupportedException) {
870 Assert.Fail ("IList.Insert threw wrong exception type");
873 Assert.Fail ("IList.Insert shouldn't get this far");
877 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
878 public void TestIList_Remove () {
879 int[] myLengthsArray = new int[2] { 3, 5 };
880 int[] myBoundsArray = new int[2] { 2, 3 };
882 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
884 ((IList)myArray).Remove ("can not");
885 Assert.Fail ("IList.Remove should throw");
887 catch (NotSupportedException) {
891 Assert.Fail ("IList.Remove threw wrong exception type");
894 Assert.Fail ("IList.Remove shouldn't get this far");
898 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
899 public void TestIList_RemoveAt () {
900 int[] myLengthsArray = new int[2] { 3, 5 };
901 int[] myBoundsArray = new int[2] { 2, 3 };
903 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
905 ((IList)myArray).RemoveAt (0);
906 Assert.Fail ("IList.RemoveAt should throw");
908 catch (NotSupportedException) {
912 Assert.Fail ("IList.RemoveAt threw wrong exception type");
915 Assert.Fail ("IList.RemoveAt shouldn't get this far");
919 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
920 public void TestIList_Contains () {
921 int[] myLengthsArray = new int[2] { 3, 5 };
922 int[] myBoundsArray = new int[2] { 2, 3 };
924 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
927 bool b = ((IList)myArray).Contains ("23");
928 Assert.Fail ("IList.Contains should throw with multi-dimensional arrays");
930 catch (RankException) {
931 int[] iArr = new int[3] { 1, 2, 3};
932 // check the first and last items
933 Assert.IsTrue (((IList)iArr).Contains (1), "AC01");
934 Assert.IsTrue (((IList)iArr).Contains (3), "AC02");
936 // and one that is definately not there
937 Assert.IsTrue (!((IList)iArr).Contains (42), "AC03");
941 Assert.Fail ("Should not get here");
945 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
946 public void TestIList_IndexOf () {
947 int[] myLengthsArray = new int[2] { 3, 5 };
948 int[] myBoundsArray = new int[2] { 2, 3 };
950 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
953 bool b = ((IList)myArray).Contains ("23");
954 Assert.Fail ("IList.Contains should throw with multi-dimensional arrays");
956 catch (RankException) {
957 int[] iArr = new int[3] { 1, 2, 3};
958 // check the first and last items
959 Assert.AreEqual (0, ((IList)iArr).IndexOf (1), "AD01");
960 Assert.AreEqual (2, ((IList)iArr).IndexOf (3), "AD02");
962 // and one that is definately not there
963 Assert.AreEqual (-1, ((IList)iArr).IndexOf (42), "AD03");
965 catch (Exception e) {
966 Assert.Fail ("Unexpected exception: " + e.ToString());
969 // check that wierd case whem lowerbound is Int32.MinValue,
970 // so that IndexOf() needs to return Int32.MaxValue when it cannot find the object
971 int[] myLengthArray = new int[1] { 3 };
972 int[] myBoundArray = new int[1] { Int32.MinValue };
973 Array myExtremeArray=Array.CreateInstance ( typeof(String), myLengthArray, myBoundArray );
974 Assert.AreEqual (Int32.MaxValue, ((IList)myExtremeArray).IndexOf (42), "AD04");
979 public void TestGetLength() {
981 bool errorThrown = false;
983 char[] c1 = {'a', 'b', 'c'};
985 } catch (IndexOutOfRangeException) {
988 Assert.IsTrue (errorThrown, "#H01");
991 bool errorThrown = false;
993 char[] c1 = {'a', 'b', 'c'};
995 } catch (IndexOutOfRangeException) {
998 Assert.IsTrue (errorThrown, "#H02");
1001 char[] c2 = new Char[5];
1002 Assert.AreEqual (5, c2.GetLength(0), "#H03");
1004 char[,] c3 = new Char[6,7];
1005 Assert.AreEqual (6, c3.GetLength(0), "#H04");
1006 Assert.AreEqual (7, c3.GetLength(1), "#H05");
1010 public void TestGetLowerBound() {
1012 bool errorThrown = false;
1014 char[] c = {'a', 'b', 'c'};
1015 c.GetLowerBound(-1);
1016 } catch (IndexOutOfRangeException) {
1019 Assert.IsTrue (errorThrown, "#H31");
1022 bool errorThrown = false;
1024 char[] c = {'a', 'b', 'c'};
1026 } catch (IndexOutOfRangeException) {
1029 Assert.IsTrue (errorThrown, "#H32");
1032 char[] c1 = new Char[5];
1033 Assert.AreEqual (0, c1.GetLowerBound(0), "#H33");
1035 char[,] c2 = new Char[4,4];
1036 Assert.AreEqual (0, c2.GetLowerBound(0), "#H34");
1037 Assert.AreEqual (0, c2.GetLowerBound(1), "#H35");
1041 public void TestGetUpperBound() {
1043 bool errorThrown = false;
1045 char[] c = {'a', 'b', 'c'};
1046 c.GetUpperBound(-1);
1047 } catch (IndexOutOfRangeException) {
1050 Assert.IsTrue (errorThrown, "#H61");
1053 bool errorThrown = false;
1055 char[] c = {'a', 'b', 'c'};
1057 } catch (IndexOutOfRangeException) {
1060 Assert.IsTrue (errorThrown, "#H62");
1063 char[] c1 = new Char[5];
1064 Assert.AreEqual (4, c1.GetUpperBound(0), "#H63");
1066 char[,] c2 = new Char[4,6];
1067 Assert.AreEqual (3, c2.GetUpperBound(0), "#H64");
1068 Assert.AreEqual (5, c2.GetUpperBound(1), "#H65");
1072 public void TestGetValue1() {
1074 bool errorThrown = false;
1076 char[,] c = new Char[2,2];
1078 } catch (ArgumentException) {
1081 Assert.IsTrue (errorThrown, "#I01");
1084 bool errorThrown = false;
1086 char[] c = {'a', 'b', 'c'};
1088 } catch (IndexOutOfRangeException) {
1091 Assert.IsTrue (errorThrown, "#I02");
1094 bool errorThrown = false;
1096 char[] c = {'a', 'b', 'c'};
1098 } catch (IndexOutOfRangeException) {
1101 Assert.IsTrue (errorThrown, "#I03");
1104 char[] c1 = {'a', 'b', 'c', 'd'};
1105 for (int i = 0; i < c1.Length; i++) {
1106 Assert.AreEqual (c1[i], c1.GetValue(i), "#I04(" + i + ")");
1111 public void TestGetValue2() {
1113 bool errorThrown = false;
1115 char[] c = new Char[2];
1117 } catch (ArgumentException) {
1120 Assert.IsTrue (errorThrown, "#I21");
1123 bool errorThrown = false;
1125 char[,] c = new Char[2,2];
1127 } catch (IndexOutOfRangeException) {
1130 Assert.IsTrue (errorThrown, "#I22");
1133 bool errorThrown = false;
1135 char[,] c = new Char[2,2];
1137 } catch (IndexOutOfRangeException) {
1140 Assert.IsTrue (errorThrown, "#I23");
1143 char[,] c1 = new Char[4,6];
1144 for (int i = 0; i < 24; i++) {
1147 c1[first,second] = (char)(((int)'a')+i);
1149 for (int i = 0; i < c1.GetLength(0); i++) {
1150 for (int j = 0; j < c1.GetLength(1); j++) {
1151 Assert.AreEqual (c1[i, j], c1.GetValue(i, j), "#I24(" + i + "," + j + ")");
1157 public void TestGetValue3() {
1159 bool errorThrown = false;
1161 char[] c = new Char[2];
1163 } catch (ArgumentException) {
1166 Assert.IsTrue (errorThrown, "#I41");
1169 bool errorThrown = false;
1171 char[,,] c = new Char[2,2,2];
1172 c.GetValue(-1, 1, 1);
1173 } catch (IndexOutOfRangeException) {
1176 Assert.IsTrue (errorThrown, "#I42");
1179 bool errorThrown = false;
1181 char[,,] c = new Char[2,2,2];
1183 } catch (IndexOutOfRangeException) {
1186 Assert.IsTrue (errorThrown, "#I43");
1189 char[,,] c1 = new Char[4,2,3];
1190 for (int i = 0; i < 24; i++) {
1192 int remains = i % 6;
1193 int second = remains / 3;
1194 int third = remains % 3;
1195 c1[first,second, third] = (char)(((int)'a')+i);
1197 for (int i = 0; i < c1.GetLength(0); i++) {
1198 for (int j = 0; j < c1.GetLength(1); j++) {
1199 for (int k = 0; k < c1.GetLength(2); k++) {
1200 Assert.AreEqual (c1[i, j, k], c1.GetValue(i, j, k), "#I44(" + i + "," + j + ")");
1207 [ExpectedException (typeof (ArgumentNullException))]
1208 public void TestGetValueLongArray ()
1210 char[] c = new Char[2];
1211 c.GetValue((long [])null);
1215 public void TestGetValueN() {
1217 bool errorThrown = false;
1219 char[] c = new Char[2];
1220 c.GetValue((int [])null);
1221 } catch (ArgumentNullException) {
1224 Assert.IsTrue (errorThrown, "#I61a");
1227 bool errorThrown = false;
1229 char[] c = new Char[2];
1230 int[] coords = {1, 1};
1232 } catch (ArgumentException) {
1235 Assert.IsTrue (errorThrown, "#I62");
1238 bool errorThrown = false;
1240 char[,] c = new Char[2,2];
1241 int[] coords = {-1, 1};
1243 } catch (IndexOutOfRangeException) {
1246 Assert.IsTrue (errorThrown, "#I63");
1249 bool errorThrown = false;
1251 char[,] c = new Char[2,2];
1252 int[] coords = {4, 1};
1254 } catch (IndexOutOfRangeException) {
1257 Assert.IsTrue (errorThrown, "#I64");
1260 char[,] c1 = new Char[4,6];
1261 for (int i = 0; i < 24; i++) {
1264 c1[first,second] = (char)(((int)'a')+i);
1266 for (int i = 0; i < c1.GetLength(0); i++) {
1267 for (int j = 0; j < c1.GetLength(1); j++) {
1268 int[] coords = {i, j};
1269 Assert.AreEqual (c1[i, j], c1.GetValue(coords), "#I65(" + i + "," + j + ")");
1275 public void TestIndexOf1() {
1277 bool errorThrown = false;
1279 Array.IndexOf(null, "huh?");
1280 } catch (ArgumentNullException) {
1283 Assert.IsTrue (errorThrown, "#J01");
1286 bool errorThrown = false;
1288 char[,] c = new Char[2,2];
1289 Array.IndexOf(c, "huh?");
1290 } catch (RankException) {
1293 Assert.IsTrue (errorThrown, "#J02");
1296 String[] s1 = {"this", "is", "a", "test"};
1297 Assert.AreEqual (-1, Array.IndexOf(s1, null), "#J03");
1298 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing"), "#J04");
1299 Assert.AreEqual (0, Array.IndexOf(s1, "this"), "#J05");
1300 Assert.AreEqual (3, Array.IndexOf(s1, "test"), "#J06");
1304 public void TestIndexOf2() {
1306 bool errorThrown = false;
1308 Array.IndexOf(null, "huh?", 0);
1309 } catch (ArgumentNullException) {
1312 Assert.IsTrue (errorThrown, "#J21");
1315 bool errorThrown = false;
1317 char[,] c = new Char[2,2];
1318 Array.IndexOf(c, "huh?", 0);
1319 } catch (RankException) {
1322 Assert.IsTrue (errorThrown, "#J22");
1325 bool errorThrown = false;
1327 char[] c = new Char[2];
1328 Array.IndexOf(c, "huh?", 3);
1329 } catch (ArgumentOutOfRangeException) {
1332 Assert.IsTrue (errorThrown, "#J23");
1335 String[] s1 = {"this", "is", "really", "a", "test"};
1336 Assert.AreEqual (-1, Array.IndexOf(s1, null, 1), "#J24");
1337 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing", 1), "#J25");
1338 Assert.AreEqual (-1, Array.IndexOf(s1, "this", 1), "#J26");
1339 Assert.AreEqual (1, Array.IndexOf(s1, "is", 1), "#J27");
1340 Assert.AreEqual (4, Array.IndexOf(s1, "test", 1), "#J28");
1344 public void TestIndexOf3() {
1346 bool errorThrown = false;
1348 Array.IndexOf(null, "huh?", 0, 1);
1349 } catch (ArgumentNullException) {
1352 Assert.IsTrue (errorThrown, "#J41");
1355 bool errorThrown = false;
1357 char[,] c = new Char[2,2];
1358 Array.IndexOf(c, "huh?", 0, 1);
1359 } catch (RankException) {
1362 Assert.IsTrue (errorThrown, "#J42");
1365 bool errorThrown = false;
1367 char[] c = new Char[2];
1368 Array.IndexOf(c, "huh?", 3, 1);
1369 } catch (ArgumentOutOfRangeException) {
1372 Assert.IsTrue (errorThrown, "#J43");
1375 bool errorThrown = false;
1377 char[] c = new Char[2];
1378 Array.IndexOf(c, "huh?", 0, 5);
1379 } catch (ArgumentOutOfRangeException) {
1382 Assert.IsTrue (errorThrown, "#J44");
1385 String[] s1 = {"this", "is", "really", "a", "test"};
1386 Assert.AreEqual (-1, Array.IndexOf(s1, null, 1, 3), "#J45");
1387 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing", 1, 3), "#J46");
1388 Assert.AreEqual (-1, Array.IndexOf(s1, "this", 1, 3), "#J47");
1389 Assert.AreEqual (1, Array.IndexOf(s1, "is", 1, 3), "#J48");
1390 Assert.AreEqual (-1, Array.IndexOf(s1, "test", 1, 3), "#J49");
1391 Assert.AreEqual (3, Array.IndexOf(s1, "a", 1, 3), "#J50");
1395 public void TestIndexOf_CustomEqual ()
1397 DataEqual[] test = new DataEqual [] { new DataEqual () };
1398 Assert.AreEqual (0, Array.IndexOf (test, "asdfas", 0));
1400 IList array = (IList)test;
1401 Assert.AreEqual (0, array.IndexOf ("asdfas"));
1405 public void TestLastIndexOf1() {
1407 bool errorThrown = false;
1409 Array.LastIndexOf(null, "huh?");
1410 } catch (ArgumentNullException) {
1413 Assert.IsTrue (errorThrown, "#K01");
1416 bool errorThrown = false;
1418 char[,] c = new Char[2,2];
1419 Array.LastIndexOf(c, "huh?");
1420 } catch (RankException) {
1423 Assert.IsTrue (errorThrown, "#K02");
1426 String[] s1 = {"this", "is", "a", "a", "test"};
1427 Assert.AreEqual (-1, Array.LastIndexOf(s1, null), "#K03");
1428 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing"), "#K04");
1429 Assert.AreEqual (0, Array.LastIndexOf(s1, "this"), "#K05");
1430 Assert.AreEqual (4, Array.LastIndexOf(s1, "test"), "#K06");
1431 Assert.AreEqual (3, Array.LastIndexOf(s1, "a"), "#K07");
1433 Assert.AreEqual (-1, Array.LastIndexOf (new String [0], "foo"));
1437 public void TestLastIndexOf2() {
1439 bool errorThrown = false;
1441 Array.LastIndexOf(null, "huh?", 0);
1442 } catch (ArgumentNullException) {
1445 Assert.IsTrue (errorThrown, "#K21");
1448 bool errorThrown = false;
1450 char[,] c = new Char[2,2];
1451 Array.LastIndexOf(c, "huh?", 0);
1452 } catch (RankException) {
1455 Assert.IsTrue (errorThrown, "#K22");
1458 bool errorThrown = false;
1460 char[] c = new Char[2];
1461 Array.LastIndexOf(c, "huh?", 3);
1462 } catch (ArgumentOutOfRangeException) {
1465 Assert.IsTrue (errorThrown, "#K23");
1468 String[] s1 = {"this", "is", "really", "a", "test"};
1469 Assert.AreEqual (-1, Array.LastIndexOf(s1, null, 3), "#K24");
1470 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing", 3), "#K25");
1471 Assert.AreEqual (-1, Array.LastIndexOf(s1, "test", 3), "#K26");
1472 Assert.AreEqual (3, Array.LastIndexOf(s1, "a", 3), "#K27");
1473 Assert.AreEqual (0, Array.LastIndexOf(s1, "this", 3), "#K28");
1477 public void TestLastIndexOf3() {
1479 bool errorThrown = false;
1481 Array.LastIndexOf(null, "huh?", 0, 1);
1482 } catch (ArgumentNullException) {
1485 Assert.IsTrue (errorThrown, "#K41");
1488 bool errorThrown = false;
1490 char[,] c = new Char[2,2];
1491 Array.LastIndexOf(c, "huh?", 0, 1);
1492 } catch (RankException) {
1495 Assert.IsTrue (errorThrown, "#K42");
1498 bool errorThrown = false;
1500 char[] c = new Char[2];
1501 Array.LastIndexOf(c, "huh?", 3, 1);
1502 } catch (ArgumentOutOfRangeException) {
1505 Assert.IsTrue (errorThrown, "#K43");
1508 bool errorThrown = false;
1510 char[] c = new Char[2];
1511 Array.LastIndexOf(c, "huh?", 0, 5);
1512 } catch (ArgumentOutOfRangeException) {
1515 Assert.IsTrue (errorThrown, "#K44");
1518 String[] s1 = {"this", "is", "really", "a", "test"};
1519 Assert.AreEqual (-1, Array.LastIndexOf(s1, null, 3, 3), "#K45");
1520 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing", 3, 3), "#K46");
1521 Assert.AreEqual (-1, Array.LastIndexOf(s1, "this", 3, 3), "#K47");
1522 Assert.AreEqual (1, Array.LastIndexOf(s1, "is", 3, 3), "#K48");
1523 Assert.AreEqual (-1, Array.LastIndexOf(s1, "test", 3, 3), "#K49");
1524 Assert.AreEqual (3, Array.LastIndexOf(s1, "a", 3, 3), "#K50");
1528 public void TestLastIndexOf4 ()
1530 short [] a = new short [] { 19, 238, 317, 6, 565, 0, -52, 60, -563, 753, 238, 238};
1532 Array.LastIndexOf (a, (object)16, -1);
1533 NUnit.Framework.Assert.Fail ("#1");
1534 } catch (ArgumentOutOfRangeException) { }
1537 Array.LastIndexOf<short> (a, 16, -1);
1538 NUnit.Framework.Assert.Fail ("#2");
1539 } catch (ArgumentOutOfRangeException) { }
1543 public void TestLastIndexOf5 ()
1545 char [] a = new char [] {'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'j', 'i', 'h'};
1550 for (int i = a.Length - 1; i >= 0 ; i--) {
1552 retval = Array.LastIndexOf(a, a [i], i, i + 1);
1556 Assert.IsTrue (!error);
1560 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1561 public void LastIndexOf_StartIndexOverflow ()
1563 // legal - no exception
1564 byte[] array = new byte [16];
1565 Array.LastIndexOf (array, this, Int32.MaxValue, 1);
1569 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1570 public void LastIndexOf_CountOverflow ()
1572 // legal - no exception
1573 byte[] array = new byte [16];
1574 Array.LastIndexOf (array, this, 1, Int32.MaxValue);
1578 public void LastIndexOf_0LengthArray ()
1580 Array array = Array.CreateInstance (typeof (char), 0);
1581 int idx = Array.LastIndexOf (array, (object) null, -1, 0);
1582 Assert.IsTrue (idx == -1, "#01");
1583 idx = Array.LastIndexOf (array, (object) null, -1, 10);
1584 Assert.IsTrue (idx == -1, "#02");
1585 idx = Array.LastIndexOf (array, (object) null, -100, 10);
1586 Assert.IsTrue (idx == -1, "#02");
1588 array = Array.CreateInstance (typeof (char), 1);
1590 Array.LastIndexOf (array, (object) null, -1, 0);
1591 Assert.Fail ("#04");
1592 } catch (ArgumentOutOfRangeException e) {
1595 Array.LastIndexOf (array, (object) null, -1, 10);
1596 Assert.Fail ("#05");
1597 } catch (ArgumentOutOfRangeException e) {
1600 Array.LastIndexOf (array, (object) null, -100, 10);
1601 Assert.Fail ("#06");
1602 } catch (ArgumentOutOfRangeException e) {
1607 public void TestReverse() {
1609 bool errorThrown = false;
1611 Array.Reverse(null);
1612 } catch (ArgumentNullException) {
1615 Assert.IsTrue (errorThrown, "#L01");
1618 bool errorThrown = false;
1620 char[,] c = new Char[2,2];
1622 } catch (RankException) {
1625 Assert.IsTrue (errorThrown, "#L02");
1628 char[] c1 = {'a', 'b', 'c', 'd'};
1630 Assert.AreEqual ('d', c1[0], "#L03");
1631 Assert.AreEqual ('c', c1[1], "#L04");
1632 Assert.AreEqual ('b', c1[2], "#L05");
1633 Assert.AreEqual ('a', c1[3], "#L06");
1636 bool errorThrown = false;
1638 Array.Reverse(null, 0, 0);
1639 } catch (ArgumentNullException) {
1642 Assert.IsTrue (errorThrown, "#L07");
1645 bool errorThrown = false;
1647 char[,] c = new Char[2,2];
1648 Array.Reverse(c, 0, 0);
1649 } catch (RankException) {
1652 Assert.IsTrue (errorThrown, "#L08");
1655 //bool errorThrown = false;
1657 // char[] c = new Char[2];
1658 // Array.Reverse(c, 0, 3);
1659 //} catch (ArgumentOutOfRangeException) {
1660 // errorThrown = true;
1662 //Assert.IsTrue (errorThrown, "#L09");
1665 //bool errorThrown = false;
1667 // char[] c = new Char[2];
1668 // Array.Reverse(c, 3, 0);
1669 //} catch (ArgumentOutOfRangeException) {
1670 // errorThrown = true;
1672 //Assert.IsTrue (errorThrown, "#L10");
1675 char[] c2 = { 'a', 'b', 'c', 'd'};
1676 Array.Reverse(c2, 1, 2);
1677 Assert.AreEqual ('a', c2[0], "#L11");
1678 Assert.AreEqual ('c', c2[1], "#L12");
1679 Assert.AreEqual ('b', c2[2], "#L13");
1680 Assert.AreEqual ('d', c2[3], "#L14");
1684 public void TestSetValue1() {
1686 bool errorThrown = false;
1688 char[,] c = new Char[2,2];
1689 c.SetValue("buh", 1);
1690 } catch (ArgumentException) {
1693 Assert.IsTrue (errorThrown, "#M01");
1696 bool errorThrown = false;
1698 char[] c = {'a', 'b', 'c'};
1699 c.SetValue("buh", -1);
1700 } catch (IndexOutOfRangeException) {
1703 Assert.IsTrue (errorThrown, "#M02");
1706 bool errorThrown = false;
1708 char[] c = {'a', 'b', 'c'};
1709 c.SetValue("buh", 4);
1710 } catch (IndexOutOfRangeException) {
1713 Assert.IsTrue (errorThrown, "#M03");
1716 char[] c1 = {'a', 'b', 'c', 'd'};
1717 char[] c2 = new char[4];
1718 for (int i = 0; i < c1.Length; i++) {
1719 c2.SetValue(c1[i], i);
1721 for (int i = 0; i < c1.Length; i++) {
1722 Assert.AreEqual (c1[i], c2[i], "#M04(" + i + ")");
1725 int[] c3 = { 1, 2, 3 };
1726 long[] c4 = new long [3];
1728 for (int i = 0; i < c3.Length; i++)
1729 c4.SetValue (c3 [i], i);
1733 } catch (Exception e) {
1734 Assert.Fail ("c3.CopyTo(): e=" + e);
1736 for (int i = 0; i < c3.Length; i++)
1737 Assert.IsTrue (c3[i] == c4[i], "#M05(" + i + ")");
1739 Object[] c5 = new Object [3];
1740 long[] c6 = new long [3];
1744 } catch (Exception e) {
1745 Assert.Fail ("c4.CopyTo(): e=" + e);
1750 } catch (Exception e) {
1751 Assert.Fail ("c5.CopyTo(): e=" + e);
1753 // for (int i = 0; i < c5.Length; i++)
1754 // Assert.IsTrue (c5[i] == c6[i], "#M06(" + i + ")");
1758 public void TestSetValue2() {
1760 bool errorThrown = false;
1762 char[] c = new Char[2];
1763 c.SetValue("buh", 1,1);
1764 } catch (ArgumentException) {
1767 Assert.IsTrue (errorThrown, "#M21");
1770 bool errorThrown = false;
1772 char[,] c = new Char[2,2];
1773 c.SetValue("buh", -1, 1);
1774 } catch (IndexOutOfRangeException) {
1777 Assert.IsTrue (errorThrown, "#M22");
1780 bool errorThrown = false;
1782 char[,] c = new Char[2,2];
1783 c.SetValue("buh", 4,1);
1784 } catch (IndexOutOfRangeException) {
1787 Assert.IsTrue (errorThrown, "#M23");
1790 char[,] c1 = new Char[4,6];
1791 char[,] c2 = new Char[4,6];
1792 for (int i = 0; i < 24; i++) {
1795 c1[first,second] = (char)(((int)'a')+i);
1796 c2.SetValue(c1[first,second], first, second);
1798 for (int i = 0; i < c1.GetLength(0); i++) {
1799 for (int j = 0; j < c1.GetLength(1); j++) {
1800 Assert.AreEqual (c1[i, j], c2[i, j], "#M24(" + i + "," + j + ")");
1806 public void TestSetValue3() {
1808 bool errorThrown = false;
1810 char[] c = new Char[2];
1811 c.SetValue("buh", 1,1,1);
1812 } catch (ArgumentException) {
1815 Assert.IsTrue (errorThrown, "#M41");
1818 bool errorThrown = false;
1820 char[,,] c = new Char[2,2,2];
1821 c.SetValue("buh", -1, 1, 1);
1822 } catch (IndexOutOfRangeException) {
1825 Assert.IsTrue (errorThrown, "#M42");
1828 bool errorThrown = false;
1830 char[,,] c = new Char[2,2,2];
1831 c.SetValue("buh", 4,1,1);
1832 } catch (IndexOutOfRangeException) {
1835 Assert.IsTrue (errorThrown, "#M43");
1838 char[,,] c1 = new Char[4,2,3];
1839 char[,,] c2 = new Char[4,2,3];
1840 for (int i = 0; i < 24; i++) {
1842 int remains = i % 6;
1843 int second = remains / 3;
1844 int third = remains % 3;
1845 c1[first,second, third] = (char)(((int)'a')+i);
1846 c2.SetValue(c1[first, second, third], first, second, third);
1848 for (int i = 0; i < c1.GetLength(0); i++) {
1849 for (int j = 0; j < c1.GetLength(1); j++) {
1850 for (int k = 0; k < c1.GetLength(2); k++) {
1851 Assert.AreEqual (c1[i, j, k], c2[i, j, k], "#M44(" + i + "," + j + " )");
1858 [ExpectedException (typeof (ArgumentNullException))]
1859 public void TestSetValueLongArray ()
1861 char[] c = new Char[2];
1862 c.SetValue("buh", (long [])null);
1866 public void TestSetValueN() {
1868 bool errorThrown = false;
1870 char[] c = new Char[2];
1871 c.SetValue("buh", (int [])null);
1872 } catch (ArgumentNullException) {
1875 Assert.IsTrue (errorThrown, "#M61a");
1878 bool errorThrown = false;
1880 char[] c = new Char[2];
1881 int[] coords = {1, 1};
1882 c.SetValue("buh", coords);
1883 } catch (ArgumentException) {
1886 Assert.IsTrue (errorThrown, "#M62");
1889 bool errorThrown = false;
1891 char[,] c = new Char[2,2];
1892 int[] coords = {-1, 1};
1893 c.SetValue("buh", coords);
1894 } catch (IndexOutOfRangeException) {
1897 Assert.IsTrue (errorThrown, "#M63");
1900 bool errorThrown = false;
1902 char[,] c = new Char[2,2];
1903 int[] coords = {4, 1};
1904 c.SetValue("buh", coords);
1905 } catch (IndexOutOfRangeException) {
1908 Assert.IsTrue (errorThrown, "#M64");
1911 char[,] c1 = new Char[4,6];
1912 char[,] c2 = new Char[4,6];
1913 for (int i = 0; i < 24; i++) {
1916 c1[first,second] = (char)(((int)'a')+i);
1917 int[] coords = {first, second};
1918 c2.SetValue(c1[first,second], coords);
1920 for (int i = 0; i < c1.GetLength(0); i++) {
1921 for (int j = 0; j < c1.GetLength(1); j++) {
1922 Assert.AreEqual (c1[i, j], c2[i, j], "#M65(" + i + "," + j + ")");
1928 public void TestSetValue4() {
1930 int[] c1 = { 1, 2, 3 };
1931 long[] c2 = new long [3];
1933 for (int i = 0; i < c1.Length; i++)
1934 c2.SetValue (c1 [i], i);
1936 for (int i = 0; i < c1.Length; i++) {
1937 Assert.IsTrue (c1[i] == c2[i], "#M81(" + i + ")");
1938 Assert.AreEqual (typeof (long), c2[i].GetType (), "#M82(" + i + ")");
1942 long[] c1 = { 1, 2, 3 };
1943 int[] c2 = new int [3];
1944 bool errorThrown = false;
1946 c2.SetValue (c1 [0], 0);
1947 } catch (ArgumentException) {
1950 Assert.IsTrue (errorThrown, "#M83");
1953 int[] c1 = { 1, 2, 3 };
1954 Object[] c2 = new Object [3];
1956 for (int i = 0; i < c1.Length; i++)
1957 c2.SetValue (c1 [i], i);
1959 for (int i = 0; i < c1.Length; i++)
1960 Assert.AreEqual (c1[i], Convert.ToInt32 (c2[i]), "#M84(" + i + ")");
1963 Object[] c1 = new Object [3];
1964 Object[] c2 = new Object [3];
1965 c1[0] = new Object ();
1967 for (int i = 0; i < c1.Length; i++)
1968 c2.SetValue (c1 [i], i);
1970 for (int i = 0; i < c1.Length; i++)
1971 Assert.AreEqual (c1[i], c2[i], "#M85(" + i + ")");
1974 Object[] c1 = new Object [3];
1975 string[] c2 = new String [3];
1976 string test = "hello";
1979 c2.SetValue (c1 [0], 0);
1980 Assert.AreEqual (c1[0], c2[0], "#M86");
1981 Assert.AreEqual ("hello", c2[0], "#M87");
1984 char[] c1 = { 'a', 'b', 'c' };
1985 string[] c2 = new string [3];
1987 c2.SetValue (c1 [0], 0);
1988 Assert.Fail ("#M88");
1989 } catch (InvalidCastException) {}
1992 Single[] c1 = { 1.2F, 2.3F, 3.4F, 4.5F };
1993 long[] c2 = new long [3];
1995 c2.SetValue (c1 [0], 0);
1996 Assert.Fail ("#M89");
1997 } catch (ArgumentException) {}
2025 UInt32 v11 = 235354;
2026 UInt64 v12 = 234552;
2028 Object[] va1 = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12 };
2029 Object[] va2 = { "true", "1", "a", "-1.2", "-32", "-234", "-34523", "-1",
2030 "-4.8F", "24234", "235354", "234552" };
2032 Object[][] vt = { va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1 };
2035 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2036 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
2037 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
2038 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
2039 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1,
2040 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1,
2041 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,
2042 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1,
2043 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1,
2044 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
2045 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0,
2046 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0
2051 for (int i = 0; i < types.Length; i++) {
2052 for (int j = 0; j < types.Length; j++) {
2053 Array array = Array.CreateInstance (types [j], 2);
2055 Object value = vt[j][i];
2057 bool errorThrown = false;
2059 array.SetValue (value, 0);
2060 } catch (ArgumentException) {
2064 int ex_index = (i * types.Length) + j;
2066 Assert.AreEqual (errorThrown, arg_ex [ex_index] == 1, "#M90(" + types [i] + "," + types [j] + ")");
2070 for (int i = 0; i < types.Length; i++) {
2071 String[] array = new String [2];
2073 Object value = va1 [i];
2075 bool errorThrown = false;
2077 array.SetValue (value, 0);
2078 } catch (InvalidCastException) {
2082 Assert.IsTrue (errorThrown, "#M91(" + types [i] + ")");
2085 for (int i = 0; i < types.Length; i++) {
2086 Array array = Array.CreateInstance (types [i], 2);
2088 Object value = va2 [i];
2090 bool errorThrown = false;
2092 array.SetValue (value, 0);
2093 } catch (InvalidCastException) {
2097 Assert.IsTrue (errorThrown, "#M92(" + types [i] + ")");
2100 for (int i = 0; i < types.Length; i++) {
2101 Array array = Array.CreateInstance (types [i], 2);
2103 Object value = null;
2105 bool errorThrown = false;
2107 array.SetValue (value, 0);
2108 } catch (InvalidCastException) {
2112 Assert.IsTrue (!errorThrown, "#M93(" + types [i] + ")");
2117 for (int i = 0; i < types.Length; i++) {
2118 for (int j = 0; j < types.Length; j++) {
2119 Array source = Array.CreateInstance (types [i], 2);
2120 Array array = Array.CreateInstance (types [j], 2);
2122 source.SetValue (vt[j][i], 0);
2123 source.SetValue (vt[j][i], 1);
2125 bool errorThrown = false;
2127 Array.Copy (source, array, 2);
2128 } catch (ArrayTypeMismatchException) {
2132 int ex_index = (i * types.Length) + j;
2134 Assert.AreEqual (errorThrown, arg_ex [ex_index] == 1, "#M94(" + types [i] + "," + types [j] + ")");
2138 for (int i = 0; i < types.Length; i++) {
2139 Array source = Array.CreateInstance (types [i], 2);
2140 String[] array = new String [2];
2142 source.SetValue (va1 [i], 0);
2143 source.SetValue (va1 [i], 1);
2145 bool errorThrown = false;
2147 Array.Copy (source, array, 2);
2148 } catch (ArrayTypeMismatchException) {
2152 Assert.IsTrue (errorThrown, "#M95(" + types [i] + ")");
2155 for (int i = 0; i < types.Length; i++) {
2156 String[] source = new String [2];
2157 Array array = Array.CreateInstance (types [i], 2);
2159 source.SetValue (va2 [i], 0);
2160 source.SetValue (va2 [i], 1);
2162 bool errorThrown = false;
2164 Array.Copy (source, array, 2);
2165 } catch (ArrayTypeMismatchException) {
2169 Assert.IsTrue (errorThrown, "#M96(" + types [i] + ")");
2175 public void TestSort() {
2177 bool errorThrown = false;
2180 } catch (ArgumentNullException) {
2183 Assert.IsTrue (errorThrown, "#N01");
2186 bool errorThrown = false;
2188 Array.Sort(null, 0, 1);
2189 } catch (ArgumentNullException) {
2192 Assert.IsTrue (errorThrown, "#N02");
2195 bool errorThrown = false;
2197 char[] c1 = new Char[2];
2198 Array.Sort(null, c1);
2199 } catch (ArgumentNullException) {
2202 Assert.IsTrue (errorThrown, "#N03");
2205 bool errorThrown = false;
2207 char[] c1 = new Char[2];
2208 Array.Sort(null, c1, 0, 1);
2209 } catch (ArgumentNullException) {
2212 Assert.IsTrue (errorThrown, "#N04");
2216 char[] arr = {'d', 'b', 'f', 'e', 'a', 'c'};
2219 Array.Sort (null, 0, 1);
2220 Assert.Fail ("#N" + tc.ToString ());
2222 catch (ArgumentException) {}
2223 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2227 Array.Sort (arr, -1, 3);
2228 Assert.Fail ("#N" + tc.ToString ());
2230 catch (ArgumentException) {}
2231 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2235 Array.Sort (arr, 1, -3);
2236 Assert.Fail ("#N" + tc.ToString ());
2238 catch (ArgumentException) {}
2239 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2243 Array.Sort (arr, arr.Length, arr.Length + 2);
2244 Assert.Fail ("#N" + tc.ToString ());
2246 catch (ArgumentException) {}
2247 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2250 // note: null second array => just sort first array
2251 char[] starter = {'d', 'b', 'f', 'e', 'a', 'c'};
2252 int[] starter1 = {1,2,3,4,5,6};
2254 char[] c1 = (char[])starter.Clone();
2256 Assert.AreEqual ('a', c1[0], "#N21");
2257 Assert.AreEqual ('b', c1[1], "#N22");
2258 Assert.AreEqual ('c', c1[2], "#N23");
2259 Assert.AreEqual ('d', c1[3], "#N24");
2260 Assert.AreEqual ('e', c1[4], "#N25");
2261 Assert.AreEqual ('f', c1[5], "#N26");
2264 char[] c1 = (char[])starter.Clone();
2265 int[] i1 = (int[])starter1.Clone();
2267 Assert.AreEqual ('a', c1[0], "#N41");
2268 Assert.AreEqual ('b', c1[1], "#N42");
2269 Assert.AreEqual ('c', c1[2], "#N43");
2270 Assert.AreEqual ('d', c1[3], "#N44");
2271 Assert.AreEqual ('e', c1[4], "#N45");
2272 Assert.AreEqual ('f', c1[5], "#N46");
2273 Assert.AreEqual (5, i1[0], "#N47");
2274 Assert.AreEqual (2, i1[1], "#N48");
2275 Assert.AreEqual (6, i1[2], "#N49");
2276 Assert.AreEqual (1, i1[3], "#N50");
2277 Assert.AreEqual (4, i1[4], "#N51");
2278 Assert.AreEqual (3, i1[5], "#N52");
2281 char[] c1 = (char[])starter.Clone();
2282 Array.Sort(c1, 1, 4);
2283 Assert.AreEqual ('d', c1[0], "#N61");
2284 Assert.AreEqual ('a', c1[1], "#N62");
2285 Assert.AreEqual ('b', c1[2], "#N63");
2286 Assert.AreEqual ('e', c1[3], "#N64");
2287 Assert.AreEqual ('f', c1[4], "#N65");
2288 Assert.AreEqual ('c', c1[5], "#N66");
2291 char[] c1 = (char[])starter.Clone();
2292 int[] i1 = (int[])starter1.Clone();
2293 Array.Sort(c1, i1, 1, 4);
2294 Assert.AreEqual ('d', c1[0], "#N81");
2295 Assert.AreEqual ('a', c1[1], "#N82");
2296 Assert.AreEqual ('b', c1[2], "#N83");
2297 Assert.AreEqual ('e', c1[3], "#N84");
2298 Assert.AreEqual ('f', c1[4], "#N85");
2299 Assert.AreEqual ('c', c1[5], "#N86");
2300 Assert.AreEqual (1, i1[0], "#N87");
2301 Assert.AreEqual (5, i1[1], "#N88");
2302 Assert.AreEqual (2, i1[2], "#N89");
2303 Assert.AreEqual (4, i1[3], "#N90");
2304 Assert.AreEqual (3, i1[4], "#N91");
2305 Assert.AreEqual (6, i1[5], "#N92");
2310 double[] a = new double[115];
2311 int[] b = new int[256];
2312 Array.Sort<double, int> (a, b, 0, 115);
2315 /* Check that ulong[] is not sorted as long[] */
2317 string[] names = new string[] {
2318 "A", "B", "C", "D", "E"
2321 ulong[] arr = new ulong [] {
2323 unchecked((ulong)0xffffFFFF00000000),
2330 Array.Sort (a, names, null);
2331 Assert.AreEqual (0, a.GetValue (0));
2336 public void SortNonGenericDoubleItems () {
2337 double[] doubleValues = new double[11];
2339 doubleValues[0] = 0.221788066253601;
2340 doubleValues[1] = 0.497278285809481;
2341 doubleValues[2] = 0.100565033883643;
2342 doubleValues[3] = 0.0433309347749905;
2343 doubleValues[4] = 0.00476726438463812;
2344 doubleValues[5] = 0.1354609735456;
2345 doubleValues[6] = 0.57690356588135;
2346 doubleValues[7] = 0.466239434334826;
2347 doubleValues[8] = 0.409741461978934;
2348 doubleValues[9] = 0.0112412763949565;
2349 doubleValues[10] = 0.668704347674307;
2351 int[] indices = new int[11];
2364 Array.Sort ((Array)doubleValues, (Array)indices);
2365 Assert.AreEqual (4, indices [0]);
2369 public void TestInitializeEmpty()
2381 Assert.IsTrue (!catched, "#TI01");
2385 public void TestInitializeInt()
2390 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2392 Assert.AreEqual (a[i], b[i], "#TI02 " + i);
2397 public void TestInitializeDouble()
2399 double[] a = {1.0,2.0,0.0};
2401 double[] b = {1.0,2.0,0.0};
2402 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2404 Assert.AreEqual (a[i], b[i], "#TI03 " + i);
2409 public void TestInitializeFloat()
2411 float[] a = {1.0F,2.0F,0.0F};
2413 float[] b = {1.0F,2.0F,0.0F};
2414 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2416 Assert.AreEqual (a[i], b[i], "#TI04 " + i);
2421 public void TestInitializeChar()
2423 char[] a = {'1','.','0','F','2','.','0','F'};
2425 char[] b = {'1','.','0','F','2','.','0','F'};
2426 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2428 Assert.AreEqual (a[i], b[i], "#TI05 " + i);
2433 public void TestInitializeString()
2435 string[] a = {"hola","adios","menos","mas"};
2437 string[] b = {"hola","adios","menos","mas"};
2438 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2440 Assert.AreEqual (a[i], b[i], "#TI06 " + i);
2445 public void TestInitializeEnum()
2447 enua[] a = {enua.hola,enua.adios,enua.menos,enua.mas};
2449 enua[] b = {enua.hola,enua.adios,enua.menos,enua.mas};
2450 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2452 Assert.AreEqual (a[i], b[i], "#TI07 " + i);
2457 public void TestInitializeIntNI()
2459 int[] a = new int[20];
2463 Assert.AreEqual (b, 0, "#TI08");
2468 public void TestInitializeCharNI()
2470 char[] a = new char[20];
2472 foreach(char b in a)
2474 Assert.AreEqual (b, 0, "#TI09");
2479 public void TestInitializeDoubleNI()
2481 double[] a = new double[20];
2483 foreach(double b in a)
2485 Assert.AreEqual (b, 0.0, "#TI09");
2490 public void TestInitializeStringNI()
2492 string[] a = new string[20];
2494 foreach(string b in a)
2496 Assert.AreEqual (b, null, "#TI10");
2501 public void TestInitializeObjectNI()
2503 object[] a = new object[20];
2505 foreach(object b in a)
2507 Assert.AreEqual (b, null, "#TI11");
2512 public void TestInitializeAClassNI()
2514 AClass[] a = new AClass[20];
2516 foreach(AClass b in a)
2518 Assert.AreEqual (b, null, "#TI12");
2524 public void TestInitializeAStructNI()
2526 AStruct[] a = new AStruct[20];
2528 foreach(AStruct b in a)
2530 Assert.AreEqual (b, new AStruct(), "#TI14");
2535 public void TestInitializeAStruct()
2537 AStruct[] a = new AStruct[3];
2541 AStruct[] b = new AStruct[3];
2544 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2546 Assert.AreEqual (a[i], b[i], "#TI15 " + i);
2551 public void TestInitializeDateTimeNI()
2553 DateTime[] a = new DateTime[20];
2555 foreach(DateTime b in a)
2557 Assert.AreEqual (b, new DateTime(), "#TI16");
2562 [ExpectedException (typeof (ArgumentNullException))]
2563 public void MoreSort1 ()
2565 Array.Sort (null, 0, 1);
2569 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2570 public void MoreSort2 ()
2572 Array.Sort (arrsort, -1, 3);
2576 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2577 public void MoreSort3 ()
2579 Array.Sort (arrsort, 1, -3);
2583 [ExpectedException (typeof (ArgumentException))]
2584 public void MoreSort4 ()
2586 Array.Sort (arrsort, arrsort.Length, arrsort.Length + 2);
2590 [ExpectedException (typeof (RankException))]
2591 public void MoreSort5 ()
2593 char [,] arr = new char [,] {{'a'}, {'b'}};
2594 Array.Sort (arr, 0, 1);
2598 public void MoreSort6 ()
2600 Array.Sort (arrsort, 0, 0);
2604 [ExpectedException (typeof (ArgumentException))]
2605 public void MoreSort7 ()
2607 Array.Sort (arrsort, arrsort.Length - 1, 2);
2611 [ExpectedException (typeof (ArgumentException))]
2612 public void MoreSort8 ()
2614 Array.Sort (arrsort, 0, arrsort.Length + 1);
2618 public void MoreSort9 ()
2620 Array.Sort (arrsort, null, 0, arrsort.Length, null);
2624 [ExpectedException (typeof (InvalidOperationException))]
2625 public void MoreSort10 ()
2627 object [] array = {true, 'k', SByte.MinValue, Byte.MinValue, (short) 2, 634, (long) 436, (float) 1.1, 1.23, "Hello World"};
2628 Array.Sort (array, (IComparer) null);
2631 [Test] // bug #81941
2634 double [] a = new double [2] { 0.9, 0.3 };
2635 uint [] b = new uint [2] { 4, 7 };
2637 Assert.AreEqual (0.3, a [0], "#1");
2638 Assert.AreEqual (0.9, a [1], "#2");
2639 Assert.AreEqual (7, b [0], "#3");
2640 Assert.AreEqual (4, b [1], "#4");
2644 public void ClearJaggedArray ()
2646 byte[][] matrix = new byte [8][];
2647 for (int i=0; i < 8; i++) {
2648 matrix [i] = new byte [8];
2649 for (int j=0; j < 8; j++) {
2653 Array.Clear (matrix, 0, 8);
2654 for (int i=0; i < 8; i++) {
2655 Assert.IsNull (matrix [i], i.ToString ());
2660 public void ClearMultidimentionalArray ()
2662 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2663 Array.Clear (matrix, 0, 2);
2664 Assert.AreEqual (0, matrix [0, 0], "0,0");
2665 Assert.AreEqual (0, matrix [0, 1], "0,1");
2666 Assert.AreEqual (2, matrix [1, 0], "1,0");
2667 Assert.AreEqual (2, matrix [1, 1], "1,1");
2671 [ExpectedException (typeof (IndexOutOfRangeException))]
2672 public void ClearOutsideMultidimentionalArray ()
2674 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2675 Array.Clear (matrix, 0, 5);
2679 [ExpectedException (typeof (IndexOutOfRangeException))]
2680 public void Clear_IndexOverflow ()
2682 byte[] array = new byte [16];
2683 Array.Clear (array, 4, Int32.MaxValue);
2687 [ExpectedException (typeof (IndexOutOfRangeException))]
2688 public void Clear_LengthOverflow ()
2690 byte[] array = new byte [16];
2691 Array.Clear (array, Int32.MaxValue, 4);
2695 [ExpectedException (typeof (ArgumentException))]
2696 public void Copy_SourceIndexOverflow ()
2698 byte[] array = new byte [16];
2699 Array.Copy (array, Int32.MaxValue, array, 8, 8);
2703 [ExpectedException (typeof (ArgumentException))]
2704 public void Copy_DestinationIndexOverflow ()
2706 byte[] array = new byte [16];
2707 Array.Copy (array, 8, array, Int32.MaxValue, 8);
2711 [ExpectedException (typeof (ArgumentException))]
2712 public void Copy_LengthOverflow ()
2714 byte[] array = new byte [16];
2715 Array.Copy (array, 8, array, 8, Int32.MaxValue);
2719 [ExpectedException (typeof (ArgumentException))]
2720 public void Reverse_IndexOverflow ()
2722 byte[] array = new byte [16];
2723 Array.Reverse (array, Int32.MaxValue, 8);
2727 [ExpectedException (typeof (ArgumentException))]
2728 public void Reverse_LengthOverflow ()
2730 byte[] array = new byte [16];
2731 Array.Reverse (array, 8, Int32.MaxValue);
2734 public struct CharX : IComparable {
2737 public CharX (char c)
2742 public int CompareTo (object obj)
2745 return c.CompareTo (((CharX) obj).c);
2747 return c.CompareTo (obj);
2752 public void BinarySearch_ArgPassingOrder ()
2755 // This tests that arguments are passed to the comprer in the correct
2756 // order. The IComparable of the *array* elements must get called, not
2757 // that of the search object.
2759 CharX [] x = { new CharX ('a'), new CharX ('b'), new CharX ('c') };
2760 Assert.AreEqual (1, Array.BinarySearch (x, 'b'));
2763 class Comparer: IComparer {
2765 private bool called = false;
2767 public bool Called {
2769 bool result = called;
2775 public int Compare (object x, object y)
2783 public void BinarySearch1_EmptyList ()
2785 int[] array = new int[0];
2786 Assert.AreEqual (- 1, Array.BinarySearch (array, 0), "BinarySearch");
2790 public void BinarySearch2_EmptyList ()
2792 int[] array = new int[0];
2793 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, 0), "BinarySearch");
2797 public void BinarySearch3_EmptyList ()
2799 Comparer comparer = new Comparer ();
2800 int[] array = new int[0];
2801 Assert.AreEqual (-1, Array.BinarySearch (array, 0, comparer), "BinarySearch");
2802 // bug 77030 - the comparer isn't called for an empty array/list
2803 Assert.IsTrue (!comparer.Called, "Called");
2807 public void BinarySearch4_EmptyList ()
2809 Comparer comparer = new Comparer ();
2810 int[] array = new int[0];
2811 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, comparer), "BinarySearch");
2812 // bug 77030 - the comparer isn't called for an empty array/list
2813 Assert.IsTrue (!comparer.Called, "Called");
2817 [ExpectedException (typeof (ArgumentNullException))]
2818 public void AsReadOnly_NullArray ()
2820 Array.AsReadOnly <int> (null);
2824 public void ReadOnly_Count ()
2826 Assert.AreEqual (10, Array.AsReadOnly (new int [10]).Count);
2830 public void ReadOnly_Contains ()
2832 int[] arr = new int [2];
2835 IList<int> a = Array.AsReadOnly (arr);
2837 Assert.IsTrue (a.Contains (3));
2838 Assert.IsTrue (!a.Contains (6));
2842 public void ReadOnly_IndexOf ()
2844 int[] arr = new int [2];
2847 IList<int> a = Array.AsReadOnly (arr);
2849 Assert.AreEqual (0, a.IndexOf (3));
2850 Assert.AreEqual (1, a.IndexOf (5));
2851 Assert.AreEqual (-1, a.IndexOf (6));
2855 public void ReadOnly_Indexer ()
2857 int[] arr = new int [2];
2860 IList<int> a = Array.AsReadOnly (arr);
2862 Assert.AreEqual (3, a [0]);
2863 Assert.AreEqual (5, a [1]);
2865 /* Check that modifications to the original array are visible */
2867 Assert.AreEqual (6, a [0]);
2871 public void ReadOnly_Enumerator ()
2873 int[] arr = new int [10];
2875 for (int i = 0; i < 10; ++i)
2879 foreach (int i in Array.AsReadOnly (arr))
2882 Assert.AreEqual (45, sum);
2886 public void ReadOnly_CopyTo ()
2888 int[] arr = new int [2];
2891 IList<int> a = Array.AsReadOnly (arr);
2893 int[] arr2 = new int [3];
2896 Assert.AreEqual (0, arr2 [0]);
2897 Assert.AreEqual (3, arr2 [1]);
2898 Assert.AreEqual (5, arr2 [2]);
2902 public void Resize ()
2904 int [] arr = new int [] { 1, 3, 5 };
2905 Array.Resize <int> (ref arr, 3);
2906 Assert.AreEqual (3, arr.Length, "#A1");
2907 Assert.AreEqual (1, arr [0], "#A2");
2908 Assert.AreEqual (3, arr [1], "#A3");
2909 Assert.AreEqual (5, arr [2], "#A4");
2911 Array.Resize <int> (ref arr, 2);
2912 Assert.AreEqual (2, arr.Length, "#B1");
2913 Assert.AreEqual (1, arr [0], "#B2");
2914 Assert.AreEqual (3, arr [1], "#B3");
2916 Array.Resize <int> (ref arr, 4);
2917 Assert.AreEqual (4, arr.Length, "#C1");
2918 Assert.AreEqual (1, arr [0], "#C2");
2919 Assert.AreEqual (3, arr [1], "#C3");
2920 Assert.AreEqual (0, arr [2], "#C4");
2921 Assert.AreEqual (0, arr [3], "#C5");
2925 public void Resize_null ()
2928 Array.Resize (ref arr, 10);
2929 Assert.AreEqual (arr.Length, 10);
2933 public void Test_ContainsAndIndexOf_EquatableItem ()
2935 EquatableClass[] list = new EquatableClass[] {new EquatableClass (0), new EquatableClass (1), new EquatableClass (0)};
2937 Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, list[0]), "#0");
2938 Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, new EquatableClass (0)), "#1");
2939 Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, list[0]), "#2");
2940 Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, new EquatableClass (0)), "#3");
2943 public class EquatableClass : IEquatable<EquatableClass>
2946 public EquatableClass (int x)
2951 public bool Equals (EquatableClass other)
2953 return this._x == other._x;
2958 public void AsIList ()
2960 IList<int> arr = new int [10];
2962 Assert.AreEqual (5, arr [0]);
2964 IList<FooStruct> arr2 = new FooStruct [10];
2965 FooStruct s = new FooStruct ();
2970 Assert.AreEqual (11, s.i);
2971 Assert.AreEqual (22, s.j);
2973 IList<string> arr3 = new string [10];
2975 Assert.AreEqual ("ABC", arr3 [5]);
2982 #if !TARGET_JVM // BugBUG: T[] is not yet ICollection<T> under TARGET_JVM
2985 public void ICollectionNull ()
2987 ICollection<object> test;
2989 test = new List<object>();
2990 Assert.AreEqual (test.Contains (null), false, "list<o>");
2992 test = new object[] {};
2993 Assert.AreEqual (test.Contains (null), false, "empty array");
2995 test = new object[] {null};
2996 Assert.AreEqual (test.Contains (null), true, "array with null");
2998 test = new object[] { 1, null};
2999 Assert.IsTrue (test.Contains (null), "array with last null");
3001 test = new List<object>(test);
3002 Assert.AreEqual (test.Contains (null), true, "List<object> with test");
3004 test = new object[] {new object()};
3005 Assert.AreEqual (test.Contains (null), false, "array with object");
3007 test = new List<object>(test);
3008 Assert.AreEqual (test.Contains (null), false, "array with test");
3012 public void IListNull ()
3016 test = new List<object>();
3017 Assert.AreEqual (-1, test.IndexOf (null), "list<o>");
3019 test = new object[] {};
3020 Assert.AreEqual (-1, test.IndexOf (null), "empty array");
3022 test = new object[] {null};
3023 Assert.AreEqual (0, test.IndexOf (null), "array with null");
3025 test = new object[] { 1, null};
3026 Assert.AreEqual (1, test.IndexOf (null), "array with last null");
3028 test = new List<object>(test);
3029 Assert.AreEqual (1, test.IndexOf (null), "List<object> with test");
3031 test = new object[] {new object()};
3032 Assert.AreEqual (-1, test.IndexOf (null), "array with object");
3034 test = new List<object>(test);
3035 Assert.AreEqual (-1, test.IndexOf (null), "array with test");
3038 #endif // TARGET_JVM
3042 enum ByteEnum : byte {}
3043 enum IntEnum : int {}
3046 public void TestByteEnumArrayToByteArray ()
3048 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
3049 byte[] b = new byte[a.Length];
3054 public void TestByteEnumArrayToIntArray ()
3056 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
3057 int[] b = new int[a.Length];
3062 [ExpectedException (typeof (ArrayTypeMismatchException))]
3063 public void TestIntEnumArrayToByteArray ()
3065 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
3066 byte[] b = new byte[a.Length];
3071 public void TestIntEnumArrayToIntArray ()
3073 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
3074 int[] b = new int[a.Length];
3080 [Test] // bug #322248
3081 public void IEnumerator_Reset ()
3083 int[] array = new int[] { 1, 2, 3};
3084 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3085 Assert.IsTrue (e.MoveNext (), "#A1");
3086 Assert.AreEqual (1, e.Current, "#A2");
3087 Assert.IsTrue (e.MoveNext (), "#A3");
3088 Assert.AreEqual (2, e.Current, "#A4");
3092 Assert.IsTrue (e.MoveNext (), "#C1");
3093 Assert.AreEqual (1, e.Current, "#C2");
3097 public void IEnumerator_Current_Finished ()
3099 int[] array = new int[] { 1, 2, 3 };
3100 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3101 Assert.IsTrue (e.MoveNext (), "#A1");
3102 Assert.AreEqual (1, e.Current, "#A2");
3103 Assert.IsTrue (e.MoveNext (), "#A3");
3104 Assert.AreEqual (2, e.Current, "#A4");
3105 Assert.IsTrue (e.MoveNext (), "#A5");
3106 Assert.AreEqual (3, e.Current, "#A6");
3107 Assert.IsTrue (!e.MoveNext (), "#A6");
3110 Assert.Fail ("#B1:" + e.Current);
3111 } catch (InvalidOperationException ex) {
3112 // Enumeration already finished
3113 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3114 Assert.IsNull (ex.InnerException, "#B3");
3115 Assert.IsNotNull (ex.Message, "#B4");
3120 public void IEnumerator_Current_NotStarted ()
3122 int[] array = new int[] { 1, 2, 3 };
3123 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3126 Assert.Fail ("#A1:" + e.Current);
3127 } catch (InvalidOperationException ex) {
3128 // Enumeration has not started. Call MoveNext
3129 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
3130 Assert.IsNull (ex.InnerException, "#A3");
3131 Assert.IsNotNull (ex.Message, "#A4");
3136 public void IEnumerator_Current_Reset ()
3138 int[] array = new int[] { 1, 2, 3 };
3139 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3144 Assert.Fail ("#B1:" + e.Current);
3145 } catch (InvalidOperationException ex) {
3146 // Enumeration has not started. Call MoveNext
3147 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3148 Assert.IsNull (ex.InnerException, "#B3");
3149 Assert.IsNotNull (ex.Message, "#B4");
3153 public void ICollection_IsReadOnly() {
3154 ICollection<string> arr = new string [10];
3156 Assert.IsTrue (arr.IsReadOnly);
3160 [ExpectedException (typeof (NotSupportedException))]
3161 public void ArrayCreateInstanceOfVoid ()
3163 Array.CreateInstance (typeof (void), 42);
3169 [ExpectedException (typeof (NotSupportedException))]
3170 public void ArrayCreateInstanceOfOpenGenericType ()
3172 Array.CreateInstance (typeof (Foo<>), 42);
3176 [ExpectedException (typeof (IndexOutOfRangeException))]
3177 public void ClearNegativeLength ()
3179 Array.Clear (new int [] { 1, 2 }, 0, -1);
3183 [ExpectedException (typeof (ArgumentException))]
3184 public void MultiDimension_IList_setItem ()
3186 IList array = new int [1, 1];
3191 [ExpectedException (typeof (ArgumentException))]
3192 public void MultiDimension_IList_getItem ()
3194 IList array = new int [1, 1];
3195 int a = (int) array [0];
3199 public void SetValue_Nullable () {
3200 Array array = Array.CreateInstance (typeof (int?), 7);
3204 array.SetValue (o, 0);
3205 Assert.AreEqual (42, array.GetValue (0));
3207 array.SetValue (null, 0);
3208 Assert.AreEqual (null, array.GetValue (0));
3212 public void SortNullsWithGenericVersion ()
3214 string[] s1 = new string[6]{
3222 string[] s2 = new string[]{null,
3229 Array.Sort<string> (s1);
3230 for (int i = 0; i < 6; i++) {
3231 Assert.AreEqual (s1[i], s2[i], "At:" + i);
3236 // This is a test case for the case that was broken by the code contributed
3239 // This tests the fix for: #622101
3242 public void SortActuallyWorks ()
3244 string[] data = new string[9]{"Foo", "Bar", "Dingus", null, "Dingu4", "123", "Iam", null, "NotNull"};
3245 IComparer comparer = new NullAtEndComparer ();
3246 Array.Sort (data, comparer);
3248 Assert.AreEqual (data [7], null);
3249 Assert.AreNotEqual (data [0], null);
3252 class NullAtEndComparer : IComparer {
3253 public int Compare(object x, object y)
3255 if (x == null) return 1;
3256 if (y == null) return -1;
3257 return ((string)x).CompareTo((string)y);
3263 [ExpectedException (typeof (ArgumentException))]
3264 public void CompareToWithJaggedArray () {
3265 IStructuralComparable a = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }};
3266 IStructuralComparable b = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }};
3267 a.CompareTo (b, Comparer<object>.Default);
3271 [ExpectedException (typeof (ArgumentException))]
3272 public void CompareToWithArrayOfTheWrongKind () {
3273 IStructuralComparable a = new int[] { 1, 2 };
3274 IStructuralComparable b = new double[] { 1, 2 };
3275 a.CompareTo (b, Comparer<object>.Default);
3279 [ExpectedException (typeof (ArgumentException))]
3280 public void CompareToWithNonArrayType () {
3281 IStructuralComparable a = new int[] { 1, 2 };
3282 a.CompareTo (99, Comparer<object>.Default);
3286 [ExpectedException (typeof (ArgumentException))]
3287 public void CompareToWithNonArrayOfDifferentSize () {
3288 IStructuralComparable a = new int[] { 1, 2 };
3289 IStructuralComparable b = new int[] { 1, 2, 3 };
3290 a.CompareTo (b, Comparer<object>.Default);
3294 [ExpectedException (typeof (ArgumentException))]
3295 public void CompareToWithMultiDimArray1 () {
3296 IStructuralComparable a = new int [2,2] { {10, 10 }, { 10, 10 } };
3297 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3298 a.CompareTo (b, Comparer<object>.Default);
3302 [ExpectedException (typeof (ArgumentException))]
3303 public void CompareToWithMultiDimArray2 () {
3304 IStructuralComparable a = new int [2] { 10, 10 };
3305 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3306 a.CompareTo (b, Comparer<object>.Default);
3310 [ExpectedException (typeof (ArgumentException))]
3311 public void CompareToWithMultiDimArray3 () {
3312 IStructuralComparable a = new int [4] { 10, 10, 10, 10 };
3313 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3314 a.CompareTo (b, Comparer<object>.Default);
3318 [ExpectedException (typeof (IndexOutOfRangeException))]
3319 public void CompareToWithBoundedArray1 () {
3320 IStructuralComparable a = new int [2] { 10, 10 };
3321 Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 });
3322 IStructuralComparable b = ab;
3323 ab.SetValue (10, 5);
3324 ab.SetValue (10, 6);
3326 a.CompareTo (b, Comparer<object>.Default);
3330 [ExpectedException (typeof (IndexOutOfRangeException))]
3331 public void CompareToWithBoundedArray2 () {
3332 IStructuralComparable a = new int [2] { 10, 10 };
3333 Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 });
3334 IStructuralComparable b = ab;
3335 ab.SetValue (10, 5);
3336 ab.SetValue (10, 6);
3338 //Yes, CompareTo simply doesn't work with bounded arrays!
3339 b.CompareTo (b, Comparer<object>.Default);
3343 [ExpectedException (typeof (NullReferenceException))]
3344 public void CompareToWithNullComparer () {
3345 IStructuralComparable a = new int[] { 1, 2 };
3346 IStructuralComparable b = new int[] { 1, 2 };
3347 a.CompareTo (b, null);
3351 public void CompareToWithNullArray () {
3352 IStructuralComparable a = new int[] { 1, 2 };
3353 Assert.AreEqual (1, a.CompareTo (null, Comparer<object>.Default));
3357 public void CompareToWithGoodArrays () {
3358 IStructuralComparable a = new int[] { 10, 20 };
3359 Assert.AreEqual (0, a.CompareTo (a, Comparer<object>.Default));
3360 Assert.AreEqual (0, a.CompareTo (new int [] { 10, 20 }, Comparer<object>.Default));
3361 Assert.AreEqual (-1, a.CompareTo (new int [] { 11, 20 }, Comparer<object>.Default));
3362 Assert.AreEqual (-1, a.CompareTo (new int [] { 10, 21 }, Comparer<object>.Default));
3363 Assert.AreEqual (1, a.CompareTo (new int [] { 9, 20 }, Comparer<object>.Default));
3364 Assert.AreEqual (1, a.CompareTo (new int [] { 10, 19 }, Comparer<object>.Default));
3368 public void IStructuralEquatable_Equals ()
3370 IStructuralEquatable array = new int[] {1, 2, 3};
3371 IStructuralEquatable array2 = new int[] {1, 2, 3};
3372 Assert.AreEqual (false, array.Equals (null, null));
3373 Assert.AreEqual (true, array.Equals (array, null));
3374 Assert.AreEqual (true, array.Equals (array2, EqualityComparer<int>.Default));
3378 [ExpectedException (typeof (NullReferenceException))]
3379 public void IStructuralEquatable_Equals_NoComparer ()
3381 IStructuralEquatable array = new int[] {1, 2, 3};
3382 IStructuralComparable array2 = new int[] {1, 2, 3};
3383 array.Equals (array2, null);
3387 [ExpectedException (typeof (ArgumentException))]
3388 public void IStructuralEquatable_Equals_ComparerThrows ()
3390 IStructuralEquatable array = new int[] {1, 2, 3};
3391 IStructuralComparable array2 = new int[] {1, 2, 3};
3392 array.Equals (array2, EqualityComparer<long>.Default);