This repository was archived by the owner on Apr 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_cell_map.py
More file actions
151 lines (127 loc) · 4.8 KB
/
test_cell_map.py
File metadata and controls
151 lines (127 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from __future__ import annotations
import logging
from pathlib import Path
from typing import TYPE_CHECKING, NamedTuple
import pytest
from pyk.cterm import CTerm
from pyk.kast.inner import KApply, KSequence, KToken, KVariable, build_assoc
from pyk.kcfg.show import KCFGShow
from pyk.proof import APRProof, APRProver, ProofStatus
from pyk.proof.show import APRProofNodePrinter
from pyk.testing import KCFGExploreTest, KProveTest
from pyk.utils import single
from ..utils import K_FILES
if TYPE_CHECKING:
from collections.abc import Iterable
from typing import Final
from pyk.kast import KInner
from pyk.kcfg import KCFGExplore
from pyk.ktool.kprint import KPrint
from pyk.ktool.kprove import KProve
_LOGGER: Final = logging.getLogger(__name__)
class State(NamedTuple):
pgm: str
active_accounts: str
accounts: Iterable[tuple[str, str]]
EXECUTE_TEST_DATA: Final[Iterable[tuple[str, int, State, int, State, Iterable[State]]]] = (
(
'account-nonexistent',
1,
State('#accountNonexistent(1)', 'SetItem(1)', [('1', '2')]),
1,
State('false', 'SetItem(1)', [('1', '2')]),
[],
),
)
APR_PROVE_TEST_DATA: Iterable[tuple[str, Path, str, str, int | None, int | None]] = (
('cell-map-no-branch', K_FILES / 'cell-map-spec.k', 'CELL-MAP-SPEC', 'cell-map-no-branch', 2, 1),
)
class TestCellMapProof(KCFGExploreTest, KProveTest):
KOMPILE_MAIN_FILE = K_FILES / 'cell-map.k'
@staticmethod
def config(kprint: KPrint, k: str, active_accounts: str, accounts: Iterable[tuple[str, str]]) -> CTerm:
def _parse(kt: KToken) -> KInner:
return kprint.parse_token(kt, as_rule=True)
_k_parsed = _parse(KToken(k, 'KItem'))
_active_accounts = _parse(KToken(active_accounts, 'Set'))
_accounts_parsed = (
KApply(
'AccountCellMapItem',
KApply('<id>', _parse(KToken(act_id, 'Int'))),
KApply(
'<account>',
KApply('<id>', _parse(KToken(act_id, 'Int'))),
KApply('<balance>', _parse(KToken(act_state, 'Int'))),
),
)
for act_id, act_state in accounts
)
_accounts = build_assoc(KApply('.AccountCellMap'), '_AccountCellMap_', _accounts_parsed)
return CTerm(
KApply(
'<generatedTop>',
KApply('<k>', KSequence(_k_parsed)),
KApply('<activeAccounts>', _active_accounts),
KApply('<accounts>', _accounts),
KVariable('GENERATED_COUNTER_CELL'),
),
)
@pytest.mark.parametrize(
'test_id,depth,pre,expected_depth,expected_post,expected_next_states',
EXECUTE_TEST_DATA,
ids=[test_id for test_id, *_ in EXECUTE_TEST_DATA],
)
def test_execute(
self,
kcfg_explore: KCFGExplore,
test_id: str,
depth: int,
pre: State,
expected_depth: int,
expected_post: State,
expected_next_states: Iterable[State],
) -> None:
# Given
k, aacounts, accounts = pre
expected_k, _, _ = expected_post
# When
exec_res = kcfg_explore.cterm_execute(self.config(kcfg_explore.kprint, k, aacounts, accounts), depth=depth)
actual_k = kcfg_explore.kprint.pretty_print(exec_res.state.cell('K_CELL'))
actual_depth = exec_res.depth
# Then
assert actual_depth == expected_depth
assert actual_k == expected_k
@pytest.mark.parametrize(
'test_id,spec_file,spec_module,claim_id,max_iterations,max_depth',
APR_PROVE_TEST_DATA,
ids=[test_id for test_id, *_ in APR_PROVE_TEST_DATA],
)
def test_all_path_reachability_prove(
self,
kprove: KProve,
kcfg_explore: KCFGExplore,
test_id: str,
spec_file: str,
spec_module: str,
claim_id: str,
max_iterations: int,
max_depth: int,
) -> None:
claim = single(
kprove.get_claims(Path(spec_file), spec_module_name=spec_module, claim_labels=[f'{spec_module}.{claim_id}'])
)
proof = APRProof.from_claim(kprove.definition, claim, logs={})
init = proof.kcfg.node(proof.init)
new_init_term = kcfg_explore.cterm_assume_defined(init.cterm)
proof.kcfg.replace_node(init.id, new_init_term)
prover = APRProver(proof, kcfg_explore=kcfg_explore)
prover.advance_proof(
max_iterations=max_iterations,
execute_depth=max_depth,
)
kcfg_show = KCFGShow(
kcfg_explore.kprint, node_printer=APRProofNodePrinter(proof, kcfg_explore.kprint, full_printer=True)
)
cfg_lines = kcfg_show.show(proof.kcfg)
_LOGGER.info('\n'.join(cfg_lines))
assert proof.status == ProofStatus.PASSED