예제 #1
0
        public void Stack_Can_Peek_Value()
        {
            var myStack = new MyStack();

            myStack.Push(1);
            myStack.Peek().Should().Be(1);
            myStack.IsEmpty().Should().BeFalse();
        }
        public void TestStack()
        {
            Stack <int>   stdStack = new Stack <int>();   // standard queue implemented by .NET
            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());
            }
        }