| 1 |
# (C) Copyright 2018- ECMWF. |
| 2 |
# This software is licensed under the terms of the Apache Licence Version 2.0 |
| 3 |
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 4 |
# In applying this licence, ECMWF does not waive the privileges and immunities |
| 5 |
# granted to it by virtue of its status as an intergovernmental organisation |
| 6 |
# nor does it submit to any jurisdiction. |
| 7 |
|
| 8 |
from dataclasses import FrozenInstanceError |
| 9 |
|
| 10 |
import pytest |
| 11 |
from pymbolic.primitives import Expression |
| 12 |
from pydantic import ValidationError |
| 13 |
|
| 14 |
from loki.backend import fgen |
| 15 |
from loki.expression import symbols as sym, parse_expr |
| 16 |
from loki.function import Function |
| 17 |
from loki.ir import nodes as ir |
| 18 |
from loki.subroutine import Subroutine |
| 19 |
from loki.tools import as_tuple, flatten |
| 20 |
from loki.types import BasicType, ProcedureType, Scope, SymbolAttributes |
| 21 |
|
| 22 |
|
| 23 |
@pytest.fixture(name='scope') |
| 24 |
def fixture_scope(): |
| 25 |
return Scope() |
| 26 |
|
| 27 |
@pytest.fixture(name='one') |
| 28 |
def fixture_one(): |
| 29 |
return sym.Literal(1) |
| 30 |
|
| 31 |
@pytest.fixture(name='i') |
| 32 |
def fixture_i(scope): |
| 33 |
return sym.Scalar('i', scope=scope) |
| 34 |
|
| 35 |
@pytest.fixture(name='n') |
| 36 |
def fixture_n(scope): |
| 37 |
return sym.Scalar('n', scope=scope) |
| 38 |
|
| 39 |
@pytest.fixture(name='a_i') |
| 40 |
def fixture_a_i(scope, i): |
| 41 |
return sym.Array('a', dimensions=(i,), scope=scope) |
| 42 |
|
| 43 |
@pytest.fixture(name='a_n') |
| 44 |
def fixture_a_n(scope, n): |
| 45 |
return sym.Array('a', dimensions=(n,), scope=scope) |
| 46 |
|
| 47 |
|
| 48 |
def test_assignment(scope, a_i): |
| 49 |
""" |
| 50 |
Test constructors of :any:`Assignment`. |
| 51 |
""" |
| 52 |
assign = ir.Assignment(lhs=a_i, rhs=sym.Literal(42.0)) |
| 53 |
assert isinstance(assign.lhs, Expression) |
| 54 |
assert isinstance(assign.rhs, Expression) |
| 55 |
assert assign.comment is None |
| 56 |
|
| 57 |
# Ensure "frozen" status of node objects |
| 58 |
with pytest.raises(FrozenInstanceError): |
| 59 |
assign.lhs = sym.Scalar('b', scope=scope) |
| 60 |
with pytest.raises(FrozenInstanceError): |
| 61 |
assign.rhs = sym.Scalar('b', scope=scope) |
| 62 |
|
| 63 |
# Test errors for wrong contructor usage |
| 64 |
with pytest.raises(ValidationError): |
| 65 |
ir.Assignment(lhs='a', rhs=sym.Literal(42.0)) |
| 66 |
with pytest.raises(ValidationError): |
| 67 |
ir.Assignment(lhs=a_i, rhs='42.0 + 6.0') |
| 68 |
with pytest.raises(ValidationError): |
| 69 |
ir.Assignment(lhs=a_i, rhs=sym.Literal(42.0), comment=a_i) |
| 70 |
|
| 71 |
|
| 72 |
def test_loop(scope, one, i, n, a_i): |
| 73 |
""" |
| 74 |
Test constructors of :any:`Loop`. |
| 75 |
""" |
| 76 |
assign = ir.Assignment(lhs=a_i, rhs=sym.Literal(42.0)) |
| 77 |
bounds = sym.Range((one, n)) |
| 78 |
|
| 79 |
loop = ir.Loop(variable=i, bounds=bounds, body=(assign,)) |
| 80 |
assert isinstance(loop.variable, Expression) |
| 81 |
assert isinstance(loop.bounds, Expression) |
| 82 |
assert isinstance(loop.body, tuple) |
| 83 |
assert all(isinstance(n, ir.Node) for n in loop.body) |
| 84 |
assert loop.children == ( i, bounds, (assign,) ) |
| 85 |
|
| 86 |
# Ensure "frozen" status of node objects |
| 87 |
with pytest.raises(FrozenInstanceError): |
| 88 |
loop.variable = sym.Scalar('j', scope=scope) |
| 89 |
with pytest.raises(FrozenInstanceError): |
| 90 |
loop.bounds = sym.Range((n, sym.Scalar('k', scope=scope))) |
| 91 |
with pytest.raises(FrozenInstanceError): |
| 92 |
loop.body = (assign, assign, assign) |
| 93 |
|
| 94 |
# Test auto-casting of the body to tuple |
| 95 |
loop = ir.Loop(variable=i, bounds=bounds, body=assign) |
| 96 |
assert loop.body == (assign,) |
| 97 |
loop = ir.Loop(variable=i, bounds=bounds, body=( (assign,), )) |
| 98 |
assert loop.body == (assign,) |
| 99 |
loop = ir.Loop(variable=i, bounds=bounds, body=( assign, (assign,), assign, None)) |
| 100 |
assert loop.body == (assign, assign, assign) |
| 101 |
|
| 102 |
# Test auto-casting with unnamed constructor args |
| 103 |
loop = ir.Loop(i, bounds, assign) |
| 104 |
assert loop.body == (assign,) |
| 105 |
loop = ir.Loop(i, bounds, [(assign,), None, assign]) |
| 106 |
assert loop.body == (assign, assign) |
| 107 |
|
| 108 |
# Test errors for wrong contructor usage |
| 109 |
with pytest.raises(ValidationError): |
| 110 |
ir.Loop(variable=i, bounds=bounds, body=n) |
| 111 |
with pytest.raises(ValidationError): |
| 112 |
ir.Loop(variable=None, bounds=bounds, body=(assign,)) |
| 113 |
with pytest.raises(ValidationError): |
| 114 |
ir.Loop(variable=i, bounds=None, body=(assign,)) |
| 115 |
|
| 116 |
# TODO: Test pragmas, names and labels |
| 117 |
|
| 118 |
|
| 119 |
def test_conditional(scope, n, a_i): |
| 120 |
""" |
| 121 |
Test constructors of :any:`Conditional`. |
| 122 |
""" |
| 123 |
assign = ir.Assignment(lhs=a_i, rhs=sym.Literal(42.0)) |
| 124 |
condition = parse_expr('i >= 2', scope=scope) |
| 125 |
|
| 126 |
cond = ir.Conditional( |
| 127 |
condition=condition, body=(assign,assign,), else_body=(assign,) |
| 128 |
) |
| 129 |
assert isinstance(cond.condition, Expression) |
| 130 |
assert isinstance(cond.body, tuple) and len(cond.body) == 2 |
| 131 |
assert all(isinstance(n, ir.Node) for n in cond.body) |
| 132 |
assert isinstance(cond.else_body, tuple) and len(cond.else_body) == 1 |
| 133 |
assert all(isinstance(n, ir.Node) for n in cond.else_body) |
| 134 |
assert cond.children == ( condition, (assign, assign), (assign,) ) |
| 135 |
|
| 136 |
with pytest.raises(FrozenInstanceError): |
| 137 |
cond.condition = parse_expr('k == 0', scope=scope) |
| 138 |
with pytest.raises(FrozenInstanceError): |
| 139 |
cond.body = (assign, assign, assign) |
| 140 |
with pytest.raises(FrozenInstanceError): |
| 141 |
cond.else_body = (assign, assign, assign) |
| 142 |
|
| 143 |
# Test auto-casting of the body / else_body to tuple |
| 144 |
cond = ir.Conditional(condition=condition, body=assign) |
| 145 |
assert cond.body == (assign,) and not cond.else_body |
| 146 |
cond = ir.Conditional(condition=condition, body=( (assign,), )) |
| 147 |
assert cond.body == (assign,) and not cond.else_body |
| 148 |
cond = ir.Conditional(condition=condition, body=( assign, (assign,), assign, None)) |
| 149 |
assert cond.body == (assign, assign, assign) and not cond.else_body |
| 150 |
|
| 151 |
cond = ir.Conditional(condition=condition, body=(), else_body=assign) |
| 152 |
assert not cond.body and cond.else_body == (assign,) |
| 153 |
cond = ir.Conditional(condition=condition, body=(), else_body=( (assign,), )) |
| 154 |
assert not cond.body and cond.else_body == (assign,) |
| 155 |
cond = ir.Conditional( |
| 156 |
condition=condition, body=(), else_body=( assign, (assign,), assign, None) |
| 157 |
) |
| 158 |
assert not cond.body and cond.else_body == (assign, assign, assign) |
| 159 |
|
| 160 |
# Test auto-casting with unnamed constructor args |
| 161 |
cond = ir.Conditional(condition) |
| 162 |
assert cond.body is () and not cond.else_body |
| 163 |
cond = ir.Conditional(condition, assign) |
| 164 |
assert cond.body == (assign,) and not cond.else_body |
| 165 |
cond = ir.Conditional(condition, body=[assign, (assign,)], else_body=[assign, None, (assign,)]) |
| 166 |
assert cond.body == (assign, assign) and cond.else_body == (assign, assign) |
| 167 |
|
| 168 |
# TODO: Test inline, name, has_elseif |
| 169 |
|
| 170 |
|
| 171 |
def test_conditional_nested(i, a_i): |
| 172 |
""" |
| 173 |
Test nested chains of constructors of :any:`Conditional` to form |
| 174 |
multi-conditional. |
| 175 |
""" |
| 176 |
multicond = ir.Conditional( |
| 177 |
condition=sym.Comparison(i, '==', sym.IntLiteral(1)), |
| 178 |
body=ir.Assignment(lhs=a_i, rhs=sym.Literal(1.0)), |
| 179 |
else_body=ir.Assignment(lhs=a_i, rhs=sym.Literal(42.0)) |
| 180 |
) |
| 181 |
for idx in range(2, 4): |
| 182 |
multicond = ir.Conditional( |
| 183 |
condition=sym.Comparison(i, '==', sym.IntLiteral(idx)), |
| 184 |
body=ir.Assignment(lhs=a_i, rhs=sym.Literal(float(idx))), |
| 185 |
else_body=multicond, has_elseif=True |
| 186 |
) |
| 187 |
|
| 188 |
# Check that we can recover all bodies from a nested else-if construct |
| 189 |
else_bodies = multicond.else_bodies |
| 190 |
assert len(else_bodies) == 3 |
| 191 |
assert all(isinstance(b, tuple) for b in else_bodies) |
| 192 |
assert isinstance(else_bodies[0][0], ir.Assignment) |
| 193 |
assert else_bodies[0][0].lhs == 'a(i)' and else_bodies[0][0].rhs == '2.0' |
| 194 |
assert isinstance(else_bodies[1][0], ir.Assignment) |
| 195 |
assert else_bodies[1][0].lhs == 'a(i)' and else_bodies[1][0].rhs == '1.0' |
| 196 |
assert isinstance(else_bodies[2][0], ir.Assignment) |
| 197 |
assert else_bodies[2][0].lhs == 'a(i)' and else_bodies[2][0].rhs == '42.0' |
| 198 |
|
| 199 |
# Not try without the final else |
| 200 |
multicond = ir.Conditional( |
| 201 |
condition=sym.Comparison(i, '==', sym.IntLiteral(1)), |
| 202 |
body=ir.Assignment(lhs=a_i, rhs=sym.Literal(1.0)), |
| 203 |
) |
| 204 |
for idx in range(2, 4): |
| 205 |
multicond = ir.Conditional( |
| 206 |
condition=sym.Comparison(i, '==', sym.IntLiteral(idx)), |
| 207 |
body=ir.Assignment(lhs=a_i, rhs=sym.Literal(float(idx))), |
| 208 |
else_body=multicond, has_elseif=True |
| 209 |
) |
| 210 |
else_bodies = multicond.else_bodies |
| 211 |
assert len(else_bodies) == 2 |
| 212 |
assert all(isinstance(b, tuple) for b in else_bodies) |
| 213 |
assert isinstance(else_bodies[0][0], ir.Assignment) |
| 214 |
assert else_bodies[0][0].lhs == 'a(i)' and else_bodies[0][0].rhs == '2.0' |
| 215 |
assert isinstance(else_bodies[1][0], ir.Assignment) |
| 216 |
assert else_bodies[1][0].lhs == 'a(i)' and else_bodies[1][0].rhs == '1.0' |
| 217 |
|
| 218 |
|
| 219 |
def test_section(n, a_n, a_i): |
| 220 |
""" |
| 221 |
Test constructors and behaviour of :any:`Section` nodes. |
| 222 |
""" |
| 223 |
assign = ir.Assignment(lhs=a_i, rhs=sym.Literal(42.0)) |
| 224 |
decl = ir.VariableDeclaration(symbols=(a_n,)) |
| 225 |
func = Function(name='F', spec=(decl,), body=(assign,)) |
| 226 |
|
| 227 |
# Test constructor for nodes and subroutine objects |
| 228 |
sec = ir.Section(body=(assign, assign)) |
| 229 |
assert isinstance(sec.body, tuple) and len(sec.body) == 2 |
| 230 |
assert all(isinstance(n, ir.Node) for n in sec.body) |
| 231 |
with pytest.raises(FrozenInstanceError): |
| 232 |
sec.body = (assign, assign) |
| 233 |
|
| 234 |
sec = ir.Section(body=(func, func)) |
| 235 |
assert isinstance(sec.body, tuple) and len(sec.body) == 2 |
| 236 |
assert all(isinstance(n, Scope) for n in sec.body) |
| 237 |
with pytest.raises(FrozenInstanceError): |
| 238 |
sec.body = (func, func) |
| 239 |
|
| 240 |
sec = ir.Section((assign, assign)) |
| 241 |
assert sec.body == (assign, assign) |
| 242 |
|
| 243 |
# Test auto-casting of the body to tuple |
| 244 |
sec = ir.Section(body=assign) |
| 245 |
assert sec.body == (assign,) |
| 246 |
sec = ir.Section(body=( (assign,), )) |
| 247 |
assert sec.body == (assign,) |
| 248 |
sec = ir.Section(body=( assign, (assign,), assign, None)) |
| 249 |
assert sec.body == (assign, assign, assign) |
| 250 |
sec = ir.Section((assign, (func,), assign, None)) |
| 251 |
assert sec.body == (assign, func, assign) |
| 252 |
|
| 253 |
# Test auto-casting with unnamed constructor args |
| 254 |
sec = ir.Section() |
| 255 |
assert sec.body is () |
| 256 |
sec = ir.Section([(assign,), assign, None, assign]) |
| 257 |
assert sec.body == (assign, assign, assign) |
| 258 |
|
| 259 |
# Test prepend/insert/append additions |
| 260 |
sec = ir.Section(body=func) |
| 261 |
assert sec.body == (func,) |
| 262 |
sec.prepend(assign) |
| 263 |
assert sec.body == (assign, func) |
| 264 |
sec.append((assign, assign)) |
| 265 |
assert sec.body == (assign, func, assign, assign) |
| 266 |
sec.insert(pos=3, node=func) |
| 267 |
assert sec.body == (assign, func, assign, func, assign) |
| 268 |
|
| 269 |
|
| 270 |
def test_section_iterable(a_i): |
| 271 |
""" |
| 272 |
Test iterable-style access for :any:`Section`. |
| 273 |
""" |
| 274 |
assign1 = ir.Assignment(lhs=a_i, rhs=sym.Literal(42.0)) |
| 275 |
assign2 = ir.Assignment(lhs=a_i, rhs=sym.Literal(24.0)) |
| 276 |
assign3 = ir.Assignment(lhs=a_i, rhs=sym.Literal(12.0)) |
| 277 |
|
| 278 |
section = ir.Section(body=(assign1, assign2, assign1, assign3)) |
| 279 |
assert list(section) == list(section.body) |
| 280 |
assert section[0] is assign1 |
| 281 |
assert section[-1] is assign3 |
| 282 |
assert assign1 in section |
| 283 |
assert len(section) == len(section.body) == 4 |
| 284 |
assert section.index(assign1) == 0 |
| 285 |
assert section.index(assign1, 1) == 2 |
| 286 |
assert section.index(assign3, 0, 4) == 3 |
| 287 |
assert as_tuple(section) == (section,) |
| 288 |
assert flatten([section]) == [section] |
| 289 |
|
| 290 |
|
| 291 |
def test_loop_is_not_iterable(one, i, n, a_i): |
| 292 |
""" |
| 293 |
Test that iterable access is not enabled for unrelated internal nodes. |
| 294 |
""" |
| 295 |
assign = ir.Assignment(lhs=a_i, rhs=sym.Literal(42.0)) |
| 296 |
loop = ir.Loop(variable=i, bounds=sym.Range((one, n)), body=(assign,)) |
| 297 |
|
| 298 |
with pytest.raises(TypeError): |
| 299 |
iter(loop) |
| 300 |
with pytest.raises(TypeError): |
| 301 |
_ = loop[0] |
| 302 |
with pytest.raises(TypeError): |
| 303 |
len(loop) |
| 304 |
|
| 305 |
|
| 306 |
def test_associate_iterable(scope, a_i): |
| 307 |
""" |
| 308 |
Test iterable-style access for :any:`Associate` while keeping it atomic. |
| 309 |
""" |
| 310 |
b = sym.Scalar(name='b', scope=scope) |
| 311 |
b_a = sym.Array(name='a', parent=b, scope=scope) |
| 312 |
a = sym.Array(name='a', scope=scope) |
| 313 |
assign1 = ir.Assignment(lhs=a_i, rhs=sym.Literal(42.0)) |
| 314 |
assign2 = ir.Assignment(lhs=a_i.clone(parent=b), rhs=sym.Literal(66.6)) |
| 315 |
|
| 316 |
assoc = ir.Associate(associations=((b_a, a),), body=(assign1, assign2), parent=scope) # pylint: disable=unexpected-keyword-arg |
| 317 |
|
| 318 |
assert list(assoc) == list(assoc.body) |
| 319 |
assert assoc[0] is assign1 |
| 320 |
assert assoc[-1] is assign2 |
| 321 |
assert assign1 in assoc |
| 322 |
assert len(assoc) == len(assoc.body) == 2 |
| 323 |
assert as_tuple(assoc) == (assoc,) |
| 324 |
assert flatten([assoc]) == [assoc] |
| 325 |
|
| 326 |
|
| 327 |
def test_callstatement(scope, one, i, n, a_i): |
| 328 |
""" Test constructor of :any:`CallStatement` nodes. """ |
| 329 |
|
| 330 |
cname = sym.ProcedureSymbol(name='test', scope=scope) |
| 331 |
call = ir.CallStatement( |
| 332 |
name=cname, arguments=(n, a_i), kwarguments=(('i', i), ('j', one)) |
| 333 |
) |
| 334 |
assert isinstance(call.name, Expression) |
| 335 |
assert isinstance(call.arguments, tuple) |
| 336 |
assert all(isinstance(e, Expression) for e in call.arguments) |
| 337 |
assert isinstance(call.kwarguments, tuple) |
| 338 |
assert all(isinstance(e, tuple) for e in call.kwarguments) |
| 339 |
assert all( |
| 340 |
isinstance(k, str) and isinstance(v, Expression) |
| 341 |
for k, v in call.kwarguments |
| 342 |
) |
| 343 |
|
| 344 |
# Ensure "frozen" status of node objects |
| 345 |
with pytest.raises(FrozenInstanceError): |
| 346 |
call.name = sym.ProcedureSymbol('dave', scope=scope) |
| 347 |
with pytest.raises(FrozenInstanceError): |
| 348 |
call.arguments = (a_i, n, one) |
| 349 |
with pytest.raises(FrozenInstanceError): |
| 350 |
call.kwarguments = (('i', one), ('j', i)) |
| 351 |
|
| 352 |
# Test auto-casting of the body to tuple |
| 353 |
call = ir.CallStatement(name=cname, arguments=[a_i, one]) |
| 354 |
assert call.arguments == (a_i, one) and not call.kwarguments |
| 355 |
call = ir.CallStatement(name=cname, arguments=None) |
| 356 |
assert not call.arguments and not call.kwarguments |
| 357 |
call = ir.CallStatement(name=cname, kwarguments=[('i', i), ('j', one)]) |
| 358 |
assert not call.arguments and call.kwarguments == (('i', i), ('j', one)) |
| 359 |
call = ir.CallStatement(name=cname, kwarguments=None) |
| 360 |
assert not call.arguments and not call.kwarguments |
| 361 |
|
| 362 |
# Test auto-casting with unnamed constructor args |
| 363 |
call = ir.CallStatement(cname, a_i) |
| 364 |
assert call.arguments == (a_i,) and not call.kwarguments |
| 365 |
call = ir.CallStatement(cname, [a_i, one], [('i', i), ('j', one)]) |
| 366 |
assert call.arguments == (a_i, one) and call.kwarguments == (('i', i), ('j', one)) |
| 367 |
|
| 368 |
# Test errors for wrong contructor usage |
| 369 |
with pytest.raises(ValidationError): |
| 370 |
ir.CallStatement(name='a', arguments=(sym.Literal(42.0),)) |
| 371 |
with pytest.raises(ValidationError): |
| 372 |
ir.CallStatement(name=cname, arguments=('a',)) |
| 373 |
with pytest.raises(ValidationError): |
| 374 |
ir.Assignment( |
| 375 |
name=cname, arguments=(sym.Literal(42.0),), kwarguments=('i', 'i') |
| 376 |
) |
| 377 |
|
| 378 |
# TODO: Test pragmas, active and chevron |
| 379 |
|
| 380 |
|
| 381 |
def test_callstatement_routine_types(scope): |
| 382 |
"""Test type-checked access to call routine metadata.""" |
| 383 |
|
| 384 |
callee = Subroutine(name='callee') |
| 385 |
cname = sym.ProcedureSymbol( |
| 386 |
name='callee', scope=scope, type=SymbolAttributes(dtype=ProcedureType(procedure=callee)) |
| 387 |
) |
| 388 |
call = ir.CallStatement(name=cname) |
| 389 |
|
| 390 |
assert isinstance(call.procedure_type, ProcedureType) |
| 391 |
assert call.routine is callee |
| 392 |
|
| 393 |
unresolved = sym.ProcedureSymbol(name='unresolved') |
| 394 |
unresolved.type = SymbolAttributes(dtype=BasicType.DEFERRED) |
| 395 |
deferred_call = ir.CallStatement(name=unresolved) |
| 396 |
|
| 397 |
assert deferred_call.procedure_type is BasicType.DEFERRED |
| 398 |
assert deferred_call.routine is BasicType.DEFERRED |
| 399 |
assert not deferred_call.routine |
| 400 |
|
| 401 |
|
| 402 |
def test_callstatement_invalid_routine_type(): |
| 403 |
"""Test runtime failure for invalid call routine type metadata.""" |
| 404 |
|
| 405 |
cname = sym.ProcedureSymbol(name='broken') |
| 406 |
cname.type = SymbolAttributes(dtype=BasicType.DEFERRED) |
| 407 |
cname.type.dtype = object() |
| 408 |
call = ir.CallStatement(name=cname) |
| 409 |
|
| 410 |
with pytest.raises(TypeError, match=r'CallStatement\.name\.type\.dtype must be a DataType'): |
| 411 |
_ = call.procedure_type |
| 412 |
|
| 413 |
|
| 414 |
def test_associate(scope, a_i): |
| 415 |
""" |
| 416 |
Test constructors and scoping bahviour of :any:`Associate`. |
| 417 |
""" |
| 418 |
b = sym.Scalar(name='b', scope=scope) |
| 419 |
b_a = sym.Array(name='a', parent=b, scope=scope) |
| 420 |
a = sym.Array(name='a', scope=scope) |
| 421 |
assign = ir.Assignment(lhs=a_i, rhs=sym.Literal(42.0)) |
| 422 |
assign2 = ir.Assignment(lhs=a_i.clone(parent=b), rhs=sym.Literal(66.6)) |
| 423 |
|
| 424 |
assoc = ir.Associate(associations=((b_a, a),), body=(assign, assign2), parent=scope) # pylint: disable=unexpected-keyword-arg |
| 425 |
assert isinstance(assoc.associations, tuple) |
| 426 |
assert all(isinstance(n, tuple) and len(n) == 2 for n in assoc.associations) |
| 427 |
assert isinstance(assoc.body, tuple) |
| 428 |
assert all(isinstance(n, ir.Node) for n in assoc.body) |
| 429 |
|
| 430 |
# TODO: Check constructor failures, auto-casting and frozen status |
| 431 |
|
| 432 |
# Check provided symbol maps |
| 433 |
assert 'B%a' in assoc.association_map and assoc.association_map['B%a'] is a |
| 434 |
assert b_a in assoc.association_map and assoc.association_map[b_a] is a |
| 435 |
assert 'a' in assoc.inverse_map and assoc.inverse_map['a'] is b_a |
| 436 |
assert a in assoc.inverse_map and assoc.inverse_map[a] is b_a |
| 437 |
|
| 438 |
# Check rescoping facility |
| 439 |
assert assign.lhs.scope is scope |
| 440 |
assert assign2.lhs.scope is scope |
| 441 |
assoc.rescope_symbols() |
| 442 |
assert assign.lhs.scope is assoc |
| 443 |
assert assign2.lhs.scope is scope |
| 444 |
|
| 445 |
|
| 446 |
def test_multiconditional(scope, a_i, i): |
| 447 |
""" |
| 448 |
Test constructors and scoping behaviour of :any:`MultiConditional`. |
| 449 |
""" |
| 450 |
assign1 = ir.Assignment(lhs=a_i, rhs=sym.Literal(42.0)) |
| 451 |
assign2 = ir.Assignment(lhs=a_i, rhs=sym.Literal(66.6)) |
| 452 |
assign3 = ir.Assignment(lhs=a_i, rhs=sym.Literal(12.3)) |
| 453 |
values = ((sym.Literal(1),), (sym.Literal(2),)) |
| 454 |
multicond = ir.MultiConditional( |
| 455 |
expr=i, values=values, bodies=((assign1,), (assign2,)), else_body=(assign3,) |
| 456 |
) |
| 457 |
assert isinstance(multicond.expr, Expression) |
| 458 |
assert isinstance(multicond.values, tuple) |
| 459 |
assert all(isinstance(e, tuple) for e in multicond.values) |
| 460 |
assert all( |
| 461 |
all(isinstance(v, Expression) for v in val) for val in multicond.values |
| 462 |
) |
| 463 |
assert isinstance(multicond.bodies, tuple) |
| 464 |
assert all(isinstance(b, tuple) for b in multicond.bodies) |
| 465 |
assert all( |
| 466 |
all(isinstance(b, ir.Node) for b in body) for body in multicond.bodies |
| 467 |
) |
| 468 |
assert isinstance(multicond.else_body, tuple) |
| 469 |
assert all(isinstance(b, ir.Node) for b in multicond.else_body) |
| 470 |
assert multicond.children == ( |
| 471 |
i, ((sym.Literal(1),), (sym.Literal(2),)), ((assign1,), (assign2,)), (assign3,) |
| 472 |
) |
| 473 |
|
| 474 |
# Ensure "frozen" status of node objects |
| 475 |
with pytest.raises(FrozenInstanceError): |
| 476 |
multicond.expr = parse_expr('k', scope=scope) |
| 477 |
with pytest.raises(FrozenInstanceError): |
| 478 |
multicond.values = ((sym.Literal(3),), (sym.Literal(4),)) |
| 479 |
with pytest.raises(FrozenInstanceError): |
| 480 |
multicond.bodies = ((assign1,), (assign1,)) |
| 481 |
with pytest.raises(FrozenInstanceError): |
| 482 |
multicond.else_body = (assign1, assign2) |
| 483 |
|
| 484 |
# Test auto-casting of the bodies and else_body to (nested) tuple(s) |
| 485 |
multicond = ir.MultiConditional( |
| 486 |
expr=i, values=sym.Literal(1), bodies=(()), else_body=() |
| 487 |
) |
| 488 |
assert multicond.values == ((sym.Literal(1),),) |
| 489 |
multicond = ir.MultiConditional( |
| 490 |
expr=i, values=((sym.Literal(1),),sym.Literal(2),), bodies=(()), else_body=() |
| 491 |
) |
| 492 |
assert multicond.values == ((sym.Literal(1),), (sym.Literal(2),)) |
| 493 |
multicond = ir.MultiConditional( |
| 494 |
expr=i, values=values, bodies=assign1, else_body=() |
| 495 |
) |
| 496 |
assert multicond.bodies == ((assign1,),) |
| 497 |
multicond = ir.MultiConditional( |
| 498 |
expr=i, values=values, bodies=((assign1,), assign2), else_body=() |
| 499 |
) |
| 500 |
assert multicond.bodies == ((assign1,), (assign2,)) |
| 501 |
multicond = ir.MultiConditional( |
| 502 |
expr=i, values=(()), bodies=(()), else_body=assign3 |
| 503 |
) |
| 504 |
assert multicond.else_body == (assign3,) |
| 505 |
multicond = ir.MultiConditional( |
| 506 |
expr=i, values=(()), bodies=(()), else_body=((assign3,), assign2) |
| 507 |
) |
| 508 |
assert multicond.else_body == (assign3, assign2) |
| 509 |
|
| 510 |
|
| 511 |
def test_stmt_nodes(n, i): |
| 512 |
""" |
| 513 |
Test constructors and scoping behaviour of various :any:`GenericStmt` nodes. |
| 514 |
""" |
| 515 |
|
| 516 |
assert fgen(ir.GenericStmt('Hello World')) == 'Hello World' |
| 517 |
with pytest.raises(ValidationError): |
| 518 |
ir.GenericStmt(n) |
| 519 |
|
| 520 |
# Potential symbol quantifiers |
| 521 |
assert fgen(ir.ImplicitStmt()) == 'IMPLICIT NONE' |
| 522 |
assert fgen(ir.ImplicitStmt('NONE')) == 'IMPLICIT NONE' |
| 523 |
assert fgen(ir.ImplicitStmt(i)) == 'IMPLICIT i' |
| 524 |
assert fgen(ir.ImplicitStmt((n, i))) == 'IMPLICIT n, i' |
| 525 |
|
| 526 |
assert fgen(ir.SaveStmt()) == 'SAVE' |
| 527 |
assert fgen(ir.SaveStmt(i)) == 'SAVE i' |
| 528 |
assert fgen(ir.SaveStmt((n, i))) == 'SAVE n, i' |
| 529 |
|
| 530 |
assert fgen(ir.PublicStmt()) == 'PUBLIC' |
| 531 |
assert fgen(ir.PublicStmt(i)) == 'PUBLIC i' |
| 532 |
assert fgen(ir.PublicStmt((n, i))) == 'PUBLIC n, i' |
| 533 |
|
| 534 |
assert fgen(ir.PrivateStmt()) == 'PRIVATE' |
| 535 |
assert fgen(ir.PrivateStmt(i)) == 'PRIVATE i' |
| 536 |
assert fgen(ir.PrivateStmt((n, i))) == 'PRIVATE n, i' |
| 537 |
|
| 538 |
assert fgen(ir.CommonStmt(i)) == 'COMMON i' |
| 539 |
assert fgen(ir.CommonStmt((n, i))) == 'COMMON n, i' |
| 540 |
with pytest.raises(ValidationError): |
| 541 |
ir.CommonStmt() # Need at least one argument |
| 542 |
|
| 543 |
# Control flow statements |
| 544 |
assert fgen(ir.ContainsStmt()) == 'CONTAINS' |
| 545 |
with pytest.raises(ValidationError): |
| 546 |
ir.ContainsStmt(n) |
| 547 |
|
| 548 |
assert fgen(ir.ReturnStmt()) == 'RETURN' |
| 549 |
with pytest.raises(ValidationError): |
| 550 |
ir.ReturnStmt(n) |
| 551 |
|
| 552 |
assert fgen(ir.CycleStmt()) == 'CYCLE' |
| 553 |
with pytest.raises(ValidationError): |
| 554 |
ir.CycleStmt(n) |
| 555 |
|
| 556 |
assert fgen(ir.ContinueStmt()) == 'CONTINUE' |
| 557 |
with pytest.raises(ValidationError): |
| 558 |
ir.ContinueStmt(n) |
| 559 |
|
| 560 |
assert fgen(ir.StopStmt()) == 'STOP' |
| 561 |
assert fgen(ir.StopStmt(1)) == 'STOP 1' |
| 562 |
assert fgen(ir.StopStmt("'all done'")) == "STOP 'all done'" |
| 563 |
|
| 564 |
assert fgen(ir.ExitStmt()) == 'EXIT' |
| 565 |
assert fgen(ir.ExitStmt(1)) == 'EXIT 1' |
| 566 |
with pytest.raises(ValidationError): |
| 567 |
ir.ExitStmt('NoStringsAllowed') |
| 568 |
|
| 569 |
assert fgen(ir.GotoStmt('2345')) == 'GO TO 2345' |
| 570 |
with pytest.raises(ValidationError): |
| 571 |
assert ir.GotoStmt() |
| 572 |
|
| 573 |
# Output statements |
| 574 |
assert fgen(ir.PrintStmt(i)) == 'PRINT i' |
| 575 |
assert fgen(ir.PrintStmt((n, i))) == 'PRINT n, i' |
| 576 |
assert ir.PrintStmt((n, i)).values == (n, i) |
| 577 |
assert ir.PrintStmt((n, i)).text == 'n, i' |
| 578 |
with pytest.raises(ValidationError): |
| 579 |
ir.PrintStmt() # Need at least one argument |
| 580 |
with pytest.raises(ValidationError, match='PrintStmt uses values='): |
| 581 |
ir.PrintStmt(text=(n, i)) |
| 582 |
|
| 583 |
assert fgen(ir.FormatStmt()) == 'FORMAT' |
| 584 |
assert fgen(ir.FormatStmt(i)) == 'FORMAT(i)' |
| 585 |
assert fgen(ir.FormatStmt((n, i))) == 'FORMAT(n, i)' |
| 586 |
assert ir.FormatStmt((n, i)).values == (n, i) |
| 587 |
assert ir.FormatStmt((n, i)).text == 'n, i' |
| 588 |
with pytest.raises(ValidationError, match='FormatStmt uses values='): |
| 589 |
ir.FormatStmt(text=(n, i)) |
| 590 |
|
| 591 |
# Check adding labels for certain control flow statements |
| 592 |
assert ir.ReturnStmt(label=42).label == '42' |
| 593 |
assert ir.ReturnStmt(label='666').label == '666' |
| 594 |
assert ir.ReturnStmt().label is None |
| 595 |
assert ir.CycleStmt(label=42).label == '42' |
| 596 |
assert ir.ContinueStmt(label=42).label == '42' |
| 597 |
assert ir.ExitStmt(label=42).label == '42' |
| 598 |
assert ir.StopStmt(label=42).label == '42' |