Newer
Older
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
import zlib
import pytest
from zlib_into import compress_into, decompress_into
def test_compress_into():
buf_size = 5000
data_in = b'abcde' * 5000
buf = bytearray(buf_size)
n_bytes_out = compress_into(data_in, buf)
assert isinstance(n_bytes_out, int)
assert 0 < n_bytes_out < buf_size
# Remaining space in buffer should not have been touched
assert bytes(buf[n_bytes_out:]) == b'\0' * (buf_size - n_bytes_out)
# Roundtrip
assert zlib.decompress(buf[:n_bytes_out]) == data_in
def test_compress_into_err():
buf_size = 5000
data_in = b'abcde' * 5000
buf = bytearray(5000)
with pytest.raises(BufferError):
compress_into(memoryview(data_in)[::2], buf) # Input not contiguous
with pytest.raises(TypeError):
compress_into(data_in, memoryview(buf).toreadonly()) # Output not writable
with pytest.raises(BufferError):
compress_into(data_in, buf[:10]) # Output too small
def test_decompress_into():
expanded_data = b'abcde' * 5000
compressed_data = zlib.compress(expanded_data)
buf = bytearray(len(expanded_data) + 10)
n_bytes_out = decompress_into(compressed_data, buf)
assert isinstance(n_bytes_out, int)
assert n_bytes_out == len(expanded_data)
assert buf[:n_bytes_out] == expanded_data
# Remaining space in buffer should not have been touched
assert bytes(buf[n_bytes_out:]) == b'\0' * (len(buf) - n_bytes_out)
# Exactly the right amount of space
buf2 = bytearray(len(expanded_data))
assert decompress_into(compressed_data, buf2) == len(expanded_data)
# Not enough space, by 1 byte
buf3 = bytearray(len(expanded_data) - 1)
with pytest.raises(BufferError):
decompress_into(compressed_data, buf3)
def test_decompress_into_err():
expanded_data = b'abcde' * 5000
compressed_data = zlib.compress(expanded_data)
buf = bytearray(len(expanded_data) + 10)
with pytest.raises(BufferError):
compress_into(memoryview(compressed_data)[::2], buf) # Input not contiguous
with pytest.raises(TypeError):
compress_into(compressed_data, memoryview(buf).toreadonly()) # Output not writable