6aab395ac925a06888da8a45752ee333538450e0
[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 => 1732;
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             binmode FH, "utf8" if $utf8;
50             print FH $value;
51             close FH;
52             foreach my $offset (0, 3, 7, 22, -1, -3, -5, -7) {
53                 foreach my $length (0, 2, 5, 10) {
54                     # Will read the lesser of the length of the file and the
55                     # read length
56                     my $will_read = $value;
57                     if ($length < length $will_read) {
58                         substr ($will_read, $length) = '';
59                     }
60                     # Going to trash this so need a copy
61                     my $buffer = $initial_buffer;
62
63                     my $expect = $buffer;
64                     if ($offset > 0) {
65                         # Right pad with NUL bytes
66                         $expect .= "\0" x $offset;
67                         substr ($expect, $offset) = '';
68                     }
69                     substr ($expect, $offset) = $will_read;
70
71                     open FH, $tmpfile or die "Can't open $tmpfile: $!";
72                     binmode FH, "utf8" if $utf8;
73                     my $what = sprintf "%d into %d l $length o $offset",
74                         ord $value, ord $buffer;
75                     $what .= ' u' if $utf8;
76                     $got = read (FH, $buffer, $length, $offset);
77                     is ($got, length $will_read, "got $what");
78                     is ($buffer, $expect, "buffer $what");
79                 }
80             }
81         }
82     }
83 }
84
85
86