|
| 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") |
@@ -789,8 +792,30 @@ def test_abs(self): |
789 | 792 | for num in nums: |
790 | 793 | self.assertAlmostEqual((num.real**2 + num.imag**2) ** 0.5, abs(num)) |
791 | 794 |
|
| 795 | + for x in 0.0, -0.0, INF, -INF, NAN: |
| 796 | + for y in 0.0, -0.0, INF, -INF, NAN: |
| 797 | + with self.subTest(x=x, y=y): |
| 798 | + z = complex(x, y) |
| 799 | + r = abs(z) |
| 800 | + if cmath.isfinite(z): |
| 801 | + self.assertFloatsAreIdentical(r, 0.0) |
| 802 | + elif cmath.isinf(z): |
| 803 | + self.assertEqual(r, INF) |
| 804 | + else: |
| 805 | + self.assertTrue(cmath.isnan(z)) |
| 806 | + self.assertTrue(isnan(r)) |
| 807 | + |
792 | 808 | self.assertRaises(OverflowError, abs, complex(DBL_MAX, DBL_MAX)) |
793 | 809 |
|
| 810 | + def test_abs_errno_handling(self): |
| 811 | + _testcapi = import_helper.import_module('_testcapi') |
| 812 | + z = complex('nan') |
| 813 | + _testcapi.set_errno(errno.ERANGE) |
| 814 | + try: |
| 815 | + self.assertTrue(isnan(abs(z))) |
| 816 | + finally: |
| 817 | + _testcapi.set_errno(0) |
| 818 | + |
794 | 819 | def test_repr_str(self): |
795 | 820 | def test(v, expected, test_fn=self.assertEqual): |
796 | 821 | test_fn(repr(v), expected) |
|
0 commit comments