Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
jobh committed Feb 27, 2025
1 parent 7ef350f commit 8f0c7ea
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
21 changes: 21 additions & 0 deletions hypothesis-python/tests/conjecture/test_float_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.

import math
import struct
import sys

import pytest

from hypothesis import HealthCheck, assume, example, given, settings, strategies as st
from hypothesis.internal.compat import ceil, extract_bits, floor
from hypothesis.internal.conjecture import floats as flt
from hypothesis.internal.conjecture.data import ConjectureData
from hypothesis.internal.conjecture.engine import ConjectureRunner
from hypothesis.internal.floats import float_to_int

from tests.conjecture.common import shrinking_from

EXPONENTS = list(range(flt.MAX_EXPONENT + 1))
assert len(EXPONENTS) == 2**11

Expand Down Expand Up @@ -200,3 +205,19 @@ def test_reject_out_of_bounds_floats_while_shrinking():
kwargs = {"min_value": 103.0}
g = minimal_from(103.1, lambda x: x >= 100, kwargs=kwargs)
assert g == 103.0


@pytest.mark.parametrize(
"nan",
[math.nan, -math.nan, struct.unpack('d', struct.pack('Q', 0xfff8000000000001))[0]]
)
def test_shrinks_to_canonical_nan(nan):
@shrinking_from([nan])
def shrinker(data: ConjectureData):
value = data.draw_float()
if math.isnan(value):
data.mark_interesting()

shrinker.shrink()
assert len(shrinker.choices) == 1
assert float_to_int(shrinker.choices[0]) == float_to_int(math.nan)
26 changes: 1 addition & 25 deletions hypothesis-python/tests/quality/test_float_shrinking.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,12 @@
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.

import math
import struct

import pytest

from hypothesis import example, given, seed, strategies as st
from hypothesis import example, given, strategies as st
from hypothesis.internal.compat import ceil
from hypothesis.internal.conjecture.data import ConjectureData

from tests.common.debug import minimal
from tests.conjecture.common import shrinking_from


def test_shrinks_to_simple_floats():
Expand Down Expand Up @@ -50,22 +45,3 @@ def test_shrinks_downwards_to_integers_when_fractional(b):
).filter(lambda x: int(x) != x)
)
assert g == b + 0.5


@pytest.mark.parametrize("nan",
[
math.nan,
-math.nan,
struct.unpack('d', struct.pack('Q', 0xfff8000000000001))[0]
]
)
def test_shrinks_to_canonical_nan(nan):
@shrinking_from([nan])
def shrinker(data: ConjectureData):
value = data.draw_float()
if math.isnan(value):
data.mark_interesting()

shrinker.shrink()
assert len(shrinker.choices) == 1
assert struct.pack("d", shrinker.choices[0]) == struct.pack("d", math.nan)

0 comments on commit 8f0c7ea

Please sign in to comment.