TestInline.py 1.1 KB
Newer Older
Robert Bradshaw's avatar
Robert Bradshaw committed
1
from Cython.Shadow import inline
2
from Cython.Build.Inline import safe_type
Robert Bradshaw's avatar
Robert Bradshaw committed
3 4
from Cython.TestUtils import CythonTest

5 6 7 8 9 10 11 12 13 14
try:
    import numpy
    has_numpy = True
except:
    has_numpy = False

test_kwds = dict(force=True, quiet=True)

global_value = 100

15
class TestInline(CythonTest):
Robert Bradshaw's avatar
Robert Bradshaw committed
16

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
    def test_simple(self):
        self.assertEquals(inline("return 1+2", **test_kwds), 3)

    def test_types(self):
        self.assertEquals(inline("""
            cimport cython
            return cython.typeof(a), cython.typeof(b)
        """, a=1.0, b=[], **test_kwds), ('double', 'list object'))

    def test_locals(self):
        a = 1
        b = 2
        self.assertEquals(inline("return a+b", **test_kwds), 3)

    def test_globals(self):
        self.assertEquals(inline("return global_value + 1", **test_kwds), global_value + 1)

    if has_numpy:
35

36 37 38 39 40 41
        def test_numpy(self):
            import numpy
            a = numpy.ndarray((10, 20))
            a[0,0] = 10
            self.assertEquals(safe_type(a), 'numpy.ndarray[numpy.float64_t, ndim=2]')
            self.assertEquals(inline("return a[0,0]", a=a, **test_kwds), 10.0)