|
8 | 8 | # v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
9 | 9 | # obtain one at https://mozilla.org/MPL/2.0/.
|
10 | 10 |
|
| 11 | +import math |
| 12 | +import struct |
| 13 | + |
11 | 14 | import pytest
|
12 | 15 |
|
13 | 16 | from hypothesis import example, given, seed, strategies as st
|
14 | 17 | from hypothesis.internal.compat import ceil
|
| 18 | +from hypothesis.internal.conjecture.data import ConjectureData |
15 | 19 |
|
16 | 20 | from tests.common.debug import minimal
|
| 21 | +from tests.conjecture.common import shrinking_from |
17 | 22 |
|
18 | 23 |
|
19 | 24 | def test_shrinks_to_simple_floats():
|
@@ -47,21 +52,20 @@ def test_shrinks_downwards_to_integers_when_fractional(b):
|
47 | 52 | assert g == b + 0.5
|
48 | 53 |
|
49 | 54 |
|
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() |
63 | 68 |
|
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