3 source [file dirname [info script]]/testing.tcl
6 needs cmd try tclcompat
8 test tailcall-1.1 {Basic tailcall} {
9 # Demo -- a tail-recursive factorial function
10 proc fac {x {val 1}} {
14 tailcall fac [expr {$x -1}] [expr {$x * $val}]
20 test tailcall-1.2 {Tailcall in try} {
22 proc a {} { upvar x x; incr x }
23 proc b {} { upvar x x; incr x 4; try { tailcall a } finally { incr x 8 }}
28 test tailcall-1.3 {Tailcall does return} {
30 proc a {} { upvar x x; incr x }
31 proc b {} { upvar x x; incr x 4; tailcall a; incr x 8}
36 test tailcall-1.5 {interaction of uplevel and tailcall} {
41 lappend result [uplevel 1 a c]
42 lappend result [uplevel 1 a c]
50 test tailcall-1.6 {tailcall pass through return} {
52 # return from $script should pass through back to the caller
53 tailcall foreach i {1 2 3} $script
63 test tailcall-1.7 {tailcall with namespaces} jim {
68 set d [local lambda {} { c }]
69 # $d should resolve in namespace 'a', not ""
75 test tailcall-1.8 {tailcall with local} jim {
77 tailcall [local proc b {} { return c }]
82 test tailcall-1.9 {tailcall with large number of invocations} {
93 test tailcall-1.10 {tailcall through uplevel} {
94 proc a {} { tailcall b }
95 proc b {} { uplevel 1 c }
96 proc c {} { tailcall d }
97 proc d {} { return [info level] }
101 test tailcall-1.11 {chained tailcall} {
103 proc b {} { tailcall tailcall c }
104 proc c {} { return [info level] }
108 test tailcall-1.12 {uplevel tailcall} {
110 proc b {} { uplevel 1 tailcall c }
111 proc c {} { return [info level] }