This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
In ext/POSIX/t/sysconf.t, make the tests more strict.
[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
a572b909 11use Test::More tests => 87;
e6c299c8 12
212caf55 13use POSIX qw(fcntl_h signal_h limits_h _exit getcwd open read strftime write
4a948f3f 14 errno localeconv);
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
a572b909
NC
54my $testfd = open("Makefile.PL", O_RDONLY, 0);
55like($testfd, qr/\A\d+\z/, '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
a572b909
NC
67my $test = next_test();
68write(1,"ok $test\nnot ok $test\n", 5);
e6c299c8
JH
69
70SKIP: {
71 skip("no pipe() support on DOS", 2) if $Is_Dos;
a0d0e21e 72
10de532f 73 @fds = POSIX::pipe();
a572b909 74 cmp_ok($fds[0], '>', $testfd, 'POSIX::pipe');
e6c299c8 75
10de532f
JH
76 CORE::open($reader = \*READER, "<&=".$fds[0]);
77 CORE::open($writer = \*WRITER, ">&=".$fds[1]);
a572b909
NC
78 my $test = next_test();
79 print $writer "ok $test\n";
10de532f
JH
80 close $writer;
81 print <$reader>;
82 close $reader;
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
a572b909
NC
147 cmp_ok(&_POSIX_OPEN_MAX, '>=', 16,
148 "The minimum allowed values according to susv2" );
4e0f6e8c 149
c9ff6e92 150}
a0d0e21e 151
d536870a 152my $pat;
2adbc9b6
NC
153if ( $unix_mode ) {
154 $pat = qr#[\\/]POSIX$#i;
e6c299c8
JH
155}
156else {
2adbc9b6 157 $pat = qr/\.POSIX]/i;
d536870a 158}
e6c299c8 159like( getcwd(), qr/$pat/, 'getcwd' );
a0d0e21e 160
a89d8a78
DH
161# Check string conversion functions.
162
e6c299c8 163SKIP: {
a572b909 164 skip("strtod() not present", 2) unless $Config{d_strtod};
e6c299c8 165
ff68c719 166 $lc = &POSIX::setlocale(&POSIX::LC_NUMERIC, 'C') if $Config{d_setlocale};
e6c299c8
JH
167
168 # we're just checking that strtod works, not how accurate it is
a89d8a78 169 ($n, $x) = &POSIX::strtod('3.14159_OR_SO');
a572b909
NC
170 cmp_ok(abs("3.14159" - $n), '<', 1e-6, 'strtod works');
171 is($x, 6, 'strtod works');
e6c299c8 172
ff68c719 173 &POSIX::setlocale(&POSIX::LC_NUMERIC, $lc) if $Config{d_setlocale};
e6c299c8
JH
174}
175
176SKIP: {
177 skip("strtol() not present", 2) unless $Config{d_strtol};
a89d8a78 178
a89d8a78 179 ($n, $x) = &POSIX::strtol('21_PENGUINS');
e6c299c8
JH
180 is($n, 21, 'strtol() number');
181 is($x, 9, ' unparsed chars');
182}
183
184SKIP: {
185 skip("strtoul() not present", 2) unless $Config{d_strtoul};
a89d8a78 186
a89d8a78 187 ($n, $x) = &POSIX::strtoul('88_TEARS');
e6c299c8
JH
188 is($n, 88, 'strtoul() number');
189 is($x, 6, ' unparsed chars');
190}
a89d8a78 191
a0d0e21e 192# Pick up whether we're really able to dynamically load everything.
a572b909 193cmp_ok(&POSIX::acos(1.0), '==', 0.0, 'dynamic loading');
a0d0e21e 194
84ef74c4
AD
195# This can coredump if struct tm has a timezone field and we
196# didn't detect it. If this fails, try adding
197# -DSTRUCT_TM_HASZONE to your cflags when compiling ext/POSIX/POSIX.c.
198# See ext/POSIX/hints/sunos_4.pl and ext/POSIX/hints/linux.pl
a572b909
NC
199$test = next_test();
200print POSIX::strftime("ok $test # %H:%M, on %m/%d/%y\n", localtime());
84ef74c4 201
33c0e3ec
SB
202# If that worked, validate the mini_mktime() routine's normalisation of
203# input fields to strftime().
204sub try_strftime {
33c0e3ec
SB
205 my $expect = shift;
206 my $got = POSIX::strftime("%a %b %d %H:%M:%S %Y %j", @_);
61a515a6 207 is($got, $expect, "validating mini_mktime() and strftime(): $expect");
33c0e3ec
SB
208}
209
210$lc = &POSIX::setlocale(&POSIX::LC_TIME, 'C') if $Config{d_setlocale};
e6c299c8 211try_strftime("Wed Feb 28 00:00:00 1996 059", 0,0,0, 28,1,96);
53059177 212SKIP: {
abbe0d1a 213 skip("VC++ 8 and Vista's CRTs regard 60 seconds as an invalid parameter", 1)
6fa15125 214 if ($Is_W32 and (($Config{cc} eq 'cl' and
abbe0d1a 215 $Config{ccversion} =~ /^(\d+)/ and $1 >= 14) or
6fa15125 216 (Win32::GetOSVersion())[1] >= 6));
53059177
SH
217
218 try_strftime("Thu Feb 29 00:00:60 1996 060", 60,0,-24, 30,1,96);
219}
e6c299c8
JH
220try_strftime("Fri Mar 01 00:00:00 1996 061", 0,0,-24, 31,1,96);
221try_strftime("Sun Feb 28 00:00:00 1999 059", 0,0,0, 28,1,99);
222try_strftime("Mon Mar 01 00:00:00 1999 060", 0,0,24, 28,1,99);
223try_strftime("Mon Feb 28 00:00:00 2000 059", 0,0,0, 28,1,100);
224try_strftime("Tue Feb 29 00:00:00 2000 060", 0,0,0, 0,2,100);
225try_strftime("Wed Mar 01 00:00:00 2000 061", 0,0,0, 1,2,100);
226try_strftime("Fri Mar 31 00:00:00 2000 091", 0,0,0, 31,2,100);
33c0e3ec
SB
227&POSIX::setlocale(&POSIX::LC_TIME, $lc) if $Config{d_setlocale};
228
212caf55
TS
229{
230 for my $test (0, 1) {
231 $! = 0;
232 # POSIX::errno is autoloaded.
233 # Autoloading requires many system calls.
234 # errno() looks at $! to generate its result.
235 # Autoloading should not munge the value.
236 my $foo = $!;
237 my $errno = POSIX::errno();
e6c299c8 238
e6c299c8
JH
239 # Force numeric context.
240 is( $errno + 0, $foo + 0, 'autoloading and errno() mix' );
212caf55
TS
241 }
242}
243
d4742b2c
NC
244SKIP: {
245 skip("no kill() support on Mac OS", 1) if $Is_MacOS;
246 is (eval "kill 0", 0, "check we have CORE::kill")
247 or print "\$\@ is " . _qq($@) . "\n";
248}
249
250# Check that we can import the POSIX kill routine
251POSIX->import ('kill');
252my $result = eval "kill 0";
253is ($result, undef, "we should now have POSIX::kill");
254# Check usage.
255like ($@, qr/^Usage: POSIX::kill\(pid, sig\)/, "check its usage message");
256
257# Check unimplemented.
258$result = eval {POSIX::offsetof};
259is ($result, undef, "offsetof should fail");
260like ($@, qr/^Unimplemented: POSIX::offsetof\(\) is C-specific/,
261 "check its unimplemented message");
262
263# Check reimplemented.
264$result = eval {POSIX::fgets};
265is ($result, undef, "fgets should fail");
266like ($@, qr/^Use method IO::Handle::gets\(\) instead/,
267 "check its redef message");
268
4b3c6531
RGS
269# Simplistic tests for the isXXX() functions (bug #16799)
270ok( POSIX::isalnum('1'), 'isalnum' );
271ok(!POSIX::isalnum('*'), 'isalnum' );
272ok( POSIX::isalpha('f'), 'isalpha' );
273ok(!POSIX::isalpha('7'), 'isalpha' );
274ok( POSIX::iscntrl("\cA"),'iscntrl' );
275ok(!POSIX::iscntrl("A"), 'iscntrl' );
276ok( POSIX::isdigit('1'), 'isdigit' );
277ok(!POSIX::isdigit('z'), 'isdigit' );
278ok( POSIX::isgraph('@'), 'isgraph' );
279ok(!POSIX::isgraph(' '), 'isgraph' );
280ok( POSIX::islower('l'), 'islower' );
281ok(!POSIX::islower('L'), 'islower' );
282ok( POSIX::isupper('U'), 'isupper' );
283ok(!POSIX::isupper('u'), 'isupper' );
284ok( POSIX::isprint('$'), 'isprint' );
285ok(!POSIX::isprint("\n"), 'isprint' );
286ok( POSIX::ispunct('%'), 'ispunct' );
287ok(!POSIX::ispunct('u'), 'ispunct' );
288ok( POSIX::isspace("\t"), 'isspace' );
289ok(!POSIX::isspace('_'), 'isspace' );
290ok( POSIX::isxdigit('f'), 'isxdigit' );
291ok(!POSIX::isxdigit('g'), 'isxdigit' );
117206bb
RGS
292# metaphysical question : what should be returned for an empty string ?
293# anyway this shouldn't segfault (bug #24554)
294ok( POSIX::isalnum(''), 'isalnum empty string' );
295ok( POSIX::isalnum(undef),'isalnum undef' );
767bb2e0
TS
296# those functions should stringify their arguments
297ok(!POSIX::isalpha([]), 'isalpha []' );
298ok( POSIX::isprint([]), 'isprint []' );
2ae48df0
RGS
299
300eval { use strict; POSIX->import("S_ISBLK"); my $x = S_ISBLK };
301unlike( $@, qr/Can't use string .* as a symbol ref/, "Can import autoloaded constants" );
4a948f3f
NC
302
303SKIP: {
304 skip("localeconv() not present", 20) unless $Config{d_locconv};
305 my $conv = localeconv;
306 is(ref $conv, 'HASH', 'localconv returns a hash reference');
307
308 foreach (qw(decimal_point thousands_sep grouping int_curr_symbol
309 currency_symbol mon_decimal_point mon_thousands_sep
310 mon_grouping positive_sign negative_sign)) {
311 SKIP: {
312 skip("localeconv has no result for $_", 1)
313 unless exists $conv->{$_};
314 unlike(delete $conv->{$_}, qr/\A\z/,
315 "localeconv returned a non-empty string for $_");
316 }
317 }
318
319 foreach (qw(int_frac_digits frac_digits p_cs_precedes p_sep_by_space
320 n_cs_precedes n_sep_by_space p_sign_posn n_sign_posn)) {
321 SKIP: {
322 skip("localeconv has no result for $_", 1)
323 unless exists $conv->{$_};
324 like(delete $conv->{$_}, qr/\A-?\d+\z/,
325 "localeconv returned an integer for $_");
326 }
327 }
328 is_deeply([%$conv], [], 'no unexpected keys returned by localeconv');
329}
330
404d038e
PG
331# Check that output is not flushed by _exit. This test should be last
332# in the file, and is not counted in the total number of tests.
333if ($^O eq 'vos') {
334 print "# TODO - hit VOS bug posix-885 - _exit flushes output buffers.\n";
335} else {
336 $| = 0;
337 # The following line assumes buffered output, which may be not true:
338 print '@#!*$@(!@#$' unless ($Is_MacOS || $Is_OS2 || $Is_UWin || $Is_OS390 ||
e6c299c8 339 $Is_VMS ||
601f2d16
SB
340 (defined $ENV{PERLIO} &&
341 $ENV{PERLIO} eq 'unix' &&
342 $Config::Config{useperlio}));
404d038e
PG
343 _exit(0);
344}