diff options
Diffstat (limited to 'tests/overflow')
-rw-r--r-- | tests/overflow/tdistinct_range.nim | 6 | ||||
-rw-r--r-- | tests/overflow/toverflow.nim | 82 | ||||
-rw-r--r-- | tests/overflow/toverflow2.nim | 7 | ||||
-rw-r--r-- | tests/overflow/toverflow_reorder.nim | 84 | ||||
-rw-r--r-- | tests/overflow/tovfint.nim | 20 | ||||
-rw-r--r-- | tests/overflow/trangechecks.nim | 48 | ||||
-rw-r--r-- | tests/overflow/twronginference.nim | 10 |
7 files changed, 257 insertions, 0 deletions
diff --git a/tests/overflow/tdistinct_range.nim b/tests/overflow/tdistinct_range.nim new file mode 100644 index 000000000..f53515d45 --- /dev/null +++ b/tests/overflow/tdistinct_range.nim @@ -0,0 +1,6 @@ +discard """ + outputsub: "Error: unhandled exception: over- or underflow [OverflowDefect]" + exitcode: "1" +""" +var x: distinct range[0..5] +dec(x) \ No newline at end of file diff --git a/tests/overflow/toverflow.nim b/tests/overflow/toverflow.nim new file mode 100644 index 000000000..972f929c6 --- /dev/null +++ b/tests/overflow/toverflow.nim @@ -0,0 +1,82 @@ +discard """ + output: "ok" + matrix: "--overflowChecks:off; --overflowChecks:off --b:js" +""" +# Tests nim's ability to detect overflows + +{.push overflowChecks: on.} + +var + a = high(int) + b = -2 + overflowDetected = false + +try: + echo(b - a) +except OverflowDefect: + overflowDetected = true + +{.pop.} # overflow check + +doAssert(overflowDetected) + +block: # Overflow checks in a proc + var + a = high(int) + b = -2 + overflowDetected = false + + {.push overflowChecks: on.} + proc foo() = + let c = b - a + {.pop.} + + try: + foo() + except OverflowDefect: + overflowDetected = true + + doAssert(overflowDetected) + +block: # Overflow checks in a forward declared proc + var + a = high(int) + b = -2 + overflowDetected = false + + proc foo() + + {.push overflowChecks: on.} + proc foo() = + let c = b - a + {.pop.} + + try: + foo() + except OverflowDefect: + overflowDetected = true + + doAssert(overflowDetected) + +block: # Overflow checks doesn't affect fwd declaration + var + a = high(int) + b = -2 + overflowDetected = false + + {.push overflowChecks: on.} + proc foo() + {.pop.} + + proc foo() = + let c = b - a + + try: + foo() + except OverflowDefect: + overflowDetected = true + + doAssert(not overflowDetected) + + +echo "ok" diff --git a/tests/overflow/toverflow2.nim b/tests/overflow/toverflow2.nim new file mode 100644 index 000000000..c06b35c6d --- /dev/null +++ b/tests/overflow/toverflow2.nim @@ -0,0 +1,7 @@ +discard """ + outputsub: "Error: unhandled exception: over- or underflow [OverflowDefect]" + exitcode: "1" +""" +var a : int32 = 2147483647 +var b : int32 = 2147483647 +var c = a + b diff --git a/tests/overflow/toverflow_reorder.nim b/tests/overflow/toverflow_reorder.nim new file mode 100644 index 000000000..fcf7b0c82 --- /dev/null +++ b/tests/overflow/toverflow_reorder.nim @@ -0,0 +1,84 @@ +{.experimental: "codeReordering".} + +discard """ + output: "ok" + cmd: "nim $target --overflowChecks:off $options $file" +""" +# Tests nim's ability to detect overflows + +{.push overflowChecks: on.} + +var + a = high(int) + b = -2 + overflowDetected = false + +try: + echo b - a +except OverflowDefect: + overflowDetected = true + +{.pop.} # overflow check + +doAssert(overflowDetected) + +block: # Overflow checks in a proc + var + a = high(int) + b = -2 + overflowDetected = false + + {.push overflowChecks: on.} + proc foo() = + let c = b - a + {.pop.} + + try: + foo() + except OverflowDefect: + overflowDetected = true + + doAssert(overflowDetected) + +block: # Overflow checks in a forward declared proc + var + a = high(int) + b = -2 + overflowDetected = false + + proc foo() + + {.push overflowChecks: on.} + proc foo() = + let c = b - a + {.pop.} + + try: + foo() + except OverflowDefect: + overflowDetected = true + + doAssert(overflowDetected) + +block: # Overflow checks doesn't affect fwd declaration + var + a = high(int) + b = -2 + overflowDetected = false + + {.push overflowChecks: on.} + proc foo() + {.pop.} + + proc foo() = + let c = b - a + + try: + foo() + except OverflowDefect: + overflowDetected = true + + doAssert(not overflowDetected) + + +echo "ok" diff --git a/tests/overflow/tovfint.nim b/tests/overflow/tovfint.nim new file mode 100644 index 000000000..5c440a540 --- /dev/null +++ b/tests/overflow/tovfint.nim @@ -0,0 +1,20 @@ +discard """ + output: "works!" +""" +# this tests the new overflow literals + +var + i: int +i = int(0xffffffff'i32) +when defined(cpu64): + if i == -1: + write(stdout, "works!\n") + else: + write(stdout, "broken!\n") +else: + if i == -1: + write(stdout, "works!\n") + else: + write(stdout, "broken!\n") + +#OUT works! diff --git a/tests/overflow/trangechecks.nim b/tests/overflow/trangechecks.nim new file mode 100644 index 000000000..e48b1272b --- /dev/null +++ b/tests/overflow/trangechecks.nim @@ -0,0 +1,48 @@ +discard """ + output: '''10 +10 +1 +1 +true''' +""" + +# bug #1344 + +var expected: int +var x: range[1..10] = 10 + +try: + x += 1 + echo x +except OverflowDefect, RangeDefect: + expected += 1 + echo x + +try: + inc x + echo x +except OverflowDefect, RangeDefect: + expected += 1 + echo x + +x = 1 +try: + x -= 1 + echo x +except OverflowDefect, RangeDefect: + expected += 1 + echo x + +try: + dec x + echo x +except OverflowDefect, RangeDefect: + expected += 1 + echo x + +echo expected == 4 + +# bug #13698 +var + x45 = "hello".cstring + p = x45.len.int32 diff --git a/tests/overflow/twronginference.nim b/tests/overflow/twronginference.nim new file mode 100644 index 000000000..34a982976 --- /dev/null +++ b/tests/overflow/twronginference.nim @@ -0,0 +1,10 @@ +discard """ + errormsg: "cannot convert 256 to int8" + line: 9 +""" + +# issue #23177 + +var x: int8 +x = 256 +echo x # 0 |