test_startup_mutation.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 [2:0] limit_ls = 3'b000;
  46. reg [2:0] limit_hs = 3'b000;
  47. reg limit_sync_valid = 1'b0;
  48. wire startup_done;
  49. wire motor_start;
  50. wire [2:0] motor_active_mask;
  51. wire [2:0] motor_dir_mask;
  52. wire [2:0] motor_stop_mask;
  53. wire [12:0] motor_step_count;
  54. reg motor_done = 1'b0;
  55. always #1 clk = ~clk;
  56. startup_close_seek #(
  57. .CLOSE_STEPS(8),
  58. .BACKOFF_STEPS(4),
  59. .AXIS_COUNT(3)
  60. ) dut (
  61. .clk(clk), .rst_n(rst_n),
  62. .limit_sync_valid(limit_sync_valid),
  63. .close_limit(limit_ls), .open_limit(limit_hs),
  64. .startup_done(startup_done), .motor_start(motor_start),
  65. .motor_active_mask(motor_active_mask), .motor_dir_mask(motor_dir_mask),
  66. .motor_step_count(motor_step_count), .motor_stop_mask(motor_stop_mask),
  67. .motor_done(motor_done)
  68. );
  69. integer i;
  70. initial begin
  71. repeat (3) @(posedge clk);
  72. rst_n = 1'b1;
  73. for (i = 0; i < 20; i = i + 1) begin
  74. @(posedge clk);
  75. if (motor_start) begin
  76. $display("MUTATION_DETECTED");
  77. $finish;
  78. end
  79. end
  80. $fatal(1, "mutation was not detected");
  81. end
  82. endmodule
  83. """,
  84. encoding="utf-8",
  85. )
  86. vvp_out = tmp / "a.out"
  87. env = prod2u_p4_harness.tool_env()
  88. compile_result = subprocess.run(
  89. [iverilog, "-g2005-sv", "-s", "tb", "-o", str(vvp_out), str(mutated), str(tb)],
  90. cwd=tmp,
  91. capture_output=True,
  92. text=True,
  93. env=env,
  94. )
  95. self.assertEqual(compile_result.returncode, 0, compile_result.stderr)
  96. run_result = subprocess.run(
  97. [vvp, str(vvp_out)],
  98. cwd=tmp,
  99. capture_output=True,
  100. text=True,
  101. env=env,
  102. )
  103. self.assertIn("MUTATION_DETECTED", run_result.stdout)
  104. if __name__ == "__main__":
  105. unittest.main()