Example #1
0
    def test_peek(self):
        new_stack = Stack()
        new_stack.add(5)
        new_stack.add(10)
        self.assertIs(new_stack.peek(), 10)

        new_stack.add(20)
        self.assertIs(new_stack.peek(), 20)
    def test_push_single_element_in_empty_stack(self):
        stack = Stack()
        stack.push(1)

        result_list = stack.stack
        check_list = [1]

        self.assertEqual(
            result_list, check_list,
            "Testing 'Push'. Pushing single element. Stack lists not equal")
Example #3
0
 def test_remove(self):
     new_stack = Stack()
     new_stack.add(5)
     new_stack.add(10)
     new_stack.add(20)
     new_stack.remove()
     result = new_stack.return_data()
     self.assertEqual(result, [5, 10])
    def test_peek_empty_stack(self):
        stack = Stack()

        test_value = stack.peek()

        result_list = stack.stack
        check_list = []

        self.assertEqual(
            result_list, check_list,
            "Testing 'Peek'. Peek from empty stack. Stack lists not equal")
        self.assertIsNone(
            test_value,
            "Testing 'Peek'. Peek from empty stack. Peek value is incorrect")
    def test_pop_multiple_elements_from_normal_stack(self):
        stack = Stack()
        stack.push(1)
        stack.push(2)
        stack.push(3)

        test_value1 = stack.pop()
        check_value1 = 3

        test_value2 = stack.pop()
        check_value2 = 2

        result_list = stack.stack
        check_list = [1]

        self.assertEqual(
            result_list, check_list,
            "Testing 'Pop'. Pop multiple elements from normal stack. Stack lists not equal"
        )
        self.assertEqual(
            test_value1, check_value1,
            "Testing 'Pop'. Pop multiple elements from normal stack. Popped value not correct"
        )
        self.assertEqual(
            test_value2, check_value2,
            "Testing 'Pop'. Pop multiple elements from normal stack. Popped value not correct"
        )
    def test_pop_single_element_from_empty_stack(self):
        stack = Stack()

        test_value = stack.pop()

        result_list = stack.stack
        check_list = []

        self.assertEqual(
            result_list, check_list,
            "Testing 'Pop'. Pop single element from empty stack. Stack lists not equal"
        )
        self.assertIsNone(
            test_value,
            "Testing 'Pop'. Pop single element from empty stack. Popped value not correct"
        )
Example #7
0
 def test_pop_all(self):
     my_stack = Stack()
     my_stack.push(1)
     my_stack.push(2)
     my_stack.push(3)
     pop_el1 = my_stack.pop()
     pop_el2 = my_stack.pop()
     pop_el3 = my_stack.pop()
     data = my_stack.stack + [pop_el1] + [pop_el2] + [pop_el3]
     res =  [3, 2, 1]
     self.assertEqual( data, res)
Example #8
0
 def test_pop_all_e(self):
     my_stack = Stack()
     my_stack.push_to_end(1)
     my_stack.push_to_end(2)
     my_stack.push_to_end(3)
     pop_el1 = my_stack.pop_from_end()
     pop_el2 = my_stack.pop_from_end()
     pop_el3 = my_stack.pop_from_end()
     data = my_stack.stack + [pop_el1] + [pop_el2] + [pop_el3]
     res =  [3, 2, 1]
     self.assertEqual( data, res)
Example #9
0
    def test_empty(self):
        new_stack = Stack()
        new_stack.add(60)

        result_not_empty = new_stack.is_empty()
        self.assertTrue(result_not_empty)

        new_stack.remove()

        result_empty = new_stack.is_empty()
        self.assertFalse(result_empty)
Example #10
0
 def test_peek_e(self):
     my_stack = Stack()
     my_stack.push_to_end(1)
     my_stack.push_to_end(2)
     my_stack.push_to_end(3)
     data = my_stack.stack + [my_stack.peek_from_end()]
     res =  [1, 2, 3, 3]
     self.assertEqual( data, res)
Example #11
0
 def test_peek(self):
     my_stack = Stack()
     my_stack.push(1)
     my_stack.push(2)
     my_stack.push(3)
     data = my_stack.stack + [my_stack.peek()]
     res =  [3, 2, 1, 3]
     self.assertEqual( data, res)
Example #12
0
 def test_size(self):
     my_stack = Stack()
     my_stack.push(1)
     my_stack.push(2)
     my_stack.push(3)
     data = my_stack.size()
     res = 3
     self.assertEqual( data, res)
Example #13
0
 def test_pop_in_middle(self):
     my_stack = Stack()
     my_stack.push(1)
     my_stack.push(2)
     pop_el = my_stack.pop()
     my_stack.push(3)
     data = my_stack.stack + [pop_el]
     res =  [3, 1, 2]
     self.assertEqual( data, res)
Example #14
0
 def test_pop_one_e(self):
     my_stack = Stack()
     my_stack.push_to_end(1)
     my_stack.push_to_end(2)
     my_stack.push_to_end(3)
     pop_el = my_stack.pop_from_end()
     data = my_stack.stack + [pop_el]
     res =  [1, 2, 3]
     self.assertEqual( data, res)
Example #15
0
 def test_pop_one(self):
     my_stack = Stack()
     my_stack.push(1)
     my_stack.push(2)
     my_stack.push(3)
     pop_el = my_stack.pop()
     data = my_stack.stack + [pop_el]
     res =  [2, 1, 3]
     self.assertEqual( data, res)
Example #16
0
 def test_pop_in_middle_e(self):
     my_stack = Stack()
     my_stack.push_to_end(1)
     my_stack.push_to_end(2)
     pop_el = my_stack.pop_from_end()
     my_stack.push_to_end(3)
     data = my_stack.stack + [pop_el]
     res =  [1, 3, 2]
     self.assertEqual( data, res)
Example #17
0
 def test_push_e(self):
     my_stack = Stack()
     my_stack.push_to_end(1)
     my_stack.push_to_end(2)
     my_stack.push_to_end(3)
     data = my_stack.stack
     res =  [1, 2, 3]
     self.assertEqual( data, res)
Example #18
0
 def test_push(self):
     my_stack = Stack()
     my_stack.push(1)
     my_stack.push(2)
     my_stack.push(3)
     data = my_stack.stack
     res =  [3, 2, 1]
     self.assertEqual( data, res)
Example #19
0
def test_stack():
    print('Stack.push() 1, 3, 5, 7')
    stack = Stack()
    stack.push(1)
    stack.push(3)
    stack.push(5)
    stack.push(7)

    print('Stack.pop() loop')
    for item in stack:
        print('Item: %s Len: %d' % (item, len(stack)))
Example #20
0
    def test_push_multiple_elements_in_empty_stack(self):
        stack = Stack()
        stack.push(1)
        stack.push(2)
        stack.push(3)

        result_list = stack.stack
        check_list = [3, 2, 1]

        self.assertEqual(
            result_list, check_list,
            "Testing 'Push'. Pushing multiple elements. Stack lists not equal")
def check_balance(input_text, brackets=BRACKETS):
    brackets_stack = Stack()
    brackets_stack.list_of_values = []
    open_brackets = brackets[::2]
    close_brackets = brackets[1::2]
    # Проходим по каждому символу входного текста
    for symbol in input_text:
        # если символ это открывающая скобка, то добавляем его в стек
        if symbol in open_brackets:
            brackets_stack.push(open_brackets.index(symbol))
        # или если символ это закрывающая скобка
        elif symbol in close_brackets:
            # и если стек не пуст и  последняя добавленная открывающая скобка такого же типа, то удаляем
            if (not brackets_stack.is_empty()) and (brackets_stack.peek() == close_brackets.index(symbol)):
                brackets_stack.pop()
            # если не такого же типа, то сразу выводим ошибку
            else:
                return print('Несбалансированно')
    # проверяем стек, если он пустой, то возвращаем 'Сбалансированно', иначе - 'Несбалансированно'
    if not brackets_stack.list_of_values:
        return print('Сбалансированно')
    else:
        return print('Несбалансированно')
Example #22
0
    def test_peek_normal_stack(self):
        stack = Stack()
        stack.push(1)
        stack.push(2)
        stack.push(3)

        test_value = stack.peek()
        check_value = 3

        result_list = stack.stack
        check_list = [3, 2, 1]

        self.assertEqual(
            result_list, check_list,
            "Testing 'Peek'. Peek from normal stack. Stack lists not equal")
        self.assertEqual(
            test_value, check_value,
            "Testing 'Peek'. Peek from normal stack. Peek value is incorrect")
Example #23
0
 def test_random(self):
     for i in range(10000):
         s = Stack()
         res = []
         comand = random.randint(0, 1) # 0 - pop, 1 - push
         if comand:
             t = random.randint(0,9)
             s.push(t)
             res.append(t)
         else:
             s.pop()
             try:
                 res.pop()
             except:
                 pass
         data = s.stack + [s.size()]
         res =  res[::-1] + [len(res)]
         self.assertEqual( data, res)
Example #24
0
    def test_pop_single_element_from_normal_stack(self):
        stack = Stack()
        stack.push(1)
        stack.push(2)
        stack.push(3)

        test_value = stack.pop()
        check_value = 3

        result_list = stack.stack
        check_list = [2, 1]

        self.assertEqual(
            result_list, check_list,
            "Testing 'Pop'. Pop single element from normal stack. Stack lists not equal"
        )
        self.assertEqual(
            test_value, check_value,
            "Testing 'Pop'. Pop single element from normal stack. Popped value not correct"
        )
Example #25
0
 def testLastelement(self):
     s = Stack()
     s.add("Andy")
     self.assertTrue(s.check_last_item() == "Andy")
     s.add("Pignanelli")
     self.assertTrue(s.check_last_item() == "Pignanelli")
Example #26
0
 def testEmpty(self):
     s = Stack()
     self.assertTrue(s.isEmpty())
     s.add("Stuff")
     self.assertFalse(s.isEmpty())
Example #27
0
 def testSize(self):
     s = Stack()
     s.add("1")
     self.assertTrue(s.size() == 1)
     s.remove()
     self.assertFalse(s.size() == 1)
Example #28
0
 def testRemove(self):
     s = Stack()
     s.add("1")
     s.add("2")
     s.remove()
     self.assertTrue(s.size() == 1)
Example #29
0
 def testAdd(self):
     s = Stack()
     s.add("1")
     s.add("2")
     self.assertTrue(s.size() == 2)
Example #30
0
import pytest
from main import Stack

test_Stack = Stack()


class TestStack:
    def setup(self):
        print("method setup")

    def teardown(self):
        print("method teardown")

    def test_isEmpty(self):
        print(test_Stack)
        assert test_Stack.isEmpty()

    def test_push(self):
        test_Stack.push('test_1')
        last_el = test_Stack.stack[-1]
        del test_Stack.stack[-1]
        assert 'test_1' == last_el

    def test_pop(self):
        test_Stack.push('test_1')
        result = test_Stack.pop()
        assert result == 'test_1' and test_Stack.isEmpty

    def test_peek(self):
        test_Stack.push('test_1')
        assert test_Stack.peek() == test_Stack.stack[-1]