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);
2332 /* Check that ulong[] is not sorted as long[] */
2334 string[] names = new string[] {
2335 "A", "B", "C", "D", "E"
2338 ulong[] arr = new ulong [] {
2340 unchecked((ulong)0xffffFFFF00000000),
2347 Array.Sort (a, names, null);
2348 Assert.AreEqual (0, a.GetValue (0));
2353 public void SortNonGenericDoubleItems () {
2354 double[] doubleValues = new double[11];
2356 doubleValues[0] = 0.221788066253601;
2357 doubleValues[1] = 0.497278285809481;
2358 doubleValues[2] = 0.100565033883643;
2359 doubleValues[3] = 0.0433309347749905;
2360 doubleValues[4] = 0.00476726438463812;
2361 doubleValues[5] = 0.1354609735456;
2362 doubleValues[6] = 0.57690356588135;
2363 doubleValues[7] = 0.466239434334826;
2364 doubleValues[8] = 0.409741461978934;
2365 doubleValues[9] = 0.0112412763949565;
2366 doubleValues[10] = 0.668704347674307;
2368 int[] indices = new int[11];
2381 Array.Sort ((Array)doubleValues, (Array)indices);
2382 Assert.AreEqual (4, indices [0]);
2386 public void TestInitializeEmpty()
2398 Assert.IsTrue (!catched, "#TI01");
2402 public void TestInitializeInt()
2407 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2409 Assert.AreEqual (a[i], b[i], "#TI02 " + i);
2414 public void TestInitializeDouble()
2416 double[] a = {1.0,2.0,0.0};
2418 double[] b = {1.0,2.0,0.0};
2419 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2421 Assert.AreEqual (a[i], b[i], "#TI03 " + i);
2426 public void TestInitializeFloat()
2428 float[] a = {1.0F,2.0F,0.0F};
2430 float[] b = {1.0F,2.0F,0.0F};
2431 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2433 Assert.AreEqual (a[i], b[i], "#TI04 " + i);
2438 public void TestInitializeChar()
2440 char[] a = {'1','.','0','F','2','.','0','F'};
2442 char[] b = {'1','.','0','F','2','.','0','F'};
2443 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2445 Assert.AreEqual (a[i], b[i], "#TI05 " + i);
2450 public void TestInitializeString()
2452 string[] a = {"hola","adios","menos","mas"};
2454 string[] b = {"hola","adios","menos","mas"};
2455 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2457 Assert.AreEqual (a[i], b[i], "#TI06 " + i);
2462 public void TestInitializeEnum()
2464 enua[] a = {enua.hola,enua.adios,enua.menos,enua.mas};
2466 enua[] b = {enua.hola,enua.adios,enua.menos,enua.mas};
2467 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2469 Assert.AreEqual (a[i], b[i], "#TI07 " + i);
2474 public void TestInitializeIntNI()
2476 int[] a = new int[20];
2480 Assert.AreEqual (b, 0, "#TI08");
2485 public void TestInitializeCharNI()
2487 char[] a = new char[20];
2489 foreach(char b in a)
2491 Assert.AreEqual (b, 0, "#TI09");
2496 public void TestInitializeDoubleNI()
2498 double[] a = new double[20];
2500 foreach(double b in a)
2502 Assert.AreEqual (b, 0.0, "#TI09");
2507 public void TestInitializeStringNI()
2509 string[] a = new string[20];
2511 foreach(string b in a)
2513 Assert.AreEqual (b, null, "#TI10");
2518 public void TestInitializeObjectNI()
2520 object[] a = new object[20];
2522 foreach(object b in a)
2524 Assert.AreEqual (b, null, "#TI11");
2529 public void TestInitializeAClassNI()
2531 AClass[] a = new AClass[20];
2533 foreach(AClass b in a)
2535 Assert.AreEqual (b, null, "#TI12");
2541 public void TestInitializeAStructNI()
2543 AStruct[] a = new AStruct[20];
2545 foreach(AStruct b in a)
2547 Assert.AreEqual (b, new AStruct(), "#TI14");
2552 public void TestInitializeAStruct()
2554 AStruct[] a = new AStruct[3];
2558 AStruct[] b = new AStruct[3];
2561 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2563 Assert.AreEqual (a[i], b[i], "#TI15 " + i);
2568 public void TestInitializeDateTimeNI()
2570 DateTime[] a = new DateTime[20];
2572 foreach(DateTime b in a)
2574 Assert.AreEqual (b, new DateTime(), "#TI16");
2579 [ExpectedException (typeof (ArgumentNullException))]
2580 public void MoreSort1 ()
2582 Array.Sort (null, 0, 1);
2586 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2587 public void MoreSort2 ()
2589 Array.Sort (arrsort, -1, 3);
2593 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2594 public void MoreSort3 ()
2596 Array.Sort (arrsort, 1, -3);
2600 [ExpectedException (typeof (ArgumentException))]
2601 public void MoreSort4 ()
2603 Array.Sort (arrsort, arrsort.Length, arrsort.Length + 2);
2607 [ExpectedException (typeof (RankException))]
2608 public void MoreSort5 ()
2610 char [,] arr = new char [,] {{'a'}, {'b'}};
2611 Array.Sort (arr, 0, 1);
2615 public void MoreSort6 ()
2617 Array.Sort (arrsort, 0, 0);
2621 [ExpectedException (typeof (ArgumentException))]
2622 public void MoreSort7 ()
2624 Array.Sort (arrsort, arrsort.Length - 1, 2);
2628 [ExpectedException (typeof (ArgumentException))]
2629 public void MoreSort8 ()
2631 Array.Sort (arrsort, 0, arrsort.Length + 1);
2635 public void MoreSort9 ()
2637 Array.Sort (arrsort, null, 0, arrsort.Length, null);
2641 [ExpectedException (typeof (InvalidOperationException))]
2642 public void MoreSort10 ()
2644 object [] array = {true, 'k', SByte.MinValue, Byte.MinValue, (short) 2, 634, (long) 436, (float) 1.1, 1.23, "Hello World"};
2645 Array.Sort (array, (IComparer) null);
2648 [Test] // bug #81941
2651 double [] a = new double [2] { 0.9, 0.3 };
2652 uint [] b = new uint [2] { 4, 7 };
2654 Assert.AreEqual (0.3, a [0], "#1");
2655 Assert.AreEqual (0.9, a [1], "#2");
2656 Assert.AreEqual (7, b [0], "#3");
2657 Assert.AreEqual (4, b [1], "#4");
2661 public void ClearJaggedArray ()
2663 byte[][] matrix = new byte [8][];
2664 for (int i=0; i < 8; i++) {
2665 matrix [i] = new byte [8];
2666 for (int j=0; j < 8; j++) {
2670 Array.Clear (matrix, 0, 8);
2671 for (int i=0; i < 8; i++) {
2672 Assert.IsNull (matrix [i], i.ToString ());
2677 public void ClearMultidimentionalArray ()
2679 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2680 Array.Clear (matrix, 0, 2);
2681 Assert.AreEqual (0, matrix [0, 0], "0,0");
2682 Assert.AreEqual (0, matrix [0, 1], "0,1");
2683 Assert.AreEqual (2, matrix [1, 0], "1,0");
2684 Assert.AreEqual (2, matrix [1, 1], "1,1");
2688 [ExpectedException (typeof (IndexOutOfRangeException))]
2689 public void ClearOutsideMultidimentionalArray ()
2691 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2692 Array.Clear (matrix, 0, 5);
2696 [ExpectedException (typeof (IndexOutOfRangeException))]
2697 public void Clear_IndexOverflow ()
2699 byte[] array = new byte [16];
2700 Array.Clear (array, 4, Int32.MaxValue);
2704 [ExpectedException (typeof (IndexOutOfRangeException))]
2705 public void Clear_LengthOverflow ()
2707 byte[] array = new byte [16];
2708 Array.Clear (array, Int32.MaxValue, 4);
2712 [ExpectedException (typeof (ArgumentException))]
2713 public void Copy_SourceIndexOverflow ()
2715 byte[] array = new byte [16];
2716 Array.Copy (array, Int32.MaxValue, array, 8, 8);
2720 [ExpectedException (typeof (ArgumentException))]
2721 public void Copy_DestinationIndexOverflow ()
2723 byte[] array = new byte [16];
2724 Array.Copy (array, 8, array, Int32.MaxValue, 8);
2728 [ExpectedException (typeof (ArgumentException))]
2729 public void Copy_LengthOverflow ()
2731 byte[] array = new byte [16];
2732 Array.Copy (array, 8, array, 8, Int32.MaxValue);
2736 [ExpectedException (typeof (ArgumentException))]
2737 public void Reverse_IndexOverflow ()
2739 byte[] array = new byte [16];
2740 Array.Reverse (array, Int32.MaxValue, 8);
2744 [ExpectedException (typeof (ArgumentException))]
2745 public void Reverse_LengthOverflow ()
2747 byte[] array = new byte [16];
2748 Array.Reverse (array, 8, Int32.MaxValue);
2751 public struct CharX : IComparable {
2754 public CharX (char c)
2759 public int CompareTo (object obj)
2762 return c.CompareTo (((CharX) obj).c);
2764 return c.CompareTo (obj);
2769 public void BinarySearch_ArgPassingOrder ()
2772 // This tests that arguments are passed to the comprer in the correct
2773 // order. The IComparable of the *array* elements must get called, not
2774 // that of the search object.
2776 CharX [] x = { new CharX ('a'), new CharX ('b'), new CharX ('c') };
2777 Assert.AreEqual (1, Array.BinarySearch (x, 'b'));
2780 class Comparer: IComparer {
2782 private bool called = false;
2784 public bool Called {
2786 bool result = called;
2792 public int Compare (object x, object y)
2800 public void BinarySearch1_EmptyList ()
2802 int[] array = new int[0];
2803 Assert.AreEqual (- 1, Array.BinarySearch (array, 0), "BinarySearch");
2807 public void BinarySearch2_EmptyList ()
2809 int[] array = new int[0];
2810 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, 0), "BinarySearch");
2814 public void BinarySearch3_EmptyList ()
2816 Comparer comparer = new Comparer ();
2817 int[] array = new int[0];
2818 Assert.AreEqual (-1, Array.BinarySearch (array, 0, comparer), "BinarySearch");
2819 // bug 77030 - the comparer isn't called for an empty array/list
2820 Assert.IsTrue (!comparer.Called, "Called");
2824 public void BinarySearch4_EmptyList ()
2826 Comparer comparer = new Comparer ();
2827 int[] array = new int[0];
2828 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, comparer), "BinarySearch");
2829 // bug 77030 - the comparer isn't called for an empty array/list
2830 Assert.IsTrue (!comparer.Called, "Called");
2835 [ExpectedException (typeof (ArgumentNullException))]
2836 public void AsReadOnly_NullArray ()
2838 Array.AsReadOnly <int> (null);
2842 public void ReadOnly_Count ()
2844 Assert.AreEqual (10, Array.AsReadOnly (new int [10]).Count);
2848 public void ReadOnly_Contains ()
2850 int[] arr = new int [2];
2853 IList<int> a = Array.AsReadOnly (arr);
2855 Assert.IsTrue (a.Contains (3));
2856 Assert.IsTrue (!a.Contains (6));
2860 public void ReadOnly_IndexOf ()
2862 int[] arr = new int [2];
2865 IList<int> a = Array.AsReadOnly (arr);
2867 Assert.AreEqual (0, a.IndexOf (3));
2868 Assert.AreEqual (1, a.IndexOf (5));
2869 Assert.AreEqual (-1, a.IndexOf (6));
2873 public void ReadOnly_Indexer ()
2875 int[] arr = new int [2];
2878 IList<int> a = Array.AsReadOnly (arr);
2880 Assert.AreEqual (3, a [0]);
2881 Assert.AreEqual (5, a [1]);
2883 /* Check that modifications to the original array are visible */
2885 Assert.AreEqual (6, a [0]);
2889 public void ReadOnly_Enumerator ()
2891 int[] arr = new int [10];
2893 for (int i = 0; i < 10; ++i)
2897 foreach (int i in Array.AsReadOnly (arr))
2900 Assert.AreEqual (45, sum);
2904 public void ReadOnly_CopyTo ()
2906 int[] arr = new int [2];
2909 IList<int> a = Array.AsReadOnly (arr);
2911 int[] arr2 = new int [3];
2914 Assert.AreEqual (0, arr2 [0]);
2915 Assert.AreEqual (3, arr2 [1]);
2916 Assert.AreEqual (5, arr2 [2]);
2920 public void Resize ()
2922 int [] arr = new int [] { 1, 3, 5 };
2923 Array.Resize <int> (ref arr, 3);
2924 Assert.AreEqual (3, arr.Length, "#A1");
2925 Assert.AreEqual (1, arr [0], "#A2");
2926 Assert.AreEqual (3, arr [1], "#A3");
2927 Assert.AreEqual (5, arr [2], "#A4");
2929 Array.Resize <int> (ref arr, 2);
2930 Assert.AreEqual (2, arr.Length, "#B1");
2931 Assert.AreEqual (1, arr [0], "#B2");
2932 Assert.AreEqual (3, arr [1], "#B3");
2934 Array.Resize <int> (ref arr, 4);
2935 Assert.AreEqual (4, arr.Length, "#C1");
2936 Assert.AreEqual (1, arr [0], "#C2");
2937 Assert.AreEqual (3, arr [1], "#C3");
2938 Assert.AreEqual (0, arr [2], "#C4");
2939 Assert.AreEqual (0, arr [3], "#C5");
2943 public void Resize_null ()
2946 Array.Resize (ref arr, 10);
2947 Assert.AreEqual (arr.Length, 10);
2951 public void Test_ContainsAndIndexOf_EquatableItem ()
2953 EquatableClass[] list = new EquatableClass[] {new EquatableClass (0), new EquatableClass (1), new EquatableClass (0)};
2955 Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, list[0]), "#0");
2956 Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, new EquatableClass (0)), "#1");
2957 Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, list[0]), "#2");
2958 Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, new EquatableClass (0)), "#3");
2961 public class EquatableClass : IEquatable<EquatableClass>
2964 public EquatableClass (int x)
2969 public bool Equals (EquatableClass other)
2971 return this._x == other._x;
2976 public void AsIList ()
2978 IList<int> arr = new int [10];
2980 Assert.AreEqual (5, arr [0]);
2982 IList<FooStruct> arr2 = new FooStruct [10];
2983 FooStruct s = new FooStruct ();
2988 Assert.AreEqual (11, s.i);
2989 Assert.AreEqual (22, s.j);
2991 IList<string> arr3 = new string [10];
2993 Assert.AreEqual ("ABC", arr3 [5]);
3000 #if !TARGET_JVM // BugBUG: T[] is not yet ICollection<T> under TARGET_JVM
3003 public void ICollectionNull ()
3005 ICollection<object> test;
3007 test = new List<object>();
3008 Assert.AreEqual (test.Contains (null), false, "list<o>");
3010 test = new object[] {};
3011 Assert.AreEqual (test.Contains (null), false, "empty array");
3013 test = new object[] {null};
3014 Assert.AreEqual (test.Contains (null), true, "array with null");
3016 test = new object[] { 1, null};
3017 Assert.IsTrue (test.Contains (null), "array with last null");
3019 test = new List<object>(test);
3020 Assert.AreEqual (test.Contains (null), true, "List<object> with test");
3022 test = new object[] {new object()};
3023 Assert.AreEqual (test.Contains (null), false, "array with object");
3025 test = new List<object>(test);
3026 Assert.AreEqual (test.Contains (null), false, "array with test");
3028 #endif // TARGET_JVM
3033 enum ByteEnum : byte {}
3034 enum IntEnum : int {}
3037 public void TestByteEnumArrayToByteArray ()
3039 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
3040 byte[] b = new byte[a.Length];
3045 public void TestByteEnumArrayToIntArray ()
3047 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
3048 int[] b = new int[a.Length];
3053 [ExpectedException (typeof (ArrayTypeMismatchException))]
3054 public void TestIntEnumArrayToByteArray ()
3056 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
3057 byte[] b = new byte[a.Length];
3062 public void TestIntEnumArrayToIntArray ()
3064 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
3065 int[] b = new int[a.Length];
3072 [Test] // bug #322248
3073 public void IEnumerator_Reset ()
3075 int[] array = new int[] { 1, 2, 3};
3076 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3077 Assert.IsTrue (e.MoveNext (), "#A1");
3078 Assert.AreEqual (1, e.Current, "#A2");
3079 Assert.IsTrue (e.MoveNext (), "#A3");
3080 Assert.AreEqual (2, e.Current, "#A4");
3084 Assert.IsTrue (e.MoveNext (), "#C1");
3085 Assert.AreEqual (1, e.Current, "#C2");
3089 public void IEnumerator_Current_Finished ()
3091 int[] array = new int[] { 1, 2, 3 };
3092 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3093 Assert.IsTrue (e.MoveNext (), "#A1");
3094 Assert.AreEqual (1, e.Current, "#A2");
3095 Assert.IsTrue (e.MoveNext (), "#A3");
3096 Assert.AreEqual (2, e.Current, "#A4");
3097 Assert.IsTrue (e.MoveNext (), "#A5");
3098 Assert.AreEqual (3, e.Current, "#A6");
3099 Assert.IsTrue (!e.MoveNext (), "#A6");
3102 Assert.Fail ("#B1:" + e.Current);
3103 } catch (InvalidOperationException ex) {
3104 // Enumeration already finished
3105 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3106 Assert.IsNull (ex.InnerException, "#B3");
3107 Assert.IsNotNull (ex.Message, "#B4");
3112 public void IEnumerator_Current_NotStarted ()
3114 int[] array = new int[] { 1, 2, 3 };
3115 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3118 Assert.Fail ("#A1:" + e.Current);
3119 } catch (InvalidOperationException ex) {
3120 // Enumeration has not started. Call MoveNext
3121 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
3122 Assert.IsNull (ex.InnerException, "#A3");
3123 Assert.IsNotNull (ex.Message, "#A4");
3128 public void IEnumerator_Current_Reset ()
3130 int[] array = new int[] { 1, 2, 3 };
3131 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3136 Assert.Fail ("#B1:" + e.Current);
3137 } catch (InvalidOperationException ex) {
3138 // Enumeration has not started. Call MoveNext
3139 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3140 Assert.IsNull (ex.InnerException, "#B3");
3141 Assert.IsNotNull (ex.Message, "#B4");
3145 public void ICollection_IsReadOnly() {
3146 ICollection<string> arr = new string [10];
3148 Assert.IsTrue (arr.IsReadOnly);
3153 [ExpectedException (typeof (NotSupportedException))]
3154 public void ArrayCreateInstanceOfVoid ()
3156 Array.CreateInstance (typeof (void), 42);
3162 [ExpectedException (typeof (NotSupportedException))]
3163 public void ArrayCreateInstanceOfOpenGenericType ()
3165 Array.CreateInstance (typeof (Foo<>), 42);
3169 [ExpectedException (typeof (IndexOutOfRangeException))]
3170 public void ClearNegativeLength ()
3172 Array.Clear (new int [] { 1, 2 }, 0, -1);
3176 [ExpectedException (typeof (ArgumentException))]
3177 public void MultiDimension_IList_setItem ()
3179 IList array = new int [1, 1];
3184 [ExpectedException (typeof (ArgumentException))]
3185 public void MultiDimension_IList_getItem ()
3187 IList array = new int [1, 1];
3188 int a = (int) array [0];
3192 public void SetValue_Nullable () {
3193 Array array = Array.CreateInstance (typeof (int?), 7);
3197 array.SetValue (o, 0);
3198 Assert.AreEqual (42, array.GetValue (0));
3200 array.SetValue (null, 0);
3201 Assert.AreEqual (null, array.GetValue (0));
3205 public void SortNullsWithGenericVersion ()
3207 string[] s1 = new string[6]{
3215 string[] s2 = new string[]{null,
3222 Array.Sort<string> (s1);
3223 for (int i = 0; i < 6; i++) {
3224 Assert.AreEqual (s1[i], s2[i], "At:" + i);
3229 // This is a test case for the case that was broken by the code contributed
3232 // This tests the fix for: #622101
3235 public void SortActuallyWorks ()
3237 string[] data = new string[9]{"Foo", "Bar", "Dingus", null, "Dingu4", "123", "Iam", null, "NotNull"};
3238 IComparer comparer = new NullAtEndComparer ();
3239 Array.Sort (data, comparer);
3241 Assert.AreEqual (data [7], null);
3242 Assert.AreNotEqual (data [0], null);
3245 class NullAtEndComparer : IComparer {
3246 public int Compare(object x, object y)
3248 if (x == null) return 1;
3249 if (y == null) return -1;
3250 return ((string)x).CompareTo((string)y);
3256 [ExpectedException (typeof (ArgumentException))]
3257 public void CompareToWithJaggedArray () {
3258 IStructuralComparable a = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }};
3259 IStructuralComparable b = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }};
3260 a.CompareTo (b, Comparer<object>.Default);
3264 [ExpectedException (typeof (ArgumentException))]
3265 public void CompareToWithArrayOfTheWrongKind () {
3266 IStructuralComparable a = new int[] { 1, 2 };
3267 IStructuralComparable b = new double[] { 1, 2 };
3268 a.CompareTo (b, Comparer<object>.Default);
3272 [ExpectedException (typeof (ArgumentException))]
3273 public void CompareToWithNonArrayType () {
3274 IStructuralComparable a = new int[] { 1, 2 };
3275 a.CompareTo (99, Comparer<object>.Default);
3279 [ExpectedException (typeof (ArgumentException))]
3280 public void CompareToWithNonArrayOfDifferentSize () {
3281 IStructuralComparable a = new int[] { 1, 2 };
3282 IStructuralComparable b = new int[] { 1, 2, 3 };
3283 a.CompareTo (b, Comparer<object>.Default);
3287 [ExpectedException (typeof (ArgumentException))]
3288 public void CompareToWithMultiDimArray1 () {
3289 IStructuralComparable a = new int [2,2] { {10, 10 }, { 10, 10 } };
3290 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3291 a.CompareTo (b, Comparer<object>.Default);
3295 [ExpectedException (typeof (ArgumentException))]
3296 public void CompareToWithMultiDimArray2 () {
3297 IStructuralComparable a = new int [2] { 10, 10 };
3298 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3299 a.CompareTo (b, Comparer<object>.Default);
3303 [ExpectedException (typeof (ArgumentException))]
3304 public void CompareToWithMultiDimArray3 () {
3305 IStructuralComparable a = new int [4] { 10, 10, 10, 10 };
3306 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3307 a.CompareTo (b, Comparer<object>.Default);
3311 [ExpectedException (typeof (IndexOutOfRangeException))]
3312 public void CompareToWithBoundedArray1 () {
3313 IStructuralComparable a = new int [2] { 10, 10 };
3314 Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 });
3315 IStructuralComparable b = ab;
3316 ab.SetValue (10, 5);
3317 ab.SetValue (10, 6);
3319 a.CompareTo (b, Comparer<object>.Default);
3323 [ExpectedException (typeof (IndexOutOfRangeException))]
3324 public void CompareToWithBoundedArray2 () {
3325 IStructuralComparable a = new int [2] { 10, 10 };
3326 Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 });
3327 IStructuralComparable b = ab;
3328 ab.SetValue (10, 5);
3329 ab.SetValue (10, 6);
3331 //Yes, CompareTo simply doesn't work with bounded arrays!
3332 b.CompareTo (b, Comparer<object>.Default);
3336 [ExpectedException (typeof (NullReferenceException))]
3337 public void CompareToWithNullComparer () {
3338 IStructuralComparable a = new int[] { 1, 2 };
3339 IStructuralComparable b = new int[] { 1, 2 };
3340 a.CompareTo (b, null);
3344 public void CompareToWithNullArray () {
3345 IStructuralComparable a = new int[] { 1, 2 };
3346 Assert.AreEqual (1, a.CompareTo (null, Comparer<object>.Default));
3350 public void CompareToWithGoodArrays () {
3351 IStructuralComparable a = new int[] { 10, 20 };
3352 Assert.AreEqual (0, a.CompareTo (a, Comparer<object>.Default));
3353 Assert.AreEqual (0, a.CompareTo (new int [] { 10, 20 }, Comparer<object>.Default));
3354 Assert.AreEqual (-1, a.CompareTo (new int [] { 11, 20 }, Comparer<object>.Default));
3355 Assert.AreEqual (-1, a.CompareTo (new int [] { 10, 21 }, Comparer<object>.Default));
3356 Assert.AreEqual (1, a.CompareTo (new int [] { 9, 20 }, Comparer<object>.Default));
3357 Assert.AreEqual (1, a.CompareTo (new int [] { 10, 19 }, Comparer<object>.Default));
3361 public void IStructuralEquatable_Equals ()
3363 IStructuralEquatable array = new int[] {1, 2, 3};
3364 IStructuralEquatable array2 = new int[] {1, 2, 3};
3365 Assert.AreEqual (false, array.Equals (null, null));
3366 Assert.AreEqual (true, array.Equals (array, null));
3367 Assert.AreEqual (true, array.Equals (array2, EqualityComparer<int>.Default));
3371 [ExpectedException (typeof (NullReferenceException))]
3372 public void IStructuralEquatable_Equals_NoComparer ()
3374 IStructuralEquatable array = new int[] {1, 2, 3};
3375 IStructuralComparable array2 = new int[] {1, 2, 3};
3376 array.Equals (array2, null);
3380 [ExpectedException (typeof (ArgumentException))]
3381 public void IStructuralEquatable_Equals_ComparerThrows ()
3383 IStructuralEquatable array = new int[] {1, 2, 3};
3384 IStructuralComparable array2 = new int[] {1, 2, 3};
3385 array.Equals (array2, EqualityComparer<long>.Default);