def test_case() -> None: assert_equal( Case(Variable("x"), [(Number(5), Number(1)), (Number(10), Number(2))]).z3expr(), z3.If( z3.Int("x") == z3.IntVal(5), z3.IntVal(1), z3.If(z3.Int("x") == z3.IntVal(10), z3.IntVal(2), z3.BoolVal(False)), ), )
def test_case_simplified(self) -> None: self.assertEqual( Case( Add(Number(21), Number(21)), [ (Variable("X"), Number(21)), (Variable("Y"), Add(Number(21), Number(21))), (Add(Number(21), Number(21)), Variable("Z")), ], ).simplified(), Case( Number(42), [ (Variable("X"), Number(21)), (Variable("Y"), Number(42)), (Number(42), Variable("Z")), ], ), )
def test_case_substituted() -> None: assert_equal( Case(Variable("X"), [(Variable("Y"), Variable("Z"))]).substituted( lambda x: Variable(f"P_{x}") if isinstance(x, Variable) else x ), Case(Variable("P_X"), [(Variable("P_Y"), Variable("P_Z"))]), ) assert_equal( Case(Variable("X"), [(Variable("Y"), Number(0))]).substituted( lambda x: Variable(f"P_{x}") if isinstance(x, Variable) else ( Case(x.control_expression, [*x.case_statements, (Variable("Z"), Number(1))]) if isinstance(x, Case) else x ) ), Case(Variable("P_X"), [(Variable("P_Y"), Number(0)), (Variable("P_Z"), Number(1))]), )
def test_case_variables(self) -> None: self.assertEqual( Case( Add(Number(21), Number(21)), [ (Variable("X"), Number(21)), (Variable("Y"), Add(Number(21), Number(21))), (Add(Number(21), Number(21)), Variable("Z")), ], ).variables(), [Variable("X"), Variable("Y"), Variable("Z")], )
def test_case_str() -> None: assert_equal( str( Case( Variable("X"), [ (Variable("Y"), Number(1)), (Variable("Z"), Number(1)), (Variable("others"), Number(2)), ], ) ), multilinestr( """(case X is when Y | Z => 1, when others => 2)""" ), )