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