class TestLinkedList: def setup(self): e1 = Element(1) e2 = Element(2) e3 = Element(3) self.ll = LinkedList(e1) self.ll.append(e2) self.ll.append(e3) def test_get_position(self): assert self.ll.head.next.next.value == 3 assert self.ll.get_position(3).value == 3 assert self.ll.get_position(1).value == 1 assert self.ll.get_position(2).value == 2 def test_insert_position(self): e4 = Element(4) e5 = Element(5) self.ll.insert(e4, 3) assert self.ll.get_position(3).value == 4 self.ll.insert(e5, 3) assert self.ll.get_position(3).value == 5 assert self.ll.get_position(4).value == 4 def test_delete_position(self): self.ll.delete(1) assert self.ll.get_position(1).value == 2 assert self.ll.get_position(2).value == 3 assert self.ll.get_position(3).value == 3
def test_delete_unexistent_item(self): linked_list = LinkedList() linked_list.insert("Flash") linked_list.insert("Arrow") with self.assertRaises(ValueError): linked_list.delete("Harrison Wells")
def test_delete_next_and_reassignment(self): linked_list = LinkedList() linked_list.insert("Tony Stark") linked_list.insert("Thor") linked_list.insert("Bruce Banner") linked_list.insert("Clint Barton") linked_list.delete("Clint Barton") linked_list.delete("Thor") self.assertEqual(linked_list.head.get_data(), "Bruce Banner")
def test_delete(self): linked_list = LinkedList() linked_list.insert("Darth Vader") linked_list.insert("R2-D2") linked_list.insert("Luke Skywalker") linked_list.delete("Luke Skywalker") self.assertEqual(linked_list.head.get_data(), "R2-D2") linked_list.delete("R2-D2") self.assertEqual(linked_list.head.get_next(), None)
def test_delete_empty_list(self): linked_list = LinkedList() with self.assertRaises(ValueError): linked_list.delete("Barry Allen")
class TestLinkedList(unittest.TestCase): def setUp(self): self.list = LinkedList() def tearDown(self): self.list = None def test_insert(self): self.list.insert("David") self.assertTrue(self.list.head.get_data() == "David") self.assertTrue(self.list.head.get_next() is None) def test_insert_two(self): self.list.insert("David") self.list.insert("Thomas") self.assertTrue(self.list.head.get_data() == "Thomas") head_next = self.list.head.get_next() self.assertTrue(head_next.get_data() == "David") def test_nextNode(self): self.list.insert("Jacob") self.list.insert("Pallymay") self.list.insert("Rasmus") self.assertTrue(self.list.head.get_data() == "Rasmus") head_next = self.list.head.get_next() self.assertTrue(head_next.get_data() == "Pallymay") last = head_next.get_next() self.assertTrue(last.get_data() == "Jacob") def test_positive_search(self): self.list.insert("Jacob") self.list.insert("Pallymay") self.list.insert("Rasmus") found = self.list.search("Jacob") self.assertTrue(found.get_data() == "Jacob") found = self.list.search("Pallymay") self.assertTrue(found.get_data() == "Pallymay") found = self.list.search("Jacob") self.assertTrue(found.get_data() == "Jacob") def test_searchNone(self): self.list.insert("Jacob") self.list.insert("Pallymay") # make sure reg search works found = self.list.search("Jacob") self.assertTrue(found.get_data() == "Jacob") with self.assertRaises(ValueError): self.list.search("Vincent") def test_delete(self): self.list.insert("Jacob") self.list.insert("Pallymay") self.list.insert("Rasmus") # Delete the list head self.list.delete("Rasmus") self.assertTrue(self.list.head.get_data() == "Pallymay") # Delete the list tail self.list.delete("Jacob") self.assertTrue(self.list.head.get_next() is None) def test_delete_value_not_in_list(self): self.list.insert("Jacob") self.list.insert("Pallymay") self.list.insert("Rasmus") with self.assertRaises(ValueError): self.list.delete("Sunny") def test_delete_empty_list(self): with self.assertRaises(ValueError): self.list.delete("Sunny") def test_delete_next_reassignment(self): self.list.insert("Jacob") self.list.insert("Cid") self.list.insert("Pallymay") self.list.insert("Rasmus") self.list.delete("Pallymay") self.list.delete("Cid") self.assertTrue(self.list.head.next_node.get_data() == "Jacob")