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;
17 using System.Collections.Generic;
20 namespace MonoTests.System
23 enum enua {hola,adios,mas,menos};
49 public override bool Equals (object obj)
54 public override int GetHashCode ()
60 //End Auxiliary Things
63 public class ArrayTest
65 char [] arrsort = {'d', 'b', 'f', 'e', 'a', 'c'};
70 public void TestIsFixedSize() {
72 Assert.IsTrue (a1.IsFixedSize, "All arrays are fixed");
76 public void TestIsReadOnly() {
78 Assert.IsTrue (!a1.IsReadOnly, "No array is readonly");
82 public void TestIsSynchronized() {
84 Assert.IsTrue (!a1.IsSynchronized, "No array is synchronized");
88 public void TestLength() {
91 Assert.AreEqual (0, a1.Length, "Zero length array");
95 Assert.AreEqual (1, a1.Length, "One-length array");
98 char[] a1 = {'c', 'c'};
99 Assert.AreEqual (2, a1.Length, "Two-length array");
104 public void TestRank() {
105 char[] a1 = { 'c', 'd', 'e' };
106 Assert.AreEqual (1, a1.Rank, "Rank one");
108 char[,] a2 = new Char[3,3];
109 Assert.AreEqual (2, a2.Rank, "Rank two");
111 char[,,] a3 = new Char[3,3,3];
112 Assert.AreEqual (3, a3.Rank, "Rank three");
116 public void TestBinarySearch1() {
117 bool errorThrown = false;
119 Array.BinarySearch(null, "blue");
120 } catch (ArgumentNullException) {
123 Assert.IsTrue (errorThrown, "#B01");
126 char[,] c1 = new Char[2,2];
127 Array.BinarySearch(c1, "needle");
128 } catch (RankException) {
131 Assert.IsTrue (errorThrown, "#B02");
134 char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
135 Assert.IsTrue (Array.BinarySearch(arr, 'c') >= 3, "#B05");
136 Assert.IsTrue (Array.BinarySearch(arr, 'c') < 6, "#B06");
139 char[] arr = {'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
140 Assert.AreEqual (-4, Array.BinarySearch(arr, 'c'), "#B07");
143 char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
144 Assert.AreEqual (-9, Array.BinarySearch(arr, 'e'), "#B08");
149 public void TestBinarySearch2() {
150 bool errorThrown = false;
152 Array.BinarySearch(null, 0, 1, "blue");
153 } catch (ArgumentNullException) {
156 Assert.IsTrue (errorThrown, "#B20");
159 char[,] c1 = new Char[2,2];
160 Array.BinarySearch(c1, 0, 1, "needle");
161 } catch (RankException) {
164 Assert.IsTrue (errorThrown, "#B21");
168 Array.BinarySearch(c1, -1, 1, 'a');
169 } catch (ArgumentOutOfRangeException) {
172 Assert.IsTrue (errorThrown, "#B22");
176 Array.BinarySearch(c1, 0, -1, 'a');
177 } catch (ArgumentOutOfRangeException) {
180 Assert.IsTrue (errorThrown, "#B23");
184 Array.BinarySearch(c1, 0, 4, 'a');
185 } catch (ArgumentException) {
188 Assert.IsTrue (errorThrown, "#B24");
191 char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
192 Assert.IsTrue (Array.BinarySearch(arr, 2, 8, 'c') >= 5, "#B26");
193 Assert.IsTrue (Array.BinarySearch(arr, 2, 8, 'c') < 8, "#B27");
196 char[] arr = {'z', 'z', 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
197 Assert.AreEqual (-6, Array.BinarySearch(arr, 2, 8, 'c'), "#B28");
200 char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
201 Assert.AreEqual (-11, Array.BinarySearch(arr, 2, 8, 'e'), "#B29");
205 public void TestBinarySearch3()
207 int[] array = new int[100];
209 for (int i = 0; i < 100; i++)
212 Assert.AreEqual (49, Array.BinarySearch(array, 10), "#B30");
216 public void BinarySearch_NullValue ()
218 int[] array = new int[1];
219 Assert.AreEqual (-1, Array.BinarySearch (array, null), "I=a,o");
220 Assert.AreEqual (-1, Array.BinarySearch (array, null, null), "I=a,o,c");
221 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 1, null), "I=a,i,i,o");
222 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 1, null, null), "I=a,i,i,o,c");
224 object[] o = new object [3] { this, this, null };
225 Assert.AreEqual (-1, Array.BinarySearch (o, null), "O=a,o");
226 Assert.AreEqual (-1, Array.BinarySearch (o, null, null), "O=a,o,c");
227 Assert.AreEqual (-1, Array.BinarySearch (o, 0, 3, null), "O=a,i,i,o");
228 Assert.AreEqual (-1, Array.BinarySearch (o, 0, 3, null, null), "O=a,i,i,o,c");
231 // TODO - testBinarySearch with explicit IComparer args
234 public void TestClear() {
235 bool errorThrown = false;
237 Array.Clear(null, 0, 1);
238 } catch (ArgumentNullException) {
241 Assert.IsTrue (errorThrown, "#C01");
243 int[] i1 = { 1, 2, 3, 4 };
245 int[] compare = {1,2,3,4};
246 Assert.AreEqual (compare[0], i1[0], "#C02");
247 Assert.AreEqual (compare[1], i1[1], "#C03");
248 Assert.AreEqual (compare[2], i1[2], "#C04");
249 Assert.AreEqual (compare[3], i1[3], "#C05");
251 Array.Clear(i1, 3, 1);
253 int[] compare = {1,2,3,0};
254 Assert.AreEqual (compare[0], i1[0], "#C06");
255 Assert.AreEqual (compare[1], i1[1], "#C07");
256 Assert.AreEqual (compare[2], i1[2], "#C08");
257 Assert.AreEqual (compare[3], i1[3], "#C09");
259 Array.Clear(i1, 1, 1);
261 int[] compare = {1,0,3,0};
262 Assert.AreEqual (compare[0], i1[0], "#C10");
263 Assert.AreEqual (compare[1], i1[1], "#C11");
264 Assert.AreEqual (compare[2], i1[2], "#C12");
265 Assert.AreEqual (compare[3], i1[3], "#C13");
267 Array.Clear(i1, 1, 3);
269 int[] compare = {1,0,0,0};
270 Assert.AreEqual (compare[0], i1[0], "#C14");
271 Assert.AreEqual (compare[1], i1[1], "#C15");
272 Assert.AreEqual (compare[2], i1[2], "#C16");
273 Assert.AreEqual (compare[3], i1[3], "#C17");
276 string[] s1 = { "red", "green", "blue" };
277 Array.Clear(s1, 0, 3);
279 string[] compare = {null, null, null};
280 Assert.AreEqual (compare[0], s1[0], "#C18");
281 Assert.AreEqual (compare[1], s1[1], "#C19");
282 Assert.AreEqual (compare[2], s1[2], "#C20");
287 public void TestClone() {
288 char[] c1 = {'a', 'b', 'c'};
289 char[] c2 = (char[])c1.Clone();
290 Assert.AreEqual (c1[0], c2[0], "#D01");
291 Assert.AreEqual (c1[1], c2[1], "#D02");
292 Assert.AreEqual (c1[2], c2[2], "#D03");
294 char[] d10 = {'a', 'b'};
295 char[] d11 = {'a', 'c'};
296 char[] d12 = {'b', 'c'};
297 char[][] d1 = {d10, d11, d12};
298 char[][] d2 = (char[][])d1.Clone();
299 Assert.AreEqual (d1[0], d2[0], "#D04");
300 Assert.AreEqual (d1[1], d2[1], "#D05");
301 Assert.AreEqual (d1[2], d2[2], "#D06");
304 Assert.AreEqual (d1[0], d2[0], "#D07");
308 public void TestMemberwiseClone () {
309 int[] array = new int[] { 1, 2, 3 };
310 MethodBase mi = array.GetType ().GetMethod("MemberwiseClone",
311 BindingFlags.Instance | BindingFlags.NonPublic);
312 int[] res = (int[])mi.Invoke (array, null);
313 Assert.AreEqual (3, res.Length);
316 [Test] public void TestIndexer ()
318 int [] a = new int [10];
322 Assert.Fail ("IList.this [-1] should throw");
323 } catch (IndexOutOfRangeException) {
326 Assert.Fail ("Should have thrown an IndexOutOfRangeException");
331 public void TestCopy() {
333 bool errorThrown = false;
336 Array.Copy(c1, null, 1);
337 } catch (ArgumentNullException) {
340 Assert.IsTrue (errorThrown, "#E01");
343 bool errorThrown = false;
346 Array.Copy(null, c1, 1);
347 } catch (ArgumentNullException) {
350 Assert.IsTrue (errorThrown, "#E02");
353 bool errorThrown = false;
355 Char[] c1 = new Char[1];
356 Char[,] c2 = new Char[1,1];
357 Array.Copy(c1, c2, 1);
358 } catch (RankException) {
361 Assert.IsTrue (errorThrown, "#E03");
364 bool errorThrown = false;
366 Char[] c1 = new Char[1];
367 string[] s1 = new String[1];
368 Array.Copy(c1, s1, 1);
369 } catch (ArrayTypeMismatchException) {
372 Assert.IsTrue (errorThrown, "#E04");
375 bool errorThrown = false;
377 Char[] c1 = new Char[1];
378 Object[] o1 = new Object[1];
380 Array.Copy(o1, c1, 1);
381 } catch (InvalidCastException) {
384 Assert.IsTrue (errorThrown, "#E05");
387 bool errorThrown = false;
389 Char[] c1 = new Char[1];
390 Char[] c2 = new Char[1];
391 Array.Copy(c1, c2, -1);
392 } catch (ArgumentOutOfRangeException) {
395 Assert.IsTrue (errorThrown, "#E06");
398 bool errorThrown = false;
400 Char[] c1 = new Char[1];
401 Char[] c2 = new Char[2];
402 Array.Copy(c1, c2, 2);
403 } catch (ArgumentException) {
406 Assert.IsTrue (errorThrown, "#E07");
409 bool errorThrown = false;
411 Char[] c1 = new Char[1];
412 Char[] c2 = new Char[2];
413 Array.Copy(c2, c1, 2);
414 } catch (ArgumentException) {
417 Assert.IsTrue (errorThrown, "#E08");
420 char[] orig = {'a', 'b', 'd', 'a'};
421 char[] copy = new Char[4];
422 Array.Copy(orig, copy, 4);
423 for (int i = 0; i < orig.Length; i++) {
424 Assert.AreEqual (orig[i], copy[i], "#E09(" + i + ")");
426 Array.Clear(copy, 0, copy.Length);
427 for (int i = 0; i < orig.Length; i++) {
428 Assert.AreEqual ((char)0, copy[i], "#E10(" + i + ")");
430 Array.Copy(orig, copy, 2);
431 Assert.AreEqual (orig[0], copy[0], "#E11");
432 Assert.AreEqual (orig[1], copy[1], "#E12");
433 Assert.IsTrue (orig[2] != copy[2], "#E13");
434 Assert.IsTrue (orig[3] != copy[3], "#E14");
438 public void TestCopy2() {
440 bool errorThrown = false;
442 Char[] c1 = new Char[2];
443 Char[] c2 = new Char[2];
444 Array.Copy(c2, 1, c1, 0, 2);
445 } catch (ArgumentException) {
448 Assert.IsTrue (errorThrown, "#E31");
451 bool errorThrown = false;
453 Char[] c1 = new Char[2];
454 Char[] c2 = new Char[2];
455 Array.Copy(c2, 0, c1, 1, 2);
456 } catch (ArgumentException) {
459 Assert.IsTrue (errorThrown, "#E32");
462 char[] orig = {'a', 'b', 'd', 'a'};
463 char[] copy = new Char[4];
464 Array.Copy(orig, 1, copy, 1, 3);
465 Assert.IsTrue (copy[0] != orig[0], "#E33");
466 for (int i = 1; i < orig.Length; i++) {
467 Assert.AreEqual (orig[i], copy[i], "#E34(" + i + ")");
469 Array.Clear(copy, 0, copy.Length);
470 Array.Copy(orig, 1, copy, 0, 2);
471 Assert.AreEqual (orig[1], copy[0], "#E35");
472 Assert.AreEqual (orig[2], copy[1], "#E36");
473 Assert.IsTrue (copy[2] != orig[2], "#E37");
474 Assert.IsTrue (copy[3] != orig[3], "#E38");
478 [ExpectedException (typeof (InvalidCastException))]
479 public void Copy_InvalidCast () {
480 object[] arr1 = new object [10];
481 Type[] arr2 = new Type [10];
483 arr1 [0] = new object ();
485 Array.Copy (arr1, 0, arr2, 0, 10);
489 public void TestCopyTo() {
491 bool errorThrown = false;
493 Char[] c1 = new Char[2];
495 } catch (ArgumentNullException) {
498 Assert.IsTrue (errorThrown, "#E61");
501 bool errorThrown = false;
503 Char[] c1 = new Char[2];
504 Char[,] c2 = new Char[2,2];
506 } catch (ArgumentException) {
509 #if TARGET_JVM // This is really implementation dependent behaviour.
510 catch (RankException) {
514 Assert.IsTrue (errorThrown, "#E62");
517 bool errorThrown = false;
519 Char[,] c1 = new Char[2,2];
520 Char[] c2 = new Char[2];
522 } catch (RankException) {
525 Assert.IsTrue (errorThrown, "#E63");
528 bool errorThrown = false;
530 Char[,] c1 = new Char[2,2];
531 Char[] c2 = new Char[2];
533 } catch (RankException) {
536 Assert.IsTrue (errorThrown, "#E64");
539 bool errorThrown = false;
541 Char[] c1 = new Char[2];
542 Char[] c2 = new Char[2];
544 } catch (ArgumentOutOfRangeException) {
547 Assert.IsTrue (errorThrown, "#E65");
550 bool errorThrown = false;
552 Char[] c1 = new Char[2];
553 Char[] c2 = new Char[2];
555 } catch (ArgumentException) {
558 Assert.IsTrue (errorThrown, "#E66");
561 bool errorThrown = false;
563 Char[] c1 = new Char[2];
564 Char[] c2 = new Char[2];
566 } catch (ArgumentException) {
569 Assert.IsTrue (errorThrown, "#E67");
573 bool errorThrown = false;
575 String[] c1 = new String[2];
576 // TODO: this crashes mono if there are null
577 // values in the array.
580 Char[] c2 = new Char[2];
584 } catch (ArrayTypeMismatchException) {
587 Assert.IsTrue (errorThrown, "#E68");
590 Char[] orig = {'a', 'b', 'c', 'd'};
591 Char[] copy = new Char[10];
592 Array.Clear(copy, 0, copy.Length);
593 orig.CopyTo(copy, 3);
594 Assert.AreEqual ((char)0, copy[0], "#E69");
595 Assert.AreEqual ((char)0, copy[1], "#E70");
596 Assert.AreEqual ((char)0, copy[2], "#E71");
597 Assert.AreEqual (orig[0], copy[3], "#E72");
598 Assert.AreEqual (orig[1], copy[4], "#E73");
599 Assert.AreEqual (orig[2], copy[5], "#E74");
600 Assert.AreEqual (orig[3], copy[6], "#E75");
601 Assert.AreEqual ((char)0, copy[7], "#E76");
602 Assert.AreEqual ((char)0, copy[8], "#E77");
603 Assert.AreEqual ((char)0, copy[9], "#E78");
606 // The following is valid and must not throw an exception.
607 bool errorThrown = false;
609 int[] src = new int [0];
610 int[] dest = new int [0];
611 src.CopyTo (dest, 0);
612 } catch (ArgumentException) {
615 Assert.IsTrue (!errorThrown, "#E79");
620 bool errorThrown = false;
622 CClass[] src = new CClass [] { new CClass () };
623 BClass[] dest = new BClass [1];
625 src.CopyTo (dest, 0);
627 } catch (ArrayTypeMismatchException) {
630 Assert.IsTrue (errorThrown, "#E80");
635 public void TestCreateInstance() {
637 bool errorThrown = false;
639 Array.CreateInstance(null, 12);
640 } catch (ArgumentNullException) {
643 Assert.IsTrue (errorThrown, "#F01");
646 bool errorThrown = false;
648 Array.CreateInstance(Type.GetType("System.Char"), -3);
649 } catch (ArgumentOutOfRangeException) {
652 Assert.IsTrue (errorThrown, "#F02");
655 bool errorThrown = false;
657 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
658 } catch (ArgumentNullException) {
661 Assert.IsTrue (errorThrown, "#F03a");
665 bool errorThrown = false;
667 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
668 } catch (ArgumentNullException) {
671 Assert.IsTrue (errorThrown, "#F03b");
674 #if !TARGET_JVM // Arrays lower bounds are not supported for TARGET_JVM
676 bool errorThrown = false;
678 Array.CreateInstance(Type.GetType("System.Char"), null, null);
679 } catch (ArgumentNullException) {
682 Assert.IsTrue (errorThrown, "#F04");
686 bool errorThrown = false;
688 int[] lengths = new int [0];
689 Array.CreateInstance(Type.GetType("System.Char"), lengths);
690 } catch (ArgumentException) {
693 Assert.IsTrue (errorThrown, "#F05");
695 #if !TARGET_JVM // CreateInstance with lower bounds not supported for TARGET_JVM
697 bool errorThrown = false;
699 int[] lengths = new int [1];
700 int[] bounds = new int [2];
701 Array.CreateInstance(Type.GetType("System.Char"), lengths, bounds);
703 } catch (ArgumentException) {
706 Assert.IsTrue (errorThrown, "#F06");
709 char[] c1 = (char[])Array.CreateInstance(Type.GetType("System.Char"), 12);
710 Assert.AreEqual (12, c1.Length, "#F07");
712 Array c2 = Array.CreateInstance(Type.GetType("System.Char"), 12, 5);
713 Assert.AreEqual (2, c2.Rank, "#F08");
714 Assert.AreEqual (60, c2.Length, "#F09");
718 int[] lengths = { 3 };
719 int[] bounds = { 5 };
720 int[] src = { 512, 718, 912 };
721 Array array = Array.CreateInstance(typeof(int), lengths, bounds);
723 Assert.AreEqual (3, array.Length, "#F10");
724 Assert.AreEqual (5, array.GetLowerBound(0), "#F11");
725 Assert.AreEqual (7, array.GetUpperBound(0), "#F12");
727 src.CopyTo (array, 5);
729 for (int i = 0; i < src.Length; i++)
730 Assert.AreEqual (src[i], array.GetValue(i+5), "#F13(" + i + ")");
733 // Test that a 1 dimensional array with 0 lower bound is the
734 // same as an szarray
735 Type szarrayType = new int [10].GetType ();
736 Assert.IsTrue (szarrayType == (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0})).GetType ());
737 Assert.IsTrue (szarrayType != (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {1})).GetType ());
742 [ExpectedException (typeof (ArgumentNullException))]
743 public void TestCreateInstance2 ()
745 Array.CreateInstance (typeof (Int32), (int[])null);
750 [ExpectedException (typeof (ArgumentNullException))]
752 [ExpectedException (typeof (NullReferenceException))]
754 public void TestCreateInstance2b ()
756 Array.CreateInstance (typeof (Int32), (long[])null);
760 public void TestGetEnumerator() {
761 String[] s1 = {"this", "is", "a", "test"};
762 IEnumerator en = s1.GetEnumerator ();
763 Assert.IsNotNull (en, "#G01");
765 Assert.IsTrue (en.MoveNext (), "#G02");
766 Assert.AreEqual ("this", en.Current, "#G03");
767 Assert.IsTrue (en.MoveNext (), "#G04");
768 Assert.AreEqual ("is", en.Current, "#G05");
769 Assert.IsTrue (en.MoveNext (), "#G06");
770 Assert.AreEqual ("a", en.Current, "#G07");
771 Assert.IsTrue (en.MoveNext (), "#G08");
772 Assert.AreEqual ("test", en.Current, "#G09");
773 Assert.IsTrue (!en.MoveNext (), "#G10");
776 Assert.IsTrue (en.MoveNext (), "#G11");
777 Assert.AreEqual ("this", en.Current, "#G12");
779 // mutation does not invalidate array enumerator!
780 s1.SetValue ("change", 1);
781 Assert.IsTrue (en.MoveNext (), "#G13");
782 Assert.AreEqual ("change", en.Current, "#G14");
786 public void TestGetEnumeratorMultipleDimension() {
787 String[,] s1 = {{"this", "is"}, {"a", "test"}};
788 IEnumerator en = s1.GetEnumerator ();
789 Assert.IsNotNull (en, "#AA01");
791 Assert.IsTrue (en.MoveNext (), "#AA02");
792 Assert.AreEqual ("this", en.Current, "#AA03");
793 Assert.IsTrue (en.MoveNext (), "#AA04");
794 Assert.AreEqual ("is", en.Current, "#AA05");
795 Assert.IsTrue (en.MoveNext (), "#AA06");
796 Assert.AreEqual ("a", en.Current, "#AA07");
797 Assert.IsTrue (en.MoveNext (), "#AA08");
798 Assert.AreEqual ("test", en.Current, "#AA09");
799 Assert.IsTrue (!en.MoveNext (), "#AA10");
802 Assert.IsTrue (en.MoveNext (), "#AA11");
803 Assert.AreEqual ("this", en.Current, "#AA12");
806 // mutation does not invalidate array enumerator!
807 s1.SetValue ("change", idxs);
808 Assert.IsTrue (en.MoveNext (), "#AA13");
809 Assert.AreEqual ("change", en.Current, "#AA14");
813 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
814 public void TestGetEnumeratorNonZeroLowerBounds() {
815 int[] myLengthsArray = new int[2] { 3, 5 };
816 int[] myBoundsArray = new int[2] { 2, 3 };
818 Array myArray=Array.CreateInstance( typeof(String), myLengthsArray, myBoundsArray );
819 for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
820 for ( int j = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++ ) {
821 int[] myIndicesArray = new int[2] { i, j };
822 myArray.SetValue( Convert.ToString(i) + j, myIndicesArray );
824 IEnumerator en = myArray.GetEnumerator ();
825 Assert.IsNotNull (en, "#AB01");
827 // check the first couple of values
828 Assert.IsTrue (en.MoveNext (), "#AB02");
829 Assert.AreEqual ("23", en.Current, "#AB03");
830 Assert.IsTrue (en.MoveNext (), "#AB04");
831 Assert.AreEqual ("24", en.Current, "#AB05");
833 // then check the last element's value
836 lastElement = (string)en.Current;
837 } while (en.MoveNext());
838 Assert.AreEqual ("47", lastElement, "#AB06");
842 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
843 public void TestIList_Add () {
844 int[] myLengthsArray = new int[2] { 3, 5 };
845 int[] myBoundsArray = new int[2] { 2, 3 };
847 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
849 ((IList)myArray).Add ("can not");
850 Assert.Fail ("IList.Add should throw");
852 catch (NotSupportedException) {
856 Assert.Fail ("IList.Add threw wrong exception type");
859 Assert.Fail ("IList.Add shouldn't get this far");
863 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
864 public void TestIList_Insert () {
865 int[] myLengthsArray = new int[2] { 3, 5 };
866 int[] myBoundsArray = new int[2] { 2, 3 };
868 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
870 ((IList)myArray).Insert (0, "can not");
871 Assert.Fail ("IList.Insert should throw");
873 catch (NotSupportedException) {
877 Assert.Fail ("IList.Insert threw wrong exception type");
880 Assert.Fail ("IList.Insert shouldn't get this far");
884 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
885 public void TestIList_Remove () {
886 int[] myLengthsArray = new int[2] { 3, 5 };
887 int[] myBoundsArray = new int[2] { 2, 3 };
889 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
891 ((IList)myArray).Remove ("can not");
892 Assert.Fail ("IList.Remove should throw");
894 catch (NotSupportedException) {
898 Assert.Fail ("IList.Remove threw wrong exception type");
901 Assert.Fail ("IList.Remove shouldn't get this far");
905 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
906 public void TestIList_RemoveAt () {
907 int[] myLengthsArray = new int[2] { 3, 5 };
908 int[] myBoundsArray = new int[2] { 2, 3 };
910 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
912 ((IList)myArray).RemoveAt (0);
913 Assert.Fail ("IList.RemoveAt should throw");
915 catch (NotSupportedException) {
919 Assert.Fail ("IList.RemoveAt threw wrong exception type");
922 Assert.Fail ("IList.RemoveAt shouldn't get this far");
926 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
927 public void TestIList_Contains () {
928 int[] myLengthsArray = new int[2] { 3, 5 };
929 int[] myBoundsArray = new int[2] { 2, 3 };
931 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
934 bool b = ((IList)myArray).Contains ("23");
935 Assert.Fail ("IList.Contains should throw with multi-dimensional arrays");
937 catch (RankException) {
938 int[] iArr = new int[3] { 1, 2, 3};
939 // check the first and last items
940 Assert.IsTrue (((IList)iArr).Contains (1), "AC01");
941 Assert.IsTrue (((IList)iArr).Contains (3), "AC02");
943 // and one that is definately not there
944 Assert.IsTrue (!((IList)iArr).Contains (42), "AC03");
948 Assert.Fail ("Should not get here");
952 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
953 public void TestIList_IndexOf () {
954 int[] myLengthsArray = new int[2] { 3, 5 };
955 int[] myBoundsArray = new int[2] { 2, 3 };
957 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
960 bool b = ((IList)myArray).Contains ("23");
961 Assert.Fail ("IList.Contains should throw with multi-dimensional arrays");
963 catch (RankException) {
964 int[] iArr = new int[3] { 1, 2, 3};
965 // check the first and last items
966 Assert.AreEqual (0, ((IList)iArr).IndexOf (1), "AD01");
967 Assert.AreEqual (2, ((IList)iArr).IndexOf (3), "AD02");
969 // and one that is definately not there
970 Assert.AreEqual (-1, ((IList)iArr).IndexOf (42), "AD03");
972 catch (Exception e) {
973 Assert.Fail ("Unexpected exception: " + e.ToString());
976 // check that wierd case whem lowerbound is Int32.MinValue,
977 // so that IndexOf() needs to return Int32.MaxValue when it cannot find the object
978 int[] myLengthArray = new int[1] { 3 };
979 int[] myBoundArray = new int[1] { Int32.MinValue };
980 Array myExtremeArray=Array.CreateInstance ( typeof(String), myLengthArray, myBoundArray );
981 Assert.AreEqual (Int32.MaxValue, ((IList)myExtremeArray).IndexOf (42), "AD04");
986 public void TestGetLength() {
988 bool errorThrown = false;
990 char[] c1 = {'a', 'b', 'c'};
992 } catch (IndexOutOfRangeException) {
995 Assert.IsTrue (errorThrown, "#H01");
998 bool errorThrown = false;
1000 char[] c1 = {'a', 'b', 'c'};
1002 } catch (IndexOutOfRangeException) {
1005 Assert.IsTrue (errorThrown, "#H02");
1008 char[] c2 = new Char[5];
1009 Assert.AreEqual (5, c2.GetLength(0), "#H03");
1011 char[,] c3 = new Char[6,7];
1012 Assert.AreEqual (6, c3.GetLength(0), "#H04");
1013 Assert.AreEqual (7, c3.GetLength(1), "#H05");
1017 public void TestGetLowerBound() {
1019 bool errorThrown = false;
1021 char[] c = {'a', 'b', 'c'};
1022 c.GetLowerBound(-1);
1023 } catch (IndexOutOfRangeException) {
1026 Assert.IsTrue (errorThrown, "#H31");
1029 bool errorThrown = false;
1031 char[] c = {'a', 'b', 'c'};
1033 } catch (IndexOutOfRangeException) {
1036 Assert.IsTrue (errorThrown, "#H32");
1039 char[] c1 = new Char[5];
1040 Assert.AreEqual (0, c1.GetLowerBound(0), "#H33");
1042 char[,] c2 = new Char[4,4];
1043 Assert.AreEqual (0, c2.GetLowerBound(0), "#H34");
1044 Assert.AreEqual (0, c2.GetLowerBound(1), "#H35");
1048 public void TestGetUpperBound() {
1050 bool errorThrown = false;
1052 char[] c = {'a', 'b', 'c'};
1053 c.GetUpperBound(-1);
1054 } catch (IndexOutOfRangeException) {
1057 Assert.IsTrue (errorThrown, "#H61");
1060 bool errorThrown = false;
1062 char[] c = {'a', 'b', 'c'};
1064 } catch (IndexOutOfRangeException) {
1067 Assert.IsTrue (errorThrown, "#H62");
1070 char[] c1 = new Char[5];
1071 Assert.AreEqual (4, c1.GetUpperBound(0), "#H63");
1073 char[,] c2 = new Char[4,6];
1074 Assert.AreEqual (3, c2.GetUpperBound(0), "#H64");
1075 Assert.AreEqual (5, c2.GetUpperBound(1), "#H65");
1079 public void TestGetValue1() {
1081 bool errorThrown = false;
1083 char[,] c = new Char[2,2];
1085 } catch (ArgumentException) {
1088 Assert.IsTrue (errorThrown, "#I01");
1091 bool errorThrown = false;
1093 char[] c = {'a', 'b', 'c'};
1095 } catch (IndexOutOfRangeException) {
1098 Assert.IsTrue (errorThrown, "#I02");
1101 bool errorThrown = false;
1103 char[] c = {'a', 'b', 'c'};
1105 } catch (IndexOutOfRangeException) {
1108 Assert.IsTrue (errorThrown, "#I03");
1111 char[] c1 = {'a', 'b', 'c', 'd'};
1112 for (int i = 0; i < c1.Length; i++) {
1113 Assert.AreEqual (c1[i], c1.GetValue(i), "#I04(" + i + ")");
1118 public void TestGetValue2() {
1120 bool errorThrown = false;
1122 char[] c = new Char[2];
1124 } catch (ArgumentException) {
1127 Assert.IsTrue (errorThrown, "#I21");
1130 bool errorThrown = false;
1132 char[,] c = new Char[2,2];
1134 } catch (IndexOutOfRangeException) {
1137 Assert.IsTrue (errorThrown, "#I22");
1140 bool errorThrown = false;
1142 char[,] c = new Char[2,2];
1144 } catch (IndexOutOfRangeException) {
1147 Assert.IsTrue (errorThrown, "#I23");
1150 char[,] c1 = new Char[4,6];
1151 for (int i = 0; i < 24; i++) {
1154 c1[first,second] = (char)(((int)'a')+i);
1156 for (int i = 0; i < c1.GetLength(0); i++) {
1157 for (int j = 0; j < c1.GetLength(1); j++) {
1158 Assert.AreEqual (c1[i, j], c1.GetValue(i, j), "#I24(" + i + "," + j + ")");
1164 public void TestGetValue3() {
1166 bool errorThrown = false;
1168 char[] c = new Char[2];
1170 } catch (ArgumentException) {
1173 Assert.IsTrue (errorThrown, "#I41");
1176 bool errorThrown = false;
1178 char[,,] c = new Char[2,2,2];
1179 c.GetValue(-1, 1, 1);
1180 } catch (IndexOutOfRangeException) {
1183 Assert.IsTrue (errorThrown, "#I42");
1186 bool errorThrown = false;
1188 char[,,] c = new Char[2,2,2];
1190 } catch (IndexOutOfRangeException) {
1193 Assert.IsTrue (errorThrown, "#I43");
1196 char[,,] c1 = new Char[4,2,3];
1197 for (int i = 0; i < 24; i++) {
1199 int remains = i % 6;
1200 int second = remains / 3;
1201 int third = remains % 3;
1202 c1[first,second, third] = (char)(((int)'a')+i);
1204 for (int i = 0; i < c1.GetLength(0); i++) {
1205 for (int j = 0; j < c1.GetLength(1); j++) {
1206 for (int k = 0; k < c1.GetLength(2); k++) {
1207 Assert.AreEqual (c1[i, j, k], c1.GetValue(i, j, k), "#I44(" + i + "," + j + ")");
1215 [ExpectedException (typeof (ArgumentNullException))]
1217 [ExpectedException (typeof (NullReferenceException))]
1219 public void TestGetValueLongArray ()
1221 char[] c = new Char[2];
1222 c.GetValue((long [])null);
1226 public void TestGetValueN() {
1228 bool errorThrown = false;
1230 char[] c = new Char[2];
1231 c.GetValue((int [])null);
1232 } catch (ArgumentNullException) {
1235 Assert.IsTrue (errorThrown, "#I61a");
1238 bool errorThrown = false;
1240 char[] c = new Char[2];
1241 int[] coords = {1, 1};
1243 } catch (ArgumentException) {
1246 Assert.IsTrue (errorThrown, "#I62");
1249 bool errorThrown = false;
1251 char[,] c = new Char[2,2];
1252 int[] coords = {-1, 1};
1254 } catch (IndexOutOfRangeException) {
1257 Assert.IsTrue (errorThrown, "#I63");
1260 bool errorThrown = false;
1262 char[,] c = new Char[2,2];
1263 int[] coords = {4, 1};
1265 } catch (IndexOutOfRangeException) {
1268 Assert.IsTrue (errorThrown, "#I64");
1271 char[,] c1 = new Char[4,6];
1272 for (int i = 0; i < 24; i++) {
1275 c1[first,second] = (char)(((int)'a')+i);
1277 for (int i = 0; i < c1.GetLength(0); i++) {
1278 for (int j = 0; j < c1.GetLength(1); j++) {
1279 int[] coords = {i, j};
1280 Assert.AreEqual (c1[i, j], c1.GetValue(coords), "#I65(" + i + "," + j + ")");
1286 public void TestIndexOf1() {
1288 bool errorThrown = false;
1290 Array.IndexOf(null, "huh?");
1291 } catch (ArgumentNullException) {
1294 Assert.IsTrue (errorThrown, "#J01");
1297 bool errorThrown = false;
1299 char[,] c = new Char[2,2];
1300 Array.IndexOf(c, "huh?");
1301 } catch (RankException) {
1304 Assert.IsTrue (errorThrown, "#J02");
1307 String[] s1 = {"this", "is", "a", "test"};
1308 Assert.AreEqual (-1, Array.IndexOf(s1, null), "#J03");
1309 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing"), "#J04");
1310 Assert.AreEqual (0, Array.IndexOf(s1, "this"), "#J05");
1311 Assert.AreEqual (3, Array.IndexOf(s1, "test"), "#J06");
1315 public void TestIndexOf2() {
1317 bool errorThrown = false;
1319 Array.IndexOf(null, "huh?", 0);
1320 } catch (ArgumentNullException) {
1323 Assert.IsTrue (errorThrown, "#J21");
1326 bool errorThrown = false;
1328 char[,] c = new Char[2,2];
1329 Array.IndexOf(c, "huh?", 0);
1330 } catch (RankException) {
1333 Assert.IsTrue (errorThrown, "#J22");
1336 bool errorThrown = false;
1338 char[] c = new Char[2];
1339 Array.IndexOf(c, "huh?", 3);
1340 } catch (ArgumentOutOfRangeException) {
1343 Assert.IsTrue (errorThrown, "#J23");
1346 String[] s1 = {"this", "is", "really", "a", "test"};
1347 Assert.AreEqual (-1, Array.IndexOf(s1, null, 1), "#J24");
1348 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing", 1), "#J25");
1349 Assert.AreEqual (-1, Array.IndexOf(s1, "this", 1), "#J26");
1350 Assert.AreEqual (1, Array.IndexOf(s1, "is", 1), "#J27");
1351 Assert.AreEqual (4, Array.IndexOf(s1, "test", 1), "#J28");
1355 public void TestIndexOf3() {
1357 bool errorThrown = false;
1359 Array.IndexOf(null, "huh?", 0, 1);
1360 } catch (ArgumentNullException) {
1363 Assert.IsTrue (errorThrown, "#J41");
1366 bool errorThrown = false;
1368 char[,] c = new Char[2,2];
1369 Array.IndexOf(c, "huh?", 0, 1);
1370 } catch (RankException) {
1373 Assert.IsTrue (errorThrown, "#J42");
1376 bool errorThrown = false;
1378 char[] c = new Char[2];
1379 Array.IndexOf(c, "huh?", 3, 1);
1380 } catch (ArgumentOutOfRangeException) {
1383 Assert.IsTrue (errorThrown, "#J43");
1386 bool errorThrown = false;
1388 char[] c = new Char[2];
1389 Array.IndexOf(c, "huh?", 0, 5);
1390 } catch (ArgumentOutOfRangeException) {
1393 Assert.IsTrue (errorThrown, "#J44");
1396 String[] s1 = {"this", "is", "really", "a", "test"};
1397 Assert.AreEqual (-1, Array.IndexOf(s1, null, 1, 3), "#J45");
1398 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing", 1, 3), "#J46");
1399 Assert.AreEqual (-1, Array.IndexOf(s1, "this", 1, 3), "#J47");
1400 Assert.AreEqual (1, Array.IndexOf(s1, "is", 1, 3), "#J48");
1401 Assert.AreEqual (-1, Array.IndexOf(s1, "test", 1, 3), "#J49");
1402 Assert.AreEqual (3, Array.IndexOf(s1, "a", 1, 3), "#J50");
1406 public void TestIndexOf_CustomEqual ()
1408 DataEqual[] test = new DataEqual [] { new DataEqual () };
1409 Assert.AreEqual (0, Array.IndexOf (test, "asdfas", 0));
1411 IList array = (IList)test;
1412 Assert.AreEqual (0, array.IndexOf ("asdfas"));
1416 public void TestLastIndexOf1() {
1418 bool errorThrown = false;
1420 Array.LastIndexOf(null, "huh?");
1421 } catch (ArgumentNullException) {
1424 Assert.IsTrue (errorThrown, "#K01");
1427 bool errorThrown = false;
1429 char[,] c = new Char[2,2];
1430 Array.LastIndexOf(c, "huh?");
1431 } catch (RankException) {
1434 Assert.IsTrue (errorThrown, "#K02");
1437 String[] s1 = {"this", "is", "a", "a", "test"};
1438 Assert.AreEqual (-1, Array.LastIndexOf(s1, null), "#K03");
1439 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing"), "#K04");
1440 Assert.AreEqual (0, Array.LastIndexOf(s1, "this"), "#K05");
1441 Assert.AreEqual (4, Array.LastIndexOf(s1, "test"), "#K06");
1442 Assert.AreEqual (3, Array.LastIndexOf(s1, "a"), "#K07");
1444 Assert.AreEqual (-1, Array.LastIndexOf (new String [0], "foo"));
1448 public void TestLastIndexOf2() {
1450 bool errorThrown = false;
1452 Array.LastIndexOf(null, "huh?", 0);
1453 } catch (ArgumentNullException) {
1456 Assert.IsTrue (errorThrown, "#K21");
1459 bool errorThrown = false;
1461 char[,] c = new Char[2,2];
1462 Array.LastIndexOf(c, "huh?", 0);
1463 } catch (RankException) {
1466 Assert.IsTrue (errorThrown, "#K22");
1469 bool errorThrown = false;
1471 char[] c = new Char[2];
1472 Array.LastIndexOf(c, "huh?", 3);
1473 } catch (ArgumentOutOfRangeException) {
1476 Assert.IsTrue (errorThrown, "#K23");
1479 String[] s1 = {"this", "is", "really", "a", "test"};
1480 Assert.AreEqual (-1, Array.LastIndexOf(s1, null, 3), "#K24");
1481 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing", 3), "#K25");
1482 Assert.AreEqual (-1, Array.LastIndexOf(s1, "test", 3), "#K26");
1483 Assert.AreEqual (3, Array.LastIndexOf(s1, "a", 3), "#K27");
1484 Assert.AreEqual (0, Array.LastIndexOf(s1, "this", 3), "#K28");
1488 public void TestLastIndexOf3() {
1490 bool errorThrown = false;
1492 Array.LastIndexOf(null, "huh?", 0, 1);
1493 } catch (ArgumentNullException) {
1496 Assert.IsTrue (errorThrown, "#K41");
1499 bool errorThrown = false;
1501 char[,] c = new Char[2,2];
1502 Array.LastIndexOf(c, "huh?", 0, 1);
1503 } catch (RankException) {
1506 Assert.IsTrue (errorThrown, "#K42");
1509 bool errorThrown = false;
1511 char[] c = new Char[2];
1512 Array.LastIndexOf(c, "huh?", 3, 1);
1513 } catch (ArgumentOutOfRangeException) {
1516 Assert.IsTrue (errorThrown, "#K43");
1519 bool errorThrown = false;
1521 char[] c = new Char[2];
1522 Array.LastIndexOf(c, "huh?", 0, 5);
1523 } catch (ArgumentOutOfRangeException) {
1526 Assert.IsTrue (errorThrown, "#K44");
1529 String[] s1 = {"this", "is", "really", "a", "test"};
1530 Assert.AreEqual (-1, Array.LastIndexOf(s1, null, 3, 3), "#K45");
1531 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing", 3, 3), "#K46");
1532 Assert.AreEqual (-1, Array.LastIndexOf(s1, "this", 3, 3), "#K47");
1533 Assert.AreEqual (1, Array.LastIndexOf(s1, "is", 3, 3), "#K48");
1534 Assert.AreEqual (-1, Array.LastIndexOf(s1, "test", 3, 3), "#K49");
1535 Assert.AreEqual (3, Array.LastIndexOf(s1, "a", 3, 3), "#K50");
1539 public void TestLastIndexOf4 ()
1541 short [] a = new short [] { 19, 238, 317, 6, 565, 0, -52, 60, -563, 753, 238, 238};
1543 Array.LastIndexOf (a, (object)16, -1);
1544 NUnit.Framework.Assert.Fail ("#1");
1545 } catch (ArgumentOutOfRangeException) { }
1549 Array.LastIndexOf<short> (a, 16, -1);
1550 NUnit.Framework.Assert.Fail ("#2");
1551 } catch (ArgumentOutOfRangeException) { }
1556 public void TestLastIndexOf5 ()
1558 char [] a = new char [] {'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'j', 'i', 'h'};
1563 for (int i = a.Length - 1; i >= 0 ; i--) {
1565 retval = Array.LastIndexOf(a, a [i], i, i + 1);
1569 Assert.IsTrue (!error);
1573 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1574 public void LastIndexOf_StartIndexOverflow ()
1576 // legal - no exception
1577 byte[] array = new byte [16];
1578 Array.LastIndexOf (array, this, Int32.MaxValue, 1);
1582 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1583 public void LastIndexOf_CountOverflow ()
1585 // legal - no exception
1586 byte[] array = new byte [16];
1587 Array.LastIndexOf (array, this, 1, Int32.MaxValue);
1591 public void LastIndexOf_0LengthArray ()
1593 Array array = Array.CreateInstance (typeof (char), 0);
1594 int idx = Array.LastIndexOf (array, (object) null, -1, 0);
1595 Assert.IsTrue (idx == -1, "#01");
1596 idx = Array.LastIndexOf (array, (object) null, -1, 10);
1597 Assert.IsTrue (idx == -1, "#02");
1598 idx = Array.LastIndexOf (array, (object) null, -100, 10);
1599 Assert.IsTrue (idx == -1, "#02");
1601 array = Array.CreateInstance (typeof (char), 1);
1603 Array.LastIndexOf (array, (object) null, -1, 0);
1604 Assert.Fail ("#04");
1605 } catch (ArgumentOutOfRangeException e) {
1608 Array.LastIndexOf (array, (object) null, -1, 10);
1609 Assert.Fail ("#05");
1610 } catch (ArgumentOutOfRangeException e) {
1613 Array.LastIndexOf (array, (object) null, -100, 10);
1614 Assert.Fail ("#06");
1615 } catch (ArgumentOutOfRangeException e) {
1620 public void TestReverse() {
1622 bool errorThrown = false;
1624 Array.Reverse(null);
1625 } catch (ArgumentNullException) {
1628 Assert.IsTrue (errorThrown, "#L01");
1631 bool errorThrown = false;
1633 char[,] c = new Char[2,2];
1635 } catch (RankException) {
1638 Assert.IsTrue (errorThrown, "#L02");
1641 char[] c1 = {'a', 'b', 'c', 'd'};
1643 Assert.AreEqual ('d', c1[0], "#L03");
1644 Assert.AreEqual ('c', c1[1], "#L04");
1645 Assert.AreEqual ('b', c1[2], "#L05");
1646 Assert.AreEqual ('a', c1[3], "#L06");
1649 bool errorThrown = false;
1651 Array.Reverse(null, 0, 0);
1652 } catch (ArgumentNullException) {
1655 Assert.IsTrue (errorThrown, "#L07");
1658 bool errorThrown = false;
1660 char[,] c = new Char[2,2];
1661 Array.Reverse(c, 0, 0);
1662 } catch (RankException) {
1665 Assert.IsTrue (errorThrown, "#L08");
1668 //bool errorThrown = false;
1670 // char[] c = new Char[2];
1671 // Array.Reverse(c, 0, 3);
1672 //} catch (ArgumentOutOfRangeException) {
1673 // errorThrown = true;
1675 //Assert.IsTrue (errorThrown, "#L09");
1678 //bool errorThrown = false;
1680 // char[] c = new Char[2];
1681 // Array.Reverse(c, 3, 0);
1682 //} catch (ArgumentOutOfRangeException) {
1683 // errorThrown = true;
1685 //Assert.IsTrue (errorThrown, "#L10");
1688 char[] c2 = { 'a', 'b', 'c', 'd'};
1689 Array.Reverse(c2, 1, 2);
1690 Assert.AreEqual ('a', c2[0], "#L11");
1691 Assert.AreEqual ('c', c2[1], "#L12");
1692 Assert.AreEqual ('b', c2[2], "#L13");
1693 Assert.AreEqual ('d', c2[3], "#L14");
1697 public void TestSetValue1() {
1699 bool errorThrown = false;
1701 char[,] c = new Char[2,2];
1702 c.SetValue("buh", 1);
1703 } catch (ArgumentException) {
1706 Assert.IsTrue (errorThrown, "#M01");
1709 bool errorThrown = false;
1711 char[] c = {'a', 'b', 'c'};
1712 c.SetValue("buh", -1);
1713 } catch (IndexOutOfRangeException) {
1716 Assert.IsTrue (errorThrown, "#M02");
1719 bool errorThrown = false;
1721 char[] c = {'a', 'b', 'c'};
1722 c.SetValue("buh", 4);
1723 } catch (IndexOutOfRangeException) {
1726 Assert.IsTrue (errorThrown, "#M03");
1729 char[] c1 = {'a', 'b', 'c', 'd'};
1730 char[] c2 = new char[4];
1731 for (int i = 0; i < c1.Length; i++) {
1732 c2.SetValue(c1[i], i);
1734 for (int i = 0; i < c1.Length; i++) {
1735 Assert.AreEqual (c1[i], c2[i], "#M04(" + i + ")");
1738 int[] c3 = { 1, 2, 3 };
1739 long[] c4 = new long [3];
1741 for (int i = 0; i < c3.Length; i++)
1742 c4.SetValue (c3 [i], i);
1746 } catch (Exception e) {
1747 Assert.Fail ("c3.CopyTo(): e=" + e);
1749 for (int i = 0; i < c3.Length; i++)
1750 Assert.IsTrue (c3[i] == c4[i], "#M05(" + i + ")");
1752 Object[] c5 = new Object [3];
1753 long[] c6 = new long [3];
1757 } catch (Exception e) {
1758 Assert.Fail ("c4.CopyTo(): e=" + e);
1763 } catch (Exception e) {
1764 Assert.Fail ("c5.CopyTo(): e=" + e);
1766 // for (int i = 0; i < c5.Length; i++)
1767 // Assert.IsTrue (c5[i] == c6[i], "#M06(" + i + ")");
1771 public void TestSetValue2() {
1773 bool errorThrown = false;
1775 char[] c = new Char[2];
1776 c.SetValue("buh", 1,1);
1777 } catch (ArgumentException) {
1780 Assert.IsTrue (errorThrown, "#M21");
1783 bool errorThrown = false;
1785 char[,] c = new Char[2,2];
1786 c.SetValue("buh", -1, 1);
1787 } catch (IndexOutOfRangeException) {
1790 Assert.IsTrue (errorThrown, "#M22");
1793 bool errorThrown = false;
1795 char[,] c = new Char[2,2];
1796 c.SetValue("buh", 4,1);
1797 } catch (IndexOutOfRangeException) {
1800 Assert.IsTrue (errorThrown, "#M23");
1803 char[,] c1 = new Char[4,6];
1804 char[,] c2 = new Char[4,6];
1805 for (int i = 0; i < 24; i++) {
1808 c1[first,second] = (char)(((int)'a')+i);
1809 c2.SetValue(c1[first,second], first, second);
1811 for (int i = 0; i < c1.GetLength(0); i++) {
1812 for (int j = 0; j < c1.GetLength(1); j++) {
1813 Assert.AreEqual (c1[i, j], c2[i, j], "#M24(" + i + "," + j + ")");
1819 public void TestSetValue3() {
1821 bool errorThrown = false;
1823 char[] c = new Char[2];
1824 c.SetValue("buh", 1,1,1);
1825 } catch (ArgumentException) {
1828 Assert.IsTrue (errorThrown, "#M41");
1831 bool errorThrown = false;
1833 char[,,] c = new Char[2,2,2];
1834 c.SetValue("buh", -1, 1, 1);
1835 } catch (IndexOutOfRangeException) {
1838 Assert.IsTrue (errorThrown, "#M42");
1841 bool errorThrown = false;
1843 char[,,] c = new Char[2,2,2];
1844 c.SetValue("buh", 4,1,1);
1845 } catch (IndexOutOfRangeException) {
1848 Assert.IsTrue (errorThrown, "#M43");
1851 char[,,] c1 = new Char[4,2,3];
1852 char[,,] c2 = new Char[4,2,3];
1853 for (int i = 0; i < 24; i++) {
1855 int remains = i % 6;
1856 int second = remains / 3;
1857 int third = remains % 3;
1858 c1[first,second, third] = (char)(((int)'a')+i);
1859 c2.SetValue(c1[first, second, third], first, second, third);
1861 for (int i = 0; i < c1.GetLength(0); i++) {
1862 for (int j = 0; j < c1.GetLength(1); j++) {
1863 for (int k = 0; k < c1.GetLength(2); k++) {
1864 Assert.AreEqual (c1[i, j, k], c2[i, j, k], "#M44(" + i + "," + j + " )");
1872 [ExpectedException (typeof (ArgumentNullException))]
1874 [ExpectedException (typeof (NullReferenceException))]
1876 public void TestSetValueLongArray ()
1878 char[] c = new Char[2];
1879 c.SetValue("buh", (long [])null);
1883 public void TestSetValueN() {
1885 bool errorThrown = false;
1887 char[] c = new Char[2];
1888 c.SetValue("buh", (int [])null);
1889 } catch (ArgumentNullException) {
1892 Assert.IsTrue (errorThrown, "#M61a");
1895 bool errorThrown = false;
1897 char[] c = new Char[2];
1898 int[] coords = {1, 1};
1899 c.SetValue("buh", coords);
1900 } catch (ArgumentException) {
1903 Assert.IsTrue (errorThrown, "#M62");
1906 bool errorThrown = false;
1908 char[,] c = new Char[2,2];
1909 int[] coords = {-1, 1};
1910 c.SetValue("buh", coords);
1911 } catch (IndexOutOfRangeException) {
1914 Assert.IsTrue (errorThrown, "#M63");
1917 bool errorThrown = false;
1919 char[,] c = new Char[2,2];
1920 int[] coords = {4, 1};
1921 c.SetValue("buh", coords);
1922 } catch (IndexOutOfRangeException) {
1925 Assert.IsTrue (errorThrown, "#M64");
1928 char[,] c1 = new Char[4,6];
1929 char[,] c2 = new Char[4,6];
1930 for (int i = 0; i < 24; i++) {
1933 c1[first,second] = (char)(((int)'a')+i);
1934 int[] coords = {first, second};
1935 c2.SetValue(c1[first,second], coords);
1937 for (int i = 0; i < c1.GetLength(0); i++) {
1938 for (int j = 0; j < c1.GetLength(1); j++) {
1939 Assert.AreEqual (c1[i, j], c2[i, j], "#M65(" + i + "," + j + ")");
1945 public void TestSetValue4() {
1947 int[] c1 = { 1, 2, 3 };
1948 long[] c2 = new long [3];
1950 for (int i = 0; i < c1.Length; i++)
1951 c2.SetValue (c1 [i], i);
1953 for (int i = 0; i < c1.Length; i++) {
1954 Assert.IsTrue (c1[i] == c2[i], "#M81(" + i + ")");
1955 Assert.AreEqual (typeof (long), c2[i].GetType (), "#M82(" + i + ")");
1959 long[] c1 = { 1, 2, 3 };
1960 int[] c2 = new int [3];
1961 bool errorThrown = false;
1963 c2.SetValue (c1 [0], 0);
1964 } catch (ArgumentException) {
1967 Assert.IsTrue (errorThrown, "#M83");
1970 int[] c1 = { 1, 2, 3 };
1971 Object[] c2 = new Object [3];
1973 for (int i = 0; i < c1.Length; i++)
1974 c2.SetValue (c1 [i], i);
1976 for (int i = 0; i < c1.Length; i++)
1977 Assert.AreEqual (c1[i], Convert.ToInt32 (c2[i]), "#M84(" + i + ")");
1980 Object[] c1 = new Object [3];
1981 Object[] c2 = new Object [3];
1982 c1[0] = new Object ();
1984 for (int i = 0; i < c1.Length; i++)
1985 c2.SetValue (c1 [i], i);
1987 for (int i = 0; i < c1.Length; i++)
1988 Assert.AreEqual (c1[i], c2[i], "#M85(" + i + ")");
1991 Object[] c1 = new Object [3];
1992 string[] c2 = new String [3];
1993 string test = "hello";
1996 c2.SetValue (c1 [0], 0);
1997 Assert.AreEqual (c1[0], c2[0], "#M86");
1998 Assert.AreEqual ("hello", c2[0], "#M87");
2001 char[] c1 = { 'a', 'b', 'c' };
2002 string[] c2 = new string [3];
2004 c2.SetValue (c1 [0], 0);
2005 Assert.Fail ("#M88");
2006 } catch (InvalidCastException) {}
2009 Single[] c1 = { 1.2F, 2.3F, 3.4F, 4.5F };
2010 long[] c2 = new long [3];
2012 c2.SetValue (c1 [0], 0);
2013 Assert.Fail ("#M89");
2014 } catch (ArgumentException) {}
2042 UInt32 v11 = 235354;
2043 UInt64 v12 = 234552;
2045 Object[] va1 = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12 };
2046 Object[] va2 = { "true", "1", "a", "-1.2", "-32", "-234", "-34523", "-1",
2047 "-4.8F", "24234", "235354", "234552" };
2049 Object[][] vt = { va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1 };
2052 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2053 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
2054 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
2055 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
2056 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1,
2057 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1,
2058 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,
2059 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1,
2060 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1,
2061 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
2062 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0,
2063 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0
2068 for (int i = 0; i < types.Length; i++) {
2069 for (int j = 0; j < types.Length; j++) {
2070 Array array = Array.CreateInstance (types [j], 2);
2072 Object value = vt[j][i];
2074 bool errorThrown = false;
2076 array.SetValue (value, 0);
2077 } catch (ArgumentException) {
2081 int ex_index = (i * types.Length) + j;
2083 Assert.AreEqual (errorThrown, arg_ex [ex_index] == 1, "#M90(" + types [i] + "," + types [j] + ")");
2087 for (int i = 0; i < types.Length; i++) {
2088 String[] array = new String [2];
2090 Object value = va1 [i];
2092 bool errorThrown = false;
2094 array.SetValue (value, 0);
2095 } catch (InvalidCastException) {
2099 Assert.IsTrue (errorThrown, "#M91(" + types [i] + ")");
2102 for (int i = 0; i < types.Length; i++) {
2103 Array array = Array.CreateInstance (types [i], 2);
2105 Object value = va2 [i];
2107 bool errorThrown = false;
2109 array.SetValue (value, 0);
2110 } catch (InvalidCastException) {
2114 Assert.IsTrue (errorThrown, "#M92(" + types [i] + ")");
2117 for (int i = 0; i < types.Length; i++) {
2118 Array array = Array.CreateInstance (types [i], 2);
2120 Object value = null;
2122 bool errorThrown = false;
2124 array.SetValue (value, 0);
2125 } catch (InvalidCastException) {
2129 Assert.IsTrue (!errorThrown, "#M93(" + types [i] + ")");
2134 for (int i = 0; i < types.Length; i++) {
2135 for (int j = 0; j < types.Length; j++) {
2136 Array source = Array.CreateInstance (types [i], 2);
2137 Array array = Array.CreateInstance (types [j], 2);
2139 source.SetValue (vt[j][i], 0);
2140 source.SetValue (vt[j][i], 1);
2142 bool errorThrown = false;
2144 Array.Copy (source, array, 2);
2145 } catch (ArrayTypeMismatchException) {
2149 int ex_index = (i * types.Length) + j;
2151 Assert.AreEqual (errorThrown, arg_ex [ex_index] == 1, "#M94(" + types [i] + "," + types [j] + ")");
2155 for (int i = 0; i < types.Length; i++) {
2156 Array source = Array.CreateInstance (types [i], 2);
2157 String[] array = new String [2];
2159 source.SetValue (va1 [i], 0);
2160 source.SetValue (va1 [i], 1);
2162 bool errorThrown = false;
2164 Array.Copy (source, array, 2);
2165 } catch (ArrayTypeMismatchException) {
2169 Assert.IsTrue (errorThrown, "#M95(" + types [i] + ")");
2172 for (int i = 0; i < types.Length; i++) {
2173 String[] source = new String [2];
2174 Array array = Array.CreateInstance (types [i], 2);
2176 source.SetValue (va2 [i], 0);
2177 source.SetValue (va2 [i], 1);
2179 bool errorThrown = false;
2181 Array.Copy (source, array, 2);
2182 } catch (ArrayTypeMismatchException) {
2186 Assert.IsTrue (errorThrown, "#M96(" + types [i] + ")");
2192 public void TestSort() {
2194 bool errorThrown = false;
2197 } catch (ArgumentNullException) {
2200 Assert.IsTrue (errorThrown, "#N01");
2203 bool errorThrown = false;
2205 Array.Sort(null, 0, 1);
2206 } catch (ArgumentNullException) {
2209 Assert.IsTrue (errorThrown, "#N02");
2212 bool errorThrown = false;
2214 char[] c1 = new Char[2];
2215 Array.Sort(null, c1);
2216 } catch (ArgumentNullException) {
2219 Assert.IsTrue (errorThrown, "#N03");
2222 bool errorThrown = false;
2224 char[] c1 = new Char[2];
2225 Array.Sort(null, c1, 0, 1);
2226 } catch (ArgumentNullException) {
2229 Assert.IsTrue (errorThrown, "#N04");
2233 char[] arr = {'d', 'b', 'f', 'e', 'a', 'c'};
2236 Array.Sort (null, 0, 1);
2237 Assert.Fail ("#N" + tc.ToString ());
2239 catch (ArgumentException) {}
2240 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2244 Array.Sort (arr, -1, 3);
2245 Assert.Fail ("#N" + tc.ToString ());
2247 catch (ArgumentException) {}
2248 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2252 Array.Sort (arr, 1, -3);
2253 Assert.Fail ("#N" + tc.ToString ());
2255 catch (ArgumentException) {}
2256 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2260 Array.Sort (arr, arr.Length, arr.Length + 2);
2261 Assert.Fail ("#N" + tc.ToString ());
2263 catch (ArgumentException) {}
2264 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2267 // note: null second array => just sort first array
2268 char[] starter = {'d', 'b', 'f', 'e', 'a', 'c'};
2269 int[] starter1 = {1,2,3,4,5,6};
2271 char[] c1 = (char[])starter.Clone();
2273 Assert.AreEqual ('a', c1[0], "#N21");
2274 Assert.AreEqual ('b', c1[1], "#N22");
2275 Assert.AreEqual ('c', c1[2], "#N23");
2276 Assert.AreEqual ('d', c1[3], "#N24");
2277 Assert.AreEqual ('e', c1[4], "#N25");
2278 Assert.AreEqual ('f', c1[5], "#N26");
2281 char[] c1 = (char[])starter.Clone();
2282 int[] i1 = (int[])starter1.Clone();
2284 Assert.AreEqual ('a', c1[0], "#N41");
2285 Assert.AreEqual ('b', c1[1], "#N42");
2286 Assert.AreEqual ('c', c1[2], "#N43");
2287 Assert.AreEqual ('d', c1[3], "#N44");
2288 Assert.AreEqual ('e', c1[4], "#N45");
2289 Assert.AreEqual ('f', c1[5], "#N46");
2290 Assert.AreEqual (5, i1[0], "#N47");
2291 Assert.AreEqual (2, i1[1], "#N48");
2292 Assert.AreEqual (6, i1[2], "#N49");
2293 Assert.AreEqual (1, i1[3], "#N50");
2294 Assert.AreEqual (4, i1[4], "#N51");
2295 Assert.AreEqual (3, i1[5], "#N52");
2298 char[] c1 = (char[])starter.Clone();
2299 Array.Sort(c1, 1, 4);
2300 Assert.AreEqual ('d', c1[0], "#N61");
2301 Assert.AreEqual ('a', c1[1], "#N62");
2302 Assert.AreEqual ('b', c1[2], "#N63");
2303 Assert.AreEqual ('e', c1[3], "#N64");
2304 Assert.AreEqual ('f', c1[4], "#N65");
2305 Assert.AreEqual ('c', c1[5], "#N66");
2308 char[] c1 = (char[])starter.Clone();
2309 int[] i1 = (int[])starter1.Clone();
2310 Array.Sort(c1, i1, 1, 4);
2311 Assert.AreEqual ('d', c1[0], "#N81");
2312 Assert.AreEqual ('a', c1[1], "#N82");
2313 Assert.AreEqual ('b', c1[2], "#N83");
2314 Assert.AreEqual ('e', c1[3], "#N84");
2315 Assert.AreEqual ('f', c1[4], "#N85");
2316 Assert.AreEqual ('c', c1[5], "#N86");
2317 Assert.AreEqual (1, i1[0], "#N87");
2318 Assert.AreEqual (5, i1[1], "#N88");
2319 Assert.AreEqual (2, i1[2], "#N89");
2320 Assert.AreEqual (4, i1[3], "#N90");
2321 Assert.AreEqual (3, i1[4], "#N91");
2322 Assert.AreEqual (6, i1[5], "#N92");
2327 double[] a = new double[115];
2328 int[] b = new int[256];
2329 Array.Sort<double, int> (a, b, 0, 115);
2334 public void SortNonGenericDoubleItems () {
2335 double[] doubleValues = new double[11];
2337 doubleValues[0] = 0.221788066253601;
2338 doubleValues[1] = 0.497278285809481;
2339 doubleValues[2] = 0.100565033883643;
2340 doubleValues[3] = 0.0433309347749905;
2341 doubleValues[4] = 0.00476726438463812;
2342 doubleValues[5] = 0.1354609735456;
2343 doubleValues[6] = 0.57690356588135;
2344 doubleValues[7] = 0.466239434334826;
2345 doubleValues[8] = 0.409741461978934;
2346 doubleValues[9] = 0.0112412763949565;
2347 doubleValues[10] = 0.668704347674307;
2349 int[] indices = new int[11];
2362 Array.Sort ((Array)doubleValues, (Array)indices);
2363 Assert.AreEqual (4, indices [0]);
2367 public void TestInitializeEmpty()
2379 Assert.IsTrue (!catched, "#TI01");
2383 public void TestInitializeInt()
2388 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2390 Assert.AreEqual (a[i], b[i], "#TI02 " + i);
2395 public void TestInitializeDouble()
2397 double[] a = {1.0,2.0,0.0};
2399 double[] b = {1.0,2.0,0.0};
2400 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2402 Assert.AreEqual (a[i], b[i], "#TI03 " + i);
2407 public void TestInitializeFloat()
2409 float[] a = {1.0F,2.0F,0.0F};
2411 float[] b = {1.0F,2.0F,0.0F};
2412 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2414 Assert.AreEqual (a[i], b[i], "#TI04 " + i);
2419 public void TestInitializeChar()
2421 char[] a = {'1','.','0','F','2','.','0','F'};
2423 char[] b = {'1','.','0','F','2','.','0','F'};
2424 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2426 Assert.AreEqual (a[i], b[i], "#TI05 " + i);
2431 public void TestInitializeString()
2433 string[] a = {"hola","adios","menos","mas"};
2435 string[] b = {"hola","adios","menos","mas"};
2436 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2438 Assert.AreEqual (a[i], b[i], "#TI06 " + i);
2443 public void TestInitializeEnum()
2445 enua[] a = {enua.hola,enua.adios,enua.menos,enua.mas};
2447 enua[] b = {enua.hola,enua.adios,enua.menos,enua.mas};
2448 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2450 Assert.AreEqual (a[i], b[i], "#TI07 " + i);
2455 public void TestInitializeIntNI()
2457 int[] a = new int[20];
2461 Assert.AreEqual (b, 0, "#TI08");
2466 public void TestInitializeCharNI()
2468 char[] a = new char[20];
2470 foreach(char b in a)
2472 Assert.AreEqual (b, 0, "#TI09");
2477 public void TestInitializeDoubleNI()
2479 double[] a = new double[20];
2481 foreach(double b in a)
2483 Assert.AreEqual (b, 0.0, "#TI09");
2488 public void TestInitializeStringNI()
2490 string[] a = new string[20];
2492 foreach(string b in a)
2494 Assert.AreEqual (b, null, "#TI10");
2499 public void TestInitializeObjectNI()
2501 object[] a = new object[20];
2503 foreach(object b in a)
2505 Assert.AreEqual (b, null, "#TI11");
2510 public void TestInitializeAClassNI()
2512 AClass[] a = new AClass[20];
2514 foreach(AClass b in a)
2516 Assert.AreEqual (b, null, "#TI12");
2522 public void TestInitializeAStructNI()
2524 AStruct[] a = new AStruct[20];
2526 foreach(AStruct b in a)
2528 Assert.AreEqual (b, new AStruct(), "#TI14");
2533 public void TestInitializeAStruct()
2535 AStruct[] a = new AStruct[3];
2539 AStruct[] b = new AStruct[3];
2542 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2544 Assert.AreEqual (a[i], b[i], "#TI15 " + i);
2549 public void TestInitializeDateTimeNI()
2551 DateTime[] a = new DateTime[20];
2553 foreach(DateTime b in a)
2555 Assert.AreEqual (b, new DateTime(), "#TI16");
2560 [ExpectedException (typeof (ArgumentNullException))]
2561 public void MoreSort1 ()
2563 Array.Sort (null, 0, 1);
2567 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2568 public void MoreSort2 ()
2570 Array.Sort (arrsort, -1, 3);
2574 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2575 public void MoreSort3 ()
2577 Array.Sort (arrsort, 1, -3);
2581 [ExpectedException (typeof (ArgumentException))]
2582 public void MoreSort4 ()
2584 Array.Sort (arrsort, arrsort.Length, arrsort.Length + 2);
2588 [ExpectedException (typeof (RankException))]
2589 public void MoreSort5 ()
2591 char [,] arr = new char [,] {{'a'}, {'b'}};
2592 Array.Sort (arr, 0, 1);
2596 public void MoreSort6 ()
2598 Array.Sort (arrsort, 0, 0);
2602 [ExpectedException (typeof (ArgumentException))]
2603 public void MoreSort7 ()
2605 Array.Sort (arrsort, arrsort.Length - 1, 2);
2609 [ExpectedException (typeof (ArgumentException))]
2610 public void MoreSort8 ()
2612 Array.Sort (arrsort, 0, arrsort.Length + 1);
2616 public void MoreSort9 ()
2618 Array.Sort (arrsort, null, 0, arrsort.Length, null);
2622 [ExpectedException (typeof (InvalidOperationException))]
2623 public void MoreSort10 ()
2625 object [] array = {true, 'k', SByte.MinValue, Byte.MinValue, (short) 2, 634, (long) 436, (float) 1.1, 1.23, "Hello World"};
2626 Array.Sort (array, (IComparer) null);
2629 [Test] // bug #81941
2632 double [] a = new double [2] { 0.9, 0.3 };
2633 uint [] b = new uint [2] { 4, 7 };
2635 Assert.AreEqual (0.3, a [0], "#1");
2636 Assert.AreEqual (0.9, a [1], "#2");
2637 Assert.AreEqual (7, b [0], "#3");
2638 Assert.AreEqual (4, b [1], "#4");
2642 public void ClearJaggedArray ()
2644 byte[][] matrix = new byte [8][];
2645 for (int i=0; i < 8; i++) {
2646 matrix [i] = new byte [8];
2647 for (int j=0; j < 8; j++) {
2651 Array.Clear (matrix, 0, 8);
2652 for (int i=0; i < 8; i++) {
2653 Assert.IsNull (matrix [i], i.ToString ());
2658 public void ClearMultidimentionalArray ()
2660 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2661 Array.Clear (matrix, 0, 2);
2662 Assert.AreEqual (0, matrix [0, 0], "0,0");
2663 Assert.AreEqual (0, matrix [0, 1], "0,1");
2664 Assert.AreEqual (2, matrix [1, 0], "1,0");
2665 Assert.AreEqual (2, matrix [1, 1], "1,1");
2669 [ExpectedException (typeof (IndexOutOfRangeException))]
2670 public void ClearOutsideMultidimentionalArray ()
2672 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2673 Array.Clear (matrix, 0, 5);
2677 [ExpectedException (typeof (IndexOutOfRangeException))]
2678 public void Clear_IndexOverflow ()
2680 byte[] array = new byte [16];
2681 Array.Clear (array, 4, Int32.MaxValue);
2685 [ExpectedException (typeof (IndexOutOfRangeException))]
2686 public void Clear_LengthOverflow ()
2688 byte[] array = new byte [16];
2689 Array.Clear (array, Int32.MaxValue, 4);
2693 [ExpectedException (typeof (ArgumentException))]
2694 public void Copy_SourceIndexOverflow ()
2696 byte[] array = new byte [16];
2697 Array.Copy (array, Int32.MaxValue, array, 8, 8);
2701 [ExpectedException (typeof (ArgumentException))]
2702 public void Copy_DestinationIndexOverflow ()
2704 byte[] array = new byte [16];
2705 Array.Copy (array, 8, array, Int32.MaxValue, 8);
2709 [ExpectedException (typeof (ArgumentException))]
2710 public void Copy_LengthOverflow ()
2712 byte[] array = new byte [16];
2713 Array.Copy (array, 8, array, 8, Int32.MaxValue);
2717 [ExpectedException (typeof (ArgumentException))]
2718 public void Reverse_IndexOverflow ()
2720 byte[] array = new byte [16];
2721 Array.Reverse (array, Int32.MaxValue, 8);
2725 [ExpectedException (typeof (ArgumentException))]
2726 public void Reverse_LengthOverflow ()
2728 byte[] array = new byte [16];
2729 Array.Reverse (array, 8, Int32.MaxValue);
2732 public struct CharX : IComparable {
2735 public CharX (char c)
2740 public int CompareTo (object obj)
2743 return c.CompareTo (((CharX) obj).c);
2745 return c.CompareTo (obj);
2750 public void BinarySearch_ArgPassingOrder ()
2753 // This tests that arguments are passed to the comprer in the correct
2754 // order. The IComparable of the *array* elements must get called, not
2755 // that of the search object.
2757 CharX [] x = { new CharX ('a'), new CharX ('b'), new CharX ('c') };
2758 Assert.AreEqual (1, Array.BinarySearch (x, 'b'));
2761 class Comparer: IComparer {
2763 private bool called = false;
2765 public bool Called {
2767 bool result = called;
2773 public int Compare (object x, object y)
2781 public void BinarySearch1_EmptyList ()
2783 int[] array = new int[0];
2784 Assert.AreEqual (- 1, Array.BinarySearch (array, 0), "BinarySearch");
2788 public void BinarySearch2_EmptyList ()
2790 int[] array = new int[0];
2791 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, 0), "BinarySearch");
2795 public void BinarySearch3_EmptyList ()
2797 Comparer comparer = new Comparer ();
2798 int[] array = new int[0];
2799 Assert.AreEqual (-1, Array.BinarySearch (array, 0, comparer), "BinarySearch");
2800 // bug 77030 - the comparer isn't called for an empty array/list
2801 Assert.IsTrue (!comparer.Called, "Called");
2805 public void BinarySearch4_EmptyList ()
2807 Comparer comparer = new Comparer ();
2808 int[] array = new int[0];
2809 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, comparer), "BinarySearch");
2810 // bug 77030 - the comparer isn't called for an empty array/list
2811 Assert.IsTrue (!comparer.Called, "Called");
2816 [ExpectedException (typeof (ArgumentNullException))]
2817 public void AsReadOnly_NullArray ()
2819 Array.AsReadOnly <int> (null);
2823 public void ReadOnly_Count ()
2825 Assert.AreEqual (10, Array.AsReadOnly (new int [10]).Count);
2829 public void ReadOnly_Contains ()
2831 int[] arr = new int [2];
2834 IList<int> a = Array.AsReadOnly (arr);
2836 Assert.IsTrue (a.Contains (3));
2837 Assert.IsTrue (!a.Contains (6));
2841 public void ReadOnly_IndexOf ()
2843 int[] arr = new int [2];
2846 IList<int> a = Array.AsReadOnly (arr);
2848 Assert.AreEqual (0, a.IndexOf (3));
2849 Assert.AreEqual (1, a.IndexOf (5));
2850 Assert.AreEqual (-1, a.IndexOf (6));
2854 public void ReadOnly_Indexer ()
2856 int[] arr = new int [2];
2859 IList<int> a = Array.AsReadOnly (arr);
2861 Assert.AreEqual (3, a [0]);
2862 Assert.AreEqual (5, a [1]);
2864 /* Check that modifications to the original array are visible */
2866 Assert.AreEqual (6, a [0]);
2870 public void ReadOnly_Enumerator ()
2872 int[] arr = new int [10];
2874 for (int i = 0; i < 10; ++i)
2878 foreach (int i in Array.AsReadOnly (arr))
2881 Assert.AreEqual (45, sum);
2885 public void ReadOnly_CopyTo ()
2887 int[] arr = new int [2];
2890 IList<int> a = Array.AsReadOnly (arr);
2892 int[] arr2 = new int [3];
2895 Assert.AreEqual (0, arr2 [0]);
2896 Assert.AreEqual (3, arr2 [1]);
2897 Assert.AreEqual (5, arr2 [2]);
2901 public void Resize ()
2903 int [] arr = new int [] { 1, 3, 5 };
2904 Array.Resize <int> (ref arr, 3);
2905 Assert.AreEqual (3, arr.Length, "#A1");
2906 Assert.AreEqual (1, arr [0], "#A2");
2907 Assert.AreEqual (3, arr [1], "#A3");
2908 Assert.AreEqual (5, arr [2], "#A4");
2910 Array.Resize <int> (ref arr, 2);
2911 Assert.AreEqual (2, arr.Length, "#B1");
2912 Assert.AreEqual (1, arr [0], "#B2");
2913 Assert.AreEqual (3, arr [1], "#B3");
2915 Array.Resize <int> (ref arr, 4);
2916 Assert.AreEqual (4, arr.Length, "#C1");
2917 Assert.AreEqual (1, arr [0], "#C2");
2918 Assert.AreEqual (3, arr [1], "#C3");
2919 Assert.AreEqual (0, arr [2], "#C4");
2920 Assert.AreEqual (0, arr [3], "#C5");
2924 public void Resize_null ()
2927 Array.Resize (ref arr, 10);
2928 Assert.AreEqual (arr.Length, 10);
2932 public void Test_ContainsAndIndexOf_EquatableItem ()
2934 EquatableClass[] list = new EquatableClass[] {new EquatableClass (0), new EquatableClass (1), new EquatableClass (0)};
2936 Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, list[0]), "#0");
2937 Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, new EquatableClass (0)), "#1");
2938 Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, list[0]), "#2");
2939 Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, new EquatableClass (0)), "#3");
2942 public class EquatableClass : IEquatable<EquatableClass>
2945 public EquatableClass (int x)
2950 public bool Equals (EquatableClass other)
2952 return this._x == other._x;
2957 public void AsIList ()
2959 IList<int> arr = new int [10];
2961 Assert.AreEqual (5, arr [0]);
2963 IList<FooStruct> arr2 = new FooStruct [10];
2964 FooStruct s = new FooStruct ();
2969 Assert.AreEqual (11, s.i);
2970 Assert.AreEqual (22, s.j);
2972 IList<string> arr3 = new string [10];
2974 Assert.AreEqual ("ABC", arr3 [5]);
2981 #if !TARGET_JVM // BugBUG: T[] is not yet ICollection<T> under TARGET_JVM
2984 public void ICollectionNull ()
2986 ICollection<object> test;
2988 test = new List<object>();
2989 Assert.AreEqual (test.Contains (null), false, "list<o>");
2991 test = new object[] {};
2992 Assert.AreEqual (test.Contains (null), false, "empty array");
2994 test = new object[] {null};
2995 Assert.AreEqual (test.Contains (null), true, "array with null");
2997 test = new List<object>(test);
2998 Assert.AreEqual (test.Contains (null), true, "List<object> with test");
3000 test = new object[] {new object()};
3001 Assert.AreEqual (test.Contains (null), false, "array with object");
3003 test = new List<object>(test);
3004 Assert.AreEqual (test.Contains (null), false, "array with test");
3006 #endif // TARGET_JVM
3011 enum ByteEnum : byte {}
3012 enum IntEnum : int {}
3015 public void TestByteEnumArrayToByteArray ()
3017 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
3018 byte[] b = new byte[a.Length];
3023 public void TestByteEnumArrayToIntArray ()
3025 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
3026 int[] b = new int[a.Length];
3031 [ExpectedException (typeof (ArrayTypeMismatchException))]
3032 public void TestIntEnumArrayToByteArray ()
3034 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
3035 byte[] b = new byte[a.Length];
3040 public void TestIntEnumArrayToIntArray ()
3042 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
3043 int[] b = new int[a.Length];
3050 [Test] // bug #322248
3051 public void IEnumerator_Reset ()
3053 int[] array = new int[] { 1, 2, 3};
3054 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3055 Assert.IsTrue (e.MoveNext (), "#A1");
3056 Assert.AreEqual (1, e.Current, "#A2");
3057 Assert.IsTrue (e.MoveNext (), "#A3");
3058 Assert.AreEqual (2, e.Current, "#A4");
3062 Assert.IsTrue (e.MoveNext (), "#C1");
3063 Assert.AreEqual (1, e.Current, "#C2");
3067 public void IEnumerator_Current_Finished ()
3069 int[] array = new int[] { 1, 2, 3 };
3070 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3071 Assert.IsTrue (e.MoveNext (), "#A1");
3072 Assert.AreEqual (1, e.Current, "#A2");
3073 Assert.IsTrue (e.MoveNext (), "#A3");
3074 Assert.AreEqual (2, e.Current, "#A4");
3075 Assert.IsTrue (e.MoveNext (), "#A5");
3076 Assert.AreEqual (3, e.Current, "#A6");
3077 Assert.IsTrue (!e.MoveNext (), "#A6");
3080 Assert.Fail ("#B1:" + e.Current);
3081 } catch (InvalidOperationException ex) {
3082 // Enumeration already finished
3083 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3084 Assert.IsNull (ex.InnerException, "#B3");
3085 Assert.IsNotNull (ex.Message, "#B4");
3090 public void IEnumerator_Current_NotStarted ()
3092 int[] array = new int[] { 1, 2, 3 };
3093 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3096 Assert.Fail ("#A1:" + e.Current);
3097 } catch (InvalidOperationException ex) {
3098 // Enumeration has not started. Call MoveNext
3099 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
3100 Assert.IsNull (ex.InnerException, "#A3");
3101 Assert.IsNotNull (ex.Message, "#A4");
3106 public void IEnumerator_Current_Reset ()
3108 int[] array = new int[] { 1, 2, 3 };
3109 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3114 Assert.Fail ("#B1:" + e.Current);
3115 } catch (InvalidOperationException ex) {
3116 // Enumeration has not started. Call MoveNext
3117 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3118 Assert.IsNull (ex.InnerException, "#B3");
3119 Assert.IsNotNull (ex.Message, "#B4");
3123 public void ICollection_IsReadOnly() {
3124 ICollection<string> arr = new string [10];
3126 Assert.IsTrue (arr.IsReadOnly);
3131 [ExpectedException (typeof (NotSupportedException))]
3132 public void ArrayCreateInstanceOfVoid ()
3134 Array.CreateInstance (typeof (void), 42);
3140 [ExpectedException (typeof (NotSupportedException))]
3141 public void ArrayCreateInstanceOfOpenGenericType ()
3143 Array.CreateInstance (typeof (Foo<>), 42);
3147 [ExpectedException (typeof (IndexOutOfRangeException))]
3148 public void ClearNegativeLength ()
3150 Array.Clear (new int [] { 1, 2 }, 0, -1);
3154 [ExpectedException (typeof (ArgumentException))]
3155 public void MultiDimension_IList_setItem ()
3157 IList array = new int [1, 1];
3162 [ExpectedException (typeof (ArgumentException))]
3163 public void MultiDimension_IList_getItem ()
3165 IList array = new int [1, 1];
3166 int a = (int) array [0];
3170 public void SetValue_Nullable () {
3171 Array array = Array.CreateInstance (typeof (int?), 7);
3175 array.SetValue (o, 0);
3176 Assert.AreEqual (42, array.GetValue (0));
3178 array.SetValue (null, 0);
3179 Assert.AreEqual (null, array.GetValue (0));
3183 public void SortNullsWithGenericVersion ()
3185 string[] s1 = new string[6]{
3193 string[] s2 = new string[]{null,
3200 Array.Sort<string> (s1);
3201 for (int i = 0; i < 6; i++) {
3202 Assert.AreEqual (s1[i], s2[i], "At:" + i);
3207 // This is a test case for the case that was broken by the code contributed
3210 // This tests the fix for: #622101
3213 public void SortActuallyWorks ()
3215 string[] data = new string[9]{"Foo", "Bar", "Dingus", null, "Dingu4", "123", "Iam", null, "NotNull"};
3216 IComparer comparer = new NullAtEndComparer ();
3217 Array.Sort (data, comparer);
3219 Assert.AreEqual (data [7], null);
3220 Assert.AreNotEqual (data [0], null);
3223 class NullAtEndComparer : IComparer {
3224 public int Compare(object x, object y)
3226 if (x == null) return 1;
3227 if (y == null) return -1;
3228 return ((string)x).CompareTo((string)y);
3234 [ExpectedException (typeof (ArgumentException))]
3235 public void CompareToWithJaggedArray () {
3236 IStructuralComparable a = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }};
3237 IStructuralComparable b = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }};
3238 a.CompareTo (b, Comparer<object>.Default);
3242 [ExpectedException (typeof (ArgumentException))]
3243 public void CompareToWithArrayOfTheWrongKind () {
3244 IStructuralComparable a = new int[] { 1, 2 };
3245 IStructuralComparable b = new double[] { 1, 2 };
3246 a.CompareTo (b, Comparer<object>.Default);
3250 [ExpectedException (typeof (ArgumentException))]
3251 public void CompareToWithNonArrayType () {
3252 IStructuralComparable a = new int[] { 1, 2 };
3253 a.CompareTo (99, Comparer<object>.Default);
3257 [ExpectedException (typeof (ArgumentException))]
3258 public void CompareToWithNonArrayOfDifferentSize () {
3259 IStructuralComparable a = new int[] { 1, 2 };
3260 IStructuralComparable b = new int[] { 1, 2, 3 };
3261 a.CompareTo (b, Comparer<object>.Default);
3265 [ExpectedException (typeof (ArgumentException))]
3266 public void CompareToWithMultiDimArray1 () {
3267 IStructuralComparable a = new int [2,2] { {10, 10 }, { 10, 10 } };
3268 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3269 a.CompareTo (b, Comparer<object>.Default);
3273 [ExpectedException (typeof (ArgumentException))]
3274 public void CompareToWithMultiDimArray2 () {
3275 IStructuralComparable a = new int [2] { 10, 10 };
3276 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3277 a.CompareTo (b, Comparer<object>.Default);
3281 [ExpectedException (typeof (ArgumentException))]
3282 public void CompareToWithMultiDimArray3 () {
3283 IStructuralComparable a = new int [4] { 10, 10, 10, 10 };
3284 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3285 a.CompareTo (b, Comparer<object>.Default);
3289 [ExpectedException (typeof (IndexOutOfRangeException))]
3290 public void CompareToWithBoundedArray1 () {
3291 IStructuralComparable a = new int [2] { 10, 10 };
3292 Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 });
3293 IStructuralComparable b = ab;
3294 ab.SetValue (10, 5);
3295 ab.SetValue (10, 6);
3297 a.CompareTo (b, Comparer<object>.Default);
3301 [ExpectedException (typeof (IndexOutOfRangeException))]
3302 public void CompareToWithBoundedArray2 () {
3303 IStructuralComparable a = new int [2] { 10, 10 };
3304 Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 });
3305 IStructuralComparable b = ab;
3306 ab.SetValue (10, 5);
3307 ab.SetValue (10, 6);
3309 //Yes, CompareTo simply doesn't work with bounded arrays!
3310 b.CompareTo (b, Comparer<object>.Default);
3314 [ExpectedException (typeof (NullReferenceException))]
3315 public void CompareToWithNullComparer () {
3316 IStructuralComparable a = new int[] { 1, 2 };
3317 IStructuralComparable b = new int[] { 1, 2 };
3318 a.CompareTo (b, null);
3322 public void CompareToWithNullArray () {
3323 IStructuralComparable a = new int[] { 1, 2 };
3324 Assert.AreEqual (1, a.CompareTo (null, Comparer<object>.Default));
3328 public void CompareToWithGoodArrays () {
3329 IStructuralComparable a = new int[] { 10, 20 };
3330 Assert.AreEqual (0, a.CompareTo (a, Comparer<object>.Default));
3331 Assert.AreEqual (0, a.CompareTo (new int [] { 10, 20 }, Comparer<object>.Default));
3332 Assert.AreEqual (-1, a.CompareTo (new int [] { 11, 20 }, Comparer<object>.Default));
3333 Assert.AreEqual (-1, a.CompareTo (new int [] { 10, 21 }, Comparer<object>.Default));
3334 Assert.AreEqual (1, a.CompareTo (new int [] { 9, 20 }, Comparer<object>.Default));
3335 Assert.AreEqual (1, a.CompareTo (new int [] { 10, 19 }, Comparer<object>.Default));
3339 public void IStructuralEquatable_Equals ()
3341 IStructuralEquatable array = new int[] {1, 2, 3};
3342 IStructuralEquatable array2 = new int[] {1, 2, 3};
3343 Assert.AreEqual (false, array.Equals (null, null));
3344 Assert.AreEqual (true, array.Equals (array, null));
3345 Assert.AreEqual (true, array.Equals (array2, EqualityComparer<int>.Default));
3349 [ExpectedException (typeof (NullReferenceException))]
3350 public void IStructuralEquatable_Equals_NoComparer ()
3352 IStructuralEquatable array = new int[] {1, 2, 3};
3353 IStructuralComparable array2 = new int[] {1, 2, 3};
3354 array.Equals (array2, null);
3358 [ExpectedException (typeof (ArgumentException))]
3359 public void IStructuralEquatable_Equals_ComparerThrows ()
3361 IStructuralEquatable array = new int[] {1, 2, 3};
3362 IStructuralComparable array2 = new int[] {1, 2, 3};
3363 array.Equals (array2, EqualityComparer<long>.Default);