test_startup_mutation.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. wire [1:0] motor_stop_mask;
  64. reg motor_done = 1'b0;
  65. always #1 clk = ~clk;
  66. startup_close_seek #(
  67. .FIXED_STEPS(450),
  68. .DIR_SETUP_TICKS(2),
  69. .STEP_HALF_CYCLES(12500),
  70. .MAX_BURSTS(10),
  71. .INTER_BURST_TICKS(4),
  72. .DIR_MASK(2'b11)
  73. ) dut (
  74. .clk(clk), .rst_n(rst_n), .tick_500us(tick_500us),
  75. .limit_sync_valid(limit_sync_valid), .limit_sync(limit),
  76. .startup_done(startup_done), .motor_start(motor_start),
  77. .motor_active_mask(motor_active_mask), .motor_dir_mask(motor_dir_mask),
  78. .motor_stop_mask(motor_stop_mask),
  79. .motor_done(motor_done)
  80. );
  81. integer i;
  82. initial begin
  83. repeat (3) @(posedge clk);
  84. rst_n = 1'b1;
  85. // limit_sync_valid stays 0; limit is not reached. A correct module waits
  86. // here; the mutant should start the motor immediately.
  87. for (i = 0; i < 20; i = i + 1) begin
  88. @(posedge clk);
  89. if (motor_start) begin
  90. $display("MUTATION_DETECTED");
  91. $finish;
  92. end
  93. end
  94. $fatal(1, "mutation was not detected");
  95. end
  96. endmodule
  97. """,
  98. encoding="utf-8",
  99. )
  100. vvp_out = tmp / "a.out"
  101. env = prod2u_p4_harness.tool_env()
  102. compile_result = subprocess.run(
  103. [iverilog, "-g2005-sv", "-s", "tb", "-o", str(vvp_out), str(mutated), str(tb)],
  104. cwd=tmp,
  105. capture_output=True,
  106. text=True,
  107. env=env,
  108. )
  109. self.assertEqual(compile_result.returncode, 0, compile_result.stderr)
  110. run_result = subprocess.run([vvp, str(vvp_out)], cwd=tmp, capture_output=True, text=True, env=env)
  111. self.assertIn("MUTATION_DETECTED", run_result.stdout)
  112. if __name__ == "__main__":
  113. unittest.main()