2023-12-20 13:19:45 +00:00
|
|
|
from typing import Optional, Dict, Set
|
|
|
|
|
|
|
|
from frozendict import frozendict
|
|
|
|
|
|
|
|
import instrument
|
|
|
|
import operators
|
|
|
|
|
|
|
|
|
|
|
|
class Archive:
|
|
|
|
true_branches: Dict[int, any]
|
|
|
|
false_branches: Dict[int, any]
|
|
|
|
false_score: Dict[int, any]
|
|
|
|
true_score: 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 = {}
|
|
|
|
self.true_score = {}
|
|
|
|
self.false_score = {}
|
|
|
|
|
|
|
|
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()))
|
|
|
|
|
2023-12-21 22:54:39 +00:00
|
|
|
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])
|
|
|
|
|
2023-12-20 13:19:45 +00:00
|
|
|
def consider_test(self, test_case: frozendict):
|
2023-12-21 22:54:39 +00:00
|
|
|
try:
|
|
|
|
instrument.invoke(self.f_name, test_case)
|
|
|
|
except AssertionError:
|
|
|
|
return
|
2023-12-20 13:19:45 +00:00
|
|
|
|
|
|
|
range_start, range_end = instrument.n_of_branches[self.f_name]
|
|
|
|
|
|
|
|
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):
|
|
|
|
self.true_branches[branch] = test_case
|
|
|
|
if (branch in operators.distances_false and
|
|
|
|
operators.distances_false[branch] == 0 and branch not in self.false_branches):
|
|
|
|
self.false_branches[branch] = test_case
|
|
|
|
|
2023-12-21 22:54:39 +00:00
|
|
|
def satisfies_unseen_branch(self, test_case: frozendict) -> Optional[str]:
|
2023-12-20 13:19:45 +00:00
|
|
|
try:
|
|
|
|
instrument.invoke(self.f_name, test_case)
|
|
|
|
except AssertionError:
|
|
|
|
return None
|
|
|
|
range_start, range_end = instrument.n_of_branches[self.f_name]
|
|
|
|
|
|
|
|
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):
|
|
|
|
return f"{branch}T"
|
|
|
|
if (branch in operators.distances_false and
|
|
|
|
operators.distances_false[branch] == 0 and
|
|
|
|
branch not in self.false_branches):
|
|
|
|
return f"{branch}F"
|
|
|
|
|
|
|
|
return None
|