test_startup_mutation.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. sys.path.insert(0, str(ROOT / "tools"))
  12. import prod2u_p4_harness # type: ignore
  13. def mutated_source() -> str:
  14. text = SOURCE.read_text(encoding="utf-8")
  15. text = re.sub(
  16. r"if \(limit_sync_valid\)\s*state\s*<=\s*ST_CHECK;",
  17. "state <= ST_CHECK;",
  18. text,
  19. count=1,
  20. )
  21. text = re.sub(
  22. 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*",
  23. "",
  24. text,
  25. count=1,
  26. )
  27. return text
  28. class StartupMutationTests(unittest.TestCase):
  29. def test_removing_wait_sync_fails(self):
  30. iverilog = prod2u_p4_harness.resolve_tool("iverilog")
  31. vvp = prod2u_p4_harness.resolve_tool("vvp")
  32. if not iverilog or not vvp:
  33. self.skipTest("iverilog/vvp not available")
  34. with tempfile.TemporaryDirectory() as d:
  35. tmp = Path(d)
  36. mutated = tmp / "startup_close_seek_mut.v"
  37. mutated.write_text(mutated_source(), encoding="utf-8")
  38. tb = tmp / "tb.v"
  39. tb.write_text(
  40. """
  41. `timescale 1ns/1ps
  42. module tb;
  43. reg clk = 1'b0;
  44. reg rst_n = 1'b0;
  45. reg tick_500us = 1'b0;
  46. reg [2:0] limit_ls = 3'b000;
  47. reg [2:0] limit_hs = 3'b000;
  48. reg limit_sync_valid = 1'b0;
  49. wire startup_done;
  50. wire motor_start;
  51. wire [2:0] motor_active_mask;
  52. wire [2:0] motor_dir_mask;
  53. wire [2:0] motor_stop_mask;
  54. wire [12:0] motor_step_count;
  55. reg motor_done = 1'b0;
  56. always #1 clk = ~clk;
  57. startup_close_seek #(
  58. .CLOSE_STEPS(8),
  59. .BACKOFF_STEPS(4),
  60. .AXIS_COUNT(3)
  61. ) dut (
  62. .clk(clk), .rst_n(rst_n), .tick_500us(tick_500us),
  63. .limit_sync_valid(limit_sync_valid),
  64. .limit_ls_sync(limit_ls), .limit_hs_sync(limit_hs),
  65. .startup_done(startup_done), .motor_start(motor_start),
  66. .motor_active_mask(motor_active_mask), .motor_dir_mask(motor_dir_mask),
  67. .motor_step_count(motor_step_count), .motor_stop_mask(motor_stop_mask),
  68. .motor_done(motor_done)
  69. );
  70. integer i;
  71. initial begin
  72. repeat (3) @(posedge clk);
  73. rst_n = 1'b1;
  74. for (i = 0; i < 20; i = i + 1) begin
  75. @(posedge clk);
  76. if (motor_start) begin
  77. $display("MUTATION_DETECTED");
  78. $finish;
  79. end
  80. end
  81. $fatal(1, "mutation was not detected");
  82. end
  83. endmodule
  84. """,
  85. encoding="utf-8",
  86. )
  87. vvp_out = tmp / "a.out"
  88. env = prod2u_p4_harness.tool_env()
  89. compile_result = subprocess.run(
  90. [iverilog, "-g2005-sv", "-s", "tb", "-o", str(vvp_out), str(mutated), str(tb)],
  91. cwd=tmp,
  92. capture_output=True,
  93. text=True,
  94. env=env,
  95. )
  96. self.assertEqual(compile_result.returncode, 0, compile_result.stderr)
  97. run_result = subprocess.run(
  98. [vvp, str(vvp_out)],
  99. cwd=tmp,
  100. capture_output=True,
  101. text=True,
  102. env=env,
  103. )
  104. self.assertIn("MUTATION_DETECTED", run_result.stdout)
  105. if __name__ == "__main__":
  106. unittest.main()