Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/tests/proc-old.test @ 25

Last change on this file since 25 was 25, checked in by landauf, 16 years ago

added tcl to libs

File size: 14.8 KB
Line 
1# Commands covered:  proc, return, global
2#
3# This file, proc-old.test, includes the original set of tests for Tcl's
4# proc, return, and global commands. There is now a new file proc.test
5# that contains tests for the tclProc.c source file.
6#
7# Sourcing this file into Tcl runs the tests and generates output for
8# errors.  No output means no errors were found.
9#
10# Copyright (c) 1991-1993 The Regents of the University of California.
11# Copyright (c) 1994-1997 Sun Microsystems, Inc.
12# Copyright (c) 1998-1999 by Scriptics Corporation.
13#
14# See the file "license.terms" for information on usage and redistribution
15# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
16#
17# RCS: @(#) $Id: proc-old.test,v 1.15 2006/10/09 19:15:45 msofer Exp $
18
19if {[lsearch [namespace children] ::tcltest] == -1} {
20    package require tcltest
21    namespace import -force ::tcltest::*
22}
23
24catch {rename t1 ""}
25catch {rename foo ""}
26
27proc tproc {} {return a; return b}
28test proc-old-1.1 {simple procedure call and return} {tproc} a
29proc tproc x {
30    set x [expr $x+1]
31    return $x
32}
33test proc-old-1.2 {simple procedure call and return} {tproc 2} 3
34test proc-old-1.3 {simple procedure call and return} {
35    proc tproc {} {return foo}
36} {}
37test proc-old-1.4 {simple procedure call and return} {
38    proc tproc {} {return}
39    tproc
40} {}
41proc tproc1 {a}   {incr a; return $a}
42proc tproc2 {a b} {incr a; return $a}
43test proc-old-1.5 {simple procedure call and return (2 procs with same body but different parameters)} {
44    list [tproc1 123] [tproc2 456 789]
45} {124 457}
46test proc-old-1.6 {simple procedure call and return (shared proc body string)} {
47    set x {}
48    proc tproc {} {}   ;# body is shared with x
49    list [tproc] [append x foo]
50} {{} foo}
51
52test proc-old-2.1 {local and global variables} {
53    proc tproc x {
54        set x [expr $x+1]
55        return $x
56    }
57    set x 42
58    list [tproc 6] $x
59} {7 42}
60test proc-old-2.2 {local and global variables} {
61    proc tproc x {
62        set y [expr $x+1]
63        return $y
64    }
65    set y 18
66    list [tproc 6] $y
67} {7 18}
68test proc-old-2.3 {local and global variables} {
69    proc tproc x {
70        global y
71        set y [expr $x+1]
72        return $y
73    }
74    set y 189
75    list [tproc 6] $y
76} {7 7}
77test proc-old-2.4 {local and global variables} {
78    proc tproc x {
79        global y
80        return [expr $x+$y]
81    }
82    set y 189
83    list [tproc 6] $y
84} {195 189}
85catch {unset _undefined_}
86test proc-old-2.5 {local and global variables} {
87    proc tproc x {
88        global _undefined_
89        return $_undefined_
90    }
91    list [catch {tproc xxx} msg] $msg
92} {1 {can't read "_undefined_": no such variable}}
93test proc-old-2.6 {local and global variables} {
94    set a 114
95    set b 115
96    global a b
97    list $a $b
98} {114 115}
99
100proc do {cmd} {eval $cmd}
101test proc-old-3.1 {local and global arrays} {
102    catch {unset a}
103    set a(0) 22
104    list [catch {do {global a; set a(0)}} msg] $msg
105} {0 22}
106test proc-old-3.2 {local and global arrays} {
107    catch {unset a}
108    set a(x) 22
109    list [catch {do {global a; set a(x) newValue}} msg] $msg $a(x)
110} {0 newValue newValue}
111test proc-old-3.3 {local and global arrays} {
112    catch {unset a}
113    set a(x) 22
114    set a(y) 33
115    list [catch {do {global a; unset a(y)}; array names a} msg] $msg
116} {0 x}
117test proc-old-3.4 {local and global arrays} {
118    catch {unset a}
119    set a(x) 22
120    set a(y) 33
121    list [catch {do {global a; unset a; info exists a}} msg] $msg \
122            [info exists a]
123} {0 0 0}
124test proc-old-3.5 {local and global arrays} {
125    catch {unset a}
126    set a(x) 22
127    set a(y) 33
128    list [catch {do {global a; unset a(y); array names a}} msg] $msg
129} {0 x}
130catch {unset a}
131test proc-old-3.6 {local and global arrays} {
132    catch {unset a}
133    set a(x) 22
134    set a(y) 33
135    do {global a; do {global a; unset a}; set a(z) 22}
136    list [catch {array names a} msg] $msg
137} {0 z}
138test proc-old-3.7 {local and global arrays} {
139    proc t1 {args} {global info; set info 1}
140    catch {unset a}
141    set info {}
142    do {global a; trace var a(1) w t1}
143    set a(1) 44
144    set info
145} 1
146test proc-old-3.8 {local and global arrays} {
147    proc t1 {args} {global info; set info 1}
148    catch {unset a}
149    trace var a(1) w t1
150    set info {}
151    do {global a; trace vdelete a(1) w t1}
152    set a(1) 44
153    set info
154} {}
155test proc-old-3.9 {local and global arrays} {
156    proc t1 {args} {global info; set info 1}
157    catch {unset a}
158    trace var a(1) w t1
159    do {global a; trace vinfo a(1)}
160} {{w t1}}
161catch {unset a}
162
163test proc-old-30.1 {arguments and defaults} {
164    proc tproc {x y z} {
165        return [list $x $y $z]
166    }
167    tproc 11 12 13
168} {11 12 13}
169test proc-old-30.2 {arguments and defaults} {
170    proc tproc {x y z} {
171        return [list $x $y $z]
172    }
173    list [catch {tproc 11 12} msg] $msg
174} {1 {wrong # args: should be "tproc x y z"}}
175test proc-old-30.3 {arguments and defaults} {
176    proc tproc {x y z} {
177        return [list $x $y $z]
178    }
179    list [catch {tproc 11 12 13 14} msg] $msg
180} {1 {wrong # args: should be "tproc x y z"}}
181test proc-old-30.4 {arguments and defaults} {
182    proc tproc {x {y y-default} {z z-default}} {
183        return [list $x $y $z]
184    }
185    tproc 11 12 13
186} {11 12 13}
187test proc-old-30.5 {arguments and defaults} {
188    proc tproc {x {y y-default} {z z-default}} {
189        return [list $x $y $z]
190    }
191    tproc 11 12
192} {11 12 z-default}
193test proc-old-30.6 {arguments and defaults} {
194    proc tproc {x {y y-default} {z z-default}} {
195        return [list $x $y $z]
196    }
197    tproc 11
198} {11 y-default z-default}
199test proc-old-30.7 {arguments and defaults} {
200    proc tproc {x {y y-default} {z z-default}} {
201        return [list $x $y $z]
202    }
203    list [catch {tproc} msg] $msg
204} {1 {wrong # args: should be "tproc x ?y? ?z?"}}
205test proc-old-30.8 {arguments and defaults} {
206    list [catch {
207        proc tproc {x {y y-default} z} {
208            return [list $x $y $z]
209        }
210        tproc 2 3
211    } msg] $msg
212} {1 {wrong # args: should be "tproc x ?y? z"}}
213test proc-old-30.9 {arguments and defaults} {
214    proc tproc {x {y y-default} args} {
215        return [list $x $y $args]
216    }
217    tproc 2 3 4 5
218} {2 3 {4 5}}
219test proc-old-30.10 {arguments and defaults} {
220    proc tproc {x {y y-default} args} {
221        return [list $x $y $args]
222    }
223    tproc 2 3
224} {2 3 {}}
225test proc-old-30.11 {arguments and defaults} {
226    proc tproc {x {y y-default} args} {
227        return [list $x $y $args]
228    }
229    tproc 2
230} {2 y-default {}}
231test proc-old-30.12 {arguments and defaults} {
232    proc tproc {x {y y-default} args} {
233        return [list $x $y $args]
234    }
235    list [catch {tproc} msg] $msg
236} {1 {wrong # args: should be "tproc x ?y? ..."}}
237
238test proc-old-4.1 {variable numbers of arguments} {
239    proc tproc args {return $args}
240    tproc
241} {}
242test proc-old-4.2 {variable numbers of arguments} {
243    proc tproc args {return $args}
244    tproc 1 2 3 4 5 6 7 8
245} {1 2 3 4 5 6 7 8}
246test proc-old-4.3 {variable numbers of arguments} {
247    proc tproc args {return $args}
248    tproc 1 {2 3} {4 {5 6} {{{7}}}} 8
249} {1 {2 3} {4 {5 6} {{{7}}}} 8}
250test proc-old-4.4 {variable numbers of arguments} {
251    proc tproc {x y args} {return $args}
252    tproc 1 2 3 4 5 6 7
253} {3 4 5 6 7}
254test proc-old-4.5 {variable numbers of arguments} {
255    proc tproc {x y args} {return $args}
256    tproc 1 2
257} {}
258test proc-old-4.6 {variable numbers of arguments} {
259    proc tproc {x missing args} {return $args}
260    list [catch {tproc 1} msg] $msg
261} {1 {wrong # args: should be "tproc x missing ..."}}
262
263test proc-old-5.1 {error conditions} {
264    list [catch {proc} msg] $msg
265} {1 {wrong # args: should be "proc name args body"}}
266test proc-old-5.2 {error conditions} {
267    list [catch {proc tproc b} msg] $msg
268} {1 {wrong # args: should be "proc name args body"}}
269test proc-old-5.3 {error conditions} {
270    list [catch {proc tproc b c d e} msg] $msg
271} {1 {wrong # args: should be "proc name args body"}}
272test proc-old-5.4 {error conditions} {
273    list [catch {proc tproc \{xyz {return foo}} msg] $msg
274} {1 {unmatched open brace in list}}
275test proc-old-5.5 {error conditions} {
276    list [catch {proc tproc {{} y} {return foo}} msg] $msg
277} {1 {argument with no name}}
278test proc-old-5.6 {error conditions} {
279    list [catch {proc tproc {{} y} {return foo}} msg] $msg
280} {1 {argument with no name}}
281test proc-old-5.7 {error conditions} {
282    list [catch {proc tproc {{x 1 2} y} {return foo}} msg] $msg
283} {1 {too many fields in argument specifier "x 1 2"}}
284test proc-old-5.8 {error conditions} {
285    catch {return}
286} 2
287test proc-old-5.9 {error conditions} {
288    list [catch {global} msg] $msg
289} {1 {wrong # args: should be "global varName ?varName ...?"}}
290proc tproc {} {
291    set a 22
292    global a
293}
294test proc-old-5.10 {error conditions} {
295    list [catch {tproc} msg] $msg
296} {1 {variable "a" already exists}}
297test proc-old-5.11 {error conditions} {
298    catch {rename tproc {}}
299    catch {
300        proc tproc {x {} z} {return foo}
301    }
302    list [catch {tproc 1} msg] $msg
303} {1 {invalid command name "tproc"}}
304test proc-old-5.12 {error conditions} {
305    proc tproc {} {
306        set a 22
307        error "error in procedure"
308        return
309    }
310    list [catch tproc msg] $msg
311} {1 {error in procedure}}
312test proc-old-5.13 {error conditions} {
313    proc tproc {} {
314        set a 22
315        error "error in procedure"
316        return
317    }
318    catch tproc msg
319    set ::errorInfo
320} {error in procedure
321    while executing
322"error "error in procedure""
323    (procedure "tproc" line 3)
324    invoked from within
325"tproc"}
326test proc-old-5.14 {error conditions} {
327    proc tproc {} {
328        set a 22
329        break
330        return
331    }
332    catch tproc msg
333    set ::errorInfo
334} {invoked "break" outside of a loop
335    (procedure "tproc" line 1)
336    invoked from within
337"tproc"}
338test proc-old-5.15 {error conditions} {
339    proc tproc {} {
340        set a 22
341        continue
342        return
343    }
344    catch tproc msg
345    set ::errorInfo
346} {invoked "continue" outside of a loop
347    (procedure "tproc" line 1)
348    invoked from within
349"tproc"}
350test proc-old-5.16 {error conditions} {
351    proc foo args {
352        global fooMsg
353        set fooMsg "foo was called: $args"
354    }
355    proc tproc {} {
356        set x 44
357        trace var x u foo
358        while {$x < 100} {
359            error "Nested error"
360        }
361    }
362    set fooMsg "foo not called"
363    list [catch tproc msg] $msg $::errorInfo $fooMsg
364} {1 {Nested error} {Nested error
365    while executing
366"error "Nested error""
367    (procedure "tproc" line 5)
368    invoked from within
369"tproc"} {foo was called: x {} u}}
370
371# The tests below will really only be useful when run under Purify or
372# some other system that can detect accesses to freed memory...
373
374test proc-old-6.1 {procedure that redefines itself} {
375    proc tproc {} {
376        proc tproc {} {
377            return 44
378        }
379        return 45
380    }
381    tproc
382} 45
383test proc-old-6.2 {procedure that deletes itself} {
384    proc tproc {} {
385        rename tproc {}
386        return 45
387    }
388    tproc
389} 45
390
391proc tproc code {
392    return -code $code abc
393}
394test proc-old-7.1 {return with special completion code} {
395    list [catch {tproc ok} msg] $msg
396} {0 abc}
397test proc-old-7.2 {return with special completion code} {
398    list [catch {tproc error} msg] $msg $::errorInfo $::errorCode
399} {1 abc {abc
400    while executing
401"tproc error"} NONE}
402test proc-old-7.3 {return with special completion code} {
403    list [catch {tproc return} msg] $msg
404} {2 abc}
405test proc-old-7.4 {return with special completion code} {
406    list [catch {tproc break} msg] $msg
407} {3 abc}
408test proc-old-7.5 {return with special completion code} {
409    list [catch {tproc continue} msg] $msg
410} {4 abc}
411test proc-old-7.6 {return with special completion code} {
412    list [catch {tproc -14} msg] $msg
413} {-14 abc}
414test proc-old-7.7 {return with special completion code} {
415    list [catch {tproc gorp} msg] $msg
416} {1 {bad completion code "gorp": must be ok, error, return, break, continue, or an integer}}
417test proc-old-7.8 {return with special completion code} {
418    list [catch {tproc 10b} msg] $msg
419} {1 {bad completion code "10b": must be ok, error, return, break, continue, or an integer}}
420test proc-old-7.9 {return with special completion code} {
421    proc tproc2 {} {
422        tproc return
423    }
424    list [catch tproc2 msg] $msg
425} {0 abc}
426test proc-old-7.10 {return with special completion code} {
427    proc tproc2 {} {
428        return -code error
429    }
430    list [catch tproc2 msg] $msg
431} {1 {}}
432test proc-old-7.11 {return with special completion code} {
433    proc tproc2 {} {
434        global errorCode errorInfo
435        catch {open _bad_file_name r} msg
436        return -code error -errorinfo $errorInfo -errorcode $errorCode $msg
437    }
438    set msg [list [catch tproc2 msg] $msg $::errorInfo $::errorCode]
439    regsub -all [file join {} _bad_file_name] $msg "_bad_file_name" msg
440    normalizeMsg $msg
441} {1 {couldn't open "_bad_file_name": no such file or directory} {couldn't open "_bad_file_name": no such file or directory
442    while executing
443"open _bad_file_name r"
444    invoked from within
445"tproc2"} {posix enoent {no such file or directory}}}
446test proc-old-7.12 {return with special completion code} {
447    proc tproc2 {} {
448        global errorCode errorInfo
449        catch {open _bad_file_name r} msg
450        return -code error -errorcode $errorCode $msg
451    }
452    set msg [list [catch tproc2 msg] $msg $::errorInfo $::errorCode]
453    regsub -all [file join {} _bad_file_name] $msg "_bad_file_name" msg
454    normalizeMsg $msg
455} {1 {couldn't open "_bad_file_name": no such file or directory} {couldn't open "_bad_file_name": no such file or directory
456    while executing
457"tproc2"} {posix enoent {no such file or directory}}}
458test proc-old-7.13 {return with special completion code} {
459    proc tproc2 {} {
460        global errorCode errorInfo
461        catch {open _bad_file_name r} msg
462        return -code error -errorinfo $errorInfo $msg
463    }
464    set msg [list [catch tproc2 msg] $msg $::errorInfo $::errorCode]
465    regsub -all [file join {} _bad_file_name] $msg "_bad_file_name" msg
466    normalizeMsg $msg
467} {1 {couldn't open "_bad_file_name": no such file or directory} {couldn't open "_bad_file_name": no such file or directory
468    while executing
469"open _bad_file_name r"
470    invoked from within
471"tproc2"} none}
472test proc-old-7.14 {return with special completion code} {
473    proc tproc2 {} {
474        global errorCode errorInfo
475        catch {open _bad_file_name r} msg
476        return -code error $msg
477    }
478    set msg [list [catch tproc2 msg] $msg $::errorInfo $::errorCode]
479    regsub -all [file join {} _bad_file_name] $msg "_bad_file_name" msg
480    normalizeMsg $msg
481} {1 {couldn't open "_bad_file_name": no such file or directory} {couldn't open "_bad_file_name": no such file or directory
482    while executing
483"tproc2"} none}
484test proc-old-7.15 {return with special completion code} {
485    list [catch {return -badOption foo message} msg] $msg
486} {2 message}
487
488test proc-old-8.1 {unset and undefined local arrays} {
489    proc t1 {} {
490        foreach v {xxx, yyy} {
491            catch {unset $v}
492        }
493        set yyy(foo) bar
494    }
495    t1
496} bar
497
498test proc-old-9.1 {empty command name} {
499    catch {rename {} ""}
500    proc t1 {args} {
501        return
502    }
503    set v [t1]
504    catch {$v}
505} 1
506
507test proc-old-10.1 {ByteCode epoch change during recursive proc execution} {
508    proc t1 x {
509        set y 20
510        rename expr expr.old
511        rename expr.old expr
512        if $x then {t1 0} ;# recursive call after foo's code is invalidated
513        return 20
514    }
515    t1 1
516} 20
517
518# cleanup
519catch {rename t1 ""}
520catch {rename foo ""}
521::tcltest::cleanupTests
522return
Note: See TracBrowser for help on using the repository browser.