Beispiel #1
0
        public void Question_3_3_BasicCases()
        {
            var item1 = 1;
            var item2 = 2;
            var item3 = 3;
            var item4 = 0;
            var item5 = -1;

            var stackSet = new SetOfStacks <int>();

            stackSet.Push(item1);
            stackSet.Push(item2);
            stackSet.Push(item3);
            stackSet.Push(item4);
            stackSet.Push(item5);
            var pop1 = stackSet.Pop();
            var pop2 = stackSet.Pop();
            var pop3 = stackSet.Pop();
            var pop4 = stackSet.Pop();
            var pop5 = stackSet.Pop();

            Assert.AreEqual(pop1, item5);
            Assert.AreEqual(pop2, item4);
            Assert.AreEqual(pop3, item3);
            Assert.AreEqual(pop4, item2);
            Assert.AreEqual(pop5, item1);
        }
        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());

        }
Beispiel #3
0
        public void SetOfStacksTest()
        {
            SetOfStacks s = new SetOfStacks(3);

            s.Push(1); s.Push(1); s.Push(1);
            s.Push(2); s.Push(2); s.Push(2);
            s.Push(3); s.Push(3); s.Push(3);

            var x = s.Pop();

            Assert.AreEqual(x, 3);

            s.Pop(); s.Pop();
            x = s.Pop();
            Assert.AreEqual(x, 2);

            s.Push(2); s.Push(3); s.Push(3); s.Push(3);

            x = s.PopAt(0);
            Assert.AreEqual(x, 1);

            x = s.PopAt(1);
            Assert.AreEqual(x, 2);

            x = s.PopAt(2);
            Assert.AreEqual(x, 3);

            Assert.ThrowsException <ArgumentOutOfRangeException>(() => s.PopAt(4));
        }
Beispiel #4
0
 public void C3Q3_2()
 {
     SetOfStacks<int> st = new SetOfStacks<int>();
     st.Push(5);
     st.Push(6);
     st.Push(7);
     st.Push(8);
     Assert.AreEqual(st.PopAt(0), 6);
     Assert.AreEqual(st.Pop(), 8);
     Assert.AreEqual(st.Pop(), 7);
     Assert.AreEqual(st.Pop(), 5);
 }
            public void SetOfStacksTest2()
            {
                var stack = new SetOfStacks <int>(3);

                stack.Push(1);
                stack.Push(2);
                stack.Push(3);
                stack.Push(4);
                stack.Push(5);
                stack.Push(6);
                stack.Push(7);

                Assert.AreEqual(7, stack.Pop());
                Assert.AreEqual(6, stack.Pop());
                Assert.AreEqual(5, stack.Pop());
                Assert.AreEqual(4, stack.Pop());

                stack.Push(4);
                stack.Push(5);
                stack.Push(6);
                stack.Push(7);

                Assert.AreEqual(7, stack.Pop());
                Assert.AreEqual(6, stack.Pop());
                Assert.AreEqual(5, stack.Pop());
                Assert.AreEqual(4, stack.Pop());
            }
Beispiel #6
0
        public void Run()
        {
            const int capacityPerSubstack = 5;
            var set = new SetOfStacks(capacityPerSubstack);

            Console.WriteLine("IsEmpty? {0}", set.IsEmpty());

            for (var i = 0; i < 34; i++)
            {
                set.Push(i);
            }
            Console.WriteLine("IsEmpty? {0}", set.IsEmpty());

            for (var i = 0; i < 34; i++)
            {
                if (i == 0)
                {
                    set.PopAt(i);
                }
                else
                {
                    Console.WriteLine("Popped " + set.Pop());
                }
            }
        }
        public void Run()
        {
            const int capacityPerSubstack = 5;
            var       set = new SetOfStacks(capacityPerSubstack);

            Console.WriteLine("IsEmpty? {0}", set.IsEmpty());

            for (var i = 0; i < 34; i++)
            {
                set.Push(i);
            }
            Console.WriteLine("IsEmpty? {0}", set.IsEmpty());

            for (var i = 0; i < 34; i++)
            {
                if (i == 0)
                {
                    set.PopAt(i);
                }
                else
                {
                    Console.WriteLine("Popped " + set.Pop());
                }
            }
        }
Beispiel #8
0
        public void TesSetOfStacks()
        {
            var set = new SetOfStacks <int>(3);

            for (var num = 0; num < 30; num++)
            {
                set.Push(num);
            }
            Assert.AreEqual(10, set.StacksCount);
            for (var num = 29; num >= 0; num--)
            {
                Assert.AreEqual(num, set.Pop());
            }

            for (var num = 0; num < 30; num++)
            {
                set.Push(num);
            }
            Assert.AreEqual(10, set.StacksCount);

            Assert.AreEqual(29, set.PopAt(9));
            Assert.AreEqual(28, set.PopAt(9));

            Assert.AreEqual(5, set.PopAt(1));
            Assert.AreEqual(4, set.PopAt(1));
            Assert.AreEqual(3, set.PopAt(1));

            Assert.AreEqual(8, set.PopAt(1));
        }
Beispiel #9
0
        public void BasicTest()
        {
            var stack = new SetOfStacks <int>();

            const int N = 100;

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

            for (int i = N; i > 0; i--)
            {
                Assert.AreEqual(i - 1, stack.Pop());
            }

            stack.Push(0);
            Assert.AreEqual(0, stack.Pop());
        }
Beispiel #10
0
        public void Pop_Test()
        {
            var stack = new SetOfStacks(2);

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

            var value = stack.Pop();

            Assert.AreEqual(3, value);
            Assert.AreEqual(1, stack.GetStackNumber());

            value = stack.Pop();
            Assert.AreEqual(2, value);
            Assert.AreEqual(1, stack.GetStackNumber());

            value = stack.Pop();
            Assert.AreEqual(1, value);
            Assert.AreEqual(0, stack.GetStackNumber());
        }
Beispiel #11
0
        public void Run()
        {
            const int   capacityPerSubstack = 5;
            SetOfStacks set = new SetOfStacks(capacityPerSubstack);

            for (int i = 0; i < 34; i++)
            {
                set.Push(i);
            }
            for (int i = 0; i < 34; i++)
            {
                Console.WriteLine("Popped " + set.Pop());
            }
        }
Beispiel #12
0
        public static void PerformStackSetOperations()
        {
            SetOfStacks plates = new SetOfStacks(3);

            plates.Push(1);
            plates.Push(2);
            Console.WriteLine("Stack Count:" + plates.StackCount());
            plates.Push(3);
            Console.WriteLine("Stack Count:" + plates.StackCount());
            plates.Push(4);
            plates.Push(5);
            plates.Push(6);
            Console.WriteLine("Stack Count:" + plates.StackCount());
            plates.Push(7);
            Console.WriteLine("Stack Count:" + plates.StackCount());
            plates.Push(8);
            Console.WriteLine("Stack Count:" + plates.StackCount());
            Console.WriteLine("Popped element:" + plates.Pop());
            Console.WriteLine("Popped element:" + plates.Pop());
            Console.WriteLine("Stack Count:" + plates.StackCount());
            Console.WriteLine("Popped element:" + plates.Pop());
            Console.WriteLine("Stack Count:" + plates.StackCount());
        }
Beispiel #13
0
        public void SetOfStacksTest()
        {
            var s = new SetOfStacks();

            Assert.Equal(1, s.StacksCount);
            s.Push(1);
            s.Push(2);
            s.Push(3);
            s.Push(4);
            Assert.Equal(2, s.StacksCount);
            Assert.Equal(4, s.Pop());
            Assert.Equal(3, s.Pop());
            Assert.Equal(2, s.Pop());
            Assert.Equal(1, s.Pop());
            Assert.Throws <InvalidOperationException>(() => s.Pop());

            s.Push(1); // stack 0
            s.Push(2);
            s.Push(3);
            s.Push(4); // stack 1
            s.Push(5);
            s.Push(6);
            s.Push(7); // stack 2

            Assert.Equal(6, s.PopAt(1));
            Assert.Equal(5, s.PopAt(1));
            Assert.Equal(4, s.PopAt(1));
            Assert.Throws <InvalidOperationException>(() => s.PopAt(1));
            Assert.Throws <ArgumentOutOfRangeException>(() => s.PopAt(3));
            Assert.Throws <ArgumentOutOfRangeException>(() => s.PopAt(-1));
            Assert.Equal(3, s.StacksCount);
            // for follow up use Pop2
            Assert.Equal(7, s.Pop2());
            Assert.Equal(3, s.Pop2());
            Assert.Equal(1, s.StacksCount);
        }
        public void PopTests()
        {
            var s = new SetOfStacks<int>(3);

            for (var i = 0; i < 10; i++)
            {
                s.Push(i);
            }

            for (var i = s.Length - 1; i >= 0; i--)
            {
                Assert.AreEqual(i / 3 + 1, s.NumStacks);
                Assert.AreEqual(i, s.Pop());
                Assert.AreEqual(i, s.Length);
                Assert.AreEqual(Math.Ceiling(i / 3.0), s.NumStacks);
            }

            Assert.AreEqual(0, s.Length);
            Assert.AreEqual(0, s.NumStacks);
        }
Beispiel #15
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);
            }
        }
        public void PopAtTests()
        {
            var s = new SetOfStacks<int>(3);

            for (var i = 0; i < 10; i++)
            {
                s.Push(i);
            }

            Assert.AreEqual(2, s.PopAt(0));
            Assert.AreEqual(5, s.PopAt(1));
            Assert.AreEqual(8, s.PopAt(2));
            Assert.AreEqual(9, s.PopAt(3));

            Assert.AreEqual(7, s.Pop());
            Assert.AreEqual(6, s.Pop());
            Assert.AreEqual(4, s.Pop());
            Assert.AreEqual(3, s.Pop());
            Assert.AreEqual(1, s.Pop());
            Assert.AreEqual(0, s.Pop());
        }
 public void SetOfStacksEmptyTests()
 {
     var s = new SetOfStacks<int>(10);
     s.Pop();
 }
Beispiel #18
0
        public void InvalidOperationsTest()
        {
            var stack = new SetOfStacks <int>();

            TestHelpers.AssertExceptionThrown(() => stack.Pop(), typeof(InvalidOperationException));
        }
Beispiel #19
0
        public void Run()
        {
		    const int capacityPerSubstack = 5;
		    SetOfStacks set = new SetOfStacks(capacityPerSubstack);
		    for (int i = 0; i < 34; i++)
            {
			    set.Push(i);
		    }
		    for (int i = 0; i < 34; i++)
            {
			    Console.WriteLine("Popped " + set.Pop());
		    }
        }