Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_roundtrip_multiframe_3(data):
nframes = 4
compressed = b''
ctx = lz4frame.create_compression_context()
for _ in range(nframes):
compressed += lz4frame.compress_begin(ctx)
compressed += lz4frame.compress_chunk(ctx, data)
compressed += lz4frame.compress_flush(ctx)
decompressed = b''
ctx = lz4frame.create_decompression_context()
for _ in range(nframes):
d, bytes_read, eof = lz4frame.decompress_chunk(ctx, compressed)
decompressed += d
assert eof == True
assert bytes_read == len(compressed) // nframes
assert len(decompressed) == nframes * len(data)
assert data * nframes == decompressed
def test_decompress_chunk_return_type_4():
c = lz4frame.compress(b'', return_bytearray=False)
d = lz4frame.create_decompression_context()
r = lz4frame.decompress_chunk(
d,
c,
return_bytearray=True,
)
assert isinstance(r, tuple)
assert isinstance(r[0], bytearray)
assert isinstance(r[1], int)
assert isinstance(r[2], bool)
def test_decompress_chunk_return_type_1():
c = lz4frame.compress(b'', return_bytearray=False)
d = lz4frame.create_decompression_context()
r, b, e = lz4frame.decompress_chunk(
d,
c,
return_bytearray=False,
)
assert isinstance(r, bytes)
assert isinstance(b, int)
assert isinstance(e, bool)
def test_decompress_chunk_return_type_2():
c = lz4frame.compress(b'', return_bytearray=False)
d = lz4frame.create_decompression_context()
r, b, e = lz4frame.decompress_chunk(
d,
c,
return_bytearray=True,
)
assert isinstance(r, bytearray)
assert isinstance(b, int)
assert isinstance(e, bool)
def test_frame_decompress_chunk_mem_usage(data):
tracemalloc = pytest.importorskip('tracemalloc')
tracemalloc.start()
compressed = lz4.frame.compress(data)
prev_snapshot = None
for i in range(1000):
context = lz4.frame.create_decompression_context()
decompressed = lz4.frame.decompress_chunk( # noqa: F841
context, compressed
)
if i % 100 == 0:
gc.collect()
snapshot = tracemalloc.take_snapshot()
if prev_snapshot:
stats = snapshot.compare_to(prev_snapshot, 'lineno')
assert stats[0].size_diff < MEM_INCREASE_LIMIT
prev_snapshot = snapshot
def test_decompress_chunk_return_type_3():
c = lz4frame.compress(b'', return_bytearray=False)
d = lz4frame.create_decompression_context()
r = lz4frame.decompress_chunk(
d,
c,
return_bytearray=False,
)
assert isinstance(r, tuple)
assert isinstance(r[0], bytes)
assert isinstance(r[1], int)
assert isinstance(r[2], bool)
def test_reset_decompression_context_2():
if lz4.library_version_number() >= 10800:
c = lz4frame.compress(b'1234', return_bytearray=False)
context = lz4frame.create_decompression_context()
try:
# Simulate an error by passing junk to decompress
d = lz4frame.decompress_chunk(context, c[1:3])
except:
pass
r = lz4frame.reset_decompression_context(context)
assert r is None
# And confirm we can use the context after reset
d, bytes_read, eof = lz4frame.decompress_chunk(context, c)
assert d == b'1234'
assert bytes_read == len(c)
assert eof is True
else:
pass
len(data),
store_size,
block_size,
block_linked,
content_checksum,
block_checksum,
)
d_context = lz4frame.create_decompression_context()
compressed_in = get_chunked(compressed, d_chunks)
decompressed = b''
bytes_read = 0
eofs = []
try:
while True:
d, b, e = lz4frame.decompress_chunk(
d_context,
next(compressed_in),
)
decompressed += d
bytes_read += b
eofs.append(e)
except StopIteration:
pass
finally:
del compressed_in
assert bytes_read == len(compressed)
assert decompressed == data
assert eofs[-1] == True
assert (True in eofs[:-2]) == False