def test_singly_linked_list_length(self): test_sll = SinglyLinkedList() test_sll.append('x') test_sll.append('y') self.assertEqual(2, test_sll.get_length()) self.assertEqual(2, test_sll.get_length(test_sll.head)) self.assertEqual(1, test_sll.get_length(test_sll.head.next))
def test_insert_after(self): print('Test: test_insert_after') linked_list = SinglyLinkedList() print('Test: None input') self.assertRaises(ValueError, linked_list.insert_after, None, None) self.assertRaises(TypeError, linked_list.insert_after, 1, None) print('Test: insert_after general case') linked_list.append(1) linked_list.insert_after(2, linked_list.first()) self.assertEqual(linked_list.first().next.data, 2) print('Success: test_insert_after')
def test_singly_linked_list_push(self): test_sll = SinglyLinkedList() test_values = (1, 2) test_sll.push(test_values[0]) self.assertEqual(test_values[0], test_sll.head.value) test_sll.push(test_values[1]) self.assertEqual(test_values[1], test_sll.head.value)
def test_append(self): print('Test: test_append') linked_list = SinglyLinkedList() print('Test: None input') self.assertRaises(ValueError, linked_list.append, None) print('Test: empty list') linked_list.append(0) self.assertEqual(linked_list.first().data, 0) print('Test: general case') linked_list = SinglyLinkedList() for item in range(5): linked_list.append(item) for node, item in zip(linked_list, range(5)): self.assertEqual(node.data, item) print('Success: test_append')
def test_insert_to_front(self): print('Test: test_insert_to_front') linked_list = SinglyLinkedList() print('Test: None input') self.assertRaises(ValueError, linked_list.insert_to_front, None) print('Test: empty list') linked_list.insert_to_front(1) self.assertEqual(linked_list.first().data, 1) print('Test: general case') linked_list = SinglyLinkedList() for item in range(3): linked_list.insert_to_front(item) for node, item in zip(linked_list, reversed(range(3))): self.assertEqual(node.data, item) print('Success: test_insert_to_front')
def test_singly_linked_list_cycled(self): test_sll = SinglyLinkedList() test_sll.append('x') test_sll.append('y') self.assertFalse(test_sll.is_linked_list_cycled()) """make a cycled list""" test_sll.head.next.next = test_sll.head self.assertTrue(test_sll.is_linked_list_cycled())
def test_singly_linked_list_reversal(self): test_sll = SinglyLinkedList() test_sll.append('x') test_sll.append('y') self.assertEqual('x', test_sll.head.value) self.assertEqual('y', test_sll.head.next.value) test_sll.reverse() self.assertEqual('y', test_sll.head.value) self.assertEqual('x', test_sll.head.next.value)
def test_singly_linked_list_append(self): test_sll = SinglyLinkedList() test_values = (1, 2) test_sll.append(test_values[0]) self.assertEqual(test_sll.head.value, test_values[0]) self.assertEqual(test_sll.head.next, None) test_sll.append(test_values[1]) self.assertEqual(test_values[0], test_sll.head.value) self.assertEqual(test_values[1], test_sll.head.next.value)
def test_is_empty(self): print('Test: is_empty') linked_list = SinglyLinkedList() self.assertEqual(linked_list.is_empty(), True) for item in range(5): linked_list.append(item) self.assertEqual(linked_list.is_empty(), False) print('Success: test_is_empty')
def test_delete(self): print('Test: test_delete') linked_list = SinglyLinkedList() print('Test: None input') self.assertRaises(ValueError, linked_list.delete, None) print('Test: delete on an empty list') self.assertRaises(ValueError, linked_list.delete, 5) print('Test: delete general case') for item in range(3): linked_list.append(item) linked_list.delete(1) self.assertRaises(ValueError, linked_list.find, 1) self.assertEqual(linked_list.first().next.data, 2) print('Test: delete with no matches') self.assertRaises(ValueError, linked_list.delete, 5) print('Success: test_delete')
def test_find(self): print('Test: test_find') linked_list = SinglyLinkedList() print('Test: find on an empty list') self.assertRaises(ValueError, linked_list.find, 5) print('Test: find the last element') for item in range(5): linked_list.append(item) self.assertEqual(linked_list.find(None).data, 4) print('Test: find with no matches') self.assertRaises(ValueError, linked_list.find, 5) print('Test: find general case') self.assertEqual(linked_list.find(2).data, 2) print('Success: test_find')
def test_singly_linked_list_values_to_list(self): test_sll = SinglyLinkedList() test_sll.append('x') test_sll.append('y') self.assertTupleEqual(('x', 'y'), tuple(test_sll.values_to_list()))
def test_singly_linked_list_init(self): test_sll = SinglyLinkedList() self.assertEqual(SinglyLinkedList, type(test_sll)) self.assertEqual(test_sll.head, None) with self.assertRaises(TypeError): SinglyLinkedList('abc')