2002-04-09 Nick Drochak <ndrochak@gol.com>
[mono.git] / mcs / class / corlib / Test / System / ArrayTest.cs
index b289531de24a5a5ebe80b1aabd54ea7b0b5f9a1b..33e76c49a37bff92c21bccd8b0edec93810c0d6d 100644 (file)
@@ -90,14 +90,6 @@ public class ArrayTest : TestCase
                }\r
                Assert("#B02", errorThrown);\r
 \r
-               {\r
-                       char[] bad = {'d', 'a', 'd', 'a', 'c', 'a'};\r
-                       AssertEquals("#B03", -1, Array.BinarySearch(bad, 'c'));\r
-               }\r
-               {\r
-                       char[] bad = {'a', 'd', 'a', 'd', 'a', 'c', 'a'};\r
-                       AssertEquals("#B04", -2, Array.BinarySearch(bad, 'c'));\r
-               }\r
                {\r
                        char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};\r
                        Assert("#B05", \r
@@ -157,16 +149,6 @@ public class ArrayTest : TestCase
                }\r
                Assert("#B24", errorThrown);\r
 \r
-               // FIXME - see commented-out tests in TestBinarySearch1, above\r
-               \r
-               {\r
-                       char[] bad = {'z', 'z', 'd', 'a', 'd', 'a', 'c', 'a'};\r
-                       AssertEquals("#B25", -3, Array.BinarySearch(bad, 2, 6, 'c'));\r
-               }\r
-               {\r
-                       char[] bad = {'z', 'z', 'a', 'd', 'a', 'd', 'a', 'c', 'a'};\r
-                       AssertEquals("#B25", -4, Array.BinarySearch(bad, 2, 7, 'c'));\r
-               }\r
                {\r
                        char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};\r
                        Assert("#B26", \r
@@ -312,10 +294,6 @@ public class ArrayTest : TestCase
                                Array.Copy(o1, c1, 1);\r
                        } catch (InvalidCastException) {\r
                                errorThrown = true;\r
-                       } catch (ArrayTypeMismatchException) {\r
-                               // FIXME: Our implementation currently doesn't distinguish\r
-                               //        between InvalidCastException and ArrayTypeMismatchException\r
-                               errorThrown = true;\r
                        }\r
                        Assert("#E05", errorThrown);\r
                }\r
@@ -621,16 +599,213 @@ public class ArrayTest : TestCase
        }\r
 \r
        public void TestGetEnumerator() {\r
-               // FIXME: Not yet implemented\r
-               return;\r
                String[] s1 = {"this", "is", "a", "test"};\r
-               IEnumerator en = s1.GetEnumerator();\r
-               AssertNotNull("#G01", en);\r
+               IEnumerator en = s1.GetEnumerator ();\r
+               AssertNotNull ("#G01", en);\r
+\r
+               Assert ("#G02", en.MoveNext ());\r
+               AssertEquals ("#G03", "this", en.Current);\r
+               Assert ("#G04", en.MoveNext ());\r
+               AssertEquals ("#G05", "is", en.Current);\r
+               Assert ("#G06", en.MoveNext ());\r
+               AssertEquals ("#G07", "a", en.Current);\r
+               Assert ("#G08", en.MoveNext ());\r
+               AssertEquals ("#G09", "test", en.Current);\r
+               Assert ("#G10", !en.MoveNext ());\r
+\r
+               en.Reset ();\r
+               Assert("#G11", en.MoveNext ());\r
+               AssertEquals ("#G12", "this", en.Current);\r
+\r
+               // mutation does not invalidate array enumerator!\r
+               s1.SetValue ("change", 1);\r
+               Assert ("#G13", en.MoveNext ());\r
+               AssertEquals ("#G14", "change", en.Current);\r
+       }\r
+\r
+       public void TestGetEnumeratorMultipleDimension() {\r
+               String[,] s1 = {{"this", "is"}, {"a", "test"}};\r
+               IEnumerator en = s1.GetEnumerator ();\r
+               AssertNotNull ("#AA01", en);\r
+\r
+               Assert ("#AA02", en.MoveNext ());\r
+               AssertEquals ("#AA03", "this", en.Current);\r
+               Assert ("#AA04", en.MoveNext ());\r
+               AssertEquals ("#AA05", "is", en.Current);\r
+               Assert ("#AA06", en.MoveNext ());\r
+               AssertEquals ("#AA07", "a", en.Current);\r
+               Assert ("#AA08", en.MoveNext ());\r
+               AssertEquals ("#AA09", "test", en.Current);\r
+               Assert ("#AA10", !en.MoveNext ());\r
+\r
+               en.Reset ();\r
+               Assert("#AA11", en.MoveNext ());\r
+               AssertEquals ("#AA12", "this", en.Current);\r
+\r
+               int[] idxs = {0,1};\r
+               // mutation does not invalidate array enumerator!\r
+               s1.SetValue ("change", idxs);\r
+               Assert ("#AA13", en.MoveNext ());\r
+               AssertEquals ("#AA14", "change", en.Current);\r
+       }\r
 \r
-               for (int i = 0; i < s1.Length; i++) {\r
-                       en.MoveNext();\r
-                       AssertEquals("#G02", s1[i], en.Current);\r
+       public void TestGetEnumeratorNonZeroLowerBounds() {\r
+               int[] myLengthsArray = new int[2] { 3, 5 };\r
+               int[] myBoundsArray = new int[2] { 2, 3 };\r
+\r
+               Array myArray=Array.CreateInstance( typeof(String), myLengthsArray, myBoundsArray );\r
+               for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )\r
+                       for ( int j = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++ )  {\r
+                               int[] myIndicesArray = new int[2] { i, j };\r
+                               myArray.SetValue( Convert.ToString(i) + j, myIndicesArray );\r
+                       }\r
+               IEnumerator en = myArray.GetEnumerator ();\r
+               AssertNotNull ("#AB01", en);\r
+\r
+               // check the first couple of values\r
+               Assert ("#AB02", en.MoveNext ());\r
+               AssertEquals ("#AB03", "23", en.Current);\r
+               Assert ("#AB04", en.MoveNext ());\r
+               AssertEquals ("#AB05", "24", en.Current);\r
+\r
+               // then check the last element's value\r
+               string lastElement;\r
+               do {  \r
+                       lastElement = (string)en.Current;\r
+               } while (en.MoveNext());\r
+               AssertEquals ("#AB06", "47", lastElement);\r
+       }\r
+\r
+       public void TestIList_Add () {\r
+               int[] myLengthsArray = new int[2] { 3, 5 };\r
+               int[] myBoundsArray = new int[2] { 2, 3 };\r
+\r
+               Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );\r
+               try {\r
+                       ((IList)myArray).Add ("can not");\r
+                       Fail ("IList.Add should throw");    \r
                }\r
+               catch (NotSupportedException) {\r
+                       return;\r
+               }\r
+               catch (Exception) {\r
+                       Fail ("IList.Add threw wrong exception type");    \r
+               }\r
+\r
+               Fail("IList.Add shouldn't get this far");\r
+       }\r
+\r
+       public void TestIList_Insert () {\r
+               int[] myLengthsArray = new int[2] { 3, 5 };\r
+               int[] myBoundsArray = new int[2] { 2, 3 };\r
+\r
+               Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );\r
+               try {\r
+                       ((IList)myArray).Insert (0, "can not");\r
+                       Fail ("IList.Insert should throw");    \r
+               }\r
+               catch (NotSupportedException) {\r
+                       return;\r
+               }\r
+               catch (Exception) {\r
+                       Fail ("IList.Insert threw wrong exception type");    \r
+               }\r
+\r
+               Fail("IList.Insert shouldn't get this far");\r
+       }\r
+\r
+       public void TestIList_Remove () {\r
+               int[] myLengthsArray = new int[2] { 3, 5 };\r
+               int[] myBoundsArray = new int[2] { 2, 3 };\r
+\r
+               Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );\r
+               try {\r
+                       ((IList)myArray).Remove ("can not");\r
+                       Fail ("IList.Remove should throw");    \r
+               }\r
+               catch (NotSupportedException) {\r
+                       return;\r
+               }\r
+               catch (Exception) {\r
+                       Fail ("IList.Remove threw wrong exception type");    \r
+               }\r
+\r
+               Fail("IList.Remove shouldn't get this far");\r
+       }\r
+\r
+       public void TestIList_RemoveAt () {\r
+               int[] myLengthsArray = new int[2] { 3, 5 };\r
+               int[] myBoundsArray = new int[2] { 2, 3 };\r
+\r
+               Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );\r
+               try {\r
+                       ((IList)myArray).RemoveAt (0);\r
+                       Fail ("IList.RemoveAt should throw");    \r
+               }\r
+               catch (NotSupportedException) {\r
+                       return;\r
+               }\r
+               catch (Exception) {\r
+                       Fail ("IList.RemoveAt threw wrong exception type");    \r
+               }\r
+\r
+               Fail("IList.RemoveAt shouldn't get this far");\r
+       }\r
+\r
+       public void TestIList_Contains () {\r
+               int[] myLengthsArray = new int[2] { 3, 5 };\r
+               int[] myBoundsArray = new int[2] { 2, 3 };\r
+\r
+               Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );\r
+\r
+               try {\r
+                       bool b = ((IList)myArray).Contains ("23");\r
+                       Fail("IList.Contains should throw with multi-dimensional arrays");\r
+               }\r
+               catch (RankException) {\r
+                       int[] iArr = new int[3] { 1, 2, 3};\r
+                       // check the first and last items\r
+                       Assert("AC01", ((IList)iArr).Contains (1));\r
+                       Assert("AC02", ((IList)iArr).Contains (3));\r
+\r
+                       // and one that is definately not there\r
+                       Assert("AC03", !((IList)iArr).Contains (42));\r
+                       return;\r
+               }\r
+\r
+               Fail("Should not get here");\r
+       }\r
+\r
+       public void TestIList_IndexOf () {\r
+               int[] myLengthsArray = new int[2] { 3, 5 };\r
+               int[] myBoundsArray = new int[2] { 2, 3 };\r
+\r
+               Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );\r
+\r
+               try {\r
+                       bool b = ((IList)myArray).Contains ("23");\r
+                       Fail("IList.Contains should throw with multi-dimensional arrays");\r
+               }\r
+               catch (RankException) {\r
+                       int[] iArr = new int[3] { 1, 2, 3};\r
+                       // check the first and last items\r
+                       AssertEquals("AD01", 0, ((IList)iArr).IndexOf (1));\r
+                       AssertEquals("AD02", 2, ((IList)iArr).IndexOf (3));\r
+\r
+                       // and one that is definately not there\r
+                       AssertEquals("AD03", -1, ((IList)iArr).IndexOf (42));\r
+               }\r
+               catch (Exception) {\r
+                       Fail("Should not get here");\r
+               }\r
+\r
+               // check that wierd case whem lowerbound is Int32.MinValue,\r
+               // so that IndexOf() needs to return Int32.MaxValue when it cannot find the object\r
+               int[] myLengthArray = new int[1] { 3 };\r
+               int[] myBoundArray = new int[1] { Int32.MinValue };\r
+               Array myExtremeArray=Array.CreateInstance ( typeof(String), myLengthArray, myBoundArray );\r
+               AssertEquals("AD04", Int32.MaxValue, ((IList)myExtremeArray).IndexOf (42));\r
+\r
        }\r
 \r
        public void TestGetLength() {\r
@@ -1587,6 +1762,8 @@ public class ArrayTest : TestCase
                                1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0\r
                        };\r
 \r
+                       // SetValue\r
+\r
                        for (int i = 0; i < types.Length; i++) {\r
                                for (int j = 0; j < types.Length; j++) {\r
                                        Array array = Array.CreateInstance (types [j], 2);\r
@@ -1651,6 +1828,64 @@ public class ArrayTest : TestCase
 \r
                                Assert ("#M93(" + types [i] + ")", !errorThrown);\r
                        }\r
+\r
+                       // Copy\r
+\r
+                       for (int i = 0; i < types.Length; i++) {\r
+                               for (int j = 0; j < types.Length; j++) {\r
+                                       Array source = Array.CreateInstance (types [i], 2);\r
+                                       Array array = Array.CreateInstance (types [j], 2);\r
+\r
+                                       source.SetValue (vt[j][i], 0);\r
+                                       source.SetValue (vt[j][i], 1);\r
+\r
+                                       bool errorThrown = false;\r
+                                       try {\r
+                                               Array.Copy (source, array, 2);\r
+                                       } catch (ArrayTypeMismatchException) {\r
+                                               errorThrown = true;\r
+                                       }\r
+\r
+                                       int ex_index = (i * types.Length) + j;\r
+\r
+                                       AssertEquals ("#M94(" + types [i] + "," + types [j] + ")",\r
+                                                     errorThrown, arg_ex [ex_index] == 1);\r
+                               }\r
+                       }\r
+\r
+                       for (int i = 0; i < types.Length; i++) {\r
+                               Array source = Array.CreateInstance (types [i], 2);\r
+                               String[] array = new String [2];\r
+\r
+                               source.SetValue (va1 [i], 0);\r
+                               source.SetValue (va1 [i], 1);\r
+\r
+                               bool errorThrown = false;\r
+                               try {\r
+                                       Array.Copy (source, array, 2);\r
+                               } catch (ArrayTypeMismatchException) {\r
+                                       errorThrown = true;\r
+                               }\r
+\r
+                               Assert ("#M95(" + types [i] + ")", errorThrown);\r
+                       }\r
+\r
+                       for (int i = 0; i < types.Length; i++) {\r
+                               String[] source = new String [2];\r
+                               Array array = Array.CreateInstance (types [i], 2);\r
+\r
+                               source.SetValue (va2 [i], 0);\r
+                               source.SetValue (va2 [i], 1);\r
+\r
+                               bool errorThrown = false;\r
+                               try {\r
+                                       Array.Copy (source, array, 2);\r
+                               } catch (ArrayTypeMismatchException) {\r
+                                       errorThrown = true;\r
+                               }\r
+\r
+                               Assert ("#M96(" + types [i] + ")", errorThrown);\r
+                       }\r
                }\r
        }\r
 \r