Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / cython / src / Cython / Build / Tests / TestInline.py
1 import os, tempfile
2 from Cython.Shadow import inline
3 from Cython.Build.Inline import safe_type
4 from Cython.TestUtils import CythonTest
5
6 try:
7     import numpy
8     has_numpy = True
9 except:
10     has_numpy = False
11
12 test_kwds = dict(force=True, quiet=True)
13
14 global_value = 100
15
16 class TestInline(CythonTest):
17     def setUp(self):
18         CythonTest.setUp(self)
19         self.test_kwds = dict(test_kwds)
20         if os.path.isdir('BUILD'):
21             lib_dir = os.path.join('BUILD','inline')
22         else:
23             lib_dir = tempfile.mkdtemp(prefix='cython_inline_')
24         self.test_kwds['lib_dir'] = lib_dir
25
26     def test_simple(self):
27         self.assertEquals(inline("return 1+2", **self.test_kwds), 3)
28
29     def test_types(self):
30         self.assertEquals(inline("""
31             cimport cython
32             return cython.typeof(a), cython.typeof(b)
33         """, a=1.0, b=[], **self.test_kwds), ('double', 'list object'))
34
35     def test_locals(self):
36         a = 1
37         b = 2
38         self.assertEquals(inline("return a+b", **self.test_kwds), 3)
39
40     def test_globals(self):
41         self.assertEquals(inline("return global_value + 1", **self.test_kwds), global_value + 1)
42
43     def test_pure(self):    
44         import cython as cy
45         b = inline("""
46         b = cy.declare(float, a)
47         c = cy.declare(cy.pointer(cy.float), &b)
48         return b
49         """, a=3)
50         self.assertEquals(type(b), float)
51
52     if has_numpy:
53
54         def test_numpy(self):
55             import numpy
56             a = numpy.ndarray((10, 20))
57             a[0,0] = 10
58             self.assertEquals(safe_type(a), 'numpy.ndarray[numpy.float64_t, ndim=2]')
59             self.assertEquals(inline("return a[0,0]", a=a, **self.test_kwds), 10.0)