test_startup_mutation.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. """Mutation test: removing WAIT_SYNC must cause startup_close_seek to fail."""
  2. from __future__ import annotations
  3. import re
  4. import subprocess
  5. import sys
  6. import tempfile
  7. import unittest
  8. from pathlib import Path
  9. ROOT = Path(__file__).resolve().parents[2]
  10. SOURCE = ROOT / "src" / "modules" / "startup_close_seek.v"
  11. # Import harness tooling for toolchain discovery.
  12. sys.path.insert(0, str(ROOT / "tools"))
  13. import prod2u_p4_harness # type: ignore
  14. def mutated_source() -> str:
  15. text = SOURCE.read_text(encoding="utf-8")
  16. # Make the module ignore limit_sync_valid by removing the WAIT_SYNC state and
  17. # the valid gating on reached_mask_next.
  18. text = re.sub(
  19. r"state\s*<=\s*ST_WAIT_SYNC;",
  20. "state <= ST_CHECK;",
  21. text,
  22. count=1,
  23. )
  24. text = re.sub(
  25. r"reached_mask\s*\|\s*\(limit_sync_valid\s*\?\s*limit_sync\s*:\s*2'b00\)",
  26. "reached_mask | limit_sync",
  27. text,
  28. count=1,
  29. )
  30. # Remove the WAIT_SYNC case block entirely.
  31. text = re.sub(
  32. 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*",
  33. "",
  34. text,
  35. count=1,
  36. )
  37. return text
  38. class StartupMutationTests(unittest.TestCase):
  39. def test_removing_wait_sync_fails(self):
  40. """A mutant that ignores limit_sync_valid must assert motor_start early."""
  41. iverilog = prod2u_p4_harness.resolve_tool("iverilog")
  42. vvp = prod2u_p4_harness.resolve_tool("vvp")
  43. if not iverilog or not vvp:
  44. self.skipTest("iverilog/vvp not available")
  45. with tempfile.TemporaryDirectory() as d:
  46. tmp = Path(d)
  47. mutated = tmp / "startup_close_seek_mut.v"
  48. mutated.write_text(mutated_source(), encoding="utf-8")
  49. tb = tmp / "tb.v"
  50. tb.write_text(
  51. """
  52. `timescale 1ns/1ps
  53. module tb;
  54. reg clk = 1'b0;
  55. reg rst_n = 1'b0;
  56. reg tick_500us = 1'b0;
  57. reg [1:0] limit = 2'b00;
  58. reg limit_sync_valid = 1'b0;
  59. wire startup_done;
  60. wire motor_start;
  61. wire [1:0] motor_active_mask;
  62. wire [1:0] motor_dir_mask;
  63. reg motor_done = 1'b0;
  64. always #1 clk = ~clk;
  65. startup_close_seek #(
  66. .FIXED_STEPS(450),
  67. .DIR_SETUP_TICKS(2),
  68. .STEP_HALF_CYCLES(12500),
  69. .MAX_BURSTS(10),
  70. .INTER_BURST_TICKS(4),
  71. .DIR_MASK(2'b11)
  72. ) dut (
  73. .clk(clk), .rst_n(rst_n), .tick_500us(tick_500us),
  74. .limit_sync_valid(limit_sync_valid), .limit_sync(limit),
  75. .startup_done(startup_done), .motor_start(motor_start),
  76. .motor_active_mask(motor_active_mask), .motor_dir_mask(motor_dir_mask),
  77. .motor_done(motor_done)
  78. );
  79. integer i;
  80. initial begin
  81. repeat (3) @(posedge clk);
  82. rst_n = 1'b1;
  83. // limit_sync_valid stays 0; limit is not reached. A correct module waits
  84. // here; the mutant should start the motor immediately.
  85. for (i = 0; i < 20; i = i + 1) begin
  86. @(posedge clk);
  87. if (motor_start) begin
  88. $display("MUTATION_DETECTED");
  89. $finish;
  90. end
  91. end
  92. $fatal(1, "mutation was not detected");
  93. end
  94. endmodule
  95. """,
  96. encoding="utf-8",
  97. )
  98. vvp_out = tmp / "a.out"
  99. env = prod2u_p4_harness.tool_env()
  100. compile_result = subprocess.run(
  101. [iverilog, "-g2005-sv", "-s", "tb", "-o", str(vvp_out), str(mutated), str(tb)],
  102. cwd=tmp,
  103. capture_output=True,
  104. text=True,
  105. env=env,
  106. )
  107. self.assertEqual(compile_result.returncode, 0, compile_result.stderr)
  108. run_result = subprocess.run([vvp, str(vvp_out)], cwd=tmp, capture_output=True, text=True, env=env)
  109. self.assertIn("MUTATION_DETECTED", run_result.stdout)
  110. if __name__ == "__main__":
  111. unittest.main()