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 TestGetEnumerator() {
784 String[] s1 = {"this", "is", "a", "test"};
785 IEnumerator en = s1.GetEnumerator ();
786 Assert.IsNotNull (en, "#G01");
788 Assert.IsTrue (en.MoveNext (), "#G02");
789 Assert.AreEqual ("this", en.Current, "#G03");
790 Assert.IsTrue (en.MoveNext (), "#G04");
791 Assert.AreEqual ("is", en.Current, "#G05");
792 Assert.IsTrue (en.MoveNext (), "#G06");
793 Assert.AreEqual ("a", en.Current, "#G07");
794 Assert.IsTrue (en.MoveNext (), "#G08");
795 Assert.AreEqual ("test", en.Current, "#G09");
796 Assert.IsTrue (!en.MoveNext (), "#G10");
799 Assert.IsTrue (en.MoveNext (), "#G11");
800 Assert.AreEqual ("this", en.Current, "#G12");
802 // mutation does not invalidate array enumerator!
803 s1.SetValue ("change", 1);
804 Assert.IsTrue (en.MoveNext (), "#G13");
805 Assert.AreEqual ("change", en.Current, "#G14");
809 public void TestGetEnumeratorMultipleDimension() {
810 String[,] s1 = {{"this", "is"}, {"a", "test"}};
811 IEnumerator en = s1.GetEnumerator ();
812 Assert.IsNotNull (en, "#AA01");
814 Assert.IsTrue (en.MoveNext (), "#AA02");
815 Assert.AreEqual ("this", en.Current, "#AA03");
816 Assert.IsTrue (en.MoveNext (), "#AA04");
817 Assert.AreEqual ("is", en.Current, "#AA05");
818 Assert.IsTrue (en.MoveNext (), "#AA06");
819 Assert.AreEqual ("a", en.Current, "#AA07");
820 Assert.IsTrue (en.MoveNext (), "#AA08");
821 Assert.AreEqual ("test", en.Current, "#AA09");
822 Assert.IsTrue (!en.MoveNext (), "#AA10");
825 Assert.IsTrue (en.MoveNext (), "#AA11");
826 Assert.AreEqual ("this", en.Current, "#AA12");
829 // mutation does not invalidate array enumerator!
830 s1.SetValue ("change", idxs);
831 Assert.IsTrue (en.MoveNext (), "#AA13");
832 Assert.AreEqual ("change", en.Current, "#AA14");
836 public void TestGetEnumeratorNonZeroLowerBounds() {
837 int[] myLengthsArray = new int[2] { 3, 5 };
838 int[] myBoundsArray = new int[2] { 2, 3 };
840 Array myArray=Array.CreateInstance( typeof(String), myLengthsArray, myBoundsArray );
841 for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
842 for ( int j = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++ ) {
843 int[] myIndicesArray = new int[2] { i, j };
844 myArray.SetValue( Convert.ToString(i) + j, myIndicesArray );
846 IEnumerator en = myArray.GetEnumerator ();
847 Assert.IsNotNull (en, "#AB01");
849 // check the first couple of values
850 Assert.IsTrue (en.MoveNext (), "#AB02");
851 Assert.AreEqual ("23", en.Current, "#AB03");
852 Assert.IsTrue (en.MoveNext (), "#AB04");
853 Assert.AreEqual ("24", en.Current, "#AB05");
855 // then check the last element's value
858 lastElement = (string)en.Current;
859 } while (en.MoveNext());
860 Assert.AreEqual ("47", lastElement, "#AB06");
864 public void TestIList_Add () {
865 int[] myLengthsArray = new int[2] { 3, 5 };
866 int[] myBoundsArray = new int[2] { 2, 3 };
868 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
870 ((IList)myArray).Add ("can not");
871 Assert.Fail ("IList.Add should throw");
873 catch (NotSupportedException) {
877 Assert.Fail ("IList.Add threw wrong exception type");
880 Assert.Fail ("IList.Add shouldn't get this far");
884 public void TestIList_Insert () {
885 int[] myLengthsArray = new int[2] { 3, 5 };
886 int[] myBoundsArray = new int[2] { 2, 3 };
888 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
890 ((IList)myArray).Insert (0, "can not");
891 Assert.Fail ("IList.Insert should throw");
893 catch (NotSupportedException) {
897 Assert.Fail ("IList.Insert threw wrong exception type");
900 Assert.Fail ("IList.Insert shouldn't get this far");
904 public void TestIList_Remove () {
905 int[] myLengthsArray = new int[2] { 3, 5 };
906 int[] myBoundsArray = new int[2] { 2, 3 };
908 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
910 ((IList)myArray).Remove ("can not");
911 Assert.Fail ("IList.Remove should throw");
913 catch (NotSupportedException) {
917 Assert.Fail ("IList.Remove threw wrong exception type");
920 Assert.Fail ("IList.Remove shouldn't get this far");
924 public void TestIList_RemoveAt () {
925 int[] myLengthsArray = new int[2] { 3, 5 };
926 int[] myBoundsArray = new int[2] { 2, 3 };
928 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
930 ((IList)myArray).RemoveAt (0);
931 Assert.Fail ("IList.RemoveAt should throw");
933 catch (NotSupportedException) {
937 Assert.Fail ("IList.RemoveAt threw wrong exception type");
940 Assert.Fail ("IList.RemoveAt shouldn't get this far");
944 public void TestIList_Contains () {
945 int[] myLengthsArray = new int[2] { 3, 5 };
946 int[] myBoundsArray = new int[2] { 2, 3 };
948 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
951 bool b = ((IList)myArray).Contains ("23");
952 Assert.Fail ("IList.Contains should throw with multi-dimensional arrays");
954 catch (RankException) {
955 int[] iArr = new int[3] { 1, 2, 3};
956 // check the first and last items
957 Assert.IsTrue (((IList)iArr).Contains (1), "AC01");
958 Assert.IsTrue (((IList)iArr).Contains (3), "AC02");
960 // and one that is definately not there
961 Assert.IsTrue (!((IList)iArr).Contains (42), "AC03");
965 Assert.Fail ("Should not get here");
969 public void TestIList_IndexOf () {
970 int[] myLengthsArray = new int[2] { 3, 5 };
971 int[] myBoundsArray = new int[2] { 2, 3 };
973 Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
976 bool b = ((IList)myArray).Contains ("23");
977 Assert.Fail ("IList.Contains should throw with multi-dimensional arrays");
979 catch (RankException) {
980 int[] iArr = new int[3] { 1, 2, 3};
981 // check the first and last items
982 Assert.AreEqual (0, ((IList)iArr).IndexOf (1), "AD01");
983 Assert.AreEqual (2, ((IList)iArr).IndexOf (3), "AD02");
985 // and one that is definately not there
986 Assert.AreEqual (-1, ((IList)iArr).IndexOf (42), "AD03");
988 catch (Exception e) {
989 Assert.Fail ("Unexpected exception: " + e.ToString());
992 // check that wierd case whem lowerbound is Int32.MinValue,
993 // so that IndexOf() needs to return Int32.MaxValue when it cannot find the object
994 int[] myLengthArray = new int[1] { 3 };
995 int[] myBoundArray = new int[1] { Int32.MinValue };
996 Array myExtremeArray=Array.CreateInstance ( typeof(String), myLengthArray, myBoundArray );
997 Assert.AreEqual (Int32.MaxValue, ((IList)myExtremeArray).IndexOf (42), "AD04");
1002 public void TestGetLength() {
1004 bool errorThrown = false;
1006 char[] c1 = {'a', 'b', 'c'};
1008 } catch (IndexOutOfRangeException) {
1011 Assert.IsTrue (errorThrown, "#H01");
1014 bool errorThrown = false;
1016 char[] c1 = {'a', 'b', 'c'};
1018 } catch (IndexOutOfRangeException) {
1021 Assert.IsTrue (errorThrown, "#H02");
1024 char[] c2 = new Char[5];
1025 Assert.AreEqual (5, c2.GetLength(0), "#H03");
1027 char[,] c3 = new Char[6,7];
1028 Assert.AreEqual (6, c3.GetLength(0), "#H04");
1029 Assert.AreEqual (7, c3.GetLength(1), "#H05");
1033 public void TestGetLowerBound() {
1035 bool errorThrown = false;
1037 char[] c = {'a', 'b', 'c'};
1038 c.GetLowerBound(-1);
1039 } catch (IndexOutOfRangeException) {
1042 Assert.IsTrue (errorThrown, "#H31");
1045 bool errorThrown = false;
1047 char[] c = {'a', 'b', 'c'};
1049 } catch (IndexOutOfRangeException) {
1052 Assert.IsTrue (errorThrown, "#H32");
1055 char[] c1 = new Char[5];
1056 Assert.AreEqual (0, c1.GetLowerBound(0), "#H33");
1058 char[,] c2 = new Char[4,4];
1059 Assert.AreEqual (0, c2.GetLowerBound(0), "#H34");
1060 Assert.AreEqual (0, c2.GetLowerBound(1), "#H35");
1064 public void TestGetUpperBound() {
1066 bool errorThrown = false;
1068 char[] c = {'a', 'b', 'c'};
1069 c.GetUpperBound(-1);
1070 } catch (IndexOutOfRangeException) {
1073 Assert.IsTrue (errorThrown, "#H61");
1076 bool errorThrown = false;
1078 char[] c = {'a', 'b', 'c'};
1080 } catch (IndexOutOfRangeException) {
1083 Assert.IsTrue (errorThrown, "#H62");
1086 char[] c1 = new Char[5];
1087 Assert.AreEqual (4, c1.GetUpperBound(0), "#H63");
1089 char[,] c2 = new Char[4,6];
1090 Assert.AreEqual (3, c2.GetUpperBound(0), "#H64");
1091 Assert.AreEqual (5, c2.GetUpperBound(1), "#H65");
1095 public void TestGetValue1() {
1097 bool errorThrown = false;
1099 char[,] c = new Char[2,2];
1101 } catch (ArgumentException) {
1104 Assert.IsTrue (errorThrown, "#I01");
1107 bool errorThrown = false;
1109 char[] c = {'a', 'b', 'c'};
1111 } catch (IndexOutOfRangeException) {
1114 Assert.IsTrue (errorThrown, "#I02");
1117 bool errorThrown = false;
1119 char[] c = {'a', 'b', 'c'};
1121 } catch (IndexOutOfRangeException) {
1124 Assert.IsTrue (errorThrown, "#I03");
1127 char[] c1 = {'a', 'b', 'c', 'd'};
1128 for (int i = 0; i < c1.Length; i++) {
1129 Assert.AreEqual (c1[i], c1.GetValue(i), "#I04(" + i + ")");
1134 public void TestGetValue2() {
1136 bool errorThrown = false;
1138 char[] c = new Char[2];
1140 } catch (ArgumentException) {
1143 Assert.IsTrue (errorThrown, "#I21");
1146 bool errorThrown = false;
1148 char[,] c = new Char[2,2];
1150 } catch (IndexOutOfRangeException) {
1153 Assert.IsTrue (errorThrown, "#I22");
1156 bool errorThrown = false;
1158 char[,] c = new Char[2,2];
1160 } catch (IndexOutOfRangeException) {
1163 Assert.IsTrue (errorThrown, "#I23");
1166 char[,] c1 = new Char[4,6];
1167 for (int i = 0; i < 24; i++) {
1170 c1[first,second] = (char)(((int)'a')+i);
1172 for (int i = 0; i < c1.GetLength(0); i++) {
1173 for (int j = 0; j < c1.GetLength(1); j++) {
1174 Assert.AreEqual (c1[i, j], c1.GetValue(i, j), "#I24(" + i + "," + j + ")");
1180 public void TestGetValue3() {
1182 bool errorThrown = false;
1184 char[] c = new Char[2];
1186 } catch (ArgumentException) {
1189 Assert.IsTrue (errorThrown, "#I41");
1192 bool errorThrown = false;
1194 char[,,] c = new Char[2,2,2];
1195 c.GetValue(-1, 1, 1);
1196 } catch (IndexOutOfRangeException) {
1199 Assert.IsTrue (errorThrown, "#I42");
1202 bool errorThrown = false;
1204 char[,,] c = new Char[2,2,2];
1206 } catch (IndexOutOfRangeException) {
1209 Assert.IsTrue (errorThrown, "#I43");
1212 char[,,] c1 = new Char[4,2,3];
1213 for (int i = 0; i < 24; i++) {
1215 int remains = i % 6;
1216 int second = remains / 3;
1217 int third = remains % 3;
1218 c1[first,second, third] = (char)(((int)'a')+i);
1220 for (int i = 0; i < c1.GetLength(0); i++) {
1221 for (int j = 0; j < c1.GetLength(1); j++) {
1222 for (int k = 0; k < c1.GetLength(2); k++) {
1223 Assert.AreEqual (c1[i, j, k], c1.GetValue(i, j, k), "#I44(" + i + "," + j + ")");
1230 [ExpectedException (typeof (ArgumentNullException))]
1231 public void TestGetValueLongArray ()
1233 char[] c = new Char[2];
1234 c.GetValue((long [])null);
1238 public void TestGetValueN() {
1240 bool errorThrown = false;
1242 char[] c = new Char[2];
1243 c.GetValue((int [])null);
1244 } catch (ArgumentNullException) {
1247 Assert.IsTrue (errorThrown, "#I61a");
1250 bool errorThrown = false;
1252 char[] c = new Char[2];
1253 int[] coords = {1, 1};
1255 } catch (ArgumentException) {
1258 Assert.IsTrue (errorThrown, "#I62");
1261 bool errorThrown = false;
1263 char[,] c = new Char[2,2];
1264 int[] coords = {-1, 1};
1266 } catch (IndexOutOfRangeException) {
1269 Assert.IsTrue (errorThrown, "#I63");
1272 bool errorThrown = false;
1274 char[,] c = new Char[2,2];
1275 int[] coords = {4, 1};
1277 } catch (IndexOutOfRangeException) {
1280 Assert.IsTrue (errorThrown, "#I64");
1283 char[,] c1 = new Char[4,6];
1284 for (int i = 0; i < 24; i++) {
1287 c1[first,second] = (char)(((int)'a')+i);
1289 for (int i = 0; i < c1.GetLength(0); i++) {
1290 for (int j = 0; j < c1.GetLength(1); j++) {
1291 int[] coords = {i, j};
1292 Assert.AreEqual (c1[i, j], c1.GetValue(coords), "#I65(" + i + "," + j + ")");
1298 public void TestIndexOf1() {
1300 bool errorThrown = false;
1302 Array.IndexOf(null, "huh?");
1303 } catch (ArgumentNullException) {
1306 Assert.IsTrue (errorThrown, "#J01");
1309 bool errorThrown = false;
1311 char[,] c = new Char[2,2];
1312 Array.IndexOf(c, "huh?");
1313 } catch (RankException) {
1316 Assert.IsTrue (errorThrown, "#J02");
1319 String[] s1 = {"this", "is", "a", "test"};
1320 Assert.AreEqual (-1, Array.IndexOf(s1, null), "#J03");
1321 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing"), "#J04");
1322 Assert.AreEqual (0, Array.IndexOf(s1, "this"), "#J05");
1323 Assert.AreEqual (3, Array.IndexOf(s1, "test"), "#J06");
1327 public void TestIndexOf2() {
1329 bool errorThrown = false;
1331 Array.IndexOf(null, "huh?", 0);
1332 } catch (ArgumentNullException) {
1335 Assert.IsTrue (errorThrown, "#J21");
1338 bool errorThrown = false;
1340 char[,] c = new Char[2,2];
1341 Array.IndexOf(c, "huh?", 0);
1342 } catch (RankException) {
1345 Assert.IsTrue (errorThrown, "#J22");
1348 bool errorThrown = false;
1350 char[] c = new Char[2];
1351 Array.IndexOf(c, "huh?", 3);
1352 } catch (ArgumentOutOfRangeException) {
1355 Assert.IsTrue (errorThrown, "#J23");
1358 String[] s1 = {"this", "is", "really", "a", "test"};
1359 Assert.AreEqual (-1, Array.IndexOf(s1, null, 1), "#J24");
1360 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing", 1), "#J25");
1361 Assert.AreEqual (-1, Array.IndexOf(s1, "this", 1), "#J26");
1362 Assert.AreEqual (1, Array.IndexOf(s1, "is", 1), "#J27");
1363 Assert.AreEqual (4, Array.IndexOf(s1, "test", 1), "#J28");
1367 public void TestIndexOf3() {
1369 bool errorThrown = false;
1371 Array.IndexOf(null, "huh?", 0, 1);
1372 } catch (ArgumentNullException) {
1375 Assert.IsTrue (errorThrown, "#J41");
1378 bool errorThrown = false;
1380 char[,] c = new Char[2,2];
1381 Array.IndexOf(c, "huh?", 0, 1);
1382 } catch (RankException) {
1385 Assert.IsTrue (errorThrown, "#J42");
1388 bool errorThrown = false;
1390 char[] c = new Char[2];
1391 Array.IndexOf(c, "huh?", 3, 1);
1392 } catch (ArgumentOutOfRangeException) {
1395 Assert.IsTrue (errorThrown, "#J43");
1398 bool errorThrown = false;
1400 char[] c = new Char[2];
1401 Array.IndexOf(c, "huh?", 0, 5);
1402 } catch (ArgumentOutOfRangeException) {
1405 Assert.IsTrue (errorThrown, "#J44");
1408 String[] s1 = {"this", "is", "really", "a", "test"};
1409 Assert.AreEqual (-1, Array.IndexOf(s1, null, 1, 3), "#J45");
1410 Assert.AreEqual (-1, Array.IndexOf(s1, "nothing", 1, 3), "#J46");
1411 Assert.AreEqual (-1, Array.IndexOf(s1, "this", 1, 3), "#J47");
1412 Assert.AreEqual (1, Array.IndexOf(s1, "is", 1, 3), "#J48");
1413 Assert.AreEqual (-1, Array.IndexOf(s1, "test", 1, 3), "#J49");
1414 Assert.AreEqual (3, Array.IndexOf(s1, "a", 1, 3), "#J50");
1418 public void TestIndexOf_CustomEqual ()
1420 DataEqual[] test = new DataEqual [] { new DataEqual () };
1421 Assert.AreEqual (0, Array.IndexOf (test, "asdfas", 0));
1423 IList array = (IList)test;
1424 Assert.AreEqual (0, array.IndexOf ("asdfas"));
1428 public void TestLastIndexOf1() {
1430 bool errorThrown = false;
1432 Array.LastIndexOf(null, "huh?");
1433 } catch (ArgumentNullException) {
1436 Assert.IsTrue (errorThrown, "#K01");
1439 bool errorThrown = false;
1441 char[,] c = new Char[2,2];
1442 Array.LastIndexOf(c, "huh?");
1443 } catch (RankException) {
1446 Assert.IsTrue (errorThrown, "#K02");
1449 String[] s1 = {"this", "is", "a", "a", "test"};
1450 Assert.AreEqual (-1, Array.LastIndexOf(s1, null), "#K03");
1451 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing"), "#K04");
1452 Assert.AreEqual (0, Array.LastIndexOf(s1, "this"), "#K05");
1453 Assert.AreEqual (4, Array.LastIndexOf(s1, "test"), "#K06");
1454 Assert.AreEqual (3, Array.LastIndexOf(s1, "a"), "#K07");
1456 Assert.AreEqual (-1, Array.LastIndexOf (new String [0], "foo"));
1460 public void TestLastIndexOf2() {
1462 bool errorThrown = false;
1464 Array.LastIndexOf(null, "huh?", 0);
1465 } catch (ArgumentNullException) {
1468 Assert.IsTrue (errorThrown, "#K21");
1471 bool errorThrown = false;
1473 char[,] c = new Char[2,2];
1474 Array.LastIndexOf(c, "huh?", 0);
1475 } catch (RankException) {
1478 Assert.IsTrue (errorThrown, "#K22");
1481 bool errorThrown = false;
1483 char[] c = new Char[2];
1484 Array.LastIndexOf(c, "huh?", 3);
1485 } catch (ArgumentOutOfRangeException) {
1488 Assert.IsTrue (errorThrown, "#K23");
1491 String[] s1 = {"this", "is", "really", "a", "test"};
1492 Assert.AreEqual (-1, Array.LastIndexOf(s1, null, 3), "#K24");
1493 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing", 3), "#K25");
1494 Assert.AreEqual (-1, Array.LastIndexOf(s1, "test", 3), "#K26");
1495 Assert.AreEqual (3, Array.LastIndexOf(s1, "a", 3), "#K27");
1496 Assert.AreEqual (0, Array.LastIndexOf(s1, "this", 3), "#K28");
1500 public void TestLastIndexOf3() {
1502 bool errorThrown = false;
1504 Array.LastIndexOf(null, "huh?", 0, 1);
1505 } catch (ArgumentNullException) {
1508 Assert.IsTrue (errorThrown, "#K41");
1511 bool errorThrown = false;
1513 char[,] c = new Char[2,2];
1514 Array.LastIndexOf(c, "huh?", 0, 1);
1515 } catch (RankException) {
1518 Assert.IsTrue (errorThrown, "#K42");
1521 bool errorThrown = false;
1523 char[] c = new Char[2];
1524 Array.LastIndexOf(c, "huh?", 3, 1);
1525 } catch (ArgumentOutOfRangeException) {
1528 Assert.IsTrue (errorThrown, "#K43");
1531 bool errorThrown = false;
1533 char[] c = new Char[2];
1534 Array.LastIndexOf(c, "huh?", 0, 5);
1535 } catch (ArgumentOutOfRangeException) {
1538 Assert.IsTrue (errorThrown, "#K44");
1541 String[] s1 = {"this", "is", "really", "a", "test"};
1542 Assert.AreEqual (-1, Array.LastIndexOf(s1, null, 3, 3), "#K45");
1543 Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing", 3, 3), "#K46");
1544 Assert.AreEqual (-1, Array.LastIndexOf(s1, "this", 3, 3), "#K47");
1545 Assert.AreEqual (1, Array.LastIndexOf(s1, "is", 3, 3), "#K48");
1546 Assert.AreEqual (-1, Array.LastIndexOf(s1, "test", 3, 3), "#K49");
1547 Assert.AreEqual (3, Array.LastIndexOf(s1, "a", 3, 3), "#K50");
1551 public void TestLastIndexOf4 ()
1553 short [] a = new short [] { 19, 238, 317, 6, 565, 0, -52, 60, -563, 753, 238, 238};
1555 Array.LastIndexOf (a, (object)16, -1);
1556 NUnit.Framework.Assert.Fail ("#1");
1557 } catch (ArgumentOutOfRangeException) { }
1560 Array.LastIndexOf<short> (a, 16, -1);
1561 NUnit.Framework.Assert.Fail ("#2");
1562 } catch (ArgumentOutOfRangeException) { }
1566 public void TestLastIndexOf5 ()
1568 char [] a = new char [] {'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'j', 'i', 'h'};
1573 for (int i = a.Length - 1; i >= 0 ; i--) {
1575 retval = Array.LastIndexOf(a, a [i], i, i + 1);
1579 Assert.IsTrue (!error);
1583 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1584 public void LastIndexOf_StartIndexOverflow ()
1586 // legal - no exception
1587 byte[] array = new byte [16];
1588 Array.LastIndexOf (array, this, Int32.MaxValue, 1);
1592 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1593 public void LastIndexOf_CountOverflow ()
1595 // legal - no exception
1596 byte[] array = new byte [16];
1597 Array.LastIndexOf (array, this, 1, Int32.MaxValue);
1601 public void LastIndexOf_0LengthArray ()
1603 Array array = Array.CreateInstance (typeof (char), 0);
1604 int idx = Array.LastIndexOf (array, (object) null, -1, 0);
1605 Assert.IsTrue (idx == -1, "#01");
1606 idx = Array.LastIndexOf (array, (object) null, -1, 10);
1607 Assert.IsTrue (idx == -1, "#02");
1608 idx = Array.LastIndexOf (array, (object) null, -100, 10);
1609 Assert.IsTrue (idx == -1, "#02");
1611 array = Array.CreateInstance (typeof (char), 1);
1613 Array.LastIndexOf (array, (object) null, -1, 0);
1614 Assert.Fail ("#04");
1615 } catch (ArgumentOutOfRangeException e) {
1618 Array.LastIndexOf (array, (object) null, -1, 10);
1619 Assert.Fail ("#05");
1620 } catch (ArgumentOutOfRangeException e) {
1623 Array.LastIndexOf (array, (object) null, -100, 10);
1624 Assert.Fail ("#06");
1625 } catch (ArgumentOutOfRangeException e) {
1631 public void FindIndexTest ()
1633 var a = new int[] { 2, 2, 2, 3, 2 };
1634 Assert.AreEqual (2, Array.FindIndex (a, 2, 2, l => true));
1638 public void FindIndex_Invalid ()
1640 var array = new int [] { 1, 2, 3, 4, 5 };
1643 Array.FindIndex (array, null);
1645 } catch (ArgumentNullException) {
1649 Array.FindIndex (array, -1, l => true);
1651 } catch (ArgumentOutOfRangeException) {
1655 Array.FindIndex (array, -1, 0, l => true);
1656 Assert.Fail ("#2b");
1657 } catch (ArgumentOutOfRangeException) {
1661 Array.FindIndex (array, 0, -1, l => true);
1663 } catch (ArgumentOutOfRangeException) {
1667 Array.FindIndex (array, 100, l => true);
1669 } catch (ArgumentOutOfRangeException) {
1673 Array.FindIndex (array, 100, 0, l => true);
1674 Assert.Fail ("#4b");
1675 } catch (ArgumentOutOfRangeException) {
1679 Array.FindIndex (array, 7, 2, l => true);
1681 } catch (ArgumentOutOfRangeException) {
1685 [Test, ExpectedException (typeof (ArgumentNullException))]
1686 public void FindLastNullTest ()
1688 var array = new int [] { 1, 2, 3, 4, 5 };
1689 Array.FindLast (array, null);
1693 public void FindLastIndexTest ()
1695 var array = new int [] { 1, 2, 3, 4, 5 };
1697 Assert.AreEqual (2, Array.FindLastIndex (array, 2, 3, l => true));
1698 Assert.AreEqual (2, Array.FindLastIndex (array, 2, 2, l => true));
1699 Assert.AreEqual (1, Array.FindLastIndex (array, 1, 2, l => true));
1703 public void FindLastIndex_Invalid ()
1705 var array = new int [] { 1, 2, 3, 4, 5 };
1707 Array.FindLastIndex (array, null);
1709 } catch (ArgumentNullException) {
1713 Array.FindLastIndex (array, -1, l => true);
1715 } catch (ArgumentOutOfRangeException) {
1719 Array.FindLastIndex (array, -1, 0, l => true);
1720 Assert.Fail ("#2b");
1721 } catch (ArgumentOutOfRangeException) {
1725 Array.FindLastIndex (array, 0, -1, l => true);
1727 } catch (ArgumentOutOfRangeException) {
1731 Array.FindLastIndex (array, 100, l => true);
1733 } catch (ArgumentOutOfRangeException) {
1737 Array.FindLastIndex (array, 100, 0, l => true);
1738 Assert.Fail ("#4b");
1739 } catch (ArgumentOutOfRangeException) {
1743 Array.FindLastIndex (array, 2, 4, l => true);
1745 } catch (ArgumentOutOfRangeException) {
1750 public void TestReverse() {
1752 bool errorThrown = false;
1754 Array.Reverse(null);
1755 } catch (ArgumentNullException) {
1758 Assert.IsTrue (errorThrown, "#L01");
1761 bool errorThrown = false;
1763 char[,] c = new Char[2,2];
1765 } catch (RankException) {
1768 Assert.IsTrue (errorThrown, "#L02");
1771 char[] c1 = {'a', 'b', 'c', 'd'};
1773 Assert.AreEqual ('d', c1[0], "#L03");
1774 Assert.AreEqual ('c', c1[1], "#L04");
1775 Assert.AreEqual ('b', c1[2], "#L05");
1776 Assert.AreEqual ('a', c1[3], "#L06");
1779 bool errorThrown = false;
1781 Array.Reverse(null, 0, 0);
1782 } catch (ArgumentNullException) {
1785 Assert.IsTrue (errorThrown, "#L07");
1788 bool errorThrown = false;
1790 char[,] c = new Char[2,2];
1791 Array.Reverse(c, 0, 0);
1792 } catch (RankException) {
1795 Assert.IsTrue (errorThrown, "#L08");
1798 //bool errorThrown = false;
1800 // char[] c = new Char[2];
1801 // Array.Reverse(c, 0, 3);
1802 //} catch (ArgumentOutOfRangeException) {
1803 // errorThrown = true;
1805 //Assert.IsTrue (errorThrown, "#L09");
1808 //bool errorThrown = false;
1810 // char[] c = new Char[2];
1811 // Array.Reverse(c, 3, 0);
1812 //} catch (ArgumentOutOfRangeException) {
1813 // errorThrown = true;
1815 //Assert.IsTrue (errorThrown, "#L10");
1818 char[] c2 = { 'a', 'b', 'c', 'd'};
1819 Array.Reverse(c2, 1, 2);
1820 Assert.AreEqual ('a', c2[0], "#L11");
1821 Assert.AreEqual ('c', c2[1], "#L12");
1822 Assert.AreEqual ('b', c2[2], "#L13");
1823 Assert.AreEqual ('d', c2[3], "#L14");
1828 public void ReverseStruct () {
1829 BStruct[] c3 = new BStruct[2];
1830 c3 [0] = new BStruct () { i1 = 1, i2 = 2, i3 = 3 };
1831 c3 [1] = new BStruct () { i1 = 4, i2 = 5, i3 = 6 };
1833 Assert.AreEqual (4, c3 [0].i1);
1834 Assert.AreEqual (5, c3 [0].i2);
1835 Assert.AreEqual (6, c3 [0].i3);
1836 Assert.AreEqual (1, c3 [1].i1);
1837 Assert.AreEqual (2, c3 [1].i2);
1838 Assert.AreEqual (3, c3 [1].i3);
1842 public int i1, i2, i3;
1846 public void TestSetValue1() {
1848 bool errorThrown = false;
1850 char[,] c = new Char[2,2];
1851 c.SetValue("buh", 1);
1852 } catch (ArgumentException) {
1855 Assert.IsTrue (errorThrown, "#M01");
1858 bool errorThrown = false;
1860 char[] c = {'a', 'b', 'c'};
1861 c.SetValue("buh", -1);
1862 } catch (IndexOutOfRangeException) {
1865 Assert.IsTrue (errorThrown, "#M02");
1868 bool errorThrown = false;
1870 char[] c = {'a', 'b', 'c'};
1871 c.SetValue("buh", 4);
1872 } catch (IndexOutOfRangeException) {
1875 Assert.IsTrue (errorThrown, "#M03");
1878 char[] c1 = {'a', 'b', 'c', 'd'};
1879 char[] c2 = new char[4];
1880 for (int i = 0; i < c1.Length; i++) {
1881 c2.SetValue(c1[i], i);
1883 for (int i = 0; i < c1.Length; i++) {
1884 Assert.AreEqual (c1[i], c2[i], "#M04(" + i + ")");
1887 int[] c3 = { 1, 2, 3 };
1888 long[] c4 = new long [3];
1890 for (int i = 0; i < c3.Length; i++)
1891 c4.SetValue (c3 [i], i);
1895 } catch (Exception e) {
1896 Assert.Fail ("c3.CopyTo(): e=" + e);
1898 for (int i = 0; i < c3.Length; i++)
1899 Assert.IsTrue (c3[i] == c4[i], "#M05(" + i + ")");
1901 Object[] c5 = new Object [3];
1902 long[] c6 = new long [3];
1906 } catch (Exception e) {
1907 Assert.Fail ("c4.CopyTo(): e=" + e);
1912 } catch (Exception e) {
1913 Assert.Fail ("c5.CopyTo(): e=" + e);
1915 // for (int i = 0; i < c5.Length; i++)
1916 // Assert.IsTrue (c5[i] == c6[i], "#M06(" + i + ")");
1920 public void TestSetValue2() {
1922 bool errorThrown = false;
1924 char[] c = new Char[2];
1925 c.SetValue("buh", 1,1);
1926 } catch (ArgumentException) {
1929 Assert.IsTrue (errorThrown, "#M21");
1932 bool errorThrown = false;
1934 char[,] c = new Char[2,2];
1935 c.SetValue("buh", -1, 1);
1936 } catch (IndexOutOfRangeException) {
1939 Assert.IsTrue (errorThrown, "#M22");
1942 bool errorThrown = false;
1944 char[,] c = new Char[2,2];
1945 c.SetValue("buh", 4,1);
1946 } catch (IndexOutOfRangeException) {
1949 Assert.IsTrue (errorThrown, "#M23");
1952 char[,] c1 = new Char[4,6];
1953 char[,] c2 = new Char[4,6];
1954 for (int i = 0; i < 24; i++) {
1957 c1[first,second] = (char)(((int)'a')+i);
1958 c2.SetValue(c1[first,second], first, second);
1960 for (int i = 0; i < c1.GetLength(0); i++) {
1961 for (int j = 0; j < c1.GetLength(1); j++) {
1962 Assert.AreEqual (c1[i, j], c2[i, j], "#M24(" + i + "," + j + ")");
1968 public void TestSetValue3() {
1970 bool errorThrown = false;
1972 char[] c = new Char[2];
1973 c.SetValue("buh", 1,1,1);
1974 } catch (ArgumentException) {
1977 Assert.IsTrue (errorThrown, "#M41");
1980 bool errorThrown = false;
1982 char[,,] c = new Char[2,2,2];
1983 c.SetValue("buh", -1, 1, 1);
1984 } catch (IndexOutOfRangeException) {
1987 Assert.IsTrue (errorThrown, "#M42");
1990 bool errorThrown = false;
1992 char[,,] c = new Char[2,2,2];
1993 c.SetValue("buh", 4,1,1);
1994 } catch (IndexOutOfRangeException) {
1997 Assert.IsTrue (errorThrown, "#M43");
2000 char[,,] c1 = new Char[4,2,3];
2001 char[,,] c2 = new Char[4,2,3];
2002 for (int i = 0; i < 24; i++) {
2004 int remains = i % 6;
2005 int second = remains / 3;
2006 int third = remains % 3;
2007 c1[first,second, third] = (char)(((int)'a')+i);
2008 c2.SetValue(c1[first, second, third], first, second, third);
2010 for (int i = 0; i < c1.GetLength(0); i++) {
2011 for (int j = 0; j < c1.GetLength(1); j++) {
2012 for (int k = 0; k < c1.GetLength(2); k++) {
2013 Assert.AreEqual (c1[i, j, k], c2[i, j, k], "#M44(" + i + "," + j + " )");
2020 [ExpectedException (typeof (ArgumentNullException))]
2021 public void TestSetValueLongArray ()
2023 char[] c = new Char[2];
2024 c.SetValue("buh", (long [])null);
2028 public void TestSetValueN() {
2030 bool errorThrown = false;
2032 char[] c = new Char[2];
2033 c.SetValue("buh", (int [])null);
2034 } catch (ArgumentNullException) {
2037 Assert.IsTrue (errorThrown, "#M61a");
2040 bool errorThrown = false;
2042 char[] c = new Char[2];
2043 int[] coords = {1, 1};
2044 c.SetValue("buh", coords);
2045 } catch (ArgumentException) {
2048 Assert.IsTrue (errorThrown, "#M62");
2051 bool errorThrown = false;
2053 char[,] c = new Char[2,2];
2054 int[] coords = {-1, 1};
2055 c.SetValue("buh", coords);
2056 } catch (IndexOutOfRangeException) {
2059 Assert.IsTrue (errorThrown, "#M63");
2062 bool errorThrown = false;
2064 char[,] c = new Char[2,2];
2065 int[] coords = {4, 1};
2066 c.SetValue("buh", coords);
2067 } catch (IndexOutOfRangeException) {
2070 Assert.IsTrue (errorThrown, "#M64");
2073 char[,] c1 = new Char[4,6];
2074 char[,] c2 = new Char[4,6];
2075 for (int i = 0; i < 24; i++) {
2078 c1[first,second] = (char)(((int)'a')+i);
2079 int[] coords = {first, second};
2080 c2.SetValue(c1[first,second], coords);
2082 for (int i = 0; i < c1.GetLength(0); i++) {
2083 for (int j = 0; j < c1.GetLength(1); j++) {
2084 Assert.AreEqual (c1[i, j], c2[i, j], "#M65(" + i + "," + j + ")");
2090 public void TestSetValue4() {
2092 int[] c1 = { 1, 2, 3 };
2093 long[] c2 = new long [3];
2095 for (int i = 0; i < c1.Length; i++)
2096 c2.SetValue (c1 [i], i);
2098 for (int i = 0; i < c1.Length; i++) {
2099 Assert.IsTrue (c1[i] == c2[i], "#M81(" + i + ")");
2100 Assert.AreEqual (typeof (long), c2[i].GetType (), "#M82(" + i + ")");
2104 long[] c1 = { 1, 2, 3 };
2105 int[] c2 = new int [3];
2106 bool errorThrown = false;
2108 c2.SetValue (c1 [0], 0);
2109 } catch (ArgumentException) {
2112 Assert.IsTrue (errorThrown, "#M83");
2115 int[] c1 = { 1, 2, 3 };
2116 Object[] c2 = new Object [3];
2118 for (int i = 0; i < c1.Length; i++)
2119 c2.SetValue (c1 [i], i);
2121 for (int i = 0; i < c1.Length; i++)
2122 Assert.AreEqual (c1[i], Convert.ToInt32 (c2[i]), "#M84(" + i + ")");
2125 Object[] c1 = new Object [3];
2126 Object[] c2 = new Object [3];
2127 c1[0] = new Object ();
2129 for (int i = 0; i < c1.Length; i++)
2130 c2.SetValue (c1 [i], i);
2132 for (int i = 0; i < c1.Length; i++)
2133 Assert.AreEqual (c1[i], c2[i], "#M85(" + i + ")");
2136 Object[] c1 = new Object [3];
2137 string[] c2 = new String [3];
2138 string test = "hello";
2141 c2.SetValue (c1 [0], 0);
2142 Assert.AreEqual (c1[0], c2[0], "#M86");
2143 Assert.AreEqual ("hello", c2[0], "#M87");
2146 char[] c1 = { 'a', 'b', 'c' };
2147 string[] c2 = new string [3];
2149 c2.SetValue (c1 [0], 0);
2150 Assert.Fail ("#M88");
2151 } catch (InvalidCastException) {}
2154 Single[] c1 = { 1.2F, 2.3F, 3.4F, 4.5F };
2155 long[] c2 = new long [3];
2157 c2.SetValue (c1 [0], 0);
2158 Assert.Fail ("#M89");
2159 } catch (ArgumentException) {}
2187 UInt32 v11 = 235354;
2188 UInt64 v12 = 234552;
2190 Object[] va1 = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12 };
2191 Object[] va2 = { "true", "1", "a", "-1.2", "-32", "-234", "-34523", "-1",
2192 "-4.8F", "24234", "235354", "234552" };
2194 Object[][] vt = { va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1 };
2197 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2198 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
2199 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
2200 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
2201 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1,
2202 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1,
2203 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,
2204 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1,
2205 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1,
2206 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
2207 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0,
2208 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0
2213 for (int i = 0; i < types.Length; i++) {
2214 for (int j = 0; j < types.Length; j++) {
2215 Array array = Array.CreateInstance (types [j], 2);
2217 Object value = vt[j][i];
2219 bool errorThrown = false;
2221 array.SetValue (value, 0);
2222 } catch (ArgumentException) {
2226 int ex_index = (i * types.Length) + j;
2228 Assert.AreEqual (errorThrown, arg_ex [ex_index] == 1, "#M90(" + types [i] + "," + types [j] + ")");
2232 for (int i = 0; i < types.Length; i++) {
2233 String[] array = new String [2];
2235 Object value = va1 [i];
2237 bool errorThrown = false;
2239 array.SetValue (value, 0);
2240 } catch (InvalidCastException) {
2244 Assert.IsTrue (errorThrown, "#M91(" + types [i] + ")");
2247 for (int i = 0; i < types.Length; i++) {
2248 Array array = Array.CreateInstance (types [i], 2);
2250 Object value = va2 [i];
2252 bool errorThrown = false;
2254 array.SetValue (value, 0);
2255 } catch (InvalidCastException) {
2259 Assert.IsTrue (errorThrown, "#M92(" + types [i] + ")");
2262 for (int i = 0; i < types.Length; i++) {
2263 Array array = Array.CreateInstance (types [i], 2);
2265 Object value = null;
2267 bool errorThrown = false;
2269 array.SetValue (value, 0);
2270 } catch (InvalidCastException) {
2274 Assert.IsTrue (!errorThrown, "#M93(" + types [i] + ")");
2279 for (int i = 0; i < types.Length; i++) {
2280 for (int j = 0; j < types.Length; j++) {
2281 Array source = Array.CreateInstance (types [i], 2);
2282 Array array = Array.CreateInstance (types [j], 2);
2284 source.SetValue (vt[j][i], 0);
2285 source.SetValue (vt[j][i], 1);
2287 bool errorThrown = false;
2289 Array.Copy (source, array, 2);
2290 } catch (ArrayTypeMismatchException) {
2294 int ex_index = (i * types.Length) + j;
2296 Assert.AreEqual (errorThrown, arg_ex [ex_index] == 1, "#M94(" + types [i] + "," + types [j] + ")");
2300 for (int i = 0; i < types.Length; i++) {
2301 Array source = Array.CreateInstance (types [i], 2);
2302 String[] array = new String [2];
2304 source.SetValue (va1 [i], 0);
2305 source.SetValue (va1 [i], 1);
2307 bool errorThrown = false;
2309 Array.Copy (source, array, 2);
2310 } catch (ArrayTypeMismatchException) {
2314 Assert.IsTrue (errorThrown, "#M95(" + types [i] + ")");
2317 for (int i = 0; i < types.Length; i++) {
2318 String[] source = new String [2];
2319 Array array = Array.CreateInstance (types [i], 2);
2321 source.SetValue (va2 [i], 0);
2322 source.SetValue (va2 [i], 1);
2324 bool errorThrown = false;
2326 Array.Copy (source, array, 2);
2327 } catch (ArrayTypeMismatchException) {
2331 Assert.IsTrue (errorThrown, "#M96(" + types [i] + ")");
2337 public void TestSort() {
2339 bool errorThrown = false;
2342 } catch (ArgumentNullException) {
2345 Assert.IsTrue (errorThrown, "#N01");
2348 bool errorThrown = false;
2350 Array.Sort(null, 0, 1);
2351 } catch (ArgumentNullException) {
2354 Assert.IsTrue (errorThrown, "#N02");
2357 bool errorThrown = false;
2359 char[] c1 = new Char[2];
2360 Array.Sort(null, c1);
2361 } catch (ArgumentNullException) {
2364 Assert.IsTrue (errorThrown, "#N03");
2367 bool errorThrown = false;
2369 char[] c1 = new Char[2];
2370 Array.Sort(null, c1, 0, 1);
2371 } catch (ArgumentNullException) {
2374 Assert.IsTrue (errorThrown, "#N04");
2378 char[] arr = {'d', 'b', 'f', 'e', 'a', 'c'};
2381 Array.Sort (null, 0, 1);
2382 Assert.Fail ("#N" + tc.ToString ());
2384 catch (ArgumentException) {}
2385 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2389 Array.Sort (arr, -1, 3);
2390 Assert.Fail ("#N" + tc.ToString ());
2392 catch (ArgumentException) {}
2393 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2397 Array.Sort (arr, 1, -3);
2398 Assert.Fail ("#N" + tc.ToString ());
2400 catch (ArgumentException) {}
2401 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2405 Array.Sort (arr, arr.Length, arr.Length + 2);
2406 Assert.Fail ("#N" + tc.ToString ());
2408 catch (ArgumentException) {}
2409 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2412 // note: null second array => just sort first array
2413 char[] starter = {'d', 'b', 'f', 'e', 'a', 'c'};
2414 int[] starter1 = {1,2,3,4,5,6};
2416 char[] c1 = (char[])starter.Clone();
2418 Assert.AreEqual ('a', c1[0], "#N21");
2419 Assert.AreEqual ('b', c1[1], "#N22");
2420 Assert.AreEqual ('c', c1[2], "#N23");
2421 Assert.AreEqual ('d', c1[3], "#N24");
2422 Assert.AreEqual ('e', c1[4], "#N25");
2423 Assert.AreEqual ('f', c1[5], "#N26");
2426 char[] c1 = (char[])starter.Clone();
2427 int[] i1 = (int[])starter1.Clone();
2429 Assert.AreEqual ('a', c1[0], "#N41");
2430 Assert.AreEqual ('b', c1[1], "#N42");
2431 Assert.AreEqual ('c', c1[2], "#N43");
2432 Assert.AreEqual ('d', c1[3], "#N44");
2433 Assert.AreEqual ('e', c1[4], "#N45");
2434 Assert.AreEqual ('f', c1[5], "#N46");
2435 Assert.AreEqual (5, i1[0], "#N47");
2436 Assert.AreEqual (2, i1[1], "#N48");
2437 Assert.AreEqual (6, i1[2], "#N49");
2438 Assert.AreEqual (1, i1[3], "#N50");
2439 Assert.AreEqual (4, i1[4], "#N51");
2440 Assert.AreEqual (3, i1[5], "#N52");
2443 char[] c1 = (char[])starter.Clone();
2444 Array.Sort(c1, 1, 4);
2445 Assert.AreEqual ('d', c1[0], "#N61");
2446 Assert.AreEqual ('a', c1[1], "#N62");
2447 Assert.AreEqual ('b', c1[2], "#N63");
2448 Assert.AreEqual ('e', c1[3], "#N64");
2449 Assert.AreEqual ('f', c1[4], "#N65");
2450 Assert.AreEqual ('c', c1[5], "#N66");
2453 char[] c1 = (char[])starter.Clone();
2454 int[] i1 = (int[])starter1.Clone();
2455 Array.Sort(c1, i1, 1, 4);
2456 Assert.AreEqual ('d', c1[0], "#N81");
2457 Assert.AreEqual ('a', c1[1], "#N82");
2458 Assert.AreEqual ('b', c1[2], "#N83");
2459 Assert.AreEqual ('e', c1[3], "#N84");
2460 Assert.AreEqual ('f', c1[4], "#N85");
2461 Assert.AreEqual ('c', c1[5], "#N86");
2462 Assert.AreEqual (1, i1[0], "#N87");
2463 Assert.AreEqual (5, i1[1], "#N88");
2464 Assert.AreEqual (2, i1[2], "#N89");
2465 Assert.AreEqual (4, i1[3], "#N90");
2466 Assert.AreEqual (3, i1[4], "#N91");
2467 Assert.AreEqual (6, i1[5], "#N92");
2472 double[] a = new double[115];
2473 int[] b = new int[256];
2474 Array.Sort<double, int> (a, b, 0, 115);
2477 /* Check that ulong[] is not sorted as long[] */
2479 string[] names = new string[] {
2480 "A", "B", "C", "D", "E"
2483 ulong[] arr = new ulong [] {
2485 unchecked((ulong)0xffffFFFF00000000),
2492 Array.Sort (a, names, null);
2493 Assert.AreEqual (0, a.GetValue (0));
2498 public void Sort_NullValues ()
2500 var s = new [] { "a", null, "b", null };
2501 Array.Sort (s, (a, b) => {
2503 return b == null ? 0 : 1;
2509 return a.CompareTo (b);
2512 Assert.AreEqual ("a", s [0], "#1");
2513 Assert.AreEqual ("b", s [1], "#2");
2514 Assert.IsNull (s [2], "#3");
2515 Assert.IsNull (s [3], "#4");
2519 public void SortNonGenericDoubleItems () {
2520 double[] doubleValues = new double[11];
2522 doubleValues[0] = 0.221788066253601;
2523 doubleValues[1] = 0.497278285809481;
2524 doubleValues[2] = 0.100565033883643;
2525 doubleValues[3] = 0.0433309347749905;
2526 doubleValues[4] = 0.00476726438463812;
2527 doubleValues[5] = 0.1354609735456;
2528 doubleValues[6] = 0.57690356588135;
2529 doubleValues[7] = 0.466239434334826;
2530 doubleValues[8] = 0.409741461978934;
2531 doubleValues[9] = 0.0112412763949565;
2532 doubleValues[10] = 0.668704347674307;
2534 int[] indices = new int[11];
2547 Array.Sort ((Array)doubleValues, (Array)indices);
2548 Assert.AreEqual (4, indices [0]);
2552 public void TestSortComparable()
2554 int[] source = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
2555 int[] expected = { 6, 5, 4, 3, 2, 1, 7, 8, 9 };
2556 Comp[] c = { new Comp (100), new Comp (16), new Comp (11), new Comp (9), new Comp (0), new Comp (-100) };
2557 IComparer<Comp> comp = null;
2558 Array.Sort<Comp, int> (c, source, comp);
2560 Assert.AreEqual (expected, source);
2563 class Comp : IComparable
2572 int IComparable.CompareTo (object obj)
2574 return val.CompareTo ((obj as Comp).val);
2579 public void TestInitializeEmpty()
2591 Assert.IsTrue (!catched, "#TI01");
2595 public void TestInitializeInt()
2600 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2602 Assert.AreEqual (a[i], b[i], "#TI02 " + i);
2607 public void TestInitializeDouble()
2609 double[] a = {1.0,2.0,0.0};
2611 double[] b = {1.0,2.0,0.0};
2612 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2614 Assert.AreEqual (a[i], b[i], "#TI03 " + i);
2619 public void TestInitializeFloat()
2621 float[] a = {1.0F,2.0F,0.0F};
2623 float[] b = {1.0F,2.0F,0.0F};
2624 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2626 Assert.AreEqual (a[i], b[i], "#TI04 " + i);
2631 public void TestInitializeChar()
2633 char[] a = {'1','.','0','F','2','.','0','F'};
2635 char[] b = {'1','.','0','F','2','.','0','F'};
2636 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2638 Assert.AreEqual (a[i], b[i], "#TI05 " + i);
2643 public void TestInitializeString()
2645 string[] a = {"hola","adios","menos","mas"};
2647 string[] b = {"hola","adios","menos","mas"};
2648 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2650 Assert.AreEqual (a[i], b[i], "#TI06 " + i);
2655 public void TestInitializeEnum()
2657 enua[] a = {enua.hola,enua.adios,enua.menos,enua.mas};
2659 enua[] b = {enua.hola,enua.adios,enua.menos,enua.mas};
2660 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2662 Assert.AreEqual (a[i], b[i], "#TI07 " + i);
2667 public void TestInitializeIntNI()
2669 int[] a = new int[20];
2673 Assert.AreEqual (b, 0, "#TI08");
2678 public void TestInitializeCharNI()
2680 char[] a = new char[20];
2682 foreach(char b in a)
2684 Assert.AreEqual (b, 0, "#TI09");
2689 public void TestInitializeDoubleNI()
2691 double[] a = new double[20];
2693 foreach(double b in a)
2695 Assert.AreEqual (b, 0.0, "#TI09");
2700 public void TestInitializeStringNI()
2702 string[] a = new string[20];
2704 foreach(string b in a)
2706 Assert.AreEqual (b, null, "#TI10");
2711 public void TestInitializeObjectNI()
2713 object[] a = new object[20];
2715 foreach(object b in a)
2717 Assert.AreEqual (b, null, "#TI11");
2722 public void TestInitializeAClassNI()
2724 AClass[] a = new AClass[20];
2726 foreach(AClass b in a)
2728 Assert.AreEqual (b, null, "#TI12");
2734 public void TestInitializeAStructNI()
2736 AStruct[] a = new AStruct[20];
2738 foreach(AStruct b in a)
2740 Assert.AreEqual (b, new AStruct(), "#TI14");
2745 public void TestInitializeAStruct()
2747 AStruct[] a = new AStruct[3];
2751 AStruct[] b = new AStruct[3];
2754 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2756 Assert.AreEqual (a[i], b[i], "#TI15 " + i);
2761 public void TestInitializeDateTimeNI()
2763 DateTime[] a = new DateTime[20];
2765 foreach(DateTime b in a)
2767 Assert.AreEqual (b, new DateTime(), "#TI16");
2772 [ExpectedException (typeof (ArgumentNullException))]
2773 public void MoreSort1 ()
2775 Array.Sort (null, 0, 1);
2779 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2780 public void MoreSort2 ()
2782 Array.Sort (arrsort, -1, 3);
2786 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2787 public void MoreSort3 ()
2789 Array.Sort (arrsort, 1, -3);
2793 [ExpectedException (typeof (ArgumentException))]
2794 public void MoreSort4 ()
2796 Array.Sort (arrsort, arrsort.Length, arrsort.Length + 2);
2800 [ExpectedException (typeof (RankException))]
2801 public void MoreSort5 ()
2803 char [,] arr = new char [,] {{'a'}, {'b'}};
2804 Array.Sort (arr, 0, 1);
2808 public void MoreSort6 ()
2810 Array.Sort (arrsort, 0, 0);
2814 [ExpectedException (typeof (ArgumentException))]
2815 public void MoreSort7 ()
2817 Array.Sort (arrsort, arrsort.Length - 1, 2);
2821 [ExpectedException (typeof (ArgumentException))]
2822 public void MoreSort8 ()
2824 Array.Sort (arrsort, 0, arrsort.Length + 1);
2828 public void MoreSort9 ()
2830 Array.Sort (arrsort, null, 0, arrsort.Length, null);
2834 [ExpectedException (typeof (InvalidOperationException))]
2835 public void MoreSort10 ()
2837 object [] array = {true, 'k', SByte.MinValue, Byte.MinValue, (short) 2, 634, (long) 436, (float) 1.1, 1.23, "Hello World"};
2838 Array.Sort (array, (IComparer) null);
2841 [Test] // bug #81941
2844 double [] a = new double [2] { 0.9, 0.3 };
2845 uint [] b = new uint [2] { 4, 7 };
2847 Assert.AreEqual (0.3, a [0], "#1");
2848 Assert.AreEqual (0.9, a [1], "#2");
2849 Assert.AreEqual (7, b [0], "#3");
2850 Assert.AreEqual (4, b [1], "#4");
2854 public void ClearJaggedArray ()
2856 byte[][] matrix = new byte [8][];
2857 for (int i=0; i < 8; i++) {
2858 matrix [i] = new byte [8];
2859 for (int j=0; j < 8; j++) {
2863 Array.Clear (matrix, 0, 8);
2864 for (int i=0; i < 8; i++) {
2865 Assert.IsNull (matrix [i], i.ToString ());
2870 public void ClearMultidimentionalArray ()
2872 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2873 Array.Clear (matrix, 0, 2);
2874 Assert.AreEqual (0, matrix [0, 0], "0,0");
2875 Assert.AreEqual (0, matrix [0, 1], "0,1");
2876 Assert.AreEqual (2, matrix [1, 0], "1,0");
2877 Assert.AreEqual (2, matrix [1, 1], "1,1");
2881 [ExpectedException (typeof (IndexOutOfRangeException))]
2882 public void ClearOutsideMultidimentionalArray ()
2884 byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
2885 Array.Clear (matrix, 0, 5);
2889 [ExpectedException (typeof (IndexOutOfRangeException))]
2890 public void Clear_IndexOverflow ()
2892 byte[] array = new byte [16];
2893 Array.Clear (array, 4, Int32.MaxValue);
2897 [ExpectedException (typeof (IndexOutOfRangeException))]
2898 public void Clear_LengthOverflow ()
2900 byte[] array = new byte [16];
2901 Array.Clear (array, Int32.MaxValue, 4);
2905 [ExpectedException (typeof (ArgumentException))]
2906 public void Copy_SourceIndexOverflow ()
2908 byte[] array = new byte [16];
2909 Array.Copy (array, Int32.MaxValue, array, 8, 8);
2913 [ExpectedException (typeof (ArgumentException))]
2914 public void Copy_DestinationIndexOverflow ()
2916 byte[] array = new byte [16];
2917 Array.Copy (array, 8, array, Int32.MaxValue, 8);
2921 [ExpectedException (typeof (ArgumentException))]
2922 public void Copy_LengthOverflow ()
2924 byte[] array = new byte [16];
2925 Array.Copy (array, 8, array, 8, Int32.MaxValue);
2929 [ExpectedException (typeof (ArgumentException))]
2930 public void Reverse_IndexOverflow ()
2932 byte[] array = new byte [16];
2933 Array.Reverse (array, Int32.MaxValue, 8);
2937 [ExpectedException (typeof (ArgumentException))]
2938 public void Reverse_LengthOverflow ()
2940 byte[] array = new byte [16];
2941 Array.Reverse (array, 8, Int32.MaxValue);
2944 public struct CharX : IComparable {
2947 public CharX (char c)
2952 public int CompareTo (object obj)
2955 return c.CompareTo (((CharX) obj).c);
2957 return c.CompareTo (obj);
2962 public void BinarySearch_ArgPassingOrder ()
2965 // This tests that arguments are passed to the comprer in the correct
2966 // order. The IComparable of the *array* elements must get called, not
2967 // that of the search object.
2969 CharX [] x = { new CharX ('a'), new CharX ('b'), new CharX ('c') };
2970 Assert.AreEqual (1, Array.BinarySearch (x, 'b'));
2973 class Comparer: IComparer {
2975 private bool called = false;
2977 public bool Called {
2979 bool result = called;
2985 public int Compare (object x, object y)
2993 public void BinarySearch1_EmptyList ()
2995 int[] array = new int[0];
2996 Assert.AreEqual (- 1, Array.BinarySearch (array, 0), "BinarySearch");
3000 public void BinarySearch2_EmptyList ()
3002 int[] array = new int[0];
3003 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, 0), "BinarySearch");
3007 public void BinarySearch3_EmptyList ()
3009 Comparer comparer = new Comparer ();
3010 int[] array = new int[0];
3011 Assert.AreEqual (-1, Array.BinarySearch (array, 0, comparer), "BinarySearch");
3012 // bug 77030 - the comparer isn't called for an empty array/list
3013 Assert.IsTrue (!comparer.Called, "Called");
3017 public void BinarySearch4_EmptyList ()
3019 Comparer comparer = new Comparer ();
3020 int[] array = new int[0];
3021 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, comparer), "BinarySearch");
3022 // bug 77030 - the comparer isn't called for an empty array/list
3023 Assert.IsTrue (!comparer.Called, "Called");
3027 [ExpectedException (typeof (ArgumentNullException))]
3028 public void AsReadOnly_NullArray ()
3030 Array.AsReadOnly <int> (null);
3034 public void ReadOnly_Count ()
3036 Assert.AreEqual (10, Array.AsReadOnly (new int [10]).Count);
3040 public void ReadOnly_Contains ()
3042 int[] arr = new int [2];
3045 IList<int> a = Array.AsReadOnly (arr);
3047 Assert.IsTrue (a.Contains (3));
3048 Assert.IsTrue (!a.Contains (6));
3052 public void ReadOnly_IndexOf ()
3054 int[] arr = new int [2];
3057 IList<int> a = Array.AsReadOnly (arr);
3059 Assert.AreEqual (0, a.IndexOf (3));
3060 Assert.AreEqual (1, a.IndexOf (5));
3061 Assert.AreEqual (-1, a.IndexOf (6));
3065 public void ReadOnly_Indexer ()
3067 int[] arr = new int [2];
3070 IList<int> a = Array.AsReadOnly (arr);
3072 Assert.AreEqual (3, a [0]);
3073 Assert.AreEqual (5, a [1]);
3075 /* Check that modifications to the original array are visible */
3077 Assert.AreEqual (6, a [0]);
3081 public void ReadOnly_Enumerator ()
3083 int[] arr = new int [10];
3085 for (int i = 0; i < 10; ++i)
3089 foreach (int i in Array.AsReadOnly (arr))
3092 Assert.AreEqual (45, sum);
3096 public void ReadOnly_CopyTo ()
3098 int[] arr = new int [2];
3101 IList<int> a = Array.AsReadOnly (arr);
3103 int[] arr2 = new int [3];
3106 Assert.AreEqual (0, arr2 [0]);
3107 Assert.AreEqual (3, arr2 [1]);
3108 Assert.AreEqual (5, arr2 [2]);
3112 public void Resize ()
3114 int [] arr = new int [] { 1, 3, 5 };
3115 Array.Resize <int> (ref arr, 3);
3116 Assert.AreEqual (3, arr.Length, "#A1");
3117 Assert.AreEqual (1, arr [0], "#A2");
3118 Assert.AreEqual (3, arr [1], "#A3");
3119 Assert.AreEqual (5, arr [2], "#A4");
3121 Array.Resize <int> (ref arr, 2);
3122 Assert.AreEqual (2, arr.Length, "#B1");
3123 Assert.AreEqual (1, arr [0], "#B2");
3124 Assert.AreEqual (3, arr [1], "#B3");
3126 Array.Resize <int> (ref arr, 4);
3127 Assert.AreEqual (4, arr.Length, "#C1");
3128 Assert.AreEqual (1, arr [0], "#C2");
3129 Assert.AreEqual (3, arr [1], "#C3");
3130 Assert.AreEqual (0, arr [2], "#C4");
3131 Assert.AreEqual (0, arr [3], "#C5");
3135 public void Resize_null ()
3138 Array.Resize (ref arr, 10);
3139 Assert.AreEqual (arr.Length, 10);
3143 public void Test_ContainsAndIndexOf_EquatableItem ()
3145 EquatableClass[] list = new EquatableClass[] {new EquatableClass (0), new EquatableClass (1), new EquatableClass (0)};
3147 Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, list[0]), "#0");
3148 Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, new EquatableClass (0)), "#1");
3149 Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, list[0]), "#2");
3150 Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, new EquatableClass (0)), "#3");
3153 public class EquatableClass : IEquatable<EquatableClass>
3156 public EquatableClass (int x)
3161 public bool Equals (EquatableClass other)
3163 return this._x == other._x;
3168 public void AsIList ()
3170 IList<int> arr = new int [10];
3172 Assert.AreEqual (5, arr [0]);
3174 IList<FooStruct> arr2 = new FooStruct [10];
3175 FooStruct s = new FooStruct ();
3180 Assert.AreEqual (11, s.i);
3181 Assert.AreEqual (22, s.j);
3183 IList<string> arr3 = new string [10];
3185 Assert.AreEqual ("ABC", arr3 [5]);
3194 public void ICollectionNull ()
3196 ICollection<object> test;
3198 test = new List<object>();
3199 Assert.AreEqual (test.Contains (null), false, "list<o>");
3201 test = new object[] {};
3202 Assert.AreEqual (test.Contains (null), false, "empty array");
3204 test = new object[] {null};
3205 Assert.AreEqual (test.Contains (null), true, "array with null");
3207 test = new object[] { 1, null};
3208 Assert.IsTrue (test.Contains (null), "array with last null");
3210 test = new List<object>(test);
3211 Assert.AreEqual (test.Contains (null), true, "List<object> with test");
3213 test = new object[] {new object()};
3214 Assert.AreEqual (test.Contains (null), false, "array with object");
3216 test = new List<object>(test);
3217 Assert.AreEqual (test.Contains (null), false, "array with test");
3221 public void IListNull ()
3225 test = new List<object>();
3226 Assert.AreEqual (-1, test.IndexOf (null), "list<o>");
3228 test = new object[] {};
3229 Assert.AreEqual (-1, test.IndexOf (null), "empty array");
3231 test = new object[] {null};
3232 Assert.AreEqual (0, test.IndexOf (null), "array with null");
3234 test = new object[] { 1, null};
3235 Assert.AreEqual (1, test.IndexOf (null), "array with last null");
3237 test = new List<object>(test);
3238 Assert.AreEqual (1, test.IndexOf (null), "List<object> with test");
3240 test = new object[] {new object()};
3241 Assert.AreEqual (-1, test.IndexOf (null), "array with object");
3243 test = new List<object>(test);
3244 Assert.AreEqual (-1, test.IndexOf (null), "array with test");
3250 enum ByteEnum : byte {}
3251 enum IntEnum : int {}
3254 public void TestByteEnumArrayToByteArray ()
3256 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
3257 byte[] b = new byte[a.Length];
3262 public void TestByteEnumArrayToIntArray ()
3264 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
3265 int[] b = new int[a.Length];
3270 [ExpectedException (typeof (ArrayTypeMismatchException))]
3271 public void TestIntEnumArrayToByteArray ()
3273 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
3274 byte[] b = new byte[a.Length];
3279 public void TestIntEnumArrayToIntArray ()
3281 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
3282 int[] b = new int[a.Length];
3288 [Test] // bug #322248
3289 public void IEnumerator_Reset ()
3291 int[] array = new int[] { 1, 2, 3};
3292 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3293 Assert.IsTrue (e.MoveNext (), "#A1");
3294 Assert.AreEqual (1, e.Current, "#A2");
3295 Assert.IsTrue (e.MoveNext (), "#A3");
3296 Assert.AreEqual (2, e.Current, "#A4");
3300 Assert.IsTrue (e.MoveNext (), "#C1");
3301 Assert.AreEqual (1, e.Current, "#C2");
3305 public void IEnumerator_Current_Finished ()
3307 int[] array = new int[] { 1, 2, 3 };
3308 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3309 Assert.IsTrue (e.MoveNext (), "#A1");
3310 Assert.AreEqual (1, e.Current, "#A2");
3311 Assert.IsTrue (e.MoveNext (), "#A3");
3312 Assert.AreEqual (2, e.Current, "#A4");
3313 Assert.IsTrue (e.MoveNext (), "#A5");
3314 Assert.AreEqual (3, e.Current, "#A6");
3315 Assert.IsTrue (!e.MoveNext (), "#A6");
3318 Assert.Fail ("#B1:" + e.Current);
3319 } catch (InvalidOperationException ex) {
3320 // Enumeration already finished
3321 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3322 Assert.IsNull (ex.InnerException, "#B3");
3323 Assert.IsNotNull (ex.Message, "#B4");
3328 public void IEnumerator_Current_NotStarted ()
3330 int[] array = new int[] { 1, 2, 3 };
3331 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3334 Assert.Fail ("#A1:" + e.Current);
3335 } catch (InvalidOperationException ex) {
3336 // Enumeration has not started. Call MoveNext
3337 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
3338 Assert.IsNull (ex.InnerException, "#A3");
3339 Assert.IsNotNull (ex.Message, "#A4");
3344 public void IEnumerator_Current_Reset ()
3346 int[] array = new int[] { 1, 2, 3 };
3347 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3352 Assert.Fail ("#B1:" + e.Current);
3353 } catch (InvalidOperationException ex) {
3354 // Enumeration has not started. Call MoveNext
3355 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
3356 Assert.IsNull (ex.InnerException, "#B3");
3357 Assert.IsNotNull (ex.Message, "#B4");
3361 public void ICollection_IsReadOnly() {
3362 ICollection<string> arr = new string [10];
3364 Assert.IsTrue (arr.IsReadOnly);
3368 [ExpectedException (typeof (NotSupportedException))]
3369 public void ArrayCreateInstanceOfVoid ()
3371 Array.CreateInstance (typeof (void), 42);
3377 [ExpectedException (typeof (NotSupportedException))]
3378 public void ArrayCreateInstanceOfOpenGenericType ()
3380 Array.CreateInstance (typeof (Foo<>), 42);
3384 [ExpectedException (typeof (IndexOutOfRangeException))]
3385 public void ClearNegativeLength ()
3387 Array.Clear (new int [] { 1, 2 }, 0, -1);
3391 [ExpectedException (typeof (ArgumentException))]
3392 public void MultiDimension_IList_setItem ()
3394 IList array = new int [1, 1];
3399 [ExpectedException (typeof (ArgumentException))]
3400 public void MultiDimension_IList_getItem ()
3402 IList array = new int [1, 1];
3403 int a = (int) array [0];
3407 public void SetValue_Nullable () {
3408 Array array = Array.CreateInstance (typeof (int?), 7);
3412 array.SetValue (o, 0);
3413 Assert.AreEqual (42, array.GetValue (0));
3415 array.SetValue (null, 0);
3416 Assert.AreEqual (null, array.GetValue (0));
3420 public void SortNullsWithGenericVersion ()
3422 string[] s1 = new string[6]{
3430 string[] s2 = new string[]{null,
3437 Array.Sort<string> (s1);
3438 for (int i = 0; i < 6; i++) {
3439 Assert.AreEqual (s1[i], s2[i], "At:" + i);
3444 // This is a test case for the case that was broken by the code contributed
3447 // This tests the fix for: #622101
3450 public void SortActuallyWorks ()
3452 string[] data = new string[9]{"Foo", "Bar", "Dingus", null, "Dingu4", "123", "Iam", null, "NotNull"};
3453 IComparer comparer = new NullAtEndComparer ();
3454 Array.Sort (data, comparer);
3456 Assert.AreEqual (data [7], null);
3457 Assert.AreNotEqual (data [0], null);
3460 class NullAtEndComparer : IComparer {
3461 public int Compare(object x, object y)
3463 if (x == null) return 1;
3464 if (y == null) return -1;
3465 return ((string)x).CompareTo((string)y);
3470 public void UnalignedArrayClear ()
3472 byte[] input = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
3473 byte[] expected = new byte[] { 1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
3474 Array.Clear (input, 5, 11);
3476 Assert.AreEqual (input, expected);
3480 [ExpectedException (typeof (ArgumentException))]
3481 public void CompareToWithJaggedArray () {
3482 IStructuralComparable a = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }};
3483 IStructuralComparable b = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }};
3484 a.CompareTo (b, Comparer<object>.Default);
3488 [ExpectedException (typeof (ArgumentException))]
3489 public void CompareToWithArrayOfTheWrongKind () {
3490 IStructuralComparable a = new int[] { 1, 2 };
3491 IStructuralComparable b = new double[] { 1, 2 };
3492 a.CompareTo (b, Comparer<object>.Default);
3496 [ExpectedException (typeof (ArgumentException))]
3497 public void CompareToWithNonArrayType () {
3498 IStructuralComparable a = new int[] { 1, 2 };
3499 a.CompareTo (99, Comparer<object>.Default);
3503 [ExpectedException (typeof (ArgumentException))]
3504 public void CompareToWithNonArrayOfDifferentSize () {
3505 IStructuralComparable a = new int[] { 1, 2 };
3506 IStructuralComparable b = new int[] { 1, 2, 3 };
3507 a.CompareTo (b, Comparer<object>.Default);
3511 [ExpectedException (typeof (ArgumentException))]
3512 public void CompareToWithMultiDimArray1 () {
3513 IStructuralComparable a = new int [2,2] { {10, 10 }, { 10, 10 } };
3514 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3515 a.CompareTo (b, Comparer<object>.Default);
3519 [ExpectedException (typeof (ArgumentException))]
3520 public void CompareToWithMultiDimArray2 () {
3521 IStructuralComparable a = new int [2] { 10, 10 };
3522 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3523 a.CompareTo (b, Comparer<object>.Default);
3527 [ExpectedException (typeof (ArgumentException))]
3528 public void CompareToWithMultiDimArray3 () {
3529 IStructuralComparable a = new int [4] { 10, 10, 10, 10 };
3530 IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
3531 a.CompareTo (b, Comparer<object>.Default);
3535 [ExpectedException (typeof (IndexOutOfRangeException))]
3536 public void CompareToWithBoundedArray1 () {
3537 IStructuralComparable a = new int [2] { 10, 10 };
3538 Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 });
3539 IStructuralComparable b = ab;
3540 ab.SetValue (10, 5);
3541 ab.SetValue (10, 6);
3543 a.CompareTo (b, Comparer<object>.Default);
3547 [ExpectedException (typeof (IndexOutOfRangeException))]
3548 public void CompareToWithBoundedArray2 () {
3549 IStructuralComparable a = new int [2] { 10, 10 };
3550 Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 });
3551 IStructuralComparable b = ab;
3552 ab.SetValue (10, 5);
3553 ab.SetValue (10, 6);
3555 //Yes, CompareTo simply doesn't work with bounded arrays!
3556 b.CompareTo (b, Comparer<object>.Default);
3560 [ExpectedException (typeof (NullReferenceException))]
3561 public void CompareToWithNullComparer () {
3562 IStructuralComparable a = new int[] { 1, 2 };
3563 IStructuralComparable b = new int[] { 1, 2 };
3564 a.CompareTo (b, null);
3568 public void CompareToWithNullArray () {
3569 IStructuralComparable a = new int[] { 1, 2 };
3570 Assert.AreEqual (1, a.CompareTo (null, Comparer<object>.Default));
3574 public void CompareToWithGoodArrays () {
3575 IStructuralComparable a = new int[] { 10, 20 };
3576 Assert.AreEqual (0, a.CompareTo (a, Comparer<object>.Default));
3577 Assert.AreEqual (0, a.CompareTo (new int [] { 10, 20 }, Comparer<object>.Default));
3578 Assert.AreEqual (-1, a.CompareTo (new int [] { 11, 20 }, Comparer<object>.Default));
3579 Assert.AreEqual (-1, a.CompareTo (new int [] { 10, 21 }, Comparer<object>.Default));
3580 Assert.AreEqual (1, a.CompareTo (new int [] { 9, 20 }, Comparer<object>.Default));
3581 Assert.AreEqual (1, a.CompareTo (new int [] { 10, 19 }, Comparer<object>.Default));
3585 public void IStructuralEquatable_Equals ()
3587 IStructuralEquatable array = new int[] {1, 2, 3};
3588 IStructuralEquatable array2 = new int[] {1, 2, 3};
3589 Assert.AreEqual (false, array.Equals (null, null));
3590 Assert.AreEqual (true, array.Equals (array, null));
3591 Assert.AreEqual (true, array.Equals (array2, EqualityComparer<int>.Default));
3595 [ExpectedException (typeof (NullReferenceException))]
3596 public void IStructuralEquatable_Equals_NoComparer ()
3598 IStructuralEquatable array = new int[] {1, 2, 3};
3599 IStructuralComparable array2 = new int[] {1, 2, 3};
3600 array.Equals (array2, null);
3604 [ExpectedException (typeof (ArgumentException))]
3605 public void IStructuralEquatable_Equals_ComparerThrows ()
3607 IStructuralEquatable array = new int[] {1, 2, 3};
3608 IStructuralComparable array2 = new int[] {1, 2, 3};
3609 array.Equals (array2, EqualityComparer<long>.Default);
3613 [ExpectedException (typeof (ArgumentNullException))]
3614 public void IStructuralEquatable_GetHashCode_NullComparer ()
3616 IStructuralEquatable a = new int[] { 1, 2 };
3617 a.GetHashCode (null);
3620 class TestComparer_GetHashCode : IEqualityComparer
3624 bool IEqualityComparer.Equals (object x, object y)
3626 throw new NotImplementedException ();
3629 public int GetHashCode (object obj)
3636 public void IStructuralEquatable_GetHashCode ()
3638 IStructuralEquatable a = new int[] { 1, 2, 9 };
3640 var c = new TestComparer_GetHashCode ();
3642 Assert.AreEqual (3, c.Counter);
3647 public void JaggedArrayCtor ()
3649 var type = Type.GetType ("System.Object[][]");
3651 ConstructorInfo ctor = null;
3652 foreach (var c in type.GetConstructors ()) {
3653 if (c.GetParameters ().Length == 2)
3656 Assert.IsNotNull (ctor);
3657 var arr = (object[])ctor.Invoke (new object [] { 4, 10 });
3658 for (int i = 0; i < 4; ++i) {
3659 Assert.IsNotNull (arr [i]);
3660 Assert.AreEqual (10, ((object[])arr [i]).Length);