From 480bfa8c5af4ec0aea04c1f88d750f21af123c65 Mon Sep 17 00:00:00 2001 From: Sridhar Periyasamy Date: Thu, 23 Jul 2015 09:51:41 -0700 Subject: [PATCH] Example Given-When-Then model for unit testing --- .../tests/ArrayList/CopyToTests.cs | 655 ++++++++++++++++-- 1 file changed, 611 insertions(+), 44 deletions(-) diff --git a/src/System.Collections.NonGeneric/tests/ArrayList/CopyToTests.cs b/src/System.Collections.NonGeneric/tests/ArrayList/CopyToTests.cs index 98c675c6da4a..89fa59cb6445 100644 --- a/src/System.Collections.NonGeneric/tests/ArrayList/CopyToTests.cs +++ b/src/System.Collections.NonGeneric/tests/ArrayList/CopyToTests.cs @@ -37,10 +37,47 @@ public class CopyToTests null }; + string[] strHeroesWithoutNulls = + { + "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 + /// + /// + /// An ArrayList with elements + /// + /// + /// Copying the ArrayList to an array with sufficient space using ArrayList.CopyTo(array) + /// + /// + /// The array must be populated with all the elements from ArrayList in the same order. + /// + /// [Fact] - public void TestCopyToBasic() + public void TestCopyToBasic1() { //-------------------------------------------------------------------------- // Variable definitions. @@ -61,6 +98,28 @@ public void TestCopyToBasic() { Assert.Equal(strHeroes[i], arrCopy[i]); } + } + + + /// + /// + /// An empty ArrayList + /// + /// + /// Copying the ArrayList to an array using ArrayList.CopyTo(array) + /// + /// + /// The array must be must be unchanged. + /// + /// + [Fact] + public void TestCopyToBasic2() + { + //-------------------------------------------------------------------------- + // Variable definitions. + //-------------------------------------------------------------------------- + ArrayList arrList = null; + String[] arrCopy = null; //[] Normal Copy Test 2 - copy 0 elements // Construct ArrayList. @@ -90,6 +149,27 @@ public void TestCopyToBasic() { Assert.Equal(strHeroes[i], arrCopy[i]); } + } + + /// + /// + /// An empty ArrayList + /// + /// + /// Copying the ArrayList to an empty array using ArrayList.CopyTo(array) + /// + /// + /// The array must be must remain empty. + /// + /// + [Fact] + public void TestCopyToBasic3() + { + //-------------------------------------------------------------------------- + // Variable definitions. + //-------------------------------------------------------------------------- + ArrayList arrList = null; + String[] arrCopy = null; //we'll make sure by copying only 0 arrList = new ArrayList(); @@ -98,6 +178,27 @@ public void TestCopyToBasic() //copying 0 elements into arrCopy arrList.CopyTo(arrCopy); Assert.Equal(0, arrCopy.Length); + } + + /// + /// + /// An empty ArrayList + /// + /// + /// Copying the ArrayList to a null array using ArrayList.CopyTo(array) + /// + /// + /// ArrayList.CopyTo(array) must throw ArgumentNullException. + /// + /// + [Fact] + public void TestCopyToBasic4() + { + //-------------------------------------------------------------------------- + // Variable definitions. + //-------------------------------------------------------------------------- + ArrayList arrList = null; + String[] arrCopy = null; //[] Copy so that exception should be thrown Assert.Throws(() => @@ -111,8 +212,21 @@ public void TestCopyToBasic() }); } + + /// + /// + /// An ArrayList with elements + /// + /// + /// Copying the ArrayList to an array starting at index 0 using ArrayList.CopyTo(array, index) + /// + /// + /// The array must be populated with all the elements from ArrayList in the same order starting from index 0. + /// + /// + /// [Fact] - public void TestArrayListWrappers() + public void TestArrayListWrappers1() { //-------------------------------------------------------------------------- // Variable definitions. @@ -144,7 +258,43 @@ public void TestArrayListWrappers() { Assert.Equal(strHeroes[i], arrCopy[i]); } + } + } + + + /// + /// + /// An empty ArrayList + /// + /// + /// Copying the ArrayList to an array starting at a valid index using ArrayList.CopyTo(array, index) + /// + /// + /// The array must be must be unchanged. + /// + /// + [Fact] + public void TestArrayListWrappers2() + { + //-------------------------------------------------------------------------- + // 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) + { //[] Normal Copy Test 2 - copy 0 elements arrList.Clear(); arrList.Add(null); @@ -192,6 +342,43 @@ public void TestArrayListWrappers() { Assert.Equal(strHeroes[i], arrCopy[i]); } + } + } + + /// + /// + /// An ArrayList + /// + /// + /// Copying the ArrayList to an array starting at a negative index using ArrayList.CopyTo(array, index) + /// + /// + /// ArrayList.CopyTo(array, index) must throw ArgumentOutOfRangeException + /// + /// + [Fact] + public void TestArrayListWrappers3() + { + //-------------------------------------------------------------------------- + // 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) + { + //[] Copy so that exception should be thrown arrList.Clear(); @@ -210,16 +397,52 @@ public void TestArrayListWrappers() // [] CopyTo with negative index Assert.Throws(() => arrList.CopyTo(arrCopy, -1)); + } + } + + /// + /// + /// An ArrayList + /// + /// + /// Using ArrayList.CopyTo(array, index) the number of elements in the source ArrayList is greater than the available space from 'index' to the end of the destination 'array'. + /// + /// + /// ArrayList.CopyTo(array, index) must throw ArgumentException + /// + /// + [Fact] + public void TestArrayListWrappers4() + { + //-------------------------------------------------------------------------- + // Variable definitions. + //-------------------------------------------------------------------------- + ArrayList arrList = 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) + { // [] CopyTo with array with index is not large enough Assert.Throws(() => - { - arrList.Clear(); - for (int i = 0; i < 10; i++) - arrList.Add(i); + { + arrList.Clear(); + for (int i = 0; i < 10; i++) + arrList.Add(i); - arrList.CopyTo(new Object[11], 2); - }); + arrList.CopyTo(new Object[11], 2); + }); // [] CopyTo with null array Assert.Throws(() => arrList.CopyTo(null, 0)); @@ -229,40 +452,104 @@ public void TestArrayListWrappers() } } + /// + /// + /// An ArrayList + /// + /// + /// Copying the ArrayList to a null array ArrayList.CopyTo(array, index) + /// + /// + /// ArrayList.CopyTo(array, index) must throw ArgumentNullException + /// + /// [Fact] - public void TestCopyToWithCount() + public void TestArrayListWrappers5() { //-------------------------------------------------------------------------- // Variable definitions. //-------------------------------------------------------------------------- ArrayList arrList = null; - string[] arrCopy = null; - string[] strHeroes = + + 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) { - "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", - }; + // [] CopyTo with null array + Assert.Throws(() => arrList.CopyTo(null, 0)); + } + } + + /// + /// + /// An ArrayList + /// + /// + /// Copying the ArrayList to a multi-dimensional array ArrayList.CopyTo(array, index) + /// + /// + /// ArrayList.CopyTo(array, index) must throw ArgumentException + /// + /// + [Fact] + public void TestArrayListWrappers6() + { + //-------------------------------------------------------------------------- + // Variable definitions. + //-------------------------------------------------------------------------- + ArrayList arrList = 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) + { + // [] CopyTo with multidimentional array + Assert.Throws(() => arrList.CopyTo(new Object[10, 10], 1)); + } + } + + /// + /// + /// An ArrayList with elements + /// + /// + /// Copying the ArrayList to an array using ArrayList.CopyTo(index, array, arrayIndex, count) with valid 'index', 'arrayIndex' and 'count' + /// + /// + /// The array starting at 'arrayIndex' must be populated with the correct 'count' of elements from the ArrayList starting from 'index'. + /// + /// + /// + [Fact] + public void TestCopyToWithCount1() + { + //-------------------------------------------------------------------------- + // Variable definitions. + //-------------------------------------------------------------------------- + ArrayList arrList = null; + string[] arrCopy = null; + + // // Construct array list. @@ -271,13 +558,13 @@ public void TestCopyToWithCount() Assert.NotNull(arrList); // Add items to the lists. - for (int ii = 0; ii < strHeroes.Length; ++ii) + for (int ii = 0; ii < strHeroesWithoutNulls.Length; ++ii) { - arrList.Add(strHeroes[ii]); + arrList.Add(strHeroesWithoutNulls[ii]); } // Verify items added to list. - Assert.Equal(strHeroes.Length, arrList.Count); + Assert.Equal(strHeroesWithoutNulls.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 @@ -310,17 +597,69 @@ public void TestCopyToWithCount() { Assert.Equal(0, ((String)arrList[ii]).CompareTo(arrCopy[ii])); } + } + } + /// + /// + /// An ArrayList with elements + /// + /// + /// When copying the ArrayList to an array using ArrayList.CopyTo(index, array, arrayIndex, count) with 'count' greater than number of elements in Arraylist starting at 'index' + /// + /// + /// ArrayList.CopyTo(index, array, arrayIndex, count) must throw ArgumentException. + /// + /// + /// + [Fact] + public void TestCopyToWithCount2() + { + //-------------------------------------------------------------------------- + // Variable definitions. + //-------------------------------------------------------------------------- + ArrayList arrList = null; + string[] arrCopy = null; + + + + // + // Construct array list. + // + arrList = new ArrayList(); + Assert.NotNull(arrList); + + // Add items to the lists. + for (int ii = 0; ii < strHeroesWithoutNulls.Length; ++ii) + { + arrList.Add(strHeroesWithoutNulls[ii]); + } + + // Verify items added to list. + Assert.Equal(strHeroesWithoutNulls.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) + { // // [] Invalid Arguments // // 2nd throw ArgumentOutOfRangeException // rest throw ArgumentException - Assert.ThrowsAny( () => arrList.CopyTo(0, arrCopy, -100, 1000) ); + Assert.ThrowsAny(() => arrList.CopyTo(0, arrCopy, -100, 1000)); + - Assert.Throws(() => arrList.CopyTo(-1, arrCopy, 0, 1)); - Assert.Throws(() => arrList.CopyTo(0, arrCopy, 0, -1)); // this is valid now arrCopy = new String[100]; @@ -332,17 +671,245 @@ public void TestCopyToWithCount() arrList.CopyTo(arrList.Count - 1, arrCopy, 0, 24); }); + + } + } + + + /// + /// + /// Given an ArrayList with elements + /// + /// + /// When copying the ArrayList to a multi-dimensional array using ArrayList.CopyTo(index, array, arrayIndex, count) + /// + /// + /// ArrayList.CopyTo(index, array, arrayIndex, count) must throw ArgumentException. + /// + /// + /// + [Fact] + public void TestCopyToWithCount3() + { + //-------------------------------------------------------------------------- + // Variable definitions. + //-------------------------------------------------------------------------- + ArrayList arrList = null; + + + + + // + // Construct array list. + // + arrList = new ArrayList(); + Assert.NotNull(arrList); + + // Add items to the lists. + for (int ii = 0; ii < strHeroesWithoutNulls.Length; ++ii) + { + arrList.Add(strHeroesWithoutNulls[ii]); + } + + // Verify items added to list. + Assert.Equal(strHeroesWithoutNulls.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) + { + Assert.Throws(() => arrList.CopyTo(0, new Object[arrList.Count, arrList.Count], 0, arrList.Count)); + // same as above, some iteration throws different exceptions: ArgumentOutOfRangeException + Assert.ThrowsAny(() => arrList.CopyTo(0, new Object[arrList.Count, arrList.Count], 0, -1)); + } + } + + /// + /// + /// Given an ArrayList with elements + /// + /// + /// When copying the ArrayList to a null array using ArrayList.CopyTo(index, array, arrayIndex, count) + /// + /// + /// ArrayList.CopyTo(index, array, arrayIndex, count) must throw ArgumentNullException. + /// + /// + /// + [Fact] + public void TestCopyToWithCount4() + { + //-------------------------------------------------------------------------- + // Variable definitions. + //-------------------------------------------------------------------------- + ArrayList arrList = null; + + + + + // + // Construct array list. + // + arrList = new ArrayList(); + Assert.NotNull(arrList); + + // Add items to the lists. + for (int ii = 0; ii < strHeroesWithoutNulls.Length; ++ii) + { + arrList.Add(strHeroesWithoutNulls[ii]); + } + + // Verify items added to list. + Assert.Equal(strHeroesWithoutNulls.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) + { Assert.Throws(() => arrList.CopyTo(0, null, 3, 15)); + } + } + + /// + /// + /// Given an ArrayList with elements + /// + /// + /// When copying the ArrayList to a array using ArrayList.CopyTo(index, array, arrayIndex, count) with negative 'index' or 'arrayIndex' or 'count' + /// + /// + /// ArrayList.CopyTo(index, array, arrayIndex, count) must throw ArgumentOutOfRangeException. + /// + /// + /// + [Fact] + public void TestCopyToWithCount5() + { + //-------------------------------------------------------------------------- + // Variable definitions. + //-------------------------------------------------------------------------- + ArrayList arrList = null; + string[] arrCopy = null; + + + + // + // Construct array list. + // + arrList = new ArrayList(); + Assert.NotNull(arrList); + + // Add items to the lists. + for (int ii = 0; ii < strHeroesWithoutNulls.Length; ++ii) + { + arrList.Add(strHeroesWithoutNulls[ii]); + } + + // Verify items added to list. + Assert.Equal(strHeroesWithoutNulls.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) + { + // + // [] Invalid Arguments + // + + // 2nd throw ArgumentOutOfRangeException + // Allocate sting array. + arrCopy = new String[100]; + Assert.Throws(() => arrList.CopyTo(-1, arrCopy, 0, 1)); + Assert.Throws(() => arrList.CopyTo(0, arrCopy, 0, -1)); + + + + + } + } + + /// + /// + /// Given an ArrayList with elements + /// + /// + /// When copying the ArrayList to an array using ArrayList.CopyTo(index, array, arrayIndex, count) with 'count' greater than number of elements in the array starting at 'arrayIndex' + /// + /// + /// ArrayList.CopyTo(index, array, arrayIndex, count) must throw ArgumentException. + /// + /// + [Fact] + public void TestCopyToWithCount6() + { + //-------------------------------------------------------------------------- + // Variable definitions. + //-------------------------------------------------------------------------- + ArrayList arrList = null; + string[] arrCopy = null; + + + + // + // Construct array list. + // + arrList = new ArrayList(); + Assert.NotNull(arrList); + + // Add items to the lists. + for (int ii = 0; ii < strHeroesWithoutNulls.Length; ++ii) + { + arrList.Add(strHeroesWithoutNulls[ii]); + } + + // Verify items added to list. + Assert.Equal(strHeroesWithoutNulls.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) + { Assert.Throws(() => { arrCopy = new String[1]; arrList.CopyTo(0, arrCopy, 3, 15); }); - - Assert.Throws(() => arrList.CopyTo(0, new Object[arrList.Count, arrList.Count], 0, arrList.Count)); - // same as above, some iteration throws different exceptions: ArgumentOutOfRangeException - Assert.ThrowsAny(() => arrList.CopyTo(0, new Object[arrList.Count, arrList.Count], 0, -1)); } } }