Пример #1
0
def test_selfbalancing():
    size = 200000
    import random
    random.seed(1)
    randvals = [random.randint(1, 10000000) for x in range(size)]

    vt = VariantTree()
    for i,x in enumerate(randvals):
        vt.set_item(x,1)
        if i % 1000 == 0:
            assert vt.verify()

    random.shuffle(randvals)

    for i,x in enumerate(randvals):
        vt.clear_item(x)
        if i % 1000 == 0:
            assert vt.verify()
Пример #2
0
def test_basics():
    vt = VariantTree()
    assert vt.refcode == 0
    assert vt.node_count() == 0 
    assert vt.get_item(10) == 0
    assert 10 not in vt
    assert not list(vt.keys())
    assert not list(vt.values())

    vt.set_item(10, 1)
    assert vt.node_count() == 1
    assert vt.get_item(10) == 1
    assert list(vt.keys()) == [10]
    assert list(vt.values()) == [1]
    assert 10 in vt

    vt.set_item(100, 1)
    assert vt.node_count() == 2
    assert vt.get_item(100) == 1
    assert list(vt.keys()) == [10, 100]
    assert list(vt.values()) == [1,1]
    assert 100 in vt

    vt.set_item(11, 2)
    assert vt.node_count() == 2
    assert vt.get_item(11) == 2
    assert list(vt.keys()) == [10,11,100]
    assert list(vt.values()) == [1,2,1]
    assert 11 in vt

    vt.clear_item(11)
    assert vt.node_count() == 2
    assert vt.get_item(11) == 0
    assert list(vt.keys()) == [10,100]
    assert list(vt.values()) == [1,1]
    assert 11 not in vt

    vt.clear_item(100)
    assert vt.node_count() == 1
    assert 100 not in vt

    vt.clear_item(10)
    assert vt.node_count() == 0
    assert 10 not in vt

    import random
    random.seed(1)
    rvals = list(set([random.randint(1,10000) for x in range(1000)]))
    vt = VariantTree()
    assert vt.empty()

    for rval in rvals:
        assert rval not in vt
        vt.set_item(rval, 1)
        assert rval in vt

    assert vt.node_count() == len({x // vt._binsize() for x in rvals})
    random.shuffle(rvals)

    for rval in rvals:
        assert rval in vt
        vt.clear_item(rval)
        assert rval not in vt

    assert vt.node_count() == 0
    assert vt.empty()