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