This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
some WinCE compilers require a little correction
[perl5.git] / t / op / sysio.t
CommitLineData
bbce6d69 1#!./perl
2
b56ec344 3print "1..39\n";
bbce6d69 4
bfb65faf 5chdir('op') || chdir('t/op') || die "sysio.t: cannot look for myself: $!";
bbce6d69 6
7open(I, 'sysio.t') || die "sysio.t: cannot find myself: $!";
8
502257bb
A
9$reopen = ($^O eq 'VMS' ||
10 $^O eq 'os2' ||
11 $^O eq 'MSWin32' ||
12 $^O eq 'NetWare' ||
13 $^O eq 'dos' ||
57af3f31 14 $^O eq 'mpeix');
9fcfb7d3 15
bbce6d69 16$x = 'abc';
17
18# should not be able to do negative lengths
19eval { sysread(I, $x, -1) };
20print 'not ' unless ($@ =~ /^Negative length /);
21print "ok 1\n";
22
23# $x should be intact
24print 'not ' unless ($x eq 'abc');
25print "ok 2\n";
26
27# should not be able to read before the buffer
28eval { sysread(I, $x, 1, -4) };
29print 'not ' unless ($x eq 'abc');
30print "ok 3\n";
31
32# $x should be intact
33print 'not ' unless ($x eq 'abc');
34print "ok 4\n";
35
36$a ='0123456789';
37
38# default offset 0
39print 'not ' unless(sysread(I, $a, 3) == 3);
40print "ok 5\n";
41
42# $a should be as follows
43print 'not ' unless ($a eq '#!.');
44print "ok 6\n";
45
46# reading past the buffer should zero pad
47print 'not ' unless(sysread(I, $a, 2, 5) == 2);
48print "ok 7\n";
49
50# the zero pad should be seen now
51print 'not ' unless ($a eq "#!.\0\0/p");
52print "ok 8\n";
53
54# try changing the last two characters of $a
55print 'not ' unless(sysread(I, $a, 3, -2) == 3);
56print "ok 9\n";
57
58# the last two characters of $a should have changed (into three)
59print 'not ' unless ($a eq "#!.\0\0erl");
60print "ok 10\n";
61
62$outfile = 'sysio.out';
63
64open(O, ">$outfile") || die "sysio.t: cannot write $outfile: $!";
65
66select(O); $|=1; select(STDOUT);
67
68# cannot write negative lengths
69eval { syswrite(O, $x, -1) };
70print 'not ' unless ($@ =~ /^Negative length /);
71print "ok 11\n";
72
73# $x still intact
74print 'not ' unless ($x eq 'abc');
75print "ok 12\n";
76
77# $outfile still intact
78print 'not ' if (-s $outfile);
79print "ok 13\n";
80
81# should not be able to write from after the buffer
82eval { syswrite(O, $x, 1, 3) };
83print 'not ' unless ($@ =~ /^Offset outside string /);
84print "ok 14\n";
85
86# $x still intact
87print 'not ' unless ($x eq 'abc');
88print "ok 15\n";
89
90# $outfile still intact
9fcfb7d3
CB
91if ($reopen) { # must close file to update EOF marker for stat
92 close O; open(O, ">>$outfile") || die "sysio.t: cannot write $outfile: $!";
93}
bbce6d69 94print 'not ' if (-s $outfile);
95print "ok 16\n";
96
97# should not be able to write from before the buffer
98
99eval { syswrite(O, $x, 1, -4) };
100print 'not ' unless ($@ =~ /^Offset outside string /);
101print "ok 17\n";
102
103# $x still intact
104print 'not ' unless ($x eq 'abc');
105print "ok 18\n";
106
107# $outfile still intact
9fcfb7d3
CB
108if ($reopen) { # must close file to update EOF marker for stat
109 close O; open(O, ">>$outfile") || die "sysio.t: cannot write $outfile: $!";
110}
bbce6d69 111print 'not ' if (-s $outfile);
112print "ok 19\n";
113
114# default offset 0
502257bb
A
115if (syswrite(O, $a, 2) == 2){
116 print "ok 20\n";
117} else {
118 print "# $!\nnot ok 20\n";
119 # most other tests make no sense after e.g. "No space left on device"
120 die $!;
121}
122
bbce6d69 123
124# $a still intact
125print 'not ' unless ($a eq "#!.\0\0erl");
126print "ok 21\n";
127
128# $outfile should have grown now
9fcfb7d3
CB
129if ($reopen) { # must close file to update EOF marker for stat
130 close O; open(O, ">>$outfile") || die "sysio.t: cannot write $outfile: $!";
131}
bbce6d69 132print 'not ' unless (-s $outfile == 2);
133print "ok 22\n";
134
135# with offset
136print 'not ' unless (syswrite(O, $a, 2, 5) == 2);
137print "ok 23\n";
138
139# $a still intact
140print 'not ' unless ($a eq "#!.\0\0erl");
141print "ok 24\n";
142
143# $outfile should have grown now
9fcfb7d3
CB
144if ($reopen) { # must close file to update EOF marker for stat
145 close O; open(O, ">>$outfile") || die "sysio.t: cannot write $outfile: $!";
146}
bbce6d69 147print 'not ' unless (-s $outfile == 4);
148print "ok 25\n";
149
150# with negative offset and a bit too much length
151print 'not ' unless (syswrite(O, $a, 5, -3) == 3);
152print "ok 26\n";
153
154# $a still intact
155print 'not ' unless ($a eq "#!.\0\0erl");
156print "ok 27\n";
157
158# $outfile should have grown now
9fcfb7d3
CB
159if ($reopen) { # must close file to update EOF marker for stat
160 close O; open(O, ">>$outfile") || die "sysio.t: cannot write $outfile: $!";
161}
bbce6d69 162print 'not ' unless (-s $outfile == 7);
163print "ok 28\n";
164
b56ec344
JH
165# with implicit length argument
166print 'not ' unless (syswrite(O, $x) == 3);
167print "ok 29\n";
168
169# $a still intact
170print 'not ' unless ($x eq "abc");
171print "ok 30\n";
172
173# $outfile should have grown now
174if ($reopen) { # must close file to update EOF marker for stat
175 close O; open(O, ">>$outfile") || die "sysio.t: cannot write $outfile: $!";
176}
177print 'not ' unless (-s $outfile == 10);
178print "ok 31\n";
179
bbce6d69 180close(O);
181
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
JH
187print 'not ' unless (sysread(I, $b, 100) == 10);
188print "ok 32\n";
bbce6d69 189# this we should have
b56ec344
JH
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
502257bb 216chdir('..');
8ebc5c01 217
bbce6d69 2181;
219
220# eof