File: cpp11_std_unique_ptr_runme.tcl

package info (click to toggle)
swig 4.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 45,980 kB
  • sloc: cpp: 54,284; ansic: 29,022; java: 17,595; python: 12,734; cs: 10,421; ruby: 7,263; yacc: 6,501; makefile: 5,860; javascript: 5,538; sh: 5,422; perl: 4,246; php: 3,733; ml: 2,198; tcl: 2,015; lisp: 1,448; xml: 115
file content (408 lines) | stat: -rw-r--r-- 9,908 bytes parent folder | download | duplicates (2)
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408

if [ catch { load ./cpp11_std_unique_ptr[info sharedlibextension] Cpp11_std_unique_ptr} err_msg ] {
	puts stderr "Could not load shared object:\n$err_msg"
}


proc checkCount {expected_count} {
  set actual_count [Klass_getTotal_count]
  if {$actual_count != $expected_count} {
    error "Counts incorrect, expected: $expected_count actual: $actual_count"
  }
}

################################# Tcl pointer recycling bug start
#
# ### Possibly related to premature object deletion problem mentioned in newobject1_runme.tcl. ###
#
# While this won't be repeatable on all machines, the following caused the underlying C++
# pointer value for k1 to be reused for k4.
#
# If the C/C++ memory allocator uses the same pointer value again, then a command name that
# contains a pointer encoding, such as, _b09b1148bd550000_p_Klass (not a variable name) will be
# re-used in SWIG_Tcl_NewInstanceObj. The command should have disappeared from the Tcl side when
# the object was deleted, but there is some sort of bug preventing this from happening in this
# scenario as follows:
#
# Below creates a struct via the call to Tcl_CreateObjCommand in
# SWIG_Tcl_NewInstanceObj (creates a command name with a pointer encoding such as
# _50fb3608ce550000_p_Klass) which also makes a second call to Tcl_CreateObjCommand in
# SWIG_Tcl_ObjectConstructor (creates a command name with the name k1).
Klass k1 "one"
# Line below calls Tcl_DeleteCommandFromToken but is only called for the command created in the
# second call (k1) and not the first call to Tcl_CreateObjCommand.
k1 -delete
set k2 [makeKlassUniquePtr "two"]
set k3 [makeKlassUniquePtr "three"]
$k2 -delete
# If the memory allocator uses the same pointer value, then SWIG_Tcl_NewInstanceObj will find
# the undeleted command _50fb3608ce550000_p_Klass and re-use it. This command should surely
# have been deleted !??
set k4 [makeKlassUniquePtr "four"]
$k3 -delete
$k4 -delete
checkCount 0
################################# Tcl pointer recycling bug end

# Test raw pointer handling involving virtual inheritance
KlassInheritance kini "KlassInheritanceInput"
checkCount 1
set s [useKlassRawPtr kini]
kini -delete
checkCount 0


# #### INPUT BY VALUE ####
# unique_ptr as input
Klass kin "KlassInput"
checkCount 1
set s [takeKlassUniquePtr kin]
checkCount 0
if {[kin cget -thisown]} {
  error "thisown should be false"
}
if {$s != "KlassInput"} {
  error "Incorrect string: $s"
}
if {![is_nullptr kin]} {
  error "is_nullptr failed"
}
kin -delete # Should not fail, even though already deleted
checkCount 0

Klass kin "KlassInput"
checkCount 1
set s [takeKlassUniquePtr kin]
checkCount 0
if {[kin cget -thisown]} {
  error "thisown should be false"
}
if {$s != "KlassInput"} {
  error "Incorrect string: $s"
}
if {![is_nullptr kin]} {
  error "is_nullptr failed"
}
set exception_thrown 0
if [ catch { set s [takeKlassUniquePtr kin] } e ] {
  if {[string first "cannot release ownership as memory is not owned" $e] == -1} {
    error "incorrect exception message: $e"
  }
  set exception_thrown 1
}
if {!$exception_thrown} {
  error "double usage of takeKlassUniquePtr should have been an error"
}
kin -delete # Should not fail, even though already deleted
checkCount 0

Klass kin "KlassInput"
set exception_thrown 0
set notowned [get_not_owned_ptr kin]
if [ catch {
  takeKlassUniquePtr notowned
} ] {
  set exception_thrown 1
}
if {!$exception_thrown} {
  error "Should have thrown 'Cannot release ownership as memory is not owned' error"
}
checkCount 1
kin -delete
checkCount 0

KlassInheritance kini "KlassInheritanceInput"
checkCount 1
set s [takeKlassUniquePtr kini]
checkCount 0
if {[kini cget -thisown]} {
  error "thisown should be false"
}
if {$s != "KlassInheritanceInput"} {
  error "Incorrect string: $s"
}
if {![is_nullptr kini]} {
  error "is_nullptr failed"
}
kini -delete # Should not fail, even though already deleted
checkCount 0

takeKlassUniquePtr "NULL"
takeKlassUniquePtr [make_null]
checkCount 0

# overloaded parameters
if {[overloadTest] != 0} {
  error "overloadTest failed"
}
if {[overloadTest "NULL"] != 1} {
  error "overloadTest failed"
}
if {[overloadTest [Klass k "over"]] != 1} {
  error "overloadTest failed"
}
checkCount 0


# #### INPUT BY RVALUE REF ####
# unique_ptr as input
Klass kin "KlassInput"
checkCount 1
set s [moveKlassUniquePtr kin]
checkCount 0
if {[kin cget -thisown]} {
  error "thisown should be false"
}
if {$s != "KlassInput"} {
  error "Incorrect string: $s"
}
if {![is_nullptr kin]} {
  error "is_nullptr failed"
}
kin -delete # Should not fail, even though already deleted
checkCount 0

Klass kin "KlassInput"
checkCount 1
set s [moveKlassUniquePtr kin]
checkCount 0
if {[kin cget -thisown]} {
  error "thisown should be false"
}
if {$s != "KlassInput"} {
  error "Incorrect string: $s"
}
if {![is_nullptr kin]} {
  error "is_nullptr failed"
}
set exception_thrown 0
if [ catch { set s [moveKlassUniquePtr kin] } e ] {
  if {[string first "cannot release ownership as memory is not owned" $e] == -1} {
    error "incorrect exception message: $e"
  }
  set exception_thrown 1
}
if {!$exception_thrown} {
  error "double usage of moveKlassUniquePtr should have been an error"
}
kin -delete # Should not fail, even though already deleted
checkCount 0

Klass kin "KlassInput"
set exception_thrown 0
set notowned [get_not_owned_ptr kin]
if [ catch {
  moveKlassUniquePtr notowned
} ] {
  set exception_thrown 1
}
if {!$exception_thrown} {
  error "Should have thrown 'Cannot release ownership as memory is not owned' error"
}
checkCount 1
kin -delete
checkCount 0

KlassInheritance kini "KlassInheritanceInput"
checkCount 1
set s [moveKlassUniquePtr kini]
checkCount 0
if {[kini cget -thisown]} {
  error "thisown should be false"
}
if {$s != "KlassInheritanceInput"} {
  error "Incorrect string: $s"
}
if {![is_nullptr kini]} {
  error "is_nullptr failed"
}
kini -delete # Should not fail, even though already deleted
checkCount 0

moveKlassUniquePtr "NULL"
moveKlassUniquePtr [make_null]
checkCount 0

# overloaded parameters
if {[moveOverloadTest] != 0} {
  error "moveOverloadTest failed"
}
if {[moveOverloadTest "NULL"] != 1} {
  error "moveOverloadTest failed"
}
if {[moveOverloadTest [Klass k "over"]] != 1} {
  error "moveOverloadTest failed"
}
checkCount 0


# #### INPUT BY NON-CONST LVALUE REF ####
# unique_ptr as input
Klass kin "KlassInput"
checkCount 1
set s [moveRefKlassUniquePtr kin]
checkCount 0
if {[kin cget -thisown]} {
  error "thisown should be false"
}
if {$s != "KlassInput"} {
  error "Incorrect string: $s"
}
if {![is_nullptr kin]} {
  error "is_nullptr failed"
}
kin -delete # Should not fail, even though already deleted
checkCount 0

Klass kin "KlassInput"
checkCount 1
set s [moveRefKlassUniquePtr kin]
checkCount 0
if {[kin cget -thisown]} {
  error "thisown should be false"
}
if {$s != "KlassInput"} {
  error "Incorrect string: $s"
}
if {![is_nullptr kin]} {
  error "is_nullptr failed"
}
set exception_thrown 0
if [ catch { set s [moveRefKlassUniquePtr kin] } e ] {
  if {[string first "cannot release ownership as memory is not owned" $e] == -1} {
    error "incorrect exception message: $e"
  }
  set exception_thrown 1
}
if {!$exception_thrown} {
  error "double usage of moveRefKlassUniquePtr should have been an error"
}
kin -delete # Should not fail, even though already deleted
checkCount 0

Klass kin "KlassInput"
set exception_thrown 0
set notowned [get_not_owned_ptr kin]
if [ catch {
  moveRefKlassUniquePtr notowned
} ] {
  set exception_thrown 1
}
if {!$exception_thrown} {
  error "Should have thrown 'Cannot release ownership as memory is not owned' error"
}
checkCount 1
kin -delete
checkCount 0

KlassInheritance kini "KlassInheritanceInput"
checkCount 1
set s [moveRefKlassUniquePtr kini]
checkCount 0
if {[kini cget -thisown]} {
  error "thisown should be false"
}
if {$s != "KlassInheritanceInput"} {
  error "Incorrect string: $s"
}
if {![is_nullptr kini]} {
  error "is_nullptr failed"
}
kini -delete # Should not fail, even though already deleted
checkCount 0

moveRefKlassUniquePtr "NULL"
moveRefKlassUniquePtr [make_null]
checkCount 0

# overloaded parameters
if {[moveRefOverloadTest] != 0} {
  error "moveRefOverloadTest failed"
}
if {[moveRefOverloadTest "NULL"] != 1} {
  error "moveRefOverloadTest failed"
}
if {[moveRefOverloadTest [Klass k "over"]] != 1} {
  error "moveRefOverloadTest failed"
}
checkCount 0


# #### INPUT BY CONST LVALUE REF ####
# unique_ptr as input
Klass kin "KlassInput"
checkCount 1
set s [useRefKlassUniquePtr kin]
checkCount 1
if {$s != "KlassInput"} {
  error "Incorrect string: $s"
}
kin -delete
checkCount 0

KlassInheritance kini "KlassInheritanceInput"
checkCount 1
set s [useRefKlassUniquePtr kini]
checkCount 1
if {$s != "KlassInheritanceInput"} {
  error "Incorrect string: $s"
}
kini -delete
checkCount 0

useRefKlassUniquePtr "NULL"
useRefKlassUniquePtr [make_null]
checkCount 0

# overloaded parameters
if {[useRefOverloadTest] != 0} {
  error "useRefOverloadTest failed"
}
if {[useRefOverloadTest "NULL"] != 1} {
  error "useRefOverloadTest failed"
}
Klass kin "over"
if {[useRefOverloadTest kin] != 1} {
  error "useRefOverloadTest failed"
}
checkCount 1
kin -delete
checkCount 0


# unique_ptr as output
set k1 [makeKlassUniquePtr "first"]
set k2 [makeKlassUniquePtr "second"]
checkCount 2

$k1 -delete
checkCount 1

if {[$k2 getLabel] != "second"} {
  error "wrong object label"
}

$k2 -delete
checkCount 0

if {[makeNullUniquePtr] != "NULL"} {
  error "null failure"
}

# unique_ptr as output (rvalue ref)
set k1 [makeRVRKlassUniquePtr "first"]
if {[$k1 getLabel] != "first"} {
  error "wrong object label"
}
if {[makeRVRKlassUniquePtr "NULL"] != "NULL"} {
  error "null failure"
}

# unique_ptr as output (lvalue ref)
set k1 [makeRefKlassUniquePtr "lvalueref"]
if {[$k1 getLabel] != "lvalueref"} {
  error "wrong object label"
}
if {[makeRefKlassUniquePtr "NULL"] != "NULL"} {
  error "null failure"
}