1 // ArrayTest.cs - NUnit Test Cases for the System.Array class
3 // David Brandt (bucky@keystreams.com)
4 // Eduardo Garcia (kiwnix@yahoo.es)
6 // (C) Ximian, Inc. http://www.ximian.com
7 // Copyright (C) 2004 Novell (http://www.novell.com)
10 using NUnit.Framework;
12 using System.Collections;
13 using System.Globalization;
16 using System.Collections.Generic;
19 namespace MonoTests.System
22 enum enua {hola,adios,mas,menos};
48 public override bool Equals (object obj)
54 //End Auxiliary Things
57 public class ArrayTest
59 char [] arrsort = {'d', 'b', 'f', 'e', 'a', 'c'};
64 public void TestIsFixedSize() {
66 Assert.IsTrue (a1.IsFixedSize, "All arrays are fixed");
70 public void TestIsReadOnly() {
72 Assert.IsTrue (!a1.IsReadOnly, "No array is readonly");
76 public void TestIsSynchronized() {
78 Assert.IsTrue (!a1.IsSynchronized, "No array is synchronized");
82 public void TestLength() {
85 Assert.AreEqual (0, a1.Length, "Zero length array");
89 Assert.AreEqual (1, a1.Length, "One-length array");
92 char[] a1 = {'c', 'c'};
93 Assert.AreEqual (2, a1.Length, "Two-length array");
98 public void TestRank() {
99 char[] a1 = { 'c', 'd', 'e' };
100 Assert.AreEqual (1, a1.Rank, "Rank one");
102 char[,] a2 = new Char[3,3];
103 Assert.AreEqual (2, a2.Rank, "Rank two");
105 char[,,] a3 = new Char[3,3,3];
106 Assert.AreEqual (3, a3.Rank, "Rank three");
110 public void TestBinarySearch1() {
111 bool errorThrown = false;
113 Array.BinarySearch(null, "blue");
114 } catch (ArgumentNullException) {
117 Assert.IsTrue (errorThrown, "#B01");
120 char[,] c1 = new Char[2,2];
121 Array.BinarySearch(c1, "needle");
122 } catch (RankException) {
125 Assert.IsTrue (errorThrown, "#B02");
128 char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
129 Assert.IsTrue (Array.BinarySearch(arr, 'c') >= 3, "#B05");
130 Assert.IsTrue (Array.BinarySearch(arr, 'c') < 6, "#B06");
133 char[] arr = {'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
134 Assert.AreEqual (-4, Array.BinarySearch(arr, 'c'), "#B07");
137 char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
138 Assert.AreEqual (-9, Array.BinarySearch(arr, 'e'), "#B08");
143 public void TestBinarySearch2() {
144 bool errorThrown = false;
146 Array.BinarySearch(null, 0, 1, "blue");
147 } catch (ArgumentNullException) {
150 Assert.IsTrue (errorThrown, "#B20");
153 char[,] c1 = new Char[2,2];
154 Array.BinarySearch(c1, 0, 1, "needle");
155 } catch (RankException) {
158 Assert.IsTrue (errorThrown, "#B21");
162 Array.BinarySearch(c1, -1, 1, 'a');
163 } catch (ArgumentOutOfRangeException) {
166 Assert.IsTrue (errorThrown, "#B22");
170 Array.BinarySearch(c1, 0, -1, 'a');
171 } catch (ArgumentOutOfRangeException) {
174 Assert.IsTrue (errorThrown, "#B23");
178 Array.BinarySearch(c1, 0, 4, 'a');
179 } catch (ArgumentException) {
182 Assert.IsTrue (errorThrown, "#B24");
185 char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
186 Assert.IsTrue (Array.BinarySearch(arr, 2, 8, 'c') >= 5, "#B26");
187 Assert.IsTrue (Array.BinarySearch(arr, 2, 8, 'c') < 8, "#B27");
190 char[] arr = {'z', 'z', 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
191 Assert.AreEqual (-6, Array.BinarySearch(arr, 2, 8, 'c'), "#B28");
194 char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
195 Assert.AreEqual (-11, Array.BinarySearch(arr, 2, 8, 'e'), "#B29");
199 public void TestBinarySearch3()
201 int[] array = new int[100];
203 for (int i = 0; i < 100; i++)
206 Assert.AreEqual (49, Array.BinarySearch(array, 10), "#B30");
210 public void BinarySearch_NullValue ()
212 int[] array = new int[1];
213 Assert.AreEqual (-1, Array.BinarySearch (array, null), "I=a,o");
214 Assert.AreEqual (-1, Array.BinarySearch (array, null, null), "I=a,o,c");
215 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 1, null), "I=a,i,i,o");
216 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 1, null, null), "I=a,i,i,o,c");
218 object[] o = new object [3] { this, this, null };
219 Assert.AreEqual (-1, Array.BinarySearch (o, null), "O=a,o");
220 Assert.AreEqual (-1, Array.BinarySearch (o, null, null), "O=a,o,c");
221 Assert.AreEqual (-1, Array.BinarySearch (o, 0, 3, null), "O=a,i,i,o");
222 Assert.AreEqual (-1, Array.BinarySearch (o, 0, 3, null, null), "O=a,i,i,o,c");
225 // TODO - testBinarySearch with explicit IComparer args
228 public void TestClear() {
229 bool errorThrown = false;
231 Array.Clear(null, 0, 1);
232 } catch (ArgumentNullException) {
235 Assert.IsTrue (errorThrown, "#C01");
237 int[] i1 = { 1, 2, 3, 4 };
239 int[] compare = {1,2,3,4};
240 Assert.AreEqual (compare[0], i1[0], "#C02");
241 Assert.AreEqual (compare[1], i1[1], "#C03");
242 Assert.AreEqual (compare[2], i1[2], "#C04");
243 Assert.AreEqual (compare[3], i1[3], "#C05");
245 Array.Clear(i1, 3, 1);
247 int[] compare = {1,2,3,0};
248 Assert.AreEqual (compare[0], i1[0], "#C06");
249 Assert.AreEqual (compare[1], i1[1], "#C07");
250 Assert.AreEqual (compare[2], i1[2], "#C08");
251 Assert.AreEqual (compare[3], i1[3], "#C09");
253 Array.Clear(i1, 1, 1);
255 int[] compare = {1,0,3,0};
256 Assert.AreEqual (compare[0], i1[0], "#C10");
257 Assert.AreEqual (compare[1], i1[1], "#C11");
258 Assert.AreEqual (compare[2], i1[2], "#C12");
259 Assert.AreEqual (compare[3], i1[3], "#C13");
261 Array.Clear(i1, 1, 3);
263 int[] compare = {1,0,0,0};
264 Assert.AreEqual (compare[0], i1[0], "#C14");
265 Assert.AreEqual (compare[1], i1[1], "#C15");
266 Assert.AreEqual (compare[2], i1[2], "#C16");
267 Assert.AreEqual (compare[3], i1[3], "#C17");
270 string[] s1 = { "red", "green", "blue" };
271 Array.Clear(s1, 0, 3);
273 string[] compare = {null, null, null};
274 Assert.AreEqual (compare[0], s1[0], "#C18");
275 Assert.AreEqual (compare[1], s1[1], "#C19");
276 Assert.AreEqual (compare[2], s1[2], "#C20");
281 public void TestClone() {
282 char[] c1 = {'a', 'b', 'c'};
283 char[] c2 = (char[])c1.Clone();
284 Assert.AreEqual (c1[0], c2[0], "#D01");
285 Assert.AreEqual (c1[1], c2[1], "#D02");
286 Assert.AreEqual (c1[2], c2[2], "#D03");
288 char[] d10 = {'a', 'b'};
289 char[] d11 = {'a', 'c'};
290 char[] d12 = {'b', 'c'};
291 char[][] d1 = {d10, d11, d12};
292 char[][] d2 = (char[][])d1.Clone();
293 Assert.AreEqual (d1[0], d2[0], "#D04");
294 Assert.AreEqual (d1[1], d2[1], "#D05");
295 Assert.AreEqual (d1[2], d2[2], "#D06");
298 Assert.AreEqual (d1[0], d2[0], "#D07");
301 [Test] public void TestIndexer ()
303 int [] a = new int [10];
307 Assert.Fail ("IList.this [-1] should throw");
308 } catch (IndexOutOfRangeException) {
311 Assert.Fail ("Should have thrown an IndexOutOfRangeException");
316 public void TestCopy() {
318 bool errorThrown = false;
321 Array.Copy(c1, null, 1);
322 } catch (ArgumentNullException) {
325 Assert.IsTrue (errorThrown, "#E01");
328 bool errorThrown = false;
331 Array.Copy(null, c1, 1);
332 } catch (ArgumentNullException) {
335 Assert.IsTrue (errorThrown, "#E02");
338 bool errorThrown = false;
340 Char[] c1 = new Char[1];
341 Char[,] c2 = new Char[1,1];
342 Array.Copy(c1, c2, 1);
343 } catch (RankException) {
346 Assert.IsTrue (errorThrown, "#E03");
349 bool errorThrown = false;
351 Char[] c1 = new Char[1];
352 string[] s1 = new String[1];
353 Array.Copy(c1, s1, 1);
354 } catch (ArrayTypeMismatchException) {
357 Assert.IsTrue (errorThrown, "#E04");
360 bool errorThrown = false;
362 Char[] c1 = new Char[1];
363 Object[] o1 = new Object[1];
365 Array.Copy(o1, c1, 1);
366 } catch (InvalidCastException) {
369 Assert.IsTrue (errorThrown, "#E05");
372 bool errorThrown = false;
374 Char[] c1 = new Char[1];
375 Char[] c2 = new Char[1];
376 Array.Copy(c1, c2, -1);
377 } catch (ArgumentOutOfRangeException) {
380 Assert.IsTrue (errorThrown, "#E06");
383 bool errorThrown = false;
385 Char[] c1 = new Char[1];
386 Char[] c2 = new Char[2];
387 Array.Copy(c1, c2, 2);
388 } catch (ArgumentException) {
391 Assert.IsTrue (errorThrown, "#E07");
394 bool errorThrown = false;
396 Char[] c1 = new Char[1];
397 Char[] c2 = new Char[2];
398 Array.Copy(c2, c1, 2);
399 } catch (ArgumentException) {
402 Assert.IsTrue (errorThrown, "#E08");
405 char[] orig = {'a', 'b', 'd', 'a'};
406 char[] copy = new Char[4];
407 Array.Copy(orig, copy, 4);
408 for (int i = 0; i < orig.Length; i++) {
409 Assert.AreEqual (orig[i], copy[i], "#E09(" + i + ")");
411 Array.Clear(copy, 0, copy.Length);
412 for (int i = 0; i < orig.Length; i++) {
413 Assert.AreEqual ((char)0, copy[i], "#E10(" + i + ")");
415 Array.Copy(orig, copy, 2);
416 Assert.AreEqual (orig[0], copy[0], "#E11");
417 Assert.AreEqual (orig[1], copy[1], "#E12");
418 Assert.IsTrue (orig[2] != copy[2], "#E13");
419 Assert.IsTrue (orig[3] != copy[3], "#E14");
423 public void TestCopy2() {
425 bool errorThrown = false;
427 Char[] c1 = new Char[2];
428 Char[] c2 = new Char[2];
429 Array.Copy(c2, 1, c1, 0, 2);
430 } catch (ArgumentException) {
433 Assert.IsTrue (errorThrown, "#E31");
436 bool errorThrown = false;
438 Char[] c1 = new Char[2];
439 Char[] c2 = new Char[2];
440 Array.Copy(c2, 0, c1, 1, 2);
441 } catch (ArgumentException) {
444 Assert.IsTrue (errorThrown, "#E32");
447 char[] orig = {'a', 'b', 'd', 'a'};
448 char[] copy = new Char[4];
449 Array.Copy(orig, 1, copy, 1, 3);
450 Assert.IsTrue (copy[0] != orig[0], "#E33");
451 for (int i = 1; i < orig.Length; i++) {
452 Assert.AreEqual (orig[i], copy[i], "#E34(" + i + ")");
454 Array.Clear(copy, 0, copy.Length);
455 Array.Copy(orig, 1, copy, 0, 2);
456 Assert.AreEqual (orig[1], copy[0], "#E35");
457 Assert.AreEqual (orig[2], copy[1], "#E36");
458 Assert.IsTrue (copy[2] != orig[2], "#E37");
459 Assert.IsTrue (copy[3] != orig[3], "#E38");
463 [ExpectedException (typeof (InvalidCastException))]
464 public void Copy_InvalidCast () {
465 object[] arr1 = new object [10];
466 Type[] arr2 = new Type [10];
468 arr1 [0] = new object ();
470 Array.Copy (arr1, 0, arr2, 0, 10);
474 public void TestCopyTo() {
476 bool errorThrown = false;
478 Char[] c1 = new Char[2];
480 } catch (ArgumentNullException) {
483 Assert.IsTrue (errorThrown, "#E61");
486 bool errorThrown = false;
488 Char[] c1 = new Char[2];
489 Char[,] c2 = new Char[2,2];
491 } catch (ArgumentException) {
494 #if TARGET_JVM // This is really implementation dependent behaviour.
495 catch (RankException) {
499 Assert.IsTrue (errorThrown, "#E62");
502 bool errorThrown = false;
504 Char[,] c1 = new Char[2,2];
505 Char[] c2 = new Char[2];
507 } catch (RankException) {
510 Assert.IsTrue (errorThrown, "#E63");
513 bool errorThrown = false;
515 Char[,] c1 = new Char[2,2];
516 Char[] c2 = new Char[2];
518 } catch (RankException) {
521 Assert.IsTrue (errorThrown, "#E64");
524 bool errorThrown = false;
526 Char[] c1 = new Char[2];
527 Char[] c2 = new Char[2];
529 } catch (ArgumentOutOfRangeException) {
532 Assert.IsTrue (errorThrown, "#E65");
535 bool errorThrown = false;
537 Char[] c1 = new Char[2];
538 Char[] c2 = new Char[2];
540 } catch (ArgumentException) {
543 Assert.IsTrue (errorThrown, "#E66");
546 bool errorThrown = false;
548 Char[] c1 = new Char[2];
549 Char[] c2 = new Char[2];
551 } catch (ArgumentException) {
554 Assert.IsTrue (errorThrown, "#E67");
558 bool errorThrown = false;
560 String[] c1 = new String[2];
561 // TODO: this crashes mono if there are null
562 // values in the array.
565 Char[] c2 = new Char[2];
569 } catch (ArrayTypeMismatchException) {
572 Assert.IsTrue (errorThrown, "#E68");
575 Char[] orig = {'a', 'b', 'c', 'd'};
576 Char[] copy = new Char[10];
577 Array.Clear(copy, 0, copy.Length);
578 orig.CopyTo(copy, 3);
579 Assert.AreEqual ((char)0, copy[0], "#E69");
580 Assert.AreEqual ((char)0, copy[1], "#E70");
581 Assert.AreEqual ((char)0, copy[2], "#E71");
582 Assert.AreEqual (orig[0], copy[3], "#E72");
583 Assert.AreEqual (orig[1], copy[4], "#E73");
584 Assert.AreEqual (orig[2], copy[5], "#E74");
585 Assert.AreEqual (orig[3], copy[6], "#E75");
586 Assert.AreEqual ((char)0, copy[7], "#E76");
587 Assert.AreEqual ((char)0, copy[8], "#E77");
588 Assert.AreEqual ((char)0, copy[9], "#E78");
591 // The following is valid and must not throw an exception.
592 bool errorThrown = false;
594 int[] src = new int [0];
595 int[] dest = new int [0];
596 src.CopyTo (dest, 0);
597 } catch (ArgumentException) {
600 Assert.IsTrue (!errorThrown, "#E79");
605 bool errorThrown = false;
607 CClass[] src = new CClass [] { new CClass () };
608 BClass[] dest = new BClass [1];
610 src.CopyTo (dest, 0);
612 } catch (ArrayTypeMismatchException) {
615 Assert.IsTrue (errorThrown, "#E80");
620 public void TestCreateInstance() {
622 bool errorThrown = false;
624 Array.CreateInstance(null, 12);
625 } catch (ArgumentNullException) {
628 Assert.IsTrue (errorThrown, "#F01");
631 bool errorThrown = false;
633 Array.CreateInstance(Type.GetType("System.Char"), -3);
634 } catch (ArgumentOutOfRangeException) {
637 Assert.IsTrue (errorThrown, "#F02");
640 bool errorThrown = false;
642 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
643 } catch (ArgumentNullException) {
646 Assert.IsTrue (errorThrown, "#F03a");
650 bool errorThrown = false;
652 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
653 } catch (ArgumentNullException) {
656 Assert.IsTrue (errorThrown, "#F03b");
659 #if !TARGET_JVM // Arrays lower bounds are not supported for TARGET_JVM
661 bool errorThrown = false;
663 Array.CreateInstance(Type.GetType("System.Char"), null, null);
664 } catch (ArgumentNullException) {
667 Assert.IsTrue (errorThrown, "#F04");
671 bool errorThrown = false;
673 int[] lengths = new int [0];
674 Array.CreateInstance(Type.GetType("System.Char"), lengths);
675 } catch (ArgumentException) {
678 Assert.IsTrue (errorThrown, "#F05");
680 #if !TARGET_JVM // CreateInstance with lower bounds not supported for TARGET_JVM
682 bool errorThrown = false;
684 int[] lengths = new int [1];
685 int[] bounds = new int [2];
686 Array.CreateInstance(Type.GetType("System.Char"), lengths, bounds);
688 } catch (ArgumentException) {
691 Assert.IsTrue (errorThrown, "#F06");
694 char[] c1 = (char[])Array.CreateInstance(Type.GetType("System.Char"), 12);
695 Assert.AreEqual (12, c1.Length, "#F07");
697 Array c2 = Array.CreateInstance(Type.GetType("System.Char"), 12, 5);
698 Assert.AreEqual (2, c2.Rank, "#F08");
699 Assert.AreEqual (60, c2.Length, "#F09");
703 int[] lengths = { 3 };
704 int[] bounds = { 5 };
705 int[] src = { 512, 718, 912 };
706 Array array = Array.CreateInstance(typeof(int), lengths, bounds);
708 Assert.AreEqual (3, array.Length, "#F10");
709 Assert.AreEqual (5, array.GetLowerBound(0), "#F11");
710 Assert.AreEqual (7, array.GetUpperBound(0), "#F12");
712 src.CopyTo (array, 5);
714 for (int i = 0; i < src.Length; i++)
715 Assert.AreEqual (src[i], array.GetValue(i+5), "#F13(" + i + ")");
718 // Test that a 1 dimensional array with 0 lower bound is the
719 // same as an szarray
720 Type szarrayType = new int [10].GetType ();
721 Assert.IsTrue (szarrayType == (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0})).GetType ());
722 Assert.IsTrue (szarrayType != (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {1})).GetType ());
727 [ExpectedException (typeof (ArgumentNullException))]
728 public void TestCreateInstance2 ()
730 Array.CreateInstance (typeof (Int32), (int[])null);
735 [ExpectedException (typeof (ArgumentNullException))]
737 [ExpectedException (typeof (NullReferenceException))]
739 public void TestCreateInstance2b ()
741 Array.CreateInstance (typeof (Int32), (long[])null);
745 public void TestGetEnumerator() {
746 String[] s1 = {"this", "is", "a", "test"};
747 IEnumerator en = s1.GetEnumerator ();
748 Assert.IsNotNull (en, "#G01");
750 Assert.IsTrue (en.MoveNext (), "#G02");
751 Assert.AreEqual ("this", en.Current, "#G03");
752 Assert.IsTrue (en.MoveNext (), "#G04");
753 Assert.AreEqual ("is", en.Current, "#G05");
754 Assert.IsTrue (en.MoveNext (), "#G06");
755 Assert.AreEqual ("a", en.Current, "#G07");
756 Assert.IsTrue (en.MoveNext (), "#G08");
757 Assert.AreEqual ("test", en.Current, "#G09");
758 Assert.IsTrue (!en.MoveNext (), "#G10");
761 Assert.IsTrue (en.MoveNext (), "#G11");
762 Assert.AreEqual ("this", en.Current, "#G12");
764 // mutation does not invalidate array enumerator!
765 s1.SetValue ("change", 1);
766 Assert.IsTrue (en.MoveNext (), "#G13");
767 Assert.AreEqual ("change", en.Current, "#G14");
771 public void TestGetEnumeratorMultipleDimension() {
772 String[,] s1 = {{"this", "is"}, {"a", "test"}};
773 IEnumerator en = s1.GetEnumerator ();
774 Assert.IsNotNull (en, "#AA01");
776 Assert.IsTrue (en.MoveNext (), "#AA02");
777 Assert.AreEqual ("this", en.Current, "#AA03");
778 Assert.IsTrue (en.MoveNext (), "#AA04");
779 Assert.AreEqual ("is", en.Current, "#AA05");
780 Assert.IsTrue (en.MoveNext (), "#AA06");
781 Assert.AreEqual ("a", en.Current, "#AA07");
782 Assert.IsTrue (en.MoveNext (), "#AA08");
783 Assert.AreEqual ("test", en.Current, "#AA09");
784 Assert.IsTrue (!en.MoveNext (), "#AA10");
787 Assert.IsTrue (en.MoveNext (), "#AA11");
788 Assert.AreEqual ("this", en.Current, "#AA12");
791 // mutation does not invalidate array enumerator!
792 s1.SetValue ("change", idxs);
793 Assert.IsTrue (en.MoveNext (), "#AA13");
794 Assert.AreEqual ("change", en.Current, "#AA14");
798 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
799 public void TestGetEnumeratorNonZeroLowerBounds() {
800 int[] myLengthsArray = new int[2] { 3, 5 };
801 int[] myBoundsArray = new int[2] { 2, 3 };
803 Array myArray=Array.CreateInstance( typeof(String), myLengthsArray, myBoundsArray );
804 for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
805 for ( int j = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++ ) {
806 int[] myIndicesArray = new int[2] { i, j };
807 myArray.SetValue( Convert.ToString(i) + j, myIndicesArray );
809 IEnumerator en = myArray.GetEnumerator ();
810 Assert.IsNotNull (en, "#AB01");
812 // check the first couple of values
813 Assert.IsTrue (en.MoveNext (), "#AB02");
814 Assert.AreEqual ("23", en.Current, "#AB03");
815 Assert.IsTrue (en.MoveNext (), "#AB04");
816 Assert.AreEqual ("24", en.Current, "#AB05");
818 // then check the last element's value
821 lastElement = (string)en.Current;
822 } while (en.MoveNext());
823 Assert.AreEqual ("47", lastElement, "#AB06");
827 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
828 public void TestIList_Add () {
829 int[] myLengthsArray = new int[2] { 3, 5 };
830 int[] myBoundsArray = new int[2] { 2, 3 };
832 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
834 ((IList)myArray).Add ("can not");
835 Assert.Fail ("IList.Add should throw");
837 catch (NotSupportedException) {
841 Assert.Fail ("IList.Add threw wrong exception type");
844 Assert.Fail ("IList.Add shouldn't get this far");
848 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
849 public void TestIList_Insert () {
850 int[] myLengthsArray = new int[2] { 3, 5 };
851 int[] myBoundsArray = new int[2] { 2, 3 };
853 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
855 ((IList)myArray).Insert (0, "can not");
856 Assert.Fail ("IList.Insert should throw");
858 catch (NotSupportedException) {
862 Assert.Fail ("IList.Insert threw wrong exception type");
865 Assert.Fail ("IList.Insert shouldn't get this far");
869 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
870 public void TestIList_Remove () {
871 int[] myLengthsArray = new int[2] { 3, 5 };
872 int[] myBoundsArray = new int[2] { 2, 3 };
874 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
876 ((IList)myArray).Remove ("can not");
877 Assert.Fail ("IList.Remove should throw");
879 catch (NotSupportedException) {
883 Assert.Fail ("IList.Remove threw wrong exception type");
886 Assert.Fail ("IList.Remove shouldn't get this far");
890 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
891 public void TestIList_RemoveAt () {
892 int[] myLengthsArray = new int[2] { 3, 5 };
893 int[] myBoundsArray = new int[2] { 2, 3 };
895 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
897 ((IList)myArray).RemoveAt (0);
898 Assert.Fail ("IList.RemoveAt should throw");
900 catch (NotSupportedException) {
904 Assert.Fail ("IList.RemoveAt threw wrong exception type");
907 Assert.Fail ("IList.RemoveAt shouldn't get this far");
911 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
912 public void TestIList_Contains () {
913 int[] myLengthsArray = new int[2] { 3, 5 };
914 int[] myBoundsArray = new int[2] { 2, 3 };
916 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
919 bool b = ((IList)myArray).Contains ("23");
920 Assert.Fail ("IList.Contains should throw with multi-dimensional arrays");
922 catch (RankException) {
923 int[] iArr = new int[3] { 1, 2, 3};
924 // check the first and last items
925 Assert.IsTrue (((IList)iArr).Contains (1), "AC01");
926 Assert.IsTrue (((IList)iArr).Contains (3), "AC02");
928 // and one that is definately not there
929 Assert.IsTrue (!((IList)iArr).Contains (42), "AC03");
933 Assert.Fail ("Should not get here");
937 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
938 public void TestIList_IndexOf () {
939 int[] myLengthsArray = new int[2] { 3, 5 };
940 int[] myBoundsArray = new int[2] { 2, 3 };
942 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
945 bool b = ((IList)myArray).Contains ("23");
946 Assert.Fail ("IList.Contains should throw with multi-dimensional arrays");
948 catch (RankException) {
949 int[] iArr = new int[3] { 1, 2, 3};
950 // check the first and last items
951 Assert.AreEqual (0, ((IList)iArr).IndexOf (1), "AD01");
952 Assert.AreEqual (2, ((IList)iArr).IndexOf (3), "AD02");
954 // and one that is definately not there
955 Assert.AreEqual (-1, ((IList)iArr).IndexOf (42), "AD03");
957 catch (Exception e) {
958 Assert.Fail ("Unexpected exception: " + e.ToString());
961 // check that wierd case whem lowerbound is Int32.MinValue,
962 // so that IndexOf() needs to return Int32.MaxValue when it cannot find the object
963 int[] myLengthArray = new int[1] { 3 };
964 int[] myBoundArray = new int[1] { Int32.MinValue };
965 Array myExtremeArray=Array.CreateInstance ( typeof(String), myLengthArray, myBoundArray );
966 Assert.AreEqual (Int32.MaxValue, ((IList)myExtremeArray).IndexOf (42), "AD04");
971 public void TestGetLength() {
973 bool errorThrown = false;
975 char[] c1 = {'a', 'b', 'c'};
977 } catch (IndexOutOfRangeException) {
980 Assert.IsTrue (errorThrown, "#H01");
983 bool errorThrown = false;
985 char[] c1 = {'a', 'b', 'c'};
987 } catch (IndexOutOfRangeException) {
990 Assert.IsTrue (errorThrown, "#H02");
993 char[] c2 = new Char[5];
994 Assert.AreEqual (5, c2.GetLength(0), "#H03");
996 char[,] c3 = new Char[6,7];
997 Assert.AreEqual (6, c3.GetLength(0), "#H04");
998 Assert.AreEqual (7, c3.GetLength(1), "#H05");
1002 public void TestGetLowerBound() {
1004 bool errorThrown = false;
1006 char[] c = {'a', 'b', 'c'};
1007 c.GetLowerBound(-1);
1008 } catch (IndexOutOfRangeException) {
1011 Assert.IsTrue (errorThrown, "#H31");
1014 bool errorThrown = false;
1016 char[] c = {'a', 'b', 'c'};
1018 } catch (IndexOutOfRangeException) {
1021 Assert.IsTrue (errorThrown, "#H32");
1024 char[] c1 = new Char[5];
1025 Assert.AreEqual (0, c1.GetLowerBound(0), "#H33");
1027 char[,] c2 = new Char[4,4];
1028 Assert.AreEqual (0, c2.GetLowerBound(0), "#H34");
1029 Assert.AreEqual (0, c2.GetLowerBound(1), "#H35");
1033 public void TestGetUpperBound() {
1035 bool errorThrown = false;
1037 char[] c = {'a', 'b', 'c'};
1038 c.GetUpperBound(-1);
1039 } catch (IndexOutOfRangeException) {
1042 Assert.IsTrue (errorThrown, "#H61");
1045 bool errorThrown = false;
1047 char[] c = {'a', 'b', 'c'};
1049 } catch (IndexOutOfRangeException) {
1052 Assert.IsTrue (errorThrown, "#H62");
1055 char[] c1 = new Char[5];
1056 Assert.AreEqual (4, c1.GetUpperBound(0), "#H63");
1058 char[,] c2 = new Char[4,6];
1059 Assert.AreEqual (3, c2.GetUpperBound(0), "#H64");
1060 Assert.AreEqual (5, c2.GetUpperBound(1), "#H65");
1064 public void TestGetValue1() {
1066 bool errorThrown = false;
1068 char[,] c = new Char[2,2];
1070 } catch (ArgumentException) {
1073 Assert.IsTrue (errorThrown, "#I01");
1076 bool errorThrown = false;
1078 char[] c = {'a', 'b', 'c'};
1080 } catch (IndexOutOfRangeException) {
1083 Assert.IsTrue (errorThrown, "#I02");
1086 bool errorThrown = false;
1088 char[] c = {'a', 'b', 'c'};
1090 } catch (IndexOutOfRangeException) {
1093 Assert.IsTrue (errorThrown, "#I03");
1096 char[] c1 = {'a', 'b', 'c', 'd'};
1097 for (int i = 0; i < c1.Length; i++) {
1098 Assert.AreEqual (c1[i], c1.GetValue(i), "#I04(" + i + ")");
1103 public void TestGetValue2() {
1105 bool errorThrown = false;
1107 char[] c = new Char[2];
1109 } catch (ArgumentException) {
1112 Assert.IsTrue (errorThrown, "#I21");
1115 bool errorThrown = false;
1117 char[,] c = new Char[2,2];
1119 } catch (IndexOutOfRangeException) {
1122 Assert.IsTrue (errorThrown, "#I22");
1125 bool errorThrown = false;
1127 char[,] c = new Char[2,2];
1129 } catch (IndexOutOfRangeException) {
1132 Assert.IsTrue (errorThrown, "#I23");
1135 char[,] c1 = new Char[4,6];
1136 for (int i = 0; i < 24; i++) {
1139 c1[first,second] = (char)(((int)'a')+i);
1141 for (int i = 0; i < c1.GetLength(0); i++) {
1142 for (int j = 0; j < c1.GetLength(1); j++) {
1143 Assert.AreEqual (c1[i, j], c1.GetValue(i, j), "#I24(" + i + "," + j + ")");
1149 public void TestGetValue3() {
1151 bool errorThrown = false;
1153 char[] c = new Char[2];
1155 } catch (ArgumentException) {
1158 Assert.IsTrue (errorThrown, "#I41");
1161 bool errorThrown = false;
1163 char[,,] c = new Char[2,2,2];
1164 c.GetValue(-1, 1, 1);
1165 } catch (IndexOutOfRangeException) {
1168 Assert.IsTrue (errorThrown, "#I42");
1171 bool errorThrown = false;
1173 char[,,] c = new Char[2,2,2];
1175 } catch (IndexOutOfRangeException) {
1178 Assert.IsTrue (errorThrown, "#I43");
1181 char[,,] c1 = new Char[4,2,3];
1182 for (int i = 0; i < 24; i++) {
1184 int remains = i % 6;
1185 int second = remains / 3;
1186 int third = remains % 3;
1187 c1[first,second, third] = (char)(((int)'a')+i);
1189 for (int i = 0; i < c1.GetLength(0); i++) {
1190 for (int j = 0; j < c1.GetLength(1); j++) {
1191 for (int k = 0; k < c1.GetLength(2); k++) {
1192 Assert.AreEqual (c1[i, j, k], c1.GetValue(i, j, k), "#I44(" + i + "," + j + ")");
1200 [ExpectedException (typeof (ArgumentNullException))]
1202 [ExpectedException (typeof (NullReferenceException))]
1204 public void TestGetValueLongArray ()
1206 char[] c = new Char[2];
1207 c.GetValue((long [])null);
1211 public void TestGetValueN() {
1213 bool errorThrown = false;
1215 char[] c = new Char[2];
1216 c.GetValue((int [])null);
1217 } catch (ArgumentNullException) {
1220 Assert.IsTrue (errorThrown, "#I61a");
1223 bool errorThrown = false;
1225 char[] c = new Char[2];
1226 int[] coords = {1, 1};
1228 } catch (ArgumentException) {
1231 Assert.IsTrue (errorThrown, "#I62");
1234 bool errorThrown = false;
1236 char[,] c = new Char[2,2];
1237 int[] coords = {-1, 1};
1239 } catch (IndexOutOfRangeException) {
1242 Assert.IsTrue (errorThrown, "#I63");
1245 bool errorThrown = false;
1247 char[,] c = new Char[2,2];
1248 int[] coords = {4, 1};
1250 } catch (IndexOutOfRangeException) {
1253 Assert.IsTrue (errorThrown, "#I64");
1256 char[,] c1 = new Char[4,6];
1257 for (int i = 0; i < 24; i++) {
1260 c1[first,second] = (char)(((int)'a')+i);
1262 for (int i = 0; i < c1.GetLength(0); i++) {
1263 for (int j = 0; j < c1.GetLength(1); j++) {
1264 int[] coords = {i, j};
1265 Assert.AreEqual (c1[i, j], c1.GetValue(coords), "#I65(" + i + "," + j + ")");
1271 public void TestIndexOf1() {
1273 bool errorThrown = false;
1275 Array.IndexOf(null, "huh?");
1276 } catch (ArgumentNullException) {
1279 Assert.IsTrue (errorThrown, "#J01");
1282 bool errorThrown = false;
1284 char[,] c = new Char[2,2];
1285 Array.IndexOf(c, "huh?");
1286 } catch (RankException) {
1289 Assert.IsTrue (errorThrown, "#J02");
1292 String[] s1 = {"this", "is", "a", "test"};
1293 Assert.AreEqual (-1, Array.IndexOf(s1, null), "#J03");
1294 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing"), "#J04");
1295 Assert.AreEqual (0, Array.IndexOf(s1, "this"), "#J05");
1296 Assert.AreEqual (3, Array.IndexOf(s1, "test"), "#J06");
1300 public void TestIndexOf2() {
1302 bool errorThrown = false;
1304 Array.IndexOf(null, "huh?", 0);
1305 } catch (ArgumentNullException) {
1308 Assert.IsTrue (errorThrown, "#J21");
1311 bool errorThrown = false;
1313 char[,] c = new Char[2,2];
1314 Array.IndexOf(c, "huh?", 0);
1315 } catch (RankException) {
1318 Assert.IsTrue (errorThrown, "#J22");
1321 bool errorThrown = false;
1323 char[] c = new Char[2];
1324 Array.IndexOf(c, "huh?", 3);
1325 } catch (ArgumentOutOfRangeException) {
1328 Assert.IsTrue (errorThrown, "#J23");
1331 String[] s1 = {"this", "is", "really", "a", "test"};
1332 Assert.AreEqual (-1, Array.IndexOf(s1, null, 1), "#J24");
1333 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing", 1), "#J25");
1334 Assert.AreEqual (-1, Array.IndexOf(s1, "this", 1), "#J26");
1335 Assert.AreEqual (1, Array.IndexOf(s1, "is", 1), "#J27");
1336 Assert.AreEqual (4, Array.IndexOf(s1, "test", 1), "#J28");
1340 public void TestIndexOf3() {
1342 bool errorThrown = false;
1344 Array.IndexOf(null, "huh?", 0, 1);
1345 } catch (ArgumentNullException) {
1348 Assert.IsTrue (errorThrown, "#J41");
1351 bool errorThrown = false;
1353 char[,] c = new Char[2,2];
1354 Array.IndexOf(c, "huh?", 0, 1);
1355 } catch (RankException) {
1358 Assert.IsTrue (errorThrown, "#J42");
1361 bool errorThrown = false;
1363 char[] c = new Char[2];
1364 Array.IndexOf(c, "huh?", 3, 1);
1365 } catch (ArgumentOutOfRangeException) {
1368 Assert.IsTrue (errorThrown, "#J43");
1371 bool errorThrown = false;
1373 char[] c = new Char[2];
1374 Array.IndexOf(c, "huh?", 0, 5);
1375 } catch (ArgumentOutOfRangeException) {
1378 Assert.IsTrue (errorThrown, "#J44");
1381 String[] s1 = {"this", "is", "really", "a", "test"};
1382 Assert.AreEqual (-1, Array.IndexOf(s1, null, 1, 3), "#J45");
1383 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing", 1, 3), "#J46");
1384 Assert.AreEqual (-1, Array.IndexOf(s1, "this", 1, 3), "#J47");
1385 Assert.AreEqual (1, Array.IndexOf(s1, "is", 1, 3), "#J48");
1386 Assert.AreEqual (-1, Array.IndexOf(s1, "test", 1, 3), "#J49");
1387 Assert.AreEqual (3, Array.IndexOf(s1, "a", 1, 3), "#J50");
1391 public void TestIndexOf_CustomEqual ()
1393 DataEqual[] test = new DataEqual [] { new DataEqual () };
1394 Assert.AreEqual (0, Array.IndexOf (test, "asdfas", 0));
1396 IList array = (IList)test;
1397 Assert.AreEqual (0, array.IndexOf ("asdfas"));
1401 public void TestLastIndexOf1() {
1403 bool errorThrown = false;
1405 Array.LastIndexOf(null, "huh?");
1406 } catch (ArgumentNullException) {
1409 Assert.IsTrue (errorThrown, "#K01");
1412 bool errorThrown = false;
1414 char[,] c = new Char[2,2];
1415 Array.LastIndexOf(c, "huh?");
1416 } catch (RankException) {
1419 Assert.IsTrue (errorThrown, "#K02");
1422 String[] s1 = {"this", "is", "a", "a", "test"};
1423 Assert.AreEqual (-1, Array.LastIndexOf(s1, null), "#K03");
1424 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing"), "#K04");
1425 Assert.AreEqual (0, Array.LastIndexOf(s1, "this"), "#K05");
1426 Assert.AreEqual (4, Array.LastIndexOf(s1, "test"), "#K06");
1427 Assert.AreEqual (3, Array.LastIndexOf(s1, "a"), "#K07");
1429 Assert.AreEqual (-1, Array.LastIndexOf (new String [0], "foo"));
1433 public void TestLastIndexOf2() {
1435 bool errorThrown = false;
1437 Array.LastIndexOf(null, "huh?", 0);
1438 } catch (ArgumentNullException) {
1441 Assert.IsTrue (errorThrown, "#K21");
1444 bool errorThrown = false;
1446 char[,] c = new Char[2,2];
1447 Array.LastIndexOf(c, "huh?", 0);
1448 } catch (RankException) {
1451 Assert.IsTrue (errorThrown, "#K22");
1454 bool errorThrown = false;
1456 char[] c = new Char[2];
1457 Array.LastIndexOf(c, "huh?", 3);
1458 } catch (ArgumentOutOfRangeException) {
1461 Assert.IsTrue (errorThrown, "#K23");
1464 String[] s1 = {"this", "is", "really", "a", "test"};
1465 Assert.AreEqual (-1, Array.LastIndexOf(s1, null, 3), "#K24");
1466 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing", 3), "#K25");
1467 Assert.AreEqual (-1, Array.LastIndexOf(s1, "test", 3), "#K26");
1468 Assert.AreEqual (3, Array.LastIndexOf(s1, "a", 3), "#K27");
1469 Assert.AreEqual (0, Array.LastIndexOf(s1, "this", 3), "#K28");
1473 public void TestLastIndexOf3() {
1475 bool errorThrown = false;
1477 Array.LastIndexOf(null, "huh?", 0, 1);
1478 } catch (ArgumentNullException) {
1481 Assert.IsTrue (errorThrown, "#K41");
1484 bool errorThrown = false;
1486 char[,] c = new Char[2,2];
1487 Array.LastIndexOf(c, "huh?", 0, 1);
1488 } catch (RankException) {
1491 Assert.IsTrue (errorThrown, "#K42");
1494 bool errorThrown = false;
1496 char[] c = new Char[2];
1497 Array.LastIndexOf(c, "huh?", 3, 1);
1498 } catch (ArgumentOutOfRangeException) {
1501 Assert.IsTrue (errorThrown, "#K43");
1504 bool errorThrown = false;
1506 char[] c = new Char[2];
1507 Array.LastIndexOf(c, "huh?", 0, 5);
1508 } catch (ArgumentOutOfRangeException) {
1511 Assert.IsTrue (errorThrown, "#K44");
1514 String[] s1 = {"this", "is", "really", "a", "test"};
1515 Assert.AreEqual (-1, Array.LastIndexOf(s1, null, 3, 3), "#K45");
1516 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing", 3, 3), "#K46");
1517 Assert.AreEqual (-1, Array.LastIndexOf(s1, "this", 3, 3), "#K47");
1518 Assert.AreEqual (1, Array.LastIndexOf(s1, "is", 3, 3), "#K48");
1519 Assert.AreEqual (-1, Array.LastIndexOf(s1, "test", 3, 3), "#K49");
1520 Assert.AreEqual (3, Array.LastIndexOf(s1, "a", 3, 3), "#K50");
1524 public void TestLastIndexOf4 ()
1526 short [] a = new short [] { 19, 238, 317, 6, 565, 0, -52, 60, -563, 753, 238, 238};
1528 Array.LastIndexOf (a, (object)16, -1);
1529 NUnit.Framework.Assert.Fail ("#1");
1530 } catch (ArgumentOutOfRangeException) { }
1534 Array.LastIndexOf<short> (a, 16, -1);
1535 NUnit.Framework.Assert.Fail ("#2");
1536 } catch (ArgumentOutOfRangeException) { }
1541 public void TestLastIndexOf5 ()
1543 char [] a = new char [] {'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'j', 'i', 'h'};
1548 for (int i = a.Length - 1; i >= 0 ; i--) {
1550 retval = Array.LastIndexOf(a, a [i], i, i + 1);
1554 Assert.IsTrue (!error);
1558 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1559 public void LastIndexOf_StartIndexOverflow ()
1561 // legal - no exception
1562 byte[] array = new byte [16];
1563 Array.LastIndexOf (array, this, Int32.MaxValue, 1);
1567 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1568 public void LastIndexOf_CountOverflow ()
1570 // legal - no exception
1571 byte[] array = new byte [16];
1572 Array.LastIndexOf (array, this, 1, Int32.MaxValue);
1576 public void TestReverse() {
1578 bool errorThrown = false;
1580 Array.Reverse(null);
1581 } catch (ArgumentNullException) {
1584 Assert.IsTrue (errorThrown, "#L01");
1587 bool errorThrown = false;
1589 char[,] c = new Char[2,2];
1591 } catch (RankException) {
1594 Assert.IsTrue (errorThrown, "#L02");
1597 char[] c1 = {'a', 'b', 'c', 'd'};
1599 Assert.AreEqual ('d', c1[0], "#L03");
1600 Assert.AreEqual ('c', c1[1], "#L04");
1601 Assert.AreEqual ('b', c1[2], "#L05");
1602 Assert.AreEqual ('a', c1[3], "#L06");
1605 bool errorThrown = false;
1607 Array.Reverse(null, 0, 0);
1608 } catch (ArgumentNullException) {
1611 Assert.IsTrue (errorThrown, "#L07");
1614 bool errorThrown = false;
1616 char[,] c = new Char[2,2];
1617 Array.Reverse(c, 0, 0);
1618 } catch (RankException) {
1621 Assert.IsTrue (errorThrown, "#L08");
1624 //bool errorThrown = false;
1626 // char[] c = new Char[2];
1627 // Array.Reverse(c, 0, 3);
1628 //} catch (ArgumentOutOfRangeException) {
1629 // errorThrown = true;
1631 //Assert.IsTrue (errorThrown, "#L09");
1634 //bool errorThrown = false;
1636 // char[] c = new Char[2];
1637 // Array.Reverse(c, 3, 0);
1638 //} catch (ArgumentOutOfRangeException) {
1639 // errorThrown = true;
1641 //Assert.IsTrue (errorThrown, "#L10");
1644 char[] c2 = { 'a', 'b', 'c', 'd'};
1645 Array.Reverse(c2, 1, 2);
1646 Assert.AreEqual ('a', c2[0], "#L11");
1647 Assert.AreEqual ('c', c2[1], "#L12");
1648 Assert.AreEqual ('b', c2[2], "#L13");
1649 Assert.AreEqual ('d', c2[3], "#L14");
1653 public void TestSetValue1() {
1655 bool errorThrown = false;
1657 char[,] c = new Char[2,2];
1658 c.SetValue("buh", 1);
1659 } catch (ArgumentException) {
1662 Assert.IsTrue (errorThrown, "#M01");
1665 bool errorThrown = false;
1667 char[] c = {'a', 'b', 'c'};
1668 c.SetValue("buh", -1);
1669 } catch (IndexOutOfRangeException) {
1672 Assert.IsTrue (errorThrown, "#M02");
1675 bool errorThrown = false;
1677 char[] c = {'a', 'b', 'c'};
1678 c.SetValue("buh", 4);
1679 } catch (IndexOutOfRangeException) {
1682 Assert.IsTrue (errorThrown, "#M03");
1685 char[] c1 = {'a', 'b', 'c', 'd'};
1686 char[] c2 = new char[4];
1687 for (int i = 0; i < c1.Length; i++) {
1688 c2.SetValue(c1[i], i);
1690 for (int i = 0; i < c1.Length; i++) {
1691 Assert.AreEqual (c1[i], c2[i], "#M04(" + i + ")");
1694 int[] c3 = { 1, 2, 3 };
1695 long[] c4 = new long [3];
1697 for (int i = 0; i < c3.Length; i++)
1698 c4.SetValue (c3 [i], i);
1702 } catch (Exception e) {
1703 Assert.Fail ("c3.CopyTo(): e=" + e);
1705 for (int i = 0; i < c3.Length; i++)
1706 Assert.IsTrue (c3[i] == c4[i], "#M05(" + i + ")");
1708 Object[] c5 = new Object [3];
1709 long[] c6 = new long [3];
1713 } catch (Exception e) {
1714 Assert.Fail ("c4.CopyTo(): e=" + e);
1719 } catch (Exception e) {
1720 Assert.Fail ("c5.CopyTo(): e=" + e);
1722 // for (int i = 0; i < c5.Length; i++)
1723 // Assert.IsTrue (c5[i] == c6[i], "#M06(" + i + ")");
1727 public void TestSetValue2() {
1729 bool errorThrown = false;
1731 char[] c = new Char[2];
1732 c.SetValue("buh", 1,1);
1733 } catch (ArgumentException) {
1736 Assert.IsTrue (errorThrown, "#M21");
1739 bool errorThrown = false;
1741 char[,] c = new Char[2,2];
1742 c.SetValue("buh", -1, 1);
1743 } catch (IndexOutOfRangeException) {
1746 Assert.IsTrue (errorThrown, "#M22");
1749 bool errorThrown = false;
1751 char[,] c = new Char[2,2];
1752 c.SetValue("buh", 4,1);
1753 } catch (IndexOutOfRangeException) {
1756 Assert.IsTrue (errorThrown, "#M23");
1759 char[,] c1 = new Char[4,6];
1760 char[,] c2 = new Char[4,6];
1761 for (int i = 0; i < 24; i++) {
1764 c1[first,second] = (char)(((int)'a')+i);
1765 c2.SetValue(c1[first,second], first, second);
1767 for (int i = 0; i < c1.GetLength(0); i++) {
1768 for (int j = 0; j < c1.GetLength(1); j++) {
1769 Assert.AreEqual (c1[i, j], c2[i, j], "#M24(" + i + "," + j + ")");
1775 public void TestSetValue3() {
1777 bool errorThrown = false;
1779 char[] c = new Char[2];
1780 c.SetValue("buh", 1,1,1);
1781 } catch (ArgumentException) {
1784 Assert.IsTrue (errorThrown, "#M41");
1787 bool errorThrown = false;
1789 char[,,] c = new Char[2,2,2];
1790 c.SetValue("buh", -1, 1, 1);
1791 } catch (IndexOutOfRangeException) {
1794 Assert.IsTrue (errorThrown, "#M42");
1797 bool errorThrown = false;
1799 char[,,] c = new Char[2,2,2];
1800 c.SetValue("buh", 4,1,1);
1801 } catch (IndexOutOfRangeException) {
1804 Assert.IsTrue (errorThrown, "#M43");
1807 char[,,] c1 = new Char[4,2,3];
1808 char[,,] c2 = new Char[4,2,3];
1809 for (int i = 0; i < 24; i++) {
1811 int remains = i % 6;
1812 int second = remains / 3;
1813 int third = remains % 3;
1814 c1[first,second, third] = (char)(((int)'a')+i);
1815 c2.SetValue(c1[first, second, third], first, second, third);
1817 for (int i = 0; i < c1.GetLength(0); i++) {
1818 for (int j = 0; j < c1.GetLength(1); j++) {
1819 for (int k = 0; k < c1.GetLength(2); k++) {
1820 Assert.AreEqual (c1[i, j, k], c2[i, j, k], "#M44(" + i + "," + j + " )");
1828 [ExpectedException (typeof (ArgumentNullException))]
1830 [ExpectedException (typeof (NullReferenceException))]
1832 public void TestSetValueLongArray ()
1834 char[] c = new Char[2];
1835 c.SetValue("buh", (long [])null);
1839 public void TestSetValueN() {
1841 bool errorThrown = false;
1843 char[] c = new Char[2];
1844 c.SetValue("buh", (int [])null);
1845 } catch (ArgumentNullException) {
1848 Assert.IsTrue (errorThrown, "#M61a");
1851 bool errorThrown = false;
1853 char[] c = new Char[2];
1854 int[] coords = {1, 1};
1855 c.SetValue("buh", coords);
1856 } catch (ArgumentException) {
1859 Assert.IsTrue (errorThrown, "#M62");
1862 bool errorThrown = false;
1864 char[,] c = new Char[2,2];
1865 int[] coords = {-1, 1};
1866 c.SetValue("buh", coords);
1867 } catch (IndexOutOfRangeException) {
1870 Assert.IsTrue (errorThrown, "#M63");
1873 bool errorThrown = false;
1875 char[,] c = new Char[2,2];
1876 int[] coords = {4, 1};
1877 c.SetValue("buh", coords);
1878 } catch (IndexOutOfRangeException) {
1881 Assert.IsTrue (errorThrown, "#M64");
1884 char[,] c1 = new Char[4,6];
1885 char[,] c2 = new Char[4,6];
1886 for (int i = 0; i < 24; i++) {
1889 c1[first,second] = (char)(((int)'a')+i);
1890 int[] coords = {first, second};
1891 c2.SetValue(c1[first,second], coords);
1893 for (int i = 0; i < c1.GetLength(0); i++) {
1894 for (int j = 0; j < c1.GetLength(1); j++) {
1895 Assert.AreEqual (c1[i, j], c2[i, j], "#M65(" + i + "," + j + ")");
1901 public void TestSetValue4() {
1903 int[] c1 = { 1, 2, 3 };
1904 long[] c2 = new long [3];
1906 for (int i = 0; i < c1.Length; i++)
1907 c2.SetValue (c1 [i], i);
1909 for (int i = 0; i < c1.Length; i++) {
1910 Assert.IsTrue (c1[i] == c2[i], "#M81(" + i + ")");
1911 Assert.AreEqual (typeof (long), c2[i].GetType (), "#M82(" + i + ")");
1915 long[] c1 = { 1, 2, 3 };
1916 int[] c2 = new int [3];
1917 bool errorThrown = false;
1919 c2.SetValue (c1 [0], 0);
1920 } catch (ArgumentException) {
1923 Assert.IsTrue (errorThrown, "#M83");
1926 int[] c1 = { 1, 2, 3 };
1927 Object[] c2 = new Object [3];
1929 for (int i = 0; i < c1.Length; i++)
1930 c2.SetValue (c1 [i], i);
1932 for (int i = 0; i < c1.Length; i++)
1933 Assert.AreEqual (c1[i], Convert.ToInt32 (c2[i]), "#M84(" + i + ")");
1936 Object[] c1 = new Object [3];
1937 Object[] c2 = new Object [3];
1938 c1[0] = new Object ();
1940 for (int i = 0; i < c1.Length; i++)
1941 c2.SetValue (c1 [i], i);
1943 for (int i = 0; i < c1.Length; i++)
1944 Assert.AreEqual (c1[i], c2[i], "#M85(" + i + ")");
1947 Object[] c1 = new Object [3];
1948 string[] c2 = new String [3];
1949 string test = "hello";
1952 c2.SetValue (c1 [0], 0);
1953 Assert.AreEqual (c1[0], c2[0], "#M86");
1954 Assert.AreEqual ("hello", c2[0], "#M87");
1957 char[] c1 = { 'a', 'b', 'c' };
1958 string[] c2 = new string [3];
1960 c2.SetValue (c1 [0], 0);
1961 Assert.Fail ("#M88");
1962 } catch (InvalidCastException) {}
1965 Single[] c1 = { 1.2F, 2.3F, 3.4F, 4.5F };
1966 long[] c2 = new long [3];
1968 c2.SetValue (c1 [0], 0);
1969 Assert.Fail ("#M89");
1970 } catch (ArgumentException) {}
1998 UInt32 v11 = 235354;
1999 UInt64 v12 = 234552;
2001 Object[] va1 = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12 };
2002 Object[] va2 = { "true", "1", "a", "-1.2", "-32", "-234", "-34523", "-1",
2003 "-4.8F", "24234", "235354", "234552" };
2005 Object[][] vt = { va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1 };
2008 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2009 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
2010 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
2011 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
2012 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1,
2013 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1,
2014 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,
2015 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1,
2016 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1,
2017 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
2018 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0,
2019 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0
2024 for (int i = 0; i < types.Length; i++) {
2025 for (int j = 0; j < types.Length; j++) {
2026 Array array = Array.CreateInstance (types [j], 2);
2028 Object value = vt[j][i];
2030 bool errorThrown = false;
2032 array.SetValue (value, 0);
2033 } catch (ArgumentException) {
2037 int ex_index = (i * types.Length) + j;
2039 Assert.AreEqual (errorThrown, arg_ex [ex_index] == 1, "#M90(" + types [i] + "," + types [j] + ")");
2043 for (int i = 0; i < types.Length; i++) {
2044 String[] array = new String [2];
2046 Object value = va1 [i];
2048 bool errorThrown = false;
2050 array.SetValue (value, 0);
2051 } catch (InvalidCastException) {
2055 Assert.IsTrue (errorThrown, "#M91(" + types [i] + ")");
2058 for (int i = 0; i < types.Length; i++) {
2059 Array array = Array.CreateInstance (types [i], 2);
2061 Object value = va2 [i];
2063 bool errorThrown = false;
2065 array.SetValue (value, 0);
2066 } catch (InvalidCastException) {
2070 Assert.IsTrue (errorThrown, "#M92(" + types [i] + ")");
2073 for (int i = 0; i < types.Length; i++) {
2074 Array array = Array.CreateInstance (types [i], 2);
2076 Object value = null;
2078 bool errorThrown = false;
2080 array.SetValue (value, 0);
2081 } catch (InvalidCastException) {
2085 Assert.IsTrue (!errorThrown, "#M93(" + types [i] + ")");
2090 for (int i = 0; i < types.Length; i++) {
2091 for (int j = 0; j < types.Length; j++) {
2092 Array source = Array.CreateInstance (types [i], 2);
2093 Array array = Array.CreateInstance (types [j], 2);
2095 source.SetValue (vt[j][i], 0);
2096 source.SetValue (vt[j][i], 1);
2098 bool errorThrown = false;
2100 Array.Copy (source, array, 2);
2101 } catch (ArrayTypeMismatchException) {
2105 int ex_index = (i * types.Length) + j;
2107 Assert.AreEqual (errorThrown, arg_ex [ex_index] == 1, "#M94(" + types [i] + "," + types [j] + ")");
2111 for (int i = 0; i < types.Length; i++) {
2112 Array source = Array.CreateInstance (types [i], 2);
2113 String[] array = new String [2];
2115 source.SetValue (va1 [i], 0);
2116 source.SetValue (va1 [i], 1);
2118 bool errorThrown = false;
2120 Array.Copy (source, array, 2);
2121 } catch (ArrayTypeMismatchException) {
2125 Assert.IsTrue (errorThrown, "#M95(" + types [i] + ")");
2128 for (int i = 0; i < types.Length; i++) {
2129 String[] source = new String [2];
2130 Array array = Array.CreateInstance (types [i], 2);
2132 source.SetValue (va2 [i], 0);
2133 source.SetValue (va2 [i], 1);
2135 bool errorThrown = false;
2137 Array.Copy (source, array, 2);
2138 } catch (ArrayTypeMismatchException) {
2142 Assert.IsTrue (errorThrown, "#M96(" + types [i] + ")");
2148 public void TestSort() {
2150 bool errorThrown = false;
2153 } catch (ArgumentNullException) {
2156 Assert.IsTrue (errorThrown, "#N01");
2159 bool errorThrown = false;
2161 Array.Sort(null, 0, 1);
2162 } catch (ArgumentNullException) {
2165 Assert.IsTrue (errorThrown, "#N02");
2168 bool errorThrown = false;
2170 char[] c1 = new Char[2];
2171 Array.Sort(null, c1);
2172 } catch (ArgumentNullException) {
2175 Assert.IsTrue (errorThrown, "#N03");
2178 bool errorThrown = false;
2180 char[] c1 = new Char[2];
2181 Array.Sort(null, c1, 0, 1);
2182 } catch (ArgumentNullException) {
2185 Assert.IsTrue (errorThrown, "#N04");
2189 char[] arr = {'d', 'b', 'f', 'e', 'a', 'c'};
2192 Array.Sort (null, 0, 1);
2193 Assert.Fail ("#N" + tc.ToString ());
2195 catch (ArgumentException) {}
2196 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2200 Array.Sort (arr, -1, 3);
2201 Assert.Fail ("#N" + tc.ToString ());
2203 catch (ArgumentException) {}
2204 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2208 Array.Sort (arr, 1, -3);
2209 Assert.Fail ("#N" + tc.ToString ());
2211 catch (ArgumentException) {}
2212 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2216 Array.Sort (arr, arr.Length, arr.Length + 2);
2217 Assert.Fail ("#N" + tc.ToString ());
2219 catch (ArgumentException) {}
2220 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2223 // note: null second array => just sort first array
2224 char[] starter = {'d', 'b', 'f', 'e', 'a', 'c'};
2225 int[] starter1 = {1,2,3,4,5,6};
2227 char[] c1 = (char[])starter.Clone();
2229 Assert.AreEqual ('a', c1[0], "#N21");
2230 Assert.AreEqual ('b', c1[1], "#N22");
2231 Assert.AreEqual ('c', c1[2], "#N23");
2232 Assert.AreEqual ('d', c1[3], "#N24");
2233 Assert.AreEqual ('e', c1[4], "#N25");
2234 Assert.AreEqual ('f', c1[5], "#N26");
2237 char[] c1 = (char[])starter.Clone();
2238 int[] i1 = (int[])starter1.Clone();
2240 Assert.AreEqual ('a', c1[0], "#N41");
2241 Assert.AreEqual ('b', c1[1], "#N42");
2242 Assert.AreEqual ('c', c1[2], "#N43");
2243 Assert.AreEqual ('d', c1[3], "#N44");
2244 Assert.AreEqual ('e', c1[4], "#N45");
2245 Assert.AreEqual ('f', c1[5], "#N46");
2246 Assert.AreEqual (5, i1[0], "#N47");
2247 Assert.AreEqual (2, i1[1], "#N48");
2248 Assert.AreEqual (6, i1[2], "#N49");
2249 Assert.AreEqual (1, i1[3], "#N50");
2250 Assert.AreEqual (4, i1[4], "#N51");
2251 Assert.AreEqual (3, i1[5], "#N52");
2254 char[] c1 = (char[])starter.Clone();
2255 Array.Sort(c1, 1, 4);
2256 Assert.AreEqual ('d', c1[0], "#N61");
2257 Assert.AreEqual ('a', c1[1], "#N62");
2258 Assert.AreEqual ('b', c1[2], "#N63");
2259 Assert.AreEqual ('e', c1[3], "#N64");
2260 Assert.AreEqual ('f', c1[4], "#N65");
2261 Assert.AreEqual ('c', c1[5], "#N66");
2264 char[] c1 = (char[])starter.Clone();
2265 int[] i1 = (int[])starter1.Clone();
2266 Array.Sort(c1, i1, 1, 4);
2267 Assert.AreEqual ('d', c1[0], "#N81");
2268 Assert.AreEqual ('a', c1[1], "#N82");
2269 Assert.AreEqual ('b', c1[2], "#N83");
2270 Assert.AreEqual ('e', c1[3], "#N84");
2271 Assert.AreEqual ('f', c1[4], "#N85");
2272 Assert.AreEqual ('c', c1[5], "#N86");
2273 Assert.AreEqual (1, i1[0], "#N87");
2274 Assert.AreEqual (5, i1[1], "#N88");
2275 Assert.AreEqual (2, i1[2], "#N89");
2276 Assert.AreEqual (4, i1[3], "#N90");
2277 Assert.AreEqual (3, i1[4], "#N91");
2278 Assert.AreEqual (6, i1[5], "#N92");
2283 public void TestInitializeEmpty()
2295 Assert.IsTrue (!catched, "#TI01");
2299 public void TestInitializeInt()
2304 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2306 Assert.AreEqual (a[i], b[i], "#TI02 " + i);
2311 public void TestInitializeDouble()
2313 double[] a = {1.0,2.0,0.0};
2315 double[] b = {1.0,2.0,0.0};
2316 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2318 Assert.AreEqual (a[i], b[i], "#TI03 " + i);
2323 public void TestInitializeFloat()
2325 float[] a = {1.0F,2.0F,0.0F};
2327 float[] b = {1.0F,2.0F,0.0F};
2328 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2330 Assert.AreEqual (a[i], b[i], "#TI04 " + i);
2335 public void TestInitializeChar()
2337 char[] a = {'1','.','0','F','2','.','0','F'};
2339 char[] b = {'1','.','0','F','2','.','0','F'};
2340 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2342 Assert.AreEqual (a[i], b[i], "#TI05 " + i);
2347 public void TestInitializeString()
2349 string[] a = {"hola","adios","menos","mas"};
2351 string[] b = {"hola","adios","menos","mas"};
2352 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2354 Assert.AreEqual (a[i], b[i], "#TI06 " + i);
2359 public void TestInitializeEnum()
2361 enua[] a = {enua.hola,enua.adios,enua.menos,enua.mas};
2363 enua[] b = {enua.hola,enua.adios,enua.menos,enua.mas};
2364 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2366 Assert.AreEqual (a[i], b[i], "#TI07 " + i);
2371 public void TestInitializeIntNI()
2373 int[] a = new int[20];
2377 Assert.AreEqual (b, 0, "#TI08");
2382 public void TestInitializeCharNI()
2384 char[] a = new char[20];
2386 foreach(char b in a)
2388 Assert.AreEqual (b, 0, "#TI09");
2393 public void TestInitializeDoubleNI()
2395 double[] a = new double[20];
2397 foreach(double b in a)
2399 Assert.AreEqual (b, 0.0, "#TI09");
2404 public void TestInitializeStringNI()
2406 string[] a = new string[20];
2408 foreach(string b in a)
2410 Assert.AreEqual (b, null, "#TI10");
2415 public void TestInitializeObjectNI()
2417 object[] a = new object[20];
2419 foreach(object b in a)
2421 Assert.AreEqual (b, null, "#TI11");
2426 public void TestInitializeAClassNI()
2428 AClass[] a = new AClass[20];
2430 foreach(AClass b in a)
2432 Assert.AreEqual (b, null, "#TI12");
2438 public void TestInitializeAStructNI()
2440 AStruct[] a = new AStruct[20];
2442 foreach(AStruct b in a)
2444 Assert.AreEqual (b, new AStruct(), "#TI14");
2449 public void TestInitializeAStruct()
2451 AStruct[] a = new AStruct[3];
2455 AStruct[] b = new AStruct[3];
2458 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2460 Assert.AreEqual (a[i], b[i], "#TI15 " + i);
2465 public void TestInitializeDateTimeNI()
2467 DateTime[] a = new DateTime[20];
2469 foreach(DateTime b in a)
2471 Assert.AreEqual (b, new DateTime(), "#TI16");
2476 [ExpectedException (typeof (ArgumentNullException))]
2477 public void MoreSort1 ()
2479 Array.Sort (null, 0, 1);
2483 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2484 public void MoreSort2 ()
2486 Array.Sort (arrsort, -1, 3);
2490 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2491 public void MoreSort3 ()
2493 Array.Sort (arrsort, 1, -3);
2497 [ExpectedException (typeof (ArgumentException))]
2498 public void MoreSort4 ()
2500 Array.Sort (arrsort, arrsort.Length, arrsort.Length + 2);
2504 [ExpectedException (typeof (RankException))]
2505 public void MoreSort5 ()
2507 char [,] arr = new char [,] {{'a'}, {'b'}};
2508 Array.Sort (arr, 0, 1);
2512 public void MoreSort6 ()
2514 Array.Sort (arrsort, 0, 0);
2518 [ExpectedException (typeof (ArgumentException))]
2519 public void MoreSort7 ()
2521 Array.Sort (arrsort, arrsort.Length - 1, 2);
2525 [ExpectedException (typeof (ArgumentException))]
2526 public void MoreSort8 ()
2528 Array.Sort (arrsort, 0, arrsort.Length + 1);
2532 public void MoreSort9 ()
2534 Array.Sort (arrsort, null, 0, arrsort.Length, null);
2538 [ExpectedException (typeof (InvalidOperationException))]
2539 public void MoreSort10 ()
2541 object [] array = {true, 'k', SByte.MinValue, Byte.MinValue, (short) 2, 634, (long) 436, (float) 1.1, 1.23, "Hello World"};
2542 Array.Sort (array, (IComparer) null);
2545 [Test] // bug #81941
2548 double [] a = new double [2] { 0.9, 0.3 };
2549 uint [] b = new uint [2] { 4, 7 };
2551 Assert.AreEqual (0.3, a [0], "#1");
2552 Assert.AreEqual (0.9, a [1], "#2");
2553 Assert.AreEqual (7, b [0], "#3");
2554 Assert.AreEqual (4, b [1], "#4");
2558 public void ClearJaggedArray ()
2560 byte[][] matrix = new byte [8][];
2561 for (int i=0; i < 8; i++) {
2562 matrix [i] = new byte [8];
2563 for (int j=0; j < 8; j++) {
2567 Array.Clear (matrix, 0, 8);
2568 for (int i=0; i < 8; i++) {
2569 Assert.IsNull (matrix [i], i.ToString ());
2574 public void ClearMultidimentionalArray ()
2576 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2577 Array.Clear (matrix, 0, 2);
2578 Assert.AreEqual (0, matrix [0, 0], "0,0");
2579 Assert.AreEqual (0, matrix [0, 1], "0,1");
2580 Assert.AreEqual (2, matrix [1, 0], "1,0");
2581 Assert.AreEqual (2, matrix [1, 1], "1,1");
2585 [ExpectedException (typeof (IndexOutOfRangeException))]
2586 public void ClearOutsideMultidimentionalArray ()
2588 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2589 Array.Clear (matrix, 0, 5);
2593 [ExpectedException (typeof (IndexOutOfRangeException))]
2594 public void Clear_IndexOverflow ()
2596 byte[] array = new byte [16];
2597 Array.Clear (array, 4, Int32.MaxValue);
2601 [ExpectedException (typeof (IndexOutOfRangeException))]
2602 public void Clear_LengthOverflow ()
2604 byte[] array = new byte [16];
2605 Array.Clear (array, Int32.MaxValue, 4);
2609 [ExpectedException (typeof (ArgumentException))]
2610 public void Copy_SourceIndexOverflow ()
2612 byte[] array = new byte [16];
2613 Array.Copy (array, Int32.MaxValue, array, 8, 8);
2617 [ExpectedException (typeof (ArgumentException))]
2618 public void Copy_DestinationIndexOverflow ()
2620 byte[] array = new byte [16];
2621 Array.Copy (array, 8, array, Int32.MaxValue, 8);
2625 [ExpectedException (typeof (ArgumentException))]
2626 public void Copy_LengthOverflow ()
2628 byte[] array = new byte [16];
2629 Array.Copy (array, 8, array, 8, Int32.MaxValue);
2633 [ExpectedException (typeof (ArgumentException))]
2634 public void Reverse_IndexOverflow ()
2636 byte[] array = new byte [16];
2637 Array.Reverse (array, Int32.MaxValue, 8);
2641 [ExpectedException (typeof (ArgumentException))]
2642 public void Reverse_LengthOverflow ()
2644 byte[] array = new byte [16];
2645 Array.Reverse (array, 8, Int32.MaxValue);
2648 public struct CharX : IComparable {
2651 public CharX (char c)
2656 public int CompareTo (object obj)
2659 return c.CompareTo (((CharX) obj).c);
2661 return c.CompareTo (obj);
2666 public void BinarySearch_ArgPassingOrder ()
2669 // This tests that arguments are passed to the comprer in the correct
2670 // order. The IComparable of the *array* elements must get called, not
2671 // that of the search object.
2673 CharX [] x = { new CharX ('a'), new CharX ('b'), new CharX ('c') };
2674 Assert.AreEqual (1, Array.BinarySearch (x, 'b'));
2677 class Comparer: IComparer {
2679 private bool called = false;
2681 public bool Called {
2683 bool result = called;
2689 public int Compare (object x, object y)
2697 public void BinarySearch1_EmptyList ()
2699 int[] array = new int[0];
2700 Assert.AreEqual (- 1, Array.BinarySearch (array, 0), "BinarySearch");
2704 public void BinarySearch2_EmptyList ()
2706 int[] array = new int[0];
2707 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, 0), "BinarySearch");
2711 public void BinarySearch3_EmptyList ()
2713 Comparer comparer = new Comparer ();
2714 int[] array = new int[0];
2715 Assert.AreEqual (-1, Array.BinarySearch (array, 0, comparer), "BinarySearch");
2716 // bug 77030 - the comparer isn't called for an empty array/list
2717 Assert.IsTrue (!comparer.Called, "Called");
2721 public void BinarySearch4_EmptyList ()
2723 Comparer comparer = new Comparer ();
2724 int[] array = new int[0];
2725 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, comparer), "BinarySearch");
2726 // bug 77030 - the comparer isn't called for an empty array/list
2727 Assert.IsTrue (!comparer.Called, "Called");
2732 [ExpectedException (typeof (ArgumentNullException))]
2733 public void AsReadOnly_NullArray ()
2735 Array.AsReadOnly <int> (null);
2739 public void ReadOnly_Count ()
2741 Assert.AreEqual (10, Array.AsReadOnly (new int [10]).Count);
2745 public void ReadOnly_Contains ()
2747 int[] arr = new int [2];
2750 IList<int> a = Array.AsReadOnly (arr);
2752 Assert.IsTrue (a.Contains (3));
2753 Assert.IsTrue (!a.Contains (6));
2757 public void ReadOnly_IndexOf ()
2759 int[] arr = new int [2];
2762 IList<int> a = Array.AsReadOnly (arr);
2764 Assert.AreEqual (0, a.IndexOf (3));
2765 Assert.AreEqual (1, a.IndexOf (5));
2766 Assert.AreEqual (-1, a.IndexOf (6));
2770 public void ReadOnly_Indexer ()
2772 int[] arr = new int [2];
2775 IList<int> a = Array.AsReadOnly (arr);
2777 Assert.AreEqual (3, a [0]);
2778 Assert.AreEqual (5, a [1]);
2780 /* Check that modifications to the original array are visible */
2782 Assert.AreEqual (6, a [0]);
2786 public void ReadOnly_Enumerator ()
2788 int[] arr = new int [10];
2790 for (int i = 0; i < 10; ++i)
2794 foreach (int i in Array.AsReadOnly (arr))
2797 Assert.AreEqual (45, sum);
2801 public void Resize ()
2803 int [] arr = new int [] { 1, 3, 5 };
2804 Array.Resize <int> (ref arr, 3);
2805 Assert.AreEqual (3, arr.Length, "#A1");
2806 Assert.AreEqual (1, arr [0], "#A2");
2807 Assert.AreEqual (3, arr [1], "#A3");
2808 Assert.AreEqual (5, arr [2], "#A4");
2810 Array.Resize <int> (ref arr, 2);
2811 Assert.AreEqual (2, arr.Length, "#B1");
2812 Assert.AreEqual (1, arr [0], "#B2");
2813 Assert.AreEqual (3, arr [1], "#B3");
2815 Array.Resize <int> (ref arr, 4);
2816 Assert.AreEqual (4, arr.Length, "#C1");
2817 Assert.AreEqual (1, arr [0], "#C2");
2818 Assert.AreEqual (3, arr [1], "#C3");
2819 Assert.AreEqual (0, arr [2], "#C4");
2820 Assert.AreEqual (0, arr [3], "#C5");
2824 public void Resize_null ()
2827 Array.Resize (ref arr, 10);
2828 Assert.AreEqual (arr.Length, 10);
2832 public void Test_ContainsAndIndexOf_EquatableItem ()
2834 EquatableClass[] list = new EquatableClass[] {new EquatableClass (0), new EquatableClass (1), new EquatableClass (0)};
2836 Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, list[0]), "#0");
2837 Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, new EquatableClass (0)), "#1");
2838 Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, list[0]), "#2");
2839 Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, new EquatableClass (0)), "#3");
2842 public class EquatableClass : IEquatable<EquatableClass>
2845 public EquatableClass (int x)
2850 public bool Equals (EquatableClass other)
2852 return this._x == other._x;
2857 public void AsIList ()
2859 IList<int> arr = new int [10];
2861 Assert.AreEqual (5, arr [0]);
2863 IList<FooStruct> arr2 = new FooStruct [10];
2864 FooStruct s = new FooStruct ();
2869 Assert.AreEqual (11, s.i);
2870 Assert.AreEqual (22, s.j);
2872 IList<string> arr3 = new string [10];
2874 Assert.AreEqual ("ABC", arr3 [5]);
2881 #if !TARGET_JVM // BugBUG: T[] is not yet ICollection<T> under TARGET_JVM
2884 public void ICollectionNull ()
2886 ICollection<object> test;
2888 test = new List<object>();
2889 Assert.AreEqual (test.Contains (null), false, "list<o>");
2891 test = new object[] {};
2892 Assert.AreEqual (test.Contains (null), false, "empty array");
2894 test = new object[] {null};
2895 Assert.AreEqual (test.Contains (null), true, "array with null");
2897 test = new List<object>(test);
2898 Assert.AreEqual (test.Contains (null), true, "List<object> with test");
2900 test = new object[] {new object()};
2901 Assert.AreEqual (test.Contains (null), false, "array with object");
2903 test = new List<object>(test);
2904 Assert.AreEqual (test.Contains (null), false, "array with test");
2906 #endif // TARGET_JVM
2911 enum ByteEnum : byte {}
2912 enum IntEnum : int {}
2915 public void TestByteEnumArrayToByteArray ()
2917 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
2918 byte[] b = new byte[a.Length];
2923 public void TestByteEnumArrayToIntArray ()
2925 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
2926 int[] b = new int[a.Length];
2931 [ExpectedException (typeof (ArrayTypeMismatchException))]
2932 public void TestIntEnumArrayToByteArray ()
2934 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
2935 byte[] b = new byte[a.Length];
2940 public void TestIntEnumArrayToIntArray ()
2942 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
2943 int[] b = new int[a.Length];
2950 [Test] // bug #322248
2951 public void IEnumerator_Reset ()
2953 int[] array = new int[] { 1, 2, 3};
2954 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
2955 Assert.IsTrue (e.MoveNext (), "#A1");
2956 Assert.AreEqual (1, e.Current, "#A2");
2957 Assert.IsTrue (e.MoveNext (), "#A3");
2958 Assert.AreEqual (2, e.Current, "#A4");
2962 Assert.IsTrue (e.MoveNext (), "#C1");
2963 Assert.AreEqual (1, e.Current, "#C2");
2967 public void IEnumerator_Current_Finished ()
2969 int[] array = new int[] { 1, 2, 3 };
2970 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
2971 Assert.IsTrue (e.MoveNext (), "#A1");
2972 Assert.AreEqual (1, e.Current, "#A2");
2973 Assert.IsTrue (e.MoveNext (), "#A3");
2974 Assert.AreEqual (2, e.Current, "#A4");
2975 Assert.IsTrue (e.MoveNext (), "#A5");
2976 Assert.AreEqual (3, e.Current, "#A6");
2977 Assert.IsTrue (!e.MoveNext (), "#A6");
2980 Assert.Fail ("#B1:" + e.Current);
2981 } catch (InvalidOperationException ex) {
2982 // Enumeration already finished
2983 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
2984 Assert.IsNull (ex.InnerException, "#B3");
2985 Assert.IsNotNull (ex.Message, "#B4");
2990 public void IEnumerator_Current_NotStarted ()
2992 int[] array = new int[] { 1, 2, 3 };
2993 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
2996 Assert.Fail ("#A1:" + e.Current);
2997 } catch (InvalidOperationException ex) {
2998 // Enumeration has not started. Call MoveNext
2999 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
3000 Assert.IsNull (ex.InnerException, "#A3");
3001 Assert.IsNotNull (ex.Message, "#A4");
3006 public void IEnumerator_Current_Reset ()
3008 int[] array = new int[] { 1, 2, 3 };
3009 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3014 Assert.Fail ("#B1:" + e.Current);
3015 } catch (InvalidOperationException ex) {
3016 // Enumeration has not started. Call MoveNext
3017 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3018 Assert.IsNull (ex.InnerException, "#B3");
3019 Assert.IsNotNull (ex.Message, "#B4");
3023 public void ICollection_IsReadOnly() {
3024 ICollection<string> arr = new string [10];
3026 Assert.IsTrue (arr.IsReadOnly);