tnewruntime_misc.nim 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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: 12, deallocCount: 10)
  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 newTableOwned[A, B](initialSize = defaultInitialSize): owned(TableRef[A, B]) =
  23. new(result)
  24. result[] = initTable[A, B](initialSize)
  25. proc main =
  26. var w = newTableOwned[string, owned Node]()
  27. w["key"] = Node(field: "value")
  28. echo w["key"][]
  29. echo getEnv("HEAPTRASHING")
  30. # bug #11891
  31. var x = "abc"
  32. x[1] = 'x'
  33. echo x
  34. main()
  35. # bug #11745
  36. type
  37. Foo = object
  38. bar: seq[int]
  39. var x = [Foo()]
  40. # bug #11563
  41. type
  42. MyTypeType = enum
  43. Zero, One
  44. MyType = object
  45. case kind: MyTypeType
  46. of Zero:
  47. s*: seq[MyType]
  48. of One:
  49. x*: int
  50. var t: MyType
  51. # bug #11254
  52. proc test(p: owned proc()) =
  53. let x = (proc())p
  54. test(proc() = discard)
  55. # bug #10689
  56. type
  57. O = object
  58. v: int
  59. proc `=sink`(d: var O, s: O) =
  60. d.v = s.v
  61. proc selfAssign =
  62. var o = O(v: 10)
  63. o = o
  64. echo o
  65. selfAssign()
  66. # bug #11833
  67. type FooAt = object
  68. proc testWrongAt() =
  69. var x = @[@[FooAt()]]
  70. testWrongAt()
  71. #-------------------------------------------------
  72. type
  73. Table[A, B] = object
  74. x: seq[(A, B)]
  75. proc toTable[A,B](p: sink openArray[(A, B)]): Table[A, B] =
  76. for zz in mitems(p):
  77. result.x.add move(zz)
  78. let table = {"a": new(int)}.toTable()
  79. # bug # #12051
  80. type
  81. GenericObj[T] = object
  82. val: T
  83. Generic[T] = owned ref GenericObj[T]
  84. proc `=destroy`[T](x: var GenericObj[T]) =
  85. echo "destroying GenericObj[T] ", x.typeof # to know when its being destroyed
  86. proc main12() =
  87. let gnrc = Generic[int](val: 42)
  88. echo "..."
  89. main12()
  90. #####################################################################
  91. ## bug #12827
  92. type
  93. MyObject = object
  94. x: string
  95. y: seq[string]
  96. needs_ref: ref int
  97. proc xx(xml: string): MyObject =
  98. let stream = xml
  99. result.x = xml
  100. defer: echo stream
  101. discard xx("test")
  102. # Windows has 1 extra allocation in `getEnv` - there it allocates parameter to
  103. # `_wgetenv` (WideCString). Therefore subtract by 1 to match other OSes'
  104. # allocation.
  105. when defined(windows):
  106. import std/importutils
  107. privateAccess(AllocStats)
  108. echo getAllocStats() - s1 - AllocStats(allocCount: 1, deallocCount: 1)
  109. else:
  110. echo getAllocStats() - s1
  111. # bug #13457
  112. var s = "abcde"
  113. s.setLen(3)
  114. echo s.cstring.len