def test_singly_linked_list_not_equal(self): """ Test operator "equal" - inverted. """ a_list1 = singly_linked_list.SinglyLinkedList() a_list2 = singly_linked_list.SinglyLinkedList() a_list1.append('a') a_list1.append('b') a_list1.append('c') a_list2.append('a') a_list2.append('b') a_list2.append('d') self.assertNotEqual(a_list1, a_list2)
def test_singly_linked_list_element_remove_first_two(self): """ Testing removing a linked list element from a linked list. In this test the linked list contains two elements prior to removing the first element. Before removing: list = [a] -> [b] After removing: list = [b] """ a_list = singly_linked_list.SinglyLinkedList() elem1 = singly_linked_list.SinglyLinkedListElement(a_list, 'b', None) elem0 = singly_linked_list.SinglyLinkedListElement(a_list, 'a', elem1) a_list.append(elem0.get_data()) a_list.append(elem1.get_data()) elem0.remove() res = [] res.append(a_list[0]) ref = [] ref.append(elem1) t_1 = a_list.get_head() == a_list[0] t_2 = a_list.get_tail() == a_list[0] t_3 = elem1.get_next() == None t_4 = len(a_list) == 1 t_5 = ref == res test = t_1 and t_2 and t_3 and t_4 and t_5 self.assertTrue(test)
def test_singly_linked_list_insert_at_empty(self): """ Test method "insert_at". """ a_list = singly_linked_list.SinglyLinkedList() a_list.insert_at(0, 'b') self.assertEqual('b', a_list[0].get_data())
def test_singly_linked_list_copy_equal(self): """ Test operator "copy". """ a_list1 = singly_linked_list.SinglyLinkedList() a_list1.append('a') a_list1.append('b') a_list1.append('c') a_list2 = copy.copy(a_list1) # print "" # print l1 # print l2 # print len( l1 ) # print len( l2 ) # print l1[0] # print l2[0] # print l1[1] # print l2[1] # print l1[2] # print l2[2] # print l1.get_head() # print l2.get_head() # print l1.get_tail() # # NOTE: it appears that the tail is different!!! # print l2.get_tail() self.assertEqual(a_list1, a_list2)
def test_singly_linked_list_element_insert_before_first_one(self): """ Testing inserting a linked list element into a linked list. In this test the linked list contains a single element prior to the insertion of the second element and the new element is inserted before the first element. Before inserting: list = [b] After inserting: list = [a] -> [b] """ a_list = singly_linked_list.SinglyLinkedList() elem1 = singly_linked_list.SinglyLinkedListElement(a_list, 'b', None) elemx = singly_linked_list.SinglyLinkedListElement(a_list, 'a', elem1) a_list.append(elem1.get_data()) elem1.insert_before('a') res = [] res.append(a_list[0]) res.append(a_list[1]) ref = [] ref.append(elemx) ref.append(elem1) t_1 = a_list.get_head() == a_list[0] t_2 = a_list.get_tail() == a_list[1] t_3 = a_list[0].get_next() == a_list[1] t_4 = a_list[1].get_next() == None t_5 = len(a_list) == 2 t_6 = ref == res test = t_1 and t_2 and t_3 and t_4 and t_5 and t_6 self.assertTrue(test)
def test_singly_linked_list_element_get_data(self): """ Test method (list element) "get_data". """ a_list = singly_linked_list.SinglyLinkedList() elem = singly_linked_list.SinglyLinkedListElement(a_list, 'a', None) self.assertEqual('a', elem.get_data())
def test_singly_linked_list_clear(self): """ Test method "clear". """ a_list = singly_linked_list.SinglyLinkedList() a_list.append('a') a_list.clear() self.assertTrue(a_list.is_empty())
def test_singly_linked_list_element_copy_equal(self): """ Test operator (list element) "copy". """ a_list = singly_linked_list.SinglyLinkedList() elem = singly_linked_list.SinglyLinkedListElement(a_list, 'a', None) e_copy = copy.copy(elem) self.assertEqual(elem, e_copy)
def test_singly_linked_list_element_not_equal(self): """ Test operator (list element) "equal" - inverted. """ a_list = singly_linked_list.SinglyLinkedList() elem1 = singly_linked_list.SinglyLinkedListElement(a_list, 'a', None) elem2 = singly_linked_list.SinglyLinkedListElement(a_list, 'b', None) self.assertNotEqual(elem1, elem2)
def test_singly_linked_list_element_get_next(self): """ Test method (list element) "get_next". """ a_list = singly_linked_list.SinglyLinkedList() elem2 = singly_linked_list.SinglyLinkedListElement(a_list, 'b', None) elem1 = singly_linked_list.SinglyLinkedListElement(a_list, 'a', elem2) self.assertEqual(elem2, elem1.get_next())
def test_singly_linked_list_element_copy_not_equal(self): """ Test operator (list element) "copy" - inverted. """ a_list = singly_linked_list.SinglyLinkedList() elem = singly_linked_list.SinglyLinkedListElement(a_list, 'a', None) e_copy = copy.copy(elem) elem.data = 'aa' self.assertNotEqual(elem, e_copy)
def test_singly_linked_list_insert_after_element(self): """ Test method "insert_after_element". """ a_list = singly_linked_list.SinglyLinkedList() a_list.append('a') a_list.append('b') a_list.append('c') elem2 = a_list[2] a_list.insert_after_element('cc', elem2) self.assertEqual('cc', a_list[3].get_data())
def test_singly_linked_list_copy_not_equal(self): """ Test operator "copy" - inverted. """ a_list1 = singly_linked_list.SinglyLinkedList() a_list1.append('a') a_list1.append('b') a_list1.append('c') a_list2 = copy.copy(a_list1) a_list1[len(a_list1) - 1] = 'cc' self.assertNotEqual(a_list1, a_list2)
def test_singly_linked_list_insert_at_middle(self): """ Test method "insert_at". """ a_list = singly_linked_list.SinglyLinkedList() a_list.append('a') a_list.append('b') a_list.append('c') a_list.append('d') a_list.append('e') a_list.insert_at(2, 'cc') self.assertEqual('cc', a_list[2].get_data())
def test_singly_linked_list_insert_at_head(self): """ Test method "insert_at". """ a_list = singly_linked_list.SinglyLinkedList() a_list.append('a') a_list.append('b') a_list.append('c') a_list.insert_at(0, 'aa') t_1 = 'aa' == a_list.get_head().data t_2 = a_list[0] == a_list.get_head() t_3 = a_list[len(a_list) - 1] == a_list.get_tail() test = t_1 and t_2 and t_3 self.assertTrue(test)
def test_singly_linked_list_element_remove_end(self): """ Testing removing a linked list element from a linked list. In this test the linked list contains five elements prior to removing. The element being removed is the last element. Before removing: list = [a] -> [b] -> [c] -> [d] -> [e] After removing: list = [a] -> [b] -> [c] -> [d] """ a_list = singly_linked_list.SinglyLinkedList() elem4 = singly_linked_list.SinglyLinkedListElement(a_list, 'e', None) elem3 = singly_linked_list.SinglyLinkedListElement(a_list, 'd', elem4) elem2 = singly_linked_list.SinglyLinkedListElement(a_list, 'c', elem3) elem1 = singly_linked_list.SinglyLinkedListElement(a_list, 'b', elem2) elem0 = singly_linked_list.SinglyLinkedListElement(a_list, 'a', elem1) a_list.append(elem0.get_data()) a_list.append(elem1.get_data()) a_list.append(elem2.get_data()) a_list.append(elem3.get_data()) a_list.append(elem4.get_data()) elem4.remove() res = [] res.append(a_list[0]) res.append(a_list[1]) res.append(a_list[2]) res.append(a_list[3]) ref = [] ref.append(elem0) ref.append(elem1) ref.append(elem2) ref.append(elem3) t_1 = a_list.get_head() == a_list[0] t_2 = a_list.get_tail() == a_list[3] t_3 = a_list[0].get_next() == a_list[1] t_4 = a_list[1].get_next() == a_list[2] t_5 = a_list[2].get_next() == a_list[3] t_6 = a_list[3].get_next() == None t_7 = len(a_list) == 4 t_8 = ref == res test = t_1 and t_2 and t_3 and t_4 and t_5 and t_6 and t_7 and t_8 self.assertTrue(test)
def test_singly_linked_list_element_insert_empty(self): """ Test method (list element) "insert". """ a_list = singly_linked_list.SinglyLinkedList() elem1 = singly_linked_list.SinglyLinkedListElement(a_list, 'a', None) elem1.insert('b') test1 = a_list.get_head() == a_list[0] test2 = a_list.get_tail() == a_list[0] test3 = a_list[0].get_next() == None test4 = len(a_list) == 1 test = test1 and test2 and test3 and test4 self.assertTrue(test)
def test_singly_linked_list_element_insert_before_middle(self): """ Testing inserting a linked list element into a linked list. In this test the linked list contains three elements prior to the insertion of the fourh element and the new element is inserted before the third element. Before inserting: list = [a] -> [b] -> [d] After inserting: list = [a] -> [b] -> [c] -> [d] """ a_list = singly_linked_list.SinglyLinkedList() elem2 = singly_linked_list.SinglyLinkedListElement(a_list, 'd', None) elemx = singly_linked_list.SinglyLinkedListElement(a_list, 'c', elem2) elem1 = singly_linked_list.SinglyLinkedListElement(a_list, 'b', elemx) elem0 = singly_linked_list.SinglyLinkedListElement(a_list, 'a', elem1) a_list.append(elem0.get_data()) a_list.append(elem1.get_data()) a_list.append(elem2.get_data()) elem2.insert_before('c') res = [] res.append(a_list[0]) res.append(a_list[1]) res.append(a_list[2]) res.append(a_list[3]) ref = [] ref.append(elem0) ref.append(elem1) ref.append(elemx) ref.append(elem2) t_1 = a_list.get_head() == a_list[0] t_2 = a_list.get_tail() == a_list[3] t_3 = a_list[0].get_next() == a_list[1] t_4 = a_list[1].get_next() == a_list[2] t_5 = a_list[2].get_next() == a_list[3] t_6 = a_list[3].get_next() == None t_7 = len(a_list) == 4 t_8 = ref == res test = t_1 and t_2 and t_3 and t_4 and t_5 and t_6 and t_7 and t_8 self.assertTrue(test)
def test_singly_linked_list_element_insert_middle(self): """ Test method (list element) "insert". """ a_list = singly_linked_list.SinglyLinkedList() elem5 = singly_linked_list.SinglyLinkedListElement(a_list, 'e', None) elem4 = singly_linked_list.SinglyLinkedListElement(a_list, 'd', elem5) elemx = singly_linked_list.SinglyLinkedListElement(a_list, 'cc', elem4) elem3 = singly_linked_list.SinglyLinkedListElement(a_list, 'c', elem4) elem2 = singly_linked_list.SinglyLinkedListElement(a_list, 'b', elem3) elem1 = singly_linked_list.SinglyLinkedListElement(a_list, 'a', elem2) a_list.append(elem1.get_data()) a_list.append(elem2.get_data()) a_list.append(elem3.get_data()) a_list.append(elem4.get_data()) a_list.append(elem5.get_data()) elem3.insert('cc') res = [] res.append(a_list[0]) res.append(a_list[1]) res.append(a_list[2]) res.append(a_list[3]) res.append(a_list[4]) ref = [] ref.append(elem1) ref.append(elem2) ref.append(elemx) ref.append(elem4) ref.append(elem5) t_1 = a_list.get_head() == a_list[0] t_2 = a_list.get_tail() == a_list[4] t_3 = a_list[0].get_next() == a_list[1] t_4 = a_list[1].get_next() == a_list[2] t_5 = a_list[2].get_next() == a_list[3] t_6 = a_list[3].get_next() == a_list[4] t_7 = a_list[4].get_next() == None t_8 = len(a_list) == 5 t_9 = ref == res test = t_1 and t_2 and t_3 and t_4 and t_5 and t_6 and t_7 and t_8 and t_9 self.assertTrue(test)
def test_singly_linked_list_element_insert_head(self): """ Test method (list element) "insert". """ a_list = singly_linked_list.SinglyLinkedList() elem2 = singly_linked_list.SinglyLinkedListElement(a_list, 'c', None) elem1 = singly_linked_list.SinglyLinkedListElement(a_list, 'b', elem2) elem0 = singly_linked_list.SinglyLinkedListElement(a_list, 'a', elem1) a_list.append(elem0.get_data()) a_list.append(elem1.get_data()) a_list.append(elem2.get_data()) elem0.insert('aa') test1 = a_list.get_head() == a_list[0] test2 = a_list.get_tail() == a_list[len(a_list) - 1] test3 = a_list[0].get_next() == elem1 test4 = len(a_list) == 3 test = test1 and test2 and test3 and test4 self.assertTrue(test)
def test_singly_linked_list_element_insert_after_first_two(self): """ Testing inserting a linked list element into a linked list. In this test the linked list contains two elements prior to the insertion of the third element and the new element is inserted after the second element. Before inserting: list = [a] -> [b] After inserting: list = [a] -> [b] -> [c] """ a_list = singly_linked_list.SinglyLinkedList() elemx = singly_linked_list.SinglyLinkedListElement(a_list, 'c', None) elem1 = singly_linked_list.SinglyLinkedListElement(a_list, 'b', elemx) elem0 = singly_linked_list.SinglyLinkedListElement(a_list, 'a', elem1) a_list.append(elem0.get_data()) a_list.append(elem1.get_data()) elem1.insert_after('c') res = [] res.append(a_list[0]) res.append(a_list[1]) res.append(a_list[2]) ref = [] ref.append(elem0) ref.append(elem1) ref.append(elemx) t_1 = a_list.get_head() == a_list[0] t_2 = a_list.get_tail() == a_list[2] t_3 = a_list[0].get_next() == a_list[1] t_4 = a_list[1].get_next() == a_list[2] t_5 = a_list[2].get_next() == None t_6 = len(a_list) == 3 t_7 = ref == res test = t_1 and t_2 and t_3 and t_4 and t_5 and t_6 and t_7 self.assertTrue(test)
def test_singly_linked_list_element_remove_not_present(self): """ Testing removing a linked list element from a linked list. In this test the linked list contains two elements prior to removing. The element which should be removed is not in the list, so nothing should be removed and the pointers should be intact. Before removing: list = [a] -> [b] After removing: list = [a] -> [b] """ a_list = singly_linked_list.SinglyLinkedList() elem2 = singly_linked_list.SinglyLinkedListElement(a_list, 'c', None) elem1 = singly_linked_list.SinglyLinkedListElement(a_list, 'b', elem2) elem0 = singly_linked_list.SinglyLinkedListElement(a_list, 'a', elem1) a_list.append(elem0.get_data()) a_list.append(elem1.get_data()) elem2.remove() res = [] res.append(a_list[0]) res.append(a_list[1]) ref = [] ref.append(elem0) ref.append(elem1) t_1 = a_list.get_head() == a_list[0] t_2 = a_list.get_tail() == a_list[1] t_3 = a_list[0].get_next() == a_list[1] t_4 = a_list[1].get_next() == None t_5 = len(a_list) == 2 t_6 = ref == res test = t_1 and t_2 and t_3 and t_4 and t_5 and t_6 self.assertTrue(test)
def test_singly_linked_list_element_remove_first_one(self): """ Testing removing a linked list element from a linked list. In this test the linked list contains a single element prior to removing this element. Before removing: list = [a] After removing: list = [None] """ a_list = singly_linked_list.SinglyLinkedList() elem0 = singly_linked_list.SinglyLinkedListElement(a_list, 'a', None) a_list.append(elem0.get_data()) elem0.remove() res = [] ref = [] t_1 = len(a_list) == 0 t_2 = ref == res test = t_1 and t_2 self.assertTrue(test)
def test_array_list_remove_at_2(self): """ Test of remove at methhod (association). """ array = array_list.ArrayList(5) s_list1 = singly_linked_list.SinglyLinkedList() e11 = association.Association("e11", 11) e12 = association.Association("e12", 12) e13 = association.Association("e13", 13) s_list1.append(e11) s_list1.append(e12) s_list1.append(e13) s_list2 = singly_linked_list.SinglyLinkedList() e21 = association.Association("e21", 21) e22 = association.Association("e22", 22) e23 = association.Association("e23", 23) s_list2.append(e21) s_list2.append(e22) s_list2.append(e23) s_list3 = singly_linked_list.SinglyLinkedList() e31 = association.Association("e31", 31) e32 = association.Association("e32", 32) e33 = association.Association("e33", 33) s_list3.append(e31) s_list3.append(e32) s_list3.append(e33) s_list4 = singly_linked_list.SinglyLinkedList() e41 = association.Association("e41", 41) e42 = association.Association("e42", 42) e43 = association.Association("e43", 43) s_list4.append(e41) s_list4.append(e42) s_list4.append(e43) s_list5 = singly_linked_list.SinglyLinkedList() e51 = association.Association("e51", 51) e52 = association.Association("e52", 52) e53 = association.Association("e53", 53) s_list5.append(e51) s_list5.append(e52) s_list5.append(e53) array[0] = s_list1 array[1] = s_list2 array[2] = s_list3 array[3] = s_list4 array[4] = s_list5 array[0].remove(e11) array[0].remove(e12) array[0].remove(e13) del array[0] ref = array_list.ArrayList(4) ref[0] = s_list2 ref[1] = s_list3 ref[2] = s_list4 ref[3] = s_list5 self.assertEqual(ref, array)
def setUp(self): self.list1 = singly_linked_list.SinglyLinkedList() self.list1.append('b') self.list1.append('c') self.list1.append('d')
def test_singly_linked_list_is_empty(self): """ Test method "is_empty". """ a_list = singly_linked_list.SinglyLinkedList() self.assertTrue(a_list.is_empty())