Skip to content

Commit

Permalink
rework clamper test to avoid signed zero issues with sorted()
Browse files Browse the repository at this point in the history
  • Loading branch information
tybug committed Dec 23, 2024
1 parent 7cdeb91 commit c786238
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions hypothesis-python/tests/conjecture/test_junkdrawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,27 @@ def test_pop():
x.pop()


@example(1, 5, 10)
@example(1, 10, 5)
@example(5, 10, 5)
@example(5, 1, 10)
@example(-5, 0.0, -0.0)
@example(0.0, -0.0, 5)
@given(
st.floats(allow_nan=False), st.floats(allow_nan=False), st.floats(allow_nan=False)
)
def test_clamp(lower, value, upper):
lower, upper = sorted((lower, upper))
@st.composite
def clamp_inputs(draw):
lower = draw(st.floats(allow_nan=False))
value = draw(st.floats(allow_nan=False))
upper = draw(st.floats(min_value=lower, allow_nan=False))
return (lower, value, upper)


@example((1, 5, 10))
@example((1, 10, 5))
@example((5, 10, 5))
@example((5, 1, 10))
@example((-5, 0.0, -0.0))
@example((0.0, -0.0, 5))
@given(clamp_inputs())
def test_clamp(input):
lower, value, upper = input
clamped = clamp(lower, value, upper)

assert sign_aware_lte(lower, clamped) and sign_aware_lte(clamped, upper)
assert sign_aware_lte(lower, clamped)
assert sign_aware_lte(clamped, upper)
if sign_aware_lte(lower, value) and sign_aware_lte(value, upper):
assert float_to_int(value) == float_to_int(clamped)
if lower > value:
Expand Down

0 comments on commit c786238

Please sign in to comment.