Ooops. Wise man tests with ithreads before commiting. D'oh!
[p5sagit/p5-mst-13.2.git] / t / io / layers.t
CommitLineData
39f7a870 1#!./perl
2
046e4a6a 3my $PERLIO;
4
39f7a870 5BEGIN {
6 chdir 't' if -d 't';
7 @INC = '../lib';
8 require './test.pl';
491abfa0 9 unless (find PerlIO::Layer 'perlio') {
10 print "1..0 # Skip: not perlio\n";
11 exit 0;
12 }
51dc4578 13 eval 'use Encode';
14 if ($@ =~ /dynamic loading not available/) {
15 print "1..0 # miniperl cannot load Encode\n";
16 exit 0;
17 }
3b0db4f9 18 # Makes testing easier.
19 $ENV{PERLIO} = 'stdio' if exists $ENV{PERLIO} && $ENV{PERLIO} eq '';
8d3a61d9 20 if (exists $ENV{PERLIO} && $ENV{PERLIO} !~ /^(stdio|perlio|mmap)$/) {
3b0db4f9 21 # We are not prepared for anything else.
8d3a61d9 22 print "1..0 # PERLIO='$ENV{PERLIO}' unknown\n";
23 exit 0;
24 }
046e4a6a 25 $PERLIO = exists $ENV{PERLIO} ? $ENV{PERLIO} : "(undef)";
39f7a870 26}
27
39f7a870 28use Config;
29
538204d5 30my $DOSISH = $^O =~ /^(?:MSWin32|os2|dos|NetWare|mint)$/ ? 1 : 0;
cebd85e6 31 $DOSISH = 1 if !$DOSISH and $^O =~ /^uwin/;
15b61c98 32my $NONSTDIO = exists $ENV{PERLIO} && $ENV{PERLIO} ne 'stdio' ? 1 : 0;
33my $FASTSTDIO = $Config{d_faststdio} && $Config{usefaststdio} ? 1 : 0;
7a403565 34my $UNICODE_STDIN;
35if (${^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}
aa451714 44my $NTEST = 43 - (($DOSISH || !$FASTSTDIO) ? 7 : 0) - ($DOSISH ? 5 : 0)
45 + $UNICODE_STDIN;
8229d19f 46
7c0e976d 47sub PerlIO::F_UTF8 () { 0x00008000 } # from perliol.h
48
8229d19f 49plan tests => $NTEST;
50
046e4a6a 51print <<__EOH__;
7a403565 52# PERLIO = $PERLIO
53# DOSISH = $DOSISH
54# NONSTDIO = $NONSTDIO
55# FASTSTDIO = $FASTSTDIO
56# UNICODE = ${^UNICODE}
57# UTF8LOCALE = ${^UTF8LOCALE}
aa451714 58# UNICODE_STDIN = $UNICODE_STDIN
046e4a6a 59__EOH__
52f03692 60
491abfa0 61SKIP: {
43651d81 62 # FIXME - more of these could be tested without Encode or full perl
8229d19f 63 skip("This perl does not have Encode", $NTEST)
491abfa0 64 unless " $Config{extensions} " =~ / Encode /;
43651d81 65 skip("miniperl does not have Encode", $NTEST) if $ENV{PERL_CORE_MINITEST};
39f7a870 66
67 sub check {
68 my ($result, $expected, $id) = @_;
3b0db4f9 69 # An interesting dance follows where we try to make the following
70 # IO layer stack setups to compare equal:
71 #
f0fd62e2 72 # PERLIO UNIX-like DOS-like
3b0db4f9 73 #
f0fd62e2 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
3b0db4f9 78 #
f0fd62e2 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"
046e4a6a 81 #
52f03692 82 if ($NONSTDIO) {
79d9a4d7 83 # Get rid of "unix".
84 shift @$result if $result->[0] eq "unix";
fb189484 85 # Change expectations.
046e4a6a 86 if ($FASTSTDIO) {
87 $expected->[0] = $ENV{PERLIO};
88 } else {
89 $expected->[0] = $ENV{PERLIO} if $expected->[0] eq "stdio";
90 }
e29b014f 91 } elsif (!$FASTSTDIO && !$DOSISH) {
046e4a6a 92 splice(@$result, 0, 2, "stdio")
93 if @$result >= 2 &&
94 $result->[0] eq "unix" &&
95 $result->[1] eq "perlio";
79d9a4d7 96 } elsif ($DOSISH) {
97 splice(@$result, 0, 2, "stdio")
046e4a6a 98 if @$result >= 2 &&
99 $result->[0] eq "unix" &&
79d9a4d7 100 $result->[1] eq "crlf";
fb189484 101 }
8229d19f 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 }
79d9a4d7 108 my $n = scalar @$expected;
e662ed2b 109 is(scalar @$result, $n, "$id - layers == $n");
39f7a870 110 for (my $i = 0; $i < $n; $i++) {
111 my $j = $expected->[$i];
112 if (ref $j eq 'CODE') {
fb189484 113 ok($j->($result->[$i]), "$id - $i is ok");
39f7a870 114 } else {
115 is($result->[$i], $j,
8d3a61d9 116 sprintf("$id - $i is %s",
117 defined $j ? $j : "undef"));
39f7a870 118 }
119 }
120 }
121
122 check([ PerlIO::get_layers(STDIN) ],
aa451714 123 $UNICODE_STDIN ? [ "stdio", "utf8" ] : [ "stdio" ],
39f7a870 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
8229d19f 176 # 7 tests potentially skipped.
177 unless ($DOSISH || !$FASTSTDIO) {
fb189484 178 my @results = PerlIO::get_layers(F, details => 1);
179
79d9a4d7 180 # Get rid of the args and the flags.
181 splice(@results, 1, 2) if $NONSTDIO;
fb189484 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 }
39f7a870 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)");
491abfa0 199
39f7a870 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 1 while unlink "afile";
216}