Skip to content

Commit

Permalink
Use composite strategy in trie.tools.strategies
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
carver committed Jun 18, 2020
1 parent c685189 commit 365ad59
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
18 changes: 6 additions & 12 deletions tests/test_hexary_trie_walk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions trie/tools/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down

0 comments on commit 365ad59

Please sign in to comment.