diff options
author | Dean Thompson <deansherthompson@gmail.com> | 2019-01-21 16:40:04 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-21 16:40:04 -0500 |
commit | a6de0274ee768d135bab280d2b2700a0bb475300 (patch) | |
tree | 176b91e837ab7d2217713665c16fc28163017960 /tests/vm | |
parent | 5b39c7aca91c1d20eb81425cf8f3854876aed475 (diff) | |
parent | ee89ba6bdb664fe4972f2917499cff1afdac0bab (diff) | |
download | Nim-a6de0274ee768d135bab280d2b2700a0bb475300.tar.gz |
Merge pull request #1 from nim-lang/devel
Getting the latest from nim-lang
Diffstat (limited to 'tests/vm')
-rw-r--r-- | tests/vm/tcompiletimesideeffects.nim | 42 | ||||
-rw-r--r-- | tests/vm/tgorge.nim | 2 | ||||
-rw-r--r-- | tests/vm/tissues.nim | 14 | ||||
-rw-r--r-- | tests/vm/treset.nim | 28 | ||||
-rw-r--r-- | tests/vm/tvmops.nim | 47 |
5 files changed, 125 insertions, 8 deletions
diff --git a/tests/vm/tcompiletimesideeffects.nim b/tests/vm/tcompiletimesideeffects.nim new file mode 100644 index 000000000..4cd57b3bd --- /dev/null +++ b/tests/vm/tcompiletimesideeffects.nim @@ -0,0 +1,42 @@ +discard """ + output: +''' +@[0, 1, 2] +@[3, 4, 5] +@[0, 1, 2] +3 +4 +''' +""" + +template runNTimes(n: int, f : untyped) : untyped = + var accum: seq[type(f)] + for i in 0..n-1: + accum.add(f) + accum + +var state {.compileTime.} : int = 0 +proc fill(): int {.compileTime.} = + result = state + inc state + +# invoke fill() at compile time as a compile time expression +const C1 = runNTimes(3, fill()) +echo C1 + +# invoke fill() at compile time as a set of compile time statements +const C2 = + block: + runNTimes(3, fill()) +echo C2 + +# invoke fill() at compile time after a compile time reset of state +const C3 = + block: + state = 0 + runNTimes(3, fill()) +echo C3 + +# evaluate fill() at compile time and use the results at runtime +echo fill() +echo fill() diff --git a/tests/vm/tgorge.nim b/tests/vm/tgorge.nim index 11c49a4cc..1f77d2c95 100644 --- a/tests/vm/tgorge.nim +++ b/tests/vm/tgorge.nim @@ -10,6 +10,8 @@ import os template getScriptDir(): string = parentDir(instantiationInfo(-1, true).filename) +# See also simpler test in Nim/tests/vm/tvmops.nim for a simpler +# cross platform way. block gorge: const execName = when defined(windows): "tgorge.bat" else: "./tgorge.sh" diff --git a/tests/vm/tissues.nim b/tests/vm/tissues.nim index 021b902ad..063559d2e 100644 --- a/tests/vm/tissues.nim +++ b/tests/vm/tissues.nim @@ -1,16 +1,14 @@ -discard """ - nimout: "(Field0: 2, Field1: 2, Field2: 2, Field3: 2)" -""" - import macros -block t9043: - proc foo[N: static[int]](dims: array[N, int])= +block t9043: # issue #9043 + proc foo[N: static[int]](dims: array[N, int]): string = const N1 = N const N2 = dims.len - static: echo (N, dims.len, N1, N2) + const ret = $(N, dims.len, N1, N2) + static: doAssert ret == $(N, dims.len, N1, N2) + ret - foo([1, 2]) + doAssert foo([1, 2]) == "(2, 2, 2, 2)" block t4952: proc doCheck(tree: NimNode) = diff --git a/tests/vm/treset.nim b/tests/vm/treset.nim new file mode 100644 index 000000000..56fe19b19 --- /dev/null +++ b/tests/vm/treset.nim @@ -0,0 +1,28 @@ +static: + type Obj = object + field: int + var o = Obj(field: 1) + reset(o) + doAssert o.field == 0 + +static: + var i = 2 + reset(i) + doAssert i == 0 + +static: + var i = new int + reset(i) + doAssert i.isNil + +static: + var s = @[1, 2, 3] + reset(s) + doAssert s == @[] + +static: + proc f() = + var i = 2 + reset(i) + doAssert i == 0 + f() \ No newline at end of file diff --git a/tests/vm/tvmops.nim b/tests/vm/tvmops.nim new file mode 100644 index 000000000..c9caaf32b --- /dev/null +++ b/tests/vm/tvmops.nim @@ -0,0 +1,47 @@ +#[ +test for vmops.nim +]# +import os +import math +import strutils + +template forceConst(a: untyped): untyped = + ## Force evaluation at CT, useful for example here: + ## `callFoo(forceConst(getBar1()), getBar2())` + ## instead of: + ## block: + ## const a = getBar1() + ## `callFoo(a, getBar2())` + const ret = a + ret + +static: + # TODO: add more tests + block: #getAppFilename, gorgeEx, gorge + const nim = getCurrentCompilerExe() + let ret = gorgeEx(nim & " --version") + doAssert ret.exitCode == 0 + doAssert ret.output.contains "Nim Compiler" + let ret2 = gorgeEx(nim & " --unexistant") + doAssert ret2.exitCode != 0 + let output3 = gorge(nim & " --version") + doAssert output3.contains "Nim Compiler" + + block: + const key = "D20181210T175037" + const val = "foo" + putEnv(key, val) + doAssert existsEnv(key) + doAssert getEnv(key) == val + + block: + # sanity check (we probably don't need to test for all ops) + const a1 = arcsin 0.3 + let a2 = arcsin 0.3 + doAssert a1 == a2 + +block: + # Check against bugs like #9176 + doAssert getCurrentCompilerExe() == forceConst(getCurrentCompilerExe()) + if false: #pending #9176 + doAssert gorgeEx("unexistant") == forceConst(gorgeEx("unexistant")) |