Skip to content

Commit

Permalink
Added test case for destructors
Browse files Browse the repository at this point in the history
  • Loading branch information
yglukhov committed Jul 31, 2024
1 parent 1070440 commit 78453f8
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tests/test5.nim
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,47 @@ block:
log 2

waitFor cb(5)

block:
expectOutput """
new 1
new 2
1
destroy 1
b 2
2
destroy 2
"""

type
SmartPtr = object
o: int

proc newId(): int =
var g {.global.} = 0
inc g
log "new ", g
g

proc `=destroy`(o: var SmartPtr) =
if o.o != 0:
log "destroy ", o.o
o.o = 0

proc `=copy`(a: var SmartPtr, b: SmartPtr) =
if a.o != 0:
log "delete ", a.o
if b.o == 0:
a.o = 0
else:
a.o = newId()

proc foo() {.async.} =
let a = SmartPtr(o: newId())
var b = SmartPtr(o: newId())
log 1
await sleep(1)
log "b ", b.o
log 2

waitFor foo()

0 comments on commit 78453f8

Please sign in to comment.