From 365ad592ae20851ef5573538134cc979a2369682 Mon Sep 17 00:00:00 2001 From: Jason Carver Date: Thu, 18 Jun 2020 14:21:50 -0700 Subject: [PATCH] Use composite strategy in trie.tools.strategies This is a cleanup for the backfill tests, using the shared strategy to try to expand the surface area of the hypothesis tests. Note: - The trie_from_keys() builder with a minimum_value_length=0 would try to "insert" at key b'' the value b'', which is of course a delete. So the smallest permissable size must be a byte length of 1. - An empty trie is not handled well by these backfill tests, but backfill is entirely uninteresting for an empty trie, so we can ignore the case. --- tests/test_hexary_trie_walk.py | 18 ++++++------------ trie/tools/strategies.py | 4 ++-- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/tests/test_hexary_trie_walk.py b/tests/test_hexary_trie_walk.py index 2f30c2ef..2d748887 100644 --- a/tests/test_hexary_trie_walk.py +++ b/tests/test_hexary_trie_walk.py @@ -18,13 +18,10 @@ @given( - st.lists( - st.binary(min_size=3, max_size=3), - unique=True, - max_size=1024, - ), + # starting trie keys + trie_keys_with_extensions(allow_empty_trie=False), # minimum value length (to help force trie nodes to stop embedding) - st.integers(min_value=3, max_value=32), + st.integers(min_value=1, max_value=32), st.lists( st.integers(min_value=0, max_value=0xf), max_size=4 * 2, # one byte (two nibbles) deeper than the longest key above @@ -81,13 +78,10 @@ def test_trie_walk_backfilling(trie_keys, minimum_value_length, index_nibbles): @given( - st.lists( - st.binary(min_size=3, max_size=3), - unique=True, - max_size=1024, - ), + # starting trie keys + trie_keys_with_extensions(allow_empty_trie=False), # minimum value length (to help force trie nodes to stop embedding) - st.integers(min_value=3, max_value=32), + st.integers(min_value=1, max_value=32), st.lists( st.integers(min_value=0, max_value=0xf), max_size=4 * 2, # one byte (two nibbles) deeper than the longest key above diff --git a/trie/tools/strategies.py b/trie/tools/strategies.py index b561768c..643fe34a 100644 --- a/trie/tools/strategies.py +++ b/trie/tools/strategies.py @@ -25,7 +25,7 @@ def random_trie_strategy(draw): @st.composite -def trie_keys_with_extensions(draw): +def trie_keys_with_extensions(draw, allow_empty_trie=True): """ Build trie keys that tend to have lots of extension/branch/leaf nodes. Anecdotally, this was more likely to produce examples like the one @@ -34,7 +34,7 @@ def trie_keys_with_extensions(draw): # Simplest possible trie: an empty trie # Test it about once, on average, per run of 200 tests (the default example count) # Also, this will shrink down to the empty trie as you shrink these integers. - if draw(st.integers(min_value=0, max_value=200)) == 0: + if allow_empty_trie and draw(st.integers(min_value=0, max_value=200)) == 0: return () def build_up_from_children(children):