summary refs log tree commit diff stats
path: root/tests/destructor/tnewruntime_misc.nim
blob: ac061f2f7a1b25ec797bb41b0e23e207f30a2957 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
discard """
  cmd: '''nim cpp -d:nimAllocStats --newruntime --threads:on $file'''
  output: '''(field: "value")
Indeed
axc
(v: 10)
...
destroying GenericObj[T] GenericObj[system.int]
test
(allocCount: 12, deallocCount: 10)
3'''
"""

import system / ansi_c

import tables

type
  Node = ref object
    field: string

# bug #11807
import os
putEnv("HEAPTRASHING", "Indeed")

let s1 = getAllocStats()


proc newTableOwned[A, B](initialSize = defaultInitialSize): owned(TableRef[A, B]) =
  new(result)
  result[] = initTable[A, B](initialSize)

proc main =
  var w = newTableOwned[string, owned Node]()
  w["key"] = Node(field: "value")
  echo w["key"][]
  echo getEnv("HEAPTRASHING")

  # bug #11891
  var x = "abc"
  x[1] = 'x'
  echo x

main()

# bug #11745

type
  Foo = object
    bar: seq[int]

var x = [Foo()]

# bug #11563
type
  MyTypeType = enum
    Zero, One
  MyType = object
    case kind: MyTypeType
    of Zero:
      s*: seq[MyType]
    of One:
      x*: int
var t: MyType

# bug #11254
proc test(p: owned proc()) =
  let x = (proc())p

test(proc() = discard)

# bug #10689

type
  O = object
    v: int

proc `=sink`(d: var O, s: O) =
  d.v = s.v

proc selfAssign =
  var o = O(v: 10)
  o = o
  echo o

selfAssign()

# bug #11833
type FooAt = object

proc testWrongAt() =
  var x = @[@[FooAt()]]

testWrongAt()

#-------------------------------------------------
type
  Table[A, B] = object
    x: seq[(A, B)]


proc toTable[A,B](p: sink openArray[(A, B)]): Table[A, B] =
  for zz in mitems(p):
    result.x.add move(zz)


let table = {"a": new(int)}.toTable()

# bug # #12051

type
  GenericObj[T] = object
    val: T
  Generic[T] = owned ref GenericObj[T]

proc `=destroy`[T](x: var GenericObj[T]) =
  echo "destroying GenericObj[T] ", x.typeof # to know when its being destroyed

proc main12() =
  let gnrc = Generic[int](val: 42)
  echo "..."

main12()

#####################################################################
## bug #12827
type
  MyObject = object
    x: string
    y: seq[string]
    needs_ref: ref int

proc xx(xml: string): MyObject =
  let stream = xml
  result.x  = xml
  defer: echo stream


discard xx("test")
echo getAllocStats() - s1

# bug #13457
var s = "abcde"
s.setLen(3)

echo s.cstring.len