This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
fix dist/Time-HiRes/t/*.t that assumed '.' in @INC
[perl5.git] / dist / Time-HiRes / t / stat.t
CommitLineData
90e44bf6
Z
1use strict;
2
3BEGIN {
4 require Time::HiRes;
5 unless(&Time::HiRes::d_hires_stat) {
6 require Test::More;
7 Test::More::plan(skip_all => "no hi-res stat");
8 }
9 if($^O =~ /\A(?:cygwin|MSWin)/) {
10 require Test::More;
11 Test::More::plan(skip_all =>
12 "$^O file timestamps not reliable enough for stat test");
13 }
14}
15
c4a535af 16use Test::More tests => 43;
465db51d 17BEGIN { push @INC, '.' }
90e44bf6
Z
18use t::Watchdog;
19
90e44bf6
Z
20my @atime;
21my @mtime;
22for (1..5) {
23 Time::HiRes::sleep(rand(0.1) + 0.1);
1ae6ead9 24 open(X, '>', $$);
90e44bf6
Z
25 print X $$;
26 close(X);
858dcda5
Z
27 my($a, $stat, $b) = ("a", [Time::HiRes::stat($$)], "b");
28 is $a, "a";
29 is $b, "b";
30 is ref($stat), "ARRAY";
31 push @mtime, $stat->[9];
0f0eae2c
Z
32 ($a, my $lstat, $b) = ("a", [Time::HiRes::lstat($$)], "b");
33 is $a, "a";
34 is $b, "b";
35 is_deeply $lstat, $stat;
90e44bf6 36 Time::HiRes::sleep(rand(0.1) + 0.1);
1ae6ead9 37 open(X, '<', $$);
90e44bf6
Z
38 <X>;
39 close(X);
858dcda5
Z
40 $stat = [Time::HiRes::stat($$)];
41 push @atime, $stat->[8];
0f0eae2c
Z
42 $lstat = [Time::HiRes::lstat($$)];
43 is_deeply $lstat, $stat;
90e44bf6
Z
44}
451 while unlink $$;
c4a535af
SH
46print("# mtime = @mtime\n");
47print("# atime = @atime\n");
90e44bf6
Z
48my $ai = 0;
49my $mi = 0;
50my $ss = 0;
51for (my $i = 1; $i < @atime; $i++) {
52 if ($atime[$i] >= $atime[$i-1]) {
53 $ai++;
54 }
55 if ($atime[$i] > int($atime[$i])) {
56 $ss++;
57 }
58}
59for (my $i = 1; $i < @mtime; $i++) {
60 if ($mtime[$i] >= $mtime[$i-1]) {
61 $mi++;
62 }
63 if ($mtime[$i] > int($mtime[$i])) {
64 $ss++;
65 }
66}
c4a535af 67print("# ai = $ai, mi = $mi, ss = $ss\n");
90e44bf6
Z
68# Need at least 75% of monotonical increase and
69# 20% of subsecond results. Yes, this is guessing.
70SKIP: {
71 skip "no subsecond timestamps detected", 1 if $ss == 0;
72 ok $mi/(@mtime-1) >= 0.75 && $ai/(@atime-1) >= 0.75 &&
73 $ss/(@mtime+@atime) >= 0.2;
74}
75
0f0eae2c
Z
76my $targetname = "tgt$$";
77my $linkname = "link$$";
78SKIP: {
1ae6ead9 79 open(X, '>', $targetname);
0f0eae2c
Z
80 print X $$;
81 close(X);
82 eval { symlink $targetname, $linkname or die "can't symlink: $!"; };
83 skip "can't symlink", 7 if $@ ne "";
84 my @tgt_stat = Time::HiRes::stat($targetname);
85 my @tgt_lstat = Time::HiRes::lstat($targetname);
86 my @lnk_stat = Time::HiRes::stat($linkname);
87 my @lnk_lstat = Time::HiRes::lstat($linkname);
88 is scalar(@tgt_stat), 13;
89 is scalar(@tgt_lstat), 13;
90 is scalar(@lnk_stat), 13;
91 is scalar(@lnk_lstat), 13;
92 is_deeply \@tgt_stat, \@tgt_lstat;
93 is_deeply \@tgt_stat, \@lnk_stat;
94 isnt $lnk_lstat[2], $tgt_stat[2];
95}
961 while unlink $linkname;
971 while unlink $targetname;
98
90e44bf6 991;