-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_streams.py
360 lines (280 loc) · 11.4 KB
/
test_streams.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import sys
import array
import functools
import io
import os
import unittest
from heatshrink.streams import EncodedFile
from .constants import TEXT, COMPRESSED
from .utils import TestUtilsMixin, random_string
TEST_FILENAME = 'test_{}_tmp'.format(os.getpid())
PY3 = sys.version_info[0] == 3
if PY3:
def bchr(s):
return bytes([s])
else:
def bchr(s):
return s
class EncodedFileTest(TestUtilsMixin, unittest.TestCase):
def setUp(self):
self.fp = EncodedFile(TEST_FILENAME, 'wb')
def tearDown(self):
if self.fp:
self.fp.close()
# Cleanup temporary file
if os.path.exists(TEST_FILENAME):
os.unlink(TEST_FILENAME)
def test_bad_args(self):
self.assertRaises(TypeError, EncodedFile, None)
self.assertRaises(ValueError, EncodedFile, None, mode='eb')
fp = self.assertNotRaises(EncodedFile, TEST_FILENAME, invalid_param=True)
fp.close()
def test_open_missing_file(self):
self.assertRaises(IOError, EncodedFile, 'does_not_exist.txt')
def test_mode_attribute_is_readonly(self):
self.assertEqual(self.fp.mode, 'wb')
with self.assertRaises(AttributeError):
self.fp.mode = 'rb'
def test_different_compress_params(self):
# List of a tuple containing (window_sz2, lookahead_sz2)
encode_params = [
(8, 3),
(11, 6),
(4, 3),
(15, 9),
]
encoded = []
for (window_sz2, lookahead_sz2) in encode_params:
kwargs = {
'window_sz2': window_sz2,
'lookahead_sz2': lookahead_sz2
}
with io.BytesIO() as dst:
with EncodedFile(dst, 'wb', **kwargs) as fp:
fp.write(TEXT)
encoded.append(dst.getvalue())
# Ensure that all have different values
self.assertEqual(len(encoded), len(set(encoded)))
def test_invalid_modes(self):
data = io.BytesIO()
for mode in ['a+', 'w+', 'ab', 'r+', 'U', 'x', 'xb']:
with self.assertRaisesRegexp(ValueError, '^Invalid mode: .*$'):
EncodedFile(data, mode=mode)
def test_round_trip(self):
write_str = b'Testing\nAnd Stuff'
self.fp.write(write_str)
self.fp.close()
self.fp = EncodedFile(TEST_FILENAME)
self.assertEqual(self.fp.read(), write_str)
def test_with_large_files(self):
test_sizes = [1000, 10000, 100000]
for size in test_sizes:
contents = random_string(size)
contents = contents.encode('ascii')
with EncodedFile(TEST_FILENAME, mode='wb') as fp:
fp.write(contents)
with EncodedFile(TEST_FILENAME) as fp:
read_str = fp.read()
if read_str != contents:
msg = ('Decoded and original file contents '
'do not match for size: {}')
self.fail(msg.format(size))
def test_buffered_large_files(self):
test_sizes = [1000, 10000, 100000]
for size in test_sizes:
contents = random_string(size)
contents = contents.encode('ascii')
with EncodedFile(TEST_FILENAME, mode='wb') as fp:
fp.write(contents)
with EncodedFile(TEST_FILENAME) as fp:
# Read small buffer sizes
read_func = functools.partial(fp.read, 512)
read_str = b''.join([s for s in iter(read_func, b'')])
if read_str != contents:
msg = ('Decoded and original file contents '
'do not match for size: {}')
self.fail(msg.format(size))
def test_with_file_object(self):
plain_file = open(TEST_FILENAME, 'wb')
with EncodedFile(plain_file, mode='wb') as encoded_file:
encoded_file.write(TEXT)
self.assertTrue(encoded_file.closed)
# Shouldn't close the file, as it doesn't own it
self.assertFalse(plain_file.closed)
plain_file.close()
with open(TEST_FILENAME, 'rb') as fp:
self.assertTrue(len(fp.read()) > 0)
def test_closed_true_when_file_closed(self):
self.assertFalse(self.fp.closed)
self.fp.close()
self.assertTrue(self.fp.closed)
def test_context_manager(self):
with EncodedFile(TEST_FILENAME, mode='wb') as fp:
fp.write(b'Testing\n')
fp.write(b'One, two...')
self.assertTrue(fp.closed)
def test_operations_on_closed_file(self):
self.fp.close()
self.assertRaises(ValueError, self.fp.write, b'abcde')
self.assertRaises(ValueError, self.fp.seek, 0)
self.fp = EncodedFile(TEST_FILENAME, 'rb')
self.fp.close()
self.assertRaises(ValueError, self.fp.read)
self.assertRaises(ValueError, self.fp.seek, 0)
def test_cannot_write_in_read_mode(self):
# Write some junk data
self.fp.write(b'abcde')
self.fp.close()
self.fp = EncodedFile(TEST_FILENAME)
self.assertTrue(self.fp.readable())
self.assertFalse(self.fp.writable())
self.assertRaises(IOError, self.fp.write, b'abcde')
def test_cannot_read_in_write_mode(self):
self.assertTrue(self.fp.writable())
self.assertFalse(self.fp.readable())
self.assertRaises(IOError, self.fp.read)
#################
# Seeking
#################
def test_seeking_forwards(self):
contents = TEXT
with EncodedFile(io.BytesIO(COMPRESSED)) as fp:
self.assertEqual(fp.read(100), contents[:100])
fp.seek(150) # Move 50 forwards
self.assertEqual(fp.read(100), contents[150:250])
def test_seeking_backwards(self):
with EncodedFile(io.BytesIO(COMPRESSED)) as fp:
contents = fp.read(100)
fp.seek(0)
self.assertEqual(fp.read(100), contents)
def test_seeking_forward_from_current(self):
contents = TEXT
with EncodedFile(io.BytesIO(COMPRESSED)) as fp:
self.assertEqual(fp.read(100), contents[:100])
fp.seek(50, io.SEEK_CUR) # Move 50 forwards
self.assertEqual(fp.read(100), contents[150:250])
def test_seeking_backwards_from_current(self):
with EncodedFile(io.BytesIO(COMPRESSED)) as fp:
contents = fp.read()
fp.seek(-100, io.SEEK_CUR)
self.assertEqual(fp.read(), contents[-100:])
def test_seeking_beyond_beginning_from_current(self):
with EncodedFile(io.BytesIO(COMPRESSED)) as fp:
self.assertRaises(IOError, fp.seek, -100, io.SEEK_CUR)
def test_seeking_from_end(self):
with EncodedFile(io.BytesIO(COMPRESSED)) as fp:
self.assertEqual(fp.read(100), TEXT[:100])
seeked_pos = fp.seek(-100, io.SEEK_END)
self.assertEqual(seeked_pos, len(TEXT) - 100)
self.assertEqual(fp.read(100), TEXT[-100:])
def test_seeking_from_end_beyond_beginning(self):
with EncodedFile(io.BytesIO(COMPRESSED)) as fp:
# Go to end to get size
size = fp.seek(0, io.SEEK_END)
# Go to beginning
self.assertNotRaises(fp.seek, -size, io.SEEK_END)
# One before beginning
self.assertRaises(IOError, fp.seek, -size - 1, io.SEEK_END)
def test_tell(self):
with io.BytesIO() as dst:
with EncodedFile(dst, mode='wb') as fp:
bytes_written = fp.write(b'abcde')
self.assertEqual(fp.tell(), bytes_written)
dst.seek(0) # Reset
with EncodedFile(dst) as fp:
fp.read(3)
self.assertEqual(fp.tell(), 3)
def test_peek(self):
with EncodedFile(io.BytesIO(COMPRESSED)) as fp:
pdata = fp.peek()
self.assertNotEqual(len(pdata), 0)
self.assertTrue(TEXT.startswith(pdata))
self.assertEqual(fp.read(), TEXT)
#################
# Reading
#################
def test_read_whole_file(self):
with EncodedFile(io.BytesIO(COMPRESSED)) as fp:
self.assertEqual(fp.read(), TEXT)
def test_read_buffered(self):
READ_SIZE = 128
offset = 0
with EncodedFile(io.BytesIO(COMPRESSED)) as fp:
read_buf = functools.partial(fp.read, READ_SIZE)
for i, contents in enumerate(iter(read_buf, b'')):
offset = READ_SIZE * i
self.assertEqual(
contents,
TEXT[offset: offset + READ_SIZE]
)
def test_read_one_char(self):
with EncodedFile(io.BytesIO(COMPRESSED)) as fp:
for c in TEXT:
self.assertEqual(fp.read(1), bchr(c))
def test_read1(self):
with EncodedFile(io.BytesIO(COMPRESSED)) as fp:
blocks = [buf for buf in iter(fp.read1, b'')]
self.assertEqual(b''.join(blocks), TEXT)
self.assertEqual(fp.read1(), b'')
def test_read1_0(self):
with EncodedFile(io.BytesIO(COMPRESSED)) as fp:
self.assertEqual(fp.read1(0), b'')
def test_readinto(self):
with io.BytesIO() as dst:
with EncodedFile(dst, mode='wb') as fp:
fp.write(b'abcde')
dst.seek(0) # Reset
with EncodedFile(dst) as fp:
a = array.array('b', b'x' * 10) # Fill with junk
n = fp.readinto(a)
try:
# Python 3
self.assertEqual(b'abcde', a.tobytes()[:n])
except AttributeError:
self.assertEqual(b'abcde', a.tostring()[:n])
def test_readline(self):
with EncodedFile(io.BytesIO(COMPRESSED)) as fp:
lines = TEXT.splitlines()
# Could also use zip
for i, line in enumerate(iter(fp.readline, b'')):
self.assertEqual(line, lines[i] + b'\n')
def test_readline_iterator(self):
with EncodedFile(io.BytesIO(COMPRESSED)) as fp:
lines = TEXT.splitlines()
for file_line, original_line in zip(fp, lines):
self.assertEqual(file_line, original_line + b'\n')
def test_readlines(self):
with EncodedFile(io.BytesIO(COMPRESSED)) as fp:
lines = fp.readlines()
self.assertEqual(b''.join(lines), TEXT)
#################
# Writing
#################
def test_write_buffered(self):
BUFFER_SIZE = 16
# BytesIO makes it easy to buffer
text_buf = io.BytesIO(TEXT)
with io.BytesIO() as dst:
with EncodedFile(dst, mode='wb') as fp:
while True:
chunk = text_buf.read(BUFFER_SIZE)
if not chunk:
break
fp.write(chunk)
self.assertEqual(dst.getvalue(), COMPRESSED)
def test_remaining_data_flushed_on_close(self):
with io.BytesIO() as dst:
fp = EncodedFile(dst, mode='wb')
fp.write(TEXT)
# Not flusshed
self.assertEqual(len(dst.getvalue()), 0)
# Flush
fp.close()
self.assertTrue(len(dst.getvalue()) > 0)
def test_writelines(self):
with io.BytesIO(TEXT) as fp:
lines = fp.readlines()
with io.BytesIO() as dst:
with EncodedFile(dst, mode='wb') as fp:
fp.writelines(lines)
self.assertEqual(dst.getvalue(), COMPRESSED)