tarc.nim 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. discard """
  2. output: '''
  3. @[1, 2, 3]
  4. Success
  5. @["a", "b", "c"]
  6. Hello
  7. 1
  8. 2
  9. 0
  10. List
  11. @["4", "5", "6", "", "", "a", ""]
  12. @["", "", "a", ""]
  13. '''
  14. cmd: '''nim c --gc:arc $file'''
  15. """
  16. import os
  17. import math
  18. import lists
  19. import strutils
  20. proc mkleak() =
  21. # allocate 1 MB via linked lists
  22. let numberOfLists = 100
  23. for i in countUp(1, numberOfLists):
  24. var leakList = initDoublyLinkedList[string]()
  25. let numberOfLeaks = 5000
  26. for j in countUp(1, numberOfLeaks):
  27. leakList.append(newString(200))
  28. proc mkManyLeaks() =
  29. for i in 0..0:
  30. mkleak()
  31. echo "Success"
  32. iterator foobar(c: string): seq[string] {.closure.} =
  33. yield @["a", "b", c]
  34. proc tsimpleClosureIterator =
  35. var myc = "c"
  36. for it in foobar(myc):
  37. echo it
  38. type
  39. LazyList = ref object
  40. c: proc() {.closure.}
  41. proc tlazyList =
  42. let dep = @[1, 2, 3]
  43. var x = LazyList(c: proc () = echo(dep))
  44. x.c()
  45. type
  46. Foo = ref object
  47. proc tleakingNewStmt =
  48. var x: Foo
  49. for i in 0..10:
  50. new(x)
  51. iterator infinite(): int {.closure.} =
  52. var i = 0
  53. while true:
  54. yield i
  55. inc i
  56. iterator take(it: iterator (): int, numToTake: int): int {.closure.} =
  57. var i = 0
  58. for x in it():
  59. if i >= numToTake:
  60. break
  61. yield x
  62. inc i
  63. proc take3 =
  64. for x in infinite.take(3):
  65. discard
  66. type
  67. A = ref object of RootObj
  68. x: int
  69. B = ref object of A
  70. more: string
  71. proc inheritanceBug(param: string) =
  72. var s: (A, A)
  73. s[0] = B(more: "a" & param)
  74. s[1] = B(more: "a" & param)
  75. type
  76. PAsyncHttpServer = ref object
  77. value: string
  78. proc serve(server: PAsyncHttpServer) = discard
  79. proc leakObjConstr =
  80. serve(PAsyncHttpServer(value: "asdas"))
  81. let startMem = getOccupiedMem()
  82. take3()
  83. tlazyList()
  84. inheritanceBug("whatever")
  85. mkManyLeaks()
  86. tsimpleClosureIterator()
  87. tleakingNewStmt()
  88. leakObjConstr()
  89. # bug #12964
  90. type
  91. Token* = ref object of RootObj
  92. Li* = ref object of Token
  93. proc bug12964*() =
  94. var token = Li()
  95. var tokens = @[Token()]
  96. tokens.add token
  97. bug12964()
  98. # bug #13119
  99. import streams
  100. proc bug13119 =
  101. var m = newStringStream("Hello world")
  102. let buffer = m.readStr(5)
  103. echo buffer
  104. m.close
  105. bug13119()
  106. # bug #13105
  107. type
  108. Result[T, E] = object
  109. a: T
  110. b: E
  111. D = ref object
  112. x: int
  113. R = Result[D, int]
  114. proc bug13105 =
  115. for n in [R(b: 1), R(b: 2)]:
  116. echo n.b
  117. bug13105()
  118. echo getOccupiedMem() - startMem
  119. #------------------------------------------------------------------------------
  120. # issue #14294
  121. import tables
  122. type
  123. TagKind = enum
  124. List = 0, Compound
  125. Tag = object
  126. case kind: TagKind
  127. of List:
  128. values: seq[Tag]
  129. of Compound:
  130. compound: Table[string, Tag]
  131. var a = Tag(kind: List)
  132. var b = a
  133. echo a.kind
  134. var c = a
  135. proc testAdd(i: int; yyy: openArray[string]) =
  136. var x: seq[string]
  137. x.add [$i, $(i+1), $(i+2)]
  138. x.add yyy
  139. echo x
  140. var y = newSeq[string](4)
  141. y[2] = "a"
  142. testAdd(4, y)
  143. echo y