def test_insert_after(self): ll = pyskip.LinkedList() hello = pyskip.SingleNode(value='Hello') world = pyskip.SingleNode(value='world') there = pyskip.SingleNode(value='there') the_end = pyskip.SingleNode(value='the end') ll.insert_first(hello) self.assertEqual(ll.head.value, 'Hello') self.assertEqual(ll.head.next, None) self.assertEqual(len(ll), 1) ll.insert_after(hello, world) self.assertEqual(ll.head.value, 'Hello') self.assertEqual(ll.head.next, world) self.assertEqual(len(ll), 2) ll.insert_after(hello, there) self.assertEqual(ll.head.value, 'Hello') self.assertEqual(ll.head.next, there) self.assertEqual(ll.head.next.next, world) self.assertEqual(len(ll), 3) ll.insert_after(world, the_end) self.assertEqual(ll.head.value, 'Hello') self.assertEqual(ll.head.next, there) self.assertEqual(ll.head.next.next, world) self.assertEqual(ll.head.next.next.next, the_end) self.assertEqual(len(ll), 4)
def test_init(self): n1 = pyskip.SingleNode() self.assertEqual(n1.value, None) self.assertEqual(n1.next, None) n2 = pyskip.SingleNode(value='whatever') self.assertEqual(n2.value, 'whatever') self.assertEqual(n2.next, None) n3 = pyskip.SingleNode(value='another', next=n2) self.assertEqual(n3.value, 'another') self.assertEqual(n3.next, n2)
def setUp(self): super(LinkedListTestCase, self).setUp() self.ll = pyskip.LinkedList() self.head = pyskip.SingleNode(value=0) self.first = pyskip.SingleNode(value=2) self.second = pyskip.SingleNode(value=5) self.third = pyskip.SingleNode(value=6) self.ll.insert_first(self.head) self.ll.insert_after(self.head, self.first) self.ll.insert_after(self.first, self.second) self.ll.insert_after(self.second, self.third)
def load_sorted_list(): the_list = pyskip.SortedLinkedList() for i in range(MAX_LOAD): the_list.insert(pyskip.SingleNode(value=i)) return the_list
def test_insert_first(self): ll = pyskip.LinkedList() self.assertEqual(ll.head, None) self.assertEqual(len(ll), 0) world = pyskip.SingleNode(value='world') ll.insert_first(world) self.assertEqual(ll.head.value, 'world') self.assertEqual(ll.head.next, None) self.assertEqual(len(ll), 1) hello = pyskip.SingleNode(value='Hello') ll.insert_first(hello) self.assertEqual(ll.head.value, 'Hello') self.assertEqual(ll.head.next, world) self.assertEqual(len(ll), 2)
def test_remove(self): self.assertEqual(len(self.sll), 6) self.assertTrue(self.sll.remove(pyskip.SingleNode(value=3))) self.assertEqual(len(self.sll), 5) self.assertTrue(self.sll.remove(pyskip.SingleNode(value=6))) self.assertEqual(len(self.sll), 4) # Remove the first ``2``. self.assertTrue(self.sll.remove(pyskip.SingleNode(value=2))) self.assertEqual(len(self.sll), 3) # Remove the second ``2``. self.assertTrue(self.sll.remove(pyskip.SingleNode(value=2))) self.assertEqual(len(self.sll), 2) # Nope, there are no more ``2``s there. self.assertFalse(self.sll.remove(pyskip.SingleNode(value=2))) self.assertEqual(len(self.sll), 2) the_list = iter(self.sll) # Should come out in the correct order. self.assertEqual(next(the_list).value, 0) self.assertEqual(next(the_list).value, 5)
def setUp(self): super(SortedLinkedListTestCase, self).setUp() self.sll = pyskip.SortedLinkedList() self.sll.insert(pyskip.SingleNode(value=5)) self.sll.insert(pyskip.SingleNode(value=6)) self.sll.insert(pyskip.SingleNode(value=2)) self.sll.insert(pyskip.SingleNode(value=0)) self.sll.insert(pyskip.SingleNode(value=3)) self.sll.insert(pyskip.SingleNode(value=2))
def inserts_sorted_list(the_list, randoms): for rand in randoms: the_list.insert(pyskip.SingleNode(value=rand))