class DoublyDoublyLinkedList_Test_Operations_On_List_with_1000_Random_Elements( unittest.TestCase): def setUp(self): self._linked_list = DoublyLinkedList() self._shadow_list = [] for i in range(10): random_element = randint(0, 1000) self._linked_list.append(random_element) self._shadow_list.append( random_element) #will keep track of count and index def test_count_index_remove_index_count_on_each_removal(self): """ Check count, index before and after each predictable removal operation """ for i in self._shadow_list[:]: self.assertEqual(self._linked_list.length, len(self._shadow_list)) self.assertEqual(self._linked_list.index(i), self._shadow_list.index(i)) self._shadow_list.remove(i) self._linked_list.remove(i) try: self._shadow_list.index(i) self.assertEqual(self._linked_list.index(i), self._shadow_list.index(i)) except ValueError: self.assertEqual(self._linked_list.index(i), -1) self.assertEqual(self._linked_list.length, len(self._shadow_list))
class DoublyDoublyLinkedList_Test_Remove_Non_Existing_Element_From_0_Element_List( unittest.TestCase): def setUp(self): self._doubly_linkedList = DoublyLinkedList() def test_count_of_list(self): self._doubly_linkedList.remove(-1) self.assertEqual(self._doubly_linkedList.length, 0, 'Length must be 0 for empty list') def tearDown(self): self._doubly_linkedList = None
class DoublyDoublyLinkedList_Test_Check_Head_On_Remove_In_4_Element_List( unittest.TestCase): def setUp(self): self._doubly_linkedList = DoublyLinkedList() self._doubly_linkedList.append(1) self._doubly_linkedList.append(2) self._doubly_linkedList.append(3) self._doubly_linkedList.append(4) def test_tail_node_on_remove(self): #list is [1, 2, 3, 4] self._doubly_linkedList.remove( 3) #removed head. Head must be same node self.assertEqual(self._doubly_linkedList.head, 1) #Now list is [1, 2, 4] self._doubly_linkedList.remove(1) #head should next be 2 self.assertEqual(self._doubly_linkedList.head, 2, 'head must be adjusted on deletion of first node') #Now list is [2, 4] self._doubly_linkedList.remove(2) #head should next be 4 self.assertEqual(self._doubly_linkedList.head, 4, 'head must be adjusted on deletion of first node') #Now list is [4] self._doubly_linkedList.remove(4) #head should next be None self.assertEqual(self._doubly_linkedList.head, None, 'Head must be None on empty list') def tearDown(self): self._doubly_linkedList = None
class DoublyDoublyLinkedList_Test_Check_Tail_On_Remove_In_4_Element_List( unittest.TestCase): def setUp(self): self._doubly_linkedList = DoublyLinkedList() self._doubly_linkedList.append(1) self._doubly_linkedList.append(2) self._doubly_linkedList.append(3) self._doubly_linkedList.append(4) def test_tail_node_on_remove(self): self._doubly_linkedList.remove( 1) #removed head. Tail must be same node self.assertEqual(self._doubly_linkedList.tail, 4) #Now list is [2, 3, 4] self._doubly_linkedList.remove(4) #tail should next be 3 self.assertEqual(self._doubly_linkedList.tail, 3, 'Tail must be adjusted on deletion of last node') #Now list is [2, 3] self._doubly_linkedList.remove(2) #tail should next be 3 self.assertEqual(self._doubly_linkedList.tail, 3, 'Tail must be adjusted on deletion of last node') #Now list is [3] self._doubly_linkedList.remove(3) #tail should next be None self.assertEqual(self._doubly_linkedList.tail, None, 'Tail must be None on empty list') def tearDown(self): self._doubly_linkedList = None
class DoublyDoublyLinkedList_Test_Remove_Middle_Element_From_4_Element_List( unittest.TestCase): def setUp(self): self._doubly_linkedList = DoublyLinkedList() self._doubly_linkedList.append(1) self._doubly_linkedList.append(2) self._doubly_linkedList.append(3) self._doubly_linkedList.append(4) def test_count_of_list(self): self._doubly_linkedList.remove(3) self.assertEqual( self._doubly_linkedList.length, 3, 'Length not valid after trying to remove non-existin element') def tearDown(self): self._doubly_linkedList = None
print('Tail of list is: %s' % str(double_link_list.tail)) print('Print list by iterating') #Find an element's index element = 4 element_index = double_link_list.index(element) if element_index == -1: print('Element %s was not found on list' % str(element)) else: print('Element %s found at index %s' % (str(element), str(element_index))) #Lets delete 7 print('Deleting 7') element_to_remove = 7 double_link_list.remove(element_to_remove) #Try to find it. print('Trying to find %s' % str(element_to_remove)) index_of_deleted = double_link_list.index(element_to_remove) print('Index of deleted element is %s' % str(index_of_deleted)) #print list again print_list(double_link_list) # #Lets insert 7 at index 5 print('Insert 7 into the list at index 4') element_to_insert = 7 index_at = 5 double_link_list.insert_at(index=index_at, element=element_to_insert) print_list(double_link_list)