Ejemplo n.º 1
0
    def test_extract_min(self):
        heap, elements = get_random_min_heap()

        actual_min = heap_extract_min(heap)

        assert_that(actual_min, is_(equal_to(min(elements))))
        assert_min_heap(heap)
        actual_heap_keys = heap[1:heap.heap_size]
        expected_heap_keys = sorted(elements)[1:]  # all but minimum
        assert_that(actual_heap_keys, contains_inanyorder(*expected_heap_keys))
Ejemplo n.º 2
0
    def test_min_heapify(self):
        heap, elements = get_random_min_heap()
        i = random.randint(1, heap.heap_size)
        heap[i] = elements[i - 1] = random.randint(heap[i], 999)  # randomly increase value of randomly chosen element

        min_heapify(heap, i)

        assert_that(heap.heap_size, is_(equal_to(len(elements))))
        assert_min_heap(heap)
        assert_that(heap.elements, contains_inanyorder(*elements))
Ejemplo n.º 3
0
    def test_extract_min(self):
        heap, elements = get_random_min_heap()

        actual_min = heap_extract_min(heap)

        assert_that(actual_min, is_(equal_to(min(elements))))
        assert_min_heap(heap)
        actual_heap_keys = heap[1:heap.heap_size]
        expected_heap_keys = sorted(elements)[1:]  # all but minimum
        assert_that(actual_heap_keys, contains_inanyorder(*expected_heap_keys))
Ejemplo n.º 4
0
    def test_min_heap_insert(self):
        heap, elements = get_random_min_heap()
        heap.elements.append(None)  # to increase the heap's capacity for the new element
        heap.length += 1
        new_key = random.randrange(1000)

        min_heap_insert(heap, new_key)

        assert_that(heap.heap_size, is_(equal_to(len(elements) + 1)))
        assert_min_heap(heap)
        expected_heap_keys = elements + [new_key]
        assert_that(heap.elements, contains_inanyorder(*expected_heap_keys))
Ejemplo n.º 5
0
    def test_min_heap_insert(self):
        heap, elements = get_random_min_heap()
        heap.elements.append(
            None)  # to increase the heap's capacity for the new element
        heap.length += 1
        new_key = random.randrange(1000)

        min_heap_insert(heap, new_key)

        assert_that(heap.heap_size, is_(equal_to(len(elements) + 1)))
        assert_min_heap(heap)
        expected_heap_keys = elements + [new_key]
        assert_that(heap.elements, contains_inanyorder(*expected_heap_keys))
Ejemplo n.º 6
0
    def test_heap_decrease_key(self):
        heap, elements = get_random_min_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_decrease_key).with_args(heap, i, new_key),
                        raises(RuntimeError, 'new key is larger than current key'))
        else:
            heap_decrease_key(heap, i, new_key)

            assert_that(heap.heap_size, is_(equal_to(len(elements))))
            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.º 7
0
    def test_priority_dequeue(self):
        # create a random min heap of numbers
        heap, elements = get_random_min_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 = min([element for element in heap[1:heap.heap_size]], key=lambda e: e.key)
        expected_elements.remove(expected_deleted)

        actual_deleted = priority_dequeue(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))
Ejemplo n.º 8
0
    def test_heap_decrease_key(self):
        heap, elements = get_random_min_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_decrease_key).with_args(heap, i, new_key),
                raises(RuntimeError, 'new key is larger than current key'))
        else:
            heap_decrease_key(heap, i, new_key)

            assert_that(heap.heap_size, is_(equal_to(len(elements))))
            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.º 9
0
    def test_heap_minimum(self):
        heap, elements = get_random_min_heap()

        actual_min = heap_minimum(heap)

        assert_that(actual_min, is_(equal_to(min(elements))))
Ejemplo n.º 10
0
    def test_heap_minimum(self):
        heap, elements = get_random_min_heap()

        actual_min = heap_minimum(heap)

        assert_that(actual_min, is_(equal_to(min(elements))))