コード例 #1
0
    def test_insert(self):
        """
        test insert()
        """
        # test on empty linked list
        list1 = LinkedList()
        list1.insert(10, 3)
        list_arr = list(list1)
        self.assertEqual(list_arr, [])

        # test on linked list with 1 node
        list1.insert(20, 0)
        list1.insert(30, 3)
        list_arr = list(list1)
        self.assertEqual(list_arr, [20])

        # generate linked list
        nums = 10
        list1, reference_arr = generate_filled_list(LinkedList, nums)

        # insert in the middle
        insert_idx = 3
        insert_val = 100
        list1.insert(insert_val, insert_idx)

        # compare to reference
        list_arr = list(list1)
        reference_arr = reference_arr[:insert_idx] + [insert_val] + reference_arr[insert_idx:]
        self.assertEqual(list_arr, reference_arr)
コード例 #2
0
    def test_insert_beginning(self):
        """
        test insert_beginning()
        """
        # test insert on empty linked list
        list1 = LinkedList()
        key = 10
        idx = 10
        list1.insert(key, idx)
        self.assertEqual(list(list1), [])

        # generate linked list
        list1 = LinkedList()
        list1.insert(10, 0)
        list1.insert(20, 1)
        list1.insert(30, 1)
        reference_arr = [10, 30, 20]

        # test insert
        list_arr = list(list1)
        self.assertEqual(list_arr, reference_arr)