Пример #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());
        }
Пример #2
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());
            }
        }
        public void IsEmpty_EmptyStack_ReturnsTrue()
        {
            var stack = new MyStack <int>();

            Assert.IsTrue(stack.IsEmpty);
        }