Example #1
0
def test_pq_pop_when_empty():
    """Ensures that an exception is raised when trying
    to pop() from an empty priority heap.
    """
    pq = PriorityQueue()
    with pytest.raises(IndexError):
        pq.pop()
Example #2
0
def test_peek(create_pq):
    """Ensures that peek raises an error when the priority queue
    is empty, and that it returns the right value when items are added.
    """
    pq = PriorityQueue()
    with pytest.raises(IndexError):
        pq.peek()
    pq = create_pq
    pq.insert(100, "This is so much fun!")
    assert pq.peek() == "This is so much fun!"
    assert pq.peek() != "This sucks"
Example #3
0
def test_pop_single(create_pq):
    pq = PriorityQueue()
    pq.insert(1, "Almost missed this")
    assert pq.pop() == "Almost missed this"
Example #4
0
def create_pq():
    """Creates a priotity queue, then inserts priorities and values to use
    for further testing.
    """
    pq = PriorityQueue()
    pq.insert(1, "We")
    pq.insert(2, "eee")
    pq.insert(1, "eee")
    pq.insert(3, "are")
    pq.insert(1, "the")
    pq.insert(4, "champions")
    pq.insert(1, "my")
    pq.insert(5, "friends")
    return pq
Example #5
0
def test_pq_insert2():
    """Test that inserting into a priority queue
    behaves as expected.
    """
    pq = PriorityQueue()
    pq.insert(1, "We")
    assert pq.heap[0].value == "We"
    pq.insert(2, "eee")
    assert pq.heap[0].value == "eee"
    pq.insert(1, "eee")
    assert pq.heap[0].value == "eee"
    pq.insert(3, "are")
    assert pq.heap[0].value == "are"
    pq.insert(1, "the")
    assert pq.heap[0].value == "are"
    pq.insert(4, "champions")
    assert pq.heap[0].value == "champions"
    pq.insert(1, "my")
    assert pq.heap[0].value == "champions"
    pq.insert(5, "friends")
    assert pq.heap[0].value == "friends"