New test files for rar5
[rarfile.git] / test / test_seek.py
blob702870417a1f22611d4205cb4a08b1c2d120e709
1 """Test seeking on files.
2 """
4 import io
5 import rarfile
7 from nose.tools import *
9 ARC = 'test/files/seektest.rar'
11 def do_seek(f, pos, lim):
12 ofs = pos*4
13 fsize = lim*4
15 if ofs < 0:
16 exp = 0
17 elif ofs > fsize:
18 exp = fsize
19 else:
20 exp = ofs
22 f.seek(ofs)
24 got = f.tell()
26 eq_(got, exp)
27 ln = f.read(4)
28 if got == fsize and ln:
29 raise Exception('unexpected read')
30 if not ln and got < fsize:
31 raise Exception('unexpected read failure')
32 if ln:
33 spos = int(ln)
34 eq_(spos*4, got)
36 def run_seek(rf, fn):
37 inf = rf.getinfo(fn)
38 cnt = int(inf.file_size / 4)
39 f = rf.open(fn)
41 do_seek(f, int(cnt/2), cnt)
42 do_seek(f, 0, cnt)
44 for i in range(int(cnt/2)):
45 do_seek(f, i*2, cnt)
47 for i in range(cnt):
48 do_seek(f, i*2 - int(cnt / 2), cnt)
50 for i in range(cnt + 10):
51 do_seek(f, cnt - i - 5, cnt)
53 f.close()
55 def run_arc(arc, desc):
56 files = ['stest1.txt', 'stest2.txt']
57 rf = rarfile.RarFile(arc)
58 for fn in files:
59 run_seek(rf, fn)
61 def test_seek_filename():
62 run_arc(ARC, "fn")
64 def test_seek_stringio():
65 data = open(ARC, 'rb').read()
67 # filelike: cStringIO
68 try:
69 import cStringIO
70 run_arc(cStringIO.StringIO(data), "cStringIO")
71 except ImportError:
72 pass
74 # filelike: StringIO
75 try:
76 import StringIO
77 run_arc(StringIO.StringIO(data), "StringIO")
78 except ImportError:
79 pass
81 def test_seek_bytesio():
82 # filelike: io.BytesIO, io.open()
83 data = open(ARC, 'rb').read()
84 run_arc(io.BytesIO(data), "io.BytesIO")
86 def test_seek_open():
87 # filelike: file()
88 run_arc(open(ARC, 'rb'), "open")
90 def test_seek_ioopen():
91 # filelike: io.open()
92 run_arc(io.open(ARC, 'rb'), "io.open")