|
8 | 8 | NAN = float("nan")
|
9 | 9 | # These tests ensure that complex math does the right thing
|
10 | 10 |
|
| 11 | +# decorator for skipping tests on non-IEEE 754 platforms |
| 12 | +have_getformat = hasattr(float, "__getformat__") |
| 13 | +requires_IEEE_754 = unittest.skipUnless(have_getformat and |
| 14 | + float.__getformat__("double").startswith("IEEE"), |
| 15 | + "test requires IEEE 754 doubles") |
| 16 | + |
| 17 | + |
11 | 18 | class ComplexTest(unittest.TestCase):
|
12 | 19 |
|
13 | 20 | def assertAlmostEqual(self, a, b):
|
@@ -441,6 +448,28 @@ def __rmod__(self,other):
|
441 | 448 | b = 'y %s x' % op
|
442 | 449 | self.assertTrue(type(eval(a)) is type(eval(b)) is xcomplex)
|
443 | 450 |
|
| 451 | + @requires_IEEE_754 |
| 452 | + def test_constructor_special_numbers(self): |
| 453 | + class complex2(complex): |
| 454 | + pass |
| 455 | + for x in 0.0, -0.0, INF, -INF, NAN: |
| 456 | + for y in 0.0, -0.0, INF, -INF, NAN: |
| 457 | + z = complex(x, y) |
| 458 | + self.assertFloatsAreIdentical(z.real, x) |
| 459 | + self.assertFloatsAreIdentical(z.imag, y) |
| 460 | + z = complex2(x, y) |
| 461 | + self.assertIs(type(z), complex2) |
| 462 | + self.assertFloatsAreIdentical(z.real, x) |
| 463 | + self.assertFloatsAreIdentical(z.imag, y) |
| 464 | + z = complex(complex2(x, y)) |
| 465 | + self.assertIs(type(z), complex) |
| 466 | + self.assertFloatsAreIdentical(z.real, x) |
| 467 | + self.assertFloatsAreIdentical(z.imag, y) |
| 468 | + z = complex2(complex(x, y)) |
| 469 | + self.assertIs(type(z), complex2) |
| 470 | + self.assertFloatsAreIdentical(z.real, x) |
| 471 | + self.assertFloatsAreIdentical(z.imag, y) |
| 472 | + |
444 | 473 | def test_hash(self):
|
445 | 474 | for x in xrange(-30, 30):
|
446 | 475 | self.assertEqual(hash(x), hash(complex(x, 0)))
|
|
0 commit comments