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