From 10e0c53890701abe53c80f1da4a0b64bbd05914c Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Tue, 28 Mar 2023 21:27:05 +0300 Subject: Initial commit --- src/test_utils.py | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 src/test_utils.py (limited to 'src/test_utils.py') diff --git a/src/test_utils.py b/src/test_utils.py new file mode 100644 index 0000000..1db23b2 --- /dev/null +++ b/src/test_utils.py @@ -0,0 +1,93 @@ +import random +from dataclasses import dataclass +import tempfile +import sys +import os + +def make_test_file(name: str, table: str) -> str: + return f""" + + 2 + + + + Testcase + + + Label + {name} + + + Testdata + + {table} + + + + + + + + + + """ + +@dataclass +class Param: + name: str + min: int + max: int + +class TestGroup: + def __init__(self, inputs: list[Param], outputs: list[str]): + self.inputs = inputs + self.outputs = outputs + self.cases = [] + + def generate_cases(self, n = 1000): + for _ in range(n): + result = {} + for param in self.inputs: + val = random.randrange(param.min, param.max + 1) + result[param.name] = val + + self.cases.append(result) + + def process(self): + raise NotImplemented() + + def to_table(self) -> str: + names: list[str] = [] + for param in self.inputs: + names.append(param.name) + for output in self.outputs: + names.append(output) + + ret = " ".join(names) + ret += "\n" + + for case in self.cases: + row_vals = [] + for param_name in names: + row_vals.append(str(case[param_name])) + + ret += " ".join(row_vals) + ret += "\n" + + return ret + +def run_test(name: str, table: str): + if len(sys.argv) != 2: + print(f"usage: {sys.argv[0]} ") + sys.exit(1) + + circuit_path = sys.argv[1] + _, test_path = tempfile.mkstemp(".dig") + + fc = make_test_file(name, table) + with open(test_path, "w") as f: + f.write(fc) + + stream = os.popen(f"make run-single-test circ={circuit_path} tests={test_path}") + output = stream.read() + print(output) -- cgit v1.3