class TestLinkedListStack(unittest.TestCase): def setUp(self): self.st = LinkedListStack() def tearDown(self): del self.st # default feature test - push, pop, peek def test_01(self): self.st.push(1) self.st.push(2) self.st.push(3) self.assertEqual(self.st.peek(), 3) self.assertEqual(self.st.pop(), 3) self.assertEqual(self.st.peek(), 2) self.assertEqual(self.st.pop(), 2) self.assertEqual(self.st.peek(), 1) self.assertEqual(self.st.pop(), 1) # raise error when pop with empty stack def test_02(self): with self.assertRaises(IndexError) as cm: self.st.pop() self.assertEqual("stack is empty", str(cm.exception)) # test check empty def test_03(self): self.assertTrue(self.st.is_empty())
class TestLinkedListStack(unittest.TestCase): def setUp(self): self.stack = LinkedListStack() def test_push(self): self.stack.push(1) self.assertEqual(self.stack.peek(), 1) def test_pop(self): popped = self.stack.pop() self.assertIs(popped, None) self.stack.push(1) popped = self.stack.pop() self.assertEqual(popped, 1) def test_full_stack(self): for i in range(10): self.stack.push(i) self.assertEqual(self.stack.peek(), 9) for i in range(10): self.assertEqual(self.stack.pop(), 9 - i) self.assertEqual(self.stack.pop(), None)
def test_peek(self): # empty stack stack = LinkedListStack() with self.assertRaises(StackException): stack.peek() # add one thing then peek. it should still be there stack.push('a') self.assertEqual(stack.peek(), 'a') self.assertEqual(stack.peek(), 'a')
class TestStack(unittest.TestCase): def setUp(self): self.n1 = Node(1) self.n2 = Node(2) self.n3 = Node(3) self.stack = LinkedListStack() self.stack.push(1) self.stack.push(2) self.stack.push(self.n1) def test_peek(self): item = self.stack.peek() self.assertEqual(item, self.n1) def test_push(self): self.assertEqual(self.stack.get_size(), 3) self.stack.push(self.n2) self.assertEqual(self.stack.get_size(), 4) def test_pop(self): item = self.stack.pop() self.assertEqual(item, self.n1) def test_is_empty(self): empty_stack = LinkedListStack() self.assertEqual(empty_stack.is_empty(), True) self.assertEqual(self.stack.is_empty(), False) def test_get_size(self): empty_stack = LinkedListStack() self.assertEqual(empty_stack.get_size(), 0) self.assertEqual(self.stack.get_size(), 3)