Re: Fw: Smoke 13285 /pro/3gl/CPAN/perl-current
[p5sagit/p5-mst-13.2.git] / ext / Time / HiRes / HiRes.t
1 BEGIN {
2     chdir 't' if -d 't';
3     @INC = '../lib';
4 }
5
6 BEGIN { $| = 1; print "1..25\n"; }
7
8 END {print "not ok 1\n" unless $loaded;}
9
10 use Time::HiRes qw(tv_interval);
11
12 $loaded = 1;
13
14 print "ok 1\n";
15
16 use strict;
17
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;
22
23 import Time::HiRes 'gettimeofday'       if $have_gettimeofday;
24 import Time::HiRes 'usleep'             if $have_usleep;
25 import Time::HiRes 'ualarm'             if $have_ualarm;
26
27 use Config;
28
29 # Ideally, we'd like to test that the timers are rather precise.
30 # However, if the system is busy, there are no guarantees on how
31 # quickly we will return.  This limit used to be 10%, but that
32 # was occasionally triggered falsely.  
33 # Try 20%.  
34 # Another possibility might be to print "ok" if the test completes fine
35 # with (say) 10% slosh, "skip - system may have been busy?" if the test
36 # completes fine with (say) 30% slosh, and fail otherwise.  If you do that,
37 # consider changing over to test.pl at the same time.
38 # --A.D., Nov 27, 2001
39 my $limit = 0.20; # 20% is acceptable slosh for testing timers
40
41 sub skip {
42     map { print "ok $_ (skipped)\n" } @_;
43 }
44
45 sub ok {
46     my ($n, $result, @info) = @_;
47     if ($result) {
48         print "ok $n\n";
49     }
50     else {
51         print "not ok $n\n";
52         print "# @info\n" if @info;
53     }
54 }
55
56 if (!$have_gettimeofday) {
57     skip 2..6;
58 }
59 else {
60     my @one = gettimeofday();
61     ok 2, @one == 2, 'gettimeofday returned ', 0+@one, ' args';
62     ok 3, $one[0] > 850_000_000, "@one too small";
63
64     sleep 1;
65
66     my @two = gettimeofday();
67     ok 4, ($two[0] > $one[0] || ($two[0] == $one[0] && $two[1] > $one[1])),
68             "@two is not greater than @one";
69
70     my $f = Time::HiRes::time();
71     ok 5, $f > 850_000_000, "$f too small";
72     ok 6, $f - $two[0] < 2, "$f - $two[0] >= 2";
73 }
74
75 if (!$have_usleep) {
76     skip 7..8;
77 }
78 else {
79     my $one = time;
80     usleep(10_000);
81     my $two = time;
82     usleep(10_000);
83     my $three = time;
84     ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
85
86     if (!$have_gettimeofday) {
87         skip 8;
88     }
89     else {
90         my $f = Time::HiRes::time();
91         usleep(500_000);
92         my $f2 = Time::HiRes::time();
93         my $d = $f2 - $f;
94         ok 8, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
95     }
96 }
97
98 # Two-arg tv_interval() is always available.
99 {
100     my $f = tv_interval [5, 100_000], [10, 500_000];
101     ok 9, abs($f - 5.4) < 0.001, $f;
102 }
103
104 if (!$have_gettimeofday) {
105     skip 10;
106 }
107 else {
108     my $r = [gettimeofday()];
109     my $f = tv_interval $r;
110     ok 10, $f < 2, $f;
111 }
112
113 if (!$have_usleep || !$have_gettimeofday) {
114     skip 11;
115 }
116 else {
117     my $r = [gettimeofday()];
118     #jTime::HiRes::sleep 0.5;
119     Time::HiRes::sleep( 0.5 );
120     my $f = tv_interval $r;
121     ok 11, $f > 0.4 && $f < 0.9, "slept $f instead of 0.5 secs.";
122 }
123
124 if (!$have_ualarm) {
125     skip 12..13;
126 }
127 else {
128     my $tick = 0;
129     local $SIG{ALRM} = sub { $tick++ };
130
131     my $one = time; $tick = 0; ualarm(10_000); sleep until $tick;
132     my $two = time; $tick = 0; ualarm(10_000); sleep until $tick;
133     my $three = time;
134     ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
135
136     $tick = 0;
137     ualarm(10_000, 10_000);
138     sleep until $tick >= 3;
139     ok 13, 1;
140     ualarm(0);
141 }
142
143 # new test: did we even get close?
144
145 if (!$have_time) {
146     skip 14
147 } else {
148  my ($s, $n);
149  for my $i (1 .. 100) {
150      $s += Time::HiRes::time() - time();
151      $n++;
152  }
153  # $s should be, at worst, equal to $n
154  # (time() may be rounding down, up, or closest)
155  ok 14, abs($s) / $n <= 1.0, "Time::HiRes::time() not close to time()";
156  print "# s = $s, n = $n, s/n = ", $s/$n, "\n";
157 }
158
159 unless (defined &Time::HiRes::gettimeofday
160         && defined &Time::HiRes::ualarm
161         && defined &Time::HiRes::usleep
162         && $Config{d_ualarm}) {
163     for (15..17) {
164         print "ok $_ # skipped\n";
165     }
166 } else {
167     use Time::HiRes qw (time alarm sleep);
168
169     my ($f, $r, $i, $not);
170
171     $f = time; 
172     print "# time...$f\n";
173     print "ok 15\n";
174
175     $r = [Time::HiRes::gettimeofday()];
176     sleep (0.5);
177     print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
178
179     $r = [Time::HiRes::gettimeofday()];
180     $i = 5;
181     $SIG{ALRM} = "tick";
182     while ($i > 0)
183     {
184         alarm(0.3);
185         select (undef, undef, undef, 3);
186         my $ival = Time::HiRes::tv_interval ($r);
187         print "# Select returned! $i $ival\n";
188         my $exp = 0.3 * (5 - $i);
189         # This test is more sensitive, so impose a softer limit.
190         if (abs($ival/$exp) - 1 > 3*$limit) {
191             my $ratio = abs($ival/$exp);
192             $not = "while: $exp sleep took $ival ratio $ratio";
193             last;
194         }
195     }
196
197     sub tick
198     {
199         $i--;
200         my $ival = Time::HiRes::tv_interval ($r);
201         print "# Tick! $i $ival\n";
202         my $exp = 0.3 * (5 - $i);
203         # This test is more sensitive, so impose a softer limit.
204         if (abs($ival/$exp) - 1 > 3*$limit) {
205             my $ratio = abs($ival/$exp);
206             $not = "tick: $exp sleep took $ival ratio $ratio";
207             $i = 0;
208         }
209     }
210
211     alarm(0); # can't cancel usig %SIG
212
213     print $not ? "not ok 17 # $not\n" : "ok 17\n";
214 }
215
216 unless (defined &Time::HiRes::setitimer
217         && defined &Time::HiRes::getitimer
218         && exists &Time::HiRes::ITIMER_VIRTUAL
219         && $Config{d_select}
220         && $Config{sig_name} =~ m/\bVTALRM\b/) {
221     for (18..19) {
222         print "ok $_ # Skip: no virtual interval timers\n";
223     }
224 } else {
225     use Time::HiRes qw (setitimer getitimer ITIMER_VIRTUAL);
226
227     my $i = 3;
228     my $r = [Time::HiRes::gettimeofday()];
229
230     $SIG{VTALRM} = sub {
231         $i ? $i-- : setitimer(ITIMER_VIRTUAL, 0);
232         print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
233     };  
234
235     print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
236
237     # Assume interval timer granularity of $limit * 0.5 seconds.  Too bold?
238     print "not " unless abs(getitimer(ITIMER_VIRTUAL) / 0.5) - 1 < $limit;
239     print "ok 18\n";
240
241     print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
242
243     while (getitimer(ITIMER_VIRTUAL)) {
244         my $j; $j++ for 1..1000; # Can't be unbreakable, must test getitimer().
245     }
246
247     print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
248
249     print "not " unless getitimer(ITIMER_VIRTUAL) == 0;
250     print "ok 19\n";
251
252     $SIG{VTALRM} = 'DEFAULT';
253 }
254
255 if ($have_gettimeofday) {
256     my ($t0, $td);
257
258     my $sleep = 1.5; # seconds
259     my $msg;
260
261     $t0 = gettimeofday();
262     $a = abs(sleep($sleep)        / $sleep         - 1.0);
263     $td = gettimeofday() - $t0;
264     my $ratio = 1.0 + $a;
265
266     $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
267
268     if ($td < $sleep * (1 + $limit)) {
269         print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
270     } else {
271         print "ok 20 # Skip: $msg";
272     }
273
274     $t0 = gettimeofday();
275     $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
276     $td = gettimeofday() - $t0;
277     $ratio = 1.0 + $a;
278
279     $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
280
281     if ($td < $sleep * (1 + $limit)) {
282         print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
283     } else {
284         print "ok 21 # Skip: $msg";
285     }
286
287 } else {
288     for (20..21) {
289         print "ok $_ # Skip: no gettimeofday\n";
290     }
291 }
292
293 eval { sleep(-1) };
294 print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
295     "ok 22\n" : "not ok 22\n";
296
297 eval { usleep(-2) };
298 print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
299     "ok 23\n" : "not ok 23\n";
300
301 if ($have_ualarm) {
302     eval { alarm(-3) };
303     print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
304         "ok 24\n" : "not ok 24\n";
305
306     eval { ualarm(-4) };
307     print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
308     "ok 25\n" : "not ok 25\n";
309 } else {
310     skip 24;
311     skip 25;
312 }