using System;
using System.Collections;
using System.Globalization;
-
-#if NET_2_0
+using System.Reflection;
using System.Collections.Generic;
-#endif
namespace MonoTests.System
{
{
return true;
}
+
+ public override int GetHashCode ()
+ {
+ return 0;
+ }
}
//End Auxiliary Things
Assert.AreEqual (-1, Array.BinarySearch (o, 0, 3, null, null), "O=a,i,i,o,c");
}
- // TODO - testBinarySearch with explicit IComparer args
+ class TestComparer7 : IComparer<int>
+ {
+ public int Compare (int x, int y)
+ {
+ if (y != 7)
+ throw new ApplicationException ();
+
+ return x.CompareTo (y);
+ }
+ }
+
+ [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() {
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];
}
Assert.IsTrue (errorThrown, "#F03a");
}
-#if NET_1_1
+
{
bool errorThrown = false;
try {
}
Assert.IsTrue (errorThrown, "#F03b");
}
-#endif
#if !TARGET_JVM // Arrays lower bounds are not supported for TARGET_JVM
{
bool errorThrown = false;
}
[Test]
-#if NET_2_0
[ExpectedException (typeof (ArgumentNullException))]
-#else
- [ExpectedException (typeof (NullReferenceException))]
-#endif
public void TestCreateInstance2b ()
{
Array.CreateInstance (typeof (Int32), (long[])null);
}
[Test]
-#if NET_2_0
[ExpectedException (typeof (ArgumentNullException))]
-#else
- [ExpectedException (typeof (NullReferenceException))]
-#endif
public void TestGetValueLongArray ()
{
char[] c = new Char[2];
NUnit.Framework.Assert.Fail ("#1");
} catch (ArgumentOutOfRangeException) { }
-#if NET_2_0
try {
Array.LastIndexOf<short> (a, 16, -1);
NUnit.Framework.Assert.Fail ("#2");
} catch (ArgumentOutOfRangeException) { }
-#endif
}
[Test]
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 TestReverse() {
{
}
[Test]
-#if NET_2_0
[ExpectedException (typeof (ArgumentNullException))]
-#else
- [ExpectedException (typeof (NullReferenceException))]
-#endif
public void TestSetValueLongArray ()
{
char[] c = new Char[2];
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<double, int> (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] // #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]
Assert.IsTrue (!comparer.Called, "Called");
}
-#if NET_2_0
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void AsReadOnly_NullArray ()
Assert.AreEqual (45, sum);
}
+ [Test]
+ public void ReadOnly_CopyTo ()
+ {
+ int[] arr = new int [2];
+ arr [0] = 3;
+ arr [1] = 5;
+ IList<int> 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 ()
{
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<object>(test);
Assert.AreEqual (test.Contains (null), true, "List<object> with test");
test = new List<object>(test);
Assert.AreEqual (test.Contains (null), false, "array with test");
}
+
+ [Test]
+ public void IListNull ()
+ {
+ IList<object> test;
+
+ test = new List<object>();
+ Assert.AreEqual (-1, test.IndexOf (null), "list<o>");
+
+ 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<object>(test);
+ Assert.AreEqual (1, test.IndexOf (null), "List<object> with test");
+
+ test = new object[] {new object()};
+ Assert.AreEqual (-1, test.IndexOf (null), "array with object");
+
+ test = new List<object>(test);
+ Assert.AreEqual (-1, test.IndexOf (null), "array with test");
+ }
+
#endif // TARGET_JVM
-#endif
#region Bug 80299
#endregion
-#if NET_2_0
[Test] // bug #322248
public void IEnumerator_Reset ()
{
Assert.IsTrue (arr.IsReadOnly);
}
+
+ [Test]
+ [ExpectedException (typeof (NotSupportedException))]
+ public void ArrayCreateInstanceOfVoid ()
+ {
+ Array.CreateInstance (typeof (void), 42);
+ }
+
+ class Foo<T> {}
+
+ [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<string> (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);
+ }
+ }
+
+#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<object>.Default);
+ }
+
+ [Test]
+ [ExpectedException (typeof (ArgumentException))]
+ public void CompareToWithArrayOfTheWrongKind () {
+ IStructuralComparable a = new int[] { 1, 2 };
+ IStructuralComparable b = new double[] { 1, 2 };
+ a.CompareTo (b, Comparer<object>.Default);
+ }
+
+ [Test]
+ [ExpectedException (typeof (ArgumentException))]
+ public void CompareToWithNonArrayType () {
+ IStructuralComparable a = new int[] { 1, 2 };
+ a.CompareTo (99, Comparer<object>.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<object>.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<object>.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<object>.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<object>.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<object>.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<object>.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<object>.Default));
+ }
+
+ [Test]
+ public void CompareToWithGoodArrays () {
+ IStructuralComparable a = new int[] { 10, 20 };
+ Assert.AreEqual (0, a.CompareTo (a, Comparer<object>.Default));
+ Assert.AreEqual (0, a.CompareTo (new int [] { 10, 20 }, Comparer<object>.Default));
+ Assert.AreEqual (-1, a.CompareTo (new int [] { 11, 20 }, Comparer<object>.Default));
+ Assert.AreEqual (-1, a.CompareTo (new int [] { 10, 21 }, Comparer<object>.Default));
+ Assert.AreEqual (1, a.CompareTo (new int [] { 9, 20 }, Comparer<object>.Default));
+ Assert.AreEqual (1, a.CompareTo (new int [] { 10, 19 }, Comparer<object>.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<int>.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<long>.Default);
+ }
+
#endif
+
}
}