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
This commit is contained in:
Miran 2018-10-16 10:50:10 +02:00 • committed by Andreas Rumpf
commit 749dbce4c6
96 changed files with 1763 additions and 1815 deletions

View file

@ -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)

View file

@ -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)

185
tests/overload/tissues.nim Normal file
View file

@ -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)

View file

@ -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

View file

@ -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)

View file

@ -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")

View file

@ -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

View file

@ -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"

View file

@ -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)

View file

@ -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

View file

@ -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"))

View file

@ -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

View file

@ -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
))

176
tests/overload/tvarious.nim Normal file
View file

@ -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"))

View file

@ -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)