diff options
Diffstat (limited to 'tests/tuples')
-rw-r--r-- | tests/tuples/mnimsconstunpack.nim | 4 | ||||
-rw-r--r-- | tests/tuples/t18125_1.nim | 14 | ||||
-rw-r--r-- | tests/tuples/t18125_2.nim | 20 | ||||
-rw-r--r-- | tests/tuples/t7012.nim | 7 | ||||
-rw-r--r-- | tests/tuples/tinferred_generic_const.nim | 14 | ||||
-rw-r--r-- | tests/tuples/tnimsconstunpack.nim | 8 | ||||
-rw-r--r-- | tests/tuples/ttuples_issues.nim | 211 | ||||
-rw-r--r-- | tests/tuples/ttuples_various.nim | 38 | ||||
-rw-r--r-- | tests/tuples/tuple_with_nil.nim | 15 |
9 files changed, 239 insertions, 92 deletions
diff --git a/tests/tuples/mnimsconstunpack.nim b/tests/tuples/mnimsconstunpack.nim new file mode 100644 index 000000000..65fafc12f --- /dev/null +++ b/tests/tuples/mnimsconstunpack.nim @@ -0,0 +1,4 @@ +proc foo(): tuple[a, b: string] = + result = ("a", "b") + +const (a, b*) = foo() diff --git a/tests/tuples/t18125_1.nim b/tests/tuples/t18125_1.nim new file mode 100644 index 000000000..74fdfe8f5 --- /dev/null +++ b/tests/tuples/t18125_1.nim @@ -0,0 +1,14 @@ +# issue #18125 solved with type inference + +type + Parent = ref object of RootObj + + Child = ref object of Parent + c: char + +func foo(c: char): (Parent, int) = + # Works if you use (Parent(Child(c: c)), 0) + (Child(c: c), 0) + +let x = foo('x')[0] +doAssert Child(x).c == 'x' diff --git a/tests/tuples/t18125_2.nim b/tests/tuples/t18125_2.nim new file mode 100644 index 000000000..fe0a4a8bb --- /dev/null +++ b/tests/tuples/t18125_2.nim @@ -0,0 +1,20 @@ +discard """ + errormsg: "type mismatch: got <(Child, int)> but expected '(Parent, int)'" + line: 17 +""" + +# issue #18125 solved with correct type relation + +type + Parent = ref object of RootObj + + Child = ref object of Parent + c: char + +func foo(c: char): (Parent, int) = + # Works if you use (Parent(Child(c: c)), 0) + let x = (Child(c: c), 0) + x + +let x = foo('x')[0] +doAssert Child(x).c == 'x' diff --git a/tests/tuples/t7012.nim b/tests/tuples/t7012.nim new file mode 100644 index 000000000..32d441ddd --- /dev/null +++ b/tests/tuples/t7012.nim @@ -0,0 +1,7 @@ +discard """ + errormsg: "illegal recursion in type 'Node'" +""" + +type Node[T] = tuple + next: ref Node[T] +var n: Node[int] \ No newline at end of file diff --git a/tests/tuples/tinferred_generic_const.nim b/tests/tuples/tinferred_generic_const.nim new file mode 100644 index 000000000..5ab730c38 --- /dev/null +++ b/tests/tuples/tinferred_generic_const.nim @@ -0,0 +1,14 @@ +discard """ + action: run +""" +block: + proc something(a: string or int or float) = + const (c, d) = (default a.type, default a.type) + +block: + proc something(a: string or int) = + const c = default a.type + +block: + proc something(a: string or int) = + const (c, d, e) = (default a.type, default a.type, default a.type) diff --git a/tests/tuples/tnimsconstunpack.nim b/tests/tuples/tnimsconstunpack.nim new file mode 100644 index 000000000..7860fc0a4 --- /dev/null +++ b/tests/tuples/tnimsconstunpack.nim @@ -0,0 +1,8 @@ +discard """ + action: compile + cmd: "nim e $file" +""" + +import mnimsconstunpack + +doAssert b == "b" diff --git a/tests/tuples/ttuples_issues.nim b/tests/tuples/ttuples_issues.nim index f294f2f1c..70defdfce 100644 --- a/tests/tuples/ttuples_issues.nim +++ b/tests/tuples/ttuples_issues.nim @@ -1,84 +1,133 @@ discard """ -output: '''(a: 1) -(a: 1) -(a: 1, b: 2) -''' + targets: "c cpp js" """ - -import tables - - -block t4479: - type - MyTuple = tuple - num: int - strings: seq[string] - ints: seq[int] - - var foo = MyTuple(( - num: 7, - strings: @[], - ints: @[], - )) - - var bar = ( - num: 7, - strings: @[], - ints: @[], - ).MyTuple - - var fooUnnamed = MyTuple((7, @[], @[])) - var n = 7 - var fooSym = MyTuple((num: n, strings: @[], ints: @[])) - - -block t1910: - var p = newOrderedTable[tuple[a:int], int]() - var q = newOrderedTable[tuple[x:int], int]() - for key in p.keys: - echo key.a - for key in q.keys: - echo key.x - - -block t2121: - type - Item[K,V] = tuple - key: K - value: V - - var q = newseq[Item[int,int]](1) - let (x,y) = q[0] - - -block t2369: - type HashedElem[T] = tuple[num: int, storedVal: ref T] - - proc append[T](tab: var seq[HashedElem[T]], n: int, val: ref T) = - #tab.add((num: n, storedVal: val)) - var he: HashedElem[T] = (num: n, storedVal: val) - #tab.add(he) - - var g: seq[HashedElem[int]] = @[] - - proc foo() = - var x: ref int - new(x) - x[] = 77 - g.append(44, x) - - -block t1986: - proc test(): int64 = - return 0xdeadbeef.int64 - - const items = [ - (var1: test(), var2: 100'u32), - (var1: test(), var2: 192'u32) - ] - -# bug #14911 -echo (a: 1) # works -echo (`a`: 1) # works -echo (`a`: 1, `b`: 2) # Error: named expression expected +# targets include `cpp` because in the past, there were several cpp-specific bugs with tuples. + +import std/tables + +template main() = + block: # bug #4479 + type + MyTuple = tuple + num: int + strings: seq[string] + ints: seq[int] + + var foo = MyTuple(( + num: 7, + strings: @[], + ints: @[], + )) + + var bar = MyTuple ( + num: 7, + strings: @[], + ints: @[], + ) + + var fooUnnamed = MyTuple((7, @[], @[])) + var n = 7 + var fooSym = MyTuple((num: n, strings: @[], ints: @[])) + + block: # bug #1910 + var p = newOrderedTable[tuple[a:int], int]() + var q = newOrderedTable[tuple[x:int], int]() + for key in p.keys: + echo key.a + for key in q.keys: + echo key.x + + block: # bug #2121 + type + Item[K,V] = tuple + key: K + value: V + + var q = newseq[Item[int,int]](1) + let (x,y) = q[0] + + block: # bug #2369 + type HashedElem[T] = tuple[num: int, storedVal: ref T] + + proc append[T](tab: var seq[HashedElem[T]], n: int, val: ref T) = + #tab.add((num: n, storedVal: val)) + var he: HashedElem[T] = (num: n, storedVal: val) + #tab.add(he) + + var g: seq[HashedElem[int]] = @[] + + proc foo() = + var x: ref int + new(x) + x[] = 77 + g.append(44, x) + + block: # bug #1986 + proc test(): int64 = + return 0xdeadbeef.int64 + + const items = [ + (var1: test(), var2: 100'u32), + (var1: test(), var2: 192'u32) + ] + + block: # bug #14911 + doAssert $(a: 1) == "(a: 1)" # works + doAssert $(`a`: 1) == "(a: 1)" # works + doAssert $(`a`: 1, `b`: 2) == "(a: 1, b: 2)" # was: Error: named expression expected + + block: # bug #16822 + var scores: seq[(set[char], int)] = @{{'/'} : 10} + + var x1: set[char] + for item in items(scores): + x1 = item[0] + + doAssert x1 == {'/'} + + var x2: set[char] + for (chars, value) in items(scores): + x2 = chars + + doAssert x2 == {'/'} + + block: # bug #14574 + proc fn(): auto = + let a = @[("foo", (12, 13))] + for (k,v) in a: + return (k,v) + doAssert fn() == ("foo", (12, 13)) + + block: # bug #14574 + iterator fn[T](a:T): lent T = yield a + let a = (10, (11,)) + proc bar(): auto = + for (x,y) in fn(a): + return (x,y) + doAssert bar() == (10, (11,)) + + block: # bug #16331 + type T1 = tuple[a, b: int] + + proc p(b: bool): T1 = + var x: T1 = (10, 20) + x = if b: (x.b, x.a) else: (-x.b, -x.a) + x + + doAssert p(false) == (-20, -10) + doAssert p(true) == (20, 10) + + +proc mainProc() = + # other tests should be in `main` + block: + type A = tuple[x: int, y: int] + doAssert (x: 1, y: 2).A == A (x: 1, y: 2) # MCS => can't use a template + +static: + main() + mainProc() + +main() +mainProc() diff --git a/tests/tuples/ttuples_various.nim b/tests/tuples/ttuples_various.nim index dc060da1e..e392731d2 100644 --- a/tests/tuples/ttuples_various.nim +++ b/tests/tuples/ttuples_various.nim @@ -171,3 +171,41 @@ block tuple_with_seq: echo s (s, 7) t = test(t.a) + +block: # bug #22049 + type A = object + field: tuple[a, b, c: seq[int]] + + func value(v: var A): var tuple[a, b, c: seq[int]] = + v.field + template get(v: A): tuple[a, b, c: seq[int]] = v.value + + var v = A(field: (@[1], @[2], @[3])) + var (a, b, c) = v.get() + + doAssert a == @[1] + doAssert b == @[2] + doAssert c == @[3] + +block: # bug #22054 + type A = object + field: tuple[a: int] + + func value(v: var A): var tuple[a: int] = + v.field + template get(v: A): tuple[a: int] = v.value + + var v = A(field: (a: 1314)) + doAssert get(v)[0] == 1314 + +block: # tuple unpacking assignment with underscore + var + a = 1 + b = 2 + doAssert (a, b) == (1, 2) + (a, _) = (3, 4) + doAssert (a, b) == (3, 2) + (_, a) = (5, 6) + doAssert (a, b) == (6, 2) + (b, _) = (7, 8) + doAssert (a, b) == (6, 7) diff --git a/tests/tuples/tuple_with_nil.nim b/tests/tuples/tuple_with_nil.nim index b3ed2a96b..9cad6eccd 100644 --- a/tests/tuples/tuple_with_nil.nim +++ b/tests/tuples/tuple_with_nil.nim @@ -1,23 +1,16 @@ import macros -from strutils import IdentStartChars import parseutils import unicode import math -import fenv import pegs import streams type - FormatError = object of Exception ## Error in the format string. + FormatError = object of CatchableError ## Error in the format string. Writer = concept W ## Writer to output a character `c`. - when (NimMajor, NimMinor, NimPatch) > (0, 10, 2): - write(W, 'c') - else: - block: - var x: W - write(x, char) + write(W, 'c') FmtAlign = enum ## Format alignment faDefault ## default for given format type @@ -484,7 +477,7 @@ proc writeformat(o: var Writer; b: bool; fmt: Format) = else: raise newException(FormatError, "Boolean values must of one of the following types: s,b,o,x,X,d,n") -proc writeformat(o: var Writer; ary: openarray[system.any]; fmt: Format) = +proc writeformat(o: var Writer; ary: openArray[system.any]; fmt: Format) = ## Write array `ary` according to format `fmt` using output object ## `o` and output function `add`. if ary.len == 0: return @@ -664,7 +657,7 @@ proc literal[T](x: T): NimNode {.compiletime, nosideeffect.} = result = newLit(x) proc generatefmt(fmtstr: string; - args: var openarray[tuple[arg:NimNode, cnt:int]]; + args: var openArray[tuple[arg:NimNode, cnt:int]]; arg: var int;): seq[tuple[val, fmt:NimNode]] {.compiletime.} = ## fmtstr ## the format string |