예제 #1
0
def test_init():
    """implement a heap that can be either min or max
    the choice is possible at initialization time.
    It defaults to an empty heap but allows initialization
    with an iterable"""
    assert isinstance(b.Binheap(), b.Binheap)
    assert isinstance(b.Binheap(xrange(10)), b.Binheap)
    assert isinstance(b.Binheap(prop="min"), b.Binheap)
예제 #2
0
def test_push():
    bin = b.Binheap()
    bin.push(0)
    assert bin._data[0] == 0
    bin.push(1)
    assert bin._data[0] == 1
    assert bin._data[1] == 0
    bin.push(-1)
    assert bin._data[0] == 1
    assert bin._data[1] == 0
    assert bin._data[2] == -1

    bin = b.Binheap(prop="min")
    bin.push(0)
    assert bin._data[0] == 0
    bin.push(1)
    assert bin._data[0] == 0
    assert bin._data[1] == 1
    bin.push(-1)
    assert bin._data[0] == -1
    assert bin._data[1] == 1
    assert bin._data[2] == 0
예제 #3
0
def test_pop():
    bin = b.Binheap()
    bin.push(0)  #        1
    bin.push(1)  #       / \
    bin.push(-1)  #      0  -1

    assert bin.pop() == 1  #   0
    assert bin._data[0] == 0  #   |
    assert bin._data[1] == -1  #  -1

    assert bin.pop() == 0
    assert bin.pop() == -1
    assert bin._bottom == -1  # means it's empty!

    for i in xrange(20):
        bin.push(random.random())
    for j in xrange(10):
        higher = bin.pop()
        lower = bin.pop()
        assert higher > lower

    with pytest.raises(IndexError):
        bin.pop()
예제 #4
0
def populated_binheap():
    """Create populated binary heap 2 though 9"""
    populated = binheap.Binheap()
    for x in range(2, 10):
        populated.push(x)
    return populated
예제 #5
0
def empty_binheap():
    """Create empty binary heap"""
    return binheap.Binheap()
예제 #6
0
def test_init():
    """Test init of binheap Class."""
    test = binheap.Binheap()
    assert type(test.items) is list