xref: /openbsd/gnu/usr.bin/perl/dist/Time-HiRes/t/stat.t (revision 3bef86f7)
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
16use Test::More tests => 43;
17BEGIN { push @INC, '.' }
18use t::Watchdog;
19
20my @atime;
21my @mtime;
22for (1..5) {
23    note "cycle $_";
24    Time::HiRes::sleep(rand(0.1) + 0.1);
25    open(X, '>', $$);
26    print X $$;
27    close(X);
28    my($a, $stat, $b) = ("a", [Time::HiRes::stat($$)], "b");
29    is $a, "a", "stat stack discipline";
30    is $b, "b", "stat stack discipline";
31    is ref($stat), "ARRAY", "stat returned array";
32    push @mtime, $stat->[9];
33    ($a, my $lstat, $b) = ("a", [Time::HiRes::lstat($$)], "b");
34    is $a, "a", "lstat stack discipline";
35    is $b, "b", "lstat stack discipline";
36    SKIP: {
37        if($^O eq "haiku") {
38            skip "testing stat access time on Haiku", 2;
39        }
40        is_deeply $lstat, $stat, "write: stat and lstat returned same values";
41        Time::HiRes::sleep(rand(0.1) + 0.1);
42        open(X, '<', $$);
43        <X>;
44        close(X);
45        $stat = [Time::HiRes::stat($$)];
46        push @atime, $stat->[8];
47        $lstat = [Time::HiRes::lstat($$)];
48        is_deeply $lstat, $stat, "read:  stat and lstat returned same values";
49    }
50}
511 while unlink $$;
52note ("mtime = @mtime");
53note ("atime = @atime");
54my $ai = 0;
55my $mi = 0;
56my $ss = 0;
57for (my $i = 1; $i < @atime; $i++) {
58    if ($atime[$i] >= $atime[$i-1]) {
59        $ai++;
60    }
61    if ($atime[$i] > int($atime[$i])) {
62        $ss++;
63    }
64}
65for (my $i = 1; $i < @mtime; $i++) {
66    if ($mtime[$i] >= $mtime[$i-1]) {
67        $mi++;
68    }
69    if ($mtime[$i] > int($mtime[$i])) {
70        $ss++;
71    }
72}
73note ("ai = $ai, mi = $mi, ss = $ss");
74# Need at least 75% of monotonical increase and
75# 20% of subsecond results. Yes, this is guessing.
76SKIP: {
77    skip "no subsecond timestamps detected", 1 if $ss == 0;
78    skip "testing stat access on Haiku", 1 if $^O eq "haiku";
79    ok $mi/(@mtime-1) >= 0.75 && $ai/(@atime-1) >= 0.75 &&
80             $ss/(@mtime+@atime) >= 0.2,
81        "monotonical increase and subsecond results within expected parameters";
82}
83
84my $targetname = "tgt$$";
85my $linkname = "link$$";
86SKIP: {
87    open(X, '>', $targetname);
88    print X $$;
89    close(X);
90    eval { symlink $targetname, $linkname or die "can't symlink: $!"; };
91    skip "can't symlink", 7 if $@ ne "";
92    note "compare Time::HiRes::stat with ::lstat";
93    my @tgt_stat = Time::HiRes::stat($targetname);
94    my @tgt_lstat = Time::HiRes::lstat($targetname);
95    my @lnk_stat = Time::HiRes::stat($linkname);
96    my @lnk_lstat = Time::HiRes::lstat($linkname);
97    my $exp = 13;
98    is scalar(@tgt_stat), $exp,  "stat on target";
99    is scalar(@tgt_lstat), $exp, "lstat on target";
100    is scalar(@lnk_stat), $exp,  "stat on link";
101    is scalar(@lnk_lstat), $exp, "lstat on link";
102    skip "testing stat access on Haiku", 3 if $^O eq "haiku";
103    is_deeply \@tgt_stat, \@tgt_lstat, "stat and lstat return same values on target";
104    is_deeply \@tgt_stat, \@lnk_stat,  "stat and lstat return same values on link";
105    isnt $lnk_lstat[2], $tgt_stat[2],
106        "target stat mode value differs from link lstat mode value";
107}
1081 while unlink $linkname;
1091 while unlink $targetname;
110
1111;
112