This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
084be211fd6e08fa7e6d46c71983b9b4893e18f2
[perl5.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  SKIP:
34  {
35   eval 'use PerlIO::scalar';
36   skip(q/miniperl cannnot load PerlIO::scalar/)
37       if $@ =~ /dynamic loading not available/;
38   my $fcontents = join "", map {"$_\015\012"} "a".."zzz";
39   open my $fh, "<:crlf", \$fcontents;
40   local $/ = "xxx";
41   local $_ = <$fh>;
42   my $pos = tell $fh; # pos must be behind "xxx", before "\nyyy\n"
43   seek $fh, $pos, 0;
44   $/ = "\n";
45   $s = <$fh>.<$fh>;
46   ok($s eq "\nxxy\n");
47  }
48
49  ok(close(FOO));
50 }
51 else {
52  skip_all("No perlio, so no :crlf");
53 }
54
55 sub count_chars {
56   my($text, $chars) = @_;
57   my $seen = 0;
58   $seen++ while $text =~ /$chars/g;
59   return $seen;
60 }