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