f343c0d114454e37cdfab5a26228964d005b003f
[p5sagit/p5-mst-13.2.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 => 516;
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 my $tmpfile = 'Op_read.tmp';
29
30 1 while unlink $tmpfile;
31
32 my (@values, @buffers) = ('', '');
33
34 foreach (65, 161) { # , 253, 9786) {
35     push @values, join "", map {chr $_} $_ .. $_ + 4;
36     push @buffers, join "", map {chr $_} $_ + 5 .. $_ + 20;
37 }
38
39 foreach my $value (@values) {
40     foreach my $initial_buffer (@buffers) {
41         my @utf8 = 1;
42         if ($value !~ tr/\0-\377//c) {
43             # It's all 8 bit
44             unshift @utf8, 0;
45         }
46         # foreach my $utf8 (@utf8) {
47         1 while unlink $tmpfile;
48         open FH, ">$tmpfile" or die "Can't open $tmpfile: $!";
49         print FH $value;
50         close FH;
51         foreach my $offset (0, 3, 7, 22, -1, -3, -5, -7) {
52             foreach my $length (0, 2, 5, 10) {
53                 # Will read the lesser of the length of the file and the read
54                 # length
55                 my $will_read = $value;
56                 if ($length < length $will_read) {
57                     substr ($will_read, $length) = '';
58                 }
59                 # Going to trash this so need a copy
60                 my $buffer = $initial_buffer;
61
62                 my $expect = $buffer;
63                 if ($offset > 0) {
64                     # Right pad with NUL bytes
65                     $expect .= "\0" x $offset;
66                     substr ($expect, $offset) = '';
67                 }
68                 substr ($expect, $offset) = $will_read;
69
70                 open FH, $tmpfile or die "Can't open $tmpfile: $!";
71                 printf "# %d into %d l $length o $offset\n",
72                     ord $value, ord $buffer;
73                 $got = read (FH, $buffer, $length, $offset);
74                 is ($got, length $will_read);
75                 is ($buffer, $expect);
76             }
77         }
78         # }
79     }
80 }
81
82
83