Beispiel #1
0
 public void MyTestInitialize()
 {
     my_stack   = new Stack <DSString>();
     my_b_stack = new BoundedStack <DSString>(MAX_STACK_ELEMENTS);
     addStackItems(my_stack);
     addStackItems(my_b_stack);
 }
Beispiel #2
0
        //------------------- HELPER METHODS ----------------------

        private void addStackItems(BasicStack <DSString> the_stack)
        {
            for (int i = 0; i < 10; i++)
            {
                the_stack.push(new DSString(i.ToString()));
            }
        }
        public void Push_WithValidValue_Success()
        {
            IBasicStack <int> stack = new BasicStack <int>();

            stack.Push(1);

            Assert.Equal(1, stack.Size());
        }
        public void Peek_ForNonEmptyStack_Success()
        {
            IBasicStack <int> stack = new BasicStack <int>(1);

            var itemValue = stack.Peek();

            Assert.Equal(1, itemValue);
        }
        public void PushOverCapacity(int capacity)
        {
            var sut = new BasicStack <int>(GenerateArray(capacity));

            // Assert
            Assert.DoesNotThrow(() => sut.Push(12));
            Assert.Greater(sut.Count, capacity);
        }
        public void FailPopOnEmpty()
        {
            var sut = new BasicStack <int>();

            // Assert
            Assert.Throws <InvalidOperationException>(() => sut.Peek());
            Assert.Throws <InvalidOperationException>(() => sut.Pop());
        }
        public void Size_ForNonEmptyStack_ReturnsOne()
        {
            IBasicStack <int> stack = new BasicStack <int>();

            for (var i = 0; i < 3; i++)
            {
                stack.Push(i);
            }

            Assert.Equal(3, stack.Size());
        }
        public void Size_ForNonEmptyStackAfterPushAndPop_Success()
        {
            IBasicStack <int> stack = new BasicStack <int>();

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

            Assert.Equal(2, stack.Size());
        }
        public void IsEmpty_ForNonEmptyStack_ReturnsFalse()
        {
            IBasicStack <int> stack = new BasicStack <int>();

            for (var i = 0; i < 3; i++)
            {
                stack.Push(i);
            }

            Assert.False(stack.IsEmpty());
        }
        public void Pop_ForNonEmptyStack_Success()
        {
            IBasicStack <int> stack = new BasicStack <int>();

            for (var i = 0; i < 3; i++)
            {
                stack.Push(i);
            }

            var itemValue = stack.Pop();

            Assert.Equal(2, itemValue);
        }
        public int Check(string input)
        {
            BasicStack <char> stack      = new BasicStack <char>();
            BasicStack <int>  stackIndex = new BasicStack <int>();

            int index = 0;

            foreach (var ch in input)
            {
                index++;
                //if (!"{}[]()".Contains(ch)) continue;
                if (!(ch.Equals('[') || ch.Equals(']') ||
                      ch.Equals('(') || ch.Equals(')') ||
                      ch.Equals('{') || ch.Equals('}')))
                {
                    continue;
                }
                stackIndex.Push(index);
                if (ch.Equals('[') ||
                    ch.Equals('(') ||
                    ch.Equals('{'))
                {
                    stack.Push(ch);
                    continue;
                }
                else
                {
                    if (stack.Count == 0)
                    {
                        return(stackIndex.Pop());
                    }
                    var top = stack.Pop();
                    if (
                        (top == '[' && ch != ']') ||
                        (top == '(' && ch != ')') ||
                        (top == '{' && ch != '}'))
                    {
                        return(stackIndex.Pop());
                    }
                    stackIndex.Pop();
                    stackIndex.Pop();
                }
            }
            if (stack.Count != 0)
            {
                return(stackIndex.Pop());                  // index;
            }
            return(0);
        }
        public void CorrectPushAndPop(int capacity)
        {
            int[] inputArray  = GenerateArray(capacity);
            int[] resultArray = new int[capacity];
            var   sut         = new BasicStack <int>();

            Array.ForEach(inputArray, x => sut.Push(x));

            for (int i = 0; i < capacity; i++)
            {
                resultArray[i] = sut.Pop();
            }
            // Assert
            Assert.AreEqual(inputArray.Reverse(), resultArray);
        }
Beispiel #13
0
        /// <summary>
        /// Sets up the iterate with a traversal method, an initial node to traverse from and a link
        /// to the parent binary search tree.
        /// </summary>
        /// <param name="the_traversal_method">the method of traversing the tree.</param>
        /// <param name="the_parent_root">the root of the tree.</param>
        /// <param name="the_parent">link to the parent tree itself.</param>
        public BSTIterator(GraphTraversals the_traversal_method, BinaryNode <T> the_source_node, BinarySearchTree <T> the_parent)
        {
            my_traversal_method = the_traversal_method;
            my_parent           = the_parent;

            //prepare stack for traversal
            my_element_stack = new Stack <BinaryNode <T> >(my_parent.size());
            my_element_queue = new Queue <BinaryNode <T> >();

            //add the first element if it is there
            if (my_parent.root != null)
            {
                my_element_stack.push(the_source_node);
                my_element_queue.enqueue(the_source_node);
            }
        }
Beispiel #14
0
        private void testClear(BasicStack <DSString> the_stack)
        {
            //make sure size() and isEmpty() are correct before clear
            Assert.AreEqual(false, the_stack.isEmpty());
            Assert.AreEqual(10, the_stack.size());

            //after clearing
            the_stack.clear();
            Assert.AreEqual(true, the_stack.isEmpty());
            Assert.AreEqual(0, the_stack.size());

            //after readding and clearing
            the_stack.push(new DSString("13"));
            Assert.AreEqual(false, the_stack.isEmpty());
            Assert.AreEqual(1, the_stack.size());
        }
Beispiel #15
0
        private void testPeek(BasicStack <DSString> the_stack)
        {
            //check multiple peeks
            Assert.AreEqual("9", the_stack.peek().value);
            Assert.AreEqual("9", the_stack.peek().value);

            //peek after removal
            the_stack.pop();
            Assert.AreEqual("8", the_stack.peek().value);

            //peek with nothing on the stack
            while (!the_stack.isEmpty())
            {
                the_stack.pop();
            }
            Assert.AreEqual(null, the_stack.peek());
        }
Beispiel #16
0
        private void testIsEmpty(BasicStack <DSString> the_stack)
        {
            //check while not empty
            Assert.IsFalse(the_stack.isEmpty());

            //after empty
            the_stack.clear();
            Assert.IsTrue(the_stack.isEmpty());

            //after re-addition
            the_stack.push(new DSString("1"));
            Assert.IsFalse(the_stack.isEmpty());

            //after empty again
            the_stack.clear();
            Assert.IsTrue(the_stack.isEmpty());
        }
Beispiel #17
0
        private void testSize(BasicStack <DSString> the_stack)
        {
            //check after initial insertion
            Assert.AreEqual(10, the_stack.size());

            //check after removal
            the_stack.pop();
            Assert.AreEqual(9, the_stack.size());

            //check after addition
            the_stack.push(new DSString("13"));
            Assert.AreEqual(10, the_stack.size());

            //check after removing all
            the_stack.clear();
            Assert.AreEqual(0, the_stack.size());
        }
Beispiel #18
0
        private void testPush(BasicStack <DSString> the_stack)
        {
            //items already added in initialization

            //make sure first size is right
            Assert.AreEqual(10, the_stack.size());

            //make sure first removal matches FIFO ordering
            Assert.AreEqual("9", the_stack.pop().value);

            //remove all elements and check last item to make sure removals match FIFO ordering
            string element = "";

            while (!the_stack.isEmpty())
            {
                element = the_stack.pop().value;
            }
            Assert.AreEqual("0", element);
        }
Beispiel #19
0
        private void testPop(BasicStack <DSString> the_stack)
        {
            //make sure the ordering is secure
            Assert.AreEqual("9", the_stack.pop().value);
            Assert.AreEqual("8", the_stack.pop().value);
            Assert.AreEqual("7", the_stack.pop().value);

            //add an item after removing
            the_stack.push(new DSString("13"));

            //check ordering
            Assert.AreEqual("13", the_stack.pop().value);
            Assert.AreEqual("6", the_stack.pop().value);
            Assert.AreEqual("5", the_stack.pop().value);
            Assert.AreEqual("4", the_stack.pop().value);
            Assert.AreEqual("3", the_stack.pop().value);
            Assert.AreEqual("2", the_stack.pop().value);
            Assert.AreEqual("1", the_stack.pop().value);
            Assert.AreEqual("0", the_stack.pop().value);
        }
Beispiel #20
0
        private void testContains(BasicStack <DSString> the_stack)
        {
            //make sure all items are matched inside
            Assert.AreEqual(true, the_stack.contains(new DSString("0")));
            Assert.AreEqual(true, the_stack.contains(new DSString("1")));
            Assert.AreEqual(true, the_stack.contains(new DSString("2")));
            Assert.AreEqual(true, the_stack.contains(new DSString("3")));
            Assert.AreEqual(true, the_stack.contains(new DSString("4")));
            Assert.AreEqual(true, the_stack.contains(new DSString("5")));
            Assert.AreEqual(true, the_stack.contains(new DSString("6")));
            Assert.AreEqual(true, the_stack.contains(new DSString("7")));
            Assert.AreEqual(true, the_stack.contains(new DSString("8")));
            Assert.AreEqual(true, the_stack.contains(new DSString("9")));

            //check after removal
            the_stack.pop();
            Assert.AreEqual(false, the_stack.contains(new DSString("9")));

            //check after adding
            the_stack.push(new DSString("13"));
            Assert.AreEqual(true, the_stack.contains(new DSString("13")));

            //check after removing all
            the_stack.clear();
            Assert.AreEqual(false, the_stack.contains(new DSString("13")));
            Assert.AreEqual(false, the_stack.contains(new DSString("0")));
            Assert.AreEqual(false, the_stack.contains(new DSString("1")));
            Assert.AreEqual(false, the_stack.contains(new DSString("2")));
            Assert.AreEqual(false, the_stack.contains(new DSString("3")));
            Assert.AreEqual(false, the_stack.contains(new DSString("4")));
            Assert.AreEqual(false, the_stack.contains(new DSString("5")));
            Assert.AreEqual(false, the_stack.contains(new DSString("6")));
            Assert.AreEqual(false, the_stack.contains(new DSString("7")));
            Assert.AreEqual(false, the_stack.contains(new DSString("8")));
            Assert.AreEqual(false, the_stack.contains(new DSString("9")));
        }
        public void Pop_ForEmptyStack_ThrowsException()
        {
            IBasicStack <int> stack = new BasicStack <int>();

            Assert.ThrowsAny <Exception>(() => stack.Pop());
        }
        public void Size_ForEmptyStack_ReturnsZero()
        {
            IBasicStack <int> stack = new BasicStack <int>();

            Assert.Equal(0, stack.Size());
        }
        public void IsEmpty_ForEmptyStack_ReturnsTrue()
        {
            IBasicStack <int> stack = new BasicStack <int>();

            Assert.True(stack.IsEmpty());
        }
Beispiel #24
0
 private void testAdd(BasicStack <DSString> the_stack)
 {
     //tested in testPush()
 }