|
8 | 8 | from math import atan2, isnan, copysign
|
9 | 9 | import operator
|
10 | 10 |
|
| 11 | +try: |
| 12 | + import _testcapi |
| 13 | +except ImportError: |
| 14 | + _testcapi = None |
| 15 | + |
11 | 16 | INF = float("inf")
|
12 | 17 | NAN = float("nan")
|
13 | 18 | # These tests ensure that complex math does the right thing
|
@@ -791,6 +796,34 @@ def test_format(self):
|
791 | 796 | self.assertEqual(format(complex(INF, 1), 'F'), 'INF+1.000000j')
|
792 | 797 | self.assertEqual(format(complex(INF, -1), 'F'), 'INF-1.000000j')
|
793 | 798 |
|
| 799 | + @unittest.skipIf(_testcapi is None, 'needs _testcapi') |
| 800 | + def test_PyComplex_RealAsDouble(self): |
| 801 | + from test.test_capi.test_getargs import BadComplex, Complex |
| 802 | + |
| 803 | + f = _testcapi.complex_real_as_double |
| 804 | + |
| 805 | + self.assertFloatsAreIdentical(f(1+2j), 1.0) |
| 806 | + self.assertFloatsAreIdentical(f(1), 1.0) |
| 807 | + self.assertFloatsAreIdentical(f(-1), -1.0) |
| 808 | + self.assertFloatsAreIdentical(f(Complex()), 4.25) |
| 809 | + |
| 810 | + self.assertRaises(TypeError, f, None) |
| 811 | + self.assertRaises(TypeError, f, BadComplex()) |
| 812 | + |
| 813 | + @unittest.skipIf(_testcapi is None, 'needs _testcapi') |
| 814 | + def test_PyComplex_ImagAsDouble(self): |
| 815 | + from test.test_capi.test_getargs import BadComplex, Complex |
| 816 | + |
| 817 | + f = _testcapi.complex_imag_as_double |
| 818 | + |
| 819 | + self.assertFloatsAreIdentical(f(1+2j), 2.0) |
| 820 | + self.assertFloatsAreIdentical(f(1), 0.0) |
| 821 | + self.assertFloatsAreIdentical(f(-1), 0.0) |
| 822 | + self.assertFloatsAreIdentical(f(Complex()), 0.5) |
| 823 | + |
| 824 | + self.assertRaises(TypeError, f, None) |
| 825 | + self.assertRaises(TypeError, f, BadComplex()) |
| 826 | + |
794 | 827 |
|
795 | 828 | if __name__ == "__main__":
|
796 | 829 | unittest.main()
|
0 commit comments