class LinkedList_Test_get_item_With_multiple_Elements(unittest.TestCase):
    
    def setUp(self):
        self._linkedList = LinkedList()
        self._extend_elements = [1, 2, 3, 4]
        self._linkedList.extend(self._extend_elements)
    
    def test_get_item_with_multiple_elements(self):
        for i in self._extend_elements:#loop through the items we added. item 1 will be at index 0, 2 at 1 and so on.
            self.assertEqual(self._linkedList[i - 1], i, 'list get item at index failed')
    
    def tearDown(self):
        self._linkedList = None
class LinkedList_Test_Reverse_Iteration_With_multiple_Elements(unittest.TestCase):
    
    def setUp(self):
        self._linkedList = LinkedList()
        extend_elements = [1, 2, 3, 4]
        self._linkedList.extend(extend_elements)
    
    def test_reverse_iteration_with_multiple_elements(self):
        expected_reverse_iter_list  = [4, 3, 2, 1] 
        reverse_iter_of_list = [] 
        for element in reversed(self._linkedList):
            reverse_iter_of_list.append(element)

        self.assertEqual(reverse_iter_of_list, expected_reverse_iter_list, 'reverse iteration did not yield expected list')
        
    def tearDown(self):
        self._linkedList = None
class LinkedList_Test_Remove_Duplicates_With_Duplicates_At_Middle(unittest.TestCase):
    
    def setUp(self):
        #build [2, 1, 1, 1, 3]
        self._linkedList = LinkedList()
        self._linkedList.append(2)
        element_to_append = 1
        self._linkedList.extend([element_to_append, element_to_append, element_to_append])
        self._linkedList.append(3)
        
    def test_remove_duplicates_of_list_with_2_duplicates_in_Middle(self):
        expected_head = self._linkedList._head
        expected_tail = self._linkedList._tail
        
        self._linkedList.remove_duplicates()
        expected_length = 3
        self.assertEqual(self._linkedList.length, expected_length, 'Linked list length did not add up after removing duplicates')
        self.assertEqual(self._linkedList._head, expected_head, 'Linked list head did not add up after removing duplicates')
        self.assertEqual(self._linkedList._tail, expected_tail, 'Linked list tail did not add up after removing duplicates')
        
    def tearDown(self):
        self._linkedList = None
class LinkedList_Test_Remove_Duplicates_With_Duplicates_At_Tail(unittest.TestCase):
    
    def setUp(self):
        #build [2, 1, 1, 1]
        self._linkedList = LinkedList()
        self._linkedList.append(2)
        element_to_append = 1
        self._linkedList.extend([element_to_append, element_to_append, element_to_append])
        
    def test_remove_duplicates_of_list_with_2_identical_elements(self):
        expected_head = self._linkedList._head
        expected_tail = self._linkedList._head.next_node
        
        self._linkedList.remove_duplicates()
        expected_length = 2
        #compare references of head and tail
        self.assertEqual(self._linkedList.length, expected_length, 'Linked list length did not add up after removing duplicates')
        self.assertEqual(self._linkedList._head, expected_head, 'Linked list head did not add up after removing duplicates')
        self.assertEqual(self._linkedList._tail, expected_tail, 'Linked list tail did not add up after removing duplicates')
        
    def tearDown(self):
        self._linkedList = None
class LinkedList_Test_extend_With_Predictable_Elements(unittest.TestCase):
    '''
    Test the extend operation of singly_linked_list. 
    a) Create 2 lists. 
    b) Add the second list to the end of the first list. 
    c) Check the indices of the newly added elements.
    '''
    
    def setUp(self):
        self._linkedList = LinkedList()
        self._linkedList.append(1)
        self._linkedList.append(2)
        self._linkedList.append(3)
        self._linkedList.append(4)
        #create a second linkedlist with 5, 6, 7
        self._secondList = LinkedList()
        self._secondList.append(5)
        self._secondList.append(6)
        self._secondList.append(7)
        
    def test_extend_of_list(self):
        self.assertEqual(self._linkedList.length, 4, 'Original list before extend must be 4')
        count_before_extend = self._linkedList.length
        next_index_will_be = count_before_extend
        self._linkedList.extend(self._secondList)
        #check new length
        self.assertEqual(self._linkedList.length, count_before_extend + self._secondList.length, 'New length after extend must add up.')
        #check if the elements of shadow list are present
        for element in self._secondList:
            self.assertEqual(self._linkedList.index(element), next_index_will_be
                             , 'Indices of new elements after extend must add up')
            next_index_will_be += 1
    
    def tearDown(self):
        self._linkedList = None
        self._secondList = None
예제 #6
0
    print('Reversed List is now %s' % elements_in_list)
    #length of the list
    print('Length of the list is %s' % len(link_list))


if __name__ == '__main__':
    link_list = LinkedList()

    #add one element
    print('Adding element 1 to list')
    link_list.append(1)

    #we add the following list (iterable) of elements
    print('Adding elements 2 ,3 , 4, 5, 6, 7, 8, 9, 10')
    elements = [2, 3, 4, 5, 6, 7, 8, 9, 10]
    link_list.extend(elements)

    #head
    print('Head of list is: %s' % str(link_list.head))

    #tail
    print('Tail of list is: %s' % str(link_list.tail))
    print('Print list by iterating')

    #Find an element's index
    element = 4
    element_index = 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' %