def test_construct(source, expected_list):
    """
    GIVEN source for construction and expected list
    WHEN the Heap is constructed with the source
    THEN the underlying list is set to the expected list.
    """
    test_heap = heap.Heap(source)

    assert test_heap._list == expected_list
def test_extract_max_empty():
    """
    GIVEN empty heap
    WHEN extract_max is called
    THEN IndexError is raised.
    """
    test_heap = heap.Heap()

    with pytest.raises(IndexError):
        test_heap.extract_max()
def test_size(source, expected_size):
    """
    GIVEN source for heap and expected size
    WHEN heap is constructed with the source and len is called
    THEN the expected size is returned.
    """
    test_heap = heap.Heap(source)

    size = len(test_heap)

    assert size == expected_size
def test_is_empty(source, expected_result):
    """
    GIVEN source for heap and expected result
    WHEN heap is constructed with the source and is_empty is called
    THEN the expected result is returned.
    """
    test_heap = heap.Heap(source)

    result = test_heap.is_empty()

    assert result == expected_result
def test_insert(source, value, expected):
    """
    GIVEN source, value to insert and expected final list
    WHEN the heap is constructed with the source and insert is called with the value
    THEN the underlying list contains the expected elements in the expected order.
    """
    test_heap = heap.Heap(source)

    test_heap.insert(value)

    assert test_heap._list == expected
def test_sorted(source, expected):
    """
    GIVEN source of elements and expected sorted elements
    WHEN the heap is constructed with the source and sorted is called
    THEN an iterator is returned with the elements in the expected order.
    """
    test_heap = heap.Heap(source)

    returned = list(test_heap.sorted())

    assert returned == expected
def test_heapify(initial, expected):
    """
    GIVEN initial elements
    WHEN _heapify is called
    THEN the elements are in the expected order.
    """
    test_heap = heap.Heap()
    test_heap._list = initial

    test_heap._heapify()

    assert test_heap._list == expected
def test_sift_down(initial, start, end, expected):
    """
    GIVEN initial elements for the list of a heap that satisfy the heap property
        except for possibly the start and the start and the end
    WHEN _sift down is called with the start and the end
    THEN the final list is equal to the expected list.
    """
    test_heap = heap.Heap()
    test_heap._list = initial

    test_heap._sift_down(start, end)

    assert test_heap._list == expected
def test_extract_max(source, expected_value, expected_list):
    """
    GIVEN source for heap, expected value and expected list
    WHEN a heap is constructed with the source and extract_max is called
    THEN the expected value is returned and the underlying list of the heap is the
        expected list.
    """
    test_heap = heap.Heap(source)

    value = test_heap.extract_max()

    assert value == expected_value
    assert test_heap._list == expected_list
def test_sort_random(random_list):  # pylint: disable=redefined-outer-name
    """
    GIVEN random list
    WHEN a heap is constructed with the list as the source and sorted is called
    THEN a sorted list of elements is returned.
    """
    test_heap = heap.Heap(random_list)

    returned = test_heap.sorted()

    previous = next(returned)
    element_count = 1
    for current in returned:
        assert current <= previous
        element_count += 1

    assert element_count == len(random_list)