6 BEGIN { $| = 1; print "1..17\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;
22 import Time::HiRes 'gettimeofday' if $have_gettimeofday;
23 import Time::HiRes 'usleep' if $have_usleep;
24 import Time::HiRes 'ualarm' if $have_ualarm;
27 map { print "ok $_ (skipped)\n" } @_;
31 my ($n, $result, @info) = @_;
37 print "# @info\n" if @info;
41 if (!$have_gettimeofday) {
45 my @one = gettimeofday();
46 ok 2, @one == 2, 'gettimeofday returned ', 0+@one, ' args';
47 ok 3, $one[0] > 850_000_000, "@one too small";
51 my @two = gettimeofday();
52 ok 4, ($two[0] > $one[0] || ($two[0] == $one[0] && $two[1] > $one[1])),
53 "@two is not greater than @one";
55 my $f = Time::HiRes::time;
56 ok 5, $f > 850_000_000, "$f too small";
57 ok 6, $f - $two[0] < 2, "$f - @two >= 2";
69 ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
71 if (!$have_gettimeofday) {
75 my $f = Time::HiRes::time;
77 my $f2 = Time::HiRes::time;
79 ok 8, $d > 0.4 && $d < 0.8, "slept $d secs $f to $f2";
83 # Two-arg tv_interval() is always available.
85 my $f = tv_interval [5, 100_000], [10, 500_000];
89 if (!$have_gettimeofday) {
93 my $r = [gettimeofday()];
94 my $f = tv_interval $r;
102 my $r = [gettimeofday()];
103 #jTime::HiRes::sleep 0.5;
104 Time::HiRes::sleep( 0.5 );
105 my $f = tv_interval $r;
106 ok 11, $f > 0.4 && $f < 0.8, "slept $f secs";
114 local $SIG{ALRM} = sub { $tick++ };
116 my $one = time; $tick = 0; ualarm(10_000); sleep until $tick;
117 my $two = time; $tick = 0; ualarm(10_000); sleep until $tick;
119 ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
122 ualarm(10_000, 10_000);
123 sleep until $tick >= 3;
128 # new test: did we even get close?
132 my $tf = Time::HiRes::time();
133 ok 14, ($tf >= $t) && (($tf - $t) <= 1),
134 "time $t differs from Time::HiRes::time $tf";
137 unless (defined &Time::HiRes::gettimeofday
138 && defined &Time::HiRes::ualarm
139 && defined &Time::HiRes::usleep) {
141 print "ok $_ # skipped\n";
144 use Time::HiRes qw (time alarm sleep);
153 $r = [Time::HiRes::gettimeofday];
155 print Time::HiRes::tv_interval($r), "\nok 16\n";
157 $r = [Time::HiRes::gettimeofday];
163 select (undef, undef, undef, 10);
164 print "# Select returned! ", Time::HiRes::tv_interval ($r), "\n";
169 print "# Tick! ", Time::HiRes::tv_interval ($r), "\n";
172 $SIG{ALRM} = 'DEFAULT';