This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Net::Ping 500_ping_icmp.t: remove sudo code
[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{
f10c05c1 21 plan(tests => 16 + 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 }
d3db65ff 90}
d3db65ff 91
887ede57 92sub count_chars {
14b1a0c4
RGS
93 my($text, $chars) = @_;
94 my $seen = 0;
95 $seen++ while $text =~ /$chars/g;
96 return $seen;
887ede57 97}