Пример #1
0
    def test_remove_empty(self) -> None:
        """Test remove method when the heap is empty."""
        heap = ExtHeapQueue()

        with pytest.raises(ValueError,
                           match="the given item was not found in the heap"):
            heap.remove(1992)
Пример #2
0
    def test_push(self) -> None:
        """Test pushing an item onto heap."""
        heap = ExtHeapQueue()

        assert len(heap) == 0
        heap.push(1.0, 1)
        assert len(heap) == 1
        assert heap.pop() == 1
Пример #3
0
    def test_pop_empty(self) -> None:
        """Test pop'ing an item from empty heap produces an exception."""
        heap = ExtHeapQueue()

        assert len(heap) == 0
        with pytest.raises(KeyError, match="the heap is empty"):
            heap.pop()

        assert len(heap) == 0
Пример #4
0
    def test_top_refcount(self) -> None:
        """Test manipulation with reference counter on get_top - compare with the standard heapq."""
        heap = ExtHeapQueue()

        a, b = "foo_top", "bar_top"

        assert sys.getrefcount(a) == sys.getrefcount(b)
        heap.push(1.0, a)
        assert sys.getrefcount((a)) == sys.getrefcount(b) + 1
        assert heap.get_top() == a
        assert sys.getrefcount((a)) == sys.getrefcount(b) + 1
Пример #5
0
    def test_push_refcount(self) -> None:
        """Test manipulation with reference counter on push - compare with the standard heapq."""
        heap = ExtHeapQueue()
        arr = []

        a, b = "foo_push", "bar_push"

        assert sys.getrefcount(a) == sys.getrefcount(b)

        heap.push(1.0, a)
        heapq.heappush(arr, b)

        assert sys.getrefcount(a) == sys.getrefcount(b)
Пример #6
0
    def test_heap_sort(self, arr) -> None:
        """Test manipulation with heap on heap sorting."""
        heap = ExtHeapQueue()

        # Remove duplicates.
        arr = list(dict.fromkeys(arr).keys())

        for item in arr:
            heap.push(float(item), item)

        result = []
        while len(heap) != 0:
            a = heap.pop()
            result.append(a)

        assert result == sorted(arr)
Пример #7
0
    def test_push_size(self) -> None:
        """Test push is respecting the size configured."""
        heap = ExtHeapQueue(size=2)

        a1 = "111"
        a2 = "222"
        a3 = "333"

        a1_refcount = sys.getrefcount(a1)
        a2_refcount = sys.getrefcount(a2)
        a3_refcount = sys.getrefcount(a3)

        heap.push(1.11, a1)
        heap.push(2.22, a2)
        heap.push(3.33, a3)

        assert heap.size == 2
        assert len(heap) == 2

        assert heap.pop() == "222"
        assert heap.pop() == "333"

        assert a1_refcount == sys.getrefcount(a1)
        assert a2_refcount == sys.getrefcount(a2)
        assert a3_refcount == sys.getrefcount(a3)
Пример #8
0
    def test_clear(self) -> None:
        """Test cleaning the heap."""
        heap = ExtHeapQueue()

        heap.push(1.111, "1111")
        heap.push(2.222, "2222")
        heap.push(3.333, "3333")

        heap.clear()

        assert len(heap) == 0
Пример #9
0
    def test_items(self) -> None:
        """Test raising an exception when accessing out of range."""
        heap = ExtHeapQueue()

        assert heap.items() == []

        heap.push(1.1, "11")
        heap.push(2.2, "22")
        heap.push(3.3, "33")

        assert set(heap.items()) == {"11", "22", "33"}
Пример #10
0
    def test_get(self) -> None:
        """Test getting an element by its index."""
        heap = ExtHeapQueue()

        heap.push(1.01, "101")

        assert heap.get(0) == "101"

        heap.push(2.02, "202")
        heap.push(3.03, "303")

        possibilities = {"101", "202", "303"}

        assert heap.get(0) in possibilities
        assert heap.get(1) in possibilities
        assert heap.get(2) in possibilities
Пример #11
0
    def test_pushpop_refcount_saved(self) -> None:
        """Test manipulation with reference counter on pushpop - the item is stored in heap."""
        heap = ExtHeapQueue()

        a1 = "zzzz"
        a2 = "fooo"

        heap.push(1.0, a1)
        assert heap.pushpop(0.1, a2) == a2
        assert heap.get_top() == a1
        assert heap.pop() == a1
Пример #12
0
    def test_pushpop(self) -> None:
        """Test pushpop method."""
        heap = ExtHeapQueue()

        heap.push(1.0, "1")
        assert heap.pushpop(2.0, "2") == "1"
        assert heap.pushpop(0.0, "0") == "0"
        assert heap.pop() == "2"
Пример #13
0
    def test_remove_not_found(self) -> None:
        """Test remove method when item is not found."""
        heap = ExtHeapQueue()

        heap.push(1984.0, 1984)
        with pytest.raises(ValueError,
                           match="the given item was not found in the heap"):
            heap.remove(1992)
Пример #14
0
    def test_pushpop_already_present(self) -> None:
        """Test pushpop method when the item is already present in the heap."""
        heap = ExtHeapQueue()

        heap.push(3.3, "33")

        with pytest.raises(
                ValueError,
                match="the given item is already present in the heap"):
            heap.pushpop(3.3, "33")
Пример #15
0
    def test_push_already_present(self) -> None:
        """Test pushing an item that is already present on heap."""
        heap = ExtHeapQueue()

        heap.push(1.0, 1)
        assert len(heap) == 1
        with pytest.raises(
                ValueError,
                match="the given item is already present in the heap"):
            heap.push(1.0, 1)

        assert len(heap) == 1
Пример #16
0
    def _heap_default(self) -> ExtHeapQueue:
        """Initialize the extended heap queue."""
        if self.width is not None:
            return ExtHeapQueue(size=self.width)

        return ExtHeapQueue()
Пример #17
0
 def test_pushpop_refcount(self) -> None:
     """Test manipulation with reference counter on pushpop - compare with the standard heapq."""
     heap = ExtHeapQueue()
     a = "foo_pushpop_1"
     heap.pushpop(1.0, a)
Пример #18
0
    def test_get_top(self) -> None:
        """Test manipulation with the top (the smallest) item recorded."""
        heap = ExtHeapQueue()

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

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

        heap.push(100.0, 100)
        assert heap.get_top() == -10

        heap.remove(100)
        assert heap.get_top() == -10

        heap.remove(10)
        assert heap.get_top() == -10
Пример #19
0
    def test_get_out_of_range(self) -> None:
        """Test raising an exception when accessing out of range."""
        heap = ExtHeapQueue()

        with pytest.raises(IndexError, match="index out of range"):
            heap.get(0)

        with pytest.raises(IndexError, match="index out of range"):
            heap.get(110)

        with pytest.raises(IndexError, match="index out of range"):
            heap.get(199)

        heap.push(1.1, "11")
        heap.push(2.2, "22")

        with pytest.raises(IndexError, match="index out of range"):
            heap.get(110)
Пример #20
0
    def test_get_top_empty(self) -> None:
        """Test obtaining a top from an empty heap."""
        heap = ExtHeapQueue()

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

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

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

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

        with pytest.raises(KeyError, match="the heap is empty"):
            heap.get_top()
Пример #21
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
Пример #22
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()
Пример #23
0
    def test_get_last(self) -> None:
        """Test obtaining a last item from a heap."""
        heap = ExtHeapQueue()

        heap.push(6.0, 6)
        assert heap.get_last() == 6

        heap.push(3.0, 3)
        assert heap.get_last() == 3

        heap.remove(3)
        assert heap.get_last() is None

        heap.push(8.0, 8)
        assert heap.get_last() == 8

        heap.pop()
        assert heap.get_last() is 8

        assert len(heap) == 1

        heap.pop()
        assert len(heap) == 0
Пример #24
0
    def test_pushpop_empty(self) -> None:
        """Test pushpop method when the heap is empty."""
        heap = ExtHeapQueue()

        assert heap.pushpop(1.0, 1) == 1

        heap.push(2.0, 2)
        heap.remove(2)

        assert heap.pushpop(3.0, 3) == 3

        heap.push(4.0, 4)
        heap.pop()

        assert heap.pushpop(5.0, 5) == 5
Пример #25
0
    def test_remove(self) -> None:
        """Test remove method."""
        heap = ExtHeapQueue()

        obj1 = "090x"
        obj2 = "090X"

        heap.push(1.0, obj1)
        heap.push(2.0, obj2)

        heap.remove(obj1)
        assert len(heap) == 1
        assert heap.pop() == obj2

        heap.push(2.0, obj2)
        heap.remove(obj2)
        assert len(heap) == 0