This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
kse-02/archive.py

71 lines
2.3 KiB
Python
Raw Normal View History

2023-12-24 13:55:34 +00:00
from typing import Dict, Set, List, Tuple
2023-12-20 13:19:45 +00:00
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()))
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-24 13:55:34 +00:00
branch = self.satisfies_unseen_branches(test_case)
2023-12-20 13:19:45 +00:00
2023-12-24 13:55:34 +00:00
for branch, true_or_false in branch:
if true_or_false:
2023-12-20 13:19:45 +00:00
self.true_branches[branch] = test_case
2023-12-24 13:55:34 +00:00
else:
2023-12-20 13:19:45 +00:00
self.false_branches[branch] = test_case
2023-12-24 13:55:34 +00:00
def satisfies_unseen_branches(self, test_case: frozendict) -> List[Tuple[int, bool]]:
2023-12-20 13:19:45 +00:00
try:
instrument.invoke(self.f_name, test_case)
except AssertionError:
2023-12-24 13:55:34 +00:00
return []
2023-12-20 13:19:45 +00:00
range_start, range_end = instrument.n_of_branches[self.f_name]
2023-12-24 13:55:34 +00:00
branches: List[Tuple[int, bool]] = []
2023-12-20 13:19:45 +00:00
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):
2023-12-24 13:55:34 +00:00
branches.append((branch, True))
2023-12-20 13:19:45 +00:00
if (branch in operators.distances_false and
operators.distances_false[branch] == 0 and
branch not in self.false_branches):
2023-12-24 13:55:34 +00:00
branches.append((branch, False))
return branches