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
|
|
@ -247,8 +247,7 @@ proc jsTests(r: var TResults, cat: Category, options: string) =
|
||||||
"exception/texcsub", "exception/tfinally",
|
"exception/texcsub", "exception/tfinally",
|
||||||
"exception/tfinally2", "exception/tfinally3",
|
"exception/tfinally2", "exception/tfinally3",
|
||||||
"exception/tunhandledexc",
|
"exception/tunhandledexc",
|
||||||
"actiontable/tactiontable", "method/tmultim1",
|
"actiontable/tactiontable", "method/tmultimjs",
|
||||||
"method/tmultim3", "method/tmultim4",
|
|
||||||
"varres/tvarres0", "varres/tvarres3", "varres/tvarres4",
|
"varres/tvarres0", "varres/tvarres3", "varres/tvarres4",
|
||||||
"varres/tvartup", "misc/tints", "misc/tunsignedinc",
|
"varres/tvartup", "misc/tints", "misc/tunsignedinc",
|
||||||
"async/tjsandnativeasync"]:
|
"async/tjsandnativeasync"]:
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ obj = (inner: (kind: Just, id: 7))
|
||||||
|
|
||||||
# bug #6960
|
# bug #6960
|
||||||
|
|
||||||
import future
|
import sugar
|
||||||
type
|
type
|
||||||
Kind = enum None, Just, Huge
|
Kind = enum None, Just, Huge
|
||||||
Inner = object
|
Inner = object
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ proc map[A, B](x: RandomVar[A], f: proc(a: A): B): ClosureVar[B] =
|
||||||
|
|
||||||
result.f = inner
|
result.f = inner
|
||||||
|
|
||||||
import future
|
import sugar
|
||||||
|
|
||||||
proc fakeRandom(): Random =
|
proc fakeRandom(): Random =
|
||||||
result.random = () => 0.5
|
result.random = () => 0.5
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ discard """
|
||||||
## because it's more efficient than using procedure pointers and less
|
## because it's more efficient than using procedure pointers and less
|
||||||
## verbose than defining a new callable type for every invocation of `map`.
|
## verbose than defining a new callable type for every invocation of `map`.
|
||||||
|
|
||||||
import future
|
import sugar
|
||||||
import macros
|
import macros
|
||||||
import strutils
|
import strutils
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
|
|
||||||
import sequtils, future
|
import sequtils, sugar
|
||||||
|
|
||||||
iterator permutations*[T](ys: openarray[T]): tuple[perm: seq[T], sign: int] =
|
iterator permutations*[T](ys: openarray[T]): tuple[perm: seq[T], sign: int] =
|
||||||
var
|
var
|
||||||
|
|
|
||||||
|
|
@ -1,35 +0,0 @@
|
||||||
discard """
|
|
||||||
output: '''true
|
|
||||||
false
|
|
||||||
true
|
|
||||||
false
|
|
||||||
false
|
|
||||||
true
|
|
||||||
true
|
|
||||||
false
|
|
||||||
true
|
|
||||||
true
|
|
||||||
false
|
|
||||||
true
|
|
||||||
true
|
|
||||||
false
|
|
||||||
'''
|
|
||||||
"""
|
|
||||||
|
|
||||||
type Foo = int | float
|
|
||||||
|
|
||||||
proc bar(t1, t2: typedesc): bool =
|
|
||||||
echo (t1 is t2)
|
|
||||||
(t2 is t1)
|
|
||||||
|
|
||||||
proc bar[T](x: T, t2: typedesc): bool =
|
|
||||||
echo (T is t2)
|
|
||||||
(t2 is T)
|
|
||||||
|
|
||||||
echo bar(int, Foo)
|
|
||||||
echo bar(4, Foo)
|
|
||||||
echo bar(any, int)
|
|
||||||
echo bar(int, any)
|
|
||||||
echo bar(Foo, Foo)
|
|
||||||
echo bar(any, Foo)
|
|
||||||
echo bar(Foo, any)
|
|
||||||
|
|
@ -1,24 +0,0 @@
|
||||||
discard """
|
|
||||||
file: "tlowhigh.nim"
|
|
||||||
output: "10"
|
|
||||||
"""
|
|
||||||
# Test the magic low() and high() procs
|
|
||||||
|
|
||||||
type
|
|
||||||
myEnum = enum e1, e2, e3, e4, e5
|
|
||||||
|
|
||||||
var
|
|
||||||
a: array[myEnum, int]
|
|
||||||
|
|
||||||
for i in low(a) .. high(a):
|
|
||||||
a[i] = 0
|
|
||||||
|
|
||||||
proc sum(a: openarray[int]): int =
|
|
||||||
result = 0
|
|
||||||
for i in low(a)..high(a):
|
|
||||||
inc(result, a[i])
|
|
||||||
|
|
||||||
write(stdout, sum([1, 2, 3, 4]))
|
|
||||||
#OUT 10
|
|
||||||
|
|
||||||
|
|
||||||
45
tests/magics/tmagics.nim
Normal file
45
tests/magics/tmagics.nim
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
discard """
|
||||||
|
output: '''
|
||||||
|
true
|
||||||
|
true
|
||||||
|
false
|
||||||
|
true
|
||||||
|
true
|
||||||
|
false
|
||||||
|
true
|
||||||
|
'''
|
||||||
|
"""
|
||||||
|
|
||||||
|
block tlowhigh:
|
||||||
|
type myEnum = enum e1, e2, e3, e4, e5
|
||||||
|
var a: array[myEnum, int]
|
||||||
|
|
||||||
|
for i in low(a) .. high(a):
|
||||||
|
a[i] = 0
|
||||||
|
|
||||||
|
proc sum(a: openarray[int]): int =
|
||||||
|
result = 0
|
||||||
|
for i in low(a)..high(a):
|
||||||
|
inc(result, a[i])
|
||||||
|
|
||||||
|
doAssert sum([1, 2, 3, 4]) == 10
|
||||||
|
|
||||||
|
|
||||||
|
block t8693:
|
||||||
|
type Foo = int | float
|
||||||
|
|
||||||
|
proc bar(t1, t2: typedesc): bool =
|
||||||
|
echo (t1 is t2)
|
||||||
|
(t2 is t1)
|
||||||
|
|
||||||
|
proc bar[T](x: T, t2: typedesc): bool =
|
||||||
|
echo (T is t2)
|
||||||
|
(t2 is T)
|
||||||
|
|
||||||
|
doAssert bar(int, Foo) == false
|
||||||
|
doAssert bar(4, Foo) == false
|
||||||
|
doAssert bar(any, int)
|
||||||
|
doAssert bar(int, any) == false
|
||||||
|
doAssert bar(Foo, Foo)
|
||||||
|
doAssert bar(any, Foo)
|
||||||
|
doAssert bar(Foo, any) == false
|
||||||
|
|
@ -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]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,24 +0,0 @@
|
||||||
discard """
|
|
||||||
output: '''wof!
|
|
||||||
wof!'''
|
|
||||||
"""
|
|
||||||
|
|
||||||
# bug #1659
|
|
||||||
type Animal = ref object {.inheritable.}
|
|
||||||
type Dog = ref object of Animal
|
|
||||||
|
|
||||||
method say(a: Animal): auto {.base.} = "wat!"
|
|
||||||
method say(a: Dog): auto = "wof!"
|
|
||||||
|
|
||||||
proc saySomething(a: Animal): auto = a.say()
|
|
||||||
|
|
||||||
|
|
||||||
method ec(a: Animal): auto {.base.} = echo "wat!"
|
|
||||||
method ec(a: Dog): auto = echo "wof!"
|
|
||||||
|
|
||||||
proc ech(a: Animal): auto = a.ec()
|
|
||||||
|
|
||||||
|
|
||||||
var a = Dog()
|
|
||||||
echo saySomething(a)
|
|
||||||
ech a
|
|
||||||
|
|
@ -1,11 +0,0 @@
|
||||||
# bug #2401
|
|
||||||
|
|
||||||
type MyClass = ref object of RootObj
|
|
||||||
|
|
||||||
method HelloWorld*(obj: MyClass) {.base.} =
|
|
||||||
when defined(myPragma):
|
|
||||||
echo("Hello World")
|
|
||||||
# discard # with this line enabled it works
|
|
||||||
|
|
||||||
var obj = MyClass()
|
|
||||||
obj.HelloWorld()
|
|
||||||
|
|
@ -1,15 +0,0 @@
|
||||||
#5432
|
|
||||||
type
|
|
||||||
Iterator[T] = ref object of RootObj
|
|
||||||
|
|
||||||
# base methods with `T` in the return type are okay
|
|
||||||
method methodThatWorks*[T](i: Iterator[T]): T {.base.} =
|
|
||||||
discard
|
|
||||||
|
|
||||||
# base methods without `T` (void or basic types) fail
|
|
||||||
method methodThatFails*[T](i: Iterator[T]) {.base.} =
|
|
||||||
discard
|
|
||||||
|
|
||||||
type
|
|
||||||
SpecificIterator1 = ref object of Iterator[string]
|
|
||||||
SpecificIterator2 = ref object of Iterator[int]
|
|
||||||
128
tests/method/tissues.nim
Normal file
128
tests/method/tissues.nim
Normal file
|
|
@ -0,0 +1,128 @@
|
||||||
|
discard """
|
||||||
|
output: '''
|
||||||
|
wof!
|
||||||
|
wof!
|
||||||
|
'''
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
# bug #1659
|
||||||
|
type Animal = ref object {.inheritable.}
|
||||||
|
type Dog = ref object of Animal
|
||||||
|
|
||||||
|
method say(a: Animal): auto {.base.} = "wat!"
|
||||||
|
method say(a: Dog): auto = "wof!"
|
||||||
|
|
||||||
|
proc saySomething(a: Animal): auto = a.say()
|
||||||
|
|
||||||
|
method ec(a: Animal): auto {.base.} = echo "wat!"
|
||||||
|
method ec(a: Dog): auto = echo "wof!"
|
||||||
|
|
||||||
|
proc ech(a: Animal): auto = a.ec()
|
||||||
|
|
||||||
|
var a = Dog()
|
||||||
|
echo saySomething(a)
|
||||||
|
ech a
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# bug #2401
|
||||||
|
type MyClass = ref object of RootObj
|
||||||
|
|
||||||
|
method HelloWorld*(obj: MyClass) {.base.} =
|
||||||
|
when defined(myPragma):
|
||||||
|
echo("Hello World")
|
||||||
|
# discard # with this line enabled it works
|
||||||
|
|
||||||
|
var obj = MyClass()
|
||||||
|
obj.HelloWorld()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# bug #5432
|
||||||
|
type
|
||||||
|
Iterator[T] = ref object of RootObj
|
||||||
|
|
||||||
|
# base methods with `T` in the return type are okay
|
||||||
|
method methodThatWorks*[T](i: Iterator[T]): T {.base.} =
|
||||||
|
discard
|
||||||
|
|
||||||
|
# base methods without `T` (void or basic types) fail
|
||||||
|
method methodThatFails*[T](i: Iterator[T]) {.base.} =
|
||||||
|
discard
|
||||||
|
|
||||||
|
type
|
||||||
|
SpecificIterator1 = ref object of Iterator[string]
|
||||||
|
SpecificIterator2 = ref object of Iterator[int]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# bug #3431
|
||||||
|
type
|
||||||
|
Lexer = object
|
||||||
|
buf*: string
|
||||||
|
pos*: int
|
||||||
|
lastchar*: char
|
||||||
|
|
||||||
|
ASTNode = object
|
||||||
|
|
||||||
|
method init*(self: var Lexer; buf: string) {.base.} =
|
||||||
|
self.buf = buf
|
||||||
|
self.pos = 0
|
||||||
|
self.lastchar = self.buf[0]
|
||||||
|
|
||||||
|
method init*(self: var ASTNode; val: string) =
|
||||||
|
discard
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# bug #3370
|
||||||
|
type
|
||||||
|
RefTestA*[T] = ref object of RootObj
|
||||||
|
data*: T
|
||||||
|
|
||||||
|
method tester*[S](self: S): bool =
|
||||||
|
true
|
||||||
|
|
||||||
|
type
|
||||||
|
RefTestB* = RefTestA[(string, int)]
|
||||||
|
|
||||||
|
method tester*(self: RefTestB): bool =
|
||||||
|
true
|
||||||
|
|
||||||
|
type
|
||||||
|
RefTestC = RefTestA[string]
|
||||||
|
|
||||||
|
method tester*(self: RefTestC): bool =
|
||||||
|
false
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# bug #3468
|
||||||
|
type X = ref object of RootObj
|
||||||
|
type Y = ref object of RootObj
|
||||||
|
|
||||||
|
method draw*(x: X) {.base.} = discard
|
||||||
|
method draw*(y: Y) {.base.} = discard
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# bug #3550
|
||||||
|
type
|
||||||
|
BaseClass = ref object of RootObj
|
||||||
|
Class1 = ref object of BaseClass
|
||||||
|
Class2 = ref object of BaseClass
|
||||||
|
|
||||||
|
method test(obj: Class1, obj2: BaseClass) =
|
||||||
|
discard
|
||||||
|
|
||||||
|
method test(obj: Class2, obj2: BaseClass) =
|
||||||
|
discard
|
||||||
|
|
||||||
|
var obj1 = Class1()
|
||||||
|
var obj2 = Class2()
|
||||||
|
|
||||||
|
obj1.test(obj2)
|
||||||
|
obj2.test(obj1)
|
||||||
|
|
@ -1,22 +0,0 @@
|
||||||
discard """
|
|
||||||
output: "do nothing"
|
|
||||||
"""
|
|
||||||
|
|
||||||
method somethin(obj: RootObj) {.base.} =
|
|
||||||
echo "do nothing"
|
|
||||||
|
|
||||||
type
|
|
||||||
TNode* = object {.inheritable.}
|
|
||||||
PNode* = ref TNode
|
|
||||||
|
|
||||||
PNodeFoo* = ref object of TNode
|
|
||||||
|
|
||||||
TSomethingElse = object
|
|
||||||
PSomethingElse = ref TSomethingElse
|
|
||||||
|
|
||||||
method foo(a: PNode, b: PSomethingElse) {.base.} = discard
|
|
||||||
method foo(a: PNodeFoo, b: PSomethingElse) = discard
|
|
||||||
|
|
||||||
var o: RootObj
|
|
||||||
o.somethin()
|
|
||||||
|
|
||||||
|
|
@ -1,25 +0,0 @@
|
||||||
type
|
|
||||||
Obj1 = ref object {.inheritable.}
|
|
||||||
Obj2 = ref object of Obj1
|
|
||||||
|
|
||||||
method beta(x: Obj1): int {.base.}
|
|
||||||
|
|
||||||
proc delta(x: Obj2): int =
|
|
||||||
beta(x)
|
|
||||||
|
|
||||||
method beta(x: Obj2): int
|
|
||||||
|
|
||||||
proc alpha(x: Obj1): int =
|
|
||||||
beta(x)
|
|
||||||
|
|
||||||
method beta(x: Obj1): int = 1
|
|
||||||
method beta(x: Obj2): int = 2
|
|
||||||
|
|
||||||
proc gamma(x: Obj1): int =
|
|
||||||
beta(x)
|
|
||||||
|
|
||||||
doAssert alpha(Obj1()) == 1
|
|
||||||
doAssert gamma(Obj1()) == 1
|
|
||||||
doAssert alpha(Obj2()) == 2
|
|
||||||
doAssert gamma(Obj2()) == 2
|
|
||||||
doAssert delta(Obj2()) == 2
|
|
||||||
93
tests/method/tmultim.nim
Normal file
93
tests/method/tmultim.nim
Normal file
|
|
@ -0,0 +1,93 @@
|
||||||
|
discard """
|
||||||
|
output: '''
|
||||||
|
collide: unit, thing
|
||||||
|
collide: unit, thing
|
||||||
|
collide: thing, unit
|
||||||
|
collide: thing, thing
|
||||||
|
collide: unit, thing | collide: unit, thing | collide: thing, unit |
|
||||||
|
do nothing
|
||||||
|
'''
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
# tmultim2
|
||||||
|
type
|
||||||
|
TThing = object {.inheritable.}
|
||||||
|
TUnit = object of TThing
|
||||||
|
x: int
|
||||||
|
TParticle = object of TThing
|
||||||
|
a, b: int
|
||||||
|
|
||||||
|
method collide(a, b: TThing) {.base, inline.} =
|
||||||
|
echo "collide: thing, thing"
|
||||||
|
|
||||||
|
method collide(a: TThing, b: TUnit) {.inline.} =
|
||||||
|
echo "collide: thing, unit"
|
||||||
|
|
||||||
|
method collide(a: TUnit, b: TThing) {.inline.} =
|
||||||
|
echo "collide: unit, thing"
|
||||||
|
|
||||||
|
proc test(a, b: TThing) {.inline.} =
|
||||||
|
collide(a, b)
|
||||||
|
|
||||||
|
proc staticCollide(a, b: TThing) {.inline.} =
|
||||||
|
procCall collide(a, b)
|
||||||
|
|
||||||
|
var
|
||||||
|
a: TThing
|
||||||
|
b, c: TUnit
|
||||||
|
collide(b, c) # ambiguous (unit, thing) or (thing, unit)? -> prefer unit, thing!
|
||||||
|
test(b, c)
|
||||||
|
collide(a, b)
|
||||||
|
staticCollide(a, b)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# tmultim6
|
||||||
|
type
|
||||||
|
Thing = object {.inheritable.}
|
||||||
|
Unit[T] = object of Thing
|
||||||
|
x: T
|
||||||
|
Particle = object of Thing
|
||||||
|
a, b: int
|
||||||
|
|
||||||
|
method collide(a, b: Thing) {.base, inline.} =
|
||||||
|
quit "to override!"
|
||||||
|
|
||||||
|
method collide[T](a: Thing, b: Unit[T]) {.inline.} =
|
||||||
|
write stdout, "collide: thing, unit | "
|
||||||
|
|
||||||
|
method collide[T](a: Unit[T], b: Thing) {.inline.} =
|
||||||
|
write stdout, "collide: unit, thing | "
|
||||||
|
|
||||||
|
proc test(a, b: Thing) {.inline.} =
|
||||||
|
collide(a, b)
|
||||||
|
|
||||||
|
var
|
||||||
|
aaa: Thing
|
||||||
|
bbb, ccc: Unit[string]
|
||||||
|
collide(bbb, Thing(ccc))
|
||||||
|
test(bbb, ccc)
|
||||||
|
collide(aaa, bbb)
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# tmethods1
|
||||||
|
method somethin(obj: RootObj) {.base.} =
|
||||||
|
echo "do nothing"
|
||||||
|
|
||||||
|
type
|
||||||
|
TNode* = object {.inheritable.}
|
||||||
|
PNode* = ref TNode
|
||||||
|
|
||||||
|
PNodeFoo* = ref object of TNode
|
||||||
|
|
||||||
|
TSomethingElse = object
|
||||||
|
PSomethingElse = ref TSomethingElse
|
||||||
|
|
||||||
|
method foo(a: PNode, b: PSomethingElse) {.base.} = discard
|
||||||
|
method foo(a: PNodeFoo, b: PSomethingElse) = discard
|
||||||
|
|
||||||
|
var o: RootObj
|
||||||
|
o.somethin()
|
||||||
|
|
@ -1,29 +0,0 @@
|
||||||
discard """
|
|
||||||
file: "tmultim1.nim"
|
|
||||||
output: "7"
|
|
||||||
"""
|
|
||||||
# Test multi methods
|
|
||||||
|
|
||||||
type
|
|
||||||
Expression = ref object {.inheritable.}
|
|
||||||
Literal = ref object of Expression
|
|
||||||
x: int
|
|
||||||
PlusExpr = ref object of Expression
|
|
||||||
a, b: Expression
|
|
||||||
|
|
||||||
method eval(e: Expression): int {.base.} = quit "to override!"
|
|
||||||
method eval(e: Literal): int = return e.x
|
|
||||||
method eval(e: PlusExpr): int = return eval(e.a) + eval(e.b)
|
|
||||||
|
|
||||||
proc newLit(x: int): Literal =
|
|
||||||
new(result)
|
|
||||||
result.x = x
|
|
||||||
|
|
||||||
proc newPlus(a, b: Expression): PlusExpr =
|
|
||||||
new(result)
|
|
||||||
result.a = a
|
|
||||||
result.b = b
|
|
||||||
|
|
||||||
echo eval(newPlus(newPlus(newLit(1), newLit(2)), newLit(4))) #OUT 7
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,39 +0,0 @@
|
||||||
discard """
|
|
||||||
file: "tmultim2.nim"
|
|
||||||
output: '''collide: unit, thing
|
|
||||||
collide: unit, thing
|
|
||||||
collide: thing, unit
|
|
||||||
collide: thing, thing'''
|
|
||||||
"""
|
|
||||||
# Test multi methods
|
|
||||||
|
|
||||||
type
|
|
||||||
TThing = object {.inheritable.}
|
|
||||||
TUnit = object of TThing
|
|
||||||
x: int
|
|
||||||
TParticle = object of TThing
|
|
||||||
a, b: int
|
|
||||||
|
|
||||||
method collide(a, b: TThing) {.base, inline.} =
|
|
||||||
echo "collide: thing, thing"
|
|
||||||
|
|
||||||
method collide(a: TThing, b: TUnit) {.inline.} =
|
|
||||||
echo "collide: thing, unit"
|
|
||||||
|
|
||||||
method collide(a: TUnit, b: TThing) {.inline.} =
|
|
||||||
echo "collide: unit, thing"
|
|
||||||
|
|
||||||
proc test(a, b: TThing) {.inline.} =
|
|
||||||
collide(a, b)
|
|
||||||
|
|
||||||
proc staticCollide(a, b: TThing) {.inline.} =
|
|
||||||
procCall collide(a, b)
|
|
||||||
|
|
||||||
|
|
||||||
var
|
|
||||||
a: TThing
|
|
||||||
b, c: TUnit
|
|
||||||
collide(b, c) # ambiguous (unit, thing) or (thing, unit)? -> prefer unit, thing!
|
|
||||||
test(b, c)
|
|
||||||
collide(a, b)
|
|
||||||
staticCollide(a, b)
|
|
||||||
|
|
@ -1,20 +0,0 @@
|
||||||
discard """
|
|
||||||
file: "tmultim3.nim"
|
|
||||||
output: "Hi derived!"
|
|
||||||
"""
|
|
||||||
import mmultim3
|
|
||||||
|
|
||||||
type
|
|
||||||
TBObj* = object of TObj
|
|
||||||
|
|
||||||
|
|
||||||
method test123(a : ref TBObj) =
|
|
||||||
echo("Hi derived!")
|
|
||||||
|
|
||||||
var a : ref TBObj
|
|
||||||
new(a)
|
|
||||||
myObj = a
|
|
||||||
testMyObj()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,47 +0,0 @@
|
||||||
discard """
|
|
||||||
file: "tmultim4.nim"
|
|
||||||
output: "hello"
|
|
||||||
"""
|
|
||||||
type
|
|
||||||
Test = object of RootObj
|
|
||||||
|
|
||||||
method doMethod(a: ref RootObj) {.base, raises: [IoError].} =
|
|
||||||
quit "override"
|
|
||||||
|
|
||||||
method doMethod(a: ref Test) =
|
|
||||||
echo "hello"
|
|
||||||
if a == nil:
|
|
||||||
raise newException(IoError, "arg")
|
|
||||||
|
|
||||||
proc doProc(a: ref Test) =
|
|
||||||
echo "hello"
|
|
||||||
|
|
||||||
proc newTest(): ref Test =
|
|
||||||
new(result)
|
|
||||||
|
|
||||||
var s:ref Test = newTest()
|
|
||||||
|
|
||||||
|
|
||||||
#doesn't work
|
|
||||||
for z in 1..4:
|
|
||||||
s.doMethod()
|
|
||||||
break
|
|
||||||
|
|
||||||
#works
|
|
||||||
#for z in 1..4:
|
|
||||||
# s.doProc()
|
|
||||||
# break
|
|
||||||
|
|
||||||
#works
|
|
||||||
#while true:
|
|
||||||
# s.doMethod()
|
|
||||||
# break
|
|
||||||
|
|
||||||
#works
|
|
||||||
#while true:
|
|
||||||
# s.doProc()
|
|
||||||
# break
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,30 +0,0 @@
|
||||||
discard """
|
|
||||||
output: "collide: unit, thing | collide: unit, thing | collide: thing, unit |"
|
|
||||||
"""
|
|
||||||
# Test multi methods
|
|
||||||
|
|
||||||
type
|
|
||||||
Thing = object {.inheritable.}
|
|
||||||
Unit[T] = object of Thing
|
|
||||||
x: T
|
|
||||||
Particle = object of Thing
|
|
||||||
a, b: int
|
|
||||||
|
|
||||||
method collide(a, b: Thing) {.base, inline.} =
|
|
||||||
quit "to override!"
|
|
||||||
|
|
||||||
method collide[T](a: Thing, b: Unit[T]) {.inline.} =
|
|
||||||
write stdout, "collide: thing, unit | "
|
|
||||||
|
|
||||||
method collide[T](a: Unit[T], b: Thing) {.inline.} =
|
|
||||||
write stdout, "collide: unit, thing | "
|
|
||||||
|
|
||||||
proc test(a, b: Thing) {.inline.} =
|
|
||||||
collide(a, b)
|
|
||||||
|
|
||||||
var
|
|
||||||
a: Thing
|
|
||||||
b, c: Unit[string]
|
|
||||||
collide(b, Thing(c))
|
|
||||||
test(b, c)
|
|
||||||
collide(a, b)
|
|
||||||
|
|
@ -1,48 +0,0 @@
|
||||||
|
|
||||||
# bug #3431
|
|
||||||
|
|
||||||
type
|
|
||||||
Lexer = object
|
|
||||||
buf*: string
|
|
||||||
pos*: int
|
|
||||||
lastchar*: char
|
|
||||||
|
|
||||||
ASTNode = object
|
|
||||||
|
|
||||||
method init*(self: var Lexer; buf: string) {.base.} =
|
|
||||||
self.buf = buf
|
|
||||||
self.pos = 0
|
|
||||||
self.lastchar = self.buf[0]
|
|
||||||
|
|
||||||
method init*(self: var ASTNode; val: string) =
|
|
||||||
discard
|
|
||||||
|
|
||||||
|
|
||||||
# bug #3370
|
|
||||||
type
|
|
||||||
RefTestA*[T] = ref object of RootObj
|
|
||||||
data*: T
|
|
||||||
|
|
||||||
method tester*[S](self: S): bool =
|
|
||||||
true
|
|
||||||
|
|
||||||
type
|
|
||||||
RefTestB* = RefTestA[(string, int)]
|
|
||||||
|
|
||||||
method tester*(self: RefTestB): bool =
|
|
||||||
true
|
|
||||||
|
|
||||||
type
|
|
||||||
RefTestC = RefTestA[string]
|
|
||||||
|
|
||||||
method tester*(self: RefTestC): bool =
|
|
||||||
false
|
|
||||||
|
|
||||||
|
|
||||||
# bug #3468
|
|
||||||
|
|
||||||
type X = ref object of RootObj
|
|
||||||
type Y = ref object of RootObj
|
|
||||||
|
|
||||||
method draw*(x: X) {.base.} = discard
|
|
||||||
method draw*(y: Y) {.base.} = discard
|
|
||||||
|
|
@ -1,19 +0,0 @@
|
||||||
|
|
||||||
# bug #3550
|
|
||||||
|
|
||||||
type
|
|
||||||
BaseClass = ref object of RootObj
|
|
||||||
Class1 = ref object of BaseClass
|
|
||||||
Class2 = ref object of BaseClass
|
|
||||||
|
|
||||||
method test(obj: Class1, obj2: BaseClass) =
|
|
||||||
discard
|
|
||||||
|
|
||||||
method test(obj: Class2, obj2: BaseClass) =
|
|
||||||
discard
|
|
||||||
|
|
||||||
var obj1 = Class1()
|
|
||||||
var obj2 = Class2()
|
|
||||||
|
|
||||||
obj1.test(obj2)
|
|
||||||
obj2.test(obj1)
|
|
||||||
72
tests/method/tmultimjs.nim
Normal file
72
tests/method/tmultimjs.nim
Normal file
|
|
@ -0,0 +1,72 @@
|
||||||
|
discard """
|
||||||
|
output: '''
|
||||||
|
7
|
||||||
|
Hi derived!
|
||||||
|
hello
|
||||||
|
'''
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
# tmultim1
|
||||||
|
type
|
||||||
|
Expression = ref object {.inheritable.}
|
||||||
|
Literal = ref object of Expression
|
||||||
|
x: int
|
||||||
|
PlusExpr = ref object of Expression
|
||||||
|
a, b: Expression
|
||||||
|
|
||||||
|
method eval(e: Expression): int {.base.} = quit "to override!"
|
||||||
|
method eval(e: Literal): int = return e.x
|
||||||
|
method eval(e: PlusExpr): int = return eval(e.a) + eval(e.b)
|
||||||
|
|
||||||
|
proc newLit(x: int): Literal =
|
||||||
|
new(result)
|
||||||
|
result.x = x
|
||||||
|
|
||||||
|
proc newPlus(a, b: Expression): PlusExpr =
|
||||||
|
new(result)
|
||||||
|
result.a = a
|
||||||
|
result.b = b
|
||||||
|
|
||||||
|
echo eval(newPlus(newPlus(newLit(1), newLit(2)), newLit(4))) #OUT 7
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# tmultim3
|
||||||
|
import mmultim3
|
||||||
|
|
||||||
|
type TBObj* = object of TObj
|
||||||
|
|
||||||
|
method test123(a : ref TBObj) =
|
||||||
|
echo("Hi derived!")
|
||||||
|
|
||||||
|
var aa: ref TBObj
|
||||||
|
new(aa)
|
||||||
|
myObj = aa
|
||||||
|
testMyObj()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# tmultim4
|
||||||
|
type Test = object of RootObj
|
||||||
|
|
||||||
|
method doMethod(a: ref RootObj) {.base, raises: [IoError].} =
|
||||||
|
quit "override"
|
||||||
|
|
||||||
|
method doMethod(a: ref Test) =
|
||||||
|
echo "hello"
|
||||||
|
if a == nil:
|
||||||
|
raise newException(IoError, "arg")
|
||||||
|
|
||||||
|
proc doProc(a: ref Test) =
|
||||||
|
echo "hello"
|
||||||
|
|
||||||
|
proc newTest(): ref Test =
|
||||||
|
new(result)
|
||||||
|
|
||||||
|
var s:ref Test = newTest()
|
||||||
|
|
||||||
|
#doesn't work
|
||||||
|
for z in 1..4:
|
||||||
|
s.doMethod()
|
||||||
|
break
|
||||||
|
|
@ -1,22 +0,0 @@
|
||||||
# Note: We only compile this to verify that code generation
|
|
||||||
# for recursive methods works, no code is being executed
|
|
||||||
|
|
||||||
type
|
|
||||||
Obj = ref object of RootObj
|
|
||||||
|
|
||||||
# Mutual recursion
|
|
||||||
|
|
||||||
method alpha(x: Obj) {.base.}
|
|
||||||
method beta(x: Obj) {.base.}
|
|
||||||
|
|
||||||
method alpha(x: Obj) =
|
|
||||||
beta(x)
|
|
||||||
|
|
||||||
method beta(x: Obj) =
|
|
||||||
alpha(x)
|
|
||||||
|
|
||||||
# Simple recursion
|
|
||||||
|
|
||||||
method gamma(x: Obj) {.base.} =
|
|
||||||
gamma(x)
|
|
||||||
|
|
||||||
|
|
@ -1,14 +0,0 @@
|
||||||
discard """
|
|
||||||
file: "tsimmeth.nim"
|
|
||||||
output: "HELLO WORLD!"
|
|
||||||
"""
|
|
||||||
# Test method simulation
|
|
||||||
|
|
||||||
import strutils
|
|
||||||
|
|
||||||
var x = "hello world!".toLowerAscii.toUpperAscii
|
|
||||||
x.echo()
|
|
||||||
#OUT HELLO WORLD!
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
83
tests/method/tvarious.nim
Normal file
83
tests/method/tvarious.nim
Normal file
|
|
@ -0,0 +1,83 @@
|
||||||
|
discard """
|
||||||
|
output: '''
|
||||||
|
do nothing
|
||||||
|
HELLO WORLD!
|
||||||
|
'''
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
# tmethods1
|
||||||
|
method somethin(obj: RootObj) {.base.} =
|
||||||
|
echo "do nothing"
|
||||||
|
|
||||||
|
type
|
||||||
|
TNode* = object {.inheritable.}
|
||||||
|
PNode* = ref TNode
|
||||||
|
|
||||||
|
PNodeFoo* = ref object of TNode
|
||||||
|
|
||||||
|
TSomethingElse = object
|
||||||
|
PSomethingElse = ref TSomethingElse
|
||||||
|
|
||||||
|
method foo(a: PNode, b: PSomethingElse) {.base.} = discard
|
||||||
|
method foo(a: PNodeFoo, b: PSomethingElse) = discard
|
||||||
|
|
||||||
|
var o: RootObj
|
||||||
|
o.somethin()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# tmproto
|
||||||
|
type
|
||||||
|
Obj1 = ref object {.inheritable.}
|
||||||
|
Obj2 = ref object of Obj1
|
||||||
|
|
||||||
|
method beta(x: Obj1): int {.base.}
|
||||||
|
|
||||||
|
proc delta(x: Obj2): int =
|
||||||
|
beta(x)
|
||||||
|
|
||||||
|
method beta(x: Obj2): int
|
||||||
|
|
||||||
|
proc alpha(x: Obj1): int =
|
||||||
|
beta(x)
|
||||||
|
|
||||||
|
method beta(x: Obj1): int = 1
|
||||||
|
method beta(x: Obj2): int = 2
|
||||||
|
|
||||||
|
proc gamma(x: Obj1): int =
|
||||||
|
beta(x)
|
||||||
|
|
||||||
|
doAssert alpha(Obj1()) == 1
|
||||||
|
doAssert gamma(Obj1()) == 1
|
||||||
|
doAssert alpha(Obj2()) == 2
|
||||||
|
doAssert gamma(Obj2()) == 2
|
||||||
|
doAssert delta(Obj2()) == 2
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# tsimmeth
|
||||||
|
import strutils
|
||||||
|
var x = "hello world!".toLowerAscii.toUpperAscii
|
||||||
|
x.echo()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# trecmeth
|
||||||
|
# Note: We only compile this to verify that code generation
|
||||||
|
# for recursive methods works, no code is being executed
|
||||||
|
type Obj = ref object of RootObj
|
||||||
|
|
||||||
|
# Mutual recursion
|
||||||
|
method alpha(x: Obj) {.base.}
|
||||||
|
method beta(x: Obj) {.base.}
|
||||||
|
|
||||||
|
method alpha(x: Obj) =
|
||||||
|
beta(x)
|
||||||
|
|
||||||
|
method beta(x: Obj) =
|
||||||
|
alpha(x)
|
||||||
|
|
||||||
|
# Simple recursion
|
||||||
|
method gamma(x: Obj) {.base.} =
|
||||||
|
gamma(x)
|
||||||
|
|
@ -1,53 +0,0 @@
|
||||||
discard """
|
|
||||||
output: '''tbObj of TC true
|
|
||||||
true
|
|
||||||
5'''
|
|
||||||
"""
|
|
||||||
|
|
||||||
# bug #1053
|
|
||||||
type
|
|
||||||
TA = object of RootObj
|
|
||||||
a: int
|
|
||||||
|
|
||||||
TB = object of TA
|
|
||||||
b: int
|
|
||||||
|
|
||||||
TC = object of TB
|
|
||||||
c: int
|
|
||||||
|
|
||||||
proc test(p: TA) =
|
|
||||||
#echo "p of TB ", p of TB
|
|
||||||
if p of TB:
|
|
||||||
#var tbObj = TB(p)
|
|
||||||
|
|
||||||
# tbObj is actually no longer compatible with TC:
|
|
||||||
echo "tbObj of TC ", p of TC
|
|
||||||
|
|
||||||
var v = TC()
|
|
||||||
v.a = 1
|
|
||||||
v.b = 2
|
|
||||||
v.c = 3
|
|
||||||
test(v)
|
|
||||||
|
|
||||||
|
|
||||||
# bug #924
|
|
||||||
type
|
|
||||||
MyObject = object of RootObj
|
|
||||||
x: int
|
|
||||||
|
|
||||||
var
|
|
||||||
asd: MyObject
|
|
||||||
|
|
||||||
proc isMyObject(obj: RootObj) =
|
|
||||||
echo obj of MyObject
|
|
||||||
if obj of MyObject:
|
|
||||||
let a = MyObject(obj)
|
|
||||||
echo a.x
|
|
||||||
|
|
||||||
asd.x = 5
|
|
||||||
|
|
||||||
#var asdCopy = RootObj(asd)
|
|
||||||
#echo asdCopy of MyObject
|
|
||||||
|
|
||||||
isMyObject(asd)
|
|
||||||
#isMyObject(asdCopy)
|
|
||||||
|
|
@ -1,13 +0,0 @@
|
||||||
discard """
|
|
||||||
output: '''true'''
|
|
||||||
"""
|
|
||||||
|
|
||||||
# bug #4673
|
|
||||||
type
|
|
||||||
BaseObj[T] = ref object of RootObj
|
|
||||||
SomeObj = ref object of BaseObj[int]
|
|
||||||
|
|
||||||
proc doSomething[T](o: BaseObj[T]) =
|
|
||||||
echo "true"
|
|
||||||
var o = new(SomeObj)
|
|
||||||
o.doSomething() # Error: cannot instantiate: 'T'
|
|
||||||
117
tests/objects/tissues.nim
Normal file
117
tests/objects/tissues.nim
Normal file
|
|
@ -0,0 +1,117 @@
|
||||||
|
discard """
|
||||||
|
output: '''
|
||||||
|
tbObj of TC true
|
||||||
|
true
|
||||||
|
5
|
||||||
|
true
|
||||||
|
is Nil false
|
||||||
|
'''
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
block t1053:
|
||||||
|
type
|
||||||
|
TA = object of RootObj
|
||||||
|
a: int
|
||||||
|
TB = object of TA
|
||||||
|
b: int
|
||||||
|
TC = object of TB
|
||||||
|
c: int
|
||||||
|
|
||||||
|
proc test(p: TA) =
|
||||||
|
if p of TB:
|
||||||
|
echo "tbObj of TC ", p of TC
|
||||||
|
|
||||||
|
var v = TC()
|
||||||
|
v.a = 1
|
||||||
|
v.b = 2
|
||||||
|
v.c = 3
|
||||||
|
test(v)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
block t924:
|
||||||
|
type
|
||||||
|
MyObject = object of RootObj
|
||||||
|
x: int
|
||||||
|
var asd: MyObject
|
||||||
|
|
||||||
|
proc isMyObject(obj: RootObj) =
|
||||||
|
echo obj of MyObject
|
||||||
|
if obj of MyObject:
|
||||||
|
let a = MyObject(obj)
|
||||||
|
echo a.x
|
||||||
|
|
||||||
|
asd.x = 5
|
||||||
|
isMyObject(asd)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
block t4673:
|
||||||
|
type
|
||||||
|
BaseObj[T] = ref object of RootObj
|
||||||
|
SomeObj = ref object of BaseObj[int]
|
||||||
|
|
||||||
|
proc doSomething[T](o: BaseObj[T]) =
|
||||||
|
echo "true"
|
||||||
|
var o = new(SomeObj)
|
||||||
|
o.doSomething() # Error: cannot instantiate: 'T'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
block t1658:
|
||||||
|
type
|
||||||
|
Loop = ref object
|
||||||
|
onBeforeSelect: proc (L: Loop)
|
||||||
|
|
||||||
|
var L: Loop
|
||||||
|
new L
|
||||||
|
L.onBeforeSelect = proc (bar: Loop) =
|
||||||
|
echo "is Nil ", bar.isNil
|
||||||
|
|
||||||
|
L.onBeforeSelect(L)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
block t2508:
|
||||||
|
type
|
||||||
|
GenericNodeObj[T] = ref object
|
||||||
|
obj: T
|
||||||
|
Node = ref object
|
||||||
|
children: seq[Node]
|
||||||
|
parent: Node
|
||||||
|
nodeObj: GenericNodeObj[int]
|
||||||
|
|
||||||
|
proc newNode(nodeObj: GenericNodeObj): Node =
|
||||||
|
result = Node(nodeObj: nodeObj)
|
||||||
|
newSeq(result.children, 10)
|
||||||
|
|
||||||
|
var genericObj = GenericNodeObj[int]()
|
||||||
|
var myNode = newNode(genericObj)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
block t2540:
|
||||||
|
type
|
||||||
|
BaseSceneNode[T] = ref object of RootObj
|
||||||
|
children: seq[BaseSceneNode[T]]
|
||||||
|
parent: BaseSceneNode[T]
|
||||||
|
SceneNode[T] = ref object of BaseSceneNode[T]
|
||||||
|
SomeObj = ref object
|
||||||
|
|
||||||
|
proc newSceneNode[T](): SceneNode[T] =
|
||||||
|
new result
|
||||||
|
result.children = @[]
|
||||||
|
|
||||||
|
var aNode = newSceneNode[SomeObj]()
|
||||||
|
|
||||||
|
|
||||||
|
block t3038:
|
||||||
|
type
|
||||||
|
Data[T] = ref object of RootObj
|
||||||
|
data: T
|
||||||
|
Type = ref object of RootObj
|
||||||
|
SubType[T] = ref object of Type
|
||||||
|
data: Data[T]
|
||||||
|
SubSubType = ref object of SubType
|
||||||
|
SubSubSubType = ref object of SubSubType
|
||||||
|
|
@ -1,21 +0,0 @@
|
||||||
# Tests the object implementation
|
|
||||||
|
|
||||||
type
|
|
||||||
TPoint2d {.inheritable.} = object
|
|
||||||
x, y: int
|
|
||||||
|
|
||||||
TPoint3d = object of TPoint2d
|
|
||||||
z: int # added a field
|
|
||||||
|
|
||||||
proc getPoint( p: var TPoint2d) =
|
|
||||||
{.breakpoint.}
|
|
||||||
writeLine(stdout, p.x)
|
|
||||||
|
|
||||||
var
|
|
||||||
p: TPoint3d
|
|
||||||
|
|
||||||
TPoint2d(p).x = 34
|
|
||||||
p.y = 98
|
|
||||||
p.z = 343
|
|
||||||
|
|
||||||
getPoint(p)
|
|
||||||
|
|
@ -1,15 +0,0 @@
|
||||||
discard """
|
|
||||||
output: "is Nil false"
|
|
||||||
"""
|
|
||||||
# bug #1658
|
|
||||||
|
|
||||||
type
|
|
||||||
Loop* = ref object
|
|
||||||
onBeforeSelect*: proc (L: Loop)
|
|
||||||
|
|
||||||
var L: Loop
|
|
||||||
new L
|
|
||||||
L.onBeforeSelect = proc (bar: Loop) =
|
|
||||||
echo "is Nil ", bar.isNil
|
|
||||||
|
|
||||||
L.onBeforeSelect(L)
|
|
||||||
|
|
@ -1,26 +0,0 @@
|
||||||
discard """
|
|
||||||
file: "tofopr.nim"
|
|
||||||
output: "falsetrue"
|
|
||||||
"""
|
|
||||||
# Test is operator
|
|
||||||
|
|
||||||
type
|
|
||||||
TMyType = object {.inheritable.}
|
|
||||||
len: int
|
|
||||||
data: string
|
|
||||||
|
|
||||||
TOtherType = object of TMyType
|
|
||||||
|
|
||||||
proc p(x: TMyType): bool =
|
|
||||||
return x of TOtherType
|
|
||||||
|
|
||||||
var
|
|
||||||
m: TMyType
|
|
||||||
n: TOtherType
|
|
||||||
|
|
||||||
write(stdout, p(m))
|
|
||||||
write(stdout, p(n))
|
|
||||||
|
|
||||||
#OUT falsetrue
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
||||||
discard """
|
|
||||||
output: "b"
|
|
||||||
"""
|
|
||||||
|
|
||||||
type
|
|
||||||
TA = object of RootObj
|
|
||||||
x, y: int
|
|
||||||
|
|
||||||
TB = object of TA
|
|
||||||
z: int
|
|
||||||
|
|
||||||
TC = object of TB
|
|
||||||
whatever: string
|
|
||||||
|
|
||||||
proc p(a: var TA) = echo "a"
|
|
||||||
proc p(b: var TB) = echo "b"
|
|
||||||
|
|
||||||
var c: TC
|
|
||||||
|
|
||||||
p(c)
|
|
||||||
|
|
||||||
|
|
@ -1,27 +0,0 @@
|
||||||
discard """
|
|
||||||
output: '''wohoo
|
|
||||||
baz'''
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Test to ensure the popular 'ref T' syntax works everywhere
|
|
||||||
|
|
||||||
type
|
|
||||||
Foo = object
|
|
||||||
a, b: int
|
|
||||||
s: string
|
|
||||||
|
|
||||||
FooBar = object of RootObj
|
|
||||||
n, m: string
|
|
||||||
Baz = object of FooBar
|
|
||||||
|
|
||||||
proc invoke(a: ref Baz) =
|
|
||||||
echo "baz"
|
|
||||||
|
|
||||||
# check object construction:
|
|
||||||
let x = (ref Foo)(a: 0, b: 45, s: "wohoo")
|
|
||||||
echo x.s
|
|
||||||
|
|
||||||
var y: ref FooBar = (ref Baz)(n: "n", m: "m")
|
|
||||||
|
|
||||||
invoke((ref Baz)(y))
|
|
||||||
|
|
||||||
|
|
@ -1,19 +0,0 @@
|
||||||
# bug #2508
|
|
||||||
|
|
||||||
type
|
|
||||||
GenericNodeObj[T] = ref object
|
|
||||||
obj: T
|
|
||||||
|
|
||||||
Node* = ref object
|
|
||||||
children*: seq[Node]
|
|
||||||
parent*: Node
|
|
||||||
|
|
||||||
nodeObj*: GenericNodeObj[int]
|
|
||||||
|
|
||||||
proc newNode*(nodeObj: GenericNodeObj): Node =
|
|
||||||
result = Node(nodeObj: nodeObj)
|
|
||||||
newSeq(result.children, 10)
|
|
||||||
|
|
||||||
var genericObj = GenericNodeObj[int]()
|
|
||||||
|
|
||||||
var myNode = newNode(genericObj)
|
|
||||||
|
|
@ -1,28 +0,0 @@
|
||||||
# bug #2540
|
|
||||||
|
|
||||||
type
|
|
||||||
BaseSceneNode[T] = ref object of RootObj
|
|
||||||
children*: seq[BaseSceneNode[T]]
|
|
||||||
parent*: BaseSceneNode[T]
|
|
||||||
|
|
||||||
SceneNode[T] = ref object of BaseSceneNode[T]
|
|
||||||
|
|
||||||
SomeObj = ref object
|
|
||||||
|
|
||||||
proc newSceneNode[T](): SceneNode[T] =
|
|
||||||
new result
|
|
||||||
result.children = @[]
|
|
||||||
|
|
||||||
var aNode = newSceneNode[SomeObj]()
|
|
||||||
|
|
||||||
|
|
||||||
# bug #3038
|
|
||||||
|
|
||||||
type
|
|
||||||
Data[T] = ref object of RootObj
|
|
||||||
data: T
|
|
||||||
Type = ref object of RootObj
|
|
||||||
SubType[T] = ref object of Type
|
|
||||||
data: Data[T]
|
|
||||||
SubSubType = ref object of SubType
|
|
||||||
SubSubSubType = ref object of SubSubType
|
|
||||||
87
tests/objects/tvarious.nim
Normal file
87
tests/objects/tvarious.nim
Normal file
|
|
@ -0,0 +1,87 @@
|
||||||
|
discard """
|
||||||
|
output: '''
|
||||||
|
34
|
||||||
|
b
|
||||||
|
wohoo
|
||||||
|
baz
|
||||||
|
'''
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
block tobject2:
|
||||||
|
# Tests the object implementation
|
||||||
|
type
|
||||||
|
TPoint2d {.inheritable.} = object
|
||||||
|
x, y: int
|
||||||
|
TPoint3d = object of TPoint2d
|
||||||
|
z: int # added a field
|
||||||
|
|
||||||
|
proc getPoint( p: var TPoint2d) =
|
||||||
|
{.breakpoint.}
|
||||||
|
writeLine(stdout, p.x)
|
||||||
|
|
||||||
|
var p: TPoint3d
|
||||||
|
|
||||||
|
TPoint2d(p).x = 34
|
||||||
|
p.y = 98
|
||||||
|
p.z = 343
|
||||||
|
|
||||||
|
getPoint(p)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
block tofopr:
|
||||||
|
type
|
||||||
|
TMyType = object {.inheritable.}
|
||||||
|
len: int
|
||||||
|
data: string
|
||||||
|
|
||||||
|
TOtherType = object of TMyType
|
||||||
|
|
||||||
|
proc p(x: TMyType): bool =
|
||||||
|
return x of TOtherType
|
||||||
|
|
||||||
|
var
|
||||||
|
m: TMyType
|
||||||
|
n: TOtherType
|
||||||
|
|
||||||
|
doAssert p(m) == false
|
||||||
|
doAssert p(n)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
block toop:
|
||||||
|
type
|
||||||
|
TA = object of RootObj
|
||||||
|
x, y: int
|
||||||
|
TB = object of TA
|
||||||
|
z: int
|
||||||
|
TC = object of TB
|
||||||
|
whatever: string
|
||||||
|
|
||||||
|
proc p(a: var TA) = echo "a"
|
||||||
|
proc p(b: var TB) = echo "b"
|
||||||
|
|
||||||
|
var c: TC
|
||||||
|
p(c)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
block tfefobjsyntax:
|
||||||
|
type
|
||||||
|
Foo = object
|
||||||
|
a, b: int
|
||||||
|
s: string
|
||||||
|
FooBar = object of RootObj
|
||||||
|
n, m: string
|
||||||
|
Baz = object of FooBar
|
||||||
|
|
||||||
|
proc invoke(a: ref Baz) =
|
||||||
|
echo "baz"
|
||||||
|
|
||||||
|
# check object construction:
|
||||||
|
let x = (ref Foo)(a: 0, b: 45, s: "wohoo")
|
||||||
|
echo x.s
|
||||||
|
|
||||||
|
var y: ref FooBar = (ref Baz)(n: "n", m: "m")
|
||||||
|
invoke((ref Baz)(y))
|
||||||
|
|
@ -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)
|
|
||||||
|
|
@ -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
185
tests/overload/tissues.nim
Normal 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)
|
||||||
|
|
@ -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
|
|
||||||
|
|
||||||
|
|
@ -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)
|
|
||||||
|
|
@ -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")
|
|
||||||
|
|
@ -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
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -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"
|
|
||||||
|
|
@ -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)
|
|
||||||
|
|
@ -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
|
|
||||||
|
|
@ -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"))
|
|
||||||
|
|
||||||
|
|
@ -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
|
|
||||||
|
|
@ -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
176
tests/overload/tvarious.nim
Normal 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"))
|
||||||
|
|
@ -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)
|
|
||||||
|
|
@ -1,10 +0,0 @@
|
||||||
discard """
|
|
||||||
output: "Hello"
|
|
||||||
"""
|
|
||||||
|
|
||||||
type
|
|
||||||
T = ref int
|
|
||||||
|
|
||||||
let r = new(string)
|
|
||||||
r[] = "Hello"
|
|
||||||
echo r[]
|
|
||||||
|
|
@ -1,11 +0,0 @@
|
||||||
discard """
|
|
||||||
output: "1"
|
|
||||||
"""
|
|
||||||
|
|
||||||
proc foo[T](bar: proc (x, y: T): int = system.cmp, baz: int) =
|
|
||||||
echo "1"
|
|
||||||
|
|
||||||
proc foo[T](bar: proc (x, y: T): int = system.cmp) =
|
|
||||||
echo "2"
|
|
||||||
|
|
||||||
foo[int](baz = 5)
|
|
||||||
|
|
@ -1,16 +0,0 @@
|
||||||
discard """
|
|
||||||
file: "tnestprc.nim"
|
|
||||||
output: "10"
|
|
||||||
"""
|
|
||||||
# Test nested procs without closures
|
|
||||||
|
|
||||||
proc Add3(x: int): int =
|
|
||||||
proc add(x, y: int): int {.noconv.} =
|
|
||||||
result = x + y
|
|
||||||
|
|
||||||
result = add(x, 3)
|
|
||||||
|
|
||||||
echo Add3(7) #OUT 10
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
31
tests/proc/tproc.nim
Normal file
31
tests/proc/tproc.nim
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
discard """
|
||||||
|
output: '''
|
||||||
|
Hello
|
||||||
|
1
|
||||||
|
'''
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
block t8357:
|
||||||
|
type T = ref int
|
||||||
|
|
||||||
|
let r = new(string)
|
||||||
|
r[] = "Hello"
|
||||||
|
echo r[]
|
||||||
|
|
||||||
|
|
||||||
|
block t8683:
|
||||||
|
proc foo[T](bar: proc (x, y: T): int = system.cmp, baz: int) =
|
||||||
|
echo "1"
|
||||||
|
proc foo[T](bar: proc (x, y: T): int = system.cmp) =
|
||||||
|
echo "2"
|
||||||
|
|
||||||
|
foo[int](baz = 5)
|
||||||
|
|
||||||
|
|
||||||
|
block tnestprc:
|
||||||
|
proc Add3(x: int): int =
|
||||||
|
proc add(x, y: int): int {.noconv.} =
|
||||||
|
result = x + y
|
||||||
|
result = add(x, 3)
|
||||||
|
doAssert Add3(7) == 10
|
||||||
|
|
@ -1,5 +0,0 @@
|
||||||
proc foo[T](thing: T) =
|
|
||||||
discard thing
|
|
||||||
|
|
||||||
var a: proc (thing: int) {.nimcall.} = foo[int]
|
|
||||||
|
|
||||||
|
|
@ -1,18 +1,39 @@
|
||||||
discard """
|
discard """
|
||||||
errormsg: "type mismatch"
|
output: '''
|
||||||
line: 17
|
papbpcpdpe7
|
||||||
file: "tprocvar.nim"
|
'''
|
||||||
"""
|
"""
|
||||||
|
|
||||||
type
|
block genericprocvar:
|
||||||
TCallback = proc (a, b: int)
|
proc foo[T](thing: T) =
|
||||||
|
discard thing
|
||||||
|
var a: proc (thing: int) {.nimcall.} = foo[int]
|
||||||
|
|
||||||
proc huh(x, y: var int) =
|
|
||||||
x = 0
|
|
||||||
y = x+1
|
|
||||||
|
|
||||||
proc so(c: TCallback) =
|
block tprocvar2:
|
||||||
c(2, 4)
|
proc pa() {.cdecl.} = write(stdout, "pa")
|
||||||
|
proc pb() {.cdecl.} = write(stdout, "pb")
|
||||||
|
proc pc() {.cdecl.} = write(stdout, "pc")
|
||||||
|
proc pd() {.cdecl.} = write(stdout, "pd")
|
||||||
|
proc pe() {.cdecl.} = write(stdout, "pe")
|
||||||
|
|
||||||
so(huh)
|
const algos = [pa, pb, pc, pd, pe]
|
||||||
|
var x: proc (a, b: int): int {.cdecl.}
|
||||||
|
|
||||||
|
proc ha(c, d: int): int {.cdecl.} =
|
||||||
|
echo(c + d)
|
||||||
|
result = c + d
|
||||||
|
|
||||||
|
for a in items(algos):
|
||||||
|
a()
|
||||||
|
|
||||||
|
x = ha
|
||||||
|
discard x(3, 4)
|
||||||
|
|
||||||
|
|
||||||
|
block tprocvars:
|
||||||
|
proc doSomething(v: int, x: proc(v:int):int): int = return x(v)
|
||||||
|
proc doSomething(v: int, x: proc(v:int)) = x(v)
|
||||||
|
|
||||||
|
doAssert doSomething(10, proc(v: int): int = return v div 2) == 5
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,32 +0,0 @@
|
||||||
discard """
|
|
||||||
file: "tprocvar.nim"
|
|
||||||
output: "papbpcpdpe7"
|
|
||||||
"""
|
|
||||||
# test variables of type proc
|
|
||||||
|
|
||||||
proc pa() {.cdecl.} = write(stdout, "pa")
|
|
||||||
proc pb() {.cdecl.} = write(stdout, "pb")
|
|
||||||
proc pc() {.cdecl.} = write(stdout, "pc")
|
|
||||||
proc pd() {.cdecl.} = write(stdout, "pd")
|
|
||||||
proc pe() {.cdecl.} = write(stdout, "pe")
|
|
||||||
|
|
||||||
const
|
|
||||||
algos = [pa, pb, pc, pd, pe]
|
|
||||||
|
|
||||||
var
|
|
||||||
x: proc (a, b: int): int {.cdecl.}
|
|
||||||
|
|
||||||
proc ha(c, d: int): int {.cdecl.} =
|
|
||||||
echo(c + d)
|
|
||||||
result = c + d
|
|
||||||
|
|
||||||
for a in items(algos):
|
|
||||||
a()
|
|
||||||
|
|
||||||
x = ha
|
|
||||||
discard x(3, 4)
|
|
||||||
|
|
||||||
#OUT papbpcpdpe7
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
18
tests/procvar/tprocvarmismatch.nim
Normal file
18
tests/procvar/tprocvarmismatch.nim
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
discard """
|
||||||
|
errormsg: "type mismatch"
|
||||||
|
line: 17
|
||||||
|
file: "tprocvarmismatch.nim"
|
||||||
|
"""
|
||||||
|
|
||||||
|
type
|
||||||
|
TCallback = proc (a, b: int)
|
||||||
|
|
||||||
|
proc huh(x, y: var int) =
|
||||||
|
x = 0
|
||||||
|
y = x+1
|
||||||
|
|
||||||
|
proc so(c: TCallback) =
|
||||||
|
c(2, 4)
|
||||||
|
|
||||||
|
so(huh)
|
||||||
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
||||||
proc doSomething(v: int, x: proc(v:int):int): int = return x(v)
|
|
||||||
proc doSomething(v: int, x: proc(v:int)) = x(v)
|
|
||||||
|
|
||||||
|
|
||||||
echo doSomething(10, proc(v: int): int = return v div 2)
|
|
||||||
|
|
||||||
|
|
@ -1,14 +0,0 @@
|
||||||
discard """
|
|
||||||
file: "tbug499771.nim"
|
|
||||||
output: '''TSubRange: 5 from 1 to 10
|
|
||||||
true true true'''
|
|
||||||
"""
|
|
||||||
type
|
|
||||||
TSubRange = range[1 .. 10]
|
|
||||||
TEnum = enum A, B, C
|
|
||||||
var sr: TSubRange = 5
|
|
||||||
echo("TSubRange: " & $sr & " from " & $low(TSubRange) & " to " &
|
|
||||||
$high(TSubRange))
|
|
||||||
|
|
||||||
const cset = {A} + {B}
|
|
||||||
echo A in cset, " ", B in cset, " ", C notin cset
|
|
||||||
|
|
@ -1,39 +0,0 @@
|
||||||
import strutils
|
|
||||||
|
|
||||||
type
|
|
||||||
TColor = distinct int32
|
|
||||||
|
|
||||||
proc rgb(r, g, b: range[0..255]): TColor =
|
|
||||||
result = TColor(r or g shl 8 or b shl 16)
|
|
||||||
|
|
||||||
proc `$`(c: TColor): string =
|
|
||||||
result = "#" & toHex(int32(c), 6)
|
|
||||||
|
|
||||||
echo rgb(34, 55, 255)
|
|
||||||
|
|
||||||
when false:
|
|
||||||
type
|
|
||||||
TColor = distinct int32
|
|
||||||
TColorComponent = distinct int8
|
|
||||||
|
|
||||||
proc red(a: TColor): TColorComponent =
|
|
||||||
result = TColorComponent(int32(a) and 0xff'i32)
|
|
||||||
|
|
||||||
proc green(a: TColor): TColorComponent =
|
|
||||||
result = TColorComponent(int32(a) shr 8'i32 and 0xff'i32)
|
|
||||||
|
|
||||||
proc blue(a: TColor): TColorComponent =
|
|
||||||
result = TColorComponent(int32(a) shr 16'i32 and 0xff'i32)
|
|
||||||
|
|
||||||
proc rgb(r, g, b: range[0..255]): TColor =
|
|
||||||
result = TColor(r or g shl 8 or b shl 8)
|
|
||||||
|
|
||||||
proc `+!` (a, b: TColorComponent): TColorComponent =
|
|
||||||
## saturated arithmetic:
|
|
||||||
result = TColorComponent(min(ze(int8(a)) + ze(int8(b)), 255))
|
|
||||||
|
|
||||||
proc `+` (a, b: TColor): TColor =
|
|
||||||
## saturated arithmetic for colors makes sense, I think:
|
|
||||||
return rgb(red(a) +! red(b), green(a) +! green(b), blue(a) +! blue(b))
|
|
||||||
|
|
||||||
rgb(34, 55, 255)
|
|
||||||
|
|
@ -1,41 +0,0 @@
|
||||||
discard """
|
|
||||||
output: '''0.0
|
|
||||||
0.0
|
|
||||||
0
|
|
||||||
0
|
|
||||||
0
|
|
||||||
'''
|
|
||||||
"""
|
|
||||||
|
|
||||||
include compilehelpers
|
|
||||||
|
|
||||||
type
|
|
||||||
Matrix*[M, N, T] = object
|
|
||||||
aij*: array[M, array[N, T]]
|
|
||||||
|
|
||||||
Matrix2*[T] = Matrix[range[0..1], range[0..1], T]
|
|
||||||
|
|
||||||
Matrix3*[T] = Matrix[range[0..2], range[0..2], T]
|
|
||||||
|
|
||||||
proc mn(x: Matrix): Matrix.T = x.aij[0][0]
|
|
||||||
|
|
||||||
proc m2(x: Matrix2): Matrix2.T = x.aij[0][0]
|
|
||||||
|
|
||||||
proc m3(x: Matrix3): auto = x.aij[0][0]
|
|
||||||
|
|
||||||
var
|
|
||||||
matn: Matrix[range[0..3], range[0..2], int]
|
|
||||||
mat2: Matrix2[int]
|
|
||||||
mat3: Matrix3[float]
|
|
||||||
|
|
||||||
echo m3(mat3)
|
|
||||||
echo mn(mat3)
|
|
||||||
echo m2(mat2)
|
|
||||||
echo mn(mat2)
|
|
||||||
echo mn(matn)
|
|
||||||
|
|
||||||
reject m3(mat2)
|
|
||||||
reject m3(matn)
|
|
||||||
reject m2(mat3)
|
|
||||||
reject m2(matn)
|
|
||||||
|
|
||||||
|
|
@ -1,18 +0,0 @@
|
||||||
discard """
|
|
||||||
output: '''-9'''
|
|
||||||
"""
|
|
||||||
|
|
||||||
type
|
|
||||||
n32 = range[0..high(int)]
|
|
||||||
n8* = range[0'i8..high(int8)]
|
|
||||||
|
|
||||||
proc `+`*(a: n32, b: n32{nkIntLit}): n32 = discard
|
|
||||||
|
|
||||||
proc `-`*(a: n8, b: n8): n8 = n8(system.`-`(a, b))
|
|
||||||
|
|
||||||
var x, y: n8
|
|
||||||
var z: int16
|
|
||||||
|
|
||||||
# ensure this doesn't call our '-' but system.`-` for int16:
|
|
||||||
echo z - n8(9)
|
|
||||||
|
|
||||||
107
tests/range/trange.nim
Normal file
107
tests/range/trange.nim
Normal file
|
|
@ -0,0 +1,107 @@
|
||||||
|
discard """
|
||||||
|
output: '''
|
||||||
|
TSubRange: 5 from 1 to 10
|
||||||
|
#FF3722
|
||||||
|
'''
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
block tbug499771:
|
||||||
|
type
|
||||||
|
TSubRange = range[1 .. 10]
|
||||||
|
TEnum = enum A, B, C
|
||||||
|
var sr: TSubRange = 5
|
||||||
|
echo("TSubRange: " & $sr & " from " & $low(TSubRange) & " to " &
|
||||||
|
$high(TSubRange))
|
||||||
|
|
||||||
|
const cset = {A} + {B}
|
||||||
|
doAssert A in cset
|
||||||
|
doAssert B in cset
|
||||||
|
doAssert C notin cset
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
include compilehelpers
|
||||||
|
block tmatrix3:
|
||||||
|
type
|
||||||
|
Matrix[M, N, T] = object
|
||||||
|
aij: array[M, array[N, T]]
|
||||||
|
|
||||||
|
Matrix2[T] = Matrix[range[0..1], range[0..1], T]
|
||||||
|
|
||||||
|
Matrix3[T] = Matrix[range[0..2], range[0..2], T]
|
||||||
|
|
||||||
|
proc mn(x: Matrix): Matrix.T = x.aij[0][0]
|
||||||
|
|
||||||
|
proc m2(x: Matrix2): Matrix2.T = x.aij[0][0]
|
||||||
|
|
||||||
|
proc m3(x: Matrix3): auto = x.aij[0][0]
|
||||||
|
|
||||||
|
var
|
||||||
|
matn: Matrix[range[0..3], range[0..2], int]
|
||||||
|
mat2: Matrix2[int]
|
||||||
|
mat3: Matrix3[float]
|
||||||
|
|
||||||
|
doAssert m3(mat3) == 0.0
|
||||||
|
doAssert mn(mat3) == 0.0
|
||||||
|
doAssert m2(mat2) == 0
|
||||||
|
doAssert mn(mat2) == 0
|
||||||
|
doAssert mn(matn) == 0
|
||||||
|
|
||||||
|
reject m3(mat2)
|
||||||
|
reject m3(matn)
|
||||||
|
reject m2(mat3)
|
||||||
|
reject m2(matn)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
block tn8vsint16:
|
||||||
|
type
|
||||||
|
n32 = range[0..high(int)]
|
||||||
|
n8 = range[0'i8..high(int8)]
|
||||||
|
|
||||||
|
proc `+`(a: n32, b: n32{nkIntLit}): n32 = discard
|
||||||
|
|
||||||
|
proc `-`(a: n8, b: n8): n8 = n8(system.`-`(a, b))
|
||||||
|
|
||||||
|
var x, y: n8
|
||||||
|
var z: int16
|
||||||
|
|
||||||
|
# ensure this doesn't call our '-' but system.`-` for int16:
|
||||||
|
doAssert z - n8(9) == -9
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import strutils
|
||||||
|
block tcolors:
|
||||||
|
type TColor = distinct int32
|
||||||
|
|
||||||
|
proc rgb(r, g, b: range[0..255]): TColor =
|
||||||
|
result = TColor(r or g shl 8 or b shl 16)
|
||||||
|
proc `$`(c: TColor): string =
|
||||||
|
result = "#" & toHex(int32(c), 6)
|
||||||
|
echo rgb(34, 55, 255)
|
||||||
|
|
||||||
|
when false:
|
||||||
|
type
|
||||||
|
TColor = distinct int32
|
||||||
|
TColorComponent = distinct int8
|
||||||
|
|
||||||
|
proc red(a: TColor): TColorComponent =
|
||||||
|
result = TColorComponent(int32(a) and 0xff'i32)
|
||||||
|
proc green(a: TColor): TColorComponent =
|
||||||
|
result = TColorComponent(int32(a) shr 8'i32 and 0xff'i32)
|
||||||
|
proc blue(a: TColor): TColorComponent =
|
||||||
|
result = TColorComponent(int32(a) shr 16'i32 and 0xff'i32)
|
||||||
|
proc rgb(r, g, b: range[0..255]): TColor =
|
||||||
|
result = TColor(r or g shl 8 or b shl 8)
|
||||||
|
|
||||||
|
proc `+!` (a, b: TColorComponent): TColorComponent =
|
||||||
|
## saturated arithmetic:
|
||||||
|
result = TColorComponent(min(ze(int8(a)) + ze(int8(b)), 255))
|
||||||
|
|
||||||
|
proc `+` (a, b: TColor): TColor =
|
||||||
|
## saturated arithmetic for colors makes sense, I think:
|
||||||
|
return rgb(red(a) +! red(b), green(a) +! green(b), blue(a) +! blue(b))
|
||||||
|
|
||||||
|
rgb(34, 55, 255)
|
||||||
181
tests/seq/tseq.nim
Normal file
181
tests/seq/tseq.nim
Normal file
|
|
@ -0,0 +1,181 @@
|
||||||
|
discard """
|
||||||
|
output: '''
|
||||||
|
Hithere, what's your name?Hathere, what's your name?
|
||||||
|
fA13msg1falsefB14msg2truefC15msg3false
|
||||||
|
Zip: [{"Field0": 1, "Field1": 2}, {"Field0": 3, "Field1": 4}, {"Field0": 5, "Field1": 6}]
|
||||||
|
Filter Iterator: 3
|
||||||
|
Filter Iterator: 5
|
||||||
|
Filter Iterator: 7
|
||||||
|
Filter: [3, 5, 7]
|
||||||
|
FilterIt: [1, 3, 7]
|
||||||
|
Concat: [1, 3, 5, 7, 2, 4, 6]
|
||||||
|
Deduplicate: [1, 2, 3, 4, 5, 7]
|
||||||
|
@[()]
|
||||||
|
@[1, 42, 3]
|
||||||
|
@[1, 42, 3]
|
||||||
|
2345623456
|
||||||
|
'''
|
||||||
|
"""
|
||||||
|
|
||||||
|
block tseq2:
|
||||||
|
proc `*`(a, b: seq[int]): seq[int] =
|
||||||
|
# allocate a new sequence:
|
||||||
|
newSeq(result, len(a))
|
||||||
|
# multiply two int sequences:
|
||||||
|
for i in 0..len(a)-1: result[i] = a[i] * b[i]
|
||||||
|
|
||||||
|
assert(@[1, 2, 3] * @[1, 2, 3] == @[1, 4, 9])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
block tseqcon:
|
||||||
|
const nestedFixed = true
|
||||||
|
|
||||||
|
type
|
||||||
|
TRec {.final.} = object
|
||||||
|
x, y: int
|
||||||
|
s: string
|
||||||
|
seq: seq[string]
|
||||||
|
TRecSeq = seq[TRec]
|
||||||
|
|
||||||
|
proc test() =
|
||||||
|
var s, b: seq[string]
|
||||||
|
s = @[]
|
||||||
|
add(s, "Hi")
|
||||||
|
add(s, "there, ")
|
||||||
|
add(s, "what's your name?")
|
||||||
|
|
||||||
|
b = s # deep copying here!
|
||||||
|
b[0][1] = 'a'
|
||||||
|
|
||||||
|
for i in 0 .. len(s)-1:
|
||||||
|
write(stdout, s[i])
|
||||||
|
for i in 0 .. len(b)-1:
|
||||||
|
write(stdout, b[i])
|
||||||
|
|
||||||
|
when nestedFixed:
|
||||||
|
proc nested() =
|
||||||
|
var
|
||||||
|
s: seq[seq[string]]
|
||||||
|
for i in 0..10_000: # test if the garbage collector
|
||||||
|
# now works with sequences
|
||||||
|
s = @[
|
||||||
|
@["A", "B", "C", "D"],
|
||||||
|
@["E", "F", "G", "H"],
|
||||||
|
@["I", "J", "K", "L"],
|
||||||
|
@["M", "N", "O", "P"]]
|
||||||
|
|
||||||
|
test()
|
||||||
|
when nestedFixed:
|
||||||
|
nested()
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import os
|
||||||
|
block tseqcon2:
|
||||||
|
proc rec_dir(dir: string): seq[string] =
|
||||||
|
result = @[]
|
||||||
|
for kind, path in walk_dir(dir):
|
||||||
|
if kind == pcDir:
|
||||||
|
add(result, rec_dir(path))
|
||||||
|
else:
|
||||||
|
add(result, path)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
block tseqtuple:
|
||||||
|
type
|
||||||
|
TMsg = tuple[
|
||||||
|
file: string,
|
||||||
|
line: int,
|
||||||
|
msg: string,
|
||||||
|
err: bool]
|
||||||
|
|
||||||
|
var s: seq[TMsg] = @[]
|
||||||
|
|
||||||
|
s.add(("fA", 13, "msg1", false))
|
||||||
|
s.add(("fB", 14, "msg2", true))
|
||||||
|
s.add(("fC", 15, "msg3", false))
|
||||||
|
|
||||||
|
for file, line, msg, err in items(s):
|
||||||
|
stdout.write(file)
|
||||||
|
stdout.write($line)
|
||||||
|
stdout.write(msg)
|
||||||
|
stdout.write($err)
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
|
||||||
|
import sequtils, marshal
|
||||||
|
block tsequtils:
|
||||||
|
proc testFindWhere(item : int) : bool =
|
||||||
|
if item != 1: return true
|
||||||
|
|
||||||
|
var seq1: seq[int] = @[]
|
||||||
|
|
||||||
|
seq1.add(1)
|
||||||
|
seq1.add(3)
|
||||||
|
seq1.add(5)
|
||||||
|
seq1.add(7)
|
||||||
|
|
||||||
|
var seq2: seq[int] = @[2, 4, 6]
|
||||||
|
var final = zip(seq1, seq2)
|
||||||
|
|
||||||
|
echo "Zip: ", $$(final)
|
||||||
|
|
||||||
|
#Test findWhere as a iterator
|
||||||
|
|
||||||
|
for itms in filter(seq1, testFindWhere):
|
||||||
|
echo "Filter Iterator: ", $$(itms)
|
||||||
|
|
||||||
|
|
||||||
|
#Test findWhere as a proc
|
||||||
|
|
||||||
|
var fullseq: seq[int] = filter(seq1, testFindWhere)
|
||||||
|
|
||||||
|
echo "Filter: ", $$(fullseq)
|
||||||
|
|
||||||
|
#Test findIt as a template
|
||||||
|
|
||||||
|
var finditval: seq[int] = filterIt(seq1, it!=5)
|
||||||
|
|
||||||
|
echo "FilterIt: ", $$(finditval)
|
||||||
|
|
||||||
|
var concatseq = concat(seq1,seq2)
|
||||||
|
echo "Concat: ", $$(concatseq)
|
||||||
|
|
||||||
|
var seq3 = @[1,2,3,4,5,5,5,7]
|
||||||
|
var dedupseq = deduplicate(seq3)
|
||||||
|
echo "Deduplicate: ", $$(dedupseq)
|
||||||
|
# bug #4973
|
||||||
|
type
|
||||||
|
SomeObj = object
|
||||||
|
OtherObj = object
|
||||||
|
field: SomeObj
|
||||||
|
|
||||||
|
let aSeq = @[OtherObj(field: SomeObj())]
|
||||||
|
let someObjSeq = aSeq.mapIt(it.field)
|
||||||
|
echo someObjSeq
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
block tshallowseq:
|
||||||
|
proc xxx() =
|
||||||
|
var x: seq[int] = @[1, 2, 3]
|
||||||
|
var y: seq[int]
|
||||||
|
system.shallowCopy(y, x)
|
||||||
|
y[1] = 42
|
||||||
|
echo y
|
||||||
|
echo x
|
||||||
|
xxx()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import strutils
|
||||||
|
block ttoseq:
|
||||||
|
for x in toSeq(countup(2, 6)):
|
||||||
|
stdout.write(x)
|
||||||
|
for x in items(toSeq(countup(2, 6))):
|
||||||
|
stdout.write(x)
|
||||||
|
var y: type("a b c".split)
|
||||||
|
y = "xzy"
|
||||||
|
|
@ -1,11 +0,0 @@
|
||||||
proc `*` *(a, b: seq[int]): seq[int] =
|
|
||||||
# allocate a new sequence:
|
|
||||||
newSeq(result, len(a))
|
|
||||||
# multiply two int sequences:
|
|
||||||
for i in 0..len(a)-1: result[i] = a[i] * b[i]
|
|
||||||
|
|
||||||
when isMainModule:
|
|
||||||
# test the new ``*`` operator for sequences:
|
|
||||||
assert(@[1, 2, 3] * @[1, 2, 3] == @[1, 4, 9])
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,51 +0,0 @@
|
||||||
discard """
|
|
||||||
file: "tseqcon.nim"
|
|
||||||
output: "Hithere, what\'s your name?Hathere, what\'s your name?"
|
|
||||||
"""
|
|
||||||
# Test the add proc for sequences and strings
|
|
||||||
|
|
||||||
const
|
|
||||||
nestedFixed = true
|
|
||||||
|
|
||||||
type
|
|
||||||
TRec {.final.} = object
|
|
||||||
x, y: int
|
|
||||||
s: string
|
|
||||||
seq: seq[string]
|
|
||||||
TRecSeq = seq[TRec]
|
|
||||||
|
|
||||||
proc test() =
|
|
||||||
var s, b: seq[string]
|
|
||||||
s = @[]
|
|
||||||
add(s, "Hi")
|
|
||||||
add(s, "there, ")
|
|
||||||
add(s, "what's your name?")
|
|
||||||
|
|
||||||
b = s # deep copying here!
|
|
||||||
b[0][1] = 'a'
|
|
||||||
|
|
||||||
for i in 0 .. len(s)-1:
|
|
||||||
write(stdout, s[i])
|
|
||||||
for i in 0 .. len(b)-1:
|
|
||||||
write(stdout, b[i])
|
|
||||||
|
|
||||||
|
|
||||||
when nestedFixed:
|
|
||||||
proc nested() =
|
|
||||||
var
|
|
||||||
s: seq[seq[string]]
|
|
||||||
for i in 0..10_000: # test if the garbage collector
|
|
||||||
# now works with sequences
|
|
||||||
s = @[
|
|
||||||
@["A", "B", "C", "D"],
|
|
||||||
@["E", "F", "G", "H"],
|
|
||||||
@["I", "J", "K", "L"],
|
|
||||||
@["M", "N", "O", "P"]]
|
|
||||||
|
|
||||||
test()
|
|
||||||
when nestedFixed:
|
|
||||||
nested()
|
|
||||||
|
|
||||||
#OUT Hithere, what's your name?Hathere, what's your name?
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
import os
|
|
||||||
|
|
||||||
proc rec_dir(dir: string): seq[string] =
|
|
||||||
result = @[]
|
|
||||||
for kind, path in walk_dir(dir):
|
|
||||||
if kind == pcDir:
|
|
||||||
add(result, rec_dir(path))
|
|
||||||
else:
|
|
||||||
add(result, path)
|
|
||||||
|
|
@ -1,28 +0,0 @@
|
||||||
discard """
|
|
||||||
file: "tseqtuple.nim"
|
|
||||||
output: "fA13msg1falsefB14msg2truefC15msg3false"
|
|
||||||
"""
|
|
||||||
|
|
||||||
type
|
|
||||||
TMsg = tuple[
|
|
||||||
file: string,
|
|
||||||
line: int,
|
|
||||||
msg: string,
|
|
||||||
err: bool]
|
|
||||||
|
|
||||||
var s: seq[TMsg] = @[]
|
|
||||||
|
|
||||||
s.add(("fA", 13, "msg1", false))
|
|
||||||
s.add(("fB", 14, "msg2", true))
|
|
||||||
s.add(("fC", 15, "msg3", false))
|
|
||||||
|
|
||||||
for file, line, msg, err in items(s):
|
|
||||||
stdout.write(file)
|
|
||||||
stdout.write($line)
|
|
||||||
stdout.write(msg)
|
|
||||||
stdout.write($err)
|
|
||||||
|
|
||||||
#OUT fA13msg1falsefB14msg2truefC15msg3false
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,64 +0,0 @@
|
||||||
discard """
|
|
||||||
file: "tsequtils.nim"
|
|
||||||
output: '''Zip: [{"Field0": 1, "Field1": 2}, {"Field0": 3, "Field1": 4}, {"Field0": 5, "Field1": 6}]
|
|
||||||
Filter Iterator: 3
|
|
||||||
Filter Iterator: 5
|
|
||||||
Filter Iterator: 7
|
|
||||||
Filter: [3, 5, 7]
|
|
||||||
FilterIt: [1, 3, 7]
|
|
||||||
Concat: [1, 3, 5, 7, 2, 4, 6]
|
|
||||||
Deduplicate: [1, 2, 3, 4, 5, 7]
|
|
||||||
@[()]'''
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
import sequtils, marshal
|
|
||||||
|
|
||||||
proc testFindWhere(item : int) : bool =
|
|
||||||
if item != 1: return true
|
|
||||||
|
|
||||||
var seq1: seq[int] = @[]
|
|
||||||
|
|
||||||
seq1.add(1)
|
|
||||||
seq1.add(3)
|
|
||||||
seq1.add(5)
|
|
||||||
seq1.add(7)
|
|
||||||
|
|
||||||
var seq2: seq[int] = @[2, 4, 6]
|
|
||||||
var final = zip(seq1, seq2)
|
|
||||||
|
|
||||||
echo "Zip: ", $$(final)
|
|
||||||
|
|
||||||
#Test findWhere as a iterator
|
|
||||||
|
|
||||||
for itms in filter(seq1, testFindWhere):
|
|
||||||
echo "Filter Iterator: ", $$(itms)
|
|
||||||
|
|
||||||
|
|
||||||
#Test findWhere as a proc
|
|
||||||
|
|
||||||
var fullseq: seq[int] = filter(seq1, testFindWhere)
|
|
||||||
|
|
||||||
echo "Filter: ", $$(fullseq)
|
|
||||||
|
|
||||||
#Test findIt as a template
|
|
||||||
|
|
||||||
var finditval: seq[int] = filterIt(seq1, it!=5)
|
|
||||||
|
|
||||||
echo "FilterIt: ", $$(finditval)
|
|
||||||
|
|
||||||
var concatseq = concat(seq1,seq2)
|
|
||||||
echo "Concat: ", $$(concatseq)
|
|
||||||
|
|
||||||
var seq3 = @[1,2,3,4,5,5,5,7]
|
|
||||||
var dedupseq = deduplicate(seq3)
|
|
||||||
echo "Deduplicate: ", $$(dedupseq)
|
|
||||||
# bug #4973
|
|
||||||
type
|
|
||||||
SomeObj = object
|
|
||||||
OtherObj = object
|
|
||||||
field: SomeObj
|
|
||||||
|
|
||||||
let aSeq = @[OtherObj(field: SomeObj())]
|
|
||||||
let someObjSeq = aSeq.mapIt(it.field)
|
|
||||||
echo someObjSeq
|
|
||||||
|
|
@ -1,17 +0,0 @@
|
||||||
discard """
|
|
||||||
output: '''@[1, 42, 3]
|
|
||||||
@[1, 42, 3]
|
|
||||||
'''
|
|
||||||
"""
|
|
||||||
proc xxx() =
|
|
||||||
var x: seq[int] = @[1, 2, 3]
|
|
||||||
var y: seq[int]
|
|
||||||
|
|
||||||
system.shallowCopy(y, x)
|
|
||||||
|
|
||||||
y[1] = 42
|
|
||||||
|
|
||||||
echo y
|
|
||||||
echo x
|
|
||||||
|
|
||||||
xxx()
|
|
||||||
|
|
@ -1,18 +0,0 @@
|
||||||
discard """
|
|
||||||
output: "2345623456"
|
|
||||||
"""
|
|
||||||
|
|
||||||
import sequtils
|
|
||||||
|
|
||||||
for x in toSeq(countup(2, 6)):
|
|
||||||
stdout.write(x)
|
|
||||||
for x in items(toSeq(countup(2, 6))):
|
|
||||||
stdout.write(x)
|
|
||||||
|
|
||||||
import strutils
|
|
||||||
|
|
||||||
var y: type("a b c".split)
|
|
||||||
y = "xzy"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,22 +0,0 @@
|
||||||
discard """
|
|
||||||
targets: "c c++ js"
|
|
||||||
output: '''1000
|
|
||||||
0
|
|
||||||
set is empty
|
|
||||||
'''
|
|
||||||
"""
|
|
||||||
|
|
||||||
import sets
|
|
||||||
|
|
||||||
var a = initSet[int]()
|
|
||||||
for i in 1..1000:
|
|
||||||
a.incl(i)
|
|
||||||
echo len(a)
|
|
||||||
for i in 1..1000:
|
|
||||||
discard a.pop()
|
|
||||||
echo len(a)
|
|
||||||
|
|
||||||
try:
|
|
||||||
echo a.pop()
|
|
||||||
except KeyError as e:
|
|
||||||
echo e.msg
|
|
||||||
|
|
@ -1,71 +0,0 @@
|
||||||
discard """
|
|
||||||
output: '''true'''
|
|
||||||
"""
|
|
||||||
|
|
||||||
import hashes, sets
|
|
||||||
|
|
||||||
const
|
|
||||||
data = [
|
|
||||||
"34", "12",
|
|
||||||
"90", "0",
|
|
||||||
"1", "2",
|
|
||||||
"3", "4",
|
|
||||||
"5", "6",
|
|
||||||
"7", "8",
|
|
||||||
"9", "---00",
|
|
||||||
"10", "11", "19",
|
|
||||||
"20", "30", "40",
|
|
||||||
"50", "60", "70",
|
|
||||||
"80"]
|
|
||||||
|
|
||||||
block tableTest1:
|
|
||||||
var t = initSet[tuple[x, y: int]]()
|
|
||||||
t.incl((0,0))
|
|
||||||
t.incl((1,0))
|
|
||||||
assert(not t.containsOrIncl((0,1)))
|
|
||||||
t.incl((1,1))
|
|
||||||
|
|
||||||
for x in 0..1:
|
|
||||||
for y in 0..1:
|
|
||||||
assert((x,y) in t)
|
|
||||||
#assert($t ==
|
|
||||||
# "{(x: 0, y: 0), (x: 0, y: 1), (x: 1, y: 0), (x: 1, y: 1)}")
|
|
||||||
|
|
||||||
block setTest2:
|
|
||||||
var t = initSet[string]()
|
|
||||||
t.incl("test")
|
|
||||||
t.incl("111")
|
|
||||||
t.incl("123")
|
|
||||||
t.excl("111")
|
|
||||||
t.incl("012")
|
|
||||||
t.incl("123") # test duplicates
|
|
||||||
|
|
||||||
assert "123" in t
|
|
||||||
assert "111" notin t # deleted
|
|
||||||
|
|
||||||
assert t.missingOrExcl("000") == true
|
|
||||||
assert "000" notin t
|
|
||||||
assert t.missingOrExcl("012") == false
|
|
||||||
assert "012" notin t
|
|
||||||
|
|
||||||
assert t.containsOrIncl("012") == false
|
|
||||||
assert t.containsOrIncl("012") == true
|
|
||||||
assert "012" in t # added back
|
|
||||||
|
|
||||||
for key in items(data): t.incl(key)
|
|
||||||
for key in items(data): assert key in t
|
|
||||||
|
|
||||||
for key in items(data): t.excl(key)
|
|
||||||
for key in items(data): assert key notin t
|
|
||||||
|
|
||||||
block orderedSetTest1:
|
|
||||||
var t = data.toOrderedSet
|
|
||||||
for key in items(data): assert key in t
|
|
||||||
var i = 0
|
|
||||||
# `items` needs to yield in insertion order:
|
|
||||||
for key in items(t):
|
|
||||||
assert key == data[i]
|
|
||||||
inc(i)
|
|
||||||
|
|
||||||
echo "true"
|
|
||||||
|
|
||||||
|
|
@ -1,100 +0,0 @@
|
||||||
include sets
|
|
||||||
|
|
||||||
let
|
|
||||||
s1: TSet[int] = toSet([1, 2, 4, 8, 16])
|
|
||||||
s2: TSet[int] = toSet([1, 2, 3, 5, 8])
|
|
||||||
s3: TSet[int] = toSet([3, 5, 7])
|
|
||||||
|
|
||||||
block union:
|
|
||||||
let
|
|
||||||
s1_s2 = union(s1, s2)
|
|
||||||
s1_s3 = s1 + s3
|
|
||||||
s2_s3 = s2 + s3
|
|
||||||
|
|
||||||
assert s1_s2.len == 7
|
|
||||||
assert s1_s3.len == 8
|
|
||||||
assert s2_s3.len == 6
|
|
||||||
|
|
||||||
for i in s1:
|
|
||||||
assert i in s1_s2
|
|
||||||
assert i in s1_s3
|
|
||||||
for i in s2:
|
|
||||||
assert i in s1_s2
|
|
||||||
assert i in s2_s3
|
|
||||||
for i in s3:
|
|
||||||
assert i in s1_s3
|
|
||||||
assert i in s2_s3
|
|
||||||
|
|
||||||
assert((s1 + s1) == s1)
|
|
||||||
assert((s2 + s1) == s1_s2)
|
|
||||||
|
|
||||||
block intersection:
|
|
||||||
let
|
|
||||||
s1_s2 = intersection(s1, s2)
|
|
||||||
s1_s3 = intersection(s1, s3)
|
|
||||||
s2_s3 = s2 * s3
|
|
||||||
|
|
||||||
assert s1_s2.len == 3
|
|
||||||
assert s1_s3.len == 0
|
|
||||||
assert s2_s3.len == 2
|
|
||||||
|
|
||||||
for i in s1_s2:
|
|
||||||
assert i in s1
|
|
||||||
assert i in s2
|
|
||||||
for i in s1_s3:
|
|
||||||
assert i in s1
|
|
||||||
assert i in s3
|
|
||||||
for i in s2_s3:
|
|
||||||
assert i in s2
|
|
||||||
assert i in s3
|
|
||||||
|
|
||||||
assert((s2 * s2) == s2)
|
|
||||||
assert((s3 * s2) == s2_s3)
|
|
||||||
|
|
||||||
block symmetricDifference:
|
|
||||||
let
|
|
||||||
s1_s2 = symmetricDifference(s1, s2)
|
|
||||||
s1_s3 = s1 -+- s3
|
|
||||||
s2_s3 = s2 -+- s3
|
|
||||||
|
|
||||||
assert s1_s2.len == 4
|
|
||||||
assert s1_s3.len == 8
|
|
||||||
assert s2_s3.len == 4
|
|
||||||
|
|
||||||
for i in s1:
|
|
||||||
assert i in s1_s2 xor i in s2
|
|
||||||
assert i in s1_s3 xor i in s3
|
|
||||||
for i in s2:
|
|
||||||
assert i in s1_s2 xor i in s1
|
|
||||||
assert i in s2_s3 xor i in s3
|
|
||||||
for i in s3:
|
|
||||||
assert i in s1_s3 xor i in s1
|
|
||||||
assert i in s2_s3 xor i in s2
|
|
||||||
|
|
||||||
assert((s3 -+- s3) == initSet[int]())
|
|
||||||
assert((s3 -+- s1) == s1_s3)
|
|
||||||
|
|
||||||
block difference:
|
|
||||||
let
|
|
||||||
s1_s2 = difference(s1, s2)
|
|
||||||
s1_s3 = difference(s1, s3)
|
|
||||||
s2_s3 = s2 - s3
|
|
||||||
|
|
||||||
assert s1_s2.len == 2
|
|
||||||
assert s1_s3.len == 5
|
|
||||||
assert s2_s3.len == 3
|
|
||||||
|
|
||||||
for i in s1:
|
|
||||||
assert i in s1_s2 xor i in s2
|
|
||||||
assert i in s1_s3 xor i in s3
|
|
||||||
for i in s2:
|
|
||||||
assert i in s2_s3 xor i in s3
|
|
||||||
|
|
||||||
assert((s2 - s2) == initSet[int]())
|
|
||||||
assert((s1 - s3 - s1) == s1 -+- s3)
|
|
||||||
|
|
||||||
block disjoint:
|
|
||||||
assert(not disjoint(s1, s2))
|
|
||||||
assert disjoint(s1, s3)
|
|
||||||
assert(not disjoint(s2, s3))
|
|
||||||
assert(not disjoint(s2, s2))
|
|
||||||
|
|
@ -1,12 +0,0 @@
|
||||||
discard """
|
|
||||||
output: '''true
|
|
||||||
true
|
|
||||||
true'''
|
|
||||||
"""
|
|
||||||
|
|
||||||
var s, s1: set[char]
|
|
||||||
s = {'a'..'d'}
|
|
||||||
s1 = {'a'..'c'}
|
|
||||||
echo s1 < s
|
|
||||||
echo s1 * s == {'a'..'c'}
|
|
||||||
echo s1 <= s
|
|
||||||
200
tests/sets/tvarious.nim
Normal file
200
tests/sets/tvarious.nim
Normal file
|
|
@ -0,0 +1,200 @@
|
||||||
|
discard """
|
||||||
|
output: '''
|
||||||
|
set is empty
|
||||||
|
'''
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
import sets, hashes
|
||||||
|
|
||||||
|
|
||||||
|
block tsetpop:
|
||||||
|
var a = initSet[int]()
|
||||||
|
for i in 1..1000:
|
||||||
|
a.incl(i)
|
||||||
|
doAssert len(a) == 1000
|
||||||
|
for i in 1..1000:
|
||||||
|
discard a.pop()
|
||||||
|
doAssert len(a) == 0
|
||||||
|
|
||||||
|
try:
|
||||||
|
echo a.pop()
|
||||||
|
except KeyError as e:
|
||||||
|
echo e.msg
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
block tsets_lt:
|
||||||
|
var s, s1: set[char]
|
||||||
|
s = {'a'..'d'}
|
||||||
|
s1 = {'a'..'c'}
|
||||||
|
doAssert s1 < s
|
||||||
|
doAssert s1 * s == {'a'..'c'}
|
||||||
|
doAssert s1 <= s
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
block tsets2:
|
||||||
|
const
|
||||||
|
data = [
|
||||||
|
"34", "12",
|
||||||
|
"90", "0",
|
||||||
|
"1", "2",
|
||||||
|
"3", "4",
|
||||||
|
"5", "6",
|
||||||
|
"7", "8",
|
||||||
|
"9", "---00",
|
||||||
|
"10", "11", "19",
|
||||||
|
"20", "30", "40",
|
||||||
|
"50", "60", "70",
|
||||||
|
"80"]
|
||||||
|
|
||||||
|
block tableTest1:
|
||||||
|
var t = initSet[tuple[x, y: int]]()
|
||||||
|
t.incl((0,0))
|
||||||
|
t.incl((1,0))
|
||||||
|
assert(not t.containsOrIncl((0,1)))
|
||||||
|
t.incl((1,1))
|
||||||
|
|
||||||
|
for x in 0..1:
|
||||||
|
for y in 0..1:
|
||||||
|
assert((x,y) in t)
|
||||||
|
#assert($t ==
|
||||||
|
# "{(x: 0, y: 0), (x: 0, y: 1), (x: 1, y: 0), (x: 1, y: 1)}")
|
||||||
|
|
||||||
|
block setTest2:
|
||||||
|
var t = initSet[string]()
|
||||||
|
t.incl("test")
|
||||||
|
t.incl("111")
|
||||||
|
t.incl("123")
|
||||||
|
t.excl("111")
|
||||||
|
t.incl("012")
|
||||||
|
t.incl("123") # test duplicates
|
||||||
|
|
||||||
|
assert "123" in t
|
||||||
|
assert "111" notin t # deleted
|
||||||
|
|
||||||
|
assert t.missingOrExcl("000")
|
||||||
|
assert "000" notin t
|
||||||
|
assert t.missingOrExcl("012") == false
|
||||||
|
assert "012" notin t
|
||||||
|
|
||||||
|
assert t.containsOrIncl("012") == false
|
||||||
|
assert t.containsOrIncl("012")
|
||||||
|
assert "012" in t # added back
|
||||||
|
|
||||||
|
for key in items(data): t.incl(key)
|
||||||
|
for key in items(data): assert key in t
|
||||||
|
|
||||||
|
for key in items(data): t.excl(key)
|
||||||
|
for key in items(data): assert key notin t
|
||||||
|
|
||||||
|
block orderedSetTest1:
|
||||||
|
var t = data.toOrderedSet
|
||||||
|
for key in items(data): assert key in t
|
||||||
|
var i = 0
|
||||||
|
# `items` needs to yield in insertion order:
|
||||||
|
for key in items(t):
|
||||||
|
assert key == data[i]
|
||||||
|
inc(i)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
block tsets3:
|
||||||
|
let
|
||||||
|
s1: TSet[int] = toSet([1, 2, 4, 8, 16])
|
||||||
|
s2: TSet[int] = toSet([1, 2, 3, 5, 8])
|
||||||
|
s3: TSet[int] = toSet([3, 5, 7])
|
||||||
|
|
||||||
|
block union:
|
||||||
|
let
|
||||||
|
s1_s2 = union(s1, s2)
|
||||||
|
s1_s3 = s1 + s3
|
||||||
|
s2_s3 = s2 + s3
|
||||||
|
|
||||||
|
assert s1_s2.len == 7
|
||||||
|
assert s1_s3.len == 8
|
||||||
|
assert s2_s3.len == 6
|
||||||
|
|
||||||
|
for i in s1:
|
||||||
|
assert i in s1_s2
|
||||||
|
assert i in s1_s3
|
||||||
|
for i in s2:
|
||||||
|
assert i in s1_s2
|
||||||
|
assert i in s2_s3
|
||||||
|
for i in s3:
|
||||||
|
assert i in s1_s3
|
||||||
|
assert i in s2_s3
|
||||||
|
|
||||||
|
assert((s1 + s1) == s1)
|
||||||
|
assert((s2 + s1) == s1_s2)
|
||||||
|
|
||||||
|
block intersection:
|
||||||
|
let
|
||||||
|
s1_s2 = intersection(s1, s2)
|
||||||
|
s1_s3 = intersection(s1, s3)
|
||||||
|
s2_s3 = s2 * s3
|
||||||
|
|
||||||
|
assert s1_s2.len == 3
|
||||||
|
assert s1_s3.len == 0
|
||||||
|
assert s2_s3.len == 2
|
||||||
|
|
||||||
|
for i in s1_s2:
|
||||||
|
assert i in s1
|
||||||
|
assert i in s2
|
||||||
|
for i in s1_s3:
|
||||||
|
assert i in s1
|
||||||
|
assert i in s3
|
||||||
|
for i in s2_s3:
|
||||||
|
assert i in s2
|
||||||
|
assert i in s3
|
||||||
|
|
||||||
|
assert((s2 * s2) == s2)
|
||||||
|
assert((s3 * s2) == s2_s3)
|
||||||
|
|
||||||
|
block symmetricDifference:
|
||||||
|
let
|
||||||
|
s1_s2 = symmetricDifference(s1, s2)
|
||||||
|
s1_s3 = s1 -+- s3
|
||||||
|
s2_s3 = s2 -+- s3
|
||||||
|
|
||||||
|
assert s1_s2.len == 4
|
||||||
|
assert s1_s3.len == 8
|
||||||
|
assert s2_s3.len == 4
|
||||||
|
|
||||||
|
for i in s1:
|
||||||
|
assert i in s1_s2 xor i in s2
|
||||||
|
assert i in s1_s3 xor i in s3
|
||||||
|
for i in s2:
|
||||||
|
assert i in s1_s2 xor i in s1
|
||||||
|
assert i in s2_s3 xor i in s3
|
||||||
|
for i in s3:
|
||||||
|
assert i in s1_s3 xor i in s1
|
||||||
|
assert i in s2_s3 xor i in s2
|
||||||
|
|
||||||
|
assert((s3 -+- s3) == initSet[int]())
|
||||||
|
assert((s3 -+- s1) == s1_s3)
|
||||||
|
|
||||||
|
block difference:
|
||||||
|
let
|
||||||
|
s1_s2 = difference(s1, s2)
|
||||||
|
s1_s3 = difference(s1, s3)
|
||||||
|
s2_s3 = s2 - s3
|
||||||
|
|
||||||
|
assert s1_s2.len == 2
|
||||||
|
assert s1_s3.len == 5
|
||||||
|
assert s2_s3.len == 3
|
||||||
|
|
||||||
|
for i in s1:
|
||||||
|
assert i in s1_s2 xor i in s2
|
||||||
|
assert i in s1_s3 xor i in s3
|
||||||
|
for i in s2:
|
||||||
|
assert i in s2_s3 xor i in s3
|
||||||
|
|
||||||
|
assert((s2 - s2) == initSet[int]())
|
||||||
|
|
||||||
|
block disjoint:
|
||||||
|
assert(not disjoint(s1, s2))
|
||||||
|
assert disjoint(s1, s3)
|
||||||
|
assert(not disjoint(s2, s3))
|
||||||
|
assert(not disjoint(s2, s2))
|
||||||
|
|
@ -4,7 +4,7 @@ discard """
|
||||||
|
|
||||||
{.warning[TypelessParam]: off.}
|
{.warning[TypelessParam]: off.}
|
||||||
|
|
||||||
import future
|
import sugar
|
||||||
|
|
||||||
# bug #3329
|
# bug #3329
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
|
|
||||||
import future
|
import sugar
|
||||||
|
|
||||||
template tempo(s) =
|
template tempo(s) =
|
||||||
s("arg")
|
s("arg")
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ discard """
|
||||||
|
|
||||||
# bug #3561
|
# bug #3561
|
||||||
|
|
||||||
import macros, future, strutils
|
import macros, sugar, strutils
|
||||||
|
|
||||||
type
|
type
|
||||||
Option[T] = ref object
|
Option[T] = ref object
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ static:
|
||||||
assert str == "abc"
|
assert str == "abc"
|
||||||
|
|
||||||
# #6086
|
# #6086
|
||||||
import math, sequtils, future
|
import math, sequtils, sugar
|
||||||
|
|
||||||
block:
|
block:
|
||||||
proc f: int =
|
proc f: int =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue