diff options
author | Hugh Bellamy <hughbellars@gmail.com> | 2016-01-30 01:49:48 +0300 |
---|---|---|
committer | Hugh Bellamy <hughbellars@gmail.com> | 2016-02-13 17:49:22 +0300 |
commit | ea877feadbec1b295ae9c67b7ee67e0f8422bae0 (patch) | |
tree | ab038b2f650f6edcbdee690f2359dc32e023048d /src/System.Collections.NonGeneric/tests/ArrayList | |
parent | f6293e53242425c5c0c09ada35fb21e3d245c282 (diff) |
Cleanup ArrayList tests
Diffstat (limited to 'src/System.Collections.NonGeneric/tests/ArrayList')
37 files changed, 4257 insertions, 7972 deletions
diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/AdapterTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/AdapterTests.cs deleted file mode 100644 index 8ea1f0d010..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/AdapterTests.cs +++ /dev/null @@ -1,162 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_AdapterTests - { - [Fact] - public void TestNullIListParameter() - { - ArrayList lAdapter = null; - - //[] check for ArgumentNullException when argument null - Assert.Throws<ArgumentNullException>(() => { lAdapter = ArrayList.Adapter(null); }); - } - - [Fact] - public void TestPopulateChangesToList() - { - const string fromBefore = " from before"; - - //[] make sure changes Through listAdapter show up in list - // populate the list - ArrayList tempList = CreateArrayList(count: 10, optStr: fromBefore); - // wrap the list - ArrayList lAdapter = ArrayList.Adapter(tempList); - // make changes through listAdapter and make sure they are reflected in tempList - lAdapter.Reverse(0, lAdapter.Count); - - int j = 9; - for (int i = 0; i < lAdapter.Count; i++) - { - Assert.Equal(j.ToString() + fromBefore, lAdapter[i]); - j--; - } - } - - [Fact] - public void TestClearList() - { - //[] make sure changes Through list show up in listAdapter - // populate the list - ArrayList tempList = CreateArrayList(count: 10, optStr: " from before"); - - // wrap the list - ArrayList lAdapter = ArrayList.Adapter(tempList); - - // make changes through listAdapter and make sure they are reflected in tempList - tempList.Clear(); - - Assert.Equal(0, lAdapter.Count); - } - - [Fact] - public void TestEnumerators() - { - //[] test to see if enumerators are correctly enumerate through elements - // populate the list - ArrayList tempList = CreateArrayList(10); - IEnumerator ienumList = tempList.GetEnumerator(); - - // wrap the list - ArrayList lAdapter = ArrayList.Adapter(tempList); - IEnumerator ienumWrap = tempList.GetEnumerator(); - - int j = 0; - while (ienumList.MoveNext()) - { - Assert.True(ienumList.Current.Equals(j.ToString()), "Error, enumerator on list expected to return " + j + " but returned " + ienumList.Current); - j++; - } - - j = 0; - while (ienumWrap.MoveNext()) - { - Assert.True(ienumWrap.Current.Equals(j.ToString()), "Error, enumerator on listadapter expected to return " + j + " but returned " + ienumWrap.Current); - j++; - } - } - - [Fact] - public void TestEnumeratorsModifiedList() - { - //[] test to see if enumerators are correctly getting invalidated with list modified through list - // populate the list - ArrayList tempList = CreateArrayList(10); - IEnumerator ienumList = tempList.GetEnumerator(); - - // wrap the list - ArrayList lAdapter = ArrayList.Adapter(tempList); - IEnumerator ienumWrap = tempList.GetEnumerator(); - - // start enumeration - ienumList.MoveNext(); - ienumWrap.MoveNext(); - - // now modify list through tempList - tempList.Add("Hey this is new element"); - - // make sure accessing ienumList will throw - Assert.Throws<InvalidOperationException>(() => ienumList.MoveNext()); - - // make sure accessing ienumWrap will throw - Assert.Throws<InvalidOperationException>(() => ienumWrap.MoveNext()); - } - - [Fact] - public void TestEnumeratorsModifiedAdapter() - { - //[] test to see if enumerators are correctly getting invalidated with list modified through listAdapter - - // populate the list - ArrayList tempList = CreateArrayList(10); - IEnumerator ienumList = tempList.GetEnumerator(); - - // wrap the list - ArrayList lAdapter = ArrayList.Adapter(tempList); - IEnumerator ienumWrap = tempList.GetEnumerator(); - - // start enumeration - ienumList.MoveNext(); - ienumWrap.MoveNext(); - - // now modify list through adapter - lAdapter.Add("Hey this is new element"); - - // make sure accessing ienumList will throw - Assert.Throws<InvalidOperationException>(() => ienumList.MoveNext()); - // make sure accessing ienumWrap will throw - Assert.Throws<InvalidOperationException>(() => ienumWrap.MoveNext()); - } - - [Fact] - public void TestInsertRange() - { - //[] to see if listadaptor modified using InsertRange works - // populate the list - ArrayList tempList = CreateArrayList(10); - ArrayList lAdapter = ArrayList.Adapter(tempList); - - // now add a few more elements using insertrange - ArrayList tempListSecond = CreateArrayList(10, 10); - lAdapter.InsertRange(lAdapter.Count, tempListSecond); - - Assert.Equal(20, lAdapter.Count); - } - - private static ArrayList CreateArrayList(int count, int start = 0, string optStr = null) - { - ArrayList arrayList = new ArrayList(); - for (int i = start; i < start + count; i++) - { - arrayList.Add(i.ToString() + optStr); - } - - return arrayList; - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/AddRangeTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/AddRangeTests.cs deleted file mode 100644 index d680a000cb..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/AddRangeTests.cs +++ /dev/null @@ -1,144 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_AddRangeTests - { - [Fact] - public void TestAddRangeBasic() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList1 = null; - ArrayList arrList2 = null; - ArrayList olst1; - Queue que; - - string[] strHeroes = - { - "Batman", - "Superman", - "SpiderMan", - "Wonder Woman", - "Green Lantern", - "Flash", - "Steel" - }; - - string[] strIdentities = - { - "Bruce Wayne", - "Clark Kent", - "Peter Parker", - "Diana Prince", - "Kyle Rayner", - "Wally West", - "John Henry Irons" - }; - - // - // Construct array lists. - // - arrList1 = new ArrayList(); - arrList2 = new ArrayList(); - - Assert.NotNull(arrList1); - Assert.NotNull(arrList2); - - // Add items to the lists. - for (int ii = 0; ii < strHeroes.Length; ++ii) - { - arrList1.Add(strHeroes[ii]); - arrList2.Add(strIdentities[ii]); - } - - // Verify items added to list. - Assert.Equal(strHeroes.Length, arrList1.Count); - Assert.Equal(strIdentities.Length, arrList2.Count); - - // - // [] Append the second list to the first list. - // - // Append the list. - arrList1.AddRange(arrList2); - // Verify the size. - Assert.Equal(strHeroes.Length + strIdentities.Length, arrList1.Count); - - // - // [] Attempt invalid AddRange - null - // - // Append the list. - Assert.Throws<ArgumentNullException>(() => arrList1.AddRange(null)); - - // [] Different ICollection types - arrList1 = new ArrayList(); - for (int i = 0; i < 10; i++) - arrList1.Add(i); - - olst1 = new ArrayList(); - for (int i = 0; i < 10; i++) - olst1.Add(i + 10); - - arrList1.AddRange(olst1); - - for (int i = 0; i < arrList1.Count; i++) - { - Assert.Equal(i, (int)arrList1[i]); - } - - que = new Queue(); - for (int i = 0; i < 10; i++) - que.Enqueue(i + 20); - arrList1.AddRange(que); - - for (int i = 0; i < arrList1.Count; i++) - { - Assert.Equal(i, (int)arrList1[i]); - } - - //[] we will copy the arraylist to itself - arrList1.AddRange(arrList1); - for (int i = 0; i < arrList1.Count / 2; i++) - { - Assert.Equal(i, (int)arrList1[i]); - } - - for (int i = arrList1.Count / 2; i < arrList1.Count; i++) - { - Assert.Equal((i - arrList1.Count / 2), (int)arrList1[i]); - } - - //[] ICollection has different type objects to the existing ArrayList - arrList1 = new ArrayList(); - for (int i = 0; i < 10; i++) - arrList1.Add(i); - - que = new Queue(); - for (int i = 10; i < 20; i++) - que.Enqueue("String_" + i); - arrList1.AddRange(que); - - for (int i = 0; i < 10; i++) - { - Assert.Equal(i, (int)arrList1[i]); - } - - for (int i = 10; i < 20; i++) - { - Assert.Equal("String_" + i, (string)arrList1[i]); - } - - //[]Team review feedback - Add an empty ICollection - arrList1 = new ArrayList(); - que = new Queue(); - arrList1.AddRange(que); - - Assert.Equal(0, arrList1.Count); - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/AddTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/AddTests.cs deleted file mode 100644 index ee5096619b..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/AddTests.cs +++ /dev/null @@ -1,70 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Text; -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_AddTests - { - [Fact] - public void TestAddAndRemove() - { - StringBuilder sbl3 = new StringBuilder(99); - StringBuilder sbl4 = new StringBuilder(99); - - int[] in4a = new int[9]; - - // Construct, and verify small capacity - ArrayList al2 = new ArrayList(1); - in4a[0] = al2.Capacity; - Assert.Equal(1, in4a[0]); - - // Add the first obj - sbl3.Length = 0; - sbl3.Append("hi mom"); - - al2.Add(sbl3); - sbl4 = (StringBuilder)al2[0]; - - Assert.Equal(sbl4.ToString(), sbl3.ToString()); - - // Add another obj, verify that Add auto increases Capacity when needed. - sbl3.Length = 0; - sbl3.Append("low dad"); - - al2.Add(sbl3); - in4a[1] = al2.Capacity; - Assert.True(in4a[1] > 1); - Assert.True(in4a[1] > 1); - - sbl3 = (StringBuilder)al2[1]; - Assert.Equal(sbl4.ToString(), sbl3.ToString()); - - // - int p_inLoops0 = 2; - int p_inLoops1 = 2; - - al2 = new ArrayList(); - - for (int aa = 0; aa < p_inLoops0; aa++) - { - al2.Capacity = 1; - - for (int bb = 0; bb < p_inLoops1; bb++) - { - al2.Add("aa==" + aa + " ,bb==" + bb); - } - - while (al2.Count > 0) - { - al2.RemoveAt(0); - } - } - - Assert.Equal(0, al2.Count); - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/ArrayListTests.TestData.cs b/src/System.Collections.NonGeneric/tests/ArrayList/ArrayListTests.TestData.cs new file mode 100644 index 0000000000..7447615541 --- /dev/null +++ b/src/System.Collections.NonGeneric/tests/ArrayList/ArrayListTests.TestData.cs @@ -0,0 +1,766 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Collections.Tests +{ + public static partial class ArrayListTests + { + private static readonly string[] basicTestData = new string[] + { + "Aquaman", + "Atom", + "Batman", + "Black Canary", + "Captain America", + "Captain Atom", + "Catwoman", + "Cyborg", + "Flash", + "Green Arrow", + "Green Lantern", + "Hawkman", + "Huntress", + "Ironman", + "Nightwing", + "Robin", + "SpiderMan", + "Steel", + "Superman", + "Thor", + "Wildcat", + "Wonder Woman" + }; + + private static readonly string[] nullContainingTestData = new string[] + { + "Aquaman", + "Atom", + "Batman", + "Black Canary", + "Captain America", + "Captain Atom", + "Catwoman", + "Cyborg", + "Flash", + "Green Arrow", + "Green Lantern", + "Hawkman", + null, + "Ironman", + "Nightwing", + "Robin", + "SpiderMan", + "Steel", + null, + "Thor", + "Wildcat", + null + }; + + private static readonly string[] binarySearchFindTestData = new string[] + { + "Batman", + "Superman", + "SpiderMan", + "Wonder Woman", + "Green Lantern", + "Flash", + "Steel" + }; + + private static readonly string[] setRangeTestData = new string[] + { + "Hardware", + "Icon", + "Johnny Quest", + "Captain Sisko", + "Captain Picard", + "Captain Kirk", + "Agent J", + "Agent K", + "Space Ghost", + "Wolverine", + "Cyclops", + "Storm", + "Lone Ranger", + "Tonto", + "Supergirl" + }; + + public static string[] indexOfUniqueTestData = new string[] + { + "Aquaman", + "Atom", + "Batman", + "Black Canary", + "Captain America", + "Captain Atom", + "Catwoman", + "Cyborg", + "Flash", + "Green Arrow", + "Green Lantern", + "Hawkman", + "Daniel Takacs", + "Ironman", + "Nightwing", + "Robin", + "SpiderMan", + "Steel", + "Gene", + "Thor", + "Wildcat", + null + }; + + + private static readonly string[] duplicateContainingTestData = new string[] + { + "Aquaman", + "Atom", + "Batman", + "Black Canary", + "Captain America", + "Captain Atom", + "Batman", + "Catwoman", + "Cyborg", + "Flash", + "Green Arrow", + "Batman", + "Green Lantern", + "Hawkman", + "Huntress", + "Ironman", + "Nightwing", + "Batman", + "Robin", + "SpiderMan", + "Steel", + "Superman", + "Thor", + "Batman", + "Wildcat", + "Wonder Woman", + "Batman" + }; + + private static readonly string[] duplicateAndNullContainingTestData = new string[] + { + "Aquaman", + "Atom", + "Batman", + "Black Canary", + "Captain America", + "Captain Atom", + "Batman", + "Catwoman", + "Cyborg", + "Flash", + "Green Arrow", + "Batman", + "Green Lantern", + "Hawkman", + "Huntress", + "Ironman", + "Nightwing", + "Batman", + "Robin", + "SpiderMan", + "Steel", + "Superman", + "Thor", + "Batman", + "Wildcat", + "Wonder Woman", + "Batman", + null + }; + + private static readonly string[] insertRangeRangeToInsertTestData = new string[] + { + "Dr. Fate", + "Dr. Light", + "Dr. Manhattan", + "Hardware", + "Hawkeye", + "Icon", + "Spawn", + "Spectre", + "Supergirl" + }; + + private static readonly string[] insertRangeExpectedTestData = new string[] + { + "Aquaman", + "Atom", + "Batman", + "Dr. Fate", + "Dr. Light", + "Dr. Manhattan", + "Hardware", + "Hawkeye", + "Icon", + "Spawn", + "Spectre", + "Supergirl", + "Black Canary", + "Captain America", + "Captain Atom", + "Catwoman", + "Cyborg", + "Flash", + "Green Arrow", + "Green Lantern", + "Hawkman", + "Huntress", + "Ironman", + "Nightwing", + "Robin", + "SpiderMan", + "Steel", + "Superman", + "Thor", + "Wildcat", + "Wonder Woman" + }; + + private static readonly string[] insertObjectsToInsertTestData = new string[] + { + "Dr. Fate", + "Dr. Light", + "Dr. Manhattan", + "Hardware", + "Hawkeye", + "Icon", + "Spawn", + "Spectre", + "Supergirl" + }; + + private static readonly string[] insertExpectedTestData = new string[] + { + "Aquaman", + "Atom", + "Batman", + "Dr. Fate", + "Dr. Light", + "Dr. Manhattan", + "Hardware", + "Hawkeye", + "Icon", + "Spawn", + "Spectre", + "Supergirl", + "Black Canary", + "Captain America", + "Captain Atom", + "Catwoman", + "Cyborg", + "Flash", + "Green Arrow", + "Green Lantern", + "Hawkman", + "Huntress", + "Ironman", + "Nightwing", + "Robin", + "SpiderMan", + "Steel", + "Superman", + "Thor", + "Wildcat", + "Wonder Woman" + }; + + private static readonly string[] removeAtExpectedTestData = new string[] + { + "Aquaman", + "Atom", + "Batman", + "Superman", + "Thor", + "Wildcat", + "Wonder Woman" + }; + + private static readonly string[] removeRangeExpectedTestData = new string[] + { + "Aquaman", + "Atom", + "Batman", + "Superman", + "Thor", + "Wildcat", + "Wonder Woman" + }; + + private static readonly string[] reverseExpectedTestData = new string[] + { + "Aquaman", + "Atom", + "Batman", + "Black Canary", + "Captain America", + "Flash", + "Cyborg", + "Catwoman", + "Captain Atom", + "Green Arrow", + "Green Lantern", + "Hawkman", + "Huntress", + "Ironman", + "Nightwing", + "Robin", + "SpiderMan", + "Steel", + "Superman", + "Thor", + "Wildcat", + "Wonder Woman" + }; + + private static readonly string[] sortTestData = new string[] + { + "Green Arrow", + "Atom", + "Batman", + "Steel", + "Superman", + "Wonder Woman", + "Hawkman", + "Flash", + "Aquaman", + "Green Lantern", + "Catwoman", + "Huntress", + "Robin", + "Captain Atom", + "Wildcat", + "Nightwing", + "Ironman", + "SpiderMan", + "Black Canary", + "Thor", + "Cyborg", + "Captain America" + }; + + private static readonly string[] sortDescendingExpectedTestData = new string[] + { + "Wonder Woman", + "Wildcat", + "Thor", + "Superman", + "Steel", + "SpiderMan", + "Robin", + "Nightwing", + "Ironman", + "Huntress", + "Hawkman", + "Green Lantern", + "Green Arrow", + "Flash", + "Cyborg", + "Catwoman", + "Captain Atom", + "Captain America", + "Black Canary", + "Batman", + "Atom", + "Aquaman" + }; + + private static readonly string[] sortAscendingExpectedTestData = new string[] + { + "Aquaman", + "Atom", + "Batman", + "Black Canary", + "Captain America", + "Captain Atom", + "Catwoman", + "Cyborg", + "Flash", + "Green Arrow", + "Green Lantern", + "Hawkman", + "Huntress", + "Ironman", + "Nightwing", + "Robin", + "SpiderMan", + "Steel", + "Superman", + "Thor", + "Wildcat", + "Wonder Woman" + }; + + private static readonly string[] sortRangeAscendingExpectedTestData = new string[] + { + "Green Arrow", + "Atom", + "Batman", + "Flash", + "Hawkman", + "Steel", + "Superman", + "Wonder Woman", + "Aquaman", + "Green Lantern", + "Catwoman", + "Huntress", + "Robin", + "Captain Atom", + "Wildcat", + "Nightwing", + "Ironman", + "SpiderMan", + "Black Canary", + "Thor", + "Cyborg", + "Captain America" + }; + + public static string[] synchronizedTestData = new string[] + { + "Aquaman", + "Atom", + "Batman", + "Black Canary", + "Aquaman", + "Atom", + "Batman", + "Black Canary", + "Captain America", + "Captain Atom", + "Catwoman", + "Cyborg", + null, + "Thor", + "Wildcat", + null, + "Aquaman", + "Atom", + "Batman", + "Black Canary", + "Captain America", + "Captain Atom", + "Catwoman", + "Cyborg", + "Flash", + "Green Arrow", + "Green Lantern", + "Hawkman", + null, + "Ironman", + "Nightwing", + "Robin", + "SpiderMan", + "Steel", + null, + "Thor", + "Wildcat", + null, + "Aquaman", + "Atom", + "Batman", + "Black Canary", + "Captain America", + "Captain Atom", + "Catwoman", + "Cyborg", + "Flash", + "Green Arrow", + "Green Lantern", + "Hawkman", + null, + "Ironman", + "Nightwing", + "Robin", + "SpiderMan", + "Steel", + null, + "Thor", + "Wildcat", + null, + "Aquaman", + "Atom", + "Batman", + "Black Canary", + "Captain America", + "Captain Atom", + "Catwoman", + "Cyborg", + "Flash", + "Green Arrow", + "Green Lantern", + "Hawkman", + null, + "Ironman", + "Nightwing", + "Robin", + "SpiderMan", + "Steel", + null, + "Thor", + "Wildcat", + null, + "Aquaman", + "Atom", + "Batman", + "Black Canary", + "Captain America", + "Captain Atom", + "Catwoman", + "Cyborg", + "Flash", + "Green Arrow", + "Green Lantern", + "Hawkman", + null, + "Ironman", + "Nightwing", + "Robin", + "SpiderMan", + "Steel", + null, + "Thor", + "Wildcat", + null, + "Aquaman", + "Atom", + "Batman", + "Black Canary", + "Captain America", + "Captain Atom", + "Catwoman", + "Cyborg", + "Flash", + "Green Arrow", + "Green Lantern", + "Hawkman", + null, + "Ironman", + "Nightwing", + "Robin", + "SpiderMan", + "Steel", + null, + "Thor", + "Wildcat", + null, + "Aquaman", + "Atom", + "Batman", + "Black Canary", + "Captain America", + "Captain Atom", + "Catwoman", + "Cyborg", + "Flash", + "Green Arrow", + "Green Lantern", + "Hawkman", + null, + "Ironman", + "Nightwing", + "Robin", + "SpiderMan", + "Steel", + null, + "Thor", + "Wildcat", + null, + "Aquaman", + "Atom", + "Batman", + "Black Canary", + "Captain America", + "Captain Atom", + "Catwoman", + "Cyborg", + "Flash", + "Green Arrow", + "Green Lantern", + "Hawkman", + null, + "Ironman", + "Nightwing", + "Robin", + "SpiderMan", + "Steel", + null, + "Thor", + "Wildcat", + null, + "Aquaman", + "Atom", + "Batman", + "Black Canary", + "Captain America", + "Captain Atom", + "Catwoman", + "Cyborg", + "Flash", + "Green Arrow", + "Green Lantern", + "Hawkman", + null, + "Ironman", + "Nightwing", + "Robin", + "SpiderMan", + "Steel", + null, + "Thor", + "Wildcat", + null, + "Aquaman", + "Atom", + "Batman", + "Black Canary", + "Captain America" + }; + + private class BinarySearchComparer : IComparer + { + public virtual int Compare(object x, object y) + { + if (x is string) + { + return ((string)x).CompareTo((string)y); + } + + var comparer = new Comparer(Globalization.CultureInfo.InvariantCulture); + if (x is int || y is string) + { + return comparer.Compare(x, y); + } + + return -1; + } + } + + private class CompareWithNullEnabled : IComparer + { + public int Compare(object a, object b) + { + if (a == b) return 0; + if (a == null) return -1; + if (b == null) return 1; + + IComparable ia = a as IComparable; + if (ia != null) + return ia.CompareTo(b); + + IComparable ib = b as IComparable; + if (ib != null) + return -ib.CompareTo(a); + + throw new ArgumentException("Wrong stuff"); + } + } + + private class Foo + { + private string _stringValue = "Hello World"; + public string StringValue + { + get { return _stringValue; } + set { _stringValue = value; } + } + } + + private class MyCollection : ICollection + { + private ICollection _collection; + private Array _array; + private int _startIndex; + + public MyCollection(ICollection collection) + { + _collection = collection; + } + + public Array Array + { + get + { + return _array; + } + } + + public int StartIndex + { + get + { + return _startIndex; + } + } + + public int Count + { + get + { + return _collection.Count; + } + } + + public object SyncRoot + { + get + { + return _collection.SyncRoot; + } + } + + public bool IsSynchronized + { + get + { + return _collection.IsSynchronized; + } + } + + public void CopyTo(Array array, int startIndex) + { + _array = array; + _startIndex = startIndex; + _collection.CopyTo(array, startIndex); + } + + public IEnumerator GetEnumerator() + { + throw new NotSupportedException(); + } + } + + private class AscendingComparer : IComparer + { + public virtual int Compare(object x, object y) + { + return ((string)x).CompareTo((string)y); + } + } + + private class DescendingComparer : IComparer + { + public virtual int Compare(object x, object y) + { + return -((string)x).CompareTo((string)y); + } + } + + private class DerivedArrayList: ArrayList + { + public DerivedArrayList(ICollection c) : base(c) + { + } + } + } +} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/ArrayListTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/ArrayListTests.cs new file mode 100644 index 0000000000..60b2e50ee7 --- /dev/null +++ b/src/System.Collections.NonGeneric/tests/ArrayList/ArrayListTests.cs @@ -0,0 +1,3491 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using System.Diagnostics; +using System.Reflection; +using System.Threading.Tasks; + +using Xunit; + +namespace System.Collections.Tests +{ + public static partial class ArrayListTests + { + [Fact] + public static void TestCtor_Empty() + { + var arrList = new ArrayList(); + Assert.Equal(0, arrList.Count); + Assert.Equal(0, arrList.Capacity); + + Assert.False(arrList.IsFixedSize); + Assert.False(arrList.IsReadOnly); + Assert.False(arrList.IsSynchronized); + } + + [Fact] + public static void TestCtor_Capacity() + { + var arrList = new ArrayList(16); + Assert.Equal(16, arrList.Capacity); + + Assert.False(arrList.IsFixedSize); + Assert.False(arrList.IsReadOnly); + Assert.False(arrList.IsSynchronized); + } + + [Fact] + public static void TestCtor_Capacity_Invalid() + { + Assert.Throws<ArgumentOutOfRangeException>(() => new ArrayList(-1)); // Capacity < 0 + } + + [Fact] + public static void TestCtor_ICollection() + { + ArrayList sourceList = Helpers.CreateIntArrayList(100); + var arrList = new ArrayList(sourceList); + + Assert.Equal(100, arrList.Count); + + Assert.False(arrList.IsFixedSize); + Assert.False(arrList.IsReadOnly); + Assert.False(arrList.IsSynchronized); + } + + [Fact] + public static void TestCtor_ICollection_Empty() + { + ICollection arrListCollection = new ArrayList(); + ArrayList arrList = new ArrayList(arrListCollection); + + Assert.Equal(0, arrList.Count); + + Assert.False(arrList.IsFixedSize); + Assert.False(arrList.IsReadOnly); + Assert.False(arrList.IsSynchronized); + } + + [Fact] + public static void TestCtor_ICollection_Invalid() + { + Assert.Throws<ArgumentNullException>(() => new ArrayList(null)); // Collection is null + } + + [Fact] + public static void TestDebuggerAttribute() + { + DebuggerAttributes.ValidateDebuggerDisplayReferences(new ArrayList()); + DebuggerAttributes.ValidateDebuggerTypeProxyProperties(new ArrayList() { "a", 1, "b", 2 }); + + bool threwNull = false; + try + { + DebuggerAttributes.ValidateDebuggerTypeProxyProperties(typeof(ArrayList), null); + } + catch (TargetInvocationException ex) + { + ArgumentNullException nullException = ex.InnerException as ArgumentNullException; + threwNull = nullException != null; + } + + Assert.True(threwNull); + } + + [Fact] + public static void TestAdapter_ArrayList() + { + ArrayList arrList = ArrayList.Adapter(new ArrayList()); + Assert.False(arrList.IsFixedSize); + Assert.False(arrList.IsReadOnly); + Assert.False(arrList.IsSynchronized); + } + + [Fact] + public static void TestAdapter_FixedSizeArrayList() + { + ArrayList arrList = ArrayList.Adapter(ArrayList.FixedSize(new ArrayList())); + Assert.True(arrList.IsFixedSize); + Assert.False(arrList.IsReadOnly); + Assert.False(arrList.IsSynchronized); + } + + [Fact] + public static void TestAdapter_ReadOnlyArrayList() + { + ArrayList arrList = ArrayList.Adapter(ArrayList.ReadOnly(new ArrayList())); + Assert.True(arrList.IsFixedSize); + Assert.True(arrList.IsReadOnly); + Assert.False(arrList.IsSynchronized); + } + + [Fact] + public static void TestAdapter_SynchronizedArrayList() + { + ArrayList arrList = ArrayList.Adapter(ArrayList.Synchronized(new ArrayList())); + Assert.False(arrList.IsFixedSize); + Assert.False(arrList.IsReadOnly); + Assert.True(arrList.IsSynchronized); + } + + [Fact] + public static void TestAdapter_PopulateChangesToList() + { + const string fromBefore = " from before"; + + // Make sure changes through listAdapter show up in list + // Populate the list + ArrayList arrList = Helpers.CreateStringArrayList(count: 10, optionalString: fromBefore); + // Wrap the list + ArrayList adapter = ArrayList.Adapter(arrList); + // Make changes through listAdapter and make sure they are reflected in arrList + adapter.Reverse(0, adapter.Count); + + int j = 9; + for (int i = 0; i < adapter.Count; i++) + { + Assert.Equal(j.ToString() + fromBefore, adapter[i]); + j--; + } + } + + [Fact] + public static void TestAdapter_ClearList() + { + // Make sure changes through list show up in listAdapter + // Populate the list + ArrayList arrList = Helpers.CreateIntArrayList(100); + + // Wrap the list + ArrayList adapter = ArrayList.Adapter(arrList); + + // Make changes through listAdapter and make sure they are reflected in arrList + arrList.Clear(); + Assert.Equal(0, adapter.Count); + } + + [Fact] + public static void TestAdapter_Enumerators() + { + // Test to see if enumerators are correctly enumerate through elements + // Populate the list + ArrayList arrList = Helpers.CreateIntArrayList(10); + IEnumerator ienumList = arrList.GetEnumerator(); + + // Wrap the list + ArrayList adapter = ArrayList.Adapter(arrList); + IEnumerator ienumWrap = arrList.GetEnumerator(); + + int j = 0; + while (ienumList.MoveNext()) + { + Assert.Equal(j, ienumList.Current); + j++; + } + + j = 0; + while (ienumWrap.MoveNext()) + { + Assert.Equal(j, ienumWrap.Current); + j++; + } + } + + [Fact] + public static void TestAdapter_EnumeratorsModifiedList() + { + // Test to see if enumerators are correctly getting invalidated with list modified through list + // Populate the list + ArrayList arrList = Helpers.CreateIntArrayList(10); + IEnumerator ienumList = arrList.GetEnumerator(); + + // Wrap the list + ArrayList adapter = ArrayList.Adapter(arrList); + IEnumerator ienumWrap = arrList.GetEnumerator(); + + // Start enumeration + ienumList.MoveNext(); + ienumWrap.MoveNext(); + + // Now modify list through arrList + arrList.Add(100); + + // Make sure accessing ienumList and ienumWrap will throw + Assert.Throws<InvalidOperationException>(() => ienumList.MoveNext()); + Assert.Throws<InvalidOperationException>(() => ienumWrap.MoveNext()); + } + + [Fact] + public static void TestAdapter_EnumeratorsModifiedAdapter() + { + // Test to see if enumerators are correctly getting invalidated with list modified through listAdapter + // Populate the list + ArrayList arrList = Helpers.CreateStringArrayList(10); + IEnumerator ienumList = arrList.GetEnumerator(); + + // Wrap the list + ArrayList adapter = ArrayList.Adapter(arrList); + IEnumerator ienumWrap = arrList.GetEnumerator(); + + // Start enumeration + ienumList.MoveNext(); + ienumWrap.MoveNext(); + + // Now modify list through adapter + adapter.Add("Hey this is new element"); + + // Make sure accessing ienumList and ienumWrap will throw + Assert.Throws<InvalidOperationException>(() => ienumList.MoveNext()); + Assert.Throws<InvalidOperationException>(() => ienumWrap.MoveNext()); + } + + [Fact] + public static void TestAdapter_InsertRange() + { + // Test too see if listAdapator modified using InsertRange works + // Populate the list + ArrayList arrList = Helpers.CreateIntArrayList(10); + ArrayList adapter = ArrayList.Adapter(arrList); + + // Now add a few more elements using InsertRange + ArrayList arrListSecond = Helpers.CreateIntArrayList(10); + adapter.InsertRange(adapter.Count, arrListSecond); + + Assert.Equal(20, adapter.Count); + } + + [Fact] + public static void TestAdapter_Capacity_Set() + { + ArrayList arrList = Helpers.CreateIntArrayList(10); + ArrayList adapter = ArrayList.Adapter(arrList); + + adapter.Capacity = 10; + Assert.Equal(10, adapter.Capacity); + } + + [Fact] + public static void TestAdapter_Invalid() + { + Assert.Throws<ArgumentNullException>(() => ArrayList.Adapter(null)); // List is null + } + + [Fact] + public static void TestAddRange_Basic() + { + ArrayList arrList1 = Helpers.CreateIntArrayList(10); + ArrayList arrList2 = Helpers.CreateIntArrayList(20, 10); + + VerifyAddRange(arrList1, arrList2); + } + + [Fact] + public static void TestAddRange_DifferentCollection() + { + ArrayList arrList = Helpers.CreateIntArrayList(10); + Queue queue = new Queue(); + for (int i = 10; i < 20; i++) + { + queue.Enqueue(i); + } + VerifyAddRange(arrList, queue); + } + + [Fact] + public static void TestAddRange_Self() + { + ArrayList arrList1 = Helpers.CreateIntArrayList(10); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsFixedSize) + { + return; + } + + arrList2.AddRange(arrList2); + for (int i = 0; i < arrList2.Count / 2; i++) + { + Assert.Equal(i, arrList2[i]); + } + + for (int i = arrList2.Count / 2; i < arrList2.Count; i++) + { + Assert.Equal(i - arrList2.Count / 2, arrList2[i]); + } + }); + } + + private static void VerifyAddRange(ArrayList arrList1, ICollection c) + { + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsFixedSize) + { + return; + } + + int expectedCount = arrList2.Count + c.Count; + arrList2.AddRange(c); + + Assert.Equal(expectedCount, arrList2.Count); + for (int i = 0; i < arrList2.Count; i++) + { + Assert.Equal(i, arrList2[i]); + } + // Assumes that the array list and collection contain integer types + // and the first item in the collection is the count of the array list + for (int i = arrList2.Count; i < c.Count; i++) + { + Assert.Equal(i, arrList2[i]); + } + }); + } + + [Fact] + public static void TestAddRange_DifferentObjectTypes() + { + // Add an ICollection with different type objects + ArrayList arrList1 = Helpers.CreateIntArrayList(10); // Array list contains only integers currently + var queue = new Queue(); // Queue contains strings + for (int i = 10; i < 20; i++) + queue.Enqueue("String_" + i); + + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsFixedSize) + { + return; + } + + arrList2.AddRange(queue); + + for (int i = 0; i < 10; i++) + { + Assert.Equal(i, arrList2[i]); + } + + for (int i = 10; i < 20; i++) + { + Assert.Equal("String_" + i, arrList2[i]); + } + }); + } + + [Fact] + public static void TestAddRange_EmptyCollection() + { + var emptyCollection = new Queue(); + ArrayList arrList1 = Helpers.CreateIntArrayList(100); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsFixedSize) + { + return; + } + + arrList2.AddRange(emptyCollection); + Assert.Equal(100, arrList2.Count); + }); + } + + [Fact] + public static void TestAddRange_Invalid() + { + var arrList1 = new ArrayList(); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsFixedSize) + { + return; + } + + Assert.Throws<ArgumentNullException>(() => arrList2.AddRange(null)); // Collection is null + }); + } + + [Theory] + [InlineData(1)] + [InlineData(10)] + [InlineData(100)] + public static void TestAdd(int count) + { + VerifyAdd(new ArrayList(), count); + } + + [Theory] + [InlineData(1)] + [InlineData(10)] + [InlineData(100)] + public static void TestAdd_SmallCapacity(int count) + { + VerifyAdd(new ArrayList(1), count); + } + + private static void VerifyAdd(ArrayList arrList1, int count) + { + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsFixedSize) + { + return; + } + + for (int i = 0; i < count; i++) + { + arrList2.Add(i); + Assert.Equal(i, arrList2[i]); + Assert.Equal(i + 1, arrList2.Count); + Assert.True(arrList2.Capacity >= arrList2.Count); + } + + Assert.Equal(count, arrList2.Count); + + for (int i = 0; i < count; i++) + { + arrList2.RemoveAt(0); + } + Assert.Equal(0, arrList2.Count); + }); + } + + [Fact] + public static void TestBinarySearch_Basic() + { + var arrList1 = new ArrayList(basicTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, (Action<ArrayList>)(arrList2 => + { + for (int i = 0; i < binarySearchFindTestData.Length; ++i) + { + int ndx = arrList2.BinarySearch(binarySearchFindTestData[i]); + Assert.True(ndx >= 0); + Assert.Equal((string)ArrayListTests.basicTestData[ndx], binarySearchFindTestData[i]); + } + })); + } + + [Fact] + public static void TestBinarySearch_Basic_NotFoundReturnsNextElementIndex() + { + // The zero-based index of the value in the sorted ArrayList, if value is found; otherwise, a negative number, + // which is the bitwise complement of the index of the next element. + ArrayList arrList1 = Helpers.CreateIntArrayList(100); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + Assert.Equal(100, ~arrList2.BinarySearch(150)); + + // Searching for null items should return -1. + Assert.Equal(-1, arrList2.BinarySearch(null)); + }); + } + + [Fact] + public static void TestBinarySearch_Basic_NullObject() + { + ArrayList arrList1 = Helpers.CreateIntArrayList(100); + arrList1.Add(null); + arrList1.Sort(); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + Assert.Equal(0, arrList2.BinarySearch(null)); + }); + } + + [Fact] + public static void TestBinarySearch_Basic_DuplicateResults() + { + // If we have duplicate results, return the first. + var arrList1 = new ArrayList(); + for (int i = 0; i < 100; i++) + arrList1.Add(5); + + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + // Remember, this is BinarySearch. + Assert.Equal(49, arrList2.BinarySearch(5)); + }); + } + + [Fact] + public static void TestBinarySearch_IComparer() + { + var arrList1 = new ArrayList(basicTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, (Action<ArrayList>)(arrList2 => + { + // Use BinarySearch to search and verify selected items. + for (int i = 0; i < binarySearchFindTestData.Length; ++i) + { + int ndx = arrList2.BinarySearch(binarySearchFindTestData[i], new BinarySearchComparer()); + Assert.True(ndx < ArrayListTests.basicTestData.Length); + Assert.Equal(0, (int)ArrayListTests.basicTestData[ndx].CompareTo(binarySearchFindTestData[i])); + } + })); + } + + [Fact] + public static void TestBinarySearch_IComparer_NotFoundReturnsNextElementIndex() + { + ArrayList arrList1 = Helpers.CreateIntArrayList(100); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + Assert.Equal(100, ~arrList2.BinarySearch(150, new BinarySearchComparer())); + + // Searching for null items should return -1. + Assert.Equal(-1, arrList2.BinarySearch(null, new BinarySearchComparer())); + }); + } + + [Fact] + public static void TestBinarySearch_IComparer_NullObject() + { + ArrayList arrList1 = Helpers.CreateIntArrayList(100); + arrList1.Add(null); + arrList1.Sort(); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + Assert.Equal(0, arrList2.BinarySearch(null)); + }); + } + + [Fact] + public static void TestBinarySearch_IComparer_DuplicateResults() + { + // If we have duplicate results, return the first. + var arrList1 = new ArrayList(); + for (int i = 0; i < 100; i++) + arrList1.Add(5); + + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + // Remember, this is BinarySearch. + Assert.Equal(49, arrList2.BinarySearch(5, new BinarySearchComparer())); + }); + } + + [Fact] + public static void TestBinarySearch_Int_Int_IComparer() + { + var arrList1 = new ArrayList(basicTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, (Action<ArrayList>)(arrList2 => + { + for (int i = 0; i < binarySearchFindTestData.Length; ++i) + { + int ndx = arrList2.BinarySearch(0, arrList2.Count, binarySearchFindTestData[i], new BinarySearchComparer()); + Assert.True(ndx >= 0); + Assert.Equal((string)ArrayListTests.basicTestData[ndx], binarySearchFindTestData[i]); + } + })); + } + + [Fact] + public static void TestBinarySearch_Int_Int_IComparer_ObjectOutsideIndex() + { + var arrList1 = new ArrayList(basicTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + // Index > list.IndexOf(object) + int ndx = arrList2.BinarySearch(1, arrList2.Count - 1, "Aquaman", new BinarySearchComparer()); + Assert.Equal(-2, ndx); + + // Index + count < list.IndexOf(object) + ndx = arrList2.BinarySearch(0, arrList2.Count - 2, "Wonder Woman", new BinarySearchComparer()); + Assert.Equal(-21, ndx); + }); + } + + [Fact] + public static void TestBinarySearch_Int_Int_IComparer_NullComparer() + { + var arrList1 = new ArrayList(basicTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + // Locating item in list using a null comparer uses default comparer. + int ndx1 = arrList2.BinarySearch(0, arrList2.Count, "Batman", null); + int ndx2 = arrList2.BinarySearch("Batman", null); + int ndx3 = arrList2.BinarySearch("Batman"); + Assert.Equal(ndx1, ndx2); + Assert.Equal(ndx1, ndx3); + Assert.Equal(2, ndx1); + }); + } + + [Fact] + public static void TestBinarySearch_Int_Int_IComparer_Invalid() + { + var arrList1 = new ArrayList(basicTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + IComparer comparer = new BinarySearchComparer(); + + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.BinarySearch(-1, 1000, arrList2.Count, comparer)); // Index < 0 + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.BinarySearch(-1, 1000, "Batman", comparer)); // Index < 0 + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.BinarySearch(-1, arrList2.Count, "Batman", comparer)); // Index < 0 + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.BinarySearch(0, -1, "Batman", comparer)); // Count < 0 + + // Index + Count >= list.Count + Assert.Throws<ArgumentException>(() => arrList2.BinarySearch(1, arrList2.Count, "Batman", comparer)); + Assert.Throws<ArgumentException>(() => arrList2.BinarySearch(3, arrList2.Count - 2, "Batman", comparer)); + }); + } + + [Fact] + public static void TestCapacity_Get() + { + var arrList = new ArrayList(basicTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList, arrList2 => + { + Assert.True(arrList2.Capacity >= arrList2.Count); + }); + } + + [Fact] + public static void TestCapacity_Set() + { + var arrList = new ArrayList(basicTestData); + int nCapacity = 2 * arrList.Capacity; + arrList.Capacity = nCapacity; + Assert.Equal(nCapacity, arrList.Capacity); + + // Synchronized + arrList = ArrayList.Synchronized(new ArrayList(basicTestData)); + arrList.Capacity = 1000; + Assert.Equal(1000, arrList.Capacity); + + // Range ignores setter + arrList = new ArrayList(basicTestData); + arrList = arrList.GetRange(0, arrList.Count); + arrList.Capacity = 1000; + Assert.NotEqual(100, arrList.Capacity); + } + + [Fact] + public static void TestCapacity_Set_Zero() + { + var arrList = new ArrayList(1); + + arrList.Capacity = 0; + Assert.Equal(4, arrList.Capacity); + + for (int i = 0; i < 32; i++) + arrList.Add(i); + + for (int i = 0; i < 32; i++) + Assert.Equal(i, arrList[i]); + } + + [Fact] + public static void TestCapacity_Set_One() + { + var arrList = new ArrayList(4); + + arrList.Capacity = 1; + Assert.Equal(1, arrList.Capacity); + + for (int i = 0; i < 32; i++) + arrList.Add(i); + + for (int i = 0; i < 32; i++) + Assert.Equal(i, arrList[i]); + } + + [Fact] + public static void TestCapacity_Set_Invalid() + { + var arrList1 = Helpers.CreateIntArrayList(10); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsFixedSize) + { + return; + } + + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.Capacity = -1); // Capacity < 0 + + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.Capacity = arrList1.Count - 1); // Capacity < list.Count + }); + } + + [Fact] + public static void TestClear() + { + var arrList1 = new ArrayList(nullContainingTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsFixedSize) + { + return; + } + + arrList2.Clear(); + Assert.Equal(0, arrList2.Count); + }); + } + + [Fact] + public static void TestClear_EmptyArrayList() + { + var arrList1 = new ArrayList(); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsFixedSize) + { + return; + } + + arrList2.Clear(); + Assert.Equal(0, arrList2.Count); + }); + } + + + [Fact] + public static void TestClear_FixedSizeArrayList_Invalid() + { + // FixedArray + IList sourceArrList = ArrayList.FixedSize(Helpers.CreateIntArrayList(10)); + ArrayList arrList = ArrayList.Adapter(sourceArrList); + Assert.Throws<NotSupportedException>(() => arrList.Clear()); + } + + [Theory] + [InlineData(0)] + [InlineData(1)] + [InlineData(10)] + [InlineData(100)] + public static void TestClone(int count) + { + // Clone should exactly replicate a collection to another object reference + // afterwards these 2 should not hold the same object references + ArrayList arrList1 = Helpers.CreateIntArrayList(count); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + ArrayList clone = (ArrayList)arrList2.Clone(); + + Assert.Equal(arrList2.Count, clone.Count); + + Assert.Equal(arrList2.IsReadOnly, clone.IsReadOnly); + Assert.Equal(arrList2.IsSynchronized, clone.IsSynchronized); + Assert.Equal(arrList2.IsFixedSize, clone.IsFixedSize); + + for (int i = 0; i < arrList2.Count; i++) + { + Assert.Equal(arrList2[i], clone[i]); + } + }); + } + + [Fact] + public static void TestClone_IsShallowCopy() + { + var arrList = new ArrayList(); + for (int i = 0; i < 10; i++) + { + arrList.Add(new Foo()); + } + + ArrayList clone = (ArrayList)arrList.Clone(); + + string stringValue = "Hello World"; + for (int i = 0; i < 10; i++) + { + Assert.Equal(stringValue, ((Foo)clone[i]).StringValue); + } + + // Now we remove an object from the original list, but this should still be present in the clone + arrList.RemoveAt(9); + Assert.Equal(stringValue, ((Foo)clone[9]).StringValue); + + stringValue = "Good Bye"; + ((Foo)arrList[0]).StringValue = stringValue; + Assert.Equal(stringValue, ((Foo)arrList[0]).StringValue); + Assert.Equal(stringValue, ((Foo)clone[0]).StringValue); + + // If we change the object, of course, the previous should not happen + clone[0] = new Foo(); + + stringValue = "Good Bye"; + Assert.Equal(stringValue, ((Foo)arrList[0]).StringValue); + + stringValue = "Hello World"; + Assert.Equal(stringValue, ((Foo)clone[0]).StringValue); + } + + [Fact] + public static void TestContains() + { + var arrList1 = new ArrayList(nullContainingTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, (Action<ArrayList>)(arrList2 => + { + for (int i = 0; i < ArrayListTests.nullContainingTestData.Length; i++) + { + Assert.True(arrList2.Contains((object)ArrayListTests.nullContainingTestData[i])); + } + + if (!arrList2.IsFixedSize) + { + // Remove an element, and make sure that the element, however many times it is in the list, is removed. + for (int i = 0; i < ArrayListTests.nullContainingTestData.Length; i++) + { + for (int j = 0; j < ArrayListTests.nullContainingTestData.Length; j++) + { + arrList2.Remove((object)ArrayListTests.nullContainingTestData[i]); + } + + Assert.False(arrList2.Contains((object)ArrayListTests.nullContainingTestData[i])); + } + } + })); + } + + [Fact] + public static void TestContains_NonExistentObject() + { + ArrayList arrList1 = Helpers.CreateIntArrayList(100); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + Assert.False(arrList2.Contains(101)); + Assert.False(arrList2.Contains("50")); + Assert.False(arrList2.Contains(null)); + }); + } + + [Fact] + public static void TestContains_EmptyArrayList() + { + var arrList1 = new ArrayList(); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + Assert.False(arrList2.Contains(1)); + Assert.False(arrList2.Contains("hello world")); + Assert.False(arrList2.Contains(null)); + }); + } + + [Fact] + public static void TestCopyTo_Basic() + { + var arrList1 = new ArrayList(nullContainingTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, (Action<ArrayList>)(arrList2 => + { + var arrCopy = new string[ArrayListTests.nullContainingTestData.Length]; + arrList2.CopyTo(arrCopy); + Assert.Equal(arrList2.Count, arrCopy.Length); + for (int i = 0; i < arrCopy.Length; i++) + { + Assert.Equal((string)ArrayListTests.nullContainingTestData[i], arrCopy[i]); + } + })); + } + + [Fact] + public static void TestCopyTo_Basic_EmptyArrayListToFilledArray() + { + var arrList1 = new ArrayList(); + string[] arrCopy = (string[])nullContainingTestData.Clone(); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, (Action<ArrayList>)(arrList2 => + { + arrList2.CopyTo(arrCopy); + + // Make sure sentinels stay the same + for (int i = 0; i < arrCopy.Length; i++) + { + Assert.Equal((string)ArrayListTests.nullContainingTestData[i], arrCopy[i]); + } + })); + } + + [Fact] + public static void TestCopyTo_Basic_EmptyArray() + { + var arrList1 = new ArrayList(); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + var arrCopy = new string[0]; + arrList2.CopyTo(arrCopy); + Assert.Equal(0, arrCopy.Length); + }); + } + + [Fact] + public static void TestCopyTo_Basic_Invalid() + { + Assert.Throws<ArgumentNullException>(() => new ArrayList().CopyTo(null)); // Array is null + Assert.Throws<ArgumentException>(() => new ArrayList().CopyTo(new object[10, 10])); // Array is multidimensional + } + + [Fact] + public static void TestCopyTo_Int() + { + var arrList1 = Helpers.CreateIntArrayList(10); + int index = 1; + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + var arrCopy = new int[arrList2.Count + index]; + arrCopy.SetValue(400, 0); + arrList2.CopyTo(arrCopy, index); + Assert.Equal(arrList2.Count + index, arrCopy.Length); + + for (int i = 0; i < arrCopy.Length; i++) + { + if (i == 0) + { + Assert.Equal(400, arrCopy.GetValue(i)); + } + else + { + Assert.Equal(arrList2[i - 1], arrCopy.GetValue(i)); + } + } + }); + } + + [Fact] + public static void TestCopyTo_Int_EqualToLength() + { + var arrList1 = new ArrayList(); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + var arrCopy = new string[2]; + arrList2.CopyTo(arrCopy, arrCopy.Length); // Should not throw + }); + } + + [Fact] + public static void TestCopyTo_Int_EmptyArrayListToFilledArray() + { + var arrList1 = new ArrayList(); + string[] arrCopy = (string[])nullContainingTestData.Clone(); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, (Action<ArrayList>)(arrList2 => + { + arrList2.CopyTo(arrCopy, 3); + + // Make sure sentinels stay the same + for (int i = 0; i < arrCopy.Length; i++) + { + Assert.Equal((string)ArrayListTests.nullContainingTestData[i], arrCopy[i]); + } + })); + } + + [Fact] + public static void TestCopyTo_Int_EmptyArray() + { + var arrList1 = new ArrayList(); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + var arrCopy = new string[0]; + arrList2.CopyTo(arrCopy, 0); + Assert.Equal(0, arrCopy.Length); + }); + } + + [Fact] + public static void TestCopyTo_Int_Invalid() + { + var arrList1 = new ArrayList(nullContainingTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, (Action<ArrayList>)(arrList2 => + { + var arrCopy = new string[ArrayListTests.nullContainingTestData.Length]; + + Assert.Throws<ArgumentNullException>(() => arrList2.CopyTo(null)); // Array is null + Assert.Throws<ArgumentException>(() => arrList2.CopyTo(new object[10, 10])); // Array is multidimensional + + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.CopyTo(arrCopy, -1)); // Index < 0 + })); + + arrList1 = Helpers.CreateIntArrayList(10); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + // Invalid length / index + Assert.Throws<ArgumentException>(() => arrList2.CopyTo(new object[11], 2)); + }); + } + + [Fact] + public static void TestCopyTo_Int_Int() + { + var arrList1 = new ArrayList(basicTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + int start = 3; + int count = 15; + var arrCopy = new string[100]; + arrList2.CopyTo(start, arrCopy, start, count); + Assert.Equal(100, arrCopy.Length); + for (int i = start; i < start + count; ++i) + { + Assert.Equal(arrList1[i], arrCopy[i]); + } + }); + } + + [Fact] + public static void TestCopyTo_Int_Int_EmptyArrayListToFilledArray() + { + var arrList1 = new ArrayList(); + string[] arrCopy = (string[])nullContainingTestData.Clone(); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, (Action<ArrayList>)(arrList2 => + { + arrList2.CopyTo(0, arrCopy, 3, 0); + + // Make sure sentinels stay the same + for (int i = 0; i < arrCopy.Length; i++) + { + Assert.Equal((string)ArrayListTests.nullContainingTestData[i], arrCopy[i]); + } + })); + } + + [Fact] + public static void TestCopyTo_Int_Int_Invalid() + { + var arrList1 = new ArrayList(basicTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + var arrCopy = new string[10]; + // Should throw ArgumentOutOfRangeException + Assert.ThrowsAny<ArgumentException>(() => arrList2.CopyTo(0, arrCopy, -1, 1000)); // Array index < 0 + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.CopyTo(-1, arrCopy, 0, 1)); // Index < 0 + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.CopyTo(0, arrCopy, 0, -1)); // Count < 0 + + Assert.Throws<ArgumentException>(() => + { + arrCopy = new string[100]; + arrList2.CopyTo(arrList2.Count - 1, arrCopy, 0, 24); + }); + + Assert.Throws<ArgumentNullException>(() => arrList2.CopyTo(0, null, 3, 15)); // Array is null + + // Array index and count is out of bounds + Assert.Throws<ArgumentException>(() => + { + arrCopy = new string[1]; + arrList2.CopyTo(0, arrCopy, 3, 15); + }); + + // Array is multidimensional + Assert.Throws<ArgumentException>(() => arrList2.CopyTo(0, new object[arrList2.Count, arrList2.Count], 0, arrList2.Count)); + + // Should throw ArgumentOutOfRangeException + Assert.ThrowsAny<ArgumentException>(() => arrList2.CopyTo(0, new object[arrList2.Count, arrList2.Count], 0, -1)); + }); + } + + [Fact] + public static void TestFixedSize_ArrayList() + { + ArrayList arrList1 = Helpers.CreateIntArrayList(10); + ArrayList arrList2 = ArrayList.FixedSize(arrList1); + + Assert.True(arrList2.IsFixedSize); + Assert.False(arrList2.IsReadOnly); + Assert.False(arrList2.IsSynchronized); + + Assert.Equal(arrList1.Count, arrList2.Count); + for (int i = 0; i < arrList1.Count; i++) + { + Assert.Equal(arrList1[i], arrList2[i]); + } + + // Remove an object from the original list and verify the object underneath has been cut + arrList1.RemoveAt(9); + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2[9]); + + // We cant remove or add to the fixed list + Assert.Throws<NotSupportedException>(() => arrList2.RemoveAt(0)); + Assert.Throws<NotSupportedException>(() => arrList2.Remove(5)); + Assert.Throws<NotSupportedException>(() => arrList2.RemoveRange(0, 1)); + Assert.Throws<NotSupportedException>(() => arrList2.Clear()); + Assert.Throws<NotSupportedException>(() => arrList2.Add(5)); + Assert.Throws<NotSupportedException>(() => arrList2.AddRange(new ArrayList())); + Assert.Throws<NotSupportedException>(() => arrList2.Insert(0, 5)); + Assert.Throws<NotSupportedException>(() => arrList2.InsertRange(0, new ArrayList())); + + Assert.Throws<NotSupportedException>(() => arrList2.TrimToSize()); + Assert.Throws<NotSupportedException>(() => arrList2.Capacity = 10); + } + + [Fact] + public static void TestFixedSize_ReadOnlyArrayList() + { + ArrayList arrList = ArrayList.FixedSize(ArrayList.ReadOnly(new ArrayList())); + Assert.True(arrList.IsFixedSize); + Assert.True(arrList.IsReadOnly); + Assert.False(arrList.IsSynchronized); + } + + [Fact] + public static void TestFixedSize_SynchronizedArrayList() + { + ArrayList arrList = ArrayList.FixedSize(ArrayList.Synchronized(new ArrayList())); + Assert.True(arrList.IsFixedSize); + Assert.False(arrList.IsReadOnly); + Assert.True(arrList.IsSynchronized); + } + + [Fact] + public static void TestFixedSize_RangeArrayList() + { + ArrayList arrList = ArrayList.FixedSize(new ArrayList()).GetRange(0, 0); + Assert.True(arrList.IsFixedSize); + } + + [Fact] + public static void TestFixedSize_ArrayList_CanChangeExistingItems() + { + ArrayList arrList1 = Helpers.CreateIntArrayList(10); + ArrayList arrList2 = ArrayList.FixedSize(arrList1); + + arrList2[0] = 10; + Assert.Equal(10, arrList2[0]); + } + + [Fact] + public static void TestFixedSize_ArrayList_Invalid() + { + Assert.Throws<ArgumentNullException>(() => ArrayList.FixedSize(null)); // List is null + } + + [Fact] + public static void TestFixedSize_IList() + { + ArrayList arrList = Helpers.CreateIntArrayList(10); + IList iList = ArrayList.FixedSize((IList)arrList); + + Assert.True(iList.IsFixedSize); + Assert.False(iList.IsReadOnly); + Assert.False(iList.IsSynchronized); + + Assert.Equal(arrList.Count, iList.Count); + for (int i = 0; i < arrList.Count; i++) + { + Assert.Equal(arrList[i], iList[i]); + } + } + + [Fact] + public static void TestFixedSize_SynchronizedIList() + { + IList iList = ArrayList.FixedSize((IList)ArrayList.Synchronized(new ArrayList())); + Assert.True(iList.IsFixedSize); + Assert.False(iList.IsReadOnly); + Assert.True(iList.IsSynchronized); + } + + [Fact] + public static void TestFixedSize_IList_Contains() + { + ArrayList arrList = Helpers.CreateIntArrayList(10); + IList iList = ArrayList.FixedSize((IList)arrList); + for (int i = 0; i < iList.Count; i++) + { + Assert.True(iList.Contains(i)); + } + } + + [Fact] + public static void TestFixedSize_IList_IndexOf() + { + ArrayList arrList = Helpers.CreateIntArrayList(10); + IList iList = ArrayList.FixedSize((IList)arrList); + for (int i = 0; i < iList.Count; i++) + { + Assert.Equal(i, iList.IndexOf(i)); + } + } + + [Fact] + public static void TestFixedSize_IList_SyncRoot() + { + ArrayList arrList = Helpers.CreateIntArrayList(10); + IList iList = ArrayList.FixedSize((IList)arrList); + + Assert.Same(arrList.SyncRoot, iList.SyncRoot); + } + + [Fact] + public static void TestFixedSize_IList_CopyTo() + { + ArrayList arrList = Helpers.CreateIntArrayList(10); + IList iList = ArrayList.FixedSize((IList)arrList); + + int index = 50; + var array = new object[iList.Count + index]; + iList.CopyTo(array, index); + + Assert.Equal(iList.Count + index, array.Length); + for (int i = index; i < arrList.Count; i++) + { + Assert.Equal(arrList[i], array[i]); + } + } + + [Fact] + public static void TestFixedSize_IList_GetEnumerator() + { + ArrayList arrList = Helpers.CreateIntArrayList(10); + IList iList = ArrayList.FixedSize((IList)arrList); + + IEnumerator enumerator = iList.GetEnumerator(); + int count = 0; + + while (enumerator.MoveNext()) + { + Assert.Equal(iList[count], enumerator.Current); + count++; + } + Assert.Equal(iList.Count, count); + } + + [Fact] + public static void TestFixedSizeIList_GetEnumerator_Invalid() + { + ArrayList arrList = Helpers.CreateIntArrayList(10); + IList iList = ArrayList.FixedSize((IList)arrList); + + IEnumerator enumerator = iList.GetEnumerator(); + // Index < 0 + Assert.Throws<InvalidOperationException>(() => enumerator.Current); + + // Index >= count + while (enumerator.MoveNext()) ; + Assert.False(enumerator.MoveNext()); + Assert.Throws<InvalidOperationException>(() => enumerator.Current); + + // Resetting should throw + enumerator.Reset(); + + enumerator.MoveNext(); + enumerator.Reset(); + Assert.Throws<InvalidOperationException>(() => enumerator.Current); + } + + [Fact] + public static void TestFixedSize_IList_NotSupportedMethods() + { + ArrayList arrList = Helpers.CreateIntArrayList(10); + IList iList = ArrayList.FixedSize((IList)arrList); + + // Remove an object from the original list. Verify the object underneath has been cut + arrList.RemoveAt(9); + Assert.Throws<ArgumentOutOfRangeException>(() => iList[9]); + + // We cant remove or add to the fixed list + Assert.Throws<NotSupportedException>(() => iList.RemoveAt(0)); + Assert.Throws<NotSupportedException>(() => iList.Clear()); + Assert.Throws<NotSupportedException>(() => iList.Add(5)); + Assert.Throws<NotSupportedException>(() => iList.Insert(0, 5)); + Assert.Throws<NotSupportedException>(() => iList.Remove(5)); + } + + [Fact] + public static void TestFixedSize_IList_CanChangeExistingItems() + { + ArrayList arrList1 = Helpers.CreateIntArrayList(10); + IList iList = ArrayList.FixedSize((IList)arrList1); + + // Ensure we can change existing objects stored in the list + iList[0] = 10; + Assert.Equal(10, iList[0]); + } + + [Fact] + public static void TestFixedSize_IList_Invalid() + { + Assert.Throws<ArgumentNullException>(() => ArrayList.FixedSize((IList)null)); // List is null + } + + [Fact] + public static void TestGetEnumerator_Basic() + { + var arrList1 = new ArrayList(basicTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, (Action<ArrayList>)(arrList2 => + { + IEnumerator enumerator1 = arrList2.GetEnumerator(); + IEnumerator enumerator2 = arrList2.GetEnumerator(); + + IEnumerator[] enuArray = { enumerator1, enumerator2 }; + + foreach (IEnumerator enumerator in enuArray) + { + for (int i = 0; i < 2; i++) + { + Assert.NotNull(enumerator); + for (int j = 0; j < ArrayListTests.basicTestData.Length; j++) + { + if (enumerator.MoveNext()) + { + Assert.Equal((object)ArrayListTests.basicTestData[j], enumerator.Current); + } + } + + Assert.False(enumerator.MoveNext()); + Assert.False(enumerator.MoveNext()); + Assert.False(enumerator.MoveNext()); + + enumerator.Reset(); + } + } + })); + } + + [Fact] + public static void TestGetEnumerator_Basic_ArrayListContainingItself() + { + // Verify the enumerator works correctly when the ArrayList itself is in the ArrayList + var arrList1 = new ArrayList(basicTestData); + arrList1.Add(arrList1); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + IEnumerator enumerator = arrList2.GetEnumerator(); + + for (int i = 0; i < 2; i++) + { + int index = 0; + while (enumerator.MoveNext()) + { + Assert.StrictEqual(enumerator.Current, arrList2[index]); + index++; + } + enumerator.Reset(); + } + }); + } + + [Fact] + public static void TestGetEnumerator_Basic_DerivedArrayList() + { + // The enumerator for a derived (subclassed) ArrayList is different to a normal ArrayList as it does not run an optimized MoveNext() function + var arrList = new DerivedArrayList(basicTestData); + + IEnumerator enumerator = arrList.GetEnumerator(); + for (int i = 0; i < 2; i++) + { + int index = 0; + while (enumerator.MoveNext()) + { + Assert.StrictEqual(enumerator.Current, arrList[index]); + index++; + } + enumerator.Reset(); + } + } + + [Fact] + public static void TestGetEnumerator_Basic_Invalid() + { + var arrList1 = new ArrayList(basicTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + IEnumerator enumerator = arrList2.GetEnumerator(); + // If the underlying collection is modified, MoveNext and Reset throw, but Current doesn't + if (!arrList2.IsReadOnly) + { + enumerator.MoveNext(); + + object originalValue = arrList2[0]; + arrList2[0] = 10; + + object temp = enumerator.Current; + + Assert.Throws<InvalidOperationException>(() => enumerator.MoveNext()); + Assert.Throws<InvalidOperationException>(() => enumerator.Reset()); + + arrList2[0] = originalValue; + } + + // Index < 0 + enumerator = arrList2.GetEnumerator(); + Assert.Throws<InvalidOperationException>(() => enumerator.Current); + + // Current throws after resetting + enumerator = arrList2.GetEnumerator(); + enumerator.MoveNext(); + enumerator.Reset(); + Assert.Throws<InvalidOperationException>(() => enumerator.Current); + + // Current throws if the current index is >= count + enumerator = arrList2.GetEnumerator(); + while (enumerator.MoveNext()) ; + Assert.False(enumerator.MoveNext()); + Assert.Throws<InvalidOperationException>(() => enumerator.Current); + }); + } + + [Fact] + public static void TestGetEnumerator_Int_Int() + { + int start = 3; + int count = 15; + + var arrList1 = new ArrayList(basicTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, (Action<ArrayList>)(arrList2 => + { + IEnumerator enumerator = arrList2.GetEnumerator(start, count); + Assert.NotNull(enumerator); + + for (int i = start; i < start + count; i++) + { + if (enumerator.MoveNext()) + { + Assert.Equal((object)ArrayListTests.basicTestData[i], enumerator.Current); + } + } + + Assert.False(enumerator.MoveNext()); + Assert.False(enumerator.MoveNext()); + Assert.False(enumerator.MoveNext()); + Assert.False(enumerator.MoveNext()); + })); + } + + [Fact] + public static void TestGetEnumerator_Int_Int_ArrayListContainingItself() + { + // Verify the enumerator works correctly when the ArrayList itself is in the ArrayList + var arrList1 = new ArrayList(basicTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, (Action<ArrayList>)(arrList2 => + { + if (arrList2.IsFixedSize) + { + return; + } + + arrList2.Insert(0, arrList2); + arrList2.Insert(arrList2.Count, arrList2); + arrList2.Insert(arrList2.Count / 2, arrList2); + + var tempArray = new object[ArrayListTests.basicTestData.Length + 3]; + tempArray[0] = arrList2; + tempArray[tempArray.Length / 2] = arrList2; + tempArray[tempArray.Length - 1] = arrList2; + + Array.Copy((Array)ArrayListTests.basicTestData, 0, tempArray, 1, (int)(ArrayListTests.basicTestData.Length / 2)); + Array.Copy((Array)ArrayListTests.basicTestData, (int)(ArrayListTests.basicTestData.Length / 2), tempArray, (tempArray.Length / 2) + 1, (int)(ArrayListTests.basicTestData.Length - (ArrayListTests.basicTestData.Length / 2))); + + // Enumerate the entire collection + IEnumerator enumerator = arrList2.GetEnumerator(0, tempArray.Length); + + for (int loop = 0; loop < 2; ++loop) + { + for (int i = 0; i < tempArray.Length; ++i) + { + enumerator.MoveNext(); + Assert.StrictEqual(tempArray[i], enumerator.Current); + } + + Assert.False(enumerator.MoveNext()); + enumerator.Reset(); + } + + // Enumerate only part of the collection + enumerator = arrList2.GetEnumerator(1, tempArray.Length - 2); + + for (int loop = 0; loop < 2; ++loop) + { + for (int i = 1; i < tempArray.Length - 1; ++i) + { + enumerator.MoveNext(); + Assert.StrictEqual(tempArray[i], enumerator.Current); + } + + Assert.False(enumerator.MoveNext()); + enumerator.Reset(); + } + })); + } + + [Fact] + public static void TestGetEnumerator_Int_Int_ZeroCount() + { + var arrList1 = new ArrayList(basicTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + IEnumerator enumerator = arrList2.GetEnumerator(0, 0); + Assert.False(enumerator.MoveNext()); + Assert.False(enumerator.MoveNext()); + Assert.False(enumerator.MoveNext()); + }); + } + + [Fact] + public static void TestGetEnumerator_Int_Int_Invalid() + { + int start = 3; + int count = 15; + + var arrList1 = new ArrayList(basicTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + IEnumerator enumerator = arrList2.GetEnumerator(start, count); + // If the underlying collection is modified, MoveNext and Reset throw, but Current doesn't + if (!arrList2.IsReadOnly) + { + enumerator.MoveNext(); + + object originalValue = arrList2[0]; + arrList2[0] = 10; + + object temp = enumerator.Current; + + Assert.Throws<InvalidOperationException>(() => enumerator.MoveNext()); + Assert.Throws<InvalidOperationException>(() => enumerator.Reset()); + + arrList2[0] = originalValue; + } + + // Current throws after resetting + enumerator = arrList2.GetEnumerator(start, count); + enumerator.Reset(); + Assert.Throws<InvalidOperationException>(() => enumerator.Current); + + // Current throws if the current index is < 0 or >= count + enumerator = arrList2.GetEnumerator(start, count); + Assert.Throws<InvalidOperationException>(() => enumerator.Current); + + while (enumerator.MoveNext()) ; + Assert.Throws<InvalidOperationException>(() => enumerator.Current); + + // Invalid parameters + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.GetEnumerator(-1, arrList2.Count)); // Index < 0 + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.GetEnumerator(0, -1)); // Count < 0 + Assert.Throws<ArgumentException>(() => arrList2.GetEnumerator(0, arrList2.Count + 1)); // Count + list.Count + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.GetEnumerator(-1, arrList2.Count + 1)); // Index < 0 and count > list.Count + }); + } + + [Fact] + public static void TestGetRange() + { + int index = 10; + int count = 50; + + ArrayList arrList1 = Helpers.CreateIntArrayList(100); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + ArrayList range = arrList2.GetRange(index, count); + + Assert.Equal(count, range.Count); + + for (int i = 0; i < range.Count; i++) + { + Assert.Equal(arrList2[i + index], range[i]); + } + + Assert.Equal(arrList2.IsFixedSize, range.IsFixedSize); + Assert.Equal(arrList2.IsReadOnly, range.IsReadOnly); + Assert.False(range.IsSynchronized); + + Assert.Throws<NotSupportedException>(() => range.TrimToSize()); + }); + } + + [Fact] + public static void TestGetRange_ChangeUnderlyingCollection() + { + int index = 10; + int count = 50; + + ArrayList arrList1 = Helpers.CreateIntArrayList(100); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + ArrayList range = arrList2.GetRange(index, count);// We can change the underlying collection through the range and this[int index] + if (!range.IsReadOnly) + { + for (int i = 0; i < 50; i++) + range[i] = (int)range[i] + 1; + + for (int i = 0; i < 50; i++) + { + Assert.Equal(i + 10 + 1, range[i]); + } + + for (int i = 0; i < 50; i++) + range[i] = (int)range[i] - 1; + } + + // We can change the underlying collection through the range and Add + if (!range.IsFixedSize) + { + for (int i = 0; i < 100; i++) + range.Add(i + 1000); + + Assert.Equal(150, range.Count); + Assert.Equal(200, arrList2.Count); + + for (int i = 0; i < 50; i++) + { + Assert.Equal(i + 10, range[i]); + } + + for (int i = 0; i < 100; i++) + { + Assert.Equal(i + 1000, range[50 + i]); + } + } + }); + } + + [Fact] + public static void TestGetRange_ChangeUnderlyingCollection_Invalid() + { + int index = 10; + int count = 50; + + ArrayList arrList1 = Helpers.CreateIntArrayList(100); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + ArrayList range = arrList2.GetRange(index, count); + + // If we change the underlying collection through set this[int index] range will start to throw + if (arrList2.IsReadOnly) + { + Assert.Throws<NotSupportedException>(() => arrList2[arrList2.Count - 1] = -1); + int iTemp = range.Count; + } + else + { + arrList2[arrList2.Count - 1] = -1; + Assert.Throws<InvalidOperationException>(() => range.Count); + } + + // If we change the underlying collection through add range will start to throw + range = arrList2.GetRange(10, 50); + if (arrList2.IsFixedSize) + { + Assert.Throws<NotSupportedException>(() => arrList2.Add(arrList2.Count + 1000)); + int iTemp = range.Count; + } + else + { + arrList2.Add(arrList2.Count + 1000); + Assert.Throws<InvalidOperationException>(() => range.Count); + } + }); + } + + [Fact] + public static void TestGetRange_Invalid() + { + ArrayList arrList1 = Helpers.CreateIntArrayList(100); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.GetRange(-1, 50)); // Index < 0 + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.GetRange(0, -1)); // Count < 0 + + Assert.Throws<ArgumentException>(() => arrList2.GetRange(0, 500)); // Index + count > list.count + Assert.Throws<ArgumentException>(() => arrList2.GetRange(arrList2.Count, 1)); // Index >= list.count + }); + } + + [Fact] + public static void TestGetRange_Empty() + { + // We should be able to get a range of 0 + var arrList1 = new ArrayList(); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + ArrayList range = arrList2.GetRange(0, 0); + Assert.Equal(0, range.Count); + }); + } + + [Fact] + public static void TestSetRange() + { + int start = 3; + + var arrList1 = new ArrayList(basicTestData); + var arrSetRange = new ArrayList(setRangeTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsReadOnly) + { + return; + } + + arrList2.SetRange(start, arrSetRange); + + // Verify set + for (int i = 0; i < arrSetRange.Count; ++i) + { + Assert.Equal(arrSetRange[i], arrList2[start + i]); + } + }); + } + + [Fact] + public static void TestSetRange_Invalid() + { + var arrList1 = new ArrayList(basicTestData); + var arrSetRange = new ArrayList(setRangeTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsReadOnly) + { + return; + } + + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.SetRange(3, arrList2)); // Index + collection.Count > list.Count + + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.SetRange(-1, arrSetRange)); // Index < 0 + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.SetRange(arrList2.Count, arrSetRange)); // Index > list.Count + + Assert.Throws<ArgumentNullException>(() => arrList2.SetRange(0, null)); // Collection is null + }); + } + + [Fact] + public static void TestSetRange_EmptyCollection() + { + var arrList1 = new ArrayList(basicTestData); + ICollection emptyCollection = new ArrayList(); + + Helpers.PerformActionOnAllArrayListWrappers(arrList1, (Action<ArrayList>)(arrList2 => + { + if (arrList2.IsReadOnly) + { + return; + } + + // No change + arrList2.SetRange(0, emptyCollection); + for (int i = 0; i < arrList2.Count; i++) + { + Assert.Equal((object)ArrayListTests.basicTestData[i], arrList2[i]); + } + })); + } + + [Fact] + public static void TestIndexOf_Basic() + { + var arrList1 = new ArrayList(indexOfUniqueTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + for (int i = 0; i < indexOfUniqueTestData.Length; i++) + { + Assert.Equal(i, arrList2.IndexOf(indexOfUniqueTestData[i])); + } + }); + } + + [Fact] + public static void TestIndexOf_Basic_DuplicateItems() + { + var arrList1 = new ArrayList(); + arrList1.Add(null); + arrList1.Add(arrList1); + arrList1.Add(null); + + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + Assert.Equal(0, arrList2.IndexOf(null)); + }); + } + + [Fact] + public static void TestIndexOf_Basic_NonExistentObject() + { + // Try to find a non-existent object (expects -1) + var arrList1 = new ArrayList(duplicateContainingTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + Assert.Equal(-1, arrList2.IndexOf(null)); + Assert.Equal(-1, arrList2.IndexOf("hello")); + Assert.Equal(-1, arrList2.IndexOf(5)); + }); + } + + [Fact] + public static void TestIndexOf_Int() + { + var arrList1 = new ArrayList(duplicateContainingTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, (Action<ArrayList>)(arrList2 => + { + int startIndex = 0; + int index = -1; + while (startIndex < arrList2.Count && (index = arrList2.IndexOf("Batman", startIndex)) != -1) + { + Assert.True(startIndex <= index); + Assert.Equal((object)ArrayListTests.duplicateContainingTestData[index], arrList2[index]); + startIndex = index + 1; + } + })); + } + + [Fact] + public static void TestIndexOf_Int_NonExistentObject() + { + // Try to find a non-Existent object (expects -1) + var arrList1 = new ArrayList(duplicateContainingTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + Assert.Equal(-1, arrList2.IndexOf(null, 0)); + Assert.Equal(-1, arrList2.IndexOf("hello", 1)); + Assert.Equal(-1, arrList2.IndexOf(5, 2)); + }); + } + + [Fact] + public static void TestIndexOf_Int_ExistentObjectNotInRange() + { + // Find an existing object before the index (expects -1) + ArrayList arrList1 = Helpers.CreateIntArrayList(10); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + Assert.Equal(-1, arrList2.IndexOf(0, 1)); + }); + } + + [Fact] + public static void TestIndexOf_Int_Invalid() + { + var arrList1 = new ArrayList(duplicateContainingTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.IndexOf("Batman", -1)); // Start index < 0 + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.IndexOf("Batman", arrList2.Count + 1)); // Start index > list.Count + + Assert.Equal(-1, arrList2.IndexOf("Batman", arrList2.Count, 0)); // Index = list.Count + }); + } + + [Fact] + public static void TestIndexOf_Int_Int() + { + var arrList1 = new ArrayList(duplicateAndNullContainingTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, (Action<ArrayList>)(arrList2 => + { + int index = 0; + int startIndex = 0; + int tmpNdx = 0; + while (startIndex < arrList2.Count && (index = arrList2.IndexOf("Batman", startIndex, (arrList2.Count - startIndex))) != -1) + { + Assert.True(index >= startIndex); + + Assert.Equal((object)ArrayListTests.duplicateAndNullContainingTestData[index], arrList2[index]); + + tmpNdx = arrList2.IndexOf("Batman", startIndex, index - startIndex + 1); + Assert.Equal(index, tmpNdx); + + tmpNdx = arrList2.IndexOf("Batman", startIndex, index - startIndex); + Assert.Equal(-1, tmpNdx); + + startIndex = index + 1; + } + + index = arrList2.IndexOf(null, 0, arrList2.Count); + Assert.Null(arrList2[index]); + })); + } + + [Fact] + public static void TestIndexOf_Int_Int_NonExistentObject() + { + // Try to find non-existent object (expects -1) + var arrList1 = new ArrayList(duplicateContainingTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + Assert.Equal(-1, arrList2.IndexOf(null, 0, arrList2.Count)); + Assert.Equal(-1, arrList2.IndexOf("hello", 1, arrList2.Count - 1)); + Assert.Equal(-1, arrList2.IndexOf(5, 2, arrList2.Count - 2)); + }); + } + + [Fact] + public static void TestIndexOf_Int_Int_ExistentObjectNotInRange() + { + // Find an existing object before the startIndex or after startIndex + count (expects -1) + ArrayList arrList1 = Helpers.CreateIntArrayList(10); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + Assert.Equal(-1, arrList2.IndexOf(0, 1, arrList2.Count - 1)); + Assert.Equal(-1, arrList2.IndexOf(10, 0, 5)); + }); + } + + [Fact] + public static void TestIndexOf_Int_Int_Invalid() + { + var arrList1 = new ArrayList(duplicateAndNullContainingTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.IndexOf("Batman", -1, arrList2.Count)); // Start index < 0 + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.IndexOf("Batman", arrList2.Count + 1, arrList2.Count)); // Start index > Count + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.IndexOf("Batman", 0, -1)); // Count < 0 + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.IndexOf("Batman", 3, arrList2.Count + 1)); // Count > list.Count + + Assert.Equal(-1, arrList2.IndexOf("Batman", arrList2.Count, 0)); // Index = list.Count + }); + } + + [Fact] + public static void TestInsertRange() + { + var arrList1 = new ArrayList(basicTestData); + var arrInsert = new ArrayList(insertRangeRangeToInsertTestData); + int start = 3; + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsFixedSize) + { + return; + } + + // Insert collection into array list and verify. + arrList2.InsertRange(start, arrInsert); + for (int i = 0; i < insertRangeExpectedTestData.Length; ++i) + { + Assert.Equal(insertRangeExpectedTestData[i], arrList2[i]); + } + }); + } + + [Fact] + public static void TestInsertRange_LargeCapacity() + { + // Add a range large enough to increase the capacity of the arrayList by more than a factor of two + var arrList1 = new ArrayList(); + ArrayList arrInsert = Helpers.CreateIntArrayList(128); + + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsFixedSize) + { + return; + } + + arrList2.InsertRange(0, arrInsert); + + for (int i = 0; i < arrInsert.Count; i++) + { + Assert.Equal(i, arrList2[i]); + } + }); + } + + [Fact] + public static void TestInsertRange_EmptyCollection() + { + ArrayList arrList1 = Helpers.CreateIntArrayList(100); + var emptyCollection = new Queue(); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsFixedSize) + { + return; + } + + arrList2.InsertRange(0, emptyCollection); + Assert.Equal(100, arrList2.Count); + }); + } + + [Fact] + public static void TestInsertRange_WrappedNonArrayList() + { + // Create an array list by wrapping a non-ArrayList object (e.g. List<T>) + var list = new List<string>(basicTestData); + ArrayList arrList = ArrayList.Adapter(list); + var arrInsert = new ArrayList(insertRangeRangeToInsertTestData); + + arrList.InsertRange(3, arrInsert); + for (int i = 0; i < insertRangeExpectedTestData.Length; ++i) + { + Assert.Equal(insertRangeExpectedTestData[i], arrList[i]); + } + } + + [Fact] + public static void TestInsertRange_Itself() + { + var arrList1 = new ArrayList(basicTestData); + int start = 3; + Helpers.PerformActionOnAllArrayListWrappers(arrList1, (Action<ArrayList>)(arrList2 => + { + if (arrList2.IsFixedSize) + { + return; + } + + arrList2.InsertRange(start, arrList2); + for (int i = 0; i < arrList2.Count; ++i) + { + string expectedItem; + + if (i < start) + { + expectedItem = ArrayListTests.basicTestData[i]; + } + else if (start <= i && i - start < ArrayListTests.basicTestData.Length) + { + expectedItem = ArrayListTests.basicTestData[i - start]; + } + else + { + expectedItem = ArrayListTests.basicTestData[(int)(i - ArrayListTests.basicTestData.Length)]; + } + Assert.Equal(expectedItem, arrList2[i]); + } + + // Verify that ArrayList does not pass the internal array to CopyTo + arrList2.Clear(); + for (int i = 0; i < 64; ++i) + { + arrList2.Add(i); + } + + ArrayList arrInsert = Helpers.CreateIntArrayList(4); + + MyCollection myCollection = new MyCollection(arrInsert); + arrList2.InsertRange(4, myCollection); + + Assert.Equal(0, myCollection.StartIndex); + + Assert.Equal(4, myCollection.Array.Length); + })); + } + + [Fact] + public static void InsertRange_Invalid() + { + var arrList1 = new ArrayList(basicTestData); + var arrListInsert = new ArrayList(insertRangeRangeToInsertTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsFixedSize) + { + return; + } + + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.InsertRange(-1, arrListInsert)); // Index < 0 + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.InsertRange(1000, arrListInsert)); // Index > count + + Assert.Throws<ArgumentNullException>(() => arrList2.InsertRange(3, null)); // Collection is null + }); + } + + [Fact] + public static void TestInsert() + { + var arrList1 = new ArrayList(basicTestData); + int start = 3; + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsFixedSize) + { + return; + } + + for (int ii = 0; ii < insertObjectsToInsertTestData.Length; ++ii) + { + arrList2.Insert(start + ii, insertObjectsToInsertTestData[ii]); + } + + for (int ii = 0; ii < insertExpectedTestData.Length; ++ii) + { + Assert.Equal(insertExpectedTestData[ii], arrList2[ii]); + } + }); + } + + [Fact] + public static void TestInsert_Invalid() + { + var arrList1 = Helpers.CreateIntArrayList(10); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsFixedSize) + { + return; + } + + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.Insert(-1, "Batman")); // Index < 0 + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.Insert(arrList2.Count + 1, "Batman")); // Index > count + }); + } + + [Fact] + public static void TestItem_Get() + { + var arrList1 = new ArrayList(basicTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, (Action<ArrayList>)(arrList2 => + { + for (int i = 0; i < ArrayListTests.basicTestData.Length; ++i) + { + Assert.Equal((object)ArrayListTests.basicTestData[i], arrList2[i]); + } + })); + } + + [Fact] + public static void TestItem_Get_Invalid() + { + var arrList1 = new ArrayList(basicTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2[-1]); // Index < 0 + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2[arrList2.Count]); // Index >= list.Count + }); + } + + [Fact] + public static void TestItem_Set() + { + var arrList1 = new ArrayList(basicTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsReadOnly) + { + return; + } + + arrList2[0] = "Lone Ranger"; + Assert.Equal("Lone Ranger", arrList2[0]); + + arrList2[1] = null; + Assert.Null(arrList2[1]); + }); + } + + [Fact] + public static void TestItem_Set_Invalid() + { + var arrList1 = new ArrayList(basicTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsReadOnly) + { + return; + } + + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2[-1] = "Lone Ranger"); // Index < 0 + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2[arrList2.Count] = "Lone Ranger"); // Index > list.Count + }); + } + + [Fact] + public static void TestLastIndexOf_Basic() + { + var arrList1 = new ArrayList(duplicateContainingTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, (Action<ArrayList>)(arrList2 => + { + int ndx = arrList2.LastIndexOf("Batman"); + Assert.Equal((object)ArrayListTests.duplicateContainingTestData[ndx], arrList2[ndx]); + Assert.Equal(arrList1.Count - 1, ndx); + + ndx = arrList2.LastIndexOf(null); + Assert.Equal(-1, ndx); + })); + } + + [Fact] + public static void TestLastIndexOf_Basic_EmptyArrayList() + { + var arrList1 = new ArrayList(); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + Assert.Equal(-1, arrList2.IndexOf("Batman")); + }); + } + + [Fact] + public static void TestLastIndexOf_Basic_NonExistentObject() + { + var arrList1 = new ArrayList(duplicateContainingTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + Assert.Equal(-1, arrList2.IndexOf("hello")); + }); + } + + [Fact] + public static void TestLastIndexOf_Int() + { + var arrList1 = new ArrayList(duplicateAndNullContainingTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, (Action<ArrayList>)(arrList2 => + { + int ndx = arrList2.Count; + while ((ndx = arrList2.LastIndexOf("Batman", --ndx)) != -1) + { + Assert.Equal((object)ArrayListTests.duplicateAndNullContainingTestData[ndx], arrList2[ndx]); + } + + ndx = arrList2.IndexOf(null); + Assert.Equal(arrList2.Count - 1, ndx); + })); + } + + [Fact] + public static void TestLastIndexOf_Int_NonExistentObject() + { + ArrayList arrList1 = Helpers.CreateIntArrayList(10); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + int ndx = arrList1.IndexOf(100, 0); + Assert.Equal(-1, ndx); + }); + } + + [Fact] + public static void TestLastIndexOf_Int_ObjectOutOfRange() + { + ArrayList arrList1 = Helpers.CreateIntArrayList(10); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + int ndx = arrList1.IndexOf(0, 1); + Assert.Equal(-1, ndx); + }); + } + + [Fact] + public static void TestLastIndexOf_Int_Invalid() + { + ArrayList arrList1 = Helpers.CreateIntArrayList(10); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.LastIndexOf(0, -1)); // Index < 0 + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.LastIndexOf(0, arrList2.Count)); // Index >= list.Count + }); + } + + [Fact] + public static void TestLastIndexOf_Int_Int() + { + var arrList1 = new ArrayList(duplicateAndNullContainingTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, (Action<ArrayList>)(arrList2 => + { + int startIndex = arrList2.Count - 1; + int ndx = -1; + int tmpNdx = 0; + while (0 < startIndex && (ndx = arrList2.LastIndexOf("Batman", startIndex, startIndex + 1)) != -1) + { + Assert.True(ndx <= startIndex); + + Assert.Equal((object)ArrayListTests.duplicateAndNullContainingTestData[ndx], arrList2[ndx]); + + tmpNdx = arrList2.LastIndexOf("Batman", startIndex, startIndex - ndx + 1); + Assert.Equal(ndx, tmpNdx); + + tmpNdx = arrList2.LastIndexOf("Batman", startIndex, startIndex - ndx); + Assert.Equal(-1, tmpNdx); + + startIndex = ndx - 1; + } + + ndx = arrList2.LastIndexOf(null, arrList2.Count - 1, arrList2.Count); + Assert.NotEqual(-1, ndx); + Assert.Null(arrList2[ndx]); + })); + } + + [Fact] + public static void TestLastIndexOf_Int_Int_EmptyArrayList() + { + var arrList1 = new ArrayList(); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + Assert.Equal(-1, arrList2.LastIndexOf("hello", 0, 0)); + }); + } + + [Fact] + public static void TestLastIndexOf_Int_Int_NonExistentObject() + { + ArrayList arrList1 = Helpers.CreateIntArrayList(10); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + Assert.Equal(-1, arrList2.IndexOf(100, 0, arrList2.Count)); + }); + } + + [Fact] + public static void TestLastIndexOf_Int_Int_ObjectOutOfRange() + { + ArrayList arrList1 = Helpers.CreateIntArrayList(10); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + int ndx = arrList2.IndexOf(0, 1, arrList2.Count - 1); // Start index > object's index + Assert.Equal(-1, ndx); + + ndx = arrList2.IndexOf(10, 0, arrList2.Count - 2); // Start index + count < object's index + Assert.Equal(-1, ndx); + }); + } + + [Fact] + public static void TestLastIndexOf_Int_Int_Invalid() + { + ArrayList arrList1 = Helpers.CreateIntArrayList(10); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.LastIndexOf(0, -1, 2)); // Index < 0 + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.LastIndexOf(0, arrList2.Count, 2)); // Index >= list.Count + + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.LastIndexOf(0, 0, -1)); // Count < 0 + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.LastIndexOf(0, 0, arrList2.Count + 1)); // Count > list.Count + + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.LastIndexOf(0, 4, arrList2.Count - 4)); // Index + count > list.Count + }); + } + [Fact] + public static void TestReadOnly_ArrayList() + { + ArrayList arrList1 = Helpers.CreateIntArrayList(10); + ArrayList arrList2 = ArrayList.ReadOnly(arrList1); + + Assert.True(arrList2.IsFixedSize); + Assert.True(arrList2.IsReadOnly); + Assert.False(arrList2.IsSynchronized); + + Assert.Equal(arrList1.Count, arrList2.Count); + for (int i = 0; i < arrList1.Count; i++) + { + Assert.Equal(arrList1[i], arrList2[i]); + } + + // Remove an object from the original list and verify the object underneath has been cut + arrList1.RemoveAt(9); + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2[9]); + + // We cant remove, change or add to the readonly list + Assert.Throws<NotSupportedException>(() => arrList2.RemoveAt(0)); + Assert.Throws<NotSupportedException>(() => arrList2.Remove(5)); + Assert.Throws<NotSupportedException>(() => arrList2.RemoveRange(0, 1)); + Assert.Throws<NotSupportedException>(() => arrList2.Clear()); + Assert.Throws<NotSupportedException>(() => arrList2.Add(5)); + Assert.Throws<NotSupportedException>(() => arrList2.AddRange(new ArrayList())); + Assert.Throws<NotSupportedException>(() => arrList2.Insert(0, 5)); + Assert.Throws<NotSupportedException>(() => arrList2.InsertRange(0, new ArrayList())); + + Assert.Throws<NotSupportedException>(() => arrList2.Reverse()); + Assert.Throws<NotSupportedException>(() => arrList2.Sort()); + + Assert.Throws<NotSupportedException>(() => arrList2.TrimToSize()); + Assert.Throws<NotSupportedException>(() => arrList2.Capacity = 10); + + Assert.Throws<NotSupportedException>(() => arrList2[2] = 5); + Assert.Throws<NotSupportedException>(() => arrList2.SetRange(0, new ArrayList())); + + // We can get a readonly from this readonly + ArrayList arrList3 = ArrayList.ReadOnly(arrList2); + Assert.True(arrList2.IsReadOnly); + Assert.True(arrList3.IsReadOnly); + + // Verify we cant access remove, change or add to the readonly list + Assert.Throws<NotSupportedException>(() => arrList2.RemoveAt(0)); + } + + [Fact] + public static void TestReadOnly_SynchronizedArrayList() + { + ArrayList arrList = ArrayList.ReadOnly(ArrayList.Synchronized(new ArrayList())); + Assert.True(arrList.IsFixedSize); + Assert.True(arrList.IsReadOnly); + Assert.True(arrList.IsSynchronized); + } + + [Fact] + public static void TestReadOnly_ArrayList_Invalid() + { + Assert.Throws<ArgumentNullException>(() => ArrayList.ReadOnly(null)); // List is null + } + + [Fact] + public static void TestReadOnly_IList() + { + ArrayList arrList1 = Helpers.CreateIntArrayList(10); + IList iList = ArrayList.ReadOnly((IList)arrList1); + + Assert.True(iList.IsFixedSize); + Assert.True(iList.IsReadOnly); + Assert.False(iList.IsSynchronized); + + Assert.Equal(arrList1.Count, iList.Count); + for (int i = 0; i < iList.Count; i++) + { + Assert.Equal(arrList1[i], iList[i]); + } + } + + [Fact] + public static void TestReadOnly_SynchronizedIList() + { + IList iList = ArrayList.ReadOnly((IList)ArrayList.Synchronized(new ArrayList())); + Assert.True(iList.IsFixedSize); + Assert.True(iList.IsReadOnly); + Assert.True(iList.IsSynchronized); + } + + [Fact] + public static void TestReadOnly_IList_Contains() + { + ArrayList arrList = Helpers.CreateIntArrayList(10); + IList iList = ArrayList.ReadOnly((IList)arrList); + for (int i = 0; i < iList.Count; i++) + { + Assert.True(iList.Contains(i)); + } + } + + [Fact] + public static void TestReadOnly_IList_IndexOf() + { + ArrayList arrList = Helpers.CreateIntArrayList(10); + IList iList = ArrayList.ReadOnly((IList)arrList); + for (int i = 0; i < iList.Count; i++) + { + Assert.Equal(i, iList.IndexOf(i)); + } + } + + [Fact] + public static void TestReadOnly_IList_SyncRoot() + { + ArrayList arrList = Helpers.CreateIntArrayList(10); + IList iList = ArrayList.ReadOnly((IList)arrList); + + Assert.Equal(arrList.SyncRoot, iList.SyncRoot); + } + + [Fact] + public static void TestReadOnly_IList_CopyTo() + { + ArrayList arrList = Helpers.CreateIntArrayList(10); + IList iList = ArrayList.ReadOnly((IList)arrList); + + int index = 50; + var array = new object[iList.Count + index]; + iList.CopyTo(array, index); + + Assert.Equal(iList.Count + index, array.Length); + for (int i = index; i < arrList.Count; i++) + { + Assert.Equal(arrList[i], array[i]); + } + } + + [Fact] + public static void TestReadOnly_IList_GetEnumerator() + { + ArrayList arrList = Helpers.CreateIntArrayList(10); + IList iList = ArrayList.ReadOnly((IList)arrList); + + IEnumerator enumerator = iList.GetEnumerator(); + int count = 0; + + while (enumerator.MoveNext()) + { + Assert.Equal(iList[count], enumerator.Current); + count++; + } + Assert.Equal(iList.Count, count); + } + + [Fact] + public static void TestReadOnly_IList_GetEnumerator_Invalid() + { + ArrayList arrList = Helpers.CreateIntArrayList(10); + IList iList = ArrayList.ReadOnly((IList)arrList); + + IEnumerator enumerator = iList.GetEnumerator(); + // Index < 0 + Assert.Throws<InvalidOperationException>(() => enumerator.Current); + + // Index >= count + while (enumerator.MoveNext()) ; + Assert.False(enumerator.MoveNext()); + Assert.Throws<InvalidOperationException>(() => enumerator.Current); + + // Resetting should throw + enumerator.Reset(); + + enumerator.MoveNext(); + enumerator.Reset(); + Assert.Throws<InvalidOperationException>(() => enumerator.Current); + } + + [Fact] + public static void TestReadOnly_IList_NotSupportedMethods() + { + ArrayList arrList = Helpers.CreateIntArrayList(10); + IList iList = ArrayList.ReadOnly((IList)arrList); + + // Remove an object from the original list. Verify the object underneath has been cut + arrList.RemoveAt(9); + Assert.Throws<ArgumentOutOfRangeException>(() => iList[9]); + + // We cant remove or add to the fixed list + Assert.Throws<NotSupportedException>(() => iList.RemoveAt(0)); + Assert.Throws<NotSupportedException>(() => iList.Clear()); + Assert.Throws<NotSupportedException>(() => iList.Add(5)); + + Assert.Throws<NotSupportedException>(() => iList.Insert(0, 5)); + Assert.Throws<NotSupportedException>(() => iList.Remove(5)); + Assert.Throws<NotSupportedException>(() => iList.RemoveAt(5)); + + Assert.Throws<NotSupportedException>(() => iList[2] = 5); + } + + [Fact] + public static void TestReadOnly_IList_Invalid() + { + Assert.Throws<ArgumentNullException>(() => ArrayList.ReadOnly((IList)null)); // List is null + } + + [Fact] + public static void TestRemoveAt() + { + int start = 3; + int count = 15; + + var arrList1 = new ArrayList(basicTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsFixedSize) + { + return; + } + + for (int i = 0; i < count; ++i) + { + arrList2.RemoveAt(start); + } + + // Verify the items in the array. + for (int i = 0; i < removeAtExpectedTestData.Length; ++i) + { + Assert.Equal(removeAtExpectedTestData[i], arrList2[i]); + } + }); + } + + [Fact] + public static void TestRemoveAt_Invalid() + { + ArrayList arrList1 = Helpers.CreateIntArrayList(10); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsFixedSize) + { + return; + } + + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.RemoveAt(-1)); // Index < 0 + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.RemoveAt(arrList2.Count)); // Index >= list.Count + }); + } + + [Fact] + public static void TestRemoveRange() + { + var arrList1 = new ArrayList(basicTestData); + int start = 3; + int count = 15; + + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsFixedSize) + { + return; + } + + arrList2.RemoveRange(start, count); + + // Verify remove + for (int i = 0; i < removeRangeExpectedTestData.Length; ++i) + { + Assert.Equal(removeRangeExpectedTestData[i], arrList2[i]); + } + }); + } + + [Fact] + public static void TestRemoveRange_ZeroCount() + { + var arrList1 = new ArrayList(basicTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, (Action<ArrayList>)(arrList2 => + { + if (arrList2.IsFixedSize) + { + return; + } + + arrList2.RemoveRange(3, 0); + Assert.Equal((int)ArrayListTests.basicTestData.Length, arrList2.Count); + + // No change + for (int i = 0; i < ArrayListTests.basicTestData.Length; i++) + { + Assert.Equal((object)ArrayListTests.basicTestData[i], arrList2[i]); + } + })); + } + + [Fact] + public static void TestRemoveRange_Invalid() + { + ArrayList arrList1 = Helpers.CreateIntArrayList(10); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsFixedSize) + { + return; + } + + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.RemoveRange(-1, 1)); // Index < 0 + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.RemoveRange(1, -1)); // Count < 0 + + Assert.Throws<ArgumentException>(() => arrList2.RemoveRange(arrList2.Count, 1)); // Index > list.Count + Assert.Throws<ArgumentException>(() => arrList2.RemoveRange(0, arrList2.Count + 1)); // Count > list.Count + Assert.Throws<ArgumentException>(() => arrList2.RemoveRange(5, arrList2.Count - 1)); // Index + count > list.Count + }); + } + + [Fact] + public static void TestRemove() + { + var arrList = new ArrayList(nullContainingTestData); + + // Remove each element and make sure count decrements each time + for (int i = 0; i < nullContainingTestData.Length; i++) + { + arrList.Remove(nullContainingTestData[i]); + Assert.Equal(nullContainingTestData.Length - i - 1, arrList.Count); + } + } + + [Fact] + public static void TestRemove_Null() + { + var arrList = new ArrayList(); + + arrList.Add(null); + arrList.Add(arrList); + arrList.Add(null); + arrList.Remove(arrList); + arrList.Remove(null); + arrList.Remove(null); + + Assert.Equal(0, arrList.Count); + } + + [Fact] + public static void TestRemove_NonExistentObject() + { + var arrList = new ArrayList(); + arrList.Remove(null); + arrList.Remove(arrList); + } + + [Fact] + public static void TestRepeat() + { + ArrayList arrList = ArrayList.Repeat(5, 100); + for (int i = 0; i < arrList.Count; i++) + { + Assert.Equal(5, arrList[i]); + } + } + + [Fact] + public static void TestRepeat_Null() + { + ArrayList arrList = ArrayList.Repeat(null, 100); + for (int i = 0; i < arrList.Count; i++) + { + Assert.Null(arrList[i]); + } + } + + [Fact] + public static void TestRepeat_ZeroCount() + { + ArrayList arrList = ArrayList.Repeat(5, 0); + Assert.Equal(0, arrList.Count); + } + + [Fact] + public static void TestRepeat_Invalid() + { + Assert.Throws<ArgumentOutOfRangeException>(() => ArrayList.Repeat(5, -1)); // Count < 0 + } + + [Fact] + public static void TestReverse_Basic() + { + var arrList1 = new ArrayList(basicTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, (Action<ArrayList>)(arrList2 => + { + if (arrList2.IsReadOnly) + { + return; + } + + arrList2.Reverse(); + + Assert.Equal(arrList1.Count, arrList2.Count); + for (int i = 0; i < arrList2.Count; ++i) + { + Assert.Equal((object)ArrayListTests.basicTestData[i], arrList2[arrList2.Count - i - 1]); + } + })); + } + + [Fact] + public static void TestReverse_Basic_EmptyArrayList() + { + var arrList1 = new ArrayList(); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsReadOnly) + { + return; + } + + arrList2.Reverse(); + Assert.Equal(0, arrList2.Count); + }); + } + + [Fact] + public static void TestReverse_Basic_SingleObjectArrayList() + { + var arrList1 = new ArrayList(); + arrList1.Add(0); + + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsReadOnly) + { + return; + } + + arrList2.Reverse(); + + Assert.Equal(0, arrList2[0]); + Assert.Equal(1, arrList2.Count); + }); + } + + [Fact] + public static void TestReverse_Int_Int() + { + var arrList1 = new ArrayList(basicTestData); + int start = 5; + int count = 4; + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsReadOnly) + { + return; + } + + arrList2.Reverse(start, count); + + Assert.Equal(arrList1.Count, arrList2.Count); + for (int i = 0; i < arrList2.Count; i++) + { + Assert.Equal(reverseExpectedTestData[i], arrList2[i]); + } + }); + } + + [Fact] + public static void TestReverse_Int_Int_ZeroCount() + { + var arrList1 = new ArrayList(basicTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, (Action<ArrayList>)(arrList2 => + { + if (arrList2.IsReadOnly) + { + return; + } + + arrList2.Reverse(0, 0); + + // No change + Assert.Equal(arrList1.Count, arrList2.Count); + for (int i = 0; i < arrList2.Count; i++) + { + Assert.Equal((object)ArrayListTests.basicTestData[i], arrList2[i]); + } + })); + } + + [Fact] + public static void TestReverse_Int_Int_Invalid() + { + ArrayList arrList1 = Helpers.CreateIntArrayList(10); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsReadOnly) + { + return; + } + + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.Reverse(-1, arrList2.Count)); // Index < 0 + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.Reverse(0, -1)); // Count < 0 + Assert.Throws<ArgumentException>(() => arrList2.Reverse(1000, arrList2.Count)); // Index is too big + }); + } + + [Fact] + public static void TestSort_Basic() + { + var arrList1 = new ArrayList(sortTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsReadOnly) + { + return; + } + + arrList2.Sort(); + for (int i = 0; i < arrList2.Count; i++) + { + Assert.Equal(sortAscendingExpectedTestData[i], arrList2[i]); + } + }); + } + + [Fact] + public static void TestSort_Basic_EmptyArrayList() + { + var arrList1 = new ArrayList(); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsReadOnly) + { + return; + } + + arrList2.Sort(); + Assert.Equal(0, arrList2.Count); + }); + } + + [Fact] + public static void TestSort_Basic_SingleObjectArrayList() + { + var arrList1 = new ArrayList(); + arrList1.Add(1); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsReadOnly) + { + return; + } + + arrList2.Sort(); + Assert.Equal(1, arrList2.Count); + }); + } + + [Fact] + public static void TestSort_IComparer() + { + var arrList1 = new ArrayList(sortTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsReadOnly) + { + return; + } + + arrList2.Sort(new AscendingComparer()); + for (int i = 0; i < arrList2.Count; i++) + { + Assert.Equal(sortAscendingExpectedTestData[i], arrList2[i]); + } + + arrList2.Sort(new DescendingComparer()); + for (int i = 0; i < arrList2.Count; i++) + { + Assert.Equal(sortDescendingExpectedTestData[i], arrList2[i]); + } + }); + } + + [Fact] + public static void TestSort_IComparer_Null() + { + var arrList1 = new ArrayList(sortTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsReadOnly) + { + return; + } + + arrList2.Sort(null); + for (int i = 0; i < arrList2.Count; i++) + { + Assert.Equal(sortAscendingExpectedTestData[i], arrList2[i]); + } + }); + } + + [Fact] + public static void TestSort_Int_Int_IComparer() + { + var arrList1 = new ArrayList(sortTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsReadOnly) + { + return; + } + + arrList2.Sort(3, 5, new AscendingComparer()); + for (int i = 0; i < arrList2.Count; i++) + { + Assert.Equal(sortRangeAscendingExpectedTestData[i], arrList2[i]); + } + }); + } + + [Fact] + public static void TestSort_Int_Int_IComparer_Invalid() + { + var arrList1 = new ArrayList(sortTestData); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsReadOnly) + { + return; + } + + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.Sort(-1, arrList2.Count, null)); // Index < 0 + Assert.Throws<ArgumentOutOfRangeException>(() => arrList2.Sort(0, -1, null)); // Count < 0 + + Assert.Throws<ArgumentException>(() => arrList2.Sort(arrList2.Count, arrList2.Count, null)); // Index >= list.Count + Assert.Throws<ArgumentException>(() => arrList2.Sort(0, arrList2.Count + 1, null)); // Count = list.Count + }); + } + + [Fact] + public static void TestSort_MultipleDataTypes_ThrowsInvalidOperationException() + { + var arrList1 = new ArrayList(); + arrList1.Add((short)1); + arrList1.Add(1); + arrList1.Add((long)1); + arrList1.Add((ushort)1); + arrList1.Add((uint)1); + arrList1.Add((ulong)1); + + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + if (arrList2.IsReadOnly) + { + return; + } + + Assert.Throws<InvalidOperationException>(() => arrList2.Sort()); + }); + } + + [Fact] + public static void TestSynchronized_ArrayList() + { + ArrayList arrList = ArrayList.Synchronized(new ArrayList()); + Assert.False(arrList.IsFixedSize); + Assert.False(arrList.IsReadOnly); + Assert.True(arrList.IsSynchronized); + } + + [Fact] + public static void TestSynchronized_FixedSizeArrayList() + { + ArrayList arrList = ArrayList.Synchronized(ArrayList.FixedSize(new ArrayList())); + Assert.True(arrList.IsFixedSize); + Assert.False(arrList.IsReadOnly); + Assert.True(arrList.IsSynchronized); + } + + [Fact] + public static void TestSynchronized_ReadOnlyArrayList() + { + ArrayList arrList = ArrayList.Synchronized(ArrayList.ReadOnly(new ArrayList())); + Assert.True(arrList.IsFixedSize); + Assert.True(arrList.IsReadOnly); + Assert.True(arrList.IsSynchronized); + } + + [Fact] + public static void TestSynchronized_ArrayList_Invalid() + { + Assert.Throws<ArgumentNullException>(() => ArrayList.Synchronized(null)); // List is null + } + + [Fact] + public static void TestSynchronized_IList() + { + IList iList = ArrayList.Synchronized((IList)new ArrayList()); + Assert.False(iList.IsFixedSize); + Assert.False(iList.IsReadOnly); + Assert.True(iList.IsSynchronized); + } + + [Fact] + public static void TestSynchronized_FixedSizeIList() + { + IList iList = ArrayList.Synchronized((IList)ArrayList.FixedSize(new ArrayList())); + Assert.True(iList.IsFixedSize); + Assert.False(iList.IsReadOnly); + Assert.True(iList.IsSynchronized); + } + + [Fact] + public static void TestSynchronized_ReadOnlyIList() + { + IList iList = ArrayList.Synchronized((IList)ArrayList.ReadOnly(new ArrayList())); + Assert.True(iList.IsFixedSize); + Assert.True(iList.IsReadOnly); + Assert.True(iList.IsSynchronized); + } + + [Fact] + public static void TestSynchronized_IList_Indexer() + { + ArrayList arrList = Helpers.CreateIntArrayList(10); + IList iList = ArrayList.Synchronized((IList)arrList); + + for (int i = 0; i < iList.Count; i++) + { + string newValue = "Hello_" + i; + iList[i] = newValue; + Assert.Equal(newValue, iList[i]); + } + } + + [Fact] + public static void TestSynchronized_IList_Add_Remove() + { + var arrList = new ArrayList(); + IList iList = ArrayList.Synchronized((IList)arrList); + for (int i = 0; i < 10; i++) + { + iList.Add(i); + } + + Assert.Equal(10, iList.Count); + Assert.Equal(10, arrList.Count); + + for (int i = 0; i < 10; i++) + { + iList.Remove(i); + } + Assert.Equal(0, iList.Count); + } + + [Fact] + public static void TestSynchronized_IList_InsertRemoveAt() + { + var arrList = new ArrayList(); + IList iList = ArrayList.Synchronized((IList)arrList); + for (int i = 0; i < 10; i++) + { + iList.Insert(0, i); + } + + Assert.Equal(10, iList.Count); + Assert.Equal(10, arrList.Count); + + for (int i = 0; i < 10; i++) + { + iList.RemoveAt(0); + } + Assert.Equal(0, iList.Count); + } + + [Fact] + public static void TestSynchronized_IList_Clear() + { + ArrayList arrList = Helpers.CreateIntArrayList(10); + IList iList = ArrayList.Synchronized((IList)arrList); + + iList.Clear(); + Assert.Equal(0, iList.Count); + } + + [Fact] + public static void TestSynchronized_IList_Contains() + { + ArrayList arrList = Helpers.CreateIntArrayList(10); + IList iList = ArrayList.Synchronized((IList)arrList); + for (int i = 0; i < 10; i++) + { + Assert.True(iList.Contains(i)); + } + } + + [Fact] + public static void TestSynchronized_IList_IndexOf() + { + ArrayList arrList = Helpers.CreateIntArrayList(10); + IList iList = ArrayList.Synchronized((IList)arrList); + for (int i = 0; i < 10; i++) + { + Assert.Equal(i, iList.IndexOf(i)); + } + } + + [Fact] + public static void TestSynchronized_IList_SyncRoot() + { + ArrayList arrList = Helpers.CreateIntArrayList(10); + IList iList = ArrayList.Synchronized((IList)arrList); + + Assert.Equal(arrList.SyncRoot, iList.SyncRoot); + } + + [Fact] + public static void TestSynchronized_IList_CopyTo() + { + ArrayList arrList = Helpers.CreateIntArrayList(10); + IList iList = ArrayList.Synchronized((IList)arrList); + + int index = 50; + var array = new object[iList.Count + index]; + iList.CopyTo(array, index); + + Assert.Equal(iList.Count + index, array.Length); + for (int i = index; i < arrList.Count; i++) + { + Assert.Equal(arrList[i], array[i]); + } + } + + [Fact] + public static void TestSynchronized_IList_GetEnumerator() + { + ArrayList arrList = Helpers.CreateIntArrayList(10); + IList iList = ArrayList.Synchronized((IList)arrList); + + IEnumerator enumerator = iList.GetEnumerator(); + int count = 0; + + while (enumerator.MoveNext()) + { + Assert.Equal(iList[count], enumerator.Current); + count++; + } + Assert.Equal(iList.Count, count); + } + + [Fact] + public static void TestSynchronizedIList_GetEnumerator_Invalid() + { + ArrayList arrList = Helpers.CreateIntArrayList(10); + IList iList = ArrayList.Synchronized((IList)arrList); + + IEnumerator enumerator = iList.GetEnumerator(); + // Index < 0 + Assert.Throws<InvalidOperationException>(() => enumerator.Current); + + // Index >= count + while (enumerator.MoveNext()) ; + Assert.False(enumerator.MoveNext()); + Assert.Throws<InvalidOperationException>(() => enumerator.Current); + + // Resetting should throw + enumerator.Reset(); + + enumerator.MoveNext(); + enumerator.Reset(); + Assert.Throws<InvalidOperationException>(() => enumerator.Current); + } + + [Fact] + public static void TestSynchronized_IList_Invalid() + { + Assert.Throws<ArgumentNullException>(() => ArrayList.Synchronized((IList)null)); // List is null + } + + [Fact] + public static void TestToArray() + { + // ToArray returns an array of this. We will not extensively test this method as + // this is a thin wrapper on Array.Copy which is extensively tested + ArrayList arrList1 = Helpers.CreateIntArrayList(10); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + object[] arr1 = arrList2.ToArray(); + Array arr2 = arrList2.ToArray(typeof(int)); + + for (int i = 0; i < 10; i++) + { + Assert.Equal(i, arr1[i]); + Assert.Equal(i, arr2.GetValue(i)); + } + }); + } + + [Fact] + public static void TestToArray_EmptyArrayList() + { + var arrList1 = new ArrayList(); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + object[] arr1 = arrList2.ToArray(); + Assert.Equal(0, arr1.Length); + + Array arr2 = arrList2.ToArray(typeof(object)); + Assert.Equal(0, arr2.Length); + }); + } + + [Fact] + public static void TestToArray_Invalid() + { + + ArrayList arrList1 = Helpers.CreateIntArrayList(100); + Helpers.PerformActionOnAllArrayListWrappers(arrList1, arrList2 => + { + // This should be covered in Array.Copy, but lets do it for completion's sake + Assert.Throws<InvalidCastException>(() => arrList2.ToArray(typeof(string))); // Objects stored are not strings + Assert.Throws<ArgumentNullException>(() => arrList2.ToArray(null)); // Type is null + }); + } + + [Fact] + public static void TestTrimToSize() + { + var arrList = new ArrayList(basicTestData); + arrList.Capacity = 2 * arrList.Count; + Assert.True(arrList.Capacity > arrList.Count); + + arrList.TrimToSize(); + Assert.Equal(arrList.Count, arrList.Capacity); + + // Test on Adapter + arrList = ArrayList.Adapter(arrList); + arrList.TrimToSize(); + + // Test on Synchronized + arrList = ArrayList.Synchronized(new ArrayList(basicTestData)); + arrList.TrimToSize(); + Assert.Equal(arrList.Count, arrList.Capacity); + } + } + + public class ArrayList_SyncRootTests + { + private ArrayList _arrDaughter; + private ArrayList _arrGrandDaughter; + + [Fact] + public void TestGetSyncRoot() + { + int iNumberOfElements = 100; + int iValue; + bool fDescending; + + int iNumberOfWorkers = 10; + + // Testing SyncRoot is not as simple as its implementation looks like. This is the working + // scenrio we have in mind. + // 1) Create your Down to earth mother ArrayList + // 2) Get a Fixed wrapper from it + // 3) Get a Synchronized wrapper from 2) + // 4) Get a synchronized wrapper of the mother from 1) + // 5) all of these should SyncRoot to the mother earth + + ArrayList arrMother1 = Helpers.CreateIntArrayList(iNumberOfElements); + Helpers.PerformActionOnAllArrayListWrappers(arrMother1, arrMother2 => + { + ArrayList arrSon1 = ArrayList.FixedSize(arrMother2); + ArrayList arrSon2 = ArrayList.ReadOnly(arrMother2); + + _arrGrandDaughter = ArrayList.Synchronized(arrMother2); + _arrDaughter = ArrayList.Synchronized(arrMother2); + + Assert.False(arrMother2.SyncRoot is ArrayList); + Assert.False(arrSon1.SyncRoot is ArrayList); + Assert.False(arrSon2.SyncRoot is ArrayList); + Assert.False(_arrDaughter.SyncRoot is ArrayList); + Assert.Equal(arrSon1.SyncRoot, arrMother2.SyncRoot); + Assert.False(_arrGrandDaughter.SyncRoot is ArrayList); + + arrMother2 = new ArrayList(); + for (int i = 0; i < iNumberOfElements; i++) + { + arrMother2.Add(i); + } + + arrSon1 = ArrayList.FixedSize(arrMother2); + arrSon2 = ArrayList.ReadOnly(arrMother2); + _arrGrandDaughter = ArrayList.Synchronized(arrSon1); + _arrDaughter = ArrayList.Synchronized(arrMother2); + + // We are going to rumble with the ArrayLists with 2 threads + var workers = new Task[iNumberOfWorkers]; + var action1 = new Action(SortElements); + var action2 = new Action(ReverseElements); + for (int iThreads = 0; iThreads < iNumberOfWorkers; iThreads += 2) + { + workers[iThreads] = Task.Run(action1); + workers[iThreads + 1] = Task.Run(action2); + } + + Task.WaitAll(workers); + + // Checking time + // Now lets see how this is done. + // Reverse and sort - ascending more likely + // Sort followed up Reverse - descending + fDescending = false; + if (((int)arrMother2[0]).CompareTo((int)arrMother2[1]) > 0) + fDescending = true; + + iValue = (int)arrMother2[0]; + for (int i = 1; i < iNumberOfElements; i++) + { + if (fDescending) + { + Assert.True(iValue.CompareTo((int)arrMother2[i]) > 0); + } + else + { + Assert.True(iValue.CompareTo((int)arrMother2[i]) < 0); + } + iValue = (int)arrMother2[i]; + } + }); + } + + private void SortElements() + { + _arrGrandDaughter.Sort(); + } + + private void ReverseElements() + { + _arrDaughter.Reverse(); + } + } + + public class ArrayList_SynchronizedTests + { + private IList _iList; + private int _iNumberOfElements = 10; + private const string _prefix = "String_"; + + public ArrayList _arrList; + public Hashtable _hash; // This will verify that threads will only add elements the num of times they are specified to + + [Fact] + public void TestSynchronized_ArrayList() + { + // Make 40 threads which add strHeroes to an ArrayList + // the outcome is that the length of the ArrayList should be the same size as the strHeroes array + _arrList = ArrayList.Synchronized(new ArrayList()); + _hash = Hashtable.Synchronized(new Hashtable()); + + // Initialize the threads + var workers = new Task[7]; + for (int i = 0; i < workers.Length; i++) + { + string name = "ThreadID " + i.ToString(); + Action delegStartMethod = () => AddElems(name); + workers[i] = Task.Run(delegStartMethod); + } + + Task.WaitAll(workers); + + Assert.Equal(workers.Length * ArrayListTests.synchronizedTestData.Length, _arrList.Count); + } + + [Fact] + public void TestSynchronized_IList() + { + int iNumberOfWorkers = 10; + + _iList = ArrayList.Synchronized((IList)new ArrayList()); + + var workers = new Task[10]; + var action = new Action(AddElements); + + for (int i = 0; i < workers.Length; i++) + { + workers[i] = Task.Run(action); + } + + Task.WaitAll(workers); + + // Checking time + Assert.Equal(_iNumberOfElements * iNumberOfWorkers, _iList.Count); + + for (int i = 0; i < _iNumberOfElements; i++) + { + int iNumberOfTimes = 0; + for (int j = 0; j < _iList.Count; j++) + { + if (((string)_iList[j]).Equals(_prefix + i)) + iNumberOfTimes++; + } + + Assert.Equal(iNumberOfTimes, iNumberOfWorkers); + } + + workers = new Task[iNumberOfWorkers]; + action = new Action(RemoveElements); + + for (int i = 0; i < workers.Length; i++) + { + workers[i] = Task.Run(action); + } + + Task.WaitAll(workers); + + Assert.Equal(0, _iList.Count); + } + + public void AddElems(string currThreadName) + { + int iNumTimesThreadUsed = 0; + + for (int i = 0; i < ArrayListTests.synchronizedTestData.Length; i++) + { + // To test that we only use the right threads the right number of times keep track with the hashtable + // how many times we use this thread + try + { + _hash.Add(currThreadName, null); + // this test assumes ADD will throw for dup elements + } + catch (ArgumentException) + { + iNumTimesThreadUsed++; + } + + Assert.NotNull(_arrList); + Assert.True(_arrList.IsSynchronized); + + _arrList.Add(ArrayListTests.synchronizedTestData[i]); + } + + Assert.Equal(ArrayListTests.synchronizedTestData.Length - 1, iNumTimesThreadUsed); + } + + private void AddElements() + { + for (int i = 0; i < _iNumberOfElements; i++) + { + _iList.Add(_prefix + i); + } + } + + private void RemoveElements() + { + for (int i = 0; i < _iNumberOfElements; i++) + { + _iList.Remove(_prefix + i); + } + } + } +} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/BinarySearchTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/BinarySearchTests.cs deleted file mode 100644 index d38878ab95..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/BinarySearchTests.cs +++ /dev/null @@ -1,284 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_BinarySearchTests : IComparer - { - #region "Test Data - Keep the data close to tests so it can vary independently from other tests" - - static string[] strHeroes = - { - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - "Huntress", - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - "Superman", - "Thor", - "Wildcat", - "Wonder Woman", - }; - - static string[] strFindHeroes = - { - "Batman", - "Superman", - "SpiderMan", - "Wonder Woman", - "Green Lantern", - "Flash", - "Steel" - }; - - #endregion - - [Fact] - public void TestStandardArrayList() - { - // - // Construct array list. - // - ArrayList list = new ArrayList(); - // Add items to the lists. - for (int ii = 0; ii < strHeroes.Length; ++ii) - list.Add(strHeroes[ii]); - - // Verify items added to list. - Assert.Equal(strHeroes.Length, list.Count); - - // - // [] Use BinarySearch to find selected items. - // - // Search and verify selected items. - for (int ii = 0; ii < strFindHeroes.Length; ++ii) - { - // Locate item. - int ndx = list.BinarySearch(strFindHeroes[ii]); - Assert.True(ndx >= 0); - - // Verify item. - Assert.Equal(0, strHeroes[ndx].CompareTo(strFindHeroes[ii])); - } - - // Return Value; - // The zero-based index of the value in the sorted ArrayList, if value is found; otherwise, a negative number, - // which is the bitwise complement of the index of the next element. - - list = new ArrayList(); - for (int i = 0; i < 100; i++) - list.Add(i); - list.Sort(); - - Assert.Equal(100, ~list.BinarySearch(150)); - - //[]null - should return -1 - Assert.Equal(-1, list.BinarySearch(null)); - - //[]we can add null as a value and then search it!!! - list.Add(null); - list.Sort(); - Assert.Equal(0, list.BinarySearch(null)); - - //[]duplicate values, always return the first one - list = new ArrayList(); - for (int i = 0; i < 100; i++) - list.Add(5); - - list.Sort(); - //remember this is BinarySeearch - Assert.Equal(49, list.BinarySearch(5)); - } - - [Fact] - public void TestArrayListWrappers() - { - // - // Construct array list. - // - ArrayList arrList = new ArrayList(); - - // Add items to the lists. - for (int ii = 0; ii < strHeroes.Length; ++ii) - { - arrList.Add(strHeroes[ii]); - } - - // Verify items added to list. - Assert.Equal(strHeroes.Length, arrList.Count); - - //Adapter, GetRange, Synchronized, ReadOnly returns a slightly different version of - //BinarySearch, Following variable cotains each one of these types of array lists - - ArrayList[] arrayListTypes = { - arrList, - ArrayList.Adapter(arrList), - ArrayList.FixedSize(arrList), - arrList.GetRange(0, arrList.Count), - ArrayList.ReadOnly(arrList), - ArrayList.Synchronized(arrList)}; - - int ndx = 0; - foreach (ArrayList arrayListType in arrayListTypes) - { - arrList = arrayListType; - - // - // [] Use BinarySearch to find selected items. - // - // Search and verify selected items. - for (int ii = 0; ii < strFindHeroes.Length; ++ii) - { - // Locate item. - ndx = arrList.BinarySearch(0, arrList.Count, strFindHeroes[ii], this); - Assert.True(ndx >= 0); - - // Verify item. - Assert.Equal(0, strHeroes[ndx].CompareTo(strFindHeroes[ii])); - } - - // - // [] Locate item in list using null comparer. - // - ndx = arrList.BinarySearch(0, arrList.Count, "Batman", null); - Assert.Equal(2, ndx); - - // - // [] Locate insertion index of new list item. - // - // Append the list. - ndx = arrList.BinarySearch(0, arrList.Count, "Batgirl", this); - Assert.Equal(2, ~ndx); - - // - // [] Bogus Arguments - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.BinarySearch(-100, 1000, arrList.Count, this)); - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.BinarySearch(-100, 1000, "Batman", this)); - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.BinarySearch(-1, arrList.Count, "Batman", this)); - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.BinarySearch(0, -1, "Batman", this)); - - Assert.Throws<ArgumentException>(() => arrList.BinarySearch(1, arrList.Count, "Batman", this)); - Assert.Throws<ArgumentException>(() => arrList.BinarySearch(3, arrList.Count - 2, "Batman", this)); - } - } - - [Fact] - public void TestCustomComparer() - { - ArrayList list = null; - list = new ArrayList(); - - // Add items to the lists. - for (int ii = 0; ii < strHeroes.Length; ++ii) - list.Add(strHeroes[ii]); - - // Verify items added to list. - Assert.Equal(strHeroes.Length, list.Count); - - // - // [] Use BinarySearch to find selected items. - // - // Search and verify selected items. - for (int ii = 0; ii < strFindHeroes.Length; ++ii) - { - // Locate item. - int ndx = list.BinarySearch(strFindHeroes[ii], new ArrayList_BinarySearchTests()); - Assert.True(ndx < strHeroes.Length); - - // Verify item. - Assert.Equal(0, strHeroes[ndx].CompareTo(strFindHeroes[ii])); - } - - //Return Value; - //The zero-based index of the value in the sorted ArrayList, if value is found; otherwise, a negative number, which is the - //bitwise complement of the index of the next element. - - list = new ArrayList(); - for (int i = 0; i < 100; i++) - list.Add(i); - list.Sort(); - - Assert.Equal(100, ~list.BinarySearch(150, new ArrayList_BinarySearchTests())); - - //[]null - should return -1 - Assert.Equal(-1, list.BinarySearch(null, new ArrayList_BinarySearchTests())); - - //[]we can add null as a value and then search it!!! - - list.Add(null); - list.Sort(); - Assert.Equal(0, list.BinarySearch(null, new CompareWithNullEnabled())); - - //[]duplicate values, always return the first one - - list = new ArrayList(); - for (int i = 0; i < 100; i++) - list.Add(5); - list.Sort(); - - //remember this is BinarySeearch - Assert.Equal(49, list.BinarySearch(5, new ArrayList_BinarySearchTests())); - - //[]IC as null - list = new ArrayList(); - for (int i = 0; i < 100; i++) - list.Add(5); - list.Sort(); - //remember this is BinarySeearch - Assert.Equal(49, list.BinarySearch(5, null)); - } - - public virtual int Compare(object x, object y) - { - if (x is string) - { - return ((string)x).CompareTo((string)y); - } - - var comparer = new Comparer(System.Globalization.CultureInfo.InvariantCulture); - if (x is int || y is string) - { - return comparer.Compare(x, y); - } - - return -1; - } - } - - class CompareWithNullEnabled : IComparer - { - public int Compare(Object a, Object b) - { - if (a == b) return 0; - if (a == null) return -1; - if (b == null) return 1; - - IComparable ia = a as IComparable; - if (ia != null) - return ia.CompareTo(b); - - IComparable ib = b as IComparable; - if (ib != null) - return -ib.CompareTo(a); - - throw new ArgumentException("Wrong stuff"); - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/ClearTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/ClearTests.cs deleted file mode 100644 index ca1cffd1d3..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/ClearTests.cs +++ /dev/null @@ -1,58 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_ClearTests - { - [Fact] - public void TestClearBasic() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - string[] strHeroes = new string[] - { - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - null, - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - null, - "Thor", - "Wildcat", - null - }; - - //[] Clear list with elements - - // Construct ArrayList. - ArrayList arrList = new ArrayList(strHeroes); - arrList.Clear(); - - Assert.Equal(0, arrList.Count); - - //[] Clear list with no elements - // Construct ArrayList. - arrList = new ArrayList(); - arrList.Clear(); - Assert.Equal(0, arrList.Count); - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/CloneTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/CloneTests.cs deleted file mode 100644 index 0d313fe8b8..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/CloneTests.cs +++ /dev/null @@ -1,96 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_CloneTests - { - [Fact] - public void TestCloneBasic() - { - ArrayList alst1; - ArrayList alst2; - - String strValue; - Object oValue; - - //[] Vanila test case - Clone should exactly replicate a collection to another object reference - //afterwards these 2 should not hold the same object references - - alst1 = new ArrayList(); - for (int i = 0; i < 10; i++) - { - strValue = "String_" + i; - alst1.Add(strValue); - } - - alst2 = (ArrayList)alst1.Clone(); - - for (int i = 0; i < 10; i++) - { - strValue = "String_" + i; - Assert.Equal(strValue, (String)alst2[i]); - } - - //now we remove an object from the original list - alst1.RemoveAt(9); - Assert.Throws<ArgumentOutOfRangeException>(() => { oValue = alst1[9]; }); - - strValue = "String_" + 9; - Assert.Equal(strValue, (String)alst2[9]); - - //[]now we try other test cases - //are all the 'other' properties of the arraylist the same? - - alst1 = new ArrayList(1000); - alst2 = (ArrayList)alst1.Clone(); - - // Capacity is not expected to be the same - Assert.NotEqual(alst1.Capacity, alst2.Capacity); - Assert.Equal(alst1.Count, alst2.Count); - Assert.Equal(alst1.IsReadOnly, alst2.IsReadOnly); - Assert.Equal(alst1.IsSynchronized, alst2.IsSynchronized); - - //[]Clone is a shallow copy, so the objects of the objets reference should be the same - alst1 = new ArrayList(); - for (int i = 0; i < 10; i++) - { - alst1.Add(new Foo()); - } - - alst2 = (ArrayList)alst1.Clone(); - strValue = "Hello World"; - for (int i = 0; i < 10; i++) - { - Assert.Equal(strValue, ((Foo)alst2[i]).strValue); - } - - strValue = "Good Bye"; - ((Foo)alst1[0]).strValue = strValue; - Assert.Equal(strValue, ((Foo)alst1[0]).strValue); - Assert.Equal(strValue, ((Foo)alst2[0]).strValue); - - //[]if we change the object, of course, the previous should not happen - alst2[0] = new Foo(); - - strValue = "Good Bye"; - Assert.Equal(strValue, ((Foo)alst1[0]).strValue); - - strValue = "Hello World"; - Assert.Equal(strValue, ((Foo)alst2[0]).strValue); - } - - internal class Foo - { - internal String strValue; - - internal Foo() - { - strValue = "Hello World"; - } - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/ContainsTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/ContainsTests.cs deleted file mode 100644 index 319a7e2142..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/ContainsTests.cs +++ /dev/null @@ -1,96 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_ContainsTests - { - [Fact] - public void TestArrayListWrappers() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - - string[] strHeroes = - { - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - null, - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - null, - "Thor", - "Wildcat", - null - }; - - //[] Vanila Contains - - // Construct ArrayList. - arrList = new ArrayList(strHeroes); - - //Adapter, GetRange, Synchronized, ReadOnly returns a slightly different version of - //BinarySearch, Following variable cotains each one of these types of array lists - - ArrayList[] arrayListTypes = { - (ArrayList)arrList.Clone(), - (ArrayList)ArrayList.Adapter(arrList).Clone(), - (ArrayList)ArrayList.FixedSize(arrList).Clone(), - (ArrayList)arrList.GetRange(0, arrList.Count).Clone(), - (ArrayList)ArrayList.Synchronized(arrList).Clone()}; - - foreach (ArrayList arrayListType in arrayListTypes) - { - arrList = arrayListType; - for (int i = 0; i < strHeroes.Length; i++) - { - Assert.True(arrList.Contains(strHeroes[i]), "Error, Contains returns false but shour return true at position " + i.ToString()); - } - - if (!arrList.IsFixedSize) - { - //[] Normal Contains which expects false - for (int i = 0; i < strHeroes.Length; i++) - { - // remove element, if element is in 2 times make sure we remove it completely. - for (int j = 0; j < strHeroes.Length; j++) - { - arrList.Remove(strHeroes[i]); - } - - Assert.False(arrList.Contains(strHeroes[i]), "Error, Contains returns true but should return false at position " + i.ToString()); - } - } - - if (!arrList.IsFixedSize) - { - //[] Normal Contains on empty list - arrList.Clear(); - - for (int i = 0; i < strHeroes.Length; i++) - { - Assert.False(arrList.Contains(strHeroes[i]), "Error, Contains returns true but should return false at position " + i.ToString()); - } - } - } - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/CopyToTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/CopyToTests.cs deleted file mode 100644 index b6c914d1a6..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/CopyToTests.cs +++ /dev/null @@ -1,348 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_CopyToTests - { - #region "Test Data - Keep the data close to tests so it can vary independently from other tests" - - static string[] strHeroes = - { - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - null, - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - null, - "Thor", - "Wildcat", - null - }; - - #endregion - - [Fact] - public void TestCopyToBasic() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - String[] arrCopy = null; - - // - // [] CopyTo an array normal - // - // [] Normal Copy Test 1 - - arrList = new ArrayList(strHeroes); - arrCopy = new String[strHeroes.Length]; - arrList.CopyTo(arrCopy); - - for (int i = 0; i < arrCopy.Length; i++) - { - Assert.Equal(strHeroes[i], arrCopy[i]); - } - - //[] Normal Copy Test 2 - copy 0 elements - // Construct ArrayList. - arrList = new ArrayList(); - arrList.Add(null); - arrList.Add(arrList); - arrList.Add(null); - arrList.Remove(null); - arrList.Remove(null); - arrList.Remove(arrList); - - Assert.Equal(0, arrList.Count); - - arrCopy = new String[strHeroes.Length]; - - // put some elements in arrCopy that should not be overriden - for (int i = 0; i < strHeroes.Length; i++) - { - arrCopy[i] = strHeroes[i]; - } - - //copying 0 elements into arrCopy - arrList.CopyTo(arrCopy); - - // check to make sure sentinals stay the same - for (int i = 0; i < arrCopy.Length; i++) - { - Assert.Equal(strHeroes[i], arrCopy[i]); - } - - //we'll make sure by copying only 0 - arrList = new ArrayList(); - arrCopy = new String[0]; - - //copying 0 elements into arrCopy - arrList.CopyTo(arrCopy); - Assert.Equal(0, arrCopy.Length); - - //[] Copy so that exception should be thrown - Assert.Throws<ArgumentNullException>(() => - { - // Construct ArrayList. - arrList = new ArrayList(); - arrCopy = null; - - //copying 0 elements into arrCopy, into INVALID index of arrCopy - arrList.CopyTo(arrCopy); - }); - } - - [Fact] - public void TestArrayListWrappers() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - String[] arrCopy = null; - - arrList = new ArrayList(strHeroes); - - //Adapter, GetRange, Synchronized, ReadOnly returns a slightly different version of - //BinarySearch, Following variable cotains each one of these types of array lists - - ArrayList[] arrayListTypes = { - (ArrayList)arrList.Clone(), - (ArrayList)ArrayList.Adapter(arrList).Clone(), - (ArrayList)arrList.GetRange(0, arrList.Count).Clone(), - (ArrayList)ArrayList.Synchronized(arrList).Clone()}; - - foreach (ArrayList arrayListType in arrayListTypes) - { - arrList = arrayListType; - // - // [] CopyTo an array normal - // - arrCopy = new String[strHeroes.Length]; - arrList.CopyTo(arrCopy, 0); - - for (int i = 0; i < arrCopy.Length; i++) - { - Assert.Equal<string>(strHeroes[i], arrCopy[i]); - } - - //[] Normal Copy Test 2 - copy 0 elements - arrList.Clear(); - arrList.Add(null); - arrList.Add(arrList); - arrList.Add(null); - arrList.Remove(null); - arrList.Remove(null); - arrList.Remove(arrList); - - Assert.Equal(0, arrList.Count); - - arrCopy = new String[strHeroes.Length]; - // put some elements in arrCopy that should not be overriden - for (int i = 0; i < strHeroes.Length; i++) - { - arrCopy[i] = strHeroes[i]; - } - - //copying 0 elements into arrCopy - arrList.CopyTo(arrCopy, 1); - - // check to make sure sentinals stay the same - for (int i = 0; i < arrCopy.Length; i++) - { - Assert.Equal<string>(strHeroes[i], arrCopy[i]); - } - - //[] Normal Copy Test 3 - copy 0 elements from the end - arrList.Clear(); - Assert.Equal(0, arrList.Count); - - arrCopy = new String[strHeroes.Length]; - - // put some elements in arrCopy that should not be overriden - for (int i = 0; i < strHeroes.Length; i++) - { - arrCopy[i] = strHeroes[i]; - } - - //copying 0 elements into arrCopy, into last valid index of arrCopy - arrList.CopyTo(arrCopy, arrCopy.Length - 1); - - // check to make sure sentinals stay the same - for (int i = 0; i < arrCopy.Length; i++) - { - Assert.Equal<string>(strHeroes[i], arrCopy[i]); - } - - //[] Copy so that exception should be thrown - arrList.Clear(); - arrCopy = new String[2]; - - //copying 0 elements into arrCopy - arrList.CopyTo(arrCopy, arrCopy.Length); - - // [] Copy so that exception should be thrown 2 - arrList.Clear(); - Assert.Equal(0, arrList.Count); - - arrCopy = new String[0]; - //copying 0 elements into arrCopy - arrList.CopyTo(arrCopy, 0); - - // [] CopyTo with negative index - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.CopyTo(arrCopy, -1)); - - // [] CopyTo with array with index is not large enough - Assert.Throws<ArgumentException>(() => - { - arrList.Clear(); - for (int i = 0; i < 10; i++) - arrList.Add(i); - - arrList.CopyTo(new Object[11], 2); - }); - - // [] CopyTo with null array - Assert.Throws<ArgumentNullException>(() => arrList.CopyTo(null, 0)); - - // [] CopyTo with multidimentional array - Assert.Throws<ArgumentException>(() => arrList.CopyTo(new Object[10, 10], 1)); - } - } - - [Fact] - public void TestCopyToWithCount() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - string[] arrCopy = null; - - string[] strHeroes = - { - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - "Huntress", - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - "Superman", - "Thor", - "Wildcat", - "Wonder Woman", - }; - - // - // Construct array list. - // - arrList = new ArrayList(); - Assert.NotNull(arrList); - - // Add items to the lists. - for (int ii = 0; ii < strHeroes.Length; ++ii) - { - arrList.Add(strHeroes[ii]); - } - - // Verify items added to list. - Assert.Equal(strHeroes.Length, arrList.Count); - - //Adapter, GetRange, Synchronized, ReadOnly returns a slightly different version of - //BinarySearch, Following variable cotains each one of these types of array lists - - ArrayList[] arrayListTypes = { - arrList, - ArrayList.Adapter(arrList), - ArrayList.FixedSize(arrList), - arrList.GetRange(0, arrList.Count), - ArrayList.ReadOnly(arrList), - ArrayList.Synchronized(arrList)}; - - foreach (ArrayList arrayListType in arrayListTypes) - { - arrList = arrayListType; - // - // [] Use CopyTo copy range of items to array. - // - int start = 3; - int count = 15; - - // Allocate sting array. - arrCopy = new String[100]; - - // Obtain string from ArrayList. - arrList.CopyTo(start, arrCopy, start, count); - - // Verify the items in the array. - for (int ii = start; ii < start + count; ++ii) - { - Assert.Equal(0, ((String)arrList[ii]).CompareTo(arrCopy[ii])); - } - - // - // [] Invalid Arguments - // - - // 2nd throw ArgumentOutOfRangeException - // rest throw ArgumentException - Assert.ThrowsAny<ArgumentException>( () => arrList.CopyTo(0, arrCopy, -100, 1000) ); - - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.CopyTo(-1, arrCopy, 0, 1)); - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.CopyTo(0, arrCopy, 0, -1)); - - // this is valid now - arrCopy = new String[100]; - arrList.CopyTo(arrList.Count, arrCopy, 0, 0); - - Assert.Throws<ArgumentException>(() => - { - arrCopy = new String[100]; - arrList.CopyTo(arrList.Count - 1, arrCopy, 0, 24); - }); - - Assert.Throws<ArgumentNullException>(() => arrList.CopyTo(0, null, 3, 15)); - - Assert.Throws<ArgumentException>(() => - { - arrCopy = new String[1]; - arrList.CopyTo(0, arrCopy, 3, 15); - }); - - Assert.Throws<ArgumentException>(() => arrList.CopyTo(0, new Object[arrList.Count, arrList.Count], 0, arrList.Count)); - // same as above, some iteration throws different exceptions: ArgumentOutOfRangeException - Assert.ThrowsAny<ArgumentException>(() => arrList.CopyTo(0, new Object[arrList.Count, arrList.Count], 0, -1)); - } - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/CtorTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/CtorTests.cs deleted file mode 100644 index cf088995a7..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/CtorTests.cs +++ /dev/null @@ -1,96 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Diagnostics; -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_CtorTests - { - [Fact] - public void CtorTest() - { - ArrayList arrList = null; - // - // [] Construct ArrayList. - // - arrList = new ArrayList(); - Assert.NotNull(arrList); - - // - // [] Verify new ArrayList. - // - Assert.Equal(0, arrList.Count); - Assert.Equal(0, arrList.Capacity); - } - - [Fact] - public void CtorIntTest() - { - // - // [] Construct ArrayList with capacity of 16 entries. - // - int nCapacity = 16; - ArrayList arrList = new ArrayList(nCapacity); - Assert.NotNull(arrList); - - // - // Verify new ArrayList. - // - Assert.Equal(nCapacity, arrList.Capacity); - - // - // [] Bogus negative capacity. - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList = new ArrayList(-1000)); - } - - [Fact] - public void CtorCollectionTest() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - ArrayList arrListColl = null; - int nItems = 100; - - // - // Construct ArrayList. - // - // Construct ArrayList. - arrList = new ArrayList(); - - // Add items to list. - for (int ii = 0; ii < nItems; ++ii) - { - arrList.Add(ii.ToString()); - } - - // Verify items added to list. - Assert.Equal(nItems, arrList.Count); - - // - // [] Construct new ArrayList from current ArrayList (collection) - // - arrListColl = new ArrayList(arrList); - - // Verify the size of the new ArrayList. - Assert.Equal(nItems, arrListColl.Count); - - // - // [] Attempt invalid construction (parm) - // - Assert.Throws<ArgumentNullException>(() => arrListColl = new ArrayList(null)); - } - - [Fact] - public void DebuggerAttributeTests() - { - DebuggerAttributes.ValidateDebuggerDisplayReferences(new ArrayList()); - DebuggerAttributes.ValidateDebuggerTypeProxyProperties(new ArrayList() { "a", 1, "b", 2 }); - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/DerivedClassTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/DerivedClassTests.cs deleted file mode 100644 index 860e3d8f5e..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/DerivedClassTests.cs +++ /dev/null @@ -1,65 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_DerivedClassTest - { - [Fact] - public void TestEnumerator() - { - ArrayList al; - IEnumerator enumerator; - //[] Make sure Enumerator works - al = new MySimpleArrayList(); - al.Add(5); - - enumerator = al.GetEnumerator(); - enumerator.MoveNext(); - Assert.Equal(5, (int)enumerator.Current); - } - - public class MySimpleArrayList : ArrayList - { - public MySimpleArrayList() : base() - { - } - - private Object _val; - private bool _filled; - - public override int Add(Object o) - { - if (_filled) - { - throw new Exception(); - } - - _filled = true; - _val = o; - - return 1; - } - - public override int Count - { - get - { - return _filled ? 1 : 0; - } - } - - public override Object this[int x] - { - get - { - if (x != 0 || !_filled) throw new Exception(); - return _val; - } - } - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/FixedSizeTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/FixedSizeTests.cs deleted file mode 100644 index 5d6652ef73..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/FixedSizeTests.cs +++ /dev/null @@ -1,162 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_FixedSizeTests - { - [Fact] - public void TestArrayListParameter() - { - ArrayList alst1; - ArrayList alst2; - - String strValue; - Object oValue; - - //[] Vanila test case - FixedSize returns an IList that cant be flexed - // The idea is that a developer will pass the fixed IList to another user who will not be able to - //shrink or expand it!! - - alst1 = new ArrayList(); - for (int i = 0; i < 10; i++) - { - strValue = "String_" + i; - alst1.Add(strValue); - } - - alst2 = ArrayList.FixedSize(alst1); - - for (int i = 0; i < 10; i++) - { - strValue = "String_" + i; - Assert.Equal(strValue, (String)alst2[i]); - } - - //[]now we remove an object from the original list - alst1.RemoveAt(9); - Assert.Throws<ArgumentOutOfRangeException>(() => { oValue = alst1[9]; }); - - //[]we cant access this in our fixed list obejct as well - the object underneath has been cut - Assert.Throws<ArgumentOutOfRangeException>(() => { oValue = alst2[9]; }); - - //[]we cant access remove or add to the fixed list - Assert.Throws<NotSupportedException>(() => alst2.RemoveAt(0)); - Assert.Throws<NotSupportedException>(() => alst2.Clear()); - Assert.Throws<NotSupportedException>(() => alst2.Add("This sort of thing will not be allowed")); - Assert.Throws<NotSupportedException>(() => alst2.Insert(0, "This sort of thing will not be allowed")); - - //[]but we can change the already existing items - alst1 = new ArrayList(); - for (int i = 0; i < 10; i++) - { - strValue = "String_" + i; - alst1.Add(strValue); - } - - alst2 = ArrayList.FixedSize(alst1); - - strValue = "Hello World"; - alst2[0] = strValue; - Assert.Equal(strValue, (string)alst2[0]); - - // [] Try passing in a null ArrayList - Assert.Throws<ArgumentNullException>(() => - { - ArrayList nullArrayList = null; - ArrayList.FixedSize(nullArrayList); - }); - } - - [Fact] - public void TestIListParameter() - { - ArrayList alst1; - IList ilst1; - ArrayList olst1; - - string strValue; - object oValue; - - //[] Vanila test case - FixedSize returns an IList that cant be flexed - // The idea is that a developer will pass the fixed IList to another user who will not be able to - //shrink or expand it!! - alst1 = new ArrayList(); - for (int i = 0; i < 10; i++) - { - strValue = "String_" + i; - alst1.Add(strValue); - } - - ilst1 = ArrayList.FixedSize((IList)alst1); - - for (int i = 0; i < 10; i++) - { - strValue = "String_" + i; - Assert.Equal(strValue, (string)ilst1[i]); - } - - //[]now we remove an object from the original list - alst1.RemoveAt(9); - Assert.Throws<ArgumentOutOfRangeException>(() => { oValue = alst1[9]; }); - - //[]we cant access this in our fixed list obejct as well - the object underneath has been cut - Assert.Throws<ArgumentOutOfRangeException>(() => { oValue = ilst1[9]; }); - - //[]we cant access remove or add to the fixed list - Assert.Throws<NotSupportedException>(() => ilst1.RemoveAt(0)); - Assert.Throws<NotSupportedException>(() => ilst1.Clear()); - Assert.Throws<NotSupportedException>(() => ilst1.Add("This sort of thing will not be allowed")); - Assert.Throws<NotSupportedException>(() => ilst1.Insert(0, "This sort of thing will not be allowed")); - Assert.Throws<NotSupportedException>(() => ilst1.Remove("String_1")); - - //[]but we can change the already existing items - alst1 = new ArrayList(); - for (int i = 0; i < 10; i++) - { - strValue = "String_" + i; - alst1.Add(strValue); - } - - ilst1 = ArrayList.FixedSize((IList)alst1); - - strValue = "Hello World"; - ilst1[0] = strValue; - Assert.Equal(strValue, (string)ilst1[0]); - - //[]we should be able to get other object that implement IList from FixedSize - - olst1 = new ArrayList(); - for (int i = 0; i < 10; i++) - { - strValue = "String_" + i; - olst1.Add(strValue); - } - - ilst1 = ArrayList.FixedSize((IList)olst1); - for (int i = 0; i < 10; i++) - { - strValue = "String_" + i; - Assert.Equal(strValue, (string)ilst1[i]); - } - - //[]we'll test some of the other methods in IList that should work!! - for (int i = 0; i < 10; i++) - { - strValue = "String_" + i; - Assert.True(ilst1.Contains(strValue), "Expected value not returned, " + strValue); - Assert.Equal(i, ilst1.IndexOf(strValue)); - } - - // [] Try passing in a null IList - Assert.Throws<ArgumentNullException>(() => - { - IList nullIList = null; - ArrayList.FixedSize(nullIList); - }); - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/GetEnumeratorTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/GetEnumeratorTests.cs deleted file mode 100644 index 0159a57006..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/GetEnumeratorTests.cs +++ /dev/null @@ -1,386 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_GetEnumeratorTests - { - #region "Test Data - Keep the data close to tests so it can vary independently from other tests" - - string[] strHeroes = - { - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - "Huntress", - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - "Superman", - "Thor", - "Wildcat", - "Wonder Woman", - }; - - string[] strResult = - { - "Aquaman", - "Atom", - "Batman", - "Superman", - "Thor", - "Wildcat", - "Wonder Woman", - }; - #endregion - - [Fact] - public void TestArrayListWrappers01() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - int ii = 0; - int start = 3; - int count = 15; - bool bGetNext = false; - object[] tempArray1; - - // - // Construct array lists. - // - arrList = new ArrayList((ICollection)strHeroes); - Assert.NotNull(arrList); - - //Adapter, GetRange, Synchronized, ReadOnly returns a slightly different version of - //BinarySearch, Following variable cotains each one of these types of array lists - - ArrayList[] arrayListTypes = { - (ArrayList)arrList.Clone(), - (ArrayList)ArrayList.Adapter(arrList).Clone(), - (ArrayList)ArrayList.FixedSize(arrList).Clone(), - (ArrayList)arrList.GetRange(0, arrList.Count).Clone(), - (ArrayList)ArrayList.ReadOnly(arrList).Clone(), - (ArrayList)ArrayList.Synchronized(arrList).Clone()}; - - IEnumerator enu; - foreach (ArrayList arrayListType in arrayListTypes) - { - arrList = arrayListType; - - // Obtain the enumerator for the test range. - enu = (IEnumerator)arrList.GetEnumerator(start, count); - Assert.NotNull(enu); - - // Verify the enumerator. - for (ii = start; ii < start + count; ++ii) - { - bGetNext = enu.MoveNext(); - if (bGetNext == false) - break; - - Assert.Equal(0, strHeroes[ii].CompareTo((string)enu.Current)); - } - - ii -= start; - Assert.Equal(count, ii); - - bGetNext = enu.MoveNext(); - Assert.False(bGetNext); - Assert.False(enu.MoveNext()); - Assert.False(enu.MoveNext()); - Assert.False(enu.MoveNext()); - - // Obtain and verify enumerator with 0 count"); - // Obtain the enumerator for the test range. - enu = (IEnumerator)arrList.GetEnumerator(start, 0); - Assert.False(enu.MoveNext()); - Assert.False(enu.MoveNext()); - Assert.False(enu.MoveNext()); - - enu.Reset(); - Assert.Throws<InvalidOperationException>(() => - { - object test = enu.Current; - }); - - Assert.False(enu.MoveNext()); - Assert.False(enu.MoveNext()); - Assert.False(enu.MoveNext()); - - // - // [] Make Sure both MoveNext and Reset throw InvalidOperationException if underlying collection has been modified but Current should not throw - // - if (!arrList.IsReadOnly) - { - object origValue = arrList[arrList.Count - 1]; - - // [] MoveNext and Reset throw if collection has been modified - try - { - IEnumerator enu1 = (IEnumerator)arrList.GetEnumerator(start, count); - enu1.MoveNext(); - - arrList[arrList.Count - 1] = "Underdog"; - object myValue = enu1.Current; - - Assert.Throws<InvalidOperationException>(() => enu1.MoveNext()); - Assert.Throws<InvalidOperationException>(() => enu1.Reset()); - - } - finally - { - arrList[arrList.Count - 1] = origValue; - } - } - - // - // [] Verify Current throws InvalidOperationException when positioned before the first element or after the last - // - enu = (IEnumerator)arrList.GetEnumerator(start, count); - Assert.Throws<InvalidOperationException>(() => - { - object myValue = enu.Current; - }); - - while (enu.MoveNext()) ; - Assert.Throws<InvalidOperationException>(() => - { - object myValue = enu.Current; - }); - - // - // [] Use invalid parameters. - // - Assert.Throws<ArgumentException>(() => - { - IEnumerator enu0 = (IEnumerator)arrList.GetEnumerator(0, 10000); - }); - - Assert.Throws<ArgumentOutOfRangeException>(() => - { - IEnumerator enu1 = (IEnumerator)arrList.GetEnumerator(-1, arrList.Count); - }); - - Assert.Throws<ArgumentOutOfRangeException>(() => - { - IEnumerator enu2 = (IEnumerator)arrList.GetEnumerator(0, -1); - }); - - Assert.Throws<ArgumentOutOfRangeException>(() => - { - IEnumerator enu3 = (IEnumerator)arrList.GetEnumerator(-1, arrList.Count); - }); - - //[] Verify the eneumerator works correctly when the ArrayList itself is in the ArrayList - if (!arrList.IsFixedSize) - { - arrList.Insert(0, arrList); - arrList.Insert(arrList.Count, arrList); - arrList.Insert(arrList.Count / 2, arrList); - - tempArray1 = new object[strHeroes.Length + 3]; - tempArray1[0] = (object)arrList; - tempArray1[tempArray1.Length / 2] = arrList; - tempArray1[tempArray1.Length - 1] = arrList; - - Array.Copy(strHeroes, 0, tempArray1, 1, strHeroes.Length / 2); - Array.Copy(strHeroes, strHeroes.Length / 2, tempArray1, (tempArray1.Length / 2) + 1, strHeroes.Length - (strHeroes.Length / 2)); - - //[] Enumerate the entire collection - enu = arrList.GetEnumerator(0, tempArray1.Length); - - for (int loop = 0; loop < 2; ++loop) - { - for (int i = 0; i < tempArray1.Length; ++i) - { - enu.MoveNext(); - - Assert.StrictEqual(tempArray1[i], enu.Current); - } - - Assert.False(enu.MoveNext()); - enu.Reset(); - } - - //[] Enumerate only part of the collection - enu = arrList.GetEnumerator(1, tempArray1.Length - 2); - - for (int loop = 0; loop < 2; ++loop) - { - for (int i = 1; i < tempArray1.Length - 1; ++i) - { - enu.MoveNext(); - - Assert.StrictEqual(tempArray1[i], enu.Current); - } - - Assert.False(enu.MoveNext()); - enu.Reset(); - } - } - } - } - - [Fact] - public void TestArrayListWrappers02() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - int ii = 0; - bool bGetNext = false; - object o1; - - IEnumerator ien1; - - // - // Construct array lists. - // - arrList = new ArrayList((ICollection)strHeroes); - - //Adapter, GetRange, Synchronized, ReadOnly returns a slightly different version of - //BinarySearch, Following variable cotains each one of these types of array lists - - ArrayList[] arrayListTypes = { - (ArrayList)arrList.Clone(), - (ArrayList)ArrayList.Adapter(arrList).Clone(), - (ArrayList)arrList.GetRange(0, arrList.Count).Clone(), - (ArrayList)ArrayList.Synchronized(arrList).Clone(), - (ArrayList)(new MyArrayList((ICollection) strHeroes))}; - - IEnumerator enu = null; - foreach (ArrayList arrayListType in arrayListTypes) - { - arrList = arrayListType; - // - // [] Add the ArrayList itself to the ArrayList - // - try - { - arrList.Add(arrList); - enu = arrList.GetEnumerator(); - - int index; - - index = 0; - while (enu.MoveNext()) - { - Assert.StrictEqual(enu.Current, arrList[index]); - ++index; - } - - enu.Reset(); - - index = 0; - while (enu.MoveNext()) - { - Assert.StrictEqual(enu.Current, arrList[index]); - ++index; - } - } - finally - { - arrList.RemoveAt(arrList.Count - 1); - } - - // - // [] Vanila - Obtain and verify enumerator. - // - // Obtain the enumerator for the test range. - enu = arrList.GetEnumerator(); - IEnumerator enuClone = arrList.GetEnumerator(); - - IEnumerator[] enuArray = { enu, enuClone }; - - //Verify both this instance and the cloned copy - foreach (IEnumerator enumerator in enuArray) - { - enu = enumerator; - - for (int i = 0; i < 2; i++) - { - Assert.NotNull(enu); - - // Verify the enumerator. - for (ii = 0; ; ii++) - { - bGetNext = enu.MoveNext(); - if (bGetNext == false) - break; - - Assert.Equal(0, strHeroes[ii].CompareTo((string)enu.Current)); - } - - Assert.Equal(strHeroes.Length, ii); - - bGetNext = enu.MoveNext(); - Assert.False(bGetNext); - Assert.False(enu.MoveNext()); - Assert.False(enu.MoveNext()); - Assert.False(enu.MoveNext()); - - enu.Reset(); - } - } - - //[]we'll make sure that the enumerator throws if the underlying collection is changed with MoveNext() but not at Current - - arrList.Clear(); - for (int i = 0; i < 100; i++) - arrList.Add(i); - - ien1 = arrList.GetEnumerator(); - ien1.MoveNext(); - ien1.MoveNext(); - arrList[10] = 1000; - - Assert.Equal(1, (int)ien1.Current); - Assert.Throws<InvalidOperationException>(() => ien1.MoveNext()); - Assert.Throws<InvalidOperationException>(() => ien1.Reset()); - - //[]we'll make sure that the enumerator throws before and after MoveNext returns false - - arrList.Clear(); - for (int i = 0; i < 100; i++) - arrList.Add(i); - - ien1 = arrList.GetEnumerator(); - - Assert.Throws<InvalidOperationException>(() => - { - o1 = ien1.Current; - }); - - while (ien1.MoveNext()) ; - Assert.Throws<InvalidOperationException>(() => - { - o1 = ien1.Current; - }); - } - } - - public class MyArrayList : ArrayList - { - public MyArrayList(ICollection c) : base(c) - { - } - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/GetSetRangeTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/GetSetRangeTests.cs deleted file mode 100644 index 53b8e0acd2..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/GetSetRangeTests.cs +++ /dev/null @@ -1,262 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_GetSetRangeTests - { - [Fact] - public void TestGetRange() - { - string strValue = string.Empty; - - ArrayList list; - ArrayList range; - - //[]vanila - list = new ArrayList(); - - for (int i = 0; i < 100; i++) - list.Add(i); - - //Adapter, GetRange, Synchronized, ReadOnly returns a slightly different version of - //BinarySearch, Following variable cotains each one of these types of array lists - - ArrayList[] arrayListTypes = { - (ArrayList)list.Clone(), - (ArrayList)ArrayList.Adapter(list).Clone(), - (ArrayList)ArrayList.FixedSize(list).Clone(), - (ArrayList)list.GetRange(0, list.Count).Clone(), - (ArrayList)ArrayList.ReadOnly(list).Clone(), - (ArrayList)ArrayList.Synchronized(list).Clone() - }; - - foreach (ArrayList arrayListType in arrayListTypes) - { - list = arrayListType; - - range = list.GetRange(10, 50); - Assert.Equal(50, range.Count); - - for (int i = 0; i < range.Count; i++) - { - Assert.Equal(i + 10, (int)range[i]); - } - - Assert.Equal(list.IsFixedSize, range.IsFixedSize); - - Assert.Equal(list.IsReadOnly, range.IsReadOnly); - - //[]we can change the underlying collection through the range and this[int index] - if (!range.IsReadOnly) - { - - for (int i = 0; i < 50; i++) - range[i] = ((int)range[i]) + 1; - - for (int i = 0; i < 50; i++) - { - Assert.Equal((i + 10) + 1, (int)range[i]); - } - - for (int i = 0; i < 50; i++) - range[i] = (int)range[i] - 1; - } - - //[]we can change the underlying collection through the range and Add - if (!range.IsFixedSize) - { - - for (int i = 0; i < 100; i++) - range.Add(i + 1000); - - Assert.Equal(150, range.Count); - Assert.Equal(200, list.Count); - - for (int i = 0; i < 50; i++) - { - Assert.Equal(i + 10, (int)range[i]); - } - - for (int i = 0; i < 100; i++) - { - Assert.Equal(i + 1000, (int)range[50 + i]); - } - } - - ////[]if we change the underlying collection through set this[int index] range will start to throw - if (list.IsReadOnly) - { - Assert.Throws<NotSupportedException>(() => - { - list[list.Count - 1] = -1; - }); - - Int32 iTemp = range.Count; - } - else - { - list[list.Count - 1] = -1; - - Assert.Throws<InvalidOperationException>(() => - { - Int32 iTemp = range.Count; - }); - } - - //[]if we change the underlying collection through add range will start to throw - range = list.GetRange(10, 50); - if (list.IsFixedSize) - { - Assert.Throws<NotSupportedException>(() => - { - list.Add(list.Count + 1000); - }); - - Int32 iTemp = range.Count; - } - else - { - list.Add(list.Count + 1000); - Assert.Throws<InvalidOperationException>(() => - { - Int32 iTemp = range.Count; - }); - } - - //[]parm tests - Assert.Throws<ArgumentException>(() => - { - range = list.GetRange(0, 500); - }); - - Assert.Throws<ArgumentOutOfRangeException>(() => - { - range = list.GetRange(0, -1); - }); - - Assert.Throws<ArgumentOutOfRangeException>(() => - { - range = list.GetRange(-1, 50); - }); - - Assert.Throws<ArgumentException>(() => - { - range = list.GetRange(list.Count, 1); - }); - - //[]we should be able to get a range of 0! - list = new ArrayList(); - range = list.GetRange(0, 0); - Assert.Equal(0, range.Count); - } - } - - [Fact] - public void TestSetRange() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - ArrayList arrSetRange = null; - int start = 3; - - string[] strHeroes = - { - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - "Huntress", - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - "Superman", - "Thor", - "Wildcat", - "Wonder Woman", - };//22 - - string[] strSetRange = - { - "Hardware", - "Icon", - "Johnny Quest", - "Captain Sisko", - "Captain Picard", - "Captain Kirk", - "Agent J", - "Agent K", - "Space Ghost", - "Wolverine", - "Cyclops", - "Storm", - "Lone Ranger", - "Tonto", - "Supergirl", - };//15 - - // Construct ArrayList. - arrList = new ArrayList((ICollection)strHeroes); - arrSetRange = new ArrayList((ICollection)strSetRange); - - //Adapter, GetRange, Synchronized, ReadOnly returns a slightly different version of - //BinarySearch, Following variable cotains each one of these types of array lists - - ArrayList[] arrayListTypes = { - (ArrayList)arrList.Clone(), - (ArrayList)ArrayList.Adapter(arrList).Clone(), - (ArrayList)ArrayList.FixedSize(arrList).Clone(), - (ArrayList)arrList.GetRange(0, arrList.Count).Clone(), - (ArrayList)ArrayList.Synchronized(arrList).Clone()}; - - foreach (ArrayList arrayListType in arrayListTypes) - { - arrList = arrayListType; - - // SetRange entire array list. - arrList.SetRange(start, arrSetRange); - - // Verify set. - for (int ii = 0; ii < arrSetRange.Count; ++ii) - { - Assert.Equal(0, ((string)arrList[start + ii]).CompareTo((String)arrSetRange[ii])); - } - - // - // [] Attempt invalid SetRange using collection that exceed valid range. - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.SetRange(start, arrList)); - - // - // [] Attempt invalid SetRange using negative index - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.SetRange(-100, arrSetRange)); - - // - // [] Attempt SetRange using out of range index - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.SetRange(1000, arrSetRange)); - - // - // [] Attempt SetRange using null collection. - // - Assert.Throws<ArgumentNullException>(() => arrList.SetRange(0, null)); - } - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/IndexOfTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/IndexOfTests.cs deleted file mode 100644 index 90db3b2843..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/IndexOfTests.cs +++ /dev/null @@ -1,304 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_IndexOfTests - { - [Fact] - public void TestArrayListWrappers01() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - // no null - string[] strHeroes = - { - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Batman", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Batman", - "Green Lantern", - "Hawkman", - "Huntress", - "Ironman", - "Nightwing", - "Batman", - "Robin", - "SpiderMan", - "Steel", - "Superman", - "Thor", - "Batman", - "Wildcat", - "Wonder Woman", - "Batman", - }; - - ArrayList arrList = null; - int ndx = -1; - - // Construct ArrayList. - arrList = new ArrayList((ICollection)strHeroes); - - //Adapter, GetRange, Synchronized, ReadOnly returns a slightly different version of - //BinarySearch, Following variable cotains each one of these types of array lists - - ArrayList[] arrayListTypes = { - (ArrayList)arrList.Clone(), - (ArrayList)ArrayList.Adapter(arrList).Clone(), - (ArrayList)arrList.GetRange(0, arrList.Count).Clone(), - (ArrayList)ArrayList.Synchronized(arrList).Clone()}; - - foreach (ArrayList arrayListType in arrayListTypes) - { - arrList = arrayListType; - // - // Construct array lists. - // - Assert.NotNull(arrList); - - // - // [] Obtain index of "Batman" items. - // - int startIndex = 0; - while (startIndex < arrList.Count && (ndx = arrList.IndexOf("Batman", startIndex)) != -1) - { - Assert.True(startIndex <= ndx); - - Assert.Equal(0, strHeroes[ndx].CompareTo((string)arrList[ndx])); - - // - // [] Attempt to find null object. - // - // Remove range of items. - ndx = arrList.IndexOf(null, 0); - Assert.Equal(-1, ndx); - - // - // [] Attempt invalid IndexOf using negative index - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.IndexOf("Batman", -1000)); - - // - // [] Attempt invalid IndexOf using out of range index - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.IndexOf("Batman", 1000)); - - // []Team review feedback - query for an existing object after the index. expects -1 - arrList.Clear(); - for (int i = 0; i < 10; i++) - arrList.Add(i); - - Assert.Equal(-1, arrList.IndexOf(0, 1)); - } - } - } - - [Fact] - public void TestArrayListWrappers02() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - int ndx = -1; - - string[] strHeroes = - { - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Batman", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Batman", - "Green Lantern", - "Hawkman", - "Huntress", - "Ironman", - "Nightwing", - "Batman", - "Robin", - "SpiderMan", - "Steel", - "Superman", - "Thor", - "Batman", - "Wildcat", - "Wonder Woman", - "Batman", - null - }; - - // - // Construct array lists. - // - arrList = new ArrayList((ICollection)strHeroes); - Assert.NotNull(arrList); - - //Adapter, GetRange, Synchronized, ReadOnly returns a slightly different version of - //BinarySearch, Following variable cotains each one of these types of array lists - - ArrayList[] arrayListTypes = { - arrList, - ArrayList.Adapter(arrList), - ArrayList.FixedSize(arrList), - arrList.GetRange(0, arrList.Count), - ArrayList.ReadOnly(arrList), - ArrayList.Synchronized(arrList)}; - - foreach (ArrayList arrayListType in arrayListTypes) - { - arrList = arrayListType; - // - // [] Obtain index of "Batman" items. - // - ndx = 0; - - int startIndex = 0; - int tmpNdx = 0; - while (startIndex < arrList.Count && (ndx = arrList.IndexOf("Batman", startIndex, (arrList.Count - startIndex))) != -1) - { - Assert.True(ndx >= startIndex); - - Assert.Equal(0, strHeroes[ndx].CompareTo((string)arrList[ndx])); - - tmpNdx = arrList.IndexOf("Batman", startIndex, ndx - startIndex + 1); - Assert.Equal(ndx, tmpNdx); - - tmpNdx = arrList.IndexOf("Batman", startIndex, ndx - startIndex); - Assert.Equal(-1, tmpNdx); - - startIndex = ndx + 1; - } - - // - // [] Attempt to find null object when a null element exists in the collections - // - ndx = arrList.IndexOf(null, 0, arrList.Count); - Assert.Null(arrList[ndx]); - - // - // [] Attempt invalid IndexOf using negative index - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.IndexOf("Batman", -1000, arrList.Count)); - - // - // [] Attempt invalid IndexOf using out of range index - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.IndexOf("Batman", 1000, arrList.Count)); - - // - // [] Attempt invalid IndexOf using index=Count - // - Assert.Equal(-1, arrList.IndexOf("Batman", arrList.Count, 0)); - - - // - // [] Attempt invalid IndexOf using endIndex greater than the size. - // - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.IndexOf("Batman", 3, arrList.Count + 10)); - - - //[]Team Review feedback - attempt to find non-existent object and confirm that -1 returned - arrList = new ArrayList(); - for (int i = 0; i < 10; i++) - arrList.Add(i); - - Assert.Equal(-1, arrList.IndexOf(50, 0, arrList.Count)); - Assert.Equal(-1, arrList.IndexOf(0, 1, arrList.Count - 1)); - } - } - - [Fact] - public void TestDuplicatedItems() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - - string[] strHeroes = - { - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - "Daniel Takacs", - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - "Gene", - "Thor", - "Wildcat", - null - }; - - // Construct ArrayList. - arrList = new ArrayList(strHeroes); - - //Adapter, GetRange, Synchronized, ReadOnly returns a slightly different version of - //BinarySearch, Following variable cotains each one of these types of array lists - - ArrayList[] arrayListTypes = { - (ArrayList)arrList.Clone(), - (ArrayList)ArrayList.Adapter(arrList).Clone(), - (ArrayList)arrList.GetRange(0, arrList.Count).Clone(), - (ArrayList)ArrayList.Synchronized(arrList).Clone()}; - - foreach (ArrayList arrayListType in arrayListTypes) - { - - arrList = arrayListType; - // - // [] IndexOf an array normal - // - for (int i = 0; i < strHeroes.Length; i++) - { - Assert.Equal(i, arrList.IndexOf(strHeroes[i])); - } - - - //[] Check IndexOf when element is in list twice - arrList.Clear(); - arrList.Add(null); - arrList.Add(arrList); - arrList.Add(null); - - Assert.Equal(0, arrList.IndexOf(null)); - - //[] check for something which does not exist in a list - arrList.Clear(); - Assert.Equal(-1, arrList.IndexOf(null)); - } - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/InsertRangeTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/InsertRangeTests.cs deleted file mode 100644 index a26699ab96..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/InsertRangeTests.cs +++ /dev/null @@ -1,365 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_InsertRangeTests - { - #region "Test data - Keep the data close to tests so it can vary independently from other tests" - string[] strHeroes = - { - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - "Huntress", - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - "Superman", - "Thor", - "Wildcat", - "Wonder Woman", - }; - - string[] strInsert = - { - "Dr. Fate", - "Dr. Light", - "Dr. Manhattan", - "Hardware", - "Hawkeye", - "Icon", - "Spawn", - "Spectre", - "Supergirl", - }; - - string[] strResult = - { - "Aquaman", - "Atom", - "Batman", - "Dr. Fate", - "Dr. Light", - "Dr. Manhattan", - "Hardware", - "Hawkeye", - "Icon", - "Spawn", - "Spectre", - "Supergirl", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - "Huntress", - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - "Superman", - "Thor", - "Wildcat", - "Wonder Woman", - }; - #endregion - - [Fact] - public void TestInsertRangeBasic() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - ArrayList arrInsert = null; - int ii = 0; - int start = 3; - - // - // Construct array lists. - // - arrList = new ArrayList((ICollection)strHeroes); - - // - // Construct insert array list. - // - arrInsert = new ArrayList((ICollection)strInsert); - - //Adapter, GetRange, Synchronized, ReadOnly returns a slightly different version of - //BinarySearch, Following variable cotains each one of these types of array lists - - ArrayList[] arrayListTypes = { - (ArrayList)arrList.Clone(), - (ArrayList)ArrayList.Adapter(arrList).Clone(), - (ArrayList)arrList.GetRange(0, arrList.Count).Clone(), - (ArrayList)ArrayList.Synchronized(arrList).Clone()}; - - foreach (ArrayList arrayListType in arrayListTypes) - { - arrList = arrayListType; - // - // [] Insert collection into array list. - // - // InsertRange values. - arrList.InsertRange(start, arrInsert); - - // Verify InsertRange. - for (ii = 0; ii < strResult.Length; ++ii) - { - Assert.Equal(0, strResult[ii].CompareTo((String)arrList[ii])); - } - - // - // [] Attempt invalid InsertRange using negative index - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.InsertRange(-1000, arrInsert)); - - // - // [] Attempt invalid InsertRange using out of range index - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.InsertRange(1000, arrInsert)); - - // - // [] Attempt insertion of null collection. - arrInsert = new ArrayList((ICollection)strInsert); - Assert.Throws<ArgumentNullException>(() => arrList.InsertRange(start, null)); - - // []Insert an empty ICollection - arrList = new ArrayList(); - Queue que = new Queue(); - arrList.InsertRange(0, que); - - Assert.Equal(0, arrList.Count); - } - } - - [Fact] - public void TestLargeCapacity() - { - // - // [] Add a range large enough to increase the capacity of the arrayList by more than a factor of two - // - ArrayList arrInsert = new ArrayList(); - - for (int i = 0; i < 128; i++) - { - arrInsert.Add(-i); - } - - ArrayList arrList = new ArrayList(); - - ArrayList[] arrayListTypes1 = { - (ArrayList)arrList.Clone(), - (ArrayList)ArrayList.Adapter(arrList).Clone(), - (ArrayList)arrList.GetRange(0, arrList.Count).Clone(), - (ArrayList)ArrayList.Synchronized(arrList).Clone()}; - - foreach (ArrayList arrayListType in arrayListTypes1) - { - arrList = arrayListType; - - arrList.InsertRange(0, arrInsert); - - for (int i = 0; i < arrInsert.Count; i++) - { - Assert.Equal(-i, (int)arrList[i]); - } - } - } - - [Fact] - public void TestInsertItself() - { - // - // [] Insert itself into array list. - // - ArrayList arrList = new ArrayList((ICollection)strHeroes); - - ArrayList[] arrayListTypes = new ArrayList[] { - (ArrayList)arrList.Clone(), - (ArrayList)ArrayList.Adapter(arrList).Clone(), - (ArrayList)arrList.GetRange(0, arrList.Count).Clone(), - (ArrayList)ArrayList.Synchronized(arrList).Clone()}; - - int start = 3; - foreach (ArrayList arrayListType in arrayListTypes) - { - arrList = arrayListType; - // InsertRange values. - arrList.InsertRange(start, arrList); - - // Verify InsertRange. - for (int ii = 0; ii < arrList.Count; ++ii) - { - string expectedItem; - - if (ii < start) - { - expectedItem = strHeroes[ii]; - } - else if (start <= ii && ii - start < strHeroes.Length) - { - expectedItem = strHeroes[ii - start]; - } - else - { - expectedItem = strHeroes[(ii - strHeroes.Length)]; - } - - Assert.Equal(0, expectedItem.CompareTo((string)arrList[ii])); - } - - //[] Verify that ArrayList does not pass the internal array to CopyTo - arrList.Clear(); - for (int i = 0; i < 64; ++i) - { - arrList.Add(i); - } - - ArrayList arrInsert = new ArrayList(); - - for (int i = 0; i < 4; ++i) - { - arrInsert.Add(i); - } - - MyCollection myCollection = new MyCollection(arrInsert); - arrList.InsertRange(4, myCollection); - - Assert.Equal(0, myCollection.StartIndex); - - Assert.Equal(4, myCollection.Array.Length); - } - } - - [Fact] - public void TestInsertItselfWithRange() - { - // - // [] Insert itself into array list. with range - // - ArrayList arrList = new ArrayList((ICollection)strHeroes); - - ArrayList[] arrayListTypes = new ArrayList[] { - (ArrayList)arrList.Clone(), - (ArrayList)ArrayList.Adapter(arrList).Clone(), - (ArrayList)ArrayList.Synchronized(arrList).Clone() - }; - - int start = 3; - foreach (ArrayList arrayListType in arrayListTypes) - { - arrList = arrayListType; - - // InsertRange values. - arrList.InsertRange(start, arrList.GetRange(0, arrList.Count)); - - // Verify InsertRange. - for (int ii = 0; ii < arrList.Count; ++ii) - { - string expectedItem; - - if (ii < start) - { - expectedItem = strHeroes[ii]; - } - else if (start <= ii && ii - start < strHeroes.Length) - { - expectedItem = strHeroes[ii - start]; - } - else - { - expectedItem = strHeroes[(ii - strHeroes.Length)]; - } - - Assert.Equal(0, expectedItem.CompareTo((string)arrList[ii])); - } - } - - } - - public class MyCollection : ICollection - { - private ICollection _collection; - private Array _array; - private int _startIndex; - - public MyCollection(ICollection collection) - { - _collection = collection; - } - - public Array Array - { - get - { - return _array; - } - } - - public int StartIndex - { - get - { - return _startIndex; - } - } - - public int Count - { - get - { - return _collection.Count; - } - } - - public Object SyncRoot - { - get - { - return _collection.SyncRoot; - } - } - - public bool IsSynchronized - { - get - { - return _collection.IsSynchronized; - } - } - - public void CopyTo(Array array, int startIndex) - { - _array = array; - _startIndex = startIndex; - _collection.CopyTo(array, startIndex); - } - - public IEnumerator GetEnumerator() - { - throw new NotSupportedException(); - } - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/InsertTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/InsertTests.cs deleted file mode 100644 index c8244667bc..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/InsertTests.cs +++ /dev/null @@ -1,140 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_InsertTests - { - [Fact] - public void TestArrayListWrappers() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - int ii = 0; - int start = 3; - - string[] strHeroes = - { - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - "Huntress", - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - "Superman", - "Thor", - "Wildcat", - "Wonder Woman", - }; - - string[] strInsert = - { - "Dr. Fate", - "Dr. Light", - "Dr. Manhattan", - "Hardware", - "Hawkeye", - "Icon", - "Spawn", - "Spectre", - "Supergirl", - }; - - string[] strResult = - { - "Aquaman", - "Atom", - "Batman", - "Dr. Fate", - "Dr. Light", - "Dr. Manhattan", - "Hardware", - "Hawkeye", - "Icon", - "Spawn", - "Spectre", - "Supergirl", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - "Huntress", - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - "Superman", - "Thor", - "Wildcat", - "Wonder Woman", - }; - - // - // Construct array lists. - // - arrList = new ArrayList((ICollection)strHeroes); - - //Adapter, GetRange, Synchronized, ReadOnly returns a slightly different version of - //BinarySearch, Following variable cotains each one of these types of array lists - - ArrayList[] arrayListTypes = { - (ArrayList)arrList.Clone(), - (ArrayList)ArrayList.Adapter(arrList).Clone(), - (ArrayList)arrList.GetRange(0, arrList.Count).Clone(), - (ArrayList)ArrayList.Synchronized(arrList).Clone()}; - - foreach (ArrayList arrayListType in arrayListTypes) - { - arrList = arrayListType; - - // - // [] Insert values into array list. - // - // Insert values. - for (ii = 0; ii < strInsert.Length; ++ii) - { - arrList.Insert(start + ii, strInsert[ii]); - } - - // Verify insert. - for (ii = 0; ii < strResult.Length; ++ii) - { - Assert.Equal(0, strResult[ii].CompareTo((String)arrList[ii])); - } - - // - // [] Attempt invalid Insert using negative index - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.Insert(-1000, "Batman")); - - // - // [] Attempt invalid Insert using out of range index - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.Insert(1000, "Batman")); - } - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/LastIndexOfTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/LastIndexOfTests.cs deleted file mode 100644 index 90d62b2956..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/LastIndexOfTests.cs +++ /dev/null @@ -1,248 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_LastIndexOfTests - { - #region "Test Data - Keep the data close to tests so it can vary independently from other tests" - string[] strHeroes = - { - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Batman", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Batman", - "Green Lantern", - "Hawkman", - "Huntress", - "Ironman", - "Nightwing", - "Batman", - "Robin", - "SpiderMan", - "Steel", - "Superman", - "Thor", - "Batman", - "Wildcat", - "Wonder Woman", - "Batman", - }; - - #endregion - - [Fact] - public void TestLastIndexOfBasic() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - int ndx = -1; - // - // Construct array lists. - // - arrList = new ArrayList((ICollection)strHeroes); - - // - // [] Obtain last index of "Batman" items. - // - ndx = arrList.LastIndexOf("Batman"); - if (ndx != -1) - { - Assert.Equal(0, strHeroes[ndx].CompareTo((string)arrList[ndx])); - } - - // - // [] Attempt to find null object. - // - // Remove range of items. - ndx = arrList.LastIndexOf(null); - Assert.Equal(-1, ndx); - - // [] Call LastIndexOf on an empty list - var myList = new ArrayList(); - var lastIndex = myList.LastIndexOf(6); - - Assert.Equal(-1, lastIndex); - } - - [Fact] - public void TestInvalidIndex() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - int ndx = -1; - - // - // Construct array lists. - // - arrList = new ArrayList((ICollection)strHeroes); - - // - // [] Obtain last index of "Batman" items. - // - ndx = arrList.Count; - while ((ndx = arrList.LastIndexOf("Batman", --ndx)) != -1) - { - Assert.Equal(0, strHeroes[ndx].CompareTo((string)arrList[ndx])); - } - - // - // [] Attempt to find null object. - // - // Remove range of items. - ndx = arrList.LastIndexOf(null, arrList.Count - 1); - Assert.Equal(-1, ndx); - - // - // [] Attempt invalid LastIndexOf using negative endindex - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.LastIndexOf("Batman", -1)); - - // - // [] Attempt invalid LastIndexOf using negative startindex - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.LastIndexOf("Batman", -1000)); - } - - [Fact] - public void TestArrayListWrappers() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - int ndx = -1; - - string[] strHeroes = - { - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Batman", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Batman", - "Green Lantern", - "Hawkman", - "Huntress", - "Ironman", - "Nightwing", - "Batman", - "Robin", - "SpiderMan", - "Steel", - "Superman", - "Thor", - "Batman", - "Wildcat", - "Wonder Woman", - "Batman", - null - }; - - // - // Construct array lists. - // - arrList = new ArrayList((ICollection)strHeroes); - - //Adapter, GetRange, Synchronized, ReadOnly returns a slightly different version of - //BinarySearch, Following variable cotains each one of these types of array lists - - ArrayList[] arrayListTypes = { - (ArrayList)arrList.Clone(), - (ArrayList)ArrayList.Adapter(arrList).Clone(), - (ArrayList)ArrayList.FixedSize(arrList).Clone(), - (ArrayList)arrList.GetRange(0, arrList.Count).Clone(), - (ArrayList)ArrayList.ReadOnly(arrList).Clone(), - (ArrayList)ArrayList.Synchronized(arrList).Clone()}; - - foreach (ArrayList arrayListType in arrayListTypes) - { - arrList = arrayListType; - - // - // [] Obtain last index of "Batman" items. - // - int startIndex = arrList.Count - 1; - int tmpNdx = 0; - while (0 < startIndex && (ndx = arrList.LastIndexOf("Batman", startIndex, startIndex + 1)) != -1) - { - Assert.True(ndx <= startIndex); - - Assert.Equal(0, strHeroes[ndx].CompareTo((string)arrList[ndx])); - - tmpNdx = arrList.LastIndexOf("Batman", startIndex, startIndex - ndx + 1); - Assert.Equal(ndx, tmpNdx); - - tmpNdx = arrList.LastIndexOf("Batman", startIndex, startIndex - ndx); - Assert.Equal(-1, tmpNdx); - - startIndex = ndx - 1; - } - - // - // [] Attempt to find null object. - // - // Remove range of items. - ndx = arrList.LastIndexOf(null, arrList.Count - 1, arrList.Count); - Assert.NotEqual(-1, ndx); - Assert.Null(arrList[ndx]); - - // - // [] Attempt invalid LastIndexOf using negative endindex - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.LastIndexOf("Batman", arrList.Count - 1, -1000)); - - // - // [] Attempt invalid LastIndexOf using negative startindex - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.LastIndexOf("Batman", -1000, 0)); - - // - // [] Attempt invalid LastIndexOf using endIndex greater than the size. - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.LastIndexOf("Batman", 3, arrList.Count + 10)); - - // - // [] Attempt invalid LastIndexOf using startIndex greater than the size. - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.LastIndexOf("Batman", arrList.Count + 1, arrList.Count)); - - // - // [] Attempt invalid LastIndexOf using count > starIndex + 1 - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.LastIndexOf("Batman", 3, 5)); - - // - // [] Attempt LastIndexOf on empty ArrayList - // - if (!arrList.IsFixedSize) - { - arrList.Clear(); - int index = arrList.LastIndexOf("", 0, 0); - Assert.Equal(-1, index); - } - } - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/PropertyCapacityTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/PropertyCapacityTests.cs deleted file mode 100644 index bc637b9e37..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/PropertyCapacityTests.cs +++ /dev/null @@ -1,101 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_CapacityTests - { - #region "Test Data - Keep the data close to tests so it can vary independently from other tests" - - static string[] strHeroes = - { - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - "Huntress", - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - "Superman", - "Thor", - "Wildcat", - "Wonder Woman", - }; - - #endregion - - [Fact] - public void TestGetBasic() - { - // Construct ArrayList. - ArrayList arrList = new ArrayList((ICollection)strHeroes); - Assert.NotNull(arrList); - - // [] Obtain list capacity. - Assert.True(arrList.Capacity >= arrList.Count); - } - - [Fact] - public void TestSetBasic() - { - ArrayList arrList = null; - - // - // Construct array list. - // - arrList = new ArrayList((ICollection)strHeroes); - - // - // [] Set and verify list capacity. - // - int nCapacity = 2 * arrList.Capacity; - arrList.Capacity = nCapacity; - Assert.Equal(nCapacity, arrList.Capacity); - - // - // [] Bogus negative argument. - // - Assert.Throws<ArgumentOutOfRangeException>(() => { arrList.Capacity = -1000; }); - - // - // [] Bogus super large capacity. - // - Assert.Throws<OutOfMemoryException>(() => { arrList.Capacity = Int32.MaxValue; }); - - //[] Team Review feedback - set capacity to a value less than the count - arrList = new ArrayList(); - for (int i = 0; i < 10; i++) - arrList.Add(i); - - Assert.Throws<ArgumentOutOfRangeException>(() => { arrList.Capacity = arrList.Count - 1; }); - - // - // [] Set Capacity equal to 0 - // - arrList = new ArrayList(1); - - arrList.Capacity = 0; - Assert.Equal(4, arrList.Capacity); - - for (int i = 0; i < 32; i++) - arrList.Add(-i); - - for (int i = 0; i < 32; i++) - Assert.Equal(-i, (int)arrList[i]); - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/PropertyCountTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/PropertyCountTests.cs deleted file mode 100644 index f166df5936..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/PropertyCountTests.cs +++ /dev/null @@ -1,64 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_CountTests - { - [Fact] - public void TestGetCountBasic() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - - string[] strHeroes = - { - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - "Huntress", - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - "Superman", - "Thor", - "Wildcat", - "Wonder Woman", - }; - - // - // Construct array list. - // - arrList = new ArrayList((ICollection)strHeroes); - Assert.NotNull(arrList); - - // - // [] Verify array list size. - // - Assert.Equal(strHeroes.Length, arrList.Count); - - // - // [] Verify size of empty array list. - // - arrList = new ArrayList(); - // Verify size. - Assert.Equal(0, arrList.Count); - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/PropertyIsFixedSizeTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/PropertyIsFixedSizeTests.cs deleted file mode 100644 index b566da4517..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/PropertyIsFixedSizeTests.cs +++ /dev/null @@ -1,73 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_IsFixedSizeTests - { - [Fact] - public void TestGetIsFixedSizeBasic() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList list = null; - ArrayList fixedList = null; - IList ilist = null; - // - // []vanila - should not be fixed size - // - list = new ArrayList(); - Assert.False(list.IsFixedSize); - - //[]just to make sure that we can add values here - for (int i = 0; i < 100; i++) - { - list.Add(i); - } - - //[]we woill get a fixed size one and then see - list = new ArrayList(); - - fixedList = ArrayList.FixedSize(list); - Assert.True(fixedList.IsFixedSize); - - //[]just to make sure that we can not add values here - Assert.Throws<NotSupportedException>(() => fixedList.Add(100)); - - //[]we will get one from adapater - list = ArrayList.Adapter((IList)new ArrayList()); - - Assert.False(list.IsFixedSize); - - //[]we will get one from Synchronized for an ArrayList - list = ArrayList.Synchronized(new ArrayList()); - - Assert.False(list.IsFixedSize); - - //[]we will get one from Synchronized for an IList - ilist = ArrayList.Synchronized((IList)new ArrayList()); - - Assert.False(ilist.IsFixedSize); - - //[]we will get one from FixedSize for an IList - ilist = ArrayList.FixedSize((IList)new ArrayList()); - Assert.True(ilist.IsFixedSize); - - //[]we will get one from ReadOnly for an IList - ilist = ArrayList.ReadOnly((IList)new ArrayList()); - Assert.True(ilist.IsFixedSize); - - //[]we will get one from ReadOnly for an ArrayList - list = ArrayList.ReadOnly(new ArrayList()); - Assert.True(list.IsFixedSize); - - //[]we will get one from Range for an ArrayList - list = (new ArrayList()).GetRange(0, 0); - Assert.False(list.IsFixedSize); - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/PropertyIsReadOnlyTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/PropertyIsReadOnlyTests.cs deleted file mode 100644 index ad15e19fb5..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/PropertyIsReadOnlyTests.cs +++ /dev/null @@ -1,80 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_IsReadOnlyTests - { - [Fact] - public void TestGetIsReadOnlyBasic() - { - ArrayList alst1; - ArrayList alst2; - ArrayList alst3; - - string strValue; - object oValue; - - //[] Vanila test case - ReadOnly returns an ArrayList that cant be modified - // The idea is that a developer will pass the fixed ArrayList to another user who will not be able to - // modify it!! - - alst1 = new ArrayList(); - for (int i = 0; i < 10; i++) - { - strValue = "String_" + i; - alst1.Add(strValue); - } - - alst2 = ArrayList.ReadOnly(alst1); - - for (int i = 0; i < 10; i++) - { - strValue = "String_" + i; - Assert.Equal(strValue, (string)alst2[i]); - } - - //[]now we remove an object from the original list. we cant access that object in the ReadOnly ArrayList - alst1.RemoveAt(9); - - Assert.Throws<ArgumentOutOfRangeException>(() => - { - oValue = alst1[9]; - }); - - //we cant access this in our readonly list object as well - the object underneath has been cut - Assert.Throws<ArgumentOutOfRangeException>(() => - { - oValue = alst2[9]; - }); - - //[]we cant access remove or add to the readonly list - Assert.Throws<NotSupportedException>(() => alst2.RemoveAt(0)); - Assert.Throws<NotSupportedException>(() => alst2.Remove("String_1")); - Assert.Throws<NotSupportedException>(() => alst2.Clear()); - Assert.Throws<NotSupportedException>(() => alst2.Add("This sort of thing will not be allowed")); - Assert.Throws<NotSupportedException>(() => alst2.Insert(0, "This sort of thing will not be allowed")); - Assert.Throws<NotSupportedException>(() => - { - strValue = "Hello World"; - alst2[0] = strValue; - }); - - //[]we'll do the ReadOnly test - Assert.False(alst1.IsReadOnly); - Assert.True(alst2.IsReadOnly); - - //[]we'll get a readonly from this readonly ArrayList - alst3 = ArrayList.ReadOnly(alst2); - Assert.True(alst2.IsReadOnly); - Assert.True(alst3.IsReadOnly); - - //[]we still cant access the 2nd one :) - //we cant access remove or add to the readonly list - Assert.Throws<NotSupportedException>(() => alst2.RemoveAt(0)); - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/PropertyIsSynchronizedTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/PropertyIsSynchronizedTests.cs deleted file mode 100644 index 1dcc317e3b..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/PropertyIsSynchronizedTests.cs +++ /dev/null @@ -1,92 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Threading.Tasks; -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_GetIsSynchronizedTests - { - private ArrayList _alst2; - private Int32 _iNumberOfElements = 10; - - [Fact] - public void TestGetIsSynchronized() - { - ArrayList alst1; - Int32 iNumberOfTimes; - String strValue; - - Task[] workers; - Action ts1; - Int32 iNumberOfWorkers = 10; - - //[] Vanila test case - Synchronized returns an IList that is thread safe - // We will try to test this by getting a number of threads to write some items - // to a synchronized IList - - alst1 = new ArrayList(); - _alst2 = ArrayList.Synchronized(alst1); - - workers = new Task[iNumberOfWorkers]; - ts1 = new Action(AddElements); - - for (int i = 0; i < workers.Length; i++) - { - workers[i] = Task.Run(ts1); - } - - Task.WaitAll(workers); - - //checking time - Assert.Equal(_iNumberOfElements * iNumberOfWorkers, _alst2.Count); - - for (int i = 0; i < _iNumberOfElements; i++) - { - iNumberOfTimes = 0; - strValue = "String_" + i; - for (int j = 0; j < _alst2.Count; j++) - { - if (((String)_alst2[j]).Equals(strValue)) - iNumberOfTimes++; - } - - Assert.Equal(iNumberOfTimes, iNumberOfWorkers); - } - - //I dont think that we can make an assumption on the order of these items though - //now we are going to remove all of these - workers = new Task[iNumberOfWorkers]; - ts1 = new Action(RemoveElements); - for (int i = 0; i < workers.Length; i++) - { - workers[i] = Task.Run(ts1); - } - - Task.WaitAll(workers); - - Assert.Equal(0, _alst2.Count); - Assert.False(alst1.IsSynchronized); - Assert.True(_alst2.IsSynchronized); - } - - void AddElements() - { - for (int i = 0; i < _iNumberOfElements; i++) - { - _alst2.Add("String_" + i); - } - } - - void RemoveElements() - { - for (int i = 0; i < _iNumberOfElements; i++) - { - _alst2.Remove("String_" + i); - } - } - - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/PropertyItemTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/PropertyItemTests.cs deleted file mode 100644 index 1963a70504..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/PropertyItemTests.cs +++ /dev/null @@ -1,163 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_ItemTests - { - #region "Test Data - Keep the data close to tests so it can vary independently from other tests" - string[] strHeroes = - { - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - "Huntress", - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - "Superman", - "Thor", - "Wildcat", - "Wonder Woman", - }; - #endregion - - [Fact] - public void TestGetItems() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - // - // Construct array list. - // - arrList = new ArrayList(); - - // Add items to the lists. - for (int ii = 0; ii < strHeroes.Length; ++ii) - { - arrList.Add(strHeroes[ii]); - } - - // Verify items added to list. - Assert.Equal(strHeroes.Length, arrList.Count); - - //Adapter, GetRange, Synchronized, ReadOnly returns a slightly different version of - //BinarySearch, Following variable cotains each one of these types of array lists - - ArrayList[] arrayListTypes = { - (ArrayList)arrList.Clone(), - (ArrayList)ArrayList.Adapter(arrList).Clone(), - (ArrayList)ArrayList.FixedSize(arrList).Clone(), - (ArrayList)ArrayList.ReadOnly(arrList).Clone(), - (ArrayList)arrList.GetRange(0, arrList.Count).Clone(), - (ArrayList)ArrayList.Synchronized(arrList).Clone()}; - - foreach (ArrayList arrayListType in arrayListTypes) - { - arrList = arrayListType; - // - // [] Verify get method. - // - // Search and verify selected items. - for (int ii = 0; ii < strHeroes.Length; ++ii) - { - // Verify get. - Assert.Equal(0, ((string)arrList[ii]).CompareTo(strHeroes[ii])); - } - - // - // [] Invalid Index. - // - Assert.Throws<ArgumentOutOfRangeException>(() => - { - string str = (string)arrList[(int)arrList.Count]; - }); - - Assert.Throws<ArgumentOutOfRangeException>(() => - { - string str = (string)arrList[-1]; - }); - } - } - - [Fact] - public void TestSetItems() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - - // - // Construct array list. - // - arrList = new ArrayList((ICollection)strHeroes); - - //Adapter, GetRange, Synchronized, ReadOnly returns a slightly different version of - //BinarySearch, Following variable cotains each one of these types of array lists - - ArrayList[] arrayListTypes = { - (ArrayList)arrList.Clone(), - (ArrayList)ArrayList.Adapter(arrList).Clone(), - (ArrayList)ArrayList.FixedSize(arrList).Clone(), - (ArrayList)arrList.GetRange(0, arrList.Count).Clone(), - (ArrayList)ArrayList.Synchronized(arrList).Clone()}; - - foreach (ArrayList arrayListType in arrayListTypes) - { - arrList = arrayListType; - - // - // [] Set item in the array list. - // - arrList[0] = "Lone Ranger"; - - // Verify set. - Assert.Equal(0, ((string)arrList[0]).CompareTo("Lone Ranger")); - - // - // [] Attempt invalid Set using negative index - // - Assert.Throws<ArgumentOutOfRangeException>(() => { arrList[-100] = "Lone Ranger"; }); - - // - // [] Attempt Set using out of range index=1000 - // - Assert.Throws<ArgumentOutOfRangeException>(() => { arrList[1000] = "Lone Ranger"; }); - - // - // [] Attempt Set using out of range index=-1 - // - Assert.Throws<ArgumentOutOfRangeException>(() => { arrList[-1] = "Lone Ranger"; }); - - // - // [] Attempt Set using null value. - // - arrList[0] = null; - // Verify set. - Assert.Null(arrList[0]); - } - } - - //public virtual int Compare(object x, object y) - //{ - // return ((string)x).CompareTo((string)y); - //} - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/PropertySyncRootTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/PropertySyncRootTests.cs deleted file mode 100644 index caeab2619c..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/PropertySyncRootTests.cs +++ /dev/null @@ -1,118 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Threading.Tasks; -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_SyncRootTests - { - private ArrayList _arrDaughter; - private ArrayList _arrGrandDaughter; - - [Fact] - public void TestGetSyncRoot() - { - ArrayList arrSon, arrSon2; - ArrayList arrMother; - - int iNumberOfElements = 100; - int iValue; - bool fDescending, fWrongResult; - - Task[] workers; - Action ts1; - Action ts2; - int iNumberOfWorkers = 10; - - //[] Vanila test case - testing SyncRoot is not as simple as its implementation looks like. This is the working - //scenrio we have in mind. - //1) Create your Down to earth mother ArrayList - //2) Get a Fixed wrapper from it - //3) Get a Synchronized wrapper from 2) - //4) Get a synchronized wrapper of the mother from 1) - //5) all of these should SyncRoot to the mother earth - - arrMother = new ArrayList(); - for (int i = 0; i < iNumberOfElements; i++) - { - arrMother.Add(i); - } - - arrSon = ArrayList.FixedSize(arrMother); - arrSon2 = ArrayList.ReadOnly(arrMother); - _arrGrandDaughter = ArrayList.Synchronized(arrSon); - _arrDaughter = ArrayList.Synchronized(arrMother); - - Assert.False(arrMother.SyncRoot is ArrayList); - - Assert.False(arrSon.SyncRoot is ArrayList); - - Assert.False(arrSon2.SyncRoot is ArrayList); - - Assert.False(_arrDaughter.SyncRoot is ArrayList); - - Assert.Equal(arrSon.SyncRoot, arrMother.SyncRoot); - - Assert.False(_arrGrandDaughter.SyncRoot is ArrayList); - - //we are going to rumble with the ArrayLists with 2 threads - - workers = new Task[iNumberOfWorkers]; - ts1 = new Action(SortElements); - ts2 = new Action(ReverseElements); - for (int iThreads = 0; iThreads < iNumberOfWorkers; iThreads += 2) - { - workers[iThreads] = Task.Run(ts1); - workers[iThreads + 1] = Task.Run(ts2); - } - - Task.WaitAll(workers); - - //checking time - //Now lets see how this is done. - //Reverse and sort - ascending more likely - //Sort followed up Reverse - descending - fDescending = false; - if (((int)arrMother[0]).CompareTo((int)arrMother[1]) > 0) - fDescending = true; - - fWrongResult = false; - iValue = (int)arrMother[0]; - for (int i = 1; i < iNumberOfElements; i++) - { - if (fDescending) - { - if (iValue.CompareTo((int)arrMother[i]) <= 0) - { - fWrongResult = true; - break; - } - } - else - { - if (iValue.CompareTo((int)arrMother[i]) >= 0) - { - fWrongResult = true; - break; - } - } - iValue = (int)arrMother[i]; - } - - Assert.False(fWrongResult); - } - - void SortElements() - { - _arrGrandDaughter.Sort(); - } - - void ReverseElements() - { - _arrDaughter.Reverse(); - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/ReadOnlyTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/ReadOnlyTests.cs deleted file mode 100644 index 7e84819f87..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/ReadOnlyTests.cs +++ /dev/null @@ -1,167 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_ReadOnlyTests - { - [Fact] - public void TestArrayListParameter() - { - ArrayList alst1; - ArrayList alst2; - ArrayList alst3; - - string strValue; - object oValue; - - //[] Vanila test case - ReadOnly returns an ArrayList that cant be modified - // The idea is that a developer will pass the readonly ArrayList to another user who will not be able to - // modify it!! i.e. the developer can play the pupper Master and change the list afterwards - - alst1 = new ArrayList(); - for (int i = 0; i < 10; i++) - { - strValue = "String_" + i; - alst1.Add(strValue); - } - - alst2 = ArrayList.ReadOnly(alst1); - - for (int i = 0; i < 10; i++) - { - strValue = "String_" + i; - Assert.Equal(strValue, (string)alst2[i]); - } - - //[]now we remove an object from the original list and check the ReadOnly obejct we have of it - alst1.RemoveAt(9); - Assert.Throws<ArgumentOutOfRangeException>(() => { oValue = alst1[9]; }); - - //we cant access this in our readonly list object as well - the object underneath has been cut - Assert.Throws<ArgumentOutOfRangeException>(() => { oValue = alst2[9]; }); - - //[]we cant access remove or add to the readonly list - Assert.Throws<NotSupportedException>(() => alst2.RemoveAt(0)); - Assert.Throws<NotSupportedException>(() => alst2.Remove("String_1")); - - Assert.Throws<NotSupportedException>(() => alst2.Clear()); - Assert.Throws<NotSupportedException>(() => alst2.Add("This sort of thing will not be allowed")); - Assert.Throws<NotSupportedException>(() => alst2.Insert(0, "This sort of thing will not be allowed")); - Assert.Throws<NotSupportedException>(() => - { - strValue = "Hello World"; - alst2[0] = strValue; - }); - - //[]we'll do the ReadOnly test - Assert.False(alst1.IsReadOnly); - - Assert.True(alst2.IsReadOnly); - - //[]we'll get a readonly from this readonly - alst3 = ArrayList.ReadOnly(alst2); - Assert.True(alst2.IsReadOnly); - Assert.True(alst3.IsReadOnly); - - //[]we still cant access the 2nd one :) - //we cant access remove or add to the readonly list - Assert.Throws<NotSupportedException>(() => alst2.RemoveAt(0)); - - // []Try ReadOnly with a null ArrayList - Assert.Throws<ArgumentNullException>(() => - { - ArrayList myArrayList = null; - ArrayList.ReadOnly(myArrayList); - }); - } - - [Fact] - public void TestIListParameter() - { - ArrayList alst1; - IList ilst1; - ArrayList olst1; - - string strValue; - object oValue; - - // [] Vanila test case - ReadOnly returns an IList that cant be modified - // The idea is that a developer will pass the readonly ArrayList to another user who will not be able to - // modify it!! i.e. the developer can play the pupper Master and change the list afterwards - - alst1 = new ArrayList(); - for (int i = 0; i < 10; i++) - { - strValue = "String_" + i; - alst1.Add(strValue); - } - - ilst1 = ArrayList.ReadOnly((IList)alst1); - - for (int i = 0; i < 10; i++) - { - strValue = "String_" + i; - Assert.Equal(strValue, (string)ilst1[i]); - } - - //[]now we remove an object from the original list and check the readonly - alst1.RemoveAt(9); - Assert.Throws<ArgumentOutOfRangeException>(() => { oValue = alst1[9]; }); - Assert.Throws<ArgumentOutOfRangeException>(() => { oValue = ilst1[9]; }); - - //[]we cant access remove or add to the readonly list - Assert.Throws<NotSupportedException>(() => ilst1.RemoveAt(0)); - Assert.Throws<NotSupportedException>(() => ilst1.Remove("String_1")); - Assert.Throws<NotSupportedException>(() => ilst1.Clear()); - Assert.Throws<NotSupportedException>(() => ilst1.Add("This sort of thing will not be allowed")); - Assert.Throws<NotSupportedException>(() => ilst1.Insert(0, "This sort of thing will not be allowed")); - Assert.Throws<NotSupportedException>(() => - { - strValue = "Hello World"; - ilst1[0] = strValue; - }); - - //[]we should be able to get other object that implement IList from this method - olst1 = new ArrayList(); - for (int i = 0; i < 10; i++) - { - strValue = "String_" + i; - olst1.Add(strValue); - } - - ilst1 = ArrayList.ReadOnly((IList)olst1); - for (int i = 0; i < 10; i++) - { - strValue = "String_" + i; - Assert.Equal(strValue, (string)ilst1[i]); - } - - //[]we'll test some of the other methods in IList that should work!! - for (int i = 0; i < 10; i++) - { - strValue = "String_" + i; - Assert.True(ilst1.Contains(strValue)); - - Assert.Equal(i, ilst1.IndexOf(strValue)); - } - - //[]lastly, the readonly test - Assert.False(alst1.IsReadOnly); - - Assert.False(olst1.IsReadOnly); - - Assert.True(ilst1.IsReadOnly); - - // []Try ReadOnly with a null IList - Assert.Throws<ArgumentNullException>(() => - { - IList myIList = null; - ArrayList.ReadOnly(myIList); - }); - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/RemoveAtTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/RemoveAtTests.cs deleted file mode 100644 index de4931e513..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/RemoveAtTests.cs +++ /dev/null @@ -1,103 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_RemoveAtTests - { - [Fact] - public void TestArrayListWrappers() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - int start = 3; - int count = 15; - - string[] strHeroes = - { - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - "Huntress", - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - "Superman", - "Thor", - "Wildcat", - "Wonder Woman", - }; - - string[] strResult = - { - "Aquaman", - "Atom", - "Batman", - "Superman", - "Thor", - "Wildcat", - "Wonder Woman", - }; - - // - // Construct array list. - // - // Construct ArrayList. - arrList = new ArrayList((ICollection)strHeroes); - - //Adapter, GetRange, Synchronized, ReadOnly returns a slightly different version of - //BinarySearch, Following variable cotains each one of these types of array lists - - ArrayList[] arrayListTypes = { - (ArrayList)arrList.Clone(), - (ArrayList)ArrayList.Adapter(arrList).Clone(), - (ArrayList)arrList.GetRange(0, arrList.Count).Clone(), - (ArrayList)ArrayList.Synchronized(arrList).Clone()}; - - foreach (ArrayList arrayListType in arrayListTypes) - { - arrList = arrayListType; - - // - // [] Remove objects from the ArrayList. - // - for (int ii = 0; ii < count; ++ii) - { - arrList.RemoveAt(start); - } - - // Verify the items in the array. - for (int ii = 0; ii < strResult.Length; ++ii) - { - Assert.Equal(0, strResult[ii].CompareTo((string)arrList[ii])); - } - - // - // [] Attempt invalid remove using negative index - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.RemoveAt(-100)); - - // - // [] Attempt remove using out of range index - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.RemoveAt(1000)); - } - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/RemoveRangeTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/RemoveRangeTests.cs deleted file mode 100644 index 83309fb287..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/RemoveRangeTests.cs +++ /dev/null @@ -1,127 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_RemoveRangeTests - { - [Fact] - public void TestArrayListWrappers() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - int start = 3; - int count = 15; - - string[] strHeroes = - { - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - "Huntress", - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - "Superman", - "Thor", - "Wildcat", - "Wonder Woman", - }; - - string[] strResult = - { - "Aquaman", - "Atom", - "Batman", - "Superman", - "Thor", - "Wildcat", - "Wonder Woman", - }; - - // - // Construct array lists. - // - // Construct ArrayList. - arrList = new ArrayList((ICollection)strHeroes); - - //Adapter, GetRange, Synchronized, ReadOnly returns a slightly different version of - //BinarySearch, Following variable cotains each one of these types of array lists - - ArrayList[] arrayListTypes = { - (ArrayList)arrList.Clone(), - (ArrayList)ArrayList.Adapter(arrList).Clone(), - (ArrayList)arrList.GetRange(0, arrList.Count).Clone(), - (ArrayList)ArrayList.Synchronized(arrList).Clone()}; - - foreach (ArrayList arrayListType in arrayListTypes) - { - arrList = arrayListType; - - // - // [] Remove range of items from array list. - // - // Remove range of items. - arrList.RemoveRange(start, count); - - // Verify remove. - for (int ii = 0; ii < strResult.Length; ++ii) - { - Assert.Equal(0, strResult[ii].CompareTo((string)arrList[ii])); - } - - // - // [] Attempt remove range using zero count. - // - // Remove range of items. - arrList.RemoveRange(start, 0); - - // Verify size. - Assert.Equal(strResult.Length, arrList.Count); - - // Verify remove. - for (int ii = 0; ii < strResult.Length; ++ii) - { - Assert.Equal(0, strResult[ii].CompareTo((string)arrList[ii])); - } - - // - // [] Attempt invalid RemoveRange using very large count. - // - Assert.Throws<ArgumentException>(() => arrList.RemoveRange(start, 10005)); - - // - // [] Attempt invalid RemoveRange using negative index - // - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.RemoveRange(-1000, 5)); - - // - // [] Attempt invalid RemoveRange using negative count - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.RemoveRange(start, -1)); - - // - // [] Attempt invalid RemoveRange using out of range index - // - Assert.Throws<ArgumentException>(() => arrList.RemoveRange(1000, 5)); - } - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/RemoveTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/RemoveTests.cs deleted file mode 100644 index f8efb1f223..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/RemoveTests.cs +++ /dev/null @@ -1,76 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_RemoveTests - { - [Fact] - public void TestNullItems() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - - string[] strHeroes = - { - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - null, - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - null, - "Thor", - "Wildcat", - null - }; - - //[] try removing each element make sure count goes down by one also - // Construct ArrayList. - arrList = new ArrayList(strHeroes); - - for (int i = 0; i < strHeroes.Length; i++) - { - arrList.Remove(strHeroes[i]); - Assert.Equal(strHeroes.Length - i - 1, arrList.Count); - } - - //[] make sure count goes back to 0 - // Construct ArrayList. - arrList = new ArrayList(); - arrList.Add(null); - arrList.Add(arrList); - arrList.Add(null); - arrList.Remove(arrList); - arrList.Remove(null); - arrList.Remove(null); - - Assert.Equal(0, arrList.Count); - - //[] remove from empty list - // No Exception - arrList = new ArrayList(); - arrList.Remove(null); - - //[] remove elemnt which does not exist should throw - arrList.Add(arrList); - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/RepeatTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/RepeatTests.cs deleted file mode 100644 index 799651f583..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/RepeatTests.cs +++ /dev/null @@ -1,43 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_RepeatTests - { - [Fact] - public void TestRepeatBasic() - { - ArrayList alst1; - - //[]Vanila test case - Repeat returns an ArrayList with the repeated object n times. - alst1 = ArrayList.Repeat(5, 1000); - - for (int i = 0; i < alst1.Count; i++) - { - Assert.Equal(5, (int)alst1[i]); - } - - alst1 = ArrayList.Repeat(null, 10); - - for (int i = 0; i < alst1.Count; i++) - { - Assert.Null(alst1[i]); - } - - alst1 = ArrayList.Repeat(5, 0); - - Assert.Equal(0, alst1.Count); - - //[]parm value - // - Assert.Throws<ArgumentOutOfRangeException>(() => - { - alst1 = ArrayList.Repeat(5, -1); - }); - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/ReverseTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/ReverseTests.cs deleted file mode 100644 index fe9f060a30..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/ReverseTests.cs +++ /dev/null @@ -1,181 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_ReverseTests - { - [Fact] - public void TestArrayListWrappers() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - - string[] strHeroes = - { - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - "Huntress", - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - "Superman", - "Thor", - "Wildcat", - "Wonder Woman", - }; - - // - // Construct array list. - // - arrList = new ArrayList((ICollection)strHeroes); - - //Adapter, GetRange, Synchronized, ReadOnly returns a slightly different version of - //BinarySearch, Following variable cotains each one of these types of array lists - - ArrayList[] arrayListTypes = { - (ArrayList)arrList.Clone(), - (ArrayList)ArrayList.Adapter(arrList).Clone(), - (ArrayList)ArrayList.FixedSize(arrList).Clone(), - (ArrayList)arrList.GetRange(0, arrList.Count).Clone(), - (ArrayList)ArrayList.Synchronized(arrList).Clone()}; - - foreach (ArrayList arrayListType in arrayListTypes) - { - arrList = arrayListType; - - // - // [] Reverse entire array list. - // - arrList.Reverse(0, arrList.Count); - - // Verify items have been reversed. - for (int ii = 0; ii < arrList.Count; ++ii) - { - Assert.Equal(0, strHeroes[ii].CompareTo((String)arrList[arrList.Count - ii - 1])); - } - - // - // [] Attempt invalid Reverse using negative index - // - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.Reverse(-100, arrList.Count)); - - // - // [] Attempt Reverse using out of range index - // - Assert.Throws<ArgumentException>(() => arrList.Reverse(1000, arrList.Count)); - - // - // [] Attempt Reverse using negative count. - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.Reverse(0, -arrList.Count)); - - // - // [] Attempt Reverse using zero count. - // - arrList.Reverse(0, 0); - - // Verify no reversal (List should still be reveresed of the original.) - for (int ii = 0; ii < arrList.Count; ++ii) - { - Assert.Equal(0, strHeroes[ii].CompareTo((string)arrList[arrList.Count - ii - 1])); - } - } - } - - [Fact] - public void Test02() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - - string[] strHeroes = - { - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - "Huntress", - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - "Superman", - "Thor", - "Wildcat", - "Wonder Woman", - }; - - // - // Construct array list. - // - arrList = new ArrayList((ICollection)strHeroes); - - //Adapter, GetRange, Synchronized, ReadOnly returns a slightly different version of - //BinarySearch, Following variable cotains each one of these types of array lists - - ArrayList[] arrayListTypes = { - (ArrayList)arrList.Clone(), - (ArrayList)ArrayList.Adapter(arrList).Clone(), - (ArrayList)ArrayList.FixedSize(arrList).Clone(), - (ArrayList)arrList.GetRange(0, arrList.Count).Clone(), - (ArrayList)ArrayList.Synchronized(arrList).Clone()}; - - foreach (ArrayList arrayListType in arrayListTypes) - { - arrList = arrayListType; - - // - // [] Reverse entire array list. - // - // Reverse entire array list. - arrList.Reverse(); - - // Verify items have been reversed. - for (int ii = 0; ii < arrList.Count; ++ii) - { - Assert.Equal(0, strHeroes[ii].CompareTo((String)arrList[arrList.Count - ii - 1])); - } - - //[]Team review feedback - Reversing lists of varying sizes inclusing 0 - arrList = new ArrayList(); - arrList.Reverse(); - - arrList = new ArrayList(); - for (int i = 0; i < 1; i++) - arrList.Add(i); - - arrList.Reverse(); - } - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/SortTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/SortTests.cs deleted file mode 100644 index 1a6202a817..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/SortTests.cs +++ /dev/null @@ -1,316 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_SortTests - { - #region "Test Data - Keep the data close to tests so it can vary independently from other tests" - - string[] strHeroes = - { - "Green Arrow", - "Atom", - "Batman", - "Steel", - "Superman", - "Wonder Woman", - "Hawkman", - "Flash", - "Aquaman", - "Green Lantern", - "Catwoman", - "Huntress", - "Robin", - "Captain Atom", - "Wildcat", - "Nightwing", - "Ironman", - "SpiderMan", - "Black Canary", - "Thor", - "Cyborg", - "Captain America", - }; - - string[] strHeroesSorted = - { - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - "Huntress", - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - "Superman", - "Thor", - "Wildcat", - "Wonder Woman", - }; - - #endregion - - [Fact] - public void TestAscendingAndDecendingSort() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - string[] strHeroesUnsorted = null; - - // - // Test ascending sort. - // - // Construct unsorted array. - strHeroesUnsorted = new String[strHeroes.Length]; - System.Array.Copy(strHeroes, 0, strHeroesUnsorted, 0, strHeroes.Length); - - // Sort ascending the array list. - System.Array.Sort(strHeroesUnsorted, 0, strHeroesUnsorted.Length, new SortTests_Assending()); - - // Verify ascending sort. - for (int ii = 0; ii < strHeroesUnsorted.Length; ++ii) - { - Assert.Equal(0, strHeroesSorted[ii].CompareTo(strHeroesUnsorted[ii])); - } - - // - // Test decending sort. - // - // Construct unsorted array. - strHeroesUnsorted = new String[strHeroes.Length]; - System.Array.Copy(strHeroes, 0, strHeroesUnsorted, 0, strHeroes.Length); - - // Sort decending the array list. - System.Array.Sort(strHeroesUnsorted, 0, strHeroesUnsorted.Length, new SortTests_Decending()); - - // Verify descending sort. - for (int ii = 0; ii < strHeroesUnsorted.Length; ++ii) - { - Assert.Equal(0, strHeroesSorted[ii].CompareTo(strHeroesUnsorted[strHeroesUnsorted.Length - ii - 1])); - } - - // - // [] Sort array list using default comparer. - // - arrList = new ArrayList((ICollection)strHeroesUnsorted); - Assert.NotNull(arrList); - - // Sort decending the array list. - arrList.Sort(null); - - // Verify sort. - for (int ii = 0; ii < arrList.Count; ++ii) - { - Assert.Equal(0, strHeroesSorted[ii].CompareTo((string)arrList[ii])); - } - - // - // [] Sort array list our ascending comparer. - // - arrList = new ArrayList((ICollection)strHeroesUnsorted); - Assert.NotNull(arrList); - - // Sort decending the array list. - arrList.Sort(new SortTests_Assending()); - - // Verify sort. - for (int ii = 0; ii < arrList.Count; ++ii) - { - Assert.Equal(0, strHeroesSorted[ii].CompareTo((String)arrList[ii])); - } - // - // [] Sort array list our ascending comparer. - // - arrList = new ArrayList((ICollection)strHeroesUnsorted); - Assert.NotNull(arrList); - - // Sort decending the array list. - arrList.Sort(new SortTests_Decending()); - - // Verify sort. - for (int ii = 0, jj = arrList.Count - 1; ii < arrList.Count; ++ii, jj--) - { - Assert.Equal(0, strHeroesSorted[ii].CompareTo((string)arrList[jj])); - } - } - - [Fact] - public void TestInvalidIndexOrCount() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - string[] strHeroesUnsorted = null; - - // - // Test ascending sort. - // - // Construct unsorted array. - strHeroesUnsorted = new String[strHeroes.Length]; - System.Array.Copy(strHeroes, 0, strHeroesUnsorted, 0, strHeroes.Length); - - // Sort ascending the array list. - System.Array.Sort(strHeroesUnsorted, 0, strHeroesUnsorted.Length, new SortTests_Assending()); - - // Verify ascending sort. - for (int ii = 0; ii < strHeroesUnsorted.Length; ++ii) - { - Assert.Equal(0, strHeroesSorted[ii].CompareTo(strHeroesUnsorted[ii])); - } - - // - // Test decending sort. - // - // Construct unsorted array. - strHeroesUnsorted = new String[strHeroes.Length]; - System.Array.Copy(strHeroes, 0, strHeroesUnsorted, 0, strHeroes.Length); - - // Sort decending the array list. - System.Array.Sort(strHeroesUnsorted, 0, strHeroesUnsorted.Length, new SortTests_Decending()); - - // Verify descending sort. - for (int ii = 0; ii < strHeroesUnsorted.Length; ++ii) - { - Assert.Equal(0, strHeroesSorted[ii].CompareTo(strHeroesUnsorted[strHeroesUnsorted.Length - ii - 1])); - } - - arrList = new ArrayList((ICollection)strHeroesUnsorted); - - //Adapter, GetRange, Synchronized, ReadOnly returns a slightly different version of - //BinarySearch, Following variable cotains each one of these types of array lists - - ArrayList[] arrayListTypes = { - (ArrayList)arrList.Clone(), - (ArrayList)ArrayList.Adapter(arrList).Clone(), - (ArrayList)ArrayList.FixedSize(arrList).Clone(), - (ArrayList)arrList.GetRange(0, arrList.Count).Clone(), - (ArrayList)ArrayList.Synchronized(arrList).Clone()}; - - - foreach (ArrayList arrayListType in arrayListTypes) - { - arrList = arrayListType; - - // - // [] Sort array list using default comparer. - // - Assert.NotNull(arrList); - - // Sort decending the array list. - arrList.Sort(0, arrList.Count, null); - - // Verify sort. - for (int ii = 0; ii < arrList.Count; ++ii) - { - Assert.Equal(0, strHeroesSorted[ii].CompareTo((string)arrList[ii])); - } - - // - // [] Bogus negative index. - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.Sort(-1000, arrList.Count, null)); - - // - // [] Bogus out of bounds index. - // - Assert.Throws<ArgumentException>(() => arrList.Sort(1000, arrList.Count, null)); - - // - // [] Bogus negative size parmeter. - // - Assert.Throws<ArgumentOutOfRangeException>(() => arrList.Sort(0, -1000, null)); - - // - // [] Bogus out of bounds size parmeter. - // - Assert.Throws<ArgumentException>(() => arrList.Sort(0, 1000, null)); - } - } - - internal class SortTests_Assending : IComparer - { - public virtual int Compare(Object x, Object y) - { - return ((String)x).CompareTo((String)y); - } - } - internal class SortTests_Decending : IComparer - { - public virtual int Compare(Object x, Object y) - { - return -((String)x).CompareTo((String)y); - } - } - - [Fact] - public void TestMultipleDataTypes() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - // - // [] Sort array list using default comparer. - // - arrList = new ArrayList((ICollection)strHeroes); - - // Sort decending the array list. - arrList.Sort(); - - // Verify sort. - for (int ii = 0; ii < arrList.Count; ++ii) - { - Assert.Equal(0, strHeroesSorted[ii].CompareTo((String)arrList[ii])); - } - - //[]Team review feedback - Sort an empty ArrayList - arrList = new ArrayList(); - arrList.Sort(); - - Assert.Equal(0, arrList.Count); - - //[] Sort an ArrayList with multiple data types. This will throw - short i16; - int i32; - long i64; - ushort ui16; - uint ui32; - ulong ui64; - ArrayList alst; - - i16 = 1; - i32 = 2; - i64 = 3; - ui16 = 4; - ui32 = 5; - ui64 = 6; - alst = new ArrayList(); - - alst.Add(i16); - alst.Add(i32); - alst.Add(i64); - alst.Add(ui16); - alst.Add(ui32); - alst.Add(ui64); - - Assert.Throws<InvalidOperationException>(() => alst.Sort()); - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/SynchronizedTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/SynchronizedTests.cs deleted file mode 100644 index 6c5d591e5d..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/SynchronizedTests.cs +++ /dev/null @@ -1,371 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Threading.Tasks; -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_SynchronizedTests - { - private IList _ilst1; - private Int32 _iNumberOfElements = 10; - private const string _prefix = "String_"; - - private void AddElements() - { - for (int i = 0; i < _iNumberOfElements; i++) - { - _ilst1.Add(_prefix + i); - } - } - - private void RemoveElements() - { - for (int i = 0; i < _iNumberOfElements; i++) - { - _ilst1.Remove(_prefix + i); - } - } - - [Fact] - public void TestIListParameterMultiThreads() - { - ArrayList alst1; - int iNumberOfTimes; - string strValue; - - Task[] workers; - Action ts1; - int iNumberOfWorkers = 10; - - //[] Vanila test case - Synchronized returns an IList that is thread safe - // We will try to test this by getting a number of threads to write some items - // to a synchronized IList - - alst1 = new ArrayList(); - _ilst1 = ArrayList.Synchronized((IList)alst1); - - workers = new Task[iNumberOfWorkers]; - ts1 = new Action(AddElements); - // - for (int i = 0; i < workers.Length; i++) - { - workers[i] = Task.Run(ts1); - } - - Task.WaitAll(workers); - - //checking time - Assert.Equal(_iNumberOfElements * iNumberOfWorkers, _ilst1.Count); - - for (int i = 0; i < _iNumberOfElements; i++) - { - iNumberOfTimes = 0; - strValue = _prefix + i; - for (int j = 0; j < _ilst1.Count; j++) - { - if (((string)_ilst1[j]).Equals(strValue)) - iNumberOfTimes++; - } - - Assert.Equal(iNumberOfTimes, iNumberOfWorkers); - } - - workers = new Task[iNumberOfWorkers]; - ts1 = new Action(RemoveElements); - // - for (int i = 0; i < workers.Length; i++) - { - workers[i] = Task.Run(ts1); - } - - Task.WaitAll(workers); - - Assert.Equal(0, _ilst1.Count); - Assert.False(alst1.IsSynchronized); - Assert.True(_ilst1.IsSynchronized); - - // [] Check null value - Assert.Throws<ArgumentNullException>(() => - { - IList myIList = null; - ArrayList.Synchronized(myIList); - }); - } - - - public ArrayList arrList = null; - public Hashtable hash = null; // this will verify that threads will only add elements the num of times - // they are specified to - public class MyArrrayList - { - } - - [Fact] - public void TestArrayListParameterMultiThreads() - { - // [] make 40 threads which add strHeroes to an ArrayList - // the outcome is that the length of the ArrayList should be the same size as the strHeroes array - arrList = new ArrayList(); - arrList = ArrayList.Synchronized(arrList); - - hash = new Hashtable(); // Synchronized Hashtable - hash = Hashtable.Synchronized(hash); - - Task[] workers = new Task[7]; - - // initialize the threads - for (int i = 0; i < workers.Length; i++) - { - var name = "ThreadID " + i.ToString(); - Action delegStartMethod = () => AddElems(name); - workers[i] = Task.Run(delegStartMethod); - } - - Task.WaitAll(workers); - - Assert.Equal(workers.Length * strHeroes.Length, arrList.Count); - - // [] Check null value - Assert.Throws<ArgumentNullException>(() => - { - ArrayList myArrayList = null; - ArrayList.Synchronized(myArrayList); - }); - } - - public void AddElems(string currThreadName) - { - int iNumTimesThreadUsed = 0; - - for (int i = 0; i < strHeroes.Length; i++) - { - // to test that we only use the right threads the right number of times keep track with the hashtable - // how many times we use this thread - try - { - hash.Add(currThreadName, null); - // this test assumes ADD will throw for dup elements - } - catch (ArgumentException) - { - iNumTimesThreadUsed++; - } - - Assert.NotNull(arrList); - Assert.True(arrList.IsSynchronized); - - arrList.Add(strHeroes[i]); - } - - Assert.Equal(strHeroes.Length - 1, iNumTimesThreadUsed); - } - - #region "Test Data - Keep the data close to tests so it can vary independently from other tests" - - public static string[] strHeroes = new string[] - { - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - null, - "Thor", - "Wildcat", - null, - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - null, - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - null, - "Thor", - "Wildcat", - null, - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - null, - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - null, - "Thor", - "Wildcat", - null, - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - null, - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - null, - "Thor", - "Wildcat", - null, - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - null, - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - null, - "Thor", - "Wildcat", - null, - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - null, - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - null, - "Thor", - "Wildcat", - null, - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - null, - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - null, - "Thor", - "Wildcat", - null, - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - null, - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - null, - "Thor", - "Wildcat", - null, - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - "Captain Atom", - "Catwoman", - "Cyborg", - "Flash", - "Green Arrow", - "Green Lantern", - "Hawkman", - null, - "Ironman", - "Nightwing", - "Robin", - "SpiderMan", - "Steel", - null, - "Thor", - "Wildcat", - null, - "Aquaman", - "Atom", - "Batman", - "Black Canary", - "Captain America", - }; - - #endregion - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/ToArrayTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/ToArrayTests.cs deleted file mode 100644 index e96c874ef8..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/ToArrayTests.cs +++ /dev/null @@ -1,86 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_ToArrayTests - { - [Fact] - public void TestToArrayBasic() - { - ArrayList alst1; - string strValue; - object[] oArr; - - //[] Vanila test case - ToArray returns an array of this. We will not extensively test this method as - // this is a thin wrapper on Array.Copy which is extensively tested - alst1 = new ArrayList(); - for (int i = 0; i < 10; i++) - { - strValue = "String_" + i; - alst1.Add(strValue); - } - - oArr = alst1.ToArray(); - for (int i = 0; i < 10; i++) - { - strValue = "String_" + i; - Assert.Equal(strValue, (string)oArr[i]); - } - - //[]lets try an empty list - alst1 = new ArrayList(); - oArr = alst1.ToArray(); - Assert.Equal(0, oArr.Length); - } - - [Fact] - public void TestArrayListWrappers() - { - ArrayList alst1; - string strValue; - Array arr1; - - //[] Vanila test case - ToArray returns an array of this. We will not extensively test this method as - // this is a thin wrapper on Array.Copy which is extensively tested elsewhere - alst1 = new ArrayList(); - for (int i = 0; i < 10; i++) - { - strValue = "String_" + i; - alst1.Add(strValue); - } - - ArrayList[] arrayListTypes = { - alst1, - ArrayList.Adapter(alst1), - ArrayList.FixedSize(alst1), - alst1.GetRange(0, alst1.Count), - ArrayList.ReadOnly(alst1), - ArrayList.Synchronized(alst1)}; - - foreach (ArrayList arrayListType in arrayListTypes) - { - alst1 = arrayListType; - arr1 = alst1.ToArray(typeof(string)); - - for (int i = 0; i < 10; i++) - { - strValue = "String_" + i; - Assert.Equal(strValue, (string)arr1.GetValue(i)); - } - - //[] this should be covered in Array.Copy, but lets do it for - Assert.Throws<InvalidCastException>(() => { arr1 = alst1.ToArray(typeof(int)); }); - Assert.Throws<ArgumentNullException>(() => { arr1 = alst1.ToArray(null); }); - } - - //[]lets try an empty list - alst1.Clear(); - arr1 = alst1.ToArray(typeof(Object)); - Assert.Equal(0, arr1.Length); - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/TrimToSizeTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/TrimToSizeTests.cs deleted file mode 100644 index 2234519997..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/TrimToSizeTests.cs +++ /dev/null @@ -1,62 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Collections.Tests -{ - public class ArrayList_TrimToSizeTests - { - [Fact] - public void TestTrimToSizeBasic() - { - //-------------------------------------------------------------------------- - // Variable definitions. - //-------------------------------------------------------------------------- - ArrayList arrList = null; - - string[] strHeroes = - { - "Green Arrow", - "Atom", - "Batman", - "Steel", - "Superman", - "Wonder Woman", - "Hawkman", - "Flash", - "Aquaman", - "Green Lantern", - "Catwoman", - "Huntress", - "Robin", - "Captain Atom", - "Wildcat", - "Nightwing", - "Ironman", - "SpiderMan", - "Black Canary", - "Thor", - "Cyborg", - "Captain America", - }; - - // - // Construct array list. - // - arrList = new ArrayList((ICollection)strHeroes); - - // - // [] Verify TrimToSize. - // - // Set capacity greater than the size of the ArrayList. - arrList.Capacity = 2 * arrList.Count; - Assert.True(arrList.Capacity > arrList.Count); - - // Verify TrimToSize - arrList.TrimToSize(); - Assert.Equal(arrList.Count, arrList.Capacity); - } - } -} diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/WrapperTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/WrapperTests.cs deleted file mode 100644 index bdbb85f4ad..0000000000 --- a/src/System.Collections.NonGeneric/tests/ArrayList/WrapperTests.cs +++ /dev/null @@ -1,2463 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - - -/*** The goal of this program is to call the methods of the base class on the wrapper - classes to ensure that the right thing is happening in ArrayList - */ - -using System; -using System.Collections; -using Xunit; - -public class WrapperTests -{ - [Fact] - public void TestFixedSize() - { - ArrayList alst = null; - ArrayList tst = null; - IList ilst1 = null; - IList ilst2 = null; - - Hashtable hsh1 = null; - - //[] Adapter - alst = new ArrayList(); - tst = ArrayList.Adapter(alst); - hsh1 = new Hashtable(); - - CompareObjects(alst, tst, hsh1); - - // We should always do a string comparison - Assert.True(hsh1.Count < 3); - Assert.Equal("get", (string)hsh1["Capacity"]); - Assert.Equal("Monekeyed, 301 301", (string)hsh1["TrimToSize"]); - - alst = new ArrayList(); - for (int i = 0; i < 100; i++) - alst.Add(i); - tst = ArrayList.Adapter(alst); - - hsh1 = new Hashtable(); - CompareObjects(alst, tst, hsh1); - Assert.True(hsh1.Count < 3); - Assert.Equal("get", (string)hsh1["Capacity"]); - Assert.Equal("Monekeyed, 301 301", (string)hsh1["TrimToSize"]); - - //[] FixedSize - ArrayList - alst = new ArrayList(); - tst = ArrayList.FixedSize(alst); - - hsh1 = new Hashtable(); - CompareObjects(alst, tst, hsh1); - Assert.True(hsh1.Count < 2); - Assert.True(hsh1.ContainsKey("Fixed")); - - alst = new ArrayList(); - for (int i = 0; i < 100; i++) - alst.Add(i); - tst = ArrayList.FixedSize(alst); - - hsh1 = new Hashtable(); - CompareObjects(alst, tst, hsh1); - Assert.True(hsh1.Count < 2); - Assert.True(hsh1.ContainsKey("Fixed")); - - //[] FixedSize - IList - ilst1 = new ArrayList(); - ilst2 = ArrayList.FixedSize(ilst1); - - hsh1 = new Hashtable(); - DoIListTests(ilst1, ilst2, hsh1); - Assert.True(hsh1.Count < 2); - Assert.True(hsh1.ContainsKey("Fixed")); - - ilst1 = new ArrayList(); - for (int i = 0; i < 100; i++) - { - ilst1.Add(i); - } - ilst2 = ArrayList.FixedSize(ilst1); - - hsh1 = new Hashtable(); - DoIListTests(ilst1, ilst2, hsh1); - Assert.True(hsh1.Count < 2); - Assert.True(hsh1.ContainsKey("Fixed")); - } - - [Fact] - public void TestReadOnly() - { - //[] ReadOnly - ArrayList - ArrayList alst = new ArrayList(); - ArrayList tst = ArrayList.ReadOnly(alst); - - Hashtable hsh1 = new Hashtable(); - CompareObjects(alst, tst, hsh1); - Assert.True(hsh1.Count < 3); - Assert.True(hsh1.ContainsKey("IsReadOnly")); - Assert.Equal("Exception not thrown, (Object, IComparer)", (string)hsh1["BinarySearch"]); - - alst = new ArrayList(); - for (int i = 0; i < 100; i++) - alst.Add(i); - tst = ArrayList.ReadOnly(alst); - - hsh1 = new Hashtable(); - CompareObjects(alst, tst, hsh1); - Assert.True(hsh1.Count < 3); - Assert.True(hsh1.ContainsKey("IsReadOnly")); - Assert.Equal("Exception not thrown, (Object, IComparer)", (string)hsh1["BinarySearch"]); - - //[] ReadOnly - IList - IList ilst1 = new ArrayList(); - IList ilst2 = ArrayList.ReadOnly(ilst1); - - hsh1 = new Hashtable(); - DoIListTests(ilst1, ilst2, hsh1); - Assert.True(hsh1.Count < 2); - Assert.True(hsh1.ContainsKey("IsReadOnly")); - - ilst1 = new ArrayList(); - for (int i = 0; i < 100; i++) - { - ilst1.Add(i); - } - ilst2 = ArrayList.ReadOnly(ilst1); - - hsh1 = new Hashtable(); - DoIListTests(ilst1, ilst2, hsh1); - Assert.True(hsh1.Count < 2); - Assert.True(hsh1.ContainsKey("IsReadOnly")); - } - - [Fact] - public void TestSynchronized() - { - //[] Synchronized - ArrayList - ArrayList alst = new ArrayList(); - ArrayList tst = ArrayList.Synchronized(alst); - - Hashtable hsh1 = new Hashtable(); - CompareObjects(alst, tst, hsh1); - Assert.True(hsh1.Count < 2); - Assert.True(hsh1.ContainsKey("IsSynchronized")); - - alst = new ArrayList(); - for (int i = 0; i < 100; i++) - alst.Add(i); - tst = ArrayList.Synchronized(alst); - - hsh1 = new Hashtable(); - CompareObjects(alst, tst, hsh1); - Assert.True(hsh1.Count < 2); - Assert.True(hsh1.ContainsKey("IsSynchronized")); - - //[] Synchronized - IList - IList ilst1 = new ArrayList(); - IList ilst2 = ArrayList.Synchronized(ilst1); - - hsh1 = new Hashtable(); - DoIListTests(ilst1, ilst2, hsh1); - Assert.True(hsh1.Count < 2); - Assert.True(hsh1.ContainsKey("IsSynchronized")); - - ilst1 = new ArrayList(); - for (int i = 0; i < 100; i++) - { - ilst1.Add(i); - } - ilst2 = ArrayList.Synchronized(ilst1); - - hsh1 = new Hashtable(); - DoIListTests(ilst1, ilst2, hsh1); - Assert.True(hsh1.Count < 2); - Assert.True(hsh1.ContainsKey("IsSynchronized")); - } - - [Fact] - public void TestRange() - { - //[]Range - //ArrayList changes in any way for all operation in the Range ArrayList. - //Rather than change CompareObjects which is working for all the other collections, we will implement our version - //of ComapreObjects for Range - ArrayList alst = new ArrayList(); - ArrayList tst = alst.GetRange(0, 0); - - Hashtable hsh1 = new Hashtable(); - CompareRangeObjects(alst, tst, hsh1); - Assert.True(hsh1.Count < 2); - Assert.True(hsh1.ContainsKey("TrimToSize")); - - alst = new ArrayList(); - for (int i = 0; i < 100; i++) - alst.Add(i); - tst = alst.GetRange(0, 100); - - hsh1 = new Hashtable(); - CompareRangeObjects(alst, tst, hsh1); - Assert.True(hsh1.Count < 2); - Assert.True(hsh1.ContainsKey("TrimToSize")); - } - - [Fact] - public void TestMultiDataTypes() - { - short i16 = 1; - int i32 = 2; - long i64 = 3; - ushort ui16 = 4; - uint ui32 = 5; - ulong ui64 = 6; - - ArrayList alst = new ArrayList(); - alst.Add(i16); - alst.Add(i32); - alst.Add(i64); - alst.Add(ui16); - alst.Add(ui32); - alst.Add(ui64); - - //[] we make sure that ArrayList only return true for Contains() when both the value and the type match - //in numeric types - for (int i = 0; i < alst.Count; i++) - { - Assert.True(!alst.Contains(i) || i == 2); - } - - //[]IndexOf should also work in this context - for (int i = 0; i < alst.Count; i++) - { - Assert.True((alst.IndexOf(i) != -1) || (i != 2)); - Assert.True((alst.IndexOf(i) == -1) || (i == 2)); - } - - //[]Sort should fail cause the objects are of different types - Assert.Throws<InvalidOperationException>(() => alst.Sort()); - } - - private void CompareObjects(ArrayList good, ArrayList bad, Hashtable hsh1) - { - //IList, this includes ICollection tests as well!! - DoIListTests(good, bad, hsh1); - - //we will now test ArrayList specific methods - good.Clear(); - for (int i = 0; i < 100; i++) - good.Add(i); - - //AL's CopyTo methods - int[] iArr1 = null; - int[] iArr2 = null; - iArr1 = new int[100]; - iArr2 = new int[100]; - good.CopyTo(iArr1); - bad.CopyTo(iArr2); - for (int i = 0; i < 100; i++) - { - if (iArr1[i] != iArr2[i]) - hsh1["CopyTo"] = "()"; - } - - iArr1 = new int[100]; - iArr2 = new int[100]; - good.CopyTo(0, iArr1, 0, 100); - try - { - bad.CopyTo(0, iArr2, 0, 100); - for (int i = 0; i < 100; i++) - { - if (iArr1[i] != iArr2[i]) - hsh1["CopyTo"] = "()"; - } - } - catch - { - hsh1["CopyTo"] = "(int, Array, int, int)"; - } - - iArr1 = new int[200]; - iArr2 = new int[200]; - for (int i = 0; i < 200; i++) - { - iArr1[i] = 50; - iArr2[i] = 50; - } - - good.CopyTo(50, iArr1, 100, 20); - try - { - bad.CopyTo(50, iArr2, 100, 20); - for (int i = 0; i < 200; i++) - { - if (iArr1[i] != iArr2[i]) - hsh1["CopyTo"] = "(Array, int, int)"; - } - } - catch - { - hsh1["CopyTo"] = "(int, Array, int, int)"; - } - - //Clone() - ArrayList alstClone = (ArrayList)bad.Clone(); - //lets make sure that the clone is what it says it is - if (alstClone.Count != bad.Count) - hsh1["Clone"] = "Count"; - for (int i = 0; i < bad.Count; i++) - { - if (alstClone[i] != bad[i]) - hsh1["Clone"] = "[]"; - } - - //GerEnumerator() - IEnumerator ienm1 = null; - IEnumerator ienm2 = null; - - ienm1 = good.GetEnumerator(0, 100); - try - { - ienm2 = bad.GetEnumerator(0, 100); - DoIEnumerableTest(ienm1, ienm2, good, bad, hsh1, false); - } - catch - { - hsh1["GetEnumerator"] = "(int, int)"; - } - - ienm1 = good.GetEnumerator(50, 50); - try - { - ienm2 = bad.GetEnumerator(50, 50); - DoIEnumerableTest(ienm1, ienm2, good, bad, hsh1, false); - } - catch - { - hsh1["GetEnumerator"] = "(int, int)"; - } - - try - { - bad.GetEnumerator(50, 150); - hsh1["GetEnumerator"] = "(int, int)"; - } - catch (Exception) - { - } - - ienm1 = good.GetEnumerator(0, 100); - try - { - ienm2 = bad.GetEnumerator(0, 100); - good.RemoveAt(0); - DoIEnumerableTest(ienm1, ienm2, good, bad, hsh1, true); - } - catch - { - hsh1["GetEnumerator"] = "(int, int)"; - } - - //GetRange - good.Clear(); - for (int i = 0; i < 100; i++) - good.Add(i); - - ArrayList alst1 = good.GetRange(0, good.Count); - try - { - ArrayList alst2 = bad.GetRange(0, good.Count); - for (int i = 0; i < good.Count; i++) - { - if (alst1[i] != alst2[i]) - hsh1["GetRange"] = i; - } - } - catch - { - hsh1["Range"] = "(int, int)"; - } - - //IndexOf(Object, int) - - if (bad.Count > 0) - { - for (int i = 0; i < good.Count; i++) - { - if (good.IndexOf(good[i], 0) != bad.IndexOf(good[i], 0)) - { - hsh1["IndexOf"] = "(Object, int)"; - } - if (good.IndexOf(good[i], i) != bad.IndexOf(good[i], i)) - { - hsh1["IndexOf"] = "(Object, int)"; - } - if (i < (good.Count - 1)) - { - if (good.IndexOf(good[i], i + 1) != bad.IndexOf(good[i], i + 1)) - { - hsh1["IndexOf"] = "(Object, int)"; - } - } - } - - try - { - bad.IndexOf(1, -1); - } - catch (ArgumentException) - { - } - catch (Exception ex) - { - hsh1["IndexOf"] = ex; - } - - try - { - bad.IndexOf(1, bad.Count); - } - catch (ArgumentException) - { - } - catch (Exception ex) - { - hsh1["IndexOf"] = ex; - } - - // IndexOf(Object, int, int) - // The semantics of this method has changed, the 3rd parameter now refers to count instead of length - for (int i = 0; i < good.Count; i++) - { - if (good.IndexOf(good[i], 0, good.Count - 1) != bad.IndexOf(good[i], 0, good.Count - 1)) - { - hsh1["IndexOf"] = "(Object, int, int)"; - } - if (good.IndexOf(good[i], i, good.Count - i) != bad.IndexOf(good[i], i, good.Count - i)) - { - hsh1["IndexOf"] = "(Object, int)"; - } - if (good.IndexOf(good[i], i, 0) != bad.IndexOf(good[i], i, 0)) - { - hsh1["IndexOf"] = "(Object, int)"; - } - if (i < (good.Count - 1)) - { - if (good.IndexOf(good[i], i + 1, good.Count - (i + 1)) != bad.IndexOf(good[i], i + 1, good.Count - (i + 1))) - { - hsh1["IndexOf"] = "(Object, int)"; - } - } - } - - try - { - bad.IndexOf(1, 0, -1); - } - catch (ArgumentException) - { - } - catch (Exception ex) - { - hsh1["IndexOf"] = ex; - } - - try - { - bad.IndexOf(1, 0, bad.Count); - } - catch (ArgumentException) - { - } - catch (Exception ex) - { - hsh1["IndexOf"] = ex; - } - - try - { - bad.IndexOf(1, bad.Count - 1, bad.Count - 2); - } - catch (ArgumentException) - { - } - catch (Exception ex) - { - hsh1["IndexOf"] = ex; - } - - //LastIndexOf(Object) - for (int i = 0; i < good.Count; i++) - { - if (good.LastIndexOf(good[i]) != bad.LastIndexOf(good[i])) - { - hsh1["LastIndexOf"] = "(Object)"; - } - if (good.LastIndexOf(i + 1000) != bad.LastIndexOf(i + 1000)) - { - hsh1["LastIndexOf"] = "(Object)"; - } - } - - try - { - bad.LastIndexOf(null); - } - catch (ArgumentException) - { - } - catch (Exception ex) - { - hsh1["LastIndexOf"] = ex; - } - - //LastIndexOf(Object, int) - for (int i = 0; i < good.Count; i++) - { - if (good.LastIndexOf(good[i], good.Count - 1) != bad.LastIndexOf(good[i], good.Count - 1)) - { - hsh1["LastIndexOf"] = "(Object, int)"; - } - if (good.LastIndexOf(good[i], 0) != bad.LastIndexOf(good[i], 0)) - { - hsh1["LastIndexOf"] = "(Object, int)"; - } - if (good.LastIndexOf(good[i], i) != bad.LastIndexOf(good[i], i)) - { - hsh1["LastIndexOf"] = "(Object, int)"; - } - if (i < (good.Count - 1)) - { - if (good.LastIndexOf(good[i], i + 1) != bad.LastIndexOf(good[i], i + 1)) - { - hsh1["LastIndexOf"] = "(Object, int)"; - } - } - } - - try - { - bad.LastIndexOf(1, -1); - } - catch (ArgumentException) - { - } - catch (Exception ex) - { - hsh1["LastIndexOf"] = ex; - } - - try - { - bad.LastIndexOf(1, bad.Count); - } - catch (ArgumentException) - { - } - catch (Exception ex) - { - hsh1["LastIndexOf"] = ex; - } - - //LastIndexOf(Object, int, int) - for (int i = 0; i < good.Count; i++) - { - if (good.LastIndexOf(good[i], good.Count - 1, 0) != bad.LastIndexOf(good[i], good.Count - 1, 0)) - { - hsh1["LastIndexOf"] = "(Object, int, int)"; - } - if (good.LastIndexOf(good[i], good.Count - 1, i) != bad.LastIndexOf(good[i], good.Count - 1, i)) - { - hsh1["LastIndexOf"] = "(Object, int)"; - } - if (good.LastIndexOf(good[i], i, i) != bad.LastIndexOf(good[i], i, i)) - { - hsh1["LastIndexOf"] = "(Object, int)"; - } - if (i < (good.Count - 1)) - { - if (good.LastIndexOf(good[i], good.Count - 1, i + 1) != bad.LastIndexOf(good[i], good.Count - 1, i + 1)) - { - hsh1["LastIndexOf"] = "(Object, int)"; - } - } - } - - try - { - bad.LastIndexOf(1, 1, -1); - } - catch (ArgumentException) - { - } - catch (Exception ex) - { - hsh1["LastIndexOf"] = ex; - } - - try - { - bad.LastIndexOf(1, bad.Count - 2, bad.Count - 1); - } - catch (ArgumentException) - { - } - catch (Exception ex) - { - hsh1["LastIndexOf"] = ex; - } - } - - //ReadOnly() - ArrayList alst3 = ArrayList.ReadOnly(bad); - if (!alst3.IsReadOnly) - hsh1["ReadOnly"] = "Not"; - - IList ilst1 = ArrayList.ReadOnly((IList)bad); - if (!ilst1.IsReadOnly) - hsh1["ReadOnly"] = "Not"; - - //Synchronized() - alst3 = ArrayList.Synchronized(bad); - if (!alst3.IsSynchronized) - hsh1["Synchronized"] = "Not"; - - ilst1 = ArrayList.Synchronized((IList)bad); - if (!ilst1.IsSynchronized) - hsh1["Synchronized"] = "Not"; - - //ToArray() - if (good.Count == bad.Count) - { - object[] oArr1 = good.ToArray(); - object[] oArr2 = bad.ToArray(); - for (int i = 0; i < good.Count; i++) - { - if ((int)oArr1[i] != (int)oArr2[i]) - hsh1["ToArray"] = "()"; - } - - //ToArray(type) - iArr1 = (int[])good.ToArray(typeof(int)); - iArr2 = (int[])bad.ToArray(typeof(int)); - for (int i = 0; i < good.Count; i++) - { - if (iArr1[i] != iArr2[i]) - hsh1["ToArray"] = "(Type)"; - } - } - - //Capacity - get - if (good.Capacity != bad.Capacity) - { - hsh1["Capacity"] = "get"; - } - - //Fixed size methods - if (!hsh1.ContainsKey("IsReadOnly")) - { - good.Clear(); - for (int i = 100; i > 0; i--) - good.Add(i); - //Sort() & BinarySearch(Object) - bad.Sort(); - for (int i = 0; i < bad.Count - 1; i++) - { - if ((int)bad[i] > (int)bad[i + 1]) - hsh1["Sort"] = "()"; - } - - for (int i = 0; i < bad.Count; i++) - { - if (bad.BinarySearch(bad[i]) != i) - hsh1["BinarySearch"] = "(Object)"; - } - - //Reverse() - bad.Reverse(); - if (bad.Count > 0) - { - for (int i = 0; i < 99; i++) - { - if ((int)bad[i] < (int)bad[i + 1]) - hsh1["Reverse"] = "()"; - } - - good.Clear(); - for (int i = 100; i > 0; i--) - good.Add(i.ToString()); - } - - good.Clear(); - for (int i = 90; i > 64; i--) - good.Add(((Char)i).ToString()); - - try - { - bad.Sort(new CaseInsensitiveComparer()); - if (bad.Count > 0) - { - for (int i = 0; i < (bad.Count - 1); i++) - { - if (((String)bad[i]).CompareTo(((String)bad[i + 1])) >= 0) - hsh1["Sort"] = "(IComparer)"; - } - for (int i = 0; i < bad.Count; i++) - { - if (bad.BinarySearch(bad[i], new CaseInsensitiveComparer()) != i) - hsh1["BinarySearch"] = "(Object, IComparer)"; - } - } - bad.Reverse(); - - good.Clear(); - for (int i = 65; i < 91; i++) - good.Add(((Char)i).ToString()); - - if (bad.Count > 0) - { - for (int i = 0; i < good.Count; i++) - { - if (bad.BinarySearch(0, bad.Count, bad[i], new CaseInsensitiveComparer()) != i) - hsh1["BinarySearch"] = "(int, int, Object, IComparer)"; - } - } - } - catch (Exception) - { - } - - good.Clear(); - for (int i = 0; i < 100; i++) - good.Add(i); - - Queue que = new Queue(); - for (int i = 0; i < 100; i++) - que.Enqueue(i + 5000); - - try - { - bad.SetRange(0, que); - } - catch (Exception ex) - { - hsh1["SetRange"] = "Copy_ExceptionType, " + ex.GetType().Name; - } - for (int i = bad.Count; i < bad.Count; i++) - { - if ((int)bad[i] != (i + 5000)) - { - hsh1["SetRange"] = i; - } - } - } - else - { - //we make sure that the above methods throw here - good.Clear(); - for (int i = 100; i > 0; i--) - good.Add(i); - - try - { - bad.Sort(); - hsh1["Sort"] = "Copy"; - } - catch (NotSupportedException) - { - } - catch (Exception) - { - hsh1["Sort"] = "Copy_ExceptionType"; - } - - try - { - bad.Reverse(); - hsh1["Reverse"] = "Copy"; - } - catch (NotSupportedException) - { - } - catch (Exception ex) - { - hsh1["Reverse"] = "Copy_ExceptionType, " + ex.GetType().Name; - } - - try - { - bad.Sort(new CaseInsensitiveComparer()); - hsh1["Sort"] = "Copy - Icomparer"; - } - catch (NotSupportedException) - { - } - catch (Exception) - { - hsh1["Sort"] = "Copy_ExceptionType"; - } - - try - { - bad.Sort(0, 0, new CaseInsensitiveComparer()); - hsh1["Sort"] = "Copy - int, int, IComparer"; - } - catch (NotSupportedException) - { - } - catch (Exception) - { - hsh1["Sort"] = "Copy_ExceptionType"; - } - - //BinarySearch - try - { - for (int i = 0; i < bad.Count; i++) - { - if (bad.BinarySearch(bad[i]) != i) - hsh1["BinarySearch"] = "(Object)"; - } - hsh1["BinarySearch"] = "(Object)"; - } - catch (NotSupportedException) - { - } - catch (Exception ex) - { - hsh1["BinarySearch"] = ex.GetType().Name; - } - - try - { - for (int i = 0; i < bad.Count; i++) - { - if (bad.BinarySearch(bad[i], new CaseInsensitiveComparer()) != i) - hsh1["BinarySearch"] = "(Object)"; - } - - hsh1["BinarySearch"] = "Exception not thrown, (Object, IComparer)"; - } - catch (NotSupportedException) - { - } - catch (Exception ex) - { - hsh1["BinarySearch"] = ex.GetType().Name; - } - - try - { - for (int i = 0; i < bad.Count; i++) - { - if (bad.BinarySearch(0, bad.Count, bad[i], new CaseInsensitiveComparer()) != i) - hsh1["BinarySearch"] = "(Object)"; - } - - hsh1["BinarySearch"] = "Exception not thrown, (Object, IComparer)"; - } - catch (NotSupportedException) - { - } - catch (Exception ex) - { - hsh1["BinarySearch"] = ex.GetType().Name; - } - - good.Clear(); - for (int i = 0; i < 100; i++) - good.Add(i); - - Queue que = new Queue(); - for (int i = 0; i < 100; i++) - que.Enqueue(i + 5000); - - try - { - bad.SetRange(0, que); - hsh1["Sort"] = "Copy - int, int, IComparer"; - } - catch (NotSupportedException) - { - } - catch (Exception) - { - hsh1["Sort"] = "Copy_ExceptionType"; - } - } - - //Modifiable methods - if (!hsh1.ContainsKey("IsReadOnly") && !hsh1.ContainsKey("Fixed")) - { - good.Clear(); - for (int i = 0; i < 100; i++) - good.Add(i); - - Queue que = new Queue(); - for (int i = 0; i < 100; i++) - que.Enqueue(i + 5000); - bad.InsertRange(0, que); - for (int i = 0; i < 100; i++) - { - if ((int)bad[i] != i + 5000) - { - hsh1["InsertRange"] = i; - } - } - - //AddRange() - que = new Queue(); - for (int i = 0; i < 100; i++) - que.Enqueue(i + 2222); - bad.AddRange(que); - for (int i = bad.Count - 100; i < bad.Count; i++) - { - if ((int)bad[i] != (i - (bad.Count - 100)) + 2222) - { - hsh1["AddRange"] = i + " " + (int)bad[i]; - } - } - - bad.RemoveRange(0, que.Count); - for (int i = 0; i < 100; i++) - { - if ((int)bad[i] != i) - { - hsh1["RemoveRange"] = i + " " + (int)bad[i]; - } - } - - //Capacity - try - { - bad.Capacity = bad.Capacity * 2; - } - catch (Exception ex) - { - hsh1["Capacity"] = ex.GetType().Name; - } - - try - { - bad.Capacity = -1; - hsh1["Capacity"] = "No_Exception_Thrown, -1"; - } - catch (ArgumentException) - { - } - catch (Exception ex) - { - hsh1["Capacity"] = ex.GetType().Name; - } - - int iMakeSureThisDoesNotCause = 0; - while (bad.Capacity == bad.Count) - { - if (iMakeSureThisDoesNotCause++ > 100) - break; - bad.Add(bad.Count); - } - if (iMakeSureThisDoesNotCause > 100) - hsh1["TrimToSize"] = "Monekeyed, " + bad.Count + " " + bad.Capacity; - - //TrimToSize() - try - { - bad.TrimToSize(); - if (bad.Capacity != bad.Count) - { - hsh1["TrimToSize"] = "Problems baby"; - } - } - catch (Exception ex) - { - hsh1["TrimToSize"] = ex.GetType().Name; - } - } - else - { - Queue que = new Queue(); - for (int i = 0; i < 100; i++) - que.Enqueue(i + 5000); - try - { - bad.AddRange(que); - hsh1["AddRange"] = "Copy"; - } - catch (NotSupportedException) - { - } - catch (Exception) - { - hsh1["AddRange"] = "Copy_ExceptionType"; - } - - try - { - bad.InsertRange(0, que); - hsh1["InsertRange"] = "Copy"; - } - catch (NotSupportedException) - { - } - catch (Exception) - { - hsh1["InsertRange"] = "Copy_ExceptionType"; - } - - good.Clear(); - for (int i = 0; i < 10; i++) - good.Add(i); - try - { - bad.RemoveRange(0, 10); - hsh1["RemoveRange"] = "Copy"; - } - catch (NotSupportedException) - { - } - catch (Exception ex) - { - hsh1["RemoveRange"] = "Copy_ExceptionType, " + ex.GetType().Name; - } - - try - { - bad.Capacity = bad.Capacity * 2; - hsh1["Capacity"] = "No_Exception_Thrown, bad.Capacity*2"; - } - catch (NotSupportedException) - { - } - catch (Exception ex) - { - hsh1["Capacity"] = ex.GetType().Name; - } - - try - { - bad.TrimToSize(); - hsh1["TrimToSize"] = "No_Exception_Thrown"; - } - catch (NotSupportedException) - { - } - catch (Exception ex) - { - hsh1["TrimToSize"] = ex.GetType().Name; - } - } - } - - private void DoIListTests(IList good, IList bad, Hashtable hsh1) - { - //ICollection tests - DoICollectionTests(good, bad, hsh1); - - if (bad.IsReadOnly) - hsh1["IsReadOnly"] = "yea"; - - try - { - for (int i = 0; i < good.Count; i++) - { - if (!bad.Contains(good[i])) - hsh1["Contains"] = i; - if (bad[i] != good[i]) - hsh1["Item"] = "get"; - if (bad.IndexOf(good[i]) != i) - hsh1["IndexOf"] = i; - } - - if (!hsh1.ContainsKey("IsReadOnly")) - { - for (int i = 100; i < 200; i++) - { - try - { - good.Add(i); - bad.Add(i); - } - catch (Exception) - { - hsh1["Fixed"] = null; - } - } - if (!hsh1.ContainsKey("Fixed") && (bad.Count != bad.Count)) - hsh1["Count"] = "ReadWrite"; - - if (!hsh1.ContainsKey("Fixed")) - { - try - { - bad.Clear(); - } - catch (Exception) - { - hsh1["Clear"] = null; - } - - for (int i = 0; i < 100; i++) - { - bad.Insert(0, i); - } - - for (int i = 0; i < 100; i++) - { - bad.RemoveAt(0); - } - - if (bad.Count != 0) - hsh1["Count"] = "Expected 0"; - - for (int i = 0; i < 100; i++) - { - bad.Add(i.ToString()); - } - - if (bad.Count != 100) - hsh1["Count"] = "Expected 100, " + bad.Count; - - if (good.Count != 100) - hsh1["this"] = "Not the same objects, " + good.Count; - - for (int i = 0; i < 100; i++) - { - if (!bad[i].Equals(i.ToString())) - hsh1["Item"] = "String"; - } - - for (int i = 0; i < 100; i++) - bad.Remove(i.ToString()); - - if (bad.Count != 0) - hsh1["Count"] = "Expected 0, " + bad.Count; - - for (int i = 0; i < 100; i++) - { - bad.Add(i.ToString()); - } - - for (int i = 99; i > 0; i--) - bad[i] = i.ToString(); - - if (bad.Count != 100) - hsh1["Count"] = "Expected 100, " + bad.Count; - - for (int i = 99; i > 0; i--) - { - if (!bad[i].Equals(i.ToString())) - hsh1["Item"] = "String"; - } - - bad.Clear(); - - if (bad.Count != 0) - hsh1["Count"] = "Expected 0, " + bad.Count; - } - } - - if (hsh1.ContainsKey("IsReadOnly") || hsh1.ContainsKey("Fixed")) - { - //we will work on the original and see the copy - try - { - good.Clear(); - } - catch (Exception) - { - hsh1["Clear"] = "original"; - } - - for (int i = 0; i < 100; i++) - { - good.Insert(0, i); - } - - if (bad.Count != 100) - hsh1["Count"] = "Not equal to original"; - - for (int i = 0; i < 100; i++) - { - good.RemoveAt(0); - } - - if (bad.Count != 0) - hsh1["Count"] = "Expected 0"; - - for (int i = 0; i < 100; i++) - { - good.Add(i.ToString()); - } - - if (bad.Count != 100) - hsh1["Count"] = "Expected 100, " + bad.Count; - - if (good.Count != 100) - hsh1["this"] = "Not the same objects"; - - for (int i = 0; i < 100; i++) - { - if (!bad[i].Equals(i.ToString())) - hsh1["Item"] = "String"; - } - - for (int i = 0; i < 100; i++) - good.Remove(i.ToString()); - - if (bad.Count != 0) - hsh1["Count"] = "Expected 0, " + bad.Count; - - for (int i = 0; i < 100; i++) - { - good.Add(i.ToString()); - } - - for (int i = 99; i > 0; i--) - good[i] = i.ToString(); - - if (bad.Count != 100) - hsh1["Count"] = "Expected 100, " + bad.Count; - - for (int i = 99; i > 0; i--) - { - if (!bad[i].Equals(i.ToString())) - hsh1["Item"] = "String"; - } - - good.Clear(); - - if (bad.Count != 0) - hsh1["Count"] = "Expected 0, " + bad.Count; - - //we will make sure that these methods throw by calling them anyways!!! - good.Clear(); - for (int i = 0; i < 100; i++) - { - good.Add(i); - } - - try - { - bad.Clear(); - hsh1["Clear"] = "Copy"; - } - catch (NotSupportedException) - { - } - catch (Exception) - { - hsh1["Clear"] = "Copy_ExceptionType"; - } - - try - { - bad.Insert(0, 1); - hsh1["Insert"] = "Copy"; - } - catch (NotSupportedException) - { - } - catch (Exception) - { - hsh1["Insert"] = "Copy_ExceptionType"; - } - - try - { - bad.RemoveAt(0); - hsh1["RemoveAt"] = "Copy"; - } - catch (NotSupportedException) - { - } - catch (Exception) - { - hsh1["RemoveAt"] = "Copy_ExceptionType"; - } - - try - { - bad.Add(1); - hsh1["Add"] = "Copy"; - } - catch (NotSupportedException) - { - } - catch (Exception) - { - hsh1["RemoveAt"] = "Copy_ExceptionType"; - } - - try - { - bad.Remove(1); - hsh1["Remove"] = "Copy"; - } - catch (NotSupportedException) - { - } - catch (Exception) - { - hsh1["Remove"] = "Copy_ExceptionType"; - } - } - } - catch (Exception ex) - { - hsh1["DoIListTests"] = ex.GetType().Name; - } - } - - private void DoICollectionTests(ICollection good, ICollection bad, Hashtable hsh1) - { - int[] iArr1 = null; - int[] iArr2 = null; - - if (good.Count != bad.Count) - hsh1.Add("Count", null); - if (good.IsSynchronized != bad.IsSynchronized) - hsh1.Add("IsSynchronized", null); - if (good.SyncRoot != bad.SyncRoot) - hsh1.Add("SyncRoot", null); - - iArr1 = new int[good.Count]; - iArr2 = new int[good.Count]; - good.CopyTo(iArr1, 0); - bad.CopyTo(iArr2, 0); - - for (int i = 0; i < iArr1.Length; i++) - { - if (iArr1[i] != iArr2[i]) - hsh1["CopyTo"] = "vanila"; - } - - iArr1 = new int[good.Count + 5]; - iArr2 = new int[good.Count + 5]; - good.CopyTo(iArr1, 5); - bad.CopyTo(iArr2, 5); - - for (int i = 5; i < iArr1.Length; i++) - { - if (iArr1[i] != iArr2[i]) - hsh1["CopyTo"] = "5"; - } - - DoIEnumerableTest(good.GetEnumerator(), bad.GetEnumerator(), good, bad, hsh1, false); - IEnumerator ienm1 = good.GetEnumerator(); - IEnumerator ienm2 = bad.GetEnumerator(); - if (good.Count > 0) - { - ((IList)good).RemoveAt(0); - DoIEnumerableTest(ienm1, ienm2, good, bad, hsh1, true); - } - } - - private void DoIEnumerableTest(IEnumerator ienm1, IEnumerator ienm2, IEnumerable ie1, IEnumerable ie2, Hashtable hsh1, Boolean fExpectToThrow) - { - if (!fExpectToThrow) - { - while (ienm1.MoveNext()) - { - Boolean bb = ienm2.MoveNext(); - if (ienm1.Current != ienm2.Current) - hsh1["Enumerator"] = "Current"; - } - ienm1.Reset(); - ienm2.Reset(); - while (ienm1.MoveNext()) - { - ienm2.MoveNext(); - if (ienm1.Current != ienm2.Current) - hsh1["Enumerator"] = "Reset"; - } - - ienm1.Reset(); - ienm2.Reset(); - IEnumerator ienm1Clone = null; - IEnumerator ienm2Clone = null; - Boolean fPastClone = true; - try - { - ienm1Clone = ie1.GetEnumerator(); - } - catch (Exception) - { - fPastClone = false; - } - try - { - ienm2Clone = ie2.GetEnumerator(); - } - catch (Exception) - { - fPastClone = false; - } - if (fPastClone) - { - while (ienm1Clone.MoveNext()) - { - Boolean bb = ienm2Clone.MoveNext(); - if (ienm1Clone.Current != ienm2Clone.Current) - hsh1["Enumerator"] = "Current"; - } - ienm1Clone.Reset(); - ienm2Clone.Reset(); - while (ienm1Clone.MoveNext()) - { - ienm2Clone.MoveNext(); - if (ienm1Clone.Current != ienm2Clone.Current) - hsh1["Enumerator"] = "Reset"; - } - } - } - else - { - try - { - ienm2.MoveNext(); - } - catch (InvalidOperationException) - { - } - catch (Exception) - { - hsh1["Enumerator"] = "MoveNext"; - } - try - { - ienm2.Reset(); - } - catch (InvalidOperationException) - { - } - catch (Exception) - { - hsh1["Enumerator"] = "Reset"; - } - } - } - - private void CompareRangeObjects(ArrayList good, ArrayList bad, Hashtable hsh1) - { - //IList, this includes ICollection tests as well!! - DoRangeIListTests(good, bad, hsh1); - - bad.Clear(); - for (int i = 0; i < 100; i++) - bad.Add(i); - - //we will now test ArrayList specific methods - //AL's CopyTo methods - int[] iArr1 = null; - int[] iArr2 = null; - int goodCount = good.Count; - iArr1 = new int[goodCount]; - iArr2 = new int[goodCount]; - good.CopyTo(iArr1); - bad.CopyTo(iArr2); - for (int i = 0; i < goodCount; i++) - { - if (iArr1[i] != iArr2[i]) - hsh1["CopyTo"] = "()"; - } - - iArr1 = new int[goodCount]; - iArr2 = new int[goodCount]; - good.CopyTo(0, iArr1, 0, goodCount); - try - { - bad.CopyTo(0, iArr2, 0, goodCount); - for (int i = 0; i < goodCount; i++) - { - if (iArr1[i] != iArr2[i]) - hsh1["CopyTo"] = "()"; - } - } - catch - { - hsh1["CopyTo"] = "(int, Array, int, int)"; - } - - iArr1 = new int[200]; - iArr2 = new int[200]; - for (int i = 0; i < 200; i++) - { - iArr1[i] = 50; - iArr2[i] = 50; - } - good.CopyTo(50, iArr1, 100, 20); - try - { - bad.CopyTo(50, iArr2, 100, 20); - for (int i = 0; i < 200; i++) - { - if (iArr1[i] != iArr2[i]) - hsh1["CopyTo"] = "(Array, int, int)"; - } - } - catch - { - hsh1["CopyTo"] = "(int, Array, int, int)"; - } - - //Clone() - ArrayList alstClone = (ArrayList)bad.Clone(); - //lets make sure that the clone is what it says it is - if (alstClone.Count != bad.Count) - hsh1["Clone"] = "Count"; - for (int i = 0; i < bad.Count; i++) - { - if (alstClone[i] != bad[i]) - hsh1["Clone"] = "[]"; - } - - //GerEnumerator() - IEnumerator ienm1 = null; - IEnumerator ienm2 = null; - - ienm1 = good.GetEnumerator(0, 100); - try - { - ienm2 = bad.GetEnumerator(0, 100); - DoIEnumerableTest(ienm1, ienm2, good, bad, hsh1, false); - } - catch - { - hsh1["GetEnumerator"] = "(int, int)"; - } - ienm1 = good.GetEnumerator(50, 50); - try - { - ienm2 = bad.GetEnumerator(50, 50); - DoIEnumerableTest(ienm1, ienm2, good, bad, hsh1, false); - } - catch - { - hsh1["GetEnumerator"] = "(int, int)"; - } - - try - { - bad.GetEnumerator(50, 150); - hsh1["GetEnumerator"] = "(int, int)"; - } - catch (Exception) - { - } - ienm1 = good.GetEnumerator(0, 100); - try - { - ienm2 = bad.GetEnumerator(0, 100); - bad.RemoveAt(0); - DoIEnumerableTest(ienm1, ienm2, good, bad, hsh1, true); - } - catch - { - hsh1["GetEnumerator"] = "(int, int)"; - } - - //GetRange - bad.Clear(); - for (int i = 0; i < 100; i++) - bad.Add(i); - - ArrayList alst1 = good.GetRange(0, good.Count); - try - { - ArrayList alst2 = bad.GetRange(0, good.Count); - for (int i = 0; i < good.Count; i++) - { - if (alst1[i] != alst2[i]) - hsh1["GetRange"] = i; - } - } - catch - { - hsh1["Range"] = "(int, int)"; - } - - //IndexOf(Object, int) - if (bad.Count > 0) - { - for (int i = 0; i < good.Count; i++) - { - if (good.IndexOf(good[i], 0) != bad.IndexOf(good[i], 0)) - { - hsh1["IndexOf"] = "(Object, int)"; - } - if (good.IndexOf(good[i], i) != bad.IndexOf(good[i], i)) - { - hsh1["IndexOf"] = "(Object, int)"; - } - if (i < (good.Count - 1)) - { - if (good.IndexOf(good[i], i + 1) != bad.IndexOf(good[i], i + 1)) - { - hsh1["IndexOf"] = "(Object, int)"; - } - } - } - - try - { - bad.IndexOf(1, -1); - } - catch (ArgumentException) - { - } - catch (Exception ex) - { - hsh1["IndexOf"] = ex; - } - - try - { - bad.IndexOf(1, bad.Count); - } - catch (ArgumentException) - { - } - catch (Exception ex) - { - hsh1["IndexOf"] = ex; - } - - //IndexOf(Object, int, int) - //Update - 2001/03/20: The semantics of this method has changed, the 3rd parameter now refers to count instead of length - for (int i = 0; i < good.Count; i++) - { - if (good.IndexOf(good[i], 0, good.Count - 1) != bad.IndexOf(good[i], 0, good.Count - 1)) - { - hsh1["IndexOf"] = "(Object, int, int)"; - } - if (good.IndexOf(good[i], i, good.Count - i) != bad.IndexOf(good[i], i, good.Count - i)) - { - hsh1["IndexOf"] = "(Object, int)"; - } - if (good.IndexOf(good[i], i, 0) != bad.IndexOf(good[i], i, 0)) - { - hsh1["IndexOf"] = "(Object, int)"; - } - if (i < (good.Count - 1)) - { - if (good.IndexOf(good[i], i + 1, good.Count - (i + 1)) != bad.IndexOf(good[i], i + 1, good.Count - (i + 1))) - { - hsh1["IndexOf"] = "(Object, int)"; - } - } - } - - try - { - bad.IndexOf(1, 0, -1); - } - catch (ArgumentException) - { - } - catch (Exception ex) - { - hsh1["IndexOf"] = ex; - } - - try - { - bad.IndexOf(1, 0, bad.Count); - } - catch (ArgumentException) - { - } - catch (Exception ex) - { - hsh1["IndexOf"] = ex; - } - - try - { - bad.IndexOf(1, bad.Count - 1, bad.Count - 2); - } - catch (ArgumentException) - { - } - catch (Exception ex) - { - hsh1["IndexOf"] = ex; - } - - //LastIndexOf(Object) - for (int i = 0; i < good.Count; i++) - { - if (good.LastIndexOf(good[i]) != bad.LastIndexOf(good[i])) - { - hsh1["LastIndexOf"] = "(Object)"; - } - if (good.LastIndexOf(i + 1000) != bad.LastIndexOf(i + 1000)) - { - hsh1["LastIndexOf"] = "(Object)"; - } - } - - try - { - bad.LastIndexOf(null); - } - catch (ArgumentException) - { - } - catch (Exception ex) - { - hsh1["LastIndexOf"] = ex; - } - - - //LastIndexOf(Object, int) - for (int i = 0; i < good.Count; i++) - { - if (good.LastIndexOf(good[i], good.Count - 1) != bad.LastIndexOf(good[i], good.Count - 1)) - { - hsh1["LastIndexOf"] = "(Object, int)"; - } - if (good.LastIndexOf(good[i], 0) != bad.LastIndexOf(good[i], 0)) - { - hsh1["LastIndexOf"] = "(Object, int)"; - } - if (good.LastIndexOf(good[i], i) != bad.LastIndexOf(good[i], i)) - { - hsh1["LastIndexOf"] = "(Object, int)"; - } - if (i < (good.Count - 1)) - { - if (good.LastIndexOf(good[i], i + 1) != bad.LastIndexOf(good[i], i + 1)) - { - hsh1["LastIndexOf"] = "(Object, int)"; - } - } - } - - try - { - bad.LastIndexOf(1, -1); - } - catch (ArgumentException) - { - } - catch (Exception ex) - { - hsh1["LastIndexOf"] = ex; - } - - try - { - bad.LastIndexOf(1, bad.Count); - } - catch (ArgumentException) - { - } - catch (Exception ex) - { - hsh1["LastIndexOf"] = ex; - } - - //LastIndexOf(Object, int, int) - for (int i = 0; i < good.Count; i++) - { - if (good.LastIndexOf(good[i], good.Count - 1, 0) != bad.LastIndexOf(good[i], good.Count - 1, 0)) - { - hsh1["LastIndexOf"] = "(Object, int, int)"; - } - if (good.LastIndexOf(good[i], good.Count - 1, i) != bad.LastIndexOf(good[i], good.Count - 1, i)) - { - hsh1["LastIndexOf"] = "(Object, int)"; - } - if (good.LastIndexOf(good[i], i, i) != bad.LastIndexOf(good[i], i, i)) - { - hsh1["LastIndexOf"] = "(Object, int)"; - } - if (i < (good.Count - 1)) - { - if (good.LastIndexOf(good[i], good.Count - 1, i + 1) != bad.LastIndexOf(good[i], good.Count - 1, i + 1)) - { - hsh1["LastIndexOf"] = "(Object, int)"; - } - } - } - - try - { - bad.LastIndexOf(1, 1, -1); - } - catch (ArgumentException) - { - } - catch (Exception ex) - { - hsh1["LastIndexOf"] = ex; - } - - try - { - bad.LastIndexOf(1, bad.Count - 2, bad.Count - 1); - } - catch (ArgumentException) - { - } - catch (Exception ex) - { - hsh1["LastIndexOf"] = ex; - } - } - - //ReadOnly() - ArrayList alst3 = ArrayList.ReadOnly(bad); - if (!alst3.IsReadOnly) - hsh1["ReadOnly"] = "Not"; - - IList ilst1 = ArrayList.ReadOnly((IList)bad); - if (!ilst1.IsReadOnly) - hsh1["ReadOnly"] = "Not"; - - //Synchronized() - alst3 = ArrayList.Synchronized(bad); - if (!alst3.IsSynchronized) - hsh1["Synchronized"] = "Not"; - - ilst1 = ArrayList.Synchronized((IList)bad); - if (!ilst1.IsSynchronized) - hsh1["Synchronized"] = "Not"; - - //ToArray() - if (good.Count == bad.Count) - { - Object[] oArr1 = good.ToArray(); - Object[] oArr2 = bad.ToArray(); - for (int i = 0; i < good.Count; i++) - { - if ((int)oArr1[i] != (int)oArr2[i]) - hsh1["ToArray"] = "()"; - } - - //ToArray(type) - iArr1 = (int[])good.ToArray(typeof(int)); - iArr2 = (int[])bad.ToArray(typeof(int)); - for (int i = 0; i < good.Count; i++) - { - if (iArr1[i] != iArr2[i]) - hsh1["ToArray"] = "(Type)"; - } - } - - //Capacity - get - if (good.Capacity != bad.Capacity) - { - hsh1["Capacity"] = "get"; - } - - //Fixed size methods - if (!hsh1.ContainsKey("IsReadOnly")) - { - bad.Clear(); - for (int i = 100; i > 0; i--) - bad.Add(i); - //Sort() & BinarySearch(Object) - bad.Sort(); - for (int i = 0; i < bad.Count - 1; i++) - { - if ((int)bad[i] > (int)bad[i + 1]) - hsh1["Sort"] = "()"; - } - for (int i = 0; i < bad.Count; i++) - { - if (bad.BinarySearch(bad[i]) != i) - hsh1["BinarySearch"] = "(Object)"; - } - //Reverse() - bad.Reverse(); - if (bad.Count > 0) - { - for (int i = 0; i < 99; i++) - { - if ((int)bad[i] < (int)bad[i + 1]) - hsh1["Reverse"] = "()"; - } - - bad.Clear(); - for (int i = 100; i > 0; i--) - bad.Add(i.ToString()); - } - - bad.Clear(); - for (int i = 90; i > 64; i--) - bad.Add(((Char)i).ToString()); - try - { - bad.Sort(new CaseInsensitiveComparer()); - if (bad.Count > 0) - { - for (int i = 0; i < (bad.Count - 1); i++) - { - if (((String)bad[i]).CompareTo(((String)bad[i + 1])) >= 0) - hsh1["Sort"] = "(IComparer)"; - } - for (int i = 0; i < bad.Count; i++) - { - if (bad.BinarySearch(bad[i], new CaseInsensitiveComparer()) != i) - hsh1["BinarySearch"] = "(Object, IComparer)"; - } - } - - bad.Reverse(); - - bad.Clear(); - for (int i = 65; i < 91; i++) - bad.Add(((Char)i).ToString()); - //Sort(int, int, IComparer) & BinarySearch(int, int, Object, IComparer) - if (bad.Count > 0) - { - for (int i = 0; i < good.Count; i++) - { - if (bad.BinarySearch(0, bad.Count, bad[i], new CaseInsensitiveComparer()) != i) - hsh1["BinarySearch"] = "(int, int, Object, IComparer)"; - } - } - } - catch (Exception) - { - } - - bad.Clear(); - for (int i = 0; i < 100; i++) - bad.Add(i); - - Queue que = new Queue(); - for (int i = 0; i < 100; i++) - que.Enqueue(i + 5000); - - try - { - bad.SetRange(0, que); - } - catch (Exception ex) - { - hsh1["SetRange"] = "Copy_ExceptionType, " + ex.GetType().Name; - } - for (int i = bad.Count; i < bad.Count; i++) - { - if ((int)bad[i] != (i + 5000)) - { - hsh1["SetRange"] = i; - } - } - } - else - { - //we make sure that the above methods throw here - bad.Clear(); - for (int i = 100; i > 0; i--) - bad.Add(i); - - try - { - bad.Sort(); - hsh1["Sort"] = "Copy"; - } - catch (NotSupportedException) - { - } - catch (Exception) - { - hsh1["Sort"] = "Copy_ExceptionType"; - } - - try - { - bad.Reverse(); - hsh1["Reverse"] = "Copy"; - } - catch (NotSupportedException) - { - } - catch (Exception ex) - { - hsh1["Reverse"] = "Copy_ExceptionType, " + ex.GetType().Name; - } - - try - { - bad.Sort(new CaseInsensitiveComparer()); - hsh1["Sort"] = "Copy - Icomparer"; - } - catch (NotSupportedException) - { - } - catch (Exception) - { - hsh1["Sort"] = "Copy_ExceptionType"; - } - - try - { - bad.Sort(0, 0, new CaseInsensitiveComparer()); - hsh1["Sort"] = "Copy - int, int, IComparer"; - } - catch (NotSupportedException) - { - } - catch (Exception) - { - hsh1["Sort"] = "Copy_ExceptionType"; - } - - //BinarySearch - try - { - for (int i = 0; i < bad.Count; i++) - { - if (bad.BinarySearch(bad[i]) != i) - hsh1["BinarySearch"] = "(Object)"; - } - hsh1["BinarySearch"] = "(Object)"; - } - catch (NotSupportedException) - { - } - catch (Exception ex) - { - hsh1["BinarySearch"] = ex.GetType().Name; - } - - try - { - for (int i = 0; i < bad.Count; i++) - { - if (bad.BinarySearch(bad[i], new CaseInsensitiveComparer()) != i) - hsh1["BinarySearch"] = "(Object)"; - } - hsh1["BinarySearch"] = "Exception not thrown, (Object, IComparer)"; - } - catch (NotSupportedException) - { - } - catch (Exception ex) - { - hsh1["BinarySearch"] = ex.GetType().Name; - } - - try - { - for (int i = 0; i < bad.Count; i++) - { - if (bad.BinarySearch(0, bad.Count, bad[i], new CaseInsensitiveComparer()) != i) - hsh1["BinarySearch"] = "(Object)"; - } - hsh1["BinarySearch"] = "Exception not thrown, (Object, IComparer)"; - } - catch (NotSupportedException) - { - } - catch (Exception ex) - { - hsh1["BinarySearch"] = ex.GetType().Name; - } - - bad.Clear(); - for (int i = 0; i < 100; i++) - bad.Add(i); - Queue que = new Queue(); - for (int i = 0; i < 100; i++) - que.Enqueue(i + 5000); - try - { - bad.SetRange(0, que); - hsh1["Sort"] = "Copy - int, int, IComparer"; - } - catch (NotSupportedException) - { - } - catch (Exception) - { - hsh1["Sort"] = "Copy_ExceptionType"; - } - } - - //Modifiable methods - if (!hsh1.ContainsKey("IsReadOnly") && !hsh1.ContainsKey("Fixed")) - { - bad.Clear(); - for (int i = 0; i < 100; i++) - bad.Add(i); - - Queue que = new Queue(); - - for (int i = 0; i < 100; i++) - que.Enqueue(i + 5000); - bad.InsertRange(0, que); - - for (int i = 0; i < 100; i++) - { - if ((int)bad[i] != i + 5000) - { - hsh1["InsertRange"] = i; - } - } - - //AddRange() - que = new Queue(); - for (int i = 0; i < 100; i++) - que.Enqueue(i + 2222); - - bad.AddRange(que); - for (int i = bad.Count - 100; i < bad.Count; i++) - { - if ((int)bad[i] != (i - (bad.Count - 100)) + 2222) - { - hsh1["AddRange"] = i + " " + (int)bad[i]; - } - } - - bad.RemoveRange(0, que.Count); - for (int i = 0; i < 100; i++) - { - if ((int)bad[i] != i) - { - hsh1["RemoveRange"] = i + " " + (int)bad[i]; - } - } - - //Capacity - try - { - bad.Capacity = bad.Capacity * 2; - } - catch (Exception ex) - { - hsh1["Capacity"] = ex.GetType().Name; - } - - try - { - bad.Capacity = -1; - hsh1["Capacity"] = "No_Exception_Thrown, -1"; - } - catch (ArgumentException) - { - } - catch (Exception ex) - { - hsh1["Capacity"] = ex.GetType().Name; - } - - int iMakeSureThisDoesNotCause = 0; - while (bad.Capacity == bad.Count) - { - if (iMakeSureThisDoesNotCause++ > 100) - break; - bad.Add(bad.Count); - } - if (iMakeSureThisDoesNotCause > 100) - hsh1["TrimToSize"] = "Monekeyed, " + bad.Count + " " + bad.Capacity; - - //TrimToSize() - try - { - bad.TrimToSize(); - if (bad.Capacity != bad.Count) - { - hsh1["TrimToSize"] = "Problems baby"; - } - } - catch (Exception ex) - { - hsh1["TrimToSize"] = ex.GetType().Name; - } - - GC.KeepAlive(typeof(NotSupportedException)); // This line will keep type metadata alive for Project N. - } - else - { - Queue que = new Queue(); - for (int i = 0; i < 100; i++) - que.Enqueue(i + 5000); - try - { - bad.AddRange(que); - hsh1["AddRange"] = "Copy"; - } - catch (NotSupportedException) - { - } - catch (Exception) - { - hsh1["AddRange"] = "Copy_ExceptionType"; - } - - try - { - bad.InsertRange(0, que); - hsh1["InsertRange"] = "Copy"; - } - catch (NotSupportedException) - { - } - catch (Exception) - { - hsh1["InsertRange"] = "Copy_ExceptionType"; - } - - bad.Clear(); - for (int i = 0; i < 10; i++) - bad.Add(i); - try - { - bad.RemoveRange(0, 10); - hsh1["RemoveRange"] = "Copy"; - } - catch (NotSupportedException) - { - } - catch (Exception ex) - { - hsh1["RemoveRange"] = "Copy_ExceptionType, " + ex.GetType().Name; - } - - try - { - bad.Capacity = bad.Capacity * 2; - hsh1["Capacity"] = "No_Exception_Thrown, bad.Capacity*2"; - } - catch (NotSupportedException) - { - } - catch (Exception ex) - { - hsh1["Capacity"] = ex.GetType().Name; - } - - try - { - bad.TrimToSize(); - hsh1["TrimToSize"] = "No_Exception_Thrown"; - } - catch (NotSupportedException) - { - } - catch (Exception ex) - { - hsh1["TrimToSize"] = ex.GetType().Name; - } - } - } - - private void DoRangeIListTests(IList good, IList bad, Hashtable hsh1) - { - //ICollection tests - DoRangeICollectionTests(good, bad, hsh1); - - //@Hack - if (bad.IsReadOnly) - hsh1["IsReadOnly"] = "yea"; - - try - { - for (int i = 0; i < good.Count; i++) - { - if (!bad.Contains(good[i])) - hsh1["Contains"] = i; - if (bad[i] != good[i]) - hsh1["Item"] = "get"; - if (bad.IndexOf(good[i]) != i) - hsh1["IndexOf"] = i; - } - - if (hsh1.ContainsKey("IsReadOnly") || hsh1.ContainsKey("Fixed")) - { - //we will work on the original and see the copy - try - { - good.Clear(); - } - catch (Exception) - { - hsh1["Clear"] = "original"; - } - - for (int i = 0; i < 100; i++) - { - good.Insert(0, i); - } - - if (bad.Count != 100) - hsh1["Count"] = "Not equal to original"; - - for (int i = 0; i < 100; i++) - { - good.RemoveAt(0); - } - - if (bad.Count != 0) - hsh1["Count"] = "Expected 0"; - - for (int i = 0; i < 100; i++) - { - good.Add(i.ToString()); - } - - if (bad.Count != 100) - hsh1["Count"] = "Expected 100, " + bad.Count; - - if (good.Count != 100) - hsh1["this"] = "Not the same objects"; - - for (int i = 0; i < 100; i++) - { - if (!bad[i].Equals(i.ToString())) - hsh1["Item"] = "String"; - } - - for (int i = 0; i < 100; i++) - good.Remove(i.ToString()); - - if (bad.Count != 0) - hsh1["Count"] = "Expected 0, " + bad.Count; - - for (int i = 0; i < 100; i++) - { - good.Add(i.ToString()); - } - - for (int i = 99; i > 0; i--) - good[i] = i.ToString(); - - if (bad.Count != 100) - hsh1["Count"] = "Expected 100, " + bad.Count; - - for (int i = 99; i > 0; i--) - { - if (!bad[i].Equals(i.ToString())) - hsh1["Item"] = "String"; - } - - good.Clear(); - - if (bad.Count != 0) - hsh1["Count"] = "Expected 0, " + bad.Count; - - //we will make sure that these methods throw by calling them anyways!!! - good.Clear(); - for (int i = 0; i < 100; i++) - { - good.Add(i); - } - - try - { - bad.Clear(); - hsh1["Clear"] = "Copy"; - } - catch (NotSupportedException) - { - } - catch (Exception) - { - hsh1["Clear"] = "Copy_ExceptionType"; - } - - try - { - bad.Insert(0, 1); - hsh1["Insert"] = "Copy"; - } - catch (NotSupportedException) - { - } - catch (Exception) - { - hsh1["Insert"] = "Copy_ExceptionType"; - } - - try - { - bad.RemoveAt(0); - hsh1["RemoveAt"] = "Copy"; - } - catch (NotSupportedException) - { - } - catch (Exception) - { - hsh1["RemoveAt"] = "Copy_ExceptionType"; - } - - try - { - bad.Add(1); - hsh1["Add"] = "Copy"; - } - catch (NotSupportedException) - { - } - catch (Exception) - { - hsh1["RemoveAt"] = "Copy_ExceptionType"; - } - - try - { - bad.Remove(1); - hsh1["Remove"] = "Copy"; - } - catch (NotSupportedException) - { - } - catch (Exception) - { - hsh1["Remove"] = "Copy_ExceptionType"; - } - } - } - catch (Exception ex) - { - hsh1["DoIListTests"] = ex.GetType().Name; - } - } - - private void DoRangeICollectionTests(ICollection good, ICollection bad, Hashtable hsh1) - { - int[] iArr1 = null; - int[] iArr2 = null; - - if (good.Count != bad.Count) - hsh1.Add("Count", null); - - if (good.IsSynchronized != bad.IsSynchronized) - hsh1.Add("IsSynchronized", null); - - if (good.SyncRoot != bad.SyncRoot) - hsh1.Add("SyncRoot", null); - - iArr1 = new int[good.Count]; - iArr2 = new int[good.Count]; - good.CopyTo(iArr1, 0); - bad.CopyTo(iArr2, 0); - - for (int i = 0; i < iArr1.Length; i++) - { - if (iArr1[i] != iArr2[i]) - hsh1["CopyTo"] = "vanila"; - } - - iArr1 = new int[good.Count + 5]; - iArr2 = new int[good.Count + 5]; - good.CopyTo(iArr1, 5); - bad.CopyTo(iArr2, 5); - - for (int i = 5; i < iArr1.Length; i++) - { - if (iArr1[i] != iArr2[i]) - hsh1["CopyTo"] = "5"; - } - - DoIEnumerableTest(good.GetEnumerator(), bad.GetEnumerator(), good, bad, hsh1, false); - } -} |