Skip to content

Commit 3cc76b8

Browse files
committed
Test IO::Handle.seek
Fixes #219 It currently throws when seeking past start of file, but doesn't complain about seeking past end. I left throwage tests commented out for now, will make figuring out the failure modes as part of IO improvement stuff.
1 parent efee15f commit 3cc76b8

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

S32-io/seek.t

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
use v6;
2+
use Test;
3+
4+
plan 3;
5+
6+
constant PATH = 't-S32-io-seek-t.tmp';
7+
LEAVE unlink PATH;
8+
9+
sink my $fh = open PATH, :rw, :bin, :enc<ASCII>;
10+
$fh.print: '1234567890abcdefghijABCDEFGHIJ';
11+
12+
subtest 'SeekFromBeginning' => {
13+
LEAVE $fh.seek: 0, SeekFromBeginning; $fh.seek: 0, SeekFromBeginning;
14+
15+
$fh.seek: 0, SeekFromBeginning;
16+
is-deeply $fh.read(5).decode, '12345', 'seek 0';
17+
$fh.seek: 3, SeekFromBeginning;
18+
is-deeply $fh.read(5).decode, '45678', 'seek 3';
19+
20+
$fh.seek: 10, SeekFromBeginning;
21+
$fh.seek: 20, SeekFromBeginning;
22+
is-deeply $fh.read(5).decode, 'ABCDE', 'two successive seeks';
23+
24+
# TODO XXX Currently throws just past beginning; Should it fail on both?
25+
# throws-like { $fh.seek: -300, SeekFromBeginning }, Exception,
26+
# 'seeking past beginning throws';
27+
#
28+
# throws-like { $fh.seek: 300, SeekFromBeginning }, Exception,
29+
# 'seeking past end throws';
30+
}
31+
32+
subtest 'SeekFromCurrent' => {
33+
LEAVE $fh.seek: 0, SeekFromBeginning; $fh.seek: 0, SeekFromBeginning;
34+
35+
$fh.seek: 10, SeekFromCurrent;
36+
is-deeply $fh.read(5).decode, 'abcde', 'seek 10';
37+
$fh.seek: 5, SeekFromCurrent;
38+
is-deeply $fh.read(5).decode, 'ABCDE', 'read 5, then seek 5';
39+
$fh.seek: -20, SeekFromCurrent;
40+
is-deeply $fh.read(5).decode, '67890', 'negative seek 20';
41+
42+
$fh.seek: 5, SeekFromCurrent;
43+
$fh.seek: 10, SeekFromCurrent;
44+
is-deeply $fh.read(5).decode, 'FGHIJ', 'two successive seeks (pos, pos)';
45+
46+
$fh.seek: -15, SeekFromCurrent;
47+
$fh.seek: 5, SeekFromCurrent;
48+
is-deeply $fh.read(5).decode, 'ABCDE', 'two successive seeks (neg, pos)';
49+
50+
$fh.seek: -5, SeekFromCurrent;
51+
$fh.seek: -10, SeekFromCurrent;
52+
is-deeply $fh.read(5).decode, 'abcde', 'two successive seeks (neg, neg)';
53+
54+
# TODO XXX Currently throws just past beginning; Should it fail on both?
55+
# throws-like { $fh.seek: -300, SeekFromCurrent }, Exception,
56+
# 'seeking past beginning throws';
57+
58+
# throws-like {
59+
# $fh.seek: 0, SeekFromBeginning;
60+
# $fh.seek: 300, SeekFromCurrent;
61+
# }, Exception, 'seeking past end throws';
62+
}
63+
64+
subtest 'SeekFromEnd' => {
65+
LEAVE $fh.seek: 0, SeekFromBeginning; $fh.seek: 0, SeekFromBeginning;
66+
67+
$fh.seek: -5, SeekFromEnd;
68+
is-deeply $fh.read(5).decode, 'FGHIJ', 'seek -5';
69+
$fh.seek: -30, SeekFromEnd;
70+
is-deeply $fh.read(5).decode, '12345', 'seek -30';
71+
72+
$fh.seek: -5, SeekFromEnd;
73+
$fh.seek: -10, SeekFromEnd;
74+
is-deeply $fh.read(5).decode, 'ABCDE', 'two successive seeks';
75+
76+
# TODO XXX Currently throws just past beginning; Should it fail on both?
77+
# throws-like { $fh.seek: -300, SeekFromEnd }, Exception,
78+
# 'seeking past beginning throws';
79+
80+
# throws-like { $fh.seek: 300, SeekFromEnd }, Exception,
81+
# 'seeking past end throws';
82+
}
83+
84+
# vim: ft=perl6

0 commit comments

Comments
 (0)