From 6bc68a34e34e32b56dff2145479dff970b126f79 Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone Date: Wed, 20 Sep 2023 10:00:04 -0400 Subject: [PATCH] rewrite test_small with Hypothesis Hypothesis reproduces the problem more quickly than the prior pseudorandom test. --- setup.py | 2 +- zfec/test/test_zfec.py | 42 +++++++++++++++++++++++++++++++----------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/setup.py b/setup.py index 35233659..27695b66 100644 --- a/setup.py +++ b/setup.py @@ -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(), diff --git a/zfec/test/test_zfec.py b/zfec/test/test_zfec.py index 6653c64b..e6231822 100755 --- a/zfec/test/test_zfec.py +++ b/zfec/test/test_zfec.py @@ -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 @@ -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))) @@ -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):