6 BEGIN { $| = 1; print "1..25\n"; }
8 END {print "not ok 1\n" unless $loaded;}
10 use Time::HiRes qw(tv_interval);
18 my $have_gettimeofday = defined &Time::HiRes::gettimeofday;
19 my $have_usleep = defined &Time::HiRes::usleep;
20 my $have_ualarm = defined &Time::HiRes::ualarm;
21 my $have_time = defined &Time::HiRes::time;
23 import Time::HiRes 'gettimeofday' if $have_gettimeofday;
24 import Time::HiRes 'usleep' if $have_usleep;
25 import Time::HiRes 'ualarm' if $have_ualarm;
30 map { print "ok $_ (skipped)\n" } @_;
34 my ($n, $result, @info) = @_;
40 print "# @info\n" if @info;
44 if (!$have_gettimeofday) {
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";
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";
58 my $f = Time::HiRes::time();
59 ok 5, $f > 850_000_000, "$f too small";
60 ok 6, $f - $two[0] < 2, "$f - @two >= 2";
72 ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
74 if (!$have_gettimeofday) {
78 my $f = Time::HiRes::time();
80 my $f2 = Time::HiRes::time();
82 ok 8, $d > 0.4 && $d < 0.8, "slept $d secs $f to $f2";
86 # Two-arg tv_interval() is always available.
88 my $f = tv_interval [5, 100_000], [10, 500_000];
89 ok 9, abs($f - 5.4) < 0.001, $f;
92 if (!$have_gettimeofday) {
96 my $r = [gettimeofday()];
97 my $f = tv_interval $r;
101 if (!$have_usleep || !$have_gettimeofday) {
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";
117 local $SIG{ALRM} = sub { $tick++ };
119 my $one = time; $tick = 0; ualarm(10_000); sleep until $tick;
120 my $two = time; $tick = 0; ualarm(10_000); sleep until $tick;
122 ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
125 ualarm(10_000, 10_000);
126 sleep until $tick >= 3;
131 # new test: did we even get close?
137 for my $i (1 .. 100) {
138 $s += Time::HiRes::time() - time();
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";
147 unless (defined &Time::HiRes::gettimeofday
148 && defined &Time::HiRes::ualarm
149 && defined &Time::HiRes::usleep) {
151 print "ok $_ # skipped\n";
154 use Time::HiRes qw (time alarm sleep);
159 print "# time...$f\n";
162 $r = [Time::HiRes::gettimeofday()];
164 print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
166 $r = [Time::HiRes::gettimeofday()];
172 select (undef, undef, undef, 10);
173 print "# Select returned! $i ", Time::HiRes::tv_interval ($r), "\n";
179 print "# Tick! $i ", Time::HiRes::tv_interval ($r), "\n";
181 alarm(0); # can't cancel usig %SIG
186 unless (defined &Time::HiRes::setitimer
187 && defined &Time::HiRes::getitimer
188 && exists &Time::HiRes::ITIMER_VIRTUAL
190 && $Config{sig_name} =~ m/\bVTALRM\b/) {
192 print "ok $_ # Skip: no virtual interval timers\n";
195 use Time::HiRes qw (setitimer getitimer ITIMER_VIRTUAL);
198 my $r = [Time::HiRes::gettimeofday()];
201 $i ? $i-- : setitimer(ITIMER_VIRTUAL, 0);
202 print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
205 print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
207 # Assume interval timer granularity of 0.05 seconds. Too bold?
208 print "not " unless abs(getitimer(ITIMER_VIRTUAL) / 0.5) - 1 < 0.1;
211 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
213 while (getitimer(ITIMER_VIRTUAL)) {
214 my $j; $j++ for 1..1000; # Can't be unbreakable, must test getitimer().
217 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
219 print "not " unless getitimer(ITIMER_VIRTUAL) == 0;
222 $SIG{VTALRM} = 'DEFAULT';
225 if ($have_gettimeofday) {
228 my $sleep = 1.5; # seconds
229 my $limit = 0.1; # 10% is acceptable slosh for timers
232 $t0 = gettimeofday();
233 $a = abs(sleep($sleep) / $sleep - 1.0);
234 $td = gettimeofday() - $t0;
236 $msg = "$td went by while sleeping $sleep, ratio $a\n";
238 if ($td < $sleep * (1 + $limit)) {
239 print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
241 print "ok 20 # Skip: $msg";
244 $t0 = gettimeofday();
245 $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
246 $td = gettimeofday() - $t0;
248 $msg = "$td went by while sleeping $sleep, ratio $a\n";
250 if ($td < $sleep * (1 + $limit)) {
251 print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
253 print "ok 21 # Skip: $msg";
258 print "ok $_ # Skip: no gettimeofday\n";
263 print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
264 "ok 22\n" : "not ok 22\n";
267 print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
268 "ok 23\n" : "not ok 23\n";
272 print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
273 "ok 24\n" : "not ok 24\n";
276 print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
277 "ok 25\n" : "not ok 25\n";