summaryrefslogtreecommitdiffstats
path: root/tcllib/modules/control/do.test
blob: d29a91062710cb79c4605c421dd0744a1d645209 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# do.test --
#
#         Tests for [control::do]
# 
# RCS: @(#) $Id: do.test,v 1.14 2009/11/24 04:52:49 andreas_kupries Exp $
#

# -------------------------------------------------------------------------

source [file join \
	[file dirname [file dirname [file join [pwd] [info script]]]] \
	devtools testutilities.tcl]

testsNeedTcl     8.2
testsNeedTcltest 1.0

testing {
    useLocal control.tcl control
}

# -------------------------------------------------------------------------

namespace import ::control::do

# ----------------------------------------
test {do-1.0} {do ... while} {
    set x 0
    do {incr x} while {$x < 10}
    set x
} 10

# ----------------------------------------
test {do-1.1} {do ... until} {
    set x 0
    do {incr x} until {$x > 10}
    set x
} 11

# ----------------------------------------
test {do-1.2} {break} {
    set x 0
    do {
	incr x
	if {$x == 5} {break}
    } until {$x == 10}
    set x
} 5

# ----------------------------------------
test {do-1.3} {continue} {

    set x 0
    set xx [list]
    do {
	incr x
	if {$x == 5} {continue}
	lappend xx $x
    } until {$x == 10}
    set xx
} {1 2 3 4 6 7 8 9 10}

# ----------------------------------------
test {do-1.4} {error} {
    catch {
	set x 0
	do {
	    incr x
	    if {$x == 5} {foo}
	} while {$x < 10}
    } result
    list $x $result
} {5 {invalid command name "foo"}}

# ----------------------------------------
test {do-1.5} {return} {
    proc foo {} {
	set x 0
	do {
	    incr x
	    if {$x == 5} { return $x }
	} while {$x < 10}
    }
    set result [foo]
    rename foo ""
    set result
} 5

# ----------------------------------------
test {do-1.6} {break in the first loop} {
    set x 0
    do {
	break
	incr x
    } while {$x < 10}
    set x
} 0

# ----------------------------------------
test {do-1.7} {continue in the first loop} {
    set x 0
    set xx [list]
    do {
	incr x
	if {$x == 1} {continue}
	lappend xx $x
    } until {$x == 10}
    set xx
} {2 3 4 5 6 7 8 9 10}

# ----------------------------------------
test {do-1.8} {error in the first loop} {
    set x 0
    catch {
	do {
	    foo
	    incr x
	} until {$x == 10}
    } result
    list $x $result
} {0 {invalid command name "foo"}}

# ----------------------------------------
test {do-1.9} {[do ... while] with false condition} {
    set x 0
    do {
	incr x
    } while 0
    set x
} 1

# ----------------------------------------
test do-1.10 {[do ... until] with true condition} {
    set x 0
    do {
	incr x
    } until 1
    set x
} 1

# ----------------------------------------
test do-1.11 {third arg is neither while nor until} {
    set x 0
    catch {
	do {
	    incr x
	} foo 1
	set x
    } result
    list $x $result
} {0 {bad option "foo": must be until, or while}}

# ----------------------------------------
test do-1.12 {stack traces for errors in the first iteration} {
    proc a {} b
    proc b {} {do c while 1}
    proc c {} d
    catch a
    set ::errorInfo
} {invalid command name "d"
    while executing
"d"
    (procedure "c" line 1)
    invoked from within
"c"
    ("do" body line 1)
    invoked from within
"do c while 1"
    (procedure "b" line 1)
    invoked from within
"b"
    (procedure "a" line 1)
    invoked from within
"a"}

# ----------------------------------------
test do-1.14 {stack traces for errors in subsequent iterations} tcl8.3plus {
    proc a {} b
    proc b {} {
	set i 10
	do {
	    incr i -1
	    c $i
	} while {$i}
    }
    proc c {i} {if {$i==5} e}
    catch a
    set ::errorInfo
} {invalid command name "e"
    while executing
"e"
    (procedure "c" line 1)
    invoked from within
"c $i"
    ("do" body line 3)
    invoked from within
"do {
	    incr i -1
	    c $i
	} while {$i}"
    (procedure "b" line 3)
    invoked from within
"b"
    (procedure "a" line 1)
    invoked from within
"a"}

# ----------------------------------------
test do-2.0 {one-shot do} {
    set x 0
    do {incr x}
    set x
} 1

# ----------------------------------------
test do-2.1 {one-shot do with break} {
    set x 0
    do {incr x; break; incr x}
    set x
} 1

# ----------------------------------------
test do-2.2 {wrong no of arguments} {
    set x 0
    set res [catch {do {incr x} foo} ret]
    list $x $res $errorInfo
} {0 1 {wrong # args: should be "::control::do body" or "::control::do body [until|while] test"
    while executing
"do {incr x} foo"}}

# ----------------------------------------

if {[package vsatisfies [package provide Tcl] 8.6]} {
    # 8.6+
    set do23res {1 {wrong # args: should be "do body ?arg ...?"
    while executing
"do"}}
} elseif {[package vsatisfies [package provide Tcl] 8.5]} {
    # 8.5+
    set do23res {1 {wrong # args: should be "do body ..."
    while executing
"do"}}
} else {
    # 8.4-
    set do23res {1 {wrong # args: should be "do body args"
    while executing
"do"}}
}

test do-2.3 {wrong no of arguments} {} {
    set res [catch do]
    if {[string match \
	     {no value given for parameter "body" to "do"*} \
	     $::errorInfo]
    } then {
	set ::errorInfo {wrong # args: should be "do body args"
    while executing
"do"}
    }
    list $res $::errorInfo
} $do23res

# ----------------------------------------
test do-2.4 {one-shot do with error} {
    set x 0
    set res [catch {do {
	incr x
	foo
	incr x
    }}]
    list $x $res $::errorInfo
} {1 1 {invalid command name "foo"
    while executing
"foo"
    ("do" body line 3)
    invoked from within
"do {
	incr x
	foo
	incr x
    }"}}

testsuiteCleanup

if {[info exists ::argv0] && $::argv0 == [info script]} {
    # a proc that wastes some time
    proc something {n} {
	for {set i 0} {$i < $n} {incr i} {}
    }
    
    proc main {} {
	# run it for the first time to get it byte compiled
	something 1
	
	set payload {
	    something 10
	    incr x
	}
	puts "\nComparing performance of do-while, do-until and builtin while..."
	set format "%-8s : %20s for %4d iteration(s)."
	foreach c {1 10 5000} {
	    puts ""
	    foreach {descr script} {
		{do while} {do $payload while {$x < $c}}
		{do until} {do $payload until {$x == $c}}
		{while} {while {$x < $c} $payload}
	    } {
		set x 0
		puts [format $format $descr [lrange [time $script 1] 0 1] $x]
	    }
	}
    }
    main
}

# Local variables:
# mode: tcl
# End: