1 # Commands covered: for, continue, break
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.
7 # Copyright (c) 1996 Sun Microsystems, Inc.
9 # See the file "license.terms" for information on usage and redistribution
10 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 if {[lsearch [namespace children] ::tcltest] == -1} {
15 package require tcltest
16 namespace import -force ::tcltest::*
19 # Basic "for" operation.
21 test for-1.1 {TclCompileForCmd: missing initial command} {
22 list [catch {for} msg] $msg
23 } {1 {wrong # args: should be "for start test next command"}}
24 test for-1.2 {TclCompileForCmd: error in initial command} {
25 list [catch {for {set}} msg] $msg $errorInfo
26 } {1 {wrong # args: should be "for start test next command"} {wrong # args: should be "for start test next command"
30 test for-1.3 {TclCompileForCmd: missing test expression} {
31 catch {for {set i 0}} msg
33 } {wrong # args: should be "for start test next command"}
34 test for-1.4 {TclCompileForCmd: error in test expression} {
35 catch {for {set i 0} {$i<}} msg
37 } {wrong # args: should be "for start test next command"
39 "for {set i 0} {$i<}"}
40 test for-1.5 {TclCompileForCmd: test expression is enclosed in quotes} {
42 for {} "$i > 5" {incr i} {}
44 test for-1.6 {TclCompileForCmd: missing "next" command} {
45 catch {for {set i 0} {$i < 5}} msg
47 } {wrong # args: should be "for start test next command"}
48 test for-1.7 {TclCompileForCmd: missing command body} {
49 catch {for {set i 0} {$i < 5} {incr i}} msg
51 } {wrong # args: should be "for start test next command"}
52 test for-1.8 {TclCompileForCmd: error compiling command body} {
53 catch {for {set i 0} {$i < 5} {incr i} {set}} msg
55 } {wrong # args: should be "set varName ?newValue?"
60 "for {set i 0} {$i < 5} {incr i} {set}"}
62 test for-1.9 {TclCompileForCmd: simple command body} {
64 for {set i 1} {$i<6} {set i [expr $i+1]} {
70 test for-1.10 {TclCompileForCmd: command body in quotes} {
72 for {set i 1} {$i<6} {set i [expr $i+1]} "append a x"
75 test for-1.11 {TclCompileForCmd: computed command body} {
79 set x1 {append a x1; }
81 set x2 {; append a x2}
83 for {set i 1} {$i<6} {set i [expr $i+1]} $x1$bb$x2
86 test for-1.12 {TclCompileForCmd: error in "next" command} {
87 catch {for {set i 0} {$i < 5} {set} {puts $i}} msg
89 } {wrong # args: should be "set varName ?newValue?"
92 ("for" loop-end command)
94 "for {set i 0} {$i < 5} {set} {puts $i}"}
95 test for-1.13 {TclCompileForCmd: long command body} {
97 for {set i 1} {$i<6} {set i [expr $i+1]} {
100 if {$i>6 && $tcl_platform(machine)=="xxx"} {
103 catch {incr i -5} msg
105 if {$i>6 && $tcl_platform(machine)=="xxx"} {
108 catch {incr i -5} msg
110 if {$i>6 && $tcl_platform(machine)=="xxx"} {
113 catch {incr i -5} msg
115 if {$i>6 && $tcl_platform(machine)=="xxx"} {
118 catch {incr i -5} msg
120 if {$i>6 && $tcl_platform(machine)=="xxx"} {
123 catch {incr i -5} msg
129 test for-1.14 {TclCompileForCmd: for command result} {
130 set a [for {set i 0} {$i < 5} {incr i} {}]
133 test for-1.15 {TclCompileForCmd: for command result} {
134 set a [for {set i 0} {$i < 5} {incr i} {if $i==3 break}]
138 # Check "for" and "continue".
140 test for-2.1 {TclCompileContinueCmd: arguments after "continue"} {
141 catch {continue foo} msg
143 } {wrong # args: should be "continue"}
144 test for-2.2 {TclCompileContinueCmd: continue result} {
147 test for-2.3 {continue tests} {
149 for {set i 1} {$i <= 4} {set i [expr $i+1]} {
150 if {$i == 2} continue
155 test for-2.4 {continue tests} {
157 for {set i 1} {$i <= 4} {set i [expr $i+1]} {
158 if {$i != 2} continue
163 test for-2.5 {continue tests, nested loops} {
165 for {set i 1} {$i <= 4} {incr i} {
166 for {set a 1} {$a <= 2} {incr a} {
167 if {$i>=2 && $a>=2} continue
168 set msg [concat $msg "$i.$a"]
172 } {1.1 1.2 2.1 3.1 4.1}
173 test for-2.6 {continue tests, long command body} {
175 for {set i 1} {$i<6} {set i [expr $i+1]} {
179 if {$i>6 && $tcl_platform(machine)=="xxx"} {
182 catch {incr i -5} msg
184 if {$i>6 && $tcl_platform(machine)=="xxx"} {
187 catch {incr i -5} msg
189 if {$i>6 && $tcl_platform(machine)=="xxx"} {
192 catch {incr i -5} msg
194 if {$i>6 && $tcl_platform(machine)=="xxx"} {
197 catch {incr i -5} msg
199 if {$i>6 && $tcl_platform(machine)=="xxx"} {
202 catch {incr i -5} msg
209 # Check "for" and "break".
211 test for-3.1 {TclCompileBreakCmd: arguments after "break"} {
212 catch {break foo} msg
214 } {wrong # args: should be "break"}
215 test for-3.2 {TclCompileBreakCmd: break result} {
218 test for-3.3 {break tests} {
220 for {set i 1} {$i <= 4} {incr i} {
226 test for-3.4 {break tests, nested loops} {
228 for {set i 1} {$i <= 4} {incr i} {
229 for {set a 1} {$a <= 2} {incr a} {
230 if {$i>=2 && $a>=2} break
231 set msg [concat $msg "$i.$a"]
235 } {1.1 1.2 2.1 3.1 4.1}
236 test for-3.5 {break tests, long command body} {
238 for {set i 1} {$i<6} {set i [expr $i+1]} {
242 if {$i>6 && $tcl_platform(machine)=="xxx"} {
245 catch {incr i -5} msg
247 if {$i>6 && $tcl_platform(machine)=="xxx"} {
250 catch {incr i -5} msg
252 if {$i>6 && $tcl_platform(machine)=="xxx"} {
255 catch {incr i -5} msg
258 if {$i>6 && $tcl_platform(machine)=="xxx"} {
261 catch {incr i -5} msg
263 if {$i>6 && $tcl_platform(machine)=="xxx"} {
266 catch {incr i -5} msg
272 # A simplified version of exmh's mail formatting routine to stress "for",
273 # "break", "while", and "if".
276 0 {Return-path: george@tcl} \
277 1 {Return-path: <george@tcl>} \
278 2 {Received: from tcl by tcl.Somewhere.COM (SMI-8.6/SMI-SVR4)} \
279 3 { id LAA10027; Wed, 11 Sep 1996 11:14:53 -0700} \
280 4 {Message-id: <199609111814.LAA10027@tcl.Somewhere.COM>} \
281 5 {X-mailer: exmh version 1.6.9 8/22/96} \
282 6 {Mime-version: 1.0} \
283 7 {Content-type: text/plain; charset=iso-8859-1} \
284 8 {Content-transfer-encoding: quoted-printable} \
285 9 {Content-length: 2162} \
287 11 {Subject: tcl7.6} \
288 12 {Date: Wed, 11 Sep 1996 11:14:53 -0700} \
289 13 {From: George <george@tcl>} \
290 14 {The Tcl 7.6 and Tk 4.2 releases} \
292 16 {This page contains information about Tcl 7.6 and Tk4.2, which are the most recent} \
293 17 {releases of the Tcl scripting language and the Tk toolkit. The first beta versions of these} \
294 18 {releases were released on August 30, 1996. These releases contain only minor changes,} \
295 19 {so we hope to have only a single beta release and to go final in early October, 1996. } \
300 24 {The most important changes in the releases are summarized below. See the README} \
301 25 {and changes files in the distributions for more complete information on what has} \
302 26 {changed, including both feature changes and bug fixes. } \
304 28 { There are new options to the file command for copying files (file copy),} \
305 29 { deleting files and directories (file delete), creating directories (file} \
306 30 { mkdir), and renaming files (file rename). } \
307 31 { The implementation of exec has been improved greatly for Windows 95 and} \
309 33 { There is a new memory allocator for the Macintosh version, which should be} \
310 34 { more efficient than the old one. } \
311 35 { Tk's grid geometry manager has been completely rewritten. The layout} \
312 36 { algorithm produces much better layouts than before, especially where rows or} \
313 37 { columns were stretchable. } \
314 38 { There are new commands for creating common dialog boxes:} \
315 39 { tk_chooseColor, tk_getOpenFile, tk_getSaveFile and} \
316 40 { tk_messageBox. These use native dialog boxes if they are available. } \
317 41 { There is a new virtual event mechanism for handling events in a more portable} \
318 42 { way. See the new command event. It also allows events (both physical and} \
319 43 { virtual) to be generated dynamically. } \
321 45 {Tcl 7.6 and Tk 4.2 are backwards-compatible with Tcl 7.5 and Tk 4.1 except for} \
322 46 {changes in the C APIs for custom channel drivers. Scripts written for earlier releases} \
323 47 {should work on these new releases as well. } \
325 49 {Obtaining The Releases} \
327 51 {Binary Releases} \
329 53 {Pre-compiled releases are available for the following platforms: } \
331 55 { Windows 3.1, Windows 95, and Windows NT: Fetch} \
332 56 { ftp://ftp.sunlabs.com/pub/tcl/win42b1.exe, then execute it. The file is a} \
333 57 { self-extracting executable. It will install the Tcl and Tk libraries, the wish and} \
334 58 { tclsh programs, and documentation. } \
335 59 { Macintosh (both 68K and PowerPC): Fetch} \
336 60 { ftp://ftp.sunlabs.com/pub/tcl/mactk4.2b1.sea.hqx. The file is in binhex format,} \
337 61 { which is understood by Fetch, StuffIt, and many other Mac utilities. The} \
338 62 { unpacked file is a self-installing executable: double-click on it and it will create a} \
339 63 { folder containing all that you need to run Tcl and Tk. } \
340 64 { UNIX (Solaris 2.* and SunOS, other systems soon to follow). Easy to install} \
341 65 { binary packages are now for sale at the Sun Labs Tcl/Tk Shop. Check it out!} \
347 set tag {level= type=text/plain part=0 sel Charset}
348 set ix [lsearch -regexp $tag text/enriched]
353 set breakrange {6.42 78.0}
354 set F1 [lindex $breakrange 0]
355 set F2 [lindex $breakrange 1]
356 set breakrange [lrange $breakrange 2 end]
357 if {[string length $F1] == 0} {
366 set last [array size lines]
368 for {set L 1} {$L < $last} {incr L} {
371 # Blank or empty line terminates headers
372 # Leading --- terminates headers
373 if {[regexp {^[ ]*$} $line] || [regexp {^--+} $line]} {
376 if {[regexp -nocase {^x-mailer:} $line]} {
385 # Decide whether or not to break the body line
388 if {[string first {> } $line] == 0} {
389 # This is quoted text from previous message, don't reformat
390 append result $line $NL
391 if {$quote && !$inheaders} {
392 # Fix from <sarr@umich.edu> to handle text/enriched
393 if {$L > $L1 && $L < $L2 && $line != {}} {
394 # enriched requires two newlines for each one.
396 } elseif {$L > $L2} {
397 set L1 [lindex $ranges 0]
398 set L2 [lindex $ranges 1]
399 set ranges [lrange $ranges 2 end]
400 set quote [llength $L1]
407 # Nothing left to format
408 append result $line $NL
410 } elseif {$L < $F1} {
411 # Not yet to formatted block
412 append result $line $NL
414 } elseif {$L > $F2} {
415 # Past formatted block
416 set F1 [lindex $breakrange 0]
417 set F2 [lindex $breakrange 1]
418 set breakrange [lrange $breakrange 2 end]
419 append result $line $NL
420 if {[string length $F1] == 0} {
426 set climit [expr $limit-1]
430 while {[string length $line] > $limit} {
431 for {set c [expr $limit-1]} {$c >= $cutoff} {incr c -1} {
432 set char [string index $line $c]
433 if {$char == " " || $char == "\t"} {
436 if {$char == ">"} { ;# Hack for enriched formatting
442 set c [expr $limit-1]
444 set c [string length $line]
447 set newline [string range $line 0 $c]
448 if {! $continuation} {
449 append result $newline $NL
451 append result \ $newline $NL
454 set line [string trimright [string range $line $c end]]
461 if {[string length $line] != 0} {
462 append result \ $line $NL
465 append result $line $NL
466 if {$quote && !$inheaders} {
467 if {$L > $L1 && $L < $L2 && $line != {}} {
468 # enriched requires two newlines for each one.
470 } elseif {$L > $L2} {
471 set L1 [lindex $ranges 0]
472 set L2 [lindex $ranges 1]
473 set ranges [lrange $ranges 2 end]
474 set quote [llength $L1]
481 test for-3.6 {break tests} {
483 } {Return-path: <george@tcl>
484 Received: from tcl by tcl.Somewhere.COM (SMI-8.6/SMI-SVR4)
485 id LAA10027; Wed, 11 Sep 1996 11:14:53 -0700
486 Message-id: <199609111814.LAA10027@tcl.Somewhere.COM>
488 Content-type: text/plain; charset=iso-8859-1
489 Content-transfer-encoding: quoted-printable
493 Date: Wed, 11 Sep 1996 11:14:53 -0700
494 From: George <george@tcl>
495 The Tcl 7.6 and Tk 4.2 releases
497 This page contains information about Tcl 7.6 and Tk4.2,
498 which are the most recent
499 releases of the Tcl scripting language and the Tk toolk
500 it. The first beta versions of these
501 releases were released on August 30, 1996. These releas
502 es contain only minor changes,
503 so we hope to have only a single beta release and to
504 go final in early October, 1996.
509 The most important changes in the releases are summariz
510 ed below. See the README
511 and changes files in the distributions for more complet
512 e information on what has
513 changed, including both feature changes and bug fixes.
515 There are new options to the file command for
516 copying files (file copy),
517 deleting files and directories (file delete),
518 creating directories (file
519 mkdir), and renaming files (file rename).
520 The implementation of exec has been improved great
521 ly for Windows 95 and
523 There is a new memory allocator for the Macintosh
524 version, which should be
525 more efficient than the old one.
526 Tk's grid geometry manager has been completely
527 rewritten. The layout
528 algorithm produces much better layouts than before
529 , especially where rows or
530 columns were stretchable.
531 There are new commands for creating common dialog
533 tk_chooseColor, tk_getOpenFile, tk_getSaveFile and
534 tk_messageBox. These use native dialog boxes if
536 There is a new virtual event mechanism for handlin
537 g events in a more portable
538 way. See the new command event. It also allows
539 events (both physical and
540 virtual) to be generated dynamically.
542 Tcl 7.6 and Tk 4.2 are backwards-compatible with Tcl
543 7.5 and Tk 4.1 except for
544 changes in the C APIs for custom channel drivers. Scrip
545 ts written for earlier releases
546 should work on these new releases as well.
548 Obtaining The Releases
552 Pre-compiled releases are available for the following
555 Windows 3.1, Windows 95, and Windows NT: Fetch
556 ftp://ftp.sunlabs.com/pub/tcl/win42b1.exe, then
557 execute it. The file is a
558 self-extracting executable. It will install the
559 Tcl and Tk libraries, the wish and
560 tclsh programs, and documentation.
561 Macintosh (both 68K and PowerPC): Fetch
562 ftp://ftp.sunlabs.com/pub/tcl/mactk4.2b1.sea.hqx.
563 The file is in binhex format,
564 which is understood by Fetch, StuffIt, and many
565 other Mac utilities. The
566 unpacked file is a self-installing executable:
567 double-click on it and it will create a
568 folder containing all that you need to run Tcl
570 UNIX (Solaris 2.* and SunOS, other systems
571 soon to follow). Easy to install
572 binary packages are now for sale at the Sun Labs
573 Tcl/Tk Shop. Check it out!
576 # Check that "break" resets the interpreter's result
578 test for-4.1 {break must reset the interp result} {
580 set z GLOBTESTDIR/dir2/file2.c
581 if [string match GLOBTESTDIR/dir2/* $z] {
588 # Test for incorrect "double evaluation" semantics
590 test for-5.1 {possible delayed substitution of increment command} {
591 # Increment should be 5, and lappend should always append $a
596 for {set a 1} {$a < 12} "incr a $a" {lappend i $a}
600 test for-5.2 {possible delayed substitution of increment command} {
601 # Increment should be 5, and lappend should always append $a
606 for {set a 1} {$a < 12} "incr a $a" {lappend i $a}
611 test for-5.3 {possible delayed substitution of body command} {
612 # Increment should be $a, and lappend should always append 5
615 for {set a 1} {$a < 12} {incr a $a} "lappend i $a"
618 test for-5.4 {possible delayed substitution of body command} {
619 # Increment should be $a, and lappend should always append 5
624 for {set a 1} {$a < 12} {incr a $a} "lappend i $a"
630 # In the following tests we need to bypass the bytecode compiler by
631 # substituting the command from a variable. This ensures that command
632 # procedure is invoked directly.
634 test for-6.1 {Tcl_ForObjCmd: number of args} {
638 } {wrong # args: should be "for start test next command"}
639 test for-6.2 {Tcl_ForObjCmd: number of args} {
641 catch {$z {set i 0}} msg
643 } {wrong # args: should be "for start test next command"}
644 test for-6.3 {Tcl_ForObjCmd: number of args} {
646 catch {$z {set i 0} {$i < 5}} msg
648 } {wrong # args: should be "for start test next command"}
649 test for-6.4 {Tcl_ForObjCmd: number of args} {
651 catch {$z {set i 0} {$i < 5} {incr i}} msg
653 } {wrong # args: should be "for start test next command"}
654 test for-6.5 {Tcl_ForObjCmd: number of args} {
656 catch {$z {set i 0} {$i < 5} {incr i} {body} extra} msg
658 } {wrong # args: should be "for start test next command"}
659 test for-6.6 {Tcl_ForObjCmd: error in initial command} {
661 list [catch {$z {set} {$i < 5} {incr i} {body}} msg] $msg $errorInfo
662 } {1 {wrong # args: should be "set varName ?newValue?"} {wrong # args: should be "set varName ?newValue?"
665 ("for" initial command)
667 "$z {set} {$i < 5} {incr i} {body}"}}
668 test for-6.7 {Tcl_ForObjCmd: error in test expression} {
670 list [catch {$z {set i 0} {i < 5} {incr i} {body}} msg] $msg $errorInfo
671 } {1 {syntax error in expression "i < 5": variable references require preceding $} {syntax error in expression "i < 5": variable references require preceding $
673 "$z {set i 0} {i < 5} {incr i} {body}"}}
674 test for-6.8 {Tcl_ForObjCmd: test expression is enclosed in quotes} {
677 $z {set i 6} "$i > 5" {incr i} {set y $i}
680 test for-6.9 {Tcl_ForObjCmd: error executing command body} {
682 catch {$z {set i 0} {$i < 5} {incr i} {set}} msg
684 } {wrong # args: should be "set varName ?newValue?"
689 "$z {set i 0} {$i < 5} {incr i} {set}"}
690 test for-6.10 {Tcl_ForObjCmd: simple command body} {
693 $z {set i 1} {$i<6} {set i [expr $i+1]} {
699 test for-6.11 {Tcl_ForObjCmd: command body in quotes} {
702 $z {set i 1} {$i<6} {set i [expr $i+1]} "append a x"
705 test for-6.12 {Tcl_ForObjCmd: computed command body} {
710 set x1 {append a x1; }
712 set x2 {; append a x2}
714 $z {set i 1} {$i<6} {set i [expr $i+1]} $x1$bb$x2
717 test for-6.13 {Tcl_ForObjCmd: error in "next" command} {
719 catch {$z {set i 0} {$i < 5} {set} {set j 4}} msg
721 } {wrong # args: should be "set varName ?newValue?"
724 ("for" loop-end command)
726 "$z {set i 0} {$i < 5} {set} {set j 4}"}
727 test for-6.14 {Tcl_ForObjCmd: long command body} {
730 $z {set i 1} {$i<6} {set i [expr $i+1]} {
733 if {$i>6 && $tcl_platform(machine)=="xxx"} {
736 catch {incr i -5} msg
738 if {$i>6 && $tcl_platform(machine)=="xxx"} {
741 catch {incr i -5} msg
743 if {$i>6 && $tcl_platform(machine)=="xxx"} {
746 catch {incr i -5} msg
748 if {$i>6 && $tcl_platform(machine)=="xxx"} {
751 catch {incr i -5} msg
753 if {$i>6 && $tcl_platform(machine)=="xxx"} {
756 catch {incr i -5} msg
762 test for-6.15 {Tcl_ForObjCmd: for command result} {
764 set a [$z {set i 0} {$i < 5} {incr i} {}]
767 test for-6.16 {Tcl_ForObjCmd: for command result} {
769 set a [$z {set i 0} {$i < 5} {incr i} {if $i==3 break}]
776 ::tcltest::cleanupTests