Debian Codegraph @

hypothesis-python/tests/cover/test_cathetus.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 from sys import float_info
14
15 import pytest
16
17 from hypothesis import assume, given
18 from hypothesis.internal.cathetus import cathetus
19 from hypothesis.strategies import floats
20
21
22 def test_cathetus_subnormal_underflow():
23 u = sys.float_info.min * sys.float_info.epsilon
24 h = 5 * u
25 a = 4 * u
26 assert cathetus(h, a) == 3 * u
27
28
29 def test_cathetus_simple_underflow():
30 a = sys.float_info.min
31 h = a * math.sqrt(2)
32 b = cathetus(h, a)
33 assert b > 0, f"expecting positive cathetus({h:g}, {a:g}), got {b:g}"
34
35
36 def test_cathetus_huge_no_overflow():
37 h = sys.float_info.max
38 a = h / math.sqrt(2)
39 b = cathetus(h, a)
40 assert math.isfinite(b), f"expecting finite cathetus({h:g}, {a:g}), got {b:g}"
41
42
43 def test_cathetus_large_no_overflow():
44 h = sys.float_info.max / 3
45 a = h / math.sqrt(2)
46 b = cathetus(h, a)
47 assert math.isfinite(b), f"expecting finite cathetus({h:g}, {a:g}), got {b:g}"
48
49
50 @pytest.mark.parametrize(
51 "h,a",
52 [
53 # NaN hypot
54 (math.nan, 3),
55 (math.nan, 0),
56 (math.nan, math.inf),
57 (math.nan, math.nan),
58 # Infeasible
59 (2, 3),
60 (2, -3),
61 (2, math.inf),
62 (2, math.nan),
63 # Surprisingly consistent with c99 hypot()
64 (math.inf, math.inf),
65 ],
66 )
67 def test_cathetus_nan(h, a):
68 assert math.isnan(cathetus(h, a))
69
70
71 @pytest.mark.parametrize(
72 "h,a", [(math.inf, 3), (math.inf, -3), (math.inf, 0), (math.inf, math.nan)]
73 )
74 def test_cathetus_infinite(h, a):
75 assert math.isinf(cathetus(h, a))
76
77
78 @pytest.mark.parametrize(
79 "h,a,b", [(-5, 4, 3), (5, -4, 3), (-5, -4, 3), (0, 0, 0), (1, 0, 1)]
80 )
81 def test_cathetus_signs(h, a, b):
82 assert abs(cathetus(h, a) - b) <= abs(b) * float_info.epsilon
83
84
85 @given(
86 h=floats(0) | floats(min_value=1e308, allow_infinity=False),
87 a=floats(0, allow_infinity=False)
88 | floats(min_value=0, max_value=1e250, allow_infinity=False),
89 )
90 def test_cathetus_always_leq_hypot(h, a):
91 assume(h >= a)
92 b = cathetus(h, a)
93 assert 0 <= b <= h
94
95
96 @pytest.mark.parametrize(
97 "a,b,h",
98 [
99 (3, 4, 5),
100 (5, 12, 13),
101 (8, 15, 17),
102 (7, 24, 25),
103 (20, 21, 29),
104 (12, 35, 37),
105 (9, 40, 41),
106 (28, 45, 53),
107 (11, 60, 61),
108 (16, 63, 65),
109 (33, 56, 65),
110 (48, 55, 73),
111 (13, 84, 85),
112 (36, 77, 85),
113 (39, 80, 89),
114 (65, 72, 97),
115 (20, 99, 101),
116 (60, 91, 109),
117 (15, 112, 113),
118 (44, 117, 125),
119 (88, 105, 137),
120 (17, 144, 145),
121 (24, 143, 145),
122 (51, 140, 149),
123 (85, 132, 157),
124 (119, 120, 169),
125 (52, 165, 173),
126 (19, 180, 181),
127 (57, 176, 185),
128 (104, 153, 185),
129 (95, 168, 193),
130 (28, 195, 197),
131 (84, 187, 205),
132 (133, 156, 205),
133 (21, 220, 221),
134 (140, 171, 221),
135 (60, 221, 229),
136 (105, 208, 233),
137 (120, 209, 241),
138 (32, 255, 257),
139 (23, 264, 265),
140 (96, 247, 265),
141 (69, 260, 269),
142 (115, 252, 277),
143 (160, 231, 281),
144 (161, 240, 289),
145 (68, 285, 293),
146 ],
147 )
148 def test_pythagorean_triples(a, b, h):
149 assert abs(math.hypot(a, b) - h) <= abs(h) * float_info.epsilon
150 assert abs(cathetus(h, a) - b) <= abs(b) * float_info.epsilon