Esempio n. 1
0
 def test_stack_array(self):
     arr1 = StackArray()
     arr1.push(4)
     arr1.push(5)
     self.assertEqual(arr1.size(), 2)
     self.assertEqual(arr1.is_full(), True)
     arr1.enlarge()
     self.assertEqual(arr1.is_full(), False)
     arr1.push(6)
     arr1.push(7)
     self.assertEqual(arr1.is_full(), True)
     self.assertEqual(arr1.pop(), 7)
     arr1.pop()
     arr1.pop()
     arr1.pop()
     self.assertEqual(arr1.is_empty(), True)
     arr2 = StackArray()
     arr2.enlarge()
     arr2.enlarge()
     arr2.shrink()
     self.assertEqual(arr2.capacity, 4)
     arr2.enlarge()
     arr2.push(5)
     arr2.push(9)
     arr2.push(10)
     self.assertEqual(arr2.peek(), 10)
     arr3 = StackArray()
     self.assertEqual(arr3.peek(), None)
     print(arr2.arr)
     arr4 = StackArray()
     with self.assertRaises(IndexError):
         arr4.pop()
Esempio n. 2
0
 def test_size(self):
     stk_a = StackArray(4)
     stk_a.push(6)
     stk_a.push(7)
     stk_a.push(8)
     stk_a.push(9)
     self.assertTrue(stk_a.is_full())
     with self.assertRaises(IndexError): 
         stk_a.push(0)
     self.assertEqual(stk_a.size(), 4 )
     self.assertEqual(stk_a.pop(), 9 )
     self.assertEqual(stk_a.size(), 3 )
     self.assertFalse(stk_a.is_empty())
Esempio n. 3
0
 def test_StackArray(self):
     stack = StackArray(3)
     self.assertRaises(IndexError, stack.pop)
     self.assertEqual(stack.is_empty(), True)
     stack.push('book1')
     stack.push('book2')
     self.assertEqual(stack.peek(), 'book2')
     self.assertEqual(stack.pop(), 'book2')
     self.assertEqual(stack.peek(), 'book1')
     stack.push('book2_2')
     stack.push('book3')
     self.assertRaises(IndexError, stack.push, 'book4')
     self.assertEqual(stack.size(), 3)
     self.assertEqual(stack.is_full(), True)