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