Two Cygwin patches from Gerrit.
[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
28plan tests => 43;
29
30use Config;
31
538204d5 32my $DOSISH = $^O =~ /^(?:MSWin32|os2|dos|NetWare|mint)$/ ? 1 : 0;
cebd85e6 33 $DOSISH = 1 if !$DOSISH and $^O =~ /^uwin/;
15b61c98 34my $NONSTDIO = exists $ENV{PERLIO} && $ENV{PERLIO} ne 'stdio' ? 1 : 0;
35my $FASTSTDIO = $Config{d_faststdio} && $Config{usefaststdio} ? 1 : 0;
046e4a6a 36
37print <<__EOH__;
38# PERLIO = $PERLIO
39# DOSISH = $DOSISH
40# NONSTDIO = $NONSTDIO
41# FASTSTDIO = $FASTSTDIO
42__EOH__
52f03692 43
491abfa0 44SKIP: {
45 skip("This perl does not have Encode", 43)
46 unless " $Config{extensions} " =~ / Encode /;
39f7a870 47
48 sub check {
49 my ($result, $expected, $id) = @_;
3b0db4f9 50 # An interesting dance follows where we try to make the following
51 # IO layer stack setups to compare equal:
52 #
f0fd62e2 53 # PERLIO UNIX-like DOS-like
3b0db4f9 54 #
f0fd62e2 55 # unset / "" unix perlio / stdio [1] unix crlf
56 # stdio unix perlio / stdio [1] stdio
57 # perlio unix perlio unix perlio
58 # mmap unix mmap unix mmap
3b0db4f9 59 #
f0fd62e2 60 # [1] "stdio" if Configure found out how to do "fast stdio" (depends
61 # on the stdio implementation) and in Perl 5.8, otherwise "unix perlio"
046e4a6a 62 #
52f03692 63 if ($NONSTDIO) {
79d9a4d7 64 # Get rid of "unix".
65 shift @$result if $result->[0] eq "unix";
fb189484 66 # Change expectations.
046e4a6a 67 if ($FASTSTDIO) {
68 $expected->[0] = $ENV{PERLIO};
69 } else {
70 $expected->[0] = $ENV{PERLIO} if $expected->[0] eq "stdio";
71 }
e29b014f 72 } elsif (!$FASTSTDIO && !$DOSISH) {
046e4a6a 73 splice(@$result, 0, 2, "stdio")
74 if @$result >= 2 &&
75 $result->[0] eq "unix" &&
76 $result->[1] eq "perlio";
79d9a4d7 77 } elsif ($DOSISH) {
78 splice(@$result, 0, 2, "stdio")
046e4a6a 79 if @$result >= 2 &&
80 $result->[0] eq "unix" &&
79d9a4d7 81 $result->[1] eq "crlf";
fb189484 82 }
79d9a4d7 83 my $n = scalar @$expected;
84 is($n, scalar @$expected, "$id - layers = $n");
39f7a870 85 for (my $i = 0; $i < $n; $i++) {
86 my $j = $expected->[$i];
87 if (ref $j eq 'CODE') {
fb189484 88 ok($j->($result->[$i]), "$id - $i is ok");
39f7a870 89 } else {
90 is($result->[$i], $j,
8d3a61d9 91 sprintf("$id - $i is %s",
92 defined $j ? $j : "undef"));
39f7a870 93 }
94 }
95 }
96
97 check([ PerlIO::get_layers(STDIN) ],
98 [ "stdio" ],
99 "STDIN");
100
101 open(F, ">:crlf", "afile");
102
103 check([ PerlIO::get_layers(F) ],
104 [ qw(stdio crlf) ],
105 "open :crlf");
106
107 binmode(F, ":encoding(sjis)"); # "sjis" will be canonized to "shiftjis"
108
109 check([ PerlIO::get_layers(F) ],
110 [ qw[stdio crlf encoding(shiftjis) utf8] ],
111 ":encoding(sjis)");
112
113 binmode(F, ":pop");
114
115 check([ PerlIO::get_layers(F) ],
116 [ qw(stdio crlf) ],
117 ":pop");
118
119 binmode(F, ":raw");
120
121 check([ PerlIO::get_layers(F) ],
122 [ "stdio" ],
123 ":raw");
124
79d9a4d7 125 binmode(F, ":pop") if $DOSISH; # Drop one extra :crlf.
39f7a870 126 binmode(F, ":utf8");
127
128 check([ PerlIO::get_layers(F) ],
129 [ qw(stdio utf8) ],
130 ":utf8");
131
132 binmode(F, ":bytes");
133
134 check([ PerlIO::get_layers(F) ],
135 [ "stdio" ],
136 ":bytes");
137
138 binmode(F, ":encoding(utf8)");
139
140 check([ PerlIO::get_layers(F) ],
141 [ qw[stdio encoding(utf8) utf8] ],
142 ":encoding(utf8)");
143
144 binmode(F, ":raw :crlf");
145
146 check([ PerlIO::get_layers(F) ],
147 [ qw(stdio crlf) ],
148 ":raw:crlf");
149
150 binmode(F, ":raw :encoding(latin1)"); # "latin1" will be canonized
151
79d9a4d7 152 SKIP: {
046e4a6a 153 skip("too complex layer coreography", 7) if $DOSISH || !$FASTSTDIO;
79d9a4d7 154
fb189484 155 my @results = PerlIO::get_layers(F, details => 1);
156
79d9a4d7 157 # Get rid of the args and the flags.
158 splice(@results, 1, 2) if $NONSTDIO;
fb189484 159
160 check([ @results ],
161 [ "stdio", undef, sub { $_[0] > 0 },
162 "encoding", "iso-8859-1", sub { $_[0] & PerlIO::F_UTF8() } ],
163 ":raw:encoding(latin1)");
164 }
39f7a870 165
166 binmode(F);
167
168 check([ PerlIO::get_layers(F) ],
169 [ "stdio" ],
170 "binmode");
171
172 close F;
173
174 {
175 use open(IN => ":crlf", OUT => ":encoding(cp1252)");
491abfa0 176
39f7a870 177 open F, "<afile";
178 open G, ">afile";
179
180 check([ PerlIO::get_layers(F, input => 1) ],
181 [ qw(stdio crlf) ],
182 "use open IN");
183
184 check([ PerlIO::get_layers(G, output => 1) ],
185 [ qw[stdio encoding(cp1252) utf8] ],
186 "use open OUT");
187
188 close F;
189 close G;
190 }
191
192 1 while unlink "afile";
193}