Exemplo n.º 1
0
    def test_get_max(self) -> None:
        """Test obtaining a max from a heap."""
        heap = ExtHeapQueue()

        heap.push(10.0, 10)
        assert heap.get_max() == 10

        heap.push(-10.0, -10)
        assert heap.get_max() == 10

        heap.push(100.0, 100)
        assert heap.get_max() == 100

        heap.remove(100)
        assert heap.get_max() == 10

        heap.remove(10)
        assert heap.get_max() == -10
Exemplo n.º 2
0
    def test_get_last_empty(self) -> None:
        """Test obtaining a last item from an empty heap produces an exception."""
        heap = ExtHeapQueue()

        with pytest.raises(KeyError, match="the heap is empty"):
            heap.get_last()

        heap.push(1.0, 1)
        heap.remove(1)

        with pytest.raises(KeyError, match="the heap is empty"):
            heap.get_last()

        heap.push(1.0, 1)
        heap.pop()

        with pytest.raises(KeyError, match="the heap is empty"):
            heap.get_max()
Exemplo n.º 3
0
    def test_max_refcount(self) -> None:
        """Test manipulation with reference counter on max - compare with the standard heapq."""
        heap = ExtHeapQueue()

        a, b = "foo_max", "bar_max"

        assert sys.getrefcount(a) == sys.getrefcount(b)
        heap.push(1.0, a)
        assert sys.getrefcount((a)) == sys.getrefcount(b) + 1
        assert heap.get_max() == a
        assert sys.getrefcount((a)) == sys.getrefcount(b) + 1