test(spatial): 冲突检测加阳性对照——删掉一条网的铜,看它认不认

This commit is contained in:
liuhuajie committed 2026-12-04 00:48:34 +08:00
1 parent 1d179c2506
commit d296acd34e
1 file changed
+63 -2
+63 -2
View File
@@ -46,6 +46,18 @@ def _ratsnest(board, code: int) -> int:
return -1 return -1
def _unrouted(board) -> tuple:
"""(nets with a break, total missing copper connections) over the whole board."""
codes = sorted({p.GetNetCode() for p in _pads(board) if p.GetNetCode() != 0})
bad = pairs = 0
for code in codes:
n = _ratsnest(board, code)
if n > 0:
bad += 1
pairs += n
return bad, pairs
def report(paths) -> None: def report(paths) -> None:
try: try:
import pcbnew import pcbnew
@@ -115,14 +127,63 @@ def coverage(board_path: str, netlist_path: str) -> None:
print("它正好是 scope 契约里'清单比交付多'的实物版本:网表交出的东西比板子少。") print("它正好是 scope 契约里'清单比交付多'的实物版本:网表交出的东西比板子少。")
def control(board_path: str) -> None:
"""Positive control: cut one net's copper and check the detector notices.
Every demo board is fully routed, so the disagreement count is zero -- and a
metric that always returns zero has not been tested. This removes the tracks
of one small net and shows the count go up. If it does not, the detector is
reading the wrong thing and every "0" above is meaningless.
"""
try:
import pcbnew
except Exception:
_die("pcbnew 不可用")
return
board = pcbnew.LoadBoard(board_path)
try:
board.BuildConnectivity()
except Exception:
pass
before = _unrouted(board)
per_net = {}
for t in board.GetTracks():
code = t.GetNetCode()
if code:
per_net.setdefault(code, []).append(t)
if not per_net:
print("no tracks on this board")
return
code = min(per_net, key=lambda c: (len(per_net[c]), c))
net = board.FindNet(code)
name = net.GetNetname() if net else str(code)
cut = len(per_net[code])
for t in per_net[code]:
board.Remove(t)
board.BuildConnectivity()
after = _unrouted(board)
print(f"control on {board_path.rsplit('/', 1)[-1]}")
print(f" cut net {name!r}: removed {cut} track(s)")
print(f" before: {before[0]} nets broken, {before[1]} missing connections")
print(f" after : {after[0]} nets broken, {after[1]} missing connections")
ok = after[1] > before[1]
print(f" {'PASS' if ok else 'FAIL'} the detector "
f"{'notices' if ok else 'did NOT notice'} the cut")
def main(argv=None) -> int: def main(argv=None) -> int:
argv = list(sys.argv[1:] if argv is None else argv) argv = list(sys.argv[1:] if argv is None else argv)
if len(argv) == 2: if argv and argv[0] == "control":
control(argv[1] if len(argv) > 1 else "")
elif len(argv) == 2:
coverage(argv[0], argv[1]) coverage(argv[0], argv[1])
elif argv: elif argv:
report(argv) report(argv)
else: else:
print("usage: kicad_board <a.kicad_pcb> ... | kicad_board <a.kicad_pcb> <a.net>") print("usage: kicad_board <a.kicad_pcb> ... | kicad_board <pcb> <net> | "
"kicad_board control <pcb>")
return 2 return 2
return 0 return 0