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'};
81 public void TestIsFixedSize() {
83 Assert.IsTrue (a1.IsFixedSize, "All arrays are fixed");
87 public void TestIsReadOnly() {
89 Assert.IsTrue (!a1.IsReadOnly, "No array is readonly");
93 public void TestIsSynchronized() {
95 Assert.IsTrue (!a1.IsSynchronized, "No array is synchronized");
99 public void TestLength() {
102 Assert.AreEqual (0, a1.Length, "Zero length array");
106 Assert.AreEqual (1, a1.Length, "One-length array");
109 char[] a1 = {'c', 'c'};
110 Assert.AreEqual (2, a1.Length, "Two-length array");
115 public void TestRank() {
116 char[] a1 = { 'c', 'd', 'e' };
117 Assert.AreEqual (1, a1.Rank, "Rank one");
119 char[,] a2 = new Char[3,3];
120 Assert.AreEqual (2, a2.Rank, "Rank two");
122 char[,,] a3 = new Char[3,3,3];
123 Assert.AreEqual (3, a3.Rank, "Rank three");
127 public void TestBinarySearch1() {
128 bool errorThrown = false;
130 Array.BinarySearch(null, "blue");
131 } catch (ArgumentNullException) {
134 Assert.IsTrue (errorThrown, "#B01");
137 char[,] c1 = new Char[2,2];
138 Array.BinarySearch(c1, "needle");
139 } catch (RankException) {
142 Assert.IsTrue (errorThrown, "#B02");
145 char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
146 Assert.IsTrue (Array.BinarySearch(arr, 'c') >= 3, "#B05");
147 Assert.IsTrue (Array.BinarySearch(arr, 'c') < 6, "#B06");
150 char[] arr = {'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
151 Assert.AreEqual (-4, Array.BinarySearch(arr, 'c'), "#B07");
154 char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
155 Assert.AreEqual (-9, Array.BinarySearch(arr, 'e'), "#B08");
160 public void TestBinarySearch2() {
161 bool errorThrown = false;
163 Array.BinarySearch(null, 0, 1, "blue");
164 } catch (ArgumentNullException) {
167 Assert.IsTrue (errorThrown, "#B20");
170 char[,] c1 = new Char[2,2];
171 Array.BinarySearch(c1, 0, 1, "needle");
172 } catch (RankException) {
175 Assert.IsTrue (errorThrown, "#B21");
179 Array.BinarySearch(c1, -1, 1, 'a');
180 } catch (ArgumentOutOfRangeException) {
183 Assert.IsTrue (errorThrown, "#B22");
187 Array.BinarySearch(c1, 0, -1, 'a');
188 } catch (ArgumentOutOfRangeException) {
191 Assert.IsTrue (errorThrown, "#B23");
195 Array.BinarySearch(c1, 0, 4, 'a');
196 } catch (ArgumentException) {
199 Assert.IsTrue (errorThrown, "#B24");
202 char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
203 Assert.IsTrue (Array.BinarySearch(arr, 2, 8, 'c') >= 5, "#B26");
204 Assert.IsTrue (Array.BinarySearch(arr, 2, 8, 'c') < 8, "#B27");
207 char[] arr = {'z', 'z', 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
208 Assert.AreEqual (-6, Array.BinarySearch(arr, 2, 8, 'c'), "#B28");
211 char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
212 Assert.AreEqual (-11, Array.BinarySearch(arr, 2, 8, 'e'), "#B29");
216 public void TestBinarySearch3()
218 int[] array = new int[100];
220 for (int i = 0; i < 100; i++)
223 Assert.AreEqual (49, Array.BinarySearch(array, 10), "#B30");
227 public void BinarySearch_NullValue ()
229 int[] array = new int[1];
230 Assert.AreEqual (-1, Array.BinarySearch (array, null), "I=a,o");
231 Assert.AreEqual (-1, Array.BinarySearch (array, null, null), "I=a,o,c");
232 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 1, null), "I=a,i,i,o");
233 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 1, null, null), "I=a,i,i,o,c");
235 object[] o = new object [3] { this, this, null };
236 Assert.AreEqual (-1, Array.BinarySearch (o, null), "O=a,o");
237 Assert.AreEqual (-1, Array.BinarySearch (o, null, null), "O=a,o,c");
238 Assert.AreEqual (-1, Array.BinarySearch (o, 0, 3, null), "O=a,i,i,o");
239 Assert.AreEqual (-1, Array.BinarySearch (o, 0, 3, null, null), "O=a,i,i,o,c");
242 class TestComparer7 : IComparer<int>
244 public int Compare (int x, int y)
247 throw new ApplicationException ();
249 return x.CompareTo (y);
254 public void BinarySearch_WithComparer ()
256 var a = new int[] { 2, 6, 9 };
257 Assert.AreEqual (-3, Array.BinarySearch (a, 7, new TestComparer7 ()));
261 public void TestClear() {
262 bool errorThrown = false;
264 Array.Clear(null, 0, 1);
265 } catch (ArgumentNullException) {
268 Assert.IsTrue (errorThrown, "#C01");
270 int[] i1 = { 1, 2, 3, 4 };
272 int[] compare = {1,2,3,4};
273 Assert.AreEqual (compare[0], i1[0], "#C02");
274 Assert.AreEqual (compare[1], i1[1], "#C03");
275 Assert.AreEqual (compare[2], i1[2], "#C04");
276 Assert.AreEqual (compare[3], i1[3], "#C05");
278 Array.Clear(i1, 3, 1);
280 int[] compare = {1,2,3,0};
281 Assert.AreEqual (compare[0], i1[0], "#C06");
282 Assert.AreEqual (compare[1], i1[1], "#C07");
283 Assert.AreEqual (compare[2], i1[2], "#C08");
284 Assert.AreEqual (compare[3], i1[3], "#C09");
286 Array.Clear(i1, 1, 1);
288 int[] compare = {1,0,3,0};
289 Assert.AreEqual (compare[0], i1[0], "#C10");
290 Assert.AreEqual (compare[1], i1[1], "#C11");
291 Assert.AreEqual (compare[2], i1[2], "#C12");
292 Assert.AreEqual (compare[3], i1[3], "#C13");
294 Array.Clear(i1, 1, 3);
296 int[] compare = {1,0,0,0};
297 Assert.AreEqual (compare[0], i1[0], "#C14");
298 Assert.AreEqual (compare[1], i1[1], "#C15");
299 Assert.AreEqual (compare[2], i1[2], "#C16");
300 Assert.AreEqual (compare[3], i1[3], "#C17");
303 string[] s1 = { "red", "green", "blue" };
304 Array.Clear(s1, 0, 3);
306 string[] compare = {null, null, null};
307 Assert.AreEqual (compare[0], s1[0], "#C18");
308 Assert.AreEqual (compare[1], s1[1], "#C19");
309 Assert.AreEqual (compare[2], s1[2], "#C20");
314 public void TestClone() {
315 char[] c1 = {'a', 'b', 'c'};
316 char[] c2 = (char[])c1.Clone();
317 Assert.AreEqual (c1[0], c2[0], "#D01");
318 Assert.AreEqual (c1[1], c2[1], "#D02");
319 Assert.AreEqual (c1[2], c2[2], "#D03");
321 char[] d10 = {'a', 'b'};
322 char[] d11 = {'a', 'c'};
323 char[] d12 = {'b', 'c'};
324 char[][] d1 = {d10, d11, d12};
325 char[][] d2 = (char[][])d1.Clone();
326 Assert.AreEqual (d1[0], d2[0], "#D04");
327 Assert.AreEqual (d1[1], d2[1], "#D05");
328 Assert.AreEqual (d1[2], d2[2], "#D06");
331 Assert.AreEqual (d1[0], d2[0], "#D07");
335 public void TestMemberwiseClone () {
336 int[] array = new int[] { 1, 2, 3 };
337 MethodBase mi = array.GetType ().GetMethod("MemberwiseClone",
338 BindingFlags.Instance | BindingFlags.NonPublic);
339 int[] res = (int[])mi.Invoke (array, null);
340 Assert.AreEqual (3, res.Length);
343 [Test] public void TestIndexer ()
345 int [] a = new int [10];
349 Assert.Fail ("IList.this [-1] should throw");
350 } catch (IndexOutOfRangeException) {
353 Assert.Fail ("Should have thrown an IndexOutOfRangeException");
358 public void TestCopy() {
360 bool errorThrown = false;
363 Array.Copy(c1, null, 1);
364 } catch (ArgumentNullException) {
367 Assert.IsTrue (errorThrown, "#E01");
370 bool errorThrown = false;
373 Array.Copy(null, c1, 1);
374 } catch (ArgumentNullException) {
377 Assert.IsTrue (errorThrown, "#E02");
380 bool errorThrown = false;
382 Char[] c1 = new Char[1];
383 Char[,] c2 = new Char[1,1];
384 Array.Copy(c1, c2, 1);
385 } catch (RankException) {
388 Assert.IsTrue (errorThrown, "#E03");
391 bool errorThrown = false;
393 Char[] c1 = new Char[1];
394 string[] s1 = new String[1];
395 Array.Copy(c1, s1, 1);
396 } catch (ArrayTypeMismatchException) {
399 Assert.IsTrue (errorThrown, "#E04");
402 bool errorThrown = false;
404 Char[] c1 = new Char[1];
405 Object[] o1 = new Object[1];
407 Array.Copy(o1, c1, 1);
408 } catch (InvalidCastException) {
411 Assert.IsTrue (errorThrown, "#E05");
414 bool errorThrown = false;
416 Char[] c1 = new Char[1];
417 Char[] c2 = new Char[1];
418 Array.Copy(c1, c2, -1);
419 } catch (ArgumentOutOfRangeException) {
422 Assert.IsTrue (errorThrown, "#E06");
425 bool errorThrown = false;
427 Char[] c1 = new Char[1];
428 Char[] c2 = new Char[2];
429 Array.Copy(c1, c2, 2);
430 } catch (ArgumentException) {
433 Assert.IsTrue (errorThrown, "#E07");
436 bool errorThrown = false;
438 Char[] c1 = new Char[1];
439 Char[] c2 = new Char[2];
440 Array.Copy(c2, c1, 2);
441 } catch (ArgumentException) {
444 Assert.IsTrue (errorThrown, "#E08");
447 char[] orig = {'a', 'b', 'd', 'a'};
448 char[] copy = new Char[4];
449 Array.Copy(orig, copy, 4);
450 for (int i = 0; i < orig.Length; i++) {
451 Assert.AreEqual (orig[i], copy[i], "#E09(" + i + ")");
453 Array.Clear(copy, 0, copy.Length);
454 for (int i = 0; i < orig.Length; i++) {
455 Assert.AreEqual ((char)0, copy[i], "#E10(" + i + ")");
457 Array.Copy(orig, copy, 2);
458 Assert.AreEqual (orig[0], copy[0], "#E11");
459 Assert.AreEqual (orig[1], copy[1], "#E12");
460 Assert.IsTrue (orig[2] != copy[2], "#E13");
461 Assert.IsTrue (orig[3] != copy[3], "#E14");
465 public void TestCopy2() {
467 bool errorThrown = false;
469 Char[] c1 = new Char[2];
470 Char[] c2 = new Char[2];
471 Array.Copy(c2, 1, c1, 0, 2);
472 } catch (ArgumentException) {
475 Assert.IsTrue (errorThrown, "#E31");
478 bool errorThrown = false;
480 Char[] c1 = new Char[2];
481 Char[] c2 = new Char[2];
482 Array.Copy(c2, 0, c1, 1, 2);
483 } catch (ArgumentException) {
486 Assert.IsTrue (errorThrown, "#E32");
489 char[] orig = {'a', 'b', 'd', 'a'};
490 char[] copy = new Char[4];
491 Array.Copy(orig, 1, copy, 1, 3);
492 Assert.IsTrue (copy[0] != orig[0], "#E33");
493 for (int i = 1; i < orig.Length; i++) {
494 Assert.AreEqual (orig[i], copy[i], "#E34(" + i + ")");
496 Array.Clear(copy, 0, copy.Length);
497 Array.Copy(orig, 1, copy, 0, 2);
498 Assert.AreEqual (orig[1], copy[0], "#E35");
499 Assert.AreEqual (orig[2], copy[1], "#E36");
500 Assert.IsTrue (copy[2] != orig[2], "#E37");
501 Assert.IsTrue (copy[3] != orig[3], "#E38");
505 public void Copy_InvalidCast () {
506 object[] arr1 = new object [10];
507 Type[] arr2 = new Type [10];
508 arr1 [0] = new object ();
511 Array.Copy (arr1, 0, arr2, 0, 10);
513 } catch (InvalidCastException) {
516 var arr1_2 = new I [1] { new DI () };
517 var arr2_2 = new C [1] { new DC () };
519 Array.Copy (arr2_2, arr1_2, 1);
521 } catch (InvalidCastException) {
526 public void TestCopyTo() {
528 bool errorThrown = false;
530 Char[] c1 = new Char[2];
532 } catch (ArgumentNullException) {
535 Assert.IsTrue (errorThrown, "#E61");
538 bool errorThrown = false;
540 Char[] c1 = new Char[2];
541 Char[,] c2 = new Char[2,2];
543 } catch (ArgumentException) {
546 #if TARGET_JVM // This is really implementation dependent behaviour.
547 catch (RankException) {
551 Assert.IsTrue (errorThrown, "#E62");
554 bool errorThrown = false;
556 Char[,] c1 = new Char[2,2];
557 Char[] c2 = new Char[2];
559 } catch (RankException) {
562 Assert.IsTrue (errorThrown, "#E63");
565 bool errorThrown = false;
567 Char[,] c1 = new Char[2,2];
568 Char[] c2 = new Char[2];
570 } catch (RankException) {
573 Assert.IsTrue (errorThrown, "#E64");
576 bool errorThrown = false;
578 Char[] c1 = new Char[2];
579 Char[] c2 = new Char[2];
581 } catch (ArgumentOutOfRangeException) {
584 Assert.IsTrue (errorThrown, "#E65");
587 bool errorThrown = false;
589 Char[] c1 = new Char[2];
590 Char[] c2 = new Char[2];
592 } catch (ArgumentException) {
595 Assert.IsTrue (errorThrown, "#E66");
598 bool errorThrown = false;
600 Char[] c1 = new Char[2];
601 Char[] c2 = new Char[2];
603 } catch (ArgumentException) {
606 Assert.IsTrue (errorThrown, "#E67");
610 bool errorThrown = false;
612 String[] c1 = new String[2];
613 // TODO: this crashes mono if there are null
614 // values in the array.
617 Char[] c2 = new Char[2];
621 } catch (ArrayTypeMismatchException) {
624 Assert.IsTrue (errorThrown, "#E68");
627 Char[] orig = {'a', 'b', 'c', 'd'};
628 Char[] copy = new Char[10];
629 Array.Clear(copy, 0, copy.Length);
630 orig.CopyTo(copy, 3);
631 Assert.AreEqual ((char)0, copy[0], "#E69");
632 Assert.AreEqual ((char)0, copy[1], "#E70");
633 Assert.AreEqual ((char)0, copy[2], "#E71");
634 Assert.AreEqual (orig[0], copy[3], "#E72");
635 Assert.AreEqual (orig[1], copy[4], "#E73");
636 Assert.AreEqual (orig[2], copy[5], "#E74");
637 Assert.AreEqual (orig[3], copy[6], "#E75");
638 Assert.AreEqual ((char)0, copy[7], "#E76");
639 Assert.AreEqual ((char)0, copy[8], "#E77");
640 Assert.AreEqual ((char)0, copy[9], "#E78");
643 // The following is valid and must not throw an exception.
644 bool errorThrown = false;
646 int[] src = new int [0];
647 int[] dest = new int [0];
648 src.CopyTo (dest, 0);
649 } catch (ArgumentException) {
652 Assert.IsTrue (!errorThrown, "#E79");
657 bool errorThrown = false;
659 CClass[] src = new CClass [] { new CClass () };
660 BClass[] dest = new BClass [1];
662 src.CopyTo (dest, 0);
664 } catch (ArrayTypeMismatchException) {
667 Assert.IsTrue (errorThrown, "#E80");
672 public void TestCreateInstance() {
674 bool errorThrown = false;
676 Array.CreateInstance(null, 12);
677 } catch (ArgumentNullException) {
680 Assert.IsTrue (errorThrown, "#F01");
683 bool errorThrown = false;
685 Array.CreateInstance(Type.GetType("System.Char"), -3);
686 } catch (ArgumentOutOfRangeException) {
689 Assert.IsTrue (errorThrown, "#F02");
692 bool errorThrown = false;
694 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
695 } catch (ArgumentNullException) {
698 Assert.IsTrue (errorThrown, "#F03a");
702 bool errorThrown = false;
704 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
705 } catch (ArgumentNullException) {
708 Assert.IsTrue (errorThrown, "#F03b");
710 #if !TARGET_JVM // Arrays lower bounds are not supported for TARGET_JVM
712 bool errorThrown = false;
714 Array.CreateInstance(Type.GetType("System.Char"), null, null);
715 } catch (ArgumentNullException) {
718 Assert.IsTrue (errorThrown, "#F04");
722 bool errorThrown = false;
724 int[] lengths = new int [0];
725 Array.CreateInstance(Type.GetType("System.Char"), lengths);
726 } catch (ArgumentException) {
729 Assert.IsTrue (errorThrown, "#F05");
731 #if !TARGET_JVM // CreateInstance with lower bounds not supported for TARGET_JVM
733 bool errorThrown = false;
735 int[] lengths = new int [1];
736 int[] bounds = new int [2];
737 Array.CreateInstance(Type.GetType("System.Char"), lengths, bounds);
739 } catch (ArgumentException) {
742 Assert.IsTrue (errorThrown, "#F06");
745 char[] c1 = (char[])Array.CreateInstance(Type.GetType("System.Char"), 12);
746 Assert.AreEqual (12, c1.Length, "#F07");
748 Array c2 = Array.CreateInstance(Type.GetType("System.Char"), 12, 5);
749 Assert.AreEqual (2, c2.Rank, "#F08");
750 Assert.AreEqual (60, c2.Length, "#F09");
754 int[] lengths = { 3 };
755 int[] bounds = { 5 };
756 int[] src = { 512, 718, 912 };
757 Array array = Array.CreateInstance(typeof(int), lengths, bounds);
759 Assert.AreEqual (3, array.Length, "#F10");
760 Assert.AreEqual (5, array.GetLowerBound(0), "#F11");
761 Assert.AreEqual (7, array.GetUpperBound(0), "#F12");
763 src.CopyTo (array, 5);
765 for (int i = 0; i < src.Length; i++)
766 Assert.AreEqual (src[i], array.GetValue(i+5), "#F13(" + i + ")");
769 // Test that a 1 dimensional array with 0 lower bound is the
770 // same as an szarray
771 Type szarrayType = new int [10].GetType ();
772 Assert.IsTrue (szarrayType == (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0})).GetType ());
773 Assert.IsTrue (szarrayType != (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {1})).GetType ());
778 [ExpectedException (typeof (ArgumentNullException))]
779 public void TestCreateInstance2 ()
781 Array.CreateInstance (typeof (Int32), (int[])null);
785 [ExpectedException (typeof (ArgumentNullException))]
786 public void TestCreateInstance2b ()
788 Array.CreateInstance (typeof (Int32), (long[])null);
792 public void TestGetEnumerator() {
793 String[] s1 = {"this", "is", "a", "test"};
794 IEnumerator en = s1.GetEnumerator ();
795 Assert.IsNotNull (en, "#G01");
797 Assert.IsTrue (en.MoveNext (), "#G02");
798 Assert.AreEqual ("this", en.Current, "#G03");
799 Assert.IsTrue (en.MoveNext (), "#G04");
800 Assert.AreEqual ("is", en.Current, "#G05");
801 Assert.IsTrue (en.MoveNext (), "#G06");
802 Assert.AreEqual ("a", en.Current, "#G07");
803 Assert.IsTrue (en.MoveNext (), "#G08");
804 Assert.AreEqual ("test", en.Current, "#G09");
805 Assert.IsTrue (!en.MoveNext (), "#G10");
808 Assert.IsTrue (en.MoveNext (), "#G11");
809 Assert.AreEqual ("this", en.Current, "#G12");
811 // mutation does not invalidate array enumerator!
812 s1.SetValue ("change", 1);
813 Assert.IsTrue (en.MoveNext (), "#G13");
814 Assert.AreEqual ("change", en.Current, "#G14");
818 public void TestGetEnumeratorMultipleDimension() {
819 String[,] s1 = {{"this", "is"}, {"a", "test"}};
820 IEnumerator en = s1.GetEnumerator ();
821 Assert.IsNotNull (en, "#AA01");
823 Assert.IsTrue (en.MoveNext (), "#AA02");
824 Assert.AreEqual ("this", en.Current, "#AA03");
825 Assert.IsTrue (en.MoveNext (), "#AA04");
826 Assert.AreEqual ("is", en.Current, "#AA05");
827 Assert.IsTrue (en.MoveNext (), "#AA06");
828 Assert.AreEqual ("a", en.Current, "#AA07");
829 Assert.IsTrue (en.MoveNext (), "#AA08");
830 Assert.AreEqual ("test", en.Current, "#AA09");
831 Assert.IsTrue (!en.MoveNext (), "#AA10");
834 Assert.IsTrue (en.MoveNext (), "#AA11");
835 Assert.AreEqual ("this", en.Current, "#AA12");
838 // mutation does not invalidate array enumerator!
839 s1.SetValue ("change", idxs);
840 Assert.IsTrue (en.MoveNext (), "#AA13");
841 Assert.AreEqual ("change", en.Current, "#AA14");
845 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
846 public void TestGetEnumeratorNonZeroLowerBounds() {
847 int[] myLengthsArray = new int[2] { 3, 5 };
848 int[] myBoundsArray = new int[2] { 2, 3 };
850 Array myArray=Array.CreateInstance( typeof(String), myLengthsArray, myBoundsArray );
851 for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
852 for ( int j = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++ ) {
853 int[] myIndicesArray = new int[2] { i, j };
854 myArray.SetValue( Convert.ToString(i) + j, myIndicesArray );
856 IEnumerator en = myArray.GetEnumerator ();
857 Assert.IsNotNull (en, "#AB01");
859 // check the first couple of values
860 Assert.IsTrue (en.MoveNext (), "#AB02");
861 Assert.AreEqual ("23", en.Current, "#AB03");
862 Assert.IsTrue (en.MoveNext (), "#AB04");
863 Assert.AreEqual ("24", en.Current, "#AB05");
865 // then check the last element's value
868 lastElement = (string)en.Current;
869 } while (en.MoveNext());
870 Assert.AreEqual ("47", lastElement, "#AB06");
874 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
875 public void TestIList_Add () {
876 int[] myLengthsArray = new int[2] { 3, 5 };
877 int[] myBoundsArray = new int[2] { 2, 3 };
879 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
881 ((IList)myArray).Add ("can not");
882 Assert.Fail ("IList.Add should throw");
884 catch (NotSupportedException) {
888 Assert.Fail ("IList.Add threw wrong exception type");
891 Assert.Fail ("IList.Add shouldn't get this far");
895 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
896 public void TestIList_Insert () {
897 int[] myLengthsArray = new int[2] { 3, 5 };
898 int[] myBoundsArray = new int[2] { 2, 3 };
900 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
902 ((IList)myArray).Insert (0, "can not");
903 Assert.Fail ("IList.Insert should throw");
905 catch (NotSupportedException) {
909 Assert.Fail ("IList.Insert threw wrong exception type");
912 Assert.Fail ("IList.Insert shouldn't get this far");
916 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
917 public void TestIList_Remove () {
918 int[] myLengthsArray = new int[2] { 3, 5 };
919 int[] myBoundsArray = new int[2] { 2, 3 };
921 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
923 ((IList)myArray).Remove ("can not");
924 Assert.Fail ("IList.Remove should throw");
926 catch (NotSupportedException) {
930 Assert.Fail ("IList.Remove threw wrong exception type");
933 Assert.Fail ("IList.Remove shouldn't get this far");
937 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
938 public void TestIList_RemoveAt () {
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 );
944 ((IList)myArray).RemoveAt (0);
945 Assert.Fail ("IList.RemoveAt should throw");
947 catch (NotSupportedException) {
951 Assert.Fail ("IList.RemoveAt threw wrong exception type");
954 Assert.Fail ("IList.RemoveAt shouldn't get this far");
958 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
959 public void TestIList_Contains () {
960 int[] myLengthsArray = new int[2] { 3, 5 };
961 int[] myBoundsArray = new int[2] { 2, 3 };
963 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
966 bool b = ((IList)myArray).Contains ("23");
967 Assert.Fail ("IList.Contains should throw with multi-dimensional arrays");
969 catch (RankException) {
970 int[] iArr = new int[3] { 1, 2, 3};
971 // check the first and last items
972 Assert.IsTrue (((IList)iArr).Contains (1), "AC01");
973 Assert.IsTrue (((IList)iArr).Contains (3), "AC02");
975 // and one that is definately not there
976 Assert.IsTrue (!((IList)iArr).Contains (42), "AC03");
980 Assert.Fail ("Should not get here");
984 [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
985 public void TestIList_IndexOf () {
986 int[] myLengthsArray = new int[2] { 3, 5 };
987 int[] myBoundsArray = new int[2] { 2, 3 };
989 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
992 bool b = ((IList)myArray).Contains ("23");
993 Assert.Fail ("IList.Contains should throw with multi-dimensional arrays");
995 catch (RankException) {
996 int[] iArr = new int[3] { 1, 2, 3};
997 // check the first and last items
998 Assert.AreEqual (0, ((IList)iArr).IndexOf (1), "AD01");
999 Assert.AreEqual (2, ((IList)iArr).IndexOf (3), "AD02");
1001 // and one that is definately not there
1002 Assert.AreEqual (-1, ((IList)iArr).IndexOf (42), "AD03");
1004 catch (Exception e) {
1005 Assert.Fail ("Unexpected exception: " + e.ToString());
1008 // check that wierd case whem lowerbound is Int32.MinValue,
1009 // so that IndexOf() needs to return Int32.MaxValue when it cannot find the object
1010 int[] myLengthArray = new int[1] { 3 };
1011 int[] myBoundArray = new int[1] { Int32.MinValue };
1012 Array myExtremeArray=Array.CreateInstance ( typeof(String), myLengthArray, myBoundArray );
1013 Assert.AreEqual (Int32.MaxValue, ((IList)myExtremeArray).IndexOf (42), "AD04");
1018 public void TestGetLength() {
1020 bool errorThrown = false;
1022 char[] c1 = {'a', 'b', 'c'};
1024 } catch (IndexOutOfRangeException) {
1027 Assert.IsTrue (errorThrown, "#H01");
1030 bool errorThrown = false;
1032 char[] c1 = {'a', 'b', 'c'};
1034 } catch (IndexOutOfRangeException) {
1037 Assert.IsTrue (errorThrown, "#H02");
1040 char[] c2 = new Char[5];
1041 Assert.AreEqual (5, c2.GetLength(0), "#H03");
1043 char[,] c3 = new Char[6,7];
1044 Assert.AreEqual (6, c3.GetLength(0), "#H04");
1045 Assert.AreEqual (7, c3.GetLength(1), "#H05");
1049 public void TestGetLowerBound() {
1051 bool errorThrown = false;
1053 char[] c = {'a', 'b', 'c'};
1054 c.GetLowerBound(-1);
1055 } catch (IndexOutOfRangeException) {
1058 Assert.IsTrue (errorThrown, "#H31");
1061 bool errorThrown = false;
1063 char[] c = {'a', 'b', 'c'};
1065 } catch (IndexOutOfRangeException) {
1068 Assert.IsTrue (errorThrown, "#H32");
1071 char[] c1 = new Char[5];
1072 Assert.AreEqual (0, c1.GetLowerBound(0), "#H33");
1074 char[,] c2 = new Char[4,4];
1075 Assert.AreEqual (0, c2.GetLowerBound(0), "#H34");
1076 Assert.AreEqual (0, c2.GetLowerBound(1), "#H35");
1080 public void TestGetUpperBound() {
1082 bool errorThrown = false;
1084 char[] c = {'a', 'b', 'c'};
1085 c.GetUpperBound(-1);
1086 } catch (IndexOutOfRangeException) {
1089 Assert.IsTrue (errorThrown, "#H61");
1092 bool errorThrown = false;
1094 char[] c = {'a', 'b', 'c'};
1096 } catch (IndexOutOfRangeException) {
1099 Assert.IsTrue (errorThrown, "#H62");
1102 char[] c1 = new Char[5];
1103 Assert.AreEqual (4, c1.GetUpperBound(0), "#H63");
1105 char[,] c2 = new Char[4,6];
1106 Assert.AreEqual (3, c2.GetUpperBound(0), "#H64");
1107 Assert.AreEqual (5, c2.GetUpperBound(1), "#H65");
1111 public void TestGetValue1() {
1113 bool errorThrown = false;
1115 char[,] c = new Char[2,2];
1117 } catch (ArgumentException) {
1120 Assert.IsTrue (errorThrown, "#I01");
1123 bool errorThrown = false;
1125 char[] c = {'a', 'b', 'c'};
1127 } catch (IndexOutOfRangeException) {
1130 Assert.IsTrue (errorThrown, "#I02");
1133 bool errorThrown = false;
1135 char[] c = {'a', 'b', 'c'};
1137 } catch (IndexOutOfRangeException) {
1140 Assert.IsTrue (errorThrown, "#I03");
1143 char[] c1 = {'a', 'b', 'c', 'd'};
1144 for (int i = 0; i < c1.Length; i++) {
1145 Assert.AreEqual (c1[i], c1.GetValue(i), "#I04(" + i + ")");
1150 public void TestGetValue2() {
1152 bool errorThrown = false;
1154 char[] c = new Char[2];
1156 } catch (ArgumentException) {
1159 Assert.IsTrue (errorThrown, "#I21");
1162 bool errorThrown = false;
1164 char[,] c = new Char[2,2];
1166 } catch (IndexOutOfRangeException) {
1169 Assert.IsTrue (errorThrown, "#I22");
1172 bool errorThrown = false;
1174 char[,] c = new Char[2,2];
1176 } catch (IndexOutOfRangeException) {
1179 Assert.IsTrue (errorThrown, "#I23");
1182 char[,] c1 = new Char[4,6];
1183 for (int i = 0; i < 24; i++) {
1186 c1[first,second] = (char)(((int)'a')+i);
1188 for (int i = 0; i < c1.GetLength(0); i++) {
1189 for (int j = 0; j < c1.GetLength(1); j++) {
1190 Assert.AreEqual (c1[i, j], c1.GetValue(i, j), "#I24(" + i + "," + j + ")");
1196 public void TestGetValue3() {
1198 bool errorThrown = false;
1200 char[] c = new Char[2];
1202 } catch (ArgumentException) {
1205 Assert.IsTrue (errorThrown, "#I41");
1208 bool errorThrown = false;
1210 char[,,] c = new Char[2,2,2];
1211 c.GetValue(-1, 1, 1);
1212 } catch (IndexOutOfRangeException) {
1215 Assert.IsTrue (errorThrown, "#I42");
1218 bool errorThrown = false;
1220 char[,,] c = new Char[2,2,2];
1222 } catch (IndexOutOfRangeException) {
1225 Assert.IsTrue (errorThrown, "#I43");
1228 char[,,] c1 = new Char[4,2,3];
1229 for (int i = 0; i < 24; i++) {
1231 int remains = i % 6;
1232 int second = remains / 3;
1233 int third = remains % 3;
1234 c1[first,second, third] = (char)(((int)'a')+i);
1236 for (int i = 0; i < c1.GetLength(0); i++) {
1237 for (int j = 0; j < c1.GetLength(1); j++) {
1238 for (int k = 0; k < c1.GetLength(2); k++) {
1239 Assert.AreEqual (c1[i, j, k], c1.GetValue(i, j, k), "#I44(" + i + "," + j + ")");
1246 [ExpectedException (typeof (ArgumentNullException))]
1247 public void TestGetValueLongArray ()
1249 char[] c = new Char[2];
1250 c.GetValue((long [])null);
1254 public void TestGetValueN() {
1256 bool errorThrown = false;
1258 char[] c = new Char[2];
1259 c.GetValue((int [])null);
1260 } catch (ArgumentNullException) {
1263 Assert.IsTrue (errorThrown, "#I61a");
1266 bool errorThrown = false;
1268 char[] c = new Char[2];
1269 int[] coords = {1, 1};
1271 } catch (ArgumentException) {
1274 Assert.IsTrue (errorThrown, "#I62");
1277 bool errorThrown = false;
1279 char[,] c = new Char[2,2];
1280 int[] coords = {-1, 1};
1282 } catch (IndexOutOfRangeException) {
1285 Assert.IsTrue (errorThrown, "#I63");
1288 bool errorThrown = false;
1290 char[,] c = new Char[2,2];
1291 int[] coords = {4, 1};
1293 } catch (IndexOutOfRangeException) {
1296 Assert.IsTrue (errorThrown, "#I64");
1299 char[,] c1 = new Char[4,6];
1300 for (int i = 0; i < 24; i++) {
1303 c1[first,second] = (char)(((int)'a')+i);
1305 for (int i = 0; i < c1.GetLength(0); i++) {
1306 for (int j = 0; j < c1.GetLength(1); j++) {
1307 int[] coords = {i, j};
1308 Assert.AreEqual (c1[i, j], c1.GetValue(coords), "#I65(" + i + "," + j + ")");
1314 public void TestIndexOf1() {
1316 bool errorThrown = false;
1318 Array.IndexOf(null, "huh?");
1319 } catch (ArgumentNullException) {
1322 Assert.IsTrue (errorThrown, "#J01");
1325 bool errorThrown = false;
1327 char[,] c = new Char[2,2];
1328 Array.IndexOf(c, "huh?");
1329 } catch (RankException) {
1332 Assert.IsTrue (errorThrown, "#J02");
1335 String[] s1 = {"this", "is", "a", "test"};
1336 Assert.AreEqual (-1, Array.IndexOf(s1, null), "#J03");
1337 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing"), "#J04");
1338 Assert.AreEqual (0, Array.IndexOf(s1, "this"), "#J05");
1339 Assert.AreEqual (3, Array.IndexOf(s1, "test"), "#J06");
1343 public void TestIndexOf2() {
1345 bool errorThrown = false;
1347 Array.IndexOf(null, "huh?", 0);
1348 } catch (ArgumentNullException) {
1351 Assert.IsTrue (errorThrown, "#J21");
1354 bool errorThrown = false;
1356 char[,] c = new Char[2,2];
1357 Array.IndexOf(c, "huh?", 0);
1358 } catch (RankException) {
1361 Assert.IsTrue (errorThrown, "#J22");
1364 bool errorThrown = false;
1366 char[] c = new Char[2];
1367 Array.IndexOf(c, "huh?", 3);
1368 } catch (ArgumentOutOfRangeException) {
1371 Assert.IsTrue (errorThrown, "#J23");
1374 String[] s1 = {"this", "is", "really", "a", "test"};
1375 Assert.AreEqual (-1, Array.IndexOf(s1, null, 1), "#J24");
1376 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing", 1), "#J25");
1377 Assert.AreEqual (-1, Array.IndexOf(s1, "this", 1), "#J26");
1378 Assert.AreEqual (1, Array.IndexOf(s1, "is", 1), "#J27");
1379 Assert.AreEqual (4, Array.IndexOf(s1, "test", 1), "#J28");
1383 public void TestIndexOf3() {
1385 bool errorThrown = false;
1387 Array.IndexOf(null, "huh?", 0, 1);
1388 } catch (ArgumentNullException) {
1391 Assert.IsTrue (errorThrown, "#J41");
1394 bool errorThrown = false;
1396 char[,] c = new Char[2,2];
1397 Array.IndexOf(c, "huh?", 0, 1);
1398 } catch (RankException) {
1401 Assert.IsTrue (errorThrown, "#J42");
1404 bool errorThrown = false;
1406 char[] c = new Char[2];
1407 Array.IndexOf(c, "huh?", 3, 1);
1408 } catch (ArgumentOutOfRangeException) {
1411 Assert.IsTrue (errorThrown, "#J43");
1414 bool errorThrown = false;
1416 char[] c = new Char[2];
1417 Array.IndexOf(c, "huh?", 0, 5);
1418 } catch (ArgumentOutOfRangeException) {
1421 Assert.IsTrue (errorThrown, "#J44");
1424 String[] s1 = {"this", "is", "really", "a", "test"};
1425 Assert.AreEqual (-1, Array.IndexOf(s1, null, 1, 3), "#J45");
1426 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing", 1, 3), "#J46");
1427 Assert.AreEqual (-1, Array.IndexOf(s1, "this", 1, 3), "#J47");
1428 Assert.AreEqual (1, Array.IndexOf(s1, "is", 1, 3), "#J48");
1429 Assert.AreEqual (-1, Array.IndexOf(s1, "test", 1, 3), "#J49");
1430 Assert.AreEqual (3, Array.IndexOf(s1, "a", 1, 3), "#J50");
1434 public void TestIndexOf_CustomEqual ()
1436 DataEqual[] test = new DataEqual [] { new DataEqual () };
1437 Assert.AreEqual (0, Array.IndexOf (test, "asdfas", 0));
1439 IList array = (IList)test;
1440 Assert.AreEqual (0, array.IndexOf ("asdfas"));
1444 public void TestLastIndexOf1() {
1446 bool errorThrown = false;
1448 Array.LastIndexOf(null, "huh?");
1449 } catch (ArgumentNullException) {
1452 Assert.IsTrue (errorThrown, "#K01");
1455 bool errorThrown = false;
1457 char[,] c = new Char[2,2];
1458 Array.LastIndexOf(c, "huh?");
1459 } catch (RankException) {
1462 Assert.IsTrue (errorThrown, "#K02");
1465 String[] s1 = {"this", "is", "a", "a", "test"};
1466 Assert.AreEqual (-1, Array.LastIndexOf(s1, null), "#K03");
1467 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing"), "#K04");
1468 Assert.AreEqual (0, Array.LastIndexOf(s1, "this"), "#K05");
1469 Assert.AreEqual (4, Array.LastIndexOf(s1, "test"), "#K06");
1470 Assert.AreEqual (3, Array.LastIndexOf(s1, "a"), "#K07");
1472 Assert.AreEqual (-1, Array.LastIndexOf (new String [0], "foo"));
1476 public void TestLastIndexOf2() {
1478 bool errorThrown = false;
1480 Array.LastIndexOf(null, "huh?", 0);
1481 } catch (ArgumentNullException) {
1484 Assert.IsTrue (errorThrown, "#K21");
1487 bool errorThrown = false;
1489 char[,] c = new Char[2,2];
1490 Array.LastIndexOf(c, "huh?", 0);
1491 } catch (RankException) {
1494 Assert.IsTrue (errorThrown, "#K22");
1497 bool errorThrown = false;
1499 char[] c = new Char[2];
1500 Array.LastIndexOf(c, "huh?", 3);
1501 } catch (ArgumentOutOfRangeException) {
1504 Assert.IsTrue (errorThrown, "#K23");
1507 String[] s1 = {"this", "is", "really", "a", "test"};
1508 Assert.AreEqual (-1, Array.LastIndexOf(s1, null, 3), "#K24");
1509 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing", 3), "#K25");
1510 Assert.AreEqual (-1, Array.LastIndexOf(s1, "test", 3), "#K26");
1511 Assert.AreEqual (3, Array.LastIndexOf(s1, "a", 3), "#K27");
1512 Assert.AreEqual (0, Array.LastIndexOf(s1, "this", 3), "#K28");
1516 public void TestLastIndexOf3() {
1518 bool errorThrown = false;
1520 Array.LastIndexOf(null, "huh?", 0, 1);
1521 } catch (ArgumentNullException) {
1524 Assert.IsTrue (errorThrown, "#K41");
1527 bool errorThrown = false;
1529 char[,] c = new Char[2,2];
1530 Array.LastIndexOf(c, "huh?", 0, 1);
1531 } catch (RankException) {
1534 Assert.IsTrue (errorThrown, "#K42");
1537 bool errorThrown = false;
1539 char[] c = new Char[2];
1540 Array.LastIndexOf(c, "huh?", 3, 1);
1541 } catch (ArgumentOutOfRangeException) {
1544 Assert.IsTrue (errorThrown, "#K43");
1547 bool errorThrown = false;
1549 char[] c = new Char[2];
1550 Array.LastIndexOf(c, "huh?", 0, 5);
1551 } catch (ArgumentOutOfRangeException) {
1554 Assert.IsTrue (errorThrown, "#K44");
1557 String[] s1 = {"this", "is", "really", "a", "test"};
1558 Assert.AreEqual (-1, Array.LastIndexOf(s1, null, 3, 3), "#K45");
1559 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing", 3, 3), "#K46");
1560 Assert.AreEqual (-1, Array.LastIndexOf(s1, "this", 3, 3), "#K47");
1561 Assert.AreEqual (1, Array.LastIndexOf(s1, "is", 3, 3), "#K48");
1562 Assert.AreEqual (-1, Array.LastIndexOf(s1, "test", 3, 3), "#K49");
1563 Assert.AreEqual (3, Array.LastIndexOf(s1, "a", 3, 3), "#K50");
1567 public void TestLastIndexOf4 ()
1569 short [] a = new short [] { 19, 238, 317, 6, 565, 0, -52, 60, -563, 753, 238, 238};
1571 Array.LastIndexOf (a, (object)16, -1);
1572 NUnit.Framework.Assert.Fail ("#1");
1573 } catch (ArgumentOutOfRangeException) { }
1576 Array.LastIndexOf<short> (a, 16, -1);
1577 NUnit.Framework.Assert.Fail ("#2");
1578 } catch (ArgumentOutOfRangeException) { }
1582 public void TestLastIndexOf5 ()
1584 char [] a = new char [] {'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'j', 'i', 'h'};
1589 for (int i = a.Length - 1; i >= 0 ; i--) {
1591 retval = Array.LastIndexOf(a, a [i], i, i + 1);
1595 Assert.IsTrue (!error);
1599 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1600 public void LastIndexOf_StartIndexOverflow ()
1602 // legal - no exception
1603 byte[] array = new byte [16];
1604 Array.LastIndexOf (array, this, Int32.MaxValue, 1);
1608 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1609 public void LastIndexOf_CountOverflow ()
1611 // legal - no exception
1612 byte[] array = new byte [16];
1613 Array.LastIndexOf (array, this, 1, Int32.MaxValue);
1617 public void LastIndexOf_0LengthArray ()
1619 Array array = Array.CreateInstance (typeof (char), 0);
1620 int idx = Array.LastIndexOf (array, (object) null, -1, 0);
1621 Assert.IsTrue (idx == -1, "#01");
1622 idx = Array.LastIndexOf (array, (object) null, -1, 10);
1623 Assert.IsTrue (idx == -1, "#02");
1624 idx = Array.LastIndexOf (array, (object) null, -100, 10);
1625 Assert.IsTrue (idx == -1, "#02");
1627 array = Array.CreateInstance (typeof (char), 1);
1629 Array.LastIndexOf (array, (object) null, -1, 0);
1630 Assert.Fail ("#04");
1631 } catch (ArgumentOutOfRangeException e) {
1634 Array.LastIndexOf (array, (object) null, -1, 10);
1635 Assert.Fail ("#05");
1636 } catch (ArgumentOutOfRangeException e) {
1639 Array.LastIndexOf (array, (object) null, -100, 10);
1640 Assert.Fail ("#06");
1641 } catch (ArgumentOutOfRangeException e) {
1647 public void FindIndexTest ()
1649 var a = new int[] { 2, 2, 2, 3, 2 };
1650 Assert.AreEqual (2, Array.FindIndex (a, 2, 2, l => true));
1654 public void FindIndex_Invalid ()
1656 var array = new int [] { 1, 2, 3, 4, 5 };
1659 Array.FindIndex (array, null);
1661 } catch (ArgumentNullException) {
1665 Array.FindIndex (array, -1, l => true);
1667 } catch (ArgumentOutOfRangeException) {
1671 Array.FindIndex (array, -1, 0, l => true);
1672 Assert.Fail ("#2b");
1673 } catch (ArgumentOutOfRangeException) {
1677 Array.FindIndex (array, 0, -1, l => true);
1679 } catch (ArgumentOutOfRangeException) {
1683 Array.FindIndex (array, 100, l => true);
1685 } catch (ArgumentOutOfRangeException) {
1689 Array.FindIndex (array, 100, 0, l => true);
1690 Assert.Fail ("#4b");
1691 } catch (ArgumentOutOfRangeException) {
1695 Array.FindIndex (array, 7, 2, l => true);
1697 } catch (ArgumentOutOfRangeException) {
1701 [Test, ExpectedException (typeof (ArgumentNullException))]
1702 public void FindLastNullTest ()
1704 var array = new int [] { 1, 2, 3, 4, 5 };
1705 Array.FindLast (array, null);
1709 public void FindLastIndexTest ()
1711 var array = new int [] { 1, 2, 3, 4, 5 };
1713 Assert.AreEqual (2, Array.FindLastIndex (array, 2, 3, l => true));
1714 Assert.AreEqual (2, Array.FindLastIndex (array, 2, 2, l => true));
1715 Assert.AreEqual (1, Array.FindLastIndex (array, 1, 2, l => true));
1719 public void FindLastIndex_Invalid ()
1721 var array = new int [] { 1, 2, 3, 4, 5 };
1723 Array.FindLastIndex (array, null);
1725 } catch (ArgumentNullException) {
1729 Array.FindLastIndex (array, -1, l => true);
1731 } catch (ArgumentOutOfRangeException) {
1735 Array.FindLastIndex (array, -1, 0, l => true);
1736 Assert.Fail ("#2b");
1737 } catch (ArgumentOutOfRangeException) {
1741 Array.FindLastIndex (array, 0, -1, l => true);
1743 } catch (ArgumentOutOfRangeException) {
1747 Array.FindLastIndex (array, 100, l => true);
1749 } catch (ArgumentOutOfRangeException) {
1753 Array.FindLastIndex (array, 100, 0, l => true);
1754 Assert.Fail ("#4b");
1755 } catch (ArgumentOutOfRangeException) {
1759 Array.FindLastIndex (array, 2, 4, l => true);
1761 } catch (ArgumentOutOfRangeException) {
1766 public void TestReverse() {
1768 bool errorThrown = false;
1770 Array.Reverse(null);
1771 } catch (ArgumentNullException) {
1774 Assert.IsTrue (errorThrown, "#L01");
1777 bool errorThrown = false;
1779 char[,] c = new Char[2,2];
1781 } catch (RankException) {
1784 Assert.IsTrue (errorThrown, "#L02");
1787 char[] c1 = {'a', 'b', 'c', 'd'};
1789 Assert.AreEqual ('d', c1[0], "#L03");
1790 Assert.AreEqual ('c', c1[1], "#L04");
1791 Assert.AreEqual ('b', c1[2], "#L05");
1792 Assert.AreEqual ('a', c1[3], "#L06");
1795 bool errorThrown = false;
1797 Array.Reverse(null, 0, 0);
1798 } catch (ArgumentNullException) {
1801 Assert.IsTrue (errorThrown, "#L07");
1804 bool errorThrown = false;
1806 char[,] c = new Char[2,2];
1807 Array.Reverse(c, 0, 0);
1808 } catch (RankException) {
1811 Assert.IsTrue (errorThrown, "#L08");
1814 //bool errorThrown = false;
1816 // char[] c = new Char[2];
1817 // Array.Reverse(c, 0, 3);
1818 //} catch (ArgumentOutOfRangeException) {
1819 // errorThrown = true;
1821 //Assert.IsTrue (errorThrown, "#L09");
1824 //bool errorThrown = false;
1826 // char[] c = new Char[2];
1827 // Array.Reverse(c, 3, 0);
1828 //} catch (ArgumentOutOfRangeException) {
1829 // errorThrown = true;
1831 //Assert.IsTrue (errorThrown, "#L10");
1834 char[] c2 = { 'a', 'b', 'c', 'd'};
1835 Array.Reverse(c2, 1, 2);
1836 Assert.AreEqual ('a', c2[0], "#L11");
1837 Assert.AreEqual ('c', c2[1], "#L12");
1838 Assert.AreEqual ('b', c2[2], "#L13");
1839 Assert.AreEqual ('d', c2[3], "#L14");
1844 public void ReverseStruct () {
1845 BStruct[] c3 = new BStruct[2];
1846 c3 [0] = new BStruct () { i1 = 1, i2 = 2, i3 = 3 };
1847 c3 [1] = new BStruct () { i1 = 4, i2 = 5, i3 = 6 };
1849 Assert.AreEqual (4, c3 [0].i1);
1850 Assert.AreEqual (5, c3 [0].i2);
1851 Assert.AreEqual (6, c3 [0].i3);
1852 Assert.AreEqual (1, c3 [1].i1);
1853 Assert.AreEqual (2, c3 [1].i2);
1854 Assert.AreEqual (3, c3 [1].i3);
1858 public int i1, i2, i3;
1862 public void TestSetValue1() {
1864 bool errorThrown = false;
1866 char[,] c = new Char[2,2];
1867 c.SetValue("buh", 1);
1868 } catch (ArgumentException) {
1871 Assert.IsTrue (errorThrown, "#M01");
1874 bool errorThrown = false;
1876 char[] c = {'a', 'b', 'c'};
1877 c.SetValue("buh", -1);
1878 } catch (IndexOutOfRangeException) {
1881 Assert.IsTrue (errorThrown, "#M02");
1884 bool errorThrown = false;
1886 char[] c = {'a', 'b', 'c'};
1887 c.SetValue("buh", 4);
1888 } catch (IndexOutOfRangeException) {
1891 Assert.IsTrue (errorThrown, "#M03");
1894 char[] c1 = {'a', 'b', 'c', 'd'};
1895 char[] c2 = new char[4];
1896 for (int i = 0; i < c1.Length; i++) {
1897 c2.SetValue(c1[i], i);
1899 for (int i = 0; i < c1.Length; i++) {
1900 Assert.AreEqual (c1[i], c2[i], "#M04(" + i + ")");
1903 int[] c3 = { 1, 2, 3 };
1904 long[] c4 = new long [3];
1906 for (int i = 0; i < c3.Length; i++)
1907 c4.SetValue (c3 [i], i);
1911 } catch (Exception e) {
1912 Assert.Fail ("c3.CopyTo(): e=" + e);
1914 for (int i = 0; i < c3.Length; i++)
1915 Assert.IsTrue (c3[i] == c4[i], "#M05(" + i + ")");
1917 Object[] c5 = new Object [3];
1918 long[] c6 = new long [3];
1922 } catch (Exception e) {
1923 Assert.Fail ("c4.CopyTo(): e=" + e);
1928 } catch (Exception e) {
1929 Assert.Fail ("c5.CopyTo(): e=" + e);
1931 // for (int i = 0; i < c5.Length; i++)
1932 // Assert.IsTrue (c5[i] == c6[i], "#M06(" + i + ")");
1936 public void TestSetValue2() {
1938 bool errorThrown = false;
1940 char[] c = new Char[2];
1941 c.SetValue("buh", 1,1);
1942 } catch (ArgumentException) {
1945 Assert.IsTrue (errorThrown, "#M21");
1948 bool errorThrown = false;
1950 char[,] c = new Char[2,2];
1951 c.SetValue("buh", -1, 1);
1952 } catch (IndexOutOfRangeException) {
1955 Assert.IsTrue (errorThrown, "#M22");
1958 bool errorThrown = false;
1960 char[,] c = new Char[2,2];
1961 c.SetValue("buh", 4,1);
1962 } catch (IndexOutOfRangeException) {
1965 Assert.IsTrue (errorThrown, "#M23");
1968 char[,] c1 = new Char[4,6];
1969 char[,] c2 = new Char[4,6];
1970 for (int i = 0; i < 24; i++) {
1973 c1[first,second] = (char)(((int)'a')+i);
1974 c2.SetValue(c1[first,second], first, second);
1976 for (int i = 0; i < c1.GetLength(0); i++) {
1977 for (int j = 0; j < c1.GetLength(1); j++) {
1978 Assert.AreEqual (c1[i, j], c2[i, j], "#M24(" + i + "," + j + ")");
1984 public void TestSetValue3() {
1986 bool errorThrown = false;
1988 char[] c = new Char[2];
1989 c.SetValue("buh", 1,1,1);
1990 } catch (ArgumentException) {
1993 Assert.IsTrue (errorThrown, "#M41");
1996 bool errorThrown = false;
1998 char[,,] c = new Char[2,2,2];
1999 c.SetValue("buh", -1, 1, 1);
2000 } catch (IndexOutOfRangeException) {
2003 Assert.IsTrue (errorThrown, "#M42");
2006 bool errorThrown = false;
2008 char[,,] c = new Char[2,2,2];
2009 c.SetValue("buh", 4,1,1);
2010 } catch (IndexOutOfRangeException) {
2013 Assert.IsTrue (errorThrown, "#M43");
2016 char[,,] c1 = new Char[4,2,3];
2017 char[,,] c2 = new Char[4,2,3];
2018 for (int i = 0; i < 24; i++) {
2020 int remains = i % 6;
2021 int second = remains / 3;
2022 int third = remains % 3;
2023 c1[first,second, third] = (char)(((int)'a')+i);
2024 c2.SetValue(c1[first, second, third], first, second, third);
2026 for (int i = 0; i < c1.GetLength(0); i++) {
2027 for (int j = 0; j < c1.GetLength(1); j++) {
2028 for (int k = 0; k < c1.GetLength(2); k++) {
2029 Assert.AreEqual (c1[i, j, k], c2[i, j, k], "#M44(" + i + "," + j + " )");
2036 [ExpectedException (typeof (ArgumentNullException))]
2037 public void TestSetValueLongArray ()
2039 char[] c = new Char[2];
2040 c.SetValue("buh", (long [])null);
2044 public void TestSetValueN() {
2046 bool errorThrown = false;
2048 char[] c = new Char[2];
2049 c.SetValue("buh", (int [])null);
2050 } catch (ArgumentNullException) {
2053 Assert.IsTrue (errorThrown, "#M61a");
2056 bool errorThrown = false;
2058 char[] c = new Char[2];
2059 int[] coords = {1, 1};
2060 c.SetValue("buh", coords);
2061 } catch (ArgumentException) {
2064 Assert.IsTrue (errorThrown, "#M62");
2067 bool errorThrown = false;
2069 char[,] c = new Char[2,2];
2070 int[] coords = {-1, 1};
2071 c.SetValue("buh", coords);
2072 } catch (IndexOutOfRangeException) {
2075 Assert.IsTrue (errorThrown, "#M63");
2078 bool errorThrown = false;
2080 char[,] c = new Char[2,2];
2081 int[] coords = {4, 1};
2082 c.SetValue("buh", coords);
2083 } catch (IndexOutOfRangeException) {
2086 Assert.IsTrue (errorThrown, "#M64");
2089 char[,] c1 = new Char[4,6];
2090 char[,] c2 = new Char[4,6];
2091 for (int i = 0; i < 24; i++) {
2094 c1[first,second] = (char)(((int)'a')+i);
2095 int[] coords = {first, second};
2096 c2.SetValue(c1[first,second], coords);
2098 for (int i = 0; i < c1.GetLength(0); i++) {
2099 for (int j = 0; j < c1.GetLength(1); j++) {
2100 Assert.AreEqual (c1[i, j], c2[i, j], "#M65(" + i + "," + j + ")");
2106 public void TestSetValue4() {
2108 int[] c1 = { 1, 2, 3 };
2109 long[] c2 = new long [3];
2111 for (int i = 0; i < c1.Length; i++)
2112 c2.SetValue (c1 [i], i);
2114 for (int i = 0; i < c1.Length; i++) {
2115 Assert.IsTrue (c1[i] == c2[i], "#M81(" + i + ")");
2116 Assert.AreEqual (typeof (long), c2[i].GetType (), "#M82(" + i + ")");
2120 long[] c1 = { 1, 2, 3 };
2121 int[] c2 = new int [3];
2122 bool errorThrown = false;
2124 c2.SetValue (c1 [0], 0);
2125 } catch (ArgumentException) {
2128 Assert.IsTrue (errorThrown, "#M83");
2131 int[] c1 = { 1, 2, 3 };
2132 Object[] c2 = new Object [3];
2134 for (int i = 0; i < c1.Length; i++)
2135 c2.SetValue (c1 [i], i);
2137 for (int i = 0; i < c1.Length; i++)
2138 Assert.AreEqual (c1[i], Convert.ToInt32 (c2[i]), "#M84(" + i + ")");
2141 Object[] c1 = new Object [3];
2142 Object[] c2 = new Object [3];
2143 c1[0] = new Object ();
2145 for (int i = 0; i < c1.Length; i++)
2146 c2.SetValue (c1 [i], i);
2148 for (int i = 0; i < c1.Length; i++)
2149 Assert.AreEqual (c1[i], c2[i], "#M85(" + i + ")");
2152 Object[] c1 = new Object [3];
2153 string[] c2 = new String [3];
2154 string test = "hello";
2157 c2.SetValue (c1 [0], 0);
2158 Assert.AreEqual (c1[0], c2[0], "#M86");
2159 Assert.AreEqual ("hello", c2[0], "#M87");
2162 char[] c1 = { 'a', 'b', 'c' };
2163 string[] c2 = new string [3];
2165 c2.SetValue (c1 [0], 0);
2166 Assert.Fail ("#M88");
2167 } catch (InvalidCastException) {}
2170 Single[] c1 = { 1.2F, 2.3F, 3.4F, 4.5F };
2171 long[] c2 = new long [3];
2173 c2.SetValue (c1 [0], 0);
2174 Assert.Fail ("#M89");
2175 } catch (ArgumentException) {}
2203 UInt32 v11 = 235354;
2204 UInt64 v12 = 234552;
2206 Object[] va1 = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12 };
2207 Object[] va2 = { "true", "1", "a", "-1.2", "-32", "-234", "-34523", "-1",
2208 "-4.8F", "24234", "235354", "234552" };
2210 Object[][] vt = { va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1 };
2213 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2214 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
2215 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
2216 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
2217 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1,
2218 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1,
2219 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,
2220 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1,
2221 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1,
2222 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
2223 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0,
2224 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0
2229 for (int i = 0; i < types.Length; i++) {
2230 for (int j = 0; j < types.Length; j++) {
2231 Array array = Array.CreateInstance (types [j], 2);
2233 Object value = vt[j][i];
2235 bool errorThrown = false;
2237 array.SetValue (value, 0);
2238 } catch (ArgumentException) {
2242 int ex_index = (i * types.Length) + j;
2244 Assert.AreEqual (errorThrown, arg_ex [ex_index] == 1, "#M90(" + types [i] + "," + types [j] + ")");
2248 for (int i = 0; i < types.Length; i++) {
2249 String[] array = new String [2];
2251 Object value = va1 [i];
2253 bool errorThrown = false;
2255 array.SetValue (value, 0);
2256 } catch (InvalidCastException) {
2260 Assert.IsTrue (errorThrown, "#M91(" + types [i] + ")");
2263 for (int i = 0; i < types.Length; i++) {
2264 Array array = Array.CreateInstance (types [i], 2);
2266 Object value = va2 [i];
2268 bool errorThrown = false;
2270 array.SetValue (value, 0);
2271 } catch (InvalidCastException) {
2275 Assert.IsTrue (errorThrown, "#M92(" + types [i] + ")");
2278 for (int i = 0; i < types.Length; i++) {
2279 Array array = Array.CreateInstance (types [i], 2);
2281 Object value = null;
2283 bool errorThrown = false;
2285 array.SetValue (value, 0);
2286 } catch (InvalidCastException) {
2290 Assert.IsTrue (!errorThrown, "#M93(" + types [i] + ")");
2295 for (int i = 0; i < types.Length; i++) {
2296 for (int j = 0; j < types.Length; j++) {
2297 Array source = Array.CreateInstance (types [i], 2);
2298 Array array = Array.CreateInstance (types [j], 2);
2300 source.SetValue (vt[j][i], 0);
2301 source.SetValue (vt[j][i], 1);
2303 bool errorThrown = false;
2305 Array.Copy (source, array, 2);
2306 } catch (ArrayTypeMismatchException) {
2310 int ex_index = (i * types.Length) + j;
2312 Assert.AreEqual (errorThrown, arg_ex [ex_index] == 1, "#M94(" + types [i] + "," + types [j] + ")");
2316 for (int i = 0; i < types.Length; i++) {
2317 Array source = Array.CreateInstance (types [i], 2);
2318 String[] array = new String [2];
2320 source.SetValue (va1 [i], 0);
2321 source.SetValue (va1 [i], 1);
2323 bool errorThrown = false;
2325 Array.Copy (source, array, 2);
2326 } catch (ArrayTypeMismatchException) {
2330 Assert.IsTrue (errorThrown, "#M95(" + types [i] + ")");
2333 for (int i = 0; i < types.Length; i++) {
2334 String[] source = new String [2];
2335 Array array = Array.CreateInstance (types [i], 2);
2337 source.SetValue (va2 [i], 0);
2338 source.SetValue (va2 [i], 1);
2340 bool errorThrown = false;
2342 Array.Copy (source, array, 2);
2343 } catch (ArrayTypeMismatchException) {
2347 Assert.IsTrue (errorThrown, "#M96(" + types [i] + ")");
2353 public void TestSort() {
2355 bool errorThrown = false;
2358 } catch (ArgumentNullException) {
2361 Assert.IsTrue (errorThrown, "#N01");
2364 bool errorThrown = false;
2366 Array.Sort(null, 0, 1);
2367 } catch (ArgumentNullException) {
2370 Assert.IsTrue (errorThrown, "#N02");
2373 bool errorThrown = false;
2375 char[] c1 = new Char[2];
2376 Array.Sort(null, c1);
2377 } catch (ArgumentNullException) {
2380 Assert.IsTrue (errorThrown, "#N03");
2383 bool errorThrown = false;
2385 char[] c1 = new Char[2];
2386 Array.Sort(null, c1, 0, 1);
2387 } catch (ArgumentNullException) {
2390 Assert.IsTrue (errorThrown, "#N04");
2394 char[] arr = {'d', 'b', 'f', 'e', 'a', 'c'};
2397 Array.Sort (null, 0, 1);
2398 Assert.Fail ("#N" + tc.ToString ());
2400 catch (ArgumentException) {}
2401 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2405 Array.Sort (arr, -1, 3);
2406 Assert.Fail ("#N" + tc.ToString ());
2408 catch (ArgumentException) {}
2409 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2413 Array.Sort (arr, 1, -3);
2414 Assert.Fail ("#N" + tc.ToString ());
2416 catch (ArgumentException) {}
2417 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2421 Array.Sort (arr, arr.Length, arr.Length + 2);
2422 Assert.Fail ("#N" + tc.ToString ());
2424 catch (ArgumentException) {}
2425 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2428 // note: null second array => just sort first array
2429 char[] starter = {'d', 'b', 'f', 'e', 'a', 'c'};
2430 int[] starter1 = {1,2,3,4,5,6};
2432 char[] c1 = (char[])starter.Clone();
2434 Assert.AreEqual ('a', c1[0], "#N21");
2435 Assert.AreEqual ('b', c1[1], "#N22");
2436 Assert.AreEqual ('c', c1[2], "#N23");
2437 Assert.AreEqual ('d', c1[3], "#N24");
2438 Assert.AreEqual ('e', c1[4], "#N25");
2439 Assert.AreEqual ('f', c1[5], "#N26");
2442 char[] c1 = (char[])starter.Clone();
2443 int[] i1 = (int[])starter1.Clone();
2445 Assert.AreEqual ('a', c1[0], "#N41");
2446 Assert.AreEqual ('b', c1[1], "#N42");
2447 Assert.AreEqual ('c', c1[2], "#N43");
2448 Assert.AreEqual ('d', c1[3], "#N44");
2449 Assert.AreEqual ('e', c1[4], "#N45");
2450 Assert.AreEqual ('f', c1[5], "#N46");
2451 Assert.AreEqual (5, i1[0], "#N47");
2452 Assert.AreEqual (2, i1[1], "#N48");
2453 Assert.AreEqual (6, i1[2], "#N49");
2454 Assert.AreEqual (1, i1[3], "#N50");
2455 Assert.AreEqual (4, i1[4], "#N51");
2456 Assert.AreEqual (3, i1[5], "#N52");
2459 char[] c1 = (char[])starter.Clone();
2460 Array.Sort(c1, 1, 4);
2461 Assert.AreEqual ('d', c1[0], "#N61");
2462 Assert.AreEqual ('a', c1[1], "#N62");
2463 Assert.AreEqual ('b', c1[2], "#N63");
2464 Assert.AreEqual ('e', c1[3], "#N64");
2465 Assert.AreEqual ('f', c1[4], "#N65");
2466 Assert.AreEqual ('c', c1[5], "#N66");
2469 char[] c1 = (char[])starter.Clone();
2470 int[] i1 = (int[])starter1.Clone();
2471 Array.Sort(c1, i1, 1, 4);
2472 Assert.AreEqual ('d', c1[0], "#N81");
2473 Assert.AreEqual ('a', c1[1], "#N82");
2474 Assert.AreEqual ('b', c1[2], "#N83");
2475 Assert.AreEqual ('e', c1[3], "#N84");
2476 Assert.AreEqual ('f', c1[4], "#N85");
2477 Assert.AreEqual ('c', c1[5], "#N86");
2478 Assert.AreEqual (1, i1[0], "#N87");
2479 Assert.AreEqual (5, i1[1], "#N88");
2480 Assert.AreEqual (2, i1[2], "#N89");
2481 Assert.AreEqual (4, i1[3], "#N90");
2482 Assert.AreEqual (3, i1[4], "#N91");
2483 Assert.AreEqual (6, i1[5], "#N92");
2488 double[] a = new double[115];
2489 int[] b = new int[256];
2490 Array.Sort<double, int> (a, b, 0, 115);
2493 /* Check that ulong[] is not sorted as long[] */
2495 string[] names = new string[] {
2496 "A", "B", "C", "D", "E"
2499 ulong[] arr = new ulong [] {
2501 unchecked((ulong)0xffffFFFF00000000),
2508 Array.Sort (a, names, null);
2509 Assert.AreEqual (0, a.GetValue (0));
2514 public void SortNonGenericDoubleItems () {
2515 double[] doubleValues = new double[11];
2517 doubleValues[0] = 0.221788066253601;
2518 doubleValues[1] = 0.497278285809481;
2519 doubleValues[2] = 0.100565033883643;
2520 doubleValues[3] = 0.0433309347749905;
2521 doubleValues[4] = 0.00476726438463812;
2522 doubleValues[5] = 0.1354609735456;
2523 doubleValues[6] = 0.57690356588135;
2524 doubleValues[7] = 0.466239434334826;
2525 doubleValues[8] = 0.409741461978934;
2526 doubleValues[9] = 0.0112412763949565;
2527 doubleValues[10] = 0.668704347674307;
2529 int[] indices = new int[11];
2542 Array.Sort ((Array)doubleValues, (Array)indices);
2543 Assert.AreEqual (4, indices [0]);
2547 public void TestInitializeEmpty()
2559 Assert.IsTrue (!catched, "#TI01");
2563 public void TestInitializeInt()
2568 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2570 Assert.AreEqual (a[i], b[i], "#TI02 " + i);
2575 public void TestInitializeDouble()
2577 double[] a = {1.0,2.0,0.0};
2579 double[] b = {1.0,2.0,0.0};
2580 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2582 Assert.AreEqual (a[i], b[i], "#TI03 " + i);
2587 public void TestInitializeFloat()
2589 float[] a = {1.0F,2.0F,0.0F};
2591 float[] b = {1.0F,2.0F,0.0F};
2592 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2594 Assert.AreEqual (a[i], b[i], "#TI04 " + i);
2599 public void TestInitializeChar()
2601 char[] a = {'1','.','0','F','2','.','0','F'};
2603 char[] b = {'1','.','0','F','2','.','0','F'};
2604 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2606 Assert.AreEqual (a[i], b[i], "#TI05 " + i);
2611 public void TestInitializeString()
2613 string[] a = {"hola","adios","menos","mas"};
2615 string[] b = {"hola","adios","menos","mas"};
2616 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2618 Assert.AreEqual (a[i], b[i], "#TI06 " + i);
2623 public void TestInitializeEnum()
2625 enua[] a = {enua.hola,enua.adios,enua.menos,enua.mas};
2627 enua[] b = {enua.hola,enua.adios,enua.menos,enua.mas};
2628 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2630 Assert.AreEqual (a[i], b[i], "#TI07 " + i);
2635 public void TestInitializeIntNI()
2637 int[] a = new int[20];
2641 Assert.AreEqual (b, 0, "#TI08");
2646 public void TestInitializeCharNI()
2648 char[] a = new char[20];
2650 foreach(char b in a)
2652 Assert.AreEqual (b, 0, "#TI09");
2657 public void TestInitializeDoubleNI()
2659 double[] a = new double[20];
2661 foreach(double b in a)
2663 Assert.AreEqual (b, 0.0, "#TI09");
2668 public void TestInitializeStringNI()
2670 string[] a = new string[20];
2672 foreach(string b in a)
2674 Assert.AreEqual (b, null, "#TI10");
2679 public void TestInitializeObjectNI()
2681 object[] a = new object[20];
2683 foreach(object b in a)
2685 Assert.AreEqual (b, null, "#TI11");
2690 public void TestInitializeAClassNI()
2692 AClass[] a = new AClass[20];
2694 foreach(AClass b in a)
2696 Assert.AreEqual (b, null, "#TI12");
2702 public void TestInitializeAStructNI()
2704 AStruct[] a = new AStruct[20];
2706 foreach(AStruct b in a)
2708 Assert.AreEqual (b, new AStruct(), "#TI14");
2713 public void TestInitializeAStruct()
2715 AStruct[] a = new AStruct[3];
2719 AStruct[] b = new AStruct[3];
2722 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2724 Assert.AreEqual (a[i], b[i], "#TI15 " + i);
2729 public void TestInitializeDateTimeNI()
2731 DateTime[] a = new DateTime[20];
2733 foreach(DateTime b in a)
2735 Assert.AreEqual (b, new DateTime(), "#TI16");
2740 [ExpectedException (typeof (ArgumentNullException))]
2741 public void MoreSort1 ()
2743 Array.Sort (null, 0, 1);
2747 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2748 public void MoreSort2 ()
2750 Array.Sort (arrsort, -1, 3);
2754 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2755 public void MoreSort3 ()
2757 Array.Sort (arrsort, 1, -3);
2761 [ExpectedException (typeof (ArgumentException))]
2762 public void MoreSort4 ()
2764 Array.Sort (arrsort, arrsort.Length, arrsort.Length + 2);
2768 [ExpectedException (typeof (RankException))]
2769 public void MoreSort5 ()
2771 char [,] arr = new char [,] {{'a'}, {'b'}};
2772 Array.Sort (arr, 0, 1);
2776 public void MoreSort6 ()
2778 Array.Sort (arrsort, 0, 0);
2782 [ExpectedException (typeof (ArgumentException))]
2783 public void MoreSort7 ()
2785 Array.Sort (arrsort, arrsort.Length - 1, 2);
2789 [ExpectedException (typeof (ArgumentException))]
2790 public void MoreSort8 ()
2792 Array.Sort (arrsort, 0, arrsort.Length + 1);
2796 public void MoreSort9 ()
2798 Array.Sort (arrsort, null, 0, arrsort.Length, null);
2802 [ExpectedException (typeof (InvalidOperationException))]
2803 public void MoreSort10 ()
2805 object [] array = {true, 'k', SByte.MinValue, Byte.MinValue, (short) 2, 634, (long) 436, (float) 1.1, 1.23, "Hello World"};
2806 Array.Sort (array, (IComparer) null);
2809 [Test] // bug #81941
2812 double [] a = new double [2] { 0.9, 0.3 };
2813 uint [] b = new uint [2] { 4, 7 };
2815 Assert.AreEqual (0.3, a [0], "#1");
2816 Assert.AreEqual (0.9, a [1], "#2");
2817 Assert.AreEqual (7, b [0], "#3");
2818 Assert.AreEqual (4, b [1], "#4");
2822 public void ClearJaggedArray ()
2824 byte[][] matrix = new byte [8][];
2825 for (int i=0; i < 8; i++) {
2826 matrix [i] = new byte [8];
2827 for (int j=0; j < 8; j++) {
2831 Array.Clear (matrix, 0, 8);
2832 for (int i=0; i < 8; i++) {
2833 Assert.IsNull (matrix [i], i.ToString ());
2838 public void ClearMultidimentionalArray ()
2840 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2841 Array.Clear (matrix, 0, 2);
2842 Assert.AreEqual (0, matrix [0, 0], "0,0");
2843 Assert.AreEqual (0, matrix [0, 1], "0,1");
2844 Assert.AreEqual (2, matrix [1, 0], "1,0");
2845 Assert.AreEqual (2, matrix [1, 1], "1,1");
2849 [ExpectedException (typeof (IndexOutOfRangeException))]
2850 public void ClearOutsideMultidimentionalArray ()
2852 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2853 Array.Clear (matrix, 0, 5);
2857 [ExpectedException (typeof (IndexOutOfRangeException))]
2858 public void Clear_IndexOverflow ()
2860 byte[] array = new byte [16];
2861 Array.Clear (array, 4, Int32.MaxValue);
2865 [ExpectedException (typeof (IndexOutOfRangeException))]
2866 public void Clear_LengthOverflow ()
2868 byte[] array = new byte [16];
2869 Array.Clear (array, Int32.MaxValue, 4);
2873 [ExpectedException (typeof (ArgumentException))]
2874 public void Copy_SourceIndexOverflow ()
2876 byte[] array = new byte [16];
2877 Array.Copy (array, Int32.MaxValue, array, 8, 8);
2881 [ExpectedException (typeof (ArgumentException))]
2882 public void Copy_DestinationIndexOverflow ()
2884 byte[] array = new byte [16];
2885 Array.Copy (array, 8, array, Int32.MaxValue, 8);
2889 [ExpectedException (typeof (ArgumentException))]
2890 public void Copy_LengthOverflow ()
2892 byte[] array = new byte [16];
2893 Array.Copy (array, 8, array, 8, Int32.MaxValue);
2897 [ExpectedException (typeof (ArgumentException))]
2898 public void Reverse_IndexOverflow ()
2900 byte[] array = new byte [16];
2901 Array.Reverse (array, Int32.MaxValue, 8);
2905 [ExpectedException (typeof (ArgumentException))]
2906 public void Reverse_LengthOverflow ()
2908 byte[] array = new byte [16];
2909 Array.Reverse (array, 8, Int32.MaxValue);
2912 public struct CharX : IComparable {
2915 public CharX (char c)
2920 public int CompareTo (object obj)
2923 return c.CompareTo (((CharX) obj).c);
2925 return c.CompareTo (obj);
2930 public void BinarySearch_ArgPassingOrder ()
2933 // This tests that arguments are passed to the comprer in the correct
2934 // order. The IComparable of the *array* elements must get called, not
2935 // that of the search object.
2937 CharX [] x = { new CharX ('a'), new CharX ('b'), new CharX ('c') };
2938 Assert.AreEqual (1, Array.BinarySearch (x, 'b'));
2941 class Comparer: IComparer {
2943 private bool called = false;
2945 public bool Called {
2947 bool result = called;
2953 public int Compare (object x, object y)
2961 public void BinarySearch1_EmptyList ()
2963 int[] array = new int[0];
2964 Assert.AreEqual (- 1, Array.BinarySearch (array, 0), "BinarySearch");
2968 public void BinarySearch2_EmptyList ()
2970 int[] array = new int[0];
2971 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, 0), "BinarySearch");
2975 public void BinarySearch3_EmptyList ()
2977 Comparer comparer = new Comparer ();
2978 int[] array = new int[0];
2979 Assert.AreEqual (-1, Array.BinarySearch (array, 0, comparer), "BinarySearch");
2980 // bug 77030 - the comparer isn't called for an empty array/list
2981 Assert.IsTrue (!comparer.Called, "Called");
2985 public void BinarySearch4_EmptyList ()
2987 Comparer comparer = new Comparer ();
2988 int[] array = new int[0];
2989 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, comparer), "BinarySearch");
2990 // bug 77030 - the comparer isn't called for an empty array/list
2991 Assert.IsTrue (!comparer.Called, "Called");
2995 [ExpectedException (typeof (ArgumentNullException))]
2996 public void AsReadOnly_NullArray ()
2998 Array.AsReadOnly <int> (null);
3002 public void ReadOnly_Count ()
3004 Assert.AreEqual (10, Array.AsReadOnly (new int [10]).Count);
3008 public void ReadOnly_Contains ()
3010 int[] arr = new int [2];
3013 IList<int> a = Array.AsReadOnly (arr);
3015 Assert.IsTrue (a.Contains (3));
3016 Assert.IsTrue (!a.Contains (6));
3020 public void ReadOnly_IndexOf ()
3022 int[] arr = new int [2];
3025 IList<int> a = Array.AsReadOnly (arr);
3027 Assert.AreEqual (0, a.IndexOf (3));
3028 Assert.AreEqual (1, a.IndexOf (5));
3029 Assert.AreEqual (-1, a.IndexOf (6));
3033 public void ReadOnly_Indexer ()
3035 int[] arr = new int [2];
3038 IList<int> a = Array.AsReadOnly (arr);
3040 Assert.AreEqual (3, a [0]);
3041 Assert.AreEqual (5, a [1]);
3043 /* Check that modifications to the original array are visible */
3045 Assert.AreEqual (6, a [0]);
3049 public void ReadOnly_Enumerator ()
3051 int[] arr = new int [10];
3053 for (int i = 0; i < 10; ++i)
3057 foreach (int i in Array.AsReadOnly (arr))
3060 Assert.AreEqual (45, sum);
3064 public void ReadOnly_CopyTo ()
3066 int[] arr = new int [2];
3069 IList<int> a = Array.AsReadOnly (arr);
3071 int[] arr2 = new int [3];
3074 Assert.AreEqual (0, arr2 [0]);
3075 Assert.AreEqual (3, arr2 [1]);
3076 Assert.AreEqual (5, arr2 [2]);
3080 public void Resize ()
3082 int [] arr = new int [] { 1, 3, 5 };
3083 Array.Resize <int> (ref arr, 3);
3084 Assert.AreEqual (3, arr.Length, "#A1");
3085 Assert.AreEqual (1, arr [0], "#A2");
3086 Assert.AreEqual (3, arr [1], "#A3");
3087 Assert.AreEqual (5, arr [2], "#A4");
3089 Array.Resize <int> (ref arr, 2);
3090 Assert.AreEqual (2, arr.Length, "#B1");
3091 Assert.AreEqual (1, arr [0], "#B2");
3092 Assert.AreEqual (3, arr [1], "#B3");
3094 Array.Resize <int> (ref arr, 4);
3095 Assert.AreEqual (4, arr.Length, "#C1");
3096 Assert.AreEqual (1, arr [0], "#C2");
3097 Assert.AreEqual (3, arr [1], "#C3");
3098 Assert.AreEqual (0, arr [2], "#C4");
3099 Assert.AreEqual (0, arr [3], "#C5");
3103 public void Resize_null ()
3106 Array.Resize (ref arr, 10);
3107 Assert.AreEqual (arr.Length, 10);
3111 public void Test_ContainsAndIndexOf_EquatableItem ()
3113 EquatableClass[] list = new EquatableClass[] {new EquatableClass (0), new EquatableClass (1), new EquatableClass (0)};
3115 Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, list[0]), "#0");
3116 Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, new EquatableClass (0)), "#1");
3117 Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, list[0]), "#2");
3118 Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, new EquatableClass (0)), "#3");
3121 public class EquatableClass : IEquatable<EquatableClass>
3124 public EquatableClass (int x)
3129 public bool Equals (EquatableClass other)
3131 return this._x == other._x;
3136 public void AsIList ()
3138 IList<int> arr = new int [10];
3140 Assert.AreEqual (5, arr [0]);
3142 IList<FooStruct> arr2 = new FooStruct [10];
3143 FooStruct s = new FooStruct ();
3148 Assert.AreEqual (11, s.i);
3149 Assert.AreEqual (22, s.j);
3151 IList<string> arr3 = new string [10];
3153 Assert.AreEqual ("ABC", arr3 [5]);
3160 #if !TARGET_JVM // BugBUG: T[] is not yet ICollection<T> under TARGET_JVM
3163 public void ICollectionNull ()
3165 ICollection<object> test;
3167 test = new List<object>();
3168 Assert.AreEqual (test.Contains (null), false, "list<o>");
3170 test = new object[] {};
3171 Assert.AreEqual (test.Contains (null), false, "empty array");
3173 test = new object[] {null};
3174 Assert.AreEqual (test.Contains (null), true, "array with null");
3176 test = new object[] { 1, null};
3177 Assert.IsTrue (test.Contains (null), "array with last null");
3179 test = new List<object>(test);
3180 Assert.AreEqual (test.Contains (null), true, "List<object> with test");
3182 test = new object[] {new object()};
3183 Assert.AreEqual (test.Contains (null), false, "array with object");
3185 test = new List<object>(test);
3186 Assert.AreEqual (test.Contains (null), false, "array with test");
3190 public void IListNull ()
3194 test = new List<object>();
3195 Assert.AreEqual (-1, test.IndexOf (null), "list<o>");
3197 test = new object[] {};
3198 Assert.AreEqual (-1, test.IndexOf (null), "empty array");
3200 test = new object[] {null};
3201 Assert.AreEqual (0, test.IndexOf (null), "array with null");
3203 test = new object[] { 1, null};
3204 Assert.AreEqual (1, test.IndexOf (null), "array with last null");
3206 test = new List<object>(test);
3207 Assert.AreEqual (1, test.IndexOf (null), "List<object> with test");
3209 test = new object[] {new object()};
3210 Assert.AreEqual (-1, test.IndexOf (null), "array with object");
3212 test = new List<object>(test);
3213 Assert.AreEqual (-1, test.IndexOf (null), "array with test");
3216 #endif // TARGET_JVM
3220 enum ByteEnum : byte {}
3221 enum IntEnum : int {}
3224 public void TestByteEnumArrayToByteArray ()
3226 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
3227 byte[] b = new byte[a.Length];
3232 public void TestByteEnumArrayToIntArray ()
3234 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
3235 int[] b = new int[a.Length];
3240 [ExpectedException (typeof (ArrayTypeMismatchException))]
3241 public void TestIntEnumArrayToByteArray ()
3243 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
3244 byte[] b = new byte[a.Length];
3249 public void TestIntEnumArrayToIntArray ()
3251 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
3252 int[] b = new int[a.Length];
3258 [Test] // bug #322248
3259 public void IEnumerator_Reset ()
3261 int[] array = new int[] { 1, 2, 3};
3262 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3263 Assert.IsTrue (e.MoveNext (), "#A1");
3264 Assert.AreEqual (1, e.Current, "#A2");
3265 Assert.IsTrue (e.MoveNext (), "#A3");
3266 Assert.AreEqual (2, e.Current, "#A4");
3270 Assert.IsTrue (e.MoveNext (), "#C1");
3271 Assert.AreEqual (1, e.Current, "#C2");
3275 public void IEnumerator_Current_Finished ()
3277 int[] array = new int[] { 1, 2, 3 };
3278 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3279 Assert.IsTrue (e.MoveNext (), "#A1");
3280 Assert.AreEqual (1, e.Current, "#A2");
3281 Assert.IsTrue (e.MoveNext (), "#A3");
3282 Assert.AreEqual (2, e.Current, "#A4");
3283 Assert.IsTrue (e.MoveNext (), "#A5");
3284 Assert.AreEqual (3, e.Current, "#A6");
3285 Assert.IsTrue (!e.MoveNext (), "#A6");
3288 Assert.Fail ("#B1:" + e.Current);
3289 } catch (InvalidOperationException ex) {
3290 // Enumeration already finished
3291 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3292 Assert.IsNull (ex.InnerException, "#B3");
3293 Assert.IsNotNull (ex.Message, "#B4");
3298 public void IEnumerator_Current_NotStarted ()
3300 int[] array = new int[] { 1, 2, 3 };
3301 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3304 Assert.Fail ("#A1:" + e.Current);
3305 } catch (InvalidOperationException ex) {
3306 // Enumeration has not started. Call MoveNext
3307 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
3308 Assert.IsNull (ex.InnerException, "#A3");
3309 Assert.IsNotNull (ex.Message, "#A4");
3314 public void IEnumerator_Current_Reset ()
3316 int[] array = new int[] { 1, 2, 3 };
3317 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3322 Assert.Fail ("#B1:" + e.Current);
3323 } catch (InvalidOperationException ex) {
3324 // Enumeration has not started. Call MoveNext
3325 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3326 Assert.IsNull (ex.InnerException, "#B3");
3327 Assert.IsNotNull (ex.Message, "#B4");
3331 public void ICollection_IsReadOnly() {
3332 ICollection<string> arr = new string [10];
3334 Assert.IsTrue (arr.IsReadOnly);
3338 [ExpectedException (typeof (NotSupportedException))]
3339 public void ArrayCreateInstanceOfVoid ()
3341 Array.CreateInstance (typeof (void), 42);
3347 [ExpectedException (typeof (NotSupportedException))]
3348 public void ArrayCreateInstanceOfOpenGenericType ()
3350 Array.CreateInstance (typeof (Foo<>), 42);
3354 [ExpectedException (typeof (IndexOutOfRangeException))]
3355 public void ClearNegativeLength ()
3357 Array.Clear (new int [] { 1, 2 }, 0, -1);
3361 [ExpectedException (typeof (ArgumentException))]
3362 public void MultiDimension_IList_setItem ()
3364 IList array = new int [1, 1];
3369 [ExpectedException (typeof (ArgumentException))]
3370 public void MultiDimension_IList_getItem ()
3372 IList array = new int [1, 1];
3373 int a = (int) array [0];
3377 public void SetValue_Nullable () {
3378 Array array = Array.CreateInstance (typeof (int?), 7);
3382 array.SetValue (o, 0);
3383 Assert.AreEqual (42, array.GetValue (0));
3385 array.SetValue (null, 0);
3386 Assert.AreEqual (null, array.GetValue (0));
3390 public void SortNullsWithGenericVersion ()
3392 string[] s1 = new string[6]{
3400 string[] s2 = new string[]{null,
3407 Array.Sort<string> (s1);
3408 for (int i = 0; i < 6; i++) {
3409 Assert.AreEqual (s1[i], s2[i], "At:" + i);
3414 // This is a test case for the case that was broken by the code contributed
3417 // This tests the fix for: #622101
3420 public void SortActuallyWorks ()
3422 string[] data = new string[9]{"Foo", "Bar", "Dingus", null, "Dingu4", "123", "Iam", null, "NotNull"};
3423 IComparer comparer = new NullAtEndComparer ();
3424 Array.Sort (data, comparer);
3426 Assert.AreEqual (data [7], null);
3427 Assert.AreNotEqual (data [0], null);
3430 class NullAtEndComparer : IComparer {
3431 public int Compare(object x, object y)
3433 if (x == null) return 1;
3434 if (y == null) return -1;
3435 return ((string)x).CompareTo((string)y);
3440 public void UnalignedArrayClear ()
3442 byte[] input = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
3443 byte[] expected = new byte[] { 1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
3444 Array.Clear (input, 5, 11);
3446 Assert.AreEqual (input, expected);
3451 [ExpectedException (typeof (ArgumentException))]
3452 public void CompareToWithJaggedArray () {
3453 IStructuralComparable a = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }};
3454 IStructuralComparable b = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }};
3455 a.CompareTo (b, Comparer<object>.Default);
3459 [ExpectedException (typeof (ArgumentException))]
3460 public void CompareToWithArrayOfTheWrongKind () {
3461 IStructuralComparable a = new int[] { 1, 2 };
3462 IStructuralComparable b = new double[] { 1, 2 };
3463 a.CompareTo (b, Comparer<object>.Default);
3467 [ExpectedException (typeof (ArgumentException))]
3468 public void CompareToWithNonArrayType () {
3469 IStructuralComparable a = new int[] { 1, 2 };
3470 a.CompareTo (99, Comparer<object>.Default);
3474 [ExpectedException (typeof (ArgumentException))]
3475 public void CompareToWithNonArrayOfDifferentSize () {
3476 IStructuralComparable a = new int[] { 1, 2 };
3477 IStructuralComparable b = new int[] { 1, 2, 3 };
3478 a.CompareTo (b, Comparer<object>.Default);
3482 [ExpectedException (typeof (ArgumentException))]
3483 public void CompareToWithMultiDimArray1 () {
3484 IStructuralComparable a = new int [2,2] { {10, 10 }, { 10, 10 } };
3485 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3486 a.CompareTo (b, Comparer<object>.Default);
3490 [ExpectedException (typeof (ArgumentException))]
3491 public void CompareToWithMultiDimArray2 () {
3492 IStructuralComparable a = new int [2] { 10, 10 };
3493 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3494 a.CompareTo (b, Comparer<object>.Default);
3498 [ExpectedException (typeof (ArgumentException))]
3499 public void CompareToWithMultiDimArray3 () {
3500 IStructuralComparable a = new int [4] { 10, 10, 10, 10 };
3501 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3502 a.CompareTo (b, Comparer<object>.Default);
3506 [ExpectedException (typeof (IndexOutOfRangeException))]
3507 public void CompareToWithBoundedArray1 () {
3508 IStructuralComparable a = new int [2] { 10, 10 };
3509 Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 });
3510 IStructuralComparable b = ab;
3511 ab.SetValue (10, 5);
3512 ab.SetValue (10, 6);
3514 a.CompareTo (b, Comparer<object>.Default);
3518 [ExpectedException (typeof (IndexOutOfRangeException))]
3519 public void CompareToWithBoundedArray2 () {
3520 IStructuralComparable a = new int [2] { 10, 10 };
3521 Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 });
3522 IStructuralComparable b = ab;
3523 ab.SetValue (10, 5);
3524 ab.SetValue (10, 6);
3526 //Yes, CompareTo simply doesn't work with bounded arrays!
3527 b.CompareTo (b, Comparer<object>.Default);
3531 [ExpectedException (typeof (NullReferenceException))]
3532 public void CompareToWithNullComparer () {
3533 IStructuralComparable a = new int[] { 1, 2 };
3534 IStructuralComparable b = new int[] { 1, 2 };
3535 a.CompareTo (b, null);
3539 public void CompareToWithNullArray () {
3540 IStructuralComparable a = new int[] { 1, 2 };
3541 Assert.AreEqual (1, a.CompareTo (null, Comparer<object>.Default));
3545 public void CompareToWithGoodArrays () {
3546 IStructuralComparable a = new int[] { 10, 20 };
3547 Assert.AreEqual (0, a.CompareTo (a, Comparer<object>.Default));
3548 Assert.AreEqual (0, a.CompareTo (new int [] { 10, 20 }, Comparer<object>.Default));
3549 Assert.AreEqual (-1, a.CompareTo (new int [] { 11, 20 }, Comparer<object>.Default));
3550 Assert.AreEqual (-1, a.CompareTo (new int [] { 10, 21 }, Comparer<object>.Default));
3551 Assert.AreEqual (1, a.CompareTo (new int [] { 9, 20 }, Comparer<object>.Default));
3552 Assert.AreEqual (1, a.CompareTo (new int [] { 10, 19 }, Comparer<object>.Default));
3556 public void IStructuralEquatable_Equals ()
3558 IStructuralEquatable array = new int[] {1, 2, 3};
3559 IStructuralEquatable array2 = new int[] {1, 2, 3};
3560 Assert.AreEqual (false, array.Equals (null, null));
3561 Assert.AreEqual (true, array.Equals (array, null));
3562 Assert.AreEqual (true, array.Equals (array2, EqualityComparer<int>.Default));
3566 [ExpectedException (typeof (NullReferenceException))]
3567 public void IStructuralEquatable_Equals_NoComparer ()
3569 IStructuralEquatable array = new int[] {1, 2, 3};
3570 IStructuralComparable array2 = new int[] {1, 2, 3};
3571 array.Equals (array2, null);
3575 [ExpectedException (typeof (ArgumentException))]
3576 public void IStructuralEquatable_Equals_ComparerThrows ()
3578 IStructuralEquatable array = new int[] {1, 2, 3};
3579 IStructuralComparable array2 = new int[] {1, 2, 3};
3580 array.Equals (array2, EqualityComparer<long>.Default);
3584 [ExpectedException (typeof (ArgumentNullException))]
3585 public void IStructuralEquatable_GetHashCode_NullComparer ()
3587 IStructuralEquatable a = new int[] { 1, 2 };
3588 a.GetHashCode (null);
3591 class TestComparer_GetHashCode : IEqualityComparer
3595 bool IEqualityComparer.Equals (object x, object y)
3597 throw new NotImplementedException ();
3600 public int GetHashCode (object obj)
3607 public void IStructuralEquatable_GetHashCode ()
3609 IStructuralEquatable a = new int[] { 1, 2, 9 };
3611 var c = new TestComparer_GetHashCode ();
3613 Assert.AreEqual (3, c.Counter);