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:
parent
f04c93b5dd
commit
749dbce4c6
96 changed files with 1763 additions and 1815 deletions
|
|
@ -1,22 +0,0 @@
|
|||
discard """
|
||||
output: "void"
|
||||
"""
|
||||
|
||||
# bug #898
|
||||
|
||||
import typetraits
|
||||
|
||||
proc measureTime(e: auto) =
|
||||
echo e.type.name
|
||||
|
||||
proc generate(a: int): void =
|
||||
discard
|
||||
|
||||
proc runExample =
|
||||
var builder: int = 0
|
||||
|
||||
measureTime:
|
||||
builder.generate()
|
||||
|
||||
measureTime:
|
||||
discard
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
discard """
|
||||
output: '''(Field0: "string", Field1: "string")'''
|
||||
"""
|
||||
|
||||
# 7528
|
||||
import macros
|
||||
import typetraits
|
||||
|
||||
macro bar*(n: untyped): typed =
|
||||
result = newNimNode(nnkStmtList, n)
|
||||
result.add(newCall("write", newIdentNode("stdout"), n))
|
||||
|
||||
proc foo0[T](): auto = return (T.name, T.name)
|
||||
bar foo0[string]()
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
discard """
|
||||
output: '''ok'''
|
||||
"""
|
||||
|
||||
# bug #5638
|
||||
|
||||
type X = object
|
||||
a_impl: int
|
||||
|
||||
proc a(x: X): int =
|
||||
x.a_impl
|
||||
|
||||
var x: X
|
||||
assert(not compiles((block:
|
||||
x.a = 1
|
||||
)))
|
||||
|
||||
echo "ok"
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
|
||||
|
||||
proc myGenericProc[T: object|tuple|int|ptr|ref|distinct](x: T): string =
|
||||
result = $x
|
||||
|
||||
type
|
||||
TMyObj = tuple[x, y: int]
|
||||
|
||||
var
|
||||
x: TMyObj
|
||||
|
||||
assert myGenericProc(232) == "232"
|
||||
assert myGenericProc(x) == "(x: 0, y: 0)"
|
||||
|
||||
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
type
|
||||
Test = object
|
||||
x: int
|
||||
case p: bool
|
||||
of true:
|
||||
a: int
|
||||
else:
|
||||
case q: bool
|
||||
of true:
|
||||
b: int
|
||||
else:
|
||||
discard
|
||||
|
||||
proc f[T](t: typedesc[T]): int =
|
||||
1
|
||||
|
||||
assert Test.f == 1
|
||||
150
tests/metatype/tissues.nim
Normal file
150
tests/metatype/tissues.nim
Normal file
|
|
@ -0,0 +1,150 @@
|
|||
|
||||
|
||||
import typetraits, macros
|
||||
|
||||
|
||||
block t898:
|
||||
proc measureTime(e: auto) =
|
||||
echo e.type.name
|
||||
|
||||
proc generate(a: int): void =
|
||||
discard
|
||||
|
||||
proc runExample =
|
||||
var builder: int = 0
|
||||
|
||||
measureTime:
|
||||
builder.generate()
|
||||
|
||||
measureTime:
|
||||
discard
|
||||
|
||||
|
||||
|
||||
block t7528:
|
||||
macro bar(n: untyped): typed =
|
||||
result = newNimNode(nnkStmtList, n)
|
||||
result.add(newCall("write", newIdentNode("stdout"), n))
|
||||
|
||||
proc foo0[T](): auto = return (T.name, T.name)
|
||||
bar foo0[string]()
|
||||
echo ""
|
||||
|
||||
|
||||
|
||||
block t5638:
|
||||
type X = object
|
||||
a_impl: int
|
||||
|
||||
proc a(x: X): int =
|
||||
x.a_impl
|
||||
|
||||
var x: X
|
||||
assert(not compiles((block:
|
||||
x.a = 1
|
||||
)))
|
||||
|
||||
|
||||
|
||||
block t3706:
|
||||
type Modulo[M: static[int]] = distinct int
|
||||
proc modulo(a: int, M: static[int]): Modulo[M] = Modulo[M](a %% M)
|
||||
proc `+`[M: static[int]](a, b: Modulo[M]): Modulo[M] = (a.int + b.int).modulo(M)
|
||||
proc `$`[M: static[int]](a: Modulo[M]): string = $(a.int) & " mod " & $(M)
|
||||
|
||||
let
|
||||
a = 3.modulo(7)
|
||||
b = 5.modulo(7)
|
||||
echo a + b
|
||||
|
||||
|
||||
|
||||
block t3144:
|
||||
type IntArray[N: static[int]] = array[N, int]
|
||||
|
||||
proc `$`(a: IntArray): string = $(@(a))
|
||||
|
||||
proc `+=`[N: static[int]](a: var IntArray[N], b: IntArray[N]) =
|
||||
for i in 0 ..< N:
|
||||
a[i] += b[i]
|
||||
|
||||
proc zeros(N: static[int]): IntArray[N] =
|
||||
for i in 0 ..< N:
|
||||
result[i] = 0
|
||||
|
||||
proc ones(N: static[int]): IntArray[N] =
|
||||
for i in 0 ..< N:
|
||||
result[i] = 1
|
||||
|
||||
proc sum[N: static[int]](vs: seq[IntArray[N]]): IntArray[N] =
|
||||
result = zeros(N)
|
||||
for v in vs:
|
||||
result += v
|
||||
|
||||
echo sum(@[ones(5), ones(5)])
|
||||
|
||||
|
||||
|
||||
block t6533:
|
||||
type Value[T: static[int]] = typedesc
|
||||
proc foo(order: Value[1]): auto = 0
|
||||
doAssert foo(Value[1]) == 0
|
||||
|
||||
|
||||
|
||||
block t2266:
|
||||
proc impl(op: static[int]) = echo "impl 1 called"
|
||||
proc impl(op: static[int], init: int) = echo "impl 2 called"
|
||||
|
||||
macro wrapper2: untyped = newCall(bindSym"impl", newLit(0), newLit(0))
|
||||
|
||||
wrapper2() # Code generation for this fails.
|
||||
|
||||
|
||||
|
||||
block t602:
|
||||
type
|
||||
TTest = object
|
||||
TTest2 = object
|
||||
TFoo = TTest | TTest2
|
||||
|
||||
proc f(src: ptr TFoo, dst: ptr TFoo) =
|
||||
echo("asd")
|
||||
|
||||
var x: TTest
|
||||
f(addr x, addr x)
|
||||
|
||||
|
||||
|
||||
block t3338:
|
||||
type
|
||||
Base[T] = Foo[T] | Bar[T]
|
||||
|
||||
Foo[T] = ref object
|
||||
x: T
|
||||
|
||||
Bar[T] = ref object
|
||||
x: T
|
||||
|
||||
proc test[T](ks: Foo[T], x, y: T): T =
|
||||
echo("Foo")
|
||||
return x + y + ks.x
|
||||
|
||||
proc test[T](ks: Bar[T], x, y: T): T =
|
||||
echo("Bar")
|
||||
return x
|
||||
|
||||
proc add[T](ksa: Base[T]) =
|
||||
var test = ksa.test(5, 10)
|
||||
ksa.x = test
|
||||
|
||||
var t1 = Foo[int32]()
|
||||
t1.add()
|
||||
doAssert t1.x == 15
|
||||
|
||||
var t2 = Bar[int32]()
|
||||
t2.add()
|
||||
doAssert t2.x == 5
|
||||
|
||||
|
||||
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
discard """
|
||||
output: '''1 mod 7'''
|
||||
"""
|
||||
|
||||
# bug #3706
|
||||
|
||||
type Modulo[M: static[int]] = distinct int
|
||||
|
||||
proc modulo(a: int, M: static[int]): Modulo[M] = Modulo[M](a %% M)
|
||||
|
||||
proc `+`[M: static[int]](a, b: Modulo[M]): Modulo[M] = (a.int + b.int).modulo(M)
|
||||
|
||||
proc `$`*[M: static[int]](a: Modulo[M]): string = $(a.int) & " mod " & $(M)
|
||||
|
||||
when isMainModule:
|
||||
let
|
||||
a = 3.modulo(7)
|
||||
b = 5.modulo(7)
|
||||
echo a + b
|
||||
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
|
||||
proc myFun[A](x: A): auto =
|
||||
result = float(x+10)
|
||||
|
||||
proc myMap[T,S](sIn: seq[T], f: proc (q: T): S): seq[S] =
|
||||
result = newSeq[S](sIn.len)
|
||||
for i in 0..<sIn.len:
|
||||
result[i] = f(sIn[i])
|
||||
|
||||
assert myMap(@[1,2,3], myFun) == @[11.0, 12.0, 13.0]
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
discard """
|
||||
output: '''@[2, 2, 2, 2, 2]
|
||||
0'''
|
||||
"""
|
||||
|
||||
# bug #3144
|
||||
|
||||
type IntArray[N: static[int]] = array[N, int]
|
||||
|
||||
proc `$`(a: IntArray): string = $(@(a))
|
||||
|
||||
proc `+=`[N: static[int]](a: var IntArray[N], b: IntArray[N]) =
|
||||
for i in 0 .. < N:
|
||||
a[i] += b[i]
|
||||
|
||||
proc zeros(N: static[int]): IntArray[N] =
|
||||
for i in 0 .. < N:
|
||||
result[i] = 0
|
||||
|
||||
proc ones(N: static[int]): IntArray[N] =
|
||||
for i in 0 .. < N:
|
||||
result[i] = 1
|
||||
|
||||
proc sum[N: static[int]](vs: seq[IntArray[N]]): IntArray[N] =
|
||||
result = zeros(N)
|
||||
for v in vs:
|
||||
result += v
|
||||
|
||||
echo sum(@[ones(5), ones(5)])
|
||||
|
||||
# bug #6533
|
||||
type Value[T: static[int]] = typedesc
|
||||
proc foo(order: Value[1]): auto = 0
|
||||
echo foo(Value[1])
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
# bug #2266
|
||||
|
||||
import macros
|
||||
|
||||
proc impl(op: static[int]) = echo "impl 1 called"
|
||||
proc impl(op: static[int], init: int) = echo "impl 2 called"
|
||||
|
||||
macro wrapper2: untyped = newCall(bindSym"impl", newLit(0), newLit(0))
|
||||
|
||||
wrapper2() # Code generation for this fails.
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
|
||||
# bug #602
|
||||
|
||||
type
|
||||
TTest = object
|
||||
TTest2* = object
|
||||
TFoo = TTest | TTest2
|
||||
|
||||
proc f(src: ptr TFoo, dst: ptr TFoo) =
|
||||
echo("asd")
|
||||
|
||||
var x: TTest
|
||||
f(addr x, addr x)
|
||||
|
||||
|
|
@ -1,35 +0,0 @@
|
|||
discard """
|
||||
output: '''Foo
|
||||
Bar'''
|
||||
"""
|
||||
|
||||
# bug #3338
|
||||
|
||||
type
|
||||
Base[T] = Foo[T] | Bar[T]
|
||||
|
||||
Foo[T] = ref object
|
||||
x: T
|
||||
|
||||
Bar[T] = ref object
|
||||
x: T
|
||||
|
||||
proc test[T](ks: Foo[T], x, y: T): T =
|
||||
echo("Foo")
|
||||
return x + y + ks.x
|
||||
|
||||
proc test[T](ks: Bar[T], x, y: T): T =
|
||||
echo("Bar")
|
||||
return x
|
||||
|
||||
proc add[T](ksa: Base[T]) =
|
||||
var test = ksa.test(5, 10)
|
||||
ksa.x = test
|
||||
|
||||
var t1 = Foo[int32]()
|
||||
t1.add()
|
||||
doAssert t1.x == 15
|
||||
|
||||
var t2 = Bar[int32]()
|
||||
t2.add()
|
||||
doAssert t2.x == 5
|
||||
49
tests/metatype/tvarious.nim
Normal file
49
tests/metatype/tvarious.nim
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
|
||||
|
||||
block tconstraints:
|
||||
proc myGenericProc[T: object|tuple|int|ptr|ref|distinct](x: T): string =
|
||||
result = $x
|
||||
|
||||
type TMyObj = tuple[x, y: int]
|
||||
|
||||
var x: TMyObj
|
||||
|
||||
assert myGenericProc(232) == "232"
|
||||
assert myGenericProc(x) == "(x: 0, y: 0)"
|
||||
|
||||
|
||||
|
||||
block tfieldaccessor:
|
||||
type
|
||||
Test = object
|
||||
x: int
|
||||
case p: bool
|
||||
of true:
|
||||
a: int
|
||||
else:
|
||||
case q: bool
|
||||
of true:
|
||||
b: int
|
||||
else:
|
||||
discard
|
||||
|
||||
proc f[T](t: typedesc[T]): int =
|
||||
1
|
||||
|
||||
assert Test.f == 1
|
||||
|
||||
|
||||
|
||||
block tprocbothmeta:
|
||||
proc myFun[A](x: A): auto =
|
||||
result = float(x+10)
|
||||
|
||||
proc myMap[T,S](sIn: seq[T], f: proc (q: T): S): seq[S] =
|
||||
result = newSeq[S](sIn.len)
|
||||
for i in 0..<sIn.len:
|
||||
result[i] = f(sIn[i])
|
||||
|
||||
assert myMap(@[1,2,3], myFun) == @[11.0, 12.0, 13.0]
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue