/usr/lib/python2.7/dist-packages/PySPH-1.0a4.dev0-py2.7-linux-x86_64.egg/pysph/base/tests/test_cython_generator.py is in python-pysph 0~20160514.git91867dc-4build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | """Test code for Cython code generation.
"""
import unittest
from textwrap import dedent
from math import pi, sin
from pysph.base.config import get_config, set_config
from pysph.base.cython_generator import (CythonGenerator, CythonClassHelper,
KnownType, all_numeric)
def declare(*args):
pass
class BasicEq:
def __init__(self, hidden=None, rho=0.0, c=0.0):
self.rho = rho
self.c = c
self._hidden = ['a', 'b']
class EqWithMethod(BasicEq):
def func(self, d_idx=0, d_x=[0.0, 0.0]):
tmp = abs(self.rho*self.c)*sin(pi*self.c)
d_x[d_idx] = d_x[d_idx]*tmp
class EqWithReturn(BasicEq):
def func(self, d_idx=0, d_x=[0.0, 0.0]):
return d_x[d_idx]
class EqWithKnownTypes:
def some_func(self, d_idx, d_p, WIJ, DWIJ, user, d_user, s_user):
d_p[d_idx] = WIJ*DWIJ[0]
class EqWithMatrix:
def func(self, d_idx, d_x=[0.0, 0.0]):
mat = declare('matrix((2,2))')
mat[0][0] = d_x[d_idx]
vec = declare('matrix((3,))')
vec[0] = d_x[d_idx]
class EqWithDeclare:
def func(self, d_idx, d_x=[0.0, 0.0]):
val = declare('float')
val = d_x[d_idx]
index = declare('unsigned int')
index = d_idx
def func_with_return(d_idx, d_x, x=0.0):
x += 1
return d_x[d_idx] + x
def simple_func(d_idx, d_x, x=0.0):
d_x[d_idx] += x
class TestBase(unittest.TestCase):
def assert_code_equal(self, result, expect):
expect = expect.strip()
result = result.strip()
msg = 'EXPECTED:\n%s\nGOT:\n%s'%(expect, result)
self.assertEqual(expect, result, msg)
class TestMiscUtils(TestBase):
def test_all_numeric(self):
x = [1, 2, 3.0]
self.assertTrue(all_numeric(x))
try:
x = [0.0, 1, long(3)]
except NameError:
x = [0.0, 1, 3]
self.assertTrue(all_numeric(x))
x = [0.0, 1.0, '']
self.assertFalse(all_numeric(x))
def test_detect_type(self):
cases = [(('d_something', None), 'double*'),
(('s_something', None), 'double*'),
(('d_idx', 0), 'long'),
(('x', 1), 'long'),
(('s', 'asdas'), 'str'),
(('junk', 1.0), 'double'),
(('y', [0.0, 1]), 'double*'),
(('y', [0, 1, 0]), 'double*'),
(('y', None), 'object'),
]
cg = CythonGenerator()
for args, expect in cases:
msg = 'detect_type(*%r) != %r'%(args, expect)
self.assertEqual(cg.detect_type(*args), expect, msg)
def test_cython_class_helper(self):
code = ('def f(self, x):',
' x += 1\n return x+1')
c = CythonClassHelper(name='A', public_vars={'x': 'double'},
methods=[code])
expect = dedent("""
cdef class A:
cdef public double x
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
def f(self, x):
x += 1
return x+1
""")
self.assert_code_equal(c.generate().strip(), expect.strip())
class TestCythonCodeGenerator(TestBase):
def setUp(self):
get_config().use_openmp = False
def tearDown(self):
set_config(None)
def test_simple_constructor(self):
cg = CythonGenerator()
cg.parse(BasicEq())
expect = dedent("""
cdef class BasicEq:
cdef public list _hidden
cdef public double c
cdef public double rho
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
""")
self.assert_code_equal(cg.get_code().strip(), expect.strip())
def test_simple_method(self):
cg = CythonGenerator()
cg.parse(EqWithMethod())
expect = dedent("""
cdef class EqWithMethod:
cdef public list _hidden
cdef public double c
cdef public double rho
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
cdef inline void func(self, long d_idx, double* d_x):
cdef double tmp
tmp = abs(self.rho*self.c)*sin(pi*self.c)
d_x[d_idx] = d_x[d_idx]*tmp
""")
self.assert_code_equal(cg.get_code().strip(), expect.strip())
def test_honors_use_openmp_setting(self):
# When
get_config().use_openmp = True
# Then
cg = CythonGenerator()
cg.parse(EqWithMethod())
expect = dedent("""
cdef class EqWithMethod:
cdef public list _hidden
cdef public double c
cdef public double rho
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
cdef inline void func(self, long d_idx, double* d_x) nogil:
cdef double tmp
tmp = abs(self.rho*self.c)*sin(pi*self.c)
d_x[d_idx] = d_x[d_idx]*tmp
""")
self.assert_code_equal(cg.get_code().strip(), expect.strip())
def test_python_methods(self):
cg = CythonGenerator(python_methods=True)
cg.parse(EqWithMethod())
expect = dedent("""
cdef class EqWithMethod:
cdef public list _hidden
cdef public double c
cdef public double rho
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
cdef inline void func(self, long d_idx, double* d_x):
cdef double tmp
tmp = abs(self.rho*self.c)*sin(pi*self.c)
d_x[d_idx] = d_x[d_idx]*tmp
cpdef py_func(self, long d_idx, double[:] d_x):
self.func(d_idx, &d_x[0])
""")
self.assert_code_equal(cg.get_code().strip(), expect.strip())
cg.parse(EqWithReturn())
expect = dedent("""
cdef class EqWithReturn:
cdef public list _hidden
cdef public double c
cdef public double rho
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
cdef inline double func(self, long d_idx, double* d_x):
return d_x[d_idx]
cpdef double py_func(self, long d_idx, double[:] d_x):
return self.func(d_idx, &d_x[0])
""")
self.assert_code_equal(cg.get_code().strip(), expect.strip())
cg.parse(func_with_return)
expect = dedent("""
cdef inline double func_with_return(long d_idx, double* d_x, double x):
x += 1
return d_x[d_idx] + x
cpdef double py_func_with_return(long d_idx, double[:] d_x, double x):
return func_with_return(d_idx, &d_x[0], x)
""")
self.assert_code_equal(cg.get_code().strip(), expect.strip())
def test_method_with_return(self):
cg = CythonGenerator()
cg.parse(EqWithReturn())
expect = dedent("""
cdef class EqWithReturn:
cdef public list _hidden
cdef public double c
cdef public double rho
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
cdef inline double func(self, long d_idx, double* d_x):
return d_x[d_idx]
""")
self.assert_code_equal(cg.get_code().strip(), expect.strip())
def test_method_with_matrix(self):
cg = CythonGenerator()
cg.parse(EqWithMatrix())
expect = dedent("""
cdef class EqWithMatrix:
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
cdef inline void func(self, long d_idx, double* d_x):
cdef double mat[2][2]
mat[0][0] = d_x[d_idx]
cdef double vec[3]
vec[0] = d_x[d_idx]
""")
self.assert_code_equal(cg.get_code().strip(), expect.strip())
def test_method_with_declare(self):
cg = CythonGenerator()
cg.parse(EqWithDeclare())
expect = dedent("""
cdef class EqWithDeclare:
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
cdef inline void func(self, long d_idx, double* d_x):
cdef float val
val = d_x[d_idx]
cdef unsigned int index
index = d_idx
""")
self.assert_code_equal(cg.get_code().strip(), expect.strip())
def test_method_with_known_types(self):
cg = CythonGenerator(
known_types={'WIJ':0.0, 'DWIJ':[0.0, 0.0, 0.0],
'user': KnownType('ndarray'),
'd_user': KnownType('long*'),
's_user': KnownType('int*'),
}
)
cg.parse(EqWithKnownTypes())
expect = dedent("""
cdef class EqWithKnownTypes:
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
cdef inline void some_func(self, long d_idx, double* d_p, double WIJ, double* DWIJ, ndarray user, long* d_user, int* s_user):
d_p[d_idx] = WIJ*DWIJ[0]
""")
self.assert_code_equal(cg.get_code().strip(), expect.strip())
def test_wrap_function(self):
cg = CythonGenerator()
cg.parse(func_with_return)
expect = dedent("""
cdef inline double func_with_return(long d_idx, double* d_x, double x):
x += 1
return d_x[d_idx] + x
""")
self.assert_code_equal(cg.get_code().strip(), expect.strip())
cg.parse(simple_func)
expect = dedent("""
cdef inline void simple_func(long d_idx, double* d_x, double x):
d_x[d_idx] += x
""")
self.assert_code_equal(cg.get_code().strip(), expect.strip())
if __name__ == '__main__':
unittest.main()
|