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 FailPopOnEmpty()
        {
            var sut = new BasicStack <int>();

            // Assert
            Assert.Throws <InvalidOperationException>(() => sut.Peek());
            Assert.Throws <InvalidOperationException>(() => sut.Pop());
        }
Ejemplo n.º 3
0
        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());
        }
Ejemplo n.º 4
0
        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 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);
        }
Ejemplo n.º 6
0
        public void Pop_ForEmptyStack_ThrowsException()
        {
            IBasicStack <int> stack = new BasicStack <int>();

            Assert.ThrowsAny <Exception>(() => stack.Pop());
        }