|
| 1 | +import errno |
1 | 2 | import unittest |
2 | 3 | import sys |
3 | 4 | from test import support |
| 5 | +from test.support import import_helper |
4 | 6 | from test.support.testcase import ComplexesAreIdenticalMixin |
5 | 7 | from test.support.numbers import ( |
6 | 8 | VALID_UNDERSCORE_LITERALS, |
|
9 | 11 |
|
10 | 12 | from random import random |
11 | 13 | from math import isnan, copysign |
| 14 | +import cmath |
12 | 15 | import operator |
13 | 16 |
|
14 | 17 | INF = float("inf") |
@@ -613,8 +616,30 @@ def test_abs(self): |
613 | 616 | for num in nums: |
614 | 617 | self.assertAlmostEqual((num.real**2 + num.imag**2) ** 0.5, abs(num)) |
615 | 618 |
|
| 619 | + for x in 0.0, -0.0, INF, -INF, NAN: |
| 620 | + for y in 0.0, -0.0, INF, -INF, NAN: |
| 621 | + with self.subTest(x=x, y=y): |
| 622 | + z = complex(x, y) |
| 623 | + r = abs(z) |
| 624 | + if cmath.isfinite(z): |
| 625 | + self.assertFloatsAreIdentical(r, 0.0) |
| 626 | + elif cmath.isinf(z): |
| 627 | + self.assertEqual(r, INF) |
| 628 | + else: |
| 629 | + self.assertTrue(cmath.isnan(z)) |
| 630 | + self.assertTrue(isnan(r)) |
| 631 | + |
616 | 632 | self.assertRaises(OverflowError, abs, complex(DBL_MAX, DBL_MAX)) |
617 | 633 |
|
| 634 | + def test_abs_errno_handling(self): |
| 635 | + _testcapi = import_helper.import_module('_testcapi') |
| 636 | + z = complex('nan') |
| 637 | + _testcapi.set_errno(errno.ERANGE) |
| 638 | + try: |
| 639 | + self.assertTrue(isnan(abs(z))) |
| 640 | + finally: |
| 641 | + _testcapi.set_errno(0) |
| 642 | + |
618 | 643 | def test_repr_str(self): |
619 | 644 | def test(v, expected, test_fn=self.assertEqual): |
620 | 645 | test_fn(repr(v), expected) |
|
0 commit comments