-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathrun-math.test
More file actions
105 lines (82 loc) · 3.49 KB
/
run-math.test
File metadata and controls
105 lines (82 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Test cases for the math module (compile and run)
[case testMathOps]
from typing import Any, Callable, Final
import math
from math import pi, e, tau, inf, nan
from testutil import assertRaises, float_vals, assertDomainError, assertMathRangeError
pymath: Any = math
def validate_one_arg(test: Callable[[float], float], validate: Callable[[float], float]) -> None:
"""Ensure that test and validate behave the same for various float args."""
for x in float_vals:
try:
expected = validate(x)
except Exception as e:
try:
test(x)
assert False, f"no exception raised for {x!r}, expected {e!r}"
except Exception as e2:
assert repr(e) == repr(e2), f"actual for {x!r}: {e2!r}, expected: {e!r}"
continue
actual = test(x)
assert repr(actual) == repr(expected), (
f"actual for {x!r}: {actual!r}, expected {expected!r}")
def validate_two_arg(test: Callable[[float, float], float],
validate: Callable[[float, float], float]) -> None:
"""Ensure that test and validate behave the same for various float args."""
for x in float_vals:
for y in float_vals:
args = f"({x!r}, {y!r})"
try:
expected = validate(x, y)
except Exception as e:
try:
test(x, y)
assert False, f"no exception raised for {args}, expected {e!r}"
except Exception as e2:
assert repr(e) == repr(e2), f"actual for {args}: {e2!r}, expected: {e!r}"
continue
try:
actual = test(x, y)
except Exception as e:
assert False, f"no exception expected for {args}, got {e!r}"
assert repr(actual) == repr(expected), (
f"actual for {args}: {actual!r}, expected {expected!r}")
def test_sqrt() -> None:
validate_one_arg(lambda x: math.sqrt(x), pymath.sqrt)
def test_sin() -> None:
validate_one_arg(lambda x: math.sin(x), pymath.sin)
def test_cos() -> None:
validate_one_arg(lambda x: math.cos(x), pymath.cos)
def test_tan() -> None:
validate_one_arg(lambda x: math.tan(x), pymath.tan)
def test_exp() -> None:
validate_one_arg(lambda x: math.exp(x), pymath.exp)
def test_log() -> None:
validate_one_arg(lambda x: math.log(x), pymath.log)
def test_floor() -> None:
validate_one_arg(lambda x: math.floor(x), pymath.floor)
def test_ceil() -> None:
validate_one_arg(lambda x: math.ceil(x), pymath.ceil)
def test_fabs() -> None:
validate_one_arg(lambda x: math.fabs(x), pymath.fabs)
def test_pow() -> None:
validate_two_arg(lambda x, y: math.pow(x, y), pymath.pow)
def test_copysign() -> None:
validate_two_arg(lambda x, y: math.copysign(x, y), pymath.copysign)
def test_isinf() -> None:
for x in float_vals:
assert repr(math.isinf(x)) == repr(pymath.isinf(x))
def test_isnan() -> None:
for x in float_vals:
assert repr(math.isnan(x)) == repr(pymath.isnan(x))
def test_pi_is_inlined_correctly() -> None:
assert math.pi == pi == 3.141592653589793
def test_e_is_inlined_correctly() -> None:
assert math.e == e == 2.718281828459045
def test_tau_is_inlined_correctly() -> None:
assert math.tau == tau == 6.283185307179586
def test_inf_is_inlined_correctly() -> None:
assert math.inf == inf == float("inf")
def test_nan_is_inlined_correctly() -> None:
assert math.isnan(math.nan)
assert math.isnan(nan)