Exemplo n.º 1
0
 def test_getitem(self):
     obj = ArrayOperations()
     obj.append(20)
     obj.append(40)
     with self.assertRaises(IndexError):
         obj.__getitem__(
             2
         )  # Case_1: Index 2 is out of range since count is 2, only index 0 and 1 exist, hence indexerror will be raised
     self.assertLess(
         1, len(obj)
     )  # Case_2: Index 1 is in range, as two items apppended to give self.count = 2 (index < count i.e. index in range)
     with self.assertRaises(IndexError):
         obj.__getitem__(
             -1
         )  # Case_3: Accounting for negative indexes which are also out of range
Exemplo n.º 2
0
 def test_remove(self):
     obj = ArrayOperations()
     obj.append(30)
     obj.append(40)
     obj.append(50)
     obj.remove(40)
     self.assertEqual(
         2, len(obj)
     )  # Case_1: checks whether the delete function works by comparing whether the count is equal to length after deleting an item at index 1
     self.assertEqual(
         50, obj.__getitem__(1)
     )  # Case_2: Checks whether the list has shifted after deletion of an item, this test proves that 50 has moved to index 1 from index 2
     with self.assertRaises(
             ValueError
     ):  # Case_3: Since 6 does not exist as an item in the list, the ValueError is raised.
         obj.remove(6)
Exemplo n.º 3
0
 def test_delete(self):
     obj = ArrayOperations()
     obj.append(30)
     obj.append(40)
     obj.append(50)  # obj count initially 3
     obj.delete(1)  # after delete obj count 2
     self.assertEqual(
         2, len(obj)
     )  # Case_1: checks whether the delete function works by comparing whether the count is equal to length after deleting an item at index 1
     self.assertEqual(
         50, obj.__getitem__(1)
     )  # Case_2: Checks whether the list has shifted after deletion of an item, this test proves that 50 has moved to index 1 from index 2
     with self.assertRaises(IndexError):
         obj.delete(
             3
         )  # Case_3: Checks whether the index is less than the count to check if index is valid