Ejemplo n.º 1
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())
Ejemplo n.º 2
0
    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())
Ejemplo n.º 3
0
    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')
Ejemplo n.º 4
0
    def test_size(self):
        # empty stack
        stack = LinkedListStack()
        self.assertEqual(stack.size(), 0)

        # one item
        stack.push('a')
        self.assertEqual(stack.size(), 1)

        # +1 item
        stack.push('b')
        self.assertEqual(stack.size(), 2)
Ejemplo n.º 5
0
    def test_push(self):
        """
        We can push an item on the stack. We should not get an error
        """
        stack = LinkedListStack()

        # nothing sent it
        with self.assertRaises(TypeError):
            stack.push()

        # add a bunch of items and push. no errors
        mixed_list = [1, 'a', [6, 7, 8], {"cat": "sylvester"}, None]
        for item in mixed_list:
            stack.push(item)
Ejemplo n.º 6
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)
Ejemplo n.º 7
0
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)
Ejemplo n.º 8
0
    def test_pop(self):
        """
        We can pop an item from the stack
        """
        # empty stack
        stack = LinkedListStack()
        with self.assertRaises(StackException):
            stack.pop()

        # add one thing then pop
        stack.push('a')
        self.assertEqual(stack.pop(), 'a')
        with self.assertRaises(StackException):
            stack.pop()

        # add 3 things then pop
        stack.push('a')
        stack.push('b')
        stack.push('c')
        self.assertEqual(stack.pop(), 'c')
        self.assertEqual(stack.pop(), 'b')
        self.assertEqual(stack.pop(), 'a')
        with self.assertRaises(StackException):
            stack.pop()
Ejemplo n.º 9
0
class QueueTests(unittest.TestCase):
    def setUp(self):
        self.stack = LinkedListStack()

    def test_len_returns_0_for_empty_stack(self):
        self.assertEqual(len(self.stack), 0)

    def test_len_returns_correct_length_after_push(self):
        self.assertEqual(len(self.stack), 0)
        self.stack.push(2)
        self.assertEqual(len(self.stack), 1)
        self.stack.push(4)
        self.assertEqual(len(self.stack), 2)
        self.stack.push(6)
        self.stack.push(8)
        self.stack.push(10)
        self.stack.push(12)
        self.stack.push(14)
        self.stack.push(16)
        self.stack.push(18)
        self.assertEqual(len(self.stack), 9)

    def test_empty_pop(self):
        self.assertIsNone(self.stack.pop())
        self.assertEqual(len(self.stack), 0)

    def test_pop_respects_order(self):
        self.stack.push(100)
        self.stack.push(101)
        self.stack.push(105)
        self.assertEqual(self.stack.pop(), 105)
        self.assertEqual(len(self.stack), 2)
        self.assertEqual(self.stack.pop(), 101)
        self.assertEqual(len(self.stack), 1)
        self.assertEqual(self.stack.pop(), 100)
        self.assertEqual(len(self.stack), 0)
        self.assertIsNone(self.stack.pop())
        self.assertEqual(len(self.stack), 0)