Ejemplo n.º 1
0
def test_single_node():
    node = SGNode(0)

    assert node.max_level == 0

    for level in range(256):
        assert node.get_neighbor(level, LEFT) is None
        assert node.get_neighbor(level, RIGHT) is None

    assert tuple(node.iter_down_levels(0, LEFT)) == ((0, None), )
    assert tuple(node.iter_down_levels(0, RIGHT)) == ((0, None), )
Ejemplo n.º 2
0
def test_single_node_with_only_left_neighbor():
    node = SGNode(1, neighbors_left=[0])

    assert node.max_level == 1

    assert node.get_neighbor(0, LEFT) == 0
    assert node.get_neighbor(0, RIGHT) is None

    for level in range(1, 256):
        assert node.get_neighbor(level, LEFT) is None
        assert node.get_neighbor(level, RIGHT) is None

    assert tuple(node.iter_down_levels(0, LEFT)) == ((0, 0), )
    assert tuple(node.iter_down_levels(0, RIGHT)) == ((0, None), )

    assert tuple(node.iter_down_levels(1, LEFT)) == ((1, None), (0, 0))
    assert tuple(node.iter_down_levels(1, RIGHT)) == ((1, None), (0, None))

    assert tuple(node.iter_neighbors()) == ((0, LEFT, 0), )
Ejemplo n.º 3
0
async def test_insert_sequential_to_the_correct_mixed_order():
    anchor = SGNode(0)
    graph = LocalGraph(anchor)

    node_3, node_1, node_2 = tuple(
        [await graph.insert(key) for key in (3, 1, 2)])

    validate_graph(graph)

    assert anchor.get_neighbor(0, LEFT) is None
    assert anchor.get_neighbor(0, RIGHT) == 1

    assert node_1.get_neighbor(0, LEFT) == 0
    assert node_1.get_neighbor(0, RIGHT) == 2

    assert node_2.get_neighbor(0, LEFT) == 1
    assert node_2.get_neighbor(0, RIGHT) == 3

    assert node_3.get_neighbor(0, LEFT) == 2
    assert node_3.get_neighbor(0, RIGHT) is None
Ejemplo n.º 4
0
def test_single_node_with_both_neighbors():
    node = SGNode(4, neighbors_left=[2, 0], neighbors_right=[8, 12, 28])

    assert node.max_level == 3

    assert node.get_neighbor(0, LEFT) == 2
    assert node.get_neighbor(0, RIGHT) == 8

    assert node.get_neighbor(1, LEFT) == 0
    assert node.get_neighbor(1, RIGHT) == 12

    assert node.get_neighbor(2, LEFT) is None
    assert node.get_neighbor(2, RIGHT) == 28

    for level in range(3, 256):
        assert node.get_neighbor(level, LEFT) is None
        assert node.get_neighbor(level, RIGHT) is None

    assert tuple(node.iter_down_levels(0, LEFT)) == ((0, 2), )
    assert tuple(node.iter_down_levels(0, RIGHT)) == ((0, 8), )

    assert tuple(node.iter_down_levels(1, LEFT)) == ((1, 0), (0, 2))
    assert tuple(node.iter_down_levels(1, RIGHT)) == ((1, 12), (0, 8))

    assert tuple(node.iter_down_levels(2, LEFT)) == ((2, None), (1, 0), (0, 2))
    assert tuple(node.iter_down_levels(2, RIGHT)) == ((2, 28), (1, 12), (0, 8))

    assert tuple(node.iter_down_levels(3, LEFT)) == ((3, None), (2, None),
                                                     (1, 0), (0, 2))
    assert tuple(node.iter_down_levels(3, RIGHT)) == ((3, None), (2, 28),
                                                      (1, 12), (0, 8))

    assert tuple(node.iter_neighbors()) == (
        (0, LEFT, 2),
        (0, RIGHT, 8),
        (1, LEFT, 0),
        (1, RIGHT, 12),
        (2, RIGHT, 28),
    )
Ejemplo n.º 5
0
def test_single_node_neighbor_setting():
    node = SGNode(4)

    assert node.max_level == 0
    assert node.get_neighbor(0, LEFT) is None
    assert node.get_neighbor(0, RIGHT) is None

    node.set_neighbor(0, LEFT, 2)

    assert node.max_level == 1
    assert node.get_neighbor(0, LEFT) == 2
    assert node.get_neighbor(0, RIGHT) is None

    node.set_neighbor(1, LEFT, 0)

    assert node.max_level == 2
    assert node.get_neighbor(0, LEFT) == 2
    assert node.get_neighbor(1, LEFT) == 0
    assert node.get_neighbor(0, RIGHT) is None

    # should be an error to set a neighbor above the topmost non-null neighbor.
    with pytest.raises(ValidationError):
        node.set_neighbor(1, RIGHT, 12)
    with pytest.raises(ValidationError):
        node.set_neighbor(2, LEFT, 28)

    node.set_neighbor(0, RIGHT, 8)

    with pytest.raises(ValidationError):
        node.set_neighbor(2, RIGHT, 28)

    assert node.max_level == 2
    assert node.get_neighbor(0, RIGHT) == 8
    assert node.get_neighbor(1, RIGHT) is None

    node.set_neighbor(1, RIGHT, 12)
    node.set_neighbor(2, RIGHT, 28)

    assert node.max_level == 3
    assert node.get_neighbor(0, RIGHT) == 8
    assert node.get_neighbor(1, RIGHT) == 12
    assert node.get_neighbor(2, RIGHT) == 28
    assert node.get_neighbor(3, RIGHT) is None
    assert node.get_neighbor(4, RIGHT) is None
    assert node.get_neighbor(5, RIGHT) is None
    assert node.get_neighbor(6, RIGHT) is None

    node.set_neighbor(3, RIGHT, None)
    node.set_neighbor(4, RIGHT, None)
    node.set_neighbor(5, RIGHT, None)
    node.set_neighbor(6, RIGHT, None)

    assert node.max_level == 3
    assert node.get_neighbor(0, RIGHT) == 8
    assert node.get_neighbor(1, RIGHT) == 12
    assert node.get_neighbor(2, RIGHT) == 28
    assert node.get_neighbor(3, RIGHT) is None
    assert node.get_neighbor(4, RIGHT) is None
    assert node.get_neighbor(5, RIGHT) is None
    assert node.get_neighbor(6, RIGHT) is None

    with pytest.raises(ValidationError):
        node.set_neighbor(1, RIGHT, None)
    with pytest.raises(ValidationError):
        node.set_neighbor(0, RIGHT, None)
    node.set_neighbor(2, RIGHT, None)

    assert node.max_level == 2
    assert node.get_neighbor(0, RIGHT) == 8
    assert node.get_neighbor(1, RIGHT) == 12
    assert node.get_neighbor(2, RIGHT) is None
    assert node.get_neighbor(3, RIGHT) is None
    assert node.get_neighbor(4, RIGHT) is None
    assert node.get_neighbor(5, RIGHT) is None
    assert node.get_neighbor(6, RIGHT) is None

    with pytest.raises(ValidationError):
        node.set_neighbor(0, RIGHT, None)
    node.set_neighbor(1, RIGHT, None)

    assert node.max_level == 2
    assert node.get_neighbor(0, RIGHT) == 8
    assert node.get_neighbor(1, RIGHT) is None
    assert node.get_neighbor(2, RIGHT) is None
    assert node.get_neighbor(3, RIGHT) is None
    assert node.get_neighbor(4, RIGHT) is None
    assert node.get_neighbor(5, RIGHT) is None
    assert node.get_neighbor(6, RIGHT) is None

    node.set_neighbor(0, RIGHT, 28)

    assert node.max_level == 2
    assert node.get_neighbor(0, RIGHT) == 28
    assert node.get_neighbor(1, RIGHT) is None
    assert node.get_neighbor(2, RIGHT) is None
    assert node.get_neighbor(3, RIGHT) is None
    assert node.get_neighbor(4, RIGHT) is None
    assert node.get_neighbor(5, RIGHT) is None
    assert node.get_neighbor(6, RIGHT) is None

    node.set_neighbor(0, RIGHT, None)

    assert node.max_level == 2
    assert node.get_neighbor(0, RIGHT) is None
    assert node.get_neighbor(1, RIGHT) is None
    assert node.get_neighbor(2, RIGHT) is None
    assert node.get_neighbor(3, RIGHT) is None
    assert node.get_neighbor(4, RIGHT) is None
    assert node.get_neighbor(5, RIGHT) is None
    assert node.get_neighbor(6, RIGHT) is None