I don't think trying to bracket the hires time with lores
[p5sagit/p5-mst-13.2.git] / ext / Time / HiRes / HiRes.t
CommitLineData
dcf686c9 1BEGIN {
2 chdir 't' if -d 't';
3 @INC = '../lib';
4}
5
f7916ddb 6BEGIN { $| = 1; print "1..25\n"; }
dcf686c9 7
8END {print "not ok 1\n" unless $loaded;}
9
10use Time::HiRes qw(tv_interval);
11
12$loaded = 1;
13
14print "ok 1\n";
15
16use strict;
17
18my $have_gettimeofday = defined &Time::HiRes::gettimeofday;
19my $have_usleep = defined &Time::HiRes::usleep;
20my $have_ualarm = defined &Time::HiRes::ualarm;
c9ff6e92 21my $have_time = defined &Time::HiRes::time;
dcf686c9 22
23import Time::HiRes 'gettimeofday' if $have_gettimeofday;
24import Time::HiRes 'usleep' if $have_usleep;
25import Time::HiRes 'ualarm' if $have_ualarm;
26
3c72ec00 27use Config;
28
dcf686c9 29sub skip {
30 map { print "ok $_ (skipped)\n" } @_;
31}
32
33sub ok {
34 my ($n, $result, @info) = @_;
35 if ($result) {
36 print "ok $n\n";
37 }
38 else {
39 print "not ok $n\n";
40 print "# @info\n" if @info;
41 }
42}
43
44if (!$have_gettimeofday) {
45 skip 2..6;
46}
47else {
48 my @one = gettimeofday();
49 ok 2, @one == 2, 'gettimeofday returned ', 0+@one, ' args';
50 ok 3, $one[0] > 850_000_000, "@one too small";
51
52 sleep 1;
53
54 my @two = gettimeofday();
55 ok 4, ($two[0] > $one[0] || ($two[0] == $one[0] && $two[1] > $one[1])),
56 "@two is not greater than @one";
57
c9ff6e92 58 my $f = Time::HiRes::time();
dcf686c9 59 ok 5, $f > 850_000_000, "$f too small";
60 ok 6, $f - $two[0] < 2, "$f - @two >= 2";
61}
62
63if (!$have_usleep) {
64 skip 7..8;
65}
66else {
67 my $one = time;
68 usleep(10_000);
69 my $two = time;
70 usleep(10_000);
71 my $three = time;
72 ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
73
74 if (!$have_gettimeofday) {
75 skip 8;
76 }
77 else {
c9ff6e92 78 my $f = Time::HiRes::time();
dcf686c9 79 usleep(500_000);
c9ff6e92 80 my $f2 = Time::HiRes::time();
dcf686c9 81 my $d = $f2 - $f;
82 ok 8, $d > 0.4 && $d < 0.8, "slept $d secs $f to $f2";
83 }
84}
85
86# Two-arg tv_interval() is always available.
87{
88 my $f = tv_interval [5, 100_000], [10, 500_000];
6cf89afa 89 ok 9, abs($f - 5.4) < 0.001, $f;
dcf686c9 90}
91
92if (!$have_gettimeofday) {
93 skip 10;
94}
95else {
96 my $r = [gettimeofday()];
97 my $f = tv_interval $r;
98 ok 10, $f < 2, $f;
99}
100
c9ff6e92 101if (!$have_usleep || !$have_gettimeofday) {
dcf686c9 102 skip 11;
103}
104else {
105 my $r = [gettimeofday()];
106 #jTime::HiRes::sleep 0.5;
107 Time::HiRes::sleep( 0.5 );
108 my $f = tv_interval $r;
109 ok 11, $f > 0.4 && $f < 0.8, "slept $f secs";
110}
111
112if (!$have_ualarm) {
113 skip 12..13;
114}
115else {
116 my $tick = 0;
117 local $SIG{ALRM} = sub { $tick++ };
118
119 my $one = time; $tick = 0; ualarm(10_000); sleep until $tick;
120 my $two = time; $tick = 0; ualarm(10_000); sleep until $tick;
121 my $three = time;
122 ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
123
124 $tick = 0;
125 ualarm(10_000, 10_000);
126 sleep until $tick >= 3;
127 ok 13, 1;
128 ualarm(0);
129}
130
131# new test: did we even get close?
132
c9ff6e92 133if (!$have_time) {
134 skip 14
135} else {
456811e8 136 my ($s, $n);
137 for my $i (1 .. 100) {
138 $s += Time::HiRes::time() - time();
139 $n++;
64cc9c1c 140 }
456811e8 141 # $s should be, at worst, equal to $n
142 # (time() may be rounding down, up, or closest)
143 ok 14, abs($s) / $n <= 1.0, "Time::HiRes::time() not close to time()";
144 print "# s = $s, n = $n, s/n = ", $s/$n, "\n";
dcf686c9 145}
146
147unless (defined &Time::HiRes::gettimeofday
148 && defined &Time::HiRes::ualarm
149 && defined &Time::HiRes::usleep) {
150 for (15..17) {
151 print "ok $_ # skipped\n";
152 }
153} else {
154 use Time::HiRes qw (time alarm sleep);
155
156 my ($f, $r, $i);
157
dcf686c9 158 $f = time;
add58217 159 print "# time...$f\n";
160 print "ok 15\n";
dcf686c9 161
c9ff6e92 162 $r = [Time::HiRes::gettimeofday()];
dcf686c9 163 sleep (0.5);
add58217 164 print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
dcf686c9 165
c9ff6e92 166 $r = [Time::HiRes::gettimeofday()];
dcf686c9 167 $i = 5;
168 $SIG{ALRM} = "tick";
6ea166ef 169 while ($i > 0)
dcf686c9 170 {
0e172911 171 alarm(0.3);
dcf686c9 172 select (undef, undef, undef, 10);
3c72ec00 173 print "# Select returned! $i ", Time::HiRes::tv_interval ($r), "\n";
dcf686c9 174 }
175
176 sub tick
177 {
dcf686c9 178 $i--;
3c72ec00 179 print "# Tick! $i ", Time::HiRes::tv_interval ($r), "\n";
dcf686c9 180 }
d0b83b2b 181 alarm(0); # can't cancel usig %SIG
dcf686c9 182
183 print "ok 17\n";
184}
185
3c72ec00 186unless (defined &Time::HiRes::setitimer
187 && defined &Time::HiRes::getitimer
188 && exists &Time::HiRes::ITIMER_VIRTUAL
d0b83b2b 189 && $Config{d_select}
190 && $Config{sig_name} =~ m/\bVTALRM\b/) {
3c72ec00 191 for (18..19) {
0e172911 192 print "ok $_ # Skip: no virtual interval timers\n";
3c72ec00 193 }
194} else {
195 use Time::HiRes qw (setitimer getitimer ITIMER_VIRTUAL);
196
197 my $i = 3;
c9ff6e92 198 my $r = [Time::HiRes::gettimeofday()];
3c72ec00 199
200 $SIG{VTALRM} = sub {
201 $i ? $i-- : setitimer(ITIMER_VIRTUAL, 0);
202 print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
203 };
204
0e172911 205 print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
206
207 # Assume interval timer granularity of 0.05 seconds. Too bold?
208 print "not " unless abs(getitimer(ITIMER_VIRTUAL) / 0.5) - 1 < 0.1;
209 print "ok 18\n";
3c72ec00 210
211 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
212
1c41c9bf 213 while (getitimer(ITIMER_VIRTUAL)) {
214 my $j; $j++ for 1..1000; # Can't be unbreakable, must test getitimer().
3c72ec00 215 }
216
217 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
218
0e172911 219 print "not " unless getitimer(ITIMER_VIRTUAL) == 0;
220 print "ok 19\n";
221
3c72ec00 222 $SIG{VTALRM} = 'DEFAULT';
223}
224
9c735d59 225if ($have_gettimeofday) {
226 my ($t0, $td);
52d72fba 227
9c735d59 228 my $sleep = 1.5; # seconds
229 my $limit = 0.1; # 10% is acceptable slosh for timers
230 my $msg;
231
232 $t0 = gettimeofday();
233 $a = abs(sleep($sleep) / $sleep - 1.0);
234 $td = gettimeofday() - $t0;
235
236 $msg = "$td went by while sleeping $sleep, ratio $a\n";
237
238 if ($td < $sleep * (1 + $limit)) {
239 print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
240 } else {
241 print "ok 20 # Skip: $msg";
242 }
243
244 $t0 = gettimeofday();
245 $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
246 $td = gettimeofday() - $t0;
247
248 $msg = "$td went by while sleeping $sleep, ratio $a\n";
249
250 if ($td < $sleep * (1 + $limit)) {
251 print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
252 } else {
253 print "ok 21 # Skip: $msg";
254 }
255
256} else {
257 for (20..21) {
258 print "ok $_ # Skip: no gettimeofday\n";
259 }
260}
52d72fba 261
f7916ddb 262eval { sleep(-1) };
263print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
264 "ok 22\n" : "not ok 22\n";
265
266eval { usleep(-2) };
267print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
268 "ok 23\n" : "not ok 23\n";
269
ec77158c 270if ($have_ualarm) {
c46d5038 271 eval { alarm(-3) };
272 print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
273 "ok 24\n" : "not ok 24\n";
274
ec77158c 275 eval { ualarm(-4) };
276 print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
f7916ddb 277 "ok 25\n" : "not ok 25\n";
ec77158c 278} else {
c46d5038 279 skip 24;
ec77158c 280 skip 25;
281}