コード例 #1
0
    def test_remove_by_value(self):
        """
        test remove by value
        """
        # test on empty linked list
        list1 = DoublyLinkedList()
        list1.remove_by_value(10)
        self.assertEqual(len(list1), 0)

        # generate linked list
        nums = 10
        list1, reference_arr = generate_filled_list(DoublyLinkedList,
                                                    nums,
                                                    as_str=True)

        # test removing value not present in linked list
        list1.remove_by_value('100')
        list_arr = list(list1)
        self.assertEqual(list_arr, reference_arr)

        # test remove node containing value from middle of linked list
        list1.remove_by_value('3')
        list_arr = list(list1)
        reference_arr = reference_arr[:3] + reference_arr[4:]
        self.assertEqual(list_arr, reference_arr)

        # test list from last to first
        key = get_key_from_last_to_first(list1.fild_last_node())
        self.assertEqual(key, reference_arr[::-1])
コード例 #2
0
    def test_insert(self):
        """
        test insert()
        """
        # test on empty linked list
        list1 = DoublyLinkedList()
        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(DoublyLinkedList, 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)

        # test list from last to first
        key = get_key_from_last_to_first(list1.fild_last_node())
        self.assertEqual(key, reference_arr[::-1])
コード例 #3
0
    def test_remove_by_index(self):
        """
        test remove_by_index()
        """
        # test on empty linked list
        list1 = DoublyLinkedList()
        list1.remove_by_index(10)
        self.assertEqual(len(list1), 0)

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

        # test removing index outside of linked list
        list1.remove_by_index(100)
        list_arr = list(list1)
        self.assertEqual(list_arr, reference_arr)

        # test remove index from middle of linked list
        list1.remove_by_index(3)
        list_arr = list(list1)
        reference_arr = reference_arr[:3] + reference_arr[4:]
        self.assertEqual(list_arr, reference_arr)

        # test list from last to first
        key = get_key_from_last_to_first(list1.fild_last_node())
        self.assertEqual(key, reference_arr[::-1])
コード例 #4
0
    def test_insert_beginning(self):
        """
        test insert_beginning()
        """
        # test insert on empty linked list
        list1 = DoublyLinkedList()
        key = 10
        idx = 10
        list1.insert(key, idx)
        self.assertEqual(list(list1), [])

        # generate linked list
        list1 = DoublyLinkedList()
        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)

        # test list from last to first
        key = get_key_from_last_to_first(list1.fild_last_node())
        self.assertEqual(key, reference_arr[::-1])
コード例 #5
0
    def test_fild_last_node(self):
        """
        test fild_last_node()
        """
        # test search on empty linked list
        list1 = DoublyLinkedList()
        node = list1.fild_last_node()
        self.assertEqual(node, None)

        # generate linked list
        nums = 1
        list1 = generate_filled_list(DoublyLinkedList, nums, with_ref=False)

        # test for finding last node with 1 node
        last_node = list1.fild_last_node()
        self.assertEqual(last_node.key, nums - 1)

        # generate linked list
        nums = 10
        list1 = generate_filled_list(DoublyLinkedList, nums, with_ref=False)

        # test for finding last node
        last_node = list1.fild_last_node()
        self.assertEqual(last_node.key, nums - 1)
コード例 #6
0
    def test_insert_end(self):
        """
        test insert_end()
        """
        # test insert_end on empty linked list
        list1 = DoublyLinkedList()
        key = 10
        list1.insert_end(key)
        self.assertEqual(list(list1), [key])

        # generate linked list using insert_end
        nums = 10
        list1, reference_arr = generate_filled_list(DoublyLinkedList, nums)
        list_arr = list(list1)
        self.assertEqual(list_arr, reference_arr)

        # test list from last to first
        key = get_key_from_last_to_first(list1.fild_last_node())
        self.assertEqual(key, reference_arr[::-1])
コード例 #7
0
    def test_remove_end(self):
        """
        test remove_end()
        """
        # test on empty linked list
        list1 = DoublyLinkedList()
        list1.remove_beginning()
        self.assertEqual(len(list1), 0)

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

        # remove from beginning
        list1.remove_end()
        list_arr = list(list1)

        # compare results to reference
        reference_arr = reference_arr[:-1]
        self.assertEqual(list_arr, reference_arr)

        # test list from last to first
        key = get_key_from_last_to_first(list1.fild_last_node())
        self.assertEqual(key, reference_arr[::-1])