コード例 #1
0
def test_heap_behaves_correctly_with_duplicate_values():
    heap = Heap(5, default_priority_function)
    heap.insert(1)
    heap.insert(5)
    heap.insert(3)
    heap.insert(5)
    assert heap.extract() == 5
    assert heap.extract() == 5
    assert heap.extract() == 3
    assert heap.peek() == 1
コード例 #2
0
def test_extract_returns_value_from_heap():
    heap = Heap(5, default_priority_function)
    heap.insert(1)
    heap.insert(5)
    heap.insert(6)
    result = heap.extract()
    assert result == 6
    result = heap.extract()
    assert result == 5
    result = heap.extract()
    assert result == 1
コード例 #3
0
def test_get_size_returns_size_of_heap():
    heap = Heap(5, default_priority_function)
    heap.insert(1)
    heap.insert(5)
    heap.insert(6)
    heap.insert(2)
    result = heap.get_size()
    assert result == 4
    heap.extract()
    result = heap.get_size()
    assert result == 3
コード例 #4
0
def test_insert_adds_values_to_heap_out_of_order():
    heap = Heap(5)
    heap.insert(1)
    heap.insert(3)
    heap.insert(10)
    heap.insert(6)
    heap.insert(2)
    assert heap.extract() == 10
    assert heap.extract() == 6
    assert heap.extract() == 3
    assert heap.extract() == 2
    assert heap.extract() == 1
コード例 #5
0
def test_extract_and_insert_alternating_returns_values():
    heap = Heap(5, default_priority_function)
    heap.insert(1)
    heap.insert(5)
    assert heap.extract() == 5
    heap.insert(6)
    heap.insert(2)
    assert heap.extract() == 6
    heap.insert(6)
    assert heap.extract() == 6
    heap.insert(10)
    heap.insert(1)
    assert heap.get_size() == 4
コード例 #6
0
def test_heap_extract_returns_the_proper_number_when_it_is_the_last():
    min_heap = Heap(10, max_priority_function)

    min_heap.insert(1)
    min_heap.insert(3)
    min_heap.insert(2)
    min_heap.insert(4)
    result = min_heap.extract()
    assert result == 4
コード例 #7
0
def test_delete_removes_target_item_from_heap():
    heap = Heap(5)
    heap.insert(1)
    heap.insert(3)
    heap.insert(2)
    heap.insert(4)
    heap.delete(3)
    assert heap.get_size() == 3
    assert heap.extract() == 4
    assert heap.extract() == 2
コード例 #8
0
def test_extract_returns_value_from_heap():
    def priority_function(item1, item2):
        return item1 - item2

    heap = Heap(5, priority_function)
    heap.insert(1)
    heap.insert(5)
    heap.insert(6)
    result = heap.extract()
    # assert result == 5
    print(heap.items)
    # assert heap.items[0] == 1
    # assert heap.items[1] is None


# insert things that are out of priority order and test the result
# assert the right values on each extract
# switch off between inserts and extracts in some tests
# Test duplicate priorities (insert(1)) (insert(1)) etc.
# Write a test with a bubble down with a full queue. This should move the item
# at the end