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)
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)
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
def test_remove_refcount(self) -> None: """Test manipulation with reference counter on remove - compare with the standard heapq.""" heap = ExtHeapQueue() arr = [] a, b = "foo_remove", "bar_remove" heap.push(1.0, a) heapq.heappush(arr, b) assert sys.getrefcount(a) == sys.getrefcount(b) heap.remove(a) heapq.heappop(arr) assert sys.getrefcount(a) == sys.getrefcount(b)
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
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()
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
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()
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
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