|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from typing import TYPE_CHECKING |
| 5 | + |
| 6 | +import pytest |
| 7 | +from pyk.cterm import CTerm |
| 8 | +from pyk.kast.inner import KInner, Subst |
| 9 | +from pyk.kast.manip import split_config_from |
| 10 | +from pyk.kcfg import KCFG |
| 11 | +from pyk.proof.reachability import APRProof, APRProver |
| 12 | + |
| 13 | +from kmir.kmir import KMIR |
| 14 | +from kmir.parse.parser import Parser |
| 15 | +from kmir.rust.cargo import cargo_get_smir_json |
| 16 | + |
| 17 | +if TYPE_CHECKING: |
| 18 | + from kmir.tools import Tools |
| 19 | + |
| 20 | + |
| 21 | +PROVING_DIR = (Path(__file__).parent / 'data' / 'prove-rs').resolve(strict=True) |
| 22 | +PROVING_FILES = list(PROVING_DIR.glob('*.rs')) |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.parametrize( |
| 26 | + 'rs_file', |
| 27 | + PROVING_FILES, |
| 28 | + ids=[spec.stem for spec in PROVING_FILES], |
| 29 | +) |
| 30 | +def test_prove_rs(rs_file: Path, tmp_path: Path, kmir: KMIR, tools: Tools) -> None: |
| 31 | + should_fail = rs_file.stem.endswith('fail') |
| 32 | + smir_json = cargo_get_smir_json(rs_file) |
| 33 | + |
| 34 | + parser = Parser(kmir.definition) |
| 35 | + parse_result = parser.parse_mir_json(smir_json, 'Pgm') |
| 36 | + assert parse_result is not None |
| 37 | + kmir_kast, _ = parse_result |
| 38 | + assert isinstance(kmir_kast, KInner) |
| 39 | + |
| 40 | + config = tools.make_init_config(kmir_kast, 'main') |
| 41 | + config_with_cell_vars, _ = split_config_from(config) |
| 42 | + |
| 43 | + lhs = CTerm(config) |
| 44 | + |
| 45 | + rhs_subst = Subst({'K_CELL': KMIR.Symbols.END_PROGRAM}) |
| 46 | + rhs = CTerm(rhs_subst(config_with_cell_vars)) |
| 47 | + kcfg = KCFG() |
| 48 | + init_node = kcfg.create_node(lhs) |
| 49 | + target_node = kcfg.create_node(rhs) |
| 50 | + apr_proof = APRProof('PROOF', kcfg, [], init_node.id, target_node.id, {}) |
| 51 | + with kmir.kcfg_explore('PROOF-TEST') as kcfg_explore: |
| 52 | + prover = APRProver(kcfg_explore) |
| 53 | + prover.advance_proof(apr_proof) |
| 54 | + if not should_fail: |
| 55 | + assert apr_proof.passed |
| 56 | + else: |
| 57 | + assert apr_proof.failed |
0 commit comments