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