This question is about the Hypothesis
library for property-based testing.
I want a strategy that would give me lists of positive floats with a fixed size, a specified sum, and such that no elements would be lower than some specified minimum threshold.
For example:
size = 5
sum_ = 1
threshold = 0.1
...
for _ in range(3)
print(magic_strategy.example())
could give something like this:
[0.4, 0.2, 0.1, 0.1, 0.2]
[0.15, 0.25, 0.25, 0.2, 0.15]
[0.2, 0.2, 0.2, 0.2, 0.2]
How do I write such a strategy?
Failed attempt with filtering:
from hypothesis import strategies as st
size = 5
sum_ = 1
threshold = 0.1
domain_values = st.floats(min_value=0, allow_infinity=False, exclude_min=True)
domain_values_lists = st.lists(domain_values, min_size=size, max_size=size)
normalized_lists = domain_values_lists.map(lambda values: [value * sum_ / sum(values)
for value in values])
lists_with_threshold = normalized_lists.filter(lambda values: all(value > threshold for value in values))
The problem with this approach is that for some reason whenever I'm taking example
from this strategy it always gives me lists of the same value regardless of the given input parameters:
[0.2, 0.2, 0.2, ..., 0.2]
[0.01, 0.01, 0.01, ..., 0.01]
[4.0, 4.0, 4.0, ..., 4.0]