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_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)
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)
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)
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)
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('Несбалансированно')
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" )
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" )
def test_pop_none(self): my_stack = Stack() pop_el = my_stack.pop() data = my_stack.stack + [pop_el] res = [None] self.assertEqual( data, res)