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
24 enum enua {hola,adios,mas,menos};
47 //End Auxiliary Things
50 public class ArrayTest : Assertion
52 char [] arrsort = {'d', 'b', 'f', 'e', 'a', 'c'};
57 public void TestIsFixedSize() {
59 Assert("All arrays are fixed", a1.IsFixedSize);
63 public void TestIsReadOnly() {
65 Assert("No array is readonly", !a1.IsReadOnly);
69 public void TestIsSynchronized() {
71 Assert("No array is synchronized", !a1.IsSynchronized);
75 public void TestLength() {
78 AssertEquals("Zero length array", 0, a1.Length);
82 AssertEquals("One-length array", 1, a1.Length);
85 char[] a1 = {'c', 'c'};
86 AssertEquals("Two-length array", 2, a1.Length);
91 public void TestRank() {
92 char[] a1 = { 'c', 'd', 'e' };
93 AssertEquals("Rank one", 1, a1.Rank);
95 char[,] a2 = new Char[3,3];
96 AssertEquals("Rank two", 2, a2.Rank);
98 char[,,] a3 = new Char[3,3,3];
99 AssertEquals("Rank three", 3, a3.Rank);
103 public void TestBinarySearch1() {
104 bool errorThrown = false;
106 Array.BinarySearch(null, "blue");
107 } catch (ArgumentNullException) {
110 Assert("#B01", errorThrown);
113 char[,] c1 = new Char[2,2];
114 Array.BinarySearch(c1, "needle");
115 } catch (RankException) {
118 Assert("#B02", errorThrown);
121 char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
123 Array.BinarySearch(arr, 'c') >= 3);
125 Array.BinarySearch(arr, 'c') < 6);
128 char[] arr = {'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
130 -4, Array.BinarySearch(arr, 'c'));
133 char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
135 -9, Array.BinarySearch(arr, 'e'));
140 public void TestBinarySearch2() {
141 bool errorThrown = false;
143 Array.BinarySearch(null, 0, 1, "blue");
144 } catch (ArgumentNullException) {
147 Assert("#B20", errorThrown);
150 char[,] c1 = new Char[2,2];
151 Array.BinarySearch(c1, 0, 1, "needle");
152 } catch (RankException) {
155 Assert("#B21", errorThrown);
159 Array.BinarySearch(c1, -1, 1, 'a');
160 } catch (ArgumentOutOfRangeException) {
163 Assert("#B22", errorThrown);
167 Array.BinarySearch(c1, 0, -1, 'a');
168 } catch (ArgumentOutOfRangeException) {
171 Assert("#B23", errorThrown);
175 Array.BinarySearch(c1, 0, 4, 'a');
176 } catch (ArgumentException) {
179 Assert("#B24", errorThrown);
182 char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
184 Array.BinarySearch(arr, 2, 8, 'c') >= 5);
186 Array.BinarySearch(arr, 2, 8, 'c') < 8);
189 char[] arr = {'z', 'z', 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
191 -6, Array.BinarySearch(arr, 2, 8, 'c'));
194 char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
196 -11, Array.BinarySearch(arr, 2, 8, 'e'));
200 public void TestBinarySearch3()
202 int[] array = new int[100];
204 for (int i = 0; i < 100; i++)
207 AssertEquals("#B30", 49, Array.BinarySearch(array, 10));
211 public void BinarySearch_NullValue ()
213 int[] array = new int[1];
214 AssertEquals ("I=a,o", -1, Array.BinarySearch (array, null));
215 AssertEquals ("I=a,o,c", -1, Array.BinarySearch (array, null, null));
216 AssertEquals ("I=a,i,i,o", -1, Array.BinarySearch (array, 0, 1, null));
217 AssertEquals ("I=a,i,i,o,c", -1, Array.BinarySearch (array, 0, 1, null,null));
219 object[] o = new object [3] { this, this, null };
220 AssertEquals ("O=a,o", -1, Array.BinarySearch (o, null));
221 AssertEquals ("O=a,o,c", -1, Array.BinarySearch (o, null, null));
222 AssertEquals ("O=a,i,i,o", -1, Array.BinarySearch (o, 0, 3, null));
223 AssertEquals ("O=a,i,i,o,c", -1, Array.BinarySearch (o, 0, 3, null, null));
226 // TODO - testBinarySearch with explicit IComparer args
229 public void TestClear() {
230 bool errorThrown = false;
232 Array.Clear(null, 0, 1);
233 } catch (ArgumentNullException) {
236 Assert("#C01", errorThrown);
238 int[] i1 = { 1, 2, 3, 4 };
240 int[] compare = {1,2,3,4};
241 AssertEquals("#C02", compare[0], i1[0]);
242 AssertEquals("#C03", compare[1], i1[1]);
243 AssertEquals("#C04", compare[2], i1[2]);
244 AssertEquals("#C05", compare[3], i1[3]);
246 Array.Clear(i1, 3, 1);
248 int[] compare = {1,2,3,0};
249 AssertEquals("#C06", compare[0], i1[0]);
250 AssertEquals("#C07", compare[1], i1[1]);
251 AssertEquals("#C08", compare[2], i1[2]);
252 AssertEquals("#C09", compare[3], i1[3]);
254 Array.Clear(i1, 1, 1);
256 int[] compare = {1,0,3,0};
257 AssertEquals("#C10", compare[0], i1[0]);
258 AssertEquals("#C11", compare[1], i1[1]);
259 AssertEquals("#C12", compare[2], i1[2]);
260 AssertEquals("#C13", compare[3], i1[3]);
262 Array.Clear(i1, 1, 3);
264 int[] compare = {1,0,0,0};
265 AssertEquals("#C14", compare[0], i1[0]);
266 AssertEquals("#C15", compare[1], i1[1]);
267 AssertEquals("#C16", compare[2], i1[2]);
268 AssertEquals("#C17", compare[3], i1[3]);
271 string[] s1 = { "red", "green", "blue" };
272 Array.Clear(s1, 0, 3);
274 string[] compare = {null, null, null};
275 AssertEquals("#C18", compare[0], s1[0]);
276 AssertEquals("#C19", compare[1], s1[1]);
277 AssertEquals("#C20", compare[2], s1[2]);
282 public void TestClone() {
283 char[] c1 = {'a', 'b', 'c'};
284 char[] c2 = (char[])c1.Clone();
285 AssertEquals("#D01", c1[0], c2[0]);
286 AssertEquals("#D02", c1[1], c2[1]);
287 AssertEquals("#D03", c1[2], c2[2]);
289 char[] d10 = {'a', 'b'};
290 char[] d11 = {'a', 'c'};
291 char[] d12 = {'b', 'c'};
292 char[][] d1 = {d10, d11, d12};
293 char[][] d2 = (char[][])d1.Clone();
294 AssertEquals("#D04", d1[0], d2[0]);
295 AssertEquals("#D05", d1[1], d2[1]);
296 AssertEquals("#D06", d1[2], d2[2]);
299 AssertEquals("#D07", d1[0], d2[0]);
303 public void TestCopy() {
305 bool errorThrown = false;
308 Array.Copy(c1, null, 1);
309 } catch (ArgumentNullException) {
312 Assert("#E01", errorThrown);
315 bool errorThrown = false;
318 Array.Copy(null, c1, 1);
319 } catch (ArgumentNullException) {
322 Assert("#E02", errorThrown);
325 bool errorThrown = false;
327 Char[] c1 = new Char[1];
328 Char[,] c2 = new Char[1,1];
329 Array.Copy(c1, c2, 1);
330 } catch (RankException) {
333 Assert("#E03", errorThrown);
336 bool errorThrown = false;
338 Char[] c1 = new Char[1];
339 string[] s1 = new String[1];
340 Array.Copy(c1, s1, 1);
341 } catch (ArrayTypeMismatchException) {
344 Assert("#E04", errorThrown);
347 bool errorThrown = false;
349 Char[] c1 = new Char[1];
350 Object[] o1 = new Object[1];
352 Array.Copy(o1, c1, 1);
353 } catch (InvalidCastException) {
356 Assert("#E05", errorThrown);
359 bool errorThrown = false;
361 Char[] c1 = new Char[1];
362 Char[] c2 = new Char[1];
363 Array.Copy(c1, c2, -1);
364 } catch (ArgumentOutOfRangeException) {
367 Assert("#E06", errorThrown);
370 bool errorThrown = false;
372 Char[] c1 = new Char[1];
373 Char[] c2 = new Char[2];
374 Array.Copy(c1, c2, 2);
375 } catch (ArgumentException) {
378 Assert("#E07", errorThrown);
381 bool errorThrown = false;
383 Char[] c1 = new Char[1];
384 Char[] c2 = new Char[2];
385 Array.Copy(c2, c1, 2);
386 } catch (ArgumentException) {
389 Assert("#E08", errorThrown);
392 char[] orig = {'a', 'b', 'd', 'a'};
393 char[] copy = new Char[4];
394 Array.Copy(orig, copy, 4);
395 for (int i = 0; i < orig.Length; i++) {
396 AssertEquals("#E09(" + i + ")",
399 Array.Clear(copy, 0, copy.Length);
400 for (int i = 0; i < orig.Length; i++) {
401 AssertEquals("#E10(" + i + ")",
404 Array.Copy(orig, copy, 2);
405 AssertEquals("#E11", orig[0], copy[0]);
406 AssertEquals("#E12", orig[1], copy[1]);
407 Assert("#E13", orig[2] != copy[2]);
408 Assert("#E14", orig[3] != copy[3]);
412 public void TestCopy2() {
414 bool errorThrown = false;
416 Char[] c1 = new Char[2];
417 Char[] c2 = new Char[2];
418 Array.Copy(c2, 1, c1, 0, 2);
419 } catch (ArgumentException) {
422 Assert("#E31", errorThrown);
425 bool errorThrown = false;
427 Char[] c1 = new Char[2];
428 Char[] c2 = new Char[2];
429 Array.Copy(c2, 0, c1, 1, 2);
430 } catch (ArgumentException) {
433 Assert("#E32", errorThrown);
436 char[] orig = {'a', 'b', 'd', 'a'};
437 char[] copy = new Char[4];
438 Array.Copy(orig, 1, copy, 1, 3);
439 Assert("#E33", copy[0] != orig[0]);
440 for (int i = 1; i < orig.Length; i++) {
441 AssertEquals("#E34(" + i + ")",
444 Array.Clear(copy, 0, copy.Length);
445 Array.Copy(orig, 1, copy, 0, 2);
446 AssertEquals("#E35", orig[1], copy[0]);
447 AssertEquals("#E36", orig[2], copy[1]);
448 Assert("#E37", copy[2] != orig[2]);
449 Assert("#E38", copy[3] != orig[3]);
453 public void TestCopyTo() {
455 bool errorThrown = false;
457 Char[] c1 = new Char[2];
459 } catch (ArgumentNullException) {
462 Assert("#E61", errorThrown);
465 bool errorThrown = false;
467 Char[] c1 = new Char[2];
468 Char[,] c2 = new Char[2,2];
470 } catch (ArgumentException) {
473 Assert("#E62", errorThrown);
476 bool errorThrown = false;
478 Char[,] c1 = new Char[2,2];
479 Char[] c2 = new Char[2];
481 } catch (RankException) {
484 Assert("#E63", errorThrown);
487 bool errorThrown = false;
489 Char[,] c1 = new Char[2,2];
490 Char[] c2 = new Char[2];
492 } catch (RankException) {
495 Assert("#E64", errorThrown);
498 bool errorThrown = false;
500 Char[] c1 = new Char[2];
501 Char[] c2 = new Char[2];
503 } catch (ArgumentOutOfRangeException) {
506 Assert("#E65", errorThrown);
509 bool errorThrown = false;
511 Char[] c1 = new Char[2];
512 Char[] c2 = new Char[2];
514 } catch (ArgumentException) {
517 Assert("#E66", errorThrown);
520 bool errorThrown = false;
522 Char[] c1 = new Char[2];
523 Char[] c2 = new Char[2];
525 } catch (ArgumentException) {
528 Assert("#E67", errorThrown);
532 bool errorThrown = false;
534 String[] c1 = new String[2];
535 // TODO: this crashes mono if there are null
536 // values in the array.
539 Char[] c2 = new Char[2];
543 } catch (ArrayTypeMismatchException) {
546 Assert("#E68", errorThrown);
549 Char[] orig = {'a', 'b', 'c', 'd'};
550 Char[] copy = new Char[10];
551 Array.Clear(copy, 0, copy.Length);
552 orig.CopyTo(copy, 3);
553 AssertEquals("#E69", (char)0, copy[0]);
554 AssertEquals("#E70", (char)0, copy[1]);
555 AssertEquals("#E71", (char)0, copy[2]);
556 AssertEquals("#E72", orig[0], copy[3]);
557 AssertEquals("#E73", orig[1], copy[4]);
558 AssertEquals("#E74", orig[2], copy[5]);
559 AssertEquals("#E75", orig[3], copy[6]);
560 AssertEquals("#E76", (char)0, copy[7]);
561 AssertEquals("#E77", (char)0, copy[8]);
562 AssertEquals("#E78", (char)0, copy[9]);
565 // The following is valid and must not throw an exception.
566 bool errorThrown = false;
568 int[] src = new int [0];
569 int[] dest = new int [0];
570 src.CopyTo (dest, 0);
571 } catch (ArgumentException) {
574 Assert("#E79", !errorThrown);
579 bool errorThrown = false;
581 CClass[] src = new CClass [] { new CClass () };
582 BClass[] dest = new BClass [1];
584 src.CopyTo (dest, 0);
586 } catch (ArrayTypeMismatchException) {
589 Assert("#E80", errorThrown);
594 public void TestCreateInstance() {
596 bool errorThrown = false;
598 Array.CreateInstance(null, 12);
599 } catch (ArgumentNullException) {
602 Assert("#F01", errorThrown);
605 bool errorThrown = false;
607 Array.CreateInstance(Type.GetType("System.Char"), -3);
608 } catch (ArgumentOutOfRangeException) {
611 Assert("#F02", errorThrown);
614 bool errorThrown = false;
616 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
617 } catch (ArgumentNullException) {
620 Assert("#F03a", errorThrown);
624 bool errorThrown = false;
626 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
627 } catch (ArgumentNullException) {
630 Assert("#F03b", errorThrown);
634 bool errorThrown = false;
636 Array.CreateInstance(Type.GetType("System.Char"), null, null);
637 } catch (ArgumentNullException) {
640 Assert("#F04", errorThrown);
643 bool errorThrown = false;
645 int[] lengths = new int [0];
646 Array.CreateInstance(Type.GetType("System.Char"), lengths);
647 } catch (ArgumentException) {
650 Assert("#F05", errorThrown);
653 bool errorThrown = false;
655 int[] lengths = new int [1];
656 int[] bounds = new int [2];
657 Array.CreateInstance(Type.GetType("System.Char"), lengths, bounds);
659 } catch (ArgumentException) {
662 Assert("#F06", errorThrown);
665 char[] c1 = (char[])Array.CreateInstance(Type.GetType("System.Char"), 12);
666 AssertEquals("#F07", 12, c1.Length);
668 Array c2 = Array.CreateInstance(Type.GetType("System.Char"), 12, 5);
669 AssertEquals("#F08", 2, c2.Rank);
670 AssertEquals("#F09", 60, c2.Length);
674 int[] lengths = { 3 };
675 int[] bounds = { 5 };
676 int[] src = { 512, 718, 912 };
677 Array array = Array.CreateInstance(typeof(int), lengths, bounds);
679 AssertEquals("#F10", 3, array.Length);
680 AssertEquals("#F11", 5, array.GetLowerBound(0));
681 AssertEquals("#F12", 7, array.GetUpperBound(0));
683 src.CopyTo (array, 5);
685 for (int i = 0; i < src.Length; i++)
686 AssertEquals("#F13(" + i + ")", src[i], array.GetValue(i+5));
689 // Test that a 1 dimensional array with 0 lower bound is the
690 // same as an szarray
691 Type szarrayType = new int [10].GetType ();
692 Assert (szarrayType == (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0})).GetType ());
693 Assert (szarrayType != (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {1})).GetType ());
697 [ExpectedException (typeof (ArgumentNullException))]
698 public void TestCreateInstance2 ()
700 Array a = Array.CreateInstance (typeof (Int32), (int[])null);
705 [ExpectedException (typeof (ArgumentNullException))]
707 [ExpectedException (typeof (NullReferenceException))]
709 public void TestCreateInstance2b ()
711 Array a = Array.CreateInstance (typeof (Int32), (long[])null);
715 public void TestGetEnumerator() {
716 String[] s1 = {"this", "is", "a", "test"};
717 IEnumerator en = s1.GetEnumerator ();
718 AssertNotNull ("#G01", en);
720 Assert ("#G02", en.MoveNext ());
721 AssertEquals ("#G03", "this", en.Current);
722 Assert ("#G04", en.MoveNext ());
723 AssertEquals ("#G05", "is", en.Current);
724 Assert ("#G06", en.MoveNext ());
725 AssertEquals ("#G07", "a", en.Current);
726 Assert ("#G08", en.MoveNext ());
727 AssertEquals ("#G09", "test", en.Current);
728 Assert ("#G10", !en.MoveNext ());
731 Assert("#G11", en.MoveNext ());
732 AssertEquals ("#G12", "this", en.Current);
734 // mutation does not invalidate array enumerator!
735 s1.SetValue ("change", 1);
736 Assert ("#G13", en.MoveNext ());
737 AssertEquals ("#G14", "change", en.Current);
741 public void TestGetEnumeratorMultipleDimension() {
742 String[,] s1 = {{"this", "is"}, {"a", "test"}};
743 IEnumerator en = s1.GetEnumerator ();
744 AssertNotNull ("#AA01", en);
746 Assert ("#AA02", en.MoveNext ());
747 AssertEquals ("#AA03", "this", en.Current);
748 Assert ("#AA04", en.MoveNext ());
749 AssertEquals ("#AA05", "is", en.Current);
750 Assert ("#AA06", en.MoveNext ());
751 AssertEquals ("#AA07", "a", en.Current);
752 Assert ("#AA08", en.MoveNext ());
753 AssertEquals ("#AA09", "test", en.Current);
754 Assert ("#AA10", !en.MoveNext ());
757 Assert("#AA11", en.MoveNext ());
758 AssertEquals ("#AA12", "this", en.Current);
761 // mutation does not invalidate array enumerator!
762 s1.SetValue ("change", idxs);
763 Assert ("#AA13", en.MoveNext ());
764 AssertEquals ("#AA14", "change", en.Current);
768 public void TestGetEnumeratorNonZeroLowerBounds() {
769 int[] myLengthsArray = new int[2] { 3, 5 };
770 int[] myBoundsArray = new int[2] { 2, 3 };
772 Array myArray=Array.CreateInstance( typeof(String), myLengthsArray, myBoundsArray );
773 for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
774 for ( int j = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++ ) {
775 int[] myIndicesArray = new int[2] { i, j };
776 myArray.SetValue( Convert.ToString(i) + j, myIndicesArray );
778 IEnumerator en = myArray.GetEnumerator ();
779 AssertNotNull ("#AB01", en);
781 // check the first couple of values
782 Assert ("#AB02", en.MoveNext ());
783 AssertEquals ("#AB03", "23", en.Current);
784 Assert ("#AB04", en.MoveNext ());
785 AssertEquals ("#AB05", "24", en.Current);
787 // then check the last element's value
790 lastElement = (string)en.Current;
791 } while (en.MoveNext());
792 AssertEquals ("#AB06", "47", lastElement);
796 public void TestIList_Add () {
797 int[] myLengthsArray = new int[2] { 3, 5 };
798 int[] myBoundsArray = new int[2] { 2, 3 };
800 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
802 ((IList)myArray).Add ("can not");
803 Fail ("IList.Add should throw");
805 catch (NotSupportedException) {
809 Fail ("IList.Add threw wrong exception type");
812 Fail("IList.Add shouldn't get this far");
816 public void TestIList_Insert () {
817 int[] myLengthsArray = new int[2] { 3, 5 };
818 int[] myBoundsArray = new int[2] { 2, 3 };
820 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
822 ((IList)myArray).Insert (0, "can not");
823 Fail ("IList.Insert should throw");
825 catch (NotSupportedException) {
829 Fail ("IList.Insert threw wrong exception type");
832 Fail("IList.Insert shouldn't get this far");
836 public void TestIList_Remove () {
837 int[] myLengthsArray = new int[2] { 3, 5 };
838 int[] myBoundsArray = new int[2] { 2, 3 };
840 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
842 ((IList)myArray).Remove ("can not");
843 Fail ("IList.Remove should throw");
845 catch (NotSupportedException) {
849 Fail ("IList.Remove threw wrong exception type");
852 Fail("IList.Remove shouldn't get this far");
856 public void TestIList_RemoveAt () {
857 int[] myLengthsArray = new int[2] { 3, 5 };
858 int[] myBoundsArray = new int[2] { 2, 3 };
860 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
862 ((IList)myArray).RemoveAt (0);
863 Fail ("IList.RemoveAt should throw");
865 catch (NotSupportedException) {
869 Fail ("IList.RemoveAt threw wrong exception type");
872 Fail("IList.RemoveAt shouldn't get this far");
876 public void TestIList_Contains () {
877 int[] myLengthsArray = new int[2] { 3, 5 };
878 int[] myBoundsArray = new int[2] { 2, 3 };
880 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
883 bool b = ((IList)myArray).Contains ("23");
884 Fail("IList.Contains should throw with multi-dimensional arrays");
886 catch (RankException) {
887 int[] iArr = new int[3] { 1, 2, 3};
888 // check the first and last items
889 Assert("AC01", ((IList)iArr).Contains (1));
890 Assert("AC02", ((IList)iArr).Contains (3));
892 // and one that is definately not there
893 Assert("AC03", !((IList)iArr).Contains (42));
897 Fail("Should not get here");
901 public void TestIList_IndexOf () {
902 int[] myLengthsArray = new int[2] { 3, 5 };
903 int[] myBoundsArray = new int[2] { 2, 3 };
905 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
908 bool b = ((IList)myArray).Contains ("23");
909 Fail("IList.Contains should throw with multi-dimensional arrays");
911 catch (RankException) {
912 int[] iArr = new int[3] { 1, 2, 3};
913 // check the first and last items
914 AssertEquals("AD01", 0, ((IList)iArr).IndexOf (1));
915 AssertEquals("AD02", 2, ((IList)iArr).IndexOf (3));
917 // and one that is definately not there
918 AssertEquals("AD03", -1, ((IList)iArr).IndexOf (42));
920 catch (Exception e) {
921 Fail("Unexpected exception: " + e.ToString());
924 // check that wierd case whem lowerbound is Int32.MinValue,
925 // so that IndexOf() needs to return Int32.MaxValue when it cannot find the object
926 int[] myLengthArray = new int[1] { 3 };
927 int[] myBoundArray = new int[1] { Int32.MinValue };
928 Array myExtremeArray=Array.CreateInstance ( typeof(String), myLengthArray, myBoundArray );
929 AssertEquals("AD04", Int32.MaxValue, ((IList)myExtremeArray).IndexOf (42));
934 public void TestGetLength() {
936 bool errorThrown = false;
938 char[] c1 = {'a', 'b', 'c'};
940 } catch (IndexOutOfRangeException) {
943 Assert("#H01", errorThrown);
946 bool errorThrown = false;
948 char[] c1 = {'a', 'b', 'c'};
950 } catch (IndexOutOfRangeException) {
953 Assert("#H02", errorThrown);
956 char[] c2 = new Char[5];
960 char[,] c3 = new Char[6,7];
968 public void TestGetLowerBound() {
970 bool errorThrown = false;
972 char[] c = {'a', 'b', 'c'};
974 } catch (IndexOutOfRangeException) {
977 Assert("#H31", errorThrown);
980 bool errorThrown = false;
982 char[] c = {'a', 'b', 'c'};
984 } catch (IndexOutOfRangeException) {
987 Assert("#H32", errorThrown);
990 char[] c1 = new Char[5];
992 0, c1.GetLowerBound(0));
994 char[,] c2 = new Char[4,4];
996 0, c2.GetLowerBound(0));
998 0, c2.GetLowerBound(1));
1002 public void TestGetUpperBound() {
1004 bool errorThrown = false;
1006 char[] c = {'a', 'b', 'c'};
1007 c.GetUpperBound(-1);
1008 } catch (IndexOutOfRangeException) {
1011 Assert("#H61", errorThrown);
1014 bool errorThrown = false;
1016 char[] c = {'a', 'b', 'c'};
1018 } catch (IndexOutOfRangeException) {
1021 Assert("#H62", errorThrown);
1024 char[] c1 = new Char[5];
1025 AssertEquals("#H63",
1026 4, c1.GetUpperBound(0));
1028 char[,] c2 = new Char[4,6];
1029 AssertEquals("#H64",
1030 3, c2.GetUpperBound(0));
1031 AssertEquals("#H65",
1032 5, c2.GetUpperBound(1));
1036 public void TestGetValue1() {
1038 bool errorThrown = false;
1040 char[,] c = new Char[2,2];
1042 } catch (ArgumentException) {
1045 Assert("#I01", errorThrown);
1048 bool errorThrown = false;
1050 char[] c = {'a', 'b', 'c'};
1052 } catch (IndexOutOfRangeException) {
1055 Assert("#I02", errorThrown);
1058 bool errorThrown = false;
1060 char[] c = {'a', 'b', 'c'};
1062 } catch (IndexOutOfRangeException) {
1065 Assert("#I03", errorThrown);
1068 char[] c1 = {'a', 'b', 'c', 'd'};
1069 for (int i = 0; i < c1.Length; i++) {
1070 AssertEquals("#I04(" + i + ")", c1[i], c1.GetValue(i));
1075 public void TestGetValue2() {
1077 bool errorThrown = false;
1079 char[] c = new Char[2];
1081 } catch (ArgumentException) {
1084 Assert("#I21", errorThrown);
1087 bool errorThrown = false;
1089 char[,] c = new Char[2,2];
1091 } catch (IndexOutOfRangeException) {
1094 Assert("#I22", errorThrown);
1097 bool errorThrown = false;
1099 char[,] c = new Char[2,2];
1101 } catch (IndexOutOfRangeException) {
1104 Assert("#I23", errorThrown);
1107 char[,] c1 = new Char[4,6];
1108 for (int i = 0; i < 24; i++) {
1111 c1[first,second] = (char)(((int)'a')+i);
1113 for (int i = 0; i < c1.GetLength(0); i++) {
1114 for (int j = 0; j < c1.GetLength(1); j++) {
1115 AssertEquals("#I24(" + i + "," + j + ")",
1116 c1[i,j], c1.GetValue(i, j));
1122 public void TestGetValue3() {
1124 bool errorThrown = false;
1126 char[] c = new Char[2];
1128 } catch (ArgumentException) {
1131 Assert("#I41", errorThrown);
1134 bool errorThrown = false;
1136 char[,,] c = new Char[2,2,2];
1137 c.GetValue(-1, 1, 1);
1138 } catch (IndexOutOfRangeException) {
1141 Assert("#I42", errorThrown);
1144 bool errorThrown = false;
1146 char[,,] c = new Char[2,2,2];
1148 } catch (IndexOutOfRangeException) {
1151 Assert("#I43", errorThrown);
1154 char[,,] c1 = new Char[4,2,3];
1155 for (int i = 0; i < 24; i++) {
1157 int remains = i % 6;
1158 int second = remains / 3;
1159 int third = remains % 3;
1160 c1[first,second, third] = (char)(((int)'a')+i);
1162 for (int i = 0; i < c1.GetLength(0); i++) {
1163 for (int j = 0; j < c1.GetLength(1); j++) {
1164 for (int k = 0; k < c1.GetLength(2); k++) {
1165 AssertEquals("#I44(" + i + "," + j + ")",
1166 c1[i,j,k], c1.GetValue(i,j,k));
1174 [ExpectedException (typeof (ArgumentNullException))]
1176 [ExpectedException (typeof (NullReferenceException))]
1178 public void TestGetValueLongArray ()
1180 char[] c = new Char[2];
1181 c.GetValue((long [])null);
1185 public void TestGetValueN() {
1187 bool errorThrown = false;
1189 char[] c = new Char[2];
1190 c.GetValue((int [])null);
1191 } catch (ArgumentNullException) {
1194 Assert("#I61a", errorThrown);
1197 bool errorThrown = false;
1199 char[] c = new Char[2];
1200 int[] coords = {1, 1};
1202 } catch (ArgumentException) {
1205 Assert("#I62", errorThrown);
1208 bool errorThrown = false;
1210 char[,] c = new Char[2,2];
1211 int[] coords = {-1, 1};
1213 } catch (IndexOutOfRangeException) {
1216 Assert("#I63", errorThrown);
1219 bool errorThrown = false;
1221 char[,] c = new Char[2,2];
1222 int[] coords = {4, 1};
1224 } catch (IndexOutOfRangeException) {
1227 Assert("#I64", errorThrown);
1230 char[,] c1 = new Char[4,6];
1231 for (int i = 0; i < 24; i++) {
1234 c1[first,second] = (char)(((int)'a')+i);
1236 for (int i = 0; i < c1.GetLength(0); i++) {
1237 for (int j = 0; j < c1.GetLength(1); j++) {
1238 int[] coords = {i, j};
1239 AssertEquals("#I65(" + i + "," + j + ")",
1240 c1[i,j], c1.GetValue(coords));
1246 public void TestIndexOf1() {
1248 bool errorThrown = false;
1250 Array.IndexOf(null, "huh?");
1251 } catch (ArgumentNullException) {
1254 Assert("#J01", errorThrown);
1257 bool errorThrown = false;
1259 char[,] c = new Char[2,2];
1260 Array.IndexOf(c, "huh?");
1261 } catch (RankException) {
1264 Assert("#J02", errorThrown);
1267 String[] s1 = {"this", "is", "a", "test"};
1268 AssertEquals("#J03", -1, Array.IndexOf(s1, null));
1269 AssertEquals("#J04", -1, Array.IndexOf(s1, "nothing"));
1270 AssertEquals("#J05", 0, Array.IndexOf(s1, "this"));
1271 AssertEquals("#J06", 3, Array.IndexOf(s1, "test"));
1275 public void TestIndexOf2() {
1277 bool errorThrown = false;
1279 Array.IndexOf(null, "huh?", 0);
1280 } catch (ArgumentNullException) {
1283 Assert("#J21", errorThrown);
1286 bool errorThrown = false;
1288 char[,] c = new Char[2,2];
1289 Array.IndexOf(c, "huh?", 0);
1290 } catch (RankException) {
1293 Assert("#J22", errorThrown);
1296 bool errorThrown = false;
1298 char[] c = new Char[2];
1299 Array.IndexOf(c, "huh?", 3);
1300 } catch (ArgumentOutOfRangeException) {
1303 Assert("#J23", errorThrown);
1306 String[] s1 = {"this", "is", "really", "a", "test"};
1307 AssertEquals("#J24", -1, Array.IndexOf(s1, null, 1));
1308 AssertEquals("#J25", -1, Array.IndexOf(s1, "nothing", 1));
1309 AssertEquals("#J26", -1, Array.IndexOf(s1, "this", 1));
1310 AssertEquals("#J27", 1, Array.IndexOf(s1, "is", 1));
1311 AssertEquals("#J28", 4, Array.IndexOf(s1, "test", 1));
1315 public void TestIndexOf3() {
1317 bool errorThrown = false;
1319 Array.IndexOf(null, "huh?", 0, 1);
1320 } catch (ArgumentNullException) {
1323 Assert("#J41", errorThrown);
1326 bool errorThrown = false;
1328 char[,] c = new Char[2,2];
1329 Array.IndexOf(c, "huh?", 0, 1);
1330 } catch (RankException) {
1333 Assert("#J42", errorThrown);
1336 bool errorThrown = false;
1338 char[] c = new Char[2];
1339 Array.IndexOf(c, "huh?", 3, 1);
1340 } catch (ArgumentOutOfRangeException) {
1343 Assert("#J43", errorThrown);
1346 bool errorThrown = false;
1348 char[] c = new Char[2];
1349 Array.IndexOf(c, "huh?", 0, 5);
1350 } catch (ArgumentOutOfRangeException) {
1353 Assert("#J44", errorThrown);
1356 String[] s1 = {"this", "is", "really", "a", "test"};
1357 AssertEquals("#J45", -1, Array.IndexOf(s1, null, 1, 3));
1358 AssertEquals("#J46", -1, Array.IndexOf(s1, "nothing", 1, 3));
1359 AssertEquals("#J47", -1, Array.IndexOf(s1, "this", 1, 3));
1360 AssertEquals("#J48", 1, Array.IndexOf(s1, "is", 1, 3));
1361 AssertEquals("#J49", -1, Array.IndexOf(s1, "test", 1, 3));
1362 AssertEquals("#J50", 3, Array.IndexOf(s1, "a", 1, 3));
1366 public void TestLastIndexOf1() {
1368 bool errorThrown = false;
1370 Array.LastIndexOf(null, "huh?");
1371 } catch (ArgumentNullException) {
1374 Assert("#K01", errorThrown);
1377 bool errorThrown = false;
1379 char[,] c = new Char[2,2];
1380 Array.LastIndexOf(c, "huh?");
1381 } catch (RankException) {
1384 Assert("#K02", errorThrown);
1387 String[] s1 = {"this", "is", "a", "a", "test"};
1388 AssertEquals("#K03", -1, Array.LastIndexOf(s1, null));
1389 AssertEquals("#K04", -1, Array.LastIndexOf(s1, "nothing"));
1390 AssertEquals("#K05", 0, Array.LastIndexOf(s1, "this"));
1391 AssertEquals("#K06", 4, Array.LastIndexOf(s1, "test"));
1392 AssertEquals("#K07", 3, Array.LastIndexOf(s1, "a"));
1396 public void TestLastIndexOf2() {
1398 bool errorThrown = false;
1400 Array.LastIndexOf(null, "huh?", 0);
1401 } catch (ArgumentNullException) {
1404 Assert("#K21", errorThrown);
1407 bool errorThrown = false;
1409 char[,] c = new Char[2,2];
1410 Array.LastIndexOf(c, "huh?", 0);
1411 } catch (RankException) {
1414 Assert("#K22", errorThrown);
1417 bool errorThrown = false;
1419 char[] c = new Char[2];
1420 Array.LastIndexOf(c, "huh?", 3);
1421 } catch (ArgumentOutOfRangeException) {
1424 Assert("#K23", errorThrown);
1427 String[] s1 = {"this", "is", "really", "a", "test"};
1428 AssertEquals("#K24", -1, Array.LastIndexOf(s1, null, 3));
1429 AssertEquals("#K25", -1, Array.LastIndexOf(s1, "nothing", 3));
1430 AssertEquals("#K26", -1, Array.LastIndexOf(s1, "test", 3));
1431 AssertEquals("#K27", 3, Array.LastIndexOf(s1, "a", 3));
1432 AssertEquals("#K28", 0, Array.LastIndexOf(s1, "this", 3));
1436 public void TestLastIndexOf3() {
1438 bool errorThrown = false;
1440 Array.LastIndexOf(null, "huh?", 0, 1);
1441 } catch (ArgumentNullException) {
1444 Assert("#K41", errorThrown);
1447 bool errorThrown = false;
1449 char[,] c = new Char[2,2];
1450 Array.LastIndexOf(c, "huh?", 0, 1);
1451 } catch (RankException) {
1454 Assert("#K42", errorThrown);
1457 bool errorThrown = false;
1459 char[] c = new Char[2];
1460 Array.LastIndexOf(c, "huh?", 3, 1);
1461 } catch (ArgumentOutOfRangeException) {
1464 Assert("#K43", errorThrown);
1467 bool errorThrown = false;
1469 char[] c = new Char[2];
1470 Array.LastIndexOf(c, "huh?", 0, 5);
1471 } catch (ArgumentOutOfRangeException) {
1474 Assert("#K44", errorThrown);
1477 String[] s1 = {"this", "is", "really", "a", "test"};
1478 AssertEquals("#K45",
1479 -1, Array.LastIndexOf(s1, null, 3, 3));
1480 AssertEquals("#K46",
1481 -1, Array.LastIndexOf(s1, "nothing", 3, 3));
1482 AssertEquals("#K47",
1483 -1, Array.LastIndexOf(s1, "this", 3, 3));
1484 AssertEquals("#K48",
1485 1, Array.LastIndexOf(s1, "is", 3, 3));
1486 AssertEquals("#K49",
1487 -1, Array.LastIndexOf(s1, "test", 3, 3));
1488 AssertEquals("#K50",
1489 3, Array.LastIndexOf(s1, "a", 3, 3));
1493 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1494 public void TestLastIndexOf4 ()
1496 short [] a = new short [] { 19, 238, 317, 6, 565, 0, -52, 60, -563, 753, 238, 238};
1497 Array.LastIndexOf (a, 16, -1);
1501 public void TestLastIndexOf5 ()
1503 char [] a = new char [] {'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'j', 'i', 'h'};
1508 for (int i = a.Length - 1; i >= 0 ; i--) {
1510 retval = Array.LastIndexOf(a, a [i], i, i + 1);
1518 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1519 public void LastIndexOf_StartIndexOverflow ()
1521 // legal - no exception
1522 byte[] array = new byte [16];
1523 Array.LastIndexOf (array, this, Int32.MaxValue, 1);
1527 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1528 public void LastIndexOf_CountOverflow ()
1530 // legal - no exception
1531 byte[] array = new byte [16];
1532 Array.LastIndexOf (array, this, 1, Int32.MaxValue);
1536 public void TestReverse() {
1538 bool errorThrown = false;
1540 Array.Reverse(null);
1541 } catch (ArgumentNullException) {
1544 Assert("#L01", errorThrown);
1547 bool errorThrown = false;
1549 char[,] c = new Char[2,2];
1551 } catch (RankException) {
1554 Assert("#L02", errorThrown);
1557 char[] c1 = {'a', 'b', 'c', 'd'};
1559 AssertEquals("#L03", 'd', c1[0]);
1560 AssertEquals("#L04", 'c', c1[1]);
1561 AssertEquals("#L05", 'b', c1[2]);
1562 AssertEquals("#L06", 'a', c1[3]);
1565 bool errorThrown = false;
1567 Array.Reverse(null, 0, 0);
1568 } catch (ArgumentNullException) {
1571 Assert("#L07", errorThrown);
1574 bool errorThrown = false;
1576 char[,] c = new Char[2,2];
1577 Array.Reverse(c, 0, 0);
1578 } catch (RankException) {
1581 Assert("#L08", errorThrown);
1584 //bool errorThrown = false;
1586 // char[] c = new Char[2];
1587 // Array.Reverse(c, 0, 3);
1588 //} catch (ArgumentOutOfRangeException) {
1589 // errorThrown = true;
1591 //Assert("#L09", errorThrown);
1594 //bool errorThrown = false;
1596 // char[] c = new Char[2];
1597 // Array.Reverse(c, 3, 0);
1598 //} catch (ArgumentOutOfRangeException) {
1599 // errorThrown = true;
1601 //Assert("#L10", errorThrown);
1604 char[] c2 = { 'a', 'b', 'c', 'd'};
1605 Array.Reverse(c2, 1, 2);
1606 AssertEquals("#L11", 'a', c2[0]);
1607 AssertEquals("#L12", 'c', c2[1]);
1608 AssertEquals("#L13", 'b', c2[2]);
1609 AssertEquals("#L14", 'd', c2[3]);
1613 public void TestSetValue1() {
1615 bool errorThrown = false;
1617 char[,] c = new Char[2,2];
1618 c.SetValue("buh", 1);
1619 } catch (ArgumentException) {
1622 Assert("#M01", errorThrown);
1625 bool errorThrown = false;
1627 char[] c = {'a', 'b', 'c'};
1628 c.SetValue("buh", -1);
1629 } catch (IndexOutOfRangeException) {
1632 Assert("#M02", errorThrown);
1635 bool errorThrown = false;
1637 char[] c = {'a', 'b', 'c'};
1638 c.SetValue("buh", 4);
1639 } catch (IndexOutOfRangeException) {
1642 Assert("#M03", errorThrown);
1645 char[] c1 = {'a', 'b', 'c', 'd'};
1646 char[] c2 = new char[4];
1647 for (int i = 0; i < c1.Length; i++) {
1648 c2.SetValue(c1[i], i);
1650 for (int i = 0; i < c1.Length; i++) {
1651 AssertEquals("#M04(" + i + ")", c1[i], c2[i]);
1654 int[] c3 = { 1, 2, 3 };
1655 long[] c4 = new long [3];
1657 for (int i = 0; i < c3.Length; i++)
1658 c4.SetValue (c3 [i], i);
1662 } catch (Exception e) {
1663 Fail ("c3.CopyTo(): e=" + e);
1665 for (int i = 0; i < c3.Length; i++)
1666 Assert ("#M05(" + i + ")", c3[i] == c4[i]);
1668 Object[] c5 = new Object [3];
1669 long[] c6 = new long [3];
1673 } catch (Exception e) {
1674 Fail ("c4.CopyTo(): e=" + e);
1679 } catch (Exception e) {
1680 Fail ("c5.CopyTo(): e=" + e);
1682 // for (int i = 0; i < c5.Length; i++)
1683 // Assert ("#M06(" + i + ")", c5[i] == c6[i]);
1687 public void TestSetValue2() {
1689 bool errorThrown = false;
1691 char[] c = new Char[2];
1692 c.SetValue("buh", 1,1);
1693 } catch (ArgumentException) {
1696 Assert("#M21", errorThrown);
1699 bool errorThrown = false;
1701 char[,] c = new Char[2,2];
1702 c.SetValue("buh", -1, 1);
1703 } catch (IndexOutOfRangeException) {
1706 Assert("#M22", errorThrown);
1709 bool errorThrown = false;
1711 char[,] c = new Char[2,2];
1712 c.SetValue("buh", 4,1);
1713 } catch (IndexOutOfRangeException) {
1716 Assert("#M23", errorThrown);
1719 char[,] c1 = new Char[4,6];
1720 char[,] c2 = new Char[4,6];
1721 for (int i = 0; i < 24; i++) {
1724 c1[first,second] = (char)(((int)'a')+i);
1725 c2.SetValue(c1[first,second], first, second);
1727 for (int i = 0; i < c1.GetLength(0); i++) {
1728 for (int j = 0; j < c1.GetLength(1); j++) {
1729 AssertEquals("#M24(" + i + "," + j + ")",
1736 public void TestSetValue3() {
1738 bool errorThrown = false;
1740 char[] c = new Char[2];
1741 c.SetValue("buh", 1,1,1);
1742 } catch (ArgumentException) {
1745 Assert("#M41", errorThrown);
1748 bool errorThrown = false;
1750 char[,,] c = new Char[2,2,2];
1751 c.SetValue("buh", -1, 1, 1);
1752 } catch (IndexOutOfRangeException) {
1755 Assert("#M42", errorThrown);
1758 bool errorThrown = false;
1760 char[,,] c = new Char[2,2,2];
1761 c.SetValue("buh", 4,1,1);
1762 } catch (IndexOutOfRangeException) {
1765 Assert("#M43", errorThrown);
1768 char[,,] c1 = new Char[4,2,3];
1769 char[,,] c2 = new Char[4,2,3];
1770 for (int i = 0; i < 24; i++) {
1772 int remains = i % 6;
1773 int second = remains / 3;
1774 int third = remains % 3;
1775 c1[first,second, third] = (char)(((int)'a')+i);
1776 c2.SetValue(c1[first, second, third], first, second, third);
1778 for (int i = 0; i < c1.GetLength(0); i++) {
1779 for (int j = 0; j < c1.GetLength(1); j++) {
1780 for (int k = 0; k < c1.GetLength(2); k++) {
1781 AssertEquals("#M44(" + i + "," + j + " )",
1782 c1[i,j,k], c2[i,j,k]);
1790 [ExpectedException (typeof (ArgumentNullException))]
1792 [ExpectedException (typeof (NullReferenceException))]
1794 public void TestSetValueLongArray ()
1796 char[] c = new Char[2];
1797 c.SetValue("buh", (long [])null);
1801 public void TestSetValueN() {
1803 bool errorThrown = false;
1805 char[] c = new Char[2];
1806 c.SetValue("buh", (int [])null);
1807 } catch (ArgumentNullException) {
1810 Assert("#M61a", errorThrown);
1813 bool errorThrown = false;
1815 char[] c = new Char[2];
1816 int[] coords = {1, 1};
1817 c.SetValue("buh", coords);
1818 } catch (ArgumentException) {
1821 Assert("#M62", errorThrown);
1824 bool errorThrown = false;
1826 char[,] c = new Char[2,2];
1827 int[] coords = {-1, 1};
1828 c.SetValue("buh", coords);
1829 } catch (IndexOutOfRangeException) {
1832 Assert("#M63", errorThrown);
1835 bool errorThrown = false;
1837 char[,] c = new Char[2,2];
1838 int[] coords = {4, 1};
1839 c.SetValue("buh", coords);
1840 } catch (IndexOutOfRangeException) {
1843 Assert("#M64", errorThrown);
1846 char[,] c1 = new Char[4,6];
1847 char[,] c2 = new Char[4,6];
1848 for (int i = 0; i < 24; i++) {
1851 c1[first,second] = (char)(((int)'a')+i);
1852 int[] coords = {first, second};
1853 c2.SetValue(c1[first,second], coords);
1855 for (int i = 0; i < c1.GetLength(0); i++) {
1856 for (int j = 0; j < c1.GetLength(1); j++) {
1857 AssertEquals("#M65(" + i + "," + j + ")",
1864 public void TestSetValue4() {
1866 int[] c1 = { 1, 2, 3 };
1867 long[] c2 = new long [3];
1869 for (int i = 0; i < c1.Length; i++)
1870 c2.SetValue (c1 [i], i);
1872 for (int i = 0; i < c1.Length; i++) {
1873 Assert ("#M81(" + i + ")", c1[i] == c2[i]);
1874 AssertEquals ("#M82(" + i + ")", typeof (long), c2[i].GetType ());
1878 long[] c1 = { 1, 2, 3 };
1879 int[] c2 = new int [3];
1880 bool errorThrown = false;
1882 c2.SetValue (c1 [0], 0);
1883 } catch (ArgumentException) {
1886 Assert("#M83", errorThrown);
1889 int[] c1 = { 1, 2, 3 };
1890 Object[] c2 = new Object [3];
1892 for (int i = 0; i < c1.Length; i++)
1893 c2.SetValue (c1 [i], i);
1895 for (int i = 0; i < c1.Length; i++)
1896 AssertEquals ("#M84(" + i + ")", c1[i], Convert.ToInt32 (c2[i]));
1899 Object[] c1 = new Object [3];
1900 Object[] c2 = new Object [3];
1901 c1[0] = new Object ();
1903 for (int i = 0; i < c1.Length; i++)
1904 c2.SetValue (c1 [i], i);
1906 for (int i = 0; i < c1.Length; i++)
1907 AssertEquals ("#M85(" + i + ")", c1[i], c2[i]);
1910 Object[] c1 = new Object [3];
1911 string[] c2 = new String [3];
1912 string test = "hello";
1915 c2.SetValue (c1 [0], 0);
1916 AssertEquals ("#M86", c1[0], c2[0]);
1917 AssertEquals ("#M87", "hello", c2[0]);
1920 char[] c1 = { 'a', 'b', 'c' };
1921 string[] c2 = new string [3];
1923 c2.SetValue (c1 [0], 0);
1925 } catch (InvalidCastException) {}
1928 Single[] c1 = { 1.2F, 2.3F, 3.4F, 4.5F };
1929 long[] c2 = new long [3];
1931 c2.SetValue (c1 [0], 0);
1933 } catch (ArgumentException) {}
1961 UInt32 v11 = 235354;
1962 UInt64 v12 = 234552;
1964 Object[] va1 = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12 };
1965 Object[] va2 = { "true", "1", "a", "-1.2", "-32", "-234", "-34523", "-1",
1966 "-4.8F", "24234", "235354", "234552" };
1968 Object[][] vt = { va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1 };
1971 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1972 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
1973 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
1974 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1975 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1,
1976 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1,
1977 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,
1978 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1,
1979 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1,
1980 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
1981 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0,
1982 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0
1987 for (int i = 0; i < types.Length; i++) {
1988 for (int j = 0; j < types.Length; j++) {
1989 Array array = Array.CreateInstance (types [j], 2);
1991 Object value = vt[j][i];
1993 bool errorThrown = false;
1995 array.SetValue (value, 0);
1996 } catch (ArgumentException) {
2000 int ex_index = (i * types.Length) + j;
2002 AssertEquals ("#M90(" + types [i] + "," + types [j] + ")",
2003 errorThrown, arg_ex [ex_index] == 1);
2007 for (int i = 0; i < types.Length; i++) {
2008 String[] array = new String [2];
2010 Object value = va1 [i];
2012 bool errorThrown = false;
2014 array.SetValue (value, 0);
2015 } catch (InvalidCastException) {
2019 Assert ("#M91(" + types [i] + ")", errorThrown);
2022 for (int i = 0; i < types.Length; i++) {
2023 Array array = Array.CreateInstance (types [i], 2);
2025 Object value = va2 [i];
2027 bool errorThrown = false;
2029 array.SetValue (value, 0);
2030 } catch (InvalidCastException) {
2034 Assert ("#M92(" + types [i] + ")", errorThrown);
2037 for (int i = 0; i < types.Length; i++) {
2038 Array array = Array.CreateInstance (types [i], 2);
2040 Object value = null;
2042 bool errorThrown = false;
2044 array.SetValue (value, 0);
2045 } catch (InvalidCastException) {
2049 Assert ("#M93(" + types [i] + ")", !errorThrown);
2054 for (int i = 0; i < types.Length; i++) {
2055 for (int j = 0; j < types.Length; j++) {
2056 Array source = Array.CreateInstance (types [i], 2);
2057 Array array = Array.CreateInstance (types [j], 2);
2059 source.SetValue (vt[j][i], 0);
2060 source.SetValue (vt[j][i], 1);
2062 bool errorThrown = false;
2064 Array.Copy (source, array, 2);
2065 } catch (ArrayTypeMismatchException) {
2069 int ex_index = (i * types.Length) + j;
2071 AssertEquals ("#M94(" + types [i] + "," + types [j] + ")",
2072 errorThrown, arg_ex [ex_index] == 1);
2076 for (int i = 0; i < types.Length; i++) {
2077 Array source = Array.CreateInstance (types [i], 2);
2078 String[] array = new String [2];
2080 source.SetValue (va1 [i], 0);
2081 source.SetValue (va1 [i], 1);
2083 bool errorThrown = false;
2085 Array.Copy (source, array, 2);
2086 } catch (ArrayTypeMismatchException) {
2090 Assert ("#M95(" + types [i] + ")", errorThrown);
2093 for (int i = 0; i < types.Length; i++) {
2094 String[] source = new String [2];
2095 Array array = Array.CreateInstance (types [i], 2);
2097 source.SetValue (va2 [i], 0);
2098 source.SetValue (va2 [i], 1);
2100 bool errorThrown = false;
2102 Array.Copy (source, array, 2);
2103 } catch (ArrayTypeMismatchException) {
2107 Assert ("#M96(" + types [i] + ")", errorThrown);
2113 public void TestSort() {
2115 bool errorThrown = false;
2118 } catch (ArgumentNullException) {
2121 Assert("#N01", errorThrown);
2124 bool errorThrown = false;
2126 Array.Sort(null, 0, 1);
2127 } catch (ArgumentNullException) {
2130 Assert("#N02", errorThrown);
2133 bool errorThrown = false;
2135 char[] c1 = new Char[2];
2136 Array.Sort(null, c1);
2137 } catch (ArgumentNullException) {
2140 Assert("#N03", errorThrown);
2143 bool errorThrown = false;
2145 char[] c1 = new Char[2];
2146 Array.Sort(null, c1, 0, 1);
2147 } catch (ArgumentNullException) {
2150 Assert("#N04", errorThrown);
2154 char[] arr = {'d', 'b', 'f', 'e', 'a', 'c'};
2157 Array.Sort (null, 0, 1);
2158 Fail ("#N" + tc.ToString ());
2160 catch (ArgumentException) {}
2161 catch (Exception) { Fail ("#N" + tc.ToString ()); }
2165 Array.Sort (arr, -1, 3);
2166 Fail ("#N" + tc.ToString ());
2168 catch (ArgumentException) {}
2169 catch (Exception) { Fail ("#N" + tc.ToString ()); }
2173 Array.Sort (arr, 1, -3);
2174 Fail ("#N" + tc.ToString ());
2176 catch (ArgumentException) {}
2177 catch (Exception) { Fail ("#N" + tc.ToString ()); }
2181 Array.Sort (arr, arr.Length, arr.Length + 2);
2182 Fail ("#N" + tc.ToString ());
2184 catch (ArgumentException) {}
2185 catch (Exception) { Fail ("#N" + tc.ToString ()); }
2188 // note: null second array => just sort first array
2189 char[] starter = {'d', 'b', 'f', 'e', 'a', 'c'};
2190 int[] starter1 = {1,2,3,4,5,6};
2192 char[] c1 = (char[])starter.Clone();
2194 AssertEquals("#N21", 'a', c1[0]);
2195 AssertEquals("#N22", 'b', c1[1]);
2196 AssertEquals("#N23", 'c', c1[2]);
2197 AssertEquals("#N24", 'd', c1[3]);
2198 AssertEquals("#N25", 'e', c1[4]);
2199 AssertEquals("#N26", 'f', c1[5]);
2202 char[] c1 = (char[])starter.Clone();
2203 int[] i1 = (int[])starter1.Clone();
2205 AssertEquals("#N41", 'a', c1[0]);
2206 AssertEquals("#N42", 'b', c1[1]);
2207 AssertEquals("#N43", 'c', c1[2]);
2208 AssertEquals("#N44", 'd', c1[3]);
2209 AssertEquals("#N45", 'e', c1[4]);
2210 AssertEquals("#N46", 'f', c1[5]);
2211 AssertEquals("#N47", 5, i1[0]);
2212 AssertEquals("#N48", 2, i1[1]);
2213 AssertEquals("#N49", 6, i1[2]);
2214 AssertEquals("#N50", 1, i1[3]);
2215 AssertEquals("#N51", 4, i1[4]);
2216 AssertEquals("#N52", 3, i1[5]);
2219 char[] c1 = (char[])starter.Clone();
2220 Array.Sort(c1, 1, 4);
2221 AssertEquals("#N61", 'd', c1[0]);
2222 AssertEquals("#N62", 'a', c1[1]);
2223 AssertEquals("#N63", 'b', c1[2]);
2224 AssertEquals("#N64", 'e', c1[3]);
2225 AssertEquals("#N65", 'f', c1[4]);
2226 AssertEquals("#N66", 'c', c1[5]);
2229 char[] c1 = (char[])starter.Clone();
2230 int[] i1 = (int[])starter1.Clone();
2231 Array.Sort(c1, i1, 1, 4);
2232 AssertEquals("#N81", 'd', c1[0]);
2233 AssertEquals("#N82", 'a', c1[1]);
2234 AssertEquals("#N83", 'b', c1[2]);
2235 AssertEquals("#N84", 'e', c1[3]);
2236 AssertEquals("#N85", 'f', c1[4]);
2237 AssertEquals("#N86", 'c', c1[5]);
2238 AssertEquals("#N87", 1, i1[0]);
2239 AssertEquals("#N88", 5, i1[1]);
2240 AssertEquals("#N89", 2, i1[2]);
2241 AssertEquals("#N90", 4, i1[3]);
2242 AssertEquals("#N91", 3, i1[4]);
2243 AssertEquals("#N92", 6, i1[5]);
2248 public void TestInitializeEmpty()
2260 Assert("#TI01",!catched);
2264 public void TestInitializeInt()
2269 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2271 AssertEquals("#TI02 " + i ,a[i],b[i]);
2276 public void TestInitializeDouble()
2278 double[] a = {1.0,2.0,0.0};
2280 double[] b = {1.0,2.0,0.0};
2281 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2283 AssertEquals("#TI03 " + i ,a[i],b[i]);
2288 public void TestInitializeFloat()
2290 float[] a = {1.0F,2.0F,0.0F};
2292 float[] b = {1.0F,2.0F,0.0F};
2293 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2295 AssertEquals("#TI04 " + i ,a[i],b[i]);
2300 public void TestInitializeChar()
2302 char[] a = {'1','.','0','F','2','.','0','F'};
2304 char[] b = {'1','.','0','F','2','.','0','F'};
2305 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2307 AssertEquals("#TI05 " + i ,a[i],b[i]);
2312 public void TestInitializeString()
2314 string[] a = {"hola","adios","menos","mas"};
2316 string[] b = {"hola","adios","menos","mas"};
2317 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2319 AssertEquals("#TI06 " + i ,a[i],b[i]);
2324 public void TestInitializeEnum()
2326 enua[] a = {enua.hola,enua.adios,enua.menos,enua.mas};
2328 enua[] b = {enua.hola,enua.adios,enua.menos,enua.mas};
2329 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2331 AssertEquals("#TI07 " + i ,a[i],b[i]);
2336 public void TestInitializeIntNI()
2338 int[] a = new int[20];
2342 AssertEquals("#TI08",b,0);
2347 public void TestInitializeCharNI()
2349 char[] a = new char[20];
2351 foreach(char b in a)
2353 AssertEquals("#TI09",b,0);
2358 public void TestInitializeDoubleNI()
2360 double[] a = new double[20];
2362 foreach(double b in a)
2364 AssertEquals("#TI09",b,0.0);
2369 public void TestInitializeStringNI()
2371 string[] a = new string[20];
2373 foreach(string b in a)
2375 AssertEquals("#TI10",b,null);
2380 public void TestInitializeObjectNI()
2382 object[] a = new object[20];
2384 foreach(object b in a)
2386 AssertEquals("#TI11",b,null);
2391 public void TestInitializeAClassNI()
2393 AClass[] a = new AClass[20];
2395 foreach(AClass b in a)
2397 AssertEquals("#TI12",b,null);
2403 public void TestInitializeAStructNI()
2405 AStruct[] a = new AStruct[20];
2407 foreach(AStruct b in a)
2409 AssertEquals("#TI14",b,new AStruct());
2414 public void TestInitializeAStruct()
2416 AStruct[] a = new AStruct[3];
2420 AStruct[] b = new AStruct[3];
2423 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2425 AssertEquals("#TI15 " + i ,a[i],b[i]);
2430 public void TestInitializeDateTimeNI()
2432 DateTime[] a = new DateTime[20];
2434 foreach(DateTime b in a)
2436 AssertEquals("#TI16",b,new DateTime());
2441 [ExpectedException (typeof (ArgumentNullException))]
2442 public void MoreSort1 ()
2444 Array.Sort (null, 0, 1);
2448 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2449 public void MoreSort2 ()
2451 Array.Sort (arrsort, -1, 3);
2455 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2456 public void MoreSort3 ()
2458 Array.Sort (arrsort, 1, -3);
2462 [ExpectedException (typeof (ArgumentException))]
2463 public void MoreSort4 ()
2465 Array.Sort (arrsort, arrsort.Length, arrsort.Length + 2);
2469 [ExpectedException (typeof (RankException))]
2470 public void MoreSort5 ()
2472 char [,] arr = new char [,] {{'a'}, {'b'}};
2473 Array.Sort (arr, 0, 1);
2477 public void MoreSort6 ()
2479 Array.Sort (arrsort, 0, 0);
2483 [ExpectedException (typeof (ArgumentException))]
2484 public void MoreSort7 ()
2486 Array.Sort (arrsort, arrsort.Length - 1, 2);
2490 [ExpectedException (typeof (ArgumentException))]
2491 public void MoreSort8 ()
2493 Array.Sort (arrsort, 0, arrsort.Length + 1);
2497 public void MoreSort9 ()
2499 Array.Sort (arrsort, null, 0, arrsort.Length, null);
2503 [ExpectedException (typeof (InvalidOperationException))]
2504 public void MoreSort10 ()
2506 object [] array = {true, 'k', SByte.MinValue, Byte.MinValue, (short) 2, 634, (long) 436, (float) 1.1, 1.23, "Hello World"};
2507 Array.Sort (array, (IComparer) null);
2511 public void ClearJaggedArray ()
2513 byte[][] matrix = new byte [8][];
2514 for (int i=0; i < 8; i++) {
2515 matrix [i] = new byte [8];
2516 for (int j=0; j < 8; j++) {
2520 Array.Clear (matrix, 0, 8);
2521 for (int i=0; i < 8; i++) {
2522 AssertNull (i.ToString (), matrix [i]);
2527 public void ClearMultidimentionalArray ()
2529 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2530 Array.Clear (matrix, 0, 2);
2531 AssertEquals ("0,0", 0, matrix [0,0]);
2532 AssertEquals ("0,1", 0, matrix [0,1]);
2533 AssertEquals ("1,0", 2, matrix [1,0]);
2534 AssertEquals ("1,1", 2, matrix [1,1]);
2538 [ExpectedException (typeof (IndexOutOfRangeException))]
2539 public void ClearOutsideMultidimentionalArray ()
2541 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2542 Array.Clear (matrix, 0, 5);
2546 [ExpectedException (typeof (IndexOutOfRangeException))]
2547 public void Clear_IndexOverflow ()
2549 byte[] array = new byte [16];
2550 Array.Clear (array, 4, Int32.MaxValue);
2554 [ExpectedException (typeof (IndexOutOfRangeException))]
2555 public void Clear_LengthOverflow ()
2557 byte[] array = new byte [16];
2558 Array.Clear (array, Int32.MaxValue, 4);
2562 [ExpectedException (typeof (ArgumentException))]
2563 public void Copy_SourceIndexOverflow ()
2565 byte[] array = new byte [16];
2566 Array.Copy (array, Int32.MaxValue, array, 8, 8);
2570 [ExpectedException (typeof (ArgumentException))]
2571 public void Copy_DestinationIndexOverflow ()
2573 byte[] array = new byte [16];
2574 Array.Copy (array, 8, array, Int32.MaxValue, 8);
2578 [ExpectedException (typeof (ArgumentException))]
2579 public void Copy_LengthOverflow ()
2581 byte[] array = new byte [16];
2582 Array.Copy (array, 8, array, 8, Int32.MaxValue);
2586 [ExpectedException (typeof (ArgumentException))]
2587 public void Reverse_IndexOverflow ()
2589 byte[] array = new byte [16];
2590 Array.Reverse (array, Int32.MaxValue, 8);
2594 [ExpectedException (typeof (ArgumentException))]
2595 public void Reverse_LengthOverflow ()
2597 byte[] array = new byte [16];
2598 Array.Reverse (array, 8, Int32.MaxValue);
2601 public struct CharX : IComparable {
2604 public CharX (char c)
2609 public int CompareTo (object obj)
2612 return c.CompareTo (((CharX) obj).c);
2614 return c.CompareTo (obj);
2619 public void BinarySearch_ArgPassingOrder ()
2622 // This tests that arguments are passed to the comprer in the correct
2623 // order. The IComparable of the *array* elements must get called, not
2624 // that of the search object.
2626 CharX [] x = { new CharX ('a'), new CharX ('b'), new CharX ('c') };
2627 AssertEquals (1, Array.BinarySearch (x, 'b'));
2630 class Comparer: IComparer {
2632 private bool called = false;
2634 public bool Called {
2636 bool result = called;
2642 public int Compare (object x, object y)
2650 public void BinarySearch1_EmptyList ()
2652 int[] array = new int[0];
2653 AssertEquals ("BinarySearch", - 1, Array.BinarySearch (array, 0));
2657 public void BinarySearch2_EmptyList ()
2659 int[] array = new int[0];
2660 AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, 0, 0));
2664 public void BinarySearch3_EmptyList ()
2666 Comparer comparer = new Comparer ();
2667 int[] array = new int[0];
2668 AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, comparer));
2669 // bug 77030 - the comparer isn't called for an empty array/list
2670 Assert ("Called", !comparer.Called);
2674 public void BinarySearch4_EmptyList ()
2676 Comparer comparer = new Comparer ();
2677 int[] array = new int[0];
2678 AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, 0, comparer));
2679 // bug 77030 - the comparer isn't called for an empty array/list
2680 Assert ("Called", !comparer.Called);
2685 [ExpectedException (typeof (ArgumentNullException))]
2686 public void AsReadOnly_NullArray ()
2688 Array.AsReadOnly <int> (null);
2692 public void ReadOnly_Count ()
2694 AssertEquals (10, Array.AsReadOnly (new int [10]).Count);
2698 public void ReadOnly_Contains ()
2700 int[] arr = new int [2];
2703 IList<int> a = Array.AsReadOnly (arr);
2705 Assert (a.Contains (3));
2706 Assert (!a.Contains (6));
2710 public void ReadOnly_IndexOf ()
2712 int[] arr = new int [2];
2715 IList<int> a = Array.AsReadOnly (arr);
2717 AssertEquals (0, a.IndexOf (3));
2718 AssertEquals (1, a.IndexOf (5));
2719 AssertEquals (-1, a.IndexOf (6));
2723 public void ReadOnly_Indexer ()
2725 int[] arr = new int [2];
2728 IList<int> a = Array.AsReadOnly (arr);
2730 AssertEquals (3, a [0]);
2731 AssertEquals (5, a [1]);
2733 /* Check that modifications to the original array are visible */
2735 AssertEquals (6, a [0]);
2739 public void ReadOnly_Enumerator ()
2741 int[] arr = new int [10];
2743 for (int i = 0; i < 10; ++i)
2747 foreach (int i in Array.AsReadOnly (arr))
2750 AssertEquals (45, sum);
2754 public void Resize_null ()
2757 Array.Resize (ref arr, 10);
2758 AssertEquals (arr.Length, 10);
2762 public void Test_ContainsAndIndexOf_EquatableItem ()
2764 EquatableClass[] list = new EquatableClass[] {new EquatableClass (0), new EquatableClass (1), new EquatableClass (0)};
2766 AssertEquals ("#0", 0, Array.IndexOf<EquatableClass> (list, list[0]));
2767 AssertEquals ("#1", 0, Array.IndexOf<EquatableClass> (list, new EquatableClass (0)));
2768 AssertEquals ("#2", 2, Array.LastIndexOf<EquatableClass> (list, list[0]));
2769 AssertEquals ("#3", 2, Array.LastIndexOf<EquatableClass> (list, new EquatableClass (0)));
2772 public class EquatableClass : IEquatable<EquatableClass>
2775 public EquatableClass (int x)
2780 public bool Equals (EquatableClass other)
2782 return this._x == other._x;
2789 public void ICollectionNull ()
2791 ICollection<object> test;
2793 test = new List<object>();
2794 AssertEquals ("list<o>", test.Contains (null), false);
2796 test = new object[] {};
2797 AssertEquals ("empty array", test.Contains (null), false);
2799 test = new object[] {null};
2800 AssertEquals ("array with null", test.Contains (null), true);
2802 test = new List<object>(test);
2803 AssertEquals ("List<object> with test", test.Contains (null), true);
2805 test = new object[] {new object()};
2806 AssertEquals ("array with object", test.Contains (null), false);
2808 test = new List<object>(test);
2809 AssertEquals ("array with test", test.Contains (null), false);
2815 enum ByteEnum : byte {}
2816 enum IntEnum : int {}
2819 public void TestByteEnumArrayToByteArray ()
2821 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
2822 byte[] b = new byte[a.Length];
2827 public void TestByteEnumArrayToIntArray ()
2829 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
2830 int[] b = new int[a.Length];
2835 [ExpectedException (typeof (ArrayTypeMismatchException))]
2836 public void TestIntEnumArrayToByteArray ()
2838 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
2839 byte[] b = new byte[a.Length];
2844 public void TestIntEnumArrayToIntArray ()
2846 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
2847 int[] b = new int[a.Length];