Debian Codegraph @

pypy/module/math/test/apptest_math.py

raw symbols SCIP
1 import pytest
2 import math
3 import sys
4
5 def test_product():
6 assert math.prod([1, 2, 3]) == 6
7 assert math.prod([1, 2, 3], start=0.5) == 3.0
8 assert math.prod([]) == 1.0
9 assert math.prod([], start=5) == 5
10
11 def test_julians_weird_test_prod():
12 class A:
13 def __mul__(self, other):
14 return 12
15 def __imul__(self, other):
16 return 13
17
18 # check that the implementation doesn't use *=
19 assert math.prod([1, 2], start=A())
20
21 def test_more_weird_prod():
22 start = [4]
23 assert math.prod([2], start=start) == [4, 4]
24 assert start == [4]
25 start = object()
26 assert math.prod([], start=start) is start
27
28
29 def test_comb():
30 from math import comb, factorial
31
32 assert comb(10, 11) == 0
33 for n in range(5):
34 for k in range(n + 1):
35 assert comb(n, k) == factorial(n) // (factorial(k) * factorial(n - k))
36
37 class A:
38 def __index__(self):
39 return 4
40
41 assert comb(A(), 2) == comb(4, 2)
42
43
44 def test_perm():
45 from math import perm, factorial
46
47 assert perm(10, 11) == 0
48
49 for n in range(5):
50 for k in range(n + 1):
51 assert perm(n, k) == factorial(n) // factorial(n - k)
52
53 class A:
54 def __index__(self):
55 return 4
56
57 assert perm(A(), 2) == perm(4, 2)
58
59 def test_hypot_many_args():
60 from math import hypot
61 args = math.e, math.pi, math.sqrt(2.0), math.gamma(3.5), math.sin(2.1), 1e48, 2e-47
62 for i in range(len(args)+1):
63 assert round(
64 hypot(*args[:i]) - math.sqrt(sum(s**2 for s in args[:i])), 7) == 0
65
66
67 def test_dist():
68 from math import dist
69 assert dist((1.0, 2.0, 3.0), (4.0, 2.0, -1.0)) == 5.0
70 assert dist((1, 2, 3), (4, 2, -1)) == 5.0
71 with pytest.raises(TypeError):
72 math.dist(p=(1, 2, 3), q=(2, 3, 4)) # posonly args :-/
73
74 def test_nextafter():
75 INF = float("inf")
76 NAN = float("nan")
77 assert math.nextafter(4503599627370496.0, -INF) == 4503599627370495.5
78 assert math.nextafter(4503599627370496.0, INF) == 4503599627370497.0
79 assert math.nextafter(9223372036854775808.0, 0.0) == 9223372036854774784.0
80 assert math.nextafter(-9223372036854775808.0, 0.0) == -9223372036854774784.0
81
82 # around 1.0
83 assert math.nextafter(1.0, -INF) == float.fromhex('0x1.fffffffffffffp-1')
84 assert math.nextafter(1.0, INF)== float.fromhex('0x1.0000000000001p+0')
85
86 # x == y: y is returned
87 assert math.nextafter(2.0, 2.0) == 2.0
88
89 # around 0.0
90 smallest_subnormal = sys.float_info.min * sys.float_info.epsilon
91 assert math.nextafter(+0.0, INF) == smallest_subnormal
92 assert math.nextafter(-0.0, INF) == smallest_subnormal
93 assert math.nextafter(+0.0, -INF) == -smallest_subnormal
94 assert math.nextafter(-0.0, -INF) == -smallest_subnormal
95
96 # around infinity
97 largest_normal = sys.float_info.max
98 assert math.nextafter(INF, 0.0) == largest_normal
99 assert math.nextafter(-INF, 0.0) == -largest_normal
100 assert math.nextafter(largest_normal, INF) == INF
101 assert math.nextafter(-largest_normal, -INF) == -INF
102
103 # NaN
104 assert math.isnan(math.nextafter(NAN, 1.0))
105 assert math.isnan(math.nextafter(1.0, NAN))
106 assert math.isnan(math.nextafter(NAN, NAN))
107
108 def test_ulp():
109 INF = float("inf")
110 NAN = float("nan")
111 FLOAT_MAX = sys.float_info.max
112 assert math.ulp(1.0) == sys.float_info.epsilon
113 assert math.ulp(2 ** 52) == 1.0
114 assert math.ulp(2 ** 53) == 2.0
115 assert math.ulp(2 ** 64) == 4096.0
116
117 assert math.ulp(0.0) == sys.float_info.min * sys.float_info.epsilon
118 assert math.ulp(FLOAT_MAX) == FLOAT_MAX - math.nextafter(FLOAT_MAX, -INF)
119
120 # special cases
121 assert math.ulp(INF) == INF
122 assert math.isnan(math.ulp(math.nan))
123
124 # negative number: ulp(-x) == ulp(x)
125 for x in (0.0, 1.0, 2 ** 52, 2 ** 64, INF):
126 assert math.ulp(-x) == math.ulp(x)
127
128 def test_factorial_raises():
129 with pytest.raises(TypeError) as e:
130 math.factorial(1.2)
131 assert e.value.args[0] == "'float' object cannot be interpreted as an integer"
132
133 @pytest.mark.skipif(sys.maxsize <= 2**31 - 1, reason="bigint on 32-bytes is slow")
134 def test_factorial_values():
135 def ref(n):
136 r = 1
137 for i in range(2, n + 1):
138 r *= i
139 return r
140 for x in range(1000):
141 assert math.factorial(x) == ref(x)
142
143 @pytest.mark.skipif(not hasattr(sys, 'pypy_translation_info'), reason='requires translated PyPy')
144 def test_signatures():
145 import inspect
146 assert str(inspect.signature(math.factorial)) == '(n, /)'
147 assert str(inspect.signature(math.isqrt)) == '(n, /)'
148 assert str(inspect.signature(math.gcd)) == '(*integers)'
149 assert str(inspect.signature(math.lcm)) == '(*integers)'
150