def test_01_dwf(self): # Ad-hoc method to handle printing of the new node def hrprinter_walk_XOR(self, formula): self.stream.write("(") yield formula.arg(0) self.stream.write(" *+* ") yield formula.arg(1) self.stream.write(")") # Shortcuts for function in env add_dwf = get_env().add_dynamic_walker_function create_node = get_env().formula_manager.create_node # Define the new node type and register the walkers in the env XOR = new_node_type() add_dwf(XOR, SimpleTypeChecker, SimpleTypeChecker.walk_bool_to_bool) add_dwf(XOR, HRPrinter, hrprinter_walk_XOR) # Create a test node (This implicitely calls the Type-checker) x = Symbol("x") f1 = create_node(node_type=XOR, args=(x,x)) self.assertIsNotNone(f1) # String conversion should use the function defined above. s_f1 = str(f1) self.assertEqual(s_f1, "(x *+* x)") # We did not define an implementation for the Simplifier with self.assertRaises(UnsupportedOperatorError): f1.simplify()
def test_infix(self): x, y, p = self.x, self.y, self.p with self.assertRaises(Exception): x.Implies(y) get_env().enable_infix_notation = True self.assertEqual(Implies(x,y), x.Implies(y)) self.assertEqual(p + p, Plus(p,p)) self.assertEqual(p > p, GT(p,p)) get_env().enable_infix_notation = False
def test_conversion_error(self): from pysmt.type_checker import SimpleTypeChecker add_dwf = get_env().add_dynamic_walker_function create_node = get_env().formula_manager.create_node # Create a node that is not supported by any solver idx = op.new_node_type() x = Symbol("x") add_dwf(idx, SimpleTypeChecker, SimpleTypeChecker.walk_bool_to_bool) invalid_node = create_node(idx, args=(x,x)) for sname in get_env().factory.all_solvers(logic=QF_BOOL): with self.assertRaises(ConvertExpressionError): is_sat(invalid_node, solver_name=sname, logic=QF_BOOL)
def test_shortcut_is_using_global_env(self): global_mgr = get_env().formula_manager a1 = Symbol("z", BOOL) a2 = global_mgr.Symbol("z", BOOL) self.assertEqual(a1, a2, "Symbols generated by env and Symbol are not the same")
def test_determinism(self): def get_set(env): mgr = env.formula_manager r = set(mgr.Symbol("x%d" % i) for i in xrange(1000)) for (f, _, _, _) in get_example_formulae(env): r |= set([f]) return r # As first thing on the environment we build the set of formulae l1 = list(get_set(get_env())) # We try this ten times... for _ in xrange(10): # Do something to screw up memory layout... for y in (Symbol("y%d" % i) for i in xrange(1000)): self.assertIsNotNone(y) with Environment() as new_env: # As first thing on the environment we build the set of formulae l_test = list(get_set(new_env)) # The ordering of the sets should be the same... for i,f in enumerate(l1): nf = new_env.formula_manager.normalize(f) self.assertEquals(nf, l_test[i])
def test_multiple_exit(self): for sname in get_env().factory.all_solvers(): # Multiple exits should be ignored s = Solver(name=sname) s.exit() s.exit() self.assertTrue(True)
def test_substituter_conditions(self): x = Symbol("x") y = Symbol("y") and_x_x = And(x, x) ftype = FunctionType(BOOL, [BOOL]) f = Symbol("f", ftype) # 1. All arguments must be terms args_good = {x:y} args_bad = {x:f} substitute(and_x_x, args_good) with self.assertRaisesRegex(TypeError, " substitutions"): substitute(and_x_x, args_bad) # 2. All arguments belong to the manager of the substituter. new_mgr = FormulaManager(get_env()) new_x = new_mgr.Symbol("x") self.assertNotEqual(x, new_x) args_1 = {x: new_x} args_2 = {new_x: new_x} with self.assertRaisesRegex(TypeError, "Formula Manager" ): substitute(and_x_x, args_1) with self.assertRaisesRegex(TypeError, "Formula Manager."): substitute(and_x_x, args_2) with self.assertRaisesRegex(TypeError, "substitute()"): substitute(f, {x:x})
def test_get_value_of_function_bool(self): """Proper handling of models with functions with bool args.""" hr = Symbol("hr", FunctionType(REAL, [BOOL, REAL, REAL])) hb = Symbol("hb", FunctionType(BOOL, [BOOL, REAL, REAL])) hr_0_1 = Function(hr, (TRUE(), Real(0), Real(1))) hb_0_1 = Function(hb, (TRUE(), Real(0), Real(1))) hbx = Function(hb, (Symbol("x"), Real(0), Real(1))) f = GT(hr_0_1, Real(0)) g = hb_0_1 for sname in get_env().factory.all_solvers(logic=QF_UFLIRA): with Solver(name=sname) as solver: # First hr solver.add_assertion(f) res = solver.solve() self.assertTrue(res) v = solver.get_value(hr_0_1) self.assertIsNotNone(solver.get_value(v)) # Now hb solver.add_assertion(g) res = solver.solve() self.assertTrue(res) v = solver.get_value(hb_0_1) self.assertIsNotNone(v in [TRUE(), FALSE()]) # Hbx solver.add_assertion(hbx) res = solver.solve() self.assertTrue(res) v = solver.get_value(hbx) self.assertIsNotNone(v in [TRUE(), FALSE()]) # Get model model = solver.get_model() self.assertIsNotNone(model)
def test_msat_converter_on_msat_error(self): import mathsat import _mathsat from pysmt.solvers.msat import MathSAT5Solver, MSatConverter env = get_env() msat = MathSAT5Solver(env, logic=QF_UFLIRA) new_converter = MSatConverter(env, msat.msat_env) def walk_plus(formula, args): res = mathsat.MSAT_MAKE_ERROR_TERM() return res # Replace the function used to compute the Plus() # with one that returns a msat_error new_converter.set_function(walk_plus, op.PLUS) r, s = FreshSymbol(REAL), FreshSymbol(REAL) f1 = GT(r, s) f2 = Plus(r, s) t1 = new_converter.convert(f1) self.assertFalse(mathsat.MSAT_ERROR_TERM(t1)) with self.assertRaises(InternalSolverError): new_converter.convert(f2)
def test_atoms_oracle(self): oracle = get_env().ao stc = get_env().stc for (f, _, _, _) in get_example_formulae(): atoms = oracle.get_atoms(f) if ( atoms is not None): if len(f.get_free_variables()) > 0: self.assertTrue(len(atoms) > 0) for a in atoms: ty = stc.get_type(a) self.assertEqual(ty, BOOL) self.assertFalse(a.is_and()) self.assertFalse(a.is_or()) self.assertFalse(a.is_not()) self.assertFalse(a.is_iff()) self.assertFalse(a.is_quantifier())
def test_incremental(self): a = Symbol('a', BOOL) b = Symbol('b', BOOL) c = Symbol('c', BOOL) for name in get_env().factory.all_solvers(logic=QF_BOOL): with Solver(name) as solver: solver.add_assertion(Or(a, b)) solver.add_assertion(Or(Not(b), c)) self.assertTrue(solver.solve()) try: solver.push(1) except NotImplementedError: # if push not implemented, pop shouldn't be either self.assertRaises(NotImplementedError, solver.pop) continue solver.add_assertion(And(Not(a), Not(c))) self.assertFalse(solver.solve()) solver.pop(1) self.assertTrue(solver.solve()) solver.add_assertion(FALSE()) self.assertFalse(solver.solve()) solver.reset_assertions() solver.add_assertion(a) self.assertTrue(solver.solve())
def test_types_oracle(self): get_env().enable_infix_notation = True S = Type("S") U = Type("U", 1) B = Type("B", 2) csort = B(U(S), B(BOOL, S)) v = Symbol("v", csort) types_all = self.env.typeso.get_types(v) types_custom = self.env.typeso.get_types(v, custom_only=True) self.assertIsNotNone(types_all) # Only BOOL does not appear in types_custom self.assertTrue(len(types_all) == 5) self.assertTrue(len(types_custom) == 4) # Types are in partial order: simpler is earlier idx_S = types_custom.index(S) idx_US = types_custom.index(U(S)) idx_BBS = types_custom.index(B(BOOL, S)) idx_BUSBBS = types_custom.index(B(U(S), B(BOOL, S))) self.assertIsNotNone(idx_S) self.assertIsNotNone(idx_US) self.assertIsNotNone(idx_BBS) self.assertIsNotNone(idx_BUSBBS) self.assertEqual(types_custom[0], S) self.assertTrue(idx_S < idx_US) self.assertTrue(idx_US < idx_BUSBBS) self.assertTrue(idx_BBS < idx_BUSBBS)
def test_types_oracle_examples(self): oracle = get_env().typeso for (f, _, _, _) in get_example_formulae(): types_all = oracle.get_types(f) types_custom = oracle.get_types(f, custom_only=True) # Custom types are a subset of all types s1 = set(types_all) s2 = set(types_custom) self.assertTrue(s1.issuperset(s2)) # Compare logics with types theory = self.env.theoryo.get_theory(f) if len(f.get_free_variables()) == 0: continue if theory.arrays: self.assertTrue(any(x.is_array_type() for x in types_all), (f, types_all)) if theory.bit_vectors: self.assertTrue(any(x.is_bv_type() for x in types_all), (f, types_all)) if theory.integer_arithmetic: self.assertTrue(any(x.is_int_type() for x in types_all), (f, types_all)) if theory.real_arithmetic: self.assertTrue(any(x.is_real_type() for x in types_all), (f, types_all))
def test_solving_under_assumption_theory(self): x = Symbol("x", REAL) y = Symbol("y", REAL) v1 = GT(x, Real(10)) v2 = LE(y, Real(2)) xor = Or(And(v1, Not(v2)), And(Not(v1), v2)) for name in get_env().factory.all_solvers(logic=QF_LRA): with Solver(name=name) as solver: solver.add_assertion(xor) res1 = solver.solve(assumptions=[v1, Not(v2)]) model1 = solver.get_model() res2 = solver.solve(assumptions=[Not(v1), v2]) model2 = solver.get_model() res3 = solver.solve(assumptions=[v1, v2]) self.assertTrue(res1) self.assertTrue(res2) self.assertFalse(res3) self.assertEqual(model1.get_value(v1), TRUE()) self.assertEqual(model1.get_value(v2), FALSE()) self.assertEqual(model2.get_value(v1), FALSE()) self.assertEqual(model2.get_value(v2), TRUE())
def test_examples_by_logic(self): for (f, validity, satisfiability, logic) in get_example_formulae(): if len(get_env().factory.all_solvers(logic=logic)) > 0: v = is_valid(f, logic=logic) s = is_sat(f, logic=logic) self.assertEqual(validity, v, f.serialize()) self.assertEqual(satisfiability, s, f.serialize())
def test_atoms_oracle(self): oracle = get_env().ao stc = get_env().stc for (f, _, _, _) in EXAMPLE_FORMULAS: atoms = oracle.get_atoms(f) if len(f.get_free_variables()) > 0: self.assertTrue(len(atoms) > 0) for a in atoms: ty = stc.get_type(a) self.assertEqual(ty, BOOL) self.assertFalse(a.is_and()) self.assertFalse(a.is_or()) self.assertFalse(a.is_not()) self.assertFalse(a.is_iff()) self.assertFalse(a.is_quantifier())
def test_initial_ordering(self): from pysmt.solvers.bdd import BddSolver with BddSolver(get_env(), pysmt.logics.BOOL, {"static_ordering" : [self.x, self.y], "dynamic_reordering" : True}) as s: s.add_assertion(self.big_tree) self.assertTrue(s.solve()) self.assertNotEquals(s.ddmanager.ReorderingStatus()[1], 0)
def test_options_random_seed(self): for sname in get_env().factory.all_solvers(logic=QF_BOOL): if sname in ["btor", "bdd"]: with self.assertRaises(PysmtValueError): Solver(name=sname, random_seed=42) else: s = Solver(name=sname, random_seed=42) self.assertIsNotNone(s)
def test_conj_partitioning(self): for (f, _, _, logic) in get_example_formulae(): if get_env().factory.has_solvers(logic=logic): conjuncts = list(conjunctive_partition(f)) try: ok = is_valid(Iff(f, And(conjuncts)), logic=logic) except SolverReturnedUnknownResultError: ok = not logic.quantifier_free self.assertTrue(ok)
def test_aig_examples(self): for (f, _, _, logic) in get_example_formulae(): if get_env().factory.has_solvers(logic=logic): f_aig = aig(f) try: ok = is_valid(Iff(f, f_aig), logic=logic) except SolverReturnedUnknownResultError: ok = not logic.quantifier_free self.assertTrue(ok, "Was: %s\n Got:%s" % (f, f_aig))
def test_examples_get_implicant(self): for (f, _, satisfiability, logic) in get_example_formulae(): if logic.quantifier_free: for sname in get_env().factory.all_solvers(logic=logic): f_i = get_implicant(f, logic=logic, solver_name=sname) if satisfiability: self.assertValid(Implies(f_i, f), logic=logic, msg=(f_i, f)) else: self.assertIsNone(f_i)
def test_get_implicant_sat(self): varA = Symbol("A", BOOL) varX = Symbol("X", REAL) f = And(varA, Equals(varX, Real(8))) for solver in get_env().factory.all_solvers(logic=QF_LRA): res = get_implicant(f, solver_name=solver) self.assertIsNotNone(res, "Formula was expected to be SAT") self.assertValid(Implies(res, f), logic=QF_LRA)
def test_reordering_algorithms(self): from pysmt.solvers.bdd import BddSolver, BddOptions for algo in BddOptions.CUDD_ALL_REORDERING_ALGORITHMS: with BddSolver(get_env(), pysmt.logics.BOOL, {"dynamic_reordering" : True, "reordering_algorithm" : algo}) as s: s.add_assertion(self.big_tree) self.assertTrue(s.solve()) self.assertEquals(algo, s.ddmanager.ReorderingStatus()[1])
def test_redefinition(self): env = get_env() env.factory.add_generic_solver("test__redefinition", ["/tmp/nonexistent"], [QF_UFLIRA]) with self.assertRaises(SolverRedefinitionError): env.factory.add_generic_solver("test__redefinition", ["/tmp/nonexistent"], [QF_UFLIRA])
def test_nnf_examples(self): for (f, _, _, logic) in get_example_formulae(): if get_env().factory.has_solvers(logic=logic): rf = nnf(f) try: ok = is_valid(Iff(f, rf), logic=logic) except SolverReturnedUnknownResultError: ok = not logic.quantifier_free self.assertTrue(ok)
def test_simplifying_int_plus_changes_type_of_expression(self): varA = Symbol("At", INT) varB = Symbol("Bt", INT) get_type = get_env().stc.get_type f = Plus(varB, Int(1)) old_type = get_type(f) f = f.simplify() new_type = get_type(f) self.assertEqual(new_type, old_type)
def test_default_logic_in_is_sat(self): factory = get_env().factory factory.default_logic = QF_BOOL self.assertEqual(factory.default_logic, QF_BOOL) varA = Symbol("A", BOOL) varB = Symbol("B", BOOL) f = And(varA, Not(varB)) self.assertSat(f)
def test_add_assertion(self): r = FreshSymbol(REAL) f1 = Plus(r, r) f2 = GT(r, r) for sname in get_env().factory.all_solvers(logic=QF_LRA): with Solver(name=sname) as solver: with self.assertRaises(PysmtTypeError): solver.add_assertion(f1) self.assertIsNone(solver.add_assertion(f2))
def __call__(self, test_fun): msg = "Solver for %s not available" % self.logic cond = len(get_env().factory.all_solvers(logic=self.logic)) == 0 @unittest.skipIf(cond, msg) @wraps(test_fun) def wrapper(*args, **kwargs): return test_fun(*args, **kwargs) return wrapper
def test_get_implicant_unsat(self): varA = Symbol("A", BOOL) varB = Symbol("B", BOOL) f = And(varA, Not(varB)) g = f.substitute({varB:varA}) for solver in get_env().factory.all_solvers(logic=QF_BOOL): res = get_implicant(g, solver_name=solver) self.assertIsNone(res, "Formula was expected to be UNSAT")
def __call__(self, test_fun): msg = "%s not available" % self.solver cond = self.solver not in get_env().factory.all_solvers() @unittest.skipIf(cond, msg) @wraps(test_fun) def wrapper(*args, **kwargs): return test_fun(*args, **kwargs) return wrapper
def test_default_logic_in_is_sat(self): factory = get_env().factory factory.default_logic = QF_BOOL self.assertEquals(factory.default_logic, QF_BOOL) varA = Symbol("A", BOOL) varB = Symbol("B", BOOL) f = And(varA, Not(varB)) self.assertSat(f)
def test_boolean(self): varA = Symbol("At", INT) varB = Symbol("Bt", INT) f = And(LT(varA, Plus(varB, Int(1))), GT(varA, Minus(varB, Int(1)))) g = Equals(varA, varB) h = Iff(f, g) tc = get_env().stc res = tc.walk(h) self.assertEqual(res, BOOL)
def __init__(self, environment=None): self.pysmt_env = get_env() if environment is None else environment # Placeholders for fields filled by self._reset self._current_env = None self.cache = None self.logic = None # Special tokens appearing in expressions self.parentheses = set(["(", ")"]) self.specials = set(["let", "!", "exists", "forall"])
def test_add_assertions(self): varA = Symbol("A", BOOL) varB = Symbol("B", BOOL) varC = Symbol("C", BOOL) assertions = [varA, Implies(varA, varB), Implies(varB, varC)] for name in get_env().factory.all_solvers(logic=QF_BOOL): with Solver(name) as solver: solver.add_assertions(assertions) solver.solve() self.assertTrue(solver.get_py_value(And(assertions)))
def test_parser_params(self): txt = """ (define-fun x ((y Int)) Bool (> y 0)) (declare-fun z () Int) (declare-fun y () Bool) (assert (and y (x z))) """ parser = SmtLibParser() script = parser.get_script(StringIO(txt)) self.assertEqual(len(get_env().formula_manager.get_all_symbols()), len(script.get_declared_symbols()) + len(script.get_define_fun_parameter_symbols()))
def __init__(self, doc: SmtLibDocument): self.doc = doc sym = get_env().formula_manager.get_symbol #fsym = lambda ty: FreshSymbol(ty, template='?c%d') nat = Type('Nat') skf1 = FreshSymbol(FunctionType(nat, [nat]), template='skf%d') self.ctor_pats = { nat: lambda ph: [sym('zero'), Function(sym('succ'), [Function(skf1, [ph])])] }
def skipIfNoSolverAvailable(test_fun): """Skip the test if no solver is available.""" msg = "No solver available" cond = len(get_env().factory.all_solvers()) == 0 @unittest.skipIf(cond, msg) @wraps(test_fun) def wrapper(self, *args, **kwargs): return test_fun(self, *args, **kwargs) return wrapper
def __init__(self, s: typing.TextIO): sym = get_env().formula_manager.get_or_create_symbol Nat, list_, Token = Type('Nat'), Type('list'), Type('Token') p = SmtLibDocumentParser() p.predeclare(sym('is-cons', FunctionType(BOOL, [list_]))) # p.predeclare(sym('is-succ', FunctionType(BOOL, [Nat]))) # hard-coded :/ p.predeclare(sym('is-ESC', FunctionType(BOOL, [Token]))) # self.script = p.get_script(s) self._fresh_cnt = itertools.count()
def test_formula_in_formula_manager(self): x = self.mgr.FreshSymbol() and_x_x = self.mgr.And(x, x) new_mgr = FormulaManager(get_env()) y = new_mgr.FreshSymbol() and_y_y = new_mgr.And(y, y) self.assertTrue(x in self.mgr) self.assertFalse(y in self.mgr) self.assertTrue(and_x_x in self.mgr) self.assertFalse(and_y_y in self.mgr)
def test_msat_back_formulae(self): from pysmt.solvers.msat import MathSAT5Solver, MSatConverter env = get_env() msat = MathSAT5Solver(environment=env, logic=QF_UFLIRA) new_converter = MSatConverter(env, msat.msat_env) for formula, _, _, logic in get_example_formulae(): if logic.quantifier_free: term = new_converter.convert(formula) res = new_converter.back(term) self.assertTrue(is_valid(Iff(formula, res), logic=QF_UFLIRA))
def test_int_promotion_define_fun(self): script = """ (define-fun x () Int 8) (define-fun y () Real 8) """ p = SmtLibParser() buffer = StringIO(script) s = p.get_script(buffer) get_type = get_env().stc.get_type for cmd in s: self.assertEqual(cmd.args[2], get_type(cmd.args[3]))
def setUp(self): TestCase.setUp(self) self.all_solvers = [] env = get_env() for _, _, fnames in os.walk(BASE_DIR): for f in fnames: if f.endswith(".solver.sh"): name = os.path.basename(f) path = os.path.join(BASE_DIR, "bin/" + f) env.factory.add_generic_solver(name, [path], [QF_UFLIRA]) self.all_solvers.append(f)
def test_is_sat(self): varA = Symbol("A", BOOL) varB = Symbol("B", BOOL) f = And(varA, Not(varB)) g = f.substitute({varB:varA}) self.assertUnsat(g, logic=QF_BOOL, msg="Formula was expected to be UNSAT") for solver in get_env().factory.all_solvers(): self.assertUnsat(g, solver_name=solver, msg="Formula was expected to be UNSAT")
def __init__(self, env=None, conf): if env is None: self.env = get_env() else: self.env = env IdentityDagWalker.__init__(self, env=self.env) self.mgr = self.env.formula_manager self.conf = conf self.symbol_map = dict() self.fbvand = self.mgr.Symbol( "fbvand", FunctionType(INT, (INT, INT, INT))) self.extra_constraints = set() self.zero = self.mgr.Int(0)
def test_get_solver_by_logic(self): if len(get_env().factory.all_solvers()) > 0: s = Solver(logic=pysmt.logics.QF_BOOL) self.assertIsNotNone(s) else: with self.assertRaises(NoSolverAvailableError): Solver(logic=pysmt.logics.QF_BOOL) with self.assertRaises(NoSolverAvailableError): Solver(logic=NRA) with self.assertRaises(NoSolverAvailableError): Solver(name='picosat', logic=pysmt.logics.QF_BV)
def __init__(self, characteristic_functional : CharacteristicFunctional, incremental_bmc, upper_bound_expectation, simplify_formulae, ert): self._characteristic_functional = characteristic_functional self._incremental_bmc = incremental_bmc self._bmc_formula_generator = incremental_bmc.get_formula_generator() self._upper_bound_dnf = self._characteristic_functional.get_upper_bound_expectation_dnf(upper_bound_expectation, ignore_conjuncts_with_infinity=False) self._simplify_formulae = simplify_formulae self._ert = ert # For the query, we can disregard arithmetic expressions that equal infinity since nothing is greater than infinity. self._upper_bound_dnf_for_k_inductive_query = self._characteristic_functional.get_upper_bound_expectation_dnf(upper_bound_expectation, ignore_conjuncts_with_infinity=True) self._euf_type = self._bmc_formula_generator.get_euf_type() # We have an uninterpreted function K_i for P_i (i=2,3,...) starting with K_2 self._eufs = [Symbol("K_1", FunctionType(*self._euf_type))] self._euf_substituter = EUFMGSubstituter(get_env()) self._simplifier = Simplifier(get_env()) self._prepare_first_formulae() self._unrolling_depth = 1
def test_nary_operators_in_solver_converter(self): """Conversion of n-ary operators was not handled correctly by converters.""" x = Symbol("x") r = Symbol("p", REAL) f_and_one = And(x) f_or_one = Or(x) f_plus_one = LT(Plus(r), Real(0)) ten_x = [x,x,x,x,x,x,x,x,x,x] f_and_many = And(ten_x) f_or_many = Or(ten_x) f_plus_many = LT(Plus(r,r,r,r,r,r,r,r,r,r,r), Real(0)) for name in get_env().factory.all_solvers(logic=logics.QF_BOOL): self.assertSat(f_and_one, solver_name=name) self.assertSat(f_or_one, solver_name=name) self.assertSat(f_and_many, solver_name=name) self.assertSat(f_or_many, solver_name=name) for name in get_env().factory.all_solvers(logic=logics.QF_UFLIRA): self.assertSat(f_plus_one, solver_name=name) self.assertSat(f_plus_many, solver_name=name)
def test_infix_extended(self): p, r, x, y = self.p, self.r, self.x, self.y get_env().enable_infix_notation = True self.assertEqual(Plus(p, Int(1)), p + 1) self.assertEqual(Plus(r, Real(1)), r + 1) self.assertEqual(Times(r, Real(1)), r * 1) self.assertEqual(Minus(p, Int(1)), p - 1) self.assertEqual(Minus(r, Real(1)), r - 1) self.assertEqual(Times(r, Real(1)), r * 1) self.assertEqual(Plus(r, Real(1.5)), r + 1.5) self.assertEqual(Minus(r, Real(1.5)), r - 1.5) self.assertEqual(Times(r, Real(1.5)), r * 1.5) self.assertEqual(Plus(r, Real(1.5)), 1.5 + r) self.assertEqual(Times(r, Real(1.5)), 1.5 * r) with self.assertRaises(TypeError): foo = p + 1.5 self.assertEqual(Not(x), ~x) self.assertEqual(Times(r, Real(-1)), -r) self.assertEqual(Times(p, Int(-1)), -p) self.assertEqual(Xor(x, y), x ^ y) self.assertEqual(And(x, y), x & y) self.assertEqual(Or(x, y), x | y) self.assertEqual(Or(x, TRUE()), x | True) self.assertEqual(Or(x, TRUE()), True | x) self.assertEqual(And(x, TRUE()), x & True) self.assertEqual(And(x, TRUE()), True & x) get_env().enable_infix_notation = False
def test_msat_back_not_identical(self): from pysmt.solvers.msat import MathSAT5Solver, MSatConverter env = get_env() msat = MathSAT5Solver(environment=env, logic=QF_UFLIRA) new_converter = MSatConverter(env, msat.msat_env) r, s = FreshSymbol(REAL), FreshSymbol(REAL) # r + 1 > s + 1 f = GT(Plus(r, Real(1)), Plus(s, Real(1))) term = new_converter.convert(f) res = new_converter.back(term) self.assertFalse(f == res)
def test_get_value_of_function(self): """get_value on a function should raise an exception.""" h = Symbol("h", FunctionType(REAL, [REAL, REAL])) h_0_0 = Function(h, (Real(0), Real(1))) f = GT(h_0_0, Real(0)) for sname in get_env().factory.all_solvers(logic=QF_UFLIRA): with Solver(name=sname) as solver: solver.add_assertion(f) res = solver.solve() self.assertTrue(res) with self.assertRaises(PysmtTypeError): solver.get_value(h) self.assertIsNotNone(solver.get_value(h_0_0))
def test_examples_by_logic(self): for (f, validity, satisfiability, logic) in get_example_formulae(): if len(get_env().factory.all_solvers(logic=logic)) > 0: try: v = is_valid(f, logic=logic) s = is_sat(f, logic=logic) self.assertEqual(validity, v, f.serialize()) self.assertEqual(satisfiability, s, f.serialize()) except SolverReturnedUnknownResultError: s = Solver(logic=logic) print(s, logic, f.serialize()) self.assertFalse(logic.quantifier_free, "Unkown result are accepted only on "\ "Quantified formulae")
def __init__(self, comment=""): self.vars = set([]) self.state_vars = set([]) self.input_vars = set([]) self.output_vars = set([]) self.hidden_vars = set([]) self.init = TRUE() self.trans = TRUE() self.invar = TRUE() self.ftrans = None self.comment = comment self.logic = L_BV self._pysmt_formula_mngr = get_env().formula_manager
def __init__(self, env=None, helper=None): if env is None: self.env = get_env() self.helper = Helper(self.env) else: self.env = env self.helper = helper assert (self.helper.env == env) # internal representation of the transition system self.state_vars = set() self.var_types = {} self.init = TRUE_PYSMT() self.trans = TRUE_PYSMT() self.final = FALSE_PYSMT()
def test_get_model_sat(self): varA = Symbol("A", BOOL) varX = Symbol("X", REAL) f = And(varA, Equals(varX, Real(8))) res = get_model(f, logic=QF_LRA) self.assertIsNotNone(res, "Formula was expected to be SAT") self.assertTrue(res.get_value(varA) == TRUE()) self.assertTrue(res.get_value(varX) == Real(8)) for solver in get_env().factory.all_solvers(logic=QF_LRA): res = get_model(f, solver_name=solver) self.assertIsNotNone(res, "Formula was expected to be SAT") self.assertTrue(res.get_value(varA) == TRUE()) self.assertTrue(res.get_value(varX) == Real(8))