| 1 |
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase |
| 2 |
from pypy.module.cpyext.test.test_api import BaseApiTest, raises_w |
| 3 |
from pypy.module.cpyext.complexobject import ( |
| 4 |
PyComplex_FromDoubles, PyComplex_RealAsDouble, PyComplex_ImagAsDouble) |
| 5 |
|
| 6 |
class TestComplexObject(BaseApiTest): |
| 7 |
def test_complexobject(self, space): |
| 8 |
w_value = PyComplex_FromDoubles(space, 1.2, 3.4) |
| 9 |
assert space.unwrap(w_value) == 1.2+3.4j |
| 10 |
assert PyComplex_RealAsDouble(space, w_value) == 1.2 |
| 11 |
assert PyComplex_ImagAsDouble(space, w_value) == 3.4 |
| 12 |
|
| 13 |
assert PyComplex_RealAsDouble(space, space.wrap(42)) == 42 |
| 14 |
assert PyComplex_RealAsDouble(space, space.wrap(1.5)) == 1.5 |
| 15 |
assert PyComplex_ImagAsDouble(space, space.wrap(1.5)) == 0.0 |
| 16 |
|
| 17 |
|
| 18 |
assert PyComplex_ImagAsDouble(space, space.w_None) == 0.0 |
| 19 |
with raises_w(space, TypeError): |
| 20 |
PyComplex_RealAsDouble(space, space.w_None) |
| 21 |
|
| 22 |
class AppTestCComplex(AppTestCpythonExtensionBase): |
| 23 |
def test_AsCComplex(self): |
| 24 |
module = self.import_extension('foo', [ |
| 25 |
("as_tuple", "METH_O", |
| 26 |
""" |
| 27 |
Py_complex c = PyComplex_AsCComplex(args); |
| 28 |
if (PyErr_Occurred()) return NULL; |
| 29 |
return Py_BuildValue("dd", c.real, c.imag); |
| 30 |
""")]) |
| 31 |
assert module.as_tuple(12-34j) == (12, -34) |
| 32 |
assert module.as_tuple(-3.14) == (-3.14, 0.0) |
| 33 |
raises(TypeError, module.as_tuple, "12") |
| 34 |
|
| 35 |
def test_FromCComplex(self): |
| 36 |
module = self.import_extension('foo', [ |
| 37 |
("test", "METH_NOARGS", |
| 38 |
""" |
| 39 |
Py_complex c = {1.2, 3.4}; |
| 40 |
return PyComplex_FromCComplex(c); |
| 41 |
""")]) |
| 42 |
assert module.test() == 1.2 + 3.4j |
| 43 |
|
| 44 |
def test_PyComplex_to_WComplex(self): |
| 45 |
module = self.import_extension('foo', [ |
| 46 |
("test", "METH_NOARGS", |
| 47 |
""" |
| 48 |
Py_complex c = {1.2, 3.4}; |
| 49 |
PyObject *obj = PyObject_Malloc(sizeof(PyComplexObject)); |
| 50 |
obj = PyObject_Init(obj, &PyComplex_Type); |
| 51 |
assert(obj != NULL); |
| 52 |
((PyComplexObject *)obj)->cval = c; |
| 53 |
return obj; |
| 54 |
""")]) |
| 55 |
assert module.test() == 1.2 + 3.4j |
| 56 |
|
| 57 |
def test_WComplex_to_PyComplex(self): |
| 58 |
module = self.import_extension('foo', [ |
| 59 |
("test", "METH_O", |
| 60 |
""" |
| 61 |
Py_complex c = ((PyComplexObject *)args)->cval; |
| 62 |
return Py_BuildValue("dd", c.real, c.imag); |
| 63 |
""")]) |
| 64 |
assert module.test(1.2 + 3.4j) == (1.2, 3.4) |
| 65 |
|
| 66 |
def test_PyComplex_AsCComplex(self): |
| 67 |
module = self.import_extension('foo', [ |
| 68 |
("test", "METH_O", |
| 69 |
""" |
| 70 |
Py_complex c = PyComplex_AsCComplex(args); |
| 71 |
if (c.real == -1. && PyErr_Occurred()) { |
| 72 |
return NULL; |
| 73 |
} |
| 74 |
return Py_BuildValue("dd", c.real, c.imag); |
| 75 |
""")]) |
| 76 |
|
| 77 |
class C(complex): |
| 78 |
def __complex__(self): |
| 79 |
return -10-10j |
| 80 |
|
| 81 |
assert module.test(complex(1 + 1j)) == (1, 1) |
| 82 |
assert complex(C(1 + 1j)) == -10-10j, C(1 + 1j) |
| 83 |
|
| 84 |
assert module.test(C(1 + 1j)) == (1, 1) |