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_simple.py
More file actions
122 lines (103 loc) · 3.93 KB
/
test_simple.py
File metadata and controls
122 lines (103 loc) · 3.93 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
from __future__ import annotations
from typing import TYPE_CHECKING
import pytest
from pyk.cterm import CTerm
from pyk.kast.inner import KApply, KSequence, KToken, KVariable
from pyk.kast.manip import get_cell
from pyk.prelude.ml import mlEqualsTrue, mlTop
from pyk.testing import KCFGExploreTest
from ..utils import K_FILES
if TYPE_CHECKING:
from collections.abc import Iterable
from typing import Final, Union
from pyk.kcfg import KCFGExplore
from pyk.ktool.kprint import KPrint
STATE = Union[tuple[str, str], tuple[str, str, str]]
EXECUTE_TEST_DATA: Iterable[tuple[str, int, STATE, int, STATE, list[STATE]]] = (
('branch', 3, ('a', '.Map'), 1, ('b', '.Map'), [('c', '.Map'), ('d', '.Map')]),
(
'no-branch',
1,
('foo', '3 |-> M:Int', 'notBool pred1(M:Int)'),
0,
('foo', '3 |-> M:Int'),
[],
),
)
SIMPLIFY_TEST_DATA: Final = (('bytes-return', ('mybytes', '.Map'), (r'b"\x00\x90\xa0\n\xa1\xf1a"', '.Map')),)
class TestSimpleProof(KCFGExploreTest):
KOMPILE_MAIN_FILE = K_FILES / 'simple-proofs.k'
@staticmethod
def config(kprint: KPrint, k: str, state: str, constraint: str | None = None) -> CTerm:
_k_parsed = kprint.parse_token(KToken(k, 'KItem'), as_rule=True)
_state_parsed = kprint.parse_token(KToken(state, 'Map'), as_rule=True)
_constraint = (
mlTop()
if constraint is None
else mlEqualsTrue(kprint.parse_token(KToken(constraint, 'Bool'), as_rule=True))
)
return CTerm(
KApply(
'<generatedTop>',
KApply('<k>', KSequence(_k_parsed)),
KApply('<state>', _state_parsed),
KVariable('GENERATED_COUNTER_CELL'),
),
(_constraint,),
)
@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: tuple[str, str],
expected_depth: int,
expected_post: tuple[str, str],
expected_next_states: Iterable[tuple[str, str]],
) -> None:
# Given
expected_k, expected_state, *_ = expected_post
# When
exec_res = kcfg_explore.cterm_execute(self.config(kcfg_explore.kprint, *pre), depth=depth)
actual_k = kcfg_explore.kprint.pretty_print(exec_res.state.cell('K_CELL'))
actual_state = kcfg_explore.kprint.pretty_print(exec_res.state.cell('STATE_CELL'))
actual_depth = exec_res.depth
actual_next_states = [
(
kcfg_explore.kprint.pretty_print(s.cell('K_CELL')),
kcfg_explore.kprint.pretty_print(s.cell('STATE_CELL')),
)
for s in exec_res.next_states
]
# Then
assert actual_k == expected_k
assert actual_state == expected_state
assert actual_depth == expected_depth
assert set(actual_next_states) == set(expected_next_states)
@pytest.mark.parametrize(
'test_id,pre,expected_post',
SIMPLIFY_TEST_DATA,
ids=[test_id for test_id, *_ in SIMPLIFY_TEST_DATA],
)
def test_simplify(
self,
kcfg_explore: KCFGExplore,
test_id: str,
pre: tuple[str, str],
expected_post: tuple[str, str],
) -> None:
# Given
k, state = pre
expected_k, expected_state, *_ = expected_post
# When
_unknwon_predicate, actual_post, _logs = kcfg_explore.cterm_simplify(self.config(kcfg_explore.kprint, *pre))
actual_k = kcfg_explore.kprint.pretty_print(get_cell(actual_post.kast, 'K_CELL'))
actual_state = kcfg_explore.kprint.pretty_print(get_cell(actual_post.kast, 'STATE_CELL'))
# Then
assert actual_k == expected_k
assert actual_state == expected_state