public void TestQ3StackOfPlates()
        {
            int maxStackCapacity = 3;
            int maxStacks = 2;
            SetOfStacks<int> stacks = new SetOfStacks<int>(maxStackCapacity, maxStacks);
            MyAssert.Throws<InvalidOperationException>( () => stacks.Pop());
            stacks.Push(10);
            stacks.Push(20);
            stacks.Push(30);
            // assert: there is one full stack internally.
            stacks.Push(11);
            // assert: two stacks, second with one element.
            Assert.AreEqual(11, stacks.Peek());
            Assert.AreEqual(11, stacks.Pop());
            Assert.AreEqual(30, stacks.Peek());
            Assert.AreEqual(30, stacks.Pop());
            Assert.AreEqual(20, stacks.Peek());
            stacks.Push(30);
            stacks.Push(11);
            stacks.Push(21);
            stacks.Push(31);
            MyAssert.Throws<InvalidOperationException>(() => stacks.Push(41));
            Assert.AreEqual(31, stacks.Pop());

        }
Exemplo n.º 2
0
        public void Push_EmptyStack_PushesElement()
        {
            SetOfStacks stack = new SetOfStacks();

            stack.Push(10);

            int element = stack.Peek();

            Assert.AreEqual(10, element);
        }
Exemplo n.º 3
0
        public void Push_StackAtMax_PushesToNewStack()
        {
            //Test the creation of a new internal stack
            //Insert ten items (max is set to 10), then insert an eleventh
            //Checks that the 10th and 11th values are both correct
            SetOfStacks stacks = new SetOfStacks();

            for (int i = 0; i < 10; i++)
            {
                stacks.Push(i);
            }
            int lastValue = stacks.Peek();

            stacks.Push(15);

            int lastValueOnNewStack = stacks.Peek();

            Assert.AreEqual(9, lastValue);
            Assert.AreEqual(15, lastValueOnNewStack);
        }
Exemplo n.º 4
0
        public void Test(IEnumerable <int> valuesForStack)
        {
            var stackArray = valuesForStack as int[] ?? valuesForStack.ToArray();

            Trace.WriteLine(stackArray.Aggregate(new StringBuilder(), (builder, i) => builder.AppendFormat("{0} ", i)));

            var stack = new SetOfStacks <int>(2);

            foreach (var i in stackArray)
            {
                stack.Push(i);
            }

            foreach (var i in stackArray.Reverse())
            {
                var peek = stack.Peek();
                var pop  = stack.Pop();
                Assert.AreEqual(i, peek);
                Assert.AreEqual(i, pop);
            }
        }