def test_prepend(self): linked_list = DoublyLinkedList() linked_list.add(1) linked_list.prepend(20) self.assertEqual(linked_list.head.value, 20) self.assertEqual(linked_list.tail.value, 1) self.assertEqual(linked_list.tail.previous, linked_list.head)
def test_add_one_item(self): linked_list = DoublyLinkedList() linked_list.add(1) self.assertEqual(linked_list.head.value, 1) self.assertEqual(linked_list.head.previous, None) self.assertEqual(linked_list.tail.value, 1) self.assertEqual(linked_list.tail.previous, None)
def test_add_two_items(self): linked_list = DoublyLinkedList() linked_list.add(1) linked_list.add(22) self.assertEqual(linked_list.head.next.value, 22) self.assertEqual(linked_list.head.next, linked_list.tail) self.assertEqual(linked_list.head.previous, None) self.assertEqual(linked_list.tail.previous, linked_list.head)
def test_walk_reverse(self): linked_list = DoublyLinkedList() linked_list.add(2) linked_list.add(4) linked_list.add(25) linked_list.add(20) linked_list.add(12) self.assertEqual(linked_list.walk_reverse(), [12, 20, 25, 4, 2])
def test_walk(self): linked_list = DoublyLinkedList() linked_list.add(2) linked_list.add(4) linked_list.add(25) linked_list.add(20) linked_list.add(12) self.assertEqual(linked_list.walk(), [2, 4, 25, 20, 12])
def test_search_item_at_tail(self): linked_list = DoublyLinkedList() linked_list.add(2) linked_list.add(4) linked_list.add(25) linked_list.add(20) linked_list.add(12) self.assertEqual(linked_list.search(12), True)
def test_remove_item_not_in_list(self): linked_list = DoublyLinkedList() linked_list.add(2) linked_list.add(4) linked_list.add(25) linked_list.add(20) linked_list.add(12) self.assertEqual(linked_list.remove(0), False) self.assertEqual(linked_list.walk(), [2, 4, 25, 20, 12])
def test_search_item_not_in_list(self): linked_list = DoublyLinkedList() linked_list.add(2) linked_list.add(4) linked_list.add(25) linked_list.add(52) linked_list.add(20) linked_list.add(12) self.assertEqual(linked_list.search(0), False)
def test_remove_from_middle(self): linked_list = DoublyLinkedList() linked_list.add(2) linked_list.add(4) linked_list.add(25) linked_list.add(20) linked_list.add(12) self.assertEqual(linked_list.remove(25), True) self.assertEqual(linked_list.head.next.next.previous.value, 4) self.assertEqual(linked_list.walk(), [2, 4, 20, 12])
def test_remove_from_tail(self): linked_list = DoublyLinkedList() linked_list.add(2) linked_list.add(4) linked_list.add(25) linked_list.add(20) linked_list.add(12) self.assertEqual(linked_list.remove(12), True) self.assertEqual(linked_list.tail.next, None) self.assertEqual(linked_list.walk(), [2, 4, 25, 20])
def test_remove_from_head(self): linked_list = DoublyLinkedList() linked_list.add(2) linked_list.add(4) linked_list.add(25) linked_list.add(20) linked_list.add(12) self.assertEqual(linked_list.remove(2), True) self.assertEqual(linked_list.head.previous, None) self.assertEqual(linked_list.walk(), [4, 25, 20, 12])
from doubly_linked_list.doubly_linked_list import DoublyLinkedList linked_list = DoublyLinkedList() linked_list.add(2) linked_list.add(4) linked_list.add(25) linked_list.add(20) linked_list.add(12) print(linked_list.walk()) linked_list.remove(20) print(linked_list.walk()) print(linked_list.head.next.value, linked_list.tail.previous.value)