| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
from __future__ import annotations |
| 19 |
|
| 20 |
import logging |
| 21 |
import pytest |
| 22 |
from unittest import mock |
| 23 |
from lark.lexer import Token as LarkToken |
| 24 |
from lark.tree import Tree as LarkTree |
| 25 |
|
| 26 |
from pyocd.core import exceptions |
| 27 |
from pyocd.debug.sequences.scope import Scope |
| 28 |
from pyocd.debug.sequences.sequences import ( |
| 29 |
DebugSequenceSemanticError, |
| 30 |
DebugSequenceExecutionContext, |
| 31 |
DebugSequence, |
| 32 |
Block, |
| 33 |
WhileControl, |
| 34 |
IfControl, |
| 35 |
Parser, |
| 36 |
SemanticChecker, |
| 37 |
_ConstantFolder, |
| 38 |
) |
| 39 |
from pyocd.core.session import Session |
| 40 |
from pyocd.probe.debug_probe import DebugProbe |
| 41 |
|
| 42 |
|
| 43 |
class SequenceFunctionsDelegateForTesting: |
| 44 |
def valid_fn_no_args(self): |
| 45 |
pass |
| 46 |
|
| 47 |
def valid_fn_1_arg(self, a: int): |
| 48 |
pass |
| 49 |
|
| 50 |
def valid_fn_3_arg(self, a: int, b: int, c: int): |
| 51 |
pass |
| 52 |
|
| 53 |
def valid_fn_varg(self, a: int, *vargs): |
| 54 |
pass |
| 55 |
|
| 56 |
def fn_with_str_parm(self, foo: str): |
| 57 |
pass |
| 58 |
|
| 59 |
def sequence(self, name: str): |
| 60 |
pass |
| 61 |
|
| 62 |
class SequenceDelegateForTesting: |
| 63 |
|
| 64 |
|
| 65 |
_root = Scope(name="unit-test-root") |
| 66 |
|
| 67 |
@property |
| 68 |
def sequences(self): |
| 69 |
return set() |
| 70 |
|
| 71 |
@property |
| 72 |
def cmsis_pack_device(self): |
| 73 |
return mock.MagicMock() |
| 74 |
|
| 75 |
def get_root_scope(self, context) -> Scope: |
| 76 |
self._root.set('rootvar', 42) |
| 77 |
return self._root |
| 78 |
|
| 79 |
def has_sequence_with_name(self, name, pname=None): |
| 80 |
return name == "valid" |
| 81 |
|
| 82 |
def get_sequence_with_name(self, name, pname=None): |
| 83 |
print(f"SequenceDelegateForTesting.get_sequence_by_name: name={name} pname={pname}") |
| 84 |
if name == "valid": |
| 85 |
x = mock.Mock() |
| 86 |
print(f"returning {x}") |
| 87 |
return x |
| 88 |
else: |
| 89 |
return None |
| 90 |
|
| 91 |
def get_protocol(self): |
| 92 |
return 2 |
| 93 |
|
| 94 |
def get_connection_type(self): |
| 95 |
return 1 |
| 96 |
|
| 97 |
def get_traceout(self): |
| 98 |
return 0 |
| 99 |
|
| 100 |
def get_traceclockin(self) -> int: |
| 101 |
return 0 |
| 102 |
|
| 103 |
def get_traceclockout(self) -> int: |
| 104 |
return 0 |
| 105 |
|
| 106 |
def get_sequence_functions(self) -> SequenceFunctionsDelegateForTesting: |
| 107 |
return SequenceFunctionsDelegateForTesting() |
| 108 |
|
| 109 |
|
| 110 |
class MockProbe: |
| 111 |
def __init__(self): |
| 112 |
self.wire_protocol = DebugProbe.Protocol.SWD |
| 113 |
|
| 114 |
@pytest.fixture(scope='function') |
| 115 |
def session(): |
| 116 |
s = Session(None) |
| 117 |
setattr(s, '_probe', MockProbe()) |
| 118 |
return s |
| 119 |
|
| 120 |
@pytest.fixture(scope='function') |
| 121 |
def delegate(): |
| 122 |
return SequenceDelegateForTesting() |
| 123 |
|
| 124 |
@pytest.fixture(scope='function') |
| 125 |
def scope(): |
| 126 |
s = Scope(name="fixture") |
| 127 |
s.set("a", 0) |
| 128 |
s.set("b", 128) |
| 129 |
return s |
| 130 |
|
| 131 |
@pytest.fixture(scope='function') |
| 132 |
def context(session, delegate): |
| 133 |
c = DebugSequenceExecutionContext(session, delegate, pname=None) |
| 134 |
return c |
| 135 |
|
| 136 |
@pytest.fixture(scope='function') |
| 137 |
def block_context(context: DebugSequenceExecutionContext): |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
seq = DebugSequence('test_sequence') |
| 146 |
scope = seq._create_scope(context) |
| 147 |
|
| 148 |
ctxmgr = context._push(seq, scope) |
| 149 |
return context |
| 150 |
|
| 151 |
class TestDebugSequenceScope: |
| 152 |
def test_name(self): |
| 153 |
s = Scope(name='test') |
| 154 |
assert s.name == 'test' |
| 155 |
|
| 156 |
def test_get_1(self, scope): |
| 157 |
assert scope.get('a') == 0 |
| 158 |
assert scope.get('b') == 128 |
| 159 |
assert len(scope) == 2 |
| 160 |
|
| 161 |
def test_get_invalid(self, scope): |
| 162 |
with pytest.raises(KeyError): |
| 163 |
scope.get('missing') |
| 164 |
with pytest.raises(KeyError): |
| 165 |
scope.get('') |
| 166 |
|
| 167 |
def test_set_1(self, scope): |
| 168 |
scope.set('a', 10) |
| 169 |
assert scope.get('a') == 10 |
| 170 |
assert scope.get('b') == 128 |
| 171 |
|
| 172 |
def test_set_ro(self, scope): |
| 173 |
scope.set('x', 32, readonly=True) |
| 174 |
assert scope.is_read_only('x') |
| 175 |
assert scope.get('x') == 32 |
| 176 |
with pytest.raises(RuntimeError): |
| 177 |
scope.set('x', 50) |
| 178 |
|
| 179 |
def test_freeze(self, scope): |
| 180 |
scope.set('y', 16) |
| 181 |
scope.freeze() |
| 182 |
with pytest.raises(RuntimeError): |
| 183 |
scope.set('a', 1) |
| 184 |
assert len(scope) == 3 |
| 185 |
assert scope.is_read_only('b') |
| 186 |
|
| 187 |
def test_parent_1(self, scope): |
| 188 |
sub = Scope(parent=scope, name='subscope') |
| 189 |
assert sub.parent is scope |
| 190 |
assert sub.get('a') == 0 |
| 191 |
assert sub.get('b') == 128 |
| 192 |
assert sub.is_defined('b') |
| 193 |
|
| 194 |
def test_parent_2(self, scope): |
| 195 |
sub = Scope(parent=scope, name='subscope') |
| 196 |
sub.set('x', 1) |
| 197 |
assert sub.parent is scope |
| 198 |
assert sub.get('a') == 0 |
| 199 |
assert sub.get('x') == 1 |
| 200 |
assert sub.is_defined('b') |
| 201 |
assert not scope.is_defined('x') |
| 202 |
|
| 203 |
def test_parent_3(self, scope): |
| 204 |
sub = Scope(parent=scope, name='subscope') |
| 205 |
sub.set('a', 1) |
| 206 |
assert sub.parent is scope |
| 207 |
assert sub.get('a') == 1 |
| 208 |
assert scope.get('a') == 1 |
| 209 |
assert sub.is_defined('a') |
| 210 |
assert 'a' not in sub._variables |
| 211 |
assert scope.is_defined('a') |
| 212 |
|
| 213 |
def test_parent_4(self, scope): |
| 214 |
sub = Scope(parent=scope, name='subscope') |
| 215 |
scope.set('y', 12, readonly=True) |
| 216 |
with pytest.raises(RuntimeError): |
| 217 |
sub.set('y', 1) |
| 218 |
assert sub.get('y') == 12 |
| 219 |
assert scope.get('y') == 12 |
| 220 |
|
| 221 |
def test_copy(self, scope): |
| 222 |
other = Scope(name='other') |
| 223 |
other.copy_variables(from_scope=scope, variables=['a', 'b']) |
| 224 |
assert other.is_defined('a') \ |
| 225 |
and other.is_defined('b') |
| 226 |
assert other.get('a') == scope.get('a') |
| 227 |
assert other.get('b') == scope.get('b') |
| 228 |
|
| 229 |
def test_copy_undefined(self, scope): |
| 230 |
other = Scope(name='other') |
| 231 |
other.copy_variables(from_scope=scope, variables=['a', 'missing']) |
| 232 |
assert other.is_defined('a') \ |
| 233 |
and not other.is_defined('b') \ |
| 234 |
and not other.is_defined('missing') |
| 235 |
assert other.get('a') == scope.get('a') |
| 236 |
|
| 237 |
|
| 238 |
class TestDebugSequenceParser: |
| 239 |
def test_semicolons(self): |
| 240 |
|
| 241 |
ast = Parser().parse("12;") |
| 242 |
print("ast=", ast) |
| 243 |
assert ast.children[0].children[0] == 12 |
| 244 |
|
| 245 |
|
| 246 |
ast = Parser().parse("12") |
| 247 |
print("ast=", ast) |
| 248 |
assert ast.children[0].children[0] == 12 |
| 249 |
|
| 250 |
def test_fncall_no_args(self): |
| 251 |
ast = Parser().parse("myfunc();") |
| 252 |
print("ast=", ast) |
| 253 |
fncall = ast.children[0].children[0] |
| 254 |
assert fncall.data == 'fncall' |
| 255 |
assert len(fncall.children) == 1 |
| 256 |
assert fncall.children[0] == LarkToken('IDENT', 'myfunc') |
| 257 |
|
| 258 |
def test_fncall_3_arg(self): |
| 259 |
ast = Parser().parse("myfunc(1, 2, 3);") |
| 260 |
print("ast=", ast) |
| 261 |
fncall = ast.children[0].children[0] |
| 262 |
assert fncall.data == 'fncall' |
| 263 |
assert len(fncall.children) == 4 |
| 264 |
assert fncall.children[0] == LarkToken('IDENT', 'myfunc') |
| 265 |
assert fncall.children[1] == 1 |
| 266 |
assert fncall.children[2] == 2 |
| 267 |
assert fncall.children[3] == 3 |
| 268 |
|
| 269 |
def test_bad_input(self): |
| 270 |
with pytest.raises(exceptions.Error): |
| 271 |
Parser().parse("bad input ••• wooo") |
| 272 |
|
| 273 |
def test_unary_op_assign(self): |
| 274 |
|
| 275 |
ast = Parser().parse("__Result = -1; // DAP is unavailable") |
| 276 |
print("ast=", ast) |
| 277 |
|
| 278 |
def test_assign_minus_negative(self): |
| 279 |
ast = Parser().parse("a = 1 - -1;") |
| 280 |
print("ast=", ast) |
| 281 |
e = ast.children[0].children[0].children[2] |
| 282 |
assert e.children[0] == 1 |
| 283 |
assert e.children[1] == LarkToken('MINUS', '-') |
| 284 |
assert e.children[2].data == 'unary_expr' |
| 285 |
assert e.children[2].children[0] == LarkToken('MINUS', '-') |
| 286 |
assert e.children[2].children[1] == 1 |
| 287 |
|
| 288 |
def test_assign_minus_positive(self): |
| 289 |
ast = Parser().parse("a = 1 - +1;") |
| 290 |
print("ast=", ast) |
| 291 |
e = ast.children[0].children[0].children[2] |
| 292 |
assert e.children[0] == 1 |
| 293 |
assert e.children[1] == LarkToken('MINUS', '-') |
| 294 |
assert e.children[2].data == 'unary_expr' |
| 295 |
assert e.children[2].children[0] == LarkToken('PLUS', '+') |
| 296 |
assert e.children[2].children[1] == 1 |
| 297 |
|
| 298 |
@pytest.mark.parametrize("literal", [ |
| 299 |
"1000", "0x3e8", "0x3E8", "0b1111101000", |
| 300 |
"1000u", "1000U", "0x3e8u", "0x3e8U", "0b1111101000u", "0b1111101000U", |
| 301 |
]) |
| 302 |
def test_unsigned_int_lit(self, literal): |
| 303 |
ast = Parser().parse(f"{literal};") |
| 304 |
print("ast=", ast) |
| 305 |
assert ast.children[0].children[0] == 1000 |
| 306 |
|
| 307 |
def test_simple_assign_expr(self): |
| 308 |
ast = Parser().parse("x = 10;") |
| 309 |
print("ast=", ast) |
| 310 |
e = ast.children[0].children[0] |
| 311 |
assert e.children[2] == 10 |
| 312 |
|
| 313 |
def test_multi_assign_expr(self): |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
ast = Parser().parse("x = (y += (1 + 1)) + 2;") |
| 330 |
print("ast=", ast.pretty()) |
| 331 |
e = ast.children[0].children[0] |
| 332 |
assert e.data == LarkToken('RULE', 'assign_expr') |
| 333 |
assert e.children[0] == 'x' |
| 334 |
e = e.children[2].children[0] |
| 335 |
assert e.data == LarkToken('RULE', 'assign_expr') |
| 336 |
assert e.children[0] == 'y' |
| 337 |
assert e.children[1] == "+=" |
| 338 |
|
| 339 |
def test_assign_expr_precedence_1(self): |
| 340 |
ast = Parser().parse("x = 1 ? 2 : 3;") |
| 341 |
print("ast=", ast.pretty()) |
| 342 |
e = ast.children[0].children[0] |
| 343 |
assert e.data == LarkToken('RULE', 'assign_expr') |
| 344 |
|
| 345 |
def test_assign_expr_precedence_2(self): |
| 346 |
ast = Parser().parse("1 ? x = 2 : 3;") |
| 347 |
print("ast=", ast.pretty()) |
| 348 |
e = ast.children[0].children[0] |
| 349 |
assert e.data == LarkToken('RULE', 'ternary_expr') |
| 350 |
assert e.children[1].data == LarkToken('RULE', 'assign_expr') |
| 351 |
|
| 352 |
class TestDebugSequenceBlockExecute: |
| 353 |
def test_semicolons(self, block_context): |
| 354 |
block_context.current_scope.set("a", 0) |
| 355 |
|
| 356 |
|
| 357 |
s = Block("a == 0;") |
| 358 |
s.execute(block_context) |
| 359 |
assert block_context.current_scope.get("a") == 0 |
| 360 |
|
| 361 |
|
| 362 |
s = Block("a == 0") |
| 363 |
s.execute(block_context) |
| 364 |
assert block_context.current_scope.get("a") == 0 |
| 365 |
|
| 366 |
def test_set_var(self, block_context): |
| 367 |
s = Block("__var x = 100;") |
| 368 |
s.execute(block_context) |
| 369 |
assert block_context.current_scope.get("x") == 100 |
| 370 |
|
| 371 |
def test_var_no_expr(self, block_context): |
| 372 |
s = Block("__var x;") |
| 373 |
s.execute(block_context) |
| 374 |
assert block_context.current_scope.get("x") == 0 |
| 375 |
|
| 376 |
def test_var_no_expr_separate_set(self, block_context): |
| 377 |
s = Block("__var x; x = 123;") |
| 378 |
s.execute(block_context) |
| 379 |
assert block_context.current_scope.get("x") == 123 |
| 380 |
|
| 381 |
@pytest.mark.parametrize(("expr", "result"), [ |
| 382 |
("-1", 0xffffffffffffffff), |
| 383 |
("-2", 0xfffffffffffffffe), |
| 384 |
("!1", 0), |
| 385 |
("!0", 1), |
| 386 |
("+1", 1), |
| 387 |
("~0xffff", 0xffffffffffff0000), |
| 388 |
]) |
| 389 |
def test_int_unary_ops(self, block_context, expr, result): |
| 390 |
s = Block("__var x = %s;" % expr) |
| 391 |
s.execute(block_context) |
| 392 |
assert block_context.current_scope.get("x") == result |
| 393 |
|
| 394 |
@pytest.mark.parametrize(("expr", "result"), [ |
| 395 |
("1 + 1", 2), |
| 396 |
("2 - 1", 1), |
| 397 |
("2 * 4", 8), |
| 398 |
("4 / 2", 2), |
| 399 |
("5 % 4", 1), |
| 400 |
("1 << 12", 4096), |
| 401 |
("0x80 >> 4", 0x8), |
| 402 |
("0b1000 | 0x2", 0b1010), |
| 403 |
("0b1100 & 0b0100", 0b0100), |
| 404 |
]) |
| 405 |
def test_int_expr(self, block_context, expr, result): |
| 406 |
s = Block("__var x = %s;" % expr) |
| 407 |
s.execute(block_context) |
| 408 |
assert block_context.current_scope.get("x") == result |
| 409 |
|
| 410 |
@pytest.mark.parametrize(("expr", "result"), [ |
| 411 |
("1 == 1", 1), |
| 412 |
("1 == 0", 0), |
| 413 |
("0 == 1", 0), |
| 414 |
("1 != 1", 0), |
| 415 |
("1 != 0", 1), |
| 416 |
("0 != 1", 1), |
| 417 |
("20 > 10", 1), |
| 418 |
("20 > 20", 0), |
| 419 |
("20 > 100", 0), |
| 420 |
("5 >= 2", 1), |
| 421 |
("5 >= 5", 1), |
| 422 |
("5 >= 100", 0), |
| 423 |
("10 < 20", 1), |
| 424 |
("10 < 10", 0), |
| 425 |
("10 < 4", 0), |
| 426 |
("10 <= 20", 1), |
| 427 |
("10 <= 10", 1), |
| 428 |
("10 <= 5", 0), |
| 429 |
]) |
| 430 |
def test_bool_cmp_expr(self, block_context, expr, result): |
| 431 |
s = Block("__var x = %s;" % expr) |
| 432 |
s.execute(block_context) |
| 433 |
assert block_context.current_scope.get("x") == result |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
@pytest.mark.parametrize(("expr", "result"), [ |
| 438 |
("1 && 1", 1), |
| 439 |
("1 && 0", 0), |
| 440 |
("0 && 1", 0), |
| 441 |
("0 && 0", 0), |
| 442 |
("1 || 1", 1), |
| 443 |
("1 || 0", 1), |
| 444 |
("0 || 1", 1), |
| 445 |
("0 || 0", 0), |
| 446 |
("5 && 1000", 1), |
| 447 |
("432 && 0", 0), |
| 448 |
("0 && 2", 0), |
| 449 |
("0 && 0", 0), |
| 450 |
("348 || 4536", 1), |
| 451 |
("5 || 0", 1), |
| 452 |
("0 || 199", 1), |
| 453 |
("0 || 0", 0), |
| 454 |
]) |
| 455 |
def test_bool_and_or_expr(self, block_context, expr, result): |
| 456 |
s = Block("__var x = %s;" % expr) |
| 457 |
s.execute(block_context) |
| 458 |
assert block_context.current_scope.get("x") == result |
| 459 |
|
| 460 |
@pytest.mark.parametrize(("expr", "result"), [ |
| 461 |
("1 + 2 * 5", 11), |
| 462 |
("7 * 12 + 5", 89), |
| 463 |
("1 + 5 - 3", 3), |
| 464 |
("(1 + 2) * 5", 15), |
| 465 |
("1 + (2 * 5)", 11), |
| 466 |
("2 + 16 / 2", 10), |
| 467 |
("1 + 17 % 3", 3), |
| 468 |
("2 * 3 * 4", 24), |
| 469 |
("0 || 1 && 1", 1), |
| 470 |
("0 && 1 || 0", 0), |
| 471 |
("1 == 6 > 5", 1), |
| 472 |
("1 != 6 < 12", 0), |
| 473 |
("1 << 4 > 1 << 2", 1), |
| 474 |
("1 << (4 > 1) << 2", 8), |
| 475 |
("!1 == 0", 1), |
| 476 |
]) |
| 477 |
def test_precedence(self, block_context, expr, result): |
| 478 |
s = Block("__var x = %s;" % expr) |
| 479 |
logging.info("Block: %s", s._ast.pretty()) |
| 480 |
s.execute(block_context) |
| 481 |
actual = block_context.current_scope.get("x") |
| 482 |
assert actual == result |
| 483 |
|
| 484 |
@pytest.mark.parametrize(("expr", "result"), [ |
| 485 |
("(7 * (1 << 3) + 1) >> 1", 28), |
| 486 |
]) |
| 487 |
def test_longer_expr(self, block_context, expr, result): |
| 488 |
s = Block("__var x = %s;" % expr) |
| 489 |
logging.info("Block: %s", s._ast.pretty()) |
| 490 |
s.execute(block_context) |
| 491 |
actual = block_context.current_scope.get("x") |
| 492 |
assert actual == result |
| 493 |
|
| 494 |
def test_unary_op_assign_block(self, block_context): |
| 495 |
|
| 496 |
s = Block("__Result = -1; // DAP is unavailable") |
| 497 |
logging.info(f"Block: {s._ast.pretty()}") |
| 498 |
s.execute(block_context) |
| 499 |
actual = block_context.current_scope.get("__Result") |
| 500 |
assert actual == 0xffffffffffffffff |
| 501 |
|
| 502 |
@pytest.mark.parametrize(("expr", "result"), [ |
| 503 |
("1 ? (1 + 1) : (1 - 1)", 2), |
| 504 |
("0 ? 10: 20", 20), |
| 505 |
("1 << 5 ? 17 * (2 + 1) : 0", 51), |
| 506 |
]) |
| 507 |
def test_ternary_expr(self, block_context, expr, result): |
| 508 |
s = Block(f"__var x = {expr};") |
| 509 |
logging.info("Block: %s", s._ast.pretty()) |
| 510 |
s.execute(block_context) |
| 511 |
actual = block_context.current_scope.get("x") |
| 512 |
assert actual == result |
| 513 |
|
| 514 |
@pytest.mark.parametrize(("expr", "result"), [ |
| 515 |
("x += 1", 2), |
| 516 |
("x -= 1", 0), |
| 517 |
("x *= 10", 10), |
| 518 |
("x /= 1", 1), |
| 519 |
("x %= 1", 0), |
| 520 |
("x &= 3", 1), |
| 521 |
("x |= 0x40", 0x41), |
| 522 |
("x ^= 3", 2), |
| 523 |
("x <<= 5", 1 << 5), |
| 524 |
("x >>= 0", 1), |
| 525 |
]) |
| 526 |
def test_compound_assign(self, block_context, expr, result): |
| 527 |
s = Block("__var x = 1; %s;" % expr) |
| 528 |
s.execute(block_context) |
| 529 |
assert block_context.current_scope.get("x") == result |
| 530 |
|
| 531 |
def test_assign_expr_block(self, block_context): |
| 532 |
s = Block("__var x; __var y; x = 1 + (y = 2);") |
| 533 |
logging.info(f"Block: {s._ast.pretty()}") |
| 534 |
s.execute(block_context) |
| 535 |
assert block_context.current_scope.get("x") == 3 |
| 536 |
assert block_context.current_scope.get("y") == 2 |
| 537 |
|
| 538 |
class TestConstantFolder: |
| 539 |
def _get_folded_ast(self, expr): |
| 540 |
ast = Parser.parse(expr) |
| 541 |
logging.info("Unoptimized AST:\n%s", ast.pretty()) |
| 542 |
ast_opt = _ConstantFolder().transform(ast) |
| 543 |
logging.info("Optimized AST:\n%s", ast_opt.pretty()) |
| 544 |
return ast_opt |
| 545 |
|
| 546 |
def _do_fold_test(self, expr, expected): |
| 547 |
ast_opt = self._get_folded_ast(expr) |
| 548 |
assert isinstance(ast_opt, LarkTree) |
| 549 |
assert ast_opt.data == 'start' |
| 550 |
assert ast_opt.children[0].data == 'expr_stmt' |
| 551 |
assert ast_opt.children[0].children[0] == expected |
| 552 |
|
| 553 |
@pytest.mark.parametrize(("op", "expected"), [ |
| 554 |
|
| 555 |
("+", "x"), |
| 556 |
("-", "x"), |
| 557 |
("|", "x"), |
| 558 |
("^", "x"), |
| 559 |
("<<", "x"), |
| 560 |
(">>", "x"), |
| 561 |
("||", "x"), |
| 562 |
|
| 563 |
("*", 0), |
| 564 |
("/", 0), |
| 565 |
("%", 0), |
| 566 |
("&", 0), |
| 567 |
("&&", 0), |
| 568 |
]) |
| 569 |
def test_fold_left_0(self, op, expected): |
| 570 |
self._do_fold_test(f"x {op} 0", expected) |
| 571 |
|
| 572 |
@pytest.mark.parametrize(("op", "expected"), [ |
| 573 |
|
| 574 |
("+", "x"), |
| 575 |
("-", "x"), |
| 576 |
("|", "x"), |
| 577 |
("^", "x"), |
| 578 |
("||", "x"), |
| 579 |
|
| 580 |
("*", 0), |
| 581 |
("/", 0), |
| 582 |
("%", 0), |
| 583 |
("&", 0), |
| 584 |
("<<", 0), |
| 585 |
(">>", 0), |
| 586 |
("&&", 0), |
| 587 |
]) |
| 588 |
def test_fold_right_0(self, op, expected): |
| 589 |
self._do_fold_test(f"0 {op} x", expected) |
| 590 |
|
| 591 |
@pytest.mark.parametrize(("op", "expected"), [ |
| 592 |
|
| 593 |
("*", "x"), |
| 594 |
("/", "x"), |
| 595 |
|
| 596 |
("||", 1), |
| 597 |
|
| 598 |
("%", 0), |
| 599 |
]) |
| 600 |
def test_fold_left_1(self, op, expected): |
| 601 |
self._do_fold_test(f"x {op} 1", expected) |
| 602 |
|
| 603 |
|
| 604 |
def test_unary_parens(self): |
| 605 |
self._do_fold_test(f"! (1)", 0) |
| 606 |
|
| 607 |
|
| 608 |
def test_unary_unfolded(self): |
| 609 |
ast_opt = self._get_folded_ast("! x") |
| 610 |
|
| 611 |
assert isinstance(ast_opt, LarkTree) |
| 612 |
assert ast_opt.data == 'start' |
| 613 |
assert ast_opt.children[0].data == 'expr_stmt' |
| 614 |
assert ast_opt.children[0].children[0].data == 'unary_expr' |
| 615 |
|
| 616 |
@pytest.mark.parametrize(("expr", "expected"), [ |
| 617 |
("-1", 0xffffffffffffffff), |
| 618 |
("-0", 0), |
| 619 |
]) |
| 620 |
def test_unary_fold(self, expr, expected): |
| 621 |
self._do_fold_test(expr, expected) |
| 622 |
|
| 623 |
@pytest.mark.parametrize(("expr", "expected"), [ |
| 624 |
("1 ? 10 : 20", 10), |
| 625 |
("0 ? 1 : 2", 2), |
| 626 |
("0 ? 1 : 2 + 2", 4), |
| 627 |
("1 + 1 ? 99 : 77", 99), |
| 628 |
]) |
| 629 |
def test_ternary_fold(self, expr, expected): |
| 630 |
self._do_fold_test(expr, expected) |
| 631 |
|
| 632 |
class TestSemanticChecker: |
| 633 |
|
| 634 |
@pytest.mark.parametrize("expr", [ |
| 635 |
"funkymonkey();", |
| 636 |
"valid_fn_1_arg(2134, x << 4);", |
| 637 |
"valid_fn_1_arg(\"a-string\");", |
| 638 |
"valid_fn_3_arg(1, 2);", |
| 639 |
"fn_with_str_parm(123);", |
| 640 |
"valid_fn_varg();", |
| 641 |
'"just a string in this expr";', |
| 642 |
"__var a = \"string value\";", |
| 643 |
"b = \"string value\";", |
| 644 |
"Sequence();", |
| 645 |
"Sequence(1000);", |
| 646 |
]) |
| 647 |
def test_sem_checker_raises(self, expr, context, scope): |
| 648 |
ast = Parser.parse(expr) |
| 649 |
logging.info("ast:\n%s", ast.pretty()) |
| 650 |
c = SemanticChecker(ast, scope, context) |
| 651 |
with pytest.raises(DebugSequenceSemanticError): |
| 652 |
c.check() |
| 653 |
|
| 654 |
@pytest.mark.parametrize("expr", [ |
| 655 |
"valid_fn_no_args();", |
| 656 |
"valid_fn_1_arg(2134);", |
| 657 |
"valid_fn_3_arg(1, 2, 3);", |
| 658 |
"fn_with_str_parm(\"bubblegum\");", |
| 659 |
"valid_fn_varg(a);", |
| 660 |
"valid_fn_varg(123, x, q + 1, \"hi there\", 99);", |
| 661 |
"__var a = 3 + x * 2;", |
| 662 |
"b = 3 + x * 2;", |
| 663 |
"Sequence(\"valid\");", |
| 664 |
]) |
| 665 |
def test_sem_checker_passes(self, expr, context, scope): |
| 666 |
ast = Parser.parse(expr) |
| 667 |
logging.info("ast:\n%s", ast.pretty()) |
| 668 |
c = SemanticChecker(ast, scope, context) |
| 669 |
c.check() |
| 670 |
|
| 671 |
class TestDebugSequences: |
| 672 |
def test_pname(self): |
| 673 |
seq = DebugSequence('test', pname="cm4") |
| 674 |
assert seq.name == 'test' |
| 675 |
assert seq.pname == 'cm4' |
| 676 |
|
| 677 |
assert DebugSequence('test').pname is None |
| 678 |
|
| 679 |
def test_info(self): |
| 680 |
assert DebugSequence('test').info == '' |
| 681 |
assert DebugSequence('test', info='hi there').info == 'hi there' |
| 682 |
|
| 683 |
def test_enable(self): |
| 684 |
assert DebugSequence('test').is_enabled |
| 685 |
assert not DebugSequence('test', is_enabled=False).is_enabled |
| 686 |
|
| 687 |
def test_scope_create(self, context): |
| 688 |
seq = DebugSequence('test') |
| 689 |
scope = seq._create_scope(context) |
| 690 |
assert scope.parent is context.delegate.get_root_scope(context) |
| 691 |
assert scope.get('__Result') == 0 |
| 692 |
assert not scope.is_read_only('_Result') |
| 693 |
assert scope.get('__dp') == 0 |
| 694 |
assert not scope.is_read_only('__dp') |
| 695 |
assert scope.get('__ap') == 0 |
| 696 |
assert not scope.is_read_only('__ap') |
| 697 |
assert scope.get('__apid') == 0 |
| 698 |
assert not scope.is_read_only('__apid') |
| 699 |
assert scope.get('__errorcontrol') == 0 |
| 700 |
assert not scope.is_read_only('__errorcontrol') |
| 701 |
assert scope.get('__protocol') == 2 |
| 702 |
assert scope.is_read_only('__protocol') |
| 703 |
assert scope.get('__connection') == 1 |
| 704 |
assert scope.is_read_only('__connection') |
| 705 |
assert scope.get('__traceout') == 0 |
| 706 |
assert scope.is_read_only('__traceout') |
| 707 |
assert scope.get('__FlashOp') == 0 |
| 708 |
assert scope.is_read_only('__FlashOp') |
| 709 |
assert scope.get('__FlashAddr') == 0 |
| 710 |
assert scope.is_read_only('__FlashAddr') |
| 711 |
assert scope.get('__FlashLen') == 0 |
| 712 |
assert scope.is_read_only('__FlashLen') |
| 713 |
assert scope.get('__FlashArg') == 0 |
| 714 |
assert scope.is_read_only('__FlashArg') |
| 715 |
|
| 716 |
def test_scope_create_with_calling_seq(self, block_context): |
| 717 |
|
| 718 |
super_scope = block_context.current_scope |
| 719 |
super_scope.set('__dp', 1) |
| 720 |
super_scope.set('__ap', 2) |
| 721 |
super_scope.set('__apid', 3) |
| 722 |
super_scope.set('__errorcontrol', 1) |
| 723 |
super_scope.set('__Result', 333) |
| 724 |
|
| 725 |
|
| 726 |
seq = DebugSequence('test') |
| 727 |
|
| 728 |
scope = seq._create_scope(block_context) |
| 729 |
|
| 730 |
|
| 731 |
assert scope.get('__dp') == 1 |
| 732 |
assert scope.get('__ap') == 2 |
| 733 |
assert scope.get('__apid') == 3 |
| 734 |
assert scope.get('__errorcontrol') == 1 |
| 735 |
|
| 736 |
assert scope.get('__Result') == 0 |
| 737 |
|
| 738 |
|
| 739 |
scope.set('__dp', 2) |
| 740 |
scope.set('__ap', 1) |
| 741 |
scope.set('__apid', 5) |
| 742 |
scope.set('__errorcontrol', 0) |
| 743 |
scope.set('__Result', 555) |
| 744 |
|
| 745 |
|
| 746 |
assert super_scope.get('__dp') == 1 |
| 747 |
assert super_scope.get('__ap') == 2 |
| 748 |
assert super_scope.get('__apid') == 3 |
| 749 |
assert super_scope.get('__errorcontrol') == 1 |
| 750 |
assert super_scope.get('__Result') == 333 |
| 751 |
|
| 752 |
def test_exec_block(self, context): |
| 753 |
seq = DebugSequence('test') |
| 754 |
seq.add_child(Block("__var x = 1 + 1;")) |
| 755 |
seq.execute(context) |
| 756 |
|
| 757 |
def test_exec_if(self, context): |
| 758 |
seq = DebugSequence('test') |
| 759 |
seq.add_child(Block("__var x = 1;")) |
| 760 |
seq.add_child(IfControl("x")) |
| 761 |
seq.execute(context) |
| 762 |
|
| 763 |
def test_exec_while(self, context): |
| 764 |
seq = DebugSequence('test') |
| 765 |
seq.add_child(Block("__var x = 0;")) |
| 766 |
w = WhileControl("x < 2") |
| 767 |
w.add_child(Block("x += 1;")) |
| 768 |
seq.add_child(w) |
| 769 |
seq.execute(context) |
| 770 |
|
| 771 |
def test_exec_while_with_assign(self, context: DebugSequenceExecutionContext): |
| 772 |
seq = DebugSequence('test') |
| 773 |
seq.add_child(Block("__var x = 0; __var y = 0;")) |
| 774 |
w = WhileControl("x < (y = 2)") |
| 775 |
w.add_child(Block("x += 1;")) |
| 776 |
seq.add_child(w) |
| 777 |
seq.execute(context) |