def test_multipop_maxheap(): maxheap = BinaryHeap([7, 9, 18, 1, 38, 5.4, 6, 200], minheap=False) length = len(maxheap) maxheap.pop() maxheap.pop() maxheap.push(400) maxheap.pop() maxheap.pop() assert maxheap.pop() == 9 assert len(maxheap) == length - 4
def test_multipop_minheap(): minheap = BinaryHeap([7, 9, 18, 1, 38, 5.4, 6, 200]) length = len(minheap) minheap.pop() minheap.pop() minheap.push(0) minheap.pop() minheap.pop() assert minheap.pop() == 7 assert len(minheap) == length - 4
def test_bubbledown_maxheap(): maxheap = BinaryHeap([6, 5, 4, 3, 2, 1, 0], minheap=False) maxheap[6] = 4000 maxheap._bubbleup(6) assert maxheap[0] == 4000 assert is_maxheap_sorted(maxheap)
def test_bubbledown_minheap(): minheap = BinaryHeap([0, 1, 2, 3, 4, 5, 6]) minheap[0] = 4000 minheap._bubbledown(0) assert minheap[0] == 1 assert is_minheap_sorted(minheap)
def test_swap(): minheap = BinaryHeap([0, 1, 2, 3, 4, 5, 6]) minheap._swap(0, 6) assert minheap.tree[0] == 6 assert minheap.tree[6] == 0
def test_is_unsorted_maxheap_comparison(): minheap = BinaryHeap(minheap=False) assert minheap._is_unsorted(2, 1)
def test_is_unsorted_minheap_comparison(): minheap = BinaryHeap(minheap=True) assert minheap._is_unsorted(1, 2)
def test_find_children(): minheap = BinaryHeap([0, 1, 2, 3, 4, 5, 6]) assert minheap._find_children(0) == (1, 2) assert minheap._find_children(2) == (5, 6)
def test_find_parent(): minheap = BinaryHeap([0, 1, 2, 3, 4, 5, 6]) assert minheap._find_parent(2) == 0 assert minheap._find_parent(6) == 2
def minheap_empty(): return BinaryHeap()
def test_pop_maxheap(): maxheap = BinaryHeap([7, 9, 18, 1, 38, 5.4, 6], minheap=False) maxheap.push(400) length = len(maxheap) assert maxheap.pop() == 400 assert len(maxheap) == length - 1
def test_pop_minheap(): minheap = BinaryHeap([7, 9, 18, 1, 38, 5.4, 6]) minheap.push(0) length = len(minheap) assert minheap.pop() == 0 assert len(minheap) == length - 1
def test_invalid_instantiation(bad_input): """Test that bad by creating and doing one pop""" with pytest.raises(TypeError): BinaryHeap(bad_input)
def test_valid_instantiation_max(input, output): """Test instantiation by creating and doing one pop""" heap_under_test = BinaryHeap(input, minheap=False) assert is_maxheap_sorted(heap_under_test) assert heap_under_test.pop() == output