Created
October 9, 2018 04:19
-
-
Save mcclane/4a5cf74e8727737aa76ecaea5735c154 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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