Debian Codegraph @

hypothesis-python/tests/nocover/test_subnormal_floats.py

raw
1 # This file is part of Hypothesis, which may be found at
2 # https://github.com/HypothesisWorks/hypothesis/
3 #
4 # Copyright the Hypothesis Authors.
5 # Individual contributors are listed in AUTHORS.rst and the git log.
6 #
7 # This Source Code Form is subject to the terms of the Mozilla Public License,
8 # v. 2.0. If a copy of the MPL was not distributed with this file, You can
9 # obtain one at https://mozilla.org/MPL/2.0/.
10
11 import math
12 from sys import float_info
13
14 import pytest
15
16 from hypothesis.internal.floats import width_smallest_normals
17 from hypothesis.strategies import floats
18
19 from tests.common.debug import assert_all_examples, find_any
20 from tests.common.utils import PYTHON_FTZ
21
22
23 def test_python_compiled_with_sane_math_options():
24 """Python does not flush-to-zero, which violates IEEE-754
25
26 The other tests that rely on subnormals are skipped when Python is FTZ
27 (otherwise pytest will be very noisy), so this meta test ensures CI jobs
28 still fail as we currently don't care to support such builds of Python.
29 """
30 assert not PYTHON_FTZ
31
32
33 skipif_ftz = pytest.mark.skipif(PYTHON_FTZ, reason="broken by unsafe compiler flags")
34
35
36 @skipif_ftz
37 def test_can_generate_subnormals():
38 find_any(floats().filter(lambda x: x > 0), lambda x: x < float_info.min)
39 find_any(floats().filter(lambda x: x < 0), lambda x: x > -float_info.min)
40
41
42 @skipif_ftz
43 @pytest.mark.parametrize(
44 "min_value, max_value", [(None, None), (-1, 0), (0, 1), (-1, 1)]
45 )
46 @pytest.mark.parametrize("width", [16, 32, 64])
47 def test_does_not_generate_subnormals_when_disallowed(width, min_value, max_value):
48 strat = floats(
49 min_value=min_value,
50 max_value=max_value,
51 allow_subnormal=False,
52 width=width,
53 )
54 strat = strat.filter(lambda x: x != 0.0 and math.isfinite(x))
55 smallest_normal = width_smallest_normals[width]
56 assert_all_examples(strat, lambda x: x <= -smallest_normal or x >= smallest_normal)