Debian Codegraph @

hypothesis-python/tests/cover/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 from sys import float_info
12
13 import pytest
14
15 from hypothesis.errors import InvalidArgument
16 from hypothesis.internal.floats import next_down, next_up
17 from hypothesis.strategies import floats
18 from hypothesis.strategies._internal.numbers import next_down_normal, next_up_normal
19
20 from tests.common.debug import assert_no_examples, check_can_generate_examples, find_any
21 from tests.common.utils import PYTHON_FTZ
22
23 pytestmark = [pytest.mark.skipif(PYTHON_FTZ, reason="broken by unsafe compiler flags")]
24
25
26 def kw(marks=(), **kwargs):
27 id_ = ", ".join(f"{k}={v!r}" for k, v in kwargs.items())
28 return pytest.param(kwargs, id=id_, marks=marks)
29
30
31 @pytest.mark.parametrize(
32 "kwargs",
33 [
34 kw(min_value=1),
35 kw(max_value=-1),
36 kw(min_value=float_info.min),
37 kw(min_value=next_down(float_info.min), exclude_min=True),
38 kw(max_value=-float_info.min),
39 kw(min_value=next_up(-float_info.min), exclude_max=True),
40 ],
41 )
42 def test_subnormal_validation(kwargs):
43 strat = floats(**kwargs, allow_subnormal=True)
44 with pytest.raises(InvalidArgument):
45 check_can_generate_examples(strat)
46
47
48 @pytest.mark.parametrize(
49 "kwargs",
50 [
51 # min value
52 kw(allow_subnormal=False, min_value=1),
53 kw(allow_subnormal=False, min_value=float_info.min),
54 kw(allow_subnormal=True, min_value=-1),
55 kw(allow_subnormal=True, min_value=next_down(float_info.min)),
56 # max value
57 kw(allow_subnormal=False, max_value=-1),
58 kw(allow_subnormal=False, max_value=-float_info.min),
59 kw(allow_subnormal=True, max_value=1),
60 kw(allow_subnormal=True, max_value=next_up(-float_info.min)),
61 # min/max values
62 kw(allow_subnormal=True, min_value=-1, max_value=1),
63 kw(
64 allow_subnormal=True,
65 min_value=next_down(float_info.min),
66 max_value=float_info.min,
67 ),
68 kw(
69 allow_subnormal=True,
70 min_value=-float_info.min,
71 max_value=next_up(-float_info.min),
72 ),
73 kw(allow_subnormal=False, min_value=-1, max_value=-float_info.min),
74 kw(allow_subnormal=False, min_value=float_info.min, max_value=1),
75 ],
76 )
77 def test_allow_subnormal_defaults_correctly(kwargs):
78 # copy to support our threading CI tests
79 kwargs = kwargs.copy()
80 allow_subnormal = kwargs.pop("allow_subnormal")
81 strat = floats(**kwargs).filter(lambda x: x != 0)
82 if allow_subnormal:
83 find_any(strat, lambda x: -float_info.min < x < float_info.min)
84 else:
85 assert_no_examples(strat, lambda x: -float_info.min < x < float_info.min)
86
87
88 @pytest.mark.parametrize(
89 "func, val, expected",
90 [
91 (next_up_normal, -float_info.min, -0.0),
92 (next_up_normal, +0.0, float_info.min),
93 (next_down_normal, float_info.min, +0.0),
94 (next_down_normal, -0.0, -float_info.min),
95 ],
96 )
97 def test_next_float_normal(func, val, expected):
98 assert func(value=val, width=64, allow_subnormal=False) == expected