Beispiel #1
0
def test_binheap_push_two_values_second_less_than():
    """Tests push on binary heap"""
    from binheap import Binheap
    new_binheap = Binheap()
    new_binheap.push(2)
    new_binheap.push(1)
    assert new_binheap._container == [2, 1]
Beispiel #2
0
def test_heap_handles_push_pops_with_correct_ordering():
    """."""
    from binheap import Binheap
    heap = Binheap([3, 5, 6, 2, 7, 8, 1, 0, 0, 10])
    assert heap.__repr__() == '[0, 1, 2, 3, 7, 8, 6, 5, 10]'
    assert heap.pop() == 0
    assert heap.__repr__() == '[1, 3, 2, 5, 7, 8, 6, 10]'
Beispiel #3
0
 def insert(self, item):
     """Make sure it's a tuple and try to insert it"""
     try:
         assert isinstance(item, tuple)
         assert len(item) == 2
     except AssertionError:
         raise ValueError("Only tuples of (priority, data) are valid")
     Binheap.push(self, item)
Beispiel #4
0
def test_pop():
    abh = Binheap([1, 2, 3, 4, 5, 6])

    for x in range(6, 0, -1):
        assert abh.pop() == x

    # onto an empty heap
    print abh.values
    with pytest.raises(IndexError):
        abh.pop()
Beispiel #5
0
def test_binheap_push_multiple_unordered():
    """Tests push on binary heap"""
    from binheap import Binheap
    new_binheap = Binheap()
    new_binheap.push(5)
    new_binheap.push(4)
    new_binheap.push(6)
    new_binheap.push(3)
    new_binheap.push(7)
    new_binheap.push(2)
    new_binheap.push(9)
    assert new_binheap._container == [9, 6, 7, 3, 4, 2, 5]
Beispiel #6
0
def test_create_populated():

    with pytest.raises(TypeError):
        other = Binheap(1)

    # one item
    pbh = Binheap([1])
    assert pbh.values[0] == 1
    # two or more parents unbalanced
    abh = Binheap([1, 2, 3, 4, 5, 6])
    assert abh.values[0] == 6
    assert abh.values[1] == 4
    assert abh.values[2] == 5
    assert abh.values[3] == 1
    assert abh.values[4] == 3
    assert abh.values[5] == 2
Beispiel #7
0
def test_binheap_push_one_value_pop_one_value():
    """Tests push on binary heap"""
    from binheap import Binheap
    new_binheap = Binheap()
    new_binheap.push(2)
    new_binheap.pop()
    assert new_binheap._container == []
Beispiel #8
0
 def dijkstra(self, start, end):
     """Awasome dijkstra using min heap."""
     if start not in self.graph or end not in self.graph:
         raise ValueError('no such node exist')
     heap, visited = Binheap(), set()
     heap.push([0, start, []])
     while heap:
         curdist, node, path = heap.pop()
         path = path + [node]
         if node == end:
             break
         if node not in visited:
             visited.add(node)
             for neighbor, weight in self.neighbors(node).items():
                 heap.push((curdist + weight, neighbor, path))
     else:
         return [], None
     return curdist, path
Beispiel #9
0
def binheap_five_rand_num():
    """Create a binary heap 5 random numbers."""
    from binheap import Binheap
    return Binheap([5, 4, 1, 3, 2])
Beispiel #10
0
def binheap_100_rand():
    from binheap import Binheap
    from random import randint
    heap = Binheap([randint(1, 1000) for _ in range(100)])
    return heap
Beispiel #11
0
def full_heap():
    """Fixture for easy creation of a populated heap."""
    from binheap import Binheap
    full_heap = Binheap([1, 3, 4, 5, 6, 7, 8, 9])
    return full_heap
def test_pop(value, result):
    from binheap import Binheap
    new_heap = Binheap()
    new_heap.push(value)
    new_heap.pop()
    assert new_heap.binheap == result
Beispiel #13
0
def test_init_empty():
    """Test binary heap empty on init"""
    from binheap import Binheap
    new_binheap = Binheap()
    assert new_binheap._container == []
Beispiel #14
0
def max_heap():
    """Fixture for easy creation of a populated heap."""
    from binheap import Binheap
    max_heap = Binheap(iterable=[1, 3, 4, 5, 6, 7, 8, 9], style="max")
    return max_heap
Beispiel #15
0
def empty_heap():
    """Fixture for easy creation of an empty heap."""
    from binheap import Binheap
    empty_heap = Binheap()
    return empty_heap
Beispiel #16
0
def test_push():
    ebh = Binheap()
    assert isinstance(ebh, Binheap)
    # onto an empty heap
    ebh.push(5)
    assert ebh.values[0] == 5

    ebh.push(1)
    assert ebh.values[1] == 1
    ebh.push(5)
    assert ebh.values[2] == 5

    ebh.push(2)
    assert ebh.values[1] == 2
    assert ebh.values[3] == 1
    ebh.push(3)
    assert ebh.values[4] == 2
    assert ebh.values[1] == 3

    ebh.push(7)
    assert ebh.values[0] == 7
    assert ebh.values[2] == 5
    assert ebh.values[5] == 5
    ebh.push(6)
    assert ebh.values[0] == 7
    assert ebh.values[2] == 6
    assert ebh.values[5] == 5
    assert ebh.values[6] == 5
def big_heap():
    return Binheap([100, 75, 25, 50])
def biggest_heap():
    return Binheap([100, 75, 25, 50, 30, 15])
Beispiel #19
0
def test_initalizing_with_non_iterable_or_not_numbers_raises_ValueError():
    """."""
    from binheap import Binheap
    with pytest.raises(TypeError):
        Binheap("dfsdfadgasdg")
Beispiel #20
0
def test_cant_init_w_noniter():
    """Test you can't instantiate heap with noniterable."""
    from binheap import Binheap
    with pytest.raises(TypeError):
        Binheap(8675309)
Beispiel #21
0
def full_heap():
    """Fixture for full heap."""
    from binheap import Binheap
    heap = Binheap([1, 2, 3])
    return heap
Beispiel #22
0
def binheap_empty():
    """Create a binary heap."""
    from binheap import Binheap
    return Binheap()
Beispiel #23
0
def test_binheap_push_single_value_container_head():
    """Tests push on binary heap"""
    from binheap import Binheap
    new_binheap = Binheap()
    new_binheap.push(1)
    assert new_binheap._container == [1]
Beispiel #24
0
def empty_heap():
    """Fixture for empty heap."""
    from binheap import Binheap
    heap = Binheap()
    return heap
Beispiel #25
0
def test_create_empty():
    bh = Binheap()
    assert isinstance(bh, Binheap)
def small_heap():
    return Binheap([100, 50, 25])
Beispiel #27
0
def test_init_binheap():
    """Tests initialization of binary heap"""
    from binheap import Binheap
    new_binheap = Binheap([1])
    assert new_binheap._container == [1]
def bigger_heap():
    return Binheap([100, 75, 25, 50, 30])
Beispiel #29
0
def test_binheap_init_multiple_unordered():
    """Tests init of binary heap"""
    from binheap import Binheap
    new_binheap = Binheap([5, 4, 6, 3, 7, 2, 9])
    assert new_binheap._container == [9, 6, 7, 3, 4, 2, 5]
def empty_heap():
    return Binheap()
Beispiel #31
0
def test_binheap_init_multiple_unordered_2():
    """Tests init of binary heap"""
    from binheap import Binheap
    new_binheap = Binheap([1, 2, 3, 4])
    assert new_binheap._container == [4, 3, 2, 1]