def test_insert_at_index(self):
     dll = DoublyLinkedList()
     dll.insert_at_index(0, 'B')
     assert dll.head.data == 'B'
     assert dll.tail.data == 'B'
     assert dll.size == 1
     dll.insert_at_index(1, 'C')
     assert dll.head.data == 'B'
     assert dll.tail.data == 'C'
     assert dll.size == 2
     dll.insert_at_index(0, 'A')
     assert dll.head.data == 'A'
     assert dll.tail.data == 'C'
     assert dll.size == 3
     with self.assertRaises(ValueError):
         dll.insert_at_index(4, 'D')
         dll.insert_at_index(-1, 'E')
Ejemplo n.º 2
0
 def test_insert_at_index(self):
     ll = DoublyLinkedList()
     ll.insert_at_index(0, 'B')  # append('B')
     assert ll.head.data == 'B'  # new head (at index 0)
     assert ll.tail.data == 'B'  # new tail (at index 0)
     assert ll.size == 1
     ll.insert_at_index(0, 'A')  # prepend('A')
     assert ll.head.data == 'A'  # new head (at index 0)
     assert ll.tail.data == 'B'  # unchanged (now at index 1)
     assert ll.size == 2
     ll.insert_at_index(2, 'D')  # append('D')
     assert ll.head.data == 'A'  # unchanged (at index 0)
     assert ll.tail.data == 'D'  # new tail (now at index 2)
     assert ll.size == 3
     ll.insert_at_index(2, 'C')  # insert 'C' between 'B' and 'D'
     assert ll.head.data == 'A'  # unchanged (at index 0)
     assert ll.tail.data == 'D'  # unchanged (now at index 3)
     assert ll.size == 4
     with self.assertRaises(ValueError):
         ll.insert_at_index(5, 'X')  # index too high
     with self.assertRaises(ValueError):
         ll.insert_at_index(-1, 'Y')  # index too low