mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-31 10:22:15 +00:00
added gcleak4.nim test; fails in debug mode
This commit is contained in:
42
tests/gc/gcleak4.nim
Normal file
42
tests/gc/gcleak4.nim
Normal file
@@ -0,0 +1,42 @@
|
||||
discard """
|
||||
outputsub: "no leak: "
|
||||
"""
|
||||
|
||||
when defined(GC_setMaxPause):
|
||||
GC_setMaxPause 2_000
|
||||
|
||||
type
|
||||
TExpr = object ## abstract base class for an expression
|
||||
PLiteral = ref TLiteral
|
||||
TLiteral = object of TExpr
|
||||
x: int
|
||||
TPlusExpr = object of TExpr
|
||||
a, b: ref TExpr
|
||||
|
||||
method eval(e: ref TExpr): int =
|
||||
# override this base method
|
||||
quit "to override!"
|
||||
|
||||
method eval(e: ref TLiteral): int = return e.x
|
||||
|
||||
method eval(e: ref TPlusExpr): int =
|
||||
# watch out: relies on dynamic binding
|
||||
return eval(e.a) + eval(e.b)
|
||||
|
||||
proc newLit(x: int): ref TLiteral =
|
||||
new(result)
|
||||
{.watchpoint: result.}
|
||||
result.x = x
|
||||
|
||||
proc newPlus(a, b: ref TExpr): ref TPlusExpr =
|
||||
new(result)
|
||||
{.watchpoint: result.}
|
||||
result.a = a
|
||||
result.b = b
|
||||
|
||||
for i in 0..100_000:
|
||||
if eval(newPlus(newPlus(newLit(1), newLit(2)), newLit(4))) != 7:
|
||||
quit "error: wrong result"
|
||||
if getOccupiedMem() > 3000_000: quit("still a leak!")
|
||||
|
||||
echo "no leak: ", getOccupiedMem()
|
||||
Reference in New Issue
Block a user