From 8f0c7ea8e2f9650948561cb9fc749ad8cdb52568 Mon Sep 17 00:00:00 2001 From: Joachim B Haga Date: Thu, 27 Feb 2025 17:47:37 +0100 Subject: [PATCH] Fix test --- .../tests/conjecture/test_float_encoding.py | 21 +++++++++++++++ .../tests/quality/test_float_shrinking.py | 26 +------------------ 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/hypothesis-python/tests/conjecture/test_float_encoding.py b/hypothesis-python/tests/conjecture/test_float_encoding.py index 8781ad152b..5f955f2883 100644 --- a/hypothesis-python/tests/conjecture/test_float_encoding.py +++ b/hypothesis-python/tests/conjecture/test_float_encoding.py @@ -8,6 +8,8 @@ # 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 @@ -15,9 +17,12 @@ 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 @@ -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) diff --git a/hypothesis-python/tests/quality/test_float_shrinking.py b/hypothesis-python/tests/quality/test_float_shrinking.py index cb2c938eac..415746f9ff 100644 --- a/hypothesis-python/tests/quality/test_float_shrinking.py +++ b/hypothesis-python/tests/quality/test_float_shrinking.py @@ -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(): @@ -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)