test_frame_0.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import lz4.frame as lz4frame
  2. import lz4
  3. import re
  4. def test_library_version_number():
  5. v = lz4.library_version_number()
  6. assert isinstance(v, int)
  7. assert v > 10000
  8. def test_library_version_string():
  9. v = lz4.library_version_string()
  10. assert isinstance(v, str)
  11. assert v.count('.') == 2
  12. r = re.compile(r'^[0-9]*\.[0-9]*\.[0-9]*$')
  13. assert r.match(v) is not None
  14. def test_create_compression_context():
  15. context = lz4frame.create_compression_context()
  16. assert context is not None
  17. def test_create_decompression_context():
  18. context = lz4frame.create_decompression_context()
  19. assert context is not None
  20. def test_reset_decompression_context_1():
  21. if lz4.library_version_number() >= 10800:
  22. context = lz4frame.create_decompression_context()
  23. r = lz4frame.reset_decompression_context(context)
  24. assert r is None
  25. else:
  26. pass
  27. def test_reset_decompression_context_2():
  28. if lz4.library_version_number() >= 10800:
  29. c = lz4frame.compress(b'1234', return_bytearray=False)
  30. context = lz4frame.create_decompression_context()
  31. try:
  32. # Simulate an error by passing junk to decompress
  33. d = lz4frame.decompress_chunk(context, c[4:])
  34. except RuntimeError:
  35. pass
  36. r = lz4frame.reset_decompression_context(context)
  37. assert r is None
  38. # And confirm we can use the context after reset
  39. d, bytes_read, eof = lz4frame.decompress_chunk(context, c)
  40. assert d == b'1234'
  41. assert bytes_read == len(c)
  42. assert eof is True
  43. else:
  44. pass
  45. def test_compress_return_type_1():
  46. r = lz4frame.compress(b'', return_bytearray=False)
  47. assert isinstance(r, bytes)
  48. def test_compress_return_type_2():
  49. r = lz4frame.compress(b'', return_bytearray=True)
  50. assert isinstance(r, bytearray)
  51. def test_decompress_return_type_1():
  52. c = lz4frame.compress(b'', return_bytearray=False)
  53. r = lz4frame.decompress(
  54. c,
  55. return_bytearray=False,
  56. return_bytes_read=False
  57. )
  58. assert isinstance(r, bytes)
  59. def test_decompress_return_type_2():
  60. c = lz4frame.compress(b'', return_bytearray=False)
  61. r = lz4frame.decompress(
  62. c,
  63. return_bytearray=True,
  64. return_bytes_read=False
  65. )
  66. assert isinstance(r, bytearray)
  67. def test_decompress_return_type_3():
  68. c = lz4frame.compress(b'', return_bytearray=False)
  69. r = lz4frame.decompress(
  70. c,
  71. return_bytearray=False,
  72. return_bytes_read=True
  73. )
  74. assert isinstance(r, tuple)
  75. assert isinstance(r[0], bytes)
  76. assert isinstance(r[1], int)
  77. def test_decompress_return_type_4():
  78. c = lz4frame.compress(b'', return_bytearray=False)
  79. r = lz4frame.decompress(
  80. c,
  81. return_bytearray=True,
  82. return_bytes_read=True
  83. )
  84. assert isinstance(r, tuple)
  85. assert isinstance(r[0], bytearray)
  86. assert isinstance(r[1], int)
  87. def test_decompress_chunk_return_type_1():
  88. c = lz4frame.compress(b'', return_bytearray=False)
  89. d = lz4frame.create_decompression_context()
  90. r, b, e = lz4frame.decompress_chunk(
  91. d,
  92. c,
  93. return_bytearray=False,
  94. )
  95. assert isinstance(r, bytes)
  96. assert isinstance(b, int)
  97. assert isinstance(e, bool)
  98. def test_decompress_chunk_return_type_2():
  99. c = lz4frame.compress(b'', return_bytearray=False)
  100. d = lz4frame.create_decompression_context()
  101. r, b, e = lz4frame.decompress_chunk(
  102. d,
  103. c,
  104. return_bytearray=True,
  105. )
  106. assert isinstance(r, bytearray)
  107. assert isinstance(b, int)
  108. assert isinstance(e, bool)
  109. def test_decompress_chunk_return_type_3():
  110. c = lz4frame.compress(b'', return_bytearray=False)
  111. d = lz4frame.create_decompression_context()
  112. r = lz4frame.decompress_chunk(
  113. d,
  114. c,
  115. return_bytearray=False,
  116. )
  117. assert isinstance(r, tuple)
  118. assert isinstance(r[0], bytes)
  119. assert isinstance(r[1], int)
  120. assert isinstance(r[2], bool)
  121. def test_decompress_chunk_return_type_4():
  122. c = lz4frame.compress(b'', return_bytearray=False)
  123. d = lz4frame.create_decompression_context()
  124. r = lz4frame.decompress_chunk(
  125. d,
  126. c,
  127. return_bytearray=True,
  128. )
  129. assert isinstance(r, tuple)
  130. assert isinstance(r[0], bytearray)
  131. assert isinstance(r[1], int)
  132. assert isinstance(r[2], bool)
  133. def test_block_size_constants():
  134. assert lz4frame.BLOCKSIZE_DEFAULT == 0
  135. assert lz4frame.BLOCKSIZE_MAX64KB == 4
  136. assert lz4frame.BLOCKSIZE_MAX256KB == 5
  137. assert lz4frame.BLOCKSIZE_MAX1MB == 6
  138. assert lz4frame.BLOCKSIZE_MAX4MB == 7