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