def test_with_source(self, mock_cfg, src1): standard_cfg(mock_cfg) theo = theory.Z3Theory(pp("p(X) :- q(a=X).")) theo.build_theory() self.assertEqual( (['X'], [z3r.Cube({0: 421}, 1), z3r.Cube({0: 567}, 1)]), theo.query(parser.parse_atom("p(X)")))
def test_print_pretty_doc(self): answer = [ z3r.Doc(z3r.Cube({0: z3r.Masked(0x10, 0x30)}, 1), [z3r.Cube({ 0: z3r.Masked(0x8c, 0xcc), 1: 4 }, 1)]), z3r.Doc(z3r.Cube({}, 1), [ z3r.Cube({0: z3r.Masked(0x10, 0x30)}, 1), z3r.Cube({ 0: z3r.Masked(0x8c, 0xcc), 1: 4 }, 1) ]) ] formatted = textwrap.dedent("""\ +===+=========+===+ | | X | Y | +===+=========+===+ | + | 16/48 | * | +---+---------+---+ | - | 140/204 | 4 | +===+=========+===+ | + | * | * | +---+---------+---+ | - | 16/48 | * | | - | 140/204 | 4 | +===+=========+===+ """) with capture_stdout() as out: printer.print_pretty(["X", "Y"], answer) self.assertEqual(formatted, out.getvalue())
def test_print_csv_doc(self): answer = [ z3r.Doc(z3r.Cube({0: z3r.Masked(0x10, 0x30)}, 1), [z3r.Cube({ 0: z3r.Masked(0x8c, 0xcc), 1: 4 }, 1)]), z3r.Doc(z3r.Cube({}, 1), [ z3r.Cube({0: z3r.Masked(0x10, 0x30)}, 1), z3r.Cube({ 0: z3r.Masked(0x8c, 0xcc), 1: 4 }, 1) ]) ] formatted = textwrap.dedent("""\ P,X,Y\r +,16/48,*\r -,140/204,4\r +,*,*\r -,16/48,*\r -,140/204,4\r\n """) with capture_stdout() as out: printer.print_csv(["X", "Y"], answer) self.assertEqual(formatted, out.getvalue())
def test_build_theory_simple(self, mock_cfg, src1, src2): standard_cfg(mock_cfg) theo = theory.Z3Theory(pp(PROG1)) theo.build_theory() r = theo.query(parser.parse_atom("p(X)")) self.assertEqual((['X'], [z3r.Cube({0: 3}, 1)]), r) r = theo.query(parser.parse_atom("q(X)")) self.assertEqual( (['X'], [z3r.Cube({0: 2}, 1), z3r.Cube({0: 3}, 1)]), r)
def test_print_csv(self): with capture_stdout() as out: printer.print_csv( ["X", "Y"], [z3r.Cube({ 0: 2, 1: 3 }, 1), z3r.Cube({ 0: 4, 1: 5 }, 1)]) self.assertEqual('P,X,Y\r\n+,2,3\r\n+,4,5\r\n\n', out.getvalue()) with capture_stdout() as out: printer.print_csv([], True) self.assertIs(True, "True" in out.getvalue())
def test_z3_to_array_doc(self): s = z3.BitVecSort(8) s2 = z3.BitVecSort(2) def bv(x): return z3.BitVecVal(x, s) def bv2(x): return z3.BitVecVal(x, s2) x = z3.Var(0, s) y = z3.Var(1, s) types = [primitives.TYPES['int4']] * 2 e1 = z3.Extract(3, 2, x) == bv2(3) e2 = z3.And( z3.Extract(3, 2, x) == bv2(3), z3.Extract(7, 6, x) == bv2(2)) e3 = z3.And( z3.Extract(3, 2, x) == bv2(3), z3.Extract(7, 6, x) == bv2(2), y == bv(4)) e4 = z3.And( z3.Extract(5, 4, x) == bv2(1), z3.Not( z3.And( z3.Extract(3, 2, x) == bv2(3), z3.Extract(7, 6, x) == bv2(2), y == bv(4)))) e5 = z3.And( True, z3.Not(z3.Extract(5, 4, x) == bv2(1)), z3.Not( z3.And( z3.Extract(3, 2, x) == bv2(3), z3.Extract(7, 6, x) == bv2(2), y == bv(4)))) r1 = [z3r.Cube({0: z3r.Masked(0xc, 0xc)}, 1)] r2 = [z3r.Cube({0: z3r.Masked(0x8c, 0xcc)}, 1)] r3 = [z3r.Cube({0: z3r.Masked(0x8c, 0xcc), 1: 4}, 1)] r4 = [ z3r.Doc(z3r.Cube({0: z3r.Masked(0x10, 0x30)}, 1), [z3r.Cube({ 0: z3r.Masked(0x8c, 0xcc), 1: 4 }, 1)]) ] r5 = [ z3r.Doc(z3r.Cube({}, 1), [ z3r.Cube({0: z3r.Masked(0x10, 0x30)}, 1), z3r.Cube({ 0: z3r.Masked(0x8c, 0xcc), 1: 4 }, 1) ]) ] for (r, e) in [(r1, e1), (r2, e2), (r3, e3), (r4, e4), (r5, e5)]: self.assertEqual(r, z3r.z3_to_array(e, types))
def test_print_result_pretty(self): with base.capture_stdout() as out: octant.print_result( "query", ["VarX", "Y"], [z3r.Cube({ 0: 2134, 1: 3 }, 1), z3r.Cube({ 0: 4, 1: 572 }, 1)], 3.5, True) result = out.getvalue() self.assertIs(True, "2134" in result) self.assertIs(True, "572" in result) self.assertIs(True, "VarX" in result) with base.capture_stdout() as out: octant.print_result("query", [], True, None, True) result = out.getvalue() self.assertIs(True, "True" in result)