File: test_complexobject.py

package info (click to toggle)
pypy3 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 111,848 kB
  • sloc: python: 1,291,746; ansic: 74,281; asm: 5,187; cpp: 3,017; sh: 2,533; makefile: 544; xml: 243; lisp: 45; csh: 21; awk: 4
file content (64 lines) | stat: -rw-r--r-- 2,674 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
from pypy.module.cpyext.test.test_api import BaseApiTest, raises_w
from pypy.module.cpyext.complexobject import (
    PyComplex_FromDoubles, PyComplex_RealAsDouble, PyComplex_ImagAsDouble)

class TestComplexObject(BaseApiTest):
    def test_complexobject(self, space):
        w_value = PyComplex_FromDoubles(space, 1.2, 3.4)
        assert space.unwrap(w_value) == 1.2+3.4j
        assert PyComplex_RealAsDouble(space, w_value) == 1.2
        assert PyComplex_ImagAsDouble(space, w_value) == 3.4

        assert PyComplex_RealAsDouble(space, space.wrap(42)) == 42
        assert PyComplex_RealAsDouble(space, space.wrap(1.5)) == 1.5
        assert PyComplex_ImagAsDouble(space, space.wrap(1.5)) == 0.0

        # cpython accepts anything for PyComplex_ImagAsDouble
        assert PyComplex_ImagAsDouble(space, space.w_None) == 0.0
        with raises_w(space, TypeError):
            PyComplex_RealAsDouble(space, space.w_None)

class AppTestCComplex(AppTestCpythonExtensionBase):
    def test_AsCComplex(self):
        module = self.import_extension('foo', [
            ("as_tuple", "METH_O",
             """
                 Py_complex c = PyComplex_AsCComplex(args);
                 if (PyErr_Occurred()) return NULL;
                 return Py_BuildValue("dd", c.real, c.imag);
             """)])
        assert module.as_tuple(12-34j) == (12, -34)
        assert module.as_tuple(-3.14) == (-3.14, 0.0)
        raises(TypeError, module.as_tuple, "12")

    def test_FromCComplex(self):
        module = self.import_extension('foo', [
            ("test", "METH_NOARGS",
             """
                 Py_complex c = {1.2, 3.4};
                 return PyComplex_FromCComplex(c);
             """)])
        assert module.test() == 1.2 + 3.4j

    def test_PyComplex_to_WComplex(self):
        module = self.import_extension('foo', [
            ("test", "METH_NOARGS",
             """
                 Py_complex c = {1.2, 3.4};
                 PyObject *obj = PyObject_Malloc(sizeof(PyComplexObject));
                 obj = PyObject_Init(obj, &PyComplex_Type);
                 assert(obj != NULL);
                 ((PyComplexObject *)obj)->cval = c;
                 return obj;
             """)])
        assert module.test() == 1.2 + 3.4j

    def test_WComplex_to_PyComplex(self):
        module = self.import_extension('foo', [
            ("test", "METH_O",
             """
                 Py_complex c = ((PyComplexObject *)args)->cval;
                 return Py_BuildValue("dd", c.real, c.imag);
             """)])
        assert module.test(1.2 + 3.4j) == (1.2, 3.4)