1#!./perl
2
3# $RCSfile: read.t,v $$Revision: 1.8 $$Date: 2006/03/28 19:23:13 $
4
5BEGIN {
6    chdir 't';
7    @INC = '../lib';
8    require './test.pl';
9}
10use strict;
11
12plan tests => 2564;
13
14open(FOO,'op/read.t') || open(FOO,'t/op/read.t') || open(FOO,':op:read.t') || die "Can't open op.read";
15seek(FOO,4,0) or die "Seek failed: $!";
16my $buf;
17my $got = read(FOO,$buf,4);
18
19is ($got, 4);
20is ($buf, "perl");
21
22seek (FOO,0,2) || seek(FOO,20000,0);
23$got = read(FOO,$buf,4);
24
25is ($got, 0);
26is ($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.
30my $has_perlio = !eval {
31    no warnings;
32    require Config;
33    !$Config::Config{useperlio}
34};
35
36my $tmpfile = 'Op_read.tmp';
37
38END { 1 while unlink $tmpfile }
39
40my (@values, @buffers) = ('', '');
41
42foreach (65, 161, 253, 9786) {
43    push @values, join "", map {chr $_} $_ .. $_ + 4;
44    push @buffers, join "", map {chr $_} $_ + 5 .. $_ + 20;
45}
46my @offsets = (0, 3, 7, 22, -1, -3, -5, -7);
47my @lengths = (0, 2, 5, 10);
48
49foreach 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
102