/
timer.t
179 lines (150 loc) · 3.52 KB
/
timer.t
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
#! parrot
# Copyright (C) 2001-2012, Parrot Foundation.
=head1 NAME
t/pmc/timer.t - Timer PMC tests
=head1 SYNOPSIS
% prove t/pmc/timer.t
=head1 DESCRIPTION
Tests the Timer PMC.
=cut
.sub tick
get_global $P0, 'counter'
inc $P0
.end
.sub main :main
.include 'test_more.pir'
.include "timer.pasm"
plan(17)
timer_setup()
timer_initialize()
timer_start_stop()
timer_repeat()
timer_start()
timer_stop()
.end
.sub timer_stop
.local pmc timer
.local int t1, t2, r
.const 'Sub' tick = 'tick'
$P0 = new 'Integer'
set_global 'counter', $P0
timer = new ['Timer']
timer[.PARROT_TIMER_HANDLER] = tick
timer[.PARROT_TIMER_INTERVAL] = 0.1
timer[.PARROT_TIMER_REPEAT] = -1
timer[.PARROT_TIMER_RUNNING] = 1
# Allow at least two ticks
sleep 0.2
sleep 0.2
timer[.PARROT_TIMER_RUNNING] = 0
# Give a chance to run a possible pending tick
sleep 0.2
get_global $P0, 'counter'
t1 = $P0
r = isgt t1, 1
is(r,1,'counter = 1')
# Give a chance to tick to verify that is stopped
sleep 0.2
sleep 0.2
get_global $P0, 'counter'
t2 = $P0
r = iseq t1, t2
is(r,1,'counter = 1')
.end
.sub timer_start
new $P1, ['FixedPMCArray']
set $P1, 6
set $P1[0], .PARROT_TIMER_NSEC
set $P1[1], 0.5
set $P1[2], .PARROT_TIMER_HANDLER
get_global $P2, "_timer_sub2"
set $P1[3], $P2
set $P1[4], .PARROT_TIMER_RUNNING
set $P1[5], 1
new $P0, ['Timer'], $P1
ok(1,'timer start')
sleep 1
ok(1,'timer start')
.end
.sub timer_repeat
new $P1, ['FixedPMCArray']
set $P1, 8
set $P1[0], .PARROT_TIMER_NSEC
set $P1[1], 0.2
set $P1[2], .PARROT_TIMER_HANDLER
get_global $P2, "_timer_sub2"
set $P1[3], $P2
set $P1[4], .PARROT_TIMER_REPEAT
set $P1[5], 2
set $P1[6], .PARROT_TIMER_RUNNING
set $P1[7], 1
new $P0, ['Timer'], $P1
ok(1,'repeat 1')
sleep 1
sleep 1
sleep 1
sleep 1
ok(1,'repeat 3')
.end
.sub timer_setup
new $P0, ['Timer']
set $P0[.PARROT_TIMER_SEC], 7
set $I0, $P0[.PARROT_TIMER_SEC]
$I1 = iseq $I0, 7
ok($I1,'PARROT_TIMER_SEC')
set $I2, $P0[.PARROT_TIMER_USEC]
$I3 = iseq $I2, 0
ok($I3,'PARROT_TIMER_USEC')
set $I0, $P0[.PARROT_TIMER_RUNNING]
$I1 = eq $I0, 0
ok($I1,'PARROT_TIMER_RUNNING')
.end
.sub timer_initialize
new $P1, ['FixedPMCArray']
set $P1, 4
set $P1[0], .PARROT_TIMER_SEC
set $P1[1], 8
set $P1[2], .PARROT_TIMER_USEC
set $P1[3], 400000
new $P0, ['Timer'], $P1
set $I0, $P0[.PARROT_TIMER_SEC]
$I1 = iseq $I0, 8
ok($I1,'PARROT_TIMER_SEC')
set $I0, $P0[.PARROT_TIMER_USEC]
# I0 should be one of the following three
$I1 = iseq $I0, 400000
$I2 = iseq $I0, 400001
$I3 = iseq $I0, 399999
$I4 = $I1 + $I2
$I4 += $I3
# so we assert the sum of the above registers is 1
is($I4,1,'PARROT_TIMER_USEC')
.end
.sub timer_start_stop
$P1 = new ['FixedPMCArray'], 6
set $P1[0], .PARROT_TIMER_NSEC
set $P1[1], 0.5
set $P1[2], .PARROT_TIMER_HANDLER
get_global $P2, "_timer_sub"
set $P1[3], $P2
set $P1[4], .PARROT_TIMER_RUNNING
set $P1[5], 1
$P0 = new ['Timer'], $P1
ok(1,'created Timer PMC')
set $P0[.PARROT_TIMER_RUNNING], 0
sleep 1
ok(1,'slept after stopping timer')
.end
.sub _timer_sub
print "never\n"
returncc
.end
.sub _timer_sub2
ok(1,'timer_sub2')
returncc
.end
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: