diff options
author | Miran <narimiran@users.noreply.github.com> | 2018-10-16 10:50:10 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2018-10-16 10:50:10 +0200 |
commit | 749dbce4c69224f5464908d8f714291f17aa60fa (patch) | |
tree | cc91326ef536f15d8d9aa97efab4fc8473d093c7 /tests/overload | |
parent | f04c93b5dd1ee5e185f6849ad8116d08a687026d (diff) | |
download | Nim-749dbce4c69224f5464908d8f714291f17aa60fa.tar.gz |
Merge tests into a larger file (part 5 of ∞) (#9368)
* merge magics * merge metatype tests * merge method tests * merge objects tests * change `import future` to `import sugar` Nim in Action tests are left with `import future`, to ensure compatibility. * merge overload tests * merge proc tests * merge procvar tests * merge range tests * merge seq tests * merge sets tests * remove wrong assert from `tsets3` * fix `jsTests` * better fix
Diffstat (limited to 'tests/overload')
-rw-r--r-- | tests/overload/timport.nim | 7 | ||||
-rw-r--r-- | tests/overload/tissue4475.nim | 6 | ||||
-rw-r--r-- | tests/overload/tissues.nim | 185 | ||||
-rw-r--r-- | tests/overload/toverl2.nim | 33 | ||||
-rw-r--r-- | tests/overload/toverl3.nim | 20 | ||||
-rw-r--r-- | tests/overload/toverprc.nim | 45 | ||||
-rw-r--r-- | tests/overload/toverwr.nim | 13 | ||||
-rw-r--r-- | tests/overload/tparams_after_varargs.nim | 19 | ||||
-rw-r--r-- | tests/overload/tprefer_specialized_generic.nim | 22 | ||||
-rw-r--r-- | tests/overload/tprefer_tygenericinst.nim | 68 | ||||
-rw-r--r-- | tests/overload/tstaticoverload.nim | 30 | ||||
-rw-r--r-- | tests/overload/tstmtoverload.nim | 38 | ||||
-rw-r--r-- | tests/overload/tsymtabchange_during_or.nim | 24 | ||||
-rw-r--r-- | tests/overload/tvarious.nim | 176 | ||||
-rw-r--r-- | tests/overload/tvart_varargs.nim | 18 |
15 files changed, 361 insertions, 343 deletions
diff --git a/tests/overload/timport.nim b/tests/overload/timport.nim deleted file mode 100644 index 8ea65e54d..000000000 --- a/tests/overload/timport.nim +++ /dev/null @@ -1,7 +0,0 @@ -# issue 4675 -import importA # comment this out to make it work -import importB - -var x: Foo[float] -var y: Foo[float] -let r = t1(x) + t2(y) diff --git a/tests/overload/tissue4475.nim b/tests/overload/tissue4475.nim deleted file mode 100644 index 34618cac5..000000000 --- a/tests/overload/tissue4475.nim +++ /dev/null @@ -1,6 +0,0 @@ -# Bug: https://github.com/nim-lang/Nim/issues/4475 -# Fix: https://github.com/nim-lang/Nim/pull/4477 - -proc test(x: varargs[string], y: int) = discard - -test(y = 1) diff --git a/tests/overload/tissues.nim b/tests/overload/tissues.nim new file mode 100644 index 000000000..7980f51a9 --- /dev/null +++ b/tests/overload/tissues.nim @@ -0,0 +1,185 @@ +discard """ + output: ''' +Version 2 was called. +This has the highest precedence. +This has the second-highest precedence. +This has the lowest precedence. +baseobj == +true +even better! == +true +done extraI=0 +test 0 complete, loops=0 +done extraI=1 +test 1.0 complete, loops=1 +done extraI=0 +done extraI passed 0 +test no extra complete, loops=2 +1 +''' +""" + + +# issue 4675 +import importA # comment this out to make it work +import importB + +var x: Foo[float] +var y: Foo[float] +let r = t1(x) + t2(y) + + + +# Bug: https://github.com/nim-lang/Nim/issues/4475 +# Fix: https://github.com/nim-lang/Nim/pull/4477 +proc test(x: varargs[string], y: int) = discard +test(y = 1) + + + +# bug #2220 +when true: + type A[T] = object + type B = A[int] + + proc q[X](x: X) = + echo "Version 1 was called." + + proc q(x: B) = + echo "Version 2 was called." + + q(B()) # This call reported as ambiguous. + + + +# bug #2219 +template testPred(a: untyped) = + block: + type A = object of RootObj + type B = object of A + type SomeA = A|A # A hack to make "A" a typeclass. + + when a >= 3: + proc p[X: A](x: X) = + echo "This has the highest precedence." + when a == 2: + proc p[X: SomeA](x: X) = + echo "This has the second-highest precedence." + when a >= 1: + proc p[X](x: X) = + echo "This has the lowest precedence." + + p(B()) + +testPred(3) +testPred(2) +testPred(1) + + + +# bug #6526 +type + BaseObj = ref object of RootObj + DerivedObj = ref object of BaseObj + OtherDerivate = ref object of BaseObj + +proc `==`*[T1, T2: BaseObj](a: T1, b: T2): bool = + echo "baseobj ==" + return true + +let a = DerivedObj() +let b = DerivedObj() +echo a == b + +proc `==`*[T1, T2: OtherDerivate](a: T1, b: T2): bool = + echo "even better! ==" + return true + +let a2 = OtherDerivate() +let b2 = OtherDerivate() +echo a2 == b2 + + + +# bug #2481 +import math + +template test(loopCount: int, extraI: int, testBody: untyped): typed = + block: + for i in 0..loopCount-1: + testBody + echo "done extraI=", extraI + +template test(loopCount: int, extraF: float, testBody: untyped): typed = + block: + test(loopCount, round(extraF).int, testBody) + +template test(loopCount: int, testBody: untyped): typed = + block: + test(loopCount, 0, testBody) + echo "done extraI passed 0" + +when isMainModule: + var + loops = 0 + + test 0, 0: + loops += 1 + echo "test 0 complete, loops=", loops + + test 1, 1.0: + loops += 1 + echo "test 1.0 complete, loops=", loops + + when true: + # when true we get the following compile time error: + # b.nim(35, 6) Error: expression 'loops += 1' has no type (or is ambiguous) + loops = 0 + test 2: + loops += 1 + echo "test no extra complete, loops=", loops + + + + +# bug #2229 +type + Type1 = object + id: int + Type2 = object + id: int + +proc init(self: var Type1, a: int, b: ref Type2) = + echo "1" + +proc init(self: var Type2, a: int) = + echo """ + Works when this proc commented out + Otherwise error: + test.nim(14, 4) Error: ambiguous call; both test.init(self: var Type1, a: int, b: ref Type2) and test.init(self: var Type1, a: int, b: ref Type2) match for: (Type1, int literal(1), ref Type2) + """ + +var aa: Type1 +init(aa, 1, ( + var bb = new(Type2); + bb +)) + + + +# bug #4545 +type + SomeObject = object + a: int + AbstractObject = object + objet: ptr SomeObject + +proc convert(this: var SomeObject): AbstractObject = + AbstractObject(objet: this.addr) + +proc varargProc(args: varargs[AbstractObject, convert]): int = + for arg in args: + result += arg.objet.a + +var obj = SomeObject(a: 17) +discard varargProc(obj) diff --git a/tests/overload/toverl2.nim b/tests/overload/toverl2.nim deleted file mode 100644 index 54714ac1b..000000000 --- a/tests/overload/toverl2.nim +++ /dev/null @@ -1,33 +0,0 @@ -discard """ - file: "toverl2.nim" - output: "true012innertrue" -""" -# Test new overloading resolution rules - -import strutils - -proc toverl2(x: int): string = return $x -proc toverl2(x: bool): string = return $x - -iterator toverl2(x: int): int = - var res = 0 - while res < x: - yield res - inc(res) - -var - pp: proc (x: bool): string {.nimcall.} = toverl2 - -stdout.write(pp(true)) - -for x in toverl2(3): - stdout.write(toverl2(x)) - -block: - proc toverl2(x: int): string = return "inner" - stdout.write(toverl2(5)) - stdout.write(true) - -stdout.write("\n") -#OUT true012innertrue - diff --git a/tests/overload/toverl3.nim b/tests/overload/toverl3.nim deleted file mode 100644 index 92cfc150d..000000000 --- a/tests/overload/toverl3.nim +++ /dev/null @@ -1,20 +0,0 @@ -discard """ - file: "toverl3.nim" - output: '''m1 -tup1''' -""" - -# Tests more specific generic match: - -proc m[T](x: T) = echo "m2" -proc m[T](x: var ref T) = echo "m1" - -proc tup[S, T](x: tuple[a: S, b: ref T]) = echo "tup1" -proc tup[S, T](x: tuple[a: S, b: T]) = echo "tup2" - -var - obj: ref int - tu: tuple[a: int, b: ref bool] - -m(obj) -tup(tu) diff --git a/tests/overload/toverprc.nim b/tests/overload/toverprc.nim deleted file mode 100644 index 9be2203f6..000000000 --- a/tests/overload/toverprc.nim +++ /dev/null @@ -1,45 +0,0 @@ -discard """ - output: '''another number: 123 -yay''' -""" - -# Test overloading of procs when used as function pointers - -import strutils, sequtils - -proc parseInt(x: float): int {.noSideEffect.} = discard -proc parseInt(x: bool): int {.noSideEffect.} = discard -proc parseInt(x: float32): int {.noSideEffect.} = discard -proc parseInt(x: int8): int {.noSideEffect.} = discard -proc parseInt(x: File): int {.noSideEffect.} = discard -proc parseInt(x: char): int {.noSideEffect.} = discard -proc parseInt(x: int16): int {.noSideEffect.} = discard - -proc parseInt[T](x: T): int = echo x; 34 - -type - TParseInt = proc (x: string): int {.noSideEffect.} - -var - q = TParseInt(parseInt) - p: TParseInt = parseInt - -proc takeParseInt(x: proc (y: string): int {.noSideEffect.}): int = - result = x("123") - -if false: - echo "Give a list of numbers (separated by spaces): " - var x = stdin.readline.split.map(parseInt).max - echo x, " is the maximum!" -echo "another number: ", takeParseInt(parseInt) - - -type - TFoo[a,b] = object - lorem: a - ipsum: b - -proc bar[a,b](f: TFoo[a,b], x: a) = echo(x, " ", f.lorem, f.ipsum) -proc bar[a,b](f: TFoo[a,b], x: b) = echo(x, " ", f.lorem, f.ipsum) - -discard parseInt[string]("yay") diff --git a/tests/overload/toverwr.nim b/tests/overload/toverwr.nim deleted file mode 100644 index b5791072e..000000000 --- a/tests/overload/toverwr.nim +++ /dev/null @@ -1,13 +0,0 @@ -discard """ - file: "toverwr.nim" - output: "hello" -""" -# Test the overloading resolution in connection with a qualifier - -proc write(t: File, s: string) = - discard # a nop - -system.write(stdout, "hello") -#OUT hello - - diff --git a/tests/overload/tparams_after_varargs.nim b/tests/overload/tparams_after_varargs.nim deleted file mode 100644 index ad8f19ad3..000000000 --- a/tests/overload/tparams_after_varargs.nim +++ /dev/null @@ -1,19 +0,0 @@ -discard """ - output: '''a 1 b 2 x @[3, 4, 5] y 6 z 7 -yay -12 -''' -""" - -proc test(a, b: int, x: varargs[int]; y, z: int) = - echo "a ", a, " b ", b, " x ", @x, " y ", y, " z ", z - -test 1, 2, 3, 4, 5, 6, 7 - -# XXX maybe this should also work with ``varargs[untyped]`` -template takesBlockA(a, b: untyped; x: varargs[typed]; blck: untyped): untyped = - blck - echo a, b - -takesBlockA 1, 2, "some", 0.90, "random stuff": - echo "yay" diff --git a/tests/overload/tprefer_specialized_generic.nim b/tests/overload/tprefer_specialized_generic.nim deleted file mode 100644 index 2b41502d1..000000000 --- a/tests/overload/tprefer_specialized_generic.nim +++ /dev/null @@ -1,22 +0,0 @@ -discard """ - output: '''ref ref T ptr S''' -""" - -proc foo[T](x: T) = - echo "only T" - -proc foo[T](x: ref T) = - echo "ref T" - -proc foo[T, S](x: ref ref T; y: ptr S) = - echo "ref ref T ptr S" - -proc foo[T, S](x: ref T; y: ptr S) = - echo "ref T ptr S" - -proc foo[T](x: ref T; default = 0) = - echo "ref T; default" - -var x: ref ref int -var y: ptr ptr int -foo(x, y) diff --git a/tests/overload/tprefer_tygenericinst.nim b/tests/overload/tprefer_tygenericinst.nim deleted file mode 100644 index ab461a0f4..000000000 --- a/tests/overload/tprefer_tygenericinst.nim +++ /dev/null @@ -1,68 +0,0 @@ -discard """ - output: '''Version 2 was called. -This has the highest precedence. -This has the second-highest precedence. -This has the lowest precedence. -baseobj == -true -even better! == -true''' -""" - -# bug #2220 -when true: - type A[T] = object - type B = A[int] - - proc q[X](x: X) = - echo "Version 1 was called." - - proc q(x: B) = - echo "Version 2 was called." - - q(B()) # This call reported as ambiguous. - -# bug #2219 -template testPred(a: untyped) = - block: - type A = object of RootObj - type B = object of A - type SomeA = A|A # A hack to make "A" a typeclass. - - when a >= 3: - proc p[X: A](x: X) = - echo "This has the highest precedence." - when a == 2: - proc p[X: SomeA](x: X) = - echo "This has the second-highest precedence." - when a >= 1: - proc p[X](x: X) = - echo "This has the lowest precedence." - - p(B()) - -testPred(3) -testPred(2) -testPred(1) - -# bug #6526 -type - BaseObj = ref object of RootObj - DerivedObj = ref object of BaseObj - OtherDerivate = ref object of BaseObj - -proc `==`*[T1, T2: BaseObj](a: T1, b: T2): bool = - echo "baseobj ==" - return true - -let a = DerivedObj() -let b = DerivedObj() -echo a == b - -proc `==`*[T1, T2: OtherDerivate](a: T1, b: T2): bool = - echo "even better! ==" - return true - -let a2 = OtherDerivate() -let b2 = OtherDerivate() -echo a2 == b2 diff --git a/tests/overload/tstaticoverload.nim b/tests/overload/tstaticoverload.nim deleted file mode 100644 index 33ca49e56..000000000 --- a/tests/overload/tstaticoverload.nim +++ /dev/null @@ -1,30 +0,0 @@ -discard """ -output: ''' -dynamic: let -dynamic: var -static: const -static: literal -static: constant folding -static: static string -''' -""" - -proc foo(s: string) = - echo "dynamic: ", s - -proc foo(s: static[string]) = - echo "static: ", s - -let l = "let" -var v = "var" -const c = "const" - -type staticString = static[string] - -foo(l) -foo(v) -foo(c) -foo("literal") -foo("constant" & " " & "folding") -foo(staticString("static string")) - diff --git a/tests/overload/tstmtoverload.nim b/tests/overload/tstmtoverload.nim deleted file mode 100644 index 7c0194e60..000000000 --- a/tests/overload/tstmtoverload.nim +++ /dev/null @@ -1,38 +0,0 @@ - -# bug #2481 -import math - -template test(loopCount: int, extraI: int, testBody: untyped): typed = - block: - for i in 0..loopCount-1: - testBody - echo "done extraI=", extraI - -template test(loopCount: int, extraF: float, testBody: untyped): typed = - block: - test(loopCount, round(extraF).int, testBody) - -template test(loopCount: int, testBody: untyped): typed = - block: - test(loopCount, 0, testBody) - echo "done extraI passed 0" - -when isMainModule: - var - loops = 0 - - test 0, 0: - loops += 1 - echo "test 0 complete, loops=", loops - - test 1, 1.0: - loops += 1 - echo "test 1.0 complete, loops=", loops - - when true: - # when true we get the following compile time error: - # b.nim(35, 6) Error: expression 'loops += 1' has no type (or is ambiguous) - loops = 0 - test 2: - loops += 1 - echo "test no extra complete, loops=", loops diff --git a/tests/overload/tsymtabchange_during_or.nim b/tests/overload/tsymtabchange_during_or.nim deleted file mode 100644 index b5551bcc7..000000000 --- a/tests/overload/tsymtabchange_during_or.nim +++ /dev/null @@ -1,24 +0,0 @@ - -# bug #2229 - -type Type1 = object - id: int - -type Type2 = object - id: int - -proc init(self: var Type1, a: int, b: ref Type2) = - echo "1" - -proc init(self: var Type2, a: int) = - echo """ - Works when this proc commented out - Otherwise error: - test.nim(14, 4) Error: ambiguous call; both test.init(self: var Type1, a: int, b: ref Type2) and test.init(self: var Type1, a: int, b: ref Type2) match for: (Type1, int literal(1), ref Type2) - """ - -var a: Type1 -init(a, 1, ( - var b = new(Type2); - b -)) diff --git a/tests/overload/tvarious.nim b/tests/overload/tvarious.nim new file mode 100644 index 000000000..4c17b6031 --- /dev/null +++ b/tests/overload/tvarious.nim @@ -0,0 +1,176 @@ +discard """ + output: ''' +true012innertrue +m1 +tup1 +another number: 123 +yay +helloa 1 b 2 x @[3, 4, 5] y 6 z 7 +yay +12 +ref ref T ptr S +dynamic: let +dynamic: var +static: const +static: literal +static: constant folding +static: static string +''' +""" + + +import strutils, sequtils + + +block overl2: + # Test new overloading resolution rules + proc toverl2(x: int): string = return $x + proc toverl2(x: bool): string = return $x + + iterator toverl2(x: int): int = + var res = 0 + while res < x: + yield res + inc(res) + + var + pp: proc (x: bool): string {.nimcall.} = toverl2 + + stdout.write(pp(true)) + + for x in toverl2(3): + stdout.write(toverl2(x)) + + block: + proc toverl2(x: int): string = return "inner" + stdout.write(toverl2(5)) + stdout.write(true) + + stdout.write("\n") + #OUT true012innertrue + + + +block overl3: + # Tests more specific generic match: + proc m[T](x: T) = echo "m2" + proc m[T](x: var ref T) = echo "m1" + proc tup[S, T](x: tuple[a: S, b: ref T]) = echo "tup1" + proc tup[S, T](x: tuple[a: S, b: T]) = echo "tup2" + + var + obj: ref int + tu: tuple[a: int, b: ref bool] + + m(obj) + tup(tu) + + + +block toverprc: + # Test overloading of procs when used as function pointers + proc parseInt(x: float): int {.noSideEffect.} = discard + proc parseInt(x: bool): int {.noSideEffect.} = discard + proc parseInt(x: float32): int {.noSideEffect.} = discard + proc parseInt(x: int8): int {.noSideEffect.} = discard + proc parseInt(x: File): int {.noSideEffect.} = discard + proc parseInt(x: char): int {.noSideEffect.} = discard + proc parseInt(x: int16): int {.noSideEffect.} = discard + + proc parseInt[T](x: T): int = echo x; 34 + + type + TParseInt = proc (x: string): int {.noSideEffect.} + + var + q = TParseInt(parseInt) + p: TParseInt = parseInt + + proc takeParseInt(x: proc (y: string): int {.noSideEffect.}): int = + result = x("123") + + if false: + echo "Give a list of numbers (separated by spaces): " + var x = stdin.readline.split.map(parseInt).max + echo x, " is the maximum!" + echo "another number: ", takeParseInt(parseInt) + + + type + TFoo[a,b] = object + lorem: a + ipsum: b + + proc bar[a,b](f: TFoo[a,b], x: a) = echo(x, " ", f.lorem, f.ipsum) + proc bar[a,b](f: TFoo[a,b], x: b) = echo(x, " ", f.lorem, f.ipsum) + + discard parseInt[string]("yay") + + + +block toverwr: + # Test the overloading resolution in connection with a qualifier + proc write(t: File, s: string) = + discard # a nop + system.write(stdout, "hello") + #OUT hello + + + +block tparams_after_varargs: + proc test(a, b: int, x: varargs[int]; y, z: int) = + echo "a ", a, " b ", b, " x ", @x, " y ", y, " z ", z + + test 1, 2, 3, 4, 5, 6, 7 + + # XXX maybe this should also work with ``varargs[untyped]`` + template takesBlockA(a, b: untyped; x: varargs[typed]; blck: untyped): untyped = + blck + echo a, b + + takesBlockA 1, 2, "some", 0.90, "random stuff": + echo "yay" + + + +block tprefer_specialized_generic: + proc foo[T](x: T) = + echo "only T" + + proc foo[T](x: ref T) = + echo "ref T" + + proc foo[T, S](x: ref ref T; y: ptr S) = + echo "ref ref T ptr S" + + proc foo[T, S](x: ref T; y: ptr S) = + echo "ref T ptr S" + + proc foo[T](x: ref T; default = 0) = + echo "ref T; default" + + var x: ref ref int + var y: ptr ptr int + foo(x, y) + + + +block tstaticoverload: + proc foo(s: string) = + echo "dynamic: ", s + + proc foo(s: static[string]) = + echo "static: ", s + + let l = "let" + var v = "var" + const c = "const" + + type staticString = static[string] + + foo(l) + foo(v) + foo(c) + foo("literal") + foo("constant" & " " & "folding") + foo(staticString("static string")) diff --git a/tests/overload/tvart_varargs.nim b/tests/overload/tvart_varargs.nim deleted file mode 100644 index c0c460c76..000000000 --- a/tests/overload/tvart_varargs.nim +++ /dev/null @@ -1,18 +0,0 @@ - -# bug #4545 -type SomeObject = object - a : int - -type AbstractObject = object - objet: ptr SomeObject - -proc convert(this: var SomeObject): AbstractObject = - AbstractObject(objet: this.addr) - -proc varargProc(args: varargs[AbstractObject, convert]): int = - for arg in args: - result += arg.objet.a - -var obj = SomeObject(a: 17) - -discard varargProc(obj) |