Example #1
0
def test_node_reports_balanced(nodes_to_insert, expected):
    values = iter(nodes_to_insert)
    n = PlainNode(next(values))
    for value in values:
        n.insert(value)

    assert n.balanced == expected
Example #2
0
def test_node_delete_non_existing_value():
    n = PlainNode(0)
    n.insert(5)
    n.insert(10)
    n.insert (3)
    n.insert(-5)
    with pytest.raises(KeyError):
        n.delete(7)
Example #3
0
def test_get_node_path_works(nodes_to_insert, key, expected):
    values = iter(nodes_to_insert)
    n = PlainNode(next(values))
    for value in values:
        n.insert(value)

    result = [node.value for node in n.get_node_path(key)]
    assert result == list(expected)
Example #4
0
def test_node_get_non_existing_element_raises_keyerror():
    n = PlainNode(0)
    n.insert(5)
    n.insert(10)
    n.insert(7)
    with pytest.raises(KeyError) as error:
        n.get(8)
    assert True
Example #5
0
def test_node_insert_replaces_value():
    n = PlainNode(0, "zero")
    n.insert(5, "five")
    n.insert(10, "ten")
    assert n.get(5) is n.right
    assert n.get(5).value == "five"
    assert len(n) == 3
    n.insert(5, "fake")
    assert n.get(5) is n.right
    assert n.get(5).value == "fake"
    assert len(n) == 3
Example #6
0
def test_node_insert_can_create_new():
    n = PlainNode(0, "zero")
    n.insert(5, "five")
    n.insert(10, "ten")
    assert n.get(5).value == "five"
    assert len(n) == 3
    n.insert(5, "fake", replace=False)
    assert n.get(5).value == "five"  # Retrieves first inserted value
    assert len(n) == 4
    assert n.get(5).right.left.value == "fake"
Example #7
0
def test_node_delete_root_two_branches():
    n = PlainNode(0)
    n.insert(5)
    n.insert(-5)
    n.delete(0)
    assert n.value in (5, -5)
    assert bool(n.left) ^ bool(n.right)
Example #8
0
def test_node_get_non_existing_element_returns_path_to_closest(
    nodes_to_insert,
    search_value,
    expected_return_values
):
    values = iter(nodes_to_insert)
    n = PlainNode(next(values))
    for value in values:
        n.insert(value)

    closest_nodes = n.get_closest(search_value)

    assert closest_nodes[0].value == expected_return_values[0]
    assert closest_nodes[1].value == expected_return_values[1]
Example #9
0
def test_node_insert_both_sides():
    n = PlainNode(0)
    n.insert(10)
    n.insert(-10)
    n.insert(5)
    assert n.right.value == 10
    assert n.left.value == -10
    assert n.right.left.value == 5
Example #10
0
def test_node_value_equals_key_if_not_given():
    n = PlainNode(0)
    assert n.value == 0
    assert not n.left
    assert not n.right
    assert n.key_func is None
    assert n.leaf
Example #11
0
def test_node_delete_branch():
    n = PlainNode(0)
    n.insert(5)
    n.insert(10)
    n.delete(5)
    assert n.value is 0
    assert n.right.value == 10
    assert n.right.depth == 1
    assert not n.right.left and not n.right.right
Example #12
0
def test_node_len():
    n = PlainNode(0)
    assert len(n) == 1
    n.insert(10)
    assert len(n) == 2
    n.insert(5)
    assert len(n) == 3
    n.insert(-10)
    assert len(n) == 4
Example #13
0
def test_node_delete_branch_2_leaves():
    n = PlainNode(0)
    n.insert(5)
    n.insert(10)
    n.insert (3)
    n.delete(5)
    assert n.value is 0
    assert n.right.value in (10, 3)
    assert n.right.depth == 2
    assert n.right.left and n.right.left.value == 3 or n.right.right and n.right.right.value == 10
    assert bool(n.right.left) ^ bool(n.right.right)
Example #14
0
def test_node_delete_root_one_branch():
    n = PlainNode(0)
    n.insert(5)
    n.delete(0)
    assert n.value == 5
    assert n.depth == 1
    assert not n.left and not n.right
Example #15
0
def test_traverse_to_side(nodes_to_insert, node_to_start, side, expected_last_node):
    values = iter(nodes_to_insert)
    n = PlainNode(next(values))
    for value in values:
        n.insert(value)

    path = n._traverse_to_side(n.get_node_path(node_to_start), side)
    if not path:
        assert expected_last_node is None
        return
    assert path[-1].value == expected_last_node
Example #16
0
def test_node_insert_right_left():
    n = PlainNode(0)
    n.insert(10)
    n.insert(5)
    assert n.right.left.value == 5
Example #17
0
def test_node_insert_right():
    n = PlainNode(0)
    n.insert(10)
    assert n.right.value == 10
    assert not n.left
    assert not n.leaf
Example #18
0
def test_node_get_leave_value():
    n = PlainNode(0, "zero")
    n.insert(5, "five")
    assert n.get(5).value == "five"
Example #19
0
def test_node_delete_root_single_node():
    n = PlainNode(0)
    assert not(n.delete(0))
Example #20
0
def test_node_get_branch_identity():
    n = PlainNode(0)
    n.insert(5)
    n.insert(10)
    assert n.get(5) is n.right
Example #21
0
def test_node_get_identity_or_value_independs_other_nodes():
    n = PlainNode(0, "zero")
    n.insert(5)
    n.insert(10, "ten")
    assert n.get(5).value == 5
Example #22
0
def test_node_get_branch_value():
    n = PlainNode(0, "zero")
    n.insert(5, "five")
    n.insert(10, "ten")
    assert n.get(5).value == "five"
Example #23
0
def test_node_insert_left():
    n = PlainNode(0)
    n.insert(-10)
    assert n.left.value == -10
    assert not n.right
    assert not n.leaf
Example #24
0
def test_node_get_root_value():
    n = PlainNode(0, "zero")
    assert n.get(0).value == "zero"
Example #25
0
def test_depth(values, expected):
    root = PlainNode(values[0])
    for v in values[1:]:
        root.insert(v)
    assert root.depth == expected
Example #26
0
def test_node_get_root_identity():
    n = PlainNode(0)
    assert n.get(0) is n
Example #27
0
def test_node_delete_leaf():
    n = PlainNode(0)
    n.insert(5)
    n.delete(5)
    assert not n.right
    assert not n.left
Example #28
0
def test_node_iter():
    n = PlainNode(0)
    n.insert(5)
    n.insert(-5)
    n.insert(-3)
    assert [x.value for x in n] == [-5, -3, 0, 5]
Example #29
0
def test_node_insert_right_right():
    n = PlainNode(0)
    n.insert(10)
    n.insert(20)
    assert n.right.right.value == 20
Example #30
0
def test_node_get_leave_identity():
    n = PlainNode(0)
    n.insert(5)
    assert n.get(5) is n.right