tassign.nim 3.6 KB

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