Skip to content

Instantly share code, notes, and snippets.

@mcclane
Created October 9, 2018 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcclane/4a5cf74e8727737aa76ecaea5735c154 to your computer and use it in GitHub Desktop.
Save mcclane/4a5cf74e8727737aa76ecaea5735c154 to your computer and use it in GitHub Desktop.
import unittest
# Use the imports below to test either your array-based stack
# or your link-based version
from stack_array import Stack
# from stack_linked import Stack
class TestLab2(unittest.TestCase):
def test_simple(self):
"""Test simple stack operations"""
stack = Stack(5)
stack.push(0)
self.assertFalse(stack.is_empty())
self.assertFalse(stack.is_full())
self.assertEqual(stack.size(), 1)
def test_simple_peek_return(self):
"""Test peek and return stack operations"""
stack = Stack(5)
stack.push(0)
self.assertFalse(stack.is_empty())
self.assertFalse(stack.is_full())
self.assertEqual(stack.size(),1)
self.assertEqual(stack.peek(), 0)
self.assertEqual(stack.pop(), 0)
def test_index_error_empty_pop(self):
"""Make sure popping an empty stack results in an IndexError"""
stack = Stack(10)
for i in range(10):
stack.push(i)
self.assertTrue(stack.is_full())
for i in range(9, -1, -1):
self.assertEqual(stack.pop(), i)
self.assertTrue(stack.is_empty())
with self.assertRaises(IndexError):
stack.peek()
with self.assertRaises(IndexError):
stack.pop()
def test_index_error_full_push(self):
"""Make sure pushing to a full stack results in an IndexError"""
stack = Stack(10)
for i in range(10):
stack.push(i)
with self.assertRaises(IndexError):
stack.push(10)
def test_large_stack(self):
"""Test stack operations on a larger scale"""
stack = Stack(100000)
for i in range(100000):
stack.push(i)
for i in range(99999, -1, -1):
self.assertEqual(stack.pop(), i)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment