This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Get t/uni/cache.t working under minitest
[perl5.git] / t / op / sysio.t
CommitLineData
bbce6d69 1#!./perl
2
dfeca2c8 3BEGIN {
c2970a99
NC
4 chdir 't' if -d 't';
5 @INC = '../lib';
6 require './test.pl';
dfeca2c8 7}
bbce6d69 8
dfeca2c8 9plan tests => 48;
bbce6d69 10
c2970a99 11open(I, 'op/sysio.t') || die "sysio.t: cannot find myself: $!";
bbce6d69 12
502257bb
A
13$reopen = ($^O eq 'VMS' ||
14 $^O eq 'os2' ||
15 $^O eq 'MSWin32' ||
16 $^O eq 'NetWare' ||
b5afd346 17 $^O eq 'dos');
9fcfb7d3 18
bbce6d69 19$x = 'abc';
20
21# should not be able to do negative lengths
22eval { sysread(I, $x, -1) };
dfeca2c8 23like($@, qr/^Negative length /);
bbce6d69 24
25# $x should be intact
dfeca2c8 26is($x, 'abc');
bbce6d69 27
28# should not be able to read before the buffer
29eval { sysread(I, $x, 1, -4) };
dd61358a 30like($@, qr/^Offset outside string /);
bbce6d69 31
32# $x should be intact
dfeca2c8 33is($x, 'abc');
bbce6d69 34
35$a ='0123456789';
36
37# default offset 0
dfeca2c8 38is(sysread(I, $a, 3), 3);
bbce6d69 39
40# $a should be as follows
dfeca2c8 41is($a, '#!.');
bbce6d69 42
43# reading past the buffer should zero pad
dfeca2c8 44is(sysread(I, $a, 2, 5), 2);
bbce6d69 45
46# the zero pad should be seen now
dfeca2c8 47is($a, "#!.\0\0/p");
bbce6d69 48
49# try changing the last two characters of $a
dfeca2c8 50is(sysread(I, $a, 3, -2), 3);
bbce6d69 51
52# the last two characters of $a should have changed (into three)
dfeca2c8 53is($a, "#!.\0\0erl");
bbce6d69 54
1ab9acc5 55$outfile = tempfile();
bbce6d69 56
57open(O, ">$outfile") || die "sysio.t: cannot write $outfile: $!";
58
59select(O); $|=1; select(STDOUT);
60
61# cannot write negative lengths
62eval { syswrite(O, $x, -1) };
dfeca2c8 63like($@, qr/^Negative length /);
bbce6d69 64
65# $x still intact
dfeca2c8 66is($x, 'abc');
bbce6d69 67
68# $outfile still intact
dfeca2c8 69ok(!-s $outfile);
bbce6d69 70
71# should not be able to write from after the buffer
3c946528 72eval { syswrite(O, $x, 1, 4) };
dfeca2c8 73like($@, qr/^Offset outside string /);
bbce6d69 74
75# $x still intact
dfeca2c8 76is($x, 'abc');
bbce6d69 77
3c946528
GA
78# but it should be ok to write from the end of the buffer
79syswrite(O, $x, 0, 3);
80syswrite(O, $x, 1, 3);
81
bbce6d69 82# $outfile still intact
9fcfb7d3
CB
83if ($reopen) { # must close file to update EOF marker for stat
84 close O; open(O, ">>$outfile") || die "sysio.t: cannot write $outfile: $!";
85}
dfeca2c8 86ok(!-s $outfile);
bbce6d69 87
88# should not be able to write from before the buffer
89
90eval { syswrite(O, $x, 1, -4) };
dfeca2c8
VP
91like($@, qr/^Offset outside string /);
92
93# $x still intact
94is($x, 'abc');
95
96# $outfile still intact
97if ($reopen) { # must close file to update EOF marker for stat
98 close O; open(O, ">>$outfile") || die "sysio.t: cannot write $outfile: $!";
99}
100ok(!-s $outfile);
101
102# [perl #67912] syswrite prints garbage if called with empty scalar and non-zero offset
3c946528 103eval { my $buf = ''; syswrite(O, $buf, 1, 1) };
dfeca2c8 104like($@, qr/^Offset outside string /);
bbce6d69 105
106# $x still intact
dfeca2c8 107is($x, 'abc');
bbce6d69 108
109# $outfile still intact
9fcfb7d3
CB
110if ($reopen) { # must close file to update EOF marker for stat
111 close O; open(O, ">>$outfile") || die "sysio.t: cannot write $outfile: $!";
112}
dfeca2c8
VP
113ok(!-s $outfile);
114
3c946528 115eval { my $buf = 'x'; syswrite(O, $buf, 1, 2) };
dfeca2c8
VP
116like($@, qr/^Offset outside string /);
117
118# $x still intact
119is($x, 'abc');
120
121# $outfile still intact
122if ($reopen) { # must close file to update EOF marker for stat
123 close O; open(O, ">>$outfile") || die "sysio.t: cannot write $outfile: $!";
124}
125ok(!-s $outfile);
bbce6d69 126
127# default offset 0
502257bb 128if (syswrite(O, $a, 2) == 2){
dfeca2c8 129 pass();
502257bb 130} else {
dfeca2c8
VP
131 diag($!);
132 fail();
502257bb
A
133 # most other tests make no sense after e.g. "No space left on device"
134 die $!;
135}
136
bbce6d69 137
138# $a still intact
dfeca2c8 139is($a, "#!.\0\0erl");
bbce6d69 140
141# $outfile should have grown now
9fcfb7d3
CB
142if ($reopen) { # must close file to update EOF marker for stat
143 close O; open(O, ">>$outfile") || die "sysio.t: cannot write $outfile: $!";
144}
dfeca2c8 145is(-s $outfile, 2);
bbce6d69 146
147# with offset
dfeca2c8 148is(syswrite(O, $a, 2, 5), 2);
bbce6d69 149
150# $a still intact
dfeca2c8 151is($a, "#!.\0\0erl");
bbce6d69 152
153# $outfile should have grown now
9fcfb7d3
CB
154if ($reopen) { # must close file to update EOF marker for stat
155 close O; open(O, ">>$outfile") || die "sysio.t: cannot write $outfile: $!";
156}
dfeca2c8 157is(-s $outfile, 4);
bbce6d69 158
159# with negative offset and a bit too much length
dfeca2c8 160is(syswrite(O, $a, 5, -3), 3);
bbce6d69 161
162# $a still intact
dfeca2c8 163is($a, "#!.\0\0erl");
bbce6d69 164
165# $outfile should have grown now
9fcfb7d3
CB
166if ($reopen) { # must close file to update EOF marker for stat
167 close O; open(O, ">>$outfile") || die "sysio.t: cannot write $outfile: $!";
168}
dfeca2c8 169is(-s $outfile, 7);
bbce6d69 170
b56ec344 171# with implicit length argument
dfeca2c8 172is(syswrite(O, $x), 3);
b56ec344
JH
173
174# $a still intact
dfeca2c8 175is($x, "abc");
b56ec344
JH
176
177# $outfile should have grown now
178if ($reopen) { # must close file to update EOF marker for stat
179 close O; open(O, ">>$outfile") || die "sysio.t: cannot write $outfile: $!";
180}
dfeca2c8
VP
181is(-s $outfile, 10);
182
183close(O);
b56ec344 184
bbce6d69 185open(I, $outfile) || die "sysio.t: cannot read $outfile: $!";
186
187$b = 'xyz';
188
189# reading too much only return as much as available
dfeca2c8
VP
190is(sysread(I, $b, 100), 10);
191
bbce6d69 192# this we should have
dfeca2c8 193is($b, '#!ererlabc');
bbce6d69 194
8903cb82 195# test sysseek
137443ea 196
dfeca2c8 197is(sysseek(I, 2, 0), 2);
137443ea 198sysread(I, $b, 3);
dfeca2c8 199is($b, 'ere');
137443ea 200
dfeca2c8 201is(sysseek(I, -2, 1), 3);
137443ea 202sysread(I, $b, 4);
dfeca2c8
VP
203is($b, 'rerl');
204
205ok(sysseek(I, 0, 0) eq '0 but true');
137443ea 206
dfeca2c8 207ok(not defined sysseek(I, -1, 1));
8903cb82 208
bbce6d69 209close(I);
210
5fd6a578 211unlink_all $outfile;
bbce6d69 212
6aa2f6a7
NC
213# Check that utf8 IO doesn't upgrade the scalar
214open(I, ">$outfile") || die "sysio.t: cannot write $outfile: $!";
215# Will skip harmlessly on stdioperl
216eval {binmode STDOUT, ":utf8"};
217die $@ if $@ and $@ !~ /^IO layers \(like ':utf8'\) unavailable/;
218
219# y diaresis is \w when UTF8
220$a = chr 255;
221
dfeca2c8 222unlike($a, qr/\w/);
6aa2f6a7
NC
223
224syswrite I, $a;
225
226# Should not be upgraded as a side effect of syswrite.
dfeca2c8 227unlike($a, qr/\w/);
6aa2f6a7
NC
228
229# This should work
230eval {syswrite I, 2;};
dfeca2c8 231is($@, '');
6aa2f6a7
NC
232
233close(I);
5fd6a578 234unlink_all $outfile;
6aa2f6a7 235
502257bb 236chdir('..');
8ebc5c01 237
bbce6d69 2381;
239
240# eof