Exemplo n.º 1
0
def test_op_complexity():
    beap = VerifiedBeap()
    items = []
    res = []

    RANGE = 100
    for i in range(RANGE):
        v = random.randrange(RANGE * 2)
        items.append(v)
        max_iters = math.ceil(math.sqrt(2 * len(beap.arr)))
        beap.iters = 0
        beap.insert(v)
        assert beap.iters <= max_iters

    max_iters = 2 * math.ceil(math.sqrt(2 * len(beap.arr)))

    for v in items:
        beap.iters = 0
        beap.search(v)
        assert beap.iters <= max_iters

    while beap.arr:
        max_iters = math.ceil(math.sqrt(2 * len(beap.arr)))
        beap.iters = 0
        beap.delete(0, 0)
        assert beap.iters <= max_iters
Exemplo n.º 2
0
def test_insert_remove_random():
    beap = VerifiedBeap()
    items = []
    res = []

    RANGE = 100
    for i in range(RANGE):
        v = random.randrange(RANGE * 2)
        items.append(v)
        beap.insert(v)
        beap.check_invariants()

    while items:
        idx = random.randrange(len(items))
        v = items.pop(idx)
        beap.remove(v)
        beap.check_invariants()

    assert len(beap.arr) == 0
Exemplo n.º 3
0
def test_insert():
    beap = VerifiedBeap()
    data = sorted(BEAP_DATA)
    for v in data:
        beap.insert(v)
        beap.check_invariants()
    # Doesn't work and doesn't have to work, as there're many different
    # partial orderings compatible with beap.
    #assert beap.arr == BEAP_DATA

    beap2 = VerifiedBeap()
    data = sorted(BEAP_DATA, reverse=True)
    for v in data:
        beap2.insert(v)
        beap2.check_invariants()

    for i in range(101):
        res1 = bool(beap.search(i))
        res2 = bool(beap2.search(i))
        assert res1 == res2
Exemplo n.º 4
0
def test_insert_trace_few():
    beap = VerifiedBeap()

    beap.insert(1)
    assert beap.height == 0
    assert beap.arr == [1]
    beap.check_invariants()

    beap.insert(2)
    assert beap.height == 1
    assert beap.arr == [2, 1]
    beap.check_invariants()

    beap.insert(3)
    assert beap.height == 1
    assert beap.arr == [3, 1, 2]
    beap.check_invariants()

    beap.insert(4)
    assert beap.height == 2
    assert beap.arr == [4, 3, 2, 1]
    beap.check_invariants()

    beap.insert(5)
    assert beap.height == 2
    assert beap.arr == [5, 3, 4, 1, 2]
    beap.check_invariants()

    beap.insert(6)
    assert beap.height == 2
    assert beap.arr == [6, 3, 5, 1, 2, 4]
    beap.check_invariants()

    beap.insert(7)
    assert beap.height == 3
    assert beap.arr == [7, 6, 5, 3, 2, 4, 1]
    beap.check_invariants()    
Exemplo n.º 5
0
def main():
    beap = VerifiedBeap()
    beap.arr = BEAP_DATA.copy()
    beap.height = 6
    beap.insert(54)