tassign.nim 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. discard """
  2. output:
  3. '''
  4. TEMP=C:\Programs\xyz\bin
  5. 8 5 0 0
  6. pre test a:test b:1 c:2 haha:3
  7. assignment test a:test b:1 c:2 haha:3
  8. abc123
  9. '''
  10. """
  11. #[
  12. Concrete '='
  13. Concrete '='
  14. Concrete '='
  15. Concrete '='
  16. Concrete '='
  17. GenericT[T] '=' int
  18. GenericT[T] '=' float
  19. GenericT[T] '=' float
  20. GenericT[T] '=' float
  21. GenericT[T] '=' string
  22. GenericT[T] '=' int8
  23. GenericT[T] '=' bool
  24. GenericT[T] '=' bool
  25. GenericT[T] '=' bool
  26. GenericT[T] '=' bool
  27. ]#
  28. block tassign:
  29. # Test the assignment operator for complex types which need RTTI
  30. type
  31. TRec = object
  32. x, y: int
  33. s: string
  34. seq: seq[string]
  35. arr: seq[seq[array[0..3, string]]]
  36. TRecSeq = seq[TRec]
  37. proc test() =
  38. var
  39. a, b: TRec
  40. a.x = 1
  41. a.y = 2
  42. a.s = "Hallo!"
  43. a.seq = @["abc", "def", "ghi", "jkl"]
  44. a.arr = @[]
  45. setLen(a.arr, 4)
  46. a.arr[0] = @[]
  47. a.arr[1] = @[]
  48. b = a # perform a deep copy here!
  49. b.seq = @["xyz", "huch", "was", "soll"]
  50. doAssert len(a.seq) == 4
  51. doAssert a.seq[3] == "jkl"
  52. doAssert len(b.seq) == 4
  53. doAssert b.seq[3] == "soll"
  54. doAssert b.y == 2
  55. test()
  56. import strutils
  57. block tcopy:
  58. proc main() =
  59. const
  60. example = r"TEMP=C:\Programs\xyz\bin"
  61. var
  62. a, b: string
  63. p: int
  64. p = find(example, "=")
  65. a = substr(example, 0, p-1)
  66. b = substr(example, p+1)
  67. writeLine(stdout, a & '=' & b)
  68. main()
  69. block tgenericassign:
  70. type
  71. TAny = object {.pure.}
  72. value: pointer
  73. rawType: pointer
  74. proc newAny(value, rawType: pointer): TAny =
  75. result.value = value
  76. result.rawType = rawType
  77. var name: cstring = "example"
  78. var ret: seq[tuple[name: string, a: TAny]] = @[]
  79. for i in 0 .. 8000:
  80. var tup = ($name, newAny(nil, nil))
  81. doAssert(tup[0] == "example")
  82. ret.add(tup)
  83. doAssert(ret[ret.len()-1][0] == "example")
  84. block tgenericassign_tuples:
  85. var t, s: tuple[x: string, c: int]
  86. proc ugh: seq[tuple[x: string, c: int]] =
  87. result = @[("abc", 232)]
  88. t = ugh()[0]
  89. s = t
  90. s = ugh()[0]
  91. doAssert s[0] == "abc"
  92. doAssert s[1] == 232
  93. block tobjasgn:
  94. type
  95. TSomeObj = object of RootObj
  96. a, b: int
  97. PSomeObj = ref object
  98. a, b: int
  99. var a = TSomeObj(a: 8)
  100. var b = PSomeObj(a: 5)
  101. echo a.a, " ", b.a, " ", a.b, " ", b.b
  102. # bug #575
  103. type
  104. Something = object of RootObj
  105. a: string
  106. b, c: int32
  107. type
  108. Other = object of Something
  109. haha: int
  110. proc `$`(x: Other): string =
  111. result = "a:" & x.a & " b:" & $x.b & " c:" & $x.c & " haha:" & $x.haha
  112. var
  113. t: Other
  114. t.a = "test"
  115. t.b = 1
  116. t.c = 2
  117. t.haha = 3
  118. echo "pre test ", $t
  119. var x = t
  120. echo "assignment test ", x
  121. when false:
  122. type
  123. Concrete = object
  124. a, b: string
  125. proc `=`(d: var Concrete; src: Concrete) =
  126. shallowCopy(d.a, src.a)
  127. shallowCopy(d.b, src.b)
  128. echo "Concrete '='"
  129. var x, y: array[0..2, Concrete]
  130. var cA, cB: Concrete
  131. var cATup, cBTup: tuple[x: int, ha: Concrete]
  132. x = y
  133. cA = cB
  134. cATup = cBTup
  135. type
  136. GenericT[T] = object
  137. a, b: T
  138. proc `=`[T](d: var GenericT[T]; src: GenericT[T]) =
  139. shallowCopy(d.a, src.a)
  140. shallowCopy(d.b, src.b)
  141. echo "GenericT[T] '=' ", typeof(T).name
  142. var ag: GenericT[int]
  143. var bg: GenericT[int]
  144. ag = bg
  145. var xg, yg: array[0..2, GenericT[float]]
  146. var cAg, cBg: GenericT[string]
  147. var cATupg, cBTupg: tuple[x: int, ha: GenericT[int8]]
  148. xg = yg
  149. cAg = cBg
  150. cATupg = cBTupg
  151. var caSeqg, cbSeqg: seq[GenericT[bool]]
  152. newSeq(cbSeqg, 4)
  153. caSeqg = cbSeqg
  154. when false:
  155. type
  156. Foo = object
  157. case b: bool
  158. of false: xx: GenericT[int]
  159. of true: yy: bool
  160. var
  161. a, b: Foo
  162. a = b
  163. block tgeneric_assign_varargs:
  164. template fatal(msgs: varargs[string]) =
  165. for msg in msgs:
  166. stdout.write(msg)
  167. stdout.write('\n')
  168. fatal "abc", "123"