diff options
Diffstat (limited to 'tests')
40 files changed, 809 insertions, 802 deletions
diff --git a/tests/controlflow/tbreak.nim b/tests/controlflow/tbreak.nim deleted file mode 100644 index 7deab4caf..000000000 --- a/tests/controlflow/tbreak.nim +++ /dev/null @@ -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() diff --git a/tests/controlflow/tcontinue.nim b/tests/controlflow/tcontinue.nim deleted file mode 100644 index ac4fdc2de..000000000 --- a/tests/controlflow/tcontinue.nim +++ /dev/null @@ -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" diff --git a/tests/controlflow/tcontrolflow.nim b/tests/controlflow/tcontrolflow.nim new file mode 100644 index 000000000..9019e55b2 --- /dev/null +++ b/tests/controlflow/tcontrolflow.nim @@ -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 diff --git a/tests/controlflow/tnestif.nim b/tests/controlflow/tnestif.nim deleted file mode 100644 index 3d8adb337..000000000 --- a/tests/controlflow/tnestif.nim +++ /dev/null @@ -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 - - diff --git a/tests/distinct/t4435.nim b/tests/distinct/t4435.nim deleted file mode 100644 index 4d9979fab..000000000 --- a/tests/distinct/t4435.nim +++ /dev/null @@ -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]) diff --git a/tests/distinct/t7010.nim b/tests/distinct/t7010.nim deleted file mode 100644 index 0cae002be..000000000 --- a/tests/distinct/t7010.nim +++ /dev/null @@ -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 \ No newline at end of file diff --git a/tests/distinct/t9079.nim b/tests/distinct/t9079.nim deleted file mode 100644 index a891ef27d..000000000 --- a/tests/distinct/t9079.nim +++ /dev/null @@ -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 diff --git a/tests/distinct/t9322.nim b/tests/distinct/t9322.nim deleted file mode 100644 index 2501d7fc7..000000000 --- a/tests/distinct/t9322.nim +++ /dev/null @@ -1,12 +0,0 @@ -discard """ - output: apr -""" - -type Fix = distinct string - -proc `$`(f: Fix): string {.borrow.} - -proc mystr(s: string) = - echo s - -mystr($Fix("apr")) diff --git a/tests/distinct/tborrowdot.nim b/tests/distinct/tborrowdot.nim deleted file mode 100644 index 820ee3b71..000000000 --- a/tests/distinct/tborrowdot.nim +++ /dev/null @@ -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" - diff --git a/tests/distinct/tcurrncy.nim b/tests/distinct/tcurrncy.nim deleted file mode 100644 index 2675de739..000000000 --- a/tests/distinct/tcurrncy.nim +++ /dev/null @@ -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 - - - diff --git a/tests/distinct/tdistinct.nim b/tests/distinct/tdistinct.nim new file mode 100644 index 000000000..d200b3e34 --- /dev/null +++ b/tests/distinct/tdistinct.nim @@ -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]) diff --git a/tests/distinct/tdistinct_consts.nim b/tests/distinct/tdistinct_consts.nim deleted file mode 100644 index 4f6ced2d2..000000000 --- a/tests/distinct/tdistinct_consts.nim +++ /dev/null @@ -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]) diff --git a/tests/distinct/tissues.nim b/tests/distinct/tissues.nim new file mode 100644 index 000000000..ce71344d0 --- /dev/null +++ b/tests/distinct/tissues.nim @@ -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")) diff --git a/tests/enum/tbasicenum.nim b/tests/enum/tbasicenum.nim deleted file mode 100644 index eb2182f71..000000000 --- a/tests/enum/tbasicenum.nim +++ /dev/null @@ -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) \ No newline at end of file diff --git a/tests/enum/tenum.nim b/tests/enum/tenum.nim index 6d9bdd539..72f1837f5 100644 --- a/tests/enum/tenum.nim +++ b/tests/enum/tenum.nim @@ -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 diff --git a/tests/enum/tenum2.nim b/tests/enum/tenum2.nim deleted file mode 100644 index 3e34a21ce..000000000 --- a/tests/enum/tenum2.nim +++ /dev/null @@ -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" - diff --git a/tests/enum/tenum3.nim b/tests/enum/tenum3.nim deleted file mode 100644 index 49cbf04d5..000000000 --- a/tests/enum/tenum3.nim +++ /dev/null @@ -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" - diff --git a/tests/enum/tenumalias.nim b/tests/enum/tenumalias.nim deleted file mode 100644 index 2d1f70d0e..000000000 --- a/tests/enum/tenumalias.nim +++ /dev/null @@ -1,7 +0,0 @@ -# bug #5148 - -type - A = enum foo, bar - B = A - -echo B.foo diff --git a/tests/enum/tenumhole.nim b/tests/enum/tenumhole.nim deleted file mode 100644 index 4928572f9..000000000 --- a/tests/enum/tenumhole.nim +++ /dev/null @@ -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]) - - - - diff --git a/tests/enum/tenumoffset.nim b/tests/enum/tenumoffset.nim deleted file mode 100644 index e67164604..000000000 --- a/tests/enum/tenumoffset.nim +++ /dev/null @@ -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 diff --git a/tests/enum/tnamedenumfields.nim b/tests/enum/tnamedenumfields.nim deleted file mode 100644 index e9ac88a42..000000000 --- a/tests/enum/tnamedenumfields.nim +++ /dev/null @@ -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 - - - - diff --git a/tests/enum/toptions.nim b/tests/enum/toptions.nim deleted file mode 100644 index da66f0067..000000000 --- a/tests/enum/toptions.nim +++ /dev/null @@ -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 diff --git a/tests/fields/tfieldindex.nim b/tests/fields/tfieldindex.nim deleted file mode 100644 index 6de6d54bd..000000000 --- a/tests/fields/tfieldindex.nim +++ /dev/null @@ -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") - diff --git a/tests/fields/tfielditerator.nim b/tests/fields/tfielditerator.nim index 6d15ea05d..b1c357997 100644 --- a/tests/fields/tfielditerator.nim +++ b/tests/fields/tfielditerator.nim @@ -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 + + var x: TMyTuple = ('a', 'b', 5, 6, "abc") + var y: TMyTuple = ('A', 'b', 5, 9, "abc") + + for f in fields(x): + p f + + for a, b in fields(x, y): + echo a == b + + 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 + + +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 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: TMyTuple = ('a', 'b', 5, 6, "abc") -var y: TMyTuple = ('A', 'b', 5, 9, "abc") + var x = myobj('a', 'b', 5, 6, "abc") + var y = myobj('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 "CMP ", 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 + 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 \ No newline at end of file diff --git a/tests/fields/tfielditerator2.nim b/tests/fields/tfielditerator2.nim deleted file mode 100644 index c8e230cf5..000000000 --- a/tests/fields/tfielditerator2.nim +++ /dev/null @@ -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 diff --git a/tests/fields/tfields.nim b/tests/fields/tfields.nim new file mode 100644 index 000000000..d52b5e8d2 --- /dev/null +++ b/tests/fields/tfields.nim @@ -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()) \ No newline at end of file diff --git a/tests/fields/tfields_in_template.nim b/tests/fields/tfields_in_template.nim deleted file mode 100644 index b7d5d2343..000000000 --- a/tests/fields/tfields_in_template.nim +++ /dev/null @@ -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() diff --git a/tests/fields/tfields_with_break.nim b/tests/fields/tfields_with_break.nim deleted file mode 100644 index 1f2632692..000000000 --- a/tests/fields/tfields_with_break.nim +++ /dev/null @@ -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 diff --git a/tests/fields/timplicitfieldswithpartial.nim b/tests/fields/timplicitfieldswithpartial.nim deleted file mode 100644 index 937833257..000000000 --- a/tests/fields/timplicitfieldswithpartial.nim +++ /dev/null @@ -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()) diff --git a/tests/implicit/timplictderef.nim b/tests/implicit/timplicit.nim index fcb647217..70f14db53 100644 --- a/tests/implicit/timplictderef.nim +++ b/tests/implicit/timplicit.nim @@ -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[] \ No newline at end of file diff --git a/tests/implicit/timplicititems.nim b/tests/implicit/timplicititems.nim deleted file mode 100644 index dbe321cb6..000000000 --- a/tests/implicit/timplicititems.nim +++ /dev/null @@ -1,4 +0,0 @@ - -for x in [1, 2, 3, 4]: - echo x - diff --git a/tests/iter/t2closureiters.nim b/tests/iter/t2closureiters.nim deleted file mode 100644 index ceb24548c..000000000 --- a/tests/iter/t2closureiters.nim +++ /dev/null @@ -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 diff --git a/tests/iter/t338.nim b/tests/iter/t338.nim deleted file mode 100644 index dbced8b60..000000000 --- a/tests/iter/t338.nim +++ /dev/null @@ -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() diff --git a/tests/iter/t8041.nim b/tests/iter/t8041.nim deleted file mode 100644 index c87134cfc..000000000 --- a/tests/iter/t8041.nim +++ /dev/null @@ -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 diff --git a/tests/iter/tchainediterators2.nim b/tests/iter/tchainediterators2.nim deleted file mode 100644 index c2e5e6170..000000000 --- a/tests/iter/tchainediterators2.nim +++ /dev/null @@ -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: @[] diff --git a/tests/iter/tcomplex_openarray.nim b/tests/iter/tcomplex_openarray.nim deleted file mode 100644 index 6fc191e90..000000000 --- a/tests/iter/tcomplex_openarray.nim +++ /dev/null @@ -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() diff --git a/tests/iter/tissues.nim b/tests/iter/tissues.nim new file mode 100644 index 000000000..773e7dbff --- /dev/null +++ b/tests/iter/tissues.nim @@ -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()) diff --git a/tests/iter/tkeep_state_between_yield.nim b/tests/iter/tkeep_state_between_yield.nim deleted file mode 100644 index f4f0ee363..000000000 --- a/tests/iter/tkeep_state_between_yield.nim +++ /dev/null @@ -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 diff --git a/tests/iter/tnested_closure_iter.nim b/tests/iter/tnested_closure_iter.nim deleted file mode 100644 index ec2253cf1..000000000 --- a/tests/iter/tnested_closure_iter.nim +++ /dev/null @@ -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 diff --git a/tests/iter/tobj_iter.nim b/tests/iter/tobj_iter.nim deleted file mode 100644 index a894755d7..000000000 --- a/tests/iter/tobj_iter.nim +++ /dev/null @@ -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()) |