This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Un-TODO a test on Snow Leopard
[perl5.git] / ext / POSIX / t / posix.t
CommitLineData
a0d0e21e
LW
1#!./perl
2
3BEGIN {
a0d0e21e 4 require Config; import Config;
fa6b8193 5 if ($^O ne 'VMS' and $Config{'extensions'} !~ /\bPOSIX\b/) {
c764b42b 6 print "1..0\n";
a0d0e21e
LW
7 exit 0;
8 }
9}
c07a80fd 10
849ddec1 11use Test::More tests => 66;
e6c299c8 12
212caf55
TS
13use POSIX qw(fcntl_h signal_h limits_h _exit getcwd open read strftime write
14 errno);
e6c299c8 15use strict 'subs';
a0d0e21e 16
849ddec1
NC
17sub next_test {
18 my $builder = Test::More->builder;
19 $builder->current_test($builder->current_test() + 1);
20}
21
c07a80fd 22$| = 1;
a0d0e21e 23
e6c299c8
JH
24$Is_W32 = $^O eq 'MSWin32';
25$Is_Dos = $^O eq 'dos';
26$Is_MPE = $^O eq 'mpeix';
27$Is_MacOS = $^O eq 'MacOS';
28$Is_VMS = $^O eq 'VMS';
29$Is_OS2 = $^O eq 'os2';
30$Is_UWin = $^O eq 'uwin';
31$Is_OS390 = $^O eq 'os390';
6dead956 32
6a164b5b
JM
33my $vms_unix_rpt = 0;
34my $vms_efs = 0;
35my $unix_mode = 1;
36
37if ($Is_VMS) {
38 $unix_mode = 0;
39 if (eval 'require VMS::Feature') {
40 $vms_unix_rpt = VMS::Feature::current("filename_unix_report");
41 $vms_efs = VMS::Feature::current("efs_charset");
42 } else {
43 my $unix_rpt = $ENV{'DECC$FILENAME_UNIX_REPORT'} || '';
44 my $efs_charset = $ENV{'DECC$EFS_CHARSET'} || '';
45 $vms_unix_rpt = $unix_rpt =~ /^[ET1]/i;
46 $vms_efs = $efs_charset =~ /^[ET1]/i;
47 }
48
49 # Traditional VMS mode only if VMS is not in UNIX compatible mode.
50 $unix_mode = ($vms_efs && $vms_unix_rpt);
51
52}
53
54
2adbc9b6 55ok( $testfd = open("Makefile.PL", O_RDONLY, 0), 'O_RDONLY with open' );
7814eec4 56read($testfd, $buffer, 4) if $testfd > 2;
2adbc9b6 57is( $buffer, "# Ex", ' with read' );
c07a80fd 58
7814eec4
PM
59TODO:
60{
61 local $TODO = "read to array element not working";
62
63 read($testfd, $buffer[1], 5) if $testfd > 2;
64 is( $buffer[1], "perl\n", ' read to array element' );
65}
66
67write(1,"ok 4\nnot ok 4\n", 5);
e6c299c8
JH
68next_test();
69
70SKIP: {
71 skip("no pipe() support on DOS", 2) if $Is_Dos;
a0d0e21e 72
10de532f 73 @fds = POSIX::pipe();
e6c299c8
JH
74 ok( $fds[0] > $testfd, 'POSIX::pipe' );
75
10de532f
JH
76 CORE::open($reader = \*READER, "<&=".$fds[0]);
77 CORE::open($writer = \*WRITER, ">&=".$fds[1]);
7814eec4 78 print $writer "ok 6\n";
10de532f
JH
79 close $writer;
80 print <$reader>;
81 close $reader;
e6c299c8 82 next_test();
6bbf1b34 83}
a0d0e21e 84
e6c299c8
JH
85SKIP: {
86 skip("no sigaction support on win32/dos", 6) if $Is_W32 || $Is_Dos;
87
88 my $sigset = new POSIX::SigSet 1, 3;
89 $sigset->delset(1);
90 ok(! $sigset->ismember(1), 'POSIX::SigSet->delset' );
91 ok( $sigset->ismember(3), 'POSIX::SigSet->ismember' );
be4e88b6 92
e6c299c8
JH
93 SKIP: {
94 skip("no kill() support on Mac OS", 4) if $Is_MacOS;
95
be4e88b6
MS
96 my $sigint_called = 0;
97
e6c299c8
JH
98 my $mask = new POSIX::SigSet &SIGINT;
99 my $action = new POSIX::SigAction 'main::SigHUP', $mask, 0;
10de532f
JH
100 sigaction(&SIGHUP, $action);
101 $SIG{'INT'} = 'SigINT';
7eb03357
AB
102
103 # At least OpenBSD/i386 3.3 is okay, as is NetBSD 1.5.
104 # But not NetBSD 1.6 & 1.6.1: the test makes perl crash.
105 # So the kill() must not be done with this config in order to
106 # finish the test.
107 # For others (darwin & freebsd), let the test fail without crashing.
108 my $todo = $^O eq 'netbsd' && $Config{osvers}=~/^1\.6/;
e6b15316 109 my $why_todo = "# TODO $^O $Config{osvers} seems to lose blocked signals";
22f20764
AB
110 if (!$todo) {
111 kill 'HUP', $$;
112 } else {
113 print "not ok 9 - sigaction SIGHUP ",$why_todo,"\n";
114 print "not ok 10 - sig mask delayed SIGINT ",$why_todo,"\n";
115 }
10de532f 116 sleep 1;
be4e88b6 117
7eb03357 118 $todo = 1 if ($^O eq 'freebsd')
78385c2e 119 || ($^O eq 'darwin' && $Config{osvers} < '6.6');
22f20764 120 printf "%s 11 - masked SIGINT received %s\n",
7eb03357 121 $sigint_called ? "ok" : "not ok",
22f20764 122 $todo ? $why_todo : '';
be4e88b6 123
7814eec4 124 print "ok 12 - signal masks successful\n";
10de532f
JH
125
126 sub SigHUP {
7814eec4 127 print "ok 9 - sigaction SIGHUP\n";
10de532f
JH
128 kill 'INT', $$;
129 sleep 2;
7814eec4 130 print "ok 10 - sig mask delayed SIGINT\n";
10de532f
JH
131 }
132
133 sub SigINT {
be4e88b6 134 $sigint_called++;
10de532f 135 }
e6c299c8
JH
136
137 # The order of the above tests is very important, so
138 # we use literal prints and hard coded numbers.
139 next_test() for 1..4;
d536870a 140 }
6dead956 141}
a0d0e21e 142
e6c299c8
JH
143SKIP: {
144 skip("_POSIX_OPEN_MAX is inaccurate on MPE", 1) if $Is_MPE;
145 skip("_POSIX_OPEN_MAX undefined ($fds[1])", 1) unless &_POSIX_OPEN_MAX;
146
e85e3e79 147 ok( &_POSIX_OPEN_MAX >= 16, "The minimum allowed values according to susv2" );
4e0f6e8c 148
c9ff6e92 149}
a0d0e21e 150
d536870a 151my $pat;
2adbc9b6
NC
152if ( $unix_mode ) {
153 $pat = qr#[\\/]POSIX$#i;
e6c299c8
JH
154}
155else {
2adbc9b6 156 $pat = qr/\.POSIX]/i;
d536870a 157}
e6c299c8 158like( getcwd(), qr/$pat/, 'getcwd' );
a0d0e21e 159
a89d8a78
DH
160# Check string conversion functions.
161
e6c299c8
JH
162SKIP: {
163 skip("strtod() not present", 1) unless $Config{d_strtod};
164
ff68c719 165 $lc = &POSIX::setlocale(&POSIX::LC_NUMERIC, 'C') if $Config{d_setlocale};
e6c299c8
JH
166
167 # we're just checking that strtod works, not how accurate it is
a89d8a78 168 ($n, $x) = &POSIX::strtod('3.14159_OR_SO');
e6c299c8
JH
169 ok((abs("3.14159" - $n) < 1e-6) && ($x == 6), 'strtod works');
170
ff68c719 171 &POSIX::setlocale(&POSIX::LC_NUMERIC, $lc) if $Config{d_setlocale};
e6c299c8
JH
172}
173
174SKIP: {
175 skip("strtol() not present", 2) unless $Config{d_strtol};
a89d8a78 176
a89d8a78 177 ($n, $x) = &POSIX::strtol('21_PENGUINS');
e6c299c8
JH
178 is($n, 21, 'strtol() number');
179 is($x, 9, ' unparsed chars');
180}
181
182SKIP: {
183 skip("strtoul() not present", 2) unless $Config{d_strtoul};
a89d8a78 184
a89d8a78 185 ($n, $x) = &POSIX::strtoul('88_TEARS');
e6c299c8
JH
186 is($n, 88, 'strtoul() number');
187 is($x, 6, ' unparsed chars');
188}
a89d8a78 189
a0d0e21e 190# Pick up whether we're really able to dynamically load everything.
e6c299c8 191ok( &POSIX::acos(1.0) == 0.0, 'dynamic loading' );
a0d0e21e 192
84ef74c4
AD
193# This can coredump if struct tm has a timezone field and we
194# didn't detect it. If this fails, try adding
195# -DSTRUCT_TM_HASZONE to your cflags when compiling ext/POSIX/POSIX.c.
196# See ext/POSIX/hints/sunos_4.pl and ext/POSIX/hints/linux.pl
abbe0d1a 197print POSIX::strftime("ok 21 # %H:%M, on %m/%d/%y\n", localtime());
e6c299c8 198next_test();
84ef74c4 199
33c0e3ec
SB
200# If that worked, validate the mini_mktime() routine's normalisation of
201# input fields to strftime().
202sub try_strftime {
33c0e3ec
SB
203 my $expect = shift;
204 my $got = POSIX::strftime("%a %b %d %H:%M:%S %Y %j", @_);
61a515a6 205 is($got, $expect, "validating mini_mktime() and strftime(): $expect");
33c0e3ec
SB
206}
207
208$lc = &POSIX::setlocale(&POSIX::LC_TIME, 'C') if $Config{d_setlocale};
e6c299c8 209try_strftime("Wed Feb 28 00:00:00 1996 059", 0,0,0, 28,1,96);
53059177 210SKIP: {
abbe0d1a 211 skip("VC++ 8 and Vista's CRTs regard 60 seconds as an invalid parameter", 1)
6fa15125 212 if ($Is_W32 and (($Config{cc} eq 'cl' and
abbe0d1a 213 $Config{ccversion} =~ /^(\d+)/ and $1 >= 14) or
6fa15125 214 (Win32::GetOSVersion())[1] >= 6));
53059177
SH
215
216 try_strftime("Thu Feb 29 00:00:60 1996 060", 60,0,-24, 30,1,96);
217}
e6c299c8
JH
218try_strftime("Fri Mar 01 00:00:00 1996 061", 0,0,-24, 31,1,96);
219try_strftime("Sun Feb 28 00:00:00 1999 059", 0,0,0, 28,1,99);
220try_strftime("Mon Mar 01 00:00:00 1999 060", 0,0,24, 28,1,99);
221try_strftime("Mon Feb 28 00:00:00 2000 059", 0,0,0, 28,1,100);
222try_strftime("Tue Feb 29 00:00:00 2000 060", 0,0,0, 0,2,100);
223try_strftime("Wed Mar 01 00:00:00 2000 061", 0,0,0, 1,2,100);
224try_strftime("Fri Mar 31 00:00:00 2000 091", 0,0,0, 31,2,100);
33c0e3ec
SB
225&POSIX::setlocale(&POSIX::LC_TIME, $lc) if $Config{d_setlocale};
226
212caf55
TS
227{
228 for my $test (0, 1) {
229 $! = 0;
230 # POSIX::errno is autoloaded.
231 # Autoloading requires many system calls.
232 # errno() looks at $! to generate its result.
233 # Autoloading should not munge the value.
234 my $foo = $!;
235 my $errno = POSIX::errno();
e6c299c8 236
e6c299c8
JH
237 # Force numeric context.
238 is( $errno + 0, $foo + 0, 'autoloading and errno() mix' );
212caf55
TS
239 }
240}
241
d4742b2c
NC
242SKIP: {
243 skip("no kill() support on Mac OS", 1) if $Is_MacOS;
244 is (eval "kill 0", 0, "check we have CORE::kill")
245 or print "\$\@ is " . _qq($@) . "\n";
246}
247
248# Check that we can import the POSIX kill routine
249POSIX->import ('kill');
250my $result = eval "kill 0";
251is ($result, undef, "we should now have POSIX::kill");
252# Check usage.
253like ($@, qr/^Usage: POSIX::kill\(pid, sig\)/, "check its usage message");
254
255# Check unimplemented.
256$result = eval {POSIX::offsetof};
257is ($result, undef, "offsetof should fail");
258like ($@, qr/^Unimplemented: POSIX::offsetof\(\) is C-specific/,
259 "check its unimplemented message");
260
261# Check reimplemented.
262$result = eval {POSIX::fgets};
263is ($result, undef, "fgets should fail");
264like ($@, qr/^Use method IO::Handle::gets\(\) instead/,
265 "check its redef message");
266
4b3c6531
RGS
267# Simplistic tests for the isXXX() functions (bug #16799)
268ok( POSIX::isalnum('1'), 'isalnum' );
269ok(!POSIX::isalnum('*'), 'isalnum' );
270ok( POSIX::isalpha('f'), 'isalpha' );
271ok(!POSIX::isalpha('7'), 'isalpha' );
272ok( POSIX::iscntrl("\cA"),'iscntrl' );
273ok(!POSIX::iscntrl("A"), 'iscntrl' );
274ok( POSIX::isdigit('1'), 'isdigit' );
275ok(!POSIX::isdigit('z'), 'isdigit' );
276ok( POSIX::isgraph('@'), 'isgraph' );
277ok(!POSIX::isgraph(' '), 'isgraph' );
278ok( POSIX::islower('l'), 'islower' );
279ok(!POSIX::islower('L'), 'islower' );
280ok( POSIX::isupper('U'), 'isupper' );
281ok(!POSIX::isupper('u'), 'isupper' );
282ok( POSIX::isprint('$'), 'isprint' );
283ok(!POSIX::isprint("\n"), 'isprint' );
284ok( POSIX::ispunct('%'), 'ispunct' );
285ok(!POSIX::ispunct('u'), 'ispunct' );
286ok( POSIX::isspace("\t"), 'isspace' );
287ok(!POSIX::isspace('_'), 'isspace' );
288ok( POSIX::isxdigit('f'), 'isxdigit' );
289ok(!POSIX::isxdigit('g'), 'isxdigit' );
117206bb
RGS
290# metaphysical question : what should be returned for an empty string ?
291# anyway this shouldn't segfault (bug #24554)
292ok( POSIX::isalnum(''), 'isalnum empty string' );
293ok( POSIX::isalnum(undef),'isalnum undef' );
767bb2e0
ST
294# those functions should stringify their arguments
295ok(!POSIX::isalpha([]), 'isalpha []' );
296ok( POSIX::isprint([]), 'isprint []' );
2ae48df0
RGS
297
298eval { use strict; POSIX->import("S_ISBLK"); my $x = S_ISBLK };
299unlike( $@, qr/Can't use string .* as a symbol ref/, "Can import autoloaded constants" );
4b3c6531 300
404d038e
PG
301# Check that output is not flushed by _exit. This test should be last
302# in the file, and is not counted in the total number of tests.
303if ($^O eq 'vos') {
304 print "# TODO - hit VOS bug posix-885 - _exit flushes output buffers.\n";
305} else {
306 $| = 0;
307 # The following line assumes buffered output, which may be not true:
308 print '@#!*$@(!@#$' unless ($Is_MacOS || $Is_OS2 || $Is_UWin || $Is_OS390 ||
e6c299c8 309 $Is_VMS ||
601f2d16
SB
310 (defined $ENV{PERLIO} &&
311 $ENV{PERLIO} eq 'unix' &&
312 $Config::Config{useperlio}));
404d038e
PG
313 _exit(0);
314}