// Eduardo Garcia (kiwnix@yahoo.es)
//
// (C) Ximian, Inc. http://www.ximian.com
+// Copyright (C) 2004 Novell (http://www.novell.com)
//
using NUnit.Framework;
using System.Collections;
using System.Globalization;
+#if NET_2_0
+using System.Collections.Generic;
+#endif
+
namespace MonoTests.System
{
AssertEquals("#B30", 49, Array.BinarySearch(array, 10));
}
+ [Test]
+ public void BinarySearch_NullValue ()
+ {
+ int[] array = new int[1];
+ AssertEquals ("I=a,o", -1, Array.BinarySearch (array, null));
+ AssertEquals ("I=a,o,c", -1, Array.BinarySearch (array, null, null));
+ AssertEquals ("I=a,i,i,o", -1, Array.BinarySearch (array, 0, 1, null));
+ AssertEquals ("I=a,i,i,o,c", -1, Array.BinarySearch (array, 0, 1, null,null));
+
+ object[] o = new object [3] { this, this, null };
+ AssertEquals ("O=a,o", -1, Array.BinarySearch (o, null));
+ AssertEquals ("O=a,o,c", -1, Array.BinarySearch (o, null, null));
+ AssertEquals ("O=a,i,i,o", -1, Array.BinarySearch (o, 0, 3, null));
+ AssertEquals ("O=a,i,i,o,c", -1, Array.BinarySearch (o, 0, 3, null, null));
+ }
+
// TODO - testBinarySearch with explicit IComparer args
[Test]
AssertEquals("#F13(" + i + ")", src[i], array.GetValue(i+5));
}
+ // Test that a 1 dimensional array with 0 lower bound is the
+ // same as an szarray
+ Type szarrayType = new int [10].GetType ();
+ Assert (szarrayType == (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0})).GetType ());
+ Assert (szarrayType != (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {1})).GetType ());
}
[Test]
Array a = Array.CreateInstance (typeof (Int32), (int[])null);
}
-#if NET_1_1
[Test]
- [ExpectedException (typeof (NullReferenceException))]
+#if NET_2_0
+ [ExpectedException (typeof (ArgumentNullException))]
+#else
+ [ExpectedException (typeof (NullReferenceException))]
+#endif
public void TestCreateInstance2b ()
{
Array a = Array.CreateInstance (typeof (Int32), (long[])null);
}
-#endif
[Test]
public void TestGetEnumerator() {
}
}
-#if NET_1_1
[Test]
- [ExpectedException (typeof (NullReferenceException))]
- public void TestGetValueLongArray() {
+#if NET_2_0
+ [ExpectedException (typeof (ArgumentNullException))]
+#else
+ [ExpectedException (typeof (NullReferenceException))]
+#endif
+ public void TestGetValueLongArray ()
+ {
char[] c = new Char[2];
c.GetValue((long [])null);
}
-#endif
[Test]
public void TestGetValueN() {
}
Assert (!error);
}
-
+
+ [Test]
+ [ExpectedException (typeof (ArgumentOutOfRangeException))]
+ public void LastIndexOf_StartIndexOverflow ()
+ {
+ // legal - no exception
+ byte[] array = new byte [16];
+ Array.LastIndexOf (array, this, Int32.MaxValue, 1);
+ }
+
+ [Test]
+ [ExpectedException (typeof (ArgumentOutOfRangeException))]
+ public void LastIndexOf_CountOverflow ()
+ {
+ // legal - no exception
+ byte[] array = new byte [16];
+ Array.LastIndexOf (array, this, 1, Int32.MaxValue);
+ }
+
[Test]
public void TestReverse() {
{
}
}
-#if NET_1_1
[Test]
- [ExpectedException (typeof (NullReferenceException))]
- public void TestSetValueLongArray() {
+#if NET_2_0
+ [ExpectedException (typeof (ArgumentNullException))]
+#else
+ [ExpectedException (typeof (NullReferenceException))]
+#endif
+ public void TestSetValueLongArray ()
+ {
char[] c = new Char[2];
c.SetValue("buh", (long [])null);
}
-#endif
-
[Test]
public void TestSetValueN() {
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]
+ public void ClearJaggedArray ()
+ {
+ byte[][] matrix = new byte [8][];
+ for (int i=0; i < 8; i++) {
+ matrix [i] = new byte [8];
+ for (int j=0; j < 8; j++) {
+ matrix [i][j] = 1;
+ }
+ }
+ Array.Clear (matrix, 0, 8);
+ for (int i=0; i < 8; i++) {
+ AssertNull (i.ToString (), matrix [i]);
+ }
+ }
+
+ [Test]
+ public void ClearMultidimentionalArray ()
+ {
+ 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]);
+ }
+
+ [Test]
+ [ExpectedException (typeof (IndexOutOfRangeException))]
+ public void ClearOutsideMultidimentionalArray ()
+ {
+ byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
+ Array.Clear (matrix, 0, 5);
+ }
+
+ [Test]
+ [ExpectedException (typeof (IndexOutOfRangeException))]
+ public void Clear_IndexOverflow ()
+ {
+ byte[] array = new byte [16];
+ Array.Clear (array, 4, Int32.MaxValue);
+ }
+
+ [Test]
+ [ExpectedException (typeof (IndexOutOfRangeException))]
+ public void Clear_LengthOverflow ()
+ {
+ byte[] array = new byte [16];
+ Array.Clear (array, Int32.MaxValue, 4);
+ }
+
+ [Test]
+ [ExpectedException (typeof (ArgumentException))]
+ public void Copy_SourceIndexOverflow ()
+ {
+ byte[] array = new byte [16];
+ Array.Copy (array, Int32.MaxValue, array, 8, 8);
+ }
+
+ [Test]
+ [ExpectedException (typeof (ArgumentException))]
+ public void Copy_DestinationIndexOverflow ()
+ {
+ byte[] array = new byte [16];
+ Array.Copy (array, 8, array, Int32.MaxValue, 8);
+ }
+
+ [Test]
+ [ExpectedException (typeof (ArgumentException))]
+ public void Copy_LengthOverflow ()
+ {
+ byte[] array = new byte [16];
+ Array.Copy (array, 8, array, 8, Int32.MaxValue);
+ }
+
+ [Test]
+ [ExpectedException (typeof (ArgumentException))]
+ public void Reverse_IndexOverflow ()
+ {
+ byte[] array = new byte [16];
+ Array.Reverse (array, Int32.MaxValue, 8);
+ }
+
+ [Test]
+ [ExpectedException (typeof (ArgumentException))]
+ public void Reverse_LengthOverflow ()
+ {
+ byte[] array = new byte [16];
+ Array.Reverse (array, 8, Int32.MaxValue);
+ }
+
+ public struct CharX : IComparable {
+ public char c;
+
+ public CharX (char c)
+ {
+ this.c = c;
+ }
+
+ public int CompareTo (object obj)
+ {
+ if (obj is CharX)
+ return c.CompareTo (((CharX) obj).c);
+ else
+ return c.CompareTo (obj);
+ }
+ }
+
+ [Test]
+ public void BinarySearch_ArgPassingOrder ()
+ {
+ //
+ // This tests that arguments are passed to the comprer in the correct
+ // order. The IComparable of the *array* elements must get called, not
+ // that of the search object.
+ //
+ CharX [] x = { new CharX ('a'), new CharX ('b'), new CharX ('c') };
+ AssertEquals (1, Array.BinarySearch (x, 'b'));
+ }
+
+ class Comparer: IComparer {
+
+ private bool called = false;
+
+ public bool Called {
+ get {
+ bool result = called;
+ called = false;
+ return called;
+ }
+ }
+
+ public int Compare (object x, object y)
+ {
+ called = true;
+ return 0;
+ }
+ }
+
+ [Test]
+ public void BinarySearch1_EmptyList ()
+ {
+ int[] array = new int[0];
+ AssertEquals ("BinarySearch", - 1, Array.BinarySearch (array, 0));
+ }
+
+ [Test]
+ public void BinarySearch2_EmptyList ()
+ {
+ int[] array = new int[0];
+ AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, 0, 0));
+ }
+
+ [Test]
+ public void BinarySearch3_EmptyList ()
+ {
+ Comparer comparer = new Comparer ();
+ int[] array = new int[0];
+ AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, comparer));
+ // bug 77030 - the comparer isn't called for an empty array/list
+ Assert ("Called", !comparer.Called);
+ }
+
+ [Test]
+ public void BinarySearch4_EmptyList ()
+ {
+ Comparer comparer = new Comparer ();
+ int[] array = new int[0];
+ AssertEquals ("BinarySearch", -1, Array.BinarySearch (array, 0, 0, comparer));
+ // bug 77030 - the comparer isn't called for an empty array/list
+ Assert ("Called", !comparer.Called);
+ }
+
+#if NET_2_0
+ [Test]
+ [ExpectedException (typeof (ArgumentNullException))]
+ public void AsReadOnly_NullArray ()
+ {
+ Array.AsReadOnly <int> (null);
+ }
+
+ [Test]
+ public void ReadOnly_Count ()
+ {
+ AssertEquals (10, Array.AsReadOnly (new int [10]).Count);
+ }
+
+ [Test]
+ public void ReadOnly_Contains ()
+ {
+ int[] arr = new int [2];
+ arr [0] = 3;
+ arr [1] = 5;
+ IList<int> a = Array.AsReadOnly (arr);
+
+ Assert (a.Contains (3));
+ Assert (!a.Contains (6));
+ }
+
+ [Test]
+ public void ReadOnly_IndexOf ()
+ {
+ int[] arr = new int [2];
+ arr [0] = 3;
+ arr [1] = 5;
+ IList<int> a = Array.AsReadOnly (arr);
+
+ AssertEquals (0, a.IndexOf (3));
+ AssertEquals (1, a.IndexOf (5));
+ AssertEquals (-1, a.IndexOf (6));
+ }
+
+ [Test]
+ public void ReadOnly_Indexer ()
+ {
+ int[] arr = new int [2];
+ arr [0] = 3;
+ arr [1] = 5;
+ IList<int> a = Array.AsReadOnly (arr);
+
+ AssertEquals (3, a [0]);
+ AssertEquals (5, a [1]);
+
+ /* Check that modifications to the original array are visible */
+ arr [0] = 6;
+ AssertEquals (6, a [0]);
+ }
+
+ [Test]
+ public void ReadOnly_Enumerator ()
+ {
+ int[] arr = new int [10];
+
+ for (int i = 0; i < 10; ++i)
+ arr [i] = i;
+
+ int sum = 0;
+ foreach (int i in Array.AsReadOnly (arr))
+ sum += i;
+
+ AssertEquals (45, sum);
+ }
+
+ [Test]
+ public void Resize_null ()
+ {
+ int [] arr = null;
+ Array.Resize (ref arr, 10);
+ AssertEquals (arr.Length, 10);
+ }
+
+ [Test]
+ public void Test_ContainsAndIndexOf_EquatableItem ()
+ {
+ EquatableClass[] list = new EquatableClass[] {new EquatableClass (0), new EquatableClass (1), new EquatableClass (0)};
+
+ AssertEquals ("#0", 0, Array.IndexOf<EquatableClass> (list, list[0]));
+ AssertEquals ("#1", 0, Array.IndexOf<EquatableClass> (list, new EquatableClass (0)));
+ AssertEquals ("#2", 2, Array.LastIndexOf<EquatableClass> (list, list[0]));
+ AssertEquals ("#3", 2, Array.LastIndexOf<EquatableClass> (list, new EquatableClass (0)));
+ }
+
+ public class EquatableClass : IEquatable<EquatableClass>
+ {
+ int _x;
+ public EquatableClass (int x)
+ {
+ _x = x;
+ }
+
+ public bool Equals (EquatableClass other)
+ {
+ return this._x == other._x;
+ }
+
+ }
+
+ [Test]
+ // From bug #80563
+ public void ICollectionNull ()
+ {
+ ICollection<object> test;
+
+ test = new List<object>();
+ AssertEquals ("list<o>", test.Contains (null), false);
+
+ test = new object[] {};
+ AssertEquals ("empty array", test.Contains (null), false);
+
+ test = new object[] {null};
+ AssertEquals ("array with null", test.Contains (null), true);
+
+ test = new List<object>(test);
+ AssertEquals ("List<object> with test", test.Contains (null), true);
+
+ test = new object[] {new object()};
+ AssertEquals ("array with object", test.Contains (null), false);
+
+ test = new List<object>(test);
+ AssertEquals ("array with test", test.Contains (null), false);
+ }
+#endif
+
+ #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
}
}