diff options
author | Miran <narimiran@disroot.org> | 2018-11-06 18:33:58 +0100 |
---|---|---|
committer | Arne Döring <arne.doering@gmx.net> | 2018-11-06 18:33:58 +0100 |
commit | 66a76d3165fba77b081f358dfa768b8a3203b7d8 (patch) | |
tree | 3245dafcb8746e023b678909c1cb36534562265d /tests/trmacros | |
parent | c735b75f6f9aa83d525c1cb819c8e6d83badc447 (diff) | |
download | Nim-66a76d3165fba77b081f358dfa768b8a3203b7d8.tar.gz |
Merge tests into a larger file (part 8 of ∞) (#9583)
* merge tuple tests * merge trmacros tests * merge template tests
Diffstat (limited to 'tests/trmacros')
-rw-r--r-- | tests/trmacros/targlist.nim | 9 | ||||
-rw-r--r-- | tests/trmacros/tcse.nim | 13 | ||||
-rw-r--r-- | tests/trmacros/thoist.nim | 13 | ||||
-rw-r--r-- | tests/trmacros/tmatrix.nim | 29 | ||||
-rw-r--r-- | tests/trmacros/tnoalias.nim | 16 | ||||
-rw-r--r-- | tests/trmacros/tnoalias2.nim | 19 | ||||
-rw-r--r-- | tests/trmacros/tnoendlessrec.nim | 10 | ||||
-rw-r--r-- | tests/trmacros/tpartial.nim | 11 | ||||
-rw-r--r-- | tests/trmacros/tpatterns.nim | 23 | ||||
-rw-r--r-- | tests/trmacros/trmacros_various.nim | 110 | ||||
-rw-r--r-- | tests/trmacros/trmacros_various2.nim | 79 | ||||
-rw-r--r-- | tests/trmacros/tstar.nim | 19 | ||||
-rw-r--r-- | tests/trmacros/tstatic_t_bug.nim | 24 |
13 files changed, 189 insertions, 186 deletions
diff --git a/tests/trmacros/targlist.nim b/tests/trmacros/targlist.nim deleted file mode 100644 index 46235dab1..000000000 --- a/tests/trmacros/targlist.nim +++ /dev/null @@ -1,9 +0,0 @@ -discard """ - output: "12false3ha" -""" - -proc f(x: varargs[string, `$`]) = discard -template optF{f(x)}(x: varargs[untyped]) = - writeLine(stdout, x) - -f 1, 2, false, 3, "ha" diff --git a/tests/trmacros/tcse.nim b/tests/trmacros/tcse.nim deleted file mode 100644 index 315570d8f..000000000 --- a/tests/trmacros/tcse.nim +++ /dev/null @@ -1,13 +0,0 @@ -discard """ - output: "4" -""" - -template cse{f(a, a, x)}(a: typed{(nkDotExpr|call|nkBracketExpr)&noSideEffect}, - f: typed, x: varargs[typed]): untyped = - let aa = a - f(aa, aa, x)+4 - -var - a: array[0..10, int] - i = 3 -echo a[i] + a[i] diff --git a/tests/trmacros/thoist.nim b/tests/trmacros/thoist.nim deleted file mode 100644 index 657f210a1..000000000 --- a/tests/trmacros/thoist.nim +++ /dev/null @@ -1,13 +0,0 @@ -discard """ - output: '''true -true''' -""" - -import pegs - -template optPeg{peg(pattern)}(pattern: string{lit}): Peg = - var gl {.global, gensym.} = peg(pattern) - gl - -echo match("(a b c)", peg"'(' @ ')'") -echo match("W_HI_Le", peg"\y 'while'") diff --git a/tests/trmacros/tmatrix.nim b/tests/trmacros/tmatrix.nim deleted file mode 100644 index a14ad2db0..000000000 --- a/tests/trmacros/tmatrix.nim +++ /dev/null @@ -1,29 +0,0 @@ -discard """ - output: "21" -""" - -import macros - -type - TMat = object - dummy: int - -proc `*`(a, b: TMat): TMat = nil -proc `+`(a, b: TMat): TMat = nil -proc `-`(a, b: TMat): TMat = nil -proc `$`(a: TMat): string = result = $a.dummy -proc mat21(): TMat = - result.dummy = 21 - -macro optOps{ (`+`|`-`|`*`) ** a }(a: TMat): untyped = - echo treeRepr(a) - result = newCall(bindSym"mat21") - -#macro optPlus{ `+` * a }(a: varargs[TMat]): expr = -# result = newIntLitNode(21) - -var x, y, z: TMat - -echo x + y * z - x - -#echo x + y + z diff --git a/tests/trmacros/tnoalias.nim b/tests/trmacros/tnoalias.nim deleted file mode 100644 index ec12d4712..000000000 --- a/tests/trmacros/tnoalias.nim +++ /dev/null @@ -1,16 +0,0 @@ -discard """ - output: "23" -""" - -template optslice{a = b + c}(a: untyped{noalias}, b, c: untyped): typed = - a = b - inc a, c - -var - x = 12 - y = 10 - z = 13 - -x = y+z - -echo x diff --git a/tests/trmacros/tnoalias2.nim b/tests/trmacros/tnoalias2.nim deleted file mode 100644 index 9362e764f..000000000 --- a/tests/trmacros/tnoalias2.nim +++ /dev/null @@ -1,19 +0,0 @@ -discard """ - output: '''0''' -""" - -# bug #206 -template optimizeOut{testFunc(a, b)}(a: int, b: int{alias}): untyped = 0 - -proc testFunc(a, b: int): int = result = a + b -var testVar = 1 -echo testFunc(testVar, testVar) - - -template ex{a = b + c}(a : int{noalias}, b, c : int) = - a = b - inc a, b - echo "came here" - -var x = 5 -x = x + x diff --git a/tests/trmacros/tnoendlessrec.nim b/tests/trmacros/tnoendlessrec.nim deleted file mode 100644 index 508770ca7..000000000 --- a/tests/trmacros/tnoendlessrec.nim +++ /dev/null @@ -1,10 +0,0 @@ -discard """ - output: "4" -""" - -# test that an endless recursion is avoided: - -template optLen{len(x)}(x: typed): int = len(x) - -var s = "lala" -echo len(s) diff --git a/tests/trmacros/tpartial.nim b/tests/trmacros/tpartial.nim deleted file mode 100644 index c636684d7..000000000 --- a/tests/trmacros/tpartial.nim +++ /dev/null @@ -1,11 +0,0 @@ -discard """ - output: '''-2''' -""" - -proc p(x, y: int; cond: bool): int = - result = if cond: x + y else: x - y - -template optP{p(x, y, true)}(x, y): untyped = x - y -template optP{p(x, y, false)}(x, y): untyped = x + y - -echo p(2, 4, true) diff --git a/tests/trmacros/tpatterns.nim b/tests/trmacros/tpatterns.nim deleted file mode 100644 index 907973637..000000000 --- a/tests/trmacros/tpatterns.nim +++ /dev/null @@ -1,23 +0,0 @@ -discard """ - output: '''48 -hel -lo''' -""" - -template optZero{x+x}(x: int): int = x*3 -template andthen{`*`(x,3)}(x: int): int = x*4 -template optSubstr1{x = substr(x, a, b)}(x: string, a, b: int) = setlen(x, b+1) - -var y = 12 -echo y+y - -var s: array[0..2, string] -s[0] = "hello" -s[0] = substr(s[0], 0, 2) - -echo s[0] - -# Test varargs matching -proc someVarargProc(k: varargs[string]) = doAssert(false) # this should not get called -template someVarargProcSingleArg{someVarargProc([a])}(a: string) = echo a -someVarargProc("lo") diff --git a/tests/trmacros/trmacros_various.nim b/tests/trmacros/trmacros_various.nim new file mode 100644 index 000000000..74b248739 --- /dev/null +++ b/tests/trmacros/trmacros_various.nim @@ -0,0 +1,110 @@ +discard """ +output: ''' +12false3ha +21 +optimized +''' +""" + +import macros, pegs + + +block arglist: + proc f(x: varargs[string, `$`]) = discard + template optF{f(x)}(x: varargs[untyped]) = + writeLine(stdout, x) + + f 1, 2, false, 3, "ha" + + + +block tcse: + template cse{f(a, a, x)}(a: typed{(nkDotExpr|call|nkBracketExpr)&noSideEffect}, + f: typed, x: varargs[typed]): untyped = + let aa = a + f(aa, aa, x)+4 + + var + a: array[0..10, int] + i = 3 + doAssert a[i] + a[i] == 4 + + + +block hoist: + template optPeg{peg(pattern)}(pattern: string{lit}): Peg = + var gl {.global, gensym.} = peg(pattern) + gl + doAssert match("(a b c)", peg"'(' @ ')'") + doAssert match("W_HI_Le", peg"\y 'while'") + + + +block tmatrix: + type + TMat = object + dummy: int + + proc `*`(a, b: TMat): TMat = nil + proc `+`(a, b: TMat): TMat = nil + proc `-`(a, b: TMat): TMat = nil + proc `$`(a: TMat): string = result = $a.dummy + proc mat21(): TMat = + result.dummy = 21 + + macro optOps{ (`+`|`-`|`*`) ** a }(a: TMat): untyped = + result = newCall(bindSym"mat21") + + #macro optPlus{ `+` * a }(a: varargs[TMat]): expr = + # result = newIntLitNode(21) + + var x, y, z: TMat + echo x + y * z - x + + + +block tnoalias: + template optslice{a = b + c}(a: untyped{noalias}, b, c: untyped): typed = + a = b + inc a, c + var + x = 12 + y = 10 + z = 13 + x = y+z + doAssert x == 23 + + + +block tnoendlessrec: + # test that an endless recursion is avoided: + template optLen{len(x)}(x: typed): int = len(x) + + var s = "lala" + doAssert len(s) == 4 + + + +block tstatic_t_bug: + # bug #4227 + type Vector64[N: static[int]] = array[N, int] + + proc `*`[N: static[int]](a: Vector64[N]; b: float64): Vector64[N] = + result = a + + proc `+=`[N: static[int]](a: var Vector64[N]; b: Vector64[N]) = + echo "regular" + + proc linearCombinationMut[N: static[int]](a: float64, v: var Vector64[N], w: Vector64[N]) {. inline .} = + echo "optimized" + + template rewriteLinearCombinationMut{v += `*`(w, a)}(a: float64, v: var Vector64, w: Vector64): auto = + linearCombinationMut(a, v, w) + + proc main() = + const scaleVal = 9.0 + var a, b: Vector64[7] + a += b * scaleval + + main() + diff --git a/tests/trmacros/trmacros_various2.nim b/tests/trmacros/trmacros_various2.nim new file mode 100644 index 000000000..d500c49de --- /dev/null +++ b/tests/trmacros/trmacros_various2.nim @@ -0,0 +1,79 @@ +discard """ +output: ''' +0 +-2 +48 +hel +lo +my awesome concat +''' +""" + + +block tnoalias2: + # bug #206 + template optimizeOut{testFunc(a, b)}(a: int, b: int{alias}): untyped = 0 + + proc testFunc(a, b: int): int = result = a + b + var testVar = 1 + echo testFunc(testVar, testVar) + + + template ex{a = b + c}(a : int{noalias}, b, c : int) = + a = b + inc a, b + echo "came here" + + var x = 5 + x = x + x + + + +block tpartial: + proc p(x, y: int; cond: bool): int = + result = if cond: x + y else: x - y + + template optP{p(x, y, true)}(x, y): untyped = x - y + template optP{p(x, y, false)}(x, y): untyped = x + y + + echo p(2, 4, true) + + + +block tpatterns: + template optZero{x+x}(x: int): int = x*3 + template andthen{`*`(x,3)}(x: int): int = x*4 + template optSubstr1{x = substr(x, a, b)}(x: string, a, b: int) = setlen(x, b+1) + + var y = 12 + echo y+y + + var s: array[0..2, string] + s[0] = "hello" + s[0] = substr(s[0], 0, 2) + + echo s[0] + + # Test varargs matching + proc someVarargProc(k: varargs[string]) = doAssert(false) # this should not get called + template someVarargProcSingleArg{someVarargProc([a])}(a: string) = echo a + someVarargProc("lo") + + + +block tstar: + var + calls = 0 + + proc `&&`(s: varargs[string]): string = + result = s[0] + for i in 1..len(s)-1: result.add s[i] + inc calls + + template optConc{ `&&` * a }(a: string): string = &&a + + let space = " " + echo "my" && (space & "awe" && "some " ) && "concat" + + # check that it's been optimized properly: + doAssert calls == 1 diff --git a/tests/trmacros/tstar.nim b/tests/trmacros/tstar.nim deleted file mode 100644 index 86f698232..000000000 --- a/tests/trmacros/tstar.nim +++ /dev/null @@ -1,19 +0,0 @@ -discard """ - output: "my awesome concat" -""" - -var - calls = 0 - -proc `&&`(s: varargs[string]): string = - result = s[0] - for i in 1..len(s)-1: result.add s[i] - inc calls - -template optConc{ `&&` * a }(a: string): string = &&a - -let space = " " -echo "my" && (space & "awe" && "some " ) && "concat" - -# check that it's been optimized properly: -doAssert calls == 1 diff --git a/tests/trmacros/tstatic_t_bug.nim b/tests/trmacros/tstatic_t_bug.nim deleted file mode 100644 index cdfa53514..000000000 --- a/tests/trmacros/tstatic_t_bug.nim +++ /dev/null @@ -1,24 +0,0 @@ -discard """ - output: "optimized" -""" -# bug #4227 -type Vector64[N: static[int]] = array[N, int] - -proc `*`*[N: static[int]](a: Vector64[N]; b: float64): Vector64[N] = - result = a - -proc `+=`*[N: static[int]](a: var Vector64[N]; b: Vector64[N]) = - echo "regular" - -proc linearCombinationMut[N: static[int]](a: float64, v: var Vector64[N], w: Vector64[N]) {. inline .} = - echo "optimized" - -template rewriteLinearCombinationMut*{v += `*`(w, a)}(a: float64, v: var Vector64, w: Vector64): auto = - linearCombinationMut(a, v, w) - -proc main() = - const scaleVal = 9.0 - var a, b: Vector64[7] - a += b * scaleval - -main() |