Skip to content

Commit

Permalink
rewrite test_small with Hypothesis
Browse files Browse the repository at this point in the history
Hypothesis reproduces the problem more quickly than the prior pseudorandom
test.
  • Loading branch information
exarkun committed Sep 20, 2023
1 parent 12a1e95 commit 6bc68a3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
url="https://github.com/tahoe-lafs/zfec",
extras_require={
"bench": ["pyutil >= 3.0.0"],
"test": ["twisted", "pyutil >= 3.0.0"],
"test": ["twisted", "pyutil >= 3.0.0", "hypothesis"],
},
ext_modules=extensions,
cmdclass=versioneer.get_cmdclass(),
Expand Down
42 changes: 31 additions & 11 deletions zfec/test/test_zfec.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from io import BytesIO

import unittest
from hypothesis import given
from hypothesis.strategies import integers, binary, lists, tuples, just

global VERBOSE
VERBOSE=False
Expand Down Expand Up @@ -52,12 +54,6 @@ def _help_test_random():
ss = [ randstr(l//k) for x in range(k) ]
_h(k, m, ss)

def _help_test_random_with_l(l):
m = random.randrange(1, 257)
k = random.randrange(1, m+1)
ss = [ randstr(l//k) for x in range(k) ]
_h(k, m, ss)

def _h_easy(k, m, s):
encer = zfec.easyfec.Encoder(k, m)
nums_and_blocks = list(enumerate(encer.encode(s)))
Expand Down Expand Up @@ -127,11 +123,35 @@ def test_from_agl_py(self):
# print "after decoding:"
# print "b0: %s, b1: %s" % tuple(base64.b16encode(x) for x in [b0, b1])

def test_small(self):
for i in range(16):
_help_test_random_with_l(i)
if VERBOSE:
print("%d randomized tests pass." % (i+1))
@given(
integers(min_value=0, max_value=15).flatmap(
lambda l:
integers(min_value=1, max_value=256).flatmap(
lambda m:
integers(min_value=1, max_value=m).flatmap(
lambda k:
lists(
binary(min_size=l//k, max_size=l//k),
min_size=k,
max_size=k,
).flatmap(
lambda ss: just((k, m, ss)),
),
),
),
),
)
def test_small(self, kmss):
"""
Short primary blocks (length between 0 and 15) round-trip through
Encoder / Decoder for all values of k, m, such that:
* 1 <= m <= 256
* 1 <= k <= m
"""
(k, m, ss) = kmss
_h(k, m, ss)

def test_random(self):
for i in range(3):
Expand Down

0 comments on commit 6bc68a3

Please sign in to comment.