diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/ccgbugs/tunsafeaddr.nim | 19 | ||||
-rw-r--r-- | tests/destructor/tdestructor3.nim | 47 | ||||
-rw-r--r-- | tests/exception/tdefer1.nim | 27 | ||||
-rw-r--r-- | tests/generics/mclosed_sym.nim | 10 | ||||
-rw-r--r-- | tests/generics/mmodule_same_as_proc.nim | 2 | ||||
-rw-r--r-- | tests/generics/tclosed_sym.nim | 11 | ||||
-rw-r--r-- | tests/generics/tdictdestruct.nim (renamed from tests/destructor/tdictdestruct.nim) | 0 | ||||
-rw-r--r-- | tests/generics/tdont_use_inner_scope.nim | 27 | ||||
-rw-r--r-- | tests/generics/tmodule_same_as_proc.nim | 9 | ||||
-rw-r--r-- | tests/stdlib/tmemlines.nim | 5 | ||||
-rw-r--r-- | tests/stdlib/tmemlinesBuf.nim | 6 | ||||
-rw-r--r-- | tests/stdlib/tmemslices.nim | 6 | ||||
-rw-r--r-- | tests/stdlib/tstrutil.nim | 48 | ||||
-rw-r--r-- | tests/stdlib/tunittest.nim | 5 | ||||
-rw-r--r-- | tests/template/twhen_gensym.nim | 13 | ||||
-rw-r--r-- | tests/vm/tforwardproc.nim | 17 | ||||
-rw-r--r-- | tests/vm/tstaticprintseq.nim | 12 |
17 files changed, 258 insertions, 6 deletions
diff --git a/tests/ccgbugs/tunsafeaddr.nim b/tests/ccgbugs/tunsafeaddr.nim new file mode 100644 index 000000000..4f05c7c21 --- /dev/null +++ b/tests/ccgbugs/tunsafeaddr.nim @@ -0,0 +1,19 @@ +discard """ + output: '''12''' +""" + +{.emit: """ +long sum(long* a, long len) { + long i, result = 0; + for (i = 0; i < len; ++i) result += a[i]; + return result; +} +""".} + +proc sum(a: ptr int; len: int): int {.importc, nodecl.} + +proc main = + let foo = [8, 3, 1] + echo sum(unsafeAddr foo[0], foo.len) + +main() diff --git a/tests/destructor/tdestructor3.nim b/tests/destructor/tdestructor3.nim new file mode 100644 index 000000000..0968f1fd7 --- /dev/null +++ b/tests/destructor/tdestructor3.nim @@ -0,0 +1,47 @@ +discard """ + output: '''assign +destroy +destroy +destroy Foo: 5 +5 +destroy Foo: 123 +123''' +""" + +# bug #2821 +{.experimental.} + +type T = object + +proc `=`(lhs: var T, rhs: T) = + echo "assign" + +proc `=destroy`(v: var T) = + echo "destroy" + +block: + var v1 : T + var v2 : T = v1 + + +# bug #1632 + +type + Foo = object of RootObj + x: int + +proc `=destroy`(a: var Foo) = + echo "destroy Foo: " & $a.x + +template toFooPtr(a: int{lit}): ptr Foo = + var temp = Foo(x:a) + temp.addr + +proc test(a: ptr Foo) = + echo a[].x + +proc main = + test(toFooPtr(5)) + test(toFooPtr(123)) + +main() diff --git a/tests/exception/tdefer1.nim b/tests/exception/tdefer1.nim index 61439530a..cb3d09b01 100644 --- a/tests/exception/tdefer1.nim +++ b/tests/exception/tdefer1.nim @@ -1,6 +1,11 @@ discard """ output: '''hi -hi''' +hi +1 +hi +2 +B +A''' """ # bug #1742 @@ -16,3 +21,23 @@ import strutils let x = try: parseInt("133a") except: -1 finally: echo "hi" + + +template atFuncEnd = + defer: + echo "A" + defer: + echo "B" + +template testB(): expr = + let a = 0 + defer: echo "hi" # Delete this line to make it work + a + +proc main = + atFuncEnd() + echo 1 + let i = testB() + echo 2 + +main() diff --git a/tests/generics/mclosed_sym.nim b/tests/generics/mclosed_sym.nim new file mode 100644 index 000000000..bcccd9a85 --- /dev/null +++ b/tests/generics/mclosed_sym.nim @@ -0,0 +1,10 @@ + +type R* = object + +type Data*[T] = object + d*: T + +proc same(r:R, d:int) = echo "TEST2" + +proc doIt*(d:Data, r:R) = + r.same(1) # Expecting this to invoke the local `same()` method diff --git a/tests/generics/mmodule_same_as_proc.nim b/tests/generics/mmodule_same_as_proc.nim new file mode 100644 index 000000000..048b98336 --- /dev/null +++ b/tests/generics/mmodule_same_as_proc.nim @@ -0,0 +1,2 @@ + +proc mmodule_same_as_proc*(x: string) = discard diff --git a/tests/generics/tclosed_sym.nim b/tests/generics/tclosed_sym.nim new file mode 100644 index 000000000..ff620c267 --- /dev/null +++ b/tests/generics/tclosed_sym.nim @@ -0,0 +1,11 @@ +discard """ + output: "TEST2" +""" + +# bug #2664 + +import mclosed_sym + +proc same(r:R, d:int) = echo "TEST1" + +doIt(Data[int](d:123), R()) diff --git a/tests/destructor/tdictdestruct.nim b/tests/generics/tdictdestruct.nim index 17ded4853..17ded4853 100644 --- a/tests/destructor/tdictdestruct.nim +++ b/tests/generics/tdictdestruct.nim diff --git a/tests/generics/tdont_use_inner_scope.nim b/tests/generics/tdont_use_inner_scope.nim new file mode 100644 index 000000000..45b11fc22 --- /dev/null +++ b/tests/generics/tdont_use_inner_scope.nim @@ -0,0 +1,27 @@ + +# bug #2752 + +import future, sequtils + +proc myFilter[T](it: (iterator(): T), f: (proc(anything: T):bool)): (iterator(): T) = + iterator aNameWhichWillConflict(): T {.closure.}= + for x in it(): + if f(x): + yield x + result = aNameWhichWillConflict + + +iterator testIt():int {.closure.}= + yield -1 + yield 2 + +#let unusedVariable = myFilter(testIt, (x: int) => x > 0) + +proc onlyPos(it: (iterator(): int)): (iterator(): int)= + iterator aNameWhichWillConflict(): int {.closure.}= + var filtered = onlyPos(myFilter(it, (x:int) => x > 0)) + for x in filtered(): + yield x + result = aNameWhichWillConflict + +let x = onlyPos(testIt) diff --git a/tests/generics/tmodule_same_as_proc.nim b/tests/generics/tmodule_same_as_proc.nim new file mode 100644 index 000000000..113ca1bc3 --- /dev/null +++ b/tests/generics/tmodule_same_as_proc.nim @@ -0,0 +1,9 @@ + +# bug #1965 + +import mmodule_same_as_proc + +proc test[T](t: T) = + mmodule_same_as_proc"a" + +test(0) diff --git a/tests/stdlib/tmemlines.nim b/tests/stdlib/tmemlines.nim new file mode 100644 index 000000000..19821ea26 --- /dev/null +++ b/tests/stdlib/tmemlines.nim @@ -0,0 +1,5 @@ +import memfiles +var inp = memfiles.open("readme.txt") +for line in lines(inp): + echo("#" & line & "#") +close(inp) diff --git a/tests/stdlib/tmemlinesBuf.nim b/tests/stdlib/tmemlinesBuf.nim new file mode 100644 index 000000000..21edc2322 --- /dev/null +++ b/tests/stdlib/tmemlinesBuf.nim @@ -0,0 +1,6 @@ +import memfiles +var inp = memfiles.open("readme.txt") +var buffer: TaintedString = "" +for line in lines(inp, buffer): + echo("#" & line & "#") +close(inp) diff --git a/tests/stdlib/tmemslices.nim b/tests/stdlib/tmemslices.nim new file mode 100644 index 000000000..951807cc4 --- /dev/null +++ b/tests/stdlib/tmemslices.nim @@ -0,0 +1,6 @@ +import memfiles +var inp = memfiles.open("readme.txt") +for mem in memSlices(inp): + if mem.size > 3: + echo("#" & $mem & "#") +close(inp) diff --git a/tests/stdlib/tstrutil.nim b/tests/stdlib/tstrutil.nim index 3db484faa..b15bf0e68 100644 --- a/tests/stdlib/tstrutil.nim +++ b/tests/stdlib/tstrutil.nim @@ -10,12 +10,52 @@ import proc testStrip() = write(stdout, strip(" ha ")) -proc main() = +proc testRemoveSuffix = + var s = "hello\n\r" + s.removeSuffix + assert s == "hello\n" + s.removeSuffix + assert s == "hello" + s.removeSuffix + assert s == "hello" + + s = "hello\n\n" + s.removeSuffix + assert s == "hello\n" + + s = "hello\r" + s.removeSuffix + assert s == "hello" + + s = "hello \n there" + s.removeSuffix + assert s == "hello \n there" + + s = "hello" + s.removeSuffix("llo") + assert s == "he" + s.removeSuffix('e') + assert s == "h" + + s = "hellos" + s.removeSuffix({'s','z'}) + assert s == "hello" + s.removeSuffix({'l','o'}) + assert s == "hell" + + # Contrary to Chomp in other languages + # empty string does not change behaviour + s = "hello\r\n\r\n" + s.removeSuffix("") + assert s == "hello\r\n\r\n" + +proc main() = testStrip() + testRemoveSuffix() for p in split("/home/a1:xyz:/usr/bin", {':'}): write(stdout, p) -proc testDelete = +proc testDelete = var s = "0123456789ABCDEFGH" delete(s, 4, 5) assert s == "01236789ABCDEFGH" @@ -24,8 +64,8 @@ proc testDelete = delete(s, 0, 0) assert s == "1236789ABCDEFG" -testDelete() - +testDelete() + assert(insertSep($1000_000) == "1_000_000") assert(insertSep($232) == "232") assert(insertSep($12345, ',') == "12,345") diff --git a/tests/stdlib/tunittest.nim b/tests/stdlib/tunittest.nim index fb9b02243..1389214ea 100644 --- a/tests/stdlib/tunittest.nim +++ b/tests/stdlib/tunittest.nim @@ -21,6 +21,11 @@ test "unittest typedescs": check(none(int) != some(1)) +test "unittest multiple requires": + require(true) + require(true) + + import math from strutils import parseInt proc defectiveRobot() = diff --git a/tests/template/twhen_gensym.nim b/tests/template/twhen_gensym.nim new file mode 100644 index 000000000..d84ee6f03 --- /dev/null +++ b/tests/template/twhen_gensym.nim @@ -0,0 +1,13 @@ +discard """ + output: "hi" +""" + +# bug #2670 +template testTemplate(b: bool): stmt = + when b: + var a = "hi" + else: + var a = 5 + echo a + +testTemplate(true) diff --git a/tests/vm/tforwardproc.nim b/tests/vm/tforwardproc.nim new file mode 100644 index 000000000..727ac6641 --- /dev/null +++ b/tests/vm/tforwardproc.nim @@ -0,0 +1,17 @@ +discard """ + errormsg: "cannot evaluate at compile time: initArray" + line: 11 +""" + +# bug #3066 + +proc initArray(): array[10, int] + +const + someTable = initArray() + +proc initArray(): array[10, int] = + for f in 0..<10: + result[f] = 3 + +when isMainModule: echo repr(someTable) diff --git a/tests/vm/tstaticprintseq.nim b/tests/vm/tstaticprintseq.nim index b002d366c..e4a6aa081 100644 --- a/tests/vm/tstaticprintseq.nim +++ b/tests/vm/tstaticprintseq.nim @@ -18,7 +18,8 @@ bb aa bb 24 -2147483647 2147483647''' +2147483647 2147483647 +5''' """ const s = @[1,2,3] @@ -80,3 +81,12 @@ static: static: var foo2 = int32.high echo foo2, " ", int32.high + +# bug #1329 + +static: + var a: ref int + new(a) + a[] = 5 + + echo a[] |