test_frame_1.py 2.6 KB

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