Skip to content

Commit

Permalink
Coverage + shrinking fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Nov 19, 2023
1 parent 71f38a3 commit ec7d78d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
8 changes: 4 additions & 4 deletions hypothesis-python/src/hypothesis/internal/conjecture/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ def draw_integer(
probe = max_value + 1
while max_value < probe:
self._cd.start_example(ONE_BOUND_INTEGERS_LABEL)
probe = self._draw_unbounded_integer()
probe = self._draw_unbounded_integer() + shrink_towards
self._cd.stop_example(discard=max_value < probe)
return probe

Expand All @@ -1037,7 +1037,7 @@ def draw_integer(
probe = min_value - 1
while probe < min_value:
self._cd.start_example(ONE_BOUND_INTEGERS_LABEL)
probe = self._draw_unbounded_integer()
probe = self._draw_unbounded_integer() + shrink_towards
self._cd.stop_example(discard=probe < min_value)
return probe

Expand Down Expand Up @@ -1442,8 +1442,8 @@ def draw_integer(

def draw_float(
self,
min_value: Optional[float] = None,
max_value: Optional[float] = None,
min_value: float = -math.inf,
max_value: float = math.inf,
*,
allow_nan: bool = True,
smallest_nonzero_magnitude: float = SMALLEST_SUBNORMAL,
Expand Down
6 changes: 6 additions & 0 deletions hypothesis-python/tests/conjecture/test_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ def test_can_draw_weighted_integer_range(data, n):
assert x <= n


@given(st.binary(min_size=10))
def test_can_draw_weighted_integer_range(buffer):
data = ConjectureData.for_buffer(buffer)
data.draw_integer(0, 7, weights=[1] * 8, shrink_towards=6)


def test_can_mark_invalid_with_why():
x = ConjectureData.for_buffer(b"")
with pytest.raises(StopTest):
Expand Down
12 changes: 11 additions & 1 deletion hypothesis-python/tests/conjecture/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,24 @@ def test_restricted_bits():

@pytest.mark.parametrize(
"lo,hi,to",
[(0, None, 0), (None, 1, 0), (None, 1, 1), (-1, 1, 0)],
[(1, None, 1), (1, None, 2), (None, 2, 1), (None, 1, 1), (-1, 1, 0)],
)
def test_single_bounds(lo, hi, to):
data = ConjectureData.for_buffer([0] * 100)
assert data.draw_integer(lo, hi, shrink_towards=to) == to


def test_bounds_and_weights():
for to in (0, 1, 2):
data = ConjectureData.for_buffer([0] * 100 + [2] * 100)
val = data.draw_integer(0, 2, shrink_towards=to, weights=[1, 1, 1])
assert val == to, to


def test_draw_string():
data = ConjectureData.for_buffer([0] * 10)
assert data.draw_string(IntervalSet([(0, 127)]), min_size=1) == "0"

data = ConjectureData.for_buffer([0] * 10)
assert data.draw_string(IntervalSet([(0, 1024)]), min_size=1) == "0"

Expand Down

0 comments on commit ec7d78d

Please sign in to comment.