class TestLinkedList(TestCase): def setUp(self): self.ll = LinkedList() def tearDown(self): pass def test_add_len(self): self.ll.add("one") self.ll.add("two") self.ll.add("three") self.assertEqual(self.ll.head.data, "three", "Last node added is 3") self.assertEqual(len(self.ll), 3, "length of linked list should be 3") def test_get(self): self.ll.add("one") self.ll.add("two") self.ll.add("three") self.assertEqual(self.ll[1], "two", "getter of 1st index should return two") self.assertEqual(self.ll[0], "three", "getter of 0th index should return three") def test_set(self): self.ll[0] = "apple" self.assertEqual(self.ll[0], "apple", "0th index should contain apple") self.ll.add("banana") self.ll.add("pear") self.ll[1] = "mango" self.assertEqual(self.ll[1], "mango", "1st index should contain mango") def test_remove(self): self.ll.add("apple") self.ll.add("banana") self.ll.add("pear") self.assertEqual(len(self.ll), 3, "linked list should initially have length of 3") self.ll.remove("banana") self.assertEqual(self.ll[1], "apple", "1st index of linked list should be apple") self.assertEqual(self.ll[0], "pear", "0th index of linked list should be pear") self.assertEqual(len(self.ll), 2, "new length of linked list should be 2") def test_find(self): self.ll.add("apple") self.ll.add("banana") self.ll.add("pear") self.assertEqual(self.ll.find("banana"), 1, "banana should be located at 1st index") self.assertEqual(self.ll.find("apple"), 2, "apple should be located at 2nd index")
def test_find(self): linked_list = LinkedList() linked_list.append(data=0) linked_list.append(data=1) linked_list.append(data=2) linked_list.append(data=3) linked_list.append(data=None) self.assertEqual(linked_list.find(1), linked_list.head.next) self.assertEqual(linked_list.find(None), linked_list.tail) self.assertEqual(linked_list.find(3), linked_list.tail.previous)
def test_find_unsuccessful(self): linked_list = LinkedList() linked_list.append(data=0) linked_list.append(data=1) linked_list.append(data=2) linked_list.append(data=3) self.assertEqual(linked_list.find(9), -1)
def test_removing_tail(): ll = LinkedList() ll.insert(1) ll.insert(2) ll.insert(3) ll.remove(3) assert len(ll) == 2 assert ll.tail.value == 2 assert not ll.tail.next assert not ll.head.next.next assert not ll.find(3)
def test_removing_head(): ll = LinkedList() ll.insert(1) ll.insert(2) ll.insert(3) ll.remove(1) assert len(ll) == 2 assert ll.head.value == 2 assert ll.head.next.value == 3 assert not ll.head.next.next assert not ll.find(1)
def test_works(): ll = LinkedList() elements = [random.randrange(-1000000, 1000000) for _ in range(1000)] for elem in elements: ll.insert(elem) assert len(elements) == len(ll) while elements: elem_to_delete = elements.pop() assert bool(ll.find(elem_to_delete)) ll.remove(elem_to_delete) assert len(elements) == len(ll) assert not elements assert ll.empty()
class TestLinkedList(TestCase): faker = Faker() list: LinkedList num_nodes = 5 def setUp(self): nodes = [Node(x) for x in range(0, self.num_nodes)] self.list = LinkedList() for node in nodes: self.list.add(node) def test_add_node(self): random_node = Node(self.faker.sentence()) self.list.add(random_node) last_node = self.list.find_last() self.assertEqual( last_node, random_node, f'Last node: {last_node} does not equal {random_node}.' ) def test_add_first_node(self): random_node = Node(self.faker.sentence()) self.list.add_first(random_node) self.assertEqual( self.list.head, random_node, f'Head node of list: {self.list.head} does not equal {random_node}' ) def test_traverse(self): counter = 0 for node in self.list.traverse(): self.assertEqual( node.data, counter, f'Node data {node.data} does not equal {counter}.' ) counter += 1 def test_add_after(self): random_index = random.randint(0, self.num_nodes - 1) node = self.list.find(Node(random_index)) random_node = Node(random.randint(self.num_nodes + 1, 100)) self.list.add_after(node, random_node) self.assertEqual( node.next, random_node, f'Next node of {node} does not equal {random_node}' ) def test_add_before(self): random_index = random.randint(0, self.num_nodes - 1) node = self.list.find(Node(random_index)) random_node = Node(random.randint(self.num_nodes + 1, 100)) self.list.add_before(node, random_node) self.assertEqual( random_node.next.data, node.data, f'Next node of {random_node}: {random_node.next} ' f'does not equal {node}' ) def test_find(self): random_index = random.randint(0, self.num_nodes - 1) random_node_to_find = Node(random_index) found_node = self.list.find(random_node_to_find) self.assertEqual( random_node_to_find.data, found_node.data, f'Found Node {found_node.data} is not equal ' f'to {random_node_to_find.data}' ) def test_find_non_existent(self): random_non_existent_index = random.randint(self.num_nodes + 1, 1000) res_should_be_none = self.list.find(Node(random_non_existent_index)) self.assertIsNone( res_should_be_none, f'Result: {res_should_be_none} is not None as expected.' ) def test_find_last(self): last_node = Node(self.num_nodes - 1) found_node = self.list.find_last() self.assertEqual( last_node.data, found_node.data, f'Last node {found_node} not equal to expected {last_node}' ) def test_find_before(self): random_index_not_head = random.randint(1, self.num_nodes - 1) random_node = Node(random_index_not_head) node_before = self.list.find_before(random_node) self.assertEqual( node_before.data, random_index_not_head - 1, f'Node before {node_before.data} is not equal ' f'to {random_index_not_head - 1}' ) def test_delete(self): random_node = Node(random.randint(0, self.num_nodes - 1)) self.list.delete(random_node) should_be_none = self.list.find(random_node) self.assertIsNone( should_be_none, f'{should_be_none} is not None as expected.' )
def test_find_empty_list(self): linked_list = LinkedList() self.assertEqual(linked_list.find(9), -1)