def test_values_exist_in_the_linked_list(): node1 = Node(1) node2 = Node(2) node3 = Node(3) node4 = Node(4) linked_list = LinkedList() linked_list.head = node1 node1.next = node2 node2.next = node3 node3.next = node4 assert linked_list.__str__() == "1 -> 2 -> 3 -> 4 -> None"
def test_add__node_to_the_end_of_the_linked_list(): node1 = Node(1) node2 = Node(2) node3 = Node(3) node4 = Node(4) linked_list = LinkedList() linked_list.head = node1 node1.next = node2 node2.next = node3 node3.next = node4 linked_list.append(5) assert linked_list.__str__() == "1 -> 2 -> 3 -> 4 -> 5 -> None"
def test_insert_node_after_the_last_node(): node1 = Node(1) node2 = Node(2) node3 = Node(3) node4 = Node(4) linked_list = LinkedList() linked_list.head = node1 node1.next = node2 node2.next = node3 node3.next = node4 linked_list.insert_after(4, 5) assert linked_list.__str__() == "1 -> 2 -> 3 -> 4 -> 5 -> None"
def test_insert_node_before_the_first_node(): node1 = Node(1) node2 = Node(2) node3 = Node(3) node4 = Node(4) linked_list = LinkedList() linked_list.head = node1 node1.next = node2 node2.next = node3 node3.next = node4 linked_list.insert_before(1, 5) assert linked_list.__str__() == "5 -> 1 -> 2 -> 3 -> 4 -> None"
def test_insert_node_before_node_located_in_the_middle_of_linked_list(): node1 = Node(1) node2 = Node(2) node3 = Node(3) node4 = Node(4) linked_list = LinkedList() linked_list.head = node1 node1.next = node2 node2.next = node3 node3.next = node4 linked_list.insert_before(3, 5) assert linked_list.__str__() == "1 -> 2 -> 5 -> 3 -> 4 -> None"
def test_k_is_not_postive_integer(): node1 = Node(1) node2 = Node(2) node3 = Node(3) node4 = Node(4) linked_list = LinkedList() linked_list.head = node1 node1.next = node2 node2.next = node3 node3.next = node4 actual = linked_list.kth(-3) expected = "Input k is not a positive integer" assert actual == expected
def test_k_same_length_as_list(): node1 = Node(1) node2 = Node(2) node3 = Node(3) node4 = Node(4) linked_list = LinkedList() linked_list.head = node1 node1.next = node2 node2.next = node3 node3.next = node4 actual = linked_list.kth(4) expected = None assert actual == expected
def test_kth_greater_than_the_length_of_the_linked_list(): node1 = Node(1) node2 = Node(2) node3 = Node(3) node4 = Node(4) linked_list = LinkedList() linked_list.head = node1 node1.next = node2 node2.next = node3 node3.next = node4 actual = linked_list.kth(10) expected = "Input is greater than the length of the linked list" assert actual == expected
def test_includes(): node1 = Node(1) node2 = Node(2) node3 = Node(3) node4 = Node(4) linked_list = LinkedList() linked_list.head = node1 node1.next = node2 node2.next = node3 node3.next = node4 assert linked_list.includes(1) assert linked_list.includes(2) assert linked_list.includes(3) assert linked_list.includes(4) assert linked_list.includes(5) == False assert linked_list.includes(6) == False
def test_zip_lists_second_one_longer(): node1 = Node(1) node2 = Node(2) node3 = Node(3) node4 = Node(4) node1.next = node2 node2.next = node3 node3.next = node4 linked_list1 = LinkedList() linked_list1.head = node3 linked_list2 = LinkedList() linked_list2.head = node1 linked_list3 = LinkedList() linked_list3 = linked_list3.zip_lists(linked_list1, linked_list2) assert linked_list3.__str__() == "3 -> 1 -> 4 -> 2 -> 3 -> 4 -> None"
def test_llist_init_str_repr(llist): first_node = Node(1) second_node = Node('a') assert str(llist) == 'None' assert repr(llist) == 'LinkedList()' llist._head = first_node first_node.next = second_node llist._tail = second_node assert str(llist) == '1 -> a -> None' assert repr(llist) == 'LinkedList(1, a)' llist = LinkedList([2, 'b']) assert str(llist) == '2 -> b -> None' assert repr(llist) == 'LinkedList(2, b)'