Debian Codegraph @

tests/stream/test_stream_0.py

raw symbols SCIP
1 import lz4.stream
2 import sys
3 import pytest
4 if sys.version_info <= (3, 2):
5 import struct
6
7
8 def get_stored_size(buff, block_length_size):
9 if sys.version_info > (2, 7):
10 if isinstance(buff, memoryview):
11 b = buff.tobytes()
12 else:
13 b = bytes(buff)
14 else:
15 b = bytes(buff)
16
17 if len(b) < block_length_size:
18 return None
19
20 if sys.version_info > (3, 2):
21 return int.from_bytes(b[:block_length_size], 'little')
22 else:
23 # This would not work on a memoryview object, hence buff.tobytes call
24 # above
25 fmt = {1: 'B', 2: 'H', 4: 'I', }
26 return struct.unpack('<' + fmt[block_length_size], b[:block_length_size])[0]
27
28
29 def roundtrip(x, c_kwargs, d_kwargs, dictionary):
30 if dictionary:
31 if isinstance(dictionary, tuple):
32 dict_ = x[dictionary[0]:dictionary[1]]
33 else:
34 dict_ = dictionary
35 c_kwargs['dictionary'] = dict_
36 d_kwargs['dictionary'] = dict_
37
38 c = bytes()
39 with lz4.stream.LZ4StreamCompressor(**c_kwargs) as proc:
40 for start in range(0, len(x), c_kwargs['buffer_size']):
41 chunk = x[start:start + c_kwargs['buffer_size']]
42 assert len(chunk) <= c_kwargs['buffer_size']
43 block = proc.compress(chunk)
44 if c_kwargs.get('return_bytearray'):
45 assert isinstance(block, bytearray)
46 if start == 0:
47 c = block
48 else:
49 c += block
50 assert get_stored_size(block, c_kwargs['store_comp_size']) == \
51 (len(block) - c_kwargs['store_comp_size'])
52
53 d = bytes()
54 with lz4.stream.LZ4StreamDecompressor(**d_kwargs) as proc:
55 start = 0
56 while start < len(c):
57 block = proc.get_block(c[start:])
58 chunk = proc.decompress(block)
59 if d_kwargs.get('return_bytearray'):
60 assert isinstance(chunk, bytearray)
61 if start == 0:
62 d = chunk
63 else:
64 d += chunk
65 start += d_kwargs['store_comp_size'] + len(block)
66
67 return d
68
69
70 def setup_kwargs(strategy, mode, buffer_size, store_comp_size,
71 c_return_bytearray=None, d_return_bytearray=None):
72 c_kwargs = {}
73
74 if mode[0] is not None:
75 c_kwargs['mode'] = mode[0]
76 if mode[1] is not None:
77 c_kwargs.update(mode[1])
78
79 c_kwargs['strategy'] = strategy
80 c_kwargs['buffer_size'] = buffer_size
81 c_kwargs.update(store_comp_size)
82
83 if c_return_bytearray:
84 c_kwargs.update(c_return_bytearray)
85
86 d_kwargs = {}
87
88 if d_return_bytearray:
89 d_kwargs.update(d_return_bytearray)
90
91 d_kwargs['strategy'] = strategy
92 d_kwargs['buffer_size'] = buffer_size
93 d_kwargs.update(store_comp_size)
94
95 return (c_kwargs, d_kwargs)
96
97
98 # Test single threaded usage with all valid variations of input
99 @pytest.mark.thread_unsafe
100 def test_1(data, strategy, mode, buffer_size, store_comp_size,
101 c_return_bytearray, d_return_bytearray, dictionary):
102 if buffer_size >= (1 << (8 * store_comp_size['store_comp_size'])):
103 pytest.skip("Invalid case: buffer_size too large for the block length area")
104
105 (c_kwargs, d_kwargs) = setup_kwargs(
106 strategy, mode, buffer_size, store_comp_size, c_return_bytearray, d_return_bytearray)
107
108 d = roundtrip(data, c_kwargs, d_kwargs, dictionary)
109
110 assert d == data
111
112
113 # Test multi threaded:
114 # Not relevant in the lz4.stream case (the process is highly sequential,
115 # and reuse/share the same context from one input chunk to the next one).
116 def test_2(data, strategy, mode, buffer_size, store_comp_size, dictionary): # noqa
117 pass