test_frame_2.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import lz4.frame as lz4frame
  2. import pytest
  3. import os
  4. import sys
  5. from . helpers import (
  6. get_chunked,
  7. get_frame_info_check,
  8. )
  9. test_data = [
  10. (b'', 1, 1),
  11. (os.urandom(8 * 1024), 8, 1),
  12. (os.urandom(8 * 1024), 1, 8),
  13. (b'0' * 8 * 1024, 8, 1),
  14. (b'0' * 8 * 1024, 8, 1),
  15. (bytearray(b''), 1, 1),
  16. (bytearray(os.urandom(8 * 1024)), 8, 1),
  17. ]
  18. if sys.version_info > (2, 7):
  19. test_data += [
  20. (memoryview(b''), 1, 1),
  21. (memoryview(os.urandom(8 * 1024)), 8, 1)
  22. ]
  23. @pytest.fixture(
  24. params=test_data,
  25. ids=[
  26. 'data' + str(i) for i in range(len(test_data))
  27. ]
  28. )
  29. def data(request):
  30. return request.param
  31. def test_roundtrip_chunked(data, block_size, block_linked,
  32. content_checksum, block_checksum,
  33. compression_level,
  34. auto_flush, store_size):
  35. data, c_chunks, d_chunks = data
  36. c_context = lz4frame.create_compression_context()
  37. kwargs = {}
  38. kwargs['compression_level'] = compression_level
  39. kwargs['block_size'] = block_size
  40. kwargs['block_linked'] = block_linked
  41. kwargs['content_checksum'] = content_checksum
  42. kwargs['block_checksum'] = block_checksum
  43. kwargs['auto_flush'] = auto_flush
  44. if store_size is True:
  45. kwargs['source_size'] = len(data)
  46. compressed = lz4frame.compress_begin(
  47. c_context,
  48. **kwargs
  49. )
  50. data_in = get_chunked(data, c_chunks)
  51. try:
  52. while True:
  53. compressed += lz4frame.compress_chunk(
  54. c_context,
  55. next(data_in)
  56. )
  57. except StopIteration:
  58. pass
  59. finally:
  60. del data_in
  61. compressed += lz4frame.compress_flush(c_context)
  62. get_frame_info_check(
  63. compressed,
  64. len(data),
  65. store_size,
  66. block_size,
  67. block_linked,
  68. content_checksum,
  69. block_checksum,
  70. )
  71. d_context = lz4frame.create_decompression_context()
  72. compressed_in = get_chunked(compressed, d_chunks)
  73. decompressed = b''
  74. bytes_read = 0
  75. eofs = []
  76. try:
  77. while True:
  78. d, b, e = lz4frame.decompress_chunk(
  79. d_context,
  80. next(compressed_in),
  81. )
  82. decompressed += d
  83. bytes_read += b
  84. eofs.append(e)
  85. except StopIteration:
  86. pass
  87. finally:
  88. del compressed_in
  89. assert bytes_read == len(compressed)
  90. assert decompressed == data
  91. assert eofs[-1] is True
  92. assert (True in eofs[:-2]) is False