2008-09-08 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mcs / class / corlib / Test / System / ArrayTest.cs
index a557c717c559a33c0cdd8435f83d2ada31e0f2a3..a18f160201bb95edc5574edd1cf9b18216c93a61 100644 (file)
@@ -12,10 +12,12 @@ using System;
 using System.Collections;
 using System.Globalization;
 
+#if NET_2_0
+using System.Collections.Generic;
+#endif
+
 namespace MonoTests.System
 {
-
-
        //Auxiliary Things
        enum enua  {hola,adios,mas,menos};
 
@@ -29,17 +31,26 @@ namespace MonoTests.System
 
        class BClass : AClass
        {
-    }
+       }
 
        class CClass : AClass
-    {
-    }
+       {
+       }
 
        struct AStruct
        {
                public string s;
                public string a;
        }
+       
+       class DataEqual
+       {
+               public override bool Equals (object obj)
+               {
+                       return true;
+               }
+       }
+               
        //End Auxiliary Things
 
 [TestFixture]
@@ -115,20 +126,16 @@ public class ArrayTest : Assertion
 
                {
                        char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
-                       Assert("#B05", 
-                              Array.BinarySearch(arr, 'c') >= 3);
-                       Assert("#B06", 
-                              Array.BinarySearch(arr, 'c') < 6);
+                       Assert("#B05", Array.BinarySearch(arr, 'c') >= 3);
+                       Assert("#B06", Array.BinarySearch(arr, 'c') < 6);
                }
                {
                        char[] arr = {'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
-                       AssertEquals("#B07", 
-                                    -4, Array.BinarySearch(arr, 'c'));
+                       AssertEquals("#B07", -4, Array.BinarySearch(arr, 'c'));
                }
                {
                        char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
-                       AssertEquals("#B08", 
-                                    -9, Array.BinarySearch(arr, 'e'));
+                       AssertEquals("#B08", -9, Array.BinarySearch(arr, 'e'));
                }
        }
 
@@ -176,20 +183,16 @@ public class ArrayTest : Assertion
 
                {
                        char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
-                       Assert("#B26", 
-                              Array.BinarySearch(arr, 2, 8, 'c') >= 5);
-                       Assert("#B27", 
-                              Array.BinarySearch(arr, 2, 8, 'c') < 8);
+                       Assert("#B26", Array.BinarySearch(arr, 2, 8, 'c') >= 5);
+                       Assert("#B27", Array.BinarySearch(arr, 2, 8, 'c') < 8);
                }
                {
                        char[] arr = {'z', 'z', 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
-                       AssertEquals("#B28", 
-                                    -6, Array.BinarySearch(arr, 2, 8, 'c'));
+                       AssertEquals("#B28", -6, Array.BinarySearch(arr, 2, 8, 'c'));
                }
                {
                        char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
-                       AssertEquals("#B29", 
-                                    -11, Array.BinarySearch(arr, 2, 8, 'e'));
+                       AssertEquals("#B29", -11, Array.BinarySearch(arr, 2, 8, 'e'));
                }
        }
 
@@ -203,6 +206,22 @@ public class ArrayTest : Assertion
                AssertEquals("#B30", 49, Array.BinarySearch(array, 10));
        }
 
+       [Test]
+       public void BinarySearch_NullValue () 
+       {
+               int[] array = new int[1];
+               AssertEquals ("I=a,o", -1, Array.BinarySearch (array, null));
+               AssertEquals ("I=a,o,c", -1, Array.BinarySearch (array, null, null));
+               AssertEquals ("I=a,i,i,o", -1, Array.BinarySearch (array, 0, 1, null));
+               AssertEquals ("I=a,i,i,o,c", -1, Array.BinarySearch (array, 0, 1, null,null));
+
+               object[] o = new object [3] { this, this, null };
+               AssertEquals ("O=a,o", -1, Array.BinarySearch (o, null));
+               AssertEquals ("O=a,o,c", -1, Array.BinarySearch (o, null, null));
+               AssertEquals ("O=a,i,i,o", -1, Array.BinarySearch (o, 0, 3, null));
+               AssertEquals ("O=a,i,i,o,c", -1, Array.BinarySearch (o, 0, 3, null, null));
+       }
+
        // TODO - testBinarySearch with explicit IComparer args
 
        [Test]
@@ -373,13 +392,11 @@ public class ArrayTest : Assertion
                char[] copy = new Char[4];
                Array.Copy(orig, copy, 4);
                for (int i = 0; i < orig.Length; i++) {
-                       AssertEquals("#E09(" + i + ")",
-                                    orig[i], copy[i]);
+                       AssertEquals("#E09(" + i + ")", orig[i], copy[i]);
                }
                Array.Clear(copy, 0, copy.Length);
                for (int i = 0; i < orig.Length; i++) {
-                       AssertEquals("#E10(" + i + ")",
-                                    (char)0, copy[i]);
+                       AssertEquals("#E10(" + i + ")", (char)0, copy[i]);
                }
                Array.Copy(orig, copy, 2);
                AssertEquals("#E11", orig[0], copy[0]);
@@ -418,8 +435,7 @@ public class ArrayTest : Assertion
                Array.Copy(orig, 1, copy, 1, 3);
                Assert("#E33", copy[0] != orig[0]);
                for (int i = 1; i < orig.Length; i++) {
-                       AssertEquals("#E34(" + i + ")",
-                                    orig[i], copy[i]);
+                       AssertEquals("#E34(" + i + ")", orig[i], copy[i]);
                }
                Array.Clear(copy, 0, copy.Length);
                Array.Copy(orig, 1, copy, 0, 2);
@@ -429,6 +445,17 @@ public class ArrayTest : Assertion
                Assert("#E38", copy[3] != orig[3]);
        }
 
+       [Test]
+       [ExpectedException (typeof (InvalidCastException))]
+       public void Copy_InvalidCast () {
+               object[] arr1 = new object [10];
+               Type[] arr2 = new Type [10];
+
+               arr1 [0] = new object ();
+
+               Array.Copy (arr1, 0, arr2, 0, 10);
+       }
+
        [Test]
        public void TestCopyTo() {
                {
@@ -450,6 +477,11 @@ public class ArrayTest : Assertion
                        } catch (ArgumentException) {
                                errorThrown = true;
                        }
+#if TARGET_JVM // This is really implementation dependent behaviour.
+                       catch (RankException) {
+                               errorThrown = true;
+                       }
+#endif // TARGET_JVM
                        Assert("#E62", errorThrown);
                }
                {
@@ -610,6 +642,7 @@ public class ArrayTest : Assertion
                        Assert("#F03b", errorThrown);
                }
 #endif
+#if !TARGET_JVM // Arrays lower bounds are not supported for TARGET_JVM
                {
                        bool errorThrown = false;
                        try {
@@ -619,6 +652,7 @@ public class ArrayTest : Assertion
                        }
                        Assert("#F04", errorThrown);
                }
+#endif // TARGET_JVM
                {
                        bool errorThrown = false;
                        try {
@@ -629,6 +663,7 @@ public class ArrayTest : Assertion
                        }
                        Assert("#F05", errorThrown);
                }
+#if !TARGET_JVM // CreateInstance with lower bounds not supported for TARGET_JVM
                {
                        bool errorThrown = false;
                        try {
@@ -666,23 +701,31 @@ public class ArrayTest : Assertion
                                AssertEquals("#F13(" + i + ")", src[i], array.GetValue(i+5));
                }
 
+               // Test that a 1 dimensional array with 0 lower bound is the
+               // same as an szarray
+               Type szarrayType = new int [10].GetType ();
+               Assert (szarrayType == (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0})).GetType ());
+               Assert (szarrayType != (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {1})).GetType ());
+#endif // TARGET_JVM
        }
        
        [Test]
-        [ExpectedException (typeof (ArgumentNullException))]
-        public void TestCreateInstance2 ()
-        {
-                Array a = Array.CreateInstance (typeof (Int32), (int[])null);
-        }
+       [ExpectedException (typeof (ArgumentNullException))]
+       public void TestCreateInstance2 ()
+       {
+               Array.CreateInstance (typeof (Int32), (int[])null);
+       }
 
-#if NET_1_1
        [Test]
-        [ExpectedException (typeof (NullReferenceException))]
-        public void TestCreateInstance2b ()
-        {
-                Array a = Array.CreateInstance (typeof (Int32), (long[])null);
-        }
+#if NET_2_0
+       [ExpectedException (typeof (ArgumentNullException))]
+#else
+       [ExpectedException (typeof (NullReferenceException))]
 #endif
+       public void TestCreateInstance2b ()
+       {
+               Array.CreateInstance (typeof (Int32), (long[])null);
+       }
 
        [Test]
        public void TestGetEnumerator() {
@@ -738,6 +781,7 @@ public class ArrayTest : Assertion
        }
 
        [Test]
+       [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
        public void TestGetEnumeratorNonZeroLowerBounds() {
                int[] myLengthsArray = new int[2] { 3, 5 };
                int[] myBoundsArray = new int[2] { 2, 3 };
@@ -766,6 +810,7 @@ public class ArrayTest : Assertion
        }
 
        [Test]
+       [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
        public void TestIList_Add () {
                int[] myLengthsArray = new int[2] { 3, 5 };
                int[] myBoundsArray = new int[2] { 2, 3 };
@@ -773,19 +818,20 @@ public class ArrayTest : Assertion
                Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
                try {
                        ((IList)myArray).Add ("can not");
-                       Fail ("IList.Add should throw");    
+                       Fail ("IList.Add should throw");
                }
                catch (NotSupportedException) {
                        return;
                }
                catch (Exception) {
-                       Fail ("IList.Add threw wrong exception type");    
+                       Fail ("IList.Add threw wrong exception type");
                }
 
                Fail("IList.Add shouldn't get this far");
        }
 
        [Test]
+       [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
        public void TestIList_Insert () {
                int[] myLengthsArray = new int[2] { 3, 5 };
                int[] myBoundsArray = new int[2] { 2, 3 };
@@ -793,19 +839,20 @@ public class ArrayTest : Assertion
                Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
                try {
                        ((IList)myArray).Insert (0, "can not");
-                       Fail ("IList.Insert should throw");    
+                       Fail ("IList.Insert should throw");
                }
                catch (NotSupportedException) {
                        return;
                }
                catch (Exception) {
-                       Fail ("IList.Insert threw wrong exception type");    
+                       Fail ("IList.Insert threw wrong exception type");
                }
 
                Fail("IList.Insert shouldn't get this far");
        }
 
        [Test]
+       [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
        public void TestIList_Remove () {
                int[] myLengthsArray = new int[2] { 3, 5 };
                int[] myBoundsArray = new int[2] { 2, 3 };
@@ -813,19 +860,20 @@ public class ArrayTest : Assertion
                Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
                try {
                        ((IList)myArray).Remove ("can not");
-                       Fail ("IList.Remove should throw");    
+                       Fail ("IList.Remove should throw");
                }
                catch (NotSupportedException) {
                        return;
                }
                catch (Exception) {
-                       Fail ("IList.Remove threw wrong exception type");    
+                       Fail ("IList.Remove threw wrong exception type");
                }
 
                Fail("IList.Remove shouldn't get this far");
        }
 
        [Test]
+       [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
        public void TestIList_RemoveAt () {
                int[] myLengthsArray = new int[2] { 3, 5 };
                int[] myBoundsArray = new int[2] { 2, 3 };
@@ -833,19 +881,20 @@ public class ArrayTest : Assertion
                Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
                try {
                        ((IList)myArray).RemoveAt (0);
-                       Fail ("IList.RemoveAt should throw");    
+                       Fail ("IList.RemoveAt should throw");
                }
                catch (NotSupportedException) {
                        return;
                }
                catch (Exception) {
-                       Fail ("IList.RemoveAt threw wrong exception type");    
+                       Fail ("IList.RemoveAt threw wrong exception type");
                }
 
                Fail("IList.RemoveAt shouldn't get this far");
        }
 
        [Test]
+       [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
        public void TestIList_Contains () {
                int[] myLengthsArray = new int[2] { 3, 5 };
                int[] myBoundsArray = new int[2] { 2, 3 };
@@ -871,6 +920,7 @@ public class ArrayTest : Assertion
        }
 
        [Test]
+       [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
        public void TestIList_IndexOf () {
                int[] myLengthsArray = new int[2] { 3, 5 };
                int[] myBoundsArray = new int[2] { 2, 3 };
@@ -927,14 +977,11 @@ public class ArrayTest : Assertion
                }
 
                char[] c2 = new Char[5];
-               AssertEquals("#H03", 
-                            5, c2.GetLength(0));
+               AssertEquals("#H03", 5, c2.GetLength(0));
 
                char[,] c3 = new Char[6,7];
-               AssertEquals("#H04", 
-                            6, c3.GetLength(0));
-               AssertEquals("#H05", 
-                            7, c3.GetLength(1));
+               AssertEquals("#H04", 6, c3.GetLength(0));
+               AssertEquals("#H05", 7, c3.GetLength(1));
        }
 
        [Test]
@@ -961,14 +1008,11 @@ public class ArrayTest : Assertion
                }
 
                char[] c1 = new Char[5];
-               AssertEquals("#H33", 
-                            0, c1.GetLowerBound(0));
+               AssertEquals("#H33", 0, c1.GetLowerBound(0));
 
                char[,] c2 = new Char[4,4];
-               AssertEquals("#H34", 
-                            0, c2.GetLowerBound(0));
-               AssertEquals("#H35", 
-                            0, c2.GetLowerBound(1));
+               AssertEquals("#H34", 0, c2.GetLowerBound(0));
+               AssertEquals("#H35", 0, c2.GetLowerBound(1));
        }
 
        [Test]
@@ -995,14 +1039,11 @@ public class ArrayTest : Assertion
                }
 
                char[] c1 = new Char[5];
-               AssertEquals("#H63", 
-                            4, c1.GetUpperBound(0));
+               AssertEquals("#H63", 4, c1.GetUpperBound(0));
 
                char[,] c2 = new Char[4,6];
-               AssertEquals("#H64", 
-                            3, c2.GetUpperBound(0));
-               AssertEquals("#H65", 
-                            5, c2.GetUpperBound(1));
+               AssertEquals("#H64", 3, c2.GetUpperBound(0));
+               AssertEquals("#H65", 5, c2.GetUpperBound(1));
        }
 
        [Test]
@@ -1086,7 +1127,7 @@ public class ArrayTest : Assertion
                for (int i = 0; i < c1.GetLength(0); i++) {
                        for (int j = 0; j < c1.GetLength(1); j++) {
                                AssertEquals("#I24(" + i + "," + j + ")",
-                                            c1[i,j], c1.GetValue(i, j));
+                                       c1[i,j], c1.GetValue(i, j));
                        }
                }
        }
@@ -1136,20 +1177,23 @@ public class ArrayTest : Assertion
                        for (int j = 0; j < c1.GetLength(1); j++) {
                                for (int k = 0; k < c1.GetLength(2); k++) {
                                        AssertEquals("#I44(" + i + "," + j + ")",
-                                                    c1[i,j,k], c1.GetValue(i,j,k));
+                                               c1[i,j,k], c1.GetValue(i,j,k));
                                }
                        }
                }
        }
 
-#if NET_1_1
        [Test]
-        [ExpectedException (typeof (NullReferenceException))]
-       public void TestGetValueLongArray() {
+#if NET_2_0
+       [ExpectedException (typeof (ArgumentNullException))]
+#else
+       [ExpectedException (typeof (NullReferenceException))]
+#endif
+       public void TestGetValueLongArray ()
+       {
                char[] c = new Char[2];
                c.GetValue((long [])null);
        }
-#endif
 
        [Test]
        public void TestGetValueN() {
@@ -1207,7 +1251,7 @@ public class ArrayTest : Assertion
                        for (int j = 0; j < c1.GetLength(1); j++) {
                                int[] coords = {i, j};
                                AssertEquals("#I65(" + i + "," + j + ")",
-                                            c1[i,j], c1.GetValue(coords));
+                                       c1[i,j], c1.GetValue(coords));
                        }
                }
        }
@@ -1332,6 +1376,16 @@ public class ArrayTest : Assertion
                AssertEquals("#J50", 3, Array.IndexOf(s1, "a", 1, 3));
        }
        
+       [Test]
+       public void TestIndexOf_CustomEqual ()
+       {
+               DataEqual[] test = new DataEqual [] { new DataEqual () };
+               AssertEquals (0, Array.IndexOf (test, "asdfas", 0));
+               
+               IList array = (IList)test;
+               AssertEquals (0, array.IndexOf ("asdfas"));
+       }
+       
        [Test]
        public void TestLastIndexOf1() {
                {
@@ -1360,6 +1414,8 @@ public class ArrayTest : Assertion
                AssertEquals("#K05", 0, Array.LastIndexOf(s1, "this"));
                AssertEquals("#K06", 4, Array.LastIndexOf(s1, "test"));
                AssertEquals("#K07", 3, Array.LastIndexOf(s1, "a"));
+
+               AssertEquals (-1, Array.LastIndexOf (new String [0], "foo"));
        }
 
        [Test]
@@ -1445,45 +1501,66 @@ public class ArrayTest : Assertion
                }
 
                String[] s1 = {"this", "is", "really", "a", "test"};
-               AssertEquals("#K45", 
-                            -1, Array.LastIndexOf(s1, null, 3, 3));
-               AssertEquals("#K46", 
-                            -1, Array.LastIndexOf(s1, "nothing", 3, 3));
-               AssertEquals("#K47", 
-                            -1, Array.LastIndexOf(s1, "this", 3, 3));
-               AssertEquals("#K48",
-                            1, Array.LastIndexOf(s1, "is", 3, 3));
-               AssertEquals("#K49", 
-                            -1, Array.LastIndexOf(s1, "test", 3, 3));
-               AssertEquals("#K50", 
-                            3, Array.LastIndexOf(s1, "a", 3, 3));
-       }
-
-       [Test]
-        [ExpectedException (typeof (ArgumentOutOfRangeException))]
-        public void TestLastIndexOf4 ()
-        {
-                short [] a = new short [] { 19, 238, 317, 6, 565, 0, -52, 60, -563, 753, 238, 238};
-                Array.LastIndexOf (a, 16, -1);
-        }
-
-        [Test]
-        public void TestLastIndexOf5 ()
-        {
-                char [] a = new char [] {'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'j', 'i', 'h'};
-                string s;
-                int retval;
-                bool error = false;
-
-                for (int i = a.Length - 1; i >= 0 ; i--) {
-                        s = i.ToString ();
-                        retval = Array.LastIndexOf(a, a [i], i, i + 1);
-                        if (retval != i)
-                                error = true;
-                }
-                Assert (!error);
-        }
-                           
+               AssertEquals("#K45", -1, Array.LastIndexOf(s1, null, 3, 3));
+               AssertEquals("#K46", -1, Array.LastIndexOf(s1, "nothing", 3, 3));
+               AssertEquals("#K47", -1, Array.LastIndexOf(s1, "this", 3, 3));
+               AssertEquals("#K48", 1, Array.LastIndexOf(s1, "is", 3, 3));
+               AssertEquals("#K49", -1, Array.LastIndexOf(s1, "test", 3, 3));
+               AssertEquals("#K50", 3, Array.LastIndexOf(s1, "a", 3, 3));
+       }
+
+       [Test]
+       public void TestLastIndexOf4 ()
+       {
+               short [] a = new short [] { 19, 238, 317, 6, 565, 0, -52, 60, -563, 753, 238, 238};
+               try {
+                       Array.LastIndexOf (a, (object)16, -1);
+                       NUnit.Framework.Assert.Fail ("#1");
+               } catch (ArgumentOutOfRangeException) { }
+               
+#if NET_2_0            
+               try {
+                       Array.LastIndexOf<short> (a, 16, -1);
+                       NUnit.Framework.Assert.Fail ("#2");
+               } catch (ArgumentOutOfRangeException) { }
+#endif         
+       }
+
+       [Test]
+       public void TestLastIndexOf5 ()
+       {
+               char [] a = new char [] {'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'j', 'i', 'h'};
+               string s;
+               int retval;
+               bool error = false;
+
+               for (int i = a.Length - 1; i >= 0 ; i--) {
+                       s = i.ToString ();
+                       retval = Array.LastIndexOf(a, a [i], i, i + 1);
+                       if (retval != i)
+                               error = true;
+               }
+               Assert (!error);
+       }
+
+       [Test]
+       [ExpectedException (typeof (ArgumentOutOfRangeException))]
+       public void LastIndexOf_StartIndexOverflow ()
+       {
+               // legal - no exception
+               byte[] array = new byte [16];
+               Array.LastIndexOf (array, this, Int32.MaxValue, 1);
+       }
+
+       [Test]
+       [ExpectedException (typeof (ArgumentOutOfRangeException))]
+       public void LastIndexOf_CountOverflow ()
+       {
+               // legal - no exception
+               byte[] array = new byte [16];
+               Array.LastIndexOf (array, this, 1, Int32.MaxValue);
+       }
+
        [Test]
        public void TestReverse() {
                {
@@ -1679,7 +1756,7 @@ public class ArrayTest : Assertion
                for (int i = 0; i < c1.GetLength(0); i++) {
                        for (int j = 0; j < c1.GetLength(1); j++) {
                                AssertEquals("#M24(" + i + "," + j + ")",
-                                            c1[i,j], c2[i, j]);
+                                       c1[i,j], c2[i, j]);
                        }
                }
        }
@@ -1731,21 +1808,23 @@ public class ArrayTest : Assertion
                        for (int j = 0; j < c1.GetLength(1); j++) {
                                for (int k = 0; k < c1.GetLength(2); k++) {
                                        AssertEquals("#M44(" + i + "," + j + " )",
-                                                    c1[i,j,k], c2[i,j,k]);
+                                               c1[i,j,k], c2[i,j,k]);
                                }
                        }
                }
        }
 
-#if NET_1_1
        [Test]
-        [ExpectedException (typeof (NullReferenceException))]
-       public void TestSetValueLongArray() {
+#if NET_2_0
+       [ExpectedException (typeof (ArgumentNullException))]
+#else
+       [ExpectedException (typeof (NullReferenceException))]
+#endif
+       public void TestSetValueLongArray ()
+       {
                char[] c = new Char[2];
                c.SetValue("buh", (long [])null);
        }
-#endif
-
 
        [Test]
        public void TestSetValueN() {
@@ -1805,7 +1884,7 @@ public class ArrayTest : Assertion
                for (int i = 0; i < c1.GetLength(0); i++) {
                        for (int j = 0; j < c1.GetLength(1); j++) {
                                AssertEquals("#M65(" + i + "," + j + ")",
-                                            c1[i,j], c2[i,j]);
+                                       c1[i,j], c2[i,j]);
                        }
                }
        }
@@ -1950,7 +2029,7 @@ public class ArrayTest : Assertion
                                        int ex_index = (i * types.Length) + j;
 
                                        AssertEquals ("#M90(" + types [i] + "," + types [j] + ")",
-                                                     errorThrown, arg_ex [ex_index] == 1);
+                                               errorThrown, arg_ex [ex_index] == 1);
                                }
                        }
 
@@ -2019,7 +2098,7 @@ public class ArrayTest : Assertion
                                        int ex_index = (i * types.Length) + j;
 
                                        AssertEquals ("#M94(" + types [i] + "," + types [j] + ")",
-                                                     errorThrown, arg_ex [ex_index] == 1);
+                                               errorThrown, arg_ex [ex_index] == 1);
                                }
                        }
 
@@ -2449,13 +2528,25 @@ public class ArrayTest : Assertion
                Array.Sort (arrsort, null, 0, arrsort.Length, null);
        }
 
-        [Test]
-        [ExpectedException (typeof (InvalidOperationException))]
-        public void MoreSort10 ()
-        {
-                object [] array = {true, 'k', SByte.MinValue, Byte.MinValue, (short) 2, 634, (long) 436, (float) 1.1, 1.23, "Hello World"};
-                Array.Sort (array, (IComparer) null);
-        }
+       [Test]
+       [ExpectedException (typeof (InvalidOperationException))]
+       public void MoreSort10 ()
+       {
+               object [] array = {true, 'k', SByte.MinValue, Byte.MinValue, (short) 2, 634, (long) 436, (float) 1.1, 1.23, "Hello World"};
+               Array.Sort (array, (IComparer) null);
+       }
+
+       [Test] // bug #81941
+       public void Sort ()
+       {
+               double [] a = new double [2] { 0.9, 0.3 };
+               uint [] b = new uint [2] { 4, 7 };
+               Array.Sort (a, b);
+               AssertEquals ("#1", 0.3, a [0]);
+               AssertEquals ("#2", 0.9, a [1]);
+               AssertEquals ("#3", 7, b [0]);
+               AssertEquals ("#4", 4, b [1]);
+       }
 
        [Test]
        public void ClearJaggedArray () 
@@ -2491,6 +2582,437 @@ public class ArrayTest : Assertion
                byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
                Array.Clear (matrix, 0, 5);
        }
-}
 
+       [Test]
+       [ExpectedException (typeof (IndexOutOfRangeException))]
+       public void Clear_IndexOverflow () 
+       {
+               byte[] array = new byte [16];
+               Array.Clear (array, 4, Int32.MaxValue);
+       }
+
+       [Test]
+       [ExpectedException (typeof (IndexOutOfRangeException))]
+       public void Clear_LengthOverflow () 
+       {
+               byte[] array = new byte [16];
+               Array.Clear (array, Int32.MaxValue, 4);
+       }
+
+       [Test]
+       [ExpectedException (typeof (ArgumentException))]
+       public void Copy_SourceIndexOverflow () 
+       {
+               byte[] array = new byte [16];
+               Array.Copy (array, Int32.MaxValue, array, 8, 8);
+       }
+
+       [Test]
+       [ExpectedException (typeof (ArgumentException))]
+       public void Copy_DestinationIndexOverflow () 
+       {
+               byte[] array = new byte [16];
+               Array.Copy (array, 8, array, Int32.MaxValue, 8);
+       }
+
+       [Test]
+       [ExpectedException (typeof (ArgumentException))]
+       public void Copy_LengthOverflow () 
+       {
+               byte[] array = new byte [16];
+               Array.Copy (array, 8, array, 8, Int32.MaxValue);
+       }
+
+       [Test]
+       [ExpectedException (typeof (ArgumentException))]
+       public void Reverse_IndexOverflow () 
+       {
+               byte[] array = new byte [16];
+               Array.Reverse (array, Int32.MaxValue, 8);
+       }
+
+       [Test]
+       [ExpectedException (typeof (ArgumentException))]
+       public void Reverse_LengthOverflow () 
+       {
+               byte[] array = new byte [16];
+               Array.Reverse (array, 8, Int32.MaxValue);
+       }
+       
+       public struct CharX : IComparable {
+               public char c;
+       
+               public CharX (char c)
+               {
+                       this.c = c;
+               }
+       
+               public int CompareTo (object obj)
+               {
+                       if (obj is CharX)
+                               return c.CompareTo (((CharX) obj).c);
+                       else
+                               return c.CompareTo (obj);
+               }
+       }
+
+       [Test]
+       public void BinarySearch_ArgPassingOrder ()
+       {
+               //
+               // This tests that arguments are passed to the comprer in the correct
+               // order. The IComparable of the *array* elements must get called, not
+               // that of the search object.
+               //
+               CharX [] x = { new CharX ('a'), new CharX ('b'), new CharX ('c') };
+               AssertEquals (1, Array.BinarySearch (x, 'b'));
+       }
+
+       class Comparer: IComparer {
+
+               private bool called = false;
+
+               public bool Called {
+                       get {
+                               bool result = called;
+                               called = false;
+                               return called;
+                       }
+               }
+
+               public int Compare (object x, object y)
+               {
+                       called = true;
+                       return 0;
+               }
+       }
+
+       [Test]
+       public void BinarySearch1_EmptyList ()
+       {
+               int[] array = new int[0];
+               AssertEquals ("BinarySearch", - 1, Array.BinarySearch (array, 0));
+       }
+
+       [Test]
+       public void BinarySearch2_EmptyList ()
+       {
+               int[] array = new int[0];
+               AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, 0, 0));
+       }
+
+       [Test]
+       public void BinarySearch3_EmptyList ()
+       {
+               Comparer comparer = new Comparer ();
+               int[] array = new int[0];
+               AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, comparer));
+               // bug 77030 - the comparer isn't called for an empty array/list
+               Assert ("Called", !comparer.Called);
+       }
+
+       [Test]
+       public void BinarySearch4_EmptyList ()
+       {
+               Comparer comparer = new Comparer ();
+               int[] array = new int[0];
+               AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, 0, comparer));
+               // bug 77030 - the comparer isn't called for an empty array/list
+               Assert ("Called", !comparer.Called);
+       }
+
+#if NET_2_0
+       [Test]
+       [ExpectedException (typeof (ArgumentNullException))]
+       public void AsReadOnly_NullArray ()
+       {
+               Array.AsReadOnly <int> (null);
+       }
+
+       [Test]
+       public void ReadOnly_Count ()
+       {
+               AssertEquals (10, Array.AsReadOnly (new int [10]).Count);
+       }
+
+       [Test]
+       public void ReadOnly_Contains ()
+       {
+               int[] arr = new int [2];
+               arr [0] = 3;
+               arr [1] = 5;
+               IList<int> a = Array.AsReadOnly (arr);
+
+               Assert (a.Contains (3));
+               Assert (!a.Contains (6));
+       }
+
+       [Test]
+       public void ReadOnly_IndexOf ()
+       {
+               int[] arr = new int [2];
+               arr [0] = 3;
+               arr [1] = 5;
+               IList<int> a = Array.AsReadOnly (arr);
+
+               AssertEquals (0, a.IndexOf (3));
+               AssertEquals (1, a.IndexOf (5));
+               AssertEquals (-1, a.IndexOf (6));
+       }
+
+       [Test]
+       public void ReadOnly_Indexer ()
+       {
+               int[] arr = new int [2];
+               arr [0] = 3;
+               arr [1] = 5;
+               IList<int> a = Array.AsReadOnly (arr);
+
+               AssertEquals (3, a [0]);
+               AssertEquals (5, a [1]);
+
+               /* Check that modifications to the original array are visible */
+               arr [0] = 6;
+               AssertEquals (6, a [0]);
+       }
+
+       [Test]
+       public void ReadOnly_Enumerator ()
+       {
+               int[] arr = new int [10];
+
+               for (int i = 0; i < 10; ++i)
+                       arr [i] = i;
+
+               int sum = 0;
+               foreach (int i in Array.AsReadOnly (arr))
+                       sum += i;
+
+               AssertEquals (45, sum);
+       }
+
+       [Test]
+       public void Resize ()
+       {
+               int [] arr = new int [] { 1, 3, 5 };
+               Array.Resize <int> (ref arr, 3);
+               AssertEquals ("#A1", 3, arr.Length);
+               AssertEquals ("#A2", 1, arr [0]);
+               AssertEquals ("#A3", 3, arr [1]);
+               AssertEquals ("#A4", 5, arr [2]);
+
+               Array.Resize <int> (ref arr, 2);
+               AssertEquals ("#B1", 2, arr.Length);
+               AssertEquals ("#B2", 1, arr [0]);
+               AssertEquals ("#B3", 3, arr [1]);
+
+               Array.Resize <int> (ref arr, 4);
+               AssertEquals ("#C1", 4, arr.Length);
+               AssertEquals ("#C2", 1, arr [0]);
+               AssertEquals ("#C3", 3, arr [1]);
+               AssertEquals ("#C4", 0, arr [2]);
+               AssertEquals ("#C5", 0, arr [3]);
+       }
+
+       [Test]
+       public void Resize_null ()
+       {
+               int [] arr = null;
+               Array.Resize (ref arr, 10);
+               AssertEquals (arr.Length, 10);
+       }
+
+       [Test]
+       public void Test_ContainsAndIndexOf_EquatableItem ()
+       {
+               EquatableClass[] list = new EquatableClass[] {new EquatableClass (0), new EquatableClass (1), new EquatableClass (0)};
+
+               AssertEquals ("#0", 0, Array.IndexOf<EquatableClass> (list, list[0]));
+               AssertEquals ("#1", 0, Array.IndexOf<EquatableClass> (list, new EquatableClass (0)));
+               AssertEquals ("#2", 2, Array.LastIndexOf<EquatableClass> (list, list[0]));
+               AssertEquals ("#3", 2, Array.LastIndexOf<EquatableClass> (list, new EquatableClass (0)));
+       }
+
+       public class EquatableClass : IEquatable<EquatableClass>
+       {
+               int _x;
+               public EquatableClass (int x)
+               {
+                       _x = x;
+               }
+
+               public bool Equals (EquatableClass other)
+               {
+                       return this._x == other._x;
+               }
+       }
+
+       [Test]
+       public void AsIList ()
+       {
+               IList<int> arr = new int [10];
+               arr [0] = 5;
+               AssertEquals (5, arr [0]);
+
+               IList<FooStruct> arr2 = new FooStruct [10];
+               FooStruct s = new FooStruct ();
+               s.i = 11;
+               s.j = 22;
+               arr2 [5] = s;
+               s = arr2 [5];
+               AssertEquals (11, s.i);
+               AssertEquals (22, s.j);
+
+               IList<string> arr3 = new string [10];
+               arr3 [5] = "ABC";
+               AssertEquals ("ABC", arr3 [5]);
+       }
+
+       struct FooStruct {
+               public int i, j;
+       }
+
+#if !TARGET_JVM // BugBUG: T[] is not yet ICollection<T> under TARGET_JVM
+       [Test]
+       // From bug #80563
+       public void ICollectionNull ()
+       {
+               ICollection<object> test;
+               
+               test = new List<object>();
+               AssertEquals ("list<o>", test.Contains (null), false);
+
+               test = new object[] {};
+               AssertEquals ("empty array", test.Contains (null), false);
+
+               test = new object[] {null};
+               AssertEquals ("array with null", test.Contains (null), true);
+
+               test = new List<object>(test);
+               AssertEquals ("List<object> with test", test.Contains (null), true);
+               
+               test = new object[] {new object()};
+               AssertEquals ("array with object", test.Contains (null), false);
+
+               test = new List<object>(test);
+               AssertEquals ("array with test", test.Contains (null), false);
+       }
+#endif // TARGET_JVM
+#endif
+
+       #region Bug 80299
+
+       enum ByteEnum : byte {}
+       enum IntEnum : int {}
+
+       [Test]
+       public void TestByteEnumArrayToByteArray ()
+       {
+               ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
+               byte[] b = new byte[a.Length];
+               a.CopyTo (b, 0);
+       }
+
+       [Test]
+       public void TestByteEnumArrayToIntArray ()
+       {
+               ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
+               int[] b = new int[a.Length];
+               a.CopyTo (b, 0);
+       }
+
+       [Test]
+       [ExpectedException (typeof (ArrayTypeMismatchException))]
+       public void TestIntEnumArrayToByteArray ()
+       {
+               IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
+               byte[] b = new byte[a.Length];
+               a.CopyTo (b, 0);
+       }
+
+       [Test]
+       public void TestIntEnumArrayToIntArray ()
+       {
+               IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
+               int[] b = new int[a.Length];
+               a.CopyTo (b, 0);
+       }
+
+       #endregion
+
+#if NET_2_0
+       [Test] // bug #322248
+       public void IEnumerator_Reset ()
+       {
+               int[] array = new int[] { 1, 2, 3};
+               IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
+               Assert ("#A1", e.MoveNext ());
+               AssertEquals ("#A2", 1, e.Current);
+               Assert ("#A3", e.MoveNext ());
+               AssertEquals ("#A4", 2, e.Current);
+
+               e.Reset ();
+
+               Assert ("#C1", e.MoveNext ());
+               AssertEquals ("#C2", 1, e.Current);
+       }
+
+       [Test]
+       public void IEnumerator_Current_Finished ()
+       {
+               int[] array = new int[] { 1, 2, 3 };
+               IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
+               Assert ("#A1", e.MoveNext ());
+               AssertEquals ("#A2", 1, e.Current);
+               Assert ("#A3", e.MoveNext ());
+               AssertEquals ("#A4", 2, e.Current);
+               Assert ("#A5", e.MoveNext ());
+               AssertEquals ("#A6", 3, e.Current);
+               Assert ("#A6", !e.MoveNext ());
+
+               try {
+                       Fail ("#B1:" + e.Current);
+               } catch (InvalidOperationException ex) {
+                       // Enumeration already finished
+                       AssertEquals ("#B2", typeof (InvalidOperationException), ex.GetType ());
+                       AssertNull ("#B3", ex.InnerException);
+                       AssertNotNull ("#B4", ex.Message);
+               }
+       }
+
+       [Test]
+       public void IEnumerator_Current_NotStarted ()
+       {
+               int[] array = new int[] { 1, 2, 3 };
+               IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
+
+               try {
+                       Fail ("#A1:" + e.Current);
+               } catch (InvalidOperationException ex) {
+                       // Enumeration has not started. Call MoveNext
+                       AssertEquals ("#A2", typeof (InvalidOperationException), ex.GetType ());
+                       AssertNull ("#A3", ex.InnerException);
+                       AssertNotNull ("#A4", ex.Message);
+               }
+       }
+
+       [Test]
+       public void IEnumerator_Current_Reset ()
+       {
+               int[] array = new int[] { 1, 2, 3 };
+               IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
+               e.MoveNext ();
+               e.Reset ();
+
+               try {
+                       Fail ("#B1:" + e.Current);
+               } catch (InvalidOperationException ex) {
+                       // Enumeration has not started. Call MoveNext
+                       AssertEquals ("#B2", typeof (InvalidOperationException), ex.GetType ());
+                       AssertNull ("#B3", ex.InnerException);
+                       AssertNotNull ("#B4", ex.Message);
+               }
+       }
+#endif
+}
 }