from __future__ import annotations import shutil import subprocess import tempfile import unittest from pathlib import Path from tools.prod2u_p4_harness import resolve_tool, tool_env ROOT = Path(__file__).resolve().parents[2] FIXTURES = ROOT / "tests" / "fixtures" PLANNER_RTL = ROOT / "src" / "modules" / "door_percent_planner.v" PLANNER_TB = ROOT / "tests" / "sim" / "door_percent_planner_tb.v" def _run_mutated_planner_tb() -> tuple[ subprocess.CompletedProcess[str], subprocess.CompletedProcess[str], str, ]: iverilog = resolve_tool("iverilog") vvp = resolve_tool("vvp") missing = [name for name, executable in (("iverilog", iverilog), ("vvp", vvp)) if not executable] if missing: raise unittest.SkipTest( "production RTL mutation gate unavailable: " + ", ".join(missing) ) original_source = PLANNER_RTL.read_text(encoding="utf-8") comparison = "acc <= 8'd50;" mutation = "acc <= 8'd0;" if original_source.count(comparison) != 1: raise AssertionError("door_percent_planner.v mutation anchor is not unique") with tempfile.TemporaryDirectory(prefix="prod2u-planner-mutation-") as directory: workdir = Path(directory) mutated_rtl = workdir / PLANNER_RTL.name shutil.copy2(PLANNER_RTL, mutated_rtl) mutated_source = mutated_rtl.read_text(encoding="utf-8").replace( comparison, mutation, 1, ) if mutated_source == original_source: raise AssertionError("planner mutation did not change the RTL copy") mutated_rtl.write_text(mutated_source, encoding="utf-8", newline="") output = workdir / "door_percent_planner_mutated.vvp" compile_result = subprocess.run( [ iverilog, "-g2012", "-s", "door_percent_planner_tb", "-o", str(output), str(PLANNER_TB), str(mutated_rtl), ], cwd=workdir, env=tool_env(), text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=60, check=False, ) if compile_result.returncode != 0: return compile_result, compile_result, original_source run_result = subprocess.run( [vvp, str(output)], cwd=workdir, env=tool_env(), text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=60, check=False, ) return compile_result, run_result, original_source class NegativeToolFixtureTests(unittest.TestCase): def _run_fixture(self, tool: str, args: list[str], *, cwd: Path) -> subprocess.CompletedProcess[str]: executable = resolve_tool(tool) if not executable: self.skipTest(f"{tool} is unavailable in the configured toolchain") return subprocess.run( [executable, *args], cwd=cwd, env=tool_env(), text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=60, check=False, ) def test_icarus_rejects_negative_fixture(self): with tempfile.TemporaryDirectory() as directory: output = Path(directory) / "bad.vvp" result = self._run_fixture( "iverilog", ["-g2012", "-o", str(output), str(FIXTURES / "bad_icarus_tb.v")], cwd=FIXTURES, ) self.assertNotEqual(result.returncode, 0, result.stdout + result.stderr) def test_verilator_rejects_negative_fixture(self): result = self._run_fixture( "verilator", ["--lint-only", str(FIXTURES / "bad_verilator_lint.v")], cwd=FIXTURES, ) self.assertNotEqual(result.returncode, 0, result.stdout + result.stderr) def test_formal_runner_rejects_negative_fixture(self): sby = resolve_tool("sby") if not sby: self.skipTest("sby is unavailable in the configured toolchain") with tempfile.TemporaryDirectory() as directory: root = Path(directory) shutil.copy2(FIXTURES / "bad_formal_fail.v", root / "bad_formal_fail.v") shutil.copy2(FIXTURES / "bad_formal_fail.sby", root / "bad_formal_fail.sby") result = subprocess.run( [sby, "-f", "bad_formal_fail.sby"], cwd=root, env=tool_env(), text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=60, check=False, ) self.assertNotEqual(result.returncode, 0, result.stdout + result.stderr) def test_real_production_planner_mutation_is_rejected_by_icarus(self): compile_result, run_result, original_source = _run_mutated_planner_tb() self.assertEqual( compile_result.returncode, 0, compile_result.stdout + compile_result.stderr, ) self.assertNotEqual( run_result.returncode, 0, run_result.stdout + run_result.stderr, ) self.assertEqual( PLANNER_RTL.read_text(encoding="utf-8"), original_source, ) if __name__ == "__main__": unittest.main()