Example #1
0
        public void Test1()
        {
            MyStack obj = new MyStack();

            obj.Push(1);
            obj.Push(2);
            obj.Push(3);
            Assert.AreEqual(3, obj.Pop());
            Assert.AreEqual(2, obj.Top());
            Assert.IsFalse(obj.Empty());
            Assert.AreEqual(2, obj.Pop());
            Assert.IsFalse(obj.Empty());
            Assert.AreEqual(1, obj.Pop());
            Assert.IsTrue(obj.Empty());
        }
Example #2
0
        public void StackTest()
        {
            var stack = new MyStack <int>(1, 3, 5);

            CollectionAssert.AreEqual(new int[] { 5, 3, 1 }, stack);

            stack.Push(7);
            CollectionAssert.AreEqual(new int[] { 7, 5, 3, 1 }, stack);

            stack.Pop();
            CollectionAssert.AreEqual(new int[] { 5, 3, 1 }, stack);

            Assert.AreEqual(3, stack.Count);

            stack.Clear();
            Assert.AreEqual(0, stack.Count);
            CollectionAssert.AreEqual(new int[] { }, stack);

            for (int i = 0; i < 10; i++)
            {
                stack.Push(1);
            }

            CollectionAssert.AreEqual(new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, stack);
        }
        public void Pop_EmptyStack_ThrowsException()
        {
            var stack = new MyStack <int>();

            Assert.Throws <InvalidOperationException>(() =>
            {
                stack.Pop();
            });
        }
        public void Peek_PushTwoItemsAndPop_ReturnsHeadElement()
        {
            var stack = new MyStack <int>();

            stack.Push(1);
            stack.Push(2);

            stack.Pop();

            Assert.AreEqual(1, stack.Peek());
        }
Example #5
0
        public void TestStack()
        {
            Stack <int>   stdStack = new Stack <int>();     // standard queue implemented by C#
            MyStack <int> ourStack = new MyStack <int>();   // our manually implemented stack

            // Test generates 100 random integers and adds to both queues
            Random r = new Random();

            for (int i = 0; i < 100; i++)
            {
                int randVal = r.Next();
                stdStack.Push(randVal);
                ourStack.Push(randVal);
            }

            // Test reading back half the added integers
            for (int i = 0; i < 50; i++)
            {
                Assert.AreEqual(stdStack.Pop(), ourStack.Pop());
                Assert.AreEqual(stdStack.Peek(), ourStack.Peek());
                Assert.AreEqual(stdStack.Count, ourStack.Count());
            }

            // Test adding 50 more random integers
            for (int i = 0; i < 50; i++)
            {
                int randVal = r.Next();
                stdStack.Push(randVal);
                ourStack.Push(randVal);
            }

            // Test reading back all the remaining values
            for (int i = 0; i < 100; i++)
            {
                Assert.AreEqual(stdStack.Peek(), ourStack.Peek());
                Assert.AreEqual(stdStack.Pop(), ourStack.Pop());
                Assert.AreEqual(stdStack.Count, ourStack.Count());
            }
        }