Beispiel #1
0
def test_insert_node_width(typ, value):
    """See: skiplist/doc/dot/doc_insert_remove.svg to node 3"""
    num_nodes = 4
    seq = (
        1,
        1,
        0,  # Node value 0
        1,
        0,  # Node value 1
        1,
        1,
        0,  # Node value 2
        0,  # Node value 3
    )
    assert len(seq) <= SEEDTREE_DEPTH
    orderedstructs.seed_rand(SEED_DICT[seq])
    sl = orderedstructs.SkipList(typ)
    for i in range(num_nodes):
        assert sl.lacks_integrity() == 0
        sl.insert(value * i)
        assert sl.lacks_integrity() == 0
    # Now test
    expected = {
        0: (1, 1, 2),
        1: (1, 1),
        2: (1, 2, 2),
        3: (1, ),
    }
    for i in range(num_nodes):
        assert sl.lacks_integrity() == 0
        assert sl.node_height(i) == len(expected[i])
        for level in range(sl.node_height(i)):
            assert sl.node_width(i, level) == expected[i][level]
        assert sl.lacks_integrity() == 0
Beispiel #2
0
def test_seed_tree_permutations():
    """Test the seed_tree using the orderedstructs random number generator which is
    actually std::rand() and std::srand() for seeding."""
    results = []
    for seq in itertools.product(range(2), repeat=SEEDTREE_DEPTH):
        orderedstructs.seed_rand(SEED_DICT[seq])
        random_tosses = tuple(
            [orderedstructs.toss_coin() for _i in range(SEEDTREE_DEPTH)])
        results.append(seq == random_tosses)
        assert seq == random_tosses
Beispiel #3
0
def test_insert_all_level_one(typ, value):
    """Set up the random number generator so that each node is height 1."""
    num = 3
    seq = (0, ) * num
    assert len(seq) <= SEEDTREE_DEPTH
    orderedstructs.seed_rand(SEED_DICT[seq])
    sl = orderedstructs.SkipList(typ)
    for i in range(num):
        assert sl.lacks_integrity() == 0
        sl.insert(value * i)
        assert sl.lacks_integrity() == 0
    # Now test
    for i in range(num):
        assert sl.lacks_integrity() == 0
        assert sl.node_height(i) == 1
        assert sl.lacks_integrity() == 0
Beispiel #4
0
def test_insert_levels_one_two_three_four(typ, value):
    """Set up the random number generator so that the first node is inserted
    with height=1, second node inserted with height=2, third node inserted
    with height=3 and fourth node inserted with height=4."""
    num_nodes = 4
    seq = (0, 1, 0, 1, 1, 0, 1, 1, 1, 0)
    assert len(seq) <= SEEDTREE_DEPTH
    orderedstructs.seed_rand(SEED_DICT[seq])
    sl = orderedstructs.SkipList(typ)
    for i in range(num_nodes):
        assert sl.lacks_integrity() == 0
        sl.insert(value * i)
        assert sl.lacks_integrity() == 0
    # Now test
    for i in range(num_nodes):
        assert sl.lacks_integrity() == 0
        assert sl.node_height(i) == i + 1
        assert sl.lacks_integrity() == 0
Beispiel #5
0
def test_insert_levels_one_two_three_four_permutations(typ, value):
    """Set up the random number generator so that the first node is inserted
    with height=1, second node inserted with height=2, third node inserted
    with height=3 and fourth node inserted with height=4.
    The values to be inserted are all permutations of range(4)."""
    num = 4
    seq = (0, 1, 0, 1, 1, 0, 1, 1, 1, 0)
    assert len(seq) <= SEEDTREE_DEPTH
    for values in itertools.permutations(range(num)):
        orderedstructs.seed_rand(SEED_DICT[seq])
        sl = orderedstructs.SkipList(typ)
        for i in values:
            assert sl.lacks_integrity() == 0
            sl.insert(value * i)
            assert sl.lacks_integrity() == 0
        # Now test
        for i in range(num):
            assert sl.lacks_integrity() == 0
            assert sl.has(value * i)
            assert sl.at(i) == value * i
            assert sl.lacks_integrity() == 0
Beispiel #6
0
def test_insert_levels_one_two_three_four_node_width(typ, value):
    """Set up the random number generator so that the first node is inserted
    with height=1, second node inserted with height=2, third node inserted
    with height=3 and fourth node inserted with height=4."""
    num_nodes = 4
    seq = (
        0,
        1,
        0,
        1,
        1,
        0,
        1,
        1,
        1,
        0,
    )
    assert len(seq) <= SEEDTREE_DEPTH
    orderedstructs.seed_rand(SEED_DICT[seq])
    sl = orderedstructs.SkipList(typ)
    for i in range(num_nodes):
        assert sl.lacks_integrity() == 0
        sl.insert(value * i)
        assert sl.lacks_integrity() == 0
    # Now test
    expected = {
        0: (1, ),
        1: (1, 1),
        2: (1, 1, 1),
        3: (1, 1, 1, 1),
    }
    for i in range(num_nodes):
        assert sl.lacks_integrity() == 0
        assert sl.node_height(i) == len(expected[i])
        for level in range(sl.node_height(i)):
            assert sl.node_width(i, level) == expected[i][level]
        assert sl.lacks_integrity() == 0