|
3 | 3 | from test import support
|
4 | 4 | from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
|
5 | 5 | INVALID_UNDERSCORE_LITERALS)
|
| 6 | +from test.support import import_helper |
6 | 7 |
|
7 | 8 | from random import random
|
8 | 9 | from math import atan2, isnan, copysign
|
9 | 10 | import operator
|
10 | 11 |
|
| 12 | +_testcapi = import_helper.import_module('_testcapi') |
| 13 | + |
11 | 14 | INF = float("inf")
|
12 | 15 | NAN = float("nan")
|
13 | 16 | # These tests ensure that complex math does the right thing
|
|
20 | 23 | (1, 0+0j),
|
21 | 24 | )
|
22 | 25 |
|
| 26 | +NULL = None |
| 27 | + |
23 | 28 | class ComplexTest(unittest.TestCase):
|
24 | 29 |
|
25 | 30 | def assertAlmostEqual(self, a, b):
|
@@ -72,6 +77,15 @@ def assertFloatsAreIdentical(self, x, y):
|
72 | 77 | msg += ': zeros have different signs'
|
73 | 78 | self.fail(msg.format(x, y))
|
74 | 79 |
|
| 80 | + def assertComplexesAreIdentical(self, x, y): |
| 81 | + """assert that complex numbers x and y are identical |
| 82 | +
|
| 83 | + I.e. they have identical real and imag components. |
| 84 | +
|
| 85 | + """ |
| 86 | + (self.assertFloatsAreIdentical(x.real, y.real) |
| 87 | + and self.assertFloatsAreIdentical(x.imag, y.imag)) |
| 88 | + |
75 | 89 | def assertClose(self, x, y, eps=1e-9):
|
76 | 90 | """Return true iff complexes x and y "are close"."""
|
77 | 91 | self.assertCloseAbs(x.real, y.real, eps)
|
@@ -792,5 +806,94 @@ def test_format(self):
|
792 | 806 | self.assertEqual(format(complex(INF, -1), 'F'), 'INF-1.000000j')
|
793 | 807 |
|
794 | 808 |
|
| 809 | +class CAPIComplexTest(ComplexTest): |
| 810 | + def test_check(self): |
| 811 | + # Test PyComplex_Check() |
| 812 | + from test.test_capi.test_getargs import Complex, ComplexSubclass |
| 813 | + check = _testcapi.complex_check |
| 814 | + |
| 815 | + self.assertTrue(check(1+2j)) |
| 816 | + self.assertTrue(check(ComplexSubclass(1+2j))) |
| 817 | + self.assertFalse(check(Complex())) |
| 818 | + self.assertFalse(check(3)) |
| 819 | + self.assertFalse(check([])) |
| 820 | + self.assertFalse(check(object())) |
| 821 | + |
| 822 | + # CRASHES check(NULL) |
| 823 | + |
| 824 | + def test_checkexact(self): |
| 825 | + # PyComplex_CheckExact() |
| 826 | + from test.test_capi.test_getargs import Complex, ComplexSubclass |
| 827 | + checkexact = _testcapi.complex_checkexact |
| 828 | + |
| 829 | + self.assertTrue(checkexact(1+2j)) |
| 830 | + self.assertFalse(checkexact(ComplexSubclass(1+2j))) |
| 831 | + self.assertFalse(checkexact(Complex())) |
| 832 | + self.assertFalse(checkexact(3)) |
| 833 | + self.assertFalse(checkexact([])) |
| 834 | + self.assertFalse(checkexact(object())) |
| 835 | + |
| 836 | + # CRASHES checkexact(NULL) |
| 837 | + |
| 838 | + def test_fromccomplex(self): |
| 839 | + # Test PyComplex_FromCComplex() |
| 840 | + from test.test_capi.test_getargs import ComplexSubclass |
| 841 | + fromccomplex = _testcapi.complex_fromccomplex |
| 842 | + |
| 843 | + self.assertComplexesAreIdentical(fromccomplex(1+2j), 1.0+2.0j) |
| 844 | + |
| 845 | + def test_fromdoubles(self): |
| 846 | + # Test PyComplex_FromDoubles() |
| 847 | + fromdoubles = _testcapi.complex_fromdoubles |
| 848 | + |
| 849 | + self.assertComplexesAreIdentical(fromdoubles(1.0, 2.0), 1.0+2.0j) |
| 850 | + |
| 851 | + def test_realasdouble(self): |
| 852 | + # Test PyComplex_RealAsDouble() |
| 853 | + from test.test_capi.test_getargs import BadComplex, Complex |
| 854 | + realasdouble = _testcapi.complex_realasdouble |
| 855 | + |
| 856 | + self.assertFloatsAreIdentical(realasdouble(1+2j), 1.0) |
| 857 | + self.assertFloatsAreIdentical(realasdouble(1), 1.0) |
| 858 | + self.assertFloatsAreIdentical(realasdouble(-1), -1.0) |
| 859 | + # Function doesn't support classes with __complex__ dunder, see #109598 |
| 860 | + #self.assertFloatsAreIdentical(realasdouble(Complex()), 4.25) |
| 861 | + |
| 862 | + self.assertRaises(TypeError, realasdouble, BadComplex()) |
| 863 | + |
| 864 | + # CRASHES realasdouble(NULL) |
| 865 | + |
| 866 | + def test_imagasdouble(self): |
| 867 | + # Test PyComplex_ImagAsDouble() |
| 868 | + from test.test_capi.test_getargs import BadComplex, Complex |
| 869 | + imagasdouble = _testcapi.complex_imagasdouble |
| 870 | + |
| 871 | + self.assertFloatsAreIdentical(imagasdouble(1+2j), 2.0) |
| 872 | + self.assertFloatsAreIdentical(imagasdouble(1), 0.0) |
| 873 | + self.assertFloatsAreIdentical(imagasdouble(-1), 0.0) |
| 874 | + # Function doesn't support classes with __complex__ dunder, see #109598 |
| 875 | + #self.assertFloatsAreIdentical(imagasdouble(Complex()), 0.5) |
| 876 | + |
| 877 | + # Function returns 0.0 anyway, see #109598 |
| 878 | + #self.assertRaises(TypeError, imagasdouble, BadComplex()) |
| 879 | + self.assertFloatsAreIdentical(imagasdouble(BadComplex()), 0.0) |
| 880 | + |
| 881 | + # CRASHES imagasdouble(NULL) |
| 882 | + |
| 883 | + def test_asccomplex(self): |
| 884 | + # Test PyComplex_AsCComplex() |
| 885 | + from test.test_capi.test_getargs import BadComplex, Complex |
| 886 | + asccomplex = _testcapi.complex_asccomplex |
| 887 | + |
| 888 | + self.assertComplexesAreIdentical(asccomplex(1+2j), 1.0+2.0j) |
| 889 | + self.assertComplexesAreIdentical(asccomplex(1), 1.0+0.0j) |
| 890 | + self.assertComplexesAreIdentical(asccomplex(-1), -1.0+0.0j) |
| 891 | + self.assertComplexesAreIdentical(asccomplex(Complex()), 4.25+0.5j) |
| 892 | + |
| 893 | + self.assertRaises(TypeError, asccomplex, BadComplex()) |
| 894 | + |
| 895 | + # CRASHES asccomplex(NULL) |
| 896 | + |
| 897 | + |
795 | 898 | if __name__ == "__main__":
|
796 | 899 | unittest.main()
|
0 commit comments