Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_from_state_expr(self):
change = StateChange.from_expr("none")
self.assertEqual(change, StateChange())
change = StateChange.from_expr("m=0")
self.assertEqual(change, StateChange(m=0))
change = StateChange.from_expr("x=1")
self.assertEqual(change, StateChange(x=1))
change = StateChange.from_expr("m=0,x=1")
self.assertEqual(change, StateChange(m=0, x=1))
change = StateChange.from_expr("x=0,m=1")
self.assertEqual(change, StateChange(x=0, m=1))
def test_from_state_expr(self):
change = StateChange.from_expr("none")
self.assertEqual(change, StateChange())
change = StateChange.from_expr("m=0")
self.assertEqual(change, StateChange(m=0))
change = StateChange.from_expr("x=1")
self.assertEqual(change, StateChange(x=1))
change = StateChange.from_expr("m=0,x=1")
self.assertEqual(change, StateChange(m=0, x=1))
change = StateChange.from_expr("x=0,m=1")
self.assertEqual(change, StateChange(x=0, m=1))
def test_from_state_expr(self):
change = StateChange.from_expr("none")
self.assertEqual(change, StateChange())
change = StateChange.from_expr("m=0")
self.assertEqual(change, StateChange(m=0))
change = StateChange.from_expr("x=1")
self.assertEqual(change, StateChange(x=1))
change = StateChange.from_expr("m=0,x=1")
self.assertEqual(change, StateChange(m=0, x=1))
change = StateChange.from_expr("x=0,m=1")
self.assertEqual(change, StateChange(x=0, m=1))
def test_from_state_expr(self):
change = StateChange.from_expr("none")
self.assertEqual(change, StateChange())
change = StateChange.from_expr("m=0")
self.assertEqual(change, StateChange(m=0))
change = StateChange.from_expr("x=1")
self.assertEqual(change, StateChange(x=1))
change = StateChange.from_expr("m=0,x=1")
self.assertEqual(change, StateChange(m=0, x=1))
change = StateChange.from_expr("x=0,m=1")
self.assertEqual(change, StateChange(x=0, m=1))
def test_from_state_expr(self):
change = StateChange.from_expr("none")
self.assertEqual(change, StateChange())
change = StateChange.from_expr("m=0")
self.assertEqual(change, StateChange(m=0))
change = StateChange.from_expr("x=1")
self.assertEqual(change, StateChange(x=1))
change = StateChange.from_expr("m=0,x=1")
self.assertEqual(change, StateChange(m=0, x=1))
change = StateChange.from_expr("x=0,m=1")
self.assertEqual(change, StateChange(x=0, m=1))
elif orig.typ == T.PC:
pc = int(orig.val[1:], 16)
# Assertion type.
elif new.typ not in (T.SUGGESTED_ASSERTION, T.SUGGESTED_ASSERTION_TYPE):
if orig.typ in (T.ASSERTION_TYPE, T.SUGGESTED_ASSERTION_TYPE):
orig_assert_type = (
"none" if orig.typ == T.SUGGESTED_ASSERTION_TYPE else orig.val
)
new_assert_type = new.val
assertion_type_changed = orig_assert_type != new_assert_type
# Assertion.
elif orig.typ in (T.ASSERTION, T.SUGGESTED_ASSERTION):
assertion_changed = orig.val != new.val
anything_changed = assertion_type_changed or assertion_changed
state_change = StateChange.from_expr(new.val)
if anything_changed and state_change.unknown:
raise ParserError("Invalid assertion state.", line_n)
if assertion_type_changed:
if "instruction".startswith(orig_assert_type):
self.log.deassert_instruction_state_change(pc)
elif "subroutine".startswith(orig_assert_type):
self.log.deassert_subroutine_state_change(
self.subroutine.pc, pc
)
if anything_changed:
if new_assert_type == "":
continue
elif "instruction".startswith(new_assert_type):
self.log.assert_instruction_state_change(pc, state_change)
elif "subroutine".startswith(new_assert_type):
self.log.assert_subroutine_state_change(
def do_assert_instruction(self, label_or_pc: str, state_expr: str) -> None:
"""Define how the processor state changes after an instruction's execution.
STATE_EXPR can accept the following values:
- "none" -> The subroutine does not change the state.
- "m=0" or "m=1" -> The subroutine changes the state of m to 0 or 1.
- "x=0" or "x=1" -> The subroutine changes the state of x to 0 or 1.
- "m=0,x=0" -> The subroutine changes the state of m to 0 and x to 0.
- "m=0,x=1" -> The subroutine changes the state of m to 0 and x to 1.
- "m=1,x=0" -> The subroutine changes the state of m to 1 and x to 0.
- "m=1,x=1" -> The subroutine changes the state of m to 1 and x to 1."""
state_change = StateChange.from_expr(state_expr)
instruction_pc = self._label_to_pc(label_or_pc)
self.log.assert_instruction_state_change(instruction_pc, state_change)
def do_assert_subroutine(self, label_or_pc: str, state_expr: str) -> None:
"""Define a known processor return state for a given subroutine.
STATE_EXPR can accept the following values:
- "none" -> The subroutine does not change the state.
- "m=0" or "m=1" -> The subroutine changes the state of m to 0 or 1.
- "x=0" or "x=1" -> The subroutine changes the state of x to 0 or 1.
- "m=0,x=0" -> The subroutine changes the state of m to 0 and x to 0.
- "m=0,x=1" -> The subroutine changes the state of m to 0 and x to 1.
- "m=1,x=0" -> The subroutine changes the state of m to 1 and x to 0.
- "m=1,x=1" -> The subroutine changes the state of m to 1 and x to 1."""
if not self.subroutine:
raise GilgameshError("No selected subroutine.")
# TODO: check that pc is an instruction inside the subroutine.
instr_pc = self._label_to_pc(label_or_pc)
state_change = StateChange.from_expr(state_expr)
self.log.assert_subroutine_state_change(self.subroutine, instr_pc, state_change)