9 unless (find PerlIO::Layer 'perlio') {
10 print "1..0 # Skip: not perlio\n";
14 if ($@ =~ /dynamic loading not available/) {
15 print "1..0 # miniperl cannot load Encode\n";
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";
25 $PERLIO = exists $ENV{PERLIO} ? $ENV{PERLIO} : "(undef)";
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;
35 if (${^UNICODE} & 1) {
36 if (${^UNICODE} & 64) {
37 # Conditional on the locale
38 $UNICODE_STDIN = ${^UTF8LOCALE};
44 my $NTEST = 44 - (($DOSISH || !$FASTSTDIO) ? 7 : 0) - ($DOSISH ? 5 : 0)
47 sub PerlIO::F_UTF8 () { 0x00008000 } # from perliol.h
54 # NONSTDIO = $NONSTDIO
55 # FASTSTDIO = $FASTSTDIO
56 # UNICODE = ${^UNICODE}
57 # UTF8LOCALE = ${^UTF8LOCALE}
58 # UNICODE_STDIN = $UNICODE_STDIN
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};
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:
72 # PERLIO UNIX-like DOS-like
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
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"
84 shift @$result if $result->[0] eq "unix";
85 # Change expectations.
87 $expected->[0] = $ENV{PERLIO};
89 $expected->[0] = $ENV{PERLIO} if $expected->[0] eq "stdio";
91 } elsif (!$FASTSTDIO && !$DOSISH) {
92 splice(@$result, 0, 2, "stdio")
94 $result->[0] eq "unix" &&
95 $result->[1] eq "perlio";
97 splice(@$result, 0, 2, "stdio")
99 $result->[0] eq "unix" &&
100 $result->[1] eq "crlf";
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;
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");
115 is($result->[$i], $j,
116 sprintf("$id - $i is %s",
117 defined $j ? $j : "undef"));
122 check([ PerlIO::get_layers(STDIN) ],
123 $UNICODE_STDIN ? [ "stdio", "utf8" ] : [ "stdio" ],
126 open(F, ">:crlf", "afile");
128 check([ PerlIO::get_layers(F) ],
132 binmode(F, ":encoding(sjis)"); # "sjis" will be canonized to "shiftjis"
134 check([ PerlIO::get_layers(F) ],
135 [ qw[stdio crlf encoding(shiftjis) utf8] ],
140 check([ PerlIO::get_layers(F) ],
146 check([ PerlIO::get_layers(F) ],
152 check([ PerlIO::get_layers(F) ],
156 binmode(F, ":bytes");
158 check([ PerlIO::get_layers(F) ],
162 binmode(F, ":encoding(utf8)");
164 check([ PerlIO::get_layers(F) ],
165 [ qw[stdio encoding(utf8) utf8] ],
168 binmode(F, ":raw :crlf");
170 check([ PerlIO::get_layers(F) ],
174 binmode(F, ":raw :encoding(latin1)"); # "latin1" will be canonized
176 # 7 tests potentially skipped.
177 unless ($DOSISH || !$FASTSTDIO) {
178 my @results = PerlIO::get_layers(F, details => 1);
180 # Get rid of the args and the flags.
181 splice(@results, 1, 2) if $NONSTDIO;
184 [ "stdio", undef, sub { $_[0] > 0 },
185 "encoding", "iso-8859-1", sub { $_[0] & PerlIO::F_UTF8() } ],
186 ":raw:encoding(latin1)");
191 check([ PerlIO::get_layers(F) ],
198 use open(IN => ":crlf", OUT => ":encoding(cp1252)");
203 check([ PerlIO::get_layers(F, input => 1) ],
207 check([ PerlIO::get_layers(G, output => 1) ],
208 [ qw[stdio encoding(cp1252) utf8] ],
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};
222 1 while unlink "afile";