1 // ArrayTest.cs - NUnit Test Cases for the System.Array class
3 // David Brandt (bucky@keystreams.com)
4 // Eduardo Garcia (kiwnix@yahoo.es)
6 // (C) Ximian, Inc. http://www.ximian.com
7 // Copyright (C) 2004 Novell (http://www.novell.com)
10 using NUnit.Framework;
12 using System.Collections;
13 using System.Globalization;
16 using System.Collections.Generic;
19 namespace MonoTests.System
22 enum enua {hola,adios,mas,menos};
48 public override bool Equals (object obj)
54 //End Auxiliary Things
57 public class ArrayTest
59 char [] arrsort = {'d', 'b', 'f', 'e', 'a', 'c'};
64 public void TestIsFixedSize() {
66 Assert.IsTrue (a1.IsFixedSize, "All arrays are fixed");
70 public void TestIsReadOnly() {
72 Assert.IsTrue (!a1.IsReadOnly, "No array is readonly");
76 public void TestIsSynchronized() {
78 Assert.IsTrue (!a1.IsSynchronized, "No array is synchronized");
82 public void TestLength() {
85 Assert.AreEqual (0, a1.Length, "Zero length array");
89 Assert.AreEqual (1, a1.Length, "One-length array");
92 char[] a1 = {'c', 'c'};
93 Assert.AreEqual (2, a1.Length, "Two-length array");
98 public void TestRank() {
99 char[] a1 = { 'c', 'd', 'e' };
100 Assert.AreEqual (1, a1.Rank, "Rank one");
102 char[,] a2 = new Char[3,3];
103 Assert.AreEqual (2, a2.Rank, "Rank two");
105 char[,,] a3 = new Char[3,3,3];
106 Assert.AreEqual (3, a3.Rank, "Rank three");
110 public void TestBinarySearch1() {
111 bool errorThrown = false;
113 Array.BinarySearch(null, "blue");
114 } catch (ArgumentNullException) {
117 Assert.IsTrue (errorThrown, "#B01");
120 char[,] c1 = new Char[2,2];
121 Array.BinarySearch(c1, "needle");
122 } catch (RankException) {
125 Assert.IsTrue (errorThrown, "#B02");
128 char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
129 Assert.IsTrue (Array.BinarySearch(arr, 'c') >= 3, "#B05");
130 Assert.IsTrue (Array.BinarySearch(arr, 'c') < 6, "#B06");
133 char[] arr = {'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
134 Assert.AreEqual (-4, Array.BinarySearch(arr, 'c'), "#B07");
137 char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
138 Assert.AreEqual (-9, Array.BinarySearch(arr, 'e'), "#B08");
143 public void TestBinarySearch2() {
144 bool errorThrown = false;
146 Array.BinarySearch(null, 0, 1, "blue");
147 } catch (ArgumentNullException) {
150 Assert.IsTrue (errorThrown, "#B20");
153 char[,] c1 = new Char[2,2];
154 Array.BinarySearch(c1, 0, 1, "needle");
155 } catch (RankException) {
158 Assert.IsTrue (errorThrown, "#B21");
162 Array.BinarySearch(c1, -1, 1, 'a');
163 } catch (ArgumentOutOfRangeException) {
166 Assert.IsTrue (errorThrown, "#B22");
170 Array.BinarySearch(c1, 0, -1, 'a');
171 } catch (ArgumentOutOfRangeException) {
174 Assert.IsTrue (errorThrown, "#B23");
178 Array.BinarySearch(c1, 0, 4, 'a');
179 } catch (ArgumentException) {
182 Assert.IsTrue (errorThrown, "#B24");
185 char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
186 Assert.IsTrue (Array.BinarySearch(arr, 2, 8, 'c') >= 5, "#B26");
187 Assert.IsTrue (Array.BinarySearch(arr, 2, 8, 'c') < 8, "#B27");
190 char[] arr = {'z', 'z', 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
191 Assert.AreEqual (-6, Array.BinarySearch(arr, 2, 8, 'c'), "#B28");
194 char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
195 Assert.AreEqual (-11, Array.BinarySearch(arr, 2, 8, 'e'), "#B29");
199 public void TestBinarySearch3()
201 int[] array = new int[100];
203 for (int i = 0; i < 100; i++)
206 Assert.AreEqual (49, Array.BinarySearch(array, 10), "#B30");
210 public void BinarySearch_NullValue ()
212 int[] array = new int[1];
213 Assert.AreEqual (-1, Array.BinarySearch (array, null), "I=a,o");
214 Assert.AreEqual (-1, Array.BinarySearch (array, null, null), "I=a,o,c");
215 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 1, null), "I=a,i,i,o");
216 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 1, null, null), "I=a,i,i,o,c");
218 object[] o = new object [3] { this, this, null };
219 Assert.AreEqual (-1, Array.BinarySearch (o, null), "O=a,o");
220 Assert.AreEqual (-1, Array.BinarySearch (o, null, null), "O=a,o,c");
221 Assert.AreEqual (-1, Array.BinarySearch (o, 0, 3, null), "O=a,i,i,o");
222 Assert.AreEqual (-1, Array.BinarySearch (o, 0, 3, null, null), "O=a,i,i,o,c");
225 // TODO - testBinarySearch with explicit IComparer args
228 public void TestClear() {
229 bool errorThrown = false;
231 Array.Clear(null, 0, 1);
232 } catch (ArgumentNullException) {
235 Assert.IsTrue (errorThrown, "#C01");
237 int[] i1 = { 1, 2, 3, 4 };
239 int[] compare = {1,2,3,4};
240 Assert.AreEqual (compare[0], i1[0], "#C02");
241 Assert.AreEqual (compare[1], i1[1], "#C03");
242 Assert.AreEqual (compare[2], i1[2], "#C04");
243 Assert.AreEqual (compare[3], i1[3], "#C05");
245 Array.Clear(i1, 3, 1);
247 int[] compare = {1,2,3,0};
248 Assert.AreEqual (compare[0], i1[0], "#C06");
249 Assert.AreEqual (compare[1], i1[1], "#C07");
250 Assert.AreEqual (compare[2], i1[2], "#C08");
251 Assert.AreEqual (compare[3], i1[3], "#C09");
253 Array.Clear(i1, 1, 1);
255 int[] compare = {1,0,3,0};
256 Assert.AreEqual (compare[0], i1[0], "#C10");
257 Assert.AreEqual (compare[1], i1[1], "#C11");
258 Assert.AreEqual (compare[2], i1[2], "#C12");
259 Assert.AreEqual (compare[3], i1[3], "#C13");
261 Array.Clear(i1, 1, 3);
263 int[] compare = {1,0,0,0};
264 Assert.AreEqual (compare[0], i1[0], "#C14");
265 Assert.AreEqual (compare[1], i1[1], "#C15");
266 Assert.AreEqual (compare[2], i1[2], "#C16");
267 Assert.AreEqual (compare[3], i1[3], "#C17");
270 string[] s1 = { "red", "green", "blue" };
271 Array.Clear(s1, 0, 3);
273 string[] compare = {null, null, null};
274 Assert.AreEqual (compare[0], s1[0], "#C18");
275 Assert.AreEqual (compare[1], s1[1], "#C19");
276 Assert.AreEqual (compare[2], s1[2], "#C20");
281 public void TestClone() {
282 char[] c1 = {'a', 'b', 'c'};
283 char[] c2 = (char[])c1.Clone();
284 Assert.AreEqual (c1[0], c2[0], "#D01");
285 Assert.AreEqual (c1[1], c2[1], "#D02");
286 Assert.AreEqual (c1[2], c2[2], "#D03");
288 char[] d10 = {'a', 'b'};
289 char[] d11 = {'a', 'c'};
290 char[] d12 = {'b', 'c'};
291 char[][] d1 = {d10, d11, d12};
292 char[][] d2 = (char[][])d1.Clone();
293 Assert.AreEqual (d1[0], d2[0], "#D04");
294 Assert.AreEqual (d1[1], d2[1], "#D05");
295 Assert.AreEqual (d1[2], d2[2], "#D06");
298 Assert.AreEqual (d1[0], d2[0], "#D07");
301 [Test] public void TestIndexer ()
303 int [] a = new int [10];
307 Assert.Fail ("IList.this [-1] should throw");
308 } catch (IndexOutOfRangeException) {
311 Assert.Fail ("Should have thrown an IndexOutOfRangeException");
316 public void TestCopy() {
318 bool errorThrown = false;
321 Array.Copy(c1, null, 1);
322 } catch (ArgumentNullException) {
325 Assert.IsTrue (errorThrown, "#E01");
328 bool errorThrown = false;
331 Array.Copy(null, c1, 1);
332 } catch (ArgumentNullException) {
335 Assert.IsTrue (errorThrown, "#E02");
338 bool errorThrown = false;
340 Char[] c1 = new Char[1];
341 Char[,] c2 = new Char[1,1];
342 Array.Copy(c1, c2, 1);
343 } catch (RankException) {
346 Assert.IsTrue (errorThrown, "#E03");
349 bool errorThrown = false;
351 Char[] c1 = new Char[1];
352 string[] s1 = new String[1];
353 Array.Copy(c1, s1, 1);
354 } catch (ArrayTypeMismatchException) {
357 Assert.IsTrue (errorThrown, "#E04");
360 bool errorThrown = false;
362 Char[] c1 = new Char[1];
363 Object[] o1 = new Object[1];
365 Array.Copy(o1, c1, 1);
366 } catch (InvalidCastException) {
369 Assert.IsTrue (errorThrown, "#E05");
372 bool errorThrown = false;
374 Char[] c1 = new Char[1];
375 Char[] c2 = new Char[1];
376 Array.Copy(c1, c2, -1);
377 } catch (ArgumentOutOfRangeException) {
380 Assert.IsTrue (errorThrown, "#E06");
383 bool errorThrown = false;
385 Char[] c1 = new Char[1];
386 Char[] c2 = new Char[2];
387 Array.Copy(c1, c2, 2);
388 } catch (ArgumentException) {
391 Assert.IsTrue (errorThrown, "#E07");
394 bool errorThrown = false;
396 Char[] c1 = new Char[1];
397 Char[] c2 = new Char[2];
398 Array.Copy(c2, c1, 2);
399 } catch (ArgumentException) {
402 Assert.IsTrue (errorThrown, "#E08");
405 char[] orig = {'a', 'b', 'd', 'a'};
406 char[] copy = new Char[4];
407 Array.Copy(orig, copy, 4);
408 for (int i = 0; i < orig.Length; i++) {
409 Assert.AreEqual (orig[i], copy[i], "#E09(" + i + ")");
411 Array.Clear(copy, 0, copy.Length);
412 for (int i = 0; i < orig.Length; i++) {
413 Assert.AreEqual ((char)0, copy[i], "#E10(" + i + ")");
415 Array.Copy(orig, copy, 2);
416 Assert.AreEqual (orig[0], copy[0], "#E11");
417 Assert.AreEqual (orig[1], copy[1], "#E12");
418 Assert.IsTrue (orig[2] != copy[2], "#E13");
419 Assert.IsTrue (orig[3] != copy[3], "#E14");
423 public void TestCopy2() {
425 bool errorThrown = false;
427 Char[] c1 = new Char[2];
428 Char[] c2 = new Char[2];
429 Array.Copy(c2, 1, c1, 0, 2);
430 } catch (ArgumentException) {
433 Assert.IsTrue (errorThrown, "#E31");
436 bool errorThrown = false;
438 Char[] c1 = new Char[2];
439 Char[] c2 = new Char[2];
440 Array.Copy(c2, 0, c1, 1, 2);
441 } catch (ArgumentException) {
444 Assert.IsTrue (errorThrown, "#E32");
447 char[] orig = {'a', 'b', 'd', 'a'};
448 char[] copy = new Char[4];
449 Array.Copy(orig, 1, copy, 1, 3);
450 Assert.IsTrue (copy[0] != orig[0], "#E33");
451 for (int i = 1; i < orig.Length; i++) {
452 Assert.AreEqual (orig[i], copy[i], "#E34(" + i + ")");
454 Array.Clear(copy, 0, copy.Length);
455 Array.Copy(orig, 1, copy, 0, 2);
456 Assert.AreEqual (orig[1], copy[0], "#E35");
457 Assert.AreEqual (orig[2], copy[1], "#E36");
458 Assert.IsTrue (copy[2] != orig[2], "#E37");
459 Assert.IsTrue (copy[3] != orig[3], "#E38");
463 [ExpectedException (typeof (InvalidCastException))]
464 public void Copy_InvalidCast () {
465 object[] arr1 = new object [10];
466 Type[] arr2 = new Type [10];
468 arr1 [0] = new object ();
470 Array.Copy (arr1, 0, arr2, 0, 10);
474 public void TestCopyTo() {
476 bool errorThrown = false;
478 Char[] c1 = new Char[2];
480 } catch (ArgumentNullException) {
483 Assert.IsTrue (errorThrown, "#E61");
486 bool errorThrown = false;
488 Char[] c1 = new Char[2];
489 Char[,] c2 = new Char[2,2];
491 } catch (ArgumentException) {
494 #if TARGET_JVM // This is really implementation dependent behaviour.
495 catch (RankException) {
499 Assert.IsTrue (errorThrown, "#E62");
502 bool errorThrown = false;
504 Char[,] c1 = new Char[2,2];
505 Char[] c2 = new Char[2];
507 } catch (RankException) {
510 Assert.IsTrue (errorThrown, "#E63");
513 bool errorThrown = false;
515 Char[,] c1 = new Char[2,2];
516 Char[] c2 = new Char[2];
518 } catch (RankException) {
521 Assert.IsTrue (errorThrown, "#E64");
524 bool errorThrown = false;
526 Char[] c1 = new Char[2];
527 Char[] c2 = new Char[2];
529 } catch (ArgumentOutOfRangeException) {
532 Assert.IsTrue (errorThrown, "#E65");
535 bool errorThrown = false;
537 Char[] c1 = new Char[2];
538 Char[] c2 = new Char[2];
540 } catch (ArgumentException) {
543 Assert.IsTrue (errorThrown, "#E66");
546 bool errorThrown = false;
548 Char[] c1 = new Char[2];
549 Char[] c2 = new Char[2];
551 } catch (ArgumentException) {
554 Assert.IsTrue (errorThrown, "#E67");
558 bool errorThrown = false;
560 String[] c1 = new String[2];
561 // TODO: this crashes mono if there are null
562 // values in the array.
565 Char[] c2 = new Char[2];
569 } catch (ArrayTypeMismatchException) {
572 Assert.IsTrue (errorThrown, "#E68");
575 Char[] orig = {'a', 'b', 'c', 'd'};
576 Char[] copy = new Char[10];
577 Array.Clear(copy, 0, copy.Length);
578 orig.CopyTo(copy, 3);
579 Assert.AreEqual ((char)0, copy[0], "#E69");
580 Assert.AreEqual ((char)0, copy[1], "#E70");
581 Assert.AreEqual ((char)0, copy[2], "#E71");
582 Assert.AreEqual (orig[0], copy[3], "#E72");
583 Assert.AreEqual (orig[1], copy[4], "#E73");
584 Assert.AreEqual (orig[2], copy[5], "#E74");
585 Assert.AreEqual (orig[3], copy[6], "#E75");
586 Assert.AreEqual ((char)0, copy[7], "#E76");
587 Assert.AreEqual ((char)0, copy[8], "#E77");
588 Assert.AreEqual ((char)0, copy[9], "#E78");
591 // The following is valid and must not throw an exception.
592 bool errorThrown = false;
594 int[] src = new int [0];
595 int[] dest = new int [0];
596 src.CopyTo (dest, 0);
597 } catch (ArgumentException) {
600 Assert.IsTrue (!errorThrown, "#E79");
605 bool errorThrown = false;
607 CClass[] src = new CClass [] { new CClass () };
608 BClass[] dest = new BClass [1];
610 src.CopyTo (dest, 0);
612 } catch (ArrayTypeMismatchException) {
615 Assert.IsTrue (errorThrown, "#E80");
620 public void TestCreateInstance() {
622 bool errorThrown = false;
624 Array.CreateInstance(null, 12);
625 } catch (ArgumentNullException) {
628 Assert.IsTrue (errorThrown, "#F01");
631 bool errorThrown = false;
633 Array.CreateInstance(Type.GetType("System.Char"), -3);
634 } catch (ArgumentOutOfRangeException) {
637 Assert.IsTrue (errorThrown, "#F02");
640 bool errorThrown = false;
642 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
643 } catch (ArgumentNullException) {
646 Assert.IsTrue (errorThrown, "#F03a");
650 bool errorThrown = false;
652 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
653 } catch (ArgumentNullException) {
656 Assert.IsTrue (errorThrown, "#F03b");
659 #if !TARGET_JVM // Arrays lower bounds are not supported for TARGET_JVM
661 bool errorThrown = false;
663 Array.CreateInstance(Type.GetType("System.Char"), null, null);
664 } catch (ArgumentNullException) {
667 Assert.IsTrue (errorThrown, "#F04");
671 bool errorThrown = false;
673 int[] lengths = new int [0];
674 Array.CreateInstance(Type.GetType("System.Char"), lengths);
675 } catch (ArgumentException) {
678 Assert.IsTrue (errorThrown, "#F05");
680 #if !TARGET_JVM // CreateInstance with lower bounds not supported for TARGET_JVM
682 bool errorThrown = false;
684 int[] lengths = new int [1];
685 int[] bounds = new int [2];
686 Array.CreateInstance(Type.GetType("System.Char"), lengths, bounds);
688 } catch (ArgumentException) {
691 Assert.IsTrue (errorThrown, "#F06");
694 char[] c1 = (char[])Array.CreateInstance(Type.GetType("System.Char"), 12);
695 Assert.AreEqual (12, c1.Length, "#F07");
697 Array c2 = Array.CreateInstance(Type.GetType("System.Char"), 12, 5);
698 Assert.AreEqual (2, c2.Rank, "#F08");
699 Assert.AreEqual (60, c2.Length, "#F09");
703 int[] lengths = { 3 };
704 int[] bounds = { 5 };
705 int[] src = { 512, 718, 912 };
706 Array array = Array.CreateInstance(typeof(int), lengths, bounds);
708 Assert.AreEqual (3, array.Length, "#F10");
709 Assert.AreEqual (5, array.GetLowerBound(0), "#F11");
710 Assert.AreEqual (7, array.GetUpperBound(0), "#F12");
712 src.CopyTo (array, 5);
714 for (int i = 0; i < src.Length; i++)
715 Assert.AreEqual (src[i], array.GetValue(i+5), "#F13(" + i + ")");
718 // Test that a 1 dimensional array with 0 lower bound is the
719 // same as an szarray
720 Type szarrayType = new int [10].GetType ();
721 Assert.IsTrue (szarrayType == (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0})).GetType ());
722 Assert.IsTrue (szarrayType != (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {1})).GetType ());
727 [ExpectedException (typeof (ArgumentNullException))]
728 public void TestCreateInstance2 ()
730 Array.CreateInstance (typeof (Int32), (int[])null);
735 [ExpectedException (typeof (ArgumentNullException))]
737 [ExpectedException (typeof (NullReferenceException))]
739 public void TestCreateInstance2b ()
741 Array.CreateInstance (typeof (Int32), (long[])null);
745 public void TestGetEnumerator() {
746 String[] s1 = {"this", "is", "a", "test"};
747 IEnumerator en = s1.GetEnumerator ();
748 Assert.IsNotNull (en, "#G01");
750 Assert.IsTrue (en.MoveNext (), "#G02");
751 Assert.AreEqual ("this", en.Current, "#G03");
752 Assert.IsTrue (en.MoveNext (), "#G04");
753 Assert.AreEqual ("is", en.Current, "#G05");
754 Assert.IsTrue (en.MoveNext (), "#G06");
755 Assert.AreEqual ("a", en.Current, "#G07");
756 Assert.IsTrue (en.MoveNext (), "#G08");
757 Assert.AreEqual ("test", en.Current, "#G09");
758 Assert.IsTrue (!en.MoveNext (), "#G10");
761 Assert.IsTrue (en.MoveNext (), "#G11");
762 Assert.AreEqual ("this", en.Current, "#G12");
764 // mutation does not invalidate array enumerator!
765 s1.SetValue ("change", 1);
766 Assert.IsTrue (en.MoveNext (), "#G13");
767 Assert.AreEqual ("change", en.Current, "#G14");
771 public void TestGetEnumeratorMultipleDimension() {
772 String[,] s1 = {{"this", "is"}, {"a", "test"}};
773 IEnumerator en = s1.GetEnumerator ();
774 Assert.IsNotNull (en, "#AA01");
776 Assert.IsTrue (en.MoveNext (), "#AA02");
777 Assert.AreEqual ("this", en.Current, "#AA03");
778 Assert.IsTrue (en.MoveNext (), "#AA04");
779 Assert.AreEqual ("is", en.Current, "#AA05");
780 Assert.IsTrue (en.MoveNext (), "#AA06");
781 Assert.AreEqual ("a", en.Current, "#AA07");
782 Assert.IsTrue (en.MoveNext (), "#AA08");
783 Assert.AreEqual ("test", en.Current, "#AA09");
784 Assert.IsTrue (!en.MoveNext (), "#AA10");
787 Assert.IsTrue (en.MoveNext (), "#AA11");
788 Assert.AreEqual ("this", en.Current, "#AA12");
791 // mutation does not invalidate array enumerator!
792 s1.SetValue ("change", idxs);
793 Assert.IsTrue (en.MoveNext (), "#AA13");
794 Assert.AreEqual ("change", en.Current, "#AA14");
798 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
799 public void TestGetEnumeratorNonZeroLowerBounds() {
800 int[] myLengthsArray = new int[2] { 3, 5 };
801 int[] myBoundsArray = new int[2] { 2, 3 };
803 Array myArray=Array.CreateInstance( typeof(String), myLengthsArray, myBoundsArray );
804 for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
805 for ( int j = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++ ) {
806 int[] myIndicesArray = new int[2] { i, j };
807 myArray.SetValue( Convert.ToString(i) + j, myIndicesArray );
809 IEnumerator en = myArray.GetEnumerator ();
810 Assert.IsNotNull (en, "#AB01");
812 // check the first couple of values
813 Assert.IsTrue (en.MoveNext (), "#AB02");
814 Assert.AreEqual ("23", en.Current, "#AB03");
815 Assert.IsTrue (en.MoveNext (), "#AB04");
816 Assert.AreEqual ("24", en.Current, "#AB05");
818 // then check the last element's value
821 lastElement = (string)en.Current;
822 } while (en.MoveNext());
823 Assert.AreEqual ("47", lastElement, "#AB06");
827 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
828 public void TestIList_Add () {
829 int[] myLengthsArray = new int[2] { 3, 5 };
830 int[] myBoundsArray = new int[2] { 2, 3 };
832 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
834 ((IList)myArray).Add ("can not");
835 Assert.Fail ("IList.Add should throw");
837 catch (NotSupportedException) {
841 Assert.Fail ("IList.Add threw wrong exception type");
844 Assert.Fail ("IList.Add shouldn't get this far");
848 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
849 public void TestIList_Insert () {
850 int[] myLengthsArray = new int[2] { 3, 5 };
851 int[] myBoundsArray = new int[2] { 2, 3 };
853 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
855 ((IList)myArray).Insert (0, "can not");
856 Assert.Fail ("IList.Insert should throw");
858 catch (NotSupportedException) {
862 Assert.Fail ("IList.Insert threw wrong exception type");
865 Assert.Fail ("IList.Insert shouldn't get this far");
869 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
870 public void TestIList_Remove () {
871 int[] myLengthsArray = new int[2] { 3, 5 };
872 int[] myBoundsArray = new int[2] { 2, 3 };
874 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
876 ((IList)myArray).Remove ("can not");
877 Assert.Fail ("IList.Remove should throw");
879 catch (NotSupportedException) {
883 Assert.Fail ("IList.Remove threw wrong exception type");
886 Assert.Fail ("IList.Remove shouldn't get this far");
890 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
891 public void TestIList_RemoveAt () {
892 int[] myLengthsArray = new int[2] { 3, 5 };
893 int[] myBoundsArray = new int[2] { 2, 3 };
895 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
897 ((IList)myArray).RemoveAt (0);
898 Assert.Fail ("IList.RemoveAt should throw");
900 catch (NotSupportedException) {
904 Assert.Fail ("IList.RemoveAt threw wrong exception type");
907 Assert.Fail ("IList.RemoveAt shouldn't get this far");
911 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
912 public void TestIList_Contains () {
913 int[] myLengthsArray = new int[2] { 3, 5 };
914 int[] myBoundsArray = new int[2] { 2, 3 };
916 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
919 bool b = ((IList)myArray).Contains ("23");
920 Assert.Fail ("IList.Contains should throw with multi-dimensional arrays");
922 catch (RankException) {
923 int[] iArr = new int[3] { 1, 2, 3};
924 // check the first and last items
925 Assert.IsTrue (((IList)iArr).Contains (1), "AC01");
926 Assert.IsTrue (((IList)iArr).Contains (3), "AC02");
928 // and one that is definately not there
929 Assert.IsTrue (!((IList)iArr).Contains (42), "AC03");
933 Assert.Fail ("Should not get here");
937 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
938 public void TestIList_IndexOf () {
939 int[] myLengthsArray = new int[2] { 3, 5 };
940 int[] myBoundsArray = new int[2] { 2, 3 };
942 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
945 bool b = ((IList)myArray).Contains ("23");
946 Assert.Fail ("IList.Contains should throw with multi-dimensional arrays");
948 catch (RankException) {
949 int[] iArr = new int[3] { 1, 2, 3};
950 // check the first and last items
951 Assert.AreEqual (0, ((IList)iArr).IndexOf (1), "AD01");
952 Assert.AreEqual (2, ((IList)iArr).IndexOf (3), "AD02");
954 // and one that is definately not there
955 Assert.AreEqual (-1, ((IList)iArr).IndexOf (42), "AD03");
957 catch (Exception e) {
958 Assert.Fail ("Unexpected exception: " + e.ToString());
961 // check that wierd case whem lowerbound is Int32.MinValue,
962 // so that IndexOf() needs to return Int32.MaxValue when it cannot find the object
963 int[] myLengthArray = new int[1] { 3 };
964 int[] myBoundArray = new int[1] { Int32.MinValue };
965 Array myExtremeArray=Array.CreateInstance ( typeof(String), myLengthArray, myBoundArray );
966 Assert.AreEqual (Int32.MaxValue, ((IList)myExtremeArray).IndexOf (42), "AD04");
971 public void TestGetLength() {
973 bool errorThrown = false;
975 char[] c1 = {'a', 'b', 'c'};
977 } catch (IndexOutOfRangeException) {
980 Assert.IsTrue (errorThrown, "#H01");
983 bool errorThrown = false;
985 char[] c1 = {'a', 'b', 'c'};
987 } catch (IndexOutOfRangeException) {
990 Assert.IsTrue (errorThrown, "#H02");
993 char[] c2 = new Char[5];
994 Assert.AreEqual (5, c2.GetLength(0), "#H03");
996 char[,] c3 = new Char[6,7];
997 Assert.AreEqual (6, c3.GetLength(0), "#H04");
998 Assert.AreEqual (7, c3.GetLength(1), "#H05");
1002 public void TestGetLowerBound() {
1004 bool errorThrown = false;
1006 char[] c = {'a', 'b', 'c'};
1007 c.GetLowerBound(-1);
1008 } catch (IndexOutOfRangeException) {
1011 Assert.IsTrue (errorThrown, "#H31");
1014 bool errorThrown = false;
1016 char[] c = {'a', 'b', 'c'};
1018 } catch (IndexOutOfRangeException) {
1021 Assert.IsTrue (errorThrown, "#H32");
1024 char[] c1 = new Char[5];
1025 Assert.AreEqual (0, c1.GetLowerBound(0), "#H33");
1027 char[,] c2 = new Char[4,4];
1028 Assert.AreEqual (0, c2.GetLowerBound(0), "#H34");
1029 Assert.AreEqual (0, c2.GetLowerBound(1), "#H35");
1033 public void TestGetUpperBound() {
1035 bool errorThrown = false;
1037 char[] c = {'a', 'b', 'c'};
1038 c.GetUpperBound(-1);
1039 } catch (IndexOutOfRangeException) {
1042 Assert.IsTrue (errorThrown, "#H61");
1045 bool errorThrown = false;
1047 char[] c = {'a', 'b', 'c'};
1049 } catch (IndexOutOfRangeException) {
1052 Assert.IsTrue (errorThrown, "#H62");
1055 char[] c1 = new Char[5];
1056 Assert.AreEqual (4, c1.GetUpperBound(0), "#H63");
1058 char[,] c2 = new Char[4,6];
1059 Assert.AreEqual (3, c2.GetUpperBound(0), "#H64");
1060 Assert.AreEqual (5, c2.GetUpperBound(1), "#H65");
1064 public void TestGetValue1() {
1066 bool errorThrown = false;
1068 char[,] c = new Char[2,2];
1070 } catch (ArgumentException) {
1073 Assert.IsTrue (errorThrown, "#I01");
1076 bool errorThrown = false;
1078 char[] c = {'a', 'b', 'c'};
1080 } catch (IndexOutOfRangeException) {
1083 Assert.IsTrue (errorThrown, "#I02");
1086 bool errorThrown = false;
1088 char[] c = {'a', 'b', 'c'};
1090 } catch (IndexOutOfRangeException) {
1093 Assert.IsTrue (errorThrown, "#I03");
1096 char[] c1 = {'a', 'b', 'c', 'd'};
1097 for (int i = 0; i < c1.Length; i++) {
1098 Assert.AreEqual (c1[i], c1.GetValue(i), "#I04(" + i + ")");
1103 public void TestGetValue2() {
1105 bool errorThrown = false;
1107 char[] c = new Char[2];
1109 } catch (ArgumentException) {
1112 Assert.IsTrue (errorThrown, "#I21");
1115 bool errorThrown = false;
1117 char[,] c = new Char[2,2];
1119 } catch (IndexOutOfRangeException) {
1122 Assert.IsTrue (errorThrown, "#I22");
1125 bool errorThrown = false;
1127 char[,] c = new Char[2,2];
1129 } catch (IndexOutOfRangeException) {
1132 Assert.IsTrue (errorThrown, "#I23");
1135 char[,] c1 = new Char[4,6];
1136 for (int i = 0; i < 24; i++) {
1139 c1[first,second] = (char)(((int)'a')+i);
1141 for (int i = 0; i < c1.GetLength(0); i++) {
1142 for (int j = 0; j < c1.GetLength(1); j++) {
1143 Assert.AreEqual (c1[i, j], c1.GetValue(i, j), "#I24(" + i + "," + j + ")");
1149 public void TestGetValue3() {
1151 bool errorThrown = false;
1153 char[] c = new Char[2];
1155 } catch (ArgumentException) {
1158 Assert.IsTrue (errorThrown, "#I41");
1161 bool errorThrown = false;
1163 char[,,] c = new Char[2,2,2];
1164 c.GetValue(-1, 1, 1);
1165 } catch (IndexOutOfRangeException) {
1168 Assert.IsTrue (errorThrown, "#I42");
1171 bool errorThrown = false;
1173 char[,,] c = new Char[2,2,2];
1175 } catch (IndexOutOfRangeException) {
1178 Assert.IsTrue (errorThrown, "#I43");
1181 char[,,] c1 = new Char[4,2,3];
1182 for (int i = 0; i < 24; i++) {
1184 int remains = i % 6;
1185 int second = remains / 3;
1186 int third = remains % 3;
1187 c1[first,second, third] = (char)(((int)'a')+i);
1189 for (int i = 0; i < c1.GetLength(0); i++) {
1190 for (int j = 0; j < c1.GetLength(1); j++) {
1191 for (int k = 0; k < c1.GetLength(2); k++) {
1192 Assert.AreEqual (c1[i, j, k], c1.GetValue(i, j, k), "#I44(" + i + "," + j + ")");
1200 [ExpectedException (typeof (ArgumentNullException))]
1202 [ExpectedException (typeof (NullReferenceException))]
1204 public void TestGetValueLongArray ()
1206 char[] c = new Char[2];
1207 c.GetValue((long [])null);
1211 public void TestGetValueN() {
1213 bool errorThrown = false;
1215 char[] c = new Char[2];
1216 c.GetValue((int [])null);
1217 } catch (ArgumentNullException) {
1220 Assert.IsTrue (errorThrown, "#I61a");
1223 bool errorThrown = false;
1225 char[] c = new Char[2];
1226 int[] coords = {1, 1};
1228 } catch (ArgumentException) {
1231 Assert.IsTrue (errorThrown, "#I62");
1234 bool errorThrown = false;
1236 char[,] c = new Char[2,2];
1237 int[] coords = {-1, 1};
1239 } catch (IndexOutOfRangeException) {
1242 Assert.IsTrue (errorThrown, "#I63");
1245 bool errorThrown = false;
1247 char[,] c = new Char[2,2];
1248 int[] coords = {4, 1};
1250 } catch (IndexOutOfRangeException) {
1253 Assert.IsTrue (errorThrown, "#I64");
1256 char[,] c1 = new Char[4,6];
1257 for (int i = 0; i < 24; i++) {
1260 c1[first,second] = (char)(((int)'a')+i);
1262 for (int i = 0; i < c1.GetLength(0); i++) {
1263 for (int j = 0; j < c1.GetLength(1); j++) {
1264 int[] coords = {i, j};
1265 Assert.AreEqual (c1[i, j], c1.GetValue(coords), "#I65(" + i + "," + j + ")");
1271 public void TestIndexOf1() {
1273 bool errorThrown = false;
1275 Array.IndexOf(null, "huh?");
1276 } catch (ArgumentNullException) {
1279 Assert.IsTrue (errorThrown, "#J01");
1282 bool errorThrown = false;
1284 char[,] c = new Char[2,2];
1285 Array.IndexOf(c, "huh?");
1286 } catch (RankException) {
1289 Assert.IsTrue (errorThrown, "#J02");
1292 String[] s1 = {"this", "is", "a", "test"};
1293 Assert.AreEqual (-1, Array.IndexOf(s1, null), "#J03");
1294 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing"), "#J04");
1295 Assert.AreEqual (0, Array.IndexOf(s1, "this"), "#J05");
1296 Assert.AreEqual (3, Array.IndexOf(s1, "test"), "#J06");
1300 public void TestIndexOf2() {
1302 bool errorThrown = false;
1304 Array.IndexOf(null, "huh?", 0);
1305 } catch (ArgumentNullException) {
1308 Assert.IsTrue (errorThrown, "#J21");
1311 bool errorThrown = false;
1313 char[,] c = new Char[2,2];
1314 Array.IndexOf(c, "huh?", 0);
1315 } catch (RankException) {
1318 Assert.IsTrue (errorThrown, "#J22");
1321 bool errorThrown = false;
1323 char[] c = new Char[2];
1324 Array.IndexOf(c, "huh?", 3);
1325 } catch (ArgumentOutOfRangeException) {
1328 Assert.IsTrue (errorThrown, "#J23");
1331 String[] s1 = {"this", "is", "really", "a", "test"};
1332 Assert.AreEqual (-1, Array.IndexOf(s1, null, 1), "#J24");
1333 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing", 1), "#J25");
1334 Assert.AreEqual (-1, Array.IndexOf(s1, "this", 1), "#J26");
1335 Assert.AreEqual (1, Array.IndexOf(s1, "is", 1), "#J27");
1336 Assert.AreEqual (4, Array.IndexOf(s1, "test", 1), "#J28");
1340 public void TestIndexOf3() {
1342 bool errorThrown = false;
1344 Array.IndexOf(null, "huh?", 0, 1);
1345 } catch (ArgumentNullException) {
1348 Assert.IsTrue (errorThrown, "#J41");
1351 bool errorThrown = false;
1353 char[,] c = new Char[2,2];
1354 Array.IndexOf(c, "huh?", 0, 1);
1355 } catch (RankException) {
1358 Assert.IsTrue (errorThrown, "#J42");
1361 bool errorThrown = false;
1363 char[] c = new Char[2];
1364 Array.IndexOf(c, "huh?", 3, 1);
1365 } catch (ArgumentOutOfRangeException) {
1368 Assert.IsTrue (errorThrown, "#J43");
1371 bool errorThrown = false;
1373 char[] c = new Char[2];
1374 Array.IndexOf(c, "huh?", 0, 5);
1375 } catch (ArgumentOutOfRangeException) {
1378 Assert.IsTrue (errorThrown, "#J44");
1381 String[] s1 = {"this", "is", "really", "a", "test"};
1382 Assert.AreEqual (-1, Array.IndexOf(s1, null, 1, 3), "#J45");
1383 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing", 1, 3), "#J46");
1384 Assert.AreEqual (-1, Array.IndexOf(s1, "this", 1, 3), "#J47");
1385 Assert.AreEqual (1, Array.IndexOf(s1, "is", 1, 3), "#J48");
1386 Assert.AreEqual (-1, Array.IndexOf(s1, "test", 1, 3), "#J49");
1387 Assert.AreEqual (3, Array.IndexOf(s1, "a", 1, 3), "#J50");
1391 public void TestIndexOf_CustomEqual ()
1393 DataEqual[] test = new DataEqual [] { new DataEqual () };
1394 Assert.AreEqual (0, Array.IndexOf (test, "asdfas", 0));
1396 IList array = (IList)test;
1397 Assert.AreEqual (0, array.IndexOf ("asdfas"));
1401 public void TestLastIndexOf1() {
1403 bool errorThrown = false;
1405 Array.LastIndexOf(null, "huh?");
1406 } catch (ArgumentNullException) {
1409 Assert.IsTrue (errorThrown, "#K01");
1412 bool errorThrown = false;
1414 char[,] c = new Char[2,2];
1415 Array.LastIndexOf(c, "huh?");
1416 } catch (RankException) {
1419 Assert.IsTrue (errorThrown, "#K02");
1422 String[] s1 = {"this", "is", "a", "a", "test"};
1423 Assert.AreEqual (-1, Array.LastIndexOf(s1, null), "#K03");
1424 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing"), "#K04");
1425 Assert.AreEqual (0, Array.LastIndexOf(s1, "this"), "#K05");
1426 Assert.AreEqual (4, Array.LastIndexOf(s1, "test"), "#K06");
1427 Assert.AreEqual (3, Array.LastIndexOf(s1, "a"), "#K07");
1429 Assert.AreEqual (-1, Array.LastIndexOf (new String [0], "foo"));
1433 public void TestLastIndexOf2() {
1435 bool errorThrown = false;
1437 Array.LastIndexOf(null, "huh?", 0);
1438 } catch (ArgumentNullException) {
1441 Assert.IsTrue (errorThrown, "#K21");
1444 bool errorThrown = false;
1446 char[,] c = new Char[2,2];
1447 Array.LastIndexOf(c, "huh?", 0);
1448 } catch (RankException) {
1451 Assert.IsTrue (errorThrown, "#K22");
1454 bool errorThrown = false;
1456 char[] c = new Char[2];
1457 Array.LastIndexOf(c, "huh?", 3);
1458 } catch (ArgumentOutOfRangeException) {
1461 Assert.IsTrue (errorThrown, "#K23");
1464 String[] s1 = {"this", "is", "really", "a", "test"};
1465 Assert.AreEqual (-1, Array.LastIndexOf(s1, null, 3), "#K24");
1466 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing", 3), "#K25");
1467 Assert.AreEqual (-1, Array.LastIndexOf(s1, "test", 3), "#K26");
1468 Assert.AreEqual (3, Array.LastIndexOf(s1, "a", 3), "#K27");
1469 Assert.AreEqual (0, Array.LastIndexOf(s1, "this", 3), "#K28");
1473 public void TestLastIndexOf3() {
1475 bool errorThrown = false;
1477 Array.LastIndexOf(null, "huh?", 0, 1);
1478 } catch (ArgumentNullException) {
1481 Assert.IsTrue (errorThrown, "#K41");
1484 bool errorThrown = false;
1486 char[,] c = new Char[2,2];
1487 Array.LastIndexOf(c, "huh?", 0, 1);
1488 } catch (RankException) {
1491 Assert.IsTrue (errorThrown, "#K42");
1494 bool errorThrown = false;
1496 char[] c = new Char[2];
1497 Array.LastIndexOf(c, "huh?", 3, 1);
1498 } catch (ArgumentOutOfRangeException) {
1501 Assert.IsTrue (errorThrown, "#K43");
1504 bool errorThrown = false;
1506 char[] c = new Char[2];
1507 Array.LastIndexOf(c, "huh?", 0, 5);
1508 } catch (ArgumentOutOfRangeException) {
1511 Assert.IsTrue (errorThrown, "#K44");
1514 String[] s1 = {"this", "is", "really", "a", "test"};
1515 Assert.AreEqual (-1, Array.LastIndexOf(s1, null, 3, 3), "#K45");
1516 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing", 3, 3), "#K46");
1517 Assert.AreEqual (-1, Array.LastIndexOf(s1, "this", 3, 3), "#K47");
1518 Assert.AreEqual (1, Array.LastIndexOf(s1, "is", 3, 3), "#K48");
1519 Assert.AreEqual (-1, Array.LastIndexOf(s1, "test", 3, 3), "#K49");
1520 Assert.AreEqual (3, Array.LastIndexOf(s1, "a", 3, 3), "#K50");
1524 public void TestLastIndexOf4 ()
1526 short [] a = new short [] { 19, 238, 317, 6, 565, 0, -52, 60, -563, 753, 238, 238};
1528 Array.LastIndexOf (a, (object)16, -1);
1529 NUnit.Framework.Assert.Fail ("#1");
1530 } catch (ArgumentOutOfRangeException) { }
1534 Array.LastIndexOf<short> (a, 16, -1);
1535 NUnit.Framework.Assert.Fail ("#2");
1536 } catch (ArgumentOutOfRangeException) { }
1541 public void TestLastIndexOf5 ()
1543 char [] a = new char [] {'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'j', 'i', 'h'};
1548 for (int i = a.Length - 1; i >= 0 ; i--) {
1550 retval = Array.LastIndexOf(a, a [i], i, i + 1);
1554 Assert.IsTrue (!error);
1558 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1559 public void LastIndexOf_StartIndexOverflow ()
1561 // legal - no exception
1562 byte[] array = new byte [16];
1563 Array.LastIndexOf (array, this, Int32.MaxValue, 1);
1567 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1568 public void LastIndexOf_CountOverflow ()
1570 // legal - no exception
1571 byte[] array = new byte [16];
1572 Array.LastIndexOf (array, this, 1, Int32.MaxValue);
1576 public void LastIndexOf_0LengthArray ()
1578 Array array = Array.CreateInstance (typeof (char), 0);
1579 int idx = Array.LastIndexOf (array, (object) null, -1, 0);
1580 Assert.IsTrue (idx == -1, "#01");
1581 idx = Array.LastIndexOf (array, (object) null, -1, 10);
1582 Assert.IsTrue (idx == -1, "#02");
1583 idx = Array.LastIndexOf (array, (object) null, -100, 10);
1584 Assert.IsTrue (idx == -1, "#02");
1586 array = Array.CreateInstance (typeof (char), 1);
1588 Array.LastIndexOf (array, (object) null, -1, 0);
1589 Assert.Fail ("#04");
1590 } catch (ArgumentOutOfRangeException e) {
1593 Array.LastIndexOf (array, (object) null, -1, 10);
1594 Assert.Fail ("#05");
1595 } catch (ArgumentOutOfRangeException e) {
1598 Array.LastIndexOf (array, (object) null, -100, 10);
1599 Assert.Fail ("#06");
1600 } catch (ArgumentOutOfRangeException e) {
1605 public void TestReverse() {
1607 bool errorThrown = false;
1609 Array.Reverse(null);
1610 } catch (ArgumentNullException) {
1613 Assert.IsTrue (errorThrown, "#L01");
1616 bool errorThrown = false;
1618 char[,] c = new Char[2,2];
1620 } catch (RankException) {
1623 Assert.IsTrue (errorThrown, "#L02");
1626 char[] c1 = {'a', 'b', 'c', 'd'};
1628 Assert.AreEqual ('d', c1[0], "#L03");
1629 Assert.AreEqual ('c', c1[1], "#L04");
1630 Assert.AreEqual ('b', c1[2], "#L05");
1631 Assert.AreEqual ('a', c1[3], "#L06");
1634 bool errorThrown = false;
1636 Array.Reverse(null, 0, 0);
1637 } catch (ArgumentNullException) {
1640 Assert.IsTrue (errorThrown, "#L07");
1643 bool errorThrown = false;
1645 char[,] c = new Char[2,2];
1646 Array.Reverse(c, 0, 0);
1647 } catch (RankException) {
1650 Assert.IsTrue (errorThrown, "#L08");
1653 //bool errorThrown = false;
1655 // char[] c = new Char[2];
1656 // Array.Reverse(c, 0, 3);
1657 //} catch (ArgumentOutOfRangeException) {
1658 // errorThrown = true;
1660 //Assert.IsTrue (errorThrown, "#L09");
1663 //bool errorThrown = false;
1665 // char[] c = new Char[2];
1666 // Array.Reverse(c, 3, 0);
1667 //} catch (ArgumentOutOfRangeException) {
1668 // errorThrown = true;
1670 //Assert.IsTrue (errorThrown, "#L10");
1673 char[] c2 = { 'a', 'b', 'c', 'd'};
1674 Array.Reverse(c2, 1, 2);
1675 Assert.AreEqual ('a', c2[0], "#L11");
1676 Assert.AreEqual ('c', c2[1], "#L12");
1677 Assert.AreEqual ('b', c2[2], "#L13");
1678 Assert.AreEqual ('d', c2[3], "#L14");
1682 public void TestSetValue1() {
1684 bool errorThrown = false;
1686 char[,] c = new Char[2,2];
1687 c.SetValue("buh", 1);
1688 } catch (ArgumentException) {
1691 Assert.IsTrue (errorThrown, "#M01");
1694 bool errorThrown = false;
1696 char[] c = {'a', 'b', 'c'};
1697 c.SetValue("buh", -1);
1698 } catch (IndexOutOfRangeException) {
1701 Assert.IsTrue (errorThrown, "#M02");
1704 bool errorThrown = false;
1706 char[] c = {'a', 'b', 'c'};
1707 c.SetValue("buh", 4);
1708 } catch (IndexOutOfRangeException) {
1711 Assert.IsTrue (errorThrown, "#M03");
1714 char[] c1 = {'a', 'b', 'c', 'd'};
1715 char[] c2 = new char[4];
1716 for (int i = 0; i < c1.Length; i++) {
1717 c2.SetValue(c1[i], i);
1719 for (int i = 0; i < c1.Length; i++) {
1720 Assert.AreEqual (c1[i], c2[i], "#M04(" + i + ")");
1723 int[] c3 = { 1, 2, 3 };
1724 long[] c4 = new long [3];
1726 for (int i = 0; i < c3.Length; i++)
1727 c4.SetValue (c3 [i], i);
1731 } catch (Exception e) {
1732 Assert.Fail ("c3.CopyTo(): e=" + e);
1734 for (int i = 0; i < c3.Length; i++)
1735 Assert.IsTrue (c3[i] == c4[i], "#M05(" + i + ")");
1737 Object[] c5 = new Object [3];
1738 long[] c6 = new long [3];
1742 } catch (Exception e) {
1743 Assert.Fail ("c4.CopyTo(): e=" + e);
1748 } catch (Exception e) {
1749 Assert.Fail ("c5.CopyTo(): e=" + e);
1751 // for (int i = 0; i < c5.Length; i++)
1752 // Assert.IsTrue (c5[i] == c6[i], "#M06(" + i + ")");
1756 public void TestSetValue2() {
1758 bool errorThrown = false;
1760 char[] c = new Char[2];
1761 c.SetValue("buh", 1,1);
1762 } catch (ArgumentException) {
1765 Assert.IsTrue (errorThrown, "#M21");
1768 bool errorThrown = false;
1770 char[,] c = new Char[2,2];
1771 c.SetValue("buh", -1, 1);
1772 } catch (IndexOutOfRangeException) {
1775 Assert.IsTrue (errorThrown, "#M22");
1778 bool errorThrown = false;
1780 char[,] c = new Char[2,2];
1781 c.SetValue("buh", 4,1);
1782 } catch (IndexOutOfRangeException) {
1785 Assert.IsTrue (errorThrown, "#M23");
1788 char[,] c1 = new Char[4,6];
1789 char[,] c2 = new Char[4,6];
1790 for (int i = 0; i < 24; i++) {
1793 c1[first,second] = (char)(((int)'a')+i);
1794 c2.SetValue(c1[first,second], first, second);
1796 for (int i = 0; i < c1.GetLength(0); i++) {
1797 for (int j = 0; j < c1.GetLength(1); j++) {
1798 Assert.AreEqual (c1[i, j], c2[i, j], "#M24(" + i + "," + j + ")");
1804 public void TestSetValue3() {
1806 bool errorThrown = false;
1808 char[] c = new Char[2];
1809 c.SetValue("buh", 1,1,1);
1810 } catch (ArgumentException) {
1813 Assert.IsTrue (errorThrown, "#M41");
1816 bool errorThrown = false;
1818 char[,,] c = new Char[2,2,2];
1819 c.SetValue("buh", -1, 1, 1);
1820 } catch (IndexOutOfRangeException) {
1823 Assert.IsTrue (errorThrown, "#M42");
1826 bool errorThrown = false;
1828 char[,,] c = new Char[2,2,2];
1829 c.SetValue("buh", 4,1,1);
1830 } catch (IndexOutOfRangeException) {
1833 Assert.IsTrue (errorThrown, "#M43");
1836 char[,,] c1 = new Char[4,2,3];
1837 char[,,] c2 = new Char[4,2,3];
1838 for (int i = 0; i < 24; i++) {
1840 int remains = i % 6;
1841 int second = remains / 3;
1842 int third = remains % 3;
1843 c1[first,second, third] = (char)(((int)'a')+i);
1844 c2.SetValue(c1[first, second, third], first, second, third);
1846 for (int i = 0; i < c1.GetLength(0); i++) {
1847 for (int j = 0; j < c1.GetLength(1); j++) {
1848 for (int k = 0; k < c1.GetLength(2); k++) {
1849 Assert.AreEqual (c1[i, j, k], c2[i, j, k], "#M44(" + i + "," + j + " )");
1857 [ExpectedException (typeof (ArgumentNullException))]
1859 [ExpectedException (typeof (NullReferenceException))]
1861 public void TestSetValueLongArray ()
1863 char[] c = new Char[2];
1864 c.SetValue("buh", (long [])null);
1868 public void TestSetValueN() {
1870 bool errorThrown = false;
1872 char[] c = new Char[2];
1873 c.SetValue("buh", (int [])null);
1874 } catch (ArgumentNullException) {
1877 Assert.IsTrue (errorThrown, "#M61a");
1880 bool errorThrown = false;
1882 char[] c = new Char[2];
1883 int[] coords = {1, 1};
1884 c.SetValue("buh", coords);
1885 } catch (ArgumentException) {
1888 Assert.IsTrue (errorThrown, "#M62");
1891 bool errorThrown = false;
1893 char[,] c = new Char[2,2];
1894 int[] coords = {-1, 1};
1895 c.SetValue("buh", coords);
1896 } catch (IndexOutOfRangeException) {
1899 Assert.IsTrue (errorThrown, "#M63");
1902 bool errorThrown = false;
1904 char[,] c = new Char[2,2];
1905 int[] coords = {4, 1};
1906 c.SetValue("buh", coords);
1907 } catch (IndexOutOfRangeException) {
1910 Assert.IsTrue (errorThrown, "#M64");
1913 char[,] c1 = new Char[4,6];
1914 char[,] c2 = new Char[4,6];
1915 for (int i = 0; i < 24; i++) {
1918 c1[first,second] = (char)(((int)'a')+i);
1919 int[] coords = {first, second};
1920 c2.SetValue(c1[first,second], coords);
1922 for (int i = 0; i < c1.GetLength(0); i++) {
1923 for (int j = 0; j < c1.GetLength(1); j++) {
1924 Assert.AreEqual (c1[i, j], c2[i, j], "#M65(" + i + "," + j + ")");
1930 public void TestSetValue4() {
1932 int[] c1 = { 1, 2, 3 };
1933 long[] c2 = new long [3];
1935 for (int i = 0; i < c1.Length; i++)
1936 c2.SetValue (c1 [i], i);
1938 for (int i = 0; i < c1.Length; i++) {
1939 Assert.IsTrue (c1[i] == c2[i], "#M81(" + i + ")");
1940 Assert.AreEqual (typeof (long), c2[i].GetType (), "#M82(" + i + ")");
1944 long[] c1 = { 1, 2, 3 };
1945 int[] c2 = new int [3];
1946 bool errorThrown = false;
1948 c2.SetValue (c1 [0], 0);
1949 } catch (ArgumentException) {
1952 Assert.IsTrue (errorThrown, "#M83");
1955 int[] c1 = { 1, 2, 3 };
1956 Object[] c2 = new Object [3];
1958 for (int i = 0; i < c1.Length; i++)
1959 c2.SetValue (c1 [i], i);
1961 for (int i = 0; i < c1.Length; i++)
1962 Assert.AreEqual (c1[i], Convert.ToInt32 (c2[i]), "#M84(" + i + ")");
1965 Object[] c1 = new Object [3];
1966 Object[] c2 = new Object [3];
1967 c1[0] = new Object ();
1969 for (int i = 0; i < c1.Length; i++)
1970 c2.SetValue (c1 [i], i);
1972 for (int i = 0; i < c1.Length; i++)
1973 Assert.AreEqual (c1[i], c2[i], "#M85(" + i + ")");
1976 Object[] c1 = new Object [3];
1977 string[] c2 = new String [3];
1978 string test = "hello";
1981 c2.SetValue (c1 [0], 0);
1982 Assert.AreEqual (c1[0], c2[0], "#M86");
1983 Assert.AreEqual ("hello", c2[0], "#M87");
1986 char[] c1 = { 'a', 'b', 'c' };
1987 string[] c2 = new string [3];
1989 c2.SetValue (c1 [0], 0);
1990 Assert.Fail ("#M88");
1991 } catch (InvalidCastException) {}
1994 Single[] c1 = { 1.2F, 2.3F, 3.4F, 4.5F };
1995 long[] c2 = new long [3];
1997 c2.SetValue (c1 [0], 0);
1998 Assert.Fail ("#M89");
1999 } catch (ArgumentException) {}
2027 UInt32 v11 = 235354;
2028 UInt64 v12 = 234552;
2030 Object[] va1 = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12 };
2031 Object[] va2 = { "true", "1", "a", "-1.2", "-32", "-234", "-34523", "-1",
2032 "-4.8F", "24234", "235354", "234552" };
2034 Object[][] vt = { va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1 };
2037 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2038 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
2039 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
2040 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
2041 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1,
2042 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1,
2043 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,
2044 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1,
2045 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1,
2046 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
2047 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0,
2048 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0
2053 for (int i = 0; i < types.Length; i++) {
2054 for (int j = 0; j < types.Length; j++) {
2055 Array array = Array.CreateInstance (types [j], 2);
2057 Object value = vt[j][i];
2059 bool errorThrown = false;
2061 array.SetValue (value, 0);
2062 } catch (ArgumentException) {
2066 int ex_index = (i * types.Length) + j;
2068 Assert.AreEqual (errorThrown, arg_ex [ex_index] == 1, "#M90(" + types [i] + "," + types [j] + ")");
2072 for (int i = 0; i < types.Length; i++) {
2073 String[] array = new String [2];
2075 Object value = va1 [i];
2077 bool errorThrown = false;
2079 array.SetValue (value, 0);
2080 } catch (InvalidCastException) {
2084 Assert.IsTrue (errorThrown, "#M91(" + types [i] + ")");
2087 for (int i = 0; i < types.Length; i++) {
2088 Array array = Array.CreateInstance (types [i], 2);
2090 Object value = va2 [i];
2092 bool errorThrown = false;
2094 array.SetValue (value, 0);
2095 } catch (InvalidCastException) {
2099 Assert.IsTrue (errorThrown, "#M92(" + types [i] + ")");
2102 for (int i = 0; i < types.Length; i++) {
2103 Array array = Array.CreateInstance (types [i], 2);
2105 Object value = null;
2107 bool errorThrown = false;
2109 array.SetValue (value, 0);
2110 } catch (InvalidCastException) {
2114 Assert.IsTrue (!errorThrown, "#M93(" + types [i] + ")");
2119 for (int i = 0; i < types.Length; i++) {
2120 for (int j = 0; j < types.Length; j++) {
2121 Array source = Array.CreateInstance (types [i], 2);
2122 Array array = Array.CreateInstance (types [j], 2);
2124 source.SetValue (vt[j][i], 0);
2125 source.SetValue (vt[j][i], 1);
2127 bool errorThrown = false;
2129 Array.Copy (source, array, 2);
2130 } catch (ArrayTypeMismatchException) {
2134 int ex_index = (i * types.Length) + j;
2136 Assert.AreEqual (errorThrown, arg_ex [ex_index] == 1, "#M94(" + types [i] + "," + types [j] + ")");
2140 for (int i = 0; i < types.Length; i++) {
2141 Array source = Array.CreateInstance (types [i], 2);
2142 String[] array = new String [2];
2144 source.SetValue (va1 [i], 0);
2145 source.SetValue (va1 [i], 1);
2147 bool errorThrown = false;
2149 Array.Copy (source, array, 2);
2150 } catch (ArrayTypeMismatchException) {
2154 Assert.IsTrue (errorThrown, "#M95(" + types [i] + ")");
2157 for (int i = 0; i < types.Length; i++) {
2158 String[] source = new String [2];
2159 Array array = Array.CreateInstance (types [i], 2);
2161 source.SetValue (va2 [i], 0);
2162 source.SetValue (va2 [i], 1);
2164 bool errorThrown = false;
2166 Array.Copy (source, array, 2);
2167 } catch (ArrayTypeMismatchException) {
2171 Assert.IsTrue (errorThrown, "#M96(" + types [i] + ")");
2177 public void TestSort() {
2179 bool errorThrown = false;
2182 } catch (ArgumentNullException) {
2185 Assert.IsTrue (errorThrown, "#N01");
2188 bool errorThrown = false;
2190 Array.Sort(null, 0, 1);
2191 } catch (ArgumentNullException) {
2194 Assert.IsTrue (errorThrown, "#N02");
2197 bool errorThrown = false;
2199 char[] c1 = new Char[2];
2200 Array.Sort(null, c1);
2201 } catch (ArgumentNullException) {
2204 Assert.IsTrue (errorThrown, "#N03");
2207 bool errorThrown = false;
2209 char[] c1 = new Char[2];
2210 Array.Sort(null, c1, 0, 1);
2211 } catch (ArgumentNullException) {
2214 Assert.IsTrue (errorThrown, "#N04");
2218 char[] arr = {'d', 'b', 'f', 'e', 'a', 'c'};
2221 Array.Sort (null, 0, 1);
2222 Assert.Fail ("#N" + tc.ToString ());
2224 catch (ArgumentException) {}
2225 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2229 Array.Sort (arr, -1, 3);
2230 Assert.Fail ("#N" + tc.ToString ());
2232 catch (ArgumentException) {}
2233 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2237 Array.Sort (arr, 1, -3);
2238 Assert.Fail ("#N" + tc.ToString ());
2240 catch (ArgumentException) {}
2241 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2245 Array.Sort (arr, arr.Length, arr.Length + 2);
2246 Assert.Fail ("#N" + tc.ToString ());
2248 catch (ArgumentException) {}
2249 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2252 // note: null second array => just sort first array
2253 char[] starter = {'d', 'b', 'f', 'e', 'a', 'c'};
2254 int[] starter1 = {1,2,3,4,5,6};
2256 char[] c1 = (char[])starter.Clone();
2258 Assert.AreEqual ('a', c1[0], "#N21");
2259 Assert.AreEqual ('b', c1[1], "#N22");
2260 Assert.AreEqual ('c', c1[2], "#N23");
2261 Assert.AreEqual ('d', c1[3], "#N24");
2262 Assert.AreEqual ('e', c1[4], "#N25");
2263 Assert.AreEqual ('f', c1[5], "#N26");
2266 char[] c1 = (char[])starter.Clone();
2267 int[] i1 = (int[])starter1.Clone();
2269 Assert.AreEqual ('a', c1[0], "#N41");
2270 Assert.AreEqual ('b', c1[1], "#N42");
2271 Assert.AreEqual ('c', c1[2], "#N43");
2272 Assert.AreEqual ('d', c1[3], "#N44");
2273 Assert.AreEqual ('e', c1[4], "#N45");
2274 Assert.AreEqual ('f', c1[5], "#N46");
2275 Assert.AreEqual (5, i1[0], "#N47");
2276 Assert.AreEqual (2, i1[1], "#N48");
2277 Assert.AreEqual (6, i1[2], "#N49");
2278 Assert.AreEqual (1, i1[3], "#N50");
2279 Assert.AreEqual (4, i1[4], "#N51");
2280 Assert.AreEqual (3, i1[5], "#N52");
2283 char[] c1 = (char[])starter.Clone();
2284 Array.Sort(c1, 1, 4);
2285 Assert.AreEqual ('d', c1[0], "#N61");
2286 Assert.AreEqual ('a', c1[1], "#N62");
2287 Assert.AreEqual ('b', c1[2], "#N63");
2288 Assert.AreEqual ('e', c1[3], "#N64");
2289 Assert.AreEqual ('f', c1[4], "#N65");
2290 Assert.AreEqual ('c', c1[5], "#N66");
2293 char[] c1 = (char[])starter.Clone();
2294 int[] i1 = (int[])starter1.Clone();
2295 Array.Sort(c1, i1, 1, 4);
2296 Assert.AreEqual ('d', c1[0], "#N81");
2297 Assert.AreEqual ('a', c1[1], "#N82");
2298 Assert.AreEqual ('b', c1[2], "#N83");
2299 Assert.AreEqual ('e', c1[3], "#N84");
2300 Assert.AreEqual ('f', c1[4], "#N85");
2301 Assert.AreEqual ('c', c1[5], "#N86");
2302 Assert.AreEqual (1, i1[0], "#N87");
2303 Assert.AreEqual (5, i1[1], "#N88");
2304 Assert.AreEqual (2, i1[2], "#N89");
2305 Assert.AreEqual (4, i1[3], "#N90");
2306 Assert.AreEqual (3, i1[4], "#N91");
2307 Assert.AreEqual (6, i1[5], "#N92");
2312 public void TestInitializeEmpty()
2324 Assert.IsTrue (!catched, "#TI01");
2328 public void TestInitializeInt()
2333 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2335 Assert.AreEqual (a[i], b[i], "#TI02 " + i);
2340 public void TestInitializeDouble()
2342 double[] a = {1.0,2.0,0.0};
2344 double[] b = {1.0,2.0,0.0};
2345 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2347 Assert.AreEqual (a[i], b[i], "#TI03 " + i);
2352 public void TestInitializeFloat()
2354 float[] a = {1.0F,2.0F,0.0F};
2356 float[] b = {1.0F,2.0F,0.0F};
2357 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2359 Assert.AreEqual (a[i], b[i], "#TI04 " + i);
2364 public void TestInitializeChar()
2366 char[] a = {'1','.','0','F','2','.','0','F'};
2368 char[] b = {'1','.','0','F','2','.','0','F'};
2369 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2371 Assert.AreEqual (a[i], b[i], "#TI05 " + i);
2376 public void TestInitializeString()
2378 string[] a = {"hola","adios","menos","mas"};
2380 string[] b = {"hola","adios","menos","mas"};
2381 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2383 Assert.AreEqual (a[i], b[i], "#TI06 " + i);
2388 public void TestInitializeEnum()
2390 enua[] a = {enua.hola,enua.adios,enua.menos,enua.mas};
2392 enua[] b = {enua.hola,enua.adios,enua.menos,enua.mas};
2393 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2395 Assert.AreEqual (a[i], b[i], "#TI07 " + i);
2400 public void TestInitializeIntNI()
2402 int[] a = new int[20];
2406 Assert.AreEqual (b, 0, "#TI08");
2411 public void TestInitializeCharNI()
2413 char[] a = new char[20];
2415 foreach(char b in a)
2417 Assert.AreEqual (b, 0, "#TI09");
2422 public void TestInitializeDoubleNI()
2424 double[] a = new double[20];
2426 foreach(double b in a)
2428 Assert.AreEqual (b, 0.0, "#TI09");
2433 public void TestInitializeStringNI()
2435 string[] a = new string[20];
2437 foreach(string b in a)
2439 Assert.AreEqual (b, null, "#TI10");
2444 public void TestInitializeObjectNI()
2446 object[] a = new object[20];
2448 foreach(object b in a)
2450 Assert.AreEqual (b, null, "#TI11");
2455 public void TestInitializeAClassNI()
2457 AClass[] a = new AClass[20];
2459 foreach(AClass b in a)
2461 Assert.AreEqual (b, null, "#TI12");
2467 public void TestInitializeAStructNI()
2469 AStruct[] a = new AStruct[20];
2471 foreach(AStruct b in a)
2473 Assert.AreEqual (b, new AStruct(), "#TI14");
2478 public void TestInitializeAStruct()
2480 AStruct[] a = new AStruct[3];
2484 AStruct[] b = new AStruct[3];
2487 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2489 Assert.AreEqual (a[i], b[i], "#TI15 " + i);
2494 public void TestInitializeDateTimeNI()
2496 DateTime[] a = new DateTime[20];
2498 foreach(DateTime b in a)
2500 Assert.AreEqual (b, new DateTime(), "#TI16");
2505 [ExpectedException (typeof (ArgumentNullException))]
2506 public void MoreSort1 ()
2508 Array.Sort (null, 0, 1);
2512 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2513 public void MoreSort2 ()
2515 Array.Sort (arrsort, -1, 3);
2519 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2520 public void MoreSort3 ()
2522 Array.Sort (arrsort, 1, -3);
2526 [ExpectedException (typeof (ArgumentException))]
2527 public void MoreSort4 ()
2529 Array.Sort (arrsort, arrsort.Length, arrsort.Length + 2);
2533 [ExpectedException (typeof (RankException))]
2534 public void MoreSort5 ()
2536 char [,] arr = new char [,] {{'a'}, {'b'}};
2537 Array.Sort (arr, 0, 1);
2541 public void MoreSort6 ()
2543 Array.Sort (arrsort, 0, 0);
2547 [ExpectedException (typeof (ArgumentException))]
2548 public void MoreSort7 ()
2550 Array.Sort (arrsort, arrsort.Length - 1, 2);
2554 [ExpectedException (typeof (ArgumentException))]
2555 public void MoreSort8 ()
2557 Array.Sort (arrsort, 0, arrsort.Length + 1);
2561 public void MoreSort9 ()
2563 Array.Sort (arrsort, null, 0, arrsort.Length, null);
2567 [ExpectedException (typeof (InvalidOperationException))]
2568 public void MoreSort10 ()
2570 object [] array = {true, 'k', SByte.MinValue, Byte.MinValue, (short) 2, 634, (long) 436, (float) 1.1, 1.23, "Hello World"};
2571 Array.Sort (array, (IComparer) null);
2574 [Test] // bug #81941
2577 double [] a = new double [2] { 0.9, 0.3 };
2578 uint [] b = new uint [2] { 4, 7 };
2580 Assert.AreEqual (0.3, a [0], "#1");
2581 Assert.AreEqual (0.9, a [1], "#2");
2582 Assert.AreEqual (7, b [0], "#3");
2583 Assert.AreEqual (4, b [1], "#4");
2587 public void ClearJaggedArray ()
2589 byte[][] matrix = new byte [8][];
2590 for (int i=0; i < 8; i++) {
2591 matrix [i] = new byte [8];
2592 for (int j=0; j < 8; j++) {
2596 Array.Clear (matrix, 0, 8);
2597 for (int i=0; i < 8; i++) {
2598 Assert.IsNull (matrix [i], i.ToString ());
2603 public void ClearMultidimentionalArray ()
2605 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2606 Array.Clear (matrix, 0, 2);
2607 Assert.AreEqual (0, matrix [0, 0], "0,0");
2608 Assert.AreEqual (0, matrix [0, 1], "0,1");
2609 Assert.AreEqual (2, matrix [1, 0], "1,0");
2610 Assert.AreEqual (2, matrix [1, 1], "1,1");
2614 [ExpectedException (typeof (IndexOutOfRangeException))]
2615 public void ClearOutsideMultidimentionalArray ()
2617 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2618 Array.Clear (matrix, 0, 5);
2622 [ExpectedException (typeof (IndexOutOfRangeException))]
2623 public void Clear_IndexOverflow ()
2625 byte[] array = new byte [16];
2626 Array.Clear (array, 4, Int32.MaxValue);
2630 [ExpectedException (typeof (IndexOutOfRangeException))]
2631 public void Clear_LengthOverflow ()
2633 byte[] array = new byte [16];
2634 Array.Clear (array, Int32.MaxValue, 4);
2638 [ExpectedException (typeof (ArgumentException))]
2639 public void Copy_SourceIndexOverflow ()
2641 byte[] array = new byte [16];
2642 Array.Copy (array, Int32.MaxValue, array, 8, 8);
2646 [ExpectedException (typeof (ArgumentException))]
2647 public void Copy_DestinationIndexOverflow ()
2649 byte[] array = new byte [16];
2650 Array.Copy (array, 8, array, Int32.MaxValue, 8);
2654 [ExpectedException (typeof (ArgumentException))]
2655 public void Copy_LengthOverflow ()
2657 byte[] array = new byte [16];
2658 Array.Copy (array, 8, array, 8, Int32.MaxValue);
2662 [ExpectedException (typeof (ArgumentException))]
2663 public void Reverse_IndexOverflow ()
2665 byte[] array = new byte [16];
2666 Array.Reverse (array, Int32.MaxValue, 8);
2670 [ExpectedException (typeof (ArgumentException))]
2671 public void Reverse_LengthOverflow ()
2673 byte[] array = new byte [16];
2674 Array.Reverse (array, 8, Int32.MaxValue);
2677 public struct CharX : IComparable {
2680 public CharX (char c)
2685 public int CompareTo (object obj)
2688 return c.CompareTo (((CharX) obj).c);
2690 return c.CompareTo (obj);
2695 public void BinarySearch_ArgPassingOrder ()
2698 // This tests that arguments are passed to the comprer in the correct
2699 // order. The IComparable of the *array* elements must get called, not
2700 // that of the search object.
2702 CharX [] x = { new CharX ('a'), new CharX ('b'), new CharX ('c') };
2703 Assert.AreEqual (1, Array.BinarySearch (x, 'b'));
2706 class Comparer: IComparer {
2708 private bool called = false;
2710 public bool Called {
2712 bool result = called;
2718 public int Compare (object x, object y)
2726 public void BinarySearch1_EmptyList ()
2728 int[] array = new int[0];
2729 Assert.AreEqual (- 1, Array.BinarySearch (array, 0), "BinarySearch");
2733 public void BinarySearch2_EmptyList ()
2735 int[] array = new int[0];
2736 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, 0), "BinarySearch");
2740 public void BinarySearch3_EmptyList ()
2742 Comparer comparer = new Comparer ();
2743 int[] array = new int[0];
2744 Assert.AreEqual (-1, Array.BinarySearch (array, 0, comparer), "BinarySearch");
2745 // bug 77030 - the comparer isn't called for an empty array/list
2746 Assert.IsTrue (!comparer.Called, "Called");
2750 public void BinarySearch4_EmptyList ()
2752 Comparer comparer = new Comparer ();
2753 int[] array = new int[0];
2754 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, comparer), "BinarySearch");
2755 // bug 77030 - the comparer isn't called for an empty array/list
2756 Assert.IsTrue (!comparer.Called, "Called");
2761 [ExpectedException (typeof (ArgumentNullException))]
2762 public void AsReadOnly_NullArray ()
2764 Array.AsReadOnly <int> (null);
2768 public void ReadOnly_Count ()
2770 Assert.AreEqual (10, Array.AsReadOnly (new int [10]).Count);
2774 public void ReadOnly_Contains ()
2776 int[] arr = new int [2];
2779 IList<int> a = Array.AsReadOnly (arr);
2781 Assert.IsTrue (a.Contains (3));
2782 Assert.IsTrue (!a.Contains (6));
2786 public void ReadOnly_IndexOf ()
2788 int[] arr = new int [2];
2791 IList<int> a = Array.AsReadOnly (arr);
2793 Assert.AreEqual (0, a.IndexOf (3));
2794 Assert.AreEqual (1, a.IndexOf (5));
2795 Assert.AreEqual (-1, a.IndexOf (6));
2799 public void ReadOnly_Indexer ()
2801 int[] arr = new int [2];
2804 IList<int> a = Array.AsReadOnly (arr);
2806 Assert.AreEqual (3, a [0]);
2807 Assert.AreEqual (5, a [1]);
2809 /* Check that modifications to the original array are visible */
2811 Assert.AreEqual (6, a [0]);
2815 public void ReadOnly_Enumerator ()
2817 int[] arr = new int [10];
2819 for (int i = 0; i < 10; ++i)
2823 foreach (int i in Array.AsReadOnly (arr))
2826 Assert.AreEqual (45, sum);
2830 public void ReadOnly_CopyTo ()
2832 int[] arr = new int [2];
2835 IList<int> a = Array.AsReadOnly (arr);
2837 int[] arr2 = new int [3];
2840 Assert.AreEqual (0, arr2 [0]);
2841 Assert.AreEqual (3, arr2 [1]);
2842 Assert.AreEqual (5, arr2 [2]);
2846 public void Resize ()
2848 int [] arr = new int [] { 1, 3, 5 };
2849 Array.Resize <int> (ref arr, 3);
2850 Assert.AreEqual (3, arr.Length, "#A1");
2851 Assert.AreEqual (1, arr [0], "#A2");
2852 Assert.AreEqual (3, arr [1], "#A3");
2853 Assert.AreEqual (5, arr [2], "#A4");
2855 Array.Resize <int> (ref arr, 2);
2856 Assert.AreEqual (2, arr.Length, "#B1");
2857 Assert.AreEqual (1, arr [0], "#B2");
2858 Assert.AreEqual (3, arr [1], "#B3");
2860 Array.Resize <int> (ref arr, 4);
2861 Assert.AreEqual (4, arr.Length, "#C1");
2862 Assert.AreEqual (1, arr [0], "#C2");
2863 Assert.AreEqual (3, arr [1], "#C3");
2864 Assert.AreEqual (0, arr [2], "#C4");
2865 Assert.AreEqual (0, arr [3], "#C5");
2869 public void Resize_null ()
2872 Array.Resize (ref arr, 10);
2873 Assert.AreEqual (arr.Length, 10);
2877 public void Test_ContainsAndIndexOf_EquatableItem ()
2879 EquatableClass[] list = new EquatableClass[] {new EquatableClass (0), new EquatableClass (1), new EquatableClass (0)};
2881 Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, list[0]), "#0");
2882 Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, new EquatableClass (0)), "#1");
2883 Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, list[0]), "#2");
2884 Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, new EquatableClass (0)), "#3");
2887 public class EquatableClass : IEquatable<EquatableClass>
2890 public EquatableClass (int x)
2895 public bool Equals (EquatableClass other)
2897 return this._x == other._x;
2902 public void AsIList ()
2904 IList<int> arr = new int [10];
2906 Assert.AreEqual (5, arr [0]);
2908 IList<FooStruct> arr2 = new FooStruct [10];
2909 FooStruct s = new FooStruct ();
2914 Assert.AreEqual (11, s.i);
2915 Assert.AreEqual (22, s.j);
2917 IList<string> arr3 = new string [10];
2919 Assert.AreEqual ("ABC", arr3 [5]);
2926 #if !TARGET_JVM // BugBUG: T[] is not yet ICollection<T> under TARGET_JVM
2929 public void ICollectionNull ()
2931 ICollection<object> test;
2933 test = new List<object>();
2934 Assert.AreEqual (test.Contains (null), false, "list<o>");
2936 test = new object[] {};
2937 Assert.AreEqual (test.Contains (null), false, "empty array");
2939 test = new object[] {null};
2940 Assert.AreEqual (test.Contains (null), true, "array with null");
2942 test = new List<object>(test);
2943 Assert.AreEqual (test.Contains (null), true, "List<object> with test");
2945 test = new object[] {new object()};
2946 Assert.AreEqual (test.Contains (null), false, "array with object");
2948 test = new List<object>(test);
2949 Assert.AreEqual (test.Contains (null), false, "array with test");
2951 #endif // TARGET_JVM
2956 enum ByteEnum : byte {}
2957 enum IntEnum : int {}
2960 public void TestByteEnumArrayToByteArray ()
2962 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
2963 byte[] b = new byte[a.Length];
2968 public void TestByteEnumArrayToIntArray ()
2970 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
2971 int[] b = new int[a.Length];
2976 [ExpectedException (typeof (ArrayTypeMismatchException))]
2977 public void TestIntEnumArrayToByteArray ()
2979 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
2980 byte[] b = new byte[a.Length];
2985 public void TestIntEnumArrayToIntArray ()
2987 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
2988 int[] b = new int[a.Length];
2995 [Test] // bug #322248
2996 public void IEnumerator_Reset ()
2998 int[] array = new int[] { 1, 2, 3};
2999 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3000 Assert.IsTrue (e.MoveNext (), "#A1");
3001 Assert.AreEqual (1, e.Current, "#A2");
3002 Assert.IsTrue (e.MoveNext (), "#A3");
3003 Assert.AreEqual (2, e.Current, "#A4");
3007 Assert.IsTrue (e.MoveNext (), "#C1");
3008 Assert.AreEqual (1, e.Current, "#C2");
3012 public void IEnumerator_Current_Finished ()
3014 int[] array = new int[] { 1, 2, 3 };
3015 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3016 Assert.IsTrue (e.MoveNext (), "#A1");
3017 Assert.AreEqual (1, e.Current, "#A2");
3018 Assert.IsTrue (e.MoveNext (), "#A3");
3019 Assert.AreEqual (2, e.Current, "#A4");
3020 Assert.IsTrue (e.MoveNext (), "#A5");
3021 Assert.AreEqual (3, e.Current, "#A6");
3022 Assert.IsTrue (!e.MoveNext (), "#A6");
3025 Assert.Fail ("#B1:" + e.Current);
3026 } catch (InvalidOperationException ex) {
3027 // Enumeration already finished
3028 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3029 Assert.IsNull (ex.InnerException, "#B3");
3030 Assert.IsNotNull (ex.Message, "#B4");
3035 public void IEnumerator_Current_NotStarted ()
3037 int[] array = new int[] { 1, 2, 3 };
3038 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3041 Assert.Fail ("#A1:" + e.Current);
3042 } catch (InvalidOperationException ex) {
3043 // Enumeration has not started. Call MoveNext
3044 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
3045 Assert.IsNull (ex.InnerException, "#A3");
3046 Assert.IsNotNull (ex.Message, "#A4");
3051 public void IEnumerator_Current_Reset ()
3053 int[] array = new int[] { 1, 2, 3 };
3054 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3059 Assert.Fail ("#B1:" + e.Current);
3060 } catch (InvalidOperationException ex) {
3061 // Enumeration has not started. Call MoveNext
3062 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3063 Assert.IsNull (ex.InnerException, "#B3");
3064 Assert.IsNotNull (ex.Message, "#B4");
3068 public void ICollection_IsReadOnly() {
3069 ICollection<string> arr = new string [10];
3071 Assert.IsTrue (arr.IsReadOnly);
3076 [ExpectedException (typeof (NotSupportedException))]
3077 public void ArrayCreateInstanceOfVoid ()
3079 Array.CreateInstance (typeof (void), 42);
3085 [ExpectedException (typeof (NotSupportedException))]
3086 public void ArrayCreateInstanceOfOpenGenericType ()
3088 Array.CreateInstance (typeof (Foo<>), 42);
3092 [ExpectedException (typeof (IndexOutOfRangeException))]
3093 public void ClearNegativeLength ()
3095 Array.Clear (new int [] { 1, 2 }, 0, -1);
3099 [ExpectedException (typeof (ArgumentException))]
3100 public void MultiDimension_IList_setItem ()
3102 IList array = new int [1, 1];
3107 [ExpectedException (typeof (ArgumentException))]
3108 public void MultiDimension_IList_getItem ()
3110 IList array = new int [1, 1];
3111 int a = (int) array [0];
3115 public void SetValue_Nullable () {
3116 Array array = Array.CreateInstance (typeof (int?), 7);
3120 array.SetValue (o, 0);
3121 Assert.AreEqual (42, array.GetValue (0));
3123 array.SetValue (null, 0);
3124 Assert.AreEqual (null, array.GetValue (0));
3129 [ExpectedException (typeof (ArgumentException))]
3130 public void CompareToWithJaggedArray () {
3131 IStructuralComparable a = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }};
3132 IStructuralComparable b = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }};
3133 a.CompareTo (b, Comparer<object>.Default);
3137 [ExpectedException (typeof (ArgumentException))]
3138 public void CompareToWithArrayOfTheWrongKind () {
3139 IStructuralComparable a = new int[] { 1, 2 };
3140 IStructuralComparable b = new double[] { 1, 2 };
3141 a.CompareTo (b, Comparer<object>.Default);
3145 [ExpectedException (typeof (ArgumentException))]
3146 public void CompareToWithNonArrayType () {
3147 IStructuralComparable a = new int[] { 1, 2 };
3148 a.CompareTo (99, Comparer<object>.Default);
3152 [ExpectedException (typeof (ArgumentException))]
3153 public void CompareToWithNonArrayOfDifferentSize () {
3154 IStructuralComparable a = new int[] { 1, 2 };
3155 IStructuralComparable b = new int[] { 1, 2, 3 };
3156 a.CompareTo (b, Comparer<object>.Default);
3160 [ExpectedException (typeof (ArgumentException))]
3161 public void CompareToWithMultiDimArray1 () {
3162 IStructuralComparable a = new int [2,2] { {10, 10 }, { 10, 10 } };
3163 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3164 a.CompareTo (b, Comparer<object>.Default);
3168 [ExpectedException (typeof (ArgumentException))]
3169 public void CompareToWithMultiDimArray2 () {
3170 IStructuralComparable a = new int [2] { 10, 10 };
3171 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3172 a.CompareTo (b, Comparer<object>.Default);
3176 [ExpectedException (typeof (ArgumentException))]
3177 public void CompareToWithMultiDimArray3 () {
3178 IStructuralComparable a = new int [4] { 10, 10, 10, 10 };
3179 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3180 a.CompareTo (b, Comparer<object>.Default);
3184 [ExpectedException (typeof (IndexOutOfRangeException))]
3185 public void CompareToWithBoundedArray1 () {
3186 IStructuralComparable a = new int [2] { 10, 10 };
3187 Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 });
3188 IStructuralComparable b = ab;
3189 ab.SetValue (10, 5);
3190 ab.SetValue (10, 6);
3192 a.CompareTo (b, Comparer<object>.Default);
3196 [ExpectedException (typeof (IndexOutOfRangeException))]
3197 public void CompareToWithBoundedArray2 () {
3198 IStructuralComparable a = new int [2] { 10, 10 };
3199 Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 });
3200 IStructuralComparable b = ab;
3201 ab.SetValue (10, 5);
3202 ab.SetValue (10, 6);
3204 //Yes, CompareTo simply doesn't work with bounded arrays!
3205 b.CompareTo (b, Comparer<object>.Default);
3209 [ExpectedException (typeof (NullReferenceException))]
3210 public void CompareToWithNullComparer () {
3211 IStructuralComparable a = new int[] { 1, 2 };
3212 IStructuralComparable b = new int[] { 1, 2 };
3213 a.CompareTo (b, null);
3217 public void CompareToWithNullArray () {
3218 IStructuralComparable a = new int[] { 1, 2 };
3219 Assert.AreEqual (1, a.CompareTo (null, Comparer<object>.Default));
3223 public void CompareToWithGoodArrays () {
3224 IStructuralComparable a = new int[] { 10, 20 };
3225 Assert.AreEqual (0, a.CompareTo (a, Comparer<object>.Default));
3226 Assert.AreEqual (0, a.CompareTo (new int [] { 10, 20 }, Comparer<object>.Default));
3227 Assert.AreEqual (-1, a.CompareTo (new int [] { 11, 20 }, Comparer<object>.Default));
3228 Assert.AreEqual (-1, a.CompareTo (new int [] { 10, 21 }, Comparer<object>.Default));
3229 Assert.AreEqual (1, a.CompareTo (new int [] { 9, 20 }, Comparer<object>.Default));
3230 Assert.AreEqual (1, a.CompareTo (new int [] { 10, 19 }, Comparer<object>.Default));
3234 public void IStructuralEquatable_Equals ()
3236 IStructuralEquatable array = new int[] {1, 2, 3};
3237 IStructuralEquatable array2 = new int[] {1, 2, 3};
3238 Assert.AreEqual (false, array.Equals (null, null));
3239 Assert.AreEqual (true, array.Equals (array, null));
3240 Assert.AreEqual (true, array.Equals (array2, EqualityComparer<int>.Default));
3244 [ExpectedException (typeof (NullReferenceException))]
3245 public void IStructuralEquatable_Equals_NoComparer ()
3247 IStructuralEquatable array = new int[] {1, 2, 3};
3248 IStructuralComparable array2 = new int[] {1, 2, 3};
3249 array.Equals (array2, null);
3253 [ExpectedException (typeof (ArgumentException))]
3254 public void IStructuralEquatable_Equals_ComparerThrows ()
3256 IStructuralEquatable array = new int[] {1, 2, 3};
3257 IStructuralComparable array2 = new int[] {1, 2, 3};
3258 array.Equals (array2, EqualityComparer<long>.Default);