Integrate change #11388 from macperl (the *.t parts needed hand massage).
[p5sagit/p5-mst-13.2.git] / ext / POSIX / POSIX.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6     require Config; import Config;
7     if ($^O ne 'VMS' and $Config{'extensions'} !~ /\bPOSIX\b/) {
8         print "1..0\n";
9         exit 0;
10     }
11 }
12
13 use POSIX qw(fcntl_h signal_h limits_h _exit getcwd open read strftime write
14              errno);
15 use strict subs;
16
17 $| = 1;
18 print "1..29\n";
19
20 $Is_W32 = $^O eq 'MSWin32';
21 $Is_NetWare = $^O eq 'NetWare';
22 $Is_Dos = $^O eq 'dos';
23 $Is_MPE = $^O eq 'mpeix';
24 $Is_MacOS = $^O eq 'MacOS';
25
26 $testfd = open("TEST", O_RDONLY, 0) and print "ok 1\n";
27 read($testfd, $buffer, 9) if $testfd > 2;
28 print $buffer eq "#!./perl\n" ? "ok 2\n" : "not ok 2\n";
29
30 write(1,"ok 3\nnot ok 3\n", 5);
31
32 if ($Is_Dos) {
33     for (4..5) {
34         print "ok $_ # skipped, no pipe() support on dos\n";
35     }
36 } else {
37 @fds = POSIX::pipe();
38 print $fds[0] > $testfd ? "ok 4\n" : "not ok 4\n";
39 CORE::open($reader = \*READER, "<&=".$fds[0]);
40 CORE::open($writer = \*WRITER, ">&=".$fds[1]);
41 print $writer "ok 5\n";
42 close $writer;
43 print <$reader>;
44 close $reader;
45 }
46
47 if ($Is_W32 || $Is_Dos) {
48     for (6..11) {
49         print "ok $_ # skipped, no sigaction support on win32/dos\n";
50     }
51 }
52 else {
53 $sigset = new POSIX::SigSet 1,3;
54 delset $sigset 1;
55 if (!ismember $sigset 1) { print "ok 6\n" }
56 if ( ismember $sigset 3) { print "ok 7\n" }
57
58 if ($Is_MacOS) {
59     for (8..11) {
60         print "ok $_ # skipped, no kill() support on Mac OS\n";
61     }
62 }
63 else {
64     $mask = new POSIX::SigSet &SIGINT;
65     $action = new POSIX::SigAction 'main::SigHUP', $mask, 0;
66     sigaction(&SIGHUP, $action);
67     $SIG{'INT'} = 'SigINT';
68     kill 'HUP', $$;
69     sleep 1;
70     print "ok 11\n";
71
72     sub SigHUP {
73         print "ok 8\n";
74         kill 'INT', $$;
75         sleep 2;
76         print "ok 9\n";
77     }
78
79     sub SigINT {
80         print "ok 10\n";
81     }
82 }
83
84 if ($Is_MPE) {
85     print "ok 12 # skipped, _POSIX_OPEN_MAX is inaccurate on MPE\n"
86 } else {
87     if (&_POSIX_OPEN_MAX) {
88         print &_POSIX_OPEN_MAX > $fds[1] ? "ok 12\n" : "not ok 12\n";
89     } else {
90         print "ok 12 # _POSIX_OPEN_MAX undefined ($fds[1])\n";
91     }
92 }
93
94 my $pat;
95 if ($Is_MacOS) {
96     $pat = qr/:t:$/;
97 } else {
98     $pat = qr#/t$#;
99 }
100 print getcwd() =~ $pat ? "ok 13\n" : "not ok 13\n";
101
102 # Check string conversion functions.
103
104 if ($Config{d_strtod}) {
105     $lc = &POSIX::setlocale(&POSIX::LC_NUMERIC, 'C') if $Config{d_setlocale};
106     ($n, $x) = &POSIX::strtod('3.14159_OR_SO');
107 # we're just checking that strtod works, not how accurate it is
108     print ((abs("3.14159" - $n) < 1e-6) && ($x == 6) ?
109           "ok 14\n" : "not ok 14\n");
110     &POSIX::setlocale(&POSIX::LC_NUMERIC, $lc) if $Config{d_setlocale};
111 } else { print "# strtod not present\n", "ok 14\n"; }
112
113 if ($Config{d_strtol}) {
114     ($n, $x) = &POSIX::strtol('21_PENGUINS');
115     print (($n == 21) && ($x == 9) ? "ok 15\n" : "not ok 15\n");
116 } else { print "# strtol not present\n", "ok 15\n"; }
117
118 if ($Config{d_strtoul}) {
119     ($n, $x) = &POSIX::strtoul('88_TEARS');
120     print (($n == 88) && ($x == 6) ? "ok 16\n" : "not ok 16\n");
121 } else { print "# strtoul not present\n", "ok 16\n"; }
122
123 # Pick up whether we're really able to dynamically load everything.
124 print &POSIX::acos(1.0) == 0.0 ? "ok 17\n" : "not ok 17\n";
125
126 # This can coredump if struct tm has a timezone field and we
127 # didn't detect it.  If this fails, try adding
128 # -DSTRUCT_TM_HASZONE to your cflags when compiling ext/POSIX/POSIX.c.
129 # See ext/POSIX/hints/sunos_4.pl and ext/POSIX/hints/linux.pl 
130 print POSIX::strftime("ok 18 # %H:%M, on %D\n", localtime());
131
132 # If that worked, validate the mini_mktime() routine's normalisation of
133 # input fields to strftime().
134 sub try_strftime {
135     my $num = shift;
136     my $expect = shift;
137     my $got = POSIX::strftime("%a %b %d %H:%M:%S %Y %j", @_);
138     if ($got eq $expect) {
139         print "ok $num\n";
140     }
141     else {
142         print "# expected: $expect\n# got: $got\nnot ok $num\n";
143     }
144 }
145
146 $lc = &POSIX::setlocale(&POSIX::LC_TIME, 'C') if $Config{d_setlocale};
147 try_strftime(19, "Wed Feb 28 00:00:00 1996 059", 0,0,0, 28,1,96);
148 try_strftime(20, "Thu Feb 29 00:00:60 1996 060", 60,0,-24, 30,1,96);
149 try_strftime(21, "Fri Mar 01 00:00:00 1996 061", 0,0,-24, 31,1,96);
150 try_strftime(22, "Sun Feb 28 00:00:00 1999 059", 0,0,0, 28,1,99);
151 try_strftime(23, "Mon Mar 01 00:00:00 1999 060", 0,0,24, 28,1,99);
152 try_strftime(24, "Mon Feb 28 00:00:00 2000 059", 0,0,0, 28,1,100);
153 try_strftime(25, "Tue Feb 29 00:00:00 2000 060", 0,0,0, 0,2,100);
154 try_strftime(26, "Wed Mar 01 00:00:00 2000 061", 0,0,0, 1,2,100);
155 try_strftime(27, "Fri Mar 31 00:00:00 2000 091", 0,0,0, 31,2,100);
156 &POSIX::setlocale(&POSIX::LC_TIME, $lc) if $Config{d_setlocale};
157
158 {
159     for my $test (0, 1) {
160         $! = 0;
161         # POSIX::errno is autoloaded. 
162         # Autoloading requires many system calls.
163         # errno() looks at $! to generate its result.
164         # Autoloading should not munge the value.
165         my $foo  = $!;
166         my $errno = POSIX::errno();
167         print "not " unless $errno == $foo;
168         print "ok ", 28 + $test, "\n";
169     }
170 }
171
172 $| = 0;
173 # The following line assumes buffered output, which may be not true with EMX:
174 print '@#!*$@(!@#$' unless ($Is_MacOS || $^O eq 'os2' || $^O eq 'uwin' || $^O eq 'os390');
175                             (defined $ENV{PERLIO} &&
176                              $ENV{PERLIO} eq 'unix' &&
177                              $Config::Config{useperlio}));
178 _exit(0);