def test_contains(self): self.assertTrue( PyVar('x') in PyEnvironment([PyVar('x')], [PyVar('y')])) self.assertFalse(PyVar('y') in PyEnvironment([PyVar('x')])) self.assertFalse( PyVar('x') not in PyEnvironment([PyVar('x')], [PyVar('y')])) self.assertTrue(PyVar('y') not in PyEnvironment([PyVar('x')]))
def test_init(self): self.assertEqual( str(PyEnvironment([PyVar('x')], [PyVar('y'), PyVar('z')])), '{x|y,z}') self.assertEqual(str(PyEnvironment([], [PyVar('y'), PyVar('z')])), '{|y,z}') self.assertEqual( str(PyEnvironment(real_vars=[PyVar('y'), PyVar('z')])), '{|y,z}') self.assertEqual(str(PyEnvironment([PyVar('x')], [])), '{x|}') self.assertEqual(str(PyEnvironment([PyVar('x')])), '{x|}') self.assertRaises(ValueError, PyEnvironment, [PyVar('x')], [PyVar('x'), PyVar('y')])
def test_bound_variable(self): e = PyEnvironment([PyVar('x0'), PyVar('y')], [PyVar('z')]) man: PyManager = PyBoxMPQManager() variables = [PyVar('x0'), PyVar('y')] intervals = [PyMPQInterval(-3, 2), PyMPQInterval(-2, 2, 1, 1)] b = PyBox(man, e, variables=variables, intervals=intervals) self.assertEqual(str(b.bound_variable(PyVar('y'))), '[-2,2]')
def __init__(self, manager: PyManager, variables: Set[VariableIdentifier], precursory: State = None): super().__init__(precursory=precursory) r_vars = list() for variable in variables: r_vars.append(PyVar(variable.name)) self.environment = PyEnvironment([], r_vars) self.polka = PyPolka(manager, self.environment)
def test_deepcopy(self): e = PyEnvironment([PyVar('x0'), PyVar('y')], [PyVar('z')]) x0 = PyLinexpr1(e) x1 = deepcopy(x0) x2 = x0 self.assertNotEqual(id(x0), id(x1)) self.assertEqual(id(x0), id(x2))
def test_forget(self): e = PyEnvironment([PyVar('x0'), PyVar('y')], [PyVar('z')]) man: PyManager = PyBoxMPQManager() variables = [PyVar('x0'), PyVar('y')] intervals = [PyMPQInterval(-3, 2), PyMPQInterval(-2, 2, 1, 1)] b = PyBox(man, e, variables=variables, intervals=intervals) self.assertEqual(str(b.forget([PyVar('y')])), '1·x0 + 3 >= 0 ∧ -1·x0 + 2 >= 0')
def test_make(self): e = PyEnvironment([PyVar('x0'), PyVar('y')], [PyVar('z')]) x = PyLinexpr1(e) x.set_coeff(PyVar('x0'), PyDoubleScalarCoeff(3)) x.set_coeff(PyVar('z'), PyDoubleScalarCoeff(-9)) x.set_cst(PyDoubleScalarCoeff(8)) c = PyTcons1.make(PyTexpr1(x), ConsTyp.AP_CONS_SUPEQ) self.assertEqual(str(c), '8.0 + 3.0 · x0 - 9.0 · z >= 0')
def substitute(self, var: PyVar, dst: PyTexpr1): self.tcons1.env.contents.count += 1 dim = libapron.ap_environment_dim_of_var( PyEnvironment(self.tcons1.env), var) texpr0 = self.tcons1.tcons0.texpr0 libapron.ap_texpr0_substitute_with(texpr0, dim, dst.texpr1.contents.texpr0) return self
def test_is_unsat(self): e = PyEnvironment([PyVar('x0'), PyVar('y')], [PyVar('z')]) x = PyLinexpr1(e) x.set_coeff(PyVar('x0'), PyDoubleScalarCoeff(3)) x.set_coeff(PyVar('z'), PyDoubleScalarCoeff(-9)) x.set_cst(PyDoubleScalarCoeff(8)) c = PyLincons1(ConsTyp.AP_CONS_SUPEQ, x) self.assertFalse(c.is_unsat()) self.assertTrue(PyLincons1.unsat(e).is_unsat()) self.assertTrue(PyLincons1(ConsTyp.AP_CONS_DISEQ, PyLinexpr1(e)).is_unsat())
def test_init(self): e = PyEnvironment([PyVar('x0'), PyVar('y')], [PyVar('z')]) x = PyLinexpr1(e) x.set_coeff(PyVar('x0'), PyDoubleScalarCoeff(3)) x.set_coeff(PyVar('z'), PyDoubleScalarCoeff(-9)) x.set_cst(PyDoubleScalarCoeff(8)) c = PyLincons1(ConsTyp.AP_CONS_SUPEQ, x) self.assertEqual(str(c), '3.0·x0 - 9.0·z + 8.0 >= 0') self.assertEqual(str(PyLincons1.unsat(e)), '-1.0 >= 0') self.assertEqual(str(PyLincons1(ConsTyp.AP_CONS_DISEQ, PyLinexpr1(e))), '0.0 != 0')
def test_init(self): e = PyEnvironment([PyVar('x0'), PyVar('y')], [PyVar('z')]) x = PyLinexpr1(e) x0 = PyLinexpr1(e, 0) self.assertEqual(str(PyTexpr1(x)), '0.0') self.assertEqual(str(PyTexpr1(x0)), '0.0 + 0.0 · x0 + 0.0 · y + 0.0 · z') x.set_coeff(PyVar('x0'), PyDoubleScalarCoeff(-1)) x.set_coeff(PyVar('z'), PyDoubleScalarCoeff(-9)) x.set_cst(PyDoubleScalarCoeff(8)) self.assertEqual(str(PyTexpr1(x)), '8.0 - 1.0 · x0 - 9.0 · z')
def __init__(self, domain: Type[PyAbstract1], variables: Set[VariableIdentifier], precursory: State = None): super().__init__(precursory=precursory) self.domain = domain _variables = list() for variable in variables: _variables.append(PyVar(variable.name)) self.environment = PyEnvironment([], _variables) self.state = self.domain(self.manager, self.environment)
def test_bound_texpr(self): e = PyEnvironment([PyVar('x0'), PyVar('y')], [PyVar('z')]) man: PyManager = PyBoxMPQManager() variables = [PyVar('x0'), PyVar('y')] intervals = [PyMPQInterval(-3, 2), PyMPQInterval(-2, 2, 1, 1)] b = PyBox(man, e, variables=variables, intervals=intervals) x0 = PyTexpr1.var(e, PyVar('x0')) x1 = PyTexpr1.var(e, PyVar('y')) add = PyTexpr1.binop(TexprOp.AP_TEXPR_ADD, x0, x1, TexprRtype.AP_RTYPE_REAL, TexprRdir.AP_RDIR_RND) self.assertEqual(str(b.bound_texpr(add)), '[-5,4]')
def test_deepcopy(self): e = PyEnvironment([PyVar('x0'), PyVar('y')], [PyVar('z')]) x = PyLinexpr1(e) x.set_coeff(PyVar('x0'), PyDoubleScalarCoeff(3)) x.set_coeff(PyVar('z'), PyDoubleScalarCoeff(-9)) x.set_cst(PyDoubleScalarCoeff(8)) c0 = PyTcons1.make(PyTexpr1(x), ConsTyp.AP_CONS_SUPEQ) c1 = deepcopy(c0) c2 = c0 self.assertNotEqual(id(c0), id(c1)) self.assertEqual(id(c0), id(c2))
def test_deepcopy(self): e = PyEnvironment([PyVar('x0'), PyVar('y')], [PyVar('z')]) man: PyManager = PyBoxDManager() b0 = PyBox(man, e, variables=[PyVar('x0')], intervals=[PyDoubleInterval(-2.5, 2.5)]) b1 = deepcopy(b0) b2 = b0 self.assertNotEqual(id(b0), id(b1)) self.assertEqual(id(b0), id(b2))
def test_is_real(self): e = PyEnvironment([PyVar('x0'), PyVar('y')], [PyVar('z')]) x0 = PyLinexpr1(e) x0.set_coeff(PyVar('x0'), PyDoubleScalarCoeff(3)) self.assertFalse(x0.is_real()) x1 = PyLinexpr1(e) x1.set_coeff(PyVar('x0'), PyDoubleScalarCoeff(3)) x1.set_coeff(PyVar('z'), PyDoubleScalarCoeff(-9)) self.assertFalse(x1.is_real()) x2 = PyLinexpr1(e) x2.set_coeff(PyVar('z'), PyDoubleScalarCoeff(-9)) self.assertTrue(x2.is_real())
def test_init(self): e = PyEnvironment([PyVar('x'), PyVar('y')], [PyVar('z')]) x1 = PyLinexpr1(e) x1.set_coeff(PyVar('x'), PyDoubleScalarCoeff(3)) x1.set_coeff(PyVar('z'), PyDoubleScalarCoeff(-9)) x1.set_cst(PyDoubleScalarCoeff(8)) c1 = PyLincons1(ConsTyp.AP_CONS_SUPEQ, x1) x2 = PyLinexpr1(e) x2.set_coeff(PyVar('x'), PyDoubleScalarCoeff(1)) c2 = PyLincons1(ConsTyp.AP_CONS_SUP, x2) a = PyLincons1Array([c1, c2]) self.assertEqual(str(a), '3.0·x - 9.0·z + 8.0 >= 0 ∧ 1.0·x + 0.0 > 0')
def test_is_quasilinear(self): e = PyEnvironment([PyVar('x0'), PyVar('y')], [PyVar('z')]) x0 = PyLinexpr1(e) x0.set_coeff(PyVar('x0'), PyDoubleScalarCoeff(3)) x0.set_cst(PyDoubleScalarCoeff(-9)) self.assertTrue(x0.is_quasilinear()) x1 = PyLinexpr1(e) x1.set_coeff(PyVar('x0'), PyDoubleIntervalCoeff(3, 3)) x1.set_cst(PyDoubleIntervalCoeff(-9, 9)) self.assertFalse(x1.is_quasilinear()) x2 = PyLinexpr1(e) x2.set_coeff(PyVar('x0'), PyDoubleScalarCoeff(3)) x2.set_cst(PyDoubleIntervalCoeff(-9, 9)) self.assertTrue(x2.is_quasilinear())
def test_get(self): e = PyEnvironment([PyVar('x'), PyVar('y')], [PyVar('z')]) x1 = PyLinexpr1(e) x1.set_coeff(PyVar('x'), PyDoubleScalarCoeff(3)) x1.set_coeff(PyVar('z'), PyDoubleScalarCoeff(-9)) x1.set_cst(PyDoubleScalarCoeff(8)) c1 = PyLincons1(ConsTyp.AP_CONS_SUPEQ, x1) x2 = PyLinexpr1(e) x2.set_coeff(PyVar('x'), PyDoubleScalarCoeff(1)) c2 = PyLincons1(ConsTyp.AP_CONS_SUP, x2) a = PyLincons1Array([c1, c2]) c = a.get(1) self.assertNotEqual(str(c), str(c1)) self.assertEqual(str(c), str(c2))
def main(self, path, forced_active=None, forced_inactive=None): self.path = path with open(self.path, 'r') as source: self.source = source.read() self.tree = ast.parse(self.source) self.cfg = ast_to_cfg(self.tree) _, variables, _ = self.variables r_vars = list() for variable in variables: r_vars.append(PyVar(variable.name)) environment = PyEnvironment([], r_vars) self.lyra2apron(environment) self.run(forced_active=forced_active, forced_inactive=forced_inactive)
def test_len(self): self.assertEqual( len(PyEnvironment([PyVar('x')], [PyVar('y'), PyVar('z')])), 3) self.assertEqual(len(PyEnvironment([], [PyVar('y'), PyVar('z')])), 2) self.assertEqual( len(PyEnvironment(real_vars=[PyVar('y'), PyVar('z')])), 2) self.assertEqual(len(PyEnvironment([PyVar('x')], [])), 1) self.assertEqual(len(PyEnvironment([PyVar('x')])), 1) self.assertEqual(len(PyEnvironment()), 0)
def test_substitute(self): e = PyEnvironment([PyVar('x0'), PyVar('y')], [PyVar('z')]) x0 = PyLinexpr1(e) x0.set_coeff(PyVar('x0'), PyMPQScalarCoeff(1)) x0.set_cst(PyMPQScalarCoeff(3)) t0 = PyTexpr1(x0) c0 = PyTcons1.make(t0, ConsTyp.AP_CONS_SUPEQ) self.assertEqual(str(c0), '3 + 1 · x0 >= 0') x1 = PyLinexpr1(e) x1.set_coeff(PyVar('x0'), PyMPQScalarCoeff(1)) x1.set_cst(PyMPQScalarCoeff(-1)) t1 = PyTexpr1(x1) c1 = PyTcons1.make(t1, ConsTyp.AP_CONS_SUPEQ) self.assertEqual(str(c1), '-1 + 1 · x0 >= 0') self.assertEqual(str(c0.substitute(PyVar('x0'), t1)), '3 + 1 · (-1 + 1 · x0) >= 0')
def test_substitute(self): e = PyEnvironment([PyVar('x0'), PyVar('y')], [PyVar('z')]) x0 = PyLinexpr1(e) x0.set_coeff(PyVar('x0'), PyMPQScalarCoeff(1)) x0.set_cst(PyMPQScalarCoeff(3)) t0 = PyTexpr1(x0) self.assertEqual(str(t0), '3 + 1 · x0') x1 = PyLinexpr1(e) x1.set_coeff(PyVar('x0'), PyMPQScalarCoeff(1)) x1.set_cst(PyMPQScalarCoeff(-1)) t1 = PyTexpr1(x1) self.assertEqual(str(t1), '-1 + 1 · x0') self.assertEqual(str(t0.substitute(PyVar('x0'), t1)), '3 + 1 · (-1 + 1 · x0)') x2 = PyLinexpr1(e) x2.set_coeff(PyVar('x0'), PyMPQScalarCoeff(1)) x2.set_cst(PyMPQScalarCoeff(2)) t2 = PyTexpr1(x2) self.assertEqual(str(t2), '2 + 1 · x0')
def test_top(self): e = PyEnvironment([PyVar('x0'), PyVar('y')], [PyVar('z')]) man: PyManager = PyBoxDManager() self.assertFalse(PyBox.bottom(man, e).is_top()) b1 = PyBox(man, e, variables=[PyVar('x0')], intervals=[PyDoubleInterval(-2.5, 2.5)]) self.assertFalse(b1.is_bottom()) b2 = PyBox(man, e, variables=[PyVar('y')], intervals=[PyMPQInterval(-5, 5, 2, 2)]) self.assertFalse(b2.is_bottom()) b3 = PyBox(man, e, variables=[PyVar('z')], intervals=[PyMPFRInterval(-2.5, 2.5)]) self.assertFalse(b3.is_bottom()) self.assertTrue(PyBox.top(man, e).is_top())
def main(self, path): self.path = path with open(self.path, 'r') as source: self.source = source.read() self.tree = ast.parse(self.source) self.cfg = ast_to_cfg(self.tree) # # rendering of the original CFG # renderer = CFGRenderer() # data = self.cfg # name = os.path.splitext(os.path.basename(self.path))[0] # label = f"CFG for {name}" # directory = os.path.dirname(self.path) # renderer.render(data, filename=name, label=label, directory=directory, view=True) # walking over the CFG to convert statements to APRON and to collect partitioning information _, variables, _ = self.variables r_vars = list() for variable in variables: r_vars.append(PyVar(variable.name)) environment = PyEnvironment([], r_vars) self.activations, self.splits, self.relus = self.lyra2apron( environment) self.run()
def test_deepcopy(self): e0 = PyEnvironment([PyVar('x')], [PyVar('y'), PyVar('z')]) e1 = deepcopy(e0) e2 = e0 self.assertEqual(id(e0), id(e1)) self.assertEqual(id(e0), id(e2))
def test_add(self): e1 = PyEnvironment([PyVar('x')], [PyVar('y'), PyVar('z')]) e2 = PyEnvironment([PyVar('x')], [PyVar('y')]) self.assertEqual(str(e1), str(e2.add(real_vars=[PyVar('z')]))) self.assertRaises(ValueError, e1.add, [PyVar('x')])
def test_remove(self): e1 = PyEnvironment([PyVar('x')], [PyVar('y'), PyVar('z')]) e2 = PyEnvironment([], [PyVar('y'), PyVar('z')]) self.assertEqual(str(e1.remove([PyVar('x')])), str(e2)) self.assertRaises(ValueError, e1.remove, [PyVar('w')])
def test_union(self): e1 = PyEnvironment([PyVar('x')], [PyVar('y')]) self.assertEqual( e1, PyEnvironment([PyVar('x')]).union(PyEnvironment([], [PyVar('y')]))) e2 = PyEnvironment([], [PyVar('y'), PyVar('z')]) self.assertEqual( e2, PyEnvironment([], [PyVar('y')]) | PyEnvironment([], [PyVar('z')])) e3 = PyEnvironment([PyVar('x')], []) self.assertEqual(PyEnvironment() | PyEnvironment([PyVar('x')], []), e3) self.assertEqual(PyEnvironment([PyVar('x')], []) | PyEnvironment(), e3) e4 = PyEnvironment([PyVar('x')], []) self.assertRaises(ValueError, e4.union, PyEnvironment([], [PyVar('x')]))
def test_cmp(self): self.assertTrue( PyEnvironment([PyVar('x')]) < PyEnvironment([PyVar('x')], [PyVar('y')])) self.assertFalse( PyEnvironment([PyVar('x')]) > PyEnvironment([PyVar('x')])) self.assertTrue( PyEnvironment([PyVar('x')]) == PyEnvironment([PyVar('x')], [])) self.assertFalse( PyEnvironment([PyVar('x')]) == PyEnvironment([PyVar('x')], [PyVar('y')])) self.assertTrue(PyEnvironment([PyVar('x')]) > PyEnvironment()) self.assertFalse(PyEnvironment([PyVar('x')]) < PyEnvironment()) self.assertTrue( PyEnvironment().add([PyVar('x')]) == PyEnvironment([PyVar('x')])) self.assertTrue( PyEnvironment([PyVar('x')]).remove([PyVar('x')]) == PyEnvironment())