sl@0: # Commands covered: while sl@0: # sl@0: # This file contains a collection of tests for one or more of the Tcl sl@0: # built-in commands. Sourcing this file into Tcl runs the tests and sl@0: # generates output for errors. No output means no errors were found. sl@0: # sl@0: # Copyright (c) 1996 Sun Microsystems, Inc. sl@0: # Copyright (c) 1998-1999 by Scriptics Corporation. sl@0: # sl@0: # See the file "license.terms" for information on usage and redistribution sl@0: # of this file, and for a DISCLAIMER OF ALL WARRANTIES. sl@0: # sl@0: # RCS: @(#) $Id: while.test,v 1.8 2001/12/04 15:36:29 dkf Exp $ sl@0: sl@0: if {[lsearch [namespace children] ::tcltest] == -1} { sl@0: package require tcltest sl@0: namespace import -force ::tcltest::* sl@0: } sl@0: sl@0: # Basic "while" operation. sl@0: sl@0: catch {unset i} sl@0: catch {unset a} sl@0: sl@0: test while-1.1 {TclCompileWhileCmd: missing test expression} { sl@0: catch {while } msg sl@0: set msg sl@0: } {wrong # args: should be "while test command"} sl@0: test while-1.2 {TclCompileWhileCmd: error in test expression} { sl@0: set i 0 sl@0: catch {while {$i<} break} msg sl@0: set errorInfo sl@0: } {syntax error in expression "$i<": premature end of expression sl@0: ("while" test expression) sl@0: while compiling sl@0: "while {$i<} break"} sl@0: test while-1.3 {TclCompileWhileCmd: error in test expression} { sl@0: set err [catch {while {"a"+"b"} {error "loop aborted"}} msg] sl@0: list $err $msg sl@0: } {1 {can't use non-numeric string as operand of "+"}} sl@0: test while-1.4 {TclCompileWhileCmd: multiline test expr} { sl@0: set value 1 sl@0: while {($tcl_platform(platform) != "foobar1") && \ sl@0: ($tcl_platform(platform) != "foobar2")} { sl@0: incr value sl@0: break sl@0: } sl@0: set value sl@0: } {2} sl@0: test while-1.5 {TclCompileWhileCmd: non-numeric boolean test expr} { sl@0: set value 1 sl@0: while {"true"} { sl@0: incr value; sl@0: if {$value > 5} { sl@0: break; sl@0: } sl@0: } sl@0: set value sl@0: } 6 sl@0: test while-1.6 {TclCompileWhileCmd: test expr is enclosed in quotes} { sl@0: set i 0 sl@0: while "$i > 5" {} sl@0: } {} sl@0: test while-1.7 {TclCompileWhileCmd: missing command body} { sl@0: set i 0 sl@0: catch {while {$i < 5} } msg sl@0: set msg sl@0: } {wrong # args: should be "while test command"} sl@0: test while-1.8 {TclCompileWhileCmd: error compiling command body} { sl@0: set i 0 sl@0: catch {while {$i < 5} {set}} msg sl@0: set errorInfo sl@0: } {wrong # args: should be "set varName ?newValue?" sl@0: while compiling sl@0: "set" sl@0: ("while" body line 1) sl@0: while compiling sl@0: "while {$i < 5} {set}"} sl@0: test while-1.9 {TclCompileWhileCmd: simple command body} { sl@0: set a {} sl@0: set i 1 sl@0: while {$i<6} { sl@0: if $i==4 break sl@0: set a [concat $a $i] sl@0: incr i sl@0: } sl@0: set a sl@0: } {1 2 3} sl@0: test while-1.10 {TclCompileWhileCmd: command body in quotes} { sl@0: set a {} sl@0: set i 1 sl@0: while {$i<6} "append a x; incr i" sl@0: set a sl@0: } {xxxxx} sl@0: test while-1.11 {TclCompileWhileCmd: computed command body} { sl@0: catch {unset x1} sl@0: catch {unset bb} sl@0: catch {unset x2} sl@0: set x1 {append a x1; } sl@0: set bb {break} sl@0: set x2 {; append a x2; incr i} sl@0: set a {} sl@0: set i 1 sl@0: while {$i<6} $x1$bb$x2 sl@0: set a sl@0: } {x1} sl@0: test while-1.12 {TclCompileWhileCmd: long command body} { sl@0: set a {} sl@0: set i 1 sl@0: while {$i<6} { sl@0: if $i==4 break sl@0: if $i>5 continue sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: set a [concat $a $i] sl@0: incr i sl@0: } sl@0: set a sl@0: } {1 2 3} sl@0: test while-1.13 {TclCompileWhileCmd: while command result} { sl@0: set i 0 sl@0: set a [while {$i < 5} {incr i}] sl@0: set a sl@0: } {} sl@0: test while-1.14 {TclCompileWhileCmd: while command result} { sl@0: set i 0 sl@0: set a [while {$i < 5} {if $i==3 break; incr i}] sl@0: set a sl@0: } {} sl@0: sl@0: # Check "while" and "continue". sl@0: sl@0: test while-2.1 {continue tests} { sl@0: set a {} sl@0: set i 1 sl@0: while {$i <= 4} { sl@0: incr i sl@0: if {$i == 3} continue sl@0: set a [concat $a $i] sl@0: } sl@0: set a sl@0: } {2 4 5} sl@0: test while-2.2 {continue tests} { sl@0: set a {} sl@0: set i 1 sl@0: while {$i <= 4} { sl@0: incr i sl@0: if {$i != 2} continue sl@0: set a [concat $a $i] sl@0: } sl@0: set a sl@0: } {2} sl@0: test while-2.3 {continue tests, nested loops} { sl@0: set msg {} sl@0: set i 1 sl@0: while {$i <= 4} { sl@0: incr i sl@0: set a 1 sl@0: while {$a <= 2} { sl@0: incr a sl@0: if {$i>=3 && $a>=3} continue sl@0: set msg [concat $msg "$i.$a"] sl@0: } sl@0: } sl@0: set msg sl@0: } {2.2 2.3 3.2 4.2 5.2} sl@0: test while-2.4 {continue tests, long command body} { sl@0: set a {} sl@0: set i 1 sl@0: while {$i<6} { sl@0: if $i==2 {incr i; continue} sl@0: if $i==4 break sl@0: if $i>5 continue sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: set a [concat $a $i] sl@0: incr i sl@0: } sl@0: set a sl@0: } {1 3} sl@0: sl@0: # Check "while" and "break". sl@0: sl@0: test while-3.1 {break tests} { sl@0: set a {} sl@0: set i 1 sl@0: while {$i <= 4} { sl@0: if {$i == 3} break sl@0: set a [concat $a $i] sl@0: incr i sl@0: } sl@0: set a sl@0: } {1 2} sl@0: test while-3.2 {break tests, nested loops} { sl@0: set msg {} sl@0: set i 1 sl@0: while {$i <= 4} { sl@0: set a 1 sl@0: while {$a <= 2} { sl@0: if {$i>=2 && $a>=2} break sl@0: set msg [concat $msg "$i.$a"] sl@0: incr a sl@0: } sl@0: incr i sl@0: } sl@0: set msg sl@0: } {1.1 1.2 2.1 3.1 4.1} sl@0: test while-3.3 {break tests, long command body} { sl@0: set a {} sl@0: set i 1 sl@0: while {$i<6} { sl@0: if $i==2 {incr i; continue} sl@0: if $i==5 break sl@0: if $i>5 continue sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: if $i==4 break sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: set a [concat $a $i] sl@0: incr i sl@0: } sl@0: set a sl@0: } {1 3} sl@0: sl@0: # Check "while" with computed command names. sl@0: sl@0: test while-4.1 {while and computed command names} { sl@0: set i 0 sl@0: set z while sl@0: $z {$i < 10} { sl@0: incr i sl@0: } sl@0: set i sl@0: } 10 sl@0: test while-4.2 {while (not compiled): missing test expression} { sl@0: set z while sl@0: catch {$z } msg sl@0: set msg sl@0: } {wrong # args: should be "while test command"} sl@0: test while-4.3 {while (not compiled): error in test expression} { sl@0: set i 0 sl@0: set z while sl@0: catch {$z {$i<} {set x 1}} msg sl@0: set errorInfo sl@0: } {syntax error in expression "$i<": premature end of expression sl@0: while executing sl@0: "$z {$i<} {set x 1}"} sl@0: test while-4.4 {while (not compiled): error in test expression} { sl@0: set z while sl@0: set err [catch {$z {"a"+"b"} {error "loop aborted"}} msg] sl@0: list $err $msg sl@0: } {1 {can't use non-numeric string as operand of "+"}} sl@0: test while-4.5 {while (not compiled): multiline test expr} { sl@0: set value 1 sl@0: set z while sl@0: $z {($tcl_platform(platform) != "foobar1") && \ sl@0: ($tcl_platform(platform) != "foobar2")} { sl@0: incr value sl@0: break sl@0: } sl@0: set value sl@0: } {2} sl@0: test while-4.6 {while (not compiled): non-numeric boolean test expr} { sl@0: set value 1 sl@0: set z while sl@0: $z {"true"} { sl@0: incr value; sl@0: if {$value > 5} { sl@0: break; sl@0: } sl@0: } sl@0: set value sl@0: } 6 sl@0: test while-4.7 {while (not compiled): test expr is enclosed in quotes} { sl@0: set i 0 sl@0: set z while sl@0: $z "$i > 5" {} sl@0: } {} sl@0: test while-4.8 {while (not compiled): missing command body} { sl@0: set i 0 sl@0: set z while sl@0: catch {$z {$i < 5} } msg sl@0: set msg sl@0: } {wrong # args: should be "while test command"} sl@0: test while-4.9 {while (not compiled): error compiling command body} { sl@0: set i 0 sl@0: set z while sl@0: catch {$z {$i < 5} {set}} msg sl@0: set errorInfo sl@0: } {wrong # args: should be "set varName ?newValue?" sl@0: while compiling sl@0: "set" sl@0: ("while" body line 1) sl@0: invoked from within sl@0: "$z {$i < 5} {set}"} sl@0: test while-4.10 {while (not compiled): simple command body} { sl@0: set a {} sl@0: set i 1 sl@0: set z while sl@0: $z {$i<6} { sl@0: if $i==4 break sl@0: set a [concat $a $i] sl@0: incr i sl@0: } sl@0: set a sl@0: } {1 2 3} sl@0: test while-4.11 {while (not compiled): command body in quotes} { sl@0: set a {} sl@0: set i 1 sl@0: set z while sl@0: $z {$i<6} "append a x; incr i" sl@0: set a sl@0: } {xxxxx} sl@0: test while-4.12 {while (not compiled): computed command body} { sl@0: set z while sl@0: catch {unset x1} sl@0: catch {unset bb} sl@0: catch {unset x2} sl@0: set x1 {append a x1; } sl@0: set bb {break} sl@0: set x2 {; append a x2; incr i} sl@0: set a {} sl@0: set i 1 sl@0: $z {$i<6} $x1$bb$x2 sl@0: set a sl@0: } {x1} sl@0: test while-4.13 {while (not compiled): long command body} { sl@0: set a {} sl@0: set z while sl@0: set i 1 sl@0: $z {$i<6} { sl@0: if $i==4 break sl@0: if $i>5 continue sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: set a [concat $a $i] sl@0: incr i sl@0: } sl@0: set a sl@0: } {1 2 3} sl@0: test while-4.14 {while (not compiled): while command result} { sl@0: set i 0 sl@0: set z while sl@0: set a [$z {$i < 5} {incr i}] sl@0: set a sl@0: } {} sl@0: test while-4.15 {while (not compiled): while command result} { sl@0: set i 0 sl@0: set z while sl@0: set a [$z {$i < 5} {if $i==3 break; incr i}] sl@0: set a sl@0: } {} sl@0: sl@0: # Check "break" with computed command names. sl@0: sl@0: test while-5.1 {break and computed command names} { sl@0: set i 0 sl@0: set z break sl@0: while 1 { sl@0: if {$i > 10} $z sl@0: incr i sl@0: } sl@0: set i sl@0: } 11 sl@0: test while-5.2 {break tests with computed command names} { sl@0: set a {} sl@0: set i 1 sl@0: set z break sl@0: while {$i <= 4} { sl@0: if {$i == 3} $z sl@0: set a [concat $a $i] sl@0: incr i sl@0: } sl@0: set a sl@0: } {1 2} sl@0: test while-5.3 {break tests, nested loops with computed command names} { sl@0: set msg {} sl@0: set i 1 sl@0: set z break sl@0: while {$i <= 4} { sl@0: set a 1 sl@0: while {$a <= 2} { sl@0: if {$i>=2 && $a>=2} $z sl@0: set msg [concat $msg "$i.$a"] sl@0: incr a sl@0: } sl@0: incr i sl@0: } sl@0: set msg sl@0: } {1.1 1.2 2.1 3.1 4.1} sl@0: test while-5.4 {break tests, long command body with computed command names} { sl@0: set a {} sl@0: set i 1 sl@0: set z break sl@0: while {$i<6} { sl@0: if $i==2 {incr i; continue} sl@0: if $i==5 $z sl@0: if $i>5 continue sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: if $i==4 $z sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: set a [concat $a $i] sl@0: incr i sl@0: } sl@0: set a sl@0: } {1 3} sl@0: sl@0: # Check "continue" with computed command names. sl@0: sl@0: test while-6.1 {continue and computed command names} { sl@0: set i 0 sl@0: set z continue sl@0: while 1 { sl@0: incr i sl@0: if {$i < 10} $z sl@0: break sl@0: } sl@0: set i sl@0: } 10 sl@0: test while-6.2 {continue tests} { sl@0: set a {} sl@0: set i 1 sl@0: set z continue sl@0: while {$i <= 4} { sl@0: incr i sl@0: if {$i == 3} $z sl@0: set a [concat $a $i] sl@0: } sl@0: set a sl@0: } {2 4 5} sl@0: test while-6.3 {continue tests with computed command names} { sl@0: set a {} sl@0: set i 1 sl@0: set z continue sl@0: while {$i <= 4} { sl@0: incr i sl@0: if {$i != 2} $z sl@0: set a [concat $a $i] sl@0: } sl@0: set a sl@0: } {2} sl@0: test while-6.4 {continue tests, nested loops with computed command names} { sl@0: set msg {} sl@0: set i 1 sl@0: set z continue sl@0: while {$i <= 4} { sl@0: incr i sl@0: set a 1 sl@0: while {$a <= 2} { sl@0: incr a sl@0: if {$i>=3 && $a>=3} $z sl@0: set msg [concat $msg "$i.$a"] sl@0: } sl@0: } sl@0: set msg sl@0: } {2.2 2.3 3.2 4.2 5.2} sl@0: test while-6.5 {continue tests, long command body with computed command names} { sl@0: set a {} sl@0: set i 1 sl@0: set z continue sl@0: while {$i<6} { sl@0: if $i==2 {incr i; continue} sl@0: if $i==4 break sl@0: if $i>5 $z sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: if {$i>6 && $tcl_platform(machine)=="xxx"} { sl@0: catch {set a $a} msg sl@0: catch {incr i 5} msg sl@0: catch {incr i -5} msg sl@0: } sl@0: set a [concat $a $i] sl@0: incr i sl@0: } sl@0: set a sl@0: } {1 3} sl@0: sl@0: # Test for incorrect "double evaluation" semantics sl@0: sl@0: test while-7.1 {delayed substitution of body} { sl@0: set i 0 sl@0: while {[incr i] < 10} " sl@0: set result $i sl@0: " sl@0: proc p {} { sl@0: set i 0 sl@0: while {[incr i] < 10} " sl@0: set result $i sl@0: " sl@0: set result sl@0: } sl@0: append result [p] sl@0: } {00} sl@0: sl@0: # cleanup sl@0: ::tcltest::cleanupTests sl@0: return