Skip to content

Commit

Permalink
test against libsnapp
Browse files Browse the repository at this point in the history
  • Loading branch information
pipermerriam committed Oct 8, 2018
1 parent 96e4bd0 commit a44ef05
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Expand Up @@ -20,6 +20,9 @@ common: &common
- restore_cache:
keys:
- cache-{{ .Environment.CIRCLE_JOB }}-{{ checksum "setup.py" }}-{{ checksum "tox.ini" }}
- run:
name: install libsnappy-dev
command: sudo apt install -y libsnappy-dev
- run:
name: install dependencies
command: pip install --user tox
Expand Down
26 changes: 14 additions & 12 deletions py_snappy/main.py
Expand Up @@ -159,11 +159,13 @@ def decompress(buf: bytes) -> bytes:
if length > len(dst) - d or length > src_len - s:
raise CorruptError

dst = list(itertools.chain( # noqa: E203
dst[:d],
src[s : s + length], # noqa: E203
dst[d + length :], # noqa: E203
))
dst = list(
itertools.chain( # noqa: E203
dst[:d],
src[s : s + length], # noqa: E203
dst[d + length :], # noqa: E203
)
)
d += length
s += length
continue
Expand Down Expand Up @@ -268,7 +270,7 @@ def emit_copy(offset: int, length: int) -> Iterable[int]:
MAX_TABLE_SIZE = 1 << 14


@tuple_gen
@bytes_gen
def compress(buf: bytes) -> Iterable[int]:
"""compress returns the compressed form of buf."""
src = tuple(buf)
Expand Down Expand Up @@ -308,12 +310,12 @@ def compress(buf: bytes) -> Iterable[int]:
t, table[p] = table[p] - 1, s + 1

if (
t < 0
or s - t >= MAX_OFFSET # noqa: W503
or b0 != src[t] # noqa: W503
or b1 != src[t + 1] # noqa: W503
or b2 != src[t + 2] # noqa: W503
or b3 != src[t + 3] # noqa: W503
t < 0
or s - t >= MAX_OFFSET # noqa: W503
or b0 != src[t] # noqa: W503
or b1 != src[t + 1] # noqa: W503
or b2 != src[t + 2] # noqa: W503
or b3 != src[t + 3] # noqa: W503
):
# If t is invalid or src[s:s+4] differs from src[t:t+4], accumulate a literal byte.
s += 1
Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -11,6 +11,7 @@
"pytest-xdist",
"tox>=2.9.1,<3",
"hypothesis==3.74.3",
"python-snappy>=0.5.3,<1",
],
'lint': [
"flake8==3.4.1",
Expand Down
22 changes: 22 additions & 0 deletions tests/test_libsnappy_compat.py
@@ -0,0 +1,22 @@
from hypothesis import given, settings, strategies as st

from py_snappy import compress, decompress
from snappy import compress as libsnappy_compress, decompress as libsnappy_decompress

MEGABYTE = 1000000


@given(value=st.binary(min_size=1, max_size=2 * MEGABYTE))
@settings(max_examples=1000)
def test_local_decompress_libsnappy_compressed(value):
intermediate = libsnappy_compress(value)
result = decompress(intermediate)
assert value == result


@given(value=st.binary(min_size=1, max_size=2 * MEGABYTE))
@settings(max_examples=1000)
def test_libsnappy_decompress_local_compressed(value):
intermediate = compress(value)
result = libsnappy_decompress(intermediate)
assert value == result

0 comments on commit a44ef05

Please sign in to comment.