Debian Codegraph @

mypyc/test-data/run-math.test

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