diff options
Diffstat (limited to 'tests/casestmt')
-rw-r--r-- | tests/casestmt/tcase_setconstr.nim | 15 | ||||
-rw-r--r-- | tests/casestmt/tcaseexpr1.nim | 30 | ||||
-rw-r--r-- | tests/casestmt/tcaseoverlaprange.nim | 15 | ||||
-rw-r--r-- | tests/casestmt/tcaseoverlaprange2.nim | 18 | ||||
-rw-r--r-- | tests/casestmt/tcasestm.nim | 40 | ||||
-rw-r--r-- | tests/casestmt/tcomputedgoto.nim | 47 | ||||
-rw-r--r-- | tests/casestmt/tlinearscanend.nim | 24 |
7 files changed, 189 insertions, 0 deletions
diff --git a/tests/casestmt/tcase_setconstr.nim b/tests/casestmt/tcase_setconstr.nim new file mode 100644 index 000000000..21f657c2b --- /dev/null +++ b/tests/casestmt/tcase_setconstr.nim @@ -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") + diff --git a/tests/casestmt/tcaseexpr1.nim b/tests/casestmt/tcaseexpr1.nim new file mode 100644 index 000000000..e5e08e25e --- /dev/null +++ b/tests/casestmt/tcaseexpr1.nim @@ -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" + diff --git a/tests/casestmt/tcaseoverlaprange.nim b/tests/casestmt/tcaseoverlaprange.nim new file mode 100644 index 000000000..5f24c3ca9 --- /dev/null +++ b/tests/casestmt/tcaseoverlaprange.nim @@ -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 diff --git a/tests/casestmt/tcaseoverlaprange2.nim b/tests/casestmt/tcaseoverlaprange2.nim new file mode 100644 index 000000000..d6e301508 --- /dev/null +++ b/tests/casestmt/tcaseoverlaprange2.nim @@ -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) diff --git a/tests/casestmt/tcasestm.nim b/tests/casestmt/tcasestm.nim new file mode 100644 index 000000000..003ec6e50 --- /dev/null +++ b/tests/casestmt/tcasestm.nim @@ -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 + + + diff --git a/tests/casestmt/tcomputedgoto.nim b/tests/casestmt/tcomputedgoto.nim new file mode 100644 index 000000000..b21fc07a3 --- /dev/null +++ b/tests/casestmt/tcomputedgoto.nim @@ -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() diff --git a/tests/casestmt/tlinearscanend.nim b/tests/casestmt/tlinearscanend.nim new file mode 100644 index 000000000..15fd0c70a --- /dev/null +++ b/tests/casestmt/tlinearscanend.nim @@ -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" + |