compress_test.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # Copyright 2016 The Brotli Authors. All rights reserved.
  2. #
  3. # Distributed under MIT license.
  4. # See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  5. import unittest
  6. from . import _test_utils
  7. import brotli
  8. class TestCompress(_test_utils.TestCase):
  9. VARIANTS = {'quality': (1, 6, 9, 11), 'lgwin': (10, 15, 20, 24)}
  10. def _check_decompression(self, test_data, **kwargs):
  11. kwargs = {}
  12. # Write decompression to temp file and verify it matches the original.
  13. temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data)
  14. temp_compressed = _test_utils.get_temp_compressed_name(test_data)
  15. original = test_data
  16. with open(temp_uncompressed, 'wb') as out_file:
  17. with open(temp_compressed, 'rb') as in_file:
  18. out_file.write(brotli.decompress(in_file.read(), **kwargs))
  19. self.assertFilesMatch(temp_uncompressed, original)
  20. def _compress(self, test_data, **kwargs):
  21. temp_compressed = _test_utils.get_temp_compressed_name(test_data)
  22. with open(temp_compressed, 'wb') as out_file:
  23. with open(test_data, 'rb') as in_file:
  24. out_file.write(brotli.compress(in_file.read(), **kwargs))
  25. def _test_compress(self, test_data, **kwargs):
  26. self._compress(test_data, **kwargs)
  27. self._check_decompression(test_data, **kwargs)
  28. _test_utils.generate_test_methods(TestCompress, variants=TestCompress.VARIANTS)
  29. if __name__ == '__main__':
  30. unittest.main()