new tester; all tests categorized
This commit is contained in:
parent
51ee524109
commit
20b5f31c03
481 changed files with 794 additions and 2506 deletions
15
tests/casestmt/tcase_setconstr.nim
Normal file
15
tests/casestmt/tcase_setconstr.nim
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
discard """
|
||||
output: "an identifier"
|
||||
"""
|
||||
|
||||
const
|
||||
SymChars: set[char] = {'a'..'z', 'A'..'Z', '\x80'..'\xFF'}
|
||||
|
||||
proc classify(s: string) =
|
||||
case s[0]
|
||||
of SymChars, '_': echo "an identifier"
|
||||
of {'0'..'9'}: echo "a number"
|
||||
else: echo "other"
|
||||
|
||||
classify("Hurra")
|
||||
|
||||
30
tests/casestmt/tcaseexpr1.nim
Normal file
30
tests/casestmt/tcaseexpr1.nim
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
discard """
|
||||
file: "tcaseexpr1.nim"
|
||||
|
||||
line: 29
|
||||
errormsg: "type mismatch: got (string) but expected 'int'"
|
||||
|
||||
line: 23
|
||||
errormsg: "not all cases are covered"
|
||||
"""
|
||||
|
||||
type
|
||||
E = enum A, B, C
|
||||
|
||||
proc foo(x): auto =
|
||||
return case x
|
||||
of 1..9: "digit"
|
||||
else: "number"
|
||||
|
||||
var r = foo(10)
|
||||
|
||||
var x = C
|
||||
|
||||
var t1 = case x:
|
||||
of A: "a"
|
||||
of B: "b"
|
||||
|
||||
var t2 = case x:
|
||||
of A: 10
|
||||
of B, C: "23"
|
||||
|
||||
15
tests/casestmt/tcaseoverlaprange.nim
Normal file
15
tests/casestmt/tcaseoverlaprange.nim
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
discard """
|
||||
line: 13
|
||||
errormsg: "duplicate case label"
|
||||
"""
|
||||
|
||||
type
|
||||
TE = enum A, B, C, D
|
||||
|
||||
var
|
||||
e: TE
|
||||
|
||||
case e
|
||||
of A..D, B..C:
|
||||
echo "redundant"
|
||||
else: nil
|
||||
18
tests/casestmt/tcaseoverlaprange2.nim
Normal file
18
tests/casestmt/tcaseoverlaprange2.nim
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
discard """
|
||||
line: 13
|
||||
errormsg: "duplicate case label"
|
||||
"""
|
||||
|
||||
|
||||
|
||||
|
||||
proc checkDuplicates(myval: int32): bool =
|
||||
case myval
|
||||
of 0x7B:
|
||||
echo "this should not compile"
|
||||
of 0x78 .. 0x7D:
|
||||
result = true
|
||||
else:
|
||||
nil
|
||||
|
||||
echo checkDuplicates(0x7B)
|
||||
40
tests/casestmt/tcasestm.nim
Normal file
40
tests/casestmt/tcasestm.nim
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
discard """
|
||||
file: "tcasestm.nim"
|
||||
output: "ayyydd"
|
||||
"""
|
||||
# Test the case statement
|
||||
|
||||
type
|
||||
tenum = enum eA, eB, eC
|
||||
|
||||
var
|
||||
x: string = "yyy"
|
||||
y: Tenum = eA
|
||||
i: int
|
||||
|
||||
case y
|
||||
of eA: write(stdout, "a")
|
||||
of eB, eC: write(stdout, "b or c")
|
||||
|
||||
case x
|
||||
of "Andreas", "Rumpf": write(stdout, "Hallo Meister!")
|
||||
of "aa", "bb": write(stdout, "Du bist nicht mein Meister")
|
||||
of "cc", "hash", "when": nil
|
||||
of "will", "it", "finally", "be", "generated": nil
|
||||
|
||||
var z = case i
|
||||
of 1..5, 8, 9: "aa"
|
||||
of 6, 7: "bb"
|
||||
elif x == "Ha":
|
||||
"cc"
|
||||
elif x == "yyy":
|
||||
write(stdout, x)
|
||||
"dd"
|
||||
else:
|
||||
"zz"
|
||||
|
||||
echo z
|
||||
#OUT ayyy
|
||||
|
||||
|
||||
|
||||
47
tests/casestmt/tcomputedgoto.nim
Normal file
47
tests/casestmt/tcomputedgoto.nim
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
discard """
|
||||
output: '''yeah A enumB
|
||||
yeah A enumB
|
||||
yeah CD enumD
|
||||
yeah CD enumE
|
||||
yeah A enumB
|
||||
yeah CD enumE
|
||||
yeah CD enumD
|
||||
yeah A enumB
|
||||
yeah B enumC
|
||||
yeah A enumB
|
||||
yeah A enumB
|
||||
yeah A enumB'''
|
||||
"""
|
||||
|
||||
type
|
||||
MyEnum = enum
|
||||
enumA, enumB, enumC, enumD, enumE
|
||||
|
||||
proc vm() =
|
||||
var instructions: array [0..100, MyEnum]
|
||||
instructions[2] = enumC
|
||||
instructions[3] = enumD
|
||||
instructions[4] = enumA
|
||||
instructions[5] = enumD
|
||||
instructions[6] = enumC
|
||||
instructions[7] = enumA
|
||||
instructions[8] = enumB
|
||||
|
||||
instructions[12] = enumE
|
||||
var pc = 0
|
||||
while true:
|
||||
{.computedGoto.}
|
||||
let instr = instructions[pc]
|
||||
let ra = instr.succ # instr.regA
|
||||
case instr
|
||||
of enumA:
|
||||
echo "yeah A ", ra
|
||||
of enumC, enumD:
|
||||
echo "yeah CD ", ra
|
||||
of enumB:
|
||||
echo "yeah B ", ra
|
||||
of enumE:
|
||||
break
|
||||
inc(pc)
|
||||
|
||||
vm()
|
||||
24
tests/casestmt/tlinearscanend.nim
Normal file
24
tests/casestmt/tlinearscanend.nim
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
|
||||
import strutils
|
||||
|
||||
var x = 343
|
||||
|
||||
case stdin.readline.parseInt
|
||||
of 0:
|
||||
echo "most common case"
|
||||
of 1:
|
||||
{.linearScanEnd.}
|
||||
echo "second most common case"
|
||||
of 2: echo "unlikely: use branch table"
|
||||
else:
|
||||
echo "unlikely too: use branch table"
|
||||
|
||||
|
||||
case x
|
||||
of 23: echo "23"
|
||||
of 343: echo "343"
|
||||
of 21: echo "21"
|
||||
else:
|
||||
{.linearScanEnd.}
|
||||
echo "default"
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue