mirror of
https://github.com/nim-lang/Nim.git
synced 2026-05-25 14:28:15 +00:00
Fixes #18583. ## Problem Several stdlib collection types compute the separator for `$` using `result.len > 1`, where `result` starts as the opening bracket (`"["` or `"{"`). This breaks when a collection element type has a `$` that returns an empty string: `result.len` stays at 1 after the first item contributes nothing, so the separator is never inserted for subsequent items. ```nim import std/deques type Test = object proc `$`(x: Test): string = "" echo [Test(), Test()].toDeque # prints [] — expected [, ] ``` ## Fix Replace the length check with an explicit `first` flag in all affected modules: `deques`, `heapqueue`, `lists`, `critbits`, and `strtabs`. ## Tests Regression tests added to `tdeques`, `theapqueue`, and `tlists` using a local type whose `$` returns `""`. All three test files pass with `nim c -r`. ## Notes I work with Claude as a co-processor. I'm 56, came to programming late, and this is genuinely how I learn and contribute. I understand what I'm submitting, but I didn't write it alone. If your project prefers human-only contributions, just say so and I'll close without friction. --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
119 lines
2.7 KiB
Nim
119 lines
2.7 KiB
Nim
discard """
|
|
matrix: "--mm:refc; --mm:orc"
|
|
"""
|
|
|
|
import std/heapqueue
|
|
import std/assertions
|
|
|
|
proc toSortedSeq[T](h: HeapQueue[T]): seq[T] =
|
|
var tmp = h
|
|
result = @[]
|
|
while tmp.len > 0:
|
|
result.add(pop(tmp))
|
|
|
|
proc heapProperty[T](h: HeapQueue[T]): bool =
|
|
for k in 0 .. h.len - 2: # the last element is always a leaf
|
|
let left = 2 * k + 1
|
|
if left < h.len and h[left] < h[k]:
|
|
return false
|
|
let right = left + 1
|
|
if right < h.len and h[right] < h[k]:
|
|
return false
|
|
true
|
|
|
|
template main() =
|
|
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 == @[])
|
|
|
|
block: # testing the heap proeprty
|
|
var heap = [1, 4, 2, 5].toHeapQueue
|
|
doAssert heapProperty(heap)
|
|
|
|
heap.push(42)
|
|
doAssert heapProperty(heap)
|
|
heap.push(0)
|
|
doAssert heapProperty(heap)
|
|
heap.push(3)
|
|
doAssert heapProperty(heap)
|
|
heap.push(3)
|
|
doAssert heapProperty(heap)
|
|
|
|
# [0, 3, 1, 4, 42, 2, 3, 5]
|
|
|
|
discard heap.pop()
|
|
doAssert heapProperty(heap)
|
|
discard heap.pop()
|
|
doAssert heapProperty(heap)
|
|
|
|
heap.del(2)
|
|
doAssert heapProperty(heap)
|
|
|
|
# [2, 3, 5, 4, 42]
|
|
|
|
discard heap.replace(12)
|
|
doAssert heapProperty(heap)
|
|
discard heap.replace(1)
|
|
doAssert heapProperty(heap)
|
|
|
|
discard heap.pushpop(2)
|
|
doAssert heapProperty(heap)
|
|
discard heap.pushpop(0)
|
|
doAssert heapProperty(heap)
|
|
|
|
static: main()
|
|
main()
|
|
|
|
# https://github.com/nim-lang/Nim/issues/18583
|
|
type EmptyStr18583HeapQ = object
|
|
proc `$`(x: EmptyStr18583HeapQ): string = ""
|
|
proc `<`(a, b: EmptyStr18583HeapQ): bool = false
|
|
|
|
block:
|
|
var h = initHeapQueue[EmptyStr18583HeapQ]()
|
|
push(h, EmptyStr18583HeapQ())
|
|
push(h, EmptyStr18583HeapQ())
|
|
let s = $h
|
|
doAssert s == "[, ]", "got: " & s
|