def testReturnLastNode(self): ll = CircularLinkedList() self.assertEqual(ll.return_last_node(), None) ll.append(1) self.assertEqual(ll.return_last_node().data, 1) ll.append(2) self.assertEqual(ll.return_last_node().data, 2)
def test_len(self): llist = CircularLinkedList() self.assertEqual(len(llist), 0) llist.from_list([1, 2, 3]) self.assertEqual(len(llist), 3) llist.append(9) self.assertEqual(len(llist), 4)
def testLength(self): ll = CircularLinkedList() self.assertEqual(ll.length(), 0) ll.append(1) ll.append(2) ll.append(3) self.assertEqual(ll.length(), 3)
def testAppend(self): ll = CircularLinkedList() ll.append(1) ll.append(2) self.assertEqual(ll.head.data, 1) self.assertIsNotNone(ll.head.next) self.assertEqual(ll.head.next.data, 2) self.assertEqual(ll.head, ll.head.next.next)
def testDetectLoop(self): ll = CircularLinkedList() self.assertFalse(ll.detect_loop()) ll.append(1) self.assertTrue(ll.detect_loop()) ll.append(2) ll.append(3) self.assertTrue(ll.detect_loop())
def testDelete(self): ll = CircularLinkedList() ll.append(1) ll.append(2) ll.delete(2) self.assertEqual(ll.head.data, 1) self.assertEqual(ll.head, ll.head.next) ll.delete(1) self.assertIsNone(ll.head)
def testCopyList(self): ll1 = CircularLinkedList() ll2 = ll1.copy_list() self.assertIsNone(ll2.head) ll1.append(1) ll1.append(2) ll2 = ll1.copy_list() self.assertNotEqual(ll1.head, ll2.head) # Make sure, it's a deep copy self.assertEqual(ll1.head.data, ll2.head.data) self.assertEqual(ll1.head.next.data, ll2.head.next.data) self.assertEqual(ll2.head.next.next, ll2.head)
def testRemoveNthElement(self): ll = CircularLinkedList() ll.append(1) ll.append(2) ll.append(3) self.assertEqual(ll.head.data, 1) ll.remove_nth_element(0) self.assertEqual(ll.head.data, 2) ll.remove_nth_element(1) self.assertEqual(ll.head.data, 2) self.assertEqual(ll.head, ll.head.next)
def josephus_permutation(n): l = CircularLinkedList() for i in range(1, n + 1): l.append(i) cur = l.head while cur.next != cur: l.delete_node(cur.next) cur = cur.next return cur.val
def testSearch(self): ll = CircularLinkedList() ll.append(1) ll.append(2) self.assertTrue(ll.search(1)) self.assertFalse(ll.search(100))
def test_append_start_empty(self): linkedlist = CircularLinkedList() for i in range(10): linkedlist.append(i)
def test_append_start_filled(self): linkedlist = CircularLinkedList([1, 2, 3]) for i in range(10): linkedlist.append(i)
def test_iter(self): llist = CircularLinkedList() llist.append('A') llist.append('B') llist.print_list() self.assertEqual(llist.to_list(), ['A', 'B'])