Debian Codegraph @

hypothesis-python/tests/cover/test_complex_numbers.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 import sys
13
14 import pytest
15
16 from hypothesis import HealthCheck, given, reject, settings, strategies as st
17 from hypothesis.errors import InvalidArgument
18 from hypothesis.strategies import complex_numbers
19
20 from tests.common.debug import (
21 assert_no_examples,
22 check_can_generate_examples,
23 find_any,
24 minimal,
25 )
26
27
28 def test_minimal():
29 assert minimal(complex_numbers()) == 0
30
31
32 def test_minimal_nonzero_real():
33 assert minimal(complex_numbers(), lambda x: x.real != 0) == 1
34
35
36 def test_minimal_nonzero_imaginary():
37 assert minimal(complex_numbers(), lambda x: x.imag != 0) == 1j
38
39
40 def test_minimal_quadrant1():
41 assert minimal(complex_numbers(), lambda x: x.imag > 0 and x.real > 0) == 1 + 1j
42
43
44 def test_minimal_quadrant2():
45 assert minimal(complex_numbers(), lambda x: x.imag > 0 and x.real < 0) == -1 + 1j
46
47
48 def test_minimal_quadrant3():
49 assert minimal(complex_numbers(), lambda x: x.imag < 0 and x.real < 0) == -1 - 1j
50
51
52 def test_minimal_quadrant4():
53 assert minimal(complex_numbers(), lambda x: x.imag < 0 and x.real > 0) == 1 - 1j
54
55
56 @given(st.data(), st.integers(-5, 5).map(lambda x: 10**x))
57 def test_max_magnitude_respected(data, mag):
58 c = data.draw(complex_numbers(max_magnitude=mag))
59 # Note we accept epsilon errors here as internally sqrt is used to draw
60 # complex numbers. sqrt on some platforms gets epsilon errors, which is
61 # too tricky to filter out and so - for now - we just accept them.
62 assert abs(c) <= mag * (1 + sys.float_info.epsilon)
63
64
65 @given(complex_numbers(max_magnitude=0))
66 def test_max_magnitude_zero(val):
67 assert val == 0
68
69
70 @settings(suppress_health_check=[HealthCheck.too_slow])
71 @given(st.data(), st.integers(-5, 5).map(lambda x: 10**x))
72 def test_min_magnitude_respected(data, mag):
73 c = data.draw(complex_numbers(min_magnitude=mag))
74 # See test_max_magnitude_respected comment
75 assert (
76 abs(c.real) >= mag
77 or abs(c.imag) >= mag
78 or abs(c) >= mag * (1 - sys.float_info.epsilon)
79 )
80
81
82 def test_minimal_min_magnitude_zero():
83 assert minimal(complex_numbers(min_magnitude=0)) == 0
84
85
86 def test_minimal_min_magnitude_positive():
87 assert minimal(complex_numbers(min_magnitude=0.5)) in (0.5, 1)
88
89
90 def test_minimal_minmax_magnitude():
91 assert minimal(complex_numbers(min_magnitude=0.5, max_magnitude=1.5)) in (0.5, 1)
92
93
94 @given(st.data(), st.floats(0, 10e300, allow_infinity=False, allow_nan=False))
95 def test_minmax_magnitude_equal(data, mag):
96 val = data.draw(st.complex_numbers(min_magnitude=mag, max_magnitude=mag))
97 try:
98 # Cap magnitude at 10e300 to avoid float overflow, and imprecision
99 # at very large exponents (which makes math.isclose fail)
100 assert math.isclose(abs(val), mag)
101 except OverflowError:
102 reject()
103
104
105 def _is_subnormal(x):
106 return 0 < abs(x) < sys.float_info.min
107
108
109 @pytest.mark.parametrize(
110 "allow_subnormal, min_magnitude, max_magnitude",
111 [
112 (True, 0, None),
113 (True, 1, None),
114 (False, 0, None),
115 ],
116 )
117 def test_allow_subnormal(allow_subnormal, min_magnitude, max_magnitude):
118 strat = complex_numbers(
119 min_magnitude=min_magnitude,
120 max_magnitude=max_magnitude,
121 allow_subnormal=allow_subnormal,
122 ).filter(lambda x: x.real != 0 and x.imag != 0)
123
124 if allow_subnormal:
125 find_any(strat, lambda x: _is_subnormal(x.real) or _is_subnormal(x.imag))
126 else:
127 assert_no_examples(
128 strat, lambda x: _is_subnormal(x.real) or _is_subnormal(x.imag)
129 )
130
131
132 @pytest.mark.parametrize("allow_subnormal", [1, 0.0, "False"])
133 def test_allow_subnormal_validation(allow_subnormal):
134 with pytest.raises(InvalidArgument):
135 check_can_generate_examples(complex_numbers(allow_subnormal=allow_subnormal))