def test_extract_max(self): heap, elements = get_random_max_heap() actual_max = heap_extract_max(heap) assert_that(actual_max, is_(equal_to(max(elements)))) assert_max_heap(heap) actual_heap_keys = heap[1:heap.heap_size] expected_heap_keys = sorted(elements)[:-1] # all but maximum assert_that(actual_heap_keys, contains_inanyorder(*expected_heap_keys))
def test_iterative_max_heapify(self): heap, elements = get_random_max_heap() i = random.randint(1, heap.heap_size) heap[i] = elements[i - 1] = random.randint(0, heap[i]) # randomly decrease value of randomly chosen element iterative_max_heapify(heap, i) assert_that(heap.heap_size, is_(equal_to(len(elements)))) assert_max_heap(heap) assert_that(heap.elements, contains_inanyorder(*elements))
def test_iterative_max_heapify(self): heap, elements = get_random_max_heap() i = random.randint(1, heap.heap_size) heap[i] = elements[i - 1] = random.randint( 0, heap[i]) # randomly decrease value of randomly chosen element iterative_max_heapify(heap, i) assert_that(heap.heap_size, is_(equal_to(len(elements)))) assert_max_heap(heap) assert_that(heap.elements, contains_inanyorder(*elements))
def test_multiary_max_heapify(self): ary = random.randint(2, 7) heap, elements = get_random_max_heap(ary=ary) i = random.randint(1, heap.heap_size) heap[i] = elements[i - 1] = random.randint(0, heap[i]) # randomly decrease value of randomly chosen element multiary_max_heapify(heap, ary, i) assert_that(heap.heap_size, is_(equal_to(len(elements)))) assert_max_heap(heap, ary=ary) assert_that(heap.elements, contains_inanyorder(*elements))
def test_max_heap_insert(self): heap, elements = get_random_max_heap() heap.elements.append(None) # to increase the heap's capacity for the new element heap.length += 1 new_key = random.randrange(1000) max_heap_insert(heap, new_key) assert_that(heap.heap_size, is_(equal_to(len(elements) + 1))) assert_max_heap(heap) expected_heap_keys = elements + [new_key] assert_that(heap.elements, contains_inanyorder(*expected_heap_keys))
def test_max_heap_delete(self): heap, elements = get_random_max_heap() i = random.randint(1, heap.heap_size) key_to_delete = heap[i] actual_deleted_key = max_heap_delete(heap, i) assert_that(actual_deleted_key, is_(equal_to(key_to_delete))) assert_max_heap(heap) actual_heap_keys = heap[1:heap.heap_size] expected_heap_keys = list(elements) expected_heap_keys.remove(key_to_delete) assert_that(actual_heap_keys, contains_inanyorder(*expected_heap_keys))
def test_multiary_max_heap_insert(self): ary = random.randint(2, 7) heap, elements = get_random_max_heap(ary=ary) heap.elements.append(None) # to increase the heap's capacity for the new element heap.length += 1 new_key = random.randrange(1000) multiary_max_heap_insert(heap, ary, new_key) assert_that(heap.heap_size, is_(equal_to(len(elements) + 1))) assert_max_heap(heap, ary=ary) expected_heap_keys = elements + [new_key] assert_that(heap.elements, contains_inanyorder(*expected_heap_keys))
def test_multiary_heap_increase_key(self): ary = random.randint(2, 7) heap, elements = get_random_max_heap(ary=ary) i = random.randint(1, heap.heap_size) old_key = heap[i] new_key = random.randrange(1000) real_new_key = max(old_key, new_key) multiary_heap_increase_key(heap, ary, i, new_key) assert_that(heap.heap_size, is_(equal_to(len(elements)))) assert_max_heap(heap, ary=ary) expected_heap_keys = list(elements) expected_heap_keys.remove(old_key) expected_heap_keys.append(real_new_key) assert_that(heap.elements, contains_inanyorder(*expected_heap_keys))
def test_priority_pop(self): # create a random max heap of numbers heap, elements = get_random_max_heap() # and then transform the numbers to elements with keys and data expected_elements = [] for i in range(1, heap.heap_size + 1): heap[i] = Element(heap[i], "element " + str(heap[i])) expected_elements.append(heap[i]) expected_deleted = max([element for element in heap[1:heap.heap_size]], key=lambda e: e.key) expected_elements.remove(expected_deleted) actual_deleted = priority_pop(heap) assert_that(actual_deleted, is_(equal_to(expected_deleted))) actual_elements = heap[1:heap.heap_size].elements assert_that(actual_elements, contains_inanyorder(*expected_elements))
def test_heap_increase_key(self): heap, elements = get_random_max_heap() i = random.randint(1, heap.heap_size) old_key = heap[i] new_key = random.randrange(1000) if new_key < old_key: assert_that(calling(heap_increase_key).with_args(heap, i, new_key), raises(RuntimeError, 'new key is smaller than current key')) else: heap_increase_key(heap, i, new_key) assert_that(heap.heap_size, is_(equal_to(len(elements)))) assert_max_heap(heap) expected_heap_keys = list(elements) expected_heap_keys.remove(old_key) expected_heap_keys.append(new_key) assert_that(heap.elements, contains_inanyorder(*expected_heap_keys))
def test_heap_increase_key(self): heap, elements = get_random_max_heap() i = random.randint(1, heap.heap_size) old_key = heap[i] new_key = random.randrange(1000) if new_key < old_key: assert_that( calling(heap_increase_key).with_args(heap, i, new_key), raises(RuntimeError, 'new key is smaller than current key')) else: heap_increase_key(heap, i, new_key) assert_that(heap.heap_size, is_(equal_to(len(elements)))) assert_max_heap(heap) expected_heap_keys = list(elements) expected_heap_keys.remove(old_key) expected_heap_keys.append(new_key) assert_that(heap.elements, contains_inanyorder(*expected_heap_keys))
def test_heap_maximum(self): heap, elements = get_random_max_heap() actual_max = heap_maximum(heap) assert_that(actual_max, is_(equal_to(max(elements))))