X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2Fcorlib%2FTest%2FSystem%2FArrayTest.cs;h=c0fba675eabf9de0e4eef7e6e90bdb94daf102d3;hb=c39718bbb394fe97281e6e64945b4572bef29121;hp=49d31b5906a18ce2df9f509aa52c6f08bbe69474;hpb=53e266903ec6b2d822cf5b0c566f6374df5307a4;p=mono.git diff --git a/mcs/class/corlib/Test/System/ArrayTest.cs b/mcs/class/corlib/Test/System/ArrayTest.cs index 49d31b5906a..eab4c40e143 100644 --- a/mcs/class/corlib/Test/System/ArrayTest.cs +++ b/mcs/class/corlib/Test/System/ArrayTest.cs @@ -11,15 +11,11 @@ using NUnit.Framework; using System; using System.Collections; using System.Globalization; - -#if NET_2_0 +using System.Reflection; using System.Collections.Generic; -#endif namespace MonoTests.System { - - //Auxiliary Things enum enua {hola,adios,mas,menos}; @@ -33,70 +29,98 @@ 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; + } + + public override int GetHashCode () + { + return 0; + } + } + //End Auxiliary Things [TestFixture] -public class ArrayTest : Assertion +public class ArrayTest { char [] arrsort = {'d', 'b', 'f', 'e', 'a', 'c'}; - public ArrayTest() {} + interface I + { + } + + class C + { + } + + class DC : C + { + } + + class DI : I + { + } [Test] public void TestIsFixedSize() { char[] a1 = {'a'}; - Assert("All arrays are fixed", a1.IsFixedSize); + Assert.IsTrue (a1.IsFixedSize, "All arrays are fixed"); } [Test] public void TestIsReadOnly() { char[] a1 = {'a'}; - Assert("No array is readonly", !a1.IsReadOnly); + Assert.IsTrue (!a1.IsReadOnly, "No array is readonly"); } [Test] public void TestIsSynchronized() { char[] a1 = {'a'}; - Assert("No array is synchronized", !a1.IsSynchronized); + Assert.IsTrue (!a1.IsSynchronized, "No array is synchronized"); } [Test] public void TestLength() { { char[] a1 = { }; - AssertEquals("Zero length array", 0, a1.Length); + Assert.AreEqual (0, a1.Length, "Zero length array"); } { char[] a1 = {'c'}; - AssertEquals("One-length array", 1, a1.Length); + Assert.AreEqual (1, a1.Length, "One-length array"); } { char[] a1 = {'c', 'c'}; - AssertEquals("Two-length array", 2, a1.Length); + Assert.AreEqual (2, a1.Length, "Two-length array"); } } [Test] public void TestRank() { char[] a1 = { 'c', 'd', 'e' }; - AssertEquals("Rank one", 1, a1.Rank); + Assert.AreEqual (1, a1.Rank, "Rank one"); char[,] a2 = new Char[3,3]; - AssertEquals("Rank two", 2, a2.Rank); + Assert.AreEqual (2, a2.Rank, "Rank two"); char[,,] a3 = new Char[3,3,3]; - AssertEquals("Rank three", 3, a3.Rank); + Assert.AreEqual (3, a3.Rank, "Rank three"); } [Test] @@ -107,7 +131,7 @@ public class ArrayTest : Assertion } catch (ArgumentNullException) { errorThrown = true; } - Assert("#B01", errorThrown); + Assert.IsTrue (errorThrown, "#B01"); errorThrown = false; try { char[,] c1 = new Char[2,2]; @@ -115,24 +139,20 @@ public class ArrayTest : Assertion } catch (RankException) { errorThrown = true; } - Assert("#B02", errorThrown); + Assert.IsTrue (errorThrown, "#B02"); { 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.IsTrue (Array.BinarySearch(arr, 'c') >= 3, "#B05"); + Assert.IsTrue (Array.BinarySearch(arr, 'c') < 6, "#B06"); } { char[] arr = {'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'}; - AssertEquals("#B07", - -4, Array.BinarySearch(arr, 'c')); + Assert.AreEqual (-4, Array.BinarySearch(arr, 'c'), "#B07"); } { char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'}; - AssertEquals("#B08", - -9, Array.BinarySearch(arr, 'e')); + Assert.AreEqual (-9, Array.BinarySearch(arr, 'e'), "#B08"); } } @@ -144,7 +164,7 @@ public class ArrayTest : Assertion } catch (ArgumentNullException) { errorThrown = true; } - Assert("#B20", errorThrown); + Assert.IsTrue (errorThrown, "#B20"); errorThrown = false; try { char[,] c1 = new Char[2,2]; @@ -152,7 +172,7 @@ public class ArrayTest : Assertion } catch (RankException) { errorThrown = true; } - Assert("#B21", errorThrown); + Assert.IsTrue (errorThrown, "#B21"); errorThrown = false; try { char[] c1 = {'a'}; @@ -160,7 +180,7 @@ public class ArrayTest : Assertion } catch (ArgumentOutOfRangeException) { errorThrown = true; } - Assert("#B22", errorThrown); + Assert.IsTrue (errorThrown, "#B22"); errorThrown = false; try { char[] c1 = {'a'}; @@ -168,7 +188,7 @@ public class ArrayTest : Assertion } catch (ArgumentOutOfRangeException) { errorThrown = true; } - Assert("#B23", errorThrown); + Assert.IsTrue (errorThrown, "#B23"); errorThrown = false; try { char[] c1 = {'a'}; @@ -176,24 +196,20 @@ public class ArrayTest : Assertion } catch (ArgumentException) { errorThrown = true; } - Assert("#B24", errorThrown); + Assert.IsTrue (errorThrown, "#B24"); { 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.IsTrue (Array.BinarySearch(arr, 2, 8, 'c') >= 5, "#B26"); + Assert.IsTrue (Array.BinarySearch(arr, 2, 8, 'c') < 8, "#B27"); } { char[] arr = {'z', 'z', 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'}; - AssertEquals("#B28", - -6, Array.BinarySearch(arr, 2, 8, 'c')); + Assert.AreEqual (-6, Array.BinarySearch(arr, 2, 8, 'c'), "#B28"); } { char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'}; - AssertEquals("#B29", - -11, Array.BinarySearch(arr, 2, 8, 'e')); + Assert.AreEqual (-11, Array.BinarySearch(arr, 2, 8, 'e'), "#B29"); } } @@ -204,26 +220,42 @@ public class ArrayTest : Assertion for (int i = 0; i < 100; i++) array[i] = 10; - AssertEquals("#B30", 49, Array.BinarySearch(array, 10)); + Assert.AreEqual (49, Array.BinarySearch(array, 10), "#B30"); } [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)); + Assert.AreEqual (-1, Array.BinarySearch (array, null), "I=a,o"); + Assert.AreEqual (-1, Array.BinarySearch (array, null, null), "I=a,o,c"); + Assert.AreEqual (-1, Array.BinarySearch (array, 0, 1, null), "I=a,i,i,o"); + Assert.AreEqual (-1, Array.BinarySearch (array, 0, 1, null, null), "I=a,i,i,o,c"); 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)); + Assert.AreEqual (-1, Array.BinarySearch (o, null), "O=a,o"); + Assert.AreEqual (-1, Array.BinarySearch (o, null, null), "O=a,o,c"); + Assert.AreEqual (-1, Array.BinarySearch (o, 0, 3, null), "O=a,i,i,o"); + Assert.AreEqual (-1, Array.BinarySearch (o, 0, 3, null, null), "O=a,i,i,o,c"); + } + + class TestComparer7 : IComparer + { + public int Compare (int x, int y) + { + if (y != 7) + throw new ApplicationException (); + + return x.CompareTo (y); + } } - // TODO - testBinarySearch with explicit IComparer args + [Test] + public void BinarySearch_WithComparer () + { + var a = new int[] { 2, 6, 9 }; + Assert.AreEqual (-3, Array.BinarySearch (a, 7, new TestComparer7 ())); + } [Test] public void TestClear() { @@ -233,48 +265,48 @@ public class ArrayTest : Assertion } catch (ArgumentNullException) { errorThrown = true; } - Assert("#C01", errorThrown); + Assert.IsTrue (errorThrown, "#C01"); int[] i1 = { 1, 2, 3, 4 }; { int[] compare = {1,2,3,4}; - AssertEquals("#C02", compare[0], i1[0]); - AssertEquals("#C03", compare[1], i1[1]); - AssertEquals("#C04", compare[2], i1[2]); - AssertEquals("#C05", compare[3], i1[3]); + Assert.AreEqual (compare[0], i1[0], "#C02"); + Assert.AreEqual (compare[1], i1[1], "#C03"); + Assert.AreEqual (compare[2], i1[2], "#C04"); + Assert.AreEqual (compare[3], i1[3], "#C05"); } Array.Clear(i1, 3, 1); { int[] compare = {1,2,3,0}; - AssertEquals("#C06", compare[0], i1[0]); - AssertEquals("#C07", compare[1], i1[1]); - AssertEquals("#C08", compare[2], i1[2]); - AssertEquals("#C09", compare[3], i1[3]); + Assert.AreEqual (compare[0], i1[0], "#C06"); + Assert.AreEqual (compare[1], i1[1], "#C07"); + Assert.AreEqual (compare[2], i1[2], "#C08"); + Assert.AreEqual (compare[3], i1[3], "#C09"); } Array.Clear(i1, 1, 1); { int[] compare = {1,0,3,0}; - AssertEquals("#C10", compare[0], i1[0]); - AssertEquals("#C11", compare[1], i1[1]); - AssertEquals("#C12", compare[2], i1[2]); - AssertEquals("#C13", compare[3], i1[3]); + Assert.AreEqual (compare[0], i1[0], "#C10"); + Assert.AreEqual (compare[1], i1[1], "#C11"); + Assert.AreEqual (compare[2], i1[2], "#C12"); + Assert.AreEqual (compare[3], i1[3], "#C13"); } Array.Clear(i1, 1, 3); { int[] compare = {1,0,0,0}; - AssertEquals("#C14", compare[0], i1[0]); - AssertEquals("#C15", compare[1], i1[1]); - AssertEquals("#C16", compare[2], i1[2]); - AssertEquals("#C17", compare[3], i1[3]); + Assert.AreEqual (compare[0], i1[0], "#C14"); + Assert.AreEqual (compare[1], i1[1], "#C15"); + Assert.AreEqual (compare[2], i1[2], "#C16"); + Assert.AreEqual (compare[3], i1[3], "#C17"); } string[] s1 = { "red", "green", "blue" }; Array.Clear(s1, 0, 3); { string[] compare = {null, null, null}; - AssertEquals("#C18", compare[0], s1[0]); - AssertEquals("#C19", compare[1], s1[1]); - AssertEquals("#C20", compare[2], s1[2]); + Assert.AreEqual (compare[0], s1[0], "#C18"); + Assert.AreEqual (compare[1], s1[1], "#C19"); + Assert.AreEqual (compare[2], s1[2], "#C20"); } } @@ -282,23 +314,46 @@ public class ArrayTest : Assertion public void TestClone() { char[] c1 = {'a', 'b', 'c'}; char[] c2 = (char[])c1.Clone(); - AssertEquals("#D01", c1[0], c2[0]); - AssertEquals("#D02", c1[1], c2[1]); - AssertEquals("#D03", c1[2], c2[2]); + Assert.AreEqual (c1[0], c2[0], "#D01"); + Assert.AreEqual (c1[1], c2[1], "#D02"); + Assert.AreEqual (c1[2], c2[2], "#D03"); char[] d10 = {'a', 'b'}; char[] d11 = {'a', 'c'}; char[] d12 = {'b', 'c'}; char[][] d1 = {d10, d11, d12}; char[][] d2 = (char[][])d1.Clone(); - AssertEquals("#D04", d1[0], d2[0]); - AssertEquals("#D05", d1[1], d2[1]); - AssertEquals("#D06", d1[2], d2[2]); + Assert.AreEqual (d1[0], d2[0], "#D04"); + Assert.AreEqual (d1[1], d2[1], "#D05"); + Assert.AreEqual (d1[2], d2[2], "#D06"); d1[0][0] = 'z'; - AssertEquals("#D07", d1[0], d2[0]); + Assert.AreEqual (d1[0], d2[0], "#D07"); } + [Test] + public void TestMemberwiseClone () { + int[] array = new int[] { 1, 2, 3 }; + MethodBase mi = array.GetType ().GetMethod("MemberwiseClone", + BindingFlags.Instance | BindingFlags.NonPublic); + int[] res = (int[])mi.Invoke (array, null); + Assert.AreEqual (3, res.Length); + } + + [Test] public void TestIndexer () + { + int [] a = new int [10]; + IList b = a; + try { + object c = b [-1]; + Assert.Fail ("IList.this [-1] should throw"); + } catch (IndexOutOfRangeException) { + // Good + } catch (Exception){ + Assert.Fail ("Should have thrown an IndexOutOfRangeException"); + } + } + [Test] public void TestCopy() { { @@ -309,7 +364,7 @@ public class ArrayTest : Assertion } catch (ArgumentNullException) { errorThrown = true; } - Assert("#E01", errorThrown); + Assert.IsTrue (errorThrown, "#E01"); } { bool errorThrown = false; @@ -319,7 +374,7 @@ public class ArrayTest : Assertion } catch (ArgumentNullException) { errorThrown = true; } - Assert("#E02", errorThrown); + Assert.IsTrue (errorThrown, "#E02"); } { bool errorThrown = false; @@ -330,7 +385,7 @@ public class ArrayTest : Assertion } catch (RankException) { errorThrown = true; } - Assert("#E03", errorThrown); + Assert.IsTrue (errorThrown, "#E03"); } { bool errorThrown = false; @@ -341,7 +396,7 @@ public class ArrayTest : Assertion } catch (ArrayTypeMismatchException) { errorThrown = true; } - Assert("#E04", errorThrown); + Assert.IsTrue (errorThrown, "#E04"); } { bool errorThrown = false; @@ -353,7 +408,7 @@ public class ArrayTest : Assertion } catch (InvalidCastException) { errorThrown = true; } - Assert("#E05", errorThrown); + Assert.IsTrue (errorThrown, "#E05"); } { bool errorThrown = false; @@ -364,7 +419,7 @@ public class ArrayTest : Assertion } catch (ArgumentOutOfRangeException) { errorThrown = true; } - Assert("#E06", errorThrown); + Assert.IsTrue (errorThrown, "#E06"); } { bool errorThrown = false; @@ -375,7 +430,7 @@ public class ArrayTest : Assertion } catch (ArgumentException) { errorThrown = true; } - Assert("#E07", errorThrown); + Assert.IsTrue (errorThrown, "#E07"); } { bool errorThrown = false; @@ -386,26 +441,24 @@ public class ArrayTest : Assertion } catch (ArgumentException) { errorThrown = true; } - Assert("#E08", errorThrown); + Assert.IsTrue (errorThrown, "#E08"); } char[] orig = {'a', 'b', 'd', 'a'}; 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]); + Assert.AreEqual (orig[i], copy[i], "#E09(" + i + ")"); } Array.Clear(copy, 0, copy.Length); for (int i = 0; i < orig.Length; i++) { - AssertEquals("#E10(" + i + ")", - (char)0, copy[i]); + Assert.AreEqual ((char)0, copy[i], "#E10(" + i + ")"); } Array.Copy(orig, copy, 2); - AssertEquals("#E11", orig[0], copy[0]); - AssertEquals("#E12", orig[1], copy[1]); - Assert("#E13", orig[2] != copy[2]); - Assert("#E14", orig[3] != copy[3]); + Assert.AreEqual (orig[0], copy[0], "#E11"); + Assert.AreEqual (orig[1], copy[1], "#E12"); + Assert.IsTrue (orig[2] != copy[2], "#E13"); + Assert.IsTrue (orig[3] != copy[3], "#E14"); } [Test] @@ -419,7 +472,7 @@ public class ArrayTest : Assertion } catch (ArgumentException) { errorThrown = true; } - Assert("#E31", errorThrown); + Assert.IsTrue (errorThrown, "#E31"); } { bool errorThrown = false; @@ -430,23 +483,43 @@ public class ArrayTest : Assertion } catch (ArgumentException) { errorThrown = true; } - Assert("#E32", errorThrown); + Assert.IsTrue (errorThrown, "#E32"); } char[] orig = {'a', 'b', 'd', 'a'}; char[] copy = new Char[4]; Array.Copy(orig, 1, copy, 1, 3); - Assert("#E33", copy[0] != orig[0]); + Assert.IsTrue (copy[0] != orig[0], "#E33"); for (int i = 1; i < orig.Length; i++) { - AssertEquals("#E34(" + i + ")", - orig[i], copy[i]); + Assert.AreEqual (orig[i], copy[i], "#E34(" + i + ")"); } Array.Clear(copy, 0, copy.Length); Array.Copy(orig, 1, copy, 0, 2); - AssertEquals("#E35", orig[1], copy[0]); - AssertEquals("#E36", orig[2], copy[1]); - Assert("#E37", copy[2] != orig[2]); - Assert("#E38", copy[3] != orig[3]); + Assert.AreEqual (orig[1], copy[0], "#E35"); + Assert.AreEqual (orig[2], copy[1], "#E36"); + Assert.IsTrue (copy[2] != orig[2], "#E37"); + Assert.IsTrue (copy[3] != orig[3], "#E38"); + } + + [Test] + public void Copy_InvalidCast () { + object[] arr1 = new object [10]; + Type[] arr2 = new Type [10]; + arr1 [0] = new object (); + + try { + Array.Copy (arr1, 0, arr2, 0, 10); + Assert.Fail ("#1"); + } catch (InvalidCastException) { + } + + var arr1_2 = new I [1] { new DI () }; + var arr2_2 = new C [1] { new DC () }; + try { + Array.Copy (arr2_2, arr1_2, 1); + Assert.Fail ("#1"); + } catch (InvalidCastException) { + } } [Test] @@ -459,7 +532,7 @@ public class ArrayTest : Assertion } catch (ArgumentNullException) { errorThrown = true; } - Assert("#E61", errorThrown); + Assert.IsTrue (errorThrown, "#E61"); } { bool errorThrown = false; @@ -470,7 +543,12 @@ public class ArrayTest : Assertion } catch (ArgumentException) { errorThrown = true; } - Assert("#E62", errorThrown); +#if TARGET_JVM // This is really implementation dependent behaviour. + catch (RankException) { + errorThrown = true; + } +#endif // TARGET_JVM + Assert.IsTrue (errorThrown, "#E62"); } { bool errorThrown = false; @@ -481,7 +559,7 @@ public class ArrayTest : Assertion } catch (RankException) { errorThrown = true; } - Assert("#E63", errorThrown); + Assert.IsTrue (errorThrown, "#E63"); } { bool errorThrown = false; @@ -492,7 +570,7 @@ public class ArrayTest : Assertion } catch (RankException) { errorThrown = true; } - Assert("#E64", errorThrown); + Assert.IsTrue (errorThrown, "#E64"); } { bool errorThrown = false; @@ -503,7 +581,7 @@ public class ArrayTest : Assertion } catch (ArgumentOutOfRangeException) { errorThrown = true; } - Assert("#E65", errorThrown); + Assert.IsTrue (errorThrown, "#E65"); } { bool errorThrown = false; @@ -514,7 +592,7 @@ public class ArrayTest : Assertion } catch (ArgumentException) { errorThrown = true; } - Assert("#E66", errorThrown); + Assert.IsTrue (errorThrown, "#E66"); } { bool errorThrown = false; @@ -525,7 +603,7 @@ public class ArrayTest : Assertion } catch (ArgumentException) { errorThrown = true; } - Assert("#E67", errorThrown); + Assert.IsTrue (errorThrown, "#E67"); } { @@ -543,23 +621,23 @@ public class ArrayTest : Assertion } catch (ArrayTypeMismatchException) { errorThrown = true; } - Assert("#E68", errorThrown); + Assert.IsTrue (errorThrown, "#E68"); } Char[] orig = {'a', 'b', 'c', 'd'}; Char[] copy = new Char[10]; Array.Clear(copy, 0, copy.Length); orig.CopyTo(copy, 3); - AssertEquals("#E69", (char)0, copy[0]); - AssertEquals("#E70", (char)0, copy[1]); - AssertEquals("#E71", (char)0, copy[2]); - AssertEquals("#E72", orig[0], copy[3]); - AssertEquals("#E73", orig[1], copy[4]); - AssertEquals("#E74", orig[2], copy[5]); - AssertEquals("#E75", orig[3], copy[6]); - AssertEquals("#E76", (char)0, copy[7]); - AssertEquals("#E77", (char)0, copy[8]); - AssertEquals("#E78", (char)0, copy[9]); + Assert.AreEqual ((char)0, copy[0], "#E69"); + Assert.AreEqual ((char)0, copy[1], "#E70"); + Assert.AreEqual ((char)0, copy[2], "#E71"); + Assert.AreEqual (orig[0], copy[3], "#E72"); + Assert.AreEqual (orig[1], copy[4], "#E73"); + Assert.AreEqual (orig[2], copy[5], "#E74"); + Assert.AreEqual (orig[3], copy[6], "#E75"); + Assert.AreEqual ((char)0, copy[7], "#E76"); + Assert.AreEqual ((char)0, copy[8], "#E77"); + Assert.AreEqual ((char)0, copy[9], "#E78"); { // The following is valid and must not throw an exception. @@ -571,7 +649,7 @@ public class ArrayTest : Assertion } catch (ArgumentException) { errorThrown = true; } - Assert("#E79", !errorThrown); + Assert.IsTrue (!errorThrown, "#E79"); } { @@ -586,7 +664,7 @@ public class ArrayTest : Assertion } catch (ArrayTypeMismatchException) { errorThrown = true; } - Assert("#E80", errorThrown); + Assert.IsTrue (errorThrown, "#E80"); } } @@ -599,7 +677,7 @@ public class ArrayTest : Assertion } catch (ArgumentNullException) { errorThrown = true; } - Assert("#F01", errorThrown); + Assert.IsTrue (errorThrown, "#F01"); } { bool errorThrown = false; @@ -608,7 +686,7 @@ public class ArrayTest : Assertion } catch (ArgumentOutOfRangeException) { errorThrown = true; } - Assert("#F02", errorThrown); + Assert.IsTrue (errorThrown, "#F02"); } { bool errorThrown = false; @@ -617,9 +695,9 @@ public class ArrayTest : Assertion } catch (ArgumentNullException) { errorThrown = true; } - Assert("#F03a", errorThrown); + Assert.IsTrue (errorThrown, "#F03a"); } -#if NET_1_1 + { bool errorThrown = false; try { @@ -627,9 +705,9 @@ public class ArrayTest : Assertion } catch (ArgumentNullException) { errorThrown = true; } - Assert("#F03b", errorThrown); + Assert.IsTrue (errorThrown, "#F03b"); } -#endif +#if !TARGET_JVM // Arrays lower bounds are not supported for TARGET_JVM { bool errorThrown = false; try { @@ -637,8 +715,9 @@ public class ArrayTest : Assertion } catch (ArgumentNullException) { errorThrown = true; } - Assert("#F04", errorThrown); + Assert.IsTrue (errorThrown, "#F04"); } +#endif // TARGET_JVM { bool errorThrown = false; try { @@ -647,8 +726,9 @@ public class ArrayTest : Assertion } catch (ArgumentException) { errorThrown = true; } - Assert("#F05", errorThrown); + Assert.IsTrue (errorThrown, "#F05"); } +#if !TARGET_JVM // CreateInstance with lower bounds not supported for TARGET_JVM { bool errorThrown = false; try { @@ -659,15 +739,15 @@ public class ArrayTest : Assertion } catch (ArgumentException) { errorThrown = true; } - Assert("#F06", errorThrown); + Assert.IsTrue (errorThrown, "#F06"); } char[] c1 = (char[])Array.CreateInstance(Type.GetType("System.Char"), 12); - AssertEquals("#F07", 12, c1.Length); + Assert.AreEqual (12, c1.Length, "#F07"); Array c2 = Array.CreateInstance(Type.GetType("System.Char"), 12, 5); - AssertEquals("#F08", 2, c2.Rank); - AssertEquals("#F09", 60, c2.Length); + Assert.AreEqual (2, c2.Rank, "#F08"); + Assert.AreEqual (60, c2.Length, "#F09"); { @@ -676,95 +756,93 @@ public class ArrayTest : Assertion int[] src = { 512, 718, 912 }; Array array = Array.CreateInstance(typeof(int), lengths, bounds); - AssertEquals("#F10", 3, array.Length); - AssertEquals("#F11", 5, array.GetLowerBound(0)); - AssertEquals("#F12", 7, array.GetUpperBound(0)); + Assert.AreEqual (3, array.Length, "#F10"); + Assert.AreEqual (5, array.GetLowerBound(0), "#F11"); + Assert.AreEqual (7, array.GetUpperBound(0), "#F12"); src.CopyTo (array, 5); for (int i = 0; i < src.Length; i++) - AssertEquals("#F13(" + i + ")", src[i], array.GetValue(i+5)); + Assert.AreEqual (src[i], array.GetValue(i+5), "#F13(" + i + ")"); } // 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 ()); + Assert.IsTrue (szarrayType == (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0})).GetType ()); + Assert.IsTrue (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); + } [Test] -#if NET_2_0 [ExpectedException (typeof (ArgumentNullException))] -#else - [ExpectedException (typeof (NullReferenceException))] -#endif - public void TestCreateInstance2b () - { - Array a = Array.CreateInstance (typeof (Int32), (long[])null); - } + public void TestCreateInstance2b () + { + Array.CreateInstance (typeof (Int32), (long[])null); + } [Test] public void TestGetEnumerator() { String[] s1 = {"this", "is", "a", "test"}; IEnumerator en = s1.GetEnumerator (); - AssertNotNull ("#G01", en); - - Assert ("#G02", en.MoveNext ()); - AssertEquals ("#G03", "this", en.Current); - Assert ("#G04", en.MoveNext ()); - AssertEquals ("#G05", "is", en.Current); - Assert ("#G06", en.MoveNext ()); - AssertEquals ("#G07", "a", en.Current); - Assert ("#G08", en.MoveNext ()); - AssertEquals ("#G09", "test", en.Current); - Assert ("#G10", !en.MoveNext ()); + Assert.IsNotNull (en, "#G01"); + + Assert.IsTrue (en.MoveNext (), "#G02"); + Assert.AreEqual ("this", en.Current, "#G03"); + Assert.IsTrue (en.MoveNext (), "#G04"); + Assert.AreEqual ("is", en.Current, "#G05"); + Assert.IsTrue (en.MoveNext (), "#G06"); + Assert.AreEqual ("a", en.Current, "#G07"); + Assert.IsTrue (en.MoveNext (), "#G08"); + Assert.AreEqual ("test", en.Current, "#G09"); + Assert.IsTrue (!en.MoveNext (), "#G10"); en.Reset (); - Assert("#G11", en.MoveNext ()); - AssertEquals ("#G12", "this", en.Current); + Assert.IsTrue (en.MoveNext (), "#G11"); + Assert.AreEqual ("this", en.Current, "#G12"); // mutation does not invalidate array enumerator! s1.SetValue ("change", 1); - Assert ("#G13", en.MoveNext ()); - AssertEquals ("#G14", "change", en.Current); + Assert.IsTrue (en.MoveNext (), "#G13"); + Assert.AreEqual ("change", en.Current, "#G14"); } [Test] public void TestGetEnumeratorMultipleDimension() { String[,] s1 = {{"this", "is"}, {"a", "test"}}; IEnumerator en = s1.GetEnumerator (); - AssertNotNull ("#AA01", en); - - Assert ("#AA02", en.MoveNext ()); - AssertEquals ("#AA03", "this", en.Current); - Assert ("#AA04", en.MoveNext ()); - AssertEquals ("#AA05", "is", en.Current); - Assert ("#AA06", en.MoveNext ()); - AssertEquals ("#AA07", "a", en.Current); - Assert ("#AA08", en.MoveNext ()); - AssertEquals ("#AA09", "test", en.Current); - Assert ("#AA10", !en.MoveNext ()); + Assert.IsNotNull (en, "#AA01"); + + Assert.IsTrue (en.MoveNext (), "#AA02"); + Assert.AreEqual ("this", en.Current, "#AA03"); + Assert.IsTrue (en.MoveNext (), "#AA04"); + Assert.AreEqual ("is", en.Current, "#AA05"); + Assert.IsTrue (en.MoveNext (), "#AA06"); + Assert.AreEqual ("a", en.Current, "#AA07"); + Assert.IsTrue (en.MoveNext (), "#AA08"); + Assert.AreEqual ("test", en.Current, "#AA09"); + Assert.IsTrue (!en.MoveNext (), "#AA10"); en.Reset (); - Assert("#AA11", en.MoveNext ()); - AssertEquals ("#AA12", "this", en.Current); + Assert.IsTrue (en.MoveNext (), "#AA11"); + Assert.AreEqual ("this", en.Current, "#AA12"); int[] idxs = {0,1}; // mutation does not invalidate array enumerator! s1.SetValue ("change", idxs); - Assert ("#AA13", en.MoveNext ()); - AssertEquals ("#AA14", "change", en.Current); + Assert.IsTrue (en.MoveNext (), "#AA13"); + Assert.AreEqual ("change", en.Current, "#AA14"); } [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 }; @@ -776,23 +854,24 @@ public class ArrayTest : Assertion myArray.SetValue( Convert.ToString(i) + j, myIndicesArray ); } IEnumerator en = myArray.GetEnumerator (); - AssertNotNull ("#AB01", en); + Assert.IsNotNull (en, "#AB01"); // check the first couple of values - Assert ("#AB02", en.MoveNext ()); - AssertEquals ("#AB03", "23", en.Current); - Assert ("#AB04", en.MoveNext ()); - AssertEquals ("#AB05", "24", en.Current); + Assert.IsTrue (en.MoveNext (), "#AB02"); + Assert.AreEqual ("23", en.Current, "#AB03"); + Assert.IsTrue (en.MoveNext (), "#AB04"); + Assert.AreEqual ("24", en.Current, "#AB05"); // then check the last element's value string lastElement; do { lastElement = (string)en.Current; } while (en.MoveNext()); - AssertEquals ("#AB06", "47", lastElement); + Assert.AreEqual ("47", lastElement, "#AB06"); } [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 }; @@ -800,19 +879,20 @@ public class ArrayTest : Assertion Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray ); try { ((IList)myArray).Add ("can not"); - Fail ("IList.Add should throw"); + Assert.Fail ("IList.Add should throw"); } catch (NotSupportedException) { return; } catch (Exception) { - Fail ("IList.Add threw wrong exception type"); + Assert.Fail ("IList.Add threw wrong exception type"); } - Fail("IList.Add shouldn't get this far"); + Assert.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 }; @@ -820,19 +900,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"); + Assert.Fail ("IList.Insert should throw"); } catch (NotSupportedException) { return; } catch (Exception) { - Fail ("IList.Insert threw wrong exception type"); + Assert.Fail ("IList.Insert threw wrong exception type"); } - Fail("IList.Insert shouldn't get this far"); + Assert.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 }; @@ -840,19 +921,20 @@ public class ArrayTest : Assertion Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray ); try { ((IList)myArray).Remove ("can not"); - Fail ("IList.Remove should throw"); + Assert.Fail ("IList.Remove should throw"); } catch (NotSupportedException) { return; } catch (Exception) { - Fail ("IList.Remove threw wrong exception type"); + Assert.Fail ("IList.Remove threw wrong exception type"); } - Fail("IList.Remove shouldn't get this far"); + Assert.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 }; @@ -860,19 +942,20 @@ public class ArrayTest : Assertion Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray ); try { ((IList)myArray).RemoveAt (0); - Fail ("IList.RemoveAt should throw"); + Assert.Fail ("IList.RemoveAt should throw"); } catch (NotSupportedException) { return; } catch (Exception) { - Fail ("IList.RemoveAt threw wrong exception type"); + Assert.Fail ("IList.RemoveAt threw wrong exception type"); } - Fail("IList.RemoveAt shouldn't get this far"); + Assert.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 }; @@ -881,23 +964,24 @@ public class ArrayTest : Assertion try { bool b = ((IList)myArray).Contains ("23"); - Fail("IList.Contains should throw with multi-dimensional arrays"); + Assert.Fail ("IList.Contains should throw with multi-dimensional arrays"); } catch (RankException) { int[] iArr = new int[3] { 1, 2, 3}; // check the first and last items - Assert("AC01", ((IList)iArr).Contains (1)); - Assert("AC02", ((IList)iArr).Contains (3)); + Assert.IsTrue (((IList)iArr).Contains (1), "AC01"); + Assert.IsTrue (((IList)iArr).Contains (3), "AC02"); // and one that is definately not there - Assert("AC03", !((IList)iArr).Contains (42)); + Assert.IsTrue (!((IList)iArr).Contains (42), "AC03"); return; } - Fail("Should not get here"); + Assert.Fail ("Should not get here"); } [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 }; @@ -906,19 +990,19 @@ public class ArrayTest : Assertion try { bool b = ((IList)myArray).Contains ("23"); - Fail("IList.Contains should throw with multi-dimensional arrays"); + Assert.Fail ("IList.Contains should throw with multi-dimensional arrays"); } catch (RankException) { int[] iArr = new int[3] { 1, 2, 3}; // check the first and last items - AssertEquals("AD01", 0, ((IList)iArr).IndexOf (1)); - AssertEquals("AD02", 2, ((IList)iArr).IndexOf (3)); + Assert.AreEqual (0, ((IList)iArr).IndexOf (1), "AD01"); + Assert.AreEqual (2, ((IList)iArr).IndexOf (3), "AD02"); // and one that is definately not there - AssertEquals("AD03", -1, ((IList)iArr).IndexOf (42)); + Assert.AreEqual (-1, ((IList)iArr).IndexOf (42), "AD03"); } catch (Exception e) { - Fail("Unexpected exception: " + e.ToString()); + Assert.Fail ("Unexpected exception: " + e.ToString()); } // check that wierd case whem lowerbound is Int32.MinValue, @@ -926,7 +1010,7 @@ public class ArrayTest : Assertion int[] myLengthArray = new int[1] { 3 }; int[] myBoundArray = new int[1] { Int32.MinValue }; Array myExtremeArray=Array.CreateInstance ( typeof(String), myLengthArray, myBoundArray ); - AssertEquals("AD04", Int32.MaxValue, ((IList)myExtremeArray).IndexOf (42)); + Assert.AreEqual (Int32.MaxValue, ((IList)myExtremeArray).IndexOf (42), "AD04"); } @@ -940,7 +1024,7 @@ public class ArrayTest : Assertion } catch (IndexOutOfRangeException) { errorThrown = true; } - Assert("#H01", errorThrown); + Assert.IsTrue (errorThrown, "#H01"); } { bool errorThrown = false; @@ -950,18 +1034,15 @@ public class ArrayTest : Assertion } catch (IndexOutOfRangeException) { errorThrown = true; } - Assert("#H02", errorThrown); + Assert.IsTrue (errorThrown, "#H02"); } char[] c2 = new Char[5]; - AssertEquals("#H03", - 5, c2.GetLength(0)); + Assert.AreEqual (5, c2.GetLength(0), "#H03"); char[,] c3 = new Char[6,7]; - AssertEquals("#H04", - 6, c3.GetLength(0)); - AssertEquals("#H05", - 7, c3.GetLength(1)); + Assert.AreEqual (6, c3.GetLength(0), "#H04"); + Assert.AreEqual (7, c3.GetLength(1), "#H05"); } [Test] @@ -974,7 +1055,7 @@ public class ArrayTest : Assertion } catch (IndexOutOfRangeException) { errorThrown = true; } - Assert("#H31", errorThrown); + Assert.IsTrue (errorThrown, "#H31"); } { bool errorThrown = false; @@ -984,18 +1065,15 @@ public class ArrayTest : Assertion } catch (IndexOutOfRangeException) { errorThrown = true; } - Assert("#H32", errorThrown); + Assert.IsTrue (errorThrown, "#H32"); } char[] c1 = new Char[5]; - AssertEquals("#H33", - 0, c1.GetLowerBound(0)); + Assert.AreEqual (0, c1.GetLowerBound(0), "#H33"); char[,] c2 = new Char[4,4]; - AssertEquals("#H34", - 0, c2.GetLowerBound(0)); - AssertEquals("#H35", - 0, c2.GetLowerBound(1)); + Assert.AreEqual (0, c2.GetLowerBound(0), "#H34"); + Assert.AreEqual (0, c2.GetLowerBound(1), "#H35"); } [Test] @@ -1008,7 +1086,7 @@ public class ArrayTest : Assertion } catch (IndexOutOfRangeException) { errorThrown = true; } - Assert("#H61", errorThrown); + Assert.IsTrue (errorThrown, "#H61"); } { bool errorThrown = false; @@ -1018,18 +1096,15 @@ public class ArrayTest : Assertion } catch (IndexOutOfRangeException) { errorThrown = true; } - Assert("#H62", errorThrown); + Assert.IsTrue (errorThrown, "#H62"); } char[] c1 = new Char[5]; - AssertEquals("#H63", - 4, c1.GetUpperBound(0)); + Assert.AreEqual (4, c1.GetUpperBound(0), "#H63"); char[,] c2 = new Char[4,6]; - AssertEquals("#H64", - 3, c2.GetUpperBound(0)); - AssertEquals("#H65", - 5, c2.GetUpperBound(1)); + Assert.AreEqual (3, c2.GetUpperBound(0), "#H64"); + Assert.AreEqual (5, c2.GetUpperBound(1), "#H65"); } [Test] @@ -1042,7 +1117,7 @@ public class ArrayTest : Assertion } catch (ArgumentException) { errorThrown = true; } - Assert("#I01", errorThrown); + Assert.IsTrue (errorThrown, "#I01"); } { bool errorThrown = false; @@ -1052,7 +1127,7 @@ public class ArrayTest : Assertion } catch (IndexOutOfRangeException) { errorThrown = true; } - Assert("#I02", errorThrown); + Assert.IsTrue (errorThrown, "#I02"); } { bool errorThrown = false; @@ -1062,12 +1137,12 @@ public class ArrayTest : Assertion } catch (IndexOutOfRangeException) { errorThrown = true; } - Assert("#I03", errorThrown); + Assert.IsTrue (errorThrown, "#I03"); } char[] c1 = {'a', 'b', 'c', 'd'}; for (int i = 0; i < c1.Length; i++) { - AssertEquals("#I04(" + i + ")", c1[i], c1.GetValue(i)); + Assert.AreEqual (c1[i], c1.GetValue(i), "#I04(" + i + ")"); } } @@ -1081,7 +1156,7 @@ public class ArrayTest : Assertion } catch (ArgumentException) { errorThrown = true; } - Assert("#I21", errorThrown); + Assert.IsTrue (errorThrown, "#I21"); } { bool errorThrown = false; @@ -1091,7 +1166,7 @@ public class ArrayTest : Assertion } catch (IndexOutOfRangeException) { errorThrown = true; } - Assert("#I22", errorThrown); + Assert.IsTrue (errorThrown, "#I22"); } { bool errorThrown = false; @@ -1101,7 +1176,7 @@ public class ArrayTest : Assertion } catch (IndexOutOfRangeException) { errorThrown = true; } - Assert("#I23", errorThrown); + Assert.IsTrue (errorThrown, "#I23"); } char[,] c1 = new Char[4,6]; @@ -1112,8 +1187,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)); + Assert.AreEqual (c1[i, j], c1.GetValue(i, j), "#I24(" + i + "," + j + ")"); } } } @@ -1128,7 +1202,7 @@ public class ArrayTest : Assertion } catch (ArgumentException) { errorThrown = true; } - Assert("#I41", errorThrown); + Assert.IsTrue (errorThrown, "#I41"); } { bool errorThrown = false; @@ -1138,7 +1212,7 @@ public class ArrayTest : Assertion } catch (IndexOutOfRangeException) { errorThrown = true; } - Assert("#I42", errorThrown); + Assert.IsTrue (errorThrown, "#I42"); } { bool errorThrown = false; @@ -1148,7 +1222,7 @@ public class ArrayTest : Assertion } catch (IndexOutOfRangeException) { errorThrown = true; } - Assert("#I43", errorThrown); + Assert.IsTrue (errorThrown, "#I43"); } char[,,] c1 = new Char[4,2,3]; @@ -1162,19 +1236,14 @@ public class ArrayTest : Assertion for (int i = 0; i < c1.GetLength(0); i++) { 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)); + Assert.AreEqual (c1[i, j, k], c1.GetValue(i, j, k), "#I44(" + i + "," + j + ")"); } } } } [Test] -#if NET_2_0 [ExpectedException (typeof (ArgumentNullException))] -#else - [ExpectedException (typeof (NullReferenceException))] -#endif public void TestGetValueLongArray () { char[] c = new Char[2]; @@ -1191,7 +1260,7 @@ public class ArrayTest : Assertion } catch (ArgumentNullException) { errorThrown = true; } - Assert("#I61a", errorThrown); + Assert.IsTrue (errorThrown, "#I61a"); } { bool errorThrown = false; @@ -1202,7 +1271,7 @@ public class ArrayTest : Assertion } catch (ArgumentException) { errorThrown = true; } - Assert("#I62", errorThrown); + Assert.IsTrue (errorThrown, "#I62"); } { bool errorThrown = false; @@ -1213,7 +1282,7 @@ public class ArrayTest : Assertion } catch (IndexOutOfRangeException) { errorThrown = true; } - Assert("#I63", errorThrown); + Assert.IsTrue (errorThrown, "#I63"); } { bool errorThrown = false; @@ -1224,7 +1293,7 @@ public class ArrayTest : Assertion } catch (IndexOutOfRangeException) { errorThrown = true; } - Assert("#I64", errorThrown); + Assert.IsTrue (errorThrown, "#I64"); } char[,] c1 = new Char[4,6]; @@ -1236,8 +1305,7 @@ public class ArrayTest : Assertion for (int i = 0; i < c1.GetLength(0); i++) { for (int j = 0; j < c1.GetLength(1); j++) { int[] coords = {i, j}; - AssertEquals("#I65(" + i + "," + j + ")", - c1[i,j], c1.GetValue(coords)); + Assert.AreEqual (c1[i, j], c1.GetValue(coords), "#I65(" + i + "," + j + ")"); } } } @@ -1251,7 +1319,7 @@ public class ArrayTest : Assertion } catch (ArgumentNullException) { errorThrown = true; } - Assert("#J01", errorThrown); + Assert.IsTrue (errorThrown, "#J01"); } { bool errorThrown = false; @@ -1261,14 +1329,14 @@ public class ArrayTest : Assertion } catch (RankException) { errorThrown = true; } - Assert("#J02", errorThrown); + Assert.IsTrue (errorThrown, "#J02"); } String[] s1 = {"this", "is", "a", "test"}; - AssertEquals("#J03", -1, Array.IndexOf(s1, null)); - AssertEquals("#J04", -1, Array.IndexOf(s1, "nothing")); - AssertEquals("#J05", 0, Array.IndexOf(s1, "this")); - AssertEquals("#J06", 3, Array.IndexOf(s1, "test")); + Assert.AreEqual (-1, Array.IndexOf(s1, null), "#J03"); + Assert.AreEqual (-1, Array.IndexOf(s1, "nothing"), "#J04"); + Assert.AreEqual (0, Array.IndexOf(s1, "this"), "#J05"); + Assert.AreEqual (3, Array.IndexOf(s1, "test"), "#J06"); } [Test] @@ -1280,7 +1348,7 @@ public class ArrayTest : Assertion } catch (ArgumentNullException) { errorThrown = true; } - Assert("#J21", errorThrown); + Assert.IsTrue (errorThrown, "#J21"); } { bool errorThrown = false; @@ -1290,7 +1358,7 @@ public class ArrayTest : Assertion } catch (RankException) { errorThrown = true; } - Assert("#J22", errorThrown); + Assert.IsTrue (errorThrown, "#J22"); } { bool errorThrown = false; @@ -1300,15 +1368,15 @@ public class ArrayTest : Assertion } catch (ArgumentOutOfRangeException) { errorThrown = true; } - Assert("#J23", errorThrown); + Assert.IsTrue (errorThrown, "#J23"); } String[] s1 = {"this", "is", "really", "a", "test"}; - AssertEquals("#J24", -1, Array.IndexOf(s1, null, 1)); - AssertEquals("#J25", -1, Array.IndexOf(s1, "nothing", 1)); - AssertEquals("#J26", -1, Array.IndexOf(s1, "this", 1)); - AssertEquals("#J27", 1, Array.IndexOf(s1, "is", 1)); - AssertEquals("#J28", 4, Array.IndexOf(s1, "test", 1)); + Assert.AreEqual (-1, Array.IndexOf(s1, null, 1), "#J24"); + Assert.AreEqual (-1, Array.IndexOf(s1, "nothing", 1), "#J25"); + Assert.AreEqual (-1, Array.IndexOf(s1, "this", 1), "#J26"); + Assert.AreEqual (1, Array.IndexOf(s1, "is", 1), "#J27"); + Assert.AreEqual (4, Array.IndexOf(s1, "test", 1), "#J28"); } [Test] @@ -1320,7 +1388,7 @@ public class ArrayTest : Assertion } catch (ArgumentNullException) { errorThrown = true; } - Assert("#J41", errorThrown); + Assert.IsTrue (errorThrown, "#J41"); } { bool errorThrown = false; @@ -1330,7 +1398,7 @@ public class ArrayTest : Assertion } catch (RankException) { errorThrown = true; } - Assert("#J42", errorThrown); + Assert.IsTrue (errorThrown, "#J42"); } { bool errorThrown = false; @@ -1340,7 +1408,7 @@ public class ArrayTest : Assertion } catch (ArgumentOutOfRangeException) { errorThrown = true; } - Assert("#J43", errorThrown); + Assert.IsTrue (errorThrown, "#J43"); } { bool errorThrown = false; @@ -1350,16 +1418,26 @@ public class ArrayTest : Assertion } catch (ArgumentOutOfRangeException) { errorThrown = true; } - Assert("#J44", errorThrown); + Assert.IsTrue (errorThrown, "#J44"); } String[] s1 = {"this", "is", "really", "a", "test"}; - AssertEquals("#J45", -1, Array.IndexOf(s1, null, 1, 3)); - AssertEquals("#J46", -1, Array.IndexOf(s1, "nothing", 1, 3)); - AssertEquals("#J47", -1, Array.IndexOf(s1, "this", 1, 3)); - AssertEquals("#J48", 1, Array.IndexOf(s1, "is", 1, 3)); - AssertEquals("#J49", -1, Array.IndexOf(s1, "test", 1, 3)); - AssertEquals("#J50", 3, Array.IndexOf(s1, "a", 1, 3)); + Assert.AreEqual (-1, Array.IndexOf(s1, null, 1, 3), "#J45"); + Assert.AreEqual (-1, Array.IndexOf(s1, "nothing", 1, 3), "#J46"); + Assert.AreEqual (-1, Array.IndexOf(s1, "this", 1, 3), "#J47"); + Assert.AreEqual (1, Array.IndexOf(s1, "is", 1, 3), "#J48"); + Assert.AreEqual (-1, Array.IndexOf(s1, "test", 1, 3), "#J49"); + Assert.AreEqual (3, Array.IndexOf(s1, "a", 1, 3), "#J50"); + } + + [Test] + public void TestIndexOf_CustomEqual () + { + DataEqual[] test = new DataEqual [] { new DataEqual () }; + Assert.AreEqual (0, Array.IndexOf (test, "asdfas", 0)); + + IList array = (IList)test; + Assert.AreEqual (0, array.IndexOf ("asdfas")); } [Test] @@ -1371,7 +1449,7 @@ public class ArrayTest : Assertion } catch (ArgumentNullException) { errorThrown = true; } - Assert("#K01", errorThrown); + Assert.IsTrue (errorThrown, "#K01"); } { bool errorThrown = false; @@ -1381,15 +1459,17 @@ public class ArrayTest : Assertion } catch (RankException) { errorThrown = true; } - Assert("#K02", errorThrown); + Assert.IsTrue (errorThrown, "#K02"); } String[] s1 = {"this", "is", "a", "a", "test"}; - AssertEquals("#K03", -1, Array.LastIndexOf(s1, null)); - AssertEquals("#K04", -1, Array.LastIndexOf(s1, "nothing")); - AssertEquals("#K05", 0, Array.LastIndexOf(s1, "this")); - AssertEquals("#K06", 4, Array.LastIndexOf(s1, "test")); - AssertEquals("#K07", 3, Array.LastIndexOf(s1, "a")); + Assert.AreEqual (-1, Array.LastIndexOf(s1, null), "#K03"); + Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing"), "#K04"); + Assert.AreEqual (0, Array.LastIndexOf(s1, "this"), "#K05"); + Assert.AreEqual (4, Array.LastIndexOf(s1, "test"), "#K06"); + Assert.AreEqual (3, Array.LastIndexOf(s1, "a"), "#K07"); + + Assert.AreEqual (-1, Array.LastIndexOf (new String [0], "foo")); } [Test] @@ -1401,7 +1481,7 @@ public class ArrayTest : Assertion } catch (ArgumentNullException) { errorThrown = true; } - Assert("#K21", errorThrown); + Assert.IsTrue (errorThrown, "#K21"); } { bool errorThrown = false; @@ -1411,7 +1491,7 @@ public class ArrayTest : Assertion } catch (RankException) { errorThrown = true; } - Assert("#K22", errorThrown); + Assert.IsTrue (errorThrown, "#K22"); } { bool errorThrown = false; @@ -1421,15 +1501,15 @@ public class ArrayTest : Assertion } catch (ArgumentOutOfRangeException) { errorThrown = true; } - Assert("#K23", errorThrown); + Assert.IsTrue (errorThrown, "#K23"); } String[] s1 = {"this", "is", "really", "a", "test"}; - AssertEquals("#K24", -1, Array.LastIndexOf(s1, null, 3)); - AssertEquals("#K25", -1, Array.LastIndexOf(s1, "nothing", 3)); - AssertEquals("#K26", -1, Array.LastIndexOf(s1, "test", 3)); - AssertEquals("#K27", 3, Array.LastIndexOf(s1, "a", 3)); - AssertEquals("#K28", 0, Array.LastIndexOf(s1, "this", 3)); + Assert.AreEqual (-1, Array.LastIndexOf(s1, null, 3), "#K24"); + Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing", 3), "#K25"); + Assert.AreEqual (-1, Array.LastIndexOf(s1, "test", 3), "#K26"); + Assert.AreEqual (3, Array.LastIndexOf(s1, "a", 3), "#K27"); + Assert.AreEqual (0, Array.LastIndexOf(s1, "this", 3), "#K28"); } [Test] @@ -1441,7 +1521,7 @@ public class ArrayTest : Assertion } catch (ArgumentNullException) { errorThrown = true; } - Assert("#K41", errorThrown); + Assert.IsTrue (errorThrown, "#K41"); } { bool errorThrown = false; @@ -1451,7 +1531,7 @@ public class ArrayTest : Assertion } catch (RankException) { errorThrown = true; } - Assert("#K42", errorThrown); + Assert.IsTrue (errorThrown, "#K42"); } { bool errorThrown = false; @@ -1461,7 +1541,7 @@ public class ArrayTest : Assertion } catch (ArgumentOutOfRangeException) { errorThrown = true; } - Assert("#K43", errorThrown); + Assert.IsTrue (errorThrown, "#K43"); } { bool errorThrown = false; @@ -1471,48 +1551,49 @@ public class ArrayTest : Assertion } catch (ArgumentOutOfRangeException) { errorThrown = true; } - Assert("#K44", errorThrown); + Assert.IsTrue (errorThrown, "#K44"); } 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); - } + Assert.AreEqual (-1, Array.LastIndexOf(s1, null, 3, 3), "#K45"); + Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing", 3, 3), "#K46"); + Assert.AreEqual (-1, Array.LastIndexOf(s1, "this", 3, 3), "#K47"); + Assert.AreEqual (1, Array.LastIndexOf(s1, "is", 3, 3), "#K48"); + Assert.AreEqual (-1, Array.LastIndexOf(s1, "test", 3, 3), "#K49"); + Assert.AreEqual (3, Array.LastIndexOf(s1, "a", 3, 3), "#K50"); + } + + [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) { } + + try { + Array.LastIndexOf (a, 16, -1); + NUnit.Framework.Assert.Fail ("#2"); + } catch (ArgumentOutOfRangeException) { } + } + + [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.IsTrue (!error); + } [Test] [ExpectedException (typeof (ArgumentOutOfRangeException))] @@ -1532,6 +1613,155 @@ public class ArrayTest : Assertion Array.LastIndexOf (array, this, 1, Int32.MaxValue); } + [Test] + public void LastIndexOf_0LengthArray () + { + Array array = Array.CreateInstance (typeof (char), 0); + int idx = Array.LastIndexOf (array, (object) null, -1, 0); + Assert.IsTrue (idx == -1, "#01"); + idx = Array.LastIndexOf (array, (object) null, -1, 10); + Assert.IsTrue (idx == -1, "#02"); + idx = Array.LastIndexOf (array, (object) null, -100, 10); + Assert.IsTrue (idx == -1, "#02"); + + array = Array.CreateInstance (typeof (char), 1); + try { + Array.LastIndexOf (array, (object) null, -1, 0); + Assert.Fail ("#04"); + } catch (ArgumentOutOfRangeException e) { + } + try { + Array.LastIndexOf (array, (object) null, -1, 10); + Assert.Fail ("#05"); + } catch (ArgumentOutOfRangeException e) { + } + try { + Array.LastIndexOf (array, (object) null, -100, 10); + Assert.Fail ("#06"); + } catch (ArgumentOutOfRangeException e) { + } + } + + + [Test] + public void FindIndexTest () + { + var a = new int[] { 2, 2, 2, 3, 2 }; + Assert.AreEqual (2, Array.FindIndex (a, 2, 2, l => true)); + } + + [Test] + public void FindIndex_Invalid () + { + var array = new int [] { 1, 2, 3, 4, 5 }; + + try { + Array.FindIndex (array, null); + Assert.Fail ("#1"); + } catch (ArgumentNullException) { + } + + try { + Array.FindIndex (array, -1, l => true); + Assert.Fail ("#2"); + } catch (ArgumentOutOfRangeException) { + } + + try { + Array.FindIndex (array, -1, 0, l => true); + Assert.Fail ("#2b"); + } catch (ArgumentOutOfRangeException) { + } + + try { + Array.FindIndex (array, 0, -1, l => true); + Assert.Fail ("#3"); + } catch (ArgumentOutOfRangeException) { + } + + try { + Array.FindIndex (array, 100, l => true); + Assert.Fail ("#4"); + } catch (ArgumentOutOfRangeException) { + } + + try { + Array.FindIndex (array, 100, 0, l => true); + Assert.Fail ("#4b"); + } catch (ArgumentOutOfRangeException) { + } + + try { + Array.FindIndex (array, 7, 2, l => true); + Assert.Fail ("#5"); + } catch (ArgumentOutOfRangeException) { + } + } + + [Test, ExpectedException (typeof (ArgumentNullException))] + public void FindLastNullTest () + { + var array = new int [] { 1, 2, 3, 4, 5 }; + Array.FindLast (array, null); + } + + [Test] + public void FindLastIndexTest () + { + var array = new int [] { 1, 2, 3, 4, 5 }; + + Assert.AreEqual (2, Array.FindLastIndex (array, 2, 3, l => true)); + Assert.AreEqual (2, Array.FindLastIndex (array, 2, 2, l => true)); + Assert.AreEqual (1, Array.FindLastIndex (array, 1, 2, l => true)); + } + + [Test] + public void FindLastIndex_Invalid () + { + var array = new int [] { 1, 2, 3, 4, 5 }; + try { + Array.FindLastIndex (array, null); + Assert.Fail ("#1"); + } catch (ArgumentNullException) { + } + + try { + Array.FindLastIndex (array, -1, l => true); + Assert.Fail ("#2"); + } catch (ArgumentOutOfRangeException) { + } + + try { + Array.FindLastIndex (array, -1, 0, l => true); + Assert.Fail ("#2b"); + } catch (ArgumentOutOfRangeException) { + } + + try { + Array.FindLastIndex (array, 0, -1, l => true); + Assert.Fail ("#3"); + } catch (ArgumentOutOfRangeException) { + } + + try { + Array.FindLastIndex (array, 100, l => true); + Assert.Fail ("#4"); + } catch (ArgumentOutOfRangeException) { + } + + try { + Array.FindLastIndex (array, 100, 0, l => true); + Assert.Fail ("#4b"); + } catch (ArgumentOutOfRangeException) { + } + + try { + Array.FindLastIndex (array, 2, 4, l => true); + Assert.Fail ("#5"); + } catch (ArgumentOutOfRangeException) { + } + } + [Test] public void TestReverse() { { @@ -1541,7 +1771,7 @@ public class ArrayTest : Assertion } catch (ArgumentNullException) { errorThrown = true; } - Assert("#L01", errorThrown); + Assert.IsTrue (errorThrown, "#L01"); } { bool errorThrown = false; @@ -1551,15 +1781,15 @@ public class ArrayTest : Assertion } catch (RankException) { errorThrown = true; } - Assert("#L02", errorThrown); + Assert.IsTrue (errorThrown, "#L02"); } char[] c1 = {'a', 'b', 'c', 'd'}; Array.Reverse(c1); - AssertEquals("#L03", 'd', c1[0]); - AssertEquals("#L04", 'c', c1[1]); - AssertEquals("#L05", 'b', c1[2]); - AssertEquals("#L06", 'a', c1[3]); + Assert.AreEqual ('d', c1[0], "#L03"); + Assert.AreEqual ('c', c1[1], "#L04"); + Assert.AreEqual ('b', c1[2], "#L05"); + Assert.AreEqual ('a', c1[3], "#L06"); { bool errorThrown = false; @@ -1568,7 +1798,7 @@ public class ArrayTest : Assertion } catch (ArgumentNullException) { errorThrown = true; } - Assert("#L07", errorThrown); + Assert.IsTrue (errorThrown, "#L07"); } { bool errorThrown = false; @@ -1578,7 +1808,7 @@ public class ArrayTest : Assertion } catch (RankException) { errorThrown = true; } - Assert("#L08", errorThrown); + Assert.IsTrue (errorThrown, "#L08"); } //{ //bool errorThrown = false; @@ -1588,7 +1818,7 @@ public class ArrayTest : Assertion //} catch (ArgumentOutOfRangeException) { // errorThrown = true; //} - //Assert("#L09", errorThrown); + //Assert.IsTrue (errorThrown, "#L09"); //} //{ //bool errorThrown = false; @@ -1598,15 +1828,34 @@ public class ArrayTest : Assertion //} catch (ArgumentOutOfRangeException) { // errorThrown = true; //} - //Assert("#L10", errorThrown); + //Assert.IsTrue (errorThrown, "#L10"); //} char[] c2 = { 'a', 'b', 'c', 'd'}; Array.Reverse(c2, 1, 2); - AssertEquals("#L11", 'a', c2[0]); - AssertEquals("#L12", 'c', c2[1]); - AssertEquals("#L13", 'b', c2[2]); - AssertEquals("#L14", 'd', c2[3]); + Assert.AreEqual ('a', c2[0], "#L11"); + Assert.AreEqual ('c', c2[1], "#L12"); + Assert.AreEqual ('b', c2[2], "#L13"); + Assert.AreEqual ('d', c2[3], "#L14"); + } + + [Test] + // #8904 + public void ReverseStruct () { + BStruct[] c3 = new BStruct[2]; + c3 [0] = new BStruct () { i1 = 1, i2 = 2, i3 = 3 }; + c3 [1] = new BStruct () { i1 = 4, i2 = 5, i3 = 6 }; + Array.Reverse (c3); + Assert.AreEqual (4, c3 [0].i1); + Assert.AreEqual (5, c3 [0].i2); + Assert.AreEqual (6, c3 [0].i3); + Assert.AreEqual (1, c3 [1].i1); + Assert.AreEqual (2, c3 [1].i2); + Assert.AreEqual (3, c3 [1].i3); + } + + struct BStruct { + public int i1, i2, i3; } [Test] @@ -1619,7 +1868,7 @@ public class ArrayTest : Assertion } catch (ArgumentException) { errorThrown = true; } - Assert("#M01", errorThrown); + Assert.IsTrue (errorThrown, "#M01"); } { bool errorThrown = false; @@ -1629,7 +1878,7 @@ public class ArrayTest : Assertion } catch (IndexOutOfRangeException) { errorThrown = true; } - Assert("#M02", errorThrown); + Assert.IsTrue (errorThrown, "#M02"); } { bool errorThrown = false; @@ -1639,7 +1888,7 @@ public class ArrayTest : Assertion } catch (IndexOutOfRangeException) { errorThrown = true; } - Assert("#M03", errorThrown); + Assert.IsTrue (errorThrown, "#M03"); } char[] c1 = {'a', 'b', 'c', 'd'}; @@ -1648,7 +1897,7 @@ public class ArrayTest : Assertion c2.SetValue(c1[i], i); } for (int i = 0; i < c1.Length; i++) { - AssertEquals("#M04(" + i + ")", c1[i], c2[i]); + Assert.AreEqual (c1[i], c2[i], "#M04(" + i + ")"); } int[] c3 = { 1, 2, 3 }; @@ -1660,10 +1909,10 @@ public class ArrayTest : Assertion try { c3.CopyTo (c4, 0); } catch (Exception e) { - Fail ("c3.CopyTo(): e=" + e); + Assert.Fail ("c3.CopyTo(): e=" + e); } for (int i = 0; i < c3.Length; i++) - Assert ("#M05(" + i + ")", c3[i] == c4[i]); + Assert.IsTrue (c3[i] == c4[i], "#M05(" + i + ")"); Object[] c5 = new Object [3]; long[] c6 = new long [3]; @@ -1671,16 +1920,16 @@ public class ArrayTest : Assertion try { c4.CopyTo (c5, 0); } catch (Exception e) { - Fail ("c4.CopyTo(): e=" + e); + Assert.Fail ("c4.CopyTo(): e=" + e); } try { c5.CopyTo (c6, 0); } catch (Exception e) { - Fail ("c5.CopyTo(): e=" + e); + Assert.Fail ("c5.CopyTo(): e=" + e); } // for (int i = 0; i < c5.Length; i++) - // Assert ("#M06(" + i + ")", c5[i] == c6[i]); + // Assert.IsTrue (c5[i] == c6[i], "#M06(" + i + ")"); } [Test] @@ -1693,7 +1942,7 @@ public class ArrayTest : Assertion } catch (ArgumentException) { errorThrown = true; } - Assert("#M21", errorThrown); + Assert.IsTrue (errorThrown, "#M21"); } { bool errorThrown = false; @@ -1703,7 +1952,7 @@ public class ArrayTest : Assertion } catch (IndexOutOfRangeException) { errorThrown = true; } - Assert("#M22", errorThrown); + Assert.IsTrue (errorThrown, "#M22"); } { bool errorThrown = false; @@ -1713,7 +1962,7 @@ public class ArrayTest : Assertion } catch (IndexOutOfRangeException) { errorThrown = true; } - Assert("#M23", errorThrown); + Assert.IsTrue (errorThrown, "#M23"); } char[,] c1 = new Char[4,6]; @@ -1726,8 +1975,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]); + Assert.AreEqual (c1[i, j], c2[i, j], "#M24(" + i + "," + j + ")"); } } } @@ -1742,7 +1990,7 @@ public class ArrayTest : Assertion } catch (ArgumentException) { errorThrown = true; } - Assert("#M41", errorThrown); + Assert.IsTrue (errorThrown, "#M41"); } { bool errorThrown = false; @@ -1752,7 +2000,7 @@ public class ArrayTest : Assertion } catch (IndexOutOfRangeException) { errorThrown = true; } - Assert("#M42", errorThrown); + Assert.IsTrue (errorThrown, "#M42"); } { bool errorThrown = false; @@ -1762,7 +2010,7 @@ public class ArrayTest : Assertion } catch (IndexOutOfRangeException) { errorThrown = true; } - Assert("#M43", errorThrown); + Assert.IsTrue (errorThrown, "#M43"); } char[,,] c1 = new Char[4,2,3]; @@ -1778,19 +2026,14 @@ public class ArrayTest : Assertion for (int i = 0; i < c1.GetLength(0); i++) { 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]); + Assert.AreEqual (c1[i, j, k], c2[i, j, k], "#M44(" + i + "," + j + " )"); } } } } [Test] -#if NET_2_0 [ExpectedException (typeof (ArgumentNullException))] -#else - [ExpectedException (typeof (NullReferenceException))] -#endif public void TestSetValueLongArray () { char[] c = new Char[2]; @@ -1807,7 +2050,7 @@ public class ArrayTest : Assertion } catch (ArgumentNullException) { errorThrown = true; } - Assert("#M61a", errorThrown); + Assert.IsTrue (errorThrown, "#M61a"); } { bool errorThrown = false; @@ -1818,7 +2061,7 @@ public class ArrayTest : Assertion } catch (ArgumentException) { errorThrown = true; } - Assert("#M62", errorThrown); + Assert.IsTrue (errorThrown, "#M62"); } { bool errorThrown = false; @@ -1829,7 +2072,7 @@ public class ArrayTest : Assertion } catch (IndexOutOfRangeException) { errorThrown = true; } - Assert("#M63", errorThrown); + Assert.IsTrue (errorThrown, "#M63"); } { bool errorThrown = false; @@ -1840,7 +2083,7 @@ public class ArrayTest : Assertion } catch (IndexOutOfRangeException) { errorThrown = true; } - Assert("#M64", errorThrown); + Assert.IsTrue (errorThrown, "#M64"); } char[,] c1 = new Char[4,6]; @@ -1854,8 +2097,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]); + Assert.AreEqual (c1[i, j], c2[i, j], "#M65(" + i + "," + j + ")"); } } } @@ -1870,8 +2112,8 @@ public class ArrayTest : Assertion c2.SetValue (c1 [i], i); for (int i = 0; i < c1.Length; i++) { - Assert ("#M81(" + i + ")", c1[i] == c2[i]); - AssertEquals ("#M82(" + i + ")", typeof (long), c2[i].GetType ()); + Assert.IsTrue (c1[i] == c2[i], "#M81(" + i + ")"); + Assert.AreEqual (typeof (long), c2[i].GetType (), "#M82(" + i + ")"); } } { @@ -1883,7 +2125,7 @@ public class ArrayTest : Assertion } catch (ArgumentException) { errorThrown = true; } - Assert("#M83", errorThrown); + Assert.IsTrue (errorThrown, "#M83"); } { int[] c1 = { 1, 2, 3 }; @@ -1893,7 +2135,7 @@ public class ArrayTest : Assertion c2.SetValue (c1 [i], i); for (int i = 0; i < c1.Length; i++) - AssertEquals ("#M84(" + i + ")", c1[i], Convert.ToInt32 (c2[i])); + Assert.AreEqual (c1[i], Convert.ToInt32 (c2[i]), "#M84(" + i + ")"); } { Object[] c1 = new Object [3]; @@ -1904,7 +2146,7 @@ public class ArrayTest : Assertion c2.SetValue (c1 [i], i); for (int i = 0; i < c1.Length; i++) - AssertEquals ("#M85(" + i + ")", c1[i], c2[i]); + Assert.AreEqual (c1[i], c2[i], "#M85(" + i + ")"); } { Object[] c1 = new Object [3]; @@ -1913,15 +2155,15 @@ public class ArrayTest : Assertion c1[0] = test; c2.SetValue (c1 [0], 0); - AssertEquals ("#M86", c1[0], c2[0]); - AssertEquals ("#M87", "hello", c2[0]); + Assert.AreEqual (c1[0], c2[0], "#M86"); + Assert.AreEqual ("hello", c2[0], "#M87"); } { char[] c1 = { 'a', 'b', 'c' }; string[] c2 = new string [3]; try { c2.SetValue (c1 [0], 0); - Fail ("#M88"); + Assert.Fail ("#M88"); } catch (InvalidCastException) {} } { @@ -1929,7 +2171,7 @@ public class ArrayTest : Assertion long[] c2 = new long [3]; try { c2.SetValue (c1 [0], 0); - Fail ("#M89"); + Assert.Fail ("#M89"); } catch (ArgumentException) {} } { @@ -1999,8 +2241,7 @@ public class ArrayTest : Assertion int ex_index = (i * types.Length) + j; - AssertEquals ("#M90(" + types [i] + "," + types [j] + ")", - errorThrown, arg_ex [ex_index] == 1); + Assert.AreEqual (errorThrown, arg_ex [ex_index] == 1, "#M90(" + types [i] + "," + types [j] + ")"); } } @@ -2016,7 +2257,7 @@ public class ArrayTest : Assertion errorThrown = true; } - Assert ("#M91(" + types [i] + ")", errorThrown); + Assert.IsTrue (errorThrown, "#M91(" + types [i] + ")"); } for (int i = 0; i < types.Length; i++) { @@ -2031,7 +2272,7 @@ public class ArrayTest : Assertion errorThrown = true; } - Assert ("#M92(" + types [i] + ")", errorThrown); + Assert.IsTrue (errorThrown, "#M92(" + types [i] + ")"); } for (int i = 0; i < types.Length; i++) { @@ -2046,7 +2287,7 @@ public class ArrayTest : Assertion errorThrown = true; } - Assert ("#M93(" + types [i] + ")", !errorThrown); + Assert.IsTrue (!errorThrown, "#M93(" + types [i] + ")"); } // Copy @@ -2068,8 +2309,7 @@ public class ArrayTest : Assertion int ex_index = (i * types.Length) + j; - AssertEquals ("#M94(" + types [i] + "," + types [j] + ")", - errorThrown, arg_ex [ex_index] == 1); + Assert.AreEqual (errorThrown, arg_ex [ex_index] == 1, "#M94(" + types [i] + "," + types [j] + ")"); } } @@ -2087,7 +2327,7 @@ public class ArrayTest : Assertion errorThrown = true; } - Assert ("#M95(" + types [i] + ")", errorThrown); + Assert.IsTrue (errorThrown, "#M95(" + types [i] + ")"); } for (int i = 0; i < types.Length; i++) { @@ -2104,7 +2344,7 @@ public class ArrayTest : Assertion errorThrown = true; } - Assert ("#M96(" + types [i] + ")", errorThrown); + Assert.IsTrue (errorThrown, "#M96(" + types [i] + ")"); } } } @@ -2118,7 +2358,7 @@ public class ArrayTest : Assertion } catch (ArgumentNullException) { errorThrown = true; } - Assert("#N01", errorThrown); + Assert.IsTrue (errorThrown, "#N01"); } { bool errorThrown = false; @@ -2127,7 +2367,7 @@ public class ArrayTest : Assertion } catch (ArgumentNullException) { errorThrown = true; } - Assert("#N02", errorThrown); + Assert.IsTrue (errorThrown, "#N02"); } { bool errorThrown = false; @@ -2137,7 +2377,7 @@ public class ArrayTest : Assertion } catch (ArgumentNullException) { errorThrown = true; } - Assert("#N03", errorThrown); + Assert.IsTrue (errorThrown, "#N03"); } { bool errorThrown = false; @@ -2147,7 +2387,7 @@ public class ArrayTest : Assertion } catch (ArgumentNullException) { errorThrown = true; } - Assert("#N04", errorThrown); + Assert.IsTrue (errorThrown, "#N04"); } { int tc = 5; @@ -2155,34 +2395,34 @@ public class ArrayTest : Assertion try { Array.Sort (null, 0, 1); - Fail ("#N" + tc.ToString ()); + Assert.Fail ("#N" + tc.ToString ()); } catch (ArgumentException) {} - catch (Exception) { Fail ("#N" + tc.ToString ()); } + catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); } tc++; try { Array.Sort (arr, -1, 3); - Fail ("#N" + tc.ToString ()); + Assert.Fail ("#N" + tc.ToString ()); } catch (ArgumentException) {} - catch (Exception) { Fail ("#N" + tc.ToString ()); } + catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); } tc++; try { Array.Sort (arr, 1, -3); - Fail ("#N" + tc.ToString ()); + Assert.Fail ("#N" + tc.ToString ()); } catch (ArgumentException) {} - catch (Exception) { Fail ("#N" + tc.ToString ()); } + catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); } tc++; try { Array.Sort (arr, arr.Length, arr.Length + 2); - Fail ("#N" + tc.ToString ()); + Assert.Fail ("#N" + tc.ToString ()); } catch (ArgumentException) {} - catch (Exception) { Fail ("#N" + tc.ToString ()); } + catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); } } // note: null second array => just sort first array @@ -2191,59 +2431,139 @@ public class ArrayTest : Assertion { char[] c1 = (char[])starter.Clone(); Array.Sort(c1); - AssertEquals("#N21", 'a', c1[0]); - AssertEquals("#N22", 'b', c1[1]); - AssertEquals("#N23", 'c', c1[2]); - AssertEquals("#N24", 'd', c1[3]); - AssertEquals("#N25", 'e', c1[4]); - AssertEquals("#N26", 'f', c1[5]); + Assert.AreEqual ('a', c1[0], "#N21"); + Assert.AreEqual ('b', c1[1], "#N22"); + Assert.AreEqual ('c', c1[2], "#N23"); + Assert.AreEqual ('d', c1[3], "#N24"); + Assert.AreEqual ('e', c1[4], "#N25"); + Assert.AreEqual ('f', c1[5], "#N26"); } { char[] c1 = (char[])starter.Clone(); int[] i1 = (int[])starter1.Clone(); Array.Sort(c1, i1); - AssertEquals("#N41", 'a', c1[0]); - AssertEquals("#N42", 'b', c1[1]); - AssertEquals("#N43", 'c', c1[2]); - AssertEquals("#N44", 'd', c1[3]); - AssertEquals("#N45", 'e', c1[4]); - AssertEquals("#N46", 'f', c1[5]); - AssertEquals("#N47", 5, i1[0]); - AssertEquals("#N48", 2, i1[1]); - AssertEquals("#N49", 6, i1[2]); - AssertEquals("#N50", 1, i1[3]); - AssertEquals("#N51", 4, i1[4]); - AssertEquals("#N52", 3, i1[5]); + Assert.AreEqual ('a', c1[0], "#N41"); + Assert.AreEqual ('b', c1[1], "#N42"); + Assert.AreEqual ('c', c1[2], "#N43"); + Assert.AreEqual ('d', c1[3], "#N44"); + Assert.AreEqual ('e', c1[4], "#N45"); + Assert.AreEqual ('f', c1[5], "#N46"); + Assert.AreEqual (5, i1[0], "#N47"); + Assert.AreEqual (2, i1[1], "#N48"); + Assert.AreEqual (6, i1[2], "#N49"); + Assert.AreEqual (1, i1[3], "#N50"); + Assert.AreEqual (4, i1[4], "#N51"); + Assert.AreEqual (3, i1[5], "#N52"); } { char[] c1 = (char[])starter.Clone(); Array.Sort(c1, 1, 4); - AssertEquals("#N61", 'd', c1[0]); - AssertEquals("#N62", 'a', c1[1]); - AssertEquals("#N63", 'b', c1[2]); - AssertEquals("#N64", 'e', c1[3]); - AssertEquals("#N65", 'f', c1[4]); - AssertEquals("#N66", 'c', c1[5]); + Assert.AreEqual ('d', c1[0], "#N61"); + Assert.AreEqual ('a', c1[1], "#N62"); + Assert.AreEqual ('b', c1[2], "#N63"); + Assert.AreEqual ('e', c1[3], "#N64"); + Assert.AreEqual ('f', c1[4], "#N65"); + Assert.AreEqual ('c', c1[5], "#N66"); } { char[] c1 = (char[])starter.Clone(); int[] i1 = (int[])starter1.Clone(); Array.Sort(c1, i1, 1, 4); - AssertEquals("#N81", 'd', c1[0]); - AssertEquals("#N82", 'a', c1[1]); - AssertEquals("#N83", 'b', c1[2]); - AssertEquals("#N84", 'e', c1[3]); - AssertEquals("#N85", 'f', c1[4]); - AssertEquals("#N86", 'c', c1[5]); - AssertEquals("#N87", 1, i1[0]); - AssertEquals("#N88", 5, i1[1]); - AssertEquals("#N89", 2, i1[2]); - AssertEquals("#N90", 4, i1[3]); - AssertEquals("#N91", 3, i1[4]); - AssertEquals("#N92", 6, i1[5]); + Assert.AreEqual ('d', c1[0], "#N81"); + Assert.AreEqual ('a', c1[1], "#N82"); + Assert.AreEqual ('b', c1[2], "#N83"); + Assert.AreEqual ('e', c1[3], "#N84"); + Assert.AreEqual ('f', c1[4], "#N85"); + Assert.AreEqual ('c', c1[5], "#N86"); + Assert.AreEqual (1, i1[0], "#N87"); + Assert.AreEqual (5, i1[1], "#N88"); + Assert.AreEqual (2, i1[2], "#N89"); + Assert.AreEqual (4, i1[3], "#N90"); + Assert.AreEqual (3, i1[4], "#N91"); + Assert.AreEqual (6, i1[5], "#N92"); + } + + { + // #648828 + double[] a = new double[115]; + int[] b = new int[256]; + Array.Sort (a, b, 0, 115); + } + + /* Check that ulong[] is not sorted as long[] */ + { + string[] names = new string[] { + "A", "B", "C", "D", "E" + }; + + ulong[] arr = new ulong [] { + 5, + unchecked((ulong)0xffffFFFF00000000), + 0, + 0x7FFFFFFFffffffff, + 100 + }; + + Array a = arr; + Array.Sort (a, names, null); + Assert.AreEqual (0, a.GetValue (0)); } } + [Test] + public void Sort_NullValues () + { + var s = new [] { "a", null, "b", null }; + Array.Sort (s, (a, b) => { + if (a == null) { + return b == null ? 0 : 1; + } + + if (b == null) + return -1; + + return a.CompareTo (b); + }); + + Assert.AreEqual ("a", s [0], "#1"); + Assert.AreEqual ("b", s [1], "#2"); + Assert.IsNull (s [2], "#3"); + Assert.IsNull (s [3], "#4"); + } + + [Test] // #616416 + public void SortNonGenericDoubleItems () { + double[] doubleValues = new double[11]; + + doubleValues[0] = 0.221788066253601; + doubleValues[1] = 0.497278285809481; + doubleValues[2] = 0.100565033883643; + doubleValues[3] = 0.0433309347749905; + doubleValues[4] = 0.00476726438463812; + doubleValues[5] = 0.1354609735456; + doubleValues[6] = 0.57690356588135; + doubleValues[7] = 0.466239434334826; + doubleValues[8] = 0.409741461978934; + doubleValues[9] = 0.0112412763949565; + doubleValues[10] = 0.668704347674307; + + int[] indices = new int[11]; + indices[0] = 0; + indices[1] = 1; + indices[2] = 2; + indices[3] = 3; + indices[4] = 4; + indices[5] = 5; + indices[6] = 6; + indices[7] = 7; + indices[8] = 8; + indices[9] = 9; + indices[10] = 10; + + Array.Sort ((Array)doubleValues, (Array)indices); + Assert.AreEqual (4, indices [0]); + } + [Test] public void TestInitializeEmpty() { @@ -2257,7 +2577,7 @@ public class ArrayTest : Assertion { catched=true; } - Assert("#TI01",!catched); + Assert.IsTrue (!catched, "#TI01"); } [Test] @@ -2268,7 +2588,7 @@ public class ArrayTest : Assertion int[] b = {1,2,0}; for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++) { - AssertEquals("#TI02 " + i ,a[i],b[i]); + Assert.AreEqual (a[i], b[i], "#TI02 " + i); } } @@ -2280,7 +2600,7 @@ public class ArrayTest : Assertion double[] b = {1.0,2.0,0.0}; for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++) { - AssertEquals("#TI03 " + i ,a[i],b[i]); + Assert.AreEqual (a[i], b[i], "#TI03 " + i); } } @@ -2292,7 +2612,7 @@ public class ArrayTest : Assertion float[] b = {1.0F,2.0F,0.0F}; for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++) { - AssertEquals("#TI04 " + i ,a[i],b[i]); + Assert.AreEqual (a[i], b[i], "#TI04 " + i); } } @@ -2304,7 +2624,7 @@ public class ArrayTest : Assertion char[] b = {'1','.','0','F','2','.','0','F'}; for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++) { - AssertEquals("#TI05 " + i ,a[i],b[i]); + Assert.AreEqual (a[i], b[i], "#TI05 " + i); } } @@ -2316,7 +2636,7 @@ public class ArrayTest : Assertion string[] b = {"hola","adios","menos","mas"}; for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++) { - AssertEquals("#TI06 " + i ,a[i],b[i]); + Assert.AreEqual (a[i], b[i], "#TI06 " + i); } } @@ -2328,7 +2648,7 @@ public class ArrayTest : Assertion enua[] b = {enua.hola,enua.adios,enua.menos,enua.mas}; for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++) { - AssertEquals("#TI07 " + i ,a[i],b[i]); + Assert.AreEqual (a[i], b[i], "#TI07 " + i); } } @@ -2339,7 +2659,7 @@ public class ArrayTest : Assertion a.Initialize(); foreach(int b in a) { - AssertEquals("#TI08",b,0); + Assert.AreEqual (b, 0, "#TI08"); } } @@ -2350,7 +2670,7 @@ public class ArrayTest : Assertion a.Initialize(); foreach(char b in a) { - AssertEquals("#TI09",b,0); + Assert.AreEqual (b, 0, "#TI09"); } } @@ -2361,7 +2681,7 @@ public class ArrayTest : Assertion a.Initialize(); foreach(double b in a) { - AssertEquals("#TI09",b,0.0); + Assert.AreEqual (b, 0.0, "#TI09"); } } @@ -2372,7 +2692,7 @@ public class ArrayTest : Assertion a.Initialize(); foreach(string b in a) { - AssertEquals("#TI10",b,null); + Assert.AreEqual (b, null, "#TI10"); } } @@ -2383,7 +2703,7 @@ public class ArrayTest : Assertion a.Initialize(); foreach(object b in a) { - AssertEquals("#TI11",b,null); + Assert.AreEqual (b, null, "#TI11"); } } @@ -2394,7 +2714,7 @@ public class ArrayTest : Assertion a.Initialize(); foreach(AClass b in a) { - AssertEquals("#TI12",b,null); + Assert.AreEqual (b, null, "#TI12"); } } @@ -2406,7 +2726,7 @@ public class ArrayTest : Assertion a.Initialize(); foreach(AStruct b in a) { - AssertEquals("#TI14",b,new AStruct()); + Assert.AreEqual (b, new AStruct(), "#TI14"); } } @@ -2422,7 +2742,7 @@ public class ArrayTest : Assertion b[1].s = "HOLA"; for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++) { - AssertEquals("#TI15 " + i ,a[i],b[i]); + Assert.AreEqual (a[i], b[i], "#TI15 " + i); } } @@ -2433,7 +2753,7 @@ public class ArrayTest : Assertion a.Initialize(); foreach(DateTime b in a) { - AssertEquals("#TI16",b,new DateTime()); + Assert.AreEqual (b, new DateTime(), "#TI16"); } } @@ -2499,13 +2819,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); + Assert.AreEqual (0.3, a [0], "#1"); + Assert.AreEqual (0.9, a [1], "#2"); + Assert.AreEqual (7, b [0], "#3"); + Assert.AreEqual (4, b [1], "#4"); + } [Test] public void ClearJaggedArray () @@ -2519,7 +2851,7 @@ public class ArrayTest : Assertion } Array.Clear (matrix, 0, 8); for (int i=0; i < 8; i++) { - AssertNull (i.ToString (), matrix [i]); + Assert.IsNull (matrix [i], i.ToString ()); } } @@ -2528,10 +2860,10 @@ public class ArrayTest : Assertion { byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} }; Array.Clear (matrix, 0, 2); - AssertEquals ("0,0", 0, matrix [0,0]); - AssertEquals ("0,1", 0, matrix [0,1]); - AssertEquals ("1,0", 2, matrix [1,0]); - AssertEquals ("1,1", 2, matrix [1,1]); + Assert.AreEqual (0, matrix [0, 0], "0,0"); + Assert.AreEqual (0, matrix [0, 1], "0,1"); + Assert.AreEqual (2, matrix [1, 0], "1,0"); + Assert.AreEqual (2, matrix [1, 1], "1,1"); } [Test] @@ -2624,7 +2956,7 @@ public class ArrayTest : Assertion // that of the search object. // CharX [] x = { new CharX ('a'), new CharX ('b'), new CharX ('c') }; - AssertEquals (1, Array.BinarySearch (x, 'b')); + Assert.AreEqual (1, Array.BinarySearch (x, 'b')); } class Comparer: IComparer { @@ -2650,14 +2982,14 @@ public class ArrayTest : Assertion public void BinarySearch1_EmptyList () { int[] array = new int[0]; - AssertEquals ("BinarySearch", - 1, Array.BinarySearch (array, 0)); + Assert.AreEqual (- 1, Array.BinarySearch (array, 0), "BinarySearch"); } [Test] public void BinarySearch2_EmptyList () { int[] array = new int[0]; - AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, 0, 0)); + Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, 0), "BinarySearch"); } [Test] @@ -2665,9 +2997,9 @@ public class ArrayTest : Assertion { Comparer comparer = new Comparer (); int[] array = new int[0]; - AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, comparer)); + Assert.AreEqual (-1, Array.BinarySearch (array, 0, comparer), "BinarySearch"); // bug 77030 - the comparer isn't called for an empty array/list - Assert ("Called", !comparer.Called); + Assert.IsTrue (!comparer.Called, "Called"); } [Test] @@ -2675,12 +3007,11 @@ public class ArrayTest : Assertion { Comparer comparer = new Comparer (); int[] array = new int[0]; - AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, 0, comparer)); + Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, comparer), "BinarySearch"); // bug 77030 - the comparer isn't called for an empty array/list - Assert ("Called", !comparer.Called); + Assert.IsTrue (!comparer.Called, "Called"); } -#if NET_2_0 [Test] [ExpectedException (typeof (ArgumentNullException))] public void AsReadOnly_NullArray () @@ -2691,7 +3022,7 @@ public class ArrayTest : Assertion [Test] public void ReadOnly_Count () { - AssertEquals (10, Array.AsReadOnly (new int [10]).Count); + Assert.AreEqual (10, Array.AsReadOnly (new int [10]).Count); } [Test] @@ -2702,8 +3033,8 @@ public class ArrayTest : Assertion arr [1] = 5; IList a = Array.AsReadOnly (arr); - Assert (a.Contains (3)); - Assert (!a.Contains (6)); + Assert.IsTrue (a.Contains (3)); + Assert.IsTrue (!a.Contains (6)); } [Test] @@ -2714,9 +3045,9 @@ public class ArrayTest : Assertion arr [1] = 5; IList a = Array.AsReadOnly (arr); - AssertEquals (0, a.IndexOf (3)); - AssertEquals (1, a.IndexOf (5)); - AssertEquals (-1, a.IndexOf (6)); + Assert.AreEqual (0, a.IndexOf (3)); + Assert.AreEqual (1, a.IndexOf (5)); + Assert.AreEqual (-1, a.IndexOf (6)); } [Test] @@ -2727,12 +3058,12 @@ public class ArrayTest : Assertion arr [1] = 5; IList a = Array.AsReadOnly (arr); - AssertEquals (3, a [0]); - AssertEquals (5, a [1]); + Assert.AreEqual (3, a [0]); + Assert.AreEqual (5, a [1]); /* Check that modifications to the original array are visible */ arr [0] = 6; - AssertEquals (6, a [0]); + Assert.AreEqual (6, a [0]); } [Test] @@ -2747,7 +3078,46 @@ public class ArrayTest : Assertion foreach (int i in Array.AsReadOnly (arr)) sum += i; - AssertEquals (45, sum); + Assert.AreEqual (45, sum); + } + + [Test] + public void ReadOnly_CopyTo () + { + int[] arr = new int [2]; + arr [0] = 3; + arr [1] = 5; + IList a = Array.AsReadOnly (arr); + + int[] arr2 = new int [3]; + a.CopyTo (arr2, 1); + + Assert.AreEqual (0, arr2 [0]); + Assert.AreEqual (3, arr2 [1]); + Assert.AreEqual (5, arr2 [2]); + } + + [Test] + public void Resize () + { + int [] arr = new int [] { 1, 3, 5 }; + Array.Resize (ref arr, 3); + Assert.AreEqual (3, arr.Length, "#A1"); + Assert.AreEqual (1, arr [0], "#A2"); + Assert.AreEqual (3, arr [1], "#A3"); + Assert.AreEqual (5, arr [2], "#A4"); + + Array.Resize (ref arr, 2); + Assert.AreEqual (2, arr.Length, "#B1"); + Assert.AreEqual (1, arr [0], "#B2"); + Assert.AreEqual (3, arr [1], "#B3"); + + Array.Resize (ref arr, 4); + Assert.AreEqual (4, arr.Length, "#C1"); + Assert.AreEqual (1, arr [0], "#C2"); + Assert.AreEqual (3, arr [1], "#C3"); + Assert.AreEqual (0, arr [2], "#C4"); + Assert.AreEqual (0, arr [3], "#C5"); } [Test] @@ -2755,7 +3125,7 @@ public class ArrayTest : Assertion { int [] arr = null; Array.Resize (ref arr, 10); - AssertEquals (arr.Length, 10); + Assert.AreEqual (arr.Length, 10); } [Test] @@ -2763,27 +3133,508 @@ public class ArrayTest : Assertion { EquatableClass[] list = new EquatableClass[] {new EquatableClass (0), new EquatableClass (1), new EquatableClass (0)}; - AssertEquals ("#0", 0, Array.IndexOf (list, list[0])); - AssertEquals ("#1", 0, Array.IndexOf (list, new EquatableClass (0))); - AssertEquals ("#2", 2, Array.LastIndexOf (list, list[0])); - AssertEquals ("#3", 2, Array.LastIndexOf (list, new EquatableClass (0))); + Assert.AreEqual (0, Array.IndexOf (list, list[0]), "#0"); + Assert.AreEqual (0, Array.IndexOf (list, new EquatableClass (0)), "#1"); + Assert.AreEqual (2, Array.LastIndexOf (list, list[0]), "#2"); + Assert.AreEqual (2, Array.LastIndexOf (list, new EquatableClass (0)), "#3"); } public class EquatableClass : IEquatable { - int _x; - public EquatableClass (int x) - { - _x = x; - } + int _x; + public EquatableClass (int x) + { + _x = x; + } + + public bool Equals (EquatableClass other) + { + return this._x == other._x; + } + } + + [Test] + public void AsIList () + { + IList arr = new int [10]; + arr [0] = 5; + Assert.AreEqual (5, arr [0]); + + IList arr2 = new FooStruct [10]; + FooStruct s = new FooStruct (); + s.i = 11; + s.j = 22; + arr2 [5] = s; + s = arr2 [5]; + Assert.AreEqual (11, s.i); + Assert.AreEqual (22, s.j); + + IList arr3 = new string [10]; + arr3 [5] = "ABC"; + Assert.AreEqual ("ABC", arr3 [5]); + } + + struct FooStruct { + public int i, j; + } + +#if !TARGET_JVM // BugBUG: T[] is not yet ICollection under TARGET_JVM + [Test] + // From bug #80563 + public void ICollectionNull () + { + ICollection test; + + test = new List(); + Assert.AreEqual (test.Contains (null), false, "list"); + + test = new object[] {}; + Assert.AreEqual (test.Contains (null), false, "empty array"); + + test = new object[] {null}; + Assert.AreEqual (test.Contains (null), true, "array with null"); + + test = new object[] { 1, null}; + Assert.IsTrue (test.Contains (null), "array with last null"); + + test = new List(test); + Assert.AreEqual (test.Contains (null), true, "List with test"); + + test = new object[] {new object()}; + Assert.AreEqual (test.Contains (null), false, "array with object"); + + test = new List(test); + Assert.AreEqual (test.Contains (null), false, "array with test"); + } + + [Test] + public void IListNull () + { + IList test; + + test = new List(); + Assert.AreEqual (-1, test.IndexOf (null), "list"); + + test = new object[] {}; + Assert.AreEqual (-1, test.IndexOf (null), "empty array"); + + test = new object[] {null}; + Assert.AreEqual (0, test.IndexOf (null), "array with null"); + + test = new object[] { 1, null}; + Assert.AreEqual (1, test.IndexOf (null), "array with last null"); + + test = new List(test); + Assert.AreEqual (1, test.IndexOf (null), "List with test"); + + test = new object[] {new object()}; + Assert.AreEqual (-1, test.IndexOf (null), "array with object"); - public bool Equals (EquatableClass other) - { - return this._x == other._x; - } + test = new List(test); + Assert.AreEqual (-1, test.IndexOf (null), "array with test"); + } + +#endif // TARGET_JVM + #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 + + [Test] // bug #322248 + public void IEnumerator_Reset () + { + int[] array = new int[] { 1, 2, 3}; + IEnumerator e = ((IEnumerable)array).GetEnumerator (); + Assert.IsTrue (e.MoveNext (), "#A1"); + Assert.AreEqual (1, e.Current, "#A2"); + Assert.IsTrue (e.MoveNext (), "#A3"); + Assert.AreEqual (2, e.Current, "#A4"); + + e.Reset (); + + Assert.IsTrue (e.MoveNext (), "#C1"); + Assert.AreEqual (1, e.Current, "#C2"); + } + + [Test] + public void IEnumerator_Current_Finished () + { + int[] array = new int[] { 1, 2, 3 }; + IEnumerator e = ((IEnumerable)array).GetEnumerator (); + Assert.IsTrue (e.MoveNext (), "#A1"); + Assert.AreEqual (1, e.Current, "#A2"); + Assert.IsTrue (e.MoveNext (), "#A3"); + Assert.AreEqual (2, e.Current, "#A4"); + Assert.IsTrue (e.MoveNext (), "#A5"); + Assert.AreEqual (3, e.Current, "#A6"); + Assert.IsTrue (!e.MoveNext (), "#A6"); + + try { + Assert.Fail ("#B1:" + e.Current); + } catch (InvalidOperationException ex) { + // Enumeration already finished + Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2"); + Assert.IsNull (ex.InnerException, "#B3"); + Assert.IsNotNull (ex.Message, "#B4"); + } + } + + [Test] + public void IEnumerator_Current_NotStarted () + { + int[] array = new int[] { 1, 2, 3 }; + IEnumerator e = ((IEnumerable)array).GetEnumerator (); + + try { + Assert.Fail ("#A1:" + e.Current); + } catch (InvalidOperationException ex) { + // Enumeration has not started. Call MoveNext + Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2"); + Assert.IsNull (ex.InnerException, "#A3"); + Assert.IsNotNull (ex.Message, "#A4"); + } + } + + [Test] + public void IEnumerator_Current_Reset () + { + int[] array = new int[] { 1, 2, 3 }; + IEnumerator e = ((IEnumerable)array).GetEnumerator (); + e.MoveNext (); + e.Reset (); + + try { + Assert.Fail ("#B1:" + e.Current); + } catch (InvalidOperationException ex) { + // Enumeration has not started. Call MoveNext + Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2"); + Assert.IsNull (ex.InnerException, "#B3"); + Assert.IsNotNull (ex.Message, "#B4"); + } + } + + public void ICollection_IsReadOnly() { + ICollection arr = new string [10]; + + Assert.IsTrue (arr.IsReadOnly); + } + + [Test] + [ExpectedException (typeof (NotSupportedException))] + public void ArrayCreateInstanceOfVoid () + { + Array.CreateInstance (typeof (void), 42); + } + + class Foo {} + + [Test] + [ExpectedException (typeof (NotSupportedException))] + public void ArrayCreateInstanceOfOpenGenericType () + { + Array.CreateInstance (typeof (Foo<>), 42); + } + + [Test] + [ExpectedException (typeof (IndexOutOfRangeException))] + public void ClearNegativeLength () + { + Array.Clear (new int [] { 1, 2 }, 0, -1); + } + + [Test] + [ExpectedException (typeof (ArgumentException))] + public void MultiDimension_IList_setItem () + { + IList array = new int [1, 1]; + array [0] = 2; + } + + [Test] + [ExpectedException (typeof (ArgumentException))] + public void MultiDimension_IList_getItem () + { + IList array = new int [1, 1]; + int a = (int) array [0]; + } + + [Test] + public void SetValue_Nullable () { + Array array = Array.CreateInstance (typeof (int?), 7); + + object o = 42; + + array.SetValue (o, 0); + Assert.AreEqual (42, array.GetValue (0)); + + array.SetValue (null, 0); + Assert.AreEqual (null, array.GetValue (0)); + } + + [Test] + public void SortNullsWithGenericVersion () + { + string[] s1 = new string[6]{ + "J", + "M", + null, + "P", + "T", + "A"}; + + string[] s2 = new string[]{null, + "A", + "J", + "M", + "P", + "T"}; + + Array.Sort (s1); + for (int i = 0; i < 6; i++) { + Assert.AreEqual (s1[i], s2[i], "At:" + i); + } + } + + // + // This is a test case for the case that was broken by the code contributed + // for bug #351638. + // + // This tests the fix for: #622101 + // + [Test] + public void SortActuallyWorks () + { + string[] data = new string[9]{"Foo", "Bar", "Dingus", null, "Dingu4", "123", "Iam", null, "NotNull"}; + IComparer comparer = new NullAtEndComparer (); + Array.Sort (data, comparer); + + Assert.AreEqual (data [7], null); + Assert.AreNotEqual (data [0], null); + } + + class NullAtEndComparer : IComparer { + public int Compare(object x, object y) + { + if (x == null) return 1; + if (y == null) return -1; + return ((string)x).CompareTo((string)y); + } + } + + [Test] //bxc #11184 + public void UnalignedArrayClear () + { + byte[] input = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; + byte[] expected = new byte[] { 1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + Array.Clear (input, 5, 11); + + Assert.AreEqual (input, expected); + } + +#if NET_4_0 + [Test] + [ExpectedException (typeof (ArgumentException))] + public void CompareToWithJaggedArray () { + IStructuralComparable a = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }}; + IStructuralComparable b = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }}; + a.CompareTo (b, Comparer.Default); + } + + [Test] + [ExpectedException (typeof (ArgumentException))] + public void CompareToWithArrayOfTheWrongKind () { + IStructuralComparable a = new int[] { 1, 2 }; + IStructuralComparable b = new double[] { 1, 2 }; + a.CompareTo (b, Comparer.Default); + } + + [Test] + [ExpectedException (typeof (ArgumentException))] + public void CompareToWithNonArrayType () { + IStructuralComparable a = new int[] { 1, 2 }; + a.CompareTo (99, Comparer.Default); + } + + [Test] + [ExpectedException (typeof (ArgumentException))] + public void CompareToWithNonArrayOfDifferentSize () { + IStructuralComparable a = new int[] { 1, 2 }; + IStructuralComparable b = new int[] { 1, 2, 3 }; + a.CompareTo (b, Comparer.Default); + } + + [Test] + [ExpectedException (typeof (ArgumentException))] + public void CompareToWithMultiDimArray1 () { + IStructuralComparable a = new int [2,2] { {10, 10 }, { 10, 10 } }; + IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } }; + a.CompareTo (b, Comparer.Default); + } + + [Test] + [ExpectedException (typeof (ArgumentException))] + public void CompareToWithMultiDimArray2 () { + IStructuralComparable a = new int [2] { 10, 10 }; + IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } }; + a.CompareTo (b, Comparer.Default); + } + + [Test] + [ExpectedException (typeof (ArgumentException))] + public void CompareToWithMultiDimArray3 () { + IStructuralComparable a = new int [4] { 10, 10, 10, 10 }; + IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } }; + a.CompareTo (b, Comparer.Default); + } + + [Test] + [ExpectedException (typeof (IndexOutOfRangeException))] + public void CompareToWithBoundedArray1 () { + IStructuralComparable a = new int [2] { 10, 10 }; + Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 }); + IStructuralComparable b = ab; + ab.SetValue (10, 5); + ab.SetValue (10, 6); + + a.CompareTo (b, Comparer.Default); + } + + [Test] + [ExpectedException (typeof (IndexOutOfRangeException))] + public void CompareToWithBoundedArray2 () { + IStructuralComparable a = new int [2] { 10, 10 }; + Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 }); + IStructuralComparable b = ab; + ab.SetValue (10, 5); + ab.SetValue (10, 6); + + //Yes, CompareTo simply doesn't work with bounded arrays! + b.CompareTo (b, Comparer.Default); + } + + [Test] + [ExpectedException (typeof (NullReferenceException))] + public void CompareToWithNullComparer () { + IStructuralComparable a = new int[] { 1, 2 }; + IStructuralComparable b = new int[] { 1, 2 }; + a.CompareTo (b, null); + } + + [Test] + public void CompareToWithNullArray () { + IStructuralComparable a = new int[] { 1, 2 }; + Assert.AreEqual (1, a.CompareTo (null, Comparer.Default)); + } + + [Test] + public void CompareToWithGoodArrays () { + IStructuralComparable a = new int[] { 10, 20 }; + Assert.AreEqual (0, a.CompareTo (a, Comparer.Default)); + Assert.AreEqual (0, a.CompareTo (new int [] { 10, 20 }, Comparer.Default)); + Assert.AreEqual (-1, a.CompareTo (new int [] { 11, 20 }, Comparer.Default)); + Assert.AreEqual (-1, a.CompareTo (new int [] { 10, 21 }, Comparer.Default)); + Assert.AreEqual (1, a.CompareTo (new int [] { 9, 20 }, Comparer.Default)); + Assert.AreEqual (1, a.CompareTo (new int [] { 10, 19 }, Comparer.Default)); + } + + [Test] + public void IStructuralEquatable_Equals () + { + IStructuralEquatable array = new int[] {1, 2, 3}; + IStructuralEquatable array2 = new int[] {1, 2, 3}; + Assert.AreEqual (false, array.Equals (null, null)); + Assert.AreEqual (true, array.Equals (array, null)); + Assert.AreEqual (true, array.Equals (array2, EqualityComparer.Default)); + } + + [Test] + [ExpectedException (typeof (NullReferenceException))] + public void IStructuralEquatable_Equals_NoComparer () + { + IStructuralEquatable array = new int[] {1, 2, 3}; + IStructuralComparable array2 = new int[] {1, 2, 3}; + array.Equals (array2, null); + } + + [Test] + [ExpectedException (typeof (ArgumentException))] + public void IStructuralEquatable_Equals_ComparerThrows () + { + IStructuralEquatable array = new int[] {1, 2, 3}; + IStructuralComparable array2 = new int[] {1, 2, 3}; + array.Equals (array2, EqualityComparer.Default); + } + + [Test] + [ExpectedException (typeof (ArgumentNullException))] + public void IStructuralEquatable_GetHashCode_NullComparer () + { + IStructuralEquatable a = new int[] { 1, 2 }; + a.GetHashCode (null); + } + + class TestComparer_GetHashCode : IEqualityComparer + { + public int Counter; + + bool IEqualityComparer.Equals (object x, object y) + { + throw new NotImplementedException (); + } + + public int GetHashCode (object obj) + { + return Counter++; + } + } + + [Test] + public void IStructuralEquatable_GetHashCode () + { + IStructuralEquatable a = new int[] { 1, 2, 9 }; + + var c = new TestComparer_GetHashCode (); + a.GetHashCode (c); + Assert.AreEqual (3, c.Counter); + } + #endif -} } +}