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