class StackTest(unittest.TestCase): def setUp(self): self.stack = Stack(3) # Test __init__() def test_a_Init(self): self.assertEqual(self.stack.max_size, 3) # Test __init__() -> push() -> pop() def test_b_PushPop(self): self.stack.push("test") self.assertFalse(self.stack.isEmpty()) self.assertTrue(self.stack.size() == 1) self.assertEqual(self.stack.check().value, "test") self.assertEqual(self.stack.peek().value, "test") self.assertTrue(self.stack.isEmpty()) self.stack.push("test1") self.stack.push("test2") self.stack.push("test3") self.assertTrue(self.stack.isFull()) self.assertEqual(self.stack.size(), self.stack.max_size) try: self.stack.push("test4") except Exception as ex: self.assertEqual(str(ex), "Stack overflow") self.assertEqual(self.stack.pop().value, "test3") self.assertEqual(self.stack.pop().value, "test2") self.assertEqual(self.stack.pop().value, "test1") try: self.stack.pop() except Exception as ex: self.assertEqual(str(ex), "Stack underflow")
def test_04_plein_puch__size_isEmpty_should_be_true_isFull_shouldBeFalse(self): stack = Stack(1) stack.push(1) self.assertEqual(stack.size(), 1) self.assertFalse(stack.isEmpty()) self.assertTrue(stack.isFull()) stack.pop() self.assertTrue(stack.isEmpty()) self.assertFalse(stack.isFull())
def testPushPop(self): for size in range(5, 10): stack = Stack(size) for i in range(1, size): stack.push(i) self.assertEqual(stack.size(), i) self.assertEqual(stack.check(), i) self.assertFalse(stack.isEmpty()) self.assertFalse(stack.isFull()) stack.push("last") self.assertEqual(stack.size(), size) self.assertEqual(stack.check(), "last") self.assertFalse(stack.isEmpty()) self.assertTrue(stack.isFull()) for i in range(10): self.assertRaises(ValueError, stack.push, "fail") self.assertEqual(stack.size(), size) self.assertEqual(stack.check(), "last") self.assertFalse(stack.isEmpty()) self.assertTrue(stack.isFull()) self.assertEqual(stack.pop(), "last") for i in range(size - 1, 1, -1): self.assertEqual(stack.pop(), i) self.assertFalse(stack.isEmpty()) self.assertFalse(stack.isFull()) self.assertEqual(stack.pop(), 1) self.assertTrue(stack.isEmpty()) self.assertFalse(stack.isFull()) self.assertRaises(ValueError, stack.pop)
def convert_int_to_bin(dec_num): s = Stack() while dec_num > 0: rem = dec_num % 2 s.push(rem) dec_num = dec_num // 2 binary = "" while len(s.get_stack()) > 0: binary = binary + str(s.pop()) return binary
def isBalancedSet(bracket_set): s = Stack() for bracket in bracket_set: cb = opening_brackets.get(bracket) ob = closing_brackets.get(bracket) if cb is not None: s.push(bracket) elif ob is not None: pb = s.pop() if ob == pb: continue else: return -1 else: return None return -1 if len(s.get_stack()) > 0 else 1
class testStack(unittest.TestCase): def setUp(self): self.MAX_SIZE = 10 self.stack = Stack(self.MAX_SIZE) # Cas de test pour le cas vide def test_04_Vide_isEmpty_should_be_true(self): self.assertTrue(self.stack.isEmpty()) def test_04_vide_isFull_should_BeFalse_isEmpty_should_be_true(self): self.assertFalse(self.stack.isFull()) self.assertTrue(self.stack.isEmpty()) def test_04_vide_Size_should_BeZero_isEmpty_should_be_true(self): self.assertEqual(self.stack.size(), 0) self.assertTrue(self.stack.isEmpty()) def test_04_vide_peek_should_raiseError_isEmpty_should_be_true(self): self.assertRaises(ValueError, self.stack.peek) self.assertTrue(self.stack.isEmpty()) def test_04_vide_pop_should_raiseError_isEmpty_should_be_true(self): self.stack.pop() self.assertEqual(0, self.stack.n) self.assertTrue(self.stack.isEmpty()) def test_04_vide___str____isEmpty_should_be_true(self): self.stack.__str__() self.assertTrue(self.stack.isEmpty()) # transition de l'etat vide a l'etat partiellement_plein def test_04_partiellement_plein_push__isEmpty_should_be_false_isFull_shouldBeFalse( self): self.stack.push(1) self.assertFalse(self.stack.isEmpty()) self.assertFalse(self.stack.isFull()) def test_04_partiellement_plein_pop_size_greatanThan_1__isEmpty_should_be_false_isFull_shouldBeFalse( self): self.stack.push(1) self.stack.push(2) self.stack.pop() self.assertFalse(self.stack.isEmpty()) self.assertFalse(self.stack.isFull()) def test_04_partiellement_plein_size_isEmpty_should_be_false_isFull_shouldBeFalse( self): self.stack.push(1) self.assertEqual(self.stack.size(), 1) self.assertFalse(self.stack.isEmpty()) self.assertFalse(self.stack.isFull()) def test_04_partiellement_plein_pop_isEmpty_should_be_false_isFull_shouldBeFalse( self): self.stack.push(1) self.stack.push(15) self.assertEqual(self.stack.peek(), 15) self.assertFalse(self.stack.isEmpty()) self.assertFalse(self.stack.isFull()) def test_04_partiellement_plein___str____isEmpty_should_be_false_isFull_shouldBeFalse( self): self.stack.push(1) self.stack.__str__() self.assertFalse(self.stack.isEmpty()) self.assertFalse(self.stack.isFull()) # push[n < max_size] stay in the same state def test_04_partiellement_plein_push_size_greatanThan_1__isEmpty_should_be_false_isFull_shouldBeFalse( self): self.stack.push(1) self.stack.push(2) self.assertFalse(self.stack.isEmpty()) self.assertFalse(self.stack.isFull()) # transition de l'etat partiellement_plein a l'etat plein def test_04_plein_push_size_greatanThan_1__isEmpty_should_be_false_isFull_shouldBeFalse( self): for i in range(1, self.MAX_SIZE + 1): self.stack.push(i) self.assertFalse(self.stack.isEmpty()) self.assertTrue(self.stack.isFull()) def test_04_plein_push__isEmpty_should_be_false_isFull_shouldBeFalse(self): for i in range(1, self.MAX_SIZE + 1): self.stack.push(i) self.assertEqual(self.stack.size(), self.MAX_SIZE) self.assertTrue(self.stack.isFull()) def test_04_plein_push_should_be_false_isFull_shouldBeFalse(self): for i in range(1, self.MAX_SIZE + 1): self.stack.push(i) self.assertEqual(self.stack.size(), self.MAX_SIZE) self.assertRaises(ValueError, self.stack.push, self.MAX_SIZE + 1) self.assertEqual(self.stack.peek(), 10) self.stack.push(4) self.assertEqual(self.stack.peek(), 4) self.assertEqual(self.stack.size(), 9) self.assertFalse(self.stack.isFull()) def test_04_plein_pop_should_be_false_isFull_shouldBeFalse(self): for i in range(1, self.MAX_SIZE + 1): self.stack.push(i) self.stack.pop() self.assertEqual(self.stack.size(), self.MAX_SIZE - 1) self.assertFalse(self.stack.isEmpty()) self.assertFalse(self.stack.isFull()) def test_04_plein___str____isEmpty_should_be_false_isFull_should_be_true( self): for i in range(self.MAX_SIZE): self.stack.push(i) self.stack.__str__() self.assertFalse(self.stack.isEmpty()) self.assertTrue(self.stack.isFull()) def test_04_plein_push_should_not_add_when_trials_max_trials(self): for i in range(self.MAX_SIZE): self.stack.push(i) self.assertEqual(self.stack.size(), self.MAX_SIZE) self.assertRaises(ValueError, self.stack.push, self.MAX_SIZE + 1) # tests for stack with max_size == 1 def test_04_partiellement_plein_size_isEmpty_should_be_false_isFull_shouldBeFalse( self): stack = Stack(1) stack.push(1) self.assertEqual(stack.size(), 1) self.assertFalse(stack.isEmpty()) self.assertTrue(stack.isFull()) def test_04_plein_puch__size_isEmpty_should_be_true_isFull_shouldBeFalse( self): stack = Stack(1) stack.push(1) self.assertEqual(stack.size(), 1) self.assertFalse(stack.isEmpty()) self.assertTrue(stack.isFull()) stack.pop() self.assertTrue(stack.isEmpty()) self.assertFalse(stack.isFull()) def test_04_stack_should_return_correct_value_to_string(self): self.stack.push(4) self.assertEqual(str(self.stack.peek()), '4')