Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding round trip test for lzma #16

Merged
merged 14 commits into from
May 25, 2020
69 changes: 68 additions & 1 deletion tests/test_compression.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,47 @@
import bz2
import gzip
import lzma
import unittest

from hypothesis import HealthCheck, given, settings, strategies as st

no_health_checks = settings(suppress_health_check=HealthCheck.all())


@st.composite
def lzma_filters(draw):
"""Generating filters options"""
op_filters = [
lzma.FILTER_DELTA,
lzma.FILTER_X86,
lzma.FILTER_IA64,
lzma.FILTER_ARM,
lzma.FILTER_ARMTHUMB,
lzma.FILTER_POWERPC,
lzma.FILTER_SPARC,
]
filter_ids = draw(st.lists(st.sampled_from(op_filters), max_size=3))
filter_ids.append(lzma.FILTER_LZMA2)
# create filters options
filters = []
for filter_ in filter_ids:
lc = draw(st.integers(0, 4))
lp = draw(st.integers(0, 4 - lc))
mf = [lzma.MF_HC3, lzma.MF_HC4, lzma.MF_BT2, lzma.MF_BT3, lzma.MF_BT4]
filters.append(
{
"id": filter_,
"preset": draw(st.integers(0, 9)),
"lc": lc,
"lp": lp,
"mode": draw(st.sampled_from([lzma.MODE_FAST, lzma.MODE_NORMAL])),
"mf": draw(st.sampled_from(mf)),
"depth": draw(st.integers(min_value=0)),
}
)
return filters


class TestBz2(unittest.TestCase):
@given(payload=st.binary(), compresslevel=st.integers(1, 9))
def test_bz2_round_trip(self, payload, compresslevel):
Expand Down Expand Up @@ -48,7 +83,39 @@ def test_gzip_round_trip(self, payload, compresslevel):

class TestLZMA(unittest.TestCase):
# TODO: https://docs.python.org/3/library/lzma.html
pass
@given(
payload=st.binary(),
check=st.sampled_from(
[lzma.CHECK_NONE, lzma.CHECK_CRC32, lzma.CHECK_CRC64, lzma.CHECK_SHA256]
),
compresslevel=st.integers(0, 9),
)
def test_lzma_round_trip_format_xz(self, payload, check, compresslevel):
result = lzma.decompress(
lzma.compress(
payload, format=lzma.FORMAT_XZ, check=check, preset=compresslevel
)
)
self.assertEqual(payload, result)

@given(
payload=st.binary(), compresslevel=st.integers(0, 9),
)
def test_lzma_round_trip_format_alone(self, payload, compresslevel):
result = lzma.decompress(
lzma.compress(payload, format=lzma.FORMAT_ALONE, preset=compresslevel)
)
self.assertEqual(payload, result)

@unittest.skip(reason="LZMA filter strategy too general?")
@given(payload=st.binary(), filters=lzma_filters())
def test_lzma_round_trip_format_raw(self, payload, filters):
# This test is a stub from our attempt to write a round-trip test with
# custom LZMA filters (from the strategy above). Ultimately we decided
# to defer implementation to a future PR and merge what we had working.
# TODO: work out what's happening here and fix it.
compressed = lzma.compress(payload, format=lzma.FORMAT_RAW, filters=filters)
self.assertEqual(payload, lzma.decompress(compressed))


class TestZlib(unittest.TestCase):
Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ description = Run the tests
deps =
--no-deps
-r requirements.txt
-U
git+https://github.com/Zac-HD/hypothesmith.git
Comment on lines +20 to +21
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebase on master to get rid of this bit.

commands =
python -m unittest discover tests --catch {posargs}

Expand Down