summary refs log tree commit diff stats
path: root/tests/assign/tassign.nim
blob: da097ca83f0341a2e04ddc5a866537ebc1697554 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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 = 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))
    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

  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

  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


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"