Esempio n. 1
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 == []
Esempio n. 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]'
Esempio n. 3
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]
Esempio n. 4
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
Esempio n. 5
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
Esempio n. 6
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()
Esempio n. 7
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]
Esempio n. 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
Esempio n. 9
0
def test_init_empty():
    """Test binary heap empty on init"""
    from binheap import Binheap
    new_binheap = Binheap()
    assert new_binheap._container == []
Esempio n. 10
0
def biggest_heap():
    return Binheap([100, 75, 25, 50, 30, 15])
Esempio n. 11
0
def big_heap():
    return Binheap([100, 75, 25, 50])
Esempio n. 12
0
def test_initalizing_with_non_iterable_or_not_numbers_raises_ValueError():
    """."""
    from binheap import Binheap
    with pytest.raises(TypeError):
        Binheap("dfsdfadgasdg")
Esempio n. 13
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
Esempio n. 14
0
def binheap_five_rand_num():
    """Create a binary heap 5 random numbers."""
    from binheap import Binheap
    return Binheap([5, 4, 1, 3, 2])
Esempio n. 15
0
def full_heap():
    """Fixture for full heap."""
    from binheap import Binheap
    heap = Binheap([1, 2, 3])
    return heap
Esempio n. 16
0
def test_create_empty():
    bh = Binheap()
    assert isinstance(bh, Binheap)
Esempio n. 17
0
def empty_heap():
    """Fixture for easy creation of an empty heap."""
    from binheap import Binheap
    empty_heap = Binheap()
    return empty_heap
Esempio n. 18
0
def binheap_empty():
    """Create a binary heap."""
    from binheap import Binheap
    return Binheap()
Esempio n. 19
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)
Esempio n. 20
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
Esempio n. 21
0
def binheap_100_rand():
    from binheap import Binheap
    from random import randint
    heap = Binheap([randint(1, 1000) for _ in range(100)])
    return heap
Esempio n. 22
0
def empty_heap():
    """Fixture for empty heap."""
    from binheap import Binheap
    heap = Binheap()
    return heap
Esempio n. 23
0
def test_init_binheap():
    """Tests initialization of binary heap"""
    from binheap import Binheap
    new_binheap = Binheap([1])
    assert new_binheap._container == [1]
Esempio n. 24
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]
Esempio n. 25
0
def small_heap():
    return Binheap([100, 50, 25])
Esempio n. 26
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]
Esempio n. 27
0
def bigger_heap():
    return Binheap([100, 75, 25, 50, 30])
Esempio n. 28
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]
Esempio n. 29
0
def empty_heap():
    return Binheap()
Esempio n. 30
0
def test_push_max_large(init_list):
    raw, min_heap, max_heap = init_list
    heap = Binheap(max_heap, minmax="max")
    heap.push(10)
    assert heap == [10, 1, 7, -3, -9, 5]