| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- """V2 输出契约的离线参考校验(不是生产 PublishGate)。
- 逐条执行 ``docs/plc_interface.md`` §6A 的 12 个测试向量,打印 PASS/FAIL。
- P1 的 ``tests/test_publish_gate.py`` 与 C++ ``selftest_v2`` 必须复用同一组期望;
- 本脚本只依赖标准库,不 import portcv。
- 用法::
- python scripts/check_contract_v2.py
- """
- from __future__ import annotations
- import math
- import sys
- from typing import Dict, List, Optional, Tuple
- MIN_CONF = 0.45
- def _milli(conf: float) -> int:
- """与生产 PublishGate 一致的 half-up 千分比。"""
- if not math.isfinite(conf) or conf <= 0.0:
- return 0
- if conf >= 1.0:
- return 1000
- return int(math.floor(conf * 1000.0 + 0.5))
- def _hold(last: Optional[int]) -> Tuple[int, int, int, int, Optional[int]]:
- if last in (0, 1, 2):
- return (int(last), 0, 0, 0, int(last))
- return (0, 0, 0, 0, None)
- def gate(internal_state: int,
- conf: float,
- stable: bool,
- timeout: bool,
- last: Optional[int],
- reset: bool = False) -> Tuple[int, int, int, int, Optional[int]]:
- """参考实现:返回 (state, RESULT_VALID, STABLE, confidence, last_after)。"""
- if reset:
- return (0, 0, 0, 0, None)
- if timeout:
- return _hold(last)
- valid = (internal_state in (0, 1, 2) and stable
- and math.isfinite(conf) and conf >= MIN_CONF)
- if valid:
- return (internal_state, 1, 1, _milli(conf), internal_state)
- return _hold(last)
- #: (internal, conf, stable, timeout, last, reset, expected state, RV, ST, conf, last_after, note)
- VECTORS: List[Dict[str, object]] = [
- {"i": 1, "state": 1, "conf": 0.90, "stable": True, "timeout": False, "last": 2,
- "expect": (1, 1, 1, 900, 1), "note": "正常 1"},
- {"i": 2, "state": 2, "conf": 0.88, "stable": True, "timeout": False, "last": 1,
- "expect": (2, 1, 1, 880, 2), "note": "正常 2"},
- {"i": 3, "state": 0, "conf": 0.80, "stable": True, "timeout": False, "last": 1,
- "expect": (0, 1, 1, 800, 0), "note": "已确认 0"},
- {"i": 4, "state": 255, "conf": 0.00, "stable": False, "timeout": False, "last": 1,
- "expect": (1, 0, 0, 0, 1), "note": "保持 last=1"},
- {"i": 5, "state": 1, "conf": 0.30, "stable": False, "timeout": False, "last": 2,
- "expect": (2, 0, 0, 0, 2), "note": "低置信度保持"},
- {"i": 6, "state": 2, "conf": 0.90, "stable": False, "timeout": False, "last": 1,
- "expect": (1, 0, 0, 0, 1), "note": "未稳定保持"},
- {"i": 7, "state": 1, "conf": 0.90, "stable": True, "timeout": True, "last": 2,
- "expect": (2, 0, 0, 0, 2), "note": "超时优先"},
- {"i": 8, "state": 255, "conf": 0.00, "stable": False, "timeout": False, "last": None,
- "expect": (0, 0, 0, 0, None), "note": "冷启动 0/0"},
- {"i": 9, "state": 0, "conf": 0.90, "stable": True, "timeout": False, "last": 1,
- "expect": (0, 1, 1, 900, 0), "note": "有效切换到 0"},
- {"i": 10, "state": 3, "conf": 0.90, "stable": True, "timeout": False, "last": 1,
- "expect": (1, 0, 0, 0, 1), "note": "非法 state 保持"},
- {"i": 11, "state": 1, "conf": 0.90, "stable": True, "timeout": False, "last": None,
- "expect": (1, 1, 1, 900, 1), "note": "冷启动后首个有效"},
- {"i": 12, "state": 255, "conf": 0.00, "stable": False, "timeout": False, "last": 2,
- "reset": True, "expect": (0, 0, 0, 0, None), "note": "cmd=2 复位"},
- ]
- def main() -> int:
- failures = 0
- for vec in VECTORS:
- got = gate(int(vec["state"]), float(vec["conf"]), bool(vec["stable"]),
- bool(vec["timeout"]), vec["last"], bool(vec.get("reset", False)))
- expected = tuple(vec["expect"])
- ok = got == expected
- failures += 0 if ok else 1
- print("[%s] #%-2s %-20s got=%s expect=%s"
- % ("PASS" if ok else "FAIL", vec["i"], vec["note"], got, expected))
- print("----")
- print("%d vectors, %d failed" % (len(VECTORS), failures))
- return 0 if failures == 0 else 1
- if __name__ == "__main__":
- sys.exit(main())
|