This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
(perl #133604) fix binmode on Win32 :crlf layers
[perl5.git] / t / io / crlf.t
CommitLineData
d3db65ff
NIS
1#!./perl -w
2
3BEGIN {
4 chdir 't' if -d 't';
624c42e2
N
5 require "./test.pl";
6 set_up_inc('../lib');
7 require "./charset_tools.pl";
e05e9c3d 8 skip_all_without_perlio();
d3db65ff
NIS
9}
10
11use Config;
12
d3db65ff 13
62a28c97 14my $file = tempfile();
ecb8e4d9
KW
15my $crlf = uni_to_native("\015\012");
16my $crcr = uni_to_native("\x0d\x0d");
d3db65ff 17
f10c05c1
KW
18my $ungetc_count = 8200; # Somewhat over the likely buffer size
19
e05e9c3d 20{
854affc4 21 plan(tests => 21 + 2 * $ungetc_count);
14b1a0c4
RGS
22 ok(open(FOO,">:crlf",$file));
23 ok(print FOO 'a'.((('a' x 14).qq{\n}) x 2000) || close(FOO));
24 ok(open(FOO,"<:crlf",$file));
887ede57 25
14b1a0c4
RGS
26 my $text;
27 { local $/; $text = <FOO> }
ecb8e4d9 28 is(count_chars($text, $crlf), 0);
14b1a0c4 29 is(count_chars($text, "\n"), 2000);
887ede57 30
14b1a0c4
RGS
31 binmode(FOO);
32 seek(FOO,0,0);
33 { local $/; $text = <FOO> }
ecb8e4d9 34 is(count_chars($text, $crlf), 2000);
887ede57 35
14b1a0c4
RGS
36 SKIP:
37 {
c14649af
FC
38 skip_if_miniperl("miniperl can't rely on loading PerlIO::scalar",
39 2 * $ungetc_count + 1);
40 skip("no PerlIO::scalar", 2 * $ungetc_count + 1)
41 unless $Config{extensions} =~ m!\bPerlIO/scalar\b!;
14b1a0c4 42 require PerlIO::scalar;
ecb8e4d9 43 my $fcontents = join "", map {"$_$crlf"} "a".."zzz";
14b1a0c4
RGS
44 open my $fh, "<:crlf", \$fcontents;
45 local $/ = "xxx";
46 local $_ = <$fh>;
47 my $pos = tell $fh; # pos must be behind "xxx", before "\nxxy\n"
48 seek $fh, $pos, 0;
49 $/ = "\n";
50 $s = <$fh>.<$fh>;
bd1869dc 51 is($s, "\nxxy\n");
f10c05c1
KW
52
53 for my $i (0 .. $ungetc_count - 1) {
54 my $j = $i % 256;
55 is($fh->ungetc($j), $j, "ungetc of $j returns itself");
56 }
57
58 for (my $i = $ungetc_count - 1; $i >= 0; $i--) {
59 my $j = $i % 256;
60 is(ord($fh->getc()), $j, "getc gets back $j");
61 }
14b1a0c4 62 }
e949e37c 63
14b1a0c4 64 ok(close(FOO));
8229d19f 65
14b1a0c4
RGS
66 # binmode :crlf should not cumulate.
67 # Try it first once and then twice so that even UNIXy boxes
68 # get to exercise this, for DOSish boxes even once is enough.
69 # Try also pushing :utf8 first so that there are other layers
70 # in between (this should not matter: CRLF layers still should
71 # not accumulate).
72 for my $utf8 ('', ':utf8') {
73 for my $binmode (1..2) {
74 open(FOO, ">$file");
75 # require PerlIO; print PerlIO::get_layers(FOO), "\n";
76 binmode(FOO, "$utf8:crlf") for 1..$binmode;
77 # require PerlIO; print PerlIO::get_layers(FOO), "\n";
78 print FOO "Hello\n";
79 close FOO;
80 open(FOO, "<$file");
81 binmode(FOO);
82 my $foo = scalar <FOO>;
83 close FOO;
84 print join(" ", "#", map { sprintf("%02x", $_) } unpack("C*", $foo)),
85 "\n";
ecb8e4d9
KW
86 like($foo, qr/$crlf$/);
87 unlike($foo, qr/$crcr/);
14b1a0c4
RGS
88 }
89 }
854affc4
TC
90
91 {
92 # check binmode removes :utf8
93 # 133604 - on Win32 :crlf is the base buffer layer, so
94 # binmode doesn't remove it, but the binmode handler didn't
95 # remove :utf8 either
96 ok(open(my $fh, ">", $file), "open a file");
97 ok(binmode($fh, ":utf8"), "add :utf8");
98 ok((() = grep($_ eq "utf8", PerlIO::get_layers($fh))),
99 "check :utf8 set");
100 ok(binmode($fh), "remove :utf8");
854affc4
TC
101 ok(!(() = grep($_ eq "utf8", PerlIO::get_layers($fh))),
102 "check :utf8 removed");
103 close $fh;
104 }
d3db65ff 105}
d3db65ff 106
887ede57 107sub count_chars {
14b1a0c4
RGS
108 my($text, $chars) = @_;
109 my $seen = 0;
110 $seen++ while $text =~ /$chars/g;
111 return $seen;
887ede57 112}