Exemplo n.º 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);
        }
Exemplo n.º 2
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));
        }
        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.º 4
0
        public void Push_Test()
        {
            var stack = new SetOfStacks(2);

            stack.Push(1);
            stack.Push(2);
            Assert.AreEqual(1, stack.GetStackNumber());

            stack.Push(3);
            Assert.AreEqual(2, stack.GetStackNumber());
        }
Exemplo n.º 5
0
        public void TestSetOfStackPush()
        {
            SetOfStacks <int> setOfStack = new SetOfStacks <int>(3);

            setOfStack.Push(1);
            setOfStack.Push(2);
            setOfStack.Push(3);
            setOfStack.Push(4);

            Assert.AreEqual(setOfStack.StackRepo.Count, 2);
        }
Exemplo n.º 6
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);
 }
Exemplo n.º 7
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());
                }
            }
        }
Exemplo n.º 9
0
        public void Push_EmptyStack_PushesElement()
        {
            SetOfStacks stack = new SetOfStacks();

            stack.Push(10);

            int element = stack.Peek();

            Assert.AreEqual(10, element);
        }
Exemplo n.º 10
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());
        }
Exemplo n.º 11
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.º 12
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());
        }
        public void PushTests()
        {
            var s = new SetOfStacks<int>(3);

            for (var i = 0; i < 10; i++)
            {
                s.Push(i);
                
                Assert.AreEqual(i + 1, s.Length);
                Assert.AreEqual(i / 3 + 1, s.NumStacks);
            }
        }
Exemplo n.º 14
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());
            }
        }
Exemplo n.º 15
0
        //
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for PopAt
        ///</summary>
        public void PopAtTestHelper <T>()
        {
            SetOfStacks <int> target = new SetOfStacks <int>(2); // TODO: Initialize to an appropriate value

            target.Push(1);
            target.Push(2);
            target.Push(3);
            target.Push(4);
            target.Push(5);
            target.Push(6);
            int actual = target.PopAt(1);

            ;
        }
        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);
        }
        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());
        }
Exemplo n.º 18
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);
            }
        }
Exemplo n.º 19
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());
        }
            public void SetOfStacksTest()
            {
                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());
                Assert.AreEqual(3, stack.Pop());
                Assert.AreEqual(2, stack.Pop());
                Assert.AreEqual(1, stack.Pop());
            }
Exemplo n.º 21
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);
        }
Exemplo n.º 22
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));
        }
Exemplo n.º 23
0
Arquivo: Q03_3.cs Projeto: 0x0all/ctci
        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());
		    }
        }