Skip to content

Commit c8256c5

Browse files
committed
Update Lib/test/test_cmath.py from CPython v3.12.0
1 parent e06f5cc commit c8256c5

File tree

1 file changed

+18
-33
lines changed

1 file changed

+18
-33
lines changed

Lib/test/test_cmath.py

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ def test_infinity_and_nan_constants(self):
166166
self.assertEqual(cmath.nan.imag, 0.0)
167167
self.assertEqual(cmath.nanj.real, 0.0)
168168
self.assertTrue(math.isnan(cmath.nanj.imag))
169+
# Also check that the sign of all of these is positive:
170+
self.assertEqual(math.copysign(1., cmath.nan.real), 1.)
171+
self.assertEqual(math.copysign(1., cmath.nan.imag), 1.)
172+
self.assertEqual(math.copysign(1., cmath.nanj.real), 1.)
173+
self.assertEqual(math.copysign(1., cmath.nanj.imag), 1.)
169174

170175
# Check consistency with reprs.
171176
self.assertEqual(repr(cmath.inf), "inf")
@@ -192,14 +197,7 @@ def test_user_object(self):
192197
# end up being passed to the cmath functions
193198

194199
# usual case: new-style class implementing __complex__
195-
class MyComplex(object):
196-
def __init__(self, value):
197-
self.value = value
198-
def __complex__(self):
199-
return self.value
200-
201-
# old-style class implementing __complex__
202-
class MyComplexOS:
200+
class MyComplex:
203201
def __init__(self, value):
204202
self.value = value
205203
def __complex__(self):
@@ -208,18 +206,13 @@ def __complex__(self):
208206
# classes for which __complex__ raises an exception
209207
class SomeException(Exception):
210208
pass
211-
class MyComplexException(object):
212-
def __complex__(self):
213-
raise SomeException
214-
class MyComplexExceptionOS:
209+
class MyComplexException:
215210
def __complex__(self):
216211
raise SomeException
217212

218213
# some classes not providing __float__ or __complex__
219214
class NeitherComplexNorFloat(object):
220215
pass
221-
class NeitherComplexNorFloatOS:
222-
pass
223216
class Index:
224217
def __int__(self): return 2
225218
def __index__(self): return 2
@@ -228,48 +221,32 @@ def __int__(self): return 2
228221

229222
# other possible combinations of __float__ and __complex__
230223
# that should work
231-
class FloatAndComplex(object):
224+
class FloatAndComplex:
232225
def __float__(self):
233226
return flt_arg
234227
def __complex__(self):
235228
return cx_arg
236-
class FloatAndComplexOS:
237-
def __float__(self):
238-
return flt_arg
239-
def __complex__(self):
240-
return cx_arg
241-
class JustFloat(object):
242-
def __float__(self):
243-
return flt_arg
244-
class JustFloatOS:
229+
class JustFloat:
245230
def __float__(self):
246231
return flt_arg
247232

248233
for f in self.test_functions:
249234
# usual usage
250235
self.assertEqual(f(MyComplex(cx_arg)), f(cx_arg))
251-
self.assertEqual(f(MyComplexOS(cx_arg)), f(cx_arg))
252236
# other combinations of __float__ and __complex__
253237
self.assertEqual(f(FloatAndComplex()), f(cx_arg))
254-
self.assertEqual(f(FloatAndComplexOS()), f(cx_arg))
255238
self.assertEqual(f(JustFloat()), f(flt_arg))
256-
self.assertEqual(f(JustFloatOS()), f(flt_arg))
257239
self.assertEqual(f(Index()), f(int(Index())))
258240
# TypeError should be raised for classes not providing
259241
# either __complex__ or __float__, even if they provide
260-
# __int__ or __index__. An old-style class
261-
# currently raises AttributeError instead of a TypeError;
262-
# this could be considered a bug.
242+
# __int__ or __index__:
263243
self.assertRaises(TypeError, f, NeitherComplexNorFloat())
264244
self.assertRaises(TypeError, f, MyInt())
265-
self.assertRaises(Exception, f, NeitherComplexNorFloatOS())
266245
# non-complex return value from __complex__ -> TypeError
267246
for bad_complex in non_complexes:
268247
self.assertRaises(TypeError, f, MyComplex(bad_complex))
269-
self.assertRaises(TypeError, f, MyComplexOS(bad_complex))
270248
# exceptions in __complex__ should be propagated correctly
271249
self.assertRaises(SomeException, f, MyComplexException())
272-
self.assertRaises(SomeException, f, MyComplexExceptionOS())
273250

274251
def test_input_type(self):
275252
# ints should be acceptable inputs to all cmath
@@ -647,6 +624,14 @@ def test_complex_near_zero(self):
647624
self.assertIsClose(0.001-0.001j, 0.001+0.001j, abs_tol=2e-03)
648625
self.assertIsNotClose(0.001-0.001j, 0.001+0.001j, abs_tol=1e-03)
649626

627+
def test_complex_special(self):
628+
self.assertIsNotClose(INF, INF*1j)
629+
self.assertIsNotClose(INF*1j, INF)
630+
self.assertIsNotClose(INF, -INF)
631+
self.assertIsNotClose(-INF, INF)
632+
self.assertIsNotClose(0, INF)
633+
self.assertIsNotClose(0, INF*1j)
634+
650635

651636
if __name__ == "__main__":
652637
unittest.main()

0 commit comments

Comments
 (0)