test_compatibility.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/python -u
  2. #
  3. # Python Bindings for LZMA
  4. #
  5. # Copyright (c) 2004-2006 by Joachim Bauch, mail@joachim-bauch.de
  6. # 7-Zip Copyright (C) 1999-2005 Igor Pavlov
  7. # LZMA SDK Copyright (C) 1999-2005 Igor Pavlov
  8. #
  9. # This library is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU Lesser General Public
  11. # License as published by the Free Software Foundation; either
  12. # version 2.1 of the License, or (at your option) any later version.
  13. #
  14. # This library is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. # Lesser General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU Lesser General Public
  20. # License along with this library; if not, write to the Free Software
  21. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. #
  23. # $Id: test_compatibility.py 106 2006-01-08 18:19:04Z jojo $
  24. #
  25. import md5, random
  26. import pylzma
  27. import unittest
  28. from binascii import unhexlify
  29. if not hasattr(pylzma, 'decompress_compat'):
  30. raise ImportError, 'no compatibility support available'
  31. ALL_CHARS = ''.join([chr(x) for x in xrange(256)])
  32. # cache random strings to speed up tests
  33. _random_strings = {}
  34. def generate_random(size, choice=random.choice, ALL_CHARS=ALL_CHARS):
  35. global _random_strings
  36. if _random_strings.has_key(size):
  37. return _random_strings[size]
  38. s = ''.join([choice(ALL_CHARS) for x in xrange(size)])
  39. _random_strings[size] = s
  40. return s
  41. class TestPyLZMACompability(unittest.TestCase):
  42. def setUp(self):
  43. self.plain = 'hello, this is a test string'
  44. self.plain_with_eos = unhexlify('5d0000800000341949ee8def8c6b64909b1386e370bebeb1b656f5736d653c127731a214ff7031c000')
  45. self.plain_without_eos = unhexlify('5d0000800000341949ee8def8c6b64909b1386e370bebeb1b656f5736d653c115edbe9')
  46. def test_decompression_noeos(self):
  47. # test decompression without the end of stream marker
  48. decompressed = pylzma.decompress_compat(self.plain_without_eos)
  49. self.assertEqual(decompressed, self.plain)
  50. def test_compression_decompression_noeos(self):
  51. # call compression and decompression on random data of various sizes
  52. for i in xrange(18):
  53. size = 1 << i
  54. original = generate_random(size)
  55. result = pylzma.decompress_compat(pylzma.compress(original, eos=0))[:size]
  56. self.assertEqual(md5.new(original).hexdigest(), md5.new(result).hexdigest())
  57. def test_multi(self):
  58. # call compression and decompression multiple times to detect memory leaks...
  59. for x in xrange(4):
  60. self.test_compression_decompression_noeos()
  61. def test_main():
  62. from test import test_support
  63. test_support.run_unittest(TestPyLZMACompability)
  64. if __name__ == "__main__":
  65. unittest.main()