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 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)