Ejemplo n.º 1
0
    def test_build_max_heap_(self):
        array, elements = get_random_array()

        build_max_heap_(array)

        assert_that(array, has_property('heap_size'))
        assert_that(array.heap_size, is_(equal_to(array.length)))
        assert_max_heap(array)
Ejemplo n.º 2
0
    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))
Ejemplo n.º 3
0
    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))
Ejemplo n.º 4
0
    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))
Ejemplo n.º 5
0
    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))
Ejemplo n.º 6
0
    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))
Ejemplo n.º 7
0
    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))
Ejemplo n.º 8
0
    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))
Ejemplo n.º 9
0
    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))
Ejemplo n.º 10
0
    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))
Ejemplo n.º 11
0
    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))
Ejemplo n.º 12
0
    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))
Ejemplo n.º 13
0
    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))