| [25] | 1 | # Commands covered: foreach, continue, break |
|---|
| 2 | # |
|---|
| 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. |
|---|
| 6 | # |
|---|
| 7 | # Copyright (c) 1991-1993 The Regents of the University of California. |
|---|
| 8 | # Copyright (c) 1994-1997 Sun Microsystems, Inc. |
|---|
| 9 | # |
|---|
| 10 | # See the file "license.terms" for information on usage and redistribution |
|---|
| 11 | # of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
|---|
| 12 | # |
|---|
| 13 | # RCS: @(#) $Id: foreach.test,v 1.14 2008/03/14 17:43:25 dgp Exp $ |
|---|
| 14 | |
|---|
| 15 | if {[lsearch [namespace children] ::tcltest] == -1} { |
|---|
| 16 | package require tcltest |
|---|
| 17 | namespace import -force ::tcltest::* |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | catch {unset a} |
|---|
| 21 | catch {unset x} |
|---|
| 22 | |
|---|
| 23 | # Basic "foreach" operation. |
|---|
| 24 | |
|---|
| 25 | test foreach-1.1 {basic foreach tests} { |
|---|
| 26 | set a {} |
|---|
| 27 | foreach i {a b c d} { |
|---|
| 28 | set a [concat $a $i] |
|---|
| 29 | } |
|---|
| 30 | set a |
|---|
| 31 | } {a b c d} |
|---|
| 32 | test foreach-1.2 {basic foreach tests} { |
|---|
| 33 | set a {} |
|---|
| 34 | foreach i {a b {{c d} e} {123 {{x}}}} { |
|---|
| 35 | set a [concat $a $i] |
|---|
| 36 | } |
|---|
| 37 | set a |
|---|
| 38 | } {a b {c d} e 123 {{x}}} |
|---|
| 39 | test foreach-1.3 {basic foreach tests} {catch {foreach} msg} 1 |
|---|
| 40 | test foreach-1.4 {basic foreach tests} { |
|---|
| 41 | catch {foreach} msg |
|---|
| 42 | set msg |
|---|
| 43 | } {wrong # args: should be "foreach varList list ?varList list ...? command"} |
|---|
| 44 | test foreach-1.5 {basic foreach tests} {catch {foreach i} msg} 1 |
|---|
| 45 | test foreach-1.6 {basic foreach tests} { |
|---|
| 46 | catch {foreach i} msg |
|---|
| 47 | set msg |
|---|
| 48 | } {wrong # args: should be "foreach varList list ?varList list ...? command"} |
|---|
| 49 | test foreach-1.7 {basic foreach tests} {catch {foreach i j} msg} 1 |
|---|
| 50 | test foreach-1.8 {basic foreach tests} { |
|---|
| 51 | catch {foreach i j} msg |
|---|
| 52 | set msg |
|---|
| 53 | } {wrong # args: should be "foreach varList list ?varList list ...? command"} |
|---|
| 54 | test foreach-1.9 {basic foreach tests} {catch {foreach i j k l} msg} 1 |
|---|
| 55 | test foreach-1.10 {basic foreach tests} { |
|---|
| 56 | catch {foreach i j k l} msg |
|---|
| 57 | set msg |
|---|
| 58 | } {wrong # args: should be "foreach varList list ?varList list ...? command"} |
|---|
| 59 | test foreach-1.11 {basic foreach tests} { |
|---|
| 60 | set a {} |
|---|
| 61 | foreach i {} { |
|---|
| 62 | set a [concat $a $i] |
|---|
| 63 | } |
|---|
| 64 | set a |
|---|
| 65 | } {} |
|---|
| 66 | test foreach-1.12 {foreach errors} { |
|---|
| 67 | list [catch {foreach {{a}{b}} {1 2 3} {}} msg] $msg |
|---|
| 68 | } {1 {list element in braces followed by "{b}" instead of space}} |
|---|
| 69 | test foreach-1.13 {foreach errors} { |
|---|
| 70 | list [catch {foreach a {{1 2}3} {}} msg] $msg |
|---|
| 71 | } {1 {list element in braces followed by "3" instead of space}} |
|---|
| 72 | catch {unset a} |
|---|
| 73 | test foreach-1.14 {foreach errors} { |
|---|
| 74 | catch {unset a} |
|---|
| 75 | set a(0) 44 |
|---|
| 76 | list [catch {foreach a {1 2 3} {}} msg o] $msg $::errorInfo |
|---|
| 77 | } {1 {can't set "a": variable is array} {can't set "a": variable is array |
|---|
| 78 | (setting foreach loop variable "a") |
|---|
| 79 | invoked from within |
|---|
| 80 | "foreach a {1 2 3} {}"}} |
|---|
| 81 | test foreach-1.15 {foreach errors} { |
|---|
| 82 | list [catch {foreach {} {} {}} msg] $msg |
|---|
| 83 | } {1 {foreach varlist is empty}} |
|---|
| 84 | catch {unset a} |
|---|
| 85 | |
|---|
| 86 | test foreach-2.1 {parallel foreach tests} { |
|---|
| 87 | set x {} |
|---|
| 88 | foreach {a b} {1 2 3 4} { |
|---|
| 89 | append x $b $a |
|---|
| 90 | } |
|---|
| 91 | set x |
|---|
| 92 | } {2143} |
|---|
| 93 | test foreach-2.2 {parallel foreach tests} { |
|---|
| 94 | set x {} |
|---|
| 95 | foreach {a b} {1 2 3 4 5} { |
|---|
| 96 | append x $b $a |
|---|
| 97 | } |
|---|
| 98 | set x |
|---|
| 99 | } {21435} |
|---|
| 100 | test foreach-2.3 {parallel foreach tests} { |
|---|
| 101 | set x {} |
|---|
| 102 | foreach a {1 2 3} b {4 5 6} { |
|---|
| 103 | append x $b $a |
|---|
| 104 | } |
|---|
| 105 | set x |
|---|
| 106 | } {415263} |
|---|
| 107 | test foreach-2.4 {parallel foreach tests} { |
|---|
| 108 | set x {} |
|---|
| 109 | foreach a {1 2 3} b {4 5 6 7 8} { |
|---|
| 110 | append x $b $a |
|---|
| 111 | } |
|---|
| 112 | set x |
|---|
| 113 | } {41526378} |
|---|
| 114 | test foreach-2.5 {parallel foreach tests} { |
|---|
| 115 | set x {} |
|---|
| 116 | foreach {a b} {a b A B aa bb} c {c C cc CC} { |
|---|
| 117 | append x $a $b $c |
|---|
| 118 | } |
|---|
| 119 | set x |
|---|
| 120 | } {abcABCaabbccCC} |
|---|
| 121 | test foreach-2.6 {parallel foreach tests} { |
|---|
| 122 | set x {} |
|---|
| 123 | foreach a {1 2 3} b {1 2 3} c {1 2 3} d {1 2 3} e {1 2 3} { |
|---|
| 124 | append x $a $b $c $d $e |
|---|
| 125 | } |
|---|
| 126 | set x |
|---|
| 127 | } {111112222233333} |
|---|
| 128 | test foreach-2.7 {parallel foreach tests} { |
|---|
| 129 | set x {} |
|---|
| 130 | foreach a {} b {1 2 3} c {1 2} d {1 2 3 4} e {{1 2}} { |
|---|
| 131 | append x $a $b $c $d $e |
|---|
| 132 | } |
|---|
| 133 | set x |
|---|
| 134 | } {1111 2222334} |
|---|
| 135 | test foreach-2.8 {foreach only sets vars if repeating loop} { |
|---|
| 136 | proc foo {} { |
|---|
| 137 | set rgb {65535 0 0} |
|---|
| 138 | foreach {r g b} [set rgb] {} |
|---|
| 139 | return "r=$r, g=$g, b=$b" |
|---|
| 140 | } |
|---|
| 141 | foo |
|---|
| 142 | } {r=65535, g=0, b=0} |
|---|
| 143 | test foreach-2.9 {foreach only supports local scalar variables} { |
|---|
| 144 | proc foo {} { |
|---|
| 145 | set x {} |
|---|
| 146 | foreach {a(3)} {1 2 3 4} {lappend x [set {a(3)}]} |
|---|
| 147 | set x |
|---|
| 148 | } |
|---|
| 149 | foo |
|---|
| 150 | } {1 2 3 4} |
|---|
| 151 | |
|---|
| 152 | test foreach-3.1 {compiled foreach backward jump works correctly} { |
|---|
| 153 | catch {unset x} |
|---|
| 154 | proc foo {arrayName} { |
|---|
| 155 | upvar 1 $arrayName a |
|---|
| 156 | set l {} |
|---|
| 157 | foreach member [array names a] { |
|---|
| 158 | lappend l [list $member [set a($member)]] |
|---|
| 159 | } |
|---|
| 160 | return $l |
|---|
| 161 | } |
|---|
| 162 | array set x {0 zero 1 one 2 two 3 three} |
|---|
| 163 | lsort [foo x] |
|---|
| 164 | } [lsort {{0 zero} {1 one} {2 two} {3 three}}] |
|---|
| 165 | |
|---|
| 166 | test foreach-4.1 {noncompiled foreach and shared variable or value list objects that are converted to another type} { |
|---|
| 167 | catch {unset x} |
|---|
| 168 | foreach {12.0} {a b c} { |
|---|
| 169 | set x 12.0 |
|---|
| 170 | set x [expr $x + 1] |
|---|
| 171 | } |
|---|
| 172 | set x |
|---|
| 173 | } 13.0 |
|---|
| 174 | |
|---|
| 175 | # Check "continue". |
|---|
| 176 | |
|---|
| 177 | test foreach-5.1 {continue tests} {catch continue} 4 |
|---|
| 178 | test foreach-5.2 {continue tests} { |
|---|
| 179 | set a {} |
|---|
| 180 | foreach i {a b c d} { |
|---|
| 181 | if {[string compare $i "b"] == 0} continue |
|---|
| 182 | set a [concat $a $i] |
|---|
| 183 | } |
|---|
| 184 | set a |
|---|
| 185 | } {a c d} |
|---|
| 186 | test foreach-5.3 {continue tests} { |
|---|
| 187 | set a {} |
|---|
| 188 | foreach i {a b c d} { |
|---|
| 189 | if {[string compare $i "b"] != 0} continue |
|---|
| 190 | set a [concat $a $i] |
|---|
| 191 | } |
|---|
| 192 | set a |
|---|
| 193 | } {b} |
|---|
| 194 | test foreach-5.4 {continue tests} {catch {continue foo} msg} 1 |
|---|
| 195 | test foreach-5.5 {continue tests} { |
|---|
| 196 | catch {continue foo} msg |
|---|
| 197 | set msg |
|---|
| 198 | } {wrong # args: should be "continue"} |
|---|
| 199 | |
|---|
| 200 | # Check "break". |
|---|
| 201 | |
|---|
| 202 | test foreach-6.1 {break tests} {catch break} 3 |
|---|
| 203 | test foreach-6.2 {break tests} { |
|---|
| 204 | set a {} |
|---|
| 205 | foreach i {a b c d} { |
|---|
| 206 | if {[string compare $i "c"] == 0} break |
|---|
| 207 | set a [concat $a $i] |
|---|
| 208 | } |
|---|
| 209 | set a |
|---|
| 210 | } {a b} |
|---|
| 211 | test foreach-6.3 {break tests} {catch {break foo} msg} 1 |
|---|
| 212 | test foreach-6.4 {break tests} { |
|---|
| 213 | catch {break foo} msg |
|---|
| 214 | set msg |
|---|
| 215 | } {wrong # args: should be "break"} |
|---|
| 216 | # Check for bug #406709 |
|---|
| 217 | test foreach-6.5 {break tests} { |
|---|
| 218 | proc a {} { |
|---|
| 219 | set a 1 |
|---|
| 220 | foreach b b {list [concat a; break]; incr a} |
|---|
| 221 | incr a |
|---|
| 222 | } |
|---|
| 223 | a |
|---|
| 224 | } {2} |
|---|
| 225 | |
|---|
| 226 | # Test for incorrect "double evaluation" semantics |
|---|
| 227 | test foreach-7.1 {delayed substitution of body} { |
|---|
| 228 | proc foo {} { |
|---|
| 229 | set a 0 |
|---|
| 230 | foreach a [list 1 2 3] " |
|---|
| 231 | set x $a |
|---|
| 232 | " |
|---|
| 233 | set x |
|---|
| 234 | } |
|---|
| 235 | foo |
|---|
| 236 | } {0} |
|---|
| 237 | |
|---|
| 238 | # Test for [Bug 1189274]; crash on failure |
|---|
| 239 | test foreach-8.1 {empty list handling} { |
|---|
| 240 | proc crash {} { |
|---|
| 241 | rename crash {} |
|---|
| 242 | set a "x y z" |
|---|
| 243 | set b "" |
|---|
| 244 | foreach aa $a bb $b { set x "aa = $aa bb = $bb" } |
|---|
| 245 | } |
|---|
| 246 | crash |
|---|
| 247 | } {} |
|---|
| 248 | |
|---|
| 249 | # [Bug 1671138]; infinite loop with empty var list in bytecompiled version |
|---|
| 250 | test foreach-9.1 {compiled empty var list} { |
|---|
| 251 | proc foo {} { |
|---|
| 252 | foreach {} x { |
|---|
| 253 | error "reached body" |
|---|
| 254 | } |
|---|
| 255 | } |
|---|
| 256 | list [catch { foo } msg] $msg |
|---|
| 257 | } {1 {foreach varlist is empty}} |
|---|
| 258 | |
|---|
| 259 | test foreach-10.1 {foreach: [Bug 1671087]} -setup { |
|---|
| 260 | proc demo {} { |
|---|
| 261 | set vals {1 2 3 4} |
|---|
| 262 | trace add variable x write {string length $vals ;# } |
|---|
| 263 | foreach {x y} $vals {format $y} |
|---|
| 264 | } |
|---|
| 265 | } -body { |
|---|
| 266 | demo |
|---|
| 267 | } -cleanup { |
|---|
| 268 | rename demo {} |
|---|
| 269 | } -result {} |
|---|
| 270 | |
|---|
| 271 | # cleanup |
|---|
| 272 | catch {unset a} |
|---|
| 273 | catch {unset x} |
|---|
| 274 | ::tcltest::cleanupTests |
|---|
| 275 | return |
|---|