sl@0
|
1 |
# Commands covered: append lappend
|
sl@0
|
2 |
#
|
sl@0
|
3 |
# This file contains a collection of tests for one or more of the Tcl
|
sl@0
|
4 |
# built-in commands. Sourcing this file into Tcl runs the tests and
|
sl@0
|
5 |
# generates output for errors. No output means no errors were found.
|
sl@0
|
6 |
#
|
sl@0
|
7 |
# Copyright (c) 1991-1993 The Regents of the University of California.
|
sl@0
|
8 |
# Copyright (c) 1994-1996 Sun Microsystems, Inc.
|
sl@0
|
9 |
# Copyright (c) 1998-1999 by Scriptics Corporation.
|
sl@0
|
10 |
#
|
sl@0
|
11 |
# See the file "license.terms" for information on usage and redistribution
|
sl@0
|
12 |
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
sl@0
|
13 |
#
|
sl@0
|
14 |
# RCS: @(#) $Id: appendComp.test,v 1.5.4.1 2004/10/28 00:01:05 dgp Exp $
|
sl@0
|
15 |
|
sl@0
|
16 |
if {[lsearch [namespace children] ::tcltest] == -1} {
|
sl@0
|
17 |
package require tcltest
|
sl@0
|
18 |
namespace import -force ::tcltest::*
|
sl@0
|
19 |
}
|
sl@0
|
20 |
catch {unset x}
|
sl@0
|
21 |
|
sl@0
|
22 |
test appendComp-1.1 {append command} {
|
sl@0
|
23 |
catch {unset x}
|
sl@0
|
24 |
proc foo {} {append ::x 1 2 abc "long string"}
|
sl@0
|
25 |
list [foo] $x
|
sl@0
|
26 |
} {{12abclong string} {12abclong string}}
|
sl@0
|
27 |
test appendComp-1.2 {append command} {
|
sl@0
|
28 |
proc foo {} {
|
sl@0
|
29 |
set x ""
|
sl@0
|
30 |
list [append x first] [append x second] [append x third] $x
|
sl@0
|
31 |
}
|
sl@0
|
32 |
foo
|
sl@0
|
33 |
} {first firstsecond firstsecondthird firstsecondthird}
|
sl@0
|
34 |
test appendComp-1.3 {append command} {
|
sl@0
|
35 |
proc foo {} {
|
sl@0
|
36 |
set x "abcd"
|
sl@0
|
37 |
append x
|
sl@0
|
38 |
}
|
sl@0
|
39 |
foo
|
sl@0
|
40 |
} abcd
|
sl@0
|
41 |
|
sl@0
|
42 |
test appendComp-2.1 {long appends} {
|
sl@0
|
43 |
proc foo {} {
|
sl@0
|
44 |
set x ""
|
sl@0
|
45 |
for {set i 0} {$i < 1000} {set i [expr $i+1]} {
|
sl@0
|
46 |
append x "foobar "
|
sl@0
|
47 |
}
|
sl@0
|
48 |
set y "foobar"
|
sl@0
|
49 |
set y "$y $y $y $y $y $y $y $y $y $y"
|
sl@0
|
50 |
set y "$y $y $y $y $y $y $y $y $y $y"
|
sl@0
|
51 |
set y "$y $y $y $y $y $y $y $y $y $y "
|
sl@0
|
52 |
expr {$x == $y}
|
sl@0
|
53 |
}
|
sl@0
|
54 |
foo
|
sl@0
|
55 |
} 1
|
sl@0
|
56 |
|
sl@0
|
57 |
test appendComp-3.1 {append errors} {
|
sl@0
|
58 |
proc foo {} {append}
|
sl@0
|
59 |
list [catch {foo} msg] $msg
|
sl@0
|
60 |
} {1 {wrong # args: should be "append varName ?value value ...?"}}
|
sl@0
|
61 |
test appendComp-3.2 {append errors} {
|
sl@0
|
62 |
proc foo {} {
|
sl@0
|
63 |
set x ""
|
sl@0
|
64 |
append x(0) 44
|
sl@0
|
65 |
}
|
sl@0
|
66 |
list [catch {foo} msg] $msg
|
sl@0
|
67 |
} {1 {can't set "x(0)": variable isn't array}}
|
sl@0
|
68 |
test appendComp-3.3 {append errors} {
|
sl@0
|
69 |
proc foo {} {
|
sl@0
|
70 |
catch {unset x}
|
sl@0
|
71 |
append x
|
sl@0
|
72 |
}
|
sl@0
|
73 |
list [catch {foo} msg] $msg
|
sl@0
|
74 |
} {1 {can't read "x": no such variable}}
|
sl@0
|
75 |
|
sl@0
|
76 |
test appendComp-4.1 {lappend command} {
|
sl@0
|
77 |
proc foo {} {
|
sl@0
|
78 |
global x
|
sl@0
|
79 |
catch {unset x}
|
sl@0
|
80 |
lappend x 1 2 abc "long string"
|
sl@0
|
81 |
}
|
sl@0
|
82 |
list [foo] $x
|
sl@0
|
83 |
} {{1 2 abc {long string}} {1 2 abc {long string}}}
|
sl@0
|
84 |
test appendComp-4.2 {lappend command} {
|
sl@0
|
85 |
proc foo {} {
|
sl@0
|
86 |
set x ""
|
sl@0
|
87 |
list [lappend x first] [lappend x second] [lappend x third] $x
|
sl@0
|
88 |
}
|
sl@0
|
89 |
foo
|
sl@0
|
90 |
} {first {first second} {first second third} {first second third}}
|
sl@0
|
91 |
test appendComp-4.3 {lappend command} {
|
sl@0
|
92 |
proc foo {} {
|
sl@0
|
93 |
global x
|
sl@0
|
94 |
set x old
|
sl@0
|
95 |
unset x
|
sl@0
|
96 |
lappend x new
|
sl@0
|
97 |
}
|
sl@0
|
98 |
set result [foo]
|
sl@0
|
99 |
rename foo {}
|
sl@0
|
100 |
set result
|
sl@0
|
101 |
} {new}
|
sl@0
|
102 |
test appendComp-4.4 {lappend command} {
|
sl@0
|
103 |
proc foo {} {
|
sl@0
|
104 |
set x {}
|
sl@0
|
105 |
lappend x \{\ abc
|
sl@0
|
106 |
}
|
sl@0
|
107 |
foo
|
sl@0
|
108 |
} {\{\ abc}
|
sl@0
|
109 |
test appendComp-4.5 {lappend command} {
|
sl@0
|
110 |
proc foo {} {
|
sl@0
|
111 |
set x {}
|
sl@0
|
112 |
lappend x \{ abc
|
sl@0
|
113 |
}
|
sl@0
|
114 |
foo
|
sl@0
|
115 |
} {\{ abc}
|
sl@0
|
116 |
test appendComp-4.6 {lappend command} {
|
sl@0
|
117 |
proc foo {} {
|
sl@0
|
118 |
set x {1 2 3}
|
sl@0
|
119 |
lappend x
|
sl@0
|
120 |
}
|
sl@0
|
121 |
foo
|
sl@0
|
122 |
} {1 2 3}
|
sl@0
|
123 |
test appendComp-4.7 {lappend command} {
|
sl@0
|
124 |
proc foo {} {
|
sl@0
|
125 |
set x "a\{"
|
sl@0
|
126 |
lappend x abc
|
sl@0
|
127 |
}
|
sl@0
|
128 |
foo
|
sl@0
|
129 |
} "a\\\{ abc"
|
sl@0
|
130 |
test appendComp-4.8 {lappend command} {
|
sl@0
|
131 |
proc foo {} {
|
sl@0
|
132 |
set x "\\\{"
|
sl@0
|
133 |
lappend x abc
|
sl@0
|
134 |
}
|
sl@0
|
135 |
foo
|
sl@0
|
136 |
} "\\{ abc"
|
sl@0
|
137 |
test appendComp-4.9 {lappend command} {
|
sl@0
|
138 |
proc foo {} {
|
sl@0
|
139 |
set x " \{"
|
sl@0
|
140 |
list [catch {lappend x abc} msg] $msg
|
sl@0
|
141 |
}
|
sl@0
|
142 |
foo
|
sl@0
|
143 |
} {1 {unmatched open brace in list}}
|
sl@0
|
144 |
test appendComp-4.10 {lappend command} {
|
sl@0
|
145 |
proc foo {} {
|
sl@0
|
146 |
set x " \{"
|
sl@0
|
147 |
list [catch {lappend x abc} msg] $msg
|
sl@0
|
148 |
}
|
sl@0
|
149 |
foo
|
sl@0
|
150 |
} {1 {unmatched open brace in list}}
|
sl@0
|
151 |
test appendComp-4.11 {lappend command} {
|
sl@0
|
152 |
proc foo {} {
|
sl@0
|
153 |
set x "\{\{\{"
|
sl@0
|
154 |
list [catch {lappend x abc} msg] $msg
|
sl@0
|
155 |
}
|
sl@0
|
156 |
foo
|
sl@0
|
157 |
} {1 {unmatched open brace in list}}
|
sl@0
|
158 |
test appendComp-4.12 {lappend command} {
|
sl@0
|
159 |
proc foo {} {
|
sl@0
|
160 |
set x "x \{\{\{"
|
sl@0
|
161 |
list [catch {lappend x abc} msg] $msg
|
sl@0
|
162 |
}
|
sl@0
|
163 |
foo
|
sl@0
|
164 |
} {1 {unmatched open brace in list}}
|
sl@0
|
165 |
test appendComp-4.13 {lappend command} {
|
sl@0
|
166 |
proc foo {} {
|
sl@0
|
167 |
set x "x\{\{\{"
|
sl@0
|
168 |
lappend x abc
|
sl@0
|
169 |
}
|
sl@0
|
170 |
foo
|
sl@0
|
171 |
} "x\\\{\\\{\\\{ abc"
|
sl@0
|
172 |
test appendComp-4.14 {lappend command} {
|
sl@0
|
173 |
proc foo {} {
|
sl@0
|
174 |
set x " "
|
sl@0
|
175 |
lappend x abc
|
sl@0
|
176 |
}
|
sl@0
|
177 |
foo
|
sl@0
|
178 |
} "abc"
|
sl@0
|
179 |
test appendComp-4.15 {lappend command} {
|
sl@0
|
180 |
proc foo {} {
|
sl@0
|
181 |
set x "\\ "
|
sl@0
|
182 |
lappend x abc
|
sl@0
|
183 |
}
|
sl@0
|
184 |
foo
|
sl@0
|
185 |
} "{ } abc"
|
sl@0
|
186 |
test appendComp-4.16 {lappend command} {
|
sl@0
|
187 |
proc foo {} {
|
sl@0
|
188 |
set x "x "
|
sl@0
|
189 |
lappend x abc
|
sl@0
|
190 |
}
|
sl@0
|
191 |
foo
|
sl@0
|
192 |
} "x abc"
|
sl@0
|
193 |
test appendComp-4.17 {lappend command} {
|
sl@0
|
194 |
proc foo {} { lappend x }
|
sl@0
|
195 |
foo
|
sl@0
|
196 |
} {}
|
sl@0
|
197 |
test appendComp-4.18 {lappend command} {
|
sl@0
|
198 |
proc foo {} { lappend x {} }
|
sl@0
|
199 |
foo
|
sl@0
|
200 |
} {{}}
|
sl@0
|
201 |
test appendComp-4.19 {lappend command} {
|
sl@0
|
202 |
proc foo {} { lappend x(0) }
|
sl@0
|
203 |
foo
|
sl@0
|
204 |
} {}
|
sl@0
|
205 |
test appendComp-4.20 {lappend command} {
|
sl@0
|
206 |
proc foo {} { lappend x(0) abc }
|
sl@0
|
207 |
foo
|
sl@0
|
208 |
} {abc}
|
sl@0
|
209 |
|
sl@0
|
210 |
proc check {var size} {
|
sl@0
|
211 |
set l [llength $var]
|
sl@0
|
212 |
if {$l != $size} {
|
sl@0
|
213 |
return "length mismatch: should have been $size, was $l"
|
sl@0
|
214 |
}
|
sl@0
|
215 |
for {set i 0} {$i < $size} {set i [expr $i+1]} {
|
sl@0
|
216 |
set j [lindex $var $i]
|
sl@0
|
217 |
if {$j != "item $i"} {
|
sl@0
|
218 |
return "element $i should have been \"item $i\", was \"$j\""
|
sl@0
|
219 |
}
|
sl@0
|
220 |
}
|
sl@0
|
221 |
return ok
|
sl@0
|
222 |
}
|
sl@0
|
223 |
test appendComp-5.1 {long lappends} {
|
sl@0
|
224 |
catch {unset x}
|
sl@0
|
225 |
set x ""
|
sl@0
|
226 |
for {set i 0} {$i < 300} {set i [expr $i+1]} {
|
sl@0
|
227 |
lappend x "item $i"
|
sl@0
|
228 |
}
|
sl@0
|
229 |
check $x 300
|
sl@0
|
230 |
} ok
|
sl@0
|
231 |
|
sl@0
|
232 |
test appendComp-6.1 {lappend errors} {
|
sl@0
|
233 |
proc foo {} {lappend}
|
sl@0
|
234 |
list [catch {foo} msg] $msg
|
sl@0
|
235 |
} {1 {wrong # args: should be "lappend varName ?value value ...?"}}
|
sl@0
|
236 |
test appendComp-6.2 {lappend errors} {
|
sl@0
|
237 |
proc foo {} {
|
sl@0
|
238 |
set x ""
|
sl@0
|
239 |
lappend x(0) 44
|
sl@0
|
240 |
}
|
sl@0
|
241 |
list [catch {foo} msg] $msg
|
sl@0
|
242 |
} {1 {can't set "x(0)": variable isn't array}}
|
sl@0
|
243 |
|
sl@0
|
244 |
test appendComp-7.1 {lappendComp-created var and error in trace on that var} {
|
sl@0
|
245 |
proc bar {} {
|
sl@0
|
246 |
global x
|
sl@0
|
247 |
catch {rename foo ""}
|
sl@0
|
248 |
catch {unset x}
|
sl@0
|
249 |
trace variable x w foo
|
sl@0
|
250 |
proc foo {} {global x; unset x}
|
sl@0
|
251 |
catch {lappend x 1}
|
sl@0
|
252 |
proc foo {args} {global x; unset x}
|
sl@0
|
253 |
info exists x
|
sl@0
|
254 |
set x
|
sl@0
|
255 |
lappend x 1
|
sl@0
|
256 |
list [info exists x] [catch {set x} msg] $msg
|
sl@0
|
257 |
}
|
sl@0
|
258 |
bar
|
sl@0
|
259 |
} {0 1 {can't read "x": no such variable}}
|
sl@0
|
260 |
test appendComp-7.2 {lappend var triggers read trace, index var} {
|
sl@0
|
261 |
proc bar {} {
|
sl@0
|
262 |
catch {unset myvar}
|
sl@0
|
263 |
catch {unset ::result}
|
sl@0
|
264 |
trace variable myvar r foo
|
sl@0
|
265 |
proc foo {args} {append ::result $args}
|
sl@0
|
266 |
lappend myvar a
|
sl@0
|
267 |
list [catch {set ::result} msg] $msg
|
sl@0
|
268 |
}
|
sl@0
|
269 |
bar
|
sl@0
|
270 |
} {0 {myvar {} r}}
|
sl@0
|
271 |
test appendComp-7.3 {lappend var triggers read trace, stack var} {
|
sl@0
|
272 |
proc bar {} {
|
sl@0
|
273 |
catch {unset ::myvar}
|
sl@0
|
274 |
catch {unset ::result}
|
sl@0
|
275 |
trace variable ::myvar r foo
|
sl@0
|
276 |
proc foo {args} {append ::result $args}
|
sl@0
|
277 |
lappend ::myvar a
|
sl@0
|
278 |
list [catch {set ::result} msg] $msg
|
sl@0
|
279 |
}
|
sl@0
|
280 |
bar
|
sl@0
|
281 |
} {0 {::myvar {} r}}
|
sl@0
|
282 |
test appendComp-7.4 {lappend var triggers read trace, array var} {
|
sl@0
|
283 |
# The behavior of read triggers on lappend changed in 8.0 to
|
sl@0
|
284 |
# not trigger them. Maybe not correct, but been there a while.
|
sl@0
|
285 |
proc bar {} {
|
sl@0
|
286 |
catch {unset myvar}
|
sl@0
|
287 |
catch {unset ::result}
|
sl@0
|
288 |
trace variable myvar r foo
|
sl@0
|
289 |
proc foo {args} {append ::result $args}
|
sl@0
|
290 |
lappend myvar(b) a
|
sl@0
|
291 |
list [catch {set ::result} msg] $msg
|
sl@0
|
292 |
}
|
sl@0
|
293 |
bar
|
sl@0
|
294 |
} {0 {myvar b r}}
|
sl@0
|
295 |
test appendComp-7.5 {lappend var triggers read trace, array var} {
|
sl@0
|
296 |
# The behavior of read triggers on lappend changed in 8.0 to
|
sl@0
|
297 |
# not trigger them. Maybe not correct, but been there a while.
|
sl@0
|
298 |
proc bar {} {
|
sl@0
|
299 |
catch {unset myvar}
|
sl@0
|
300 |
catch {unset ::result}
|
sl@0
|
301 |
trace variable myvar r foo
|
sl@0
|
302 |
proc foo {args} {append ::result $args}
|
sl@0
|
303 |
lappend myvar(b) a b
|
sl@0
|
304 |
list [catch {set ::result} msg] $msg
|
sl@0
|
305 |
}
|
sl@0
|
306 |
bar
|
sl@0
|
307 |
} {0 {myvar b r}}
|
sl@0
|
308 |
test appendComp-7.6 {lappend var triggers read trace, array var exists} {
|
sl@0
|
309 |
proc bar {} {
|
sl@0
|
310 |
catch {unset myvar}
|
sl@0
|
311 |
catch {unset ::result}
|
sl@0
|
312 |
set myvar(0) 1
|
sl@0
|
313 |
trace variable myvar r foo
|
sl@0
|
314 |
proc foo {args} {append ::result $args}
|
sl@0
|
315 |
lappend myvar(b) a
|
sl@0
|
316 |
list [catch {set ::result} msg] $msg
|
sl@0
|
317 |
}
|
sl@0
|
318 |
bar
|
sl@0
|
319 |
} {0 {myvar b r}}
|
sl@0
|
320 |
test appendComp-7.7 {lappend var triggers read trace, array stack var} {
|
sl@0
|
321 |
proc bar {} {
|
sl@0
|
322 |
catch {unset ::myvar}
|
sl@0
|
323 |
catch {unset ::result}
|
sl@0
|
324 |
trace variable ::myvar r foo
|
sl@0
|
325 |
proc foo {args} {append ::result $args}
|
sl@0
|
326 |
lappend ::myvar(b) a
|
sl@0
|
327 |
list [catch {set ::result} msg] $msg
|
sl@0
|
328 |
}
|
sl@0
|
329 |
bar
|
sl@0
|
330 |
} {0 {::myvar b r}}
|
sl@0
|
331 |
test appendComp-7.8 {lappend var triggers read trace, array stack var} {
|
sl@0
|
332 |
proc bar {} {
|
sl@0
|
333 |
catch {unset ::myvar}
|
sl@0
|
334 |
catch {unset ::result}
|
sl@0
|
335 |
trace variable ::myvar r foo
|
sl@0
|
336 |
proc foo {args} {append ::result $args}
|
sl@0
|
337 |
lappend ::myvar(b) a b
|
sl@0
|
338 |
list [catch {set ::result} msg] $msg
|
sl@0
|
339 |
}
|
sl@0
|
340 |
bar
|
sl@0
|
341 |
} {0 {::myvar b r}}
|
sl@0
|
342 |
test appendComp-7.9 {append var does not trigger read trace} {
|
sl@0
|
343 |
proc bar {} {
|
sl@0
|
344 |
catch {unset myvar}
|
sl@0
|
345 |
catch {unset ::result}
|
sl@0
|
346 |
trace variable myvar r foo
|
sl@0
|
347 |
proc foo {args} {append ::result $args}
|
sl@0
|
348 |
append myvar a
|
sl@0
|
349 |
info exists ::result
|
sl@0
|
350 |
}
|
sl@0
|
351 |
bar
|
sl@0
|
352 |
} {0}
|
sl@0
|
353 |
|
sl@0
|
354 |
catch {unset i x result y}
|
sl@0
|
355 |
catch {rename foo ""}
|
sl@0
|
356 |
catch {rename bar ""}
|
sl@0
|
357 |
catch {rename check ""}
|
sl@0
|
358 |
catch {rename bar {}}
|
sl@0
|
359 |
|
sl@0
|
360 |
# cleanup
|
sl@0
|
361 |
::tcltest::cleanupTests
|
sl@0
|
362 |
return
|