Integrate mainline
[p5sagit/p5-mst-13.2.git] / ext / POSIX / t / 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 require "./test.pl";
14 plan(tests => 38);
15
16
17 use POSIX qw(fcntl_h signal_h limits_h _exit getcwd open read strftime write
18              errno);
19 use strict 'subs';
20
21 $| = 1;
22
23 $Is_W32     = $^O eq 'MSWin32';
24 $Is_Dos     = $^O eq 'dos';
25 $Is_MPE     = $^O eq 'mpeix';
26 $Is_MacOS   = $^O eq 'MacOS';
27 $Is_VMS     = $^O eq 'VMS';
28 $Is_OS2     = $^O eq 'os2';
29 $Is_UWin    = $^O eq 'uwin';
30 $Is_OS390   = $^O eq 'os390';
31
32 ok( $testfd = open("TEST", O_RDONLY, 0),        'O_RDONLY with open' );
33 read($testfd, $buffer, 9) if $testfd > 2;
34 is( $buffer, "#!./perl\n",                      '    with read' );
35
36 write(1,"ok 3\nnot ok 3\n", 5);
37 next_test();
38
39 SKIP: {
40     skip("no pipe() support on DOS", 2) if $Is_Dos;
41
42     @fds = POSIX::pipe();
43     ok( $fds[0] > $testfd,      'POSIX::pipe' );
44
45     CORE::open($reader = \*READER, "<&=".$fds[0]);
46     CORE::open($writer = \*WRITER, ">&=".$fds[1]);
47     print $writer "ok 5\n";
48     close $writer;
49     print <$reader>;
50     close $reader;
51     next_test();
52 }
53
54 SKIP: {
55     skip("no sigaction support on win32/dos", 6) if $Is_W32 || $Is_Dos;
56
57     my $sigset = new POSIX::SigSet 1, 3;
58     $sigset->delset(1);
59     ok(! $sigset->ismember(1),  'POSIX::SigSet->delset' );
60     ok(  $sigset->ismember(3),  'POSIX::SigSet->ismember' );
61     
62     SKIP: {
63         skip("no kill() support on Mac OS", 4) if $Is_MacOS;
64
65        print "# warning, darwin seems to loose blocked signals (failing test 10)\n" if($^O eq 'darwin');
66         my $mask   = new POSIX::SigSet &SIGINT;
67         my $action = new POSIX::SigAction 'main::SigHUP', $mask, 0;
68         sigaction(&SIGHUP, $action);
69         $SIG{'INT'} = 'SigINT';
70         kill 'HUP', $$;
71         sleep 1;
72         print "ok 11 - signal masks successful\n";
73         
74         sub SigHUP {
75             print "ok 8 - sigaction SIGHUP\n";
76             kill 'INT', $$;
77             sleep 2;
78             print "ok 9 - sig mask delayed SIGINT\n";
79         }
80
81         sub SigINT {
82             print "ok 10 -   masked SIGINT received\n";
83         }
84
85         # The order of the above tests is very important, so
86         # we use literal prints and hard coded numbers.
87         next_test() for 1..4;
88     }
89 }
90
91 SKIP: {
92     skip("_POSIX_OPEN_MAX is inaccurate on MPE", 1) if $Is_MPE;
93     skip("_POSIX_OPEN_MAX undefined ($fds[1])",  1) unless &_POSIX_OPEN_MAX;
94
95     ok( &_POSIX_OPEN_MAX == 16 || &_POSIX_OPEN_MAX == 20, "The two allowed values according to susv2 and susv3" );
96
97 }
98
99 my $pat;
100 if ($Is_MacOS) {
101     $pat = qr/:t:$/;
102
103 elsif ( $Is_VMS ) {
104     $pat = qr/\.T]/i;
105 }
106 else {
107     $pat = qr#[\\/]t$#i;
108 }
109 like( getcwd(), qr/$pat/, 'getcwd' );
110
111 # Check string conversion functions.
112
113 SKIP: { 
114     skip("strtod() not present", 1) unless $Config{d_strtod};
115
116     $lc = &POSIX::setlocale(&POSIX::LC_NUMERIC, 'C') if $Config{d_setlocale};
117
118     # we're just checking that strtod works, not how accurate it is
119     ($n, $x) = &POSIX::strtod('3.14159_OR_SO');
120     ok((abs("3.14159" - $n) < 1e-6) && ($x == 6), 'strtod works');
121
122     &POSIX::setlocale(&POSIX::LC_NUMERIC, $lc) if $Config{d_setlocale};
123 }
124
125 SKIP: {
126     skip("strtol() not present", 2) unless $Config{d_strtol};
127
128     ($n, $x) = &POSIX::strtol('21_PENGUINS');
129     is($n, 21, 'strtol() number');
130     is($x, 9,  '         unparsed chars');
131 }
132
133 SKIP: {
134     skip("strtoul() not present", 2) unless $Config{d_strtoul};
135
136     ($n, $x) = &POSIX::strtoul('88_TEARS');
137     is($n, 88, 'strtoul() number');
138     is($x, 6,  '          unparsed chars');
139 }
140
141 # Pick up whether we're really able to dynamically load everything.
142 ok( &POSIX::acos(1.0) == 0.0,   'dynamic loading' );
143
144 # This can coredump if struct tm has a timezone field and we
145 # didn't detect it.  If this fails, try adding
146 # -DSTRUCT_TM_HASZONE to your cflags when compiling ext/POSIX/POSIX.c.
147 # See ext/POSIX/hints/sunos_4.pl and ext/POSIX/hints/linux.pl 
148 print POSIX::strftime("ok 20 # %H:%M, on %D\n", localtime());
149 next_test();
150
151 # If that worked, validate the mini_mktime() routine's normalisation of
152 # input fields to strftime().
153 sub try_strftime {
154     my $expect = shift;
155     my $got = POSIX::strftime("%a %b %d %H:%M:%S %Y %j", @_);
156     is($got, $expect, "validating mini_mktime() and strftime(): $expect");
157 }
158
159 $lc = &POSIX::setlocale(&POSIX::LC_TIME, 'C') if $Config{d_setlocale};
160 try_strftime("Wed Feb 28 00:00:00 1996 059", 0,0,0, 28,1,96);
161 try_strftime("Thu Feb 29 00:00:60 1996 060", 60,0,-24, 30,1,96);
162 try_strftime("Fri Mar 01 00:00:00 1996 061", 0,0,-24, 31,1,96);
163 try_strftime("Sun Feb 28 00:00:00 1999 059", 0,0,0, 28,1,99);
164 try_strftime("Mon Mar 01 00:00:00 1999 060", 0,0,24, 28,1,99);
165 try_strftime("Mon Feb 28 00:00:00 2000 059", 0,0,0, 28,1,100);
166 try_strftime("Tue Feb 29 00:00:00 2000 060", 0,0,0, 0,2,100);
167 try_strftime("Wed Mar 01 00:00:00 2000 061", 0,0,0, 1,2,100);
168 try_strftime("Fri Mar 31 00:00:00 2000 091", 0,0,0, 31,2,100);
169 &POSIX::setlocale(&POSIX::LC_TIME, $lc) if $Config{d_setlocale};
170
171 {
172     for my $test (0, 1) {
173         $! = 0;
174         # POSIX::errno is autoloaded. 
175         # Autoloading requires many system calls.
176         # errno() looks at $! to generate its result.
177         # Autoloading should not munge the value.
178         my $foo  = $!;
179         my $errno = POSIX::errno();
180
181         local $TODO;
182         $TODO = 'POSIX::errno() munged by autoloading on VMS' 
183             if $Is_VMS && $test == 0;
184
185         # Force numeric context.
186         is( $errno + 0, $foo + 0,     'autoloading and errno() mix' );
187     }
188 }
189
190 SKIP: {
191   skip("no kill() support on Mac OS", 1) if $Is_MacOS;
192   is (eval "kill 0", 0, "check we have CORE::kill")
193     or print "\$\@ is " . _qq($@) . "\n";
194 }
195
196 # Check that we can import the POSIX kill routine
197 POSIX->import ('kill');
198 my $result = eval "kill 0";
199 is ($result, undef, "we should now have POSIX::kill");
200 # Check usage.
201 like ($@, qr/^Usage: POSIX::kill\(pid, sig\)/, "check its usage message");
202
203 # Check unimplemented.
204 $result = eval {POSIX::offsetof};
205 is ($result, undef, "offsetof should fail");
206 like ($@, qr/^Unimplemented: POSIX::offsetof\(\) is C-specific/,
207       "check its unimplemented message");
208
209 # Check reimplemented.
210 $result = eval {POSIX::fgets};
211 is ($result, undef, "fgets should fail");
212 like ($@, qr/^Use method IO::Handle::gets\(\) instead/,
213       "check its redef message");
214
215 $| = 0;
216 # The following line assumes buffered output, which may be not true:
217 print '@#!*$@(!@#$' unless ($Is_MacOS || $Is_OS2 || $Is_UWin || $Is_OS390 ||
218                             $Is_VMS ||
219                             (defined $ENV{PERLIO} &&
220                              $ENV{PERLIO} eq 'unix' &&
221                              $Config::Config{useperlio}));
222 _exit(0);