| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- """Mutation test: removing WAIT_SYNC must cause startup_close_seek to fail."""
- from __future__ import annotations
- import re
- import subprocess
- import sys
- import tempfile
- import unittest
- from pathlib import Path
- ROOT = Path(__file__).resolve().parents[2]
- SOURCE = ROOT / "src" / "modules" / "startup_close_seek.v"
- sys.path.insert(0, str(ROOT / "tools"))
- import prod2u_p4_harness # type: ignore
- def mutated_source() -> str:
- text = SOURCE.read_text(encoding="utf-8")
- text = re.sub(
- r"if \(limit_sync_valid\)\s*state\s*<=\s*ST_CHECK;",
- "state <= ST_CHECK;",
- text,
- count=1,
- )
- text = re.sub(
- r"ST_WAIT_SYNC:\s*begin\s*motor_start\s*<=\s*1'b0;\s*if\s*\(limit_sync_valid\)\s*state\s*<=\s*ST_CHECK;\s*end\s*",
- "",
- text,
- count=1,
- )
- return text
- class StartupMutationTests(unittest.TestCase):
- def test_removing_wait_sync_fails(self):
- iverilog = prod2u_p4_harness.resolve_tool("iverilog")
- vvp = prod2u_p4_harness.resolve_tool("vvp")
- if not iverilog or not vvp:
- self.skipTest("iverilog/vvp not available")
- with tempfile.TemporaryDirectory() as d:
- tmp = Path(d)
- mutated = tmp / "startup_close_seek_mut.v"
- mutated.write_text(mutated_source(), encoding="utf-8")
- tb = tmp / "tb.v"
- tb.write_text(
- """
- `timescale 1ns/1ps
- module tb;
- reg clk = 1'b0;
- reg rst_n = 1'b0;
- reg tick_500us = 1'b0;
- reg [2:0] limit_ls = 3'b000;
- reg [2:0] limit_hs = 3'b000;
- reg limit_sync_valid = 1'b0;
- wire startup_done;
- wire motor_start;
- wire [2:0] motor_active_mask;
- wire [2:0] motor_dir_mask;
- wire [2:0] motor_stop_mask;
- wire [12:0] motor_step_count;
- reg motor_done = 1'b0;
- always #1 clk = ~clk;
- startup_close_seek #(
- .CLOSE_STEPS(8),
- .BACKOFF_STEPS(4),
- .AXIS_COUNT(3)
- ) dut (
- .clk(clk), .rst_n(rst_n), .tick_500us(tick_500us),
- .limit_sync_valid(limit_sync_valid),
- .limit_ls_sync(limit_ls), .limit_hs_sync(limit_hs),
- .startup_done(startup_done), .motor_start(motor_start),
- .motor_active_mask(motor_active_mask), .motor_dir_mask(motor_dir_mask),
- .motor_step_count(motor_step_count), .motor_stop_mask(motor_stop_mask),
- .motor_done(motor_done)
- );
- integer i;
- initial begin
- repeat (3) @(posedge clk);
- rst_n = 1'b1;
- for (i = 0; i < 20; i = i + 1) begin
- @(posedge clk);
- if (motor_start) begin
- $display("MUTATION_DETECTED");
- $finish;
- end
- end
- $fatal(1, "mutation was not detected");
- end
- endmodule
- """,
- encoding="utf-8",
- )
- vvp_out = tmp / "a.out"
- env = prod2u_p4_harness.tool_env()
- compile_result = subprocess.run(
- [iverilog, "-g2005-sv", "-s", "tb", "-o", str(vvp_out), str(mutated), str(tb)],
- cwd=tmp,
- capture_output=True,
- text=True,
- env=env,
- )
- self.assertEqual(compile_result.returncode, 0, compile_result.stderr)
- run_result = subprocess.run(
- [vvp, str(vvp_out)],
- cwd=tmp,
- capture_output=True,
- text=True,
- env=env,
- )
- self.assertIn("MUTATION_DETECTED", run_result.stdout)
- if __name__ == "__main__":
- unittest.main()
|