| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- """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"
- # Import harness tooling for toolchain discovery.
- sys.path.insert(0, str(ROOT / "tools"))
- import prod2u_p4_harness # type: ignore
- def mutated_source() -> str:
- text = SOURCE.read_text(encoding="utf-8")
- # Make the module ignore limit_sync_valid by removing the WAIT_SYNC state and
- # the valid gating on reached_mask_next.
- text = re.sub(
- r"state\s*<=\s*ST_WAIT_SYNC;",
- "state <= ST_CHECK;",
- text,
- count=1,
- )
- text = re.sub(
- r"reached_mask\s*\|\s*\(limit_sync_valid\s*\?\s*limit_sync\s*:\s*2'b00\)",
- "reached_mask | limit_sync",
- text,
- count=1,
- )
- # Remove the WAIT_SYNC case block entirely.
- text = re.sub(
- r"ST_WAIT_SYNC:\s*begin\s*motor_start\s*<=\s*1'b0;\s*if\s*\(limit_sync_valid\)\s*begin\s*state\s*<=\s*ST_CHECK;\s*end\s*end\s*",
- "",
- text,
- count=1,
- )
- return text
- class StartupMutationTests(unittest.TestCase):
- def test_removing_wait_sync_fails(self):
- """A mutant that ignores limit_sync_valid must assert motor_start early."""
- 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 [1:0] limit = 2'b00;
- reg limit_sync_valid = 1'b0;
- wire startup_done;
- wire motor_start;
- wire [1:0] motor_active_mask;
- wire [1:0] motor_dir_mask;
- reg motor_done = 1'b0;
- always #1 clk = ~clk;
- startup_close_seek #(
- .FIXED_STEPS(450),
- .DIR_SETUP_TICKS(2),
- .STEP_HALF_CYCLES(12500),
- .MAX_BURSTS(10),
- .INTER_BURST_TICKS(4),
- .DIR_MASK(2'b11)
- ) dut (
- .clk(clk), .rst_n(rst_n), .tick_500us(tick_500us),
- .limit_sync_valid(limit_sync_valid), .limit_sync(limit),
- .startup_done(startup_done), .motor_start(motor_start),
- .motor_active_mask(motor_active_mask), .motor_dir_mask(motor_dir_mask),
- .motor_done(motor_done)
- );
- integer i;
- initial begin
- repeat (3) @(posedge clk);
- rst_n = 1'b1;
- // limit_sync_valid stays 0; limit is not reached. A correct module waits
- // here; the mutant should start the motor immediately.
- 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()
|