This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Gisle noted an unused variable
[perl5.git] / t / op / read.t
CommitLineData
a687059c
LW
1#!./perl
2
79072805 3# $RCSfile: read.t,v $$Revision: 4.1 $$Date: 92/08/07 18:28:17 $
a687059c 4
1a24607b
NC
5BEGIN {
6 chdir 't';
7 @INC = '../lib';
8 require './test.pl';
9}
10use strict;
a687059c 11
8c515176 12plan tests => 2564;
a687059c 13
dc459aad 14open(FOO,'op/read.t') || open(FOO,'t/op/read.t') || open(FOO,':op:read.t') || die "Can't open op.read";
1a24607b
NC
15seek(FOO,4,0) or die "Seek failed: $!";
16my $buf;
17my $got = read(FOO,$buf,4);
a687059c 18
1a24607b
NC
19is ($got, 4);
20is ($buf, "perl");
a687059c 21
a0d0e21e 22seek (FOO,0,2) || seek(FOO,20000,0);
a687059c
LW
23$got = read(FOO,$buf,4);
24
1a24607b
NC
25is ($got, 0);
26is ($buf, "");
69938bba 27
39cd9b99
NC
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
69938bba
NC
36my $tmpfile = 'Op_read.tmp';
37
846e3505 38END { 1 while unlink $tmpfile }
69938bba
NC
39
40my (@values, @buffers) = ('', '');
41
8c515176 42foreach (65, 161, 253, 9786) {
69938bba
NC
43 push @values, join "", map {chr $_} $_ .. $_ + 4;
44 push @buffers, join "", map {chr $_} $_ + 5 .. $_ + 20;
45}
39cd9b99
NC
46my @offsets = (0, 3, 7, 22, -1, -3, -5, -7);
47my @lengths = (0, 2, 5, 10);
69938bba
NC
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 }
39cd9b99 56 SKIP:
1dd30107 57 foreach my $utf8 (@utf8) {
39cd9b99
NC
58 skip "Needs :utf8 layer but no perlio", 2 * @offsets * @lengths
59 if $utf8 and !$has_perlio;
60
1dd30107
NC
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;
39cd9b99
NC
66 foreach my $offset (@offsets) {
67 foreach my $length (@lengths) {
1dd30107
NC
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");
846e3505 93 close FH;
69938bba 94 }
69938bba
NC
95 }
96 }
69938bba
NC
97 }
98}
99
100
101