Integrate mainline
[p5sagit/p5-mst-13.2.git] / t / io / crlf.t
1 #!./perl -w
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = qw(. ../lib);
6 }
7
8 use Config;
9
10 require "test.pl";
11
12 my $file = "crlf$$.dat";
13 END {
14  unlink($file);
15 }
16
17 if (find PerlIO::Layer 'perlio') {
18  plan(tests => 8);
19  ok(open(FOO,">:crlf",$file));
20  ok(print FOO 'a'.((('a' x 14).qq{\n}) x 2000) || close(FOO));
21  ok(open(FOO,"<:crlf",$file));
22
23  my $text;
24  { local $/; $text = <FOO> }
25  is(count_chars($text, "\015\012"), 0);
26  is(count_chars($text, "\n"), 2000);
27
28  binmode(FOO);
29  seek(FOO,0,0);
30  { local $/; $text = <FOO> }
31  is(count_chars($text, "\015\012"), 2000);
32
33  {
34   my $fcontents = join "", map {"$_\r\n"} "a".."zzz";
35   open my $fh, "<:crlf", \$fcontents;
36   local $/ = "xxx";
37   local $_ = <$fh>;
38   my $pos = tell $fh; # pos must be behind "xxx", before "\nyyy\n"
39   seek $fh, $pos, 0;
40   $/ = "\n";
41   $s = <$fh>.<$fh>;
42   ok($s eq "\nxxy\n");
43  }
44
45  ok(close(FOO));
46 }
47 else {
48  skip_all("No perlio, so no :crlf");
49 }
50
51 sub count_chars {
52   my($text, $chars) = @_;
53   my $seen = 0;
54   $seen++ while $text =~ /$chars/g;
55   return $seen;
56 }