| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
from __future__ import print_function |
| 7 |
from __future__ import unicode_literals |
| 8 |
from __future__ import division |
| 9 |
from __future__ import absolute_import |
| 10 |
|
| 11 |
import pytest |
| 12 |
import CifFile |
| 13 |
from CifFile import StarFile,StarList |
| 14 |
import numpy |
| 15 |
from CifFile.drel import drel_lex,drel_ast_yacc,py_from_ast,drel_runtime |
| 16 |
from copy import copy |
| 17 |
|
| 18 |
class TestdRELRuntime(): |
| 19 |
|
| 20 |
def testListAppend(self): |
| 21 |
a = [[1,2],[3,4]] |
| 22 |
b = drel_runtime.aug_append(a,1) |
| 23 |
c = drel_runtime.aug_append(a,[3]) |
| 24 |
d = drel_runtime.aug_append(a,[[4,5,6]]) |
| 25 |
assert b == [[1,2],[3,4],1] |
| 26 |
assert c == [[1,2],[3,4],[3]] |
| 27 |
assert d == [[1,2],[3,4],[[4,5,6]]] |
| 28 |
|
| 29 |
def testListAdd(self): |
| 30 |
a = [[1,2],[3,4]] |
| 31 |
aa = 5 |
| 32 |
b = drel_runtime.aug_add(a,1) |
| 33 |
c = drel_runtime.aug_add(a,[[1,2],[7,6]]) |
| 34 |
d = drel_runtime.aug_add(5,2) |
| 35 |
assert (c == numpy.array([[2,4],[10,10]])).all() |
| 36 |
assert (b == numpy.array([[2,3],[4,5]])).all() |
| 37 |
assert d == 7 |
| 38 |
|
| 39 |
def testListUnappend(self): |
| 40 |
a = [[1,2],[3,4]] |
| 41 |
c = drel_runtime.aug_remove(a,[1,2]) |
| 42 |
assert c == [[3,4]] |
| 43 |
|
| 44 |
def testListSubtract(self): |
| 45 |
a = [[1,2],[3,4]] |
| 46 |
aa = 5 |
| 47 |
b = drel_runtime.aug_sub(a,1) |
| 48 |
c = drel_runtime.aug_sub(a,[[1,2],[7,6]]) |
| 49 |
d = drel_runtime.aug_sub(5,2) |
| 50 |
assert (c == numpy.array([[0,0],[-4,-2]])).all() |
| 51 |
assert (b == numpy.array([[0,1],[2,3]])).all() |
| 52 |
assert d == 3 |
| 53 |
|
| 54 |
def testDotProduct(self): |
| 55 |
"""Test that multiplication works correctly""" |
| 56 |
a = numpy.array([1,2,3]) |
| 57 |
b = numpy.array([4,5,6]) |
| 58 |
d = drel_runtime.drel_dot(a,b) |
| 59 |
assert d == 32 |
| 60 |
|
| 61 |
def testMatrixMultiply(self): |
| 62 |
"""Test that matrix * matrix works""" |
| 63 |
a = numpy.matrix([[1,0,0],[0,1,0],[0,0,1]]) |
| 64 |
b = numpy.matrix([[3,4,5],[6,7,8],[9,10,11]]) |
| 65 |
c = drel_runtime.drel_dot(a,b) |
| 66 |
assert (c == numpy.matrix([[3,4,5],[6,7,8],[9,10,11]])).any() |
| 67 |
|
| 68 |
def testMatVecMultiply(self): |
| 69 |
"""Test that matrix * vec works""" |
| 70 |
a = numpy.array([0,1,0]) |
| 71 |
b = numpy.matrix([[3,4,5],[6,7,8],[9,10,11]]) |
| 72 |
c = drel_runtime.drel_dot(a,b) |
| 73 |
d = drel_runtime.drel_dot(b,a) |
| 74 |
assert (d == numpy.matrix([4,7,10])).any() |
| 75 |
assert (c == numpy.matrix([6,7,8])).any() |
| 76 |
|
| 77 |
def testScalarVecMult(self): |
| 78 |
"""Test that multiplying by a scalar works""" |
| 79 |
a = [1,2,3] |
| 80 |
b = 4 |
| 81 |
c = drel_runtime.drel_dot(b,a) |
| 82 |
d = drel_runtime.drel_dot(a,b) |
| 83 |
assert (c == numpy.matrix([4,8,12])).any() |
| 84 |
assert (d == numpy.matrix([4,8,12])).any() |
| 85 |
|
| 86 |
def testArrayAppend(self): |
| 87 |
a = numpy.array([0,1,0]) |
| 88 |
b = numpy.array([1,0,0]) |
| 89 |
a = drel_runtime.aug_append(a,b) |
| 90 |
assert (a == numpy.array([[0,1,0],[1,0,0]])).any() |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
@pytest.fixture |
| 95 |
def setup(): |
| 96 |
|
| 97 |
lexer = drel_lex.lexer |
| 98 |
parser = drel_ast_yacc.parser |
| 99 |
dic = CifFile.CifDic("tests/drel/dic_for_tests.dic",grammar="STAR2") |
| 100 |
return lexer, parser, dic |
| 101 |
|
| 102 |
class TestSingleSimpleStatement(): |
| 103 |
|
| 104 |
def create_test(self,setup, instring,right_value,debug=False,array=False): |
| 105 |
"""Given a string, create and call a function then check result""" |
| 106 |
lexer, parser, dic = setup |
| 107 |
if instring[-1]!="\n": |
| 108 |
instring += '\n' |
| 109 |
res = parser.parse(instring,debug=debug,lexer=lexer) |
| 110 |
if debug: print("%s\n -> \n%r \n" % (instring, res)) |
| 111 |
realfunc = py_from_ast.make_python_function(res,"myfunc",'_a.b',have_sn=False, |
| 112 |
cif_dic=dic) |
| 113 |
if debug: print("-> %s" % realfunc) |
| 114 |
exec(realfunc,globals()) |
| 115 |
answer = myfunc(self) |
| 116 |
if debug: print(" -> {!r}".format(answer)) |
| 117 |
if not array: |
| 118 |
assert answer == right_value |
| 119 |
else: |
| 120 |
try: |
| 121 |
assert (answer == right_value).all() |
| 122 |
except: |
| 123 |
assert answer == right_value |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
def testrealnum(self,setup): |
| 130 |
"""test parsing of real numbers""" |
| 131 |
self.create_test(setup, '_a.b=5.45',5.45) |
| 132 |
self.create_test(setup, '_a.b=.45e-24',.45e-24) |
| 133 |
|
| 134 |
def testinteger(self, setup): |
| 135 |
"""test parsing an integer""" |
| 136 |
resm = [0,0,0,0] |
| 137 |
checkm = [1230,77,5,473] |
| 138 |
self.create_test(setup,'_a.b = 1230',1230) |
| 139 |
self.create_test(setup,'_a.b = 0x4D',77) |
| 140 |
self.create_test(setup,'_a.b = 0B0101',5) |
| 141 |
self.create_test(setup,'_a.b = 0o731',473) |
| 142 |
|
| 143 |
def testcomplex(self, setup): |
| 144 |
"""test parsing a complex number""" |
| 145 |
self.create_test(setup,'_a.b = 13.45j',13.45j) |
| 146 |
|
| 147 |
def testList(self, setup): |
| 148 |
"""test parsing a list over two lines""" |
| 149 |
self.create_test(setup,'_a.b = [1,2,\n 3,4,\n 5,6]',StarList([1,2,3,4,5,6])) |
| 150 |
|
| 151 |
def testparenth(self, setup): |
| 152 |
"""test parsing a parenthesis over two lines""" |
| 153 |
self.create_test(setup,'_a.b = (1,2,\n3,4)',(1,2,3,4)) |
| 154 |
|
| 155 |
def testequal(self, setup): |
| 156 |
"""test splitting equation over two lines""" |
| 157 |
self.create_test(setup, '_a.b = \n2', 2) |
| 158 |
|
| 159 |
def testshortstring(self, setup): |
| 160 |
"""test parsing a one-line string""" |
| 161 |
jk = "_a.b = \"my pink pony's mane\"" |
| 162 |
jl = "_a.b = 'my pink pony\"s mane'" |
| 163 |
self.create_test(setup,jk,jk[8:-1]) |
| 164 |
self.create_test(setup,jl,jl[8:-1]) |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
@pytest.mark.xfail |
| 170 |
def testlongstring(self, setup): |
| 171 |
"""test parsing multi-line strings""" |
| 172 |
jk = '''_a.b = """ a long string la la la '"' |
| 173 |
some more |
| 174 |
end""" ''' |
| 175 |
jl = """_a.b = ''' a long string la la la '"' |
| 176 |
some more |
| 177 |
end''' """ |
| 178 |
self.create_test(setup,jk,jk[7:-3]) |
| 179 |
self.create_test(setup,jl,jl[7:-3]) |
| 180 |
|
| 181 |
def testmathexpr(self, setup): |
| 182 |
"""test simple maths expressions """ |
| 183 |
testexpr = (("_a.b = 5.45 + 23.6e05",5.45+23.6e05), |
| 184 |
("_a.b = 11 - 45",11-45), |
| 185 |
("_a.b = 45.6 / 22.2",45.6/22.2)) |
| 186 |
for test,check in testexpr: |
| 187 |
self.create_test(setup,test,check) |
| 188 |
|
| 189 |
def testexprlist(self, setup): |
| 190 |
"""test comma-separated expressions""" |
| 191 |
test = "_a.b = 5,6,7+8.5e2" |
| 192 |
self.create_test(setup,test,(5,6,7+8.5e2)) |
| 193 |
|
| 194 |
def testparen(self, setup): |
| 195 |
"""test parentheses""" |
| 196 |
test = "_a.b = ('once', 'upon', 6,7j +.5e2)" |
| 197 |
self.create_test(setup,test,('once' , 'upon' , 6 , 7j + .5e2 )) |
| 198 |
|
| 199 |
def testlists(self, setup): |
| 200 |
"""test list parsing""" |
| 201 |
test = "_a.b = ['once', 'upon', 6,7j +.5e2]" |
| 202 |
self.create_test(setup,test,StarList(['once' , 'upon' , 6 , 7j + .5e2 ])) |
| 203 |
|
| 204 |
def test_multistatements(self, setup): |
| 205 |
"""test multiple statements""" |
| 206 |
test1 = "_a.b = 1.2\nb = 'abc'\nqrs = 4.4\n" |
| 207 |
test2 = '\n\nq = _c.d\nnumeric = "01234"\n_a.b=11.2' |
| 208 |
self.create_test(setup,test1,1.2) |
| 209 |
|
| 210 |
|
| 211 |
def test_semicolon_sep(self, setup): |
| 212 |
"""test multiple statements between semicolons""" |
| 213 |
test = "_a.b = 1.2;b = 'abc';qrs = 4.4" |
| 214 |
self.create_test(setup,test,1.2) |
| 215 |
|
| 216 |
def test_slicing(self, setup): |
| 217 |
"""Test that our slicing is parsed correctly""" |
| 218 |
test = "b = array([[1,2],[3,4],[5,6]]);_a.b=b[0,1]" |
| 219 |
self.create_test(setup,test,2) |
| 220 |
|
| 221 |
def test_slice_2(self, setup): |
| 222 |
"""Test that first/last slicing works""" |
| 223 |
test = "b = 'abcdef';_a.b=b[1:3]" |
| 224 |
self.create_test(setup,test,'bc') |
| 225 |
|
| 226 |
def test_slice_3(self, setup): |
| 227 |
test = """ |
| 228 |
c = [[0.1,1,2,3],[0.2,4,5,6],[0.3,7,8,9]] |
| 229 |
_a.b = c[:, 0] |
| 230 |
""" |
| 231 |
self.create_test(setup, test, [0.1, 0.2, 0.3]) |
| 232 |
|
| 233 |
def test_slice_4(self, setup): |
| 234 |
test = """ |
| 235 |
c = [[0.1,1,2,3],[0.2,4,5,6],[0.3,7,8,9]] |
| 236 |
_a.b = c[0:2, 0] |
| 237 |
""" |
| 238 |
self.create_test(setup, test, [0.1, 0.2]) |
| 239 |
|
| 240 |
def test_slice_5(self, setup): |
| 241 |
test = """ |
| 242 |
c = [[0.1,1,2,3],[0.2,4,5,6],[0.3,7,8,9]] |
| 243 |
_a.b = c[:2, 0] |
| 244 |
""" |
| 245 |
self.create_test(setup, test, [0.1, 0.2]) |
| 246 |
|
| 247 |
def test_paren_balance(self, setup): |
| 248 |
"""Test that multi-line parentheses work """ |
| 249 |
test = """b = ( |
| 250 |
(1,2,( |
| 251 |
3,4 |
| 252 |
) |
| 253 |
,5),6 |
| 254 |
,7)\n _a.b=b[0][2][0]""" |
| 255 |
self.create_test(setup,test,3) |
| 256 |
|
| 257 |
def test_list_constructor(self, setup): |
| 258 |
"""Test that the list constructor works""" |
| 259 |
test = """_a.b = List(1,2)""" |
| 260 |
self.create_test(setup,test,[1,2]) |
| 261 |
|
| 262 |
def test_non_python_ops(self, setup): |
| 263 |
"""Test operators that have no direct Python equivalents""" |
| 264 |
test_expr = (("b = [1,2]; _a.b = [3,4]; _a.b++=b",StarList([3,4,[1,2]])), |
| 265 |
("b = [1,2]; _a.b = [3,4]; _a.b+=b",[4,6]), |
| 266 |
("b = 3; _a.b = [3,4]; _a.b-=b",[0,1]), |
| 267 |
("b = [1,2]; _a.b = [[1,2],[3,4]]; _a.b--=b",[[3,4]])) |
| 268 |
for one_expr in test_expr: |
| 269 |
self.create_test(setup,one_expr[0],one_expr[1],debug=True,array=True) |
| 270 |
|
| 271 |
def test_tables(self, setup): |
| 272 |
"""Test that tables are parsed correctly""" |
| 273 |
teststrg = """ |
| 274 |
c = Table() |
| 275 |
c['bx'] = 25 |
| 276 |
_a.b = c |
| 277 |
""" |
| 278 |
lexer, parser, dic = setup |
| 279 |
print("Table test:") |
| 280 |
res = parser.parse(teststrg+"\n",lexer=lexer) |
| 281 |
realfunc = py_from_ast.make_python_function(res,"myfunc","_a.b",have_sn=False, |
| 282 |
cif_dic=dic) |
| 283 |
print(realfunc) |
| 284 |
exec(realfunc,globals()) |
| 285 |
b = myfunc(self) |
| 286 |
assert b['bx']==25 |
| 287 |
|
| 288 |
def test_Tables_2(self, setup): |
| 289 |
"""Test that brace-delimited tables are parsed correctly""" |
| 290 |
teststrg = """ |
| 291 |
c = {'hello':1,'goodbye':2} |
| 292 |
_a.b = c['hello'] |
| 293 |
""" |
| 294 |
print("Table test:") |
| 295 |
lexer, parser, dic = setup |
| 296 |
res = parser.parse(teststrg+"\n",lexer=lexer) |
| 297 |
realfunc = py_from_ast.make_python_function(res,"myfunc","_a.b",have_sn=False, |
| 298 |
cif_dic=dic) |
| 299 |
print(realfunc) |
| 300 |
exec(realfunc,globals()) |
| 301 |
b = myfunc(self) |
| 302 |
assert b==1 |
| 303 |
|
| 304 |
def test_subscription(self, setup): |
| 305 |
"""Test proper list of dependencies is returned""" |
| 306 |
teststrg = """ |
| 307 |
m = [15,25,35] |
| 308 |
_a.b = m [1] |
| 309 |
""" |
| 310 |
self.create_test(setup,teststrg,25) |
| 311 |
|
| 312 |
def test_list_indices(self, setup): |
| 313 |
"""Test that multi-dimensional indices are accessed correctly""" |
| 314 |
teststrg = """ |
| 315 |
m = [[1,2,3],[4,5,6],[7,8,9]] |
| 316 |
_a.b = m[1,2] |
| 317 |
""" |
| 318 |
self.create_test(setup,teststrg,6,debug=True) |
| 319 |
|
| 320 |
def test_matrix_indices(self, setup): |
| 321 |
"""Test that multi-dimensional indices work for matrices too""" |
| 322 |
teststrg = """ |
| 323 |
m = matrix([[1,2,3],[4,5,6],[7,8,9]]) |
| 324 |
_a.b = m[1,2] |
| 325 |
""" |
| 326 |
self.create_test(setup,teststrg,6,debug=True) |
| 327 |
|
| 328 |
@pytest.fixture |
| 329 |
def setupcs(): |
| 330 |
|
| 331 |
lexer = drel_lex.lexer |
| 332 |
lexer.lineno = 0 |
| 333 |
parser = drel_ast_yacc.parser |
| 334 |
dic = CifFile.CifDic("tests/drel/dic_for_tests.dic",grammar="STAR2") |
| 335 |
return lexer, parser, dic |
| 336 |
|
| 337 |
class TestSimpleCompoundStatement(): |
| 338 |
|
| 339 |
def create_test(self, setup, instring,right_value,varname="_a.b",debug=True): |
| 340 |
"""Given a string, create and call a function then check result""" |
| 341 |
|
| 342 |
lexer, parser, dic = setup |
| 343 |
if instring[-1]!="\n": |
| 344 |
instring += "\n" |
| 345 |
res = parser.parse(instring,debug=debug,lexer=lexer) |
| 346 |
if debug: print("%s\n -> \n%r \n" % (instring, res)) |
| 347 |
realfunc = py_from_ast.make_python_function(res,"myfunc",varname,have_sn=False, |
| 348 |
cif_dic=dic) |
| 349 |
if debug: print("-> %s" % realfunc) |
| 350 |
exec(realfunc,globals()) |
| 351 |
assert myfunc(self) == right_value |
| 352 |
|
| 353 |
def test_multi_assign(self, setupcs): |
| 354 |
""" Test that multiple assignments are parsed """ |
| 355 |
teststrg = """ |
| 356 |
f = _a.b |
| 357 |
p = len(f) |
| 358 |
q = 0 |
| 359 |
_a.b = 0 |
| 360 |
""" |
| 361 |
lexer, parser, dic = setupcs |
| 362 |
res = parser.parse(teststrg+"\n",lexer=lexer) |
| 363 |
realfunc = py_from_ast.make_python_function(res,"myfunc",'_a.b',cif_dic=dic) |
| 364 |
print("-> " + realfunc) |
| 365 |
|
| 366 |
def test_do_stmt(self, setupcs): |
| 367 |
"""Test how a do statement comes out""" |
| 368 |
teststrg = """ |
| 369 |
_a.b = 0 |
| 370 |
dummy = 1 |
| 371 |
do jkl = 0,20,2 { |
| 372 |
if (dummy == 1) print('dummy is 1') |
| 373 |
_a.b = _a.b + jkl |
| 374 |
} |
| 375 |
do emm = 1,5 |
| 376 |
{ |
| 377 |
_a.b = _a.b + emm |
| 378 |
} |
| 379 |
""" |
| 380 |
self.create_test(setupcs,teststrg,125) |
| 381 |
|
| 382 |
def test_do_stmt_2(self, setupcs): |
| 383 |
"""Test how another do statement comes out with long suite""" |
| 384 |
teststrg = """ |
| 385 |
_a.b = 0 |
| 386 |
geom_hbond = [(1,2),(2,3),(3,4)] |
| 387 |
do i= 0,1 |
| 388 |
{ |
| 389 |
l,s = geom_hbond [i] |
| 390 |
a = 'hello' |
| 391 |
c = int(4.5) |
| 392 |
bb = [1,c,a] |
| 393 |
_a.b += s |
| 394 |
} |
| 395 |
""" |
| 396 |
self.create_test(setupcs,teststrg,5) |
| 397 |
|
| 398 |
def test_if_stmt(self, setupcs): |
| 399 |
"""test parsing of if statement""" |
| 400 |
teststrg = """ |
| 401 |
dmin = 5.0 |
| 402 |
d1 = 4.0 |
| 403 |
rad1 = 2.2 |
| 404 |
radius_bond = 2.0 |
| 405 |
If (d1<dmin or d1>(rad1+radius_bond)) _a.b = 5 |
| 406 |
""" |
| 407 |
self.create_test(setupcs,teststrg,5) |
| 408 |
|
| 409 |
def test_double_if_stmt(self, setupcs): |
| 410 |
"""test parsing of if statement""" |
| 411 |
teststrg = """ |
| 412 |
dmin = 5.0 |
| 413 |
d1 = 4.0 |
| 414 |
rad1 = 2.2 |
| 415 |
radius_bond = 2.0 |
| 416 |
If (d1<dmin or d1>(rad1+radius_bond)) _a.b = 5 |
| 417 |
|
| 418 |
if (d1>dmin or d1<(rad1+radius_bond)) _a.b = 11 |
| 419 |
if (5 > 6 and 6 < 4) _a.b = -2 |
| 420 |
""" |
| 421 |
self.create_test(setupcs,teststrg,11) |
| 422 |
|
| 423 |
def test_if_else(self, setupcs): |
| 424 |
"""Test that else is properly handled""" |
| 425 |
teststrg = """drp = 'electron' |
| 426 |
If (drp == "neutron") _a.b = "femtometres" |
| 427 |
Else If (drp == "electron") _a.b = "volts" |
| 428 |
Else _a.b = "electrons" """ |
| 429 |
self.create_test(setupcs,teststrg,'volts') |
| 430 |
|
| 431 |
def test_for_statement(self, setupcs): |
| 432 |
"""Test for statement with list""" |
| 433 |
teststrg = """ |
| 434 |
_a.b = 0 |
| 435 |
for [c,d] in [[1,2],[3,4],[5,6]] { |
| 436 |
_a.b += c + 2*d |
| 437 |
}""" |
| 438 |
self.create_test(setupcs,teststrg,33) |
| 439 |
|
| 440 |
def test_single_line_suite(self, setupcs): |
| 441 |
"""Test singe-line suite""" |
| 442 |
teststrg = """ |
| 443 |
_a.b = 0 |
| 444 |
for [c,d] in [[1,2],[3,4],[5,6]] { _a.b += c + 2*d }""" |
| 445 |
self.create_test(setupcs, teststrg, 33) |
| 446 |
|
| 447 |
def test_funcdef(self, setupcs): |
| 448 |
"""Test function conversion""" |
| 449 |
teststrg = """ |
| 450 |
function Closest( v :[Array, Real], # coord vector to be cell translated |
| 451 |
w :[Array, Real]) { # target vector |
| 452 |
|
| 453 |
d = v - w |
| 454 |
t = Int( Mod( 99.5 + d, 1.0 ) - d ) |
| 455 |
q = 1 + 1 |
| 456 |
Closest = [ v+t, t ] |
| 457 |
} """ |
| 458 |
lexer, parser, dic = setupcs |
| 459 |
res = parser.parse(teststrg+"\n",lexer=lexer) |
| 460 |
realfunc = py_from_ast.make_python_function(res,"myfunc",None, func_def = True) |
| 461 |
|
| 462 |
exec(realfunc,globals()) |
| 463 |
retval = Closest(0.2,0.8,None) |
| 464 |
print('Closest 0.2,0.8 returns {!r},{!r}'.format(retval[0], retval[1])) |
| 465 |
assert retval == StarList([1.2,1]) |
| 466 |
|
| 467 |
@pytest.fixture(scope="module") |
| 468 |
def testdic(): |
| 469 |
testdic = CifFile.CifDic("tests/dictionaries/cif_core.dic",grammar="2.0",do_imports='Contents') |
| 470 |
|
| 471 |
testdic.initialise_drel() |
| 472 |
return testdic |
| 473 |
|
| 474 |
class TestMoreComplex(): |
| 475 |
|
| 476 |
def test_nested_stmt(self, setupcs): |
| 477 |
"""Test how a nested do statement executes""" |
| 478 |
teststrg = """ |
| 479 |
total = 0 |
| 480 |
_a.b = 0 |
| 481 |
do jkl = 0,20,2 { total = total + jkl |
| 482 |
do emm = 1,5 { _a.b = _a.b + 1 |
| 483 |
} |
| 484 |
} |
| 485 |
end_of_loop = -25.6 |
| 486 |
""" |
| 487 |
lexer, parser, dic = setupcs |
| 488 |
res = parser.parse(teststrg + "\n",lexer=lexer, debug = True) |
| 489 |
realfunc = py_from_ast.make_python_function(res,"myfunc","_a.b",have_sn=False, |
| 490 |
cif_dic = dic) |
| 491 |
exec(realfunc,globals()) |
| 492 |
othertotal = myfunc(self) |
| 493 |
assert othertotal==55 |
| 494 |
|
| 495 |
def test_complex_if(self, setupcs): |
| 496 |
"""Test if with single-statement suite""" |
| 497 |
teststrg = """ |
| 498 |
setting = 'triclinic' |
| 499 |
a = 20.0 |
| 500 |
b = 20.0 |
| 501 |
c = 20.0 |
| 502 |
d = 0.01 |
| 503 |
alp = 90.0 |
| 504 |
bet = 90.0 |
| 505 |
gam = 90.0 |
| 506 |
warn_len = 'Possible mismatch between cell lengths and cell setting' |
| 507 |
warn_ang = 'Possible mismatch between cell angles and cell setting' |
| 508 |
|
| 509 |
If(setting == 'triclinic') { |
| 510 |
If( Abs(a-b)<d || Abs(a-c)<d || Abs(b-c)<d ) _a.b = ('B', warn_len) |
| 511 |
If( Abs(alp-90)<d || Abs(bet-90)<d || Abs(gam-90)<d ) _a.b = ('B', warn_ang) |
| 512 |
} else _a.b = ('None',"") |
| 513 |
""" |
| 514 |
lexer, parser, dic = setupcs |
| 515 |
res = parser.parse(teststrg + "\n",lexer=lexer) |
| 516 |
realfunc = py_from_ast.make_python_function(res,"myfunc","_a.b",have_sn=False, |
| 517 |
cif_dic = dic) |
| 518 |
exec(realfunc,globals()) |
| 519 |
b = myfunc(self) |
| 520 |
print("if returns {!r}".format(b)) |
| 521 |
assert b==('B', 'Possible mismatch between cell angles and cell setting') |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
def test_fancy_assign(self, setupcs, testdic): |
| 526 |
"""Test fancy assignment""" |
| 527 |
teststrg = """ |
| 528 |
a = [2,3,4] |
| 529 |
b = 3 |
| 530 |
c= 4 |
| 531 |
do jkl = 1,5,1 { |
| 532 |
geom_angle( |
| 533 |
.distances = [b,c], |
| 534 |
.value = jkl) |
| 535 |
} |
| 536 |
""" |
| 537 |
lexer, parser, _ = setupcs |
| 538 |
res = parser.parse(teststrg + "\n", lexer=lexer) |
| 539 |
realfunc = py_from_ast.make_python_function(res,"myfunc","geom_angle",cat_meth = True,have_sn=False, |
| 540 |
cif_dic = testdic) |
| 541 |
print("Fancy assign: %s" % res[0]) |
| 542 |
exec(realfunc,globals()) |
| 543 |
b = myfunc(self) |
| 544 |
print("Geom_angle.angle = %s" % b['_geom_angle.value']) |
| 545 |
assert b['_geom_angle.value']==[1,2,3,4,5] |
| 546 |
|
| 547 |
@pytest.fixture |
| 548 |
def setupfull(testdic): |
| 549 |
|
| 550 |
lexer = drel_lex.lexer |
| 551 |
parser = drel_ast_yacc.parser |
| 552 |
parser.lineno = 0 |
| 553 |
|
| 554 |
testblock = CifFile.CifFile("tests/drel/nick1.cif",grammar="STAR2")["saly2_all_aniso"] |
| 555 |
testblock.assign_dictionary(testdic) |
| 556 |
testblock.provide_value = True |
| 557 |
|
| 558 |
namespace = testblock.keys() |
| 559 |
namespace = dict(zip(namespace,namespace)) |
| 560 |
special_ids = [namespace] |
| 561 |
return (lexer, parser, testblock, namespace, special_ids) |
| 562 |
|
| 563 |
class TestWithDict(): |
| 564 |
"""Now test flow control which requires a dictionary present""" |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
def test_loop_with_statement(self, testdic, setupfull): |
| 569 |
"""Test with statement on a looped category""" |
| 570 |
teststrg = """ |
| 571 |
with t as atom_type |
| 572 |
{ |
| 573 |
t.analytical_mass_percent = t.number_in_cell * 10 |
| 574 |
} |
| 575 |
""" |
| 576 |
lexer, parser, testblock, _, _ = setupfull |
| 577 |
loopable_cats = {'atom_type':["id",["id","number_in_cell"]]} |
| 578 |
ast = parser.parse(teststrg+"\n",lexer=lexer) |
| 579 |
realfunc = py_from_ast.make_python_function(ast,"myfunc","_atom_type.analytical_mass_percent", |
| 580 |
cif_dic=testdic,loopable=loopable_cats) |
| 581 |
print("With statement for looped category -> \n" + realfunc) |
| 582 |
exec(realfunc,globals()) |
| 583 |
|
| 584 |
atmass = myfunc(testblock) |
| 585 |
print('test value now {!r}'.format(atmass)) |
| 586 |
assert atmass == [120,280,240] |
| 587 |
|
| 588 |
def test_Lists(self, testdic, setupfull): |
| 589 |
"""Test case found in Cif dictionary """ |
| 590 |
teststrg = """# Store unique sites as a local list |
| 591 |
|
| 592 |
atomlist = List() |
| 593 |
Loop a as atom_site { |
| 594 |
axyz = a.fract_xyz |
| 595 |
cxyz = _atom_sites_Cartn_transform.matrix * axyz |
| 596 |
radb = _atom_type[a.type_symbol].radius_bond |
| 597 |
radc = _atom_type[a.type_symbol].radius_contact |
| 598 |
ls = List ( a.label, "1_555" ) |
| 599 |
atomlist ++= [ls, axyz, cxyz, radb, radc, 0] |
| 600 |
} |
| 601 |
_geom_bond.id = atomlist |
| 602 |
""" |
| 603 |
lexer, parser, testblock, _, _ = setupfull |
| 604 |
loop_cats = {"atom_site":["label",["fract_xyz","type_symbol","label"]], |
| 605 |
"atom_type":["id",["id","radius_bond","radius_contact"]]} |
| 606 |
res = parser.parse(teststrg + "\n",lexer=lexer) |
| 607 |
realfunc,dependencies = py_from_ast.make_python_function(res,"myfunc","_geom_bond.id",cat_meth=True, |
| 608 |
loopable=loop_cats,have_sn=False,depends=True,cif_dic=testdic) |
| 609 |
print('Simple function becomes:') |
| 610 |
print(realfunc) |
| 611 |
print('Depends on: {!r}'.format(dependencies)) |
| 612 |
exec(realfunc,globals()) |
| 613 |
b = myfunc(testblock) |
| 614 |
print("subscription returns {!r}".format(b)) |
| 615 |
|
| 616 |
def test_with_stmt(self, testdic, setupfull): |
| 617 |
"""Test what comes out of a simple flow statement, including |
| 618 |
multiple with statements""" |
| 619 |
teststrg = """ |
| 620 |
with e as exptl |
| 621 |
with c as cell { |
| 622 |
x = 22 |
| 623 |
j = 25 |
| 624 |
jj = e.crystals_number |
| 625 |
px = c.length_a |
| 626 |
_exptl.method = "single-crystal diffraction" |
| 627 |
}""" |
| 628 |
lexer, parser, testblock, _, _ = setupfull |
| 629 |
loopable_cats = {} |
| 630 |
res = parser.parse(teststrg+"\n",lexer=lexer) |
| 631 |
realfunc = py_from_ast.make_python_function(res,"myfunc","_exptl.method",cif_dic=testdic) |
| 632 |
print("With statement -> \n" + realfunc) |
| 633 |
exec(realfunc,globals()) |
| 634 |
|
| 635 |
testblock.assign_dictionary(testdic) |
| 636 |
newmeth = myfunc(testblock) |
| 637 |
print('exptl method now %s' % newmeth) |
| 638 |
assert newmeth == "single-crystal diffraction" |
| 639 |
|
| 640 |
def test_loop_with_stmt_2(self, testdic, setupfull): |
| 641 |
"""Test with statement on a looped category, no aliasing""" |
| 642 |
teststrg = """ |
| 643 |
_atom_type.analytical_mass_percent = _atom_type.number_in_cell * 10 |
| 644 |
""" |
| 645 |
lexer, parser, testblock, _, _ = setupfull |
| 646 |
loopable_cats = {'atom_type':["id",["id",'number_in_cell','test']]} |
| 647 |
ast = parser.parse(teststrg+"\n",lexer=lexer) |
| 648 |
realfunc = py_from_ast.make_python_function(ast,"myfunc","_atom_type.analytical_mass_percent", |
| 649 |
loopable=loopable_cats, |
| 650 |
cif_dic=testdic) |
| 651 |
print("With statement for looped category -> \n" + realfunc) |
| 652 |
exec(realfunc,globals()) |
| 653 |
atmass = myfunc(testblock) |
| 654 |
print('test value now {!r}'.format(atmass)) |
| 655 |
assert atmass == [120,280,240] |
| 656 |
|
| 657 |
def test_subscription(self, testdic, setupfull): |
| 658 |
"""Test proper list of dependencies is returned""" |
| 659 |
teststrg = """ |
| 660 |
_model_site.symop = _model_site.id [1] |
| 661 |
""" |
| 662 |
lexer, parser, testblock, _, _ = setupfull |
| 663 |
loopable_cats = {"model_site":["id",["id","symop"]]} |
| 664 |
res = parser.parse(teststrg,lexer=lexer) |
| 665 |
print(repr(res)) |
| 666 |
realfunc,dependencies = py_from_ast.make_python_function(res,"myfunc","_model_site.symop", |
| 667 |
loopable=loopable_cats,depends=True, |
| 668 |
cif_dic=testdic) |
| 669 |
print(realfunc, repr(dependencies)) |
| 670 |
assert dependencies == set(['_model_site.id']) |
| 671 |
|
| 672 |
def test_current_row(self, testdic, setupfull): |
| 673 |
"""Test that methods using Current_Row work properly""" |
| 674 |
teststrg = """ |
| 675 |
_atom_type.description = Current_Row() + 1 |
| 676 |
""" |
| 677 |
lexer, parser, testblock, _, _ = setupfull |
| 678 |
loopable_cats = {'atom_type':["id",['number_in_cell','atomic_mass','num']]} |
| 679 |
ast = parser.parse(teststrg+"\n",lexer=lexer) |
| 680 |
realfunc = py_from_ast.make_python_function(ast,"myfunc","_atom_type.description",loopable=loopable_cats, |
| 681 |
cif_dic=testdic) |
| 682 |
print("Current row statement -> \n" + realfunc) |
| 683 |
exec(realfunc,globals()) |
| 684 |
rownums = myfunc(testblock) |
| 685 |
print('row id now {!r}'.format(rownums)) |
| 686 |
assert rownums == [1,2,3] |
| 687 |
|
| 688 |
def test_loop_statement(self, testdic, setupfull): |
| 689 |
"""Test proper processing of loop statements""" |
| 690 |
teststrg = """ |
| 691 |
mass = 0. |
| 692 |
Loop t as atom_type { |
| 693 |
mass += t.number_in_cell * t.atomic_mass |
| 694 |
} |
| 695 |
_cell.atomic_mass = mass |
| 696 |
""" |
| 697 |
lexer, parser, testblock, _, _ = setupfull |
| 698 |
loopable_cats = {'atom_type':["id",['number_in_cell','atomic_mass']]} |
| 699 |
ast = parser.parse(teststrg+"\n",lexer=lexer) |
| 700 |
realfunc = py_from_ast.make_python_function(ast,"myfunc","_cell.atomic_mass",loopable=loopable_cats, |
| 701 |
cif_dic=testdic) |
| 702 |
print("Loop statement -> \n" + realfunc) |
| 703 |
exec(realfunc,globals()) |
| 704 |
atmass = myfunc(testblock) |
| 705 |
print('atomic mass now %f' % atmass) |
| 706 |
assert atmass == 552.488 |
| 707 |
|
| 708 |
def test_complex_f(self, testdic, setupfull): |
| 709 |
"""This calculation failed during testing""" |
| 710 |
teststrg = """ |
| 711 |
With r as refln |
| 712 |
|
| 713 |
fc = Complex (0., 0.) |
| 714 |
h = r.hkl |
| 715 |
|
| 716 |
Loop a as atom_site { |
| 717 |
|
| 718 |
f = a.site_symmetry_multiplicity * a.occupancy * ( |
| 719 |
r.form_factor_table [a.type_symbol] + |
| 720 |
_atom_type_scat[a.type_symbol].dispersion ) |
| 721 |
|
| 722 |
Loop s as space_group_symop { |
| 723 |
|
| 724 |
t = Exp(-h * s.R * a.tensor_beta * s.RT * h) |
| 725 |
|
| 726 |
fc += f * t * ExpImag(TwoPi *( h *( s.R * a.fract_xyz + s.T))) |
| 727 |
} } |
| 728 |
_refln.F_complex = fc / _space_group.multiplicity |
| 729 |
""" |
| 730 |
lexer, parser, testblock, _, _ = setupfull |
| 731 |
loopable_cats = {'space_group_symop':["id",["id","R","RT","T"]], |
| 732 |
'atom_site':["id",["id","type_symbol","occupancy","site_symmetry_multiplicity", |
| 733 |
"tensor_beta","fract_xyz"]], |
| 734 |
'atom_type_scat':["id",["id","dispersion"]], |
| 735 |
'refln':["hkl",["hkl","form_factor_table"]]} |
| 736 |
ast = parser.parse(teststrg+"\n",lexer=lexer) |
| 737 |
realfunc = py_from_ast.make_python_function(ast,"myfunc","_refln.F_complex",loopable=loopable_cats, |
| 738 |
cif_dic=testdic) |
| 739 |
print("Incoming AST: {!r}".format(ast)) |
| 740 |
print("F_complex statement -> \n" + realfunc) |
| 741 |
exec(realfunc,globals()) |
| 742 |
|
| 743 |
|
| 744 |
def test_fancy_packets(self, testdic, setupfull): |
| 745 |
"""Test that full packets can be dealt with properly""" |
| 746 |
teststrg = """[label,symop] = _model_site.id |
| 747 |
|
| 748 |
a = atom_site[label] |
| 749 |
s = space_group_symop[SymKey(symop)] |
| 750 |
|
| 751 |
_model_site.adp_matrix_beta = s.R * a.matrix_beta * s.RT""" |
| 752 |
lexer, parser, testblock, _, _ = setupfull |
| 753 |
loopable = {"model_site":["id",["id"]], |
| 754 |
"atom_site":["label",["matrix_beta","label"]], |
| 755 |
"space_group_symop":["id",["id","RT","R"]]} |
| 756 |
res = parser.parse(teststrg + "\n",lexer=lexer) |
| 757 |
realfunc,deps = py_from_ast.make_python_function(res,"myfunc","_model_site.adp_matrix_beta", |
| 758 |
depends = True,have_sn=False, |
| 759 |
loopable=loopable,cif_dic=testdic) |
| 760 |
print('model_site.adp_matrix_beta becomes...') |
| 761 |
print(realfunc) |
| 762 |
print(deps) |
| 763 |
assert '_space_group_symop.RT' in deps |
| 764 |
|
| 765 |
def test_array_access(self, testdic, setupfull): |
| 766 |
"""Test that arrays are converted and returned correctly""" |
| 767 |
teststrg = """ |
| 768 |
_model_site.symop = _model_site.id[1] |
| 769 |
""" |
| 770 |
lexer, parser, testblock, _, _ = setupfull |
| 771 |
loopable = {"model_site":["id",["id","symop","adp_eigen_system"]], |
| 772 |
"atom_site":["label",["matrix_beta","label"]], |
| 773 |
"space_group_symop":["id",["id","RT","R"]]} |
| 774 |
res = parser.parse(teststrg + "\n",lexer=lexer) |
| 775 |
realfunc,deps = py_from_ast.make_python_function(res,"myfunc","_model_site.symop", |
| 776 |
depends = True,have_sn=False, |
| 777 |
loopable=loopable,cif_dic=testdic) |
| 778 |
print(realfunc) |
| 779 |
exec(realfunc,globals()) |
| 780 |
testblock.assign_dictionary(testdic) |
| 781 |
b = myfunc(testblock) |
| 782 |
print('symops are now {!r}'.format(b)) |
| 783 |
assert b[1] == '1_555' |
| 784 |
|
| 785 |
def testIfStatement(self, testdic, setupfull): |
| 786 |
"""Test that we handle optional values appropriately""" |
| 787 |
teststrg = """ |
| 788 |
with a as atom_site |
| 789 |
label = a.label |
| 790 |
if (a.adp_type == "Uani") { |
| 791 |
Loop b as atom_site_aniso { |
| 792 |
If(label == b.label) { |
| 793 |
UIJ = b.matrix_U |
| 794 |
Break |
| 795 |
} } } |
| 796 |
|
| 797 |
Else If (a.adp_type == 'bani') { |
| 798 |
Loop b as atom_site_aniso { |
| 799 |
If(label == b.label) { |
| 800 |
|
| 801 |
UIJ = b.matrix_B / (8 * Pi**2) |
| 802 |
Break |
| 803 |
} } } |
| 804 |
Else { |
| 805 |
If (a.adp_type == 'uiso') U = a.U_iso_or_equiv |
| 806 |
Else U = a.B_iso_or_equiv / (8 * Pi**2) |
| 807 |
|
| 808 |
UIJ = U * _cell.convert_Uiso_to_Uij |
| 809 |
} |
| 810 |
_atom_site_aniso.matrix_beta = UIJ """ |
| 811 |
lexer, parser, testblock, _, _ = setupfull |
| 812 |
loopable = { |
| 813 |
"atom_site":["label",["matrix_beta","label"]], |
| 814 |
"atom_site_aniso":["label",["label","matrix_B","matrix_U"]], |
| 815 |
} |
| 816 |
res = parser.parse(teststrg + "\n",lexer=lexer) |
| 817 |
realfunc,deps = py_from_ast.make_python_function(res,"myfunc","_atom_site_aniso.matrix_beta", |
| 818 |
depends = True,have_sn=False, |
| 819 |
loopable=loopable,cif_dic=testdic) |
| 820 |
funclines = realfunc.splitlines() |
| 821 |
for n,l in enumerate(funclines): |
| 822 |
print("%2d:%s"%(n,l)) |
| 823 |
|
| 824 |
exec(realfunc,globals()) |
| 825 |
testblock.assign_dictionary(testdic) |
| 826 |
b = myfunc(testblock) |
| 827 |
print('matrix beta is now {!r}'.format(b)) |
| 828 |
assert b[1][1][1] == 0.031 |
| 829 |
|
| 830 |
def testCaptures(self, testdic, setupfull): |
| 831 |
"""Test that we catch all dictionary names""" |
| 832 |
teststrg = """ |
| 833 |
with a as atom_site_aniso |
| 834 |
label = a.label |
| 835 |
if (a.adp_type == "Uani") { |
| 836 |
UIJ = a.matrix_U |
| 837 |
} |
| 838 |
_atom_site_aniso.matrix_beta = UIJ """ |
| 839 |
lexer, parser, testblock, _, _ = setupfull |
| 840 |
loopable = { |
| 841 |
"atom_site":["label",["matrix_beta","label"]], |
| 842 |
"atom_site_aniso":["label",["label","matrix_B","matrix_U"]], |
| 843 |
} |
| 844 |
res = parser.parse(teststrg + "\n",lexer=lexer) |
| 845 |
realfunc,deps = py_from_ast.make_python_function(res,"myfunc","_atom_site_aniso.matrix_beta", |
| 846 |
depends = True,have_sn=False, |
| 847 |
loopable=loopable,cif_dic=testdic) |
| 848 |
funclines = realfunc.splitlines() |
| 849 |
for n,l in enumerate(funclines): |
| 850 |
print("%2d:%s"%(n,l)) |
| 851 |
|
| 852 |
print("Dependencies: {}".format(deps)) |
| 853 |
exec(realfunc,globals()) |
| 854 |
testblock.assign_dictionary(testdic) |
| 855 |
b = myfunc(testblock) |
| 856 |
print('matrix beta is now {!r}'.format(b)) |
| 857 |
assert b[1][1][1] == 0.031 |