Debian Codegraph @

tests/apply.test

raw
1 # Commands covered: apply
2 #
3 # This file contains a collection of tests for one or more of the Tcl
4 # built-in commands. Sourcing this file into Tcl runs the tests and
5 # generates output for errors. No output means no errors were found.
6 #
7 # Copyright (c) 1991-1993 The Regents of the University of California.
8 # Copyright (c) 1994-1996 Sun Microsystems, Inc.
9 # Copyright (c) 1998-1999 by Scriptics Corporation.
10 # Copyright (c) 2005-2006 Miguel Sofer
11 #
12 # See the file "license.terms" for information on usage and redistribution
13 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14
15 if {"::tcltest" ni [namespace children]} {
16 package require tcltest 2.5
17 namespace import -force ::tcltest::*
18 }
19 ::tcltest::loadTestedCommands
20 catch [list package require -exact tcl::test [info patchlevel]]
21
22 if {[info commands ::apply] eq {}} {
23 return
24 }
25
26 testConstraint memory [llength [info commands memory]]
27 testConstraint applylambda [llength [info commands testapplylambda]]
28
29
30 # Tests for wrong number of arguments
31
32 test apply-1.1 {not enough arguments} -returnCodes error -body {
33 apply
34 } -result {wrong # args: should be "apply lambdaExpr ?arg ...?"}
35
36 # Tests for malformed lambda
37
38 test apply-2.0 {malformed lambda} -returnCodes error -body {
39 set lambda a
40 apply $lambda
41 } -result {can't interpret "a" as a lambda expression}
42 test apply-2.1 {malformed lambda} -returnCodes error -body {
43 set lambda [list a b c d]
44 apply $lambda
45 } -result {can't interpret "a b c d" as a lambda expression}
46 test apply-2.2 {malformed lambda} {
47 set lambda [list {{}} boo]
48 list [catch {apply $lambda} msg] $msg $::errorInfo
49 } {1 {argument with no name} {argument with no name
50 (parsing lambda expression "{{}} boo")
51 invoked from within
52 "apply $lambda"}}
53 test apply-2.3 {malformed lambda} {
54 set lambda [list {{a b c}} boo]
55 list [catch {apply $lambda} msg] $msg $::errorInfo
56 } {1 {too many fields in argument specifier "a b c"} {too many fields in argument specifier "a b c"
57 (parsing lambda expression "{{a b c}} boo")
58 invoked from within
59 "apply $lambda"}}
60 test apply-2.4 {malformed lambda} {
61 set lambda [list a(1) boo]
62 list [catch {apply $lambda} msg] $msg $::errorInfo
63 } {1 {formal parameter "a(1)" is an array element} {formal parameter "a(1)" is an array element
64 (parsing lambda expression "a(1) boo")
65 invoked from within
66 "apply $lambda"}}
67 test apply-2.5 {malformed lambda} {
68 set lambda [list a::b boo]
69 list [catch {apply $lambda} msg] $msg $::errorInfo
70 } {1 {formal parameter "a::b" is not a simple name} {formal parameter "a::b" is not a simple name
71 (parsing lambda expression "a::b boo")
72 invoked from within
73 "apply $lambda"}}
74
75 # Tests for runtime errors in the lambda expression
76
77 test apply-3.1 {non-existing namespace} -body {
78 apply [list x {set x 1} ::NONEXIST::FOR::SURE] x
79 } -returnCodes error -result {namespace "::NONEXIST::FOR::SURE" not found}
80 test apply-3.2 {non-existing namespace} -body {
81 namespace eval ::NONEXIST::FOR::SURE {}
82 set lambda [list x {set x 1} ::NONEXIST::FOR::SURE]
83 apply $lambda x
84 namespace delete ::NONEXIST
85 apply $lambda x
86 } -returnCodes error -result {namespace "::NONEXIST::FOR::SURE" not found}
87 test apply-3.3 {non-existing namespace} -body {
88 apply [list x {set x 1} NONEXIST::FOR::SURE] x
89 } -returnCodes error -result {namespace "::NONEXIST::FOR::SURE" not found}
90 test apply-3.4 {non-existing namespace} -body {
91 namespace eval ::NONEXIST::FOR::SURE {}
92 set lambda [list x {set x 1} NONEXIST::FOR::SURE]
93 apply $lambda x
94 namespace delete ::NONEXIST
95 apply $lambda x
96 } -returnCodes error -result {namespace "::NONEXIST::FOR::SURE" not found}
97
98 test apply-4.1 {error in arguments to lambda expression} -body {
99 set lambda [list x {set x 1}]
100 apply $lambda
101 } -returnCodes error -result {wrong # args: should be "apply lambdaExpr x"}
102 test apply-4.2 {error in arguments to lambda expression} -body {
103 set lambda [list x {set x 1}]
104 apply $lambda a b
105 } -returnCodes error -result {wrong # args: should be "apply lambdaExpr x"}
106 test apply-4.3 {error in arguments to lambda expression} -body {
107 interp alias {} foo {} ::apply [list x {set x 1}]
108 foo a b
109 } -cleanup {
110 rename foo {}
111 } -returnCodes error -result {wrong # args: should be "foo x"}
112 test apply-4.4 {error in arguments to lambda expression} -body {
113 interp alias {} foo {} ::apply [list x {set x 1}] a
114 foo b
115 } -cleanup {
116 rename foo {}
117 } -returnCodes error -result {wrong # args: should be "foo"}
118 test apply-4.5 {error in arguments to lambda expression} -body {
119 set lambda [list x {set x 1}]
120 namespace eval a {
121 namespace ensemble create -command ::bar -map {id {::a::const foo}}
122 proc const val { return $val }
123 proc alias {object slot = command args} {
124 set map [namespace ensemble configure $object -map]
125 dict set map $slot [linsert $args 0 $command]
126 namespace ensemble configure $object -map $map
127 }
128 proc method {object name params body} {
129 set params [linsert $params 0 self]
130 alias $object $name = ::apply [list $params $body] $object
131 }
132 method ::bar boo x {return "[expr {$x*$x}] - $self"}
133 }
134 bar boo
135 } -cleanup {
136 namespace delete ::a
137 } -returnCodes error -result {wrong # args: should be "bar boo x"}
138
139 test apply-5.1 {runtime error in lambda expression} {
140 set lambda [list {} {error foo}]
141 set res [catch {apply $lambda}]
142 list $res $::errorInfo
143 } {1 {foo
144 while executing
145 "error foo"
146 (lambda term "{} {error foo}" line 1)
147 invoked from within
148 "apply $lambda"}}
149
150 # Tests for correct execution; as the implementation is the same as that for
151 # procs, the general functionality is mostly tested elsewhere
152
153 test apply-6.1 {info level} {
154 set lev [info level]
155 set lambda [list {} {info level}]
156 expr {[apply $lambda] - $lev}
157 } 1
158 test apply-6.2 {info level} {
159 set lambda [list {} {info level 0}]
160 apply $lambda
161 } {apply {{} {info level 0}}}
162 test apply-6.3 {info level} {
163 set lambda [list args {info level 0}]
164 apply $lambda x y
165 } {apply {args {info level 0}} x y}
166
167 # Tests for correct namespace scope
168
169 namespace eval ::testApply {
170 proc testApply args {return testApply}
171 }
172
173 test apply-7.1 {namespace access} {
174 set ::testApply::x 0
175 set body {set x 1; set x}
176 list [apply [list args $body ::testApply]] $::testApply::x
177 } {1 0}
178 test apply-7.2 {namespace access} {
179 set ::testApply::x 0
180 set body {variable x; set x}
181 list [apply [list args $body ::testApply]] $::testApply::x
182 } {0 0}
183 test apply-7.3 {namespace access} {
184 set ::testApply::x 0
185 set body {variable x; set x 1}
186 list [apply [list args $body ::testApply]] $::testApply::x
187 } {1 1}
188 test apply-7.4 {namespace access} {
189 set ::testApply::x 0
190 set body {testApply}
191 apply [list args $body ::testApply]
192 } testApply
193 test apply-7.5 {namespace access} {
194 set ::testApply::x 0
195 set body {set x 1; set x}
196 list [apply [list args $body testApply]] $::testApply::x
197 } {1 0}
198 test apply-7.6 {namespace access} {
199 set ::testApply::x 0
200 set body {variable x; set x}
201 list [apply [list args $body testApply]] $::testApply::x
202 } {0 0}
203 test apply-7.7 {namespace access} {
204 set ::testApply::x 0
205 set body {variable x; set x 1}
206 list [apply [list args $body testApply]] $::testApply::x
207 } {1 1}
208 test apply-7.8 {namespace access} {
209 set ::testApply::x 0
210 set body {testApply}
211 apply [list args $body testApply]
212 } testApply
213
214 # Tests for correct argument treatment
215
216 set applyBody {
217 set res {}
218 foreach v [info locals] {
219 if {$v eq "res"} continue
220 lappend res [list $v [set $v]]
221 }
222 set res
223 }
224
225 test apply-8.1 {args treatment} {
226 apply [list args $applyBody] 1 2 3
227 } {{args {1 2 3}}}
228 test apply-8.2 {args treatment} {
229 apply [list {x args} $applyBody] 1 2
230 } {{x 1} {args 2}}
231 test apply-8.3 {args treatment} {
232 apply [list {x args} $applyBody] 1 2 3
233 } {{x 1} {args {2 3}}}
234 test apply-8.4 {default values} {
235 apply [list {{x 1} {y 2}} $applyBody]
236 } {{x 1} {y 2}}
237 test apply-8.5 {default values} {
238 apply [list {{x 1} {y 2}} $applyBody] 3 4
239 } {{x 3} {y 4}}
240 test apply-8.6 {default values} {
241 apply [list {{x 1} {y 2}} $applyBody] 3
242 } {{x 3} {y 2}}
243 test apply-8.7 {default values} {
244 apply [list {x {y 2}} $applyBody] 1
245 } {{x 1} {y 2}}
246 test apply-8.8 {default values} {
247 apply [list {x {y 2}} $applyBody] 1 3
248 } {{x 1} {y 3}}
249 test apply-8.9 {default values} {
250 apply [list {x {y 2} args} $applyBody] 1
251 } {{x 1} {y 2} {args {}}}
252 test apply-8.10 {default values} {
253 apply [list {x {y 2} args} $applyBody] 1 3
254 } {{x 1} {y 3} {args {}}}
255
256 # Tests for leaks
257
258 test apply-9.1 {leaking internal rep} -setup {
259 proc getbytes {} {
260 set lines [split [memory info] "\n"]
261 lindex $lines 3 3
262 }
263 set lam [list {} {set a 1}]
264 } -constraints memory -body {
265 set end [getbytes]
266 for {set i 0} {$i < 5} {incr i} {
267 ::apply [lrange $lam 0 end]
268 set tmp $end
269 set end [getbytes]
270 }
271 set leakedBytes [expr {$end - $tmp}]
272 } -cleanup {
273 rename getbytes {}
274 unset -nocomplain lam end i tmp leakedBytes
275 } -result 0
276 test apply-9.2 {leaking internal rep} -setup {
277 proc getbytes {} {
278 set lines [split [memory info] "\n"]
279 lindex $lines 3 3
280 }
281 } -constraints memory -body {
282 set end [getbytes]
283 for {set i 0} {$i < 5} {incr i} {
284 ::apply [list {} {set a 1}]
285 set tmp $end
286 set end [getbytes]
287 }
288 set leakedBytes [expr {$end - $tmp}]
289 } -cleanup {
290 rename getbytes {}
291 unset -nocomplain end i tmp leakedBytes
292 } -result 0
293 test apply-9.3 {leaking internal rep} -setup {
294 proc getbytes {} {
295 set lines [split [memory info] "\n"]
296 lindex $lines 3 3
297 }
298 } -constraints memory -body {
299 set end [getbytes]
300 for {set i 0} {$i < 5} {incr i} {
301 set x [list {} {set a 1} ::NS::THAT::DOES::NOT::EXIST]
302 catch {::apply $x}
303 set x {}
304 set tmp $end
305 set end [getbytes]
306 }
307 set leakedBytes [expr {$end - $tmp}]
308 } -cleanup {
309 rename getbytes {}
310 unset -nocomplain end i x tmp leakedBytes
311 } -result 0
312
313 # Tests for specific bugs
314 test apply-10.1 {Test for precompiled bytecode body} -constraints {
315 applylambda
316 } -body {
317 testapplylambda
318 } -result 42
319
320 # Tests for the avoidance of recompilation
321
322 # cleanup
323
324 namespace delete testApply
325
326 ::tcltest::cleanupTests
327 return
328
329 # Local Variables:
330 # mode: tcl
331 # fill-column: 78
332 # End: