diff options
Diffstat (limited to 'tests/compiles')
-rw-r--r-- | tests/compiles/mstaticlib.nim | 1 | ||||
-rw-r--r-- | tests/compiles/t8630.nim | 13 | ||||
-rw-r--r-- | tests/compiles/tcompiles.nim | 30 | ||||
-rw-r--r-- | tests/compiles/tevilcompiles.nim | 12 | ||||
-rw-r--r-- | tests/compiles/trecursive_generic_in_compiles.nim | 94 | ||||
-rw-r--r-- | tests/compiles/tstaticlib.nim | 22 |
6 files changed, 172 insertions, 0 deletions
diff --git a/tests/compiles/mstaticlib.nim b/tests/compiles/mstaticlib.nim new file mode 100644 index 000000000..6ed593691 --- /dev/null +++ b/tests/compiles/mstaticlib.nim @@ -0,0 +1 @@ +echo 1234 \ No newline at end of file diff --git a/tests/compiles/t8630.nim b/tests/compiles/t8630.nim new file mode 100644 index 000000000..8b447d061 --- /dev/null +++ b/tests/compiles/t8630.nim @@ -0,0 +1,13 @@ +discard """ + output: ''' +foo +bar +''' +""" + +proc test(strings: seq[string]) = + for s in strings: + var p3 = addr(s) + echo p3[] + +test(@["foo", "bar"]) diff --git a/tests/compiles/tcompiles.nim b/tests/compiles/tcompiles.nim new file mode 100644 index 000000000..1a21315c8 --- /dev/null +++ b/tests/compiles/tcompiles.nim @@ -0,0 +1,30 @@ +# test the new 'compiles' feature: + +template supports(opr, x: untyped): bool = + compiles(opr(x)) or compiles(opr(x, x)) + +template ok(x) = + static: + assert(x) + +template no(x) = + static: + assert(not x) + +type + TObj = object + +var + myObj {.compileTime.}: TObj + +ok supports(`==`, myObj) +ok supports(`==`, 45) + +no supports(`++`, 34) +ok supports(`not`, true) +ok supports(`+`, 34) + +no compiles(4+5.0 * "hallo") + +no compiles(undeclaredIdentifier) +no compiles(undeclaredIdentifier) diff --git a/tests/compiles/tevilcompiles.nim b/tests/compiles/tevilcompiles.nim new file mode 100644 index 000000000..0930507d1 --- /dev/null +++ b/tests/compiles/tevilcompiles.nim @@ -0,0 +1,12 @@ +# bug #1055 +import unittest +type TMatrix*[N,M: static[int], T] = object + data*: array[0..N*M-1, T] +proc `==`*(a: distinct TMatrix; b: distinct TMatrix): bool = + result = a.data == b.data + +test "c": + var a = TMatrix[2,2,int](data: [1,2,3,4]) + var b = TMatrix[2,2,int](data: [1,2,3,4]) + check(a == b) + diff --git a/tests/compiles/trecursive_generic_in_compiles.nim b/tests/compiles/trecursive_generic_in_compiles.nim new file mode 100644 index 000000000..ab31ad0f5 --- /dev/null +++ b/tests/compiles/trecursive_generic_in_compiles.nim @@ -0,0 +1,94 @@ +discard """ +action: compile +""" + +# bug #3313 +import unittest, sugar +{.experimental: "notnil".} +type + ListNodeKind = enum + lnkNil, lnkCons + List*[T] = ref object + ## List ADT + case kind: ListNodeKind + of lnkNil: + discard + of lnkCons: + value: T + next: List[T] not nil + +proc Cons*[T](head: T, tail: List[T]): List[T] = + ## Constructs non empty list + List[T](kind: lnkCons, value: head, next: tail) + +proc Nil*[T](): List[T] = + ## Constructs empty list + List[T](kind: lnkNil) + +proc head*[T](xs: List[T]): T = + ## Returns list's head + xs.value + +# TODO +# proc headOption*[T](xs: List[T]): Option[T] = ??? + +proc tail*[T](xs: List[T]): List[T] = + ## Returns list's tail + case xs.kind + of lnkCons: xs.next + else: xs + +proc isEmpty*(xs: List): bool = + ## Checks if list is empty + xs.kind == lnkNil + +proc `==`*[T](xs, ys: List[T]): bool = + ## Compares two lists + if (xs.isEmpty, ys.isEmpty) == (true, true): true + elif (xs.isEmpty, ys.isEmpty) == (false, false): xs.head == ys.head and xs.tail == ys.tail + else: false + +proc asList*[T](xs: varargs[T]): List[T] = + ## Creates list from varargs + proc initListImpl(i: int, xs: openArray[T]): List[T] = + if i > high(xs): + Nil[T]() + else: + Cons(xs[i], initListImpl(i+1, xs)) + initListImpl(0, xs) + +proc foldRight*[T,U](xs: List[T], z: U, f: (T, U) -> U): U = + case xs.isEmpty + of true: z + else: f(xs.head, xs.tail.foldRight(z, f)) + +proc dup*[T](xs: List[T]): List[T] = + ## Duplicates the list + xs.foldRight(Nil[T](), (x: T, xs: List[T]) => Cons(x, xs)) + +type + ListFormat = enum + lfADT, lfSTD + +proc asString[T](xs: List[T], f = lfSTD): string = + proc asAdt(xs: List[T]): string = + case xs.isEmpty + of true: "Nil" + else: "Cons(" & $xs.head & ", " & xs.tail.asAdt & ")" + + proc asStd(xs: List[T]): string = + "List(" & xs.foldLeft("", (s: string, v: T) => + (if s == "": $v else: s & ", " & $v)) & ")" + + case f + of lfADT: xs.asAdt + else: xs.asStd + +proc `$`*[T](xs: List[T]): string = + ## Converts list to string + result = xs.asString + +proc foldLeft*[T,U](xs: List[T], z: U, f: (U, T) -> U): U = + case xs.isEmpty + of true: z + else: foldLeft(xs.tail, f(z, xs.head), f) diff --git a/tests/compiles/tstaticlib.nim b/tests/compiles/tstaticlib.nim new file mode 100644 index 000000000..a18b59204 --- /dev/null +++ b/tests/compiles/tstaticlib.nim @@ -0,0 +1,22 @@ +import std/[os, osproc, strformat] + + +const dir = "tests/compiles" +const fileName = dir / "mstaticlib.nim" +const nim = getCurrentCompilerExe() + +block: # bug #18578 + const libName = dir / "tstaticlib1.a" + let (_, status) = execCmdEx(fmt"{nim} c -o:{libName} --app:staticlib {fileName}") + doAssert status == 0 + doAssert fileExists(libName) + removeFile(libName) + +block: # bug #16947 + const libName = dir / "tstaticlib2.a" + writeFile(libName, "echo 124") + doAssert fileExists(libName) + let (_, status) = execCmdEx(fmt"{nim} c -o:{libName} --app:staticlib {fileName}") + doAssert status == 0 + doAssert fileExists(libName) + removeFile(libName) |