This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
99a62166ced80ab4b0030120694257c36df2a6e1
[perl5.git] / t / op / read.t
1 #!./perl
2
3 # $RCSfile: read.t,v $$Revision: 4.1 $$Date: 92/08/07 18:28:17 $
4
5 BEGIN {
6     chdir 't';
7     @INC = '../lib';
8     require './test.pl';
9 }
10 use strict;
11
12 plan tests => 2564;
13
14 open(FOO,'op/read.t') || open(FOO,'t/op/read.t') || open(FOO,':op:read.t') || die "Can't open op.read";
15 seek(FOO,4,0) or die "Seek failed: $!";
16 my $buf;
17 my $got = read(FOO,$buf,4);
18
19 is ($got, 4);
20 is ($buf, "perl");
21
22 seek (FOO,0,2) || seek(FOO,20000,0);
23 $got = read(FOO,$buf,4);
24
25 is ($got, 0);
26 is ($buf, "");
27
28 # This is true if Config is not built, or if PerlIO is enabled
29 # ie assume that PerlIO is present, unless we know for sure otherwise.
30 my $has_perlio = !eval {
31     no warnings;
32     require Config;
33     !$Config::Config{useperlio}
34 };
35
36 my $tmpfile = 'Op_read.tmp';
37
38 END { 1 while unlink $tmpfile }
39
40 my (@values, @buffers) = ('', '');
41
42 foreach (65, 161, 253, 9786) {
43     push @values, join "", map {chr $_} $_ .. $_ + 4;
44     push @buffers, join "", map {chr $_} $_ + 5 .. $_ + 20;
45 }
46 my @offsets = (0, 3, 7, 22, -1, -3, -5, -7);
47 my @lengths = (0, 2, 5, 10);
48
49 foreach my $value (@values) {
50     foreach my $initial_buffer (@buffers) {
51         my @utf8 = 1;
52         if ($value !~ tr/\0-\377//c) {
53             # It's all 8 bit
54             unshift @utf8, 0;
55         }
56       SKIP:
57         foreach my $utf8 (@utf8) {
58             skip "Needs :utf8 layer but no perlio", 2 * @offsets * @lengths
59               if $utf8 and !$has_perlio;
60
61             1 while unlink $tmpfile;
62             open FH, ">$tmpfile" or die "Can't open $tmpfile: $!";
63             binmode FH, "utf8" if $utf8;
64             print FH $value;
65             close FH;
66             foreach my $offset (@offsets) {
67                 foreach my $length (@lengths) {
68                     # Will read the lesser of the length of the file and the
69                     # read length
70                     my $will_read = $value;
71                     if ($length < length $will_read) {
72                         substr ($will_read, $length) = '';
73                     }
74                     # Going to trash this so need a copy
75                     my $buffer = $initial_buffer;
76
77                     my $expect = $buffer;
78                     if ($offset > 0) {
79                         # Right pad with NUL bytes
80                         $expect .= "\0" x $offset;
81                         substr ($expect, $offset) = '';
82                     }
83                     substr ($expect, $offset) = $will_read;
84
85                     open FH, $tmpfile or die "Can't open $tmpfile: $!";
86                     binmode FH, "utf8" if $utf8;
87                     my $what = sprintf "%d into %d l $length o $offset",
88                         ord $value, ord $buffer;
89                     $what .= ' u' if $utf8;
90                     $got = read (FH, $buffer, $length, $offset);
91                     is ($got, length $will_read, "got $what");
92                     is ($buffer, $expect, "buffer $what");
93                     close FH;
94                 }
95             }
96         }
97     }
98 }
99
100
101