rev: ebafc720a90fa0562f276f82113a43e8e7c3ee88 scopes/testing/test_rc.sc -rw-r--r-- 7.6 KiB View raw Log this file
ebafc720a90f — Leonard Ritter * merged default branch 7 days ago
                                                                                
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# refcounting

using import testing
using import Rc
using import glm

do
    let a = (Rc.wrap (One 303))
    let b = (Rc.new One 303)

    'check a

    let k = ((Rc i32) 3)
    report k
    dump k

    do
        # Rc comparisons are object comparisons
        let m = ((Rc i32) 3)
        test (k == k)
        test (k != m)

    do
        test ((Rc.strong-count k) == 1)
        let k2 = (Rc.clone k)
        test ((Rc.strong-count k) == 2)
    test ((Rc.strong-count k) == 1)

    test (k * 2 == 6)

    k = 12
    test (k == 12)

    let q = ((Rc.view k) * 2)
    test (q == 24)

    dump a b

    let c = ((Rc vec3) 1 2 3)

    test ((Rc.strong-count c) == 1)
    test ((Rc.weak-count c) == 0)

    let nullweak = ((Weak vec3))
    test ((Rc.strong-count nullweak) == 0)
    test ((Rc.weak-count nullweak) == 1)
    let nullweak2 = ((Weak vec3))
    test ((Rc.weak-count nullweak) == 1)
    test ((Rc.weak-count nullweak2) == 1)
    test-error ('upgrade nullweak2)
    let nullweak3 = ('clone nullweak)
    test (nullweak == nullweak2)
    test (nullweak == nullweak3)

    let w = (c as Weak)

    test ((Rc.strong-count c) == 1)
    test ((Rc.weak-count c) == 1)
    test ((Rc.strong-count w) == 1)
    test ((Rc.weak-count w) == 1)

    let v = ('force-upgrade w)
    let p = v

    test ((Rc.strong-count c) == 2)
    test ((Rc.weak-count c) == 1)
    test ((Rc.strong-count w) == 2)
    test ((Rc.weak-count w) == 1)
    test ((Rc.strong-count p) == 2)
    test ((Rc.weak-count p) == 1)

    let w2 = (Rc.clone w)
    test ((Rc.strong-count p) == 2)
    test ((Rc.weak-count p) == 2)
    test (w == w2)
    drop w2

    print c
    test (c.xz == (vec2 1 3))
    drop c
    drop v

    test ((Rc.strong-count w) == 0)
    test ((Rc.weak-count w) == 1)

    test-error ('upgrade w)

    ;

One.test-refcount-balanced;

# recursive declarations
do
    using import struct
    struct Node
        parent : (Rc this-type)

# using strong references to build a tree
    this tests if a complex tree of strong references is cleaned up properly
do
    using import struct
    using import Array

    global deleted_names : (GrowingArray string)

    struct DemoNode
        let RcType = (Rc this-type)
        children : (GrowingArray RcType)
        _name : string

        inline... new
        case (name : string,)
            RcType
                _name = name
        case (parent : RcType, name : string, )
            let self =
                RcType
                    _name = name
            'append parent.children (Rc.clone self)
            self

        inline __== (self other)
            if ((typeof self) == (typeof other))

        inline __drop (self)
            print "deleting" ('name self)
            'append deleted_names ('name self)
            super-type.__drop self

        inline name (self)
            self._name

        inline __repr (self)
            deref self._name

    let root = (DemoNode.new "root")
    do
        let n1 = (DemoNode.new root "n1")
        let n2 = (DemoNode.new root "n2")
        let n3 = (DemoNode.new root "n3")

        let n11 = (DemoNode.new n1 "n11")
        let n21 = (DemoNode.new n2 "n21")
        let n22 = (DemoNode.new n2 "n22")
        let n31 = (DemoNode.new n3 "n31")
        let n32 = (DemoNode.new n3 "n32")
        let n33 = (DemoNode.new n3 "n33")

        let n321 = (DemoNode.new n32 "n321")
        let n322 = (DemoNode.new n32 "n322")

        ;

    drop root
    print "ok"
    test ((countof deleted_names) == 12)
    test ((deleted_names @ 0) == "root")
    test ((deleted_names @ 1) == "n1")
    test ((deleted_names @ 2) == "n11")
    test ((deleted_names @ 3) == "n2")
    test ((deleted_names @ 4) == "n21")
    test ((deleted_names @ 5) == "n22")
    test ((deleted_names @ 6) == "n3")
    test ((deleted_names @ 7) == "n31")
    test ((deleted_names @ 8) == "n32")
    test ((deleted_names @ 9) == "n321")
    test ((deleted_names @ 10) == "n322")
    test ((deleted_names @ 11) == "n33")

# variation of same test to build a tree
    this tests if a complex tree of strong and weak references is updated correctly
do
    using import struct
    using import Array
    using import enum

    struct DemoNode
        let RcType = (Rc this-type)
        let WeakType = RcType.WeakType
        parent : WeakType
        children : (GrowingArray RcType)
        _name : string

        inline... new
        case (name : string,)
            RcType
                _name = name
        case (parent : RcType, name : string, )
            let self =
                RcType
                    parent = parent
                    _name = name
            'append parent.children (Rc.clone self)
            self

        inline __== (self other)
            if ((typeof self) == (typeof other))

        inline __drop (self)
            print "deleting" ('name self)
            super-type.__drop self

        inline name (self)
            self._name

        inline __repr (self)
            deref self._name

        fn child-index (self child)
            for i elem in (enumerate self.children)
                if (elem == child)
                    return i
            else
                assert false "unexpected error in child-index()"
                unreachable;

        fn unparent (self)
            let oldparent =
                try ('upgrade self.parent)
                else
                    return (Rc.clone self)
            let i = ('child-index oldparent self)
            self.parent = (WeakType)
            'remove oldparent.children i

        fn... reparent (self newparent)
            let self = (unparent self)
            self.parent = newparent
            'append newparent.children self

    let root = (DemoNode.new "root")
    do
        let n1 = (DemoNode.new root "n1")
        let n2 = (DemoNode.new root "n2")
        let n3 = (DemoNode.new root "n3")

        let n11 = (DemoNode.new n1 "n11")
        let n21 = (DemoNode.new n2 "n21")
        let n22 = (DemoNode.new n2 "n22")
        let n31 = (DemoNode.new n3 "n31")
        let n32 = (DemoNode.new n3 "n32")
        let n33 = (DemoNode.new n3 "n33")

        let n321 = (DemoNode.new n32 "n321")
        let n322 = (DemoNode.new n32 "n322")
        ;

    fn... print-tree (node, indent = "")
        returning void
        print
            indent .. ('name node)
            Rc.strong-count node
        test ((Rc.strong-count node) == 1)
        indent := indent .. "  "
        for child in node.children
            this-function child indent

    print-tree root
    do
        let node = (root . children @ 1 . children @ 0)
        DemoNode.reparent node root
        ;
    print-tree root

    drop root
    print "ok"

do
    # singleton test
    T := (Rc One)

    fn singleton ()
        using import Option
        global data : (Option T)
        if (not data)
            data = (T 17)
        'force-unwrap data

    local example : T = (Rc.clone (singleton))
    test (example == (singleton))
    ;
test ((One.refcount) == 1)
One.reset-refcount;


do
    using import struct

    # singleton test
    typedef Inner :: i32
        inline __typecall (cls v)
            bitcast v this-type

    struct T
        a : Inner

    RcT := (Rc T)

    fn singleton ()
        using import Option
        global data : (Option RcT)
        if (not data)
            data = (RcT (a = (Inner 17)))
        'force-unwrap data

    let k = (Rc.clone (singleton))
    local example : RcT = k
    # error: value of type %1000:<Rc T> must be unique

do
    using import struct

    struct T
        a = 0

    RcT := (Rc T)

    let k = (Rc.clone (RcT 17))

    local container : RcT
    dump (typeof container) (typeof k)
    container = k

;