def test_is_empty(self):
        # empty stack
        stack = LinkedListStack()
        self.assertTrue(stack.is_empty())

        # non-empty stack
        stack.push('a')
        self.assertFalse(stack.is_empty())
Esempio n. 2
0
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)
Esempio n. 3
0
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())
Esempio n. 4
0
 def test_is_empty(self):
     empty_stack = LinkedListStack()
     self.assertEqual(empty_stack.is_empty(), True)
     self.assertEqual(self.stack.is_empty(), False)