aboutsummaryrefslogtreecommitdiff
path: root/tests/timer.test
blob: 2f75d2869108263cbf673ce08f4c3fe311995eed (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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# This file contains a collection of tests for the procedures in the
# file tclTimer.c, which includes the "after" Tcl command.  Sourcing
# this file into Tcl runs the tests and generates output for errors.
# No output means no errors were found.
#
# This file contains a collection of tests for one or more of the Tcl
# built-in commands.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# Copyright (c) 1997 by Sun Microsystems, Inc.
# Copyright (c) 1998-1999 by Scriptics Corporation.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: timer.test,v 1.7.2.1 2001/10/13 01:14:19 hobbs Exp $

source [file dirname [info script]]/testing.tcl
needs cmd after eventloop

test timer-1.1 {Tcl_CreateTimerHandler procedure} {
    foreach i [after info] {
	after cancel $i
    }
    set x ""
    foreach i {40 80 400 20 60} {
	after $i lappend x $i
    }
    after 100
    update
    set x
} {20 40 60 80}

test timer-2.1 {Tcl_DeleteTimerHandler procedure} {
    foreach i [after info] {
	after cancel $i
    }
    set x ""
    foreach i {20 40 60 10 30} {
	after $i lappend x $i
    }
    after cancel lappend x 60
    after cancel lappend x 10
    after 50
    update
    set x
} {20 30 40}

# No tests for Tcl_ServiceTimer or ResetTimer, since it is already tested
# above.

test timer-3.1 {TimerHandlerEventProc procedure: event masks} {
    set x start
    after 20 { set x fired }
    update idletasks
    set result $x
    after 40
    update
    lappend result $x
} {start fired}
test timer-3.2 {TimerHandlerEventProc procedure: multiple timers} {
    foreach i [after info] {
	after cancel $i
    }
    foreach i {80 240 400} {
	after $i lappend x $i
    }
    after 100
    set result ""
    set x ""
    update
    lappend result $x
    after 160
    update
    lappend result $x
    after 160
    update
    lappend result $x
} {80 {80 240} {80 240 400}}
test timer-3.3 {TimerHandlerEventProc procedure: reentrant timer deletion} {
    foreach i [after info] {
	after cancel $i
    }
    set x {}
    after 20 lappend x 20
    set i [after 60 lappend x 60]
    after 40 after cancel $i
    after 80
    update
    set x
} 20
test timer-3.4 {TimerHandlerEventProc procedure: all expired timers fire} {
    foreach i [after info] {
	after cancel $i
    }
    set x {}
    after 20 lappend x a
    after 40 lappend x b
    after 60 lappend x c
    after 70
    vwait x
    set x
} {a b c}
test timer-3.5 {TimerHandlerEventProc procedure: reentrantly added timers don't fire} {
    foreach i [after info] {
	after cancel $i
    }
    set x {}
    after 20 {lappend x a; after 0 lappend x b}
    after 20
    vwait x
    set x
} a
test timer-3.6 {TimerHandlerEventProc procedure: reentrantly added timers don't fire} {
    foreach i [after info] {
	after cancel $i
    }
    set x {}
    after 20 {lappend x a; after 20 lappend x b; after 20}
    after 20
    vwait x
    set result $x
    vwait x
    lappend result $x
} {a {a b}}

# No tests for Tcl_DoWhenIdle:  it's already tested by other tests
# below.

test timer-4.1 {Tcl_CancelIdleCall procedure} {
    foreach i [after info] {
	after cancel $i
    }
    set x before
    set y before
    set z before
    after idle set x after1
    after idle set y after2
    after idle set z after3
    after cancel set y after2
    update idletasks
    concat $x $y $z
} {after1 before after3}
test timer-4.2 {Tcl_CancelIdleCall procedure} {
    foreach i [after info] {
	after cancel $i
    }
    set x before
    set y before
    set z before
    after idle set x after1
    after idle set y after2
    after idle set z after3
    after cancel set x after1
    update idletasks
    concat $x $y $z
} {before after2 after3}

test timer-5.1 {Tcl_ServiceIdle, self-rescheduling handlers} {
    foreach i [after info] {
	after cancel $i
    }
    set x 1
    set y 23
    after idle {incr x; after idle {incr x; after idle {incr x}}}
    after idle {incr y}
    vwait x
    set result "$x $y"
    update idletasks
    lappend result $x
} {2 24 4}

test timer-6.1 {Tcl_AfterCmd procedure, basics} {
    list [catch {after} msg] $msg
} {1 {wrong # args: should be "after option ?arg ...?"}}
test timer-6.2 {Tcl_AfterCmd procedure, basics} jim {
    list [catch {after 2x} msg] $msg
} {1 {bad argument "2x": must be cancel, idle, or info}}
test timer-6.3 {Tcl_AfterCmd procedure, basics} jim {
    list [catch {after gorp} msg] $msg
} {1 {bad argument "gorp": must be cancel, idle, or info}}
test timer-6.4 {Tcl_AfterCmd procedure, ms argument} {
    set x before
    after 500 {set x after}
    after 100
    update
    set y $x
    after 500
    update
    list $y $x
} {before after}

test timer-6.6 {Tcl_AfterCmd procedure, cancel option} {
    list [catch {after cancel} msg] $msg
} {1 {wrong # args: should be "after cancel id|command"}}
test timer-6.7 {Tcl_AfterCmd procedure, cancel option} {
    after cancel after#1
} {}
test timer-6.8 {Tcl_AfterCmd procedure, cancel option} {
    after cancel {foo bar}
} {}
test timer-6.9 {Tcl_AfterCmd procedure, cancel option} {
    foreach i [after info] {
	after cancel $i
    }
    set x before
    set y [after 20 set x after]
    after cancel $y
    after 40
    update
    set x
} {before}
test timer-6.10 {Tcl_AfterCmd procedure, cancel option} {
    foreach i [after info] {
	after cancel $i
    }
    set x before
    after 20 set x after
    after cancel set x after
    after 40
    update
    set x
} {before}
test timer-6.11 {Tcl_AfterCmd procedure, cancel option} {
    foreach i [after info] {
	after cancel $i
    }
    set x before
    after 20 set x after
    set id [after 60 set x after]
    after cancel $id
    after 40
    update
    set y $x
    set x cleared
    after 40
    update
    list $y $x
} {after cleared}
test timer-6.12 {Tcl_AfterCmd procedure, cancel option} {
    foreach i [after info] {
	after cancel $i
    }
    set x first
    after idle lappend x second
    after idle lappend x third
    set i [after idle lappend x fourth]
    after cancel {lappend x second}
    after cancel $i
    update idletasks
    set x
} {first third}
test timer-6.13 {Tcl_AfterCmd procedure, cancel option, multiple arguments for command} {
    foreach i [after info] {
	after cancel $i
    }
    set x first
    after idle lappend x second
    after idle lappend x third
    set i [after idle lappend x fourth]
    after cancel lappend x second
    after cancel $i
    update idletasks
    set x
} {first third}
test timer-6.14 {Tcl_AfterCmd procedure, cancel option, cancel during handler, used to dump core} {
    foreach i [after info] {
	after cancel $i
    }
    set id [
	after 20 {
	    set x done
	    after cancel $id
	}
    ]
    vwait x
} {}
test timer-6.16 {Tcl_AfterCmd procedure, idle option} {
    list [catch {after idle} msg] $msg
} {1 {wrong # args: should be "after idle script ?script ...?"}}
test timer-6.17 {Tcl_AfterCmd procedure, idle option} {
    set x before
    after idle {set x after}
    set y $x
    update idletasks
    list $y $x
} {before after}
test timer-6.18 {Tcl_AfterCmd procedure, idle option} {
    set x before
    after idle set x after
    set y $x
    update idletasks
    list $y $x
} {before after}

test timer-6.23 {Tcl_AfterCmd procedure, no option, script with NULL} {
    foreach i [after info] {
	after cancel $i
    }
    set x "hello world"
    after 1 "set x ab\0cd"
    after 10
    update
    string length $x
} {5}
test timer-6.24 {Tcl_AfterCmd procedure, no option, script with NULL} {
    foreach i [after info] {
	after cancel $i
    }
    set x "hello world"
    after 1 set x ab\0cd
    after 10
    update
    string length $x
} {5}
test timer-6.25 {Tcl_AfterCmd procedure, cancel option, script with NULL} {
    foreach i [after info] {
	after cancel $i
    }
    set x "hello world"
    after 1 set x ab\0cd
    after cancel "set x ab\0ef"
    set x [llength [after info]]
    foreach i [after info] {
	after cancel $i
    }
    set x
} {1}
test timer-6.26 {Tcl_AfterCmd procedure, cancel option, script with NULL} {
    foreach i [after info] {
	after cancel $i
    }
    set x "hello world"
    after 1 set x ab\0cd
    after cancel set x ab\0ef
    set y [llength [after info]]
    foreach i [after info] {
	after cancel $i
    }
    set y
} {1}
test timer-6.27 {Tcl_AfterCmd procedure, idle option, script with NULL} {
    foreach i [after info] {
	after cancel $i
    }
    set x "hello world"
    after idle "set x ab\0cd"
    update
    string length $x
} {5}
test timer-6.28 {Tcl_AfterCmd procedure, idle option, script with NULL} {
    foreach i [after info] {
	after cancel $i
    }
    set x "hello world"
    after idle set x ab\0cd
    update
    string length $x
} {5}
test timer-6.29 {Tcl_AfterCmd procedure, info option, script with NULL} {
    foreach i [after info] {
	after cancel $i
    }
    set x "hello world"
    set id junk
    set id [after 10 set x ab\0cd]
    update
    set y [string length [lindex [lindex [after info $id] 0] 2]]
    foreach i [after info] {
	after cancel $i
    }
    set y
} {5}

set event [after idle foo bar]
scan $event after#%d id

test timer-7.1 {GetAfterEvent procedure} {
    list [catch {after info xfter#$id} msg] $msg
} "1 {event \"xfter#$id\" doesn't exist}"
test timer-7.2 {GetAfterEvent procedure} {
    list [catch {after info afterx$id} msg] $msg
} "1 {event \"afterx$id\" doesn't exist}"
test timer-7.3 {GetAfterEvent procedure} {
    list [catch {after info after#ab} msg] $msg
} {1 {event "after#ab" doesn't exist}}
test timer-7.4 {GetAfterEvent procedure} {
    list [catch {after info after#} msg] $msg
} {1 {event "after#" doesn't exist}}
test timer-7.5 {GetAfterEvent procedure} {
    list [catch {after info after#${id}x} msg] $msg
} "1 {event \"after#${id}x\" doesn't exist}"
test timer-7.6 {GetAfterEvent procedure} {
    list [catch {after info afterx[expr $id+1]} msg] $msg
} "1 {event \"afterx[expr $id+1]\" doesn't exist}"
after cancel $event

test timer-8.1 {AfterProc procedure} {
    set x before
    proc foo {} {
	set x untouched
	after 20 {set x after}
	after 200
	update
	return $x
    }
    list [foo] $x
} {untouched after}
test timer-8.2 {AfterProc procedure} {
    catch {rename bgerror {}}
    proc bgerror msg {
	set ::x $msg
    }
    set x empty
    after 20 {error "After error"}
    after 200
    set y $x
    update
    catch {rename bgerror {}}
    list $y $x
} {empty {After error}}

test timer-8.4 {AfterProc procedure, deleting handler from itself} {
    foreach i [after info] {
	after cancel $i
    }
    proc foo {} {
	global x
	set x {}
	foreach i [after info] {
	    lappend x [after info $i]
	}
	after cancel foo
    }
    after 1000 {error "I shouldn't ever have executed"}
    after idle foo
    update idletasks
    set x
} {{{error "I shouldn't ever have executed"} timer}}

foreach i [after info] {
    after cancel $i
}

testreport