Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/tests/while.test @ 25

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

added tcl to libs

File size: 13.9 KB
Line 
1# Commands covered:  while
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) 1996 Sun Microsystems, Inc.
8# Copyright (c) 1998-1999 by Scriptics Corporation.
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: while.test,v 1.13 2006/10/09 19:15:45 msofer Exp $
14
15if {[lsearch [namespace children] ::tcltest] == -1} {
16    package require tcltest 2
17    namespace import -force ::tcltest::*
18}
19
20# Basic "while" operation.
21
22catch {unset i}
23catch {unset a}
24
25test while-1.1 {TclCompileWhileCmd: missing test expression} {
26    catch {while } msg
27    set msg
28} {wrong # args: should be "while test command"}
29test while-1.2 {TclCompileWhileCmd: error in test expression} -body {
30    set i 0
31    catch {while {$i<} break} msg
32    set ::errorInfo
33} -match glob -result {*"while {$i<} break"}
34test while-1.3 {TclCompileWhileCmd: error in test expression} {
35    set err [catch {while {"a"+"b"} {error "loop aborted"}} msg]
36    list $err $msg
37} {1 {can't use non-numeric string as operand of "+"}}
38test while-1.4 {TclCompileWhileCmd: multiline test expr} {
39    set value 1
40    while {($tcl_platform(platform) != "foobar1") && \
41            ($tcl_platform(platform) != "foobar2")} {
42        incr value
43        break
44    }
45    set value
46} {2}
47test while-1.5 {TclCompileWhileCmd: non-numeric boolean test expr} {
48    set value 1
49    while {"true"} {
50        incr value;
51        if {$value > 5} {
52            break;
53        }
54    }
55    set value
56} 6
57test while-1.6 {TclCompileWhileCmd: test expr is enclosed in quotes} {
58    set i 0
59    while "$i > 5" {}
60} {}
61test while-1.7 {TclCompileWhileCmd: missing command body} {
62    set i 0
63    catch {while {$i < 5} } msg
64    set msg
65} {wrong # args: should be "while test command"}
66test while-1.8 {TclCompileWhileCmd: error compiling command body} -body {
67    set i 0
68    catch {while {$i < 5} {set}} msg
69    set ::errorInfo
70} -match glob -result {wrong # args: should be "set varName ?newValue?"
71    while *ing
72"set"*}
73test while-1.9 {TclCompileWhileCmd: simple command body} {
74    set a {}
75    set i 1
76    while {$i<6} {
77        if $i==4 break
78        set a [concat $a $i]
79        incr i
80    }
81    set a
82} {1 2 3}
83test while-1.10 {TclCompileWhileCmd: command body in quotes} {
84    set a {}
85    set i 1
86    while {$i<6} "append a x; incr i"
87    set a
88} {xxxxx}
89test while-1.11 {TclCompileWhileCmd: computed command body} {
90    catch {unset x1}
91    catch {unset bb}
92    catch {unset x2}
93    set x1 {append a x1; }
94    set bb {break}
95    set x2 {; append a x2; incr i}
96    set a {}
97    set i 1
98    while {$i<6} $x1$bb$x2
99    set a
100} {x1}
101test while-1.12 {TclCompileWhileCmd: long command body} {
102    set a {}
103    set i 1
104    while {$i<6} {
105        if $i==4 break
106        if $i>5 continue
107        if {$i>6 && $tcl_platform(machine)=="xxx"} {
108            catch {set a $a} msg
109            catch {incr i 5} msg
110            catch {incr i -5} msg
111        }
112        if {$i>6 && $tcl_platform(machine)=="xxx"} {
113            catch {set a $a} msg
114            catch {incr i 5} msg
115            catch {incr i -5} msg
116        }
117        if {$i>6 && $tcl_platform(machine)=="xxx"} {
118            catch {set a $a} msg
119            catch {incr i 5} msg
120            catch {incr i -5} msg
121        }
122        if {$i>6 && $tcl_platform(machine)=="xxx"} {
123            catch {set a $a} msg
124            catch {incr i 5} msg
125            catch {incr i -5} msg
126        }
127        if {$i>6 && $tcl_platform(machine)=="xxx"} {
128            catch {set a $a} msg
129            catch {incr i 5} msg
130            catch {incr i -5} msg
131        }
132        set a [concat $a $i]
133        incr i
134    }
135    set a
136} {1 2 3}
137test while-1.13 {TclCompileWhileCmd: while command result} {
138    set i 0
139    set a [while {$i < 5} {incr i}]
140    set a
141} {}
142test while-1.14 {TclCompileWhileCmd: while command result} {
143    set i 0
144    set a [while {$i < 5} {if $i==3 break; incr i}]
145    set a
146} {}
147
148# Check "while" and "continue".
149
150test while-2.1 {continue tests} {
151    set a {}
152    set i 1
153    while {$i <= 4} {
154        incr i
155        if {$i == 3} continue
156        set a [concat $a $i]
157    }
158    set a
159} {2 4 5}
160test while-2.2 {continue tests} {
161    set a {}
162    set i 1
163    while {$i <= 4} {
164        incr i
165        if {$i != 2} continue
166        set a [concat $a $i]
167    }
168    set a
169} {2}
170test while-2.3 {continue tests, nested loops} {
171    set msg {}
172    set i 1
173    while {$i <= 4} {
174        incr i
175        set a 1
176        while {$a <= 2} {
177            incr a
178            if {$i>=3 && $a>=3} continue
179            set msg [concat $msg "$i.$a"]
180        }
181    }
182    set msg
183} {2.2 2.3 3.2 4.2 5.2}
184test while-2.4 {continue tests, long command body} {
185    set a {}
186    set i 1
187    while {$i<6} {
188        if $i==2 {incr i; continue}
189        if $i==4 break
190        if $i>5 continue
191        if {$i>6 && $tcl_platform(machine)=="xxx"} {
192            catch {set a $a} msg
193            catch {incr i 5} msg
194            catch {incr i -5} msg
195        }
196        if {$i>6 && $tcl_platform(machine)=="xxx"} {
197            catch {set a $a} msg
198            catch {incr i 5} msg
199            catch {incr i -5} msg
200        }
201        if {$i>6 && $tcl_platform(machine)=="xxx"} {
202            catch {set a $a} msg
203            catch {incr i 5} msg
204            catch {incr i -5} msg
205        }
206        if {$i>6 && $tcl_platform(machine)=="xxx"} {
207            catch {set a $a} msg
208            catch {incr i 5} msg
209            catch {incr i -5} msg
210        }
211        if {$i>6 && $tcl_platform(machine)=="xxx"} {
212            catch {set a $a} msg
213            catch {incr i 5} msg
214            catch {incr i -5} msg
215        }
216        set a [concat $a $i]
217        incr i
218    }
219    set a
220} {1 3}
221
222# Check "while" and "break".
223
224test while-3.1 {break tests} {
225    set a {}
226    set i 1
227    while {$i <= 4} {
228        if {$i == 3} break
229        set a [concat $a $i]
230        incr i
231    }
232    set a
233} {1 2}
234test while-3.2 {break tests, nested loops} {
235    set msg {}
236    set i 1
237    while {$i <= 4} {
238        set a 1
239        while {$a <= 2} {
240            if {$i>=2 && $a>=2} break
241            set msg [concat $msg "$i.$a"]
242            incr a
243        }
244        incr i
245    }
246    set msg
247} {1.1 1.2 2.1 3.1 4.1}
248test while-3.3 {break tests, long command body} {
249    set a {}
250    set i 1
251    while {$i<6} {
252        if $i==2 {incr i; continue}
253        if $i==5 break
254        if $i>5 continue
255        if {$i>6 && $tcl_platform(machine)=="xxx"} {
256            catch {set a $a} msg
257            catch {incr i 5} msg
258            catch {incr i -5} msg
259        }
260        if {$i>6 && $tcl_platform(machine)=="xxx"} {
261            catch {set a $a} msg
262            catch {incr i 5} msg
263            catch {incr i -5} msg
264        }
265        if {$i>6 && $tcl_platform(machine)=="xxx"} {
266            catch {set a $a} msg
267            catch {incr i 5} msg
268            catch {incr i -5} msg
269        }
270        if $i==4 break
271        if {$i>6 && $tcl_platform(machine)=="xxx"} {
272            catch {set a $a} msg
273            catch {incr i 5} msg
274            catch {incr i -5} msg
275        }
276        if {$i>6 && $tcl_platform(machine)=="xxx"} {
277            catch {set a $a} msg
278            catch {incr i 5} msg
279            catch {incr i -5} msg
280        }
281        set a [concat $a $i]
282        incr i
283    }
284    set a
285} {1 3}
286
287# Check "while" with computed command names.
288
289test while-4.1 {while and computed command names} {
290    set i 0
291    set z while
292    $z {$i < 10} {
293        incr i
294    }
295    set i
296} 10
297test while-4.2 {while (not compiled): missing test expression} {
298    set z while
299    catch {$z } msg
300    set msg
301} {wrong # args: should be "while test command"}
302test while-4.3 {while (not compiled): error in test expression} -body {
303    set i 0
304    set z while
305    catch {$z {$i<} {set x 1}} msg
306    set ::errorInfo
307} -match glob -result {*"$z {$i<} {set x 1}"}
308test while-4.4 {while (not compiled): error in test expression} {
309    set z while
310    set err [catch {$z {"a"+"b"} {error "loop aborted"}} msg]
311    list $err $msg
312} {1 {can't use non-numeric string as operand of "+"}}
313test while-4.5 {while (not compiled): multiline test expr} {
314    set value 1
315    set z while
316    $z {($tcl_platform(platform) != "foobar1") && \
317            ($tcl_platform(platform) != "foobar2")} {
318        incr value
319        break
320    }
321    set value
322} {2}
323test while-4.6 {while (not compiled): non-numeric boolean test expr} {
324    set value 1
325    set z while
326    $z {"true"} {
327        incr value;
328        if {$value > 5} {
329            break;
330        }
331    }
332    set value
333} 6
334test while-4.7 {while (not compiled): test expr is enclosed in quotes} {
335    set i 0
336    set z while
337    $z "$i > 5" {}
338} {}
339test while-4.8 {while (not compiled): missing command body} {
340    set i 0
341    set z while
342    catch {$z {$i < 5} } msg
343    set msg
344} {wrong # args: should be "while test command"}
345test while-4.9 {while (not compiled): error compiling command body} -body {
346    set i 0
347    set z while
348    catch {$z {$i < 5} {set}} msg
349    set ::errorInfo
350} -match glob -result {wrong # args: should be "set varName ?newValue?"
351    while *ing
352"set"
353    ("while" body line 1)
354    invoked from within
355"$z {$i < 5} {set}"}
356test while-4.10 {while (not compiled): simple command body} {
357    set a {}
358    set i 1
359    set z while
360    $z {$i<6} {
361        if $i==4 break
362        set a [concat $a $i]
363        incr i
364    }
365    set a
366} {1 2 3}
367test while-4.11 {while (not compiled): command body in quotes} {
368    set a {}
369    set i 1
370    set z while
371    $z {$i<6} "append a x; incr i"
372    set a
373} {xxxxx}
374test while-4.12 {while (not compiled): computed command body} {
375    set z while
376    catch {unset x1}
377    catch {unset bb}
378    catch {unset x2}
379    set x1 {append a x1; }
380    set bb {break}
381    set x2 {; append a x2; incr i}
382    set a {}
383    set i 1
384    $z {$i<6} $x1$bb$x2
385    set a
386} {x1}
387test while-4.13 {while (not compiled): long command body} {
388    set a {}
389    set z while
390    set i 1
391    $z {$i<6} {
392        if $i==4 break
393        if $i>5 continue
394        if {$i>6 && $tcl_platform(machine)=="xxx"} {
395            catch {set a $a} msg
396            catch {incr i 5} msg
397            catch {incr i -5} msg
398        }
399        if {$i>6 && $tcl_platform(machine)=="xxx"} {
400            catch {set a $a} msg
401            catch {incr i 5} msg
402            catch {incr i -5} msg
403        }
404        if {$i>6 && $tcl_platform(machine)=="xxx"} {
405            catch {set a $a} msg
406            catch {incr i 5} msg
407            catch {incr i -5} msg
408        }
409        if {$i>6 && $tcl_platform(machine)=="xxx"} {
410            catch {set a $a} msg
411            catch {incr i 5} msg
412            catch {incr i -5} msg
413        }
414        if {$i>6 && $tcl_platform(machine)=="xxx"} {
415            catch {set a $a} msg
416            catch {incr i 5} msg
417            catch {incr i -5} msg
418        }
419        set a [concat $a $i]
420        incr i
421    }
422    set a
423} {1 2 3}
424test while-4.14 {while (not compiled): while command result} {
425    set i 0
426    set z while
427    set a [$z {$i < 5} {incr i}]
428    set a
429} {}
430test while-4.15 {while (not compiled): while command result} {
431    set i 0
432    set z while
433    set a [$z {$i < 5} {if $i==3 break; incr i}]
434    set a
435} {}
436
437# Check "break" with computed command names.
438
439test while-5.1 {break and computed command names} {
440    set i 0
441    set z break
442    while 1 {
443        if {$i > 10} $z
444        incr i
445    }
446    set i
447} 11
448test while-5.2 {break tests with computed command names} {
449    set a {}
450    set i 1
451    set z break
452    while {$i <= 4} {
453        if {$i == 3} $z
454        set a [concat $a $i]
455        incr i
456    }
457    set a
458} {1 2}
459test while-5.3 {break tests, nested loops with computed command names} {
460    set msg {}
461    set i 1
462    set z break
463    while {$i <= 4} {
464        set a 1
465        while {$a <= 2} {
466            if {$i>=2 && $a>=2} $z
467            set msg [concat $msg "$i.$a"]
468            incr a
469        }
470        incr i
471    }
472    set msg
473} {1.1 1.2 2.1 3.1 4.1}
474test while-5.4 {break tests, long command body with computed command names} {
475    set a {}
476    set i 1
477    set z break
478    while {$i<6} {
479        if $i==2 {incr i; continue}
480        if $i==5 $z
481        if $i>5 continue
482        if {$i>6 && $tcl_platform(machine)=="xxx"} {
483            catch {set a $a} msg
484            catch {incr i 5} msg
485            catch {incr i -5} msg
486        }
487        if {$i>6 && $tcl_platform(machine)=="xxx"} {
488            catch {set a $a} msg
489            catch {incr i 5} msg
490            catch {incr i -5} msg
491        }
492        if {$i>6 && $tcl_platform(machine)=="xxx"} {
493            catch {set a $a} msg
494            catch {incr i 5} msg
495            catch {incr i -5} msg
496        }
497        if $i==4 $z
498        if {$i>6 && $tcl_platform(machine)=="xxx"} {
499            catch {set a $a} msg
500            catch {incr i 5} msg
501            catch {incr i -5} msg
502        }
503        if {$i>6 && $tcl_platform(machine)=="xxx"} {
504            catch {set a $a} msg
505            catch {incr i 5} msg
506            catch {incr i -5} msg
507        }
508        set a [concat $a $i]
509        incr i
510    }
511    set a
512} {1 3}
513
514# Check "continue" with computed command names.
515
516test while-6.1 {continue and computed command names} {
517    set i 0
518    set z continue
519    while 1 {
520        incr i
521        if {$i < 10} $z
522        break
523    }
524    set i
525} 10
526test while-6.2 {continue tests} {
527    set a {}
528    set i 1
529    set z continue
530    while {$i <= 4} {
531        incr i
532        if {$i == 3} $z
533        set a [concat $a $i]
534    }
535    set a
536} {2 4 5}
537test while-6.3 {continue tests with computed command names} {
538    set a {}
539    set i 1
540    set z continue
541    while {$i <= 4} {
542        incr i
543        if {$i != 2} $z
544        set a [concat $a $i]
545    }
546    set a
547} {2}
548test while-6.4 {continue tests, nested loops with computed command names} {
549    set msg {}
550    set i 1
551    set z continue
552    while {$i <= 4} {
553        incr i
554        set a 1
555        while {$a <= 2} {
556            incr a
557            if {$i>=3 && $a>=3} $z
558            set msg [concat $msg "$i.$a"]
559        }
560    }
561    set msg
562} {2.2 2.3 3.2 4.2 5.2}
563test while-6.5 {continue tests, long command body with computed command names} {
564    set a {}
565    set i 1
566    set z continue
567    while {$i<6} {
568        if $i==2 {incr i; continue}
569        if $i==4 break
570        if $i>5 $z
571        if {$i>6 && $tcl_platform(machine)=="xxx"} {
572            catch {set a $a} msg
573            catch {incr i 5} msg
574            catch {incr i -5} msg
575        }
576        if {$i>6 && $tcl_platform(machine)=="xxx"} {
577            catch {set a $a} msg
578            catch {incr i 5} msg
579            catch {incr i -5} msg
580        }
581        if {$i>6 && $tcl_platform(machine)=="xxx"} {
582            catch {set a $a} msg
583            catch {incr i 5} msg
584            catch {incr i -5} msg
585        }
586        if {$i>6 && $tcl_platform(machine)=="xxx"} {
587            catch {set a $a} msg
588            catch {incr i 5} msg
589            catch {incr i -5} msg
590        }
591        if {$i>6 && $tcl_platform(machine)=="xxx"} {
592            catch {set a $a} msg
593            catch {incr i 5} msg
594            catch {incr i -5} msg
595        }
596        set a [concat $a $i]
597        incr i
598    }
599    set a
600} {1 3}
601
602# Test for incorrect "double evaluation" semantics
603
604test while-7.1 {delayed substitution of body} {
605    set i 0
606    while {[incr i] < 10} "
607       set result $i
608    "
609    proc p {} {
610        set i 0
611        while {[incr i] < 10} "
612            set result $i
613        "
614        set result
615    }
616    append result [p]
617} {00}
618
619# cleanup
620::tcltest::cleanupTests
621return
Note: See TracBrowser for help on using the repository browser.