update at 2026-10-12 17:08:27
This commit is contained in:
Binary file not shown.
99
fiber_delay_sim/tests/test_m5_gui.py
Normal file
99
fiber_delay_sim/tests/test_m5_gui.py
Normal file
@@ -0,0 +1,99 @@
|
||||
"""M5 验收测试:GUI 六页面数据函数 + 一键导出记录包结构。
|
||||
|
||||
运行:pytest fiber_delay_sim/tests/test_m5_gui.py -v
|
||||
(无 tkinter 环境时跳过 GUI 构建部分,仅验证计算函数与导出结构。)
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from fiber_delay_sim import pipeline # noqa: E402
|
||||
from fiber_delay_sim.gui import run_page1, run_page2, run_page3, run_page4, run_page5 # noqa: E402
|
||||
|
||||
BASE = {"preset": "monte_carlo", "dt": 10.0, "window_ns": 400.0, "snr_db": 50.0,
|
||||
"ng": 1.468, "lengths": [0.0, 9.7, 19.4, 29.1], "bits": 12,
|
||||
"gain_amp": 1.0, "delay_amp": 1.0, "base_amp": 1.0, "tau": 52.8}
|
||||
|
||||
|
||||
def test_page1_phase_grid():
|
||||
"""M5 页1:光纤正向设计 → 等效网格 ~0/2.5/5/7.5 ns。"""
|
||||
r = run_page1(BASE)
|
||||
fwd = r["forward"]
|
||||
phases = sorted(fwd["sorted_phases_ns"])
|
||||
assert len(phases) == 4
|
||||
assert phases[0] < 0.01
|
||||
assert abs(phases[1] - 2.5) < 0.05
|
||||
assert abs(phases[3] - 7.5) < 0.05
|
||||
# 反向设计闭环
|
||||
inv = r["inverse"]
|
||||
assert inv["is_uniform_grid"] is True
|
||||
|
||||
|
||||
def test_page2_sampling_counts():
|
||||
r = run_page2(BASE, 42)
|
||||
assert r["n_single"] == 40 # 400 ns / 10 ns
|
||||
assert r["n_multi"] == 160 # 40 × 4 通道
|
||||
assert len(r["multi"]) == 4
|
||||
|
||||
|
||||
def test_page3_calibration_improves_continuity():
|
||||
r = run_page3(BASE, 42)
|
||||
j_raw = r["raw"]["jump"]
|
||||
j_cal = r["calibrated"]["jump"]
|
||||
assert j_cal < j_raw # 校准消除通道断层
|
||||
assert r["calibrated"]["tau"] is not None
|
||||
|
||||
|
||||
def test_page4_algorithms_consistent():
|
||||
r = run_page4(BASE, 42, snr=50)
|
||||
assert r["nls"]["tau"] is not None
|
||||
assert r["ll"]["tau"] is not None
|
||||
assert abs(r["nls"]["tau"] - 52.8) < 3.0 # NLS 接近真值
|
||||
assert abs(r["ll"]["tau"] - 52.8) < 8.0 # 对数线性估基线有偏差但可算
|
||||
|
||||
|
||||
def test_page5_scan_table():
|
||||
r = run_page5(BASE, 42, repeats=4)
|
||||
rows = r["rows"]
|
||||
assert len(rows) == 4
|
||||
for row in rows:
|
||||
assert row["single"] is not None
|
||||
assert row["fiber"] is not None
|
||||
# 4 次 MC 短窗口(100 ns)统计噪声大,只断言长窗口四相位不劣于单通道
|
||||
long_wins = [row for row in rows if row["window"] >= 300]
|
||||
assert len(long_wins) >= 2
|
||||
for row in long_wins:
|
||||
assert row["fiber"] <= row["single"] + 0.5 # 四相位(或持平)优于单通道
|
||||
|
||||
|
||||
def test_gui_export_structure(tmp_path):
|
||||
"""M5 第6页:一键导出 → 记录包结构完整(无 tkinter 也可验证)。"""
|
||||
from fiber_delay_sim.gui import GuiApp, HAS_TK
|
||||
if not HAS_TK:
|
||||
pytest.skip("无 tkinter,跳过 GUI 构建导出测试")
|
||||
import tkinter as tk
|
||||
root = tk.Tk()
|
||||
root.withdraw()
|
||||
app = GuiApp(root, workdir=str(tmp_path))
|
||||
app.params.update(BASE)
|
||||
app._run_p1()
|
||||
app._run_p5()
|
||||
app._run_p6()
|
||||
root.destroy()
|
||||
dirs = [d for d in os.listdir(tmp_path) if d.endswith("m5_gui_session")]
|
||||
assert len(dirs) == 1
|
||||
d = os.path.join(tmp_path, dirs[0])
|
||||
assert os.path.exists(os.path.join(d, "session.json"))
|
||||
assert os.path.exists(os.path.join(d, "meta.json"))
|
||||
assert os.path.exists(os.path.join(d, "page5_scan.csv"))
|
||||
figs = os.listdir(os.path.join(d, "figures"))
|
||||
assert any(f.endswith(".png") for f in figs)
|
||||
assert any(f.endswith(".svg") for f in figs)
|
||||
with open(os.path.join(d, "meta.json")) as f:
|
||||
meta = json.load(f)
|
||||
assert meta["data_origin"] == "software-simulated"
|
||||
Reference in New Issue
Block a user