diff options
author | flywind <43030857+xflywind@users.noreply.github.com> | 2020-11-22 04:16:35 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-21 12:16:35 -0800 |
commit | f3887dea2cc1969981fc62d5c91f1bafef755582 (patch) | |
tree | 1927b0492b8fb631004379292a03351ad96b49bc /tests/stdlib/theapqueue.nim | |
parent | 5e8ac485a28f12cf66fb6637e4ef86a9afc117e8 (diff) | |
download | Nim-f3887dea2cc1969981fc62d5c91f1bafef755582.tar.gz |
heapqueue minor improvement (#16088)
Diffstat (limited to 'tests/stdlib/theapqueue.nim')
-rw-r--r-- | tests/stdlib/theapqueue.nim | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/tests/stdlib/theapqueue.nim b/tests/stdlib/theapqueue.nim new file mode 100644 index 000000000..20e23e878 --- /dev/null +++ b/tests/stdlib/theapqueue.nim @@ -0,0 +1,53 @@ +import heapqueue + + +proc toSortedSeq[T](h: HeapQueue[T]): seq[T] = + var tmp = h + result = @[] + while tmp.len > 0: + result.add(pop(tmp)) + +block: # Simple sanity test + var heap = initHeapQueue[int]() + let data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0] + for item in data: + push(heap, item) + doAssert(heap == data.toHeapQueue) + doAssert(heap[0] == 0) + doAssert(heap.toSortedSeq == @[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) + +block: # Test del + var heap = initHeapQueue[int]() + let data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0] + for item in data: push(heap, item) + + heap.del(0) + doAssert(heap[0] == 1) + + heap.del(heap.find(7)) + doAssert(heap.toSortedSeq == @[1, 2, 3, 4, 5, 6, 8, 9]) + + heap.del(heap.find(5)) + doAssert(heap.toSortedSeq == @[1, 2, 3, 4, 6, 8, 9]) + + heap.del(heap.find(6)) + doAssert(heap.toSortedSeq == @[1, 2, 3, 4, 8, 9]) + + heap.del(heap.find(2)) + doAssert(heap.toSortedSeq == @[1, 3, 4, 8, 9]) + + doAssert(heap.find(2) == -1) + +block: # Test del last + var heap = initHeapQueue[int]() + let data = [1, 2, 3] + for item in data: push(heap, item) + + heap.del(2) + doAssert(heap.toSortedSeq == @[1, 2]) + + heap.del(1) + doAssert(heap.toSortedSeq == @[1]) + + heap.del(0) + doAssert(heap.toSortedSeq == @[]) |