Merge tests into a larger file (part 2 of ∞) (#9335)

* merge controlflow tests

* merge distinct tests

* merge enum tests

* merge fields tests

* merge implicit tests

* merge iter issues tests

(cherry picked from commit 3c9fcc4c30)
This commit is contained in:
Miran
2018-10-13 14:58:31 +02:00
committed by narimiran
parent 495585bc1e
commit e718dc75c5
39 changed files with 809 additions and 790 deletions

View File

@@ -1,44 +0,0 @@
discard """
output: '''10
true true
true false
false true
false false'''
"""
var
x = false
run = true
while run:
run = false
block myblock:
if true:
break
echo "leaving myblock"
x = true
doAssert(x)
# bug #1418
iterator foo: int =
for x in 0 .. 9:
for y in [10,20,30,40,50,60,70,80,90]:
yield x + y
for p in foo():
echo p
break
iterator permutations: int =
yield 10
for p in permutations():
break
# regression:
proc main =
for x in [true, false]:
for y in [true, false]:
echo x, " ", y
main()

View File

@@ -1,28 +0,0 @@
discard """
output: "came here"
"""
var i = 0
while i < 400:
if i == 10: break
elif i == 3:
inc i
continue
inc i
var f = "failure"
var j = 0
while j < 300:
for x in 0..34:
if j < 300: continue
if x == 10:
echo "failure: should never happen"
break
f = "came here"
break
if i == 10:
echo f
else:
echo "failure"

View File

@@ -0,0 +1,97 @@
discard """
output: '''
10
true true
true false
false true
false false
i == 2
'''
"""
block tbreak:
var
x = false
run = true
while run:
run = false
block myblock:
if true:
break
echo "leaving myblock"
x = true
doAssert(x)
# bug #1418
iterator foo: int =
for x in 0 .. 9:
for y in [10,20,30,40,50,60,70,80,90]:
yield x + y
for p in foo():
echo p
break
iterator permutations: int =
yield 10
for p in permutations():
break
# regression:
proc main =
for x in [true, false]:
for y in [true, false]:
echo x, " ", y
main()
block tcontinue:
var i = 0
while i < 400:
if i == 10: break
elif i == 3:
inc i
continue
inc i
var f = "failure"
var j = 0
while j < 300:
for x in 0..34:
if j < 300: continue
if x == 10:
echo "failure: should never happen"
break
f = "came here"
break
if i == 10:
doAssert f == "came here"
else:
echo "failure"
block tnestif:
var
x, y: int
x = 2
if x == 0:
write(stdout, "i == 0")
if y == 0:
write(stdout, x)
else:
write(stdout, y)
elif x == 1:
write(stdout, "i == 1")
elif x == 2:
write(stdout, "i == 2")
else:
write(stdout, "looks like Python")
#OUT i == 2

View File

@@ -1,24 +0,0 @@
discard """
file: "tnestif.nim"
output: "i == 2"
"""
# test nested ifs
var
x, y: int
x = 2
if x == 0:
write(stdout, "i == 0")
if y == 0:
write(stdout, x)
else:
write(stdout, y)
elif x == 1:
write(stdout, "i == 1")
elif x == 2:
write(stdout, "i == 2")
else:
write(stdout, "looks like Python")
#OUT i == 2

View File

@@ -1,21 +0,0 @@
discard """
output: '''
A
A
'''
"""
type
A[T] = distinct T
B[T] = distinct T
proc foo[T](x:A[T]) = echo "A"
proc foo[T](x:B[T]) = echo "B"
proc bar(x:A) = echo "A"
proc bar(x:B) = echo "B"
var
a:A[int]
foo(a) # fine
bar(a) # testdistinct.nim(14, 4) Error: ambiguous call; both testdistinct.bar(x: A) and testdistinct.bar(x: B) match for: (A[system.int])

View File

@@ -1,19 +0,0 @@
discard """
exitcode: 0
output: ''''''
"""
# Snippet not defined as ```nim
type MyInt* = distinct int
proc `+`*(x: MyInt, y: MyInt): MyInt {.borrow.}
proc `+=`*(x: var MyInt, y: MyInt) {.borrow.}
proc `=`*(x: var MyInt, y: MyInt) {.borrow.}
var next: MyInt
proc getNext*() : MyInt =
result = next
next += 1.MyInt
next = next + 1.MyInt

View File

@@ -1,19 +0,0 @@
discard """
output: '''
25.0
210.0
'''
"""
type
Dollars = distinct float
proc `$`(d: Dollars): string {.borrow.}
proc `*` *(a, b: Dollars): Dollars {.borrow.}
proc `+` *(a, b: Dollars): Dollars {.borrow.}
var a = Dollars(20)
a = Dollars(25.0)
echo a
a = 10.Dollars * (20.Dollars + 1.Dollars)
echo a

View File

@@ -1,13 +0,0 @@
type
Foo = object
a, b: int
s: string
Bar {.borrow: `.`.} = distinct Foo
var bb: ref Bar
new bb
bb.a = 90
bb.s = "abc"

View File

@@ -1,38 +0,0 @@
discard """
file: "tcurrncy.nim"
output: "25"
"""
template Additive(typ: untyped) =
proc `+` *(x, y: typ): typ {.borrow.}
proc `-` *(x, y: typ): typ {.borrow.}
# unary operators:
proc `+` *(x: typ): typ {.borrow.}
proc `-` *(x: typ): typ {.borrow.}
template Multiplicative(typ, base: untyped) =
proc `*` *(x: typ, y: base): typ {.borrow.}
proc `*` *(x: base, y: typ): typ {.borrow.}
proc `div` *(x: typ, y: base): typ {.borrow.}
proc `mod` *(x: typ, y: base): typ {.borrow.}
template Comparable(typ: untyped) =
proc `<` * (x, y: typ): bool {.borrow.}
proc `<=` * (x, y: typ): bool {.borrow.}
proc `==` * (x, y: typ): bool {.borrow.}
template DefineCurrency(typ, base: untyped) =
type
typ* = distinct base
Additive(typ)
Multiplicative(typ, base)
Comparable(typ)
proc `$` * (t: typ): string {.borrow.}
DefineCurrency(TDollar, int)
DefineCurrency(TEuro, int)
echo($( 12.TDollar + 13.TDollar )) #OUT 25

View File

@@ -0,0 +1,77 @@
discard """
output: '''
25
'''
"""
block tborrowdot:
type
Foo = object
a, b: int
s: string
Bar {.borrow: `.`.} = distinct Foo
var bb: ref Bar
new bb
bb.a = 90
bb.s = "abc"
block tcurrncy:
template Additive(typ: untyped) =
proc `+`(x, y: typ): typ {.borrow.}
proc `-`(x, y: typ): typ {.borrow.}
# unary operators:
proc `+`(x: typ): typ {.borrow.}
proc `-`(x: typ): typ {.borrow.}
template Multiplicative(typ, base: untyped) =
proc `*`(x: typ, y: base): typ {.borrow.}
proc `*`(x: base, y: typ): typ {.borrow.}
proc `div`(x: typ, y: base): typ {.borrow.}
proc `mod`(x: typ, y: base): typ {.borrow.}
template Comparable(typ: untyped) =
proc `<`(x, y: typ): bool {.borrow.}
proc `<=`(x, y: typ): bool {.borrow.}
proc `==`(x, y: typ): bool {.borrow.}
template DefineCurrency(typ, base: untyped) =
type
typ = distinct base
Additive(typ)
Multiplicative(typ, base)
Comparable(typ)
proc `$`(t: typ): string {.borrow.}
DefineCurrency(TDollar, int)
DefineCurrency(TEuro, int)
echo($( 12.TDollar + 13.TDollar )) #OUT 25
block tconsts:
# bug #2641
type MyChar = distinct char
const c:MyChar = MyChar('a')
type MyBool = distinct bool
const b:MyBool = MyBool(true)
type MyBoolSet = distinct set[bool]
const bs:MyBoolSet = MyBoolSet({true})
type MyCharSet= distinct set[char]
const cs:MyCharSet = MyCharSet({'a'})
type MyBoolSeq = distinct seq[bool]
const bseq:MyBoolSeq = MyBoolSeq(@[true, false])
type MyBoolArr = distinct array[3, bool]
const barr:MyBoolArr = MyBoolArr([true, false, true])

View File

@@ -1,20 +0,0 @@
# bug #2641
type MyChar = distinct char
const c:MyChar = MyChar('a')
type MyBool = distinct bool
const b:MyBool = MyBool(true)
type MyBoolSet = distinct set[bool]
const bs:MyBoolSet = MyBoolSet({true})
type MyCharSet= distinct set[char]
const cs:MyCharSet = MyCharSet({'a'})
type MyBoolSeq = distinct seq[bool]
const bseq:MyBoolSeq = MyBoolSeq(@[true, false])
type MyBoolArr = distinct array[3, bool]
const barr:MyBoolArr = MyBoolArr([true, false, true])

View File

@@ -0,0 +1,67 @@
discard """
output: '''
A
A
25.0
210.0
apr
'''
"""
block t4435:
type
A[T] = distinct T
B[T] = distinct T
proc foo[T](x:A[T]) = echo "A"
proc foo[T](x:B[T]) = echo "B"
proc bar(x:A) = echo "A"
proc bar(x:B) = echo "B"
var
a:A[int]
foo(a) # fine
bar(a) # testdistinct.nim(14, 4) Error: ambiguous call; both testdistinct.bar(x: A) and testdistinct.bar(x: B) match for: (A[system.int])
block t7010:
type MyInt = distinct int
proc `+`(x: MyInt, y: MyInt): MyInt {.borrow.}
proc `+=`(x: var MyInt, y: MyInt) {.borrow.}
proc `=`(x: var MyInt, y: MyInt) {.borrow.}
var next: MyInt
proc getNext() : MyInt =
result = next
next += 1.MyInt
next = next + 1.MyInt
block t9079:
type
Dollars = distinct float
proc `$`(d: Dollars): string {.borrow.}
proc `*`(a, b: Dollars): Dollars {.borrow.}
proc `+`(a, b: Dollars): Dollars {.borrow.}
var a = Dollars(20)
a = Dollars(25.0)
echo a
a = 10.Dollars * (20.Dollars + 1.Dollars)
echo a
block t9322:
type Fix = distinct string
proc `$`(f: Fix): string {.borrow.}
proc mystr(s: string) =
echo s
mystr($Fix("apr"))

View File

@@ -1,11 +0,0 @@
discard """
file: "tbasicenum.nim"
output: "ABCDC"
"""
type
MyEnum = enum
A,B,C,D
# trick the optimizer with an seq:
var x = @[A,B,C,D]
echo x[0],x[1],x[2],x[3],MyEnum(2)

View File

@@ -1,14 +1,147 @@
# Test enums
discard """
output: '''
B
B
ABCDC
foo
first0second32third64
my value A1my value Bconc2valueCabc4abc
my value A0my value Bconc1valueCabc3valueC
'''
"""
type
E = enum a, b, c, x, y, z
var
en: E
en = a
# Bug #4066
import macros
macro genEnum(): untyped = newNimNode(nnkEnumTy).add(newEmptyNode(), newIdentNode("geItem1"))
type GeneratedEnum = genEnum()
doAssert(type(geItem1) is GeneratedEnum)
block tenum1:
type E = enum a, b, c, x, y, z
var en: E
en = a
# Bug #4066
macro genEnum(): untyped = newNimNode(nnkEnumTy).add(newEmptyNode(), newIdentNode("geItem1"))
type GeneratedEnum = genEnum()
doAssert(type(geItem1) is GeneratedEnum)
block tenum2:
type
TEnumHole = enum
eA = 0,
eB = 4,
eC = 5
var
e: TEnumHole = eB
case e
of eA: echo "A"
of eB: echo "B"
of eC: echo "C"
block tenum3:
type
TEnumHole {.size: sizeof(int).} = enum
eA = 0,
eB = 4,
eC = 5
var
e: TEnumHole = eB
case e
of eA: echo "A"
of eB: echo "B"
of eC: echo "C"
block tbasic:
type
MyEnum = enum
A,B,C,D
# trick the optimizer with an seq:
var x = @[A,B,C,D]
echo x[0],x[1],x[2],x[3],MyEnum(2)
block talias:
# bug #5148
type
A = enum foo, bar
B = A
echo B.foo
block thole:
type Holed = enum
hFirst = (0,"first")
hSecond = (32,"second")
hThird = (64,"third")
var x = @[0,32,64] # This is just to avoid the compiler inlining the value of the enum
echo Holed(x[0]),ord Holed(x[0]),Holed(x[1]),ord Holed(x[1]),Holed(x[2]),ord Holed(x[2])
block toffset:
const
strValB = "my value B"
type
TMyEnum = enum
valueA = (1, "my value A"),
valueB = strValB & "conc",
valueC,
valueD = (4, "abc")
proc getValue(i:int): TMyEnum = TMyEnum(i)
# trick the optimizer with a variable:
var x = getValue(4)
echo getValue(1), ord(valueA), getValue(2), ord(valueB), getValue(3), getValue(4), ord(valueD), x
block tnamedfields:
const strValB = "my value B"
type
TMyEnum = enum
valueA = (0, "my value A"),
valueB = strValB & "conc",
valueC,
valueD = (3, "abc"),
valueE = 4
# trick the optimizer with a variable:
var x = valueD
echo valueA, ord(valueA), valueB, ord(valueB), valueC, valueD, ord(valueD), x
block toptions:
type
# please make sure we have under 32 options (improves code efficiency!)
TOption = enum
optNone, optForceFullMake, optBoehmGC, optRefcGC, optRangeCheck,
optBoundsCheck, optOverflowCheck, optNilCheck, optAssert, optLineDir,
optWarns, optHints, optListCmd, optCompileOnly,
optSafeCode, # only allow safe code
optStyleCheck, optOptimizeSpeed, optOptimizeSize, optGenDynLib,
optGenGuiApp, optStackTrace
TOptionset = set[TOption]
var
gOptions: TOptionset = {optRefcGC, optRangeCheck, optBoundsCheck,
optOverflowCheck, optAssert, optWarns, optHints, optLineDir, optStackTrace}
compilerArgs: int
gExitcode: int8

View File

@@ -1,16 +0,0 @@
# Test that enum with holes is handled correctly by case statement
type
TEnumHole = enum
eA = 0,
eB = 4,
eC = 5
var
e: TEnumHole = eB
case e
of eA: echo "A"
of eB: echo "B"
of eC: echo "C"

View File

@@ -1,16 +0,0 @@
# Test enum with explicit size
type
TEnumHole {.size: sizeof(int).} = enum
eA = 0,
eB = 4,
eC = 5
var
e: TEnumHole = eB
case e
of eA: echo "A"
of eB: echo "B"
of eC: echo "C"

View File

@@ -1,7 +0,0 @@
# bug #5148
type
A = enum foo, bar
B = A
echo B.foo

View File

@@ -1,17 +0,0 @@
discard """
file: "tenumhole.nim"
output: "first0second32third64"
"""
type Holed = enum
hFirst = (0,"first")
hSecond = (32,"second")
hThird = (64,"third")
var x = @[0,32,64] # This is just to avoid the compiler inlining the value of the enum
echo Holed(x[0]),ord Holed(x[0]),Holed(x[1]),ord Holed(x[1]),Holed(x[2]),ord Holed(x[2])

View File

@@ -1,20 +0,0 @@
discard """
file: "tenumoffset.nim"
output: "my value A1my value Bconc2valueCabc4abc"
"""
const
strValB = "my value B"
type
TMyEnum = enum
valueA = (1, "my value A"),
valueB = strValB & "conc",
valueC,
valueD = (4, "abc")
proc getValue(i:int): TMyEnum = TMyEnum(i)
# trick the optimizer with a variable:
var x = getValue(4)
echo getValue(1), ord(valueA), getValue(2), ord(valueB), getValue(3), getValue(4), ord(valueD), x

View File

@@ -1,23 +0,0 @@
discard """
file: "tnamedenumfields.nim"
output: "my value A0my value Bconc1valueCabc3abc"
"""
const
strValB = "my value B"
type
TMyEnum = enum
valueA = (0, "my value A"),
valueB = strValB & "conc",
valueC,
valueD = (3, "abc"),
valueE = 4
# trick the optimizer with a variable:
var x = valueD
echo valueA, ord(valueA), valueB, ord(valueB), valueC, valueD, ord(valueD), x

View File

@@ -1,18 +0,0 @@
type
# please make sure we have under 32 options (improves code efficiency!)
TOption = enum
optNone, optForceFullMake, optBoehmGC, optRefcGC, optRangeCheck,
optBoundsCheck, optOverflowCheck, optNilCheck, optAssert, optLineDir,
optWarns, optHints, optListCmd, optCompileOnly,
optSafeCode, # only allow safe code
optStyleCheck, optOptimizeSpeed, optOptimizeSize, optGenDynLib,
optGenGuiApp, optStackTrace
TOptionset = set[TOption]
var
gOptions: TOptionset = {optRefcGC, optRangeCheck, optBoundsCheck,
optOverflowCheck, optAssert, optWarns, optHints, optLineDir, optStackTrace}
compilerArgs: int
gExitcode: int8

View File

@@ -1,21 +0,0 @@
discard """
output: "1"
"""
type
TMyTuple = tuple[a, b: int]
proc indexOf*(t: typedesc, name: string): int =
## takes a tuple and looks for the field by name.
## returs index of that field.
var
d: t
i = 0
for n, x in fieldPairs(d):
if n == name: return i
i.inc
raise newException(ValueError, "No field " & name & " in type " &
astToStr(t))
echo TMyTuple.indexOf("b")

View File

@@ -15,32 +15,100 @@ b: b
x: 5
y: 6
z: abc
a char: true
a char: false
an int: 5
an int: 6
a string: abc
a string: I'm root!
CMP false
CMP true
CMP true
CMP false
CMP true
CMP true
a: a
b: b
x: 5
y: 6
z: abc
thaRootMan: I'm root!
myDisc: enC
c: Z
enC
Z
'''
"""
type
TMyTuple = tuple[a, b: char, x, y: int, z: string]
block titerator1:
type
TMyTuple = tuple[a, b: char, x, y: int, z: string]
proc p(x: char) = echo "a char: ", x <= 'a'
proc p(x: int) = echo "an int: ", x
proc p(x: string) = echo "a string: ", x
proc p(x: char) = echo "a char: ", x <= 'a'
proc p(x: int) = echo "an int: ", x
proc p(x: string) = echo "a string: ", x
var x: TMyTuple = ('a', 'b', 5, 6, "abc")
var y: TMyTuple = ('A', 'b', 5, 9, "abc")
var x: TMyTuple = ('a', 'b', 5, 6, "abc")
var y: TMyTuple = ('A', 'b', 5, 9, "abc")
for f in fields(x):
p f
for f in fields(x):
p f
for a, b in fields(x, y):
echo a == b
for a, b in fields(x, y):
echo a == b
for key, val in fieldPairs(x):
echo key, ": ", val
for key, val in fieldPairs(x):
echo key, ": ", val
assert x != y
assert x == x
assert(not (x < x))
assert x <= x
assert y < x
assert y <= x
assert x != y
assert x == x
assert(not (x < x))
assert x <= x
assert y < x
assert y <= x
block titerator2:
type
SomeRootObj = object of RootObj
thaRootMan: string
TMyObj = object of SomeRootObj
a, b: char
x, y: int
z: string
TEnum = enum enA, enB, enC
TMyCaseObj = object
case myDisc: TEnum
of enA: a: int
of enB: b: string
of enC: c: char
proc p(x: char) = echo "a char: ", x <= 'a'
proc p(x: int) = echo "an int: ", x
proc p(x: string) = echo "a string: ", x
proc myobj(a, b: char, x, y: int, z: string): TMyObj =
result.a = a; result.b = b; result.x = x; result.y = y; result.z = z
result.thaRootMan = "I'm root!"
var x = myobj('a', 'b', 5, 6, "abc")
var y = myobj('A', 'b', 5, 9, "abc")
for f in fields(x):
p f
for a, b in fields(x, y):
echo "CMP ", a == b
for key, val in fieldPairs(x):
echo key, ": ", val
var co: TMyCaseObj
co.myDisc = enC
co.c = 'Z'
for key, val in fieldPairs(co):
echo key, ": ", val
for val in fields(co):
echo val

View File

@@ -1,70 +0,0 @@
discard """
output: '''
a char: true
a char: false
an int: 5
an int: 6
a string: abc
a string: I'm root!
CMP false
CMP true
CMP true
CMP false
CMP true
CMP true
a: a
b: b
x: 5
y: 6
z: abc
thaRootMan: I'm root!
myDisc: enC
c: Z
enC
Z
'''
"""
type
SomeRootObj = object of RootObj
thaRootMan: string
TMyObj = object of SomeRootObj
a, b: char
x, y: int
z: string
TEnum = enum enA, enB, enC
TMyCaseObj = object
case myDisc: TEnum
of enA: a: int
of enB: b: string
of enC: c: char
proc p(x: char) = echo "a char: ", x <= 'a'
proc p(x: int) = echo "an int: ", x
proc p(x: string) = echo "a string: ", x
proc myobj(a, b: char, x, y: int, z: string): TMyObj =
result.a = a; result.b = b; result.x = x; result.y = y; result.z = z
result.thaRootMan = "I'm root!"
var x = myobj('a', 'b', 5, 6, "abc")
var y = myobj('A', 'b', 5, 9, "abc")
for f in fields(x):
p f
for a, b in fields(x, y):
echo "CMP ", a == b
for key, val in fieldPairs(x):
echo key, ": ", val
var co: TMyCaseObj
co.myDisc = enC
co.c = 'Z'
for key, val in fieldPairs(co):
echo key, ": ", val
for val in fields(co):
echo val

108
tests/fields/tfields.nim Normal file
View File

@@ -0,0 +1,108 @@
discard """
output: '''
n
n
(one: 1, two: 2, three: 3)
1
2
3
(one: 4, two: 5, three: 6)
4
(one: 7, two: 8, three: 9)
7
8
9
(foo: 38, other: "string here")
43
100
90
'''
"""
block tindex:
type
TMyTuple = tuple[a, b: int]
proc indexOf(t: typedesc, name: string): int =
## takes a tuple and looks for the field by name.
## returs index of that field.
var
d: t
i = 0
for n, x in fieldPairs(d):
if n == name: return i
i.inc
raise newException(ValueError, "No field " & name & " in type " &
astToStr(t))
doAssert TMyTuple.indexOf("b") == 1
block ttemplate:
# bug #1902
# This works.
for name, value in (n: "v").fieldPairs:
echo name
template wrapper: typed =
for name, value in (n: "v").fieldPairs:
echo name
wrapper()
block tbreak:
# bug #2134
type
TestType = object
one: int
two: int
three: int
var
ab = TestType(one:1, two:2, three:3)
ac = TestType(one:4, two:5, three:6)
ad = TestType(one:7, two:8, three:9)
tstSeq = [ab, ac, ad]
for tstElement in mitems(tstSeq):
echo tstElement
for tstField in fields(tstElement):
#for tstField in [1,2,4,6]:
echo tstField
if tstField == 4:
break
block timplicit_with_partial:
type
Base = ref object of RootObj
Foo {.partial.} = ref object of Base
proc my(f: Foo) =
#var f.next = f
let f.foo = 38
let f.other = "string here"
echo f[]
echo f.foo + 5
var g: Foo
new(g)
my(g)
type
FooTask {.partial.} = ref object of RootObj
proc foo(t: FooTask) {.liftLocals: t.} =
var x = 90
if true:
var x = 10
while x < 100:
inc x
echo x
echo x
foo(FooTask())

View File

@@ -1,15 +0,0 @@
discard """
output: '''n
n'''
"""
# bug #1902
# This works.
for name, value in (n: "v").fieldPairs:
echo name
# This doesn't compile - "expression 'name' has no type (or is ambiguous)".
template wrapper: typed =
for name, value in (n: "v").fieldPairs:
echo name
wrapper()

View File

@@ -1,33 +0,0 @@
discard """
output: '''(one: 1, two: 2, three: 3)
1
2
3
(one: 4, two: 5, three: 6)
4
(one: 7, two: 8, three: 9)
7
8
9'''
"""
# bug #2134
type
TestType = object
one: int
two: int
three: int
var
ab = TestType(one:1, two:2, three:3)
ac = TestType(one:4, two:5, three:6)
ad = TestType(one:7, two:8, three:9)
tstSeq = [ab, ac, ad]
for tstElement in mitems(tstSeq):
echo tstElement
for tstField in fields(tstElement):
#for tstField in [1,2,4,6]:
echo tstField
if tstField == 4:
break

View File

@@ -1,35 +0,0 @@
discard """
output: '''(foo: 38, other: "string here")
43
100
90'''
"""
type
Base = ref object of RootObj
Foo {.partial.} = ref object of Base
proc my(f: Foo) =
#var f.next = f
let f.foo = 38
let f.other = "string here"
echo f[]
echo f.foo + 5
var g: Foo
new(g)
my(g)
type
FooTask {.partial.} = ref object of RootObj
proc foo(t: FooTask) {.liftLocals: t.} =
var x = 90
if true:
var x = 10
while x < 100:
inc x
echo x
echo x
foo(FooTask())

View File

@@ -1,8 +1,19 @@
discard """
output: '''2
88'''
output: '''
1
2
3
4
2
88
'''
"""
for x in [1, 2, 3, 4]:
echo x
type
TValue* {.pure, final.} = object of RootObj
a: int
@@ -32,4 +43,4 @@ block:
var indirect = p
x.indirect(44)
echo x[]
echo x[]

View File

@@ -1,4 +0,0 @@
for x in [1, 2, 3, 4]:
echo x

View File

@@ -1,14 +0,0 @@
discard """
output: '''1'''
"""
# bug #3837
iterator t1(): int {.closure.} =
yield 1
iterator t2(): int {.closure.} =
for i in t1():
yield i
for i in t2():
echo $i

View File

@@ -1,20 +0,0 @@
discard """
output: '''0
1
2
3
4
'''
"""
proc moo(): iterator (): int =
iterator fooGen: int {.closure.} =
while true:
yield result
result.inc
return fooGen
var foo = moo()
for i in 0 .. 4:
echo foo()

View File

@@ -1,7 +0,0 @@
iterator xy[T](a: T, b: set[T]): T =
if a in b:
yield a
for a in xy(1'i8, {}):
for b in xy(a, {}):
echo a

View File

@@ -1,81 +0,0 @@
discard """
output: '''start
false
0
1
2
end
@[2, 4, 6, 8, 10]
@[4, 8, 12, 16, 20]'''
"""
# bug #3837
proc iter1(): (iterator: int) =
let coll = [0,1,2]
result = iterator: int {.closure.} =
for i in coll:
yield i
proc iter2(it: (iterator: int)): (iterator: int) =
result = iterator: int {.closure.} =
echo finished(it)
for i in it():
yield i
echo "start"
let myiter1 = iter1()
let myiter2 = iter2(myiter1)
for i in myiter2():
echo i
echo "end"
# start
# false
# end
from sequtils import toSeq
type Iterable*[T] = (iterator: T) | Slice[T]
## Everything that can be iterated over, iterators and slices so far.
proc toIter*[T](s: Slice[T]): iterator: T =
## Iterate over a slice.
iterator it: T {.closure.} =
for x in s.a..s.b:
yield x
return it
proc toIter*[T](i: iterator: T): iterator: T =
## Nop
i
iterator map*[T,S](i: Iterable[T], f: proc(x: T): S): S =
let i = toIter(i)
for x in i():
yield f(x)
proc filter*[T](i: Iterable[T], f: proc(x: T): bool): iterator: T =
## Iterates through an iterator and yields every item that fulfills the
## predicate `f`.
##
## .. code-block:: nim
## for x in filter(1..11, proc(x): bool = x mod 2 == 0):
## echo x
let i = toIter(i)
iterator it: T {.closure.} =
for x in i():
if f(x):
yield x
result = it
iterator filter*[T](i: Iterable[T], f: proc(x: T): bool): T =
let i = toIter(i)
for x in i():
if f(x):
yield x
var it = toSeq(filter(2..10, proc(x: int): bool = x mod 2 == 0))
echo it # @[2, 4, 6, 8, 10]
it = toSeq(map(filter(2..10, proc(x: int): bool = x mod 2 == 0), proc(x: int): int = x * 2))
echo it # Expected output: @[4, 8, 12, 16, 20], Actual output: @[]

View File

@@ -1,33 +0,0 @@
# bug #3221
import algorithm, math, sequtils
iterator permutations[T](ys: openarray[T]): seq[T] =
var
d = 1
c = newSeq[int](ys.len)
xs = newSeq[T](ys.len)
for i, y in ys: xs[i] = y
yield xs
block outer:
while true:
while d > 1:
dec d
c[d] = 0
while c[d] >= d:
inc d
if d >= ys.len: break outer
let i = if (d and 1) == 1: c[d] else: 0
swap xs[i], xs[d]
yield xs
inc c[d]
proc dig_vectors(): void =
var v_nums: seq[int]
v_nums = newSeq[int](1)
for perm in permutations(toSeq(0 .. 1)):
v_nums[0] = 1
dig_vectors()

215
tests/iter/tissues.nim Normal file
View File

@@ -0,0 +1,215 @@
discard """
output: '''
0
1
2
3
4
1
start
false
0
1
2
end
@[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42]
1002
0
1
2
7
'''
"""
import algorithm, math, sequtils, strutils
block t338:
proc moo(): iterator (): int =
iterator fooGen: int {.closure.} =
while true:
yield result
result.inc
return fooGen
var foo = moo()
for i in 0 .. 4:
echo foo()
block t8041:
iterator xy[T](a: T, b: set[T]): T =
if a in b:
yield a
for a in xy(1'i8, {}):
for b in xy(a, {}):
echo a
block t3837_chained:
iterator t1(): int {.closure.} =
yield 1
iterator t2(): int {.closure.} =
for i in t1():
yield i
for i in t2():
echo $i
proc iter1(): (iterator: int) =
let coll = [0,1,2]
result = iterator: int {.closure.} =
for i in coll:
yield i
proc iter2(it: (iterator: int)): (iterator: int) =
result = iterator: int {.closure.} =
echo finished(it)
for i in it():
yield i
echo "start"
let myiter1 = iter1()
let myiter2 = iter2(myiter1)
for i in myiter2():
echo i
echo "end"
type Iterable[T] = (iterator: T) | Slice[T]
## Everything that can be iterated over, iterators and slices so far.
proc toIter[T](s: Slice[T]): iterator: T =
## Iterate over a slice.
iterator it: T {.closure.} =
for x in s.a..s.b:
yield x
return it
proc toIter[T](i: iterator: T): iterator: T =
## Nop
i
iterator map[T,S](i: Iterable[T], f: proc(x: T): S): S =
let i = toIter(i)
for x in i():
yield f(x)
proc filter[T](i: Iterable[T], f: proc(x: T): bool): iterator: T =
let i = toIter(i)
iterator it: T {.closure.} =
for x in i():
if f(x):
yield x
result = it
iterator filter[T](i: Iterable[T], f: proc(x: T): bool): T =
let i = toIter(i)
for x in i():
if f(x):
yield x
var it = toSeq(filter(2..10, proc(x: int): bool = x mod 2 == 0))
doAssert it == @[2, 4, 6, 8, 10]
it = toSeq(map(filter(2..10, proc(x: int): bool = x mod 2 == 0), proc(x: int): int = x * 2))
doAssert it == @[4, 8, 12, 16, 20]
block t3221_complex:
iterator permutations[T](ys: openarray[T]): seq[T] =
var
d = 1
c = newSeq[int](ys.len)
xs = newSeq[T](ys.len)
for i, y in ys: xs[i] = y
yield xs
block outer:
while true:
while d > 1:
dec d
c[d] = 0
while c[d] >= d:
inc d
if d >= ys.len: break outer
let i = if (d and 1) == 1: c[d] else: 0
swap xs[i], xs[d]
yield xs
inc c[d]
proc dig_vectors(): void =
var v_nums: seq[int]
v_nums = newSeq[int](1)
for perm in permutations(toSeq(0 .. 1)):
v_nums[0] = 1
dig_vectors()
block t3499_keepstate:
proc slice[T](iter: iterator(): T {.closure.}, sl: auto): seq[T] =
var res: seq[int64] = @[]
var i = 0
for n in iter():
if i > sl.b:
break
if i >= sl.a:
res.add(n)
inc i
res
iterator harshad(): int64 {.closure.} =
for n in 1 ..< int64.high:
var sum = 0
for ch in string($n):
sum += parseInt("" & ch)
if n mod sum == 0:
yield n
echo harshad.slice 0 ..< 20
for n in harshad():
if n > 1000:
echo n
break
# bug #3499 last snippet fixed
# bug 705 last snippet fixed
block t1725_nested:
iterator factory(): int {.closure.} =
iterator bar(): int {.closure.} =
yield 0
yield 1
yield 2
for x in bar(): yield x
for x in factory():
echo x
block t2023_objiter:
type
Obj = object
iter: iterator (): int8 {.closure.}
iterator test(): int8 {.closure.} =
yield 7
proc init():Obj=
result.iter = test
var o = init()
echo(o.iter())

View File

@@ -1,36 +0,0 @@
discard """
output: '''@[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42]
1002'''
"""
import strutils
proc slice[T](iter: iterator(): T {.closure.}, sl: auto): seq[T] =
var res: seq[int64] = @[]
var i = 0
for n in iter():
if i > sl.b:
break
if i >= sl.a:
res.add(n)
inc i
res
iterator harshad(): int64 {.closure.} =
for n in 1 .. < int64.high:
var sum = 0
for ch in string($n):
sum += parseInt("" & ch)
if n mod sum == 0:
yield n
echo harshad.slice 0 .. <20
for n in harshad():
if n > 1000:
echo n
break
# bug #3499 last snippet fixed
# bug 705 last snippet fixed

View File

@@ -1,16 +0,0 @@
discard """
output: '''0
1
2'''
"""
# bug #1725
iterator factory(): int {.closure.} =
iterator bar(): int {.closure.} =
yield 0
yield 1
yield 2
for x in bar(): yield x
for x in factory():
echo x

View File

@@ -1,18 +0,0 @@
discard """
output: "7"
"""
# bug #2023
type
Obj = object
iter: iterator (): int8 {.closure.}
iterator test(): int8 {.closure.} =
yield 7
proc init():Obj=
result.iter = test
var o = init()
echo(o.iter())