Commit | Line | Data |
a687059c |
1 | #!./perl |
2 | |
1a24607b |
3 | BEGIN { |
4 | chdir 't'; |
5 | @INC = '../lib'; |
6 | require './test.pl'; |
7 | } |
8 | use strict; |
a687059c |
9 | |
8c515176 |
10 | plan tests => 2564; |
a687059c |
11 | |
dc459aad |
12 | open(FOO,'op/read.t') || open(FOO,'t/op/read.t') || open(FOO,':op:read.t') || die "Can't open op.read"; |
1a24607b |
13 | seek(FOO,4,0) or die "Seek failed: $!"; |
14 | my $buf; |
15 | my $got = read(FOO,$buf,4); |
a687059c |
16 | |
1a24607b |
17 | is ($got, 4); |
18 | is ($buf, "perl"); |
a687059c |
19 | |
a0d0e21e |
20 | seek (FOO,0,2) || seek(FOO,20000,0); |
a687059c |
21 | $got = read(FOO,$buf,4); |
22 | |
1a24607b |
23 | is ($got, 0); |
24 | is ($buf, ""); |
69938bba |
25 | |
39cd9b99 |
26 | # This is true if Config is not built, or if PerlIO is enabled |
27 | # ie assume that PerlIO is present, unless we know for sure otherwise. |
28 | my $has_perlio = !eval { |
29 | no warnings; |
30 | require Config; |
31 | !$Config::Config{useperlio} |
32 | }; |
33 | |
1c25d394 |
34 | my $tmpfile = tempfile(); |
69938bba |
35 | |
36 | my (@values, @buffers) = ('', ''); |
37 | |
8c515176 |
38 | foreach (65, 161, 253, 9786) { |
69938bba |
39 | push @values, join "", map {chr $_} $_ .. $_ + 4; |
40 | push @buffers, join "", map {chr $_} $_ + 5 .. $_ + 20; |
41 | } |
39cd9b99 |
42 | my @offsets = (0, 3, 7, 22, -1, -3, -5, -7); |
43 | my @lengths = (0, 2, 5, 10); |
69938bba |
44 | |
45 | foreach my $value (@values) { |
46 | foreach my $initial_buffer (@buffers) { |
47 | my @utf8 = 1; |
48 | if ($value !~ tr/\0-\377//c) { |
49 | # It's all 8 bit |
50 | unshift @utf8, 0; |
51 | } |
39cd9b99 |
52 | SKIP: |
1dd30107 |
53 | foreach my $utf8 (@utf8) { |
39cd9b99 |
54 | skip "Needs :utf8 layer but no perlio", 2 * @offsets * @lengths |
55 | if $utf8 and !$has_perlio; |
56 | |
1dd30107 |
57 | open FH, ">$tmpfile" or die "Can't open $tmpfile: $!"; |
58 | binmode FH, "utf8" if $utf8; |
59 | print FH $value; |
60 | close FH; |
39cd9b99 |
61 | foreach my $offset (@offsets) { |
62 | foreach my $length (@lengths) { |
1dd30107 |
63 | # Will read the lesser of the length of the file and the |
64 | # read length |
65 | my $will_read = $value; |
66 | if ($length < length $will_read) { |
67 | substr ($will_read, $length) = ''; |
68 | } |
69 | # Going to trash this so need a copy |
70 | my $buffer = $initial_buffer; |
71 | |
72 | my $expect = $buffer; |
73 | if ($offset > 0) { |
74 | # Right pad with NUL bytes |
75 | $expect .= "\0" x $offset; |
76 | substr ($expect, $offset) = ''; |
77 | } |
78 | substr ($expect, $offset) = $will_read; |
79 | |
80 | open FH, $tmpfile or die "Can't open $tmpfile: $!"; |
81 | binmode FH, "utf8" if $utf8; |
82 | my $what = sprintf "%d into %d l $length o $offset", |
83 | ord $value, ord $buffer; |
84 | $what .= ' u' if $utf8; |
85 | $got = read (FH, $buffer, $length, $offset); |
86 | is ($got, length $will_read, "got $what"); |
87 | is ($buffer, $expect, "buffer $what"); |
846e3505 |
88 | close FH; |
69938bba |
89 | } |
69938bba |
90 | } |
91 | } |
69938bba |
92 | } |
93 | } |
94 | |
95 | |
96 | |