check_contract_v2.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. """V2 输出契约的离线参考校验(不是生产 PublishGate)。
  2. 逐条执行 ``docs/plc_interface.md`` §6A 的 12 个测试向量,打印 PASS/FAIL。
  3. P1 的 ``tests/test_publish_gate.py`` 与 C++ ``selftest_v2`` 必须复用同一组期望;
  4. 本脚本只依赖标准库,不 import portcv。
  5. 用法::
  6. python scripts/check_contract_v2.py
  7. """
  8. from __future__ import annotations
  9. import math
  10. import sys
  11. from typing import Dict, List, Optional, Tuple
  12. MIN_CONF = 0.45
  13. def _milli(conf: float) -> int:
  14. """与生产 PublishGate 一致的 half-up 千分比。"""
  15. if not math.isfinite(conf) or conf <= 0.0:
  16. return 0
  17. if conf >= 1.0:
  18. return 1000
  19. return int(math.floor(conf * 1000.0 + 0.5))
  20. def _hold(last: Optional[int]) -> Tuple[int, int, int, int, Optional[int]]:
  21. if last in (0, 1, 2, 3):
  22. return (int(last), 0, 0, 0, int(last))
  23. return (0, 0, 0, 0, None)
  24. def gate(internal_state: int,
  25. conf: float,
  26. stable: bool,
  27. timeout: bool,
  28. last: Optional[int],
  29. reset: bool = False) -> Tuple[int, int, int, int, Optional[int]]:
  30. """参考实现:返回 (state, RESULT_VALID, STABLE, confidence, last_after)。"""
  31. if reset:
  32. return (0, 0, 0, 0, None)
  33. if timeout:
  34. return _hold(last)
  35. valid = (internal_state in (0, 1, 2, 3) and stable
  36. and math.isfinite(conf) and conf >= MIN_CONF)
  37. if valid:
  38. return (internal_state, 1, 1, _milli(conf), internal_state)
  39. return _hold(last)
  40. #: (internal, conf, stable, timeout, last, reset, expected state, RV, ST, conf, last_after, note)
  41. VECTORS: List[Dict[str, object]] = [
  42. {"i": 1, "state": 1, "conf": 0.90, "stable": True, "timeout": False, "last": 2,
  43. "expect": (1, 1, 1, 900, 1), "note": "正常 1"},
  44. {"i": 2, "state": 2, "conf": 0.88, "stable": True, "timeout": False, "last": 1,
  45. "expect": (2, 1, 1, 880, 2), "note": "正常 2"},
  46. {"i": 3, "state": 0, "conf": 0.80, "stable": True, "timeout": False, "last": 1,
  47. "expect": (0, 1, 1, 800, 0), "note": "已确认 0"},
  48. {"i": 4, "state": 255, "conf": 0.00, "stable": False, "timeout": False, "last": 1,
  49. "expect": (1, 0, 0, 0, 1), "note": "保持 last=1"},
  50. {"i": 5, "state": 1, "conf": 0.30, "stable": False, "timeout": False, "last": 2,
  51. "expect": (2, 0, 0, 0, 2), "note": "低置信度保持"},
  52. {"i": 6, "state": 2, "conf": 0.90, "stable": False, "timeout": False, "last": 1,
  53. "expect": (1, 0, 0, 0, 1), "note": "未稳定保持"},
  54. {"i": 7, "state": 1, "conf": 0.90, "stable": True, "timeout": True, "last": 2,
  55. "expect": (2, 0, 0, 0, 2), "note": "超时优先"},
  56. {"i": 8, "state": 255, "conf": 0.00, "stable": False, "timeout": False, "last": None,
  57. "expect": (0, 0, 0, 0, None), "note": "冷启动 0/0"},
  58. {"i": 9, "state": 0, "conf": 0.90, "stable": True, "timeout": False, "last": 1,
  59. "expect": (0, 1, 1, 900, 0), "note": "有效切换到 0"},
  60. {"i": 10, "state": 4, "conf": 0.90, "stable": True, "timeout": False, "last": 1,
  61. "expect": (1, 0, 0, 0, 1), "note": "非法 state=4 保持"},
  62. {"i": 11, "state": 1, "conf": 0.90, "stable": True, "timeout": False, "last": None,
  63. "expect": (1, 1, 1, 900, 1), "note": "冷启动后首个有效"},
  64. {"i": 12, "state": 255, "conf": 0.00, "stable": False, "timeout": False, "last": 2,
  65. "reset": True, "expect": (0, 0, 0, 0, None), "note": "cmd=2 复位"},
  66. {"i": 13, "state": 3, "conf": 0.90, "stable": True, "timeout": False, "last": 1,
  67. "expect": (3, 1, 1, 900, 3), "note": "P0-Q9 空车 3"},
  68. {"i": 14, "state": 3, "conf": 0.30, "stable": False, "timeout": False, "last": 2,
  69. "expect": (2, 0, 0, 0, 2), "note": "空车 3 低置信度保持"},
  70. ]
  71. def main() -> int:
  72. failures = 0
  73. for vec in VECTORS:
  74. got = gate(int(vec["state"]), float(vec["conf"]), bool(vec["stable"]),
  75. bool(vec["timeout"]), vec["last"], bool(vec.get("reset", False)))
  76. expected = tuple(vec["expect"])
  77. ok = got == expected
  78. failures += 0 if ok else 1
  79. print("[%s] #%-2s %-20s got=%s expect=%s"
  80. % ("PASS" if ok else "FAIL", vec["i"], vec["note"], got, expected))
  81. print("----")
  82. print("%d vectors, %d failed" % (len(VECTORS), failures))
  83. return 0 if failures == 0 else 1
  84. if __name__ == "__main__":
  85. sys.exit(main())