1 // ArrayTest.cs - NUnit Test Cases for the System.Array class
3 // David Brandt (bucky@keystreams.com)
4 // Eduardo Garcia (kiwnix@yahoo.es)
6 // (C) Ximian, Inc. http://www.ximian.com
7 // Copyright (C) 2004 Novell (http://www.novell.com)
10 using NUnit.Framework;
12 using System.Collections;
13 using System.Globalization;
14 using System.Reflection;
15 using System.Collections.Generic;
17 namespace MonoTests.System
20 enum enua {hola,adios,mas,menos};
46 public override bool Equals (object obj)
51 public override int GetHashCode ()
57 //End Auxiliary Things
60 public class ArrayTest
62 char [] arrsort = {'d', 'b', 'f', 'e', 'a', 'c'};
67 public void TestIsFixedSize() {
69 Assert.IsTrue (a1.IsFixedSize, "All arrays are fixed");
73 public void TestIsReadOnly() {
75 Assert.IsTrue (!a1.IsReadOnly, "No array is readonly");
79 public void TestIsSynchronized() {
81 Assert.IsTrue (!a1.IsSynchronized, "No array is synchronized");
85 public void TestLength() {
88 Assert.AreEqual (0, a1.Length, "Zero length array");
92 Assert.AreEqual (1, a1.Length, "One-length array");
95 char[] a1 = {'c', 'c'};
96 Assert.AreEqual (2, a1.Length, "Two-length array");
101 public void TestRank() {
102 char[] a1 = { 'c', 'd', 'e' };
103 Assert.AreEqual (1, a1.Rank, "Rank one");
105 char[,] a2 = new Char[3,3];
106 Assert.AreEqual (2, a2.Rank, "Rank two");
108 char[,,] a3 = new Char[3,3,3];
109 Assert.AreEqual (3, a3.Rank, "Rank three");
113 public void TestBinarySearch1() {
114 bool errorThrown = false;
116 Array.BinarySearch(null, "blue");
117 } catch (ArgumentNullException) {
120 Assert.IsTrue (errorThrown, "#B01");
123 char[,] c1 = new Char[2,2];
124 Array.BinarySearch(c1, "needle");
125 } catch (RankException) {
128 Assert.IsTrue (errorThrown, "#B02");
131 char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
132 Assert.IsTrue (Array.BinarySearch(arr, 'c') >= 3, "#B05");
133 Assert.IsTrue (Array.BinarySearch(arr, 'c') < 6, "#B06");
136 char[] arr = {'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
137 Assert.AreEqual (-4, Array.BinarySearch(arr, 'c'), "#B07");
140 char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
141 Assert.AreEqual (-9, Array.BinarySearch(arr, 'e'), "#B08");
146 public void TestBinarySearch2() {
147 bool errorThrown = false;
149 Array.BinarySearch(null, 0, 1, "blue");
150 } catch (ArgumentNullException) {
153 Assert.IsTrue (errorThrown, "#B20");
156 char[,] c1 = new Char[2,2];
157 Array.BinarySearch(c1, 0, 1, "needle");
158 } catch (RankException) {
161 Assert.IsTrue (errorThrown, "#B21");
165 Array.BinarySearch(c1, -1, 1, 'a');
166 } catch (ArgumentOutOfRangeException) {
169 Assert.IsTrue (errorThrown, "#B22");
173 Array.BinarySearch(c1, 0, -1, 'a');
174 } catch (ArgumentOutOfRangeException) {
177 Assert.IsTrue (errorThrown, "#B23");
181 Array.BinarySearch(c1, 0, 4, 'a');
182 } catch (ArgumentException) {
185 Assert.IsTrue (errorThrown, "#B24");
188 char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
189 Assert.IsTrue (Array.BinarySearch(arr, 2, 8, 'c') >= 5, "#B26");
190 Assert.IsTrue (Array.BinarySearch(arr, 2, 8, 'c') < 8, "#B27");
193 char[] arr = {'z', 'z', 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
194 Assert.AreEqual (-6, Array.BinarySearch(arr, 2, 8, 'c'), "#B28");
197 char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
198 Assert.AreEqual (-11, Array.BinarySearch(arr, 2, 8, 'e'), "#B29");
202 public void TestBinarySearch3()
204 int[] array = new int[100];
206 for (int i = 0; i < 100; i++)
209 Assert.AreEqual (49, Array.BinarySearch(array, 10), "#B30");
213 public void BinarySearch_NullValue ()
215 int[] array = new int[1];
216 Assert.AreEqual (-1, Array.BinarySearch (array, null), "I=a,o");
217 Assert.AreEqual (-1, Array.BinarySearch (array, null, null), "I=a,o,c");
218 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 1, null), "I=a,i,i,o");
219 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 1, null, null), "I=a,i,i,o,c");
221 object[] o = new object [3] { this, this, null };
222 Assert.AreEqual (-1, Array.BinarySearch (o, null), "O=a,o");
223 Assert.AreEqual (-1, Array.BinarySearch (o, null, null), "O=a,o,c");
224 Assert.AreEqual (-1, Array.BinarySearch (o, 0, 3, null), "O=a,i,i,o");
225 Assert.AreEqual (-1, Array.BinarySearch (o, 0, 3, null, null), "O=a,i,i,o,c");
228 class TestComparer7 : IComparer<int>
230 public int Compare (int x, int y)
233 throw new ApplicationException ();
235 return x.CompareTo (y);
240 public void BinarySearch_WithComparer ()
242 var a = new int[] { 2, 6, 9 };
243 Assert.AreEqual (-3, Array.BinarySearch (a, 7, new TestComparer7 ()));
247 public void TestClear() {
248 bool errorThrown = false;
250 Array.Clear(null, 0, 1);
251 } catch (ArgumentNullException) {
254 Assert.IsTrue (errorThrown, "#C01");
256 int[] i1 = { 1, 2, 3, 4 };
258 int[] compare = {1,2,3,4};
259 Assert.AreEqual (compare[0], i1[0], "#C02");
260 Assert.AreEqual (compare[1], i1[1], "#C03");
261 Assert.AreEqual (compare[2], i1[2], "#C04");
262 Assert.AreEqual (compare[3], i1[3], "#C05");
264 Array.Clear(i1, 3, 1);
266 int[] compare = {1,2,3,0};
267 Assert.AreEqual (compare[0], i1[0], "#C06");
268 Assert.AreEqual (compare[1], i1[1], "#C07");
269 Assert.AreEqual (compare[2], i1[2], "#C08");
270 Assert.AreEqual (compare[3], i1[3], "#C09");
272 Array.Clear(i1, 1, 1);
274 int[] compare = {1,0,3,0};
275 Assert.AreEqual (compare[0], i1[0], "#C10");
276 Assert.AreEqual (compare[1], i1[1], "#C11");
277 Assert.AreEqual (compare[2], i1[2], "#C12");
278 Assert.AreEqual (compare[3], i1[3], "#C13");
280 Array.Clear(i1, 1, 3);
282 int[] compare = {1,0,0,0};
283 Assert.AreEqual (compare[0], i1[0], "#C14");
284 Assert.AreEqual (compare[1], i1[1], "#C15");
285 Assert.AreEqual (compare[2], i1[2], "#C16");
286 Assert.AreEqual (compare[3], i1[3], "#C17");
289 string[] s1 = { "red", "green", "blue" };
290 Array.Clear(s1, 0, 3);
292 string[] compare = {null, null, null};
293 Assert.AreEqual (compare[0], s1[0], "#C18");
294 Assert.AreEqual (compare[1], s1[1], "#C19");
295 Assert.AreEqual (compare[2], s1[2], "#C20");
300 public void TestClone() {
301 char[] c1 = {'a', 'b', 'c'};
302 char[] c2 = (char[])c1.Clone();
303 Assert.AreEqual (c1[0], c2[0], "#D01");
304 Assert.AreEqual (c1[1], c2[1], "#D02");
305 Assert.AreEqual (c1[2], c2[2], "#D03");
307 char[] d10 = {'a', 'b'};
308 char[] d11 = {'a', 'c'};
309 char[] d12 = {'b', 'c'};
310 char[][] d1 = {d10, d11, d12};
311 char[][] d2 = (char[][])d1.Clone();
312 Assert.AreEqual (d1[0], d2[0], "#D04");
313 Assert.AreEqual (d1[1], d2[1], "#D05");
314 Assert.AreEqual (d1[2], d2[2], "#D06");
317 Assert.AreEqual (d1[0], d2[0], "#D07");
321 public void TestMemberwiseClone () {
322 int[] array = new int[] { 1, 2, 3 };
323 MethodBase mi = array.GetType ().GetMethod("MemberwiseClone",
324 BindingFlags.Instance | BindingFlags.NonPublic);
325 int[] res = (int[])mi.Invoke (array, null);
326 Assert.AreEqual (3, res.Length);
329 [Test] public void TestIndexer ()
331 int [] a = new int [10];
335 Assert.Fail ("IList.this [-1] should throw");
336 } catch (IndexOutOfRangeException) {
339 Assert.Fail ("Should have thrown an IndexOutOfRangeException");
344 public void TestCopy() {
346 bool errorThrown = false;
349 Array.Copy(c1, null, 1);
350 } catch (ArgumentNullException) {
353 Assert.IsTrue (errorThrown, "#E01");
356 bool errorThrown = false;
359 Array.Copy(null, c1, 1);
360 } catch (ArgumentNullException) {
363 Assert.IsTrue (errorThrown, "#E02");
366 bool errorThrown = false;
368 Char[] c1 = new Char[1];
369 Char[,] c2 = new Char[1,1];
370 Array.Copy(c1, c2, 1);
371 } catch (RankException) {
374 Assert.IsTrue (errorThrown, "#E03");
377 bool errorThrown = false;
379 Char[] c1 = new Char[1];
380 string[] s1 = new String[1];
381 Array.Copy(c1, s1, 1);
382 } catch (ArrayTypeMismatchException) {
385 Assert.IsTrue (errorThrown, "#E04");
388 bool errorThrown = false;
390 Char[] c1 = new Char[1];
391 Object[] o1 = new Object[1];
393 Array.Copy(o1, c1, 1);
394 } catch (InvalidCastException) {
397 Assert.IsTrue (errorThrown, "#E05");
400 bool errorThrown = false;
402 Char[] c1 = new Char[1];
403 Char[] c2 = new Char[1];
404 Array.Copy(c1, c2, -1);
405 } catch (ArgumentOutOfRangeException) {
408 Assert.IsTrue (errorThrown, "#E06");
411 bool errorThrown = false;
413 Char[] c1 = new Char[1];
414 Char[] c2 = new Char[2];
415 Array.Copy(c1, c2, 2);
416 } catch (ArgumentException) {
419 Assert.IsTrue (errorThrown, "#E07");
422 bool errorThrown = false;
424 Char[] c1 = new Char[1];
425 Char[] c2 = new Char[2];
426 Array.Copy(c2, c1, 2);
427 } catch (ArgumentException) {
430 Assert.IsTrue (errorThrown, "#E08");
433 char[] orig = {'a', 'b', 'd', 'a'};
434 char[] copy = new Char[4];
435 Array.Copy(orig, copy, 4);
436 for (int i = 0; i < orig.Length; i++) {
437 Assert.AreEqual (orig[i], copy[i], "#E09(" + i + ")");
439 Array.Clear(copy, 0, copy.Length);
440 for (int i = 0; i < orig.Length; i++) {
441 Assert.AreEqual ((char)0, copy[i], "#E10(" + i + ")");
443 Array.Copy(orig, copy, 2);
444 Assert.AreEqual (orig[0], copy[0], "#E11");
445 Assert.AreEqual (orig[1], copy[1], "#E12");
446 Assert.IsTrue (orig[2] != copy[2], "#E13");
447 Assert.IsTrue (orig[3] != copy[3], "#E14");
451 public void TestCopy2() {
453 bool errorThrown = false;
455 Char[] c1 = new Char[2];
456 Char[] c2 = new Char[2];
457 Array.Copy(c2, 1, c1, 0, 2);
458 } catch (ArgumentException) {
461 Assert.IsTrue (errorThrown, "#E31");
464 bool errorThrown = false;
466 Char[] c1 = new Char[2];
467 Char[] c2 = new Char[2];
468 Array.Copy(c2, 0, c1, 1, 2);
469 } catch (ArgumentException) {
472 Assert.IsTrue (errorThrown, "#E32");
475 char[] orig = {'a', 'b', 'd', 'a'};
476 char[] copy = new Char[4];
477 Array.Copy(orig, 1, copy, 1, 3);
478 Assert.IsTrue (copy[0] != orig[0], "#E33");
479 for (int i = 1; i < orig.Length; i++) {
480 Assert.AreEqual (orig[i], copy[i], "#E34(" + i + ")");
482 Array.Clear(copy, 0, copy.Length);
483 Array.Copy(orig, 1, copy, 0, 2);
484 Assert.AreEqual (orig[1], copy[0], "#E35");
485 Assert.AreEqual (orig[2], copy[1], "#E36");
486 Assert.IsTrue (copy[2] != orig[2], "#E37");
487 Assert.IsTrue (copy[3] != orig[3], "#E38");
491 [ExpectedException (typeof (InvalidCastException))]
492 public void Copy_InvalidCast () {
493 object[] arr1 = new object [10];
494 Type[] arr2 = new Type [10];
496 arr1 [0] = new object ();
498 Array.Copy (arr1, 0, arr2, 0, 10);
502 public void TestCopyTo() {
504 bool errorThrown = false;
506 Char[] c1 = new Char[2];
508 } catch (ArgumentNullException) {
511 Assert.IsTrue (errorThrown, "#E61");
514 bool errorThrown = false;
516 Char[] c1 = new Char[2];
517 Char[,] c2 = new Char[2,2];
519 } catch (ArgumentException) {
522 #if TARGET_JVM // This is really implementation dependent behaviour.
523 catch (RankException) {
527 Assert.IsTrue (errorThrown, "#E62");
530 bool errorThrown = false;
532 Char[,] c1 = new Char[2,2];
533 Char[] c2 = new Char[2];
535 } catch (RankException) {
538 Assert.IsTrue (errorThrown, "#E63");
541 bool errorThrown = false;
543 Char[,] c1 = new Char[2,2];
544 Char[] c2 = new Char[2];
546 } catch (RankException) {
549 Assert.IsTrue (errorThrown, "#E64");
552 bool errorThrown = false;
554 Char[] c1 = new Char[2];
555 Char[] c2 = new Char[2];
557 } catch (ArgumentOutOfRangeException) {
560 Assert.IsTrue (errorThrown, "#E65");
563 bool errorThrown = false;
565 Char[] c1 = new Char[2];
566 Char[] c2 = new Char[2];
568 } catch (ArgumentException) {
571 Assert.IsTrue (errorThrown, "#E66");
574 bool errorThrown = false;
576 Char[] c1 = new Char[2];
577 Char[] c2 = new Char[2];
579 } catch (ArgumentException) {
582 Assert.IsTrue (errorThrown, "#E67");
586 bool errorThrown = false;
588 String[] c1 = new String[2];
589 // TODO: this crashes mono if there are null
590 // values in the array.
593 Char[] c2 = new Char[2];
597 } catch (ArrayTypeMismatchException) {
600 Assert.IsTrue (errorThrown, "#E68");
603 Char[] orig = {'a', 'b', 'c', 'd'};
604 Char[] copy = new Char[10];
605 Array.Clear(copy, 0, copy.Length);
606 orig.CopyTo(copy, 3);
607 Assert.AreEqual ((char)0, copy[0], "#E69");
608 Assert.AreEqual ((char)0, copy[1], "#E70");
609 Assert.AreEqual ((char)0, copy[2], "#E71");
610 Assert.AreEqual (orig[0], copy[3], "#E72");
611 Assert.AreEqual (orig[1], copy[4], "#E73");
612 Assert.AreEqual (orig[2], copy[5], "#E74");
613 Assert.AreEqual (orig[3], copy[6], "#E75");
614 Assert.AreEqual ((char)0, copy[7], "#E76");
615 Assert.AreEqual ((char)0, copy[8], "#E77");
616 Assert.AreEqual ((char)0, copy[9], "#E78");
619 // The following is valid and must not throw an exception.
620 bool errorThrown = false;
622 int[] src = new int [0];
623 int[] dest = new int [0];
624 src.CopyTo (dest, 0);
625 } catch (ArgumentException) {
628 Assert.IsTrue (!errorThrown, "#E79");
633 bool errorThrown = false;
635 CClass[] src = new CClass [] { new CClass () };
636 BClass[] dest = new BClass [1];
638 src.CopyTo (dest, 0);
640 } catch (ArrayTypeMismatchException) {
643 Assert.IsTrue (errorThrown, "#E80");
648 public void TestCreateInstance() {
650 bool errorThrown = false;
652 Array.CreateInstance(null, 12);
653 } catch (ArgumentNullException) {
656 Assert.IsTrue (errorThrown, "#F01");
659 bool errorThrown = false;
661 Array.CreateInstance(Type.GetType("System.Char"), -3);
662 } catch (ArgumentOutOfRangeException) {
665 Assert.IsTrue (errorThrown, "#F02");
668 bool errorThrown = false;
670 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
671 } catch (ArgumentNullException) {
674 Assert.IsTrue (errorThrown, "#F03a");
678 bool errorThrown = false;
680 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
681 } catch (ArgumentNullException) {
684 Assert.IsTrue (errorThrown, "#F03b");
686 #if !TARGET_JVM // Arrays lower bounds are not supported for TARGET_JVM
688 bool errorThrown = false;
690 Array.CreateInstance(Type.GetType("System.Char"), null, null);
691 } catch (ArgumentNullException) {
694 Assert.IsTrue (errorThrown, "#F04");
698 bool errorThrown = false;
700 int[] lengths = new int [0];
701 Array.CreateInstance(Type.GetType("System.Char"), lengths);
702 } catch (ArgumentException) {
705 Assert.IsTrue (errorThrown, "#F05");
707 #if !TARGET_JVM // CreateInstance with lower bounds not supported for TARGET_JVM
709 bool errorThrown = false;
711 int[] lengths = new int [1];
712 int[] bounds = new int [2];
713 Array.CreateInstance(Type.GetType("System.Char"), lengths, bounds);
715 } catch (ArgumentException) {
718 Assert.IsTrue (errorThrown, "#F06");
721 char[] c1 = (char[])Array.CreateInstance(Type.GetType("System.Char"), 12);
722 Assert.AreEqual (12, c1.Length, "#F07");
724 Array c2 = Array.CreateInstance(Type.GetType("System.Char"), 12, 5);
725 Assert.AreEqual (2, c2.Rank, "#F08");
726 Assert.AreEqual (60, c2.Length, "#F09");
730 int[] lengths = { 3 };
731 int[] bounds = { 5 };
732 int[] src = { 512, 718, 912 };
733 Array array = Array.CreateInstance(typeof(int), lengths, bounds);
735 Assert.AreEqual (3, array.Length, "#F10");
736 Assert.AreEqual (5, array.GetLowerBound(0), "#F11");
737 Assert.AreEqual (7, array.GetUpperBound(0), "#F12");
739 src.CopyTo (array, 5);
741 for (int i = 0; i < src.Length; i++)
742 Assert.AreEqual (src[i], array.GetValue(i+5), "#F13(" + i + ")");
745 // Test that a 1 dimensional array with 0 lower bound is the
746 // same as an szarray
747 Type szarrayType = new int [10].GetType ();
748 Assert.IsTrue (szarrayType == (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0})).GetType ());
749 Assert.IsTrue (szarrayType != (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {1})).GetType ());
754 [ExpectedException (typeof (ArgumentNullException))]
755 public void TestCreateInstance2 ()
757 Array.CreateInstance (typeof (Int32), (int[])null);
761 [ExpectedException (typeof (ArgumentNullException))]
762 public void TestCreateInstance2b ()
764 Array.CreateInstance (typeof (Int32), (long[])null);
768 public void TestGetEnumerator() {
769 String[] s1 = {"this", "is", "a", "test"};
770 IEnumerator en = s1.GetEnumerator ();
771 Assert.IsNotNull (en, "#G01");
773 Assert.IsTrue (en.MoveNext (), "#G02");
774 Assert.AreEqual ("this", en.Current, "#G03");
775 Assert.IsTrue (en.MoveNext (), "#G04");
776 Assert.AreEqual ("is", en.Current, "#G05");
777 Assert.IsTrue (en.MoveNext (), "#G06");
778 Assert.AreEqual ("a", en.Current, "#G07");
779 Assert.IsTrue (en.MoveNext (), "#G08");
780 Assert.AreEqual ("test", en.Current, "#G09");
781 Assert.IsTrue (!en.MoveNext (), "#G10");
784 Assert.IsTrue (en.MoveNext (), "#G11");
785 Assert.AreEqual ("this", en.Current, "#G12");
787 // mutation does not invalidate array enumerator!
788 s1.SetValue ("change", 1);
789 Assert.IsTrue (en.MoveNext (), "#G13");
790 Assert.AreEqual ("change", en.Current, "#G14");
794 public void TestGetEnumeratorMultipleDimension() {
795 String[,] s1 = {{"this", "is"}, {"a", "test"}};
796 IEnumerator en = s1.GetEnumerator ();
797 Assert.IsNotNull (en, "#AA01");
799 Assert.IsTrue (en.MoveNext (), "#AA02");
800 Assert.AreEqual ("this", en.Current, "#AA03");
801 Assert.IsTrue (en.MoveNext (), "#AA04");
802 Assert.AreEqual ("is", en.Current, "#AA05");
803 Assert.IsTrue (en.MoveNext (), "#AA06");
804 Assert.AreEqual ("a", en.Current, "#AA07");
805 Assert.IsTrue (en.MoveNext (), "#AA08");
806 Assert.AreEqual ("test", en.Current, "#AA09");
807 Assert.IsTrue (!en.MoveNext (), "#AA10");
810 Assert.IsTrue (en.MoveNext (), "#AA11");
811 Assert.AreEqual ("this", en.Current, "#AA12");
814 // mutation does not invalidate array enumerator!
815 s1.SetValue ("change", idxs);
816 Assert.IsTrue (en.MoveNext (), "#AA13");
817 Assert.AreEqual ("change", en.Current, "#AA14");
821 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
822 public void TestGetEnumeratorNonZeroLowerBounds() {
823 int[] myLengthsArray = new int[2] { 3, 5 };
824 int[] myBoundsArray = new int[2] { 2, 3 };
826 Array myArray=Array.CreateInstance( typeof(String), myLengthsArray, myBoundsArray );
827 for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
828 for ( int j = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++ ) {
829 int[] myIndicesArray = new int[2] { i, j };
830 myArray.SetValue( Convert.ToString(i) + j, myIndicesArray );
832 IEnumerator en = myArray.GetEnumerator ();
833 Assert.IsNotNull (en, "#AB01");
835 // check the first couple of values
836 Assert.IsTrue (en.MoveNext (), "#AB02");
837 Assert.AreEqual ("23", en.Current, "#AB03");
838 Assert.IsTrue (en.MoveNext (), "#AB04");
839 Assert.AreEqual ("24", en.Current, "#AB05");
841 // then check the last element's value
844 lastElement = (string)en.Current;
845 } while (en.MoveNext());
846 Assert.AreEqual ("47", lastElement, "#AB06");
850 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
851 public void TestIList_Add () {
852 int[] myLengthsArray = new int[2] { 3, 5 };
853 int[] myBoundsArray = new int[2] { 2, 3 };
855 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
857 ((IList)myArray).Add ("can not");
858 Assert.Fail ("IList.Add should throw");
860 catch (NotSupportedException) {
864 Assert.Fail ("IList.Add threw wrong exception type");
867 Assert.Fail ("IList.Add shouldn't get this far");
871 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
872 public void TestIList_Insert () {
873 int[] myLengthsArray = new int[2] { 3, 5 };
874 int[] myBoundsArray = new int[2] { 2, 3 };
876 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
878 ((IList)myArray).Insert (0, "can not");
879 Assert.Fail ("IList.Insert should throw");
881 catch (NotSupportedException) {
885 Assert.Fail ("IList.Insert threw wrong exception type");
888 Assert.Fail ("IList.Insert shouldn't get this far");
892 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
893 public void TestIList_Remove () {
894 int[] myLengthsArray = new int[2] { 3, 5 };
895 int[] myBoundsArray = new int[2] { 2, 3 };
897 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
899 ((IList)myArray).Remove ("can not");
900 Assert.Fail ("IList.Remove should throw");
902 catch (NotSupportedException) {
906 Assert.Fail ("IList.Remove threw wrong exception type");
909 Assert.Fail ("IList.Remove shouldn't get this far");
913 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
914 public void TestIList_RemoveAt () {
915 int[] myLengthsArray = new int[2] { 3, 5 };
916 int[] myBoundsArray = new int[2] { 2, 3 };
918 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
920 ((IList)myArray).RemoveAt (0);
921 Assert.Fail ("IList.RemoveAt should throw");
923 catch (NotSupportedException) {
927 Assert.Fail ("IList.RemoveAt threw wrong exception type");
930 Assert.Fail ("IList.RemoveAt shouldn't get this far");
934 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
935 public void TestIList_Contains () {
936 int[] myLengthsArray = new int[2] { 3, 5 };
937 int[] myBoundsArray = new int[2] { 2, 3 };
939 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
942 bool b = ((IList)myArray).Contains ("23");
943 Assert.Fail ("IList.Contains should throw with multi-dimensional arrays");
945 catch (RankException) {
946 int[] iArr = new int[3] { 1, 2, 3};
947 // check the first and last items
948 Assert.IsTrue (((IList)iArr).Contains (1), "AC01");
949 Assert.IsTrue (((IList)iArr).Contains (3), "AC02");
951 // and one that is definately not there
952 Assert.IsTrue (!((IList)iArr).Contains (42), "AC03");
956 Assert.Fail ("Should not get here");
960 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
961 public void TestIList_IndexOf () {
962 int[] myLengthsArray = new int[2] { 3, 5 };
963 int[] myBoundsArray = new int[2] { 2, 3 };
965 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
968 bool b = ((IList)myArray).Contains ("23");
969 Assert.Fail ("IList.Contains should throw with multi-dimensional arrays");
971 catch (RankException) {
972 int[] iArr = new int[3] { 1, 2, 3};
973 // check the first and last items
974 Assert.AreEqual (0, ((IList)iArr).IndexOf (1), "AD01");
975 Assert.AreEqual (2, ((IList)iArr).IndexOf (3), "AD02");
977 // and one that is definately not there
978 Assert.AreEqual (-1, ((IList)iArr).IndexOf (42), "AD03");
980 catch (Exception e) {
981 Assert.Fail ("Unexpected exception: " + e.ToString());
984 // check that wierd case whem lowerbound is Int32.MinValue,
985 // so that IndexOf() needs to return Int32.MaxValue when it cannot find the object
986 int[] myLengthArray = new int[1] { 3 };
987 int[] myBoundArray = new int[1] { Int32.MinValue };
988 Array myExtremeArray=Array.CreateInstance ( typeof(String), myLengthArray, myBoundArray );
989 Assert.AreEqual (Int32.MaxValue, ((IList)myExtremeArray).IndexOf (42), "AD04");
994 public void TestGetLength() {
996 bool errorThrown = false;
998 char[] c1 = {'a', 'b', 'c'};
1000 } catch (IndexOutOfRangeException) {
1003 Assert.IsTrue (errorThrown, "#H01");
1006 bool errorThrown = false;
1008 char[] c1 = {'a', 'b', 'c'};
1010 } catch (IndexOutOfRangeException) {
1013 Assert.IsTrue (errorThrown, "#H02");
1016 char[] c2 = new Char[5];
1017 Assert.AreEqual (5, c2.GetLength(0), "#H03");
1019 char[,] c3 = new Char[6,7];
1020 Assert.AreEqual (6, c3.GetLength(0), "#H04");
1021 Assert.AreEqual (7, c3.GetLength(1), "#H05");
1025 public void TestGetLowerBound() {
1027 bool errorThrown = false;
1029 char[] c = {'a', 'b', 'c'};
1030 c.GetLowerBound(-1);
1031 } catch (IndexOutOfRangeException) {
1034 Assert.IsTrue (errorThrown, "#H31");
1037 bool errorThrown = false;
1039 char[] c = {'a', 'b', 'c'};
1041 } catch (IndexOutOfRangeException) {
1044 Assert.IsTrue (errorThrown, "#H32");
1047 char[] c1 = new Char[5];
1048 Assert.AreEqual (0, c1.GetLowerBound(0), "#H33");
1050 char[,] c2 = new Char[4,4];
1051 Assert.AreEqual (0, c2.GetLowerBound(0), "#H34");
1052 Assert.AreEqual (0, c2.GetLowerBound(1), "#H35");
1056 public void TestGetUpperBound() {
1058 bool errorThrown = false;
1060 char[] c = {'a', 'b', 'c'};
1061 c.GetUpperBound(-1);
1062 } catch (IndexOutOfRangeException) {
1065 Assert.IsTrue (errorThrown, "#H61");
1068 bool errorThrown = false;
1070 char[] c = {'a', 'b', 'c'};
1072 } catch (IndexOutOfRangeException) {
1075 Assert.IsTrue (errorThrown, "#H62");
1078 char[] c1 = new Char[5];
1079 Assert.AreEqual (4, c1.GetUpperBound(0), "#H63");
1081 char[,] c2 = new Char[4,6];
1082 Assert.AreEqual (3, c2.GetUpperBound(0), "#H64");
1083 Assert.AreEqual (5, c2.GetUpperBound(1), "#H65");
1087 public void TestGetValue1() {
1089 bool errorThrown = false;
1091 char[,] c = new Char[2,2];
1093 } catch (ArgumentException) {
1096 Assert.IsTrue (errorThrown, "#I01");
1099 bool errorThrown = false;
1101 char[] c = {'a', 'b', 'c'};
1103 } catch (IndexOutOfRangeException) {
1106 Assert.IsTrue (errorThrown, "#I02");
1109 bool errorThrown = false;
1111 char[] c = {'a', 'b', 'c'};
1113 } catch (IndexOutOfRangeException) {
1116 Assert.IsTrue (errorThrown, "#I03");
1119 char[] c1 = {'a', 'b', 'c', 'd'};
1120 for (int i = 0; i < c1.Length; i++) {
1121 Assert.AreEqual (c1[i], c1.GetValue(i), "#I04(" + i + ")");
1126 public void TestGetValue2() {
1128 bool errorThrown = false;
1130 char[] c = new Char[2];
1132 } catch (ArgumentException) {
1135 Assert.IsTrue (errorThrown, "#I21");
1138 bool errorThrown = false;
1140 char[,] c = new Char[2,2];
1142 } catch (IndexOutOfRangeException) {
1145 Assert.IsTrue (errorThrown, "#I22");
1148 bool errorThrown = false;
1150 char[,] c = new Char[2,2];
1152 } catch (IndexOutOfRangeException) {
1155 Assert.IsTrue (errorThrown, "#I23");
1158 char[,] c1 = new Char[4,6];
1159 for (int i = 0; i < 24; i++) {
1162 c1[first,second] = (char)(((int)'a')+i);
1164 for (int i = 0; i < c1.GetLength(0); i++) {
1165 for (int j = 0; j < c1.GetLength(1); j++) {
1166 Assert.AreEqual (c1[i, j], c1.GetValue(i, j), "#I24(" + i + "," + j + ")");
1172 public void TestGetValue3() {
1174 bool errorThrown = false;
1176 char[] c = new Char[2];
1178 } catch (ArgumentException) {
1181 Assert.IsTrue (errorThrown, "#I41");
1184 bool errorThrown = false;
1186 char[,,] c = new Char[2,2,2];
1187 c.GetValue(-1, 1, 1);
1188 } catch (IndexOutOfRangeException) {
1191 Assert.IsTrue (errorThrown, "#I42");
1194 bool errorThrown = false;
1196 char[,,] c = new Char[2,2,2];
1198 } catch (IndexOutOfRangeException) {
1201 Assert.IsTrue (errorThrown, "#I43");
1204 char[,,] c1 = new Char[4,2,3];
1205 for (int i = 0; i < 24; i++) {
1207 int remains = i % 6;
1208 int second = remains / 3;
1209 int third = remains % 3;
1210 c1[first,second, third] = (char)(((int)'a')+i);
1212 for (int i = 0; i < c1.GetLength(0); i++) {
1213 for (int j = 0; j < c1.GetLength(1); j++) {
1214 for (int k = 0; k < c1.GetLength(2); k++) {
1215 Assert.AreEqual (c1[i, j, k], c1.GetValue(i, j, k), "#I44(" + i + "," + j + ")");
1222 [ExpectedException (typeof (ArgumentNullException))]
1223 public void TestGetValueLongArray ()
1225 char[] c = new Char[2];
1226 c.GetValue((long [])null);
1230 public void TestGetValueN() {
1232 bool errorThrown = false;
1234 char[] c = new Char[2];
1235 c.GetValue((int [])null);
1236 } catch (ArgumentNullException) {
1239 Assert.IsTrue (errorThrown, "#I61a");
1242 bool errorThrown = false;
1244 char[] c = new Char[2];
1245 int[] coords = {1, 1};
1247 } catch (ArgumentException) {
1250 Assert.IsTrue (errorThrown, "#I62");
1253 bool errorThrown = false;
1255 char[,] c = new Char[2,2];
1256 int[] coords = {-1, 1};
1258 } catch (IndexOutOfRangeException) {
1261 Assert.IsTrue (errorThrown, "#I63");
1264 bool errorThrown = false;
1266 char[,] c = new Char[2,2];
1267 int[] coords = {4, 1};
1269 } catch (IndexOutOfRangeException) {
1272 Assert.IsTrue (errorThrown, "#I64");
1275 char[,] c1 = new Char[4,6];
1276 for (int i = 0; i < 24; i++) {
1279 c1[first,second] = (char)(((int)'a')+i);
1281 for (int i = 0; i < c1.GetLength(0); i++) {
1282 for (int j = 0; j < c1.GetLength(1); j++) {
1283 int[] coords = {i, j};
1284 Assert.AreEqual (c1[i, j], c1.GetValue(coords), "#I65(" + i + "," + j + ")");
1290 public void TestIndexOf1() {
1292 bool errorThrown = false;
1294 Array.IndexOf(null, "huh?");
1295 } catch (ArgumentNullException) {
1298 Assert.IsTrue (errorThrown, "#J01");
1301 bool errorThrown = false;
1303 char[,] c = new Char[2,2];
1304 Array.IndexOf(c, "huh?");
1305 } catch (RankException) {
1308 Assert.IsTrue (errorThrown, "#J02");
1311 String[] s1 = {"this", "is", "a", "test"};
1312 Assert.AreEqual (-1, Array.IndexOf(s1, null), "#J03");
1313 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing"), "#J04");
1314 Assert.AreEqual (0, Array.IndexOf(s1, "this"), "#J05");
1315 Assert.AreEqual (3, Array.IndexOf(s1, "test"), "#J06");
1319 public void TestIndexOf2() {
1321 bool errorThrown = false;
1323 Array.IndexOf(null, "huh?", 0);
1324 } catch (ArgumentNullException) {
1327 Assert.IsTrue (errorThrown, "#J21");
1330 bool errorThrown = false;
1332 char[,] c = new Char[2,2];
1333 Array.IndexOf(c, "huh?", 0);
1334 } catch (RankException) {
1337 Assert.IsTrue (errorThrown, "#J22");
1340 bool errorThrown = false;
1342 char[] c = new Char[2];
1343 Array.IndexOf(c, "huh?", 3);
1344 } catch (ArgumentOutOfRangeException) {
1347 Assert.IsTrue (errorThrown, "#J23");
1350 String[] s1 = {"this", "is", "really", "a", "test"};
1351 Assert.AreEqual (-1, Array.IndexOf(s1, null, 1), "#J24");
1352 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing", 1), "#J25");
1353 Assert.AreEqual (-1, Array.IndexOf(s1, "this", 1), "#J26");
1354 Assert.AreEqual (1, Array.IndexOf(s1, "is", 1), "#J27");
1355 Assert.AreEqual (4, Array.IndexOf(s1, "test", 1), "#J28");
1359 public void TestIndexOf3() {
1361 bool errorThrown = false;
1363 Array.IndexOf(null, "huh?", 0, 1);
1364 } catch (ArgumentNullException) {
1367 Assert.IsTrue (errorThrown, "#J41");
1370 bool errorThrown = false;
1372 char[,] c = new Char[2,2];
1373 Array.IndexOf(c, "huh?", 0, 1);
1374 } catch (RankException) {
1377 Assert.IsTrue (errorThrown, "#J42");
1380 bool errorThrown = false;
1382 char[] c = new Char[2];
1383 Array.IndexOf(c, "huh?", 3, 1);
1384 } catch (ArgumentOutOfRangeException) {
1387 Assert.IsTrue (errorThrown, "#J43");
1390 bool errorThrown = false;
1392 char[] c = new Char[2];
1393 Array.IndexOf(c, "huh?", 0, 5);
1394 } catch (ArgumentOutOfRangeException) {
1397 Assert.IsTrue (errorThrown, "#J44");
1400 String[] s1 = {"this", "is", "really", "a", "test"};
1401 Assert.AreEqual (-1, Array.IndexOf(s1, null, 1, 3), "#J45");
1402 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing", 1, 3), "#J46");
1403 Assert.AreEqual (-1, Array.IndexOf(s1, "this", 1, 3), "#J47");
1404 Assert.AreEqual (1, Array.IndexOf(s1, "is", 1, 3), "#J48");
1405 Assert.AreEqual (-1, Array.IndexOf(s1, "test", 1, 3), "#J49");
1406 Assert.AreEqual (3, Array.IndexOf(s1, "a", 1, 3), "#J50");
1410 public void TestIndexOf_CustomEqual ()
1412 DataEqual[] test = new DataEqual [] { new DataEqual () };
1413 Assert.AreEqual (0, Array.IndexOf (test, "asdfas", 0));
1415 IList array = (IList)test;
1416 Assert.AreEqual (0, array.IndexOf ("asdfas"));
1420 public void TestLastIndexOf1() {
1422 bool errorThrown = false;
1424 Array.LastIndexOf(null, "huh?");
1425 } catch (ArgumentNullException) {
1428 Assert.IsTrue (errorThrown, "#K01");
1431 bool errorThrown = false;
1433 char[,] c = new Char[2,2];
1434 Array.LastIndexOf(c, "huh?");
1435 } catch (RankException) {
1438 Assert.IsTrue (errorThrown, "#K02");
1441 String[] s1 = {"this", "is", "a", "a", "test"};
1442 Assert.AreEqual (-1, Array.LastIndexOf(s1, null), "#K03");
1443 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing"), "#K04");
1444 Assert.AreEqual (0, Array.LastIndexOf(s1, "this"), "#K05");
1445 Assert.AreEqual (4, Array.LastIndexOf(s1, "test"), "#K06");
1446 Assert.AreEqual (3, Array.LastIndexOf(s1, "a"), "#K07");
1448 Assert.AreEqual (-1, Array.LastIndexOf (new String [0], "foo"));
1452 public void TestLastIndexOf2() {
1454 bool errorThrown = false;
1456 Array.LastIndexOf(null, "huh?", 0);
1457 } catch (ArgumentNullException) {
1460 Assert.IsTrue (errorThrown, "#K21");
1463 bool errorThrown = false;
1465 char[,] c = new Char[2,2];
1466 Array.LastIndexOf(c, "huh?", 0);
1467 } catch (RankException) {
1470 Assert.IsTrue (errorThrown, "#K22");
1473 bool errorThrown = false;
1475 char[] c = new Char[2];
1476 Array.LastIndexOf(c, "huh?", 3);
1477 } catch (ArgumentOutOfRangeException) {
1480 Assert.IsTrue (errorThrown, "#K23");
1483 String[] s1 = {"this", "is", "really", "a", "test"};
1484 Assert.AreEqual (-1, Array.LastIndexOf(s1, null, 3), "#K24");
1485 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing", 3), "#K25");
1486 Assert.AreEqual (-1, Array.LastIndexOf(s1, "test", 3), "#K26");
1487 Assert.AreEqual (3, Array.LastIndexOf(s1, "a", 3), "#K27");
1488 Assert.AreEqual (0, Array.LastIndexOf(s1, "this", 3), "#K28");
1492 public void TestLastIndexOf3() {
1494 bool errorThrown = false;
1496 Array.LastIndexOf(null, "huh?", 0, 1);
1497 } catch (ArgumentNullException) {
1500 Assert.IsTrue (errorThrown, "#K41");
1503 bool errorThrown = false;
1505 char[,] c = new Char[2,2];
1506 Array.LastIndexOf(c, "huh?", 0, 1);
1507 } catch (RankException) {
1510 Assert.IsTrue (errorThrown, "#K42");
1513 bool errorThrown = false;
1515 char[] c = new Char[2];
1516 Array.LastIndexOf(c, "huh?", 3, 1);
1517 } catch (ArgumentOutOfRangeException) {
1520 Assert.IsTrue (errorThrown, "#K43");
1523 bool errorThrown = false;
1525 char[] c = new Char[2];
1526 Array.LastIndexOf(c, "huh?", 0, 5);
1527 } catch (ArgumentOutOfRangeException) {
1530 Assert.IsTrue (errorThrown, "#K44");
1533 String[] s1 = {"this", "is", "really", "a", "test"};
1534 Assert.AreEqual (-1, Array.LastIndexOf(s1, null, 3, 3), "#K45");
1535 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing", 3, 3), "#K46");
1536 Assert.AreEqual (-1, Array.LastIndexOf(s1, "this", 3, 3), "#K47");
1537 Assert.AreEqual (1, Array.LastIndexOf(s1, "is", 3, 3), "#K48");
1538 Assert.AreEqual (-1, Array.LastIndexOf(s1, "test", 3, 3), "#K49");
1539 Assert.AreEqual (3, Array.LastIndexOf(s1, "a", 3, 3), "#K50");
1543 public void TestLastIndexOf4 ()
1545 short [] a = new short [] { 19, 238, 317, 6, 565, 0, -52, 60, -563, 753, 238, 238};
1547 Array.LastIndexOf (a, (object)16, -1);
1548 NUnit.Framework.Assert.Fail ("#1");
1549 } catch (ArgumentOutOfRangeException) { }
1552 Array.LastIndexOf<short> (a, 16, -1);
1553 NUnit.Framework.Assert.Fail ("#2");
1554 } catch (ArgumentOutOfRangeException) { }
1558 public void TestLastIndexOf5 ()
1560 char [] a = new char [] {'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'j', 'i', 'h'};
1565 for (int i = a.Length - 1; i >= 0 ; i--) {
1567 retval = Array.LastIndexOf(a, a [i], i, i + 1);
1571 Assert.IsTrue (!error);
1575 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1576 public void LastIndexOf_StartIndexOverflow ()
1578 // legal - no exception
1579 byte[] array = new byte [16];
1580 Array.LastIndexOf (array, this, Int32.MaxValue, 1);
1584 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1585 public void LastIndexOf_CountOverflow ()
1587 // legal - no exception
1588 byte[] array = new byte [16];
1589 Array.LastIndexOf (array, this, 1, Int32.MaxValue);
1593 public void LastIndexOf_0LengthArray ()
1595 Array array = Array.CreateInstance (typeof (char), 0);
1596 int idx = Array.LastIndexOf (array, (object) null, -1, 0);
1597 Assert.IsTrue (idx == -1, "#01");
1598 idx = Array.LastIndexOf (array, (object) null, -1, 10);
1599 Assert.IsTrue (idx == -1, "#02");
1600 idx = Array.LastIndexOf (array, (object) null, -100, 10);
1601 Assert.IsTrue (idx == -1, "#02");
1603 array = Array.CreateInstance (typeof (char), 1);
1605 Array.LastIndexOf (array, (object) null, -1, 0);
1606 Assert.Fail ("#04");
1607 } catch (ArgumentOutOfRangeException e) {
1610 Array.LastIndexOf (array, (object) null, -1, 10);
1611 Assert.Fail ("#05");
1612 } catch (ArgumentOutOfRangeException e) {
1615 Array.LastIndexOf (array, (object) null, -100, 10);
1616 Assert.Fail ("#06");
1617 } catch (ArgumentOutOfRangeException e) {
1622 public void TestReverse() {
1624 bool errorThrown = false;
1626 Array.Reverse(null);
1627 } catch (ArgumentNullException) {
1630 Assert.IsTrue (errorThrown, "#L01");
1633 bool errorThrown = false;
1635 char[,] c = new Char[2,2];
1637 } catch (RankException) {
1640 Assert.IsTrue (errorThrown, "#L02");
1643 char[] c1 = {'a', 'b', 'c', 'd'};
1645 Assert.AreEqual ('d', c1[0], "#L03");
1646 Assert.AreEqual ('c', c1[1], "#L04");
1647 Assert.AreEqual ('b', c1[2], "#L05");
1648 Assert.AreEqual ('a', c1[3], "#L06");
1651 bool errorThrown = false;
1653 Array.Reverse(null, 0, 0);
1654 } catch (ArgumentNullException) {
1657 Assert.IsTrue (errorThrown, "#L07");
1660 bool errorThrown = false;
1662 char[,] c = new Char[2,2];
1663 Array.Reverse(c, 0, 0);
1664 } catch (RankException) {
1667 Assert.IsTrue (errorThrown, "#L08");
1670 //bool errorThrown = false;
1672 // char[] c = new Char[2];
1673 // Array.Reverse(c, 0, 3);
1674 //} catch (ArgumentOutOfRangeException) {
1675 // errorThrown = true;
1677 //Assert.IsTrue (errorThrown, "#L09");
1680 //bool errorThrown = false;
1682 // char[] c = new Char[2];
1683 // Array.Reverse(c, 3, 0);
1684 //} catch (ArgumentOutOfRangeException) {
1685 // errorThrown = true;
1687 //Assert.IsTrue (errorThrown, "#L10");
1690 char[] c2 = { 'a', 'b', 'c', 'd'};
1691 Array.Reverse(c2, 1, 2);
1692 Assert.AreEqual ('a', c2[0], "#L11");
1693 Assert.AreEqual ('c', c2[1], "#L12");
1694 Assert.AreEqual ('b', c2[2], "#L13");
1695 Assert.AreEqual ('d', c2[3], "#L14");
1700 public void ReverseStruct () {
1701 BStruct[] c3 = new BStruct[2];
1702 c3 [0] = new BStruct () { i1 = 1, i2 = 2, i3 = 3 };
1703 c3 [1] = new BStruct () { i1 = 4, i2 = 5, i3 = 6 };
1705 Assert.AreEqual (4, c3 [0].i1);
1706 Assert.AreEqual (5, c3 [0].i2);
1707 Assert.AreEqual (6, c3 [0].i3);
1708 Assert.AreEqual (1, c3 [1].i1);
1709 Assert.AreEqual (2, c3 [1].i2);
1710 Assert.AreEqual (3, c3 [1].i3);
1714 public int i1, i2, i3;
1718 public void TestSetValue1() {
1720 bool errorThrown = false;
1722 char[,] c = new Char[2,2];
1723 c.SetValue("buh", 1);
1724 } catch (ArgumentException) {
1727 Assert.IsTrue (errorThrown, "#M01");
1730 bool errorThrown = false;
1732 char[] c = {'a', 'b', 'c'};
1733 c.SetValue("buh", -1);
1734 } catch (IndexOutOfRangeException) {
1737 Assert.IsTrue (errorThrown, "#M02");
1740 bool errorThrown = false;
1742 char[] c = {'a', 'b', 'c'};
1743 c.SetValue("buh", 4);
1744 } catch (IndexOutOfRangeException) {
1747 Assert.IsTrue (errorThrown, "#M03");
1750 char[] c1 = {'a', 'b', 'c', 'd'};
1751 char[] c2 = new char[4];
1752 for (int i = 0; i < c1.Length; i++) {
1753 c2.SetValue(c1[i], i);
1755 for (int i = 0; i < c1.Length; i++) {
1756 Assert.AreEqual (c1[i], c2[i], "#M04(" + i + ")");
1759 int[] c3 = { 1, 2, 3 };
1760 long[] c4 = new long [3];
1762 for (int i = 0; i < c3.Length; i++)
1763 c4.SetValue (c3 [i], i);
1767 } catch (Exception e) {
1768 Assert.Fail ("c3.CopyTo(): e=" + e);
1770 for (int i = 0; i < c3.Length; i++)
1771 Assert.IsTrue (c3[i] == c4[i], "#M05(" + i + ")");
1773 Object[] c5 = new Object [3];
1774 long[] c6 = new long [3];
1778 } catch (Exception e) {
1779 Assert.Fail ("c4.CopyTo(): e=" + e);
1784 } catch (Exception e) {
1785 Assert.Fail ("c5.CopyTo(): e=" + e);
1787 // for (int i = 0; i < c5.Length; i++)
1788 // Assert.IsTrue (c5[i] == c6[i], "#M06(" + i + ")");
1792 public void TestSetValue2() {
1794 bool errorThrown = false;
1796 char[] c = new Char[2];
1797 c.SetValue("buh", 1,1);
1798 } catch (ArgumentException) {
1801 Assert.IsTrue (errorThrown, "#M21");
1804 bool errorThrown = false;
1806 char[,] c = new Char[2,2];
1807 c.SetValue("buh", -1, 1);
1808 } catch (IndexOutOfRangeException) {
1811 Assert.IsTrue (errorThrown, "#M22");
1814 bool errorThrown = false;
1816 char[,] c = new Char[2,2];
1817 c.SetValue("buh", 4,1);
1818 } catch (IndexOutOfRangeException) {
1821 Assert.IsTrue (errorThrown, "#M23");
1824 char[,] c1 = new Char[4,6];
1825 char[,] c2 = new Char[4,6];
1826 for (int i = 0; i < 24; i++) {
1829 c1[first,second] = (char)(((int)'a')+i);
1830 c2.SetValue(c1[first,second], first, second);
1832 for (int i = 0; i < c1.GetLength(0); i++) {
1833 for (int j = 0; j < c1.GetLength(1); j++) {
1834 Assert.AreEqual (c1[i, j], c2[i, j], "#M24(" + i + "," + j + ")");
1840 public void TestSetValue3() {
1842 bool errorThrown = false;
1844 char[] c = new Char[2];
1845 c.SetValue("buh", 1,1,1);
1846 } catch (ArgumentException) {
1849 Assert.IsTrue (errorThrown, "#M41");
1852 bool errorThrown = false;
1854 char[,,] c = new Char[2,2,2];
1855 c.SetValue("buh", -1, 1, 1);
1856 } catch (IndexOutOfRangeException) {
1859 Assert.IsTrue (errorThrown, "#M42");
1862 bool errorThrown = false;
1864 char[,,] c = new Char[2,2,2];
1865 c.SetValue("buh", 4,1,1);
1866 } catch (IndexOutOfRangeException) {
1869 Assert.IsTrue (errorThrown, "#M43");
1872 char[,,] c1 = new Char[4,2,3];
1873 char[,,] c2 = new Char[4,2,3];
1874 for (int i = 0; i < 24; i++) {
1876 int remains = i % 6;
1877 int second = remains / 3;
1878 int third = remains % 3;
1879 c1[first,second, third] = (char)(((int)'a')+i);
1880 c2.SetValue(c1[first, second, third], first, second, third);
1882 for (int i = 0; i < c1.GetLength(0); i++) {
1883 for (int j = 0; j < c1.GetLength(1); j++) {
1884 for (int k = 0; k < c1.GetLength(2); k++) {
1885 Assert.AreEqual (c1[i, j, k], c2[i, j, k], "#M44(" + i + "," + j + " )");
1892 [ExpectedException (typeof (ArgumentNullException))]
1893 public void TestSetValueLongArray ()
1895 char[] c = new Char[2];
1896 c.SetValue("buh", (long [])null);
1900 public void TestSetValueN() {
1902 bool errorThrown = false;
1904 char[] c = new Char[2];
1905 c.SetValue("buh", (int [])null);
1906 } catch (ArgumentNullException) {
1909 Assert.IsTrue (errorThrown, "#M61a");
1912 bool errorThrown = false;
1914 char[] c = new Char[2];
1915 int[] coords = {1, 1};
1916 c.SetValue("buh", coords);
1917 } catch (ArgumentException) {
1920 Assert.IsTrue (errorThrown, "#M62");
1923 bool errorThrown = false;
1925 char[,] c = new Char[2,2];
1926 int[] coords = {-1, 1};
1927 c.SetValue("buh", coords);
1928 } catch (IndexOutOfRangeException) {
1931 Assert.IsTrue (errorThrown, "#M63");
1934 bool errorThrown = false;
1936 char[,] c = new Char[2,2];
1937 int[] coords = {4, 1};
1938 c.SetValue("buh", coords);
1939 } catch (IndexOutOfRangeException) {
1942 Assert.IsTrue (errorThrown, "#M64");
1945 char[,] c1 = new Char[4,6];
1946 char[,] c2 = new Char[4,6];
1947 for (int i = 0; i < 24; i++) {
1950 c1[first,second] = (char)(((int)'a')+i);
1951 int[] coords = {first, second};
1952 c2.SetValue(c1[first,second], coords);
1954 for (int i = 0; i < c1.GetLength(0); i++) {
1955 for (int j = 0; j < c1.GetLength(1); j++) {
1956 Assert.AreEqual (c1[i, j], c2[i, j], "#M65(" + i + "," + j + ")");
1962 public void TestSetValue4() {
1964 int[] c1 = { 1, 2, 3 };
1965 long[] c2 = new long [3];
1967 for (int i = 0; i < c1.Length; i++)
1968 c2.SetValue (c1 [i], i);
1970 for (int i = 0; i < c1.Length; i++) {
1971 Assert.IsTrue (c1[i] == c2[i], "#M81(" + i + ")");
1972 Assert.AreEqual (typeof (long), c2[i].GetType (), "#M82(" + i + ")");
1976 long[] c1 = { 1, 2, 3 };
1977 int[] c2 = new int [3];
1978 bool errorThrown = false;
1980 c2.SetValue (c1 [0], 0);
1981 } catch (ArgumentException) {
1984 Assert.IsTrue (errorThrown, "#M83");
1987 int[] c1 = { 1, 2, 3 };
1988 Object[] c2 = new Object [3];
1990 for (int i = 0; i < c1.Length; i++)
1991 c2.SetValue (c1 [i], i);
1993 for (int i = 0; i < c1.Length; i++)
1994 Assert.AreEqual (c1[i], Convert.ToInt32 (c2[i]), "#M84(" + i + ")");
1997 Object[] c1 = new Object [3];
1998 Object[] c2 = new Object [3];
1999 c1[0] = new Object ();
2001 for (int i = 0; i < c1.Length; i++)
2002 c2.SetValue (c1 [i], i);
2004 for (int i = 0; i < c1.Length; i++)
2005 Assert.AreEqual (c1[i], c2[i], "#M85(" + i + ")");
2008 Object[] c1 = new Object [3];
2009 string[] c2 = new String [3];
2010 string test = "hello";
2013 c2.SetValue (c1 [0], 0);
2014 Assert.AreEqual (c1[0], c2[0], "#M86");
2015 Assert.AreEqual ("hello", c2[0], "#M87");
2018 char[] c1 = { 'a', 'b', 'c' };
2019 string[] c2 = new string [3];
2021 c2.SetValue (c1 [0], 0);
2022 Assert.Fail ("#M88");
2023 } catch (InvalidCastException) {}
2026 Single[] c1 = { 1.2F, 2.3F, 3.4F, 4.5F };
2027 long[] c2 = new long [3];
2029 c2.SetValue (c1 [0], 0);
2030 Assert.Fail ("#M89");
2031 } catch (ArgumentException) {}
2059 UInt32 v11 = 235354;
2060 UInt64 v12 = 234552;
2062 Object[] va1 = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12 };
2063 Object[] va2 = { "true", "1", "a", "-1.2", "-32", "-234", "-34523", "-1",
2064 "-4.8F", "24234", "235354", "234552" };
2066 Object[][] vt = { va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1 };
2069 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2070 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
2071 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
2072 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
2073 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1,
2074 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1,
2075 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,
2076 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1,
2077 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1,
2078 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
2079 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0,
2080 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0
2085 for (int i = 0; i < types.Length; i++) {
2086 for (int j = 0; j < types.Length; j++) {
2087 Array array = Array.CreateInstance (types [j], 2);
2089 Object value = vt[j][i];
2091 bool errorThrown = false;
2093 array.SetValue (value, 0);
2094 } catch (ArgumentException) {
2098 int ex_index = (i * types.Length) + j;
2100 Assert.AreEqual (errorThrown, arg_ex [ex_index] == 1, "#M90(" + types [i] + "," + types [j] + ")");
2104 for (int i = 0; i < types.Length; i++) {
2105 String[] array = new String [2];
2107 Object value = va1 [i];
2109 bool errorThrown = false;
2111 array.SetValue (value, 0);
2112 } catch (InvalidCastException) {
2116 Assert.IsTrue (errorThrown, "#M91(" + types [i] + ")");
2119 for (int i = 0; i < types.Length; i++) {
2120 Array array = Array.CreateInstance (types [i], 2);
2122 Object value = va2 [i];
2124 bool errorThrown = false;
2126 array.SetValue (value, 0);
2127 } catch (InvalidCastException) {
2131 Assert.IsTrue (errorThrown, "#M92(" + types [i] + ")");
2134 for (int i = 0; i < types.Length; i++) {
2135 Array array = Array.CreateInstance (types [i], 2);
2137 Object value = null;
2139 bool errorThrown = false;
2141 array.SetValue (value, 0);
2142 } catch (InvalidCastException) {
2146 Assert.IsTrue (!errorThrown, "#M93(" + types [i] + ")");
2151 for (int i = 0; i < types.Length; i++) {
2152 for (int j = 0; j < types.Length; j++) {
2153 Array source = Array.CreateInstance (types [i], 2);
2154 Array array = Array.CreateInstance (types [j], 2);
2156 source.SetValue (vt[j][i], 0);
2157 source.SetValue (vt[j][i], 1);
2159 bool errorThrown = false;
2161 Array.Copy (source, array, 2);
2162 } catch (ArrayTypeMismatchException) {
2166 int ex_index = (i * types.Length) + j;
2168 Assert.AreEqual (errorThrown, arg_ex [ex_index] == 1, "#M94(" + types [i] + "," + types [j] + ")");
2172 for (int i = 0; i < types.Length; i++) {
2173 Array source = Array.CreateInstance (types [i], 2);
2174 String[] array = new String [2];
2176 source.SetValue (va1 [i], 0);
2177 source.SetValue (va1 [i], 1);
2179 bool errorThrown = false;
2181 Array.Copy (source, array, 2);
2182 } catch (ArrayTypeMismatchException) {
2186 Assert.IsTrue (errorThrown, "#M95(" + types [i] + ")");
2189 for (int i = 0; i < types.Length; i++) {
2190 String[] source = new String [2];
2191 Array array = Array.CreateInstance (types [i], 2);
2193 source.SetValue (va2 [i], 0);
2194 source.SetValue (va2 [i], 1);
2196 bool errorThrown = false;
2198 Array.Copy (source, array, 2);
2199 } catch (ArrayTypeMismatchException) {
2203 Assert.IsTrue (errorThrown, "#M96(" + types [i] + ")");
2209 public void TestSort() {
2211 bool errorThrown = false;
2214 } catch (ArgumentNullException) {
2217 Assert.IsTrue (errorThrown, "#N01");
2220 bool errorThrown = false;
2222 Array.Sort(null, 0, 1);
2223 } catch (ArgumentNullException) {
2226 Assert.IsTrue (errorThrown, "#N02");
2229 bool errorThrown = false;
2231 char[] c1 = new Char[2];
2232 Array.Sort(null, c1);
2233 } catch (ArgumentNullException) {
2236 Assert.IsTrue (errorThrown, "#N03");
2239 bool errorThrown = false;
2241 char[] c1 = new Char[2];
2242 Array.Sort(null, c1, 0, 1);
2243 } catch (ArgumentNullException) {
2246 Assert.IsTrue (errorThrown, "#N04");
2250 char[] arr = {'d', 'b', 'f', 'e', 'a', 'c'};
2253 Array.Sort (null, 0, 1);
2254 Assert.Fail ("#N" + tc.ToString ());
2256 catch (ArgumentException) {}
2257 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2261 Array.Sort (arr, -1, 3);
2262 Assert.Fail ("#N" + tc.ToString ());
2264 catch (ArgumentException) {}
2265 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2269 Array.Sort (arr, 1, -3);
2270 Assert.Fail ("#N" + tc.ToString ());
2272 catch (ArgumentException) {}
2273 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2277 Array.Sort (arr, arr.Length, arr.Length + 2);
2278 Assert.Fail ("#N" + tc.ToString ());
2280 catch (ArgumentException) {}
2281 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2284 // note: null second array => just sort first array
2285 char[] starter = {'d', 'b', 'f', 'e', 'a', 'c'};
2286 int[] starter1 = {1,2,3,4,5,6};
2288 char[] c1 = (char[])starter.Clone();
2290 Assert.AreEqual ('a', c1[0], "#N21");
2291 Assert.AreEqual ('b', c1[1], "#N22");
2292 Assert.AreEqual ('c', c1[2], "#N23");
2293 Assert.AreEqual ('d', c1[3], "#N24");
2294 Assert.AreEqual ('e', c1[4], "#N25");
2295 Assert.AreEqual ('f', c1[5], "#N26");
2298 char[] c1 = (char[])starter.Clone();
2299 int[] i1 = (int[])starter1.Clone();
2301 Assert.AreEqual ('a', c1[0], "#N41");
2302 Assert.AreEqual ('b', c1[1], "#N42");
2303 Assert.AreEqual ('c', c1[2], "#N43");
2304 Assert.AreEqual ('d', c1[3], "#N44");
2305 Assert.AreEqual ('e', c1[4], "#N45");
2306 Assert.AreEqual ('f', c1[5], "#N46");
2307 Assert.AreEqual (5, i1[0], "#N47");
2308 Assert.AreEqual (2, i1[1], "#N48");
2309 Assert.AreEqual (6, i1[2], "#N49");
2310 Assert.AreEqual (1, i1[3], "#N50");
2311 Assert.AreEqual (4, i1[4], "#N51");
2312 Assert.AreEqual (3, i1[5], "#N52");
2315 char[] c1 = (char[])starter.Clone();
2316 Array.Sort(c1, 1, 4);
2317 Assert.AreEqual ('d', c1[0], "#N61");
2318 Assert.AreEqual ('a', c1[1], "#N62");
2319 Assert.AreEqual ('b', c1[2], "#N63");
2320 Assert.AreEqual ('e', c1[3], "#N64");
2321 Assert.AreEqual ('f', c1[4], "#N65");
2322 Assert.AreEqual ('c', c1[5], "#N66");
2325 char[] c1 = (char[])starter.Clone();
2326 int[] i1 = (int[])starter1.Clone();
2327 Array.Sort(c1, i1, 1, 4);
2328 Assert.AreEqual ('d', c1[0], "#N81");
2329 Assert.AreEqual ('a', c1[1], "#N82");
2330 Assert.AreEqual ('b', c1[2], "#N83");
2331 Assert.AreEqual ('e', c1[3], "#N84");
2332 Assert.AreEqual ('f', c1[4], "#N85");
2333 Assert.AreEqual ('c', c1[5], "#N86");
2334 Assert.AreEqual (1, i1[0], "#N87");
2335 Assert.AreEqual (5, i1[1], "#N88");
2336 Assert.AreEqual (2, i1[2], "#N89");
2337 Assert.AreEqual (4, i1[3], "#N90");
2338 Assert.AreEqual (3, i1[4], "#N91");
2339 Assert.AreEqual (6, i1[5], "#N92");
2344 double[] a = new double[115];
2345 int[] b = new int[256];
2346 Array.Sort<double, int> (a, b, 0, 115);
2349 /* Check that ulong[] is not sorted as long[] */
2351 string[] names = new string[] {
2352 "A", "B", "C", "D", "E"
2355 ulong[] arr = new ulong [] {
2357 unchecked((ulong)0xffffFFFF00000000),
2364 Array.Sort (a, names, null);
2365 Assert.AreEqual (0, a.GetValue (0));
2370 public void SortNonGenericDoubleItems () {
2371 double[] doubleValues = new double[11];
2373 doubleValues[0] = 0.221788066253601;
2374 doubleValues[1] = 0.497278285809481;
2375 doubleValues[2] = 0.100565033883643;
2376 doubleValues[3] = 0.0433309347749905;
2377 doubleValues[4] = 0.00476726438463812;
2378 doubleValues[5] = 0.1354609735456;
2379 doubleValues[6] = 0.57690356588135;
2380 doubleValues[7] = 0.466239434334826;
2381 doubleValues[8] = 0.409741461978934;
2382 doubleValues[9] = 0.0112412763949565;
2383 doubleValues[10] = 0.668704347674307;
2385 int[] indices = new int[11];
2398 Array.Sort ((Array)doubleValues, (Array)indices);
2399 Assert.AreEqual (4, indices [0]);
2403 public void TestInitializeEmpty()
2415 Assert.IsTrue (!catched, "#TI01");
2419 public void TestInitializeInt()
2424 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2426 Assert.AreEqual (a[i], b[i], "#TI02 " + i);
2431 public void TestInitializeDouble()
2433 double[] a = {1.0,2.0,0.0};
2435 double[] b = {1.0,2.0,0.0};
2436 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2438 Assert.AreEqual (a[i], b[i], "#TI03 " + i);
2443 public void TestInitializeFloat()
2445 float[] a = {1.0F,2.0F,0.0F};
2447 float[] b = {1.0F,2.0F,0.0F};
2448 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2450 Assert.AreEqual (a[i], b[i], "#TI04 " + i);
2455 public void TestInitializeChar()
2457 char[] a = {'1','.','0','F','2','.','0','F'};
2459 char[] b = {'1','.','0','F','2','.','0','F'};
2460 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2462 Assert.AreEqual (a[i], b[i], "#TI05 " + i);
2467 public void TestInitializeString()
2469 string[] a = {"hola","adios","menos","mas"};
2471 string[] b = {"hola","adios","menos","mas"};
2472 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2474 Assert.AreEqual (a[i], b[i], "#TI06 " + i);
2479 public void TestInitializeEnum()
2481 enua[] a = {enua.hola,enua.adios,enua.menos,enua.mas};
2483 enua[] b = {enua.hola,enua.adios,enua.menos,enua.mas};
2484 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2486 Assert.AreEqual (a[i], b[i], "#TI07 " + i);
2491 public void TestInitializeIntNI()
2493 int[] a = new int[20];
2497 Assert.AreEqual (b, 0, "#TI08");
2502 public void TestInitializeCharNI()
2504 char[] a = new char[20];
2506 foreach(char b in a)
2508 Assert.AreEqual (b, 0, "#TI09");
2513 public void TestInitializeDoubleNI()
2515 double[] a = new double[20];
2517 foreach(double b in a)
2519 Assert.AreEqual (b, 0.0, "#TI09");
2524 public void TestInitializeStringNI()
2526 string[] a = new string[20];
2528 foreach(string b in a)
2530 Assert.AreEqual (b, null, "#TI10");
2535 public void TestInitializeObjectNI()
2537 object[] a = new object[20];
2539 foreach(object b in a)
2541 Assert.AreEqual (b, null, "#TI11");
2546 public void TestInitializeAClassNI()
2548 AClass[] a = new AClass[20];
2550 foreach(AClass b in a)
2552 Assert.AreEqual (b, null, "#TI12");
2558 public void TestInitializeAStructNI()
2560 AStruct[] a = new AStruct[20];
2562 foreach(AStruct b in a)
2564 Assert.AreEqual (b, new AStruct(), "#TI14");
2569 public void TestInitializeAStruct()
2571 AStruct[] a = new AStruct[3];
2575 AStruct[] b = new AStruct[3];
2578 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2580 Assert.AreEqual (a[i], b[i], "#TI15 " + i);
2585 public void TestInitializeDateTimeNI()
2587 DateTime[] a = new DateTime[20];
2589 foreach(DateTime b in a)
2591 Assert.AreEqual (b, new DateTime(), "#TI16");
2596 [ExpectedException (typeof (ArgumentNullException))]
2597 public void MoreSort1 ()
2599 Array.Sort (null, 0, 1);
2603 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2604 public void MoreSort2 ()
2606 Array.Sort (arrsort, -1, 3);
2610 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2611 public void MoreSort3 ()
2613 Array.Sort (arrsort, 1, -3);
2617 [ExpectedException (typeof (ArgumentException))]
2618 public void MoreSort4 ()
2620 Array.Sort (arrsort, arrsort.Length, arrsort.Length + 2);
2624 [ExpectedException (typeof (RankException))]
2625 public void MoreSort5 ()
2627 char [,] arr = new char [,] {{'a'}, {'b'}};
2628 Array.Sort (arr, 0, 1);
2632 public void MoreSort6 ()
2634 Array.Sort (arrsort, 0, 0);
2638 [ExpectedException (typeof (ArgumentException))]
2639 public void MoreSort7 ()
2641 Array.Sort (arrsort, arrsort.Length - 1, 2);
2645 [ExpectedException (typeof (ArgumentException))]
2646 public void MoreSort8 ()
2648 Array.Sort (arrsort, 0, arrsort.Length + 1);
2652 public void MoreSort9 ()
2654 Array.Sort (arrsort, null, 0, arrsort.Length, null);
2658 [ExpectedException (typeof (InvalidOperationException))]
2659 public void MoreSort10 ()
2661 object [] array = {true, 'k', SByte.MinValue, Byte.MinValue, (short) 2, 634, (long) 436, (float) 1.1, 1.23, "Hello World"};
2662 Array.Sort (array, (IComparer) null);
2665 [Test] // bug #81941
2668 double [] a = new double [2] { 0.9, 0.3 };
2669 uint [] b = new uint [2] { 4, 7 };
2671 Assert.AreEqual (0.3, a [0], "#1");
2672 Assert.AreEqual (0.9, a [1], "#2");
2673 Assert.AreEqual (7, b [0], "#3");
2674 Assert.AreEqual (4, b [1], "#4");
2678 public void ClearJaggedArray ()
2680 byte[][] matrix = new byte [8][];
2681 for (int i=0; i < 8; i++) {
2682 matrix [i] = new byte [8];
2683 for (int j=0; j < 8; j++) {
2687 Array.Clear (matrix, 0, 8);
2688 for (int i=0; i < 8; i++) {
2689 Assert.IsNull (matrix [i], i.ToString ());
2694 public void ClearMultidimentionalArray ()
2696 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2697 Array.Clear (matrix, 0, 2);
2698 Assert.AreEqual (0, matrix [0, 0], "0,0");
2699 Assert.AreEqual (0, matrix [0, 1], "0,1");
2700 Assert.AreEqual (2, matrix [1, 0], "1,0");
2701 Assert.AreEqual (2, matrix [1, 1], "1,1");
2705 [ExpectedException (typeof (IndexOutOfRangeException))]
2706 public void ClearOutsideMultidimentionalArray ()
2708 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2709 Array.Clear (matrix, 0, 5);
2713 [ExpectedException (typeof (IndexOutOfRangeException))]
2714 public void Clear_IndexOverflow ()
2716 byte[] array = new byte [16];
2717 Array.Clear (array, 4, Int32.MaxValue);
2721 [ExpectedException (typeof (IndexOutOfRangeException))]
2722 public void Clear_LengthOverflow ()
2724 byte[] array = new byte [16];
2725 Array.Clear (array, Int32.MaxValue, 4);
2729 [ExpectedException (typeof (ArgumentException))]
2730 public void Copy_SourceIndexOverflow ()
2732 byte[] array = new byte [16];
2733 Array.Copy (array, Int32.MaxValue, array, 8, 8);
2737 [ExpectedException (typeof (ArgumentException))]
2738 public void Copy_DestinationIndexOverflow ()
2740 byte[] array = new byte [16];
2741 Array.Copy (array, 8, array, Int32.MaxValue, 8);
2745 [ExpectedException (typeof (ArgumentException))]
2746 public void Copy_LengthOverflow ()
2748 byte[] array = new byte [16];
2749 Array.Copy (array, 8, array, 8, Int32.MaxValue);
2753 [ExpectedException (typeof (ArgumentException))]
2754 public void Reverse_IndexOverflow ()
2756 byte[] array = new byte [16];
2757 Array.Reverse (array, Int32.MaxValue, 8);
2761 [ExpectedException (typeof (ArgumentException))]
2762 public void Reverse_LengthOverflow ()
2764 byte[] array = new byte [16];
2765 Array.Reverse (array, 8, Int32.MaxValue);
2768 public struct CharX : IComparable {
2771 public CharX (char c)
2776 public int CompareTo (object obj)
2779 return c.CompareTo (((CharX) obj).c);
2781 return c.CompareTo (obj);
2786 public void BinarySearch_ArgPassingOrder ()
2789 // This tests that arguments are passed to the comprer in the correct
2790 // order. The IComparable of the *array* elements must get called, not
2791 // that of the search object.
2793 CharX [] x = { new CharX ('a'), new CharX ('b'), new CharX ('c') };
2794 Assert.AreEqual (1, Array.BinarySearch (x, 'b'));
2797 class Comparer: IComparer {
2799 private bool called = false;
2801 public bool Called {
2803 bool result = called;
2809 public int Compare (object x, object y)
2817 public void BinarySearch1_EmptyList ()
2819 int[] array = new int[0];
2820 Assert.AreEqual (- 1, Array.BinarySearch (array, 0), "BinarySearch");
2824 public void BinarySearch2_EmptyList ()
2826 int[] array = new int[0];
2827 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, 0), "BinarySearch");
2831 public void BinarySearch3_EmptyList ()
2833 Comparer comparer = new Comparer ();
2834 int[] array = new int[0];
2835 Assert.AreEqual (-1, Array.BinarySearch (array, 0, comparer), "BinarySearch");
2836 // bug 77030 - the comparer isn't called for an empty array/list
2837 Assert.IsTrue (!comparer.Called, "Called");
2841 public void BinarySearch4_EmptyList ()
2843 Comparer comparer = new Comparer ();
2844 int[] array = new int[0];
2845 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, comparer), "BinarySearch");
2846 // bug 77030 - the comparer isn't called for an empty array/list
2847 Assert.IsTrue (!comparer.Called, "Called");
2851 [ExpectedException (typeof (ArgumentNullException))]
2852 public void AsReadOnly_NullArray ()
2854 Array.AsReadOnly <int> (null);
2858 public void ReadOnly_Count ()
2860 Assert.AreEqual (10, Array.AsReadOnly (new int [10]).Count);
2864 public void ReadOnly_Contains ()
2866 int[] arr = new int [2];
2869 IList<int> a = Array.AsReadOnly (arr);
2871 Assert.IsTrue (a.Contains (3));
2872 Assert.IsTrue (!a.Contains (6));
2876 public void ReadOnly_IndexOf ()
2878 int[] arr = new int [2];
2881 IList<int> a = Array.AsReadOnly (arr);
2883 Assert.AreEqual (0, a.IndexOf (3));
2884 Assert.AreEqual (1, a.IndexOf (5));
2885 Assert.AreEqual (-1, a.IndexOf (6));
2889 public void ReadOnly_Indexer ()
2891 int[] arr = new int [2];
2894 IList<int> a = Array.AsReadOnly (arr);
2896 Assert.AreEqual (3, a [0]);
2897 Assert.AreEqual (5, a [1]);
2899 /* Check that modifications to the original array are visible */
2901 Assert.AreEqual (6, a [0]);
2905 public void ReadOnly_Enumerator ()
2907 int[] arr = new int [10];
2909 for (int i = 0; i < 10; ++i)
2913 foreach (int i in Array.AsReadOnly (arr))
2916 Assert.AreEqual (45, sum);
2920 public void ReadOnly_CopyTo ()
2922 int[] arr = new int [2];
2925 IList<int> a = Array.AsReadOnly (arr);
2927 int[] arr2 = new int [3];
2930 Assert.AreEqual (0, arr2 [0]);
2931 Assert.AreEqual (3, arr2 [1]);
2932 Assert.AreEqual (5, arr2 [2]);
2936 public void Resize ()
2938 int [] arr = new int [] { 1, 3, 5 };
2939 Array.Resize <int> (ref arr, 3);
2940 Assert.AreEqual (3, arr.Length, "#A1");
2941 Assert.AreEqual (1, arr [0], "#A2");
2942 Assert.AreEqual (3, arr [1], "#A3");
2943 Assert.AreEqual (5, arr [2], "#A4");
2945 Array.Resize <int> (ref arr, 2);
2946 Assert.AreEqual (2, arr.Length, "#B1");
2947 Assert.AreEqual (1, arr [0], "#B2");
2948 Assert.AreEqual (3, arr [1], "#B3");
2950 Array.Resize <int> (ref arr, 4);
2951 Assert.AreEqual (4, arr.Length, "#C1");
2952 Assert.AreEqual (1, arr [0], "#C2");
2953 Assert.AreEqual (3, arr [1], "#C3");
2954 Assert.AreEqual (0, arr [2], "#C4");
2955 Assert.AreEqual (0, arr [3], "#C5");
2959 public void Resize_null ()
2962 Array.Resize (ref arr, 10);
2963 Assert.AreEqual (arr.Length, 10);
2967 public void Test_ContainsAndIndexOf_EquatableItem ()
2969 EquatableClass[] list = new EquatableClass[] {new EquatableClass (0), new EquatableClass (1), new EquatableClass (0)};
2971 Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, list[0]), "#0");
2972 Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, new EquatableClass (0)), "#1");
2973 Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, list[0]), "#2");
2974 Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, new EquatableClass (0)), "#3");
2977 public class EquatableClass : IEquatable<EquatableClass>
2980 public EquatableClass (int x)
2985 public bool Equals (EquatableClass other)
2987 return this._x == other._x;
2992 public void AsIList ()
2994 IList<int> arr = new int [10];
2996 Assert.AreEqual (5, arr [0]);
2998 IList<FooStruct> arr2 = new FooStruct [10];
2999 FooStruct s = new FooStruct ();
3004 Assert.AreEqual (11, s.i);
3005 Assert.AreEqual (22, s.j);
3007 IList<string> arr3 = new string [10];
3009 Assert.AreEqual ("ABC", arr3 [5]);
3016 #if !TARGET_JVM // BugBUG: T[] is not yet ICollection<T> under TARGET_JVM
3019 public void ICollectionNull ()
3021 ICollection<object> test;
3023 test = new List<object>();
3024 Assert.AreEqual (test.Contains (null), false, "list<o>");
3026 test = new object[] {};
3027 Assert.AreEqual (test.Contains (null), false, "empty array");
3029 test = new object[] {null};
3030 Assert.AreEqual (test.Contains (null), true, "array with null");
3032 test = new object[] { 1, null};
3033 Assert.IsTrue (test.Contains (null), "array with last null");
3035 test = new List<object>(test);
3036 Assert.AreEqual (test.Contains (null), true, "List<object> with test");
3038 test = new object[] {new object()};
3039 Assert.AreEqual (test.Contains (null), false, "array with object");
3041 test = new List<object>(test);
3042 Assert.AreEqual (test.Contains (null), false, "array with test");
3046 public void IListNull ()
3050 test = new List<object>();
3051 Assert.AreEqual (-1, test.IndexOf (null), "list<o>");
3053 test = new object[] {};
3054 Assert.AreEqual (-1, test.IndexOf (null), "empty array");
3056 test = new object[] {null};
3057 Assert.AreEqual (0, test.IndexOf (null), "array with null");
3059 test = new object[] { 1, null};
3060 Assert.AreEqual (1, test.IndexOf (null), "array with last null");
3062 test = new List<object>(test);
3063 Assert.AreEqual (1, test.IndexOf (null), "List<object> with test");
3065 test = new object[] {new object()};
3066 Assert.AreEqual (-1, test.IndexOf (null), "array with object");
3068 test = new List<object>(test);
3069 Assert.AreEqual (-1, test.IndexOf (null), "array with test");
3072 #endif // TARGET_JVM
3076 enum ByteEnum : byte {}
3077 enum IntEnum : int {}
3080 public void TestByteEnumArrayToByteArray ()
3082 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
3083 byte[] b = new byte[a.Length];
3088 public void TestByteEnumArrayToIntArray ()
3090 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
3091 int[] b = new int[a.Length];
3096 [ExpectedException (typeof (ArrayTypeMismatchException))]
3097 public void TestIntEnumArrayToByteArray ()
3099 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
3100 byte[] b = new byte[a.Length];
3105 public void TestIntEnumArrayToIntArray ()
3107 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
3108 int[] b = new int[a.Length];
3114 [Test] // bug #322248
3115 public void IEnumerator_Reset ()
3117 int[] array = new int[] { 1, 2, 3};
3118 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3119 Assert.IsTrue (e.MoveNext (), "#A1");
3120 Assert.AreEqual (1, e.Current, "#A2");
3121 Assert.IsTrue (e.MoveNext (), "#A3");
3122 Assert.AreEqual (2, e.Current, "#A4");
3126 Assert.IsTrue (e.MoveNext (), "#C1");
3127 Assert.AreEqual (1, e.Current, "#C2");
3131 public void IEnumerator_Current_Finished ()
3133 int[] array = new int[] { 1, 2, 3 };
3134 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3135 Assert.IsTrue (e.MoveNext (), "#A1");
3136 Assert.AreEqual (1, e.Current, "#A2");
3137 Assert.IsTrue (e.MoveNext (), "#A3");
3138 Assert.AreEqual (2, e.Current, "#A4");
3139 Assert.IsTrue (e.MoveNext (), "#A5");
3140 Assert.AreEqual (3, e.Current, "#A6");
3141 Assert.IsTrue (!e.MoveNext (), "#A6");
3144 Assert.Fail ("#B1:" + e.Current);
3145 } catch (InvalidOperationException ex) {
3146 // Enumeration already finished
3147 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3148 Assert.IsNull (ex.InnerException, "#B3");
3149 Assert.IsNotNull (ex.Message, "#B4");
3154 public void IEnumerator_Current_NotStarted ()
3156 int[] array = new int[] { 1, 2, 3 };
3157 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3160 Assert.Fail ("#A1:" + e.Current);
3161 } catch (InvalidOperationException ex) {
3162 // Enumeration has not started. Call MoveNext
3163 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
3164 Assert.IsNull (ex.InnerException, "#A3");
3165 Assert.IsNotNull (ex.Message, "#A4");
3170 public void IEnumerator_Current_Reset ()
3172 int[] array = new int[] { 1, 2, 3 };
3173 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3178 Assert.Fail ("#B1:" + e.Current);
3179 } catch (InvalidOperationException ex) {
3180 // Enumeration has not started. Call MoveNext
3181 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3182 Assert.IsNull (ex.InnerException, "#B3");
3183 Assert.IsNotNull (ex.Message, "#B4");
3187 public void ICollection_IsReadOnly() {
3188 ICollection<string> arr = new string [10];
3190 Assert.IsTrue (arr.IsReadOnly);
3194 [ExpectedException (typeof (NotSupportedException))]
3195 public void ArrayCreateInstanceOfVoid ()
3197 Array.CreateInstance (typeof (void), 42);
3203 [ExpectedException (typeof (NotSupportedException))]
3204 public void ArrayCreateInstanceOfOpenGenericType ()
3206 Array.CreateInstance (typeof (Foo<>), 42);
3210 [ExpectedException (typeof (IndexOutOfRangeException))]
3211 public void ClearNegativeLength ()
3213 Array.Clear (new int [] { 1, 2 }, 0, -1);
3217 [ExpectedException (typeof (ArgumentException))]
3218 public void MultiDimension_IList_setItem ()
3220 IList array = new int [1, 1];
3225 [ExpectedException (typeof (ArgumentException))]
3226 public void MultiDimension_IList_getItem ()
3228 IList array = new int [1, 1];
3229 int a = (int) array [0];
3233 public void SetValue_Nullable () {
3234 Array array = Array.CreateInstance (typeof (int?), 7);
3238 array.SetValue (o, 0);
3239 Assert.AreEqual (42, array.GetValue (0));
3241 array.SetValue (null, 0);
3242 Assert.AreEqual (null, array.GetValue (0));
3246 public void SortNullsWithGenericVersion ()
3248 string[] s1 = new string[6]{
3256 string[] s2 = new string[]{null,
3263 Array.Sort<string> (s1);
3264 for (int i = 0; i < 6; i++) {
3265 Assert.AreEqual (s1[i], s2[i], "At:" + i);
3270 // This is a test case for the case that was broken by the code contributed
3273 // This tests the fix for: #622101
3276 public void SortActuallyWorks ()
3278 string[] data = new string[9]{"Foo", "Bar", "Dingus", null, "Dingu4", "123", "Iam", null, "NotNull"};
3279 IComparer comparer = new NullAtEndComparer ();
3280 Array.Sort (data, comparer);
3282 Assert.AreEqual (data [7], null);
3283 Assert.AreNotEqual (data [0], null);
3286 class NullAtEndComparer : IComparer {
3287 public int Compare(object x, object y)
3289 if (x == null) return 1;
3290 if (y == null) return -1;
3291 return ((string)x).CompareTo((string)y);
3297 [ExpectedException (typeof (ArgumentException))]
3298 public void CompareToWithJaggedArray () {
3299 IStructuralComparable a = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }};
3300 IStructuralComparable b = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }};
3301 a.CompareTo (b, Comparer<object>.Default);
3305 [ExpectedException (typeof (ArgumentException))]
3306 public void CompareToWithArrayOfTheWrongKind () {
3307 IStructuralComparable a = new int[] { 1, 2 };
3308 IStructuralComparable b = new double[] { 1, 2 };
3309 a.CompareTo (b, Comparer<object>.Default);
3313 [ExpectedException (typeof (ArgumentException))]
3314 public void CompareToWithNonArrayType () {
3315 IStructuralComparable a = new int[] { 1, 2 };
3316 a.CompareTo (99, Comparer<object>.Default);
3320 [ExpectedException (typeof (ArgumentException))]
3321 public void CompareToWithNonArrayOfDifferentSize () {
3322 IStructuralComparable a = new int[] { 1, 2 };
3323 IStructuralComparable b = new int[] { 1, 2, 3 };
3324 a.CompareTo (b, Comparer<object>.Default);
3328 [ExpectedException (typeof (ArgumentException))]
3329 public void CompareToWithMultiDimArray1 () {
3330 IStructuralComparable a = new int [2,2] { {10, 10 }, { 10, 10 } };
3331 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3332 a.CompareTo (b, Comparer<object>.Default);
3336 [ExpectedException (typeof (ArgumentException))]
3337 public void CompareToWithMultiDimArray2 () {
3338 IStructuralComparable a = new int [2] { 10, 10 };
3339 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3340 a.CompareTo (b, Comparer<object>.Default);
3344 [ExpectedException (typeof (ArgumentException))]
3345 public void CompareToWithMultiDimArray3 () {
3346 IStructuralComparable a = new int [4] { 10, 10, 10, 10 };
3347 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3348 a.CompareTo (b, Comparer<object>.Default);
3352 [ExpectedException (typeof (IndexOutOfRangeException))]
3353 public void CompareToWithBoundedArray1 () {
3354 IStructuralComparable a = new int [2] { 10, 10 };
3355 Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 });
3356 IStructuralComparable b = ab;
3357 ab.SetValue (10, 5);
3358 ab.SetValue (10, 6);
3360 a.CompareTo (b, Comparer<object>.Default);
3364 [ExpectedException (typeof (IndexOutOfRangeException))]
3365 public void CompareToWithBoundedArray2 () {
3366 IStructuralComparable a = new int [2] { 10, 10 };
3367 Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 });
3368 IStructuralComparable b = ab;
3369 ab.SetValue (10, 5);
3370 ab.SetValue (10, 6);
3372 //Yes, CompareTo simply doesn't work with bounded arrays!
3373 b.CompareTo (b, Comparer<object>.Default);
3377 [ExpectedException (typeof (NullReferenceException))]
3378 public void CompareToWithNullComparer () {
3379 IStructuralComparable a = new int[] { 1, 2 };
3380 IStructuralComparable b = new int[] { 1, 2 };
3381 a.CompareTo (b, null);
3385 public void CompareToWithNullArray () {
3386 IStructuralComparable a = new int[] { 1, 2 };
3387 Assert.AreEqual (1, a.CompareTo (null, Comparer<object>.Default));
3391 public void CompareToWithGoodArrays () {
3392 IStructuralComparable a = new int[] { 10, 20 };
3393 Assert.AreEqual (0, a.CompareTo (a, Comparer<object>.Default));
3394 Assert.AreEqual (0, a.CompareTo (new int [] { 10, 20 }, Comparer<object>.Default));
3395 Assert.AreEqual (-1, a.CompareTo (new int [] { 11, 20 }, Comparer<object>.Default));
3396 Assert.AreEqual (-1, a.CompareTo (new int [] { 10, 21 }, Comparer<object>.Default));
3397 Assert.AreEqual (1, a.CompareTo (new int [] { 9, 20 }, Comparer<object>.Default));
3398 Assert.AreEqual (1, a.CompareTo (new int [] { 10, 19 }, Comparer<object>.Default));
3402 public void IStructuralEquatable_Equals ()
3404 IStructuralEquatable array = new int[] {1, 2, 3};
3405 IStructuralEquatable array2 = new int[] {1, 2, 3};
3406 Assert.AreEqual (false, array.Equals (null, null));
3407 Assert.AreEqual (true, array.Equals (array, null));
3408 Assert.AreEqual (true, array.Equals (array2, EqualityComparer<int>.Default));
3412 [ExpectedException (typeof (NullReferenceException))]
3413 public void IStructuralEquatable_Equals_NoComparer ()
3415 IStructuralEquatable array = new int[] {1, 2, 3};
3416 IStructuralComparable array2 = new int[] {1, 2, 3};
3417 array.Equals (array2, null);
3421 [ExpectedException (typeof (ArgumentException))]
3422 public void IStructuralEquatable_Equals_ComparerThrows ()
3424 IStructuralEquatable array = new int[] {1, 2, 3};
3425 IStructuralComparable array2 = new int[] {1, 2, 3};
3426 array.Equals (array2, EqualityComparer<long>.Default);
3430 [ExpectedException (typeof (ArgumentNullException))]
3431 public void IStructuralEquatable_GetHashCode_NullComparer ()
3433 IStructuralEquatable a = new int[] { 1, 2 };
3434 a.GetHashCode (null);
3437 class TestComparer_GetHashCode : IEqualityComparer
3441 bool IEqualityComparer.Equals (object x, object y)
3443 throw new NotImplementedException ();
3446 public int GetHashCode (object obj)
3453 public void IStructuralEquatable_GetHashCode ()
3455 IStructuralEquatable a = new int[] { 1, 2, 9 };
3457 var c = new TestComparer_GetHashCode ();
3459 Assert.AreEqual (3, c.Counter);