diff options
Diffstat (limited to 'tests/assign')
-rw-r--r-- | tests/assign/moverload_asgn2.nim | 4 | ||||
-rw-r--r-- | tests/assign/tassign.nim | 241 | ||||
-rw-r--r-- | tests/assign/tcopy.nim | 25 | ||||
-rw-r--r-- | tests/assign/tgenericassign.nim | 24 | ||||
-rw-r--r-- | tests/assign/tgenericassigntuples.nim | 16 | ||||
-rw-r--r-- | tests/assign/tobjasgn.nim | 44 | ||||
-rw-r--r-- | tests/assign/tobject_assign.nim | 49 | ||||
-rw-r--r-- | tests/assign/toverload_asgn1.nim | 75 | ||||
-rw-r--r-- | tests/assign/toverload_asgn2.nim | 1 | ||||
-rw-r--r-- | tests/assign/tvariantasgn.nim | 19 |
10 files changed, 282 insertions, 216 deletions
diff --git a/tests/assign/moverload_asgn2.nim b/tests/assign/moverload_asgn2.nim index 6620adbeb..cfea48cd1 100644 --- a/tests/assign/moverload_asgn2.nim +++ b/tests/assign/moverload_asgn2.nim @@ -1,3 +1,7 @@ +discard """ + matrix: "--mm:refc" +""" + type Concrete* = object a*, b*: string diff --git a/tests/assign/tassign.nim b/tests/assign/tassign.nim index 4c173d04f..fdec04d22 100644 --- a/tests/assign/tassign.nim +++ b/tests/assign/tassign.nim @@ -1,31 +1,218 @@ +discard """ + output: +''' +TEMP=C:\Programs\xyz\bin +8 5 0 0 +pre test a:test b:1 c:2 haha:3 +assignment test a:test b:1 c:2 haha:3 +abc123 +''' +""" + +#[ +Concrete '=' +Concrete '=' +Concrete '=' +Concrete '=' +Concrete '=' +GenericT[T] '=' int +GenericT[T] '=' float +GenericT[T] '=' float +GenericT[T] '=' float +GenericT[T] '=' string +GenericT[T] '=' int8 +GenericT[T] '=' bool +GenericT[T] '=' bool +GenericT[T] '=' bool +GenericT[T] '=' bool +]# + +block tassign: # Test the assignment operator for complex types which need RTTI + type + TRec = object + x, y: int + s: string + seq: seq[string] + arr: seq[seq[array[0..3, string]]] + TRecSeq = seq[TRec] + + proc test() = + var + a, b: TRec + a.x = 1 + a.y = 2 + a.s = "Hallo!" + a.seq = @["abc", "def", "ghi", "jkl"] + a.arr = @[] + setLen(a.arr, 4) + a.arr[0] = @[] + a.arr[1] = @[] + + b = a # perform a deep copy here! + b.seq = @["xyz", "huch", "was", "soll"] + doAssert len(a.seq) == 4 + doAssert a.seq[3] == "jkl" + doAssert len(b.seq) == 4 + doAssert b.seq[3] == "soll" + doAssert b.y == 2 + + test() + + + +import strutils +block tcopy: + proc main() = + const + example = r"TEMP=C:\Programs\xyz\bin" + var + a, b: string + p: int + p = find(example, "=") + a = substr(example, 0, p-1) + b = substr(example, p+1) + writeLine(stdout, a & '=' & b) + + main() + + + +block tgenericassign: + type + TAny {.pure.} = object + value: pointer + rawType: pointer + + proc newAny(value, rawType: pointer): TAny = + result.value = value + result.rawType = rawType + + var name: cstring = "example" + + var ret: seq[tuple[name: string, a: TAny]] = @[] + for i in 0 .. 8000: + var tup = ($name, newAny(nil, nil)) + doAssert(tup[0] == "example") + ret.add(tup) + doAssert(ret[ret.len()-1][0] == "example") + + + +block tgenericassign_tuples: + var t, s: tuple[x: string, c: int] + + proc ugh: seq[tuple[x: string, c: int]] = + result = @[("abc", 232)] + + t = ugh()[0] + s = t + s = ugh()[0] + + doAssert s[0] == "abc" + doAssert s[1] == 232 + + + +block tobjasgn: + type + TSomeObj = object of RootObj + a, b: int + PSomeObj = ref object + a, b: int -type - TRec = object - x, y: int - s: string - seq: seq[string] - arr: seq[seq[array[0..3, string]]] - TRecSeq = seq[TRec] + var a = TSomeObj(a: 8) + var b = PSomeObj(a: 5) + echo a.a, " ", b.a, " ", a.b, " ", b.b + + # bug #575 + + type + Something = object of RootObj + a: string + b, c: int32 + + type + Other = object of Something + haha: int + + proc `$`(x: Other): string = + result = "a:" & x.a & " b:" & $x.b & " c:" & $x.c & " haha:" & $x.haha -proc test() = var - a, b: TRec - a.x = 1 - a.y = 2 - a.s = "Hallo!" - a.seq = @["abc", "def", "ghi", "jkl"] - a.arr = @[] - setLen(a.arr, 4) - a.arr[0] = @[] - a.arr[1] = @[] - - b = a # perform a deep copy here! - b.seq = @["xyz", "huch", "was", "soll"] - writeLine(stdout, len(a.seq)) - writeLine(stdout, a.seq[3]) - writeLine(stdout, len(b.seq)) - writeLine(stdout, b.seq[3]) - writeLine(stdout, b.y) - -test() + t: Other + + t.a = "test" + t.b = 1 + t.c = 2 + t.haha = 3 + + echo "pre test ", $t + var x = t + echo "assignment test ", x + + +when false: + type + Concrete = object + a, b: string + + proc `=`(d: var Concrete; src: Concrete) = + shallowCopy(d.a, src.a) + shallowCopy(d.b, src.b) + echo "Concrete '='" + + var x, y: array[0..2, Concrete] + var cA, cB: Concrete + + var cATup, cBTup: tuple[x: int, ha: Concrete] + + x = y + cA = cB + cATup = cBTup + + type + GenericT[T] = object + a, b: T + + proc `=`[T](d: var GenericT[T]; src: GenericT[T]) = + shallowCopy(d.a, src.a) + shallowCopy(d.b, src.b) + echo "GenericT[T] '=' ", typeof(T).name + + var ag: GenericT[int] + var bg: GenericT[int] + + ag = bg + + var xg, yg: array[0..2, GenericT[float]] + var cAg, cBg: GenericT[string] + + var cATupg, cBTupg: tuple[x: int, ha: GenericT[int8]] + + xg = yg + cAg = cBg + cATupg = cBTupg + + var caSeqg, cbSeqg: seq[GenericT[bool]] + newSeq(cbSeqg, 4) + caSeqg = cbSeqg + + when false: + type + Foo = object + case b: bool + of false: xx: GenericT[int] + of true: yy: bool + + var + a, b: Foo + a = b + +block tgeneric_assign_varargs: + template fatal(msgs: varargs[string]) = + for msg in msgs: + stdout.write(msg) + stdout.write('\n') + + fatal "abc", "123" diff --git a/tests/assign/tcopy.nim b/tests/assign/tcopy.nim deleted file mode 100644 index 1e5bc3cba..000000000 --- a/tests/assign/tcopy.nim +++ /dev/null @@ -1,25 +0,0 @@ -discard """ - file: "tcopy.nim" - output: "TEMP=C:\\Programs\\xyz\\bin" -""" -# tests the substr proc - -import - strutils - -proc main() = - const - example = r"TEMP=C:\Programs\xyz\bin" - var - a, b: string - p: int - p = find(example, "=") - a = substr(example, 0, p-1) - b = substr(example, p+1) - writeLine(stdout, a & '=' & b) - #writeLine(stdout, b) - -main() -#OUT TEMP=C:\Programs\xyz\bin - - diff --git a/tests/assign/tgenericassign.nim b/tests/assign/tgenericassign.nim deleted file mode 100644 index bd9c6c21b..000000000 --- a/tests/assign/tgenericassign.nim +++ /dev/null @@ -1,24 +0,0 @@ -discard """ - output: '''came here''' -""" - -type - TAny* = object {.pure.} - value*: pointer - rawType: pointer - -proc newAny(value, rawType: pointer): TAny = - result.value = value - result.rawType = rawType - -var name: cstring = "example" - -var ret: seq[tuple[name: string, a: TAny]] = @[] -for i in 0..8000: - var tup = ($name, newAny(nil, nil)) - assert(tup[0] == "example") - ret.add(tup) - assert(ret[ret.len()-1][0] == "example") - -echo "came here" - diff --git a/tests/assign/tgenericassigntuples.nim b/tests/assign/tgenericassigntuples.nim deleted file mode 100644 index cca244577..000000000 --- a/tests/assign/tgenericassigntuples.nim +++ /dev/null @@ -1,16 +0,0 @@ -discard """ - output: '''abc232''' -""" - -var t, s: tuple[x: string, c: int] - -proc ugh: seq[tuple[x: string, c: int]] = - result = @[("abc", 232)] - -t = ugh()[0] -s = t -s = ugh()[0] - -echo s[0], t[1] - - diff --git a/tests/assign/tobjasgn.nim b/tests/assign/tobjasgn.nim deleted file mode 100644 index e731d9e93..000000000 --- a/tests/assign/tobjasgn.nim +++ /dev/null @@ -1,44 +0,0 @@ -discard """ - output: '''8 5 0 0 -pre test a:test b:1 c:2 haha:3 -assignment test a:test b:1 c:2 haha:3 -''' -""" - -# bug #1005 - -type - TSomeObj = object of TObject - a, b: int - PSomeObj = ref object - a, b: int - -var a = TSomeObj(a: 8) -var b = PSomeObj(a: 5) -echo a.a, " ", b.a, " ", a.b, " ", b.b - -# bug #575 - -type - Something = object of Tobject - a: string - b, c: int32 - -type - Other = object of Something - haha: int - -proc `$`(x: Other): string = - result = "a:" & x.a & " b:" & $x.b & " c:" & $x.c & " haha:" & $x.haha - -var - t: Other - -t.a = "test" -t.b = 1 -t.c = 2 -t.haha = 3 - -echo "pre test ", $t -var x = t -echo "assignment test ", x diff --git a/tests/assign/tobject_assign.nim b/tests/assign/tobject_assign.nim new file mode 100644 index 000000000..8e39ead53 --- /dev/null +++ b/tests/assign/tobject_assign.nim @@ -0,0 +1,49 @@ +# bug #16706 + +block: # reduced example + type + A = object of RootObj + a0: string + B = object + b0: seq[A] + var c = newSeq[A](2) + var d = B(b0: c) + +when true: # original example + import std/[options, tables, times] + + type + Data* = object + shifts*: OrderedTable[int64, Shift] + balance*: float + + Shift* = object + quoted*: bool + date*: DateTime + description*: string + start*: Option[DateTime] + finish*: Option[DateTime] + breakTime*: Option[Duration] + rate*: float + qty: Option[float] + id*: int64 + + let shift = Shift( + quoted: true, + date: parse("2000-01-01", "yyyy-MM-dd"), + description: "abcdef", + start: none(DateTime), + finish: none(DateTime), + breakTime: none(Duration), + rate: 462.11, + qty: some(10.0), + id: getTime().toUnix() + ) + + var shifts: OrderedTable[int64, Shift] + shifts[shift.id] = shift + + discard Data( + shifts: shifts, + balance: 0.00 + ) diff --git a/tests/assign/toverload_asgn1.nim b/tests/assign/toverload_asgn1.nim deleted file mode 100644 index dbc3a71c4..000000000 --- a/tests/assign/toverload_asgn1.nim +++ /dev/null @@ -1,75 +0,0 @@ -discard """ - output: '''Concrete '=' -Concrete '=' -Concrete '=' -Concrete '=' -Concrete '=' -GenericT[T] '=' int -GenericT[T] '=' float -GenericT[T] '=' float -GenericT[T] '=' float -GenericT[T] '=' string -GenericT[T] '=' int8 -GenericT[T] '=' bool -GenericT[T] '=' bool -GenericT[T] '=' bool -GenericT[T] '=' bool''' -""" - -import typetraits - -type - Concrete = object - a, b: string - -proc `=`(d: var Concrete; src: Concrete) = - shallowCopy(d.a, src.a) - shallowCopy(d.b, src.b) - echo "Concrete '='" - -var x, y: array[0..2, Concrete] -var cA, cB: Concrete - -var cATup, cBTup: tuple[x: int, ha: Concrete] - -x = y -cA = cB -cATup = cBTup - -type - GenericT[T] = object - a, b: T - -proc `=`[T](d: var GenericT[T]; src: GenericT[T]) = - shallowCopy(d.a, src.a) - shallowCopy(d.b, src.b) - echo "GenericT[T] '=' ", type(T).name - -var ag: GenericT[int] -var bg: GenericT[int] - -ag = bg - -var xg, yg: array[0..2, GenericT[float]] -var cAg, cBg: GenericT[string] - -var cATupg, cBTupg: tuple[x: int, ha: GenericT[int8]] - -xg = yg -cAg = cBg -cATupg = cBTupg - -var caSeqg, cbSeqg: seq[GenericT[bool]] -newSeq(cbSeqg, 4) -caSeqg = cbSeqg - -when false: - type - Foo = object - case b: bool - of false: xx: GenericT[int] - of true: yy: bool - - var - a, b: Foo - a = b diff --git a/tests/assign/toverload_asgn2.nim b/tests/assign/toverload_asgn2.nim index 243c90494..1104be92b 100644 --- a/tests/assign/toverload_asgn2.nim +++ b/tests/assign/toverload_asgn2.nim @@ -1,6 +1,7 @@ discard """ output: '''i value 88 2aa''' + disabled: "true" """ import moverload_asgn2 diff --git a/tests/assign/tvariantasgn.nim b/tests/assign/tvariantasgn.nim index 46cc23dd1..4c3c38ca5 100644 --- a/tests/assign/tvariantasgn.nim +++ b/tests/assign/tvariantasgn.nim @@ -1,7 +1,7 @@ discard """ - file: "tvariantasgn.nim" output: "came here" """ + #BUG type TAnyKind = enum @@ -15,16 +15,25 @@ type of nkString: strVal: string var s: TAny -s.kind = nkString -s.strVal = "test" +s = TAny(kind: nkString, strVal: "test") var nr: TAny -nr.kind = nkint -nr.intVal = 78 +s = TAny(kind: nkInt, intVal: 78) # s = nr # works nr = s # fails! echo "came here" +block: # bug #12464 + type + Foo = object + case isFunc: bool + of false: nil + of true: + fun: proc(): int + + const i = Foo(isFunc: false) + let j = i + doAssert not j.isFunc |