Skip to content

Commit

Permalink
break try tests up also
Browse files Browse the repository at this point in the history
  • Loading branch information
disruptek committed Jan 30, 2024
1 parent caf7721 commit 37632ce
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 149 deletions.
149 changes: 0 additions & 149 deletions tests/t80_try.nim → tests/t80_try1.nim
Original file line number Diff line number Diff line change
Expand Up @@ -548,152 +548,3 @@ suite "try statements":
except CatchableError as e:
check e.msg == "test", "unable to pass exception message from cps"
check r == 1

suite "defer statements":

var r = 0

block:
## a defer statement works across a continuation
r = 0
proc foo() {.cps: Cont.} =
defer:
check r == 2, "defer run before end of scope"
inc r

inc r
noop()
inc r

foo()
check r == 3

block:
## a basic defer statement is supported
r = 0
proc foo() {.cps: Cont.} =
inc r
noop()
defer:
check r == 4
inc r
inc r
defer:
check r == 3
inc r
inc r

foo()
check r == 5

block:
## a defer in a nested template is supported
r = 0

template deferChk(i: int) =
inc r
defer:
check r == i
inc r

proc foo() {.cps: Cont.} =
deferChk(5)
inc r
deferChk(4)
inc r

foo()
check r == 6

block:
## a defer inside a block statement works
r = 0
proc foo() {.cps: Cont.} =
inc r
block:
defer:
check r == 2
inc r
inc r
defer:
check r == 4
inc r
inc r

foo()
check r == 5

block:
## a naked defer is not a problem
r = 0
proc foo() {.cps: Cont.} =
defer:
inc r

foo()
check r == 1


when defined(gcArc) or defined(gcOrc):
suite "breaking deterministic memory managers":
block:
## try-except-statement splits
proc foo() {.cps: Cont.} =
var k = newKiller(3)
step 1
try:
noop()
step 2
except CatchableError:
fail "this branch should not run"
step 3

foo()

block:
## try-except splits with raise
proc foo() {.cps: Cont.} =
var k = newKiller(4)
step 1
try:
noop()
step 2
raise newException(CatchableError, "")
fail "statement run after raise"
except CatchableError:
step 3
step 4

foo()

block:
## try-finally-statement splits
proc foo() {.cps: Cont.} =
var k = newKiller(4)
step 1
try:
noop()
step 2
finally:
step 3
step 4

foo()

block:
## try-except-finally splits with raise
proc foo() {.cps: Cont.} =
var k = newKiller(5)
step 1
try:
noop()
step 2
raise newException(CatchableError, "")
fail "statement run after raise"
except CatchableError:
step 3
finally:
step 4
step 5

foo()
154 changes: 154 additions & 0 deletions tests/t80_try2.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import std/strutils

include preamble

from cps/spec import cpsStackFrames

suite "defer statements":

var r = 0

block:
## a defer statement works across a continuation
r = 0
proc foo() {.cps: Cont.} =
defer:
check r == 2, "defer run before end of scope"
inc r

inc r
noop()
inc r

foo()
check r == 3

block:
## a basic defer statement is supported
r = 0
proc foo() {.cps: Cont.} =
inc r
noop()
defer:
check r == 4
inc r
inc r
defer:
check r == 3
inc r
inc r

foo()
check r == 5

block:
## a defer in a nested template is supported
r = 0

template deferChk(i: int) =
inc r
defer:
check r == i
inc r

proc foo() {.cps: Cont.} =
deferChk(5)
inc r
deferChk(4)
inc r

foo()
check r == 6

block:
## a defer inside a block statement works
r = 0
proc foo() {.cps: Cont.} =
inc r
block:
defer:
check r == 2
inc r
inc r
defer:
check r == 4
inc r
inc r

foo()
check r == 5

block:
## a naked defer is not a problem
r = 0
proc foo() {.cps: Cont.} =
defer:
inc r

foo()
check r == 1


when defined(gcArc) or defined(gcOrc):
suite "breaking deterministic memory managers":
block:
## try-except-statement splits
proc foo() {.cps: Cont.} =
var k = newKiller(3)
step 1
try:
noop()
step 2
except CatchableError:
fail "this branch should not run"
step 3

foo()

block:
## try-except splits with raise
proc foo() {.cps: Cont.} =
var k = newKiller(4)
step 1
try:
noop()
step 2
raise newException(CatchableError, "")
fail "statement run after raise"
except CatchableError:
step 3
step 4

foo()

block:
## try-finally-statement splits
proc foo() {.cps: Cont.} =
var k = newKiller(4)
step 1
try:
noop()
step 2
finally:
step 3
step 4

foo()

block:
## try-except-finally splits with raise
proc foo() {.cps: Cont.} =
var k = newKiller(5)
step 1
try:
noop()
step 2
raise newException(CatchableError, "")
fail "statement run after raise"
except CatchableError:
step 3
finally:
step 4
step 5

foo()

0 comments on commit 37632ce

Please sign in to comment.