diff --git a/resources/stack.py b/resources/stack.py index 41d86bd5bb6de63c75351e1e80b142817476a99e..ee63fdde2a217aebf575101f57bcd0f77f62b435 100644 --- a/resources/stack.py +++ b/resources/stack.py @@ -13,3 +13,49 @@ class Stack(): """ Custom stack implementation. """ + def __init__(self): + self._data_ = [] + + def push(self, value): + """ + Adds new item to stack. + :param value: Item to add. + """ + self._data_.append(value) + + def pop(self): + """ + Remove most recent item from stack. + :return: Most recent item added to stack. + """ + if self.size() > 0: + return self._data_.pop() + else: + return None + + def is_empty(self): + """ + Checks if stack is empty. + :return: Boolean indicating if empty. + """ + if self.size() == 0: + return True + else: + return False + + def top(self): + """ + Gets top element of stack, without removing item. + :return: Top element of stack. + """ + if self.size() > 0: + return self._data_[self.size() - 1] + else: + return None + + def size(self): + """ + Gets count of elements within stack. + :return: Count of elements in stack. + """ + return len(self._data_) diff --git a/tests/resources/stack.py b/tests/resources/stack.py index 33dd055e3fea74d97eaefd3714e7a1de8750d227..990eea57d46413580f8f82a9a975211cc3ed2f38 100644 --- a/tests/resources/stack.py +++ b/tests/resources/stack.py @@ -23,7 +23,70 @@ class TestMain(unittest.TestCase): self.assertTrue(isinstance(self.test_stack, Stack)) def test_push(self): - pass + # Test initial state. + self.assertEqual(len(self.test_stack._data_), 0) + + # Test adding first element. + self.test_stack.push(4) + self.assertEqual(len(self.test_stack._data_), 1) + self.assertEqual(self.test_stack._data_[0], 4) + + # Test adding second element. + self.test_stack.push('testing') + self.assertEqual(len(self.test_stack._data_), 2) + self.assertEqual(self.test_stack._data_[0], 4) + self.assertEqual(self.test_stack._data_[1], 'testing') + + # Works with 0, 1, and 2 elements. Assuming it works for all further n+1 elements. def test_pop(self): - pass + # Test with zero elements. + self.assertIsNone(self.test_stack.pop()) + + # Test with one element. + self.test_stack.push(4) + self.assertEqual(self.test_stack.pop(), 4) + + # Test with two elements. + self.test_stack.push(4) + self.test_stack.push('testing') + self.assertEqual(self.test_stack.pop(), 'testing') + self.assertEqual(self.test_stack.pop(), 4) + + # Works with 0, 1, and 2 elements. Assuming it works for all further n+1 elements. + + def test_is_empty(self): + # Test with zero elements. + self.assertTrue(self.test_stack.is_empty()) + + # Test with more than zero elements. + self.test_stack.push('') + self.assertFalse(self.test_stack.is_empty()) + + def test_top(self): + # Test with zero elements. + self.assertIsNone(self.test_stack.top()) + + # Test with one element. + self.test_stack.push(5) + self.assertEqual(self.test_stack.top(), 5) + + # Test with two elements. + self.test_stack.push('testing') + self.assertEqual(self.test_stack.top(), 'testing') + + # Works with 0, 1, and 2 elements. Assuming it works for all further n+1 elements. + + def test_size(self): + # Test with zero elements. + self.assertEqual(self.test_stack.size(), 0) + + # Test with one element. + self.test_stack.push(6) + self.assertEqual(self.test_stack.size(), 1) + + # Test with two elements. + self.test_stack.push('testing') + self.assertEqual(self.test_stack.size(), 2) + + # Works with 0, 1, and 2 elements. Assuming it works for all further n+1 elements.