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