| 1 | import pytest |
| 2 | try: |
| 3 | import rply |
| 4 | import z3 |
| 5 | except ImportError: |
| 6 | pytest.skip('rply or z3 not installed') |
| 7 | |
| 8 | from rpython.rlib.rarithmetic import LONG_BIT, r_uint, intmask, ovfcheck, uint_mul_high |
| 9 | |
| 10 | from rpython.jit.metainterp.ruleopt.proof import * |
| 11 | |
| 12 | import os |
| 13 | with open(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "real.rules")) as f: |
| 14 | ALLRULES = f.read() |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | def test_higest_bit(): |
| 20 | for i in range(64): |
| 21 | assert highest_bit(r_uint(1) << i) == i |
| 22 | |
| 23 | |
| 24 | @pytest.mark.parametrize("name,rule", [(rule.name, rule) for rule in parse.parse(ALLRULES).rules if not rule.cantproof]) |
| 25 | def test_z3_prove(name, rule): |
| 26 | p = Prover() |
| 27 | try: |
| 28 | p.check_rule(rule) |
| 29 | except ProofProblem as e: |
| 30 | print e.format() |
| 31 | raise |
| 32 | |
| 33 | def test_sorry(): |
| 34 | s = """\ |
| 35 | eq_different_knownbits: int_eq(x, y) |
| 36 | SORRY_Z3 |
| 37 | => 0 |
| 38 | """ |
| 39 | prove_source(s) |
| 40 | |
| 41 | def test_explain_problem(): |
| 42 | s = """\ |
| 43 | bug: int_and(x, y) |
| 44 | => 1 |
| 45 | """ |
| 46 | with pytest.raises(CouldNotProve) as info: |
| 47 | prove_source(s) |
| 48 | assert info.value.format() == '''\ |
| 49 | Could not prove correctness of rule 'bug' |
| 50 | in line 1 |
| 51 | counterexample given by Z3: |
| 52 | counterexample values: |
| 53 | x: 0 |
| 54 | y: 0 |
| 55 | operation int_and(x, y) with Z3 formula x & y |
| 56 | has counterexample result vale: 0 |
| 57 | BUT |
| 58 | target expression: 1 with Z3 formula 1 |
| 59 | has counterexample value: 1''' |
| 60 | |
| 61 | def test_explain_problem_empty(): |
| 62 | s = """\ |
| 63 | never_applies: int_is_true(x) |
| 64 | check x.known_lt_const(0) and x.known_gt_const(0) # impossible condition |
| 65 | => x |
| 66 | """ |
| 67 | with pytest.raises(RuleCannotApply) as info: |
| 68 | prove_source(s) |
| 69 | assert info.value.format() == '''\ |
| 70 | Rule 'never_applies' cannot ever apply |
| 71 | in line 1 |
| 72 | Z3 did not manage to find values for variables x such that the following condition becomes True: |
| 73 | And(x <= x_upper, |
| 74 | x_lower <= x, |
| 75 | If(x_upper < 0, x_lower > 0, x_upper < 0))\ |
| 76 | ''' |