tarcmisc.nim 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. discard """
  2. output: '''
  3. 123xyzabc
  4. destroyed: false
  5. destroyed: false
  6. destroyed2: false
  7. destroyed2: false
  8. destroying variable: 2
  9. destroying variable: 1
  10. whiley ends :(
  11. 1
  12. (x: "0")
  13. (x: "1")
  14. (x: "2")
  15. (x: "3")
  16. (x: "4")
  17. (x: "5")
  18. (x: "6")
  19. (x: "7")
  20. (x: "8")
  21. (x: "9")
  22. (x: "10")
  23. 0
  24. new line before - @['a']
  25. new line after - @['a']
  26. finalizer
  27. aaaaa
  28. hello
  29. ok
  30. closed
  31. destroying variable: 20
  32. destroying variable: 10
  33. '''
  34. cmd: "nim c --gc:arc --deepcopy:on $file"
  35. """
  36. proc takeSink(x: sink string): bool = true
  37. proc b(x: sink string): string =
  38. if takeSink(x):
  39. return x & "abc"
  40. proc bbb(inp: string) =
  41. let y = inp & "xyz"
  42. echo b(y)
  43. bbb("123")
  44. # bug #13691
  45. type Variable = ref object
  46. value: int
  47. proc `=destroy`(self: var typeof(Variable()[])) =
  48. echo "destroying variable: ",self.value
  49. proc newVariable(value: int): Variable =
  50. result = Variable()
  51. result.value = value
  52. #echo "creating variable: ",result.value
  53. proc test(count: int) =
  54. var v {.global.} = newVariable(10)
  55. var count = count - 1
  56. if count == 0: return
  57. test(count)
  58. echo "destroyed: ", v.isNil
  59. test(3)
  60. proc test2(count: int) =
  61. #block: #XXX: Fails with block currently
  62. var v {.global.} = newVariable(20)
  63. var count = count - 1
  64. if count == 0: return
  65. test2(count)
  66. echo "destroyed2: ", v.isNil
  67. test2(3)
  68. proc whiley =
  69. var a = newVariable(1)
  70. while true:
  71. var b = newVariable(2)
  72. if true: raise newException(CatchableError, "test")
  73. try:
  74. whiley()
  75. except CatchableError:
  76. echo "whiley ends :("
  77. #------------------------------------------------------------------------------
  78. # issue #13810
  79. import streams
  80. type
  81. A = ref AObj
  82. AObj = object of RootObj
  83. io: Stream
  84. B = ref object of A
  85. x: int
  86. proc `=destroy`(x: var AObj) =
  87. close(x.io)
  88. echo "closed"
  89. var x = B(io: newStringStream("thestream"))
  90. #------------------------------------------------------------------------------
  91. # issue #14003
  92. proc cryptCTR*(nonce: var openArray[char]) =
  93. nonce[1] = 'A'
  94. proc main() =
  95. var nonce1 = "0123456701234567"
  96. cryptCTR(nonce1)
  97. doAssert(nonce1 == "0A23456701234567")
  98. var nonce2 = "01234567"
  99. cryptCTR(nonce2.toOpenArray(0, nonce2.len-1))
  100. doAssert(nonce2 == "0A234567")
  101. main()
  102. # bug #14079
  103. import std/algorithm
  104. let
  105. n = @["c", "b"]
  106. q = @[("c", "2"), ("b", "1")]
  107. assert n.sortedByIt(it) == @["b", "c"], "fine"
  108. assert q.sortedByIt(it[0]) == @[("b", "1"), ("c", "2")], "fails under arc"
  109. #------------------------------------------------------------------------------
  110. # issue #14236
  111. type
  112. MyType = object
  113. a: seq[int]
  114. proc re(x: static[string]): static MyType =
  115. MyType()
  116. proc match(inp: string, rg: static MyType) =
  117. doAssert rg.a.len == 0
  118. match("ac", re"a(b|c)")
  119. #------------------------------------------------------------------------------
  120. # issue #14243
  121. type
  122. Game* = ref object
  123. proc free*(game: Game) =
  124. let a = 5
  125. proc newGame*(): Game =
  126. new(result, free)
  127. var game*: Game
  128. #------------------------------------------------------------------------------
  129. # issue #14333
  130. type
  131. SimpleLoop = object
  132. Lsg = object
  133. loops: seq[ref SimpleLoop]
  134. root: ref SimpleLoop
  135. var lsg: Lsg
  136. lsg.loops.add lsg.root
  137. echo lsg.loops.len
  138. # bug #14495
  139. type
  140. Gah = ref object
  141. x: string
  142. proc bug14495 =
  143. var owners: seq[Gah]
  144. for i in 0..10:
  145. owners.add Gah(x: $i)
  146. var x: seq[Gah]
  147. for i in 0..10:
  148. x.add owners[i]
  149. for i in 0..100:
  150. setLen(x, 0)
  151. setLen(x, 10)
  152. for i in 0..x.len-1:
  153. if x[i] != nil:
  154. echo x[i][]
  155. for o in owners:
  156. echo o[]
  157. bug14495()
  158. # bug #14396
  159. type
  160. Spinny = ref object
  161. t: ref int
  162. text: string
  163. proc newSpinny*(): Spinny =
  164. Spinny(t: new(int), text: "hello")
  165. proc spinnyLoop(x: ref int, spinny: sink Spinny) =
  166. echo x[]
  167. proc start*(spinny: sink Spinny) =
  168. spinnyLoop(spinny.t, spinny)
  169. var spinner1 = newSpinny()
  170. spinner1.start()
  171. # bug #14345
  172. type
  173. SimpleLoopB = ref object
  174. children: seq[SimpleLoopB]
  175. parent: SimpleLoopB
  176. proc addChildLoop(self: SimpleLoopB, loop: SimpleLoopB) =
  177. self.children.add loop
  178. proc setParent(self: SimpleLoopB, parent: SimpleLoopB) =
  179. self.parent = parent
  180. self.parent.addChildLoop(self)
  181. var l = SimpleLoopB()
  182. l.setParent(l)
  183. # bug #14968
  184. import times
  185. let currentTime = now().utc
  186. # bug #14994
  187. import sequtils
  188. var newLine = @['a']
  189. let indent = newSeq[char]()
  190. echo "new line before - ", newline
  191. newline.insert(indent, 0)
  192. echo "new line after - ", newline
  193. # bug #15044
  194. type
  195. Test = ref object
  196. proc test: Test =
  197. # broken
  198. new(result, proc(x: Test) =
  199. echo "finalizer"
  200. )
  201. proc tdirectFinalizer =
  202. discard test()
  203. tdirectFinalizer()
  204. # bug #14480
  205. proc hello(): int =
  206. result = 42
  207. var leaves {.global.} = hello()
  208. doAssert leaves == 42
  209. # bug #15052
  210. proc mutstrings =
  211. var data = "hello"
  212. for c in data.mitems():
  213. c = 'a'
  214. echo data
  215. mutstrings()
  216. # bug #15038
  217. type
  218. Machine = ref object
  219. hello: string
  220. var machineTypes: seq[tuple[factory: proc(): Machine]]
  221. proc registerMachine(factory: proc(): Machine) =
  222. var mCreator = proc(): Machine =
  223. result = factory()
  224. machineTypes.add((factory: mCreator))
  225. proc facproc(): Machine =
  226. result = Machine(hello: "hello")
  227. registerMachine(facproc)
  228. proc createMachine =
  229. for machine in machineTypes:
  230. echo machine.factory().hello
  231. createMachine()
  232. # bug #15122
  233. import tables
  234. type
  235. BENodeKind = enum
  236. tkBytes,
  237. tkList,
  238. tkDict
  239. BENode = object
  240. case kind: BENodeKind
  241. of tkBytes: strVal: string
  242. of tkList: listVal: seq[BENode]
  243. of tkDict: dictVal: Table[string, BENode]
  244. var data = {
  245. "examples": {
  246. "values": BENode(
  247. kind: tkList,
  248. listVal: @[BENode(kind: tkBytes, strVal: "test")]
  249. )
  250. }.toTable()
  251. }.toTable()
  252. # For ARC listVal is empty for some reason
  253. doAssert data["examples"]["values"].listVal[0].strVal == "test"
  254. ###############################################################################
  255. # bug #15405
  256. import parsexml
  257. const test_xml_str = "<A><B>value</B></A>"
  258. var stream = newStringStream(test_xml_str)
  259. var xml: XmlParser
  260. open(xml, stream, "test")
  261. var xml2 = deepCopy(xml)
  262. proc text_parser(xml: var XmlParser) =
  263. var test_passed = false
  264. while true:
  265. xml.next()
  266. case xml.kind
  267. of xmlElementStart:
  268. if xml.elementName == "B":
  269. xml.next()
  270. if xml.kind == xmlCharData and xml.charData == "value":
  271. test_passed = true
  272. of xmlEof: break
  273. else: discard
  274. xml.close()
  275. doAssert(test_passed)
  276. text_parser(xml)
  277. text_parser(xml2)
  278. # bug #15599
  279. type
  280. PixelBuffer = ref object
  281. proc newPixelBuffer(): PixelBuffer =
  282. new(result) do (buffer: PixelBuffer):
  283. echo "ok"
  284. discard newPixelBuffer()