summaryrefslogtreecommitdiffstats
path: root/tests/apply.test
blob: 4e8eeea428e388d5729cb5511b25f47eb7bcd6ae (plain)
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
# Commands covered:  apply
#
# This file contains a collection of tests for one or more of the Tcl
# built-in commands.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# Copyright (c) 1991-1993 The Regents of the University of California.
# Copyright (c) 1994-1996 Sun Microsystems, Inc.
# Copyright (c) 1998-1999 by Scriptics Corporation.
# Copyright (c) 2005-2006 Miguel Sofer
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: apply.test,v 1.12.2.1 2009/10/29 17:21:17 dgp Exp $

if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest 2.2
    namespace import -force ::tcltest::*
}

if {[info commands ::apply] eq {}} {
    return
}

testConstraint memory [llength [info commands memory]]

# Tests for wrong number of arguments

test apply-1.1 {too few arguments} {
    set res [catch apply msg]
    list $res $msg
} {1 {wrong # args: should be "apply lambdaExpr ?arg1 arg2 ...?"}}

# Tests for malformed lambda

test apply-2.0 {malformed lambda} {
    set lambda a
    set res [catch {apply $lambda} msg]
    list $res $msg
} {1 {can't interpret "a" as a lambda expression}}
test apply-2.1 {malformed lambda} {
    set lambda [list a b c d]
    set res [catch {apply $lambda} msg]
    list $res $msg
} {1 {can't interpret "a b c d" as a lambda expression}}
test apply-2.2 {malformed lambda} {
    set lambda [list {{}} boo]
    set res [catch {apply $lambda} msg]
    list $res $msg $::errorInfo
} {1 {argument with no name} {argument with no name
    (parsing lambda expression "{{}} boo")
    invoked from within
"apply $lambda"}}
test apply-2.3 {malformed lambda} {
    set lambda [list {{a b c}} boo]
    set res [catch {apply $lambda} msg]
    list $res $msg $::errorInfo
} {1 {too many fields in argument specifier "a b c"} {too many fields in argument specifier "a b c"
    (parsing lambda expression "{{a b c}} boo")
    invoked from within
"apply $lambda"}}
test apply-2.4 {malformed lambda} {
    set lambda [list a(1) boo]
    set res [catch {apply $lambda} msg]
    list $res $msg $::errorInfo
} {1 {formal parameter "a(1)" is an array element} {formal parameter "a(1)" is an array element
    (parsing lambda expression "a(1) boo")
    invoked from within
"apply $lambda"}}
test apply-2.5 {malformed lambda} {
    set lambda [list a::b boo]
    set res [catch {apply $lambda} msg]
    list $res $msg $::errorInfo
} {1 {formal parameter "a::b" is not a simple name} {formal parameter "a::b" is not a simple name
    (parsing lambda expression "a::b boo")
    invoked from within
"apply $lambda"}}

# Tests for runtime errors in the lambda expression

test apply-3.1 {non-existing namespace} -body {
    apply [list x {set x 1} ::NONEXIST::FOR::SURE] x
} -returnCodes error -result {namespace "::NONEXIST::FOR::SURE" not found}
test apply-3.2 {non-existing namespace} -body {
    namespace eval ::NONEXIST::FOR::SURE {}
    set lambda [list x {set x 1} ::NONEXIST::FOR::SURE]
    apply $lambda x
    namespace delete ::NONEXIST
    apply $lambda x
} -returnCodes error -result {namespace "::NONEXIST::FOR::SURE" not found}
test apply-3.3 {non-existing namespace} -body {
    apply [list x {set x 1} NONEXIST::FOR::SURE] x
} -returnCodes error -result {namespace "::NONEXIST::FOR::SURE" not found}
test apply-3.4 {non-existing namespace} -body {
    namespace eval ::NONEXIST::FOR::SURE {}
    set lambda [list x {set x 1} NONEXIST::FOR::SURE]
    apply $lambda x
    namespace delete ::NONEXIST
    apply $lambda x
} -returnCodes error -result {namespace "::NONEXIST::FOR::SURE" not found}

test apply-4.1 {error in arguments to lambda expression} {
    set lambda [list x {set x 1}]
    set res [catch {apply $lambda} msg]
    list $res $msg
} {1 {wrong # args: should be "apply {x {set x 1}} x"}}
test apply-4.2 {error in arguments to lambda expression} {
    set lambda [list x {set x 1}]
    set res [catch {apply $lambda a b} msg]
    list $res $msg
} {1 {wrong # args: should be "apply {x {set x 1}} x"}}
test apply-4.3 {error in arguments to lambda expression} {
    set lambda [list x {set x 1}]
    interp alias {} foo {} ::apply $lambda
    set res [catch {foo a b} msg]
    list $res $msg [rename foo {}]
} {1 {wrong # args: should be "foo x"} {}}
test apply-4.4 {error in arguments to lambda expression} {
    set lambda [list x {set x 1}]
    interp alias {} foo {} ::apply $lambda a
    set res [catch {foo b} msg]
    list $res $msg [rename foo {}]
} {1 {wrong # args: should be "foo"} {}}
test apply-4.5 {error in arguments to lambda expression} {
    set lambda [list x {set x 1}]
    namespace eval a {
	namespace ensemble create -command ::bar -map {id {::a::const foo}}
	proc const val { return $val }
	proc alias {object slot = command args} {
	    set map [namespace ensemble configure $object -map]
	    dict set map $slot [linsert $args 0 $command]
	    namespace ensemble configure $object -map $map
	}
	proc method {object name params body} {
	    set params [linsert $params 0 self]
	    alias $object $name = ::apply [list $params $body] $object
	}
	method ::bar boo x {return "[expr {$x*$x}] - $self"}
    }
    set res [catch {bar boo} msg]
    list $res $msg [namespace delete ::a]
} {1 {wrong # args: should be "bar boo x"} {}}

test apply-5.1 {runtime error in lambda expression} {
    set lambda [list {} {error foo}]
    set res [catch {apply $lambda}]
    list $res $::errorInfo
} {1 {foo
    while executing
"error foo"
    (lambda term "{} {error foo}" line 1)
    invoked from within
"apply $lambda"}}

# Tests for correct execution; as the implementation is the same as that for
# procs, the general functionality is mostly tested elsewhere

test apply-6.1 {info level} {
    set lev [info level]
    set lambda [list {} {info level}]
    expr {[apply $lambda] - $lev}
} 1
test apply-6.2 {info level} {
    set lambda [list {} {info level 0}]
    apply $lambda
} {apply {{} {info level 0}}}
test apply-6.3 {info level} {
    set lambda [list args {info level 0}]
    apply $lambda x y
} {apply {args {info level 0}} x y}

# Tests for correct namespace scope

namespace eval ::testApply {
    proc testApply args {return testApply}
}

test apply-7.1 {namespace access} {
    set ::testApply::x 0
    set body {set x 1; set x}
    list [apply [list args $body ::testApply]] $::testApply::x
} {1 0}
test apply-7.2 {namespace access} {
    set ::testApply::x 0
    set body {variable x; set x}
    list [apply [list args $body ::testApply]] $::testApply::x
} {0 0}
test apply-7.3 {namespace access} {
    set ::testApply::x 0
    set body {variable x; set x 1}
    list [apply [list args $body ::testApply]] $::testApply::x
} {1 1}
test apply-7.4 {namespace access} {
    set ::testApply::x 0
    set body {testApply}
    apply [list args $body ::testApply]
} testApply
test apply-7.5 {namespace access} {
    set ::testApply::x 0
    set body {set x 1; set x}
    list [apply [list args $body testApply]] $::testApply::x
} {1 0}
test apply-7.6 {namespace access} {
    set ::testApply::x 0
    set body {variable x; set x}
    list [apply [list args $body testApply]] $::testApply::x
} {0 0}
test apply-7.7 {namespace access} {
    set ::testApply::x 0
    set body {variable x; set x 1}
    list [apply [list args $body testApply]] $::testApply::x
} {1 1}
test apply-7.8 {namespace access} {
    set ::testApply::x 0
    set body {testApply}
    apply [list args $body testApply]
} testApply

# Tests for correct argument treatment

set applyBody {
    set res {}
    foreach v [info locals] {
	if {$v eq "res"} continue
	lappend res [list $v [set $v]]
    }
    set res
}

test apply-8.1 {args treatment} {
    apply [list args $applyBody] 1 2 3
} {{args {1 2 3}}}
test apply-8.2 {args treatment} {
    apply [list {x args} $applyBody] 1 2
} {{x 1} {args 2}}
test apply-8.3 {args treatment} {
    apply [list {x args} $applyBody] 1 2 3
} {{x 1} {args {2 3}}}
test apply-8.4 {default values} {
    apply [list {{x 1} {y 2}} $applyBody] 
} {{x 1} {y 2}}
test apply-8.5 {default values} {
    apply [list {{x 1} {y 2}} $applyBody] 3 4
} {{x 3} {y 4}}
test apply-8.6 {default values} {
    apply [list {{x 1} {y 2}} $applyBody] 3
} {{x 3} {y 2}}
test apply-8.7 {default values} {
    apply [list {x {y 2}} $applyBody] 1
} {{x 1} {y 2}}
test apply-8.8 {default values} {
    apply [list {x {y 2}} $applyBody] 1 3
} {{x 1} {y 3}}
test apply-8.9 {default values} {
    apply [list {x {y 2} args} $applyBody] 1
} {{x 1} {y 2} {args {}}}
test apply-8.10 {default values} {
    apply [list {x {y 2} args} $applyBody] 1 3
} {{x 1} {y 3} {args {}}}

# Tests for leaks

test apply-9.1 {leaking internal rep} -setup {
    proc getbytes {} {
	set lines [split [memory info] "\n"]
	lindex $lines 3 3
    }
    set lam [list {} {set a 1}]
} -constraints memory -body {
    set end [getbytes]
    for {set i 0} {$i < 5} {incr i} {
	::apply [lrange $lam 0 end]
	set tmp $end
	set end [getbytes]
    }
    set leakedBytes [expr {$end - $tmp}]
} -cleanup {
    rename getbytes {}
    unset -nocomplain lam end i tmp leakedBytes
} -result 0
test apply-9.2 {leaking internal rep} -setup {
    proc getbytes {} {
	set lines [split [memory info] "\n"]
	lindex $lines 3 3
    }
} -constraints memory -body {
    set end [getbytes]
    for {set i 0} {$i < 5} {incr i} {
	::apply [list {} {set a 1}]
	set tmp $end
	set end [getbytes]
    }
    set leakedBytes [expr {$end - $tmp}]
} -cleanup {
    rename getbytes {}
    unset -nocomplain end i tmp leakedBytes
} -result 0
test apply-9.3 {leaking internal rep} -setup {
    proc getbytes {} {
	set lines [split [memory info] "\n"]
	lindex $lines 3 3
    }
} -constraints memory -body {
    set end [getbytes]
    for {set i 0} {$i < 5} {incr i} {
	set x [list {} {set a 1} ::NS::THAT::DOES::NOT::EXIST]
	catch {::apply $x}
	set x {}
	set tmp $end
	set end [getbytes]
    }
    set leakedBytes [expr {$end - $tmp}]
} -cleanup {
    rename getbytes {}
    unset -nocomplain end i x tmp leakedBytes
} -result 0

# Tests for the avoidance of recompilation

# cleanup

namespace delete testApply

::tcltest::cleanupTests
return