xref: /openbsd/gnu/usr.bin/perl/t/io/crlf.t (revision 905646f0)
1#!./perl -w
2
3BEGIN {
4    chdir 't' if -d 't';
5    require "./test.pl";
6    set_up_inc('../lib');
7    require "./charset_tools.pl";
8    skip_all_without_perlio();
9}
10
11use Config;
12
13
14my $file = tempfile();
15my $crlf = uni_to_native("\015\012");
16my $crcr = uni_to_native("\x0d\x0d");
17
18my $ungetc_count = 8200;    # Somewhat over the likely buffer size
19
20{
21    plan(tests => 21 + 2 * $ungetc_count);
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));
25
26    my $text;
27    { local $/; $text = <FOO> }
28    is(count_chars($text, $crlf), 0);
29    is(count_chars($text, "\n"), 2000);
30
31    binmode(FOO);
32    seek(FOO,0,0);
33    { local $/; $text = <FOO> }
34    is(count_chars($text, $crlf), 2000);
35
36    SKIP:
37    {
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!;
42	require PerlIO::scalar;
43	my $fcontents = join "", map {"$_$crlf"} "a".."zzz";
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>;
51	is($s, "\nxxy\n");
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        }
62    }
63
64    ok(close(FOO));
65
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";
86	    like($foo, qr/$crlf$/);
87	    unlike($foo, qr/$crcr/);
88	}
89    }
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");
101        ok(!(() = grep($_ eq "utf8", PerlIO::get_layers($fh))),
102           "check :utf8 removed");
103        close $fh;
104    }
105}
106
107sub count_chars {
108    my($text, $chars) = @_;
109    my $seen = 0;
110    $seen++ while $text =~ /$chars/g;
111    return $seen;
112}
113