/mcs/class/corlib/Test/System/ArrayTest.cs
C# | 3465 lines | 3015 code | 374 blank | 76 comment | 85 complexity | a473661e89722761313731330b3b72a7 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, Unlicense, Apache-2.0, LGPL-2.0
Large files files are truncated, but you can click here to view the full file
- // ArrayTest.cs - NUnit Test Cases for the System.Array class
- //
- // David Brandt (bucky@keystreams.com)
- // 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;
- using System.Collections;
- using System.Globalization;
- using System.Reflection;
- using System.Collections.Generic;
- namespace MonoTests.System
- {
- //Auxiliary Things
- enum enua {hola,adios,mas,menos};
- class AClass
- {
- public AClass()
- {
- }
- }
- 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
- {
- char [] arrsort = {'d', 'b', 'f', 'e', 'a', 'c'};
- public ArrayTest() {}
- [Test]
- public void TestIsFixedSize() {
- char[] a1 = {'a'};
- Assert.IsTrue (a1.IsFixedSize, "All arrays are fixed");
- }
-
- [Test]
- public void TestIsReadOnly() {
- char[] a1 = {'a'};
- Assert.IsTrue (!a1.IsReadOnly, "No array is readonly");
- }
- [Test]
- public void TestIsSynchronized() {
- char[] a1 = {'a'};
- Assert.IsTrue (!a1.IsSynchronized, "No array is synchronized");
- }
- [Test]
- public void TestLength() {
- {
- char[] a1 = { };
- Assert.AreEqual (0, a1.Length, "Zero length array");
- }
- {
- char[] a1 = {'c'};
- Assert.AreEqual (1, a1.Length, "One-length array");
- }
- {
- char[] a1 = {'c', 'c'};
- Assert.AreEqual (2, a1.Length, "Two-length array");
- }
- }
- [Test]
- public void TestRank() {
- char[] a1 = { 'c', 'd', 'e' };
- Assert.AreEqual (1, a1.Rank, "Rank one");
- char[,] a2 = new Char[3,3];
- Assert.AreEqual (2, a2.Rank, "Rank two");
- char[,,] a3 = new Char[3,3,3];
- Assert.AreEqual (3, a3.Rank, "Rank three");
- }
- [Test]
- public void TestBinarySearch1() {
- bool errorThrown = false;
- try {
- Array.BinarySearch(null, "blue");
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#B01");
- errorThrown = false;
- try {
- char[,] c1 = new Char[2,2];
- Array.BinarySearch(c1, "needle");
- } catch (RankException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#B02");
- {
- char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
- 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'};
- Assert.AreEqual (-4, Array.BinarySearch(arr, 'c'), "#B07");
- }
- {
- char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
- Assert.AreEqual (-9, Array.BinarySearch(arr, 'e'), "#B08");
- }
- }
- [Test]
- public void TestBinarySearch2() {
- bool errorThrown = false;
- try {
- Array.BinarySearch(null, 0, 1, "blue");
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#B20");
- errorThrown = false;
- try {
- char[,] c1 = new Char[2,2];
- Array.BinarySearch(c1, 0, 1, "needle");
- } catch (RankException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#B21");
- errorThrown = false;
- try {
- char[] c1 = {'a'};
- Array.BinarySearch(c1, -1, 1, 'a');
- } catch (ArgumentOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#B22");
- errorThrown = false;
- try {
- char[] c1 = {'a'};
- Array.BinarySearch(c1, 0, -1, 'a');
- } catch (ArgumentOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#B23");
- errorThrown = false;
- try {
- char[] c1 = {'a'};
- Array.BinarySearch(c1, 0, 4, 'a');
- } catch (ArgumentException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#B24");
- {
- char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
- 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'};
- Assert.AreEqual (-6, Array.BinarySearch(arr, 2, 8, 'c'), "#B28");
- }
- {
- char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
- Assert.AreEqual (-11, Array.BinarySearch(arr, 2, 8, 'e'), "#B29");
- }
- }
- public void TestBinarySearch3()
- {
- int[] array = new int[100];
- for (int i = 0; i < 100; i++)
- array[i] = 10;
- Assert.AreEqual (49, Array.BinarySearch(array, 10), "#B30");
- }
- [Test]
- public void BinarySearch_NullValue ()
- {
- int[] array = new int[1];
- 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 };
- 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<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() {
- bool errorThrown = false;
- try {
- Array.Clear(null, 0, 1);
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#C01");
- int[] i1 = { 1, 2, 3, 4 };
- {
- int[] compare = {1,2,3,4};
- 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};
- 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};
- 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};
- 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};
- Assert.AreEqual (compare[0], s1[0], "#C18");
- Assert.AreEqual (compare[1], s1[1], "#C19");
- Assert.AreEqual (compare[2], s1[2], "#C20");
- }
- }
- [Test]
- public void TestClone() {
- char[] c1 = {'a', 'b', 'c'};
- char[] c2 = (char[])c1.Clone();
- 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();
- 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';
- 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() {
- {
- bool errorThrown = false;
- try {
- Char[] c1 = {};
- Array.Copy(c1, null, 1);
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#E01");
- }
- {
- bool errorThrown = false;
- try {
- Char[] c1 = {};
- Array.Copy(null, c1, 1);
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#E02");
- }
- {
- bool errorThrown = false;
- try {
- Char[] c1 = new Char[1];
- Char[,] c2 = new Char[1,1];
- Array.Copy(c1, c2, 1);
- } catch (RankException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#E03");
- }
- {
- bool errorThrown = false;
- try {
- Char[] c1 = new Char[1];
- string[] s1 = new String[1];
- Array.Copy(c1, s1, 1);
- } catch (ArrayTypeMismatchException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#E04");
- }
- {
- bool errorThrown = false;
- try {
- Char[] c1 = new Char[1];
- Object[] o1 = new Object[1];
- o1[0] = "hello";
- Array.Copy(o1, c1, 1);
- } catch (InvalidCastException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#E05");
- }
- {
- bool errorThrown = false;
- try {
- Char[] c1 = new Char[1];
- Char[] c2 = new Char[1];
- Array.Copy(c1, c2, -1);
- } catch (ArgumentOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#E06");
- }
- {
- bool errorThrown = false;
- try {
- Char[] c1 = new Char[1];
- Char[] c2 = new Char[2];
- Array.Copy(c1, c2, 2);
- } catch (ArgumentException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#E07");
- }
- {
- bool errorThrown = false;
- try {
- Char[] c1 = new Char[1];
- Char[] c2 = new Char[2];
- Array.Copy(c2, c1, 2);
- } catch (ArgumentException) {
- errorThrown = true;
- }
- 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++) {
- Assert.AreEqual (orig[i], copy[i], "#E09(" + i + ")");
- }
- Array.Clear(copy, 0, copy.Length);
- for (int i = 0; i < orig.Length; i++) {
- Assert.AreEqual ((char)0, copy[i], "#E10(" + i + ")");
- }
- Array.Copy(orig, copy, 2);
- 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]
- public void TestCopy2() {
- {
- bool errorThrown = false;
- try {
- Char[] c1 = new Char[2];
- Char[] c2 = new Char[2];
- Array.Copy(c2, 1, c1, 0, 2);
- } catch (ArgumentException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#E31");
- }
- {
- bool errorThrown = false;
- try {
- Char[] c1 = new Char[2];
- Char[] c2 = new Char[2];
- Array.Copy(c2, 0, c1, 1, 2);
- } catch (ArgumentException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#E32");
- }
-
- char[] orig = {'a', 'b', 'd', 'a'};
- char[] copy = new Char[4];
- Array.Copy(orig, 1, copy, 1, 3);
- Assert.IsTrue (copy[0] != orig[0], "#E33");
- for (int i = 1; i < orig.Length; i++) {
- Assert.AreEqual (orig[i], copy[i], "#E34(" + i + ")");
- }
- Array.Clear(copy, 0, copy.Length);
- Array.Copy(orig, 1, copy, 0, 2);
- 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]
- [ExpectedException (typeof (InvalidCastException))]
- public void Copy_InvalidCast () {
- object[] arr1 = new object [10];
- Type[] arr2 = new Type [10];
- arr1 [0] = new object ();
- Array.Copy (arr1, 0, arr2, 0, 10);
- }
- [Test]
- public void TestCopyTo() {
- {
- bool errorThrown = false;
- try {
- Char[] c1 = new Char[2];
- c1.CopyTo(null, 2);
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#E61");
- }
- {
- bool errorThrown = false;
- try {
- Char[] c1 = new Char[2];
- Char[,] c2 = new Char[2,2];
- c1.CopyTo(c2, 2);
- } catch (ArgumentException) {
- errorThrown = true;
- }
- #if TARGET_JVM // This is really implementation dependent behaviour.
- catch (RankException) {
- errorThrown = true;
- }
- #endif // TARGET_JVM
- Assert.IsTrue (errorThrown, "#E62");
- }
- {
- bool errorThrown = false;
- try {
- Char[,] c1 = new Char[2,2];
- Char[] c2 = new Char[2];
- c1.CopyTo(c2, -1);
- } catch (RankException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#E63");
- }
- {
- bool errorThrown = false;
- try {
- Char[,] c1 = new Char[2,2];
- Char[] c2 = new Char[2];
- c1.CopyTo(c2, 2);
- } catch (RankException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#E64");
- }
- {
- bool errorThrown = false;
- try {
- Char[] c1 = new Char[2];
- Char[] c2 = new Char[2];
- c1.CopyTo(c2, -1);
- } catch (ArgumentOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#E65");
- }
- {
- bool errorThrown = false;
- try {
- Char[] c1 = new Char[2];
- Char[] c2 = new Char[2];
- c1.CopyTo(c2, 3);
- } catch (ArgumentException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#E66");
- }
- {
- bool errorThrown = false;
- try {
- Char[] c1 = new Char[2];
- Char[] c2 = new Char[2];
- c1.CopyTo(c2, 1);
- } catch (ArgumentException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#E67");
- }
- {
- bool errorThrown = false;
- try {
- String[] c1 = new String[2];
- // TODO: this crashes mono if there are null
- // values in the array.
- c1[1] = "hey";
- c1[0] = "you";
- Char[] c2 = new Char[2];
- c2[1] = 'a';
- c2[0] = 'z';
- c1.CopyTo(c2, 0);
- } catch (ArrayTypeMismatchException) {
- errorThrown = true;
- }
- 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);
- 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.
- bool errorThrown = false;
- try {
- int[] src = new int [0];
- int[] dest = new int [0];
- src.CopyTo (dest, 0);
- } catch (ArgumentException) {
- errorThrown = true;
- }
- Assert.IsTrue (!errorThrown, "#E79");
- }
- {
- // bug #38812
- bool errorThrown = false;
- try {
- CClass[] src = new CClass [] { new CClass () };
- BClass[] dest = new BClass [1];
- src.CopyTo (dest, 0);
- } catch (ArrayTypeMismatchException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#E80");
- }
- }
- [Test]
- public void TestCreateInstance() {
- {
- bool errorThrown = false;
- try {
- Array.CreateInstance(null, 12);
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#F01");
- }
- {
- bool errorThrown = false;
- try {
- Array.CreateInstance(Type.GetType("System.Char"), -3);
- } catch (ArgumentOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#F02");
- }
- {
- bool errorThrown = false;
- try {
- Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#F03a");
- }
- {
- bool errorThrown = false;
- try {
- Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#F03b");
- }
- #if !TARGET_JVM // Arrays lower bounds are not supported for TARGET_JVM
- {
- bool errorThrown = false;
- try {
- Array.CreateInstance(Type.GetType("System.Char"), null, null);
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#F04");
- }
- #endif // TARGET_JVM
- {
- bool errorThrown = false;
- try {
- int[] lengths = new int [0];
- Array.CreateInstance(Type.GetType("System.Char"), lengths);
- } catch (ArgumentException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#F05");
- }
- #if !TARGET_JVM // CreateInstance with lower bounds not supported for TARGET_JVM
- {
- bool errorThrown = false;
- try {
- int[] lengths = new int [1];
- int[] bounds = new int [2];
- Array.CreateInstance(Type.GetType("System.Char"), lengths, bounds);
- errorThrown = true;
- } catch (ArgumentException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#F06");
- }
- char[] c1 = (char[])Array.CreateInstance(Type.GetType("System.Char"), 12);
- Assert.AreEqual (12, c1.Length, "#F07");
- Array c2 = Array.CreateInstance(Type.GetType("System.Char"), 12, 5);
- Assert.AreEqual (2, c2.Rank, "#F08");
- Assert.AreEqual (60, c2.Length, "#F09");
- {
- int[] lengths = { 3 };
- int[] bounds = { 5 };
- int[] src = { 512, 718, 912 };
- Array array = Array.CreateInstance(typeof(int), lengths, bounds);
- 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++)
- 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.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.CreateInstance (typeof (Int32), (int[])null);
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void TestCreateInstance2b ()
- {
- Array.CreateInstance (typeof (Int32), (long[])null);
- }
- [Test]
- public void TestGetEnumerator() {
- String[] s1 = {"this", "is", "a", "test"};
- IEnumerator en = s1.GetEnumerator ();
- 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.IsTrue (en.MoveNext (), "#G11");
- Assert.AreEqual ("this", en.Current, "#G12");
- // mutation does not invalidate array enumerator!
- s1.SetValue ("change", 1);
- 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 ();
- 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.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.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 };
- Array myArray=Array.CreateInstance( typeof(String), myLengthsArray, myBoundsArray );
- for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
- for ( int j = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++ ) {
- int[] myIndicesArray = new int[2] { i, j };
- myArray.SetValue( Convert.ToString(i) + j, myIndicesArray );
- }
- IEnumerator en = myArray.GetEnumerator ();
- Assert.IsNotNull (en, "#AB01");
- // check the first couple of values
- 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());
- 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 };
- Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
- try {
- ((IList)myArray).Add ("can not");
- Assert.Fail ("IList.Add should throw");
- }
- catch (NotSupportedException) {
- return;
- }
- catch (Exception) {
- Assert.Fail ("IList.Add threw wrong exception type");
- }
- 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 };
- Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
- try {
- ((IList)myArray).Insert (0, "can not");
- Assert.Fail ("IList.Insert should throw");
- }
- catch (NotSupportedException) {
- return;
- }
- catch (Exception) {
- Assert.Fail ("IList.Insert threw wrong exception type");
- }
- 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 };
- Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
- try {
- ((IList)myArray).Remove ("can not");
- Assert.Fail ("IList.Remove should throw");
- }
- catch (NotSupportedException) {
- return;
- }
- catch (Exception) {
- Assert.Fail ("IList.Remove threw wrong exception type");
- }
- 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 };
- Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
- try {
- ((IList)myArray).RemoveAt (0);
- Assert.Fail ("IList.RemoveAt should throw");
- }
- catch (NotSupportedException) {
- return;
- }
- catch (Exception) {
- Assert.Fail ("IList.RemoveAt threw wrong exception type");
- }
- 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 };
- Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
- try {
- bool b = ((IList)myArray).Contains ("23");
- 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.IsTrue (((IList)iArr).Contains (1), "AC01");
- Assert.IsTrue (((IList)iArr).Contains (3), "AC02");
- // and one that is definately not there
- Assert.IsTrue (!((IList)iArr).Contains (42), "AC03");
- return;
- }
- 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 };
- Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
- try {
- bool b = ((IList)myArray).Contains ("23");
- 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.AreEqual (0, ((IList)iArr).IndexOf (1), "AD01");
- Assert.AreEqual (2, ((IList)iArr).IndexOf (3), "AD02");
- // and one that is definately not there
- Assert.AreEqual (-1, ((IList)iArr).IndexOf (42), "AD03");
- }
- catch (Exception e) {
- Assert.Fail ("Unexpected exception: " + e.ToString());
- }
- // check that wierd case whem lowerbound is Int32.MinValue,
- // so that IndexOf() needs to return Int32.MaxValue when it cannot find the object
- int[] myLengthArray = new int[1] { 3 };
- int[] myBoundArray = new int[1] { Int32.MinValue };
- Array myExtremeArray=Array.CreateInstance ( typeof(String), myLengthArray, myBoundArray );
- Assert.AreEqual (Int32.MaxValue, ((IList)myExtremeArray).IndexOf (42), "AD04");
- }
- [Test]
- public void TestGetLength() {
- {
- bool errorThrown = false;
- try {
- char[] c1 = {'a', 'b', 'c'};
- c1.GetLength(-1);
- } catch (IndexOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#H01");
- }
- {
- bool errorThrown = false;
- try {
- char[] c1 = {'a', 'b', 'c'};
- c1.GetLength(1);
- } catch (IndexOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#H02");
- }
- char[] c2 = new Char[5];
- Assert.AreEqual (5, c2.GetLength(0), "#H03");
- char[,] c3 = new Char[6,7];
- Assert.AreEqual (6, c3.GetLength(0), "#H04");
- Assert.AreEqual (7, c3.GetLength(1), "#H05");
- }
- [Test]
- public void TestGetLowerBound() {
- {
- bool errorThrown = false;
- try {
- char[] c = {'a', 'b', 'c'};
- c.GetLowerBound(-1);
- } catch (IndexOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#H31");
- }
- {
- bool errorThrown = false;
- try {
- char[] c = {'a', 'b', 'c'};
- c.GetLowerBound(1);
- } catch (IndexOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#H32");
- }
- char[] c1 = new Char[5];
- Assert.AreEqual (0, c1.GetLowerBound(0), "#H33");
- char[,] c2 = new Char[4,4];
- Assert.AreEqual (0, c2.GetLowerBound(0), "#H34");
- Assert.AreEqual (0, c2.GetLowerBound(1), "#H35");
- }
- [Test]
- public void TestGetUpperBound() {
- {
- bool errorThrown = false;
- try {
- char[] c = {'a', 'b', 'c'};
- c.GetUpperBound(-1);
- } catch (IndexOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#H61");
- }
- {
- bool errorThrown = false;
- try {
- char[] c = {'a', 'b', 'c'};
- c.GetUpperBound(1);
- } catch (IndexOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#H62");
- }
- char[] c1 = new Char[5];
- Assert.AreEqual (4, c1.GetUpperBound(0), "#H63");
- char[,] c2 = new Char[4,6];
- Assert.AreEqual (3, c2.GetUpperBound(0), "#H64");
- Assert.AreEqual (5, c2.GetUpperBound(1), "#H65");
- }
- [Test]
- public void TestGetValue1() {
- {
- bool errorThrown = false;
- try {
- char[,] c = new Char[2,2];
- c.GetValue(1);
- } catch (ArgumentException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#I01");
- }
- {
- bool errorThrown = false;
- try {
- char[] c = {'a', 'b', 'c'};
- c.GetValue(-1);
- } catch (IndexOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#I02");
- }
- {
- bool errorThrown = false;
- try {
- char[] c = {'a', 'b', 'c'};
- c.GetValue(4);
- } catch (IndexOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#I03");
- }
- char[] c1 = {'a', 'b', 'c', 'd'};
- for (int i = 0; i < c1.Length; i++) {
- Assert.AreEqual (c1[i], c1.GetValue(i), "#I04(" + i + ")");
- }
- }
- [Test]
- public void TestGetValue2() {
- {
- bool errorThrown = false;
- try {
- char[] c = new Char[2];
- c.GetValue(1,1);
- } catch (ArgumentException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#I21");
- }
- {
- bool errorThrown = false;
- try {
- char[,] c = new Char[2,2];
- c.GetValue(-1, 1);
- } catch (IndexOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#I22");
- }
- {
- bool errorThrown = false;
- try {
- char[,] c = new Char[2,2];
- c.GetValue(4,1);
- } catch (IndexOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#I23");
- }
- char[,] c1 = new Char[4,6];
- for (int i = 0; i < 24; i++) {
- int first = i / 6;
- int second = i % 6;
- c1[first,second] = (char)(((int)'a')+i);
- }
- for (int i = 0; i < c1.GetLength(0); i++) {
- for (int j = 0; j < c1.GetLength(1); j++) {
- Assert.AreEqual (c1[i, j], c1.GetValue(i, j), "#I24(" + i + "," + j + ")");
- }
- }
- }
- [Test]
- public void TestGetValue3() {
- {
- bool errorThrown = false;
- try {
- char[] c = new Char[2];
- c.GetValue(1,1,1);
- } catch (ArgumentException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#I41");
- }
- {
- bool errorThrown = false;
- try {
- char[,,] c = new Char[2,2,2];
- c.GetValue(-1, 1, 1);
- } catch (IndexOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#I42");
- }
- {
- bool errorThrown = false;
- try {
- char[,,] c = new Char[2,2,2];
- c.GetValue(4,1,1);
- } catch (IndexOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#I43");
- }
- char[,,] c1 = new Char[4,2,3];
- for (int i = 0; i < 24; i++) {
- int first = i / 6;
- int remains = i % 6;
- int second = remains / 3;
- int third = remains % 3;
- c1[first,second, third] = (char)(((int)'a')+i);
- }
- 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++) {
- Assert.AreEqual (c1[i, j, k], c1.GetValue(i, j, k), "#I44(" + i + "," + j + ")");
- }
- }
- }
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void TestGetValueLongArray ()
- {
- char[] c = new Char[2];
- c.GetValue((long [])null);
- }
- [Test]
- public void TestGetValueN() {
- {
- bool errorThrown = false;
- try {
- char[] c = new Char[2];
- c.GetValue((int [])null);
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#I61a");
- }
- {
- bool errorThrown = false;
- try {
- char[] c = new Char[2];
- int[] coords = {1, 1};
- c.GetValue(coords);
- } catch (ArgumentException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#I62");
- }
- {
- bool errorThrown = false;
- try {
- char[,] c = new Char[2,2];
- int[] coords = {-1, 1};
- c.GetValue(coords);
- } catch (IndexOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#I63");
- }
- {
- bool errorThrown = false;
- try {
- char[,] c = new Char[2,2];
- int[] coords = {4, 1};
- c.GetValue(coords);
- } catch (IndexOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#I64");
- }
- char[,] c1 = new Char[4,6];
- for (int i = 0; i < 24; i++) {
- int first = i / 6;
- int second = i % 6;
- c1[first,second] = (char)(((int)'a')+i);
- }
- for (int i = 0; i < c1.GetLength(0); i++) {
- for (int j = 0; j < c1.GetLength(1); j++) {
- int[] coords = {i, j};
- Assert.AreEqual (c1[i, j], c1.GetValue(coords), "#I65(" + i + "," + j + ")");
- }
- }
- }
- [Test]
- public void TestIndexOf1() {
- {
- bool errorThrown = false;
- try {
- Array.IndexOf(null, "huh?");
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#J01");
- }
- {
- bool errorThrown = false;
- try {
- char[,] c = new Char[2,2];
- Array.IndexOf(c, "huh?");
- } catch (RankException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#J02");
- }
- String[] s1 = {"this", "is", "a", "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]
- public void TestIndexOf2() {
- {
- bool errorThrown = false;
- try {
- Array.IndexOf(null, "huh?", 0);
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#J21");
- }
- {
- bool errorThrown = false;
- try {
- char[,] c = new Char[2,2];
- Array.IndexOf(c, "huh?", 0);
- } catch (RankException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#J22");
- }
- {
- bool errorThrown = false;
- try {
- char[] c = new Char[2];
- Array.IndexOf(c, "huh?", 3);
- } catch (ArgumentOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#J23");
- }
- String[] s1 = {"this", "is", "really", "a", "test"};
- 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]
- public void TestIndexOf3() {
- {
- bool errorThrown = false;
- try {
- Array.IndexOf(null, "huh?", 0, 1);
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#J41");
- }
- {
- bool errorThrown = false;
- try {
- char[,] c = new Char[2,2];
- Array.IndexOf(c, "huh?", 0, 1);
- } catch (RankException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#J42");
- }
- {
- bool errorThrown = false;
- try {
- char[] c = new Char[2];
- Array.IndexOf(c, "huh?", 3, 1);
- } catch (ArgumentOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#J43");
- }
- {
- bool errorThrown = false;
- try {
- char[] c = new Char[2];
- Array.IndexOf(c, "huh?", 0, 5);
- } catch (ArgumentOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#J44");
- }
- String[] s1 = {"this", "is", "really", "a", "test"};
- 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]
- public void TestLastIndexOf1() {
- {
- bool errorThrown = false;
- try {
- Array.LastIndexOf(null, "huh?");
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#K01");
- }
- {
- bool errorThrown = false;
- try {
- char[,] c = new Char[2,2];
- Array.LastIndexOf(c, "huh?");
- } catch (RankException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#K02");
- }
- String[] s1 = {"this", "is", "a", "a", "test"};
- 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]
- public void TestLastIndexOf2() {
- {
- bool errorThrown = false;
- try {
- Array.LastIndexOf(null, "huh?", 0);
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#K21");
- }
- {
- bool errorThrown = false;
- try {
- char[,] c = new Char[2,2];
- Array.LastIndexOf(c, "huh?", 0);
- } catch (RankException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#K22");
- }
- {
- bool errorThrown = false;
- try {
- char[] c = new Char[2];
- Array.LastIndexOf(c, "huh?", 3);
- } catch (ArgumentOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#K23");
- }
- String[] s1 = {"this", "is", "really", "a", "test"};
- 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]
- public void TestLastIndexOf3() {
- {
- bool errorThrown = false;
- try {
- Array.LastIndexOf(null, "huh?", 0, 1);
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#K41");
- }
- {
- bool errorThrown = false;
- try {
- char[,] c = new Char[2,2];
- Array.LastIndexOf(c, "huh?", 0, 1);
- } catch (RankException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#K42");
- }
- {
- bool errorThrown = false;
- try {
- char[] c = new Char[2];
- Array.LastIndexOf(c, "huh?", 3, 1);
- } catch (ArgumentOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#K43");
- }
- {
- bool errorThrown = false;
- try {
- char[] c = new Char[2];
- Array.LastIndexOf(c, "huh?", 0, 5);
- } catch (ArgumentOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#K44");
- }
- String[] s1 = {"this", "is", "really", "a", "test"};
- 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<short> (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))]
- 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 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() {
- {
- bool errorThrown = false;
- try {
- Array.Reverse(null);
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#L01");
- }
- {
- bool errorThrown = false;
- try {
- char[,] c = new Char[2,2];
- Array.Reverse(c);
- } catch (RankException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#L02");
- }
-
- char[] c1 = {'a', 'b', 'c', 'd'};
- Array.Reverse(c1);
- 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;
- try {
- Array.Reverse(null, 0, 0);
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#L07");
- }
- {
- bool errorThrown = false;
- try {
- char[,] c = new Char[2,2];
- Array.Reverse(c, 0, 0);
- } catch (RankException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#L08");
- }
- //{
- //bool errorThrown = false;
- //try {
- // char[] c = new Char[2];
- // Array.Reverse(c, 0, 3);
- //} catch (ArgumentOutOfRangeException) {
- // errorThrown = true;
- //}
- //Assert.IsTrue (errorThrown, "#L09");
- //}
- //{
- //bool errorThrown = false;
- //try {
- // char[] c = new Char[2];
- // Array.Reverse(c, 3, 0);
- //} catch (ArgumentOutOfRangeException) {
- // errorThrown = true;
- //}
- //Assert.IsTrue (errorThrown, "#L10");
- //}
- char[] c2 = { 'a', 'b', 'c', 'd'};
- Array.Reverse(c2, 1, 2);
- 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]
- public void TestSetValue1() {
- {
- bool errorThrown = false;
- try {
- char[,] c = new Char[2,2];
- c.SetValue("buh", 1);
- } catch (ArgumentException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#M01");
- }
- {
- bool errorThrown = false;
- try {
- char[] c = {'a', 'b', 'c'};
- c.SetValue("buh", -1);
- } catch (IndexOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#M02");
- }
- {
- bool errorThrown = false;
- try {
- char[] c = {'a', 'b', 'c'};
- c.SetValue("buh", 4);
- } catch (IndexOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#M03");
- }
- char[] c1 = {'a', 'b', 'c', 'd'};
- char[] c2 = new char[4];
- for (int i = 0; i < c1.Length; i++) {
- c2.SetValue(c1[i], i);
- }
- for (int i = 0; i < c1.Length; i++) {
- Assert.AreEqual (c1[i], c2[i], "#M04(" + i + ")");
- }
- int[] c3 = { 1, 2, 3 };
- long[] c4 = new long [3];
- for (int i = 0; i < c3.Length; i++)
- c4.SetValue (c3 [i], i);
- try {
- c3.CopyTo (c4, 0);
- } catch (Exception e) {
- Assert.Fail ("c3.CopyTo(): e=" + e);
- }
- for (int i = 0; i < c3.Length; i++)
- Assert.IsTrue (c3[i] == c4[i], "#M05(" + i + ")");
- Object[] c5 = new Object [3];
- long[] c6 = new long [3];
- try {
- c4.CopyTo (c5, 0);
- } catch (Exception e) {
- Assert.Fail ("c4.CopyTo(): e=" + e);
- }
- try {
- c5.CopyTo (c6, 0);
- } catch (Exception e) {
- Assert.Fail ("c5.CopyTo(): e=" + e);
- }
- // for (int i = 0; i < c5.Length; i++)
- // Assert.IsTrue (c5[i] == c6[i], "#M06(" + i + ")");
- }
- [Test]
- public void TestSetValue2() {
- {
- bool errorThrown = false;
- try {
- char[] c = new Char[2];
- c.SetValue("buh", 1,1);
- } catch (ArgumentException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#M21");
- }
- {
- bool errorThrown = false;
- try {
- char[,] c = new Char[2,2];
- c.SetValue("buh", -1, 1);
- } catch (IndexOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#M22");
- }
- {
- bool errorThrown = false;
- try {
- char[,] c = new Char[2,2];
- c.SetValue("buh", 4,1);
- } catch (IndexOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#M23");
- }
- char[,] c1 = new Char[4,6];
- char[,] c2 = new Char[4,6];
- for (int i = 0; i < 24; i++) {
- int first = i / 6;
- int second = i % 6;
- c1[first,second] = (char)(((int)'a')+i);
- c2.SetValue(c1[first,second], first, second);
- }
- for (int i = 0; i < c1.GetLength(0); i++) {
- for (int j = 0; j < c1.GetLength(1); j++) {
- Assert.AreEqual (c1[i, j], c2[i, j], "#M24(" + i + "," + j + ")");
- }
- }
- }
- [Test]
- public void TestSetValue3() {
- {
- bool errorThrown = false;
- try {
- char[] c = new Char[2];
- c.SetValue("buh", 1,1,1);
- } catch (ArgumentException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#M41");
- }
- {
- bool errorThrown = false;
- try {
- char[,,] c = new Char[2,2,2];
- c.SetValue("buh", -1, 1, 1);
- } catch (IndexOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#M42");
- }
- {
- bool errorThrown = false;
- try {
- char[,,] c = new Char[2,2,2];
- c.SetValue("buh", 4,1,1);
- } catch (IndexOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#M43");
- }
- char[,,] c1 = new Char[4,2,3];
- char[,,] c2 = new Char[4,2,3];
- for (int i = 0; i < 24; i++) {
- int first = i / 6;
- int remains = i % 6;
- int second = remains / 3;
- int third = remains % 3;
- c1[first,second, third] = (char)(((int)'a')+i);
- c2.SetValue(c1[first, second, third], first, second, third);
- }
- 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++) {
- Assert.AreEqual (c1[i, j, k], c2[i, j, k], "#M44(" + i + "," + j + " )");
- }
- }
- }
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void TestSetValueLongArray ()
- {
- char[] c = new Char[2];
- c.SetValue("buh", (long [])null);
- }
- [Test]
- public void TestSetValueN() {
- {
- bool errorThrown = false;
- try {
- char[] c = new Char[2];
- c.SetValue("buh", (int [])null);
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#M61a");
- }
- {
- bool errorThrown = false;
- try {
- char[] c = new Char[2];
- int[] coords = {1, 1};
- c.SetValue("buh", coords);
- } catch (ArgumentException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#M62");
- }
- {
- bool errorThrown = false;
- try {
- char[,] c = new Char[2,2];
- int[] coords = {-1, 1};
- c.SetValue("buh", coords);
- } catch (IndexOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#M63");
- }
- {
- bool errorThrown = false;
- try {
- char[,] c = new Char[2,2];
- int[] coords = {4, 1};
- c.SetValue("buh", coords);
- } catch (IndexOutOfRangeException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#M64");
- }
- char[,] c1 = new Char[4,6];
- char[,] c2 = new Char[4,6];
- for (int i = 0; i < 24; i++) {
- int first = i / 6;
- int second = i % 6;
- c1[first,second] = (char)(((int)'a')+i);
- int[] coords = {first, second};
- c2.SetValue(c1[first,second], coords);
- }
- for (int i = 0; i < c1.GetLength(0); i++) {
- for (int j = 0; j < c1.GetLength(1); j++) {
- Assert.AreEqual (c1[i, j], c2[i, j], "#M65(" + i + "," + j + ")");
- }
- }
- }
- [Test]
- public void TestSetValue4() {
- {
- int[] c1 = { 1, 2, 3 };
- long[] c2 = new long [3];
- for (int i = 0; i < c1.Length; i++)
- c2.SetValue (c1 [i], i);
- for (int i = 0; i < c1.Length; i++) {
- Assert.IsTrue (c1[i] == c2[i], "#M81(" + i + ")");
- Assert.AreEqual (typeof (long), c2[i].GetType (), "#M82(" + i + ")");
- }
- }
- {
- long[] c1 = { 1, 2, 3 };
- int[] c2 = new int [3];
- bool errorThrown = false;
- try {
- c2.SetValue (c1 [0], 0);
- } catch (ArgumentException) {
- errorThrown = true;
- }
- Assert.IsTrue (errorThrown, "#M83");
- }
- {
- int[] c1 = { 1, 2, 3 };
- Object[] c2 = new Object [3];
- for (int i = 0; i < c1.Length; i++)
- c2.SetValue (c1 [i], i);
- for (int i = 0; i < c1.Length; i++)
- Assert.AreEqual (c1[i], Convert.ToInt32 (c2[i]), "#M84(" + i + ")");
- }
- {
- Object[] c1 = new Object [3];
- Object[] c2 = new Object [3];
- c1[0] = new Object ();
- for (int i = 0; i < c1.Length; i++)
- c2.SetValue (c1 [i], i);
- for (int i = 0; i < c1.Length; i++)
- Assert.AreEqual (c1[i], c2[i], "#M85(" + i + ")");
- }
- {
- Object[] c1 = new Object [3];
- string[] c2 = new String [3];
- string test = "hello";
- c1[0] = test;
- c2.SetValue (c1 [0], 0);
- …
Large files files are truncated, but you can click here to view the full file