kse-02/archive.py

67 lines
2.2 KiB
Python

from typing import Dict, Set, List, Tuple
from frozendict import frozendict
import instrument
import operators
class Archive:
true_branches: Dict[int, any]
false_branches: Dict[int, any]
f_name: str
def __init__(self, f_name: str) -> None:
self.reset()
self.f_name = f_name
def reset(self):
self.true_branches = {}
self.false_branches = {}
def branches_covered(self) -> int:
return len(self.true_branches.keys()) + len(self.false_branches.keys())
def branches_str(self) -> str:
branch_ids = sorted([f"{branch:2d}T" for branch in self.true_branches.keys()] +
[f"{branch:2d}F" for branch in self.false_branches.keys()])
return ' '.join([branch.strip() for branch in branch_ids])
def build_suite(self) -> Set[instrument.Params]:
return set(list(self.true_branches.values()) + list(self.false_branches.values()))
def suite_str(self):
suite = self.build_suite()
return " ".join([",".join([f'{k}={repr(v)}' for k, v in test.items()]) for test in suite])
def consider_test(self, test_case: frozendict):
branch = self.satisfies_unseen_branches(test_case)
for branch, true_or_false in branch:
if true_or_false:
self.true_branches[branch] = test_case
else:
self.false_branches[branch] = test_case
def satisfies_unseen_branches(self, test_case: frozendict) -> List[Tuple[int, bool]]:
try:
instrument.invoke(self.f_name, test_case)
except AssertionError:
return []
range_start, range_end = instrument.n_of_branches[self.f_name]
branches: List[Tuple[int, bool]] = []
for branch in range(range_start, range_end):
if (branch in operators.distances_true and
operators.distances_true[branch] == 0 and
branch not in self.true_branches):
branches.append((branch, True))
if (branch in operators.distances_false and
operators.distances_false[branch] == 0 and
branch not in self.false_branches):
branches.append((branch, False))
return branches