Debian Codegraph @

tests/link.test

raw
1 # Commands covered: none
2 #
3 # This file contains a collection of tests for Tcl_LinkVar and related library
4 # procedures. Sourcing this file into Tcl runs the tests and generates output
5 # for errors. No output means no errors were found.
6 #
7 # Copyright (c) 1993 The Regents of the University of California.
8 # Copyright (c) 1994 Sun Microsystems, Inc.
9 # Copyright (c) 1998-1999 by Scriptics Corporation.
10 #
11 # See the file "license.terms" for information on usage and redistribution of
12 # this file, and for a DISCLAIMER OF ALL WARRANTIES.
13
14 if {"::tcltest" ni [namespace children]} {
15 package require tcltest 2.5
16 namespace import -force ::tcltest::*
17 }
18
19 ::tcltest::loadTestedCommands
20 catch [list package require -exact Tcltest [info patchlevel]]
21
22 testConstraint testlink [llength [info commands testlink]]
23
24 foreach i {int real bool string} {
25 unset -nocomplain $i
26 }
27
28 test link-0.1 {leak test} {testlink} {
29 interp create i
30 load {} Tcltest i
31 i eval {
32 testlink create 1 0 0 0 0 0 0 0 0 0 0 0 0 0
33 namespace delete ::
34 }
35 interp delete i
36 } {}
37
38 test link-1.1 {reading C variables from Tcl} -constraints {testlink} -setup {
39 testlink delete
40 } -body {
41 testlink set 43 1.23 4 - 12341234 64 250 30000 60000 0xbeefbabe 12321 32123 3.25 1231231234
42 testlink create 1 1 1 1 1 1 1 1 1 1 1 1 1 1
43 list $int $real $bool $string $wide
44 } -result {43 1.23 1 NULL 12341234}
45 test link-1.2 {reading C variables from Tcl} -constraints {testlink} -setup {
46 testlink delete
47 } -body {
48 testlink create 1 1 1 1 1 1 1 1 1 1 1 1 1 1
49 testlink set -3 2 0 "A long string with spaces" 43214321 64 250 30000 60000 0xbeefbabe 12321 32123 3.25 1231231234
50 list $int $real $bool $string $wide $int $real $bool $string $wide
51 } -result {-3 2.0 0 {A long string with spaces} 43214321 -3 2.0 0 {A long string with spaces} 43214321}
52
53 test link-2.1 {writing C variables from Tcl} -constraints {testlink} -setup {
54 testlink delete
55 } -body {
56 testlink set 43 1.21 4 - 56785678 64 250 30000 60000 0xbaadbeef 12321 32123 3.25 1231231234
57 testlink create 1 1 1 1 1 1 1 1 1 1 1 1 1 1
58 set int "0o0721"
59 set real -10.5
60 set bool true
61 set string abcdef
62 set wide 135135
63 set char 79
64 set uchar 161
65 set short 8000
66 set ushort 40000
67 set uint 0xc001babe
68 set long 34543
69 set ulong 567890
70 set float 1.0987654321
71 set uwide 357357357357
72 concat [testlink get] | $int $real $bool $string $wide $char $uchar $short $ushort $uint $long $ulong $float $uwide
73 } -result {465 -10.5 1 abcdef 135135 79 161 8000 40000 -1073628482 34543 567890 1.0987653732299805 357357357357 | 0o0721 -10.5 true abcdef 135135 79 161 8000 40000 0xc001babe 34543 567890 1.0987654321 357357357357}
74 test link-2.2 {writing bad values into variables} -setup {
75 testlink delete
76 } -constraints {testlink} -body {
77 testlink set 43 1.23 4 - 56785678 64 250 30000 60000 0xbeefbabe 12321 32123 3.25 1231231234
78 testlink create 1 1 1 1 1 1 1 1 1 1 1 1 1 1
79 list [catch {set int 09a} msg] $msg $int
80 } -result {1 {can't set "int": variable must have integer value} 43}
81 test link-2.3 {writing bad values into variables} -setup {
82 testlink delete
83 } -constraints {testlink} -body {
84 testlink set 43 1.23 4 - 56785678 64 250 30000 60000 0xbeefbabe 12321 32123 3.25 1231231234
85 testlink create 1 1 1 1 1 1 1 1 1 1 1 1 1 1
86 list [catch {set real 1.x3} msg] $msg $real
87 } -result {1 {can't set "real": variable must have real value} 1.23}
88 test link-2.4 {writing bad values into variables} -setup {
89 testlink delete
90 } -constraints {testlink} -body {
91 testlink set 43 1.23 4 - 56785678 64 250 30000 60000 0xbeefbabe 12321 32123 3.25 1231231234
92 testlink create 1 1 1 1 1 1 1 1 1 1 1 1 1 1
93 list [catch {set bool gorp} msg] $msg $bool
94 } -result {1 {can't set "bool": variable must have boolean value} 1}
95 test link-2.5 {writing bad values into variables} -setup {
96 testlink delete
97 } -constraints {testlink} -body {
98 testlink set 43 1.23 4 - 56785678 64 250 30000 60000 0xbeefbabe 12321 32123 3.25 1231231234
99 testlink create 1 1 1 1 1 1 1 1 1 1 1 1 1 1
100 list [catch {set wide gorp} msg] $msg $bool
101 } -result {1 {can't set "wide": variable must have integer value} 1}
102 test link-2.6 {writing C variables from Tcl} -constraints {testlink} -setup {
103 testlink delete
104 } -body {
105 testlink set 43 1.21 4 - 56785678 64 250 30000 60000 0xbaadbeef 12321 32123 3.25 1231231234
106 testlink create 1 1 1 1 1 1 1 1 1 1 1 1 1 1
107 set int "+"
108 set real "+"
109 set bool 1
110 set string "+"
111 set wide "+"
112 set char "+"
113 set uchar "+"
114 set short "+"
115 set ushort "+"
116 set uint "+"
117 set long "+"
118 set ulong "+"
119 set float "+"
120 set uwide "+"
121 concat [testlink get] | $int $real $bool $string $wide $char $uchar $short $ushort $uint $long $ulong $float $uwide
122 } -result {1 1.0 1 + 1 1 1 1 1 1 1 1 1.0 1 | + + 1 + + + + + + + + + + +}
123 test link-2.7 {writing C variables from Tcl} -constraints {testlink} -setup {
124 testlink delete
125 } -body {
126 testlink set 43 1.21 4 - 56785678 64 250 30000 60000 0xbaadbeef 12321 32123 3.25 1231231234
127 testlink create 1 1 1 1 1 1 1 1 1 1 1 1 1 1
128 set int "-"
129 set real "-"
130 set bool 0
131 set string "-"
132 set wide "-"
133 set char "-"
134 set uchar "-"
135 set short "-"
136 set ushort "-"
137 set uint "-"
138 set long "-"
139 set ulong "-"
140 set float "-"
141 set uwide "-"
142 concat [testlink get] | $int $real $bool $string $wide $char $uchar $short $ushort $uint $long $ulong $float $uwide
143 } -result {0 0.0 0 - 0 0 0 0 0 0 0 0 0.0 0 | - - 0 - - - - - - - - - - -}
144 test link-2.8 {writing C variables from Tcl} -constraints {testlink} -setup {
145 testlink delete
146 } -body {
147 testlink set 43 1.21 4 - 56785678 64 250 30000 60000 0xbaadbeef 12321 32123 3.25 1231231234
148 testlink create 1 1 1 1 1 1 1 1 1 1 1 1 1 1
149 set int "0x"
150 set real "0b"
151 set bool 0
152 set string "0"
153 set wide "0O"
154 set char "0X"
155 set uchar "0B"
156 set short "0O"
157 set ushort "0x"
158 set uint "0b"
159 set long "0o"
160 set ulong "0X"
161 set float "0B"
162 set uwide "0O"
163 concat [testlink get] | $int $real $bool $string $wide $char $uchar $short $ushort $uint $long $ulong $float $uwide
164 } -result {0 0.0 0 0 0 0 0 0 0 0 0 0 0.0 0 | 0x 0b 0 0 0O 0X 0B 0O 0x 0b 0o 0X 0B 0O}
165 test link-2.9 {writing C variables from Tcl} -constraints {testlink} -setup {
166 testlink delete
167 } -body {
168 testlink set 43 1.21 4 - 56785678 64 250 30000 60000 0xbaadbeef 12321 32123 3.25 1231231234
169 testlink create 1 1 1 1 1 1 1 1 1 1 1 1 1 1
170 set int 0
171 set real 5000e
172 set bool 0
173 set string 0
174 set wide 0
175 set char 0
176 set uchar 0
177 set short 0
178 set ushort 0
179 set uint 0
180 set long 0
181 set ulong 0
182 set float -60.00e+
183 set uwide 0
184 concat [testlink get] | $int $real $bool $string $wide $char $uchar $short $ushort $uint $long $ulong $float $uwide
185 } -result {0 5000.0 0 0 0 0 0 0 0 0 0 0 -60.0 0 | 0 5000e 0 0 0 0 0 0 0 0 0 0 -60.00e+ 0}
186
187 test link-3.1 {read-only variables} -constraints {testlink} -setup {
188 testlink delete
189 } -body {
190 testlink set 43 1.23 4 - 56785678 64 250 30000 60000 0xbeefbabe 12321 32123 3.25 1231231234
191 testlink create 0 1 1 0 0 0 0 0 0 0 0 0 0 0
192 list [catch {set int 4} msg] $msg $int \
193 [catch {set real 10.6} msg] $msg $real \
194 [catch {set bool no} msg] $msg $bool \
195 [catch {set string "new value"} msg] $msg $string \
196 [catch {set wide 12341234} msg] $msg $wide
197 } -result {1 {can't set "int": linked variable is read-only} 43 0 10.6 10.6 0 no no 1 {can't set "string": linked variable is read-only} NULL 1 {can't set "wide": linked variable is read-only} 56785678}
198 test link-3.2 {read-only variables} -constraints {testlink} -setup {
199 testlink delete
200 } -body {
201 testlink set 43 1.23 4 - 56785678 64 250 30000 60000 0xbeefbabe 12321 32123 3.25 1231231234
202 testlink create 1 0 0 1 1 0 0 0 0 0 0 0 0 0
203 list [catch {set int 4} msg] $msg $int \
204 [catch {set real 10.6} msg] $msg $real \
205 [catch {set bool no} msg] $msg $bool \
206 [catch {set string "new value"} msg] $msg $string\
207 [catch {set wide 12341234} msg] $msg $wide
208 } -result {0 4 4 1 {can't set "real": linked variable is read-only} 1.23 1 {can't set "bool": linked variable is read-only} 1 0 {new value} {new value} 0 12341234 12341234}
209
210 test link-4.1 {unsetting linked variables} -constraints {testlink} -setup {
211 testlink delete
212 } -body {
213 testlink set -6 -2.5 0 stringValue 13579 64 250 30000 60000 0xbeefbabe 12321 32123 3.25 1231231234
214 testlink create 1 1 1 1 1 1 1 1 1 1 1 1 1 1
215 unset int real bool string wide
216 list [catch {set int} msg] $msg [catch {set real} msg] $msg \
217 [catch {set bool} msg] $msg [catch {set string} msg] $msg \
218 [catch {set wide} msg] $msg
219 } -result {0 -6 0 -2.5 0 0 0 stringValue 0 13579}
220 test link-4.2 {unsetting linked variables} -constraints {testlink} -setup {
221 testlink delete
222 } -body {
223 testlink set -6 -2.1 0 stringValue 97531 64 250 30000 60000 0xbeefbabe 12321 32123 3.25 1231231234
224 testlink create 1 1 1 1 1 1 1 1 1 1 1 1 1 1
225 unset int real bool string wide
226 set int 102
227 set real 16
228 set bool true
229 set string newValue
230 set wide 333555
231 lrange [testlink get] 0 4
232 } -result {102 16.0 1 newValue 333555}
233
234 test link-5.1 {unlinking variables} -constraints {testlink} -setup {
235 testlink delete
236 } -body {
237 testlink set -6 -2.25 0 stringValue 13579 64 250 30000 60000 0xbeefbabe 12321 32123 3.25 1231231234
238 testlink delete
239 set int xx1
240 set real qrst
241 set bool bogus
242 set string 12345
243 set wide 875421
244 set char skjdf
245 set uchar dslfjk
246 set short slkf
247 set ushort skrh
248 set uint sfdkfkh
249 set long srkjh
250 set ulong sjkg
251 set float dskjfbjfd
252 set uwide isdfsngs
253 testlink get
254 } -result {-6 -2.25 0 stringValue 13579 64 250 30000 60000 -1091585346 12321 32123 3.25 1231231234}
255 test link-5.2 {unlinking variables} -constraints {testlink} -setup {
256 testlink delete
257 } -body {
258 testlink set -6 -2.25 0 stringValue 97531 64 250 30000 60000 0xbeefbabe 12321 32123 3.25 1231231234
259 testlink create 1 1 1 1 1 1 1 1 1 1 1 1 1 1
260 testlink delete
261 testlink set 25 14.7 7 - 999999 65 251 30001 60001 0xbabebeef 12322 32124 3.125 12312312340
262 list $int $real $bool $string $wide $char $uchar $short $ushort $uint $long $ulong $float $uwide
263 } -result {-6 -2.25 0 stringValue 97531 64 250 30000 60000 3203381950 12321 32123 3.25 1231231234}
264
265 test link-6.1 {errors in setting up link} -setup {
266 testlink delete
267 unset -nocomplain int
268 } -constraints {testlink} -body {
269 set int(44) 1
270 testlink create 1 1 1 1 1 1 1 1 1 1 1 1 1 1
271 } -cleanup {
272 unset -nocomplain int
273 } -returnCodes error -result {can't set "int": variable is array}
274
275 test link-7.1 {access to linked variables via upvar} -setup {
276 testlink delete
277 } -constraints {testlink} -body {
278 proc x {} {
279 upvar int y
280 unset y
281 }
282 testlink create 1 0 0 0 0 0 0 0 0 0 0 0 0 0
283 testlink set 14 {} {} {} {} {} {} {} {} {} {} {} {} {}
284 x
285 list [catch {set int} msg] $msg
286 } -result {0 14}
287 test link-7.2 {access to linked variables via upvar} -setup {
288 testlink delete
289 } -constraints {testlink} -body {
290 proc x {} {
291 upvar int y
292 return [set y]
293 }
294 testlink create 1 0 0 0 0 0 0 0 0 0 0 0 0 0
295 testlink set 0 {} {} {} {} {} {} {} {} {} {} {} {} {}
296 set int
297 testlink set 23 {} {} {} {} {} {} {} {} {} {} {} {} {}
298 x
299 list [x] $int
300 } -result {23 23}
301 test link-7.3 {access to linked variables via upvar} -setup {
302 testlink delete
303 } -constraints {testlink} -body {
304 proc x {} {
305 upvar int y
306 set y 44
307 }
308 testlink create 0 0 0 0 0 0 0 0 0 0 0 0 0 0
309 testlink set 11 {} {} {} {} {} {} {} {} {} {} {} {} {}
310 list [catch x msg] $msg $int
311 } -result {1 {can't set "y": linked variable is read-only} 11}
312 test link-7.4 {access to linked variables via upvar} -setup {
313 testlink delete
314 } -constraints {testlink} -body {
315 proc x {} {
316 upvar int y
317 set y abc
318 }
319 testlink create 1 1 1 1 1 1 1 1 1 1 1 1 1 1
320 testlink set -4 {} {} {} {} {} {} {} {} {} {} {} {} {}
321 list [catch x msg] $msg $int
322 } -result {1 {can't set "y": variable must have integer value} -4}
323 test link-7.5 {access to linked variables via upvar} -setup {
324 testlink delete
325 } -constraints {testlink} -body {
326 proc x {} {
327 upvar real y
328 set y abc
329 }
330 testlink create 1 1 1 1 1 1 1 1 1 1 1 1 1 1
331 testlink set -4 16.75 {} {} {} {} {} {} {} {} {} {} {} {}
332 list [catch x msg] $msg $real
333 } -result {1 {can't set "y": variable must have real value} 16.75}
334 test link-7.6 {access to linked variables via upvar} -setup {
335 testlink delete
336 } -constraints {testlink} -body {
337 proc x {} {
338 upvar bool y
339 set y abc
340 }
341 testlink create 1 1 1 1 1 1 1 1 1 1 1 1 1 1
342 testlink set -4 16.3 1 {} {} {} {} {} {} {} {} {} {} {}
343 list [catch x msg] $msg $bool
344 } -result {1 {can't set "y": variable must have boolean value} 1}
345 test link-7.7 {access to linked variables via upvar} -setup {
346 testlink delete
347 } -constraints {testlink} -body {
348 proc x {} {
349 upvar wide y
350 set y abc
351 }
352 testlink create 1 1 1 1 1 1 1 1 1 1 1 1 1 1
353 testlink set -4 16.3 1 {} 778899 {} {} {} {} {} {} {} {} {}
354 list [catch x msg] $msg $wide
355 } -result {1 {can't set "y": variable must have integer value} 778899}
356
357 test link-8.1 {Tcl_UpdateLinkedVar procedure} {testlink} {
358 proc x args {
359 global x int real bool string wide
360 lappend x $args $int $real $bool $string $wide
361 }
362 set x {}
363 testlink create 1 1 1 1 1 1 1 1 1 1 1 1 1 1
364 testlink set 14 -2.0 0 xyzzy 995511 64 250 30000 60000 0xbeefbabe 12321 32123 3.25 1231231234
365 trace var int w x
366 testlink update 32 4.0 3 abcd 113355 65 251 30001 60001 0xbabebeef 12322 32124 3.125 12312312340
367 trace remove variable int write x
368 return $x
369 } {{int {} w} 32 -2.0 0 xyzzy 995511}
370 test link-8.2 {Tcl_UpdateLinkedVar procedure} {testlink} {
371 proc x args {
372 global x int real bool string wide
373 lappend x $args $int $real $bool $string $wide
374 }
375 set x {}
376 testlink create 1 1 1 1 1 1 1 1 1 1 1 1 1 1
377 testlink set 14 -2.0 0 xyzzy 995511 64 250 30000 60000 0xbeefbabe 12321 32123 3.25 1231231234
378 testlink delete
379 trace var int w x
380 testlink update 32 4.0 6 abcd 113355 65 251 30001 60001 0xbabebeef 12322 32124 3.125 12312312340
381 trace remove variable int write x
382 return $x
383 } {}
384 test link-8.3 {Tcl_UpdateLinkedVar procedure, read-only variable} {testlink} {
385 testlink create 0 0 0 0 0 0 0 0 0 0 0 0 0 0
386 list [catch {
387 testlink update 47 {} {} {} {} {} {} {} {} {} {} {} {} {}
388 } msg] $msg $int
389 } {0 {} 47}
390
391 catch {testlink set 0 0 0 - 0 0 0 0 0 0 0 0 0 0}
392 catch {testlink delete}
393 foreach i {int real bool string wide} {
394 unset -nocomplain $i
395 }
396
397 # cleanup
398 ::tcltest::cleanupTests
399 return
400
401 # Local Variables:
402 # mode: tcl
403 # fill-column: 78
404 # End: