def test_insert_raises_exception_when_heap_is_full(): def priority_function(item1, item2): return item1 - item2 heap = Heap(0, priority_function) with pytest.raises(Exception): heap.insert(20)
def test_insert_grows_when_heap_is_full_and_length_not_0(): heap = Heap(1) assert heap.size == 1 assert len(heap.items) == 1 heap.insert(20) heap.insert(21) assert heap.size == 2 assert len(heap.items) == 2
def test_insert_adds_value_to_heap(): def priority_function(item1, item2): return item1 - item2 heap = Heap(5, priority_function) heap.insert(1) heap.insert(3) assert heap.items[0] == 3 assert heap.items[1] == 1
def test_running_median_challenge(): sut = RunningMedian() array = sut.parse_text_file(text) min_heap = Heap(10, max_priority_function) max_heap = Heap(10, min_priority_function) sum = 0 for float_number in array: sum += sut.running_median(min_heap, max_heap, float_number) assert sum == pytest.approx(4995738.755804, .000001)
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
def test_running_median_finds_running_median_along_way(): min_heap = Heap(10, max_priority_function) max_heap = Heap(10, min_priority_function) new_value = 4 sut = RunningMedian() result = sut.running_median(min_heap, max_heap, new_value) assert result == 4 result = sut.running_median(min_heap, max_heap, 6) assert result == 5 result = sut.running_median(min_heap, max_heap, 10) assert result == 6
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
def test_is_balanced_returns_false_when_heaps_off_by_more_than_one(): min_heap = Heap(5, max_priority_function) max_heap = Heap(5, min_priority_function) min_heap.insert(1) min_heap.insert(3) min_heap.insert(2) max_heap.insert(6) sut = RunningMedian() result = sut.is_balanced(min_heap, max_heap) assert result is False
def test_is_balanced_returns_true_when_heaps_odd_and_off_by_one(): min_heap = Heap(5, max_priority_function) max_heap = Heap(5, min_priority_function) min_heap.insert(1) min_heap.insert(3) min_heap.insert(2) max_heap.insert(6) max_heap.insert(7) sut = RunningMedian() result = sut.is_balanced(min_heap, max_heap) assert result is True
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
def test_running_media_with_sliding_window_behaves_appropriately_with_challenge( ): sut = RunningMedian() array = sut.parse_text_file(text) min_heap = Heap(10, max_priority_function) max_heap = Heap(10, min_priority_function) sum = 0 window = [] for float_number in array: sum += sut.running_median_with_sliding_window(min_heap, max_heap, float_number, window, 100) expected_result = 4995205.397700 assert sum == pytest.approx(expected_result, 0.0001)
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
def test_heap_can_be_created_with_proper_parameters(): def priority_function(item1, item2): return item1 - item2 result = Heap(5, priority_function) assert result.size == 5 assert result.items == [None, None, None, None, None, None]
def test_peek_returns_highest_priority_value(): heap = Heap(5, default_priority_function) heap.insert(1) heap.insert(5) heap.insert(6) heap.insert(2) result = heap.peek() assert result == 6
def test_maintain_sliding_window_reduces_size_appropriately(): sut = RunningMedian() array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] min_heap = Heap(10, max_priority_function) max_heap = Heap(10, min_priority_function) for i in range(5): min_heap.insert(array[i]) max_heap.insert(array[i + 5]) sut.maintain_sliding_window(min_heap, max_heap, array, 11, 10) assert array[0] == 2 assert len(array) == 10 assert array[-1] != 10
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
def test_running_median_finds_running_median_with_heaps_set_up(): min_heap = Heap(10, max_priority_function) min_heap.insert(3) min_heap.insert(5) max_heap = Heap(10, min_priority_function) max_heap.insert(7) max_heap.insert(9) new_value = 4 sut = RunningMedian() result = sut.running_median(min_heap, max_heap, new_value) assert result == 5 result = sut.running_median(min_heap, max_heap, 6) assert result == 5.5 result = sut.running_median(min_heap, max_heap, 10) assert result == 6
def test_running_media_with_sliding_window_works_with_individual_additions(): min_heap = Heap(10, max_priority_function) max_heap = Heap(10, min_priority_function) window = [] new_value = 4 sut = RunningMedian() result = sut.running_median_with_sliding_window(min_heap, max_heap, new_value, window, 3) assert result == 4 result = sut.running_median_with_sliding_window(min_heap, max_heap, 6, window, 3) assert result == 5 result = sut.running_median_with_sliding_window(min_heap, max_heap, 10, window, 3) assert result == 6 result = sut.running_median_with_sliding_window(min_heap, max_heap, 1, window, 3) assert result == 6 result = sut.running_median_with_sliding_window(min_heap, max_heap, 11, window, 3) assert result == 10 result = sut.running_median_with_sliding_window(min_heap, max_heap, 2, window, 3) assert result == 2
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
def test_balance_heaps_balanced_an_imbalanced_pair_of_heaps(): min_heap = Heap(10, max_priority_function) max_heap = Heap(10, min_priority_function) min_heap.insert(1) min_heap.insert(3) min_heap.insert(2) min_heap.insert(4) max_heap.insert(7) max_heap.insert(5) sut = RunningMedian() sut.balance_heaps(min_heap, max_heap) assert min_heap.get_size() == 3 assert max_heap.get_size() == 3 assert 4 in max_heap.items assert 4 not in min_heap.items
def test_expand_doubles_the_size_of_the_heap(): heap = Heap(5) heap.insert(1) heap.expand() assert heap.size == 10 assert len(heap.items) == 10
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
def test_insert_adds_value_to_heap(): heap = Heap(5) heap.insert(1) heap.insert(3) assert heap.items[0] == 3 assert heap.items[1] == 1
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
def test_is_balanced_returns_true_with_balanced_heaps(): min_heap = Heap(5, max_priority_function) max_heap = Heap(5, min_priority_function) min_heap.insert(1) min_heap.insert(3) min_heap.insert(2) max_heap.insert(6) max_heap.insert(7) max_heap.insert(5) sut = RunningMedian() result = sut.is_balanced(min_heap, max_heap) assert result is True
def test_delete_raises_exception_if_value_not_present(): heap = Heap(5) heap.insert(1) with pytest.raises(Exception): heap.delete(4)
def test_heap_can_be_created_with_proper_parameters(): result = Heap(5) assert result.size == 5 assert result.items == [None, None, None, None, None]