interactive-coding-challenges/stacks_queues/stack/test_stack.py

44 lines
1.1 KiB
Python
Raw Normal View History

import unittest
2015-07-03 11:06:52 +08:00
class TestStack(unittest.TestCase):
2015-07-12 03:39:59 +08:00
2015-07-03 11:06:52 +08:00
# TODO: It would be better if we had unit tests for each
# method in addition to the following end-to-end test
def test_end_to_end(self):
print('Test: Empty stack')
stack = Stack()
self.assertEqual(stack.peek(), None)
self.assertEqual(stack.pop(), None)
2015-07-03 11:06:52 +08:00
print('Test: One element')
top = Node(5)
stack = Stack(top)
self.assertEqual(stack.pop(), 5)
self.assertEqual(stack.peek(), None)
2015-07-03 11:06:52 +08:00
print('Test: More than one element')
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
self.assertEqual(stack.pop(), 3)
self.assertEqual(stack.peek(), 2)
self.assertEqual(stack.pop(), 2)
self.assertEqual(stack.peek(), 1)
self.assertEqual(stack.is_empty(), False)
self.assertEqual(stack.pop(), 1)
self.assertEqual(stack.peek(), None)
self.assertEqual(stack.is_empty(), True)
2015-07-12 03:39:59 +08:00
2015-07-03 11:06:52 +08:00
print('Success: test_end_to_end')
2015-07-12 03:39:59 +08:00
2015-07-03 11:06:52 +08:00
def main():
test = TestStack()
test.test_end_to_end()
2015-07-12 03:39:59 +08:00
2015-07-03 11:06:52 +08:00
if __name__ == '__main__':
main()