77 lines
2.4 KiB
Python
77 lines
2.4 KiB
Python
"""StereoConfig 默认值、派生区间与 JSON 加载测试(规格 §2:参数全部外置)。"""
|
||
|
||
import json
|
||
|
||
import pytest
|
||
|
||
from stereo_ranger.config import StereoConfig, load_from_json
|
||
|
||
VALID = {
|
||
"fx_px": 1663.0, "fy_px": 1663.0, "cx_px": 960.0, "cy_px": 540.0,
|
||
"baseline_m": 0.12, "z_min_m": 2.0, "z_max_m": 40.0,
|
||
"vertical_tau_px": 3.0, "height_ratio_tau": 0.3,
|
||
"disparity_min_px": 4.0, "range_tol_m": 3.0, "disp_noise_px": 1.5,
|
||
"use_center_fallback": False, "filter_enabled": True,
|
||
"filter_alpha": 0.5, "filter_jump_ratio": 0.1, "warn_interval_s": 1.0,
|
||
}
|
||
|
||
|
||
def test_defaults_and_disparity_window():
|
||
cfg = StereoConfig()
|
||
assert cfg.baseline_m == 0.06 and cfg.fx_px == 960.0
|
||
# FB = 57.6,z 窗口 [1, 50] m -> D 窗口 [57.6/50, 57.6/1]
|
||
lo, hi = cfg.disparity_bounds_px()
|
||
assert lo == pytest.approx(1.152)
|
||
assert hi == pytest.approx(57.6)
|
||
|
||
|
||
@pytest.mark.parametrize("kwargs", [
|
||
{"baseline_m": 0.0},
|
||
{"fx_px": -1.0},
|
||
{"fy_px": 0.0},
|
||
{"cx_px": -1.0},
|
||
{"z_min_m": 0.0},
|
||
{"z_max_m": 10.0, "z_min_m": 20.0},
|
||
{"vertical_tau_px": 0.0},
|
||
{"height_ratio_tau": 0.0},
|
||
{"disparity_min_px": 0.0},
|
||
{"range_tol_m": 0.0},
|
||
{"disp_noise_px": 0.0},
|
||
{"filter_alpha": 0.0},
|
||
{"filter_alpha": 1.5},
|
||
{"filter_jump_ratio": -0.1},
|
||
{"warn_interval_s": -1.0},
|
||
])
|
||
def test_invalid_config_raises(kwargs):
|
||
with pytest.raises(ValueError):
|
||
StereoConfig(**kwargs)
|
||
|
||
|
||
def test_load_from_json_roundtrip(tmp_path):
|
||
p = tmp_path / "stereo.json"
|
||
p.write_text(json.dumps(VALID), encoding="utf-8")
|
||
cfg = load_from_json(str(p))
|
||
for field, value in VALID.items():
|
||
assert getattr(cfg, field) == value
|
||
lo, hi = cfg.disparity_bounds_px()
|
||
assert hi == pytest.approx(1663.0 * 0.12 / 2.0) # FB / z_min
|
||
assert lo == pytest.approx(1663.0 * 0.12 / 40.0) # FB / z_max
|
||
|
||
|
||
def test_load_from_json_missing_field_reports_name(tmp_path):
|
||
p = tmp_path / "stereo.json"
|
||
data = dict(VALID)
|
||
del data["baseline_m"]
|
||
p.write_text(json.dumps(data), encoding="utf-8")
|
||
with pytest.raises(ValueError, match="baseline_m"):
|
||
load_from_json(str(p))
|
||
|
||
|
||
def test_load_from_json_unknown_field_rejected(tmp_path):
|
||
p = tmp_path / "stereo.json"
|
||
data = dict(VALID)
|
||
data["magic_extra"] = 1
|
||
p.write_text(json.dumps(data), encoding="utf-8")
|
||
with pytest.raises(ValueError, match="magic_extra"):
|
||
load_from_json(str(p))
|