summaryrefslogtreecommitdiff
path: root/t/io/crlf.t
blob: f59b245d511587ed0a122867391d2a0214efc56b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!./perl -w

BEGIN {
    chdir 't' if -d 't';
    @INC = qw(. ../lib);
}

use Config;

require "test.pl";

my $file = "crlf$$.dat";
END {
    1 while unlink($file);
}

if (find PerlIO::Layer 'perlio') {
 plan(tests => 16);
 ok(open(FOO,">:crlf",$file));
 ok(print FOO 'a'.((('a' x 14).qq{\n}) x 2000) || close(FOO));
 ok(open(FOO,"<:crlf",$file));

 my $text;
 { local $/; $text = <FOO> }
 is(count_chars($text, "\015\012"), 0);
 is(count_chars($text, "\n"), 2000);

 binmode(FOO);
 seek(FOO,0,0);
 { local $/; $text = <FOO> }
 is(count_chars($text, "\015\012"), 2000);

 SKIP:
 {
  skip("miniperl can't rely on loading PerlIO::scalar")
      if $ENV{PERL_CORE_MINITEST};
  skip("no PerlIO::scalar") unless $Config{extensions} =~ /PerlIO::scalar/;
  require PerlIO::scalar;
  my $fcontents = join "", map {"$_\015\012"} "a".."zzz";
  open my $fh, "<:crlf", \$fcontents;
  local $/ = "xxx";
  local $_ = <$fh>;
  my $pos = tell $fh; # pos must be behind "xxx", before "\nyyy\n"
  seek $fh, $pos, 0;
  $/ = "\n";
  $s = <$fh>.<$fh>;
  ok($s eq "\nxxy\n");
 }

 ok(close(FOO));

 # binmode :crlf should not cumulate.
 # Try it first once and then twice so that even UNIXy boxes
 # get to exercise this, for DOSish boxes even once is enough.
 # Try also pushing :utf8 first so that there are other layers
 # in between (this should not matter: CRLF layers still should
 # not accumulate).
 for my $utf8 ('', ':utf8') {
     for my $binmode (1..2) {
	 open(FOO, ">$file");
	 # require PerlIO; print PerlIO::get_layers(FOO), "\n";
	 binmode(FOO, "$utf8:crlf") for 1..$binmode;
	 # require PerlIO; print PerlIO::get_layers(FOO), "\n";
	 print FOO "Hello\n";
	 close FOO;
	 open(FOO, "<$file");
	 binmode(FOO);
	 my $foo = scalar <FOO>;
	 close FOO;
	 print join(" ", "#", map { sprintf("%02x", $_) } unpack("C*", $foo)),
	       "\n";
	 ok($foo =~ /\x0d\x0a$/);
	 ok($foo !~ /\x0d\x0d/);
     }
 }
}
else {
 skip_all("No perlio, so no :crlf");
}

sub count_chars {
  my($text, $chars) = @_;
  my $seen = 0;
  $seen++ while $text =~ /$chars/g;
  return $seen;
}