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