Skip to content

Commit 7ef350f

Browse files
committed
Fix test
1 parent 60adfc0 commit 7ef350f

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

hypothesis-python/tests/quality/test_float_shrinking.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@
88
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
99
# obtain one at https://mozilla.org/MPL/2.0/.
1010

11+
import math
12+
import struct
13+
1114
import pytest
1215

1316
from hypothesis import example, given, seed, strategies as st
1417
from hypothesis.internal.compat import ceil
18+
from hypothesis.internal.conjecture.data import ConjectureData
1519

1620
from tests.common.debug import minimal
21+
from tests.conjecture.common import shrinking_from
1722

1823

1924
def test_shrinks_to_simple_floats():
@@ -47,21 +52,20 @@ def test_shrinks_downwards_to_integers_when_fractional(b):
4752
assert g == b + 0.5
4853

4954

50-
@pytest.mark.parametrize("s", range(10))
51-
def test_shrinks_to_canonical_nan(s):
52-
# Regression test for #4277. A more reliable and minimal example could probably be found.
53-
@given(
54-
st.lists(
55-
st.just(0) | st.floats().filter(lambda a: a != a), min_size=2, max_size=2
56-
)
57-
)
58-
@seed(s)
59-
def sort_is_reversible(l):
60-
# fmt: off
61-
assert sorted(l, reverse=True) == list(reversed(sorted(l))) # noqa: C413
62-
# fmt: on
55+
@pytest.mark.parametrize("nan",
56+
[
57+
math.nan,
58+
-math.nan,
59+
struct.unpack('d', struct.pack('Q', 0xfff8000000000001))[0]
60+
]
61+
)
62+
def test_shrinks_to_canonical_nan(nan):
63+
@shrinking_from([nan])
64+
def shrinker(data: ConjectureData):
65+
value = data.draw_float()
66+
if math.isnan(value):
67+
data.mark_interesting()
6368

64-
try:
65-
sort_is_reversible()
66-
except AssertionError as e:
67-
assert "[0, nan]" in e.__notes__[0]
69+
shrinker.shrink()
70+
assert len(shrinker.choices) == 1
71+
assert struct.pack("d", shrinker.choices[0]) == struct.pack("d", math.nan)

0 commit comments

Comments
 (0)