tnewruntime_misc.nim 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. discard """
  2. cmd: '''nim cpp -d:nimAllocStats --newruntime --threads:on $file'''
  3. output: '''(field: "value")
  4. Indeed
  5. axc
  6. (v: 10)
  7. ...
  8. destroying GenericObj[T] GenericObj[system.int]
  9. test
  10. (allocCount: 13, deallocCount: 11)
  11. 3'''
  12. """
  13. import system / ansi_c
  14. import tables
  15. type
  16. Node = ref object
  17. field: string
  18. # bug #11807
  19. import os
  20. putEnv("HEAPTRASHING", "Indeed")
  21. let s1 = getAllocStats()
  22. proc main =
  23. var w = newTable[string, owned Node]()
  24. w["key"] = Node(field: "value")
  25. echo w["key"][]
  26. echo getEnv("HEAPTRASHING")
  27. # bug #11891
  28. var x = "abc"
  29. x[1] = 'x'
  30. echo x
  31. main()
  32. # bug #11745
  33. type
  34. Foo = object
  35. bar: seq[int]
  36. var x = [Foo()]
  37. # bug #11563
  38. type
  39. MyTypeType = enum
  40. Zero, One
  41. MyType = object
  42. case kind: MyTypeType
  43. of Zero:
  44. s*: seq[MyType]
  45. of One:
  46. x*: int
  47. var t: MyType
  48. # bug #11254
  49. proc test(p: owned proc()) =
  50. let x = (proc())p
  51. test(proc() = discard)
  52. # bug #10689
  53. type
  54. O = object
  55. v: int
  56. proc `=sink`(d: var O, s: O) =
  57. d.v = s.v
  58. proc selfAssign =
  59. var o = O(v: 10)
  60. o = o
  61. echo o
  62. selfAssign()
  63. # bug #11833
  64. type FooAt = object
  65. proc testWrongAt() =
  66. var x = @[@[FooAt()]]
  67. testWrongAt()
  68. #-------------------------------------------------
  69. type
  70. Table[A, B] = object
  71. x: seq[(A, B)]
  72. proc toTable[A,B](p: sink openArray[(A, B)]): Table[A, B] =
  73. for zz in mitems(p):
  74. result.x.add move(zz)
  75. let table = {"a": new(int)}.toTable()
  76. # bug # #12051
  77. type
  78. GenericObj[T] = object
  79. val: T
  80. Generic[T] = owned ref GenericObj[T]
  81. proc `=destroy`[T](x: var GenericObj[T]) =
  82. echo "destroying GenericObj[T] ", x.typeof # to know when its being destroyed
  83. proc main12() =
  84. let gnrc = Generic[int](val: 42)
  85. echo "..."
  86. main12()
  87. #####################################################################
  88. ## bug #12827
  89. type
  90. MyObject = object
  91. x: string
  92. y: seq[string]
  93. needs_ref: ref int
  94. proc xx(xml: string): MyObject =
  95. let stream = xml
  96. result.x = xml
  97. defer: echo stream
  98. discard xx("test")
  99. echo getAllocStats() - s1
  100. # bug #13457
  101. var s = "abcde"
  102. s.setLen(3)
  103. echo s.cstring.len