81 lines
2.4 KiB
Python
81 lines
2.4 KiB
Python
"""一致性监控测试(规格 §6.7/§11.4:N 帧降级、M 帧判 invalid、正常帧清零、每目标隔离)。"""
|
|
|
|
import pytest
|
|
|
|
from stereo_ranger import ConsistencyMonitor
|
|
|
|
|
|
def _monitor() -> ConsistencyMonitor:
|
|
return ConsistencyMonitor(jump_px=3.0, degrade_frames=3, invalid_frames=5)
|
|
|
|
|
|
def test_first_frame_has_no_reference_jump():
|
|
status = _monitor().advance("t", 10.0)
|
|
assert status.jump_px is None
|
|
assert status.abnormal is False
|
|
assert status.streak == 0
|
|
|
|
|
|
def test_normal_jump_resets_streak():
|
|
monitor = _monitor()
|
|
monitor.advance("t", 10.0)
|
|
abnormal = monitor.advance("t", 16.0)
|
|
assert abnormal.abnormal and abnormal.streak == 1
|
|
normal = monitor.advance("t", 17.0)
|
|
assert not normal.abnormal and normal.streak == 0
|
|
|
|
|
|
def test_jump_at_threshold_is_normal():
|
|
monitor = _monitor()
|
|
monitor.advance("t", 10.0)
|
|
assert not monitor.advance("t", 13.0).abnormal # |ΔD| = 3.0 不超门限
|
|
|
|
|
|
def test_degrades_at_n_frames_and_invalidates_at_m_frames():
|
|
monitor = _monitor()
|
|
monitor.advance("t", 10.0)
|
|
disparities = [14.0, 18.0, 22.0, 26.0, 30.0] # 每帧跳变 4 px > 3 px
|
|
statuses = [monitor.advance("t", d) for d in disparities]
|
|
assert [s.streak for s in statuses] == [1, 2, 3, 4, 5]
|
|
assert [s.confidence_degraded for s in statuses] == [False, False, True, True, True]
|
|
assert [s.invalid for s in statuses] == [False, False, False, False, True]
|
|
|
|
|
|
def test_targets_are_isolated():
|
|
monitor = _monitor()
|
|
monitor.advance("a", 10.0)
|
|
monitor.advance("b", 20.0)
|
|
monitor.advance("a", 15.0) # a 跳变异常
|
|
assert monitor.streak("a") == 1
|
|
assert monitor.streak("b") == 0
|
|
|
|
|
|
def test_drop_clears_only_target():
|
|
monitor = _monitor()
|
|
monitor.advance("a", 10.0)
|
|
monitor.advance("a", 20.0)
|
|
monitor.drop("a")
|
|
assert monitor.streak("a") == 0
|
|
assert monitor.advance("a", 40.0).jump_px is None # 状态已清空,重新起始
|
|
|
|
|
|
def test_clear_resets_all():
|
|
monitor = _monitor()
|
|
monitor.advance("a", 10.0)
|
|
monitor.advance("a", 20.0)
|
|
monitor.clear()
|
|
assert monitor.streak("a") == 0
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"kwargs",
|
|
[
|
|
{"jump_px": 0.0, "degrade_frames": 3, "invalid_frames": 5},
|
|
{"jump_px": 3.0, "degrade_frames": 0, "invalid_frames": 5},
|
|
{"jump_px": 3.0, "degrade_frames": 5, "invalid_frames": 3},
|
|
],
|
|
)
|
|
def test_invalid_ctor_args(kwargs):
|
|
with pytest.raises(ValueError):
|
|
ConsistencyMonitor(**kwargs)
|