Cygwin fails to open $^X on many occasion here
[p5sagit/p5-mst-13.2.git] / t / io / layers.t
1 #!./perl
2
3 my $PERLIO;
4
5 BEGIN {
6     chdir 't' if -d 't';
7     @INC = '../lib';
8     require './test.pl';
9     unless (find PerlIO::Layer 'perlio') {
10         print "1..0 # Skip: not perlio\n";
11         exit 0;
12     }
13     eval 'use Encode';
14     if ($@ =~ /dynamic loading not available/) {
15         print "1..0 # miniperl cannot load Encode\n";
16         exit 0;
17     }
18     # Makes testing easier.
19     $ENV{PERLIO} = 'stdio' if exists $ENV{PERLIO} && $ENV{PERLIO} eq '';
20     if (exists $ENV{PERLIO} && $ENV{PERLIO} !~ /^(stdio|perlio|mmap)$/) {
21         # We are not prepared for anything else.
22         print "1..0 # PERLIO='$ENV{PERLIO}' unknown\n";
23         exit 0;
24     }
25     $PERLIO = exists $ENV{PERLIO} ? $ENV{PERLIO} : "(undef)";
26 }
27
28 use Config;
29
30 my $DOSISH    = $^O =~ /^(?:MSWin32|os2|dos|NetWare|mint)$/ ? 1 : 0;
31    $DOSISH    = 1 if !$DOSISH and $^O =~ /^uwin/;
32 my $NONSTDIO  = exists $ENV{PERLIO} && $ENV{PERLIO} ne 'stdio'     ? 1 : 0;
33 my $FASTSTDIO = $Config{d_faststdio} && $Config{usefaststdio}      ? 1 : 0;
34 my $UNICODE_STDIN;
35 if (${^UNICODE} & 1) {
36     if (${^UNICODE} & 64) {
37         # Conditional on the locale
38         $UNICODE_STDIN = ${^UTF8LOCALE};
39     } else {
40         # Unconditional
41         $UNICODE_STDIN = 1;
42     }
43 }
44 my $NTEST = 44 - (($DOSISH || !$FASTSTDIO) ? 7 : 0) - ($DOSISH ? 5 : 0)
45     + $UNICODE_STDIN;
46
47 sub PerlIO::F_UTF8 () { 0x00008000 } # from perliol.h
48
49 plan tests => $NTEST;
50
51 print <<__EOH__;
52 # PERLIO        = $PERLIO
53 # DOSISH        = $DOSISH
54 # NONSTDIO      = $NONSTDIO
55 # FASTSTDIO     = $FASTSTDIO
56 # UNICODE       = ${^UNICODE}
57 # UTF8LOCALE    = ${^UTF8LOCALE}
58 # UNICODE_STDIN = $UNICODE_STDIN
59 __EOH__
60
61 SKIP: {
62     # FIXME - more of these could be tested without Encode or full perl
63     skip("This perl does not have Encode", $NTEST)
64         unless " $Config{extensions} " =~ / Encode /;
65     skip("miniperl does not have Encode", $NTEST) if $ENV{PERL_CORE_MINITEST};
66
67     sub check {
68         my ($result, $expected, $id) = @_;
69         # An interesting dance follows where we try to make the following
70         # IO layer stack setups to compare equal:
71         #
72         # PERLIO     UNIX-like                   DOS-like
73         #
74         # unset / "" unix perlio / stdio [1]     unix crlf
75         # stdio      unix perlio / stdio [1]     stdio
76         # perlio     unix perlio                 unix perlio
77         # mmap       unix mmap                   unix mmap
78         #
79         # [1] "stdio" if Configure found out how to do "fast stdio" (depends
80         # on the stdio implementation) and in Perl 5.8, otherwise "unix perlio"
81         #
82         if ($NONSTDIO) {
83             # Get rid of "unix".
84             shift @$result if $result->[0] eq "unix";
85             # Change expectations.
86             if ($FASTSTDIO) {
87                 $expected->[0] = $ENV{PERLIO};
88             } else {
89                 $expected->[0] = $ENV{PERLIO} if $expected->[0] eq "stdio";
90             }
91         } elsif (!$FASTSTDIO && !$DOSISH) {
92             splice(@$result, 0, 2, "stdio")
93                 if @$result >= 2 &&
94                    $result->[0] eq "unix" &&
95                    $result->[1] eq "perlio";
96         } elsif ($DOSISH) {
97             splice(@$result, 0, 2, "stdio")
98                 if @$result >= 2 &&
99                    $result->[0] eq "unix" &&
100                    $result->[1] eq "crlf";
101         }
102         if ($DOSISH && grep { $_ eq 'crlf' } @$expected) {
103             # 5 tests potentially skipped because
104             # DOSISH systems already have a CRLF layer
105             # which will make new ones not stick.
106             @$expected = grep { $_ ne 'crlf' } @$expected;
107         }
108         my $n = scalar @$expected;
109         is(scalar @$result, $n, "$id - layers == $n");
110         for (my $i = 0; $i < $n; $i++) {
111             my $j = $expected->[$i];
112             if (ref $j eq 'CODE') {
113                 ok($j->($result->[$i]), "$id - $i is ok");
114             } else {
115                 is($result->[$i], $j,
116                    sprintf("$id - $i is %s",
117                            defined $j ? $j : "undef"));
118             }
119         }
120     }
121
122     check([ PerlIO::get_layers(STDIN) ],
123           $UNICODE_STDIN ? [ "stdio", "utf8" ] : [ "stdio" ],
124           "STDIN");
125
126     open(F, ">:crlf", "afile");
127
128     check([ PerlIO::get_layers(F) ],
129           [ qw(stdio crlf) ],
130           "open :crlf");
131
132     binmode(F, ":encoding(sjis)"); # "sjis" will be canonized to "shiftjis"
133
134     check([ PerlIO::get_layers(F) ],
135           [ qw[stdio crlf encoding(shiftjis) utf8] ],
136           ":encoding(sjis)");
137     
138     binmode(F, ":pop");
139
140     check([ PerlIO::get_layers(F) ],
141           [ qw(stdio crlf) ],
142           ":pop");
143
144     binmode(F, ":raw");
145
146     check([ PerlIO::get_layers(F) ],
147           [ "stdio" ],
148           ":raw");
149
150     binmode(F, ":utf8");
151
152     check([ PerlIO::get_layers(F) ],
153           [ qw(stdio utf8) ],
154           ":utf8");
155
156     binmode(F, ":bytes");
157
158     check([ PerlIO::get_layers(F) ],
159           [ "stdio" ],
160           ":bytes");
161
162     binmode(F, ":encoding(utf8)");
163
164     check([ PerlIO::get_layers(F) ],
165             [ qw[stdio encoding(utf8) utf8] ],
166             ":encoding(utf8)");
167
168     binmode(F, ":raw :crlf");
169
170     check([ PerlIO::get_layers(F) ],
171           [ qw(stdio crlf) ],
172           ":raw:crlf");
173
174     binmode(F, ":raw :encoding(latin1)"); # "latin1" will be canonized
175
176     # 7 tests potentially skipped.
177     unless ($DOSISH || !$FASTSTDIO) {
178         my @results = PerlIO::get_layers(F, details => 1);
179
180         # Get rid of the args and the flags.
181         splice(@results, 1, 2) if $NONSTDIO;
182
183         check([ @results ],
184               [ "stdio",    undef,        sub { $_[0] > 0 },
185                 "encoding", "iso-8859-1", sub { $_[0] & PerlIO::F_UTF8() } ],
186               ":raw:encoding(latin1)");
187     }
188
189     binmode(F);
190
191     check([ PerlIO::get_layers(F) ],
192           [ "stdio" ],
193           "binmode");
194
195     close F;
196
197     {
198         use open(IN => ":crlf", OUT => ":encoding(cp1252)");
199
200         open F, "<afile";
201         open G, ">afile";
202
203         check([ PerlIO::get_layers(F, input  => 1) ],
204               [ qw(stdio crlf) ],
205               "use open IN");
206         
207         check([ PerlIO::get_layers(G, output => 1) ],
208               [ qw[stdio encoding(cp1252) utf8] ],
209               "use open OUT");
210
211         close F;
212         close G;
213     }
214
215     # Check that PL_sigwarn's reference count is correct, and that 
216     # &PerlIO::Layer::NoWarnings isn't prematurely freed.
217     fresh_perl_like (<<'EOT', qr/^CODE/);
218 open(UTF, "<:raw:encoding(utf8)", "afile") or die $!;
219 print ref *PerlIO::Layer::NoWarnings{CODE};
220 EOT
221
222     1 while unlink "afile";
223 }