Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Stack class, unit tests below it (courtesy of Blake!) (to be put in their own file!)
- class Stack:
- def __init__(self):
- """Cannot be initialized with data!"""
- self.m_data = []
- def size(self):
- """Returns the size of the stack"""
- return len(self.m_data)
- def is_empty(self):
- """Returns a bool showing whether or not the stack is empty"""
- if self.size() == 0:
- result = True
- else:
- result = False
- return result
- def push(self,input):
- """Pushes the next element onto the stack"""
- self.m_data.append(input)
- def peek(self):
- """Returns the last item on the stack"""
- return self.m_data[self.size()-1]
- def pop(self):
- """Returns the last item on the stack, and removes it from the stack"""
- return self.m_data.pop()
- #unit tests (made by Blake)
- import unittest
- import Stack
- class stackOperationMethods(unittest.TestCase):
- def test_returns_empty_stack(self):
- self.assertIsInstance(Stack.Stack(), Stack.Stack)
- def test_push_item(self):
- newStack = Stack.Stack()
- prePushSize = newStack.size()
- newStack.push(1)
- self.assertTrue(newStack.size() == prePushSize + 1)
- def test_pop(self):
- newStack = Stack.Stack()
- prePopSize = newStack.size()
- newStack.push(1)
- self.assertTrue(newStack.pop() == 1)
- self.assertTrue(newStack.size() == 0)
- def test_peek(self):
- newStack = Stack.Stack()
- prePeekSize = newStack.size()
- newStack.push(1)
- self.assertTrue(newStack.peek() == 1)
- self.assertTrue(newStack.size() == 1)
- def test_is_empty(self):
- newStack = Stack.Stack()
- self.assertTrue(newStack.is_empty() == True)
- newStack.push(1)
- self.assertFalse(newStack.is_empty() == True)
- newStack.pop()
- self.assertTrue(newStack.is_empty()== True)
- def test_size(self):
- newStack = Stack.Stack()
- for i in range(1,10):
- newStack.push(i)
- self.assertTrue(newStack.size() == 9)
- if __name__ == "__main__":
- unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement