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 Assert.IsTrue (errorThrown, "#E62");
549 bool errorThrown = false;
551 Char[,] c1 = new Char[2,2];
552 Char[] c2 = new Char[2];
554 } catch (RankException) {
557 Assert.IsTrue (errorThrown, "#E63");
560 bool errorThrown = false;
562 Char[,] c1 = new Char[2,2];
563 Char[] c2 = new Char[2];
565 } catch (RankException) {
568 Assert.IsTrue (errorThrown, "#E64");
571 bool errorThrown = false;
573 Char[] c1 = new Char[2];
574 Char[] c2 = new Char[2];
576 } catch (ArgumentOutOfRangeException) {
579 Assert.IsTrue (errorThrown, "#E65");
582 bool errorThrown = false;
584 Char[] c1 = new Char[2];
585 Char[] c2 = new Char[2];
587 } catch (ArgumentException) {
590 Assert.IsTrue (errorThrown, "#E66");
593 bool errorThrown = false;
595 Char[] c1 = new Char[2];
596 Char[] c2 = new Char[2];
598 } catch (ArgumentException) {
601 Assert.IsTrue (errorThrown, "#E67");
605 bool errorThrown = false;
607 String[] c1 = new String[2];
608 // TODO: this crashes mono if there are null
609 // values in the array.
612 Char[] c2 = new Char[2];
616 } catch (ArrayTypeMismatchException) {
619 Assert.IsTrue (errorThrown, "#E68");
622 Char[] orig = {'a', 'b', 'c', 'd'};
623 Char[] copy = new Char[10];
624 Array.Clear(copy, 0, copy.Length);
625 orig.CopyTo(copy, 3);
626 Assert.AreEqual ((char)0, copy[0], "#E69");
627 Assert.AreEqual ((char)0, copy[1], "#E70");
628 Assert.AreEqual ((char)0, copy[2], "#E71");
629 Assert.AreEqual (orig[0], copy[3], "#E72");
630 Assert.AreEqual (orig[1], copy[4], "#E73");
631 Assert.AreEqual (orig[2], copy[5], "#E74");
632 Assert.AreEqual (orig[3], copy[6], "#E75");
633 Assert.AreEqual ((char)0, copy[7], "#E76");
634 Assert.AreEqual ((char)0, copy[8], "#E77");
635 Assert.AreEqual ((char)0, copy[9], "#E78");
638 // The following is valid and must not throw an exception.
639 bool errorThrown = false;
641 int[] src = new int [0];
642 int[] dest = new int [0];
643 src.CopyTo (dest, 0);
644 } catch (ArgumentException) {
647 Assert.IsTrue (!errorThrown, "#E79");
652 bool errorThrown = false;
654 CClass[] src = new CClass [] { new CClass () };
655 BClass[] dest = new BClass [1];
657 src.CopyTo (dest, 0);
659 } catch (ArrayTypeMismatchException) {
662 Assert.IsTrue (errorThrown, "#E80");
667 public void TestCreateInstance() {
669 bool errorThrown = false;
671 Array.CreateInstance(null, 12);
672 } catch (ArgumentNullException) {
675 Assert.IsTrue (errorThrown, "#F01");
678 bool errorThrown = false;
680 Array.CreateInstance(Type.GetType("System.Char"), -3);
681 } catch (ArgumentOutOfRangeException) {
684 Assert.IsTrue (errorThrown, "#F02");
687 bool errorThrown = false;
689 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
690 } catch (ArgumentNullException) {
693 Assert.IsTrue (errorThrown, "#F03a");
697 bool errorThrown = false;
699 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
700 } catch (ArgumentNullException) {
703 Assert.IsTrue (errorThrown, "#F03b");
706 bool errorThrown = false;
708 Array.CreateInstance(Type.GetType("System.Char"), null, null);
709 } catch (ArgumentNullException) {
712 Assert.IsTrue (errorThrown, "#F04");
715 bool errorThrown = false;
717 int[] lengths = new int [0];
718 Array.CreateInstance(Type.GetType("System.Char"), lengths);
719 } catch (ArgumentException) {
722 Assert.IsTrue (errorThrown, "#F05");
725 bool errorThrown = false;
727 int[] lengths = new int [1];
728 int[] bounds = new int [2];
729 Array.CreateInstance(Type.GetType("System.Char"), lengths, bounds);
731 } catch (ArgumentException) {
734 Assert.IsTrue (errorThrown, "#F06");
737 char[] c1 = (char[])Array.CreateInstance(Type.GetType("System.Char"), 12);
738 Assert.AreEqual (12, c1.Length, "#F07");
740 Array c2 = Array.CreateInstance(Type.GetType("System.Char"), 12, 5);
741 Assert.AreEqual (2, c2.Rank, "#F08");
742 Assert.AreEqual (60, c2.Length, "#F09");
746 int[] lengths = { 3 };
747 int[] bounds = { 5 };
748 int[] src = { 512, 718, 912 };
749 Array array = Array.CreateInstance(typeof(int), lengths, bounds);
751 Assert.AreEqual (3, array.Length, "#F10");
752 Assert.AreEqual (5, array.GetLowerBound(0), "#F11");
753 Assert.AreEqual (7, array.GetUpperBound(0), "#F12");
755 src.CopyTo (array, 5);
757 for (int i = 0; i < src.Length; i++)
758 Assert.AreEqual (src[i], array.GetValue(i+5), "#F13(" + i + ")");
761 // Test that a 1 dimensional array with 0 lower bound is the
762 // same as an szarray
763 Type szarrayType = new int [10].GetType ();
764 Assert.IsTrue (szarrayType == (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0})).GetType ());
765 Assert.IsTrue (szarrayType != (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {1})).GetType ());
769 [ExpectedException (typeof (ArgumentNullException))]
770 public void TestCreateInstance2 ()
772 Array.CreateInstance (typeof (Int32), (int[])null);
776 [ExpectedException (typeof (ArgumentNullException))]
777 public void TestCreateInstance2b ()
779 Array.CreateInstance (typeof (Int32), (long[])null);
783 public void CreateInstanceVoid ()
785 Assert.Throws<NotSupportedException> (delegate () {
786 Array.CreateInstance (typeof (void), 1);
791 public void TestGetEnumerator() {
792 String[] s1 = {"this", "is", "a", "test"};
793 IEnumerator en = s1.GetEnumerator ();
794 Assert.IsNotNull (en, "#G01");
796 Assert.IsTrue (en.MoveNext (), "#G02");
797 Assert.AreEqual ("this", en.Current, "#G03");
798 Assert.IsTrue (en.MoveNext (), "#G04");
799 Assert.AreEqual ("is", en.Current, "#G05");
800 Assert.IsTrue (en.MoveNext (), "#G06");
801 Assert.AreEqual ("a", en.Current, "#G07");
802 Assert.IsTrue (en.MoveNext (), "#G08");
803 Assert.AreEqual ("test", en.Current, "#G09");
804 Assert.IsTrue (!en.MoveNext (), "#G10");
807 Assert.IsTrue (en.MoveNext (), "#G11");
808 Assert.AreEqual ("this", en.Current, "#G12");
810 // mutation does not invalidate array enumerator!
811 s1.SetValue ("change", 1);
812 Assert.IsTrue (en.MoveNext (), "#G13");
813 Assert.AreEqual ("change", en.Current, "#G14");
817 public void TestGetEnumeratorMultipleDimension() {
818 String[,] s1 = {{"this", "is"}, {"a", "test"}};
819 IEnumerator en = s1.GetEnumerator ();
820 Assert.IsNotNull (en, "#AA01");
822 Assert.IsTrue (en.MoveNext (), "#AA02");
823 Assert.AreEqual ("this", en.Current, "#AA03");
824 Assert.IsTrue (en.MoveNext (), "#AA04");
825 Assert.AreEqual ("is", en.Current, "#AA05");
826 Assert.IsTrue (en.MoveNext (), "#AA06");
827 Assert.AreEqual ("a", en.Current, "#AA07");
828 Assert.IsTrue (en.MoveNext (), "#AA08");
829 Assert.AreEqual ("test", en.Current, "#AA09");
830 Assert.IsTrue (!en.MoveNext (), "#AA10");
833 Assert.IsTrue (en.MoveNext (), "#AA11");
834 Assert.AreEqual ("this", en.Current, "#AA12");
837 // mutation does not invalidate array enumerator!
838 s1.SetValue ("change", idxs);
839 Assert.IsTrue (en.MoveNext (), "#AA13");
840 Assert.AreEqual ("change", en.Current, "#AA14");
844 public void TestGetEnumeratorNonZeroLowerBounds() {
845 int[] myLengthsArray = new int[2] { 3, 5 };
846 int[] myBoundsArray = new int[2] { 2, 3 };
848 Array myArray=Array.CreateInstance( typeof(String), myLengthsArray, myBoundsArray );
849 for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
850 for ( int j = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++ ) {
851 int[] myIndicesArray = new int[2] { i, j };
852 myArray.SetValue( Convert.ToString(i) + j, myIndicesArray );
854 IEnumerator en = myArray.GetEnumerator ();
855 Assert.IsNotNull (en, "#AB01");
857 // check the first couple of values
858 Assert.IsTrue (en.MoveNext (), "#AB02");
859 Assert.AreEqual ("23", en.Current, "#AB03");
860 Assert.IsTrue (en.MoveNext (), "#AB04");
861 Assert.AreEqual ("24", en.Current, "#AB05");
863 // then check the last element's value
866 lastElement = (string)en.Current;
867 } while (en.MoveNext());
868 Assert.AreEqual ("47", lastElement, "#AB06");
872 public void TestIList_Add () {
873 int[] myLengthsArray = new int[2] { 3, 5 };
874 int[] myBoundsArray = new int[2] { 2, 3 };
876 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
878 ((IList)myArray).Add ("can not");
879 Assert.Fail ("IList.Add should throw");
881 catch (NotSupportedException) {
885 Assert.Fail ("IList.Add threw wrong exception type");
888 Assert.Fail ("IList.Add shouldn't get this far");
892 public void TestIList_Insert () {
893 int[] myLengthsArray = new int[2] { 3, 5 };
894 int[] myBoundsArray = new int[2] { 2, 3 };
896 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
898 ((IList)myArray).Insert (0, "can not");
899 Assert.Fail ("IList.Insert should throw");
901 catch (NotSupportedException) {
905 Assert.Fail ("IList.Insert threw wrong exception type");
908 Assert.Fail ("IList.Insert shouldn't get this far");
912 public void TestIList_Remove () {
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 );
918 ((IList)myArray).Remove ("can not");
919 Assert.Fail ("IList.Remove should throw");
921 catch (NotSupportedException) {
925 Assert.Fail ("IList.Remove threw wrong exception type");
928 Assert.Fail ("IList.Remove shouldn't get this far");
932 public void TestIList_RemoveAt () {
933 int[] myLengthsArray = new int[2] { 3, 5 };
934 int[] myBoundsArray = new int[2] { 2, 3 };
936 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
938 ((IList)myArray).RemoveAt (0);
939 Assert.Fail ("IList.RemoveAt should throw");
941 catch (NotSupportedException) {
945 Assert.Fail ("IList.RemoveAt threw wrong exception type");
948 Assert.Fail ("IList.RemoveAt shouldn't get this far");
952 public void TestIList_Contains () {
953 int[] myLengthsArray = new int[2] { 3, 5 };
954 int[] myBoundsArray = new int[2] { 2, 3 };
956 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
959 bool b = ((IList)myArray).Contains ("23");
960 Assert.Fail ("IList.Contains should throw with multi-dimensional arrays");
962 catch (RankException) {
963 int[] iArr = new int[3] { 1, 2, 3};
964 // check the first and last items
965 Assert.IsTrue (((IList)iArr).Contains (1), "AC01");
966 Assert.IsTrue (((IList)iArr).Contains (3), "AC02");
968 // and one that is definately not there
969 Assert.IsTrue (!((IList)iArr).Contains (42), "AC03");
973 Assert.Fail ("Should not get here");
977 public void TestIList_IndexOf () {
978 int[] myLengthsArray = new int[2] { 3, 5 };
979 int[] myBoundsArray = new int[2] { 2, 3 };
981 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
984 bool b = ((IList)myArray).Contains ("23");
985 Assert.Fail ("IList.Contains should throw with multi-dimensional arrays");
987 catch (RankException) {
988 int[] iArr = new int[3] { 1, 2, 3};
989 // check the first and last items
990 Assert.AreEqual (0, ((IList)iArr).IndexOf (1), "AD01");
991 Assert.AreEqual (2, ((IList)iArr).IndexOf (3), "AD02");
993 // and one that is definately not there
994 Assert.AreEqual (-1, ((IList)iArr).IndexOf (42), "AD03");
996 catch (Exception e) {
997 Assert.Fail ("Unexpected exception: " + e.ToString());
1000 // check that wierd case whem lowerbound is Int32.MinValue,
1001 // so that IndexOf() needs to return Int32.MaxValue when it cannot find the object
1002 int[] myLengthArray = new int[1] { 3 };
1003 int[] myBoundArray = new int[1] { Int32.MinValue };
1004 Array myExtremeArray=Array.CreateInstance ( typeof(String), myLengthArray, myBoundArray );
1005 Assert.AreEqual (Int32.MaxValue, ((IList)myExtremeArray).IndexOf (42), "AD04");
1009 public void TestGetLength() {
1011 bool errorThrown = false;
1013 char[] c1 = {'a', 'b', 'c'};
1015 } catch (IndexOutOfRangeException) {
1018 Assert.IsTrue (errorThrown, "#H01");
1021 bool errorThrown = false;
1023 char[] c1 = {'a', 'b', 'c'};
1025 } catch (IndexOutOfRangeException) {
1028 Assert.IsTrue (errorThrown, "#H02");
1031 char[] c2 = new Char[5];
1032 Assert.AreEqual (5, c2.GetLength(0), "#H03");
1034 char[,] c3 = new Char[6,7];
1035 Assert.AreEqual (6, c3.GetLength(0), "#H04");
1036 Assert.AreEqual (7, c3.GetLength(1), "#H05");
1040 public void TestGetLowerBound() {
1042 bool errorThrown = false;
1044 char[] c = {'a', 'b', 'c'};
1045 c.GetLowerBound(-1);
1046 } catch (IndexOutOfRangeException) {
1049 Assert.IsTrue (errorThrown, "#H31");
1052 bool errorThrown = false;
1054 char[] c = {'a', 'b', 'c'};
1056 } catch (IndexOutOfRangeException) {
1059 Assert.IsTrue (errorThrown, "#H32");
1062 char[] c1 = new Char[5];
1063 Assert.AreEqual (0, c1.GetLowerBound(0), "#H33");
1065 char[,] c2 = new Char[4,4];
1066 Assert.AreEqual (0, c2.GetLowerBound(0), "#H34");
1067 Assert.AreEqual (0, c2.GetLowerBound(1), "#H35");
1071 public void TestGetUpperBound() {
1073 bool errorThrown = false;
1075 char[] c = {'a', 'b', 'c'};
1076 c.GetUpperBound(-1);
1077 } catch (IndexOutOfRangeException) {
1080 Assert.IsTrue (errorThrown, "#H61");
1083 bool errorThrown = false;
1085 char[] c = {'a', 'b', 'c'};
1087 } catch (IndexOutOfRangeException) {
1090 Assert.IsTrue (errorThrown, "#H62");
1093 char[] c1 = new Char[5];
1094 Assert.AreEqual (4, c1.GetUpperBound(0), "#H63");
1096 char[,] c2 = new Char[4,6];
1097 Assert.AreEqual (3, c2.GetUpperBound(0), "#H64");
1098 Assert.AreEqual (5, c2.GetUpperBound(1), "#H65");
1102 public void TestGetValue1() {
1104 bool errorThrown = false;
1106 char[,] c = new Char[2,2];
1108 } catch (ArgumentException) {
1111 Assert.IsTrue (errorThrown, "#I01");
1114 bool errorThrown = false;
1116 char[] c = {'a', 'b', 'c'};
1118 } catch (IndexOutOfRangeException) {
1121 Assert.IsTrue (errorThrown, "#I02");
1124 bool errorThrown = false;
1126 char[] c = {'a', 'b', 'c'};
1128 } catch (IndexOutOfRangeException) {
1131 Assert.IsTrue (errorThrown, "#I03");
1134 char[] c1 = {'a', 'b', 'c', 'd'};
1135 for (int i = 0; i < c1.Length; i++) {
1136 Assert.AreEqual (c1[i], c1.GetValue(i), "#I04(" + i + ")");
1141 public void TestGetValue2() {
1143 bool errorThrown = false;
1145 char[] c = new Char[2];
1147 } catch (ArgumentException) {
1150 Assert.IsTrue (errorThrown, "#I21");
1153 bool errorThrown = false;
1155 char[,] c = new Char[2,2];
1157 } catch (IndexOutOfRangeException) {
1160 Assert.IsTrue (errorThrown, "#I22");
1163 bool errorThrown = false;
1165 char[,] c = new Char[2,2];
1167 } catch (IndexOutOfRangeException) {
1170 Assert.IsTrue (errorThrown, "#I23");
1173 char[,] c1 = new Char[4,6];
1174 for (int i = 0; i < 24; i++) {
1177 c1[first,second] = (char)(((int)'a')+i);
1179 for (int i = 0; i < c1.GetLength(0); i++) {
1180 for (int j = 0; j < c1.GetLength(1); j++) {
1181 Assert.AreEqual (c1[i, j], c1.GetValue(i, j), "#I24(" + i + "," + j + ")");
1187 public void TestGetValue3() {
1189 bool errorThrown = false;
1191 char[] c = new Char[2];
1193 } catch (ArgumentException) {
1196 Assert.IsTrue (errorThrown, "#I41");
1199 bool errorThrown = false;
1201 char[,,] c = new Char[2,2,2];
1202 c.GetValue(-1, 1, 1);
1203 } catch (IndexOutOfRangeException) {
1206 Assert.IsTrue (errorThrown, "#I42");
1209 bool errorThrown = false;
1211 char[,,] c = new Char[2,2,2];
1213 } catch (IndexOutOfRangeException) {
1216 Assert.IsTrue (errorThrown, "#I43");
1219 char[,,] c1 = new Char[4,2,3];
1220 for (int i = 0; i < 24; i++) {
1222 int remains = i % 6;
1223 int second = remains / 3;
1224 int third = remains % 3;
1225 c1[first,second, third] = (char)(((int)'a')+i);
1227 for (int i = 0; i < c1.GetLength(0); i++) {
1228 for (int j = 0; j < c1.GetLength(1); j++) {
1229 for (int k = 0; k < c1.GetLength(2); k++) {
1230 Assert.AreEqual (c1[i, j, k], c1.GetValue(i, j, k), "#I44(" + i + "," + j + ")");
1237 [ExpectedException (typeof (ArgumentNullException))]
1238 public void TestGetValueLongArray ()
1240 char[] c = new Char[2];
1241 c.GetValue((long [])null);
1245 public void TestGetValueN() {
1247 bool errorThrown = false;
1249 char[] c = new Char[2];
1250 c.GetValue((int [])null);
1251 } catch (ArgumentNullException) {
1254 Assert.IsTrue (errorThrown, "#I61a");
1257 bool errorThrown = false;
1259 char[] c = new Char[2];
1260 int[] coords = {1, 1};
1262 } catch (ArgumentException) {
1265 Assert.IsTrue (errorThrown, "#I62");
1268 bool errorThrown = false;
1270 char[,] c = new Char[2,2];
1271 int[] coords = {-1, 1};
1273 } catch (IndexOutOfRangeException) {
1276 Assert.IsTrue (errorThrown, "#I63");
1279 bool errorThrown = false;
1281 char[,] c = new Char[2,2];
1282 int[] coords = {4, 1};
1284 } catch (IndexOutOfRangeException) {
1287 Assert.IsTrue (errorThrown, "#I64");
1290 char[,] c1 = new Char[4,6];
1291 for (int i = 0; i < 24; i++) {
1294 c1[first,second] = (char)(((int)'a')+i);
1296 for (int i = 0; i < c1.GetLength(0); i++) {
1297 for (int j = 0; j < c1.GetLength(1); j++) {
1298 int[] coords = {i, j};
1299 Assert.AreEqual (c1[i, j], c1.GetValue(coords), "#I65(" + i + "," + j + ")");
1305 public void TestIndexOf1() {
1307 bool errorThrown = false;
1309 Array.IndexOf(null, "huh?");
1310 } catch (ArgumentNullException) {
1313 Assert.IsTrue (errorThrown, "#J01");
1316 bool errorThrown = false;
1318 char[,] c = new Char[2,2];
1319 Array.IndexOf(c, "huh?");
1320 } catch (RankException) {
1323 Assert.IsTrue (errorThrown, "#J02");
1326 String[] s1 = {"this", "is", "a", "test"};
1327 Assert.AreEqual (-1, Array.IndexOf(s1, null), "#J03");
1328 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing"), "#J04");
1329 Assert.AreEqual (0, Array.IndexOf(s1, "this"), "#J05");
1330 Assert.AreEqual (3, Array.IndexOf(s1, "test"), "#J06");
1334 public void TestIndexOf2() {
1336 bool errorThrown = false;
1338 Array.IndexOf(null, "huh?", 0);
1339 } catch (ArgumentNullException) {
1342 Assert.IsTrue (errorThrown, "#J21");
1345 bool errorThrown = false;
1347 char[,] c = new Char[2,2];
1348 Array.IndexOf(c, "huh?", 0);
1349 } catch (RankException) {
1352 Assert.IsTrue (errorThrown, "#J22");
1355 bool errorThrown = false;
1357 char[] c = new Char[2];
1358 Array.IndexOf(c, "huh?", 3);
1359 } catch (ArgumentOutOfRangeException) {
1362 Assert.IsTrue (errorThrown, "#J23");
1365 String[] s1 = {"this", "is", "really", "a", "test"};
1366 Assert.AreEqual (-1, Array.IndexOf(s1, null, 1), "#J24");
1367 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing", 1), "#J25");
1368 Assert.AreEqual (-1, Array.IndexOf(s1, "this", 1), "#J26");
1369 Assert.AreEqual (1, Array.IndexOf(s1, "is", 1), "#J27");
1370 Assert.AreEqual (4, Array.IndexOf(s1, "test", 1), "#J28");
1374 public void TestIndexOf3() {
1376 bool errorThrown = false;
1378 Array.IndexOf(null, "huh?", 0, 1);
1379 } catch (ArgumentNullException) {
1382 Assert.IsTrue (errorThrown, "#J41");
1385 bool errorThrown = false;
1387 char[,] c = new Char[2,2];
1388 Array.IndexOf(c, "huh?", 0, 1);
1389 } catch (RankException) {
1392 Assert.IsTrue (errorThrown, "#J42");
1395 bool errorThrown = false;
1397 char[] c = new Char[2];
1398 Array.IndexOf(c, "huh?", 3, 1);
1399 } catch (ArgumentOutOfRangeException) {
1402 Assert.IsTrue (errorThrown, "#J43");
1405 bool errorThrown = false;
1407 char[] c = new Char[2];
1408 Array.IndexOf(c, "huh?", 0, 5);
1409 } catch (ArgumentOutOfRangeException) {
1412 Assert.IsTrue (errorThrown, "#J44");
1415 String[] s1 = {"this", "is", "really", "a", "test"};
1416 Assert.AreEqual (-1, Array.IndexOf(s1, null, 1, 3), "#J45");
1417 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing", 1, 3), "#J46");
1418 Assert.AreEqual (-1, Array.IndexOf(s1, "this", 1, 3), "#J47");
1419 Assert.AreEqual (1, Array.IndexOf(s1, "is", 1, 3), "#J48");
1420 Assert.AreEqual (-1, Array.IndexOf(s1, "test", 1, 3), "#J49");
1421 Assert.AreEqual (3, Array.IndexOf(s1, "a", 1, 3), "#J50");
1425 public void TestIndexOf_CustomEqual ()
1427 DataEqual[] test = new DataEqual [] { new DataEqual () };
1428 Assert.AreEqual (0, Array.IndexOf (test, "asdfas", 0));
1430 IList array = (IList)test;
1431 Assert.AreEqual (0, array.IndexOf ("asdfas"));
1435 public void TestLastIndexOf1() {
1437 bool errorThrown = false;
1439 Array.LastIndexOf(null, "huh?");
1440 } catch (ArgumentNullException) {
1443 Assert.IsTrue (errorThrown, "#K01");
1446 bool errorThrown = false;
1448 char[,] c = new Char[2,2];
1449 Array.LastIndexOf(c, "huh?");
1450 } catch (RankException) {
1453 Assert.IsTrue (errorThrown, "#K02");
1456 String[] s1 = {"this", "is", "a", "a", "test"};
1457 Assert.AreEqual (-1, Array.LastIndexOf(s1, null), "#K03");
1458 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing"), "#K04");
1459 Assert.AreEqual (0, Array.LastIndexOf(s1, "this"), "#K05");
1460 Assert.AreEqual (4, Array.LastIndexOf(s1, "test"), "#K06");
1461 Assert.AreEqual (3, Array.LastIndexOf(s1, "a"), "#K07");
1463 Assert.AreEqual (-1, Array.LastIndexOf (new String [0], "foo"));
1467 public void TestLastIndexOf2() {
1469 bool errorThrown = false;
1471 Array.LastIndexOf(null, "huh?", 0);
1472 } catch (ArgumentNullException) {
1475 Assert.IsTrue (errorThrown, "#K21");
1478 bool errorThrown = false;
1480 char[,] c = new Char[2,2];
1481 Array.LastIndexOf(c, "huh?", 0);
1482 } catch (RankException) {
1485 Assert.IsTrue (errorThrown, "#K22");
1488 bool errorThrown = false;
1490 char[] c = new Char[2];
1491 Array.LastIndexOf(c, "huh?", 3);
1492 } catch (ArgumentOutOfRangeException) {
1495 Assert.IsTrue (errorThrown, "#K23");
1498 String[] s1 = {"this", "is", "really", "a", "test"};
1499 Assert.AreEqual (-1, Array.LastIndexOf(s1, null, 3), "#K24");
1500 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing", 3), "#K25");
1501 Assert.AreEqual (-1, Array.LastIndexOf(s1, "test", 3), "#K26");
1502 Assert.AreEqual (3, Array.LastIndexOf(s1, "a", 3), "#K27");
1503 Assert.AreEqual (0, Array.LastIndexOf(s1, "this", 3), "#K28");
1507 public void TestLastIndexOf3() {
1509 bool errorThrown = false;
1511 Array.LastIndexOf(null, "huh?", 0, 1);
1512 } catch (ArgumentNullException) {
1515 Assert.IsTrue (errorThrown, "#K41");
1518 bool errorThrown = false;
1520 char[,] c = new Char[2,2];
1521 Array.LastIndexOf(c, "huh?", 0, 1);
1522 } catch (RankException) {
1525 Assert.IsTrue (errorThrown, "#K42");
1528 bool errorThrown = false;
1530 char[] c = new Char[2];
1531 Array.LastIndexOf(c, "huh?", 3, 1);
1532 } catch (ArgumentOutOfRangeException) {
1535 Assert.IsTrue (errorThrown, "#K43");
1538 bool errorThrown = false;
1540 char[] c = new Char[2];
1541 Array.LastIndexOf(c, "huh?", 0, 5);
1542 } catch (ArgumentOutOfRangeException) {
1545 Assert.IsTrue (errorThrown, "#K44");
1548 String[] s1 = {"this", "is", "really", "a", "test"};
1549 Assert.AreEqual (-1, Array.LastIndexOf(s1, null, 3, 3), "#K45");
1550 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing", 3, 3), "#K46");
1551 Assert.AreEqual (-1, Array.LastIndexOf(s1, "this", 3, 3), "#K47");
1552 Assert.AreEqual (1, Array.LastIndexOf(s1, "is", 3, 3), "#K48");
1553 Assert.AreEqual (-1, Array.LastIndexOf(s1, "test", 3, 3), "#K49");
1554 Assert.AreEqual (3, Array.LastIndexOf(s1, "a", 3, 3), "#K50");
1558 public void TestLastIndexOf4 ()
1560 short [] a = new short [] { 19, 238, 317, 6, 565, 0, -52, 60, -563, 753, 238, 238};
1562 Array.LastIndexOf (a, (object)16, -1);
1563 NUnit.Framework.Assert.Fail ("#1");
1564 } catch (ArgumentOutOfRangeException) { }
1567 Array.LastIndexOf<short> (a, 16, -1);
1568 NUnit.Framework.Assert.Fail ("#2");
1569 } catch (ArgumentOutOfRangeException) { }
1573 public void TestLastIndexOf5 ()
1575 char [] a = new char [] {'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'j', 'i', 'h'};
1580 for (int i = a.Length - 1; i >= 0 ; i--) {
1582 retval = Array.LastIndexOf(a, a [i], i, i + 1);
1586 Assert.IsTrue (!error);
1590 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1591 public void LastIndexOf_StartIndexOverflow ()
1593 // legal - no exception
1594 byte[] array = new byte [16];
1595 Array.LastIndexOf (array, this, Int32.MaxValue, 1);
1599 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1600 public void LastIndexOf_CountOverflow ()
1602 // legal - no exception
1603 byte[] array = new byte [16];
1604 Array.LastIndexOf (array, this, 1, Int32.MaxValue);
1608 public void LastIndexOf_0LengthArray ()
1610 Array array = Array.CreateInstance (typeof (char), 0);
1611 int idx = Array.LastIndexOf (array, (object) null, -1, 0);
1612 Assert.IsTrue (idx == -1, "#01");
1613 idx = Array.LastIndexOf (array, (object) null, -1, 10);
1614 Assert.IsTrue (idx == -1, "#02");
1615 idx = Array.LastIndexOf (array, (object) null, -100, 10);
1616 Assert.IsTrue (idx == -1, "#02");
1618 array = Array.CreateInstance (typeof (char), 1);
1620 Array.LastIndexOf (array, (object) null, -1, 0);
1621 Assert.Fail ("#04");
1622 } catch (ArgumentOutOfRangeException e) {
1625 Array.LastIndexOf (array, (object) null, -1, 10);
1626 Assert.Fail ("#05");
1627 } catch (ArgumentOutOfRangeException e) {
1630 Array.LastIndexOf (array, (object) null, -100, 10);
1631 Assert.Fail ("#06");
1632 } catch (ArgumentOutOfRangeException e) {
1638 public void FindIndexTest ()
1640 var a = new int[] { 2, 2, 2, 3, 2 };
1641 Assert.AreEqual (2, Array.FindIndex (a, 2, 2, l => true));
1645 public void FindIndex_Invalid ()
1647 var array = new int [] { 1, 2, 3, 4, 5 };
1650 Array.FindIndex (array, null);
1652 } catch (ArgumentNullException) {
1656 Array.FindIndex (array, -1, l => true);
1658 } catch (ArgumentOutOfRangeException) {
1662 Array.FindIndex (array, -1, 0, l => true);
1663 Assert.Fail ("#2b");
1664 } catch (ArgumentOutOfRangeException) {
1668 Array.FindIndex (array, 0, -1, l => true);
1670 } catch (ArgumentOutOfRangeException) {
1674 Array.FindIndex (array, 100, l => true);
1676 } catch (ArgumentOutOfRangeException) {
1680 Array.FindIndex (array, 100, 0, l => true);
1681 Assert.Fail ("#4b");
1682 } catch (ArgumentOutOfRangeException) {
1686 Array.FindIndex (array, 7, 2, l => true);
1688 } catch (ArgumentOutOfRangeException) {
1692 [Test, ExpectedException (typeof (ArgumentNullException))]
1693 public void FindLastNullTest ()
1695 var array = new int [] { 1, 2, 3, 4, 5 };
1696 Array.FindLast (array, null);
1700 public void FindLastIndexTest ()
1702 var array = new int [] { 1, 2, 3, 4, 5 };
1704 Assert.AreEqual (2, Array.FindLastIndex (array, 2, 3, l => true));
1705 Assert.AreEqual (2, Array.FindLastIndex (array, 2, 2, l => true));
1706 Assert.AreEqual (1, Array.FindLastIndex (array, 1, 2, l => true));
1710 public void FindLastIndex_Invalid ()
1712 var array = new int [] { 1, 2, 3, 4, 5 };
1714 Array.FindLastIndex (array, null);
1716 } catch (ArgumentNullException) {
1720 Array.FindLastIndex (array, -1, l => true);
1722 } catch (ArgumentOutOfRangeException) {
1726 Array.FindLastIndex (array, -1, 0, l => true);
1727 Assert.Fail ("#2b");
1728 } catch (ArgumentOutOfRangeException) {
1732 Array.FindLastIndex (array, 0, -1, l => true);
1734 } catch (ArgumentOutOfRangeException) {
1738 Array.FindLastIndex (array, 100, l => true);
1740 } catch (ArgumentOutOfRangeException) {
1744 Array.FindLastIndex (array, 100, 0, l => true);
1745 Assert.Fail ("#4b");
1746 } catch (ArgumentOutOfRangeException) {
1750 Array.FindLastIndex (array, 2, 4, l => true);
1752 } catch (ArgumentOutOfRangeException) {
1757 public void TestReverse() {
1759 bool errorThrown = false;
1761 Array.Reverse(null);
1762 } catch (ArgumentNullException) {
1765 Assert.IsTrue (errorThrown, "#L01");
1768 bool errorThrown = false;
1770 char[,] c = new Char[2,2];
1772 } catch (RankException) {
1775 Assert.IsTrue (errorThrown, "#L02");
1778 char[] c1 = {'a', 'b', 'c', 'd'};
1780 Assert.AreEqual ('d', c1[0], "#L03");
1781 Assert.AreEqual ('c', c1[1], "#L04");
1782 Assert.AreEqual ('b', c1[2], "#L05");
1783 Assert.AreEqual ('a', c1[3], "#L06");
1786 bool errorThrown = false;
1788 Array.Reverse(null, 0, 0);
1789 } catch (ArgumentNullException) {
1792 Assert.IsTrue (errorThrown, "#L07");
1795 bool errorThrown = false;
1797 char[,] c = new Char[2,2];
1798 Array.Reverse(c, 0, 0);
1799 } catch (RankException) {
1802 Assert.IsTrue (errorThrown, "#L08");
1805 //bool errorThrown = false;
1807 // char[] c = new Char[2];
1808 // Array.Reverse(c, 0, 3);
1809 //} catch (ArgumentOutOfRangeException) {
1810 // errorThrown = true;
1812 //Assert.IsTrue (errorThrown, "#L09");
1815 //bool errorThrown = false;
1817 // char[] c = new Char[2];
1818 // Array.Reverse(c, 3, 0);
1819 //} catch (ArgumentOutOfRangeException) {
1820 // errorThrown = true;
1822 //Assert.IsTrue (errorThrown, "#L10");
1825 char[] c2 = { 'a', 'b', 'c', 'd'};
1826 Array.Reverse(c2, 1, 2);
1827 Assert.AreEqual ('a', c2[0], "#L11");
1828 Assert.AreEqual ('c', c2[1], "#L12");
1829 Assert.AreEqual ('b', c2[2], "#L13");
1830 Assert.AreEqual ('d', c2[3], "#L14");
1835 public void ReverseStruct () {
1836 BStruct[] c3 = new BStruct[2];
1837 c3 [0] = new BStruct () { i1 = 1, i2 = 2, i3 = 3 };
1838 c3 [1] = new BStruct () { i1 = 4, i2 = 5, i3 = 6 };
1840 Assert.AreEqual (4, c3 [0].i1);
1841 Assert.AreEqual (5, c3 [0].i2);
1842 Assert.AreEqual (6, c3 [0].i3);
1843 Assert.AreEqual (1, c3 [1].i1);
1844 Assert.AreEqual (2, c3 [1].i2);
1845 Assert.AreEqual (3, c3 [1].i3);
1849 public int i1, i2, i3;
1853 public void TestSetValue1() {
1855 bool errorThrown = false;
1857 char[,] c = new Char[2,2];
1858 c.SetValue("buh", 1);
1859 } catch (ArgumentException) {
1862 Assert.IsTrue (errorThrown, "#M01");
1865 bool errorThrown = false;
1867 char[] c = {'a', 'b', 'c'};
1868 c.SetValue("buh", -1);
1869 } catch (IndexOutOfRangeException) {
1872 Assert.IsTrue (errorThrown, "#M02");
1875 bool errorThrown = false;
1877 char[] c = {'a', 'b', 'c'};
1878 c.SetValue("buh", 4);
1879 } catch (IndexOutOfRangeException) {
1882 Assert.IsTrue (errorThrown, "#M03");
1885 char[] c1 = {'a', 'b', 'c', 'd'};
1886 char[] c2 = new char[4];
1887 for (int i = 0; i < c1.Length; i++) {
1888 c2.SetValue(c1[i], i);
1890 for (int i = 0; i < c1.Length; i++) {
1891 Assert.AreEqual (c1[i], c2[i], "#M04(" + i + ")");
1894 int[] c3 = { 1, 2, 3 };
1895 long[] c4 = new long [3];
1897 for (int i = 0; i < c3.Length; i++)
1898 c4.SetValue (c3 [i], i);
1902 } catch (Exception e) {
1903 Assert.Fail ("c3.CopyTo(): e=" + e);
1905 for (int i = 0; i < c3.Length; i++)
1906 Assert.IsTrue (c3[i] == c4[i], "#M05(" + i + ")");
1908 Object[] c5 = new Object [3];
1909 long[] c6 = new long [3];
1913 } catch (Exception e) {
1914 Assert.Fail ("c4.CopyTo(): e=" + e);
1919 } catch (Exception e) {
1920 Assert.Fail ("c5.CopyTo(): e=" + e);
1922 // for (int i = 0; i < c5.Length; i++)
1923 // Assert.IsTrue (c5[i] == c6[i], "#M06(" + i + ")");
1927 public void TestSetValue2() {
1929 bool errorThrown = false;
1931 char[] c = new Char[2];
1932 c.SetValue("buh", 1,1);
1933 } catch (ArgumentException) {
1936 Assert.IsTrue (errorThrown, "#M21");
1939 bool errorThrown = false;
1941 char[,] c = new Char[2,2];
1942 c.SetValue("buh", -1, 1);
1943 } catch (IndexOutOfRangeException) {
1946 Assert.IsTrue (errorThrown, "#M22");
1949 bool errorThrown = false;
1951 char[,] c = new Char[2,2];
1952 c.SetValue("buh", 4,1);
1953 } catch (IndexOutOfRangeException) {
1956 Assert.IsTrue (errorThrown, "#M23");
1959 char[,] c1 = new Char[4,6];
1960 char[,] c2 = new Char[4,6];
1961 for (int i = 0; i < 24; i++) {
1964 c1[first,second] = (char)(((int)'a')+i);
1965 c2.SetValue(c1[first,second], first, second);
1967 for (int i = 0; i < c1.GetLength(0); i++) {
1968 for (int j = 0; j < c1.GetLength(1); j++) {
1969 Assert.AreEqual (c1[i, j], c2[i, j], "#M24(" + i + "," + j + ")");
1975 public void TestSetValue3() {
1977 bool errorThrown = false;
1979 char[] c = new Char[2];
1980 c.SetValue("buh", 1,1,1);
1981 } catch (ArgumentException) {
1984 Assert.IsTrue (errorThrown, "#M41");
1987 bool errorThrown = false;
1989 char[,,] c = new Char[2,2,2];
1990 c.SetValue("buh", -1, 1, 1);
1991 } catch (IndexOutOfRangeException) {
1994 Assert.IsTrue (errorThrown, "#M42");
1997 bool errorThrown = false;
1999 char[,,] c = new Char[2,2,2];
2000 c.SetValue("buh", 4,1,1);
2001 } catch (IndexOutOfRangeException) {
2004 Assert.IsTrue (errorThrown, "#M43");
2007 char[,,] c1 = new Char[4,2,3];
2008 char[,,] c2 = new Char[4,2,3];
2009 for (int i = 0; i < 24; i++) {
2011 int remains = i % 6;
2012 int second = remains / 3;
2013 int third = remains % 3;
2014 c1[first,second, third] = (char)(((int)'a')+i);
2015 c2.SetValue(c1[first, second, third], first, second, third);
2017 for (int i = 0; i < c1.GetLength(0); i++) {
2018 for (int j = 0; j < c1.GetLength(1); j++) {
2019 for (int k = 0; k < c1.GetLength(2); k++) {
2020 Assert.AreEqual (c1[i, j, k], c2[i, j, k], "#M44(" + i + "," + j + " )");
2027 [ExpectedException (typeof (ArgumentNullException))]
2028 public void TestSetValueLongArray ()
2030 char[] c = new Char[2];
2031 c.SetValue("buh", (long [])null);
2035 public void TestSetValueN() {
2037 bool errorThrown = false;
2039 char[] c = new Char[2];
2040 c.SetValue("buh", (int [])null);
2041 } catch (ArgumentNullException) {
2044 Assert.IsTrue (errorThrown, "#M61a");
2047 bool errorThrown = false;
2049 char[] c = new Char[2];
2050 int[] coords = {1, 1};
2051 c.SetValue("buh", coords);
2052 } catch (ArgumentException) {
2055 Assert.IsTrue (errorThrown, "#M62");
2058 bool errorThrown = false;
2060 char[,] c = new Char[2,2];
2061 int[] coords = {-1, 1};
2062 c.SetValue("buh", coords);
2063 } catch (IndexOutOfRangeException) {
2066 Assert.IsTrue (errorThrown, "#M63");
2069 bool errorThrown = false;
2071 char[,] c = new Char[2,2];
2072 int[] coords = {4, 1};
2073 c.SetValue("buh", coords);
2074 } catch (IndexOutOfRangeException) {
2077 Assert.IsTrue (errorThrown, "#M64");
2080 char[,] c1 = new Char[4,6];
2081 char[,] c2 = new Char[4,6];
2082 for (int i = 0; i < 24; i++) {
2085 c1[first,second] = (char)(((int)'a')+i);
2086 int[] coords = {first, second};
2087 c2.SetValue(c1[first,second], coords);
2089 for (int i = 0; i < c1.GetLength(0); i++) {
2090 for (int j = 0; j < c1.GetLength(1); j++) {
2091 Assert.AreEqual (c1[i, j], c2[i, j], "#M65(" + i + "," + j + ")");
2097 public void TestSetValue4() {
2099 int[] c1 = { 1, 2, 3 };
2100 long[] c2 = new long [3];
2102 for (int i = 0; i < c1.Length; i++)
2103 c2.SetValue (c1 [i], i);
2105 for (int i = 0; i < c1.Length; i++) {
2106 Assert.IsTrue (c1[i] == c2[i], "#M81(" + i + ")");
2107 Assert.AreEqual (typeof (long), c2[i].GetType (), "#M82(" + i + ")");
2111 long[] c1 = { 1, 2, 3 };
2112 int[] c2 = new int [3];
2113 bool errorThrown = false;
2115 c2.SetValue (c1 [0], 0);
2116 } catch (ArgumentException) {
2119 Assert.IsTrue (errorThrown, "#M83");
2122 int[] c1 = { 1, 2, 3 };
2123 Object[] c2 = new Object [3];
2125 for (int i = 0; i < c1.Length; i++)
2126 c2.SetValue (c1 [i], i);
2128 for (int i = 0; i < c1.Length; i++)
2129 Assert.AreEqual (c1[i], Convert.ToInt32 (c2[i]), "#M84(" + i + ")");
2132 Object[] c1 = new Object [3];
2133 Object[] c2 = new Object [3];
2134 c1[0] = new Object ();
2136 for (int i = 0; i < c1.Length; i++)
2137 c2.SetValue (c1 [i], i);
2139 for (int i = 0; i < c1.Length; i++)
2140 Assert.AreEqual (c1[i], c2[i], "#M85(" + i + ")");
2143 Object[] c1 = new Object [3];
2144 string[] c2 = new String [3];
2145 string test = "hello";
2148 c2.SetValue (c1 [0], 0);
2149 Assert.AreEqual (c1[0], c2[0], "#M86");
2150 Assert.AreEqual ("hello", c2[0], "#M87");
2153 char[] c1 = { 'a', 'b', 'c' };
2154 string[] c2 = new string [3];
2156 c2.SetValue (c1 [0], 0);
2157 Assert.Fail ("#M88");
2158 } catch (InvalidCastException) {}
2161 Single[] c1 = { 1.2F, 2.3F, 3.4F, 4.5F };
2162 long[] c2 = new long [3];
2164 c2.SetValue (c1 [0], 0);
2165 Assert.Fail ("#M89");
2166 } catch (ArgumentException) {}
2194 UInt32 v11 = 235354;
2195 UInt64 v12 = 234552;
2197 Object[] va1 = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12 };
2198 Object[] va2 = { "true", "1", "a", "-1.2", "-32", "-234", "-34523", "-1",
2199 "-4.8F", "24234", "235354", "234552" };
2201 Object[][] vt = { va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1 };
2204 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2205 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
2206 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
2207 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
2208 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1,
2209 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1,
2210 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,
2211 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1,
2212 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1,
2213 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
2214 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0,
2215 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0
2220 for (int i = 0; i < types.Length; i++) {
2221 for (int j = 0; j < types.Length; j++) {
2222 Array array = Array.CreateInstance (types [j], 2);
2224 Object value = vt[j][i];
2226 bool errorThrown = false;
2228 array.SetValue (value, 0);
2229 } catch (ArgumentException) {
2233 int ex_index = (i * types.Length) + j;
2235 Assert.AreEqual (errorThrown, arg_ex [ex_index] == 1, "#M90(" + types [i] + "," + types [j] + ")");
2239 for (int i = 0; i < types.Length; i++) {
2240 String[] array = new String [2];
2242 Object value = va1 [i];
2244 bool errorThrown = false;
2246 array.SetValue (value, 0);
2247 } catch (InvalidCastException) {
2251 Assert.IsTrue (errorThrown, "#M91(" + types [i] + ")");
2254 for (int i = 0; i < types.Length; i++) {
2255 Array array = Array.CreateInstance (types [i], 2);
2257 Object value = va2 [i];
2259 bool errorThrown = false;
2261 array.SetValue (value, 0);
2262 } catch (InvalidCastException) {
2266 Assert.IsTrue (errorThrown, "#M92(" + types [i] + ")");
2269 for (int i = 0; i < types.Length; i++) {
2270 Array array = Array.CreateInstance (types [i], 2);
2272 Object value = null;
2274 bool errorThrown = false;
2276 array.SetValue (value, 0);
2277 } catch (InvalidCastException) {
2281 Assert.IsTrue (!errorThrown, "#M93(" + types [i] + ")");
2286 for (int i = 0; i < types.Length; i++) {
2287 for (int j = 0; j < types.Length; j++) {
2288 Array source = Array.CreateInstance (types [i], 2);
2289 Array array = Array.CreateInstance (types [j], 2);
2291 source.SetValue (vt[j][i], 0);
2292 source.SetValue (vt[j][i], 1);
2294 bool errorThrown = false;
2296 Array.Copy (source, array, 2);
2297 } catch (ArrayTypeMismatchException) {
2301 int ex_index = (i * types.Length) + j;
2303 Assert.AreEqual (errorThrown, arg_ex [ex_index] == 1, "#M94(" + types [i] + "," + types [j] + ")");
2307 for (int i = 0; i < types.Length; i++) {
2308 Array source = Array.CreateInstance (types [i], 2);
2309 String[] array = new String [2];
2311 source.SetValue (va1 [i], 0);
2312 source.SetValue (va1 [i], 1);
2314 bool errorThrown = false;
2316 Array.Copy (source, array, 2);
2317 } catch (ArrayTypeMismatchException) {
2321 Assert.IsTrue (errorThrown, "#M95(" + types [i] + ")");
2324 for (int i = 0; i < types.Length; i++) {
2325 String[] source = new String [2];
2326 Array array = Array.CreateInstance (types [i], 2);
2328 source.SetValue (va2 [i], 0);
2329 source.SetValue (va2 [i], 1);
2331 bool errorThrown = false;
2333 Array.Copy (source, array, 2);
2334 } catch (ArrayTypeMismatchException) {
2338 Assert.IsTrue (errorThrown, "#M96(" + types [i] + ")");
2344 public void TestSort() {
2346 bool errorThrown = false;
2349 } catch (ArgumentNullException) {
2352 Assert.IsTrue (errorThrown, "#N01");
2355 bool errorThrown = false;
2357 Array.Sort(null, 0, 1);
2358 } catch (ArgumentNullException) {
2361 Assert.IsTrue (errorThrown, "#N02");
2364 bool errorThrown = false;
2366 char[] c1 = new Char[2];
2367 Array.Sort(null, c1);
2368 } catch (ArgumentNullException) {
2371 Assert.IsTrue (errorThrown, "#N03");
2374 bool errorThrown = false;
2376 char[] c1 = new Char[2];
2377 Array.Sort(null, c1, 0, 1);
2378 } catch (ArgumentNullException) {
2381 Assert.IsTrue (errorThrown, "#N04");
2385 char[] arr = {'d', 'b', 'f', 'e', 'a', 'c'};
2388 Array.Sort (null, 0, 1);
2389 Assert.Fail ("#N" + tc.ToString ());
2391 catch (ArgumentException) {}
2392 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2396 Array.Sort (arr, -1, 3);
2397 Assert.Fail ("#N" + tc.ToString ());
2399 catch (ArgumentException) {}
2400 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2404 Array.Sort (arr, 1, -3);
2405 Assert.Fail ("#N" + tc.ToString ());
2407 catch (ArgumentException) {}
2408 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2412 Array.Sort (arr, arr.Length, arr.Length + 2);
2413 Assert.Fail ("#N" + tc.ToString ());
2415 catch (ArgumentException) {}
2416 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2419 // note: null second array => just sort first array
2420 char[] starter = {'d', 'b', 'f', 'e', 'a', 'c'};
2421 int[] starter1 = {1,2,3,4,5,6};
2423 char[] c1 = (char[])starter.Clone();
2425 Assert.AreEqual ('a', c1[0], "#N21");
2426 Assert.AreEqual ('b', c1[1], "#N22");
2427 Assert.AreEqual ('c', c1[2], "#N23");
2428 Assert.AreEqual ('d', c1[3], "#N24");
2429 Assert.AreEqual ('e', c1[4], "#N25");
2430 Assert.AreEqual ('f', c1[5], "#N26");
2433 char[] c1 = (char[])starter.Clone();
2434 int[] i1 = (int[])starter1.Clone();
2436 Assert.AreEqual ('a', c1[0], "#N41");
2437 Assert.AreEqual ('b', c1[1], "#N42");
2438 Assert.AreEqual ('c', c1[2], "#N43");
2439 Assert.AreEqual ('d', c1[3], "#N44");
2440 Assert.AreEqual ('e', c1[4], "#N45");
2441 Assert.AreEqual ('f', c1[5], "#N46");
2442 Assert.AreEqual (5, i1[0], "#N47");
2443 Assert.AreEqual (2, i1[1], "#N48");
2444 Assert.AreEqual (6, i1[2], "#N49");
2445 Assert.AreEqual (1, i1[3], "#N50");
2446 Assert.AreEqual (4, i1[4], "#N51");
2447 Assert.AreEqual (3, i1[5], "#N52");
2450 char[] c1 = (char[])starter.Clone();
2451 Array.Sort(c1, 1, 4);
2452 Assert.AreEqual ('d', c1[0], "#N61");
2453 Assert.AreEqual ('a', c1[1], "#N62");
2454 Assert.AreEqual ('b', c1[2], "#N63");
2455 Assert.AreEqual ('e', c1[3], "#N64");
2456 Assert.AreEqual ('f', c1[4], "#N65");
2457 Assert.AreEqual ('c', c1[5], "#N66");
2460 char[] c1 = (char[])starter.Clone();
2461 int[] i1 = (int[])starter1.Clone();
2462 Array.Sort(c1, i1, 1, 4);
2463 Assert.AreEqual ('d', c1[0], "#N81");
2464 Assert.AreEqual ('a', c1[1], "#N82");
2465 Assert.AreEqual ('b', c1[2], "#N83");
2466 Assert.AreEqual ('e', c1[3], "#N84");
2467 Assert.AreEqual ('f', c1[4], "#N85");
2468 Assert.AreEqual ('c', c1[5], "#N86");
2469 Assert.AreEqual (1, i1[0], "#N87");
2470 Assert.AreEqual (5, i1[1], "#N88");
2471 Assert.AreEqual (2, i1[2], "#N89");
2472 Assert.AreEqual (4, i1[3], "#N90");
2473 Assert.AreEqual (3, i1[4], "#N91");
2474 Assert.AreEqual (6, i1[5], "#N92");
2479 double[] a = new double[115];
2480 int[] b = new int[256];
2481 Array.Sort<double, int> (a, b, 0, 115);
2484 /* Check that ulong[] is not sorted as long[] */
2486 string[] names = new string[] {
2487 "A", "B", "C", "D", "E"
2490 ulong[] arr = new ulong [] {
2492 unchecked((ulong)0xffffFFFF00000000),
2499 Array.Sort (a, names, null);
2500 Assert.AreEqual (0, a.GetValue (0));
2505 public void Sort_NullValues ()
2507 var s = new [] { "a", null, "b", null };
2508 Array.Sort (s, (a, b) => {
2510 return b == null ? 0 : 1;
2516 return a.CompareTo (b);
2519 Assert.AreEqual ("a", s [0], "#1");
2520 Assert.AreEqual ("b", s [1], "#2");
2521 Assert.IsNull (s [2], "#3");
2522 Assert.IsNull (s [3], "#4");
2526 public void SortNonGenericDoubleItems () {
2527 double[] doubleValues = new double[11];
2529 doubleValues[0] = 0.221788066253601;
2530 doubleValues[1] = 0.497278285809481;
2531 doubleValues[2] = 0.100565033883643;
2532 doubleValues[3] = 0.0433309347749905;
2533 doubleValues[4] = 0.00476726438463812;
2534 doubleValues[5] = 0.1354609735456;
2535 doubleValues[6] = 0.57690356588135;
2536 doubleValues[7] = 0.466239434334826;
2537 doubleValues[8] = 0.409741461978934;
2538 doubleValues[9] = 0.0112412763949565;
2539 doubleValues[10] = 0.668704347674307;
2541 int[] indices = new int[11];
2554 Array.Sort ((Array)doubleValues, (Array)indices);
2555 Assert.AreEqual (4, indices [0]);
2559 public void TestSortComparable()
2561 int[] source = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
2562 int[] expected = { 6, 5, 4, 3, 2, 1, 7, 8, 9 };
2563 Comp[] c = { new Comp (100), new Comp (16), new Comp (11), new Comp (9), new Comp (0), new Comp (-100) };
2564 IComparer<Comp> comp = null;
2565 Array.Sort<Comp, int> (c, source, comp);
2567 Assert.AreEqual (expected, source);
2570 class Comp : IComparable
2579 int IComparable.CompareTo (object obj)
2581 return val.CompareTo ((obj as Comp).val);
2586 public void TestInitializeEmpty()
2598 Assert.IsTrue (!catched, "#TI01");
2602 public void TestInitializeInt()
2607 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2609 Assert.AreEqual (a[i], b[i], "#TI02 " + i);
2614 public void TestInitializeDouble()
2616 double[] a = {1.0,2.0,0.0};
2618 double[] b = {1.0,2.0,0.0};
2619 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2621 Assert.AreEqual (a[i], b[i], "#TI03 " + i);
2626 public void TestInitializeFloat()
2628 float[] a = {1.0F,2.0F,0.0F};
2630 float[] b = {1.0F,2.0F,0.0F};
2631 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2633 Assert.AreEqual (a[i], b[i], "#TI04 " + i);
2638 public void TestInitializeChar()
2640 char[] a = {'1','.','0','F','2','.','0','F'};
2642 char[] b = {'1','.','0','F','2','.','0','F'};
2643 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2645 Assert.AreEqual (a[i], b[i], "#TI05 " + i);
2650 public void TestInitializeString()
2652 string[] a = {"hola","adios","menos","mas"};
2654 string[] b = {"hola","adios","menos","mas"};
2655 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2657 Assert.AreEqual (a[i], b[i], "#TI06 " + i);
2662 public void TestInitializeEnum()
2664 enua[] a = {enua.hola,enua.adios,enua.menos,enua.mas};
2666 enua[] b = {enua.hola,enua.adios,enua.menos,enua.mas};
2667 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2669 Assert.AreEqual (a[i], b[i], "#TI07 " + i);
2674 public void TestInitializeIntNI()
2676 int[] a = new int[20];
2680 Assert.AreEqual (b, 0, "#TI08");
2685 public void TestInitializeCharNI()
2687 char[] a = new char[20];
2689 foreach(char b in a)
2691 Assert.AreEqual (b, 0, "#TI09");
2696 public void TestInitializeDoubleNI()
2698 double[] a = new double[20];
2700 foreach(double b in a)
2702 Assert.AreEqual (b, 0.0, "#TI09");
2707 public void TestInitializeStringNI()
2709 string[] a = new string[20];
2711 foreach(string b in a)
2713 Assert.AreEqual (b, null, "#TI10");
2718 public void TestInitializeObjectNI()
2720 object[] a = new object[20];
2722 foreach(object b in a)
2724 Assert.AreEqual (b, null, "#TI11");
2729 public void TestInitializeAClassNI()
2731 AClass[] a = new AClass[20];
2733 foreach(AClass b in a)
2735 Assert.AreEqual (b, null, "#TI12");
2741 public void TestInitializeAStructNI()
2743 AStruct[] a = new AStruct[20];
2745 foreach(AStruct b in a)
2747 Assert.AreEqual (b, new AStruct(), "#TI14");
2752 public void TestInitializeAStruct()
2754 AStruct[] a = new AStruct[3];
2758 AStruct[] b = new AStruct[3];
2761 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2763 Assert.AreEqual (a[i], b[i], "#TI15 " + i);
2768 public void TestInitializeDateTimeNI()
2770 DateTime[] a = new DateTime[20];
2772 foreach(DateTime b in a)
2774 Assert.AreEqual (b, new DateTime(), "#TI16");
2779 [ExpectedException (typeof (ArgumentNullException))]
2780 public void MoreSort1 ()
2782 Array.Sort (null, 0, 1);
2786 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2787 public void MoreSort2 ()
2789 Array.Sort (arrsort, -1, 3);
2793 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2794 public void MoreSort3 ()
2796 Array.Sort (arrsort, 1, -3);
2800 [ExpectedException (typeof (ArgumentException))]
2801 public void MoreSort4 ()
2803 Array.Sort (arrsort, arrsort.Length, arrsort.Length + 2);
2807 [ExpectedException (typeof (RankException))]
2808 public void MoreSort5 ()
2810 char [,] arr = new char [,] {{'a'}, {'b'}};
2811 Array.Sort (arr, 0, 1);
2815 public void MoreSort6 ()
2817 Array.Sort (arrsort, 0, 0);
2821 [ExpectedException (typeof (ArgumentException))]
2822 public void MoreSort7 ()
2824 Array.Sort (arrsort, arrsort.Length - 1, 2);
2828 [ExpectedException (typeof (ArgumentException))]
2829 public void MoreSort8 ()
2831 Array.Sort (arrsort, 0, arrsort.Length + 1);
2835 public void MoreSort9 ()
2837 Array.Sort (arrsort, null, 0, arrsort.Length, null);
2841 [ExpectedException (typeof (InvalidOperationException))]
2842 public void MoreSort10 ()
2844 object [] array = {true, 'k', SByte.MinValue, Byte.MinValue, (short) 2, 634, (long) 436, (float) 1.1, 1.23, "Hello World"};
2845 Array.Sort (array, (IComparer) null);
2848 [Test] // bug #81941
2851 double [] a = new double [2] { 0.9, 0.3 };
2852 uint [] b = new uint [2] { 4, 7 };
2854 Assert.AreEqual (0.3, a [0], "#1");
2855 Assert.AreEqual (0.9, a [1], "#2");
2856 Assert.AreEqual (7, b [0], "#3");
2857 Assert.AreEqual (4, b [1], "#4");
2861 public void ClearJaggedArray ()
2863 byte[][] matrix = new byte [8][];
2864 for (int i=0; i < 8; i++) {
2865 matrix [i] = new byte [8];
2866 for (int j=0; j < 8; j++) {
2870 Array.Clear (matrix, 0, 8);
2871 for (int i=0; i < 8; i++) {
2872 Assert.IsNull (matrix [i], i.ToString ());
2877 public void ClearMultidimentionalArray ()
2879 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2880 Array.Clear (matrix, 0, 2);
2881 Assert.AreEqual (0, matrix [0, 0], "0,0");
2882 Assert.AreEqual (0, matrix [0, 1], "0,1");
2883 Assert.AreEqual (2, matrix [1, 0], "1,0");
2884 Assert.AreEqual (2, matrix [1, 1], "1,1");
2888 [ExpectedException (typeof (IndexOutOfRangeException))]
2889 public void ClearOutsideMultidimentionalArray ()
2891 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2892 Array.Clear (matrix, 0, 5);
2896 [ExpectedException (typeof (IndexOutOfRangeException))]
2897 public void Clear_IndexOverflow ()
2899 byte[] array = new byte [16];
2900 Array.Clear (array, 4, Int32.MaxValue);
2904 [ExpectedException (typeof (IndexOutOfRangeException))]
2905 public void Clear_LengthOverflow ()
2907 byte[] array = new byte [16];
2908 Array.Clear (array, Int32.MaxValue, 4);
2912 [ExpectedException (typeof (ArgumentException))]
2913 public void Copy_SourceIndexOverflow ()
2915 byte[] array = new byte [16];
2916 Array.Copy (array, Int32.MaxValue, array, 8, 8);
2920 [ExpectedException (typeof (ArgumentException))]
2921 public void Copy_DestinationIndexOverflow ()
2923 byte[] array = new byte [16];
2924 Array.Copy (array, 8, array, Int32.MaxValue, 8);
2928 [ExpectedException (typeof (ArgumentException))]
2929 public void Copy_LengthOverflow ()
2931 byte[] array = new byte [16];
2932 Array.Copy (array, 8, array, 8, Int32.MaxValue);
2936 [ExpectedException (typeof (ArgumentException))]
2937 public void Reverse_IndexOverflow ()
2939 byte[] array = new byte [16];
2940 Array.Reverse (array, Int32.MaxValue, 8);
2944 [ExpectedException (typeof (ArgumentException))]
2945 public void Reverse_LengthOverflow ()
2947 byte[] array = new byte [16];
2948 Array.Reverse (array, 8, Int32.MaxValue);
2951 public struct CharX : IComparable {
2954 public CharX (char c)
2959 public int CompareTo (object obj)
2962 return c.CompareTo (((CharX) obj).c);
2964 return c.CompareTo (obj);
2969 public void BinarySearch_ArgPassingOrder ()
2972 // This tests that arguments are passed to the comprer in the correct
2973 // order. The IComparable of the *array* elements must get called, not
2974 // that of the search object.
2976 CharX [] x = { new CharX ('a'), new CharX ('b'), new CharX ('c') };
2977 Assert.AreEqual (1, Array.BinarySearch (x, 'b'));
2980 class Comparer: IComparer {
2982 private bool called = false;
2984 public bool Called {
2986 bool result = called;
2992 public int Compare (object x, object y)
3000 public void BinarySearch1_EmptyList ()
3002 int[] array = new int[0];
3003 Assert.AreEqual (- 1, Array.BinarySearch (array, 0), "BinarySearch");
3007 public void BinarySearch2_EmptyList ()
3009 int[] array = new int[0];
3010 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, 0), "BinarySearch");
3014 public void BinarySearch3_EmptyList ()
3016 Comparer comparer = new Comparer ();
3017 int[] array = new int[0];
3018 Assert.AreEqual (-1, Array.BinarySearch (array, 0, comparer), "BinarySearch");
3019 // bug 77030 - the comparer isn't called for an empty array/list
3020 Assert.IsTrue (!comparer.Called, "Called");
3024 public void BinarySearch4_EmptyList ()
3026 Comparer comparer = new Comparer ();
3027 int[] array = new int[0];
3028 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, comparer), "BinarySearch");
3029 // bug 77030 - the comparer isn't called for an empty array/list
3030 Assert.IsTrue (!comparer.Called, "Called");
3034 [ExpectedException (typeof (ArgumentNullException))]
3035 public void AsReadOnly_NullArray ()
3037 Array.AsReadOnly <int> (null);
3041 public void ReadOnly_Count ()
3043 Assert.AreEqual (10, Array.AsReadOnly (new int [10]).Count);
3047 public void ReadOnly_Contains ()
3049 int[] arr = new int [2];
3052 IList<int> a = Array.AsReadOnly (arr);
3054 Assert.IsTrue (a.Contains (3));
3055 Assert.IsTrue (!a.Contains (6));
3059 public void ReadOnly_IndexOf ()
3061 int[] arr = new int [2];
3064 IList<int> a = Array.AsReadOnly (arr);
3066 Assert.AreEqual (0, a.IndexOf (3));
3067 Assert.AreEqual (1, a.IndexOf (5));
3068 Assert.AreEqual (-1, a.IndexOf (6));
3072 public void ReadOnly_Indexer ()
3074 int[] arr = new int [2];
3077 IList<int> a = Array.AsReadOnly (arr);
3079 Assert.AreEqual (3, a [0]);
3080 Assert.AreEqual (5, a [1]);
3082 /* Check that modifications to the original array are visible */
3084 Assert.AreEqual (6, a [0]);
3088 public void ReadOnly_Enumerator ()
3090 int[] arr = new int [10];
3092 for (int i = 0; i < 10; ++i)
3096 foreach (int i in Array.AsReadOnly (arr))
3099 Assert.AreEqual (45, sum);
3103 public void ReadOnly_CopyTo ()
3105 int[] arr = new int [2];
3108 IList<int> a = Array.AsReadOnly (arr);
3110 int[] arr2 = new int [3];
3113 Assert.AreEqual (0, arr2 [0]);
3114 Assert.AreEqual (3, arr2 [1]);
3115 Assert.AreEqual (5, arr2 [2]);
3119 public void Resize ()
3121 int [] arr = new int [] { 1, 3, 5 };
3122 Array.Resize <int> (ref arr, 3);
3123 Assert.AreEqual (3, arr.Length, "#A1");
3124 Assert.AreEqual (1, arr [0], "#A2");
3125 Assert.AreEqual (3, arr [1], "#A3");
3126 Assert.AreEqual (5, arr [2], "#A4");
3128 Array.Resize <int> (ref arr, 2);
3129 Assert.AreEqual (2, arr.Length, "#B1");
3130 Assert.AreEqual (1, arr [0], "#B2");
3131 Assert.AreEqual (3, arr [1], "#B3");
3133 Array.Resize <int> (ref arr, 4);
3134 Assert.AreEqual (4, arr.Length, "#C1");
3135 Assert.AreEqual (1, arr [0], "#C2");
3136 Assert.AreEqual (3, arr [1], "#C3");
3137 Assert.AreEqual (0, arr [2], "#C4");
3138 Assert.AreEqual (0, arr [3], "#C5");
3142 public void Resize_null ()
3145 Array.Resize (ref arr, 10);
3146 Assert.AreEqual (arr.Length, 10);
3150 public void Test_ContainsAndIndexOf_EquatableItem ()
3152 EquatableClass[] list = new EquatableClass[] {new EquatableClass (0), new EquatableClass (1), new EquatableClass (0)};
3154 Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, list[0]), "#0");
3155 Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, new EquatableClass (0)), "#1");
3156 Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, list[0]), "#2");
3157 Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, new EquatableClass (0)), "#3");
3160 public class EquatableClass : IEquatable<EquatableClass>
3163 public EquatableClass (int x)
3168 public bool Equals (EquatableClass other)
3170 return this._x == other._x;
3175 public void AsIList ()
3177 IList<int> arr = new int [10];
3179 Assert.AreEqual (5, arr [0]);
3181 IList<FooStruct> arr2 = new FooStruct [10];
3182 FooStruct s = new FooStruct ();
3187 Assert.AreEqual (11, s.i);
3188 Assert.AreEqual (22, s.j);
3190 IList<string> arr3 = new string [10];
3192 Assert.AreEqual ("ABC", arr3 [5]);
3201 public void ICollectionNull ()
3203 ICollection<object> test;
3205 test = new List<object>();
3206 Assert.AreEqual (test.Contains (null), false, "list<o>");
3208 test = new object[] {};
3209 Assert.AreEqual (test.Contains (null), false, "empty array");
3211 test = new object[] {null};
3212 Assert.AreEqual (test.Contains (null), true, "array with null");
3214 test = new object[] { 1, null};
3215 Assert.IsTrue (test.Contains (null), "array with last null");
3217 test = new List<object>(test);
3218 Assert.AreEqual (test.Contains (null), true, "List<object> with test");
3220 test = new object[] {new object()};
3221 Assert.AreEqual (test.Contains (null), false, "array with object");
3223 test = new List<object>(test);
3224 Assert.AreEqual (test.Contains (null), false, "array with test");
3228 public void IListNull ()
3232 test = new List<object>();
3233 Assert.AreEqual (-1, test.IndexOf (null), "list<o>");
3235 test = new object[] {};
3236 Assert.AreEqual (-1, test.IndexOf (null), "empty array");
3238 test = new object[] {null};
3239 Assert.AreEqual (0, test.IndexOf (null), "array with null");
3241 test = new object[] { 1, null};
3242 Assert.AreEqual (1, test.IndexOf (null), "array with last null");
3244 test = new List<object>(test);
3245 Assert.AreEqual (1, test.IndexOf (null), "List<object> with test");
3247 test = new object[] {new object()};
3248 Assert.AreEqual (-1, test.IndexOf (null), "array with object");
3250 test = new List<object>(test);
3251 Assert.AreEqual (-1, test.IndexOf (null), "array with test");
3257 enum ByteEnum : byte {}
3258 enum IntEnum : int {}
3261 public void TestByteEnumArrayToByteArray ()
3263 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
3264 byte[] b = new byte[a.Length];
3269 public void TestByteEnumArrayToIntArray ()
3271 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
3272 int[] b = new int[a.Length];
3277 [ExpectedException (typeof (ArrayTypeMismatchException))]
3278 public void TestIntEnumArrayToByteArray ()
3280 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
3281 byte[] b = new byte[a.Length];
3286 public void TestIntEnumArrayToIntArray ()
3288 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
3289 int[] b = new int[a.Length];
3295 [Test] // bug #322248
3296 public void IEnumerator_Reset ()
3298 int[] array = new int[] { 1, 2, 3};
3299 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3300 Assert.IsTrue (e.MoveNext (), "#A1");
3301 Assert.AreEqual (1, e.Current, "#A2");
3302 Assert.IsTrue (e.MoveNext (), "#A3");
3303 Assert.AreEqual (2, e.Current, "#A4");
3307 Assert.IsTrue (e.MoveNext (), "#C1");
3308 Assert.AreEqual (1, e.Current, "#C2");
3312 public void IEnumerator_Current_Finished ()
3314 int[] array = new int[] { 1, 2, 3 };
3315 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3316 Assert.IsTrue (e.MoveNext (), "#A1");
3317 Assert.AreEqual (1, e.Current, "#A2");
3318 Assert.IsTrue (e.MoveNext (), "#A3");
3319 Assert.AreEqual (2, e.Current, "#A4");
3320 Assert.IsTrue (e.MoveNext (), "#A5");
3321 Assert.AreEqual (3, e.Current, "#A6");
3322 Assert.IsTrue (!e.MoveNext (), "#A6");
3325 Assert.Fail ("#B1:" + e.Current);
3326 } catch (InvalidOperationException ex) {
3327 // Enumeration already finished
3328 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3329 Assert.IsNull (ex.InnerException, "#B3");
3330 Assert.IsNotNull (ex.Message, "#B4");
3335 public void IEnumerator_Current_NotStarted ()
3337 int[] array = new int[] { 1, 2, 3 };
3338 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3341 Assert.Fail ("#A1:" + e.Current);
3342 } catch (InvalidOperationException ex) {
3343 // Enumeration has not started. Call MoveNext
3344 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
3345 Assert.IsNull (ex.InnerException, "#A3");
3346 Assert.IsNotNull (ex.Message, "#A4");
3351 public void IEnumerator_Current_Reset ()
3353 int[] array = new int[] { 1, 2, 3 };
3354 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3359 Assert.Fail ("#B1:" + e.Current);
3360 } catch (InvalidOperationException ex) {
3361 // Enumeration has not started. Call MoveNext
3362 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3363 Assert.IsNull (ex.InnerException, "#B3");
3364 Assert.IsNotNull (ex.Message, "#B4");
3368 public void ICollection_IsReadOnly() {
3369 ICollection<string> arr = new string [10];
3371 Assert.IsTrue (arr.IsReadOnly);
3375 [ExpectedException (typeof (NotSupportedException))]
3376 public void ArrayCreateInstanceOfVoid ()
3378 Array.CreateInstance (typeof (void), 42);
3384 [ExpectedException (typeof (NotSupportedException))]
3385 public void ArrayCreateInstanceOfOpenGenericType ()
3387 Array.CreateInstance (typeof (Foo<>), 42);
3391 [ExpectedException (typeof (IndexOutOfRangeException))]
3392 public void ClearNegativeLength ()
3394 Array.Clear (new int [] { 1, 2 }, 0, -1);
3398 [ExpectedException (typeof (ArgumentException))]
3399 public void MultiDimension_IList_setItem ()
3401 IList array = new int [1, 1];
3406 [ExpectedException (typeof (ArgumentException))]
3407 public void MultiDimension_IList_getItem ()
3409 IList array = new int [1, 1];
3410 int a = (int) array [0];
3414 public void SetValue_Nullable () {
3415 Array array = Array.CreateInstance (typeof (int?), 7);
3419 array.SetValue (o, 0);
3420 Assert.AreEqual (42, array.GetValue (0));
3422 array.SetValue (null, 0);
3423 Assert.AreEqual (null, array.GetValue (0));
3427 public void SortNullsWithGenericVersion ()
3429 string[] s1 = new string[6]{
3437 string[] s2 = new string[]{null,
3444 Array.Sort<string> (s1);
3445 for (int i = 0; i < 6; i++) {
3446 Assert.AreEqual (s1[i], s2[i], "At:" + i);
3451 // This is a test case for the case that was broken by the code contributed
3454 // This tests the fix for: #622101
3457 public void SortActuallyWorks ()
3459 string[] data = new string[9]{"Foo", "Bar", "Dingus", null, "Dingu4", "123", "Iam", null, "NotNull"};
3460 IComparer comparer = new NullAtEndComparer ();
3461 Array.Sort (data, comparer);
3463 Assert.AreEqual (data [7], null);
3464 Assert.AreNotEqual (data [0], null);
3467 class NullAtEndComparer : IComparer {
3468 public int Compare(object x, object y)
3470 if (x == null) return 1;
3471 if (y == null) return -1;
3472 return ((string)x).CompareTo((string)y);
3477 public void UnalignedArrayClear ()
3479 byte[] input = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
3480 byte[] expected = new byte[] { 1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
3481 Array.Clear (input, 5, 11);
3483 Assert.AreEqual (input, expected);
3487 [ExpectedException (typeof (ArgumentException))]
3488 public void CompareToWithJaggedArray () {
3489 IStructuralComparable a = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }};
3490 IStructuralComparable b = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }};
3491 a.CompareTo (b, Comparer<object>.Default);
3495 [ExpectedException (typeof (ArgumentException))]
3496 public void CompareToWithArrayOfTheWrongKind () {
3497 IStructuralComparable a = new int[] { 1, 2 };
3498 IStructuralComparable b = new double[] { 1, 2 };
3499 a.CompareTo (b, Comparer<object>.Default);
3503 [ExpectedException (typeof (ArgumentException))]
3504 public void CompareToWithNonArrayType () {
3505 IStructuralComparable a = new int[] { 1, 2 };
3506 a.CompareTo (99, Comparer<object>.Default);
3510 [ExpectedException (typeof (ArgumentException))]
3511 public void CompareToWithNonArrayOfDifferentSize () {
3512 IStructuralComparable a = new int[] { 1, 2 };
3513 IStructuralComparable b = new int[] { 1, 2, 3 };
3514 a.CompareTo (b, Comparer<object>.Default);
3518 [ExpectedException (typeof (ArgumentException))]
3519 public void CompareToWithMultiDimArray1 () {
3520 IStructuralComparable a = new int [2,2] { {10, 10 }, { 10, 10 } };
3521 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3522 a.CompareTo (b, Comparer<object>.Default);
3526 [ExpectedException (typeof (ArgumentException))]
3527 public void CompareToWithMultiDimArray2 () {
3528 IStructuralComparable a = new int [2] { 10, 10 };
3529 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3530 a.CompareTo (b, Comparer<object>.Default);
3534 [ExpectedException (typeof (ArgumentException))]
3535 public void CompareToWithMultiDimArray3 () {
3536 IStructuralComparable a = new int [4] { 10, 10, 10, 10 };
3537 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3538 a.CompareTo (b, Comparer<object>.Default);
3542 [ExpectedException (typeof (IndexOutOfRangeException))]
3543 public void CompareToWithBoundedArray1 () {
3544 IStructuralComparable a = new int [2] { 10, 10 };
3545 Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 });
3546 IStructuralComparable b = ab;
3547 ab.SetValue (10, 5);
3548 ab.SetValue (10, 6);
3550 a.CompareTo (b, Comparer<object>.Default);
3554 [ExpectedException (typeof (IndexOutOfRangeException))]
3555 public void CompareToWithBoundedArray2 () {
3556 IStructuralComparable a = new int [2] { 10, 10 };
3557 Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 });
3558 IStructuralComparable b = ab;
3559 ab.SetValue (10, 5);
3560 ab.SetValue (10, 6);
3562 //Yes, CompareTo simply doesn't work with bounded arrays!
3563 b.CompareTo (b, Comparer<object>.Default);
3567 [ExpectedException (typeof (NullReferenceException))]
3568 public void CompareToWithNullComparer () {
3569 IStructuralComparable a = new int[] { 1, 2 };
3570 IStructuralComparable b = new int[] { 1, 2 };
3571 a.CompareTo (b, null);
3575 public void CompareToWithNullArray () {
3576 IStructuralComparable a = new int[] { 1, 2 };
3577 Assert.AreEqual (1, a.CompareTo (null, Comparer<object>.Default));
3581 public void CompareToWithGoodArrays () {
3582 IStructuralComparable a = new int[] { 10, 20 };
3583 Assert.AreEqual (0, a.CompareTo (a, Comparer<object>.Default));
3584 Assert.AreEqual (0, a.CompareTo (new int [] { 10, 20 }, Comparer<object>.Default));
3585 Assert.AreEqual (-1, a.CompareTo (new int [] { 11, 20 }, Comparer<object>.Default));
3586 Assert.AreEqual (-1, a.CompareTo (new int [] { 10, 21 }, Comparer<object>.Default));
3587 Assert.AreEqual (1, a.CompareTo (new int [] { 9, 20 }, Comparer<object>.Default));
3588 Assert.AreEqual (1, a.CompareTo (new int [] { 10, 19 }, Comparer<object>.Default));
3592 public void IStructuralEquatable_Equals ()
3594 IStructuralEquatable array = new int[] {1, 2, 3};
3595 IStructuralEquatable array2 = new int[] {1, 2, 3};
3596 Assert.AreEqual (false, array.Equals (null, null));
3597 Assert.AreEqual (true, array.Equals (array, null));
3598 Assert.AreEqual (true, array.Equals (array2, EqualityComparer<int>.Default));
3602 [ExpectedException (typeof (NullReferenceException))]
3603 public void IStructuralEquatable_Equals_NoComparer ()
3605 IStructuralEquatable array = new int[] {1, 2, 3};
3606 IStructuralComparable array2 = new int[] {1, 2, 3};
3607 array.Equals (array2, null);
3611 [ExpectedException (typeof (ArgumentException))]
3612 public void IStructuralEquatable_Equals_ComparerThrows ()
3614 IStructuralEquatable array = new int[] {1, 2, 3};
3615 IStructuralComparable array2 = new int[] {1, 2, 3};
3616 array.Equals (array2, EqualityComparer<long>.Default);
3620 [ExpectedException (typeof (ArgumentNullException))]
3621 public void IStructuralEquatable_GetHashCode_NullComparer ()
3623 IStructuralEquatable a = new int[] { 1, 2 };
3624 a.GetHashCode (null);
3627 class TestComparer_GetHashCode : IEqualityComparer
3631 bool IEqualityComparer.Equals (object x, object y)
3633 throw new NotImplementedException ();
3636 public int GetHashCode (object obj)
3643 public void IStructuralEquatable_GetHashCode ()
3645 IStructuralEquatable a = new int[] { 1, 2, 9 };
3647 var c = new TestComparer_GetHashCode ();
3649 Assert.AreEqual (3, c.Counter);
3654 public void JaggedArrayCtor ()
3656 var type = Type.GetType ("System.Object[][]");
3658 ConstructorInfo ctor = null;
3659 foreach (var c in type.GetConstructors ()) {
3660 if (c.GetParameters ().Length == 2)
3663 Assert.IsNotNull (ctor);
3664 var arr = (object[])ctor.Invoke (new object [] { 4, 10 });
3665 for (int i = 0; i < 4; ++i) {
3666 Assert.IsNotNull (arr [i]);
3667 Assert.AreEqual (10, ((object[])arr [i]).Length);