#!./perl
BEGIN {
chdir
't'
if
-d
't'
;
require
'./test.pl'
;
set_up_inc(
'../lib'
);
}
plan
tests
=> 45;
open
(I,
'op/sysio.t'
) ||
die
"sysio.t: cannot find myself: $!"
;
binmode
I;
$reopen
= ($^O eq
'VMS'
||
$^O eq
'os2'
||
$^O eq
'MSWin32'
);
$x
=
'abc'
;
eval
{
sysread
(I,
$x
, -1) };
like($@,
qr/^Negative length /
);
is(
$x
,
'abc'
);
eval
{
sysread
(I,
$x
, 1, -4) };
like($@,
qr/^Offset outside string /
);
is(
$x
,
'abc'
);
$a
=
'0123456789'
;
is(
sysread
(I,
$a
, 3), 3);
is(
$a
,
'#!.'
);
is(
sysread
(I,
$a
, 2, 5), 2);
is(
$a
,
"#!.\0\0/p"
);
is(
sysread
(I,
$a
, 3, -2), 3);
is(
$a
,
"#!.\0\0erl"
);
$outfile
= tempfile();
open
(O,
">$outfile"
) ||
die
"sysio.t: cannot write $outfile: $!"
;
binmode
O;
select
(O); $|=1;
select
(STDOUT);
eval
{
syswrite
(O,
$x
, -1) };
like($@,
qr/^Negative length /
);
is(
$x
,
'abc'
);
ok(!-s
$outfile
);
eval
{
syswrite
(O,
$x
, 1, 4) };
like($@,
qr/^Offset outside string /
);
is(
$x
,
'abc'
);
syswrite
(O,
$x
, 0, 3);
syswrite
(O,
$x
, 1, 3);
if
(
$reopen
) {
close
O;
open
(O,
">>$outfile"
) ||
die
"sysio.t: cannot write $outfile: $!"
;
binmode
O;
}
ok(!-s
$outfile
);
eval
{
syswrite
(O,
$x
, 1, -4) };
like($@,
qr/^Offset outside string /
);
is(
$x
,
'abc'
);
if
(
$reopen
) {
close
O;
open
(O,
">>$outfile"
) ||
die
"sysio.t: cannot write $outfile: $!"
;
binmode
O;
}
ok(!-s
$outfile
);
eval
{
my
$buf
=
''
;
syswrite
(O,
$buf
, 1, 1) };
like($@,
qr/^Offset outside string /
);
is(
$x
,
'abc'
);
if
(
$reopen
) {
close
O;
open
(O,
">>$outfile"
) ||
die
"sysio.t: cannot write $outfile: $!"
;
binmode
O;
}
ok(!-s
$outfile
);
eval
{
my
$buf
=
'x'
;
syswrite
(O,
$buf
, 1, 2) };
like($@,
qr/^Offset outside string /
);
is(
$x
,
'abc'
);
if
(
$reopen
) {
close
O;
open
(O,
">>$outfile"
) ||
die
"sysio.t: cannot write $outfile: $!"
;
binmode
O;
}
ok(!-s
$outfile
);
if
(
syswrite
(O,
$a
, 2) == 2){
pass();
}
else
{
diag($!);
fail();
die
$!;
}
is(
$a
,
"#!.\0\0erl"
);
if
(
$reopen
) {
close
O;
open
(O,
">>$outfile"
) ||
die
"sysio.t: cannot write $outfile: $!"
;
binmode
O;
}
is(-s
$outfile
, 2);
is(
syswrite
(O,
$a
, 2, 5), 2);
is(
$a
,
"#!.\0\0erl"
);
if
(
$reopen
) {
close
O;
open
(O,
">>$outfile"
) ||
die
"sysio.t: cannot write $outfile: $!"
;
binmode
O;
}
is(-s
$outfile
, 4);
is(
syswrite
(O,
$a
, 5, -3), 3);
is(
$a
,
"#!.\0\0erl"
);
if
(
$reopen
) {
close
O;
open
(O,
">>$outfile"
) ||
die
"sysio.t: cannot write $outfile: $!"
;
binmode
O;
}
is(-s
$outfile
, 7);
is(
syswrite
(O,
$x
), 3);
is(
$x
,
"abc"
);
if
(
$reopen
) {
close
O;
open
(O,
">>$outfile"
) ||
die
"sysio.t: cannot write $outfile: $!"
;
binmode
O;
}
is(-s
$outfile
, 10);
close
(O);
open
(I,
$outfile
) ||
die
"sysio.t: cannot read $outfile: $!"
;
binmode
I;
$b
=
'xyz'
;
is(
sysread
(I,
$b
, 100), 10);
is(
$b
,
'#!ererlabc'
);
is(
sysseek
(I, 2, 0), 2);
sysread
(I,
$b
, 3);
is(
$b
,
'ere'
);
is(
sysseek
(I, -2, 1), 3);
sysread
(I,
$b
, 4);
is(
$b
,
'rerl'
);
ok(
sysseek
(I, 0, 0) eq
'0 but true'
);
ok(not
defined
sysseek
(I, -1, 1));
close
(I);
unlink_all
$outfile
;
chdir
(
'..'
);
1;