def test_size(self): """ Test for size method """ stack = Stack() self.assertEqual(stack.size(), 0) stack.push(1) self.assertEqual(stack.size(), 1)
def test_push(self): """ Test for push method """ stack = Stack() self.assertEqual(stack.size(), 0) stack.push(1) stack.push(2) stack.push(3) self.assertEqual(stack.size(), 3)
def test_new_stack_is_empty(self): """ Create an empty Stack. Test that its size is 0. """ stack = Stack() self.assertTrue(stack.empty()) self.assertEqual(stack.size(), 0)
def test_push_sequence_of_elements(self): """ Push a sequence of elements in stack. Test that its size equals to the length of the given sequence. Pop all elements from stack and check reversed order. """ stack = Stack() elements = (1, 2, "string", None, 0, Stack()) map(stack.push, elements) self.assertEqual(stack.size(), len(elements)) for index, element in enumerate(reversed(elements)): top = stack.top() self.assertEqual(top, element) stack.pop() number_pop_elements = index + 1 expected_current_stack_size = len(elements) - number_pop_elements self.assertEqual(stack.size(), expected_current_stack_size) self.assertTrue(stack.empty())
def test_push_element(self): """ Push an element in stack. Test that its size is 1. """ stack = Stack() stack.push(None) self.assertFalse(stack.empty()) self.assertEqual(stack.size(), 1)
def test_new_stack_from_generator(self): """ Create a Stack from a generator. Test that its size equals to the number provided in the generator. """ stack = Stack(range(10)) self.assertFalse(stack.empty()) self.assertEqual(stack.size(), 10) self.assertEqual(stack.top(), 9)
def test_new_stack_from_list(self): """ Create a Stack from a list. Check that the size of stack equals to the size of the list. Check that the top element of stack equals to the latest element of the list. """ data_to_stack = (1, 3, 5, 7, 2, 4) stack = Stack(data_to_stack) self.assertFalse(stack.empty()) self.assertEqual(stack.size(), len(data_to_stack)) self.assertEqual(stack.top(), data_to_stack[-1])
def test_pop(self): """ Test for pop method """ stack = Stack() self.assertEqual(stack.pop(), None) stack.push(1) stack.push(2) stack.push(3) self.assertEqual(stack.pop(), 3) self.assertEqual(stack.size(), 2)
class TestStack(unittest.TestCase): def setUp(self): self.stack = Stack() def test_define_a_stack(self): self.assertIsInstance(self.stack, Stack) def test_if_stack_is_empty(self): self.assertTrue(self.stack.is_empty()) def test_if_stack_is_not_empty(self): self.stack.push(1) self.assertFalse(self.stack.is_empty()) def test_add_element_to_stack(self): self.stack.push(1) self.assertEquals(self.stack.stack, [1]) def test_add_element_to_an_fulfilled_stack(self): self.stack.push(1) with self.assertRaises(FullStackError): self.stack.push(2) def test_pop_an_element_to_stack(self): self.stack.push(1) element = self.stack.pop() self.assertEquals(element, 1) def test_pop_an_element_from_empty_stack(self): with self.assertRaises(EmptyStackError): self.stack.pop() def test_peek_an_element_from_stack(self): self.stack.max_size = 2 # Patch object's capacity self.stack.push(1) self.stack.push(2) element = self.stack.peek() self.assertEquals(element, 2) def test_peek_an_element_from_empty_stack(self): with self.assertRaises(EmptyStackError): self.stack.peek() def test_get_size_if_stack(self): self.stack.push(1) self.assertEquals(self.stack.size(), 1)
class TowerOfHanoi: def __init__(self): self.pegA = Stack("A") self.pegB = Stack("B") self.pegC = Stack("C") def init_discs(self, n=3): for i in range(n, 0, -1): self.pegA.push(i) def transfer(self, frm, to, via, n): if n == 1: el = frm.pop() to.push(el) print("{} moved from {} to {}".format(el, frm.name, to.name)) else: self.transfer(frm=frm, to=via, via=to, n=n - 1) self.transfer(frm, to, via, n=1) self.transfer(frm=via, to=to, via=frm, n=n - 1) def start(self): self.transfer(self.pegA, self.pegC, self.pegB, self.pegA.size())
def test_size_retrieved_using_len(self): stack = Stack() assert len(stack) == stack.size()
def test_initialized_stack_has_size_0(self): stack = Stack() assert stack.size() == 0