コード例 #1
0
        public void Indexer_Set_WrongIndex_Exception()
        {
            // Arrange
            ReverseCollection <int> reversedCollection = new ReverseCollection <int>(initialArray);

            int wrongIndex = Convert.ToInt32(TestContext.DataRow["Index"]);
            int valueToSet = 10;

            // Act
            // Assert
            CollectionAssert.DoesNotContain(reversedCollection.ToArray(), valueToSet);
            Assert.ThrowsException <IndexOutOfRangeException>(() => reversedCollection[wrongIndex] = valueToSet);
            CollectionAssert.DoesNotContain(reversedCollection.ToArray(), valueToSet);
        }
コード例 #2
0
        public void Insert_WrongIndex_Exception()
        {
            // Arrange
            ReverseCollection <int> reversedCollection = new ReverseCollection <int>(initialArray);

            int valueToAssert = int.MaxValue;

            int wrongIndex = Convert.ToInt32(TestContext.DataRow["Index"]);

            // Act
            // Assert
            CollectionAssert.DoesNotContain(reversedCollection.ToArray(), valueToAssert);
            Assert.ThrowsException <ArgumentOutOfRangeException>(() => reversedCollection.Insert(wrongIndex, valueToAssert));
            CollectionAssert.DoesNotContain(reversedCollection.ToArray(), valueToAssert);
        }
コード例 #3
0
        public void Indexer_Get()
        {
            // Arrange
            ReverseCollection <int> reversedCollection = new ReverseCollection <int>(initialArray);

            Random random = new Random();

            // Act
            // Assert
            CollectionAssert.AreEqual(reversedArray, reversedCollection.ToArray());

            // FOR is necessary, needs to check []
            // from start to end
            for (int i = 0; i < arrayLength; ++i)
            {
                Assert.AreEqual(reversedArray[i], reversedCollection[i]);
            }
            // from end to start
            for (int i = arrayLength - 1; i >= 0; --i)
            {
                Assert.AreEqual(reversedArray[i], reversedCollection[i]);
            }
            // random check
            for (int i = 0; i < arrayLength; ++i)
            {
                int index = random.Next(arrayLength);
                Assert.AreEqual(reversedArray[index], reversedCollection[index]);
            }
        }
コード例 #4
0
        public void Remove_ManyOccurrence()
        {
            // Arrange
            int[] startCollection = new int[] { 1, 2, 3, 3, 2, 1 };
            ReverseCollection <int> reversedCollection = new ReverseCollection <int>(startCollection);

            int  itemToRemove         = 2;
            bool expectedRemoveResult = true;

            bool expectedContainItemBeforeRemoving = true;
            bool exoectedContainItemAfterRemoving  = true;

            int[] expectedCollection = new int[] { 1, 3, 3, 2, 1 };

            // Act
            bool actualContainItemBeforeRemoving = reversedCollection.Contains(itemToRemove);
            bool actualRemoveResult             = reversedCollection.Remove(itemToRemove);
            bool actualContainItemAfterRemoving = reversedCollection.Contains(itemToRemove);

            // Assert
            Assert.AreEqual(expectedRemoveResult, actualRemoveResult);
            Assert.AreEqual(expectedContainItemBeforeRemoving, actualContainItemBeforeRemoving);
            Assert.AreEqual(exoectedContainItemAfterRemoving, actualContainItemAfterRemoving);
            CollectionAssert.Contains(reversedCollection.ToArray(), itemToRemove);
        }
コード例 #5
0
        public void Remove_OneOccurrence()
        {
            // Arrange
            ReverseCollection <int> reversedCollection = new ReverseCollection <int>(initialArray);

            int  itemToRemove         = 2;
            bool expectedRemoveResult = true;

            bool expectedContainItemBeforeRemoving = true;
            bool exoectedContainItemAfterRemoving  = false;

            int[] expectedCollection = new int[4] {
                5, 4, 3, 1
            };

            // Act
            bool actualContainItemBeforeRemoving = reversedCollection.Contains(itemToRemove);
            bool actualRemoveResult             = reversedCollection.Remove(itemToRemove);
            bool actualContainItemAfterRemoving = reversedCollection.Contains(itemToRemove);

            int[] actualCollection = reversedCollection.ToArray();

            // Assert
            Assert.AreEqual(expectedRemoveResult, actualRemoveResult);
            Assert.AreEqual(expectedContainItemBeforeRemoving, actualContainItemBeforeRemoving);
            Assert.AreEqual(exoectedContainItemAfterRemoving, actualContainItemAfterRemoving);
            CollectionAssert.AreEqual(expectedCollection, actualCollection);
            CollectionAssert.DoesNotContain(actualCollection, itemToRemove);
        }
コード例 #6
0
        public void RemoveAt()
        {
            // Arrange
            ReverseCollection <int> reversedCollection = new ReverseCollection <int>(initialArray);

            int indexToRemove = 2;
            int itemToRemove  = 3;

            int[] expectedCollection = new int[4] {
                5, 4, 2, 1
            };

            bool expectedHasItemBeforeRemoving = true;
            bool actualHasItemBeforeRemoving   = reversedArray.Contains(itemToRemove);

            bool expectedHasItemAfterRemoving = false;

            // Act
            reversedCollection.RemoveAt(indexToRemove);

            bool actualHasItemAfterRemoving = reversedCollection.Contains(itemToRemove);

            int[] actualCollectionAfterRemoving = reversedCollection.ToArray();

            // Assert
            CollectionAssert.DoesNotContain(actualCollectionAfterRemoving, itemToRemove);
            Assert.AreEqual(expectedHasItemBeforeRemoving, actualHasItemBeforeRemoving);
            Assert.AreEqual(expectedHasItemAfterRemoving, actualHasItemAfterRemoving);
            CollectionAssert.AreEqual(expectedCollection, actualCollectionAfterRemoving);
        }
コード例 #7
0
        public void ReverseConstructor()
        {
            // Arrange
            ReverseCollection <int> reversedCollection = new ReverseCollection <int>(initialArray);

            // Act
            // Assert
            CollectionAssert.AreEqual(expected: reversedArray, actual: reversedCollection.ToArray());
        }
コード例 #8
0
        public void Adding_WithForEach()
        {
            // Arrange
            ReverseCollection <int> reversedCollection = new ReverseCollection <int>();

            // Act
            foreach (int item in initialArray)
            {
                reversedCollection.Add(item);
            }

            // Assert
            CollectionAssert.AreEqual(reversedArray, reversedCollection.ToArray());
        }
コード例 #9
0
        public void Insert()
        {
            // Arrange
            ReverseCollection <int> reversedCollection = new ReverseCollection <int>(initialArray);

            int insertedElement = 10;
            int insertedIndex   = 2;

            int[] expectedCollectionBeforeInserting = reversedArray;
            int[] actualCollectionBeforeInserting   = reversedCollection.ToArray();

            int[] expectedCollectionAfterInserting = new int[6] {
                5, 4, insertedElement, 3, 2, 1
            };

            // Act
            reversedCollection.Insert(insertedIndex, insertedElement);
            int[] actualCollectionAfterInserting = reversedCollection.ToArray();

            // Assert
            CollectionAssert.Contains(actualCollectionAfterInserting, insertedElement);
            CollectionAssert.AreEqual(expectedCollectionBeforeInserting, actualCollectionBeforeInserting);
            CollectionAssert.AreEqual(expectedCollectionAfterInserting, actualCollectionAfterInserting);
        }
コード例 #10
0
        public void Indexer_Set()
        {
            // Arrange
            ReverseCollection <int> reversedCollection = new ReverseCollection <int>(initialArray);

            int oldItem    = Convert.ToInt32(TestContext.DataRow["OldValue"]);
            int newItem    = Convert.ToInt32(TestContext.DataRow["NewValue"]);
            int indexToSet = Convert.ToInt32(TestContext.DataRow["Index"]);

            // Act
            reversedCollection[indexToSet] = newItem;
            int[] actualCollection = reversedCollection.ToArray();

            // Assert
            CollectionAssert.Contains(actualCollection, newItem);
            CollectionAssert.DoesNotContain(actualCollection, oldItem);
        }
コード例 #11
0
        public void Clear()
        {
            // Arrange
            ReverseCollection <int> reversedCollection = new ReverseCollection <int>();

            int[] expectedCollectionAfterClearing = new int[0];

            // Act
            foreach (int item in initialArray)
            {
                reversedCollection.Add(item);
            }
            reversedCollection.Clear();

            // Assert
            Assert.IsTrue(reversedCollection.IsEmpty);
            CollectionAssert.AreEqual(expectedCollectionAfterClearing, reversedCollection.ToArray());
        }
コード例 #12
0
        public void Remove_WrongItem_False()
        {
            // Arrange
            ReverseCollection <int> reversedCollection = new ReverseCollection <int>(initialArray);

            int  itemToRemove         = int.MaxValue;
            bool expectedRemoveResult = false;

            bool expectedContainItemBeforeRemoving = false;
            bool exoectedContainItemAfterRemoving  = false;

            // Act
            bool actualContainItemBeforeRemoving = reversedCollection.Contains(itemToRemove);
            bool actualRemoveResult             = reversedCollection.Remove(itemToRemove);
            bool actualContainItemAfterRemoving = reversedCollection.Contains(itemToRemove);

            // Assert
            Assert.AreEqual(expectedRemoveResult, actualRemoveResult);
            Assert.AreEqual(expectedContainItemBeforeRemoving, actualContainItemBeforeRemoving);
            Assert.AreEqual(exoectedContainItemAfterRemoving, actualContainItemAfterRemoving);
            CollectionAssert.DoesNotContain(reversedCollection.ToArray(), itemToRemove);
        }
コード例 #13
0
        public void Contains()
        {
            // Arrange
            ReverseCollection <int> reversedCollection = new ReverseCollection <int>();

            int  elementToSearch = 2;
            bool expectedContainsElementBeforeAdding = false;
            bool expectedContainsElementAfterAdding  = true;

            // Act
            bool actualContainsElementBeforeAdding = reversedCollection.Contains(elementToSearch);

            foreach (int item in initialArray)
            {
                reversedCollection.Add(item);
            }
            bool actualContainsElementAfterAdding = reversedCollection.Contains(elementToSearch);

            // Assert
            Assert.AreEqual(expectedContainsElementBeforeAdding, actualContainsElementBeforeAdding);
            Assert.AreEqual(expectedContainsElementAfterAdding, actualContainsElementAfterAdding);
            CollectionAssert.Contains(reversedCollection.ToArray(), elementToSearch);
        }
コード例 #14
0
        public void Adding_ThreeRandom()
        {
            // Arrange
            ReverseCollection <int> reversedCollection = new ReverseCollection <int>();

            int[] baseCollection = new int[3]
            {
                Convert.ToInt32(TestContext.DataRow["First"]),
                Convert.ToInt32(TestContext.DataRow["Second"]),
                Convert.ToInt32(TestContext.DataRow["Third"])
            };

            int[] expectedCollection = baseCollection.Reverse().ToArray();

            // Act
            foreach (int item in baseCollection)
            {
                reversedCollection.Add(item);
            }
            int[] actualCollection = reversedCollection.ToArray();

            // Assert
            CollectionAssert.AreEqual(expectedCollection, actualCollection);
        }