MPE/iX test tweaks from Mark Bixby.
[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
3c72ec00 6BEGIN { $| = 1; print "1..19\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];
89 ok 9, $f == 5.4, $f;
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 {
dcf686c9 136 my $t = time();
137 my $tf = Time::HiRes::time();
6119b5d3 138 ok 14, (abs($tf - $t) <= 1),
dcf686c9 139 "time $t differs from Time::HiRes::time $tf";
140}
141
142unless (defined &Time::HiRes::gettimeofday
143 && defined &Time::HiRes::ualarm
144 && defined &Time::HiRes::usleep) {
145 for (15..17) {
146 print "ok $_ # skipped\n";
147 }
148} else {
149 use Time::HiRes qw (time alarm sleep);
150
151 my ($f, $r, $i);
152
153 print "# time...";
154 $f = time;
155 print "$f\nok 15\n";
156
157 print "# sleep...";
c9ff6e92 158 $r = [Time::HiRes::gettimeofday()];
dcf686c9 159 sleep (0.5);
160 print Time::HiRes::tv_interval($r), "\nok 16\n";
161
c9ff6e92 162 $r = [Time::HiRes::gettimeofday()];
dcf686c9 163 $i = 5;
164 $SIG{ALRM} = "tick";
165 while ($i)
166 {
0e172911 167 alarm(0.3);
dcf686c9 168 select (undef, undef, undef, 10);
3c72ec00 169 print "# Select returned! $i ", Time::HiRes::tv_interval ($r), "\n";
dcf686c9 170 }
171
172 sub tick
173 {
dcf686c9 174 $i--;
3c72ec00 175 print "# Tick! $i ", Time::HiRes::tv_interval ($r), "\n";
dcf686c9 176 }
177 $SIG{ALRM} = 'DEFAULT';
178
179 print "ok 17\n";
180}
181
3c72ec00 182unless (defined &Time::HiRes::setitimer
183 && defined &Time::HiRes::getitimer
184 && exists &Time::HiRes::ITIMER_VIRTUAL
185 && $Config{d_select}) {
186 for (18..19) {
0e172911 187 print "ok $_ # Skip: no virtual interval timers\n";
3c72ec00 188 }
189} else {
190 use Time::HiRes qw (setitimer getitimer ITIMER_VIRTUAL);
191
192 my $i = 3;
c9ff6e92 193 my $r = [Time::HiRes::gettimeofday()];
3c72ec00 194
195 $SIG{VTALRM} = sub {
196 $i ? $i-- : setitimer(ITIMER_VIRTUAL, 0);
197 print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
198 };
199
0e172911 200 print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
201
202 # Assume interval timer granularity of 0.05 seconds. Too bold?
203 print "not " unless abs(getitimer(ITIMER_VIRTUAL) / 0.5) - 1 < 0.1;
204 print "ok 18\n";
3c72ec00 205
206 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
207
1c41c9bf 208 while (getitimer(ITIMER_VIRTUAL)) {
209 my $j; $j++ for 1..1000; # Can't be unbreakable, must test getitimer().
3c72ec00 210 }
211
212 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
213
0e172911 214 print "not " unless getitimer(ITIMER_VIRTUAL) == 0;
215 print "ok 19\n";
216
3c72ec00 217 $SIG{VTALRM} = 'DEFAULT';
218}
219