-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathtest_complex.py
299 lines (224 loc) · 10.6 KB
/
test_complex.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
from math import isnan
import errno
import unittest
import warnings
from test.test_capi.test_getargs import (BadComplex, BadComplex2, Complex,
FloatSubclass, Float, BadFloat,
BadFloat2, ComplexSubclass)
from test.support import import_helper
from test.support.testcase import ComplexesAreIdenticalMixin
_testcapi = import_helper.import_module('_testcapi')
_testlimitedcapi = import_helper.import_module('_testlimitedcapi')
_testinternalcapi = import_helper.import_module('_testinternalcapi')
NULL = None
INF = float("inf")
NAN = float("nan")
DBL_MAX = _testcapi.DBL_MAX
class BadComplex3:
def __complex__(self):
raise RuntimeError
class CAPIComplexTest(ComplexesAreIdenticalMixin, unittest.TestCase):
def test_check(self):
# Test PyComplex_Check()
check = _testlimitedcapi.complex_check
self.assertTrue(check(1+2j))
self.assertTrue(check(ComplexSubclass(1+2j)))
self.assertFalse(check(Complex()))
self.assertFalse(check(3))
self.assertFalse(check(3.0))
self.assertFalse(check(object()))
# CRASHES check(NULL)
def test_checkexact(self):
# PyComplex_CheckExact()
checkexact = _testlimitedcapi.complex_checkexact
self.assertTrue(checkexact(1+2j))
self.assertFalse(checkexact(ComplexSubclass(1+2j)))
self.assertFalse(checkexact(Complex()))
self.assertFalse(checkexact(3))
self.assertFalse(checkexact(3.0))
self.assertFalse(checkexact(object()))
# CRASHES checkexact(NULL)
def test_fromccomplex(self):
# Test PyComplex_FromCComplex()
fromccomplex = _testcapi.complex_fromccomplex
self.assertEqual(fromccomplex(1+2j), 1.0+2.0j)
def test_fromdoubles(self):
# Test PyComplex_FromDoubles()
fromdoubles = _testlimitedcapi.complex_fromdoubles
self.assertEqual(fromdoubles(1.0, 2.0), 1.0+2.0j)
def test_realasdouble(self):
# Test PyComplex_RealAsDouble()
realasdouble = _testlimitedcapi.complex_realasdouble
self.assertEqual(realasdouble(1+2j), 1.0)
self.assertEqual(realasdouble(-1+0j), -1.0)
self.assertEqual(realasdouble(4.25), 4.25)
self.assertEqual(realasdouble(-1.0), -1.0)
self.assertEqual(realasdouble(42), 42.)
self.assertEqual(realasdouble(-1), -1.0)
# Test subclasses of complex/float
self.assertEqual(realasdouble(ComplexSubclass(1+2j)), 1.0)
self.assertEqual(realasdouble(FloatSubclass(4.25)), 4.25)
# Test types with __complex__ dunder method
self.assertEqual(realasdouble(Complex()), 4.25)
self.assertRaises(TypeError, realasdouble, BadComplex())
with self.assertWarns(DeprecationWarning):
self.assertEqual(realasdouble(BadComplex2()), 4.25)
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
self.assertRaises(DeprecationWarning, realasdouble, BadComplex2())
self.assertRaises(RuntimeError, realasdouble, BadComplex3())
# Test types with __float__ dunder method
self.assertEqual(realasdouble(Float()), 4.25)
self.assertRaises(TypeError, realasdouble, BadFloat())
with self.assertWarns(DeprecationWarning):
self.assertEqual(realasdouble(BadFloat2()), 4.25)
self.assertRaises(TypeError, realasdouble, object())
# CRASHES realasdouble(NULL)
def test_imagasdouble(self):
# Test PyComplex_ImagAsDouble()
imagasdouble = _testlimitedcapi.complex_imagasdouble
self.assertEqual(imagasdouble(1+2j), 2.0)
self.assertEqual(imagasdouble(1-1j), -1.0)
self.assertEqual(imagasdouble(4.25), 0.0)
self.assertEqual(imagasdouble(42), 0.0)
# Test subclasses of complex/float
self.assertEqual(imagasdouble(ComplexSubclass(1+2j)), 2.0)
self.assertEqual(imagasdouble(FloatSubclass(4.25)), 0.0)
# Test types with __complex__ dunder method
self.assertEqual(imagasdouble(Complex()), 0.5)
self.assertRaises(TypeError, imagasdouble, BadComplex())
with self.assertWarns(DeprecationWarning):
self.assertEqual(imagasdouble(BadComplex2()), 0.5)
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
self.assertRaises(DeprecationWarning, imagasdouble, BadComplex2())
self.assertRaises(RuntimeError, imagasdouble, BadComplex3())
# Test types with __float__ dunder method
self.assertEqual(imagasdouble(Float()), 0.0)
self.assertRaises(TypeError, imagasdouble, BadFloat())
with self.assertWarns(DeprecationWarning):
self.assertEqual(imagasdouble(BadFloat2()), 0.0)
self.assertRaises(TypeError, imagasdouble, object())
# CRASHES imagasdouble(NULL)
def test_asccomplex(self):
# Test PyComplex_AsCComplex()
asccomplex = _testcapi.complex_asccomplex
self.assertEqual(asccomplex(1+2j), 1.0+2.0j)
self.assertEqual(asccomplex(-1+2j), -1.0+2.0j)
self.assertEqual(asccomplex(4.25), 4.25+0.0j)
self.assertEqual(asccomplex(-1.0), -1.0+0.0j)
self.assertEqual(asccomplex(42), 42+0j)
self.assertEqual(asccomplex(-1), -1.0+0.0j)
# Test subclasses of complex/float
self.assertEqual(asccomplex(ComplexSubclass(1+2j)), 1.0+2.0j)
self.assertEqual(asccomplex(FloatSubclass(4.25)), 4.25+0.0j)
# Test types with __complex__ dunder method
self.assertEqual(asccomplex(Complex()), 4.25+0.5j)
self.assertRaises(TypeError, asccomplex, BadComplex())
with self.assertWarns(DeprecationWarning):
self.assertEqual(asccomplex(BadComplex2()), 4.25+0.5j)
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
self.assertRaises(DeprecationWarning, asccomplex, BadComplex2())
self.assertRaises(RuntimeError, asccomplex, BadComplex3())
# Test types with __float__ dunder method
self.assertEqual(asccomplex(Float()), 4.25+0.0j)
self.assertRaises(TypeError, asccomplex, BadFloat())
with self.assertWarns(DeprecationWarning):
self.assertEqual(asccomplex(BadFloat2()), 4.25+0.0j)
self.assertRaises(TypeError, asccomplex, object())
# CRASHES asccomplex(NULL)
def test_py_c_sum(self):
# Test _Py_c_sum()
_py_c_sum = _testcapi._py_c_sum
self.assertEqual(_py_c_sum(1, 1j), (1+1j, 0))
def test_py_cr_sum(self):
# Test _Py_cr_sum()
_py_cr_sum = _testinternalcapi._py_cr_sum
self.assertComplexesAreIdentical(_py_cr_sum(-0j, -0.0)[0],
complex(-0.0, -0.0))
def test_py_c_diff(self):
# Test _Py_c_diff()
_py_c_diff = _testcapi._py_c_diff
self.assertEqual(_py_c_diff(1, 1j), (1-1j, 0))
def test_py_cr_diff(self):
# Test _Py_cr_diff()
_py_cr_diff = _testinternalcapi._py_cr_diff
self.assertComplexesAreIdentical(_py_cr_diff(-0j, 0.0)[0],
complex(-0.0, -0.0))
def test_py_rc_diff(self):
# Test _Py_rc_diff()
_py_rc_diff = _testinternalcapi._py_rc_diff
self.assertComplexesAreIdentical(_py_rc_diff(-0.0, 0j)[0],
complex(-0.0, -0.0))
def test_py_c_neg(self):
# Test _Py_c_neg()
_py_c_neg = _testcapi._py_c_neg
self.assertEqual(_py_c_neg(1+1j), -1-1j)
def test_py_c_prod(self):
# Test _Py_c_prod()
_py_c_prod = _testcapi._py_c_prod
self.assertEqual(_py_c_prod(2, 1j), (2j, 0))
def test_py_cr_prod(self):
# Test _Py_cr_prod()
_py_cr_prod = _testinternalcapi._py_cr_prod
self.assertComplexesAreIdentical(_py_cr_prod(complex('inf+1j'), INF)[0],
complex('inf+infj'))
def test_py_c_quot(self):
# Test _Py_c_quot()
_py_c_quot = _testcapi._py_c_quot
self.assertEqual(_py_c_quot(1, 1j), (-1j, 0))
self.assertEqual(_py_c_quot(1, -1j), (1j, 0))
self.assertEqual(_py_c_quot(1j, 2), (0.5j, 0))
self.assertEqual(_py_c_quot(1j, -2), (-0.5j, 0))
self.assertEqual(_py_c_quot(1, 2j), (-0.5j, 0))
z, e = _py_c_quot(NAN, 1j)
self.assertTrue(isnan(z.real))
self.assertTrue(isnan(z.imag))
self.assertEqual(e, 0)
z, e = _py_c_quot(1j, NAN)
self.assertTrue(isnan(z.real))
self.assertTrue(isnan(z.imag))
self.assertEqual(e, 0)
self.assertEqual(_py_c_quot(1, 0j)[1], errno.EDOM)
def test_py_cr_quot(self):
# Test _Py_cr_quot()
_py_cr_quot = _testinternalcapi._py_cr_quot
self.assertComplexesAreIdentical(_py_cr_quot(complex('inf+1j'), 2**1000)[0],
INF + 2**-1000*1j)
def test_py_rc_quot(self):
# Test _Py_rc_quot()
_py_rc_quot = _testinternalcapi._py_rc_quot
self.assertComplexesAreIdentical(_py_rc_quot(1.0, complex('nan-infj'))[0],
0j)
def test_py_c_pow(self):
# Test _Py_c_pow()
_py_c_pow = _testcapi._py_c_pow
self.assertEqual(_py_c_pow(1j, 0j), (1+0j, 0))
self.assertEqual(_py_c_pow(1, 1j), (1+0j, 0))
self.assertEqual(_py_c_pow(0j, 1), (0j, 0))
self.assertAlmostEqual(_py_c_pow(1j, 2)[0], -1.0+0j)
r, e = _py_c_pow(1+1j, -1)
self.assertAlmostEqual(r, 0.5-0.5j)
self.assertEqual(e, 0)
self.assertEqual(_py_c_pow(0j, -1)[1], errno.EDOM)
self.assertEqual(_py_c_pow(0j, 1j)[1], errno.EDOM)
max_num = DBL_MAX+1j
self.assertEqual(_py_c_pow(max_num, max_num),
(complex(INF, INF), errno.ERANGE))
self.assertEqual(_py_c_pow(max_num, 2),
(complex(INF, INF), errno.ERANGE))
def test_py_c_abs(self):
# Test _Py_c_abs()
_py_c_abs = _testcapi._py_c_abs
self.assertEqual(_py_c_abs(-1), (1.0, 0))
self.assertEqual(_py_c_abs(1j), (1.0, 0))
self.assertEqual(_py_c_abs(complex('+inf+1j')), (INF, 0))
self.assertEqual(_py_c_abs(complex('-inf+1j')), (INF, 0))
self.assertEqual(_py_c_abs(complex('1.25+infj')), (INF, 0))
self.assertEqual(_py_c_abs(complex('1.25-infj')), (INF, 0))
self.assertTrue(isnan(_py_c_abs(complex('1.25+nanj'))[0]))
self.assertTrue(isnan(_py_c_abs(complex('nan-1j'))[0]))
self.assertEqual(_py_c_abs(complex(*[DBL_MAX]*2))[1], errno.ERANGE)
if __name__ == "__main__":
unittest.main()