| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- // portcv 标定记录自检(P0-5)—— 覆盖"记录能不能被 C++ 正确判定"这条链路。
- //
- // 运行:
- // ctest --test-dir build -R portcv_calib_selftest --output-on-failure
- // ./build/portcv_calib_selftest
- //
- // 覆盖(与实现文件对应):
- // [1] sha256:FIPS 180-4 标准向量(空串 / "abc" / 56 字节句子)
- // [2] canonical JSON:键排序、int/float 区分、浮点渲染、控制符转义、顶层 sha256 剔除
- // —— 断言值与 `json.dumps(..., ensure_ascii=False, sort_keys=True,
- // separators=(",", ":"))` 逐字相同(黄金向量由 Python 生成)
- // [3] 跨语言黄金记录:Python `seal()` 封存的记录 → C++ `verify_hash()` 必须为真;
- // 改一个数必须为假(这条断言就是"两侧口径一致"的证据)
- // [4] ISO-8601:时区 / Z / 纯日期 / 小数秒 / 非法值
- // [5] 判定:过期 / 未复核 / 哈希不符 / 文件缺失 / JSON 损坏 / 字段缺失 /
- // 比例尺漂移 / 箱高漂移 / 手工 stale / 未来日期不算过期
- // [6] 文件 I/O:真实读盘(写临时记录 → evaluate_calibration → 删除)
- // [7] 配置绑定:`calibration.*` 键 → CalibrationConfig;类型错/越界报错并指出键名;
- // 默认值与 Python `DEFAULTS` 同值
- //
- // 说明:断言描述用 ASCII(GBK 控制台下中文会乱码,影响现场排错),源码注释保持中文。
- // 记录文本里的中文一律写成 JSON `\uXXXX` 转义:源码保持纯 ASCII,编译器的
- // 源码/执行字符集怎么设都不会影响哈希结果(哈希只认 UTF-8 字节)。
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <string>
- #include <vector>
- #include "portcv/app_config.hpp"
- #include "portcv/calibration.hpp"
- #include "portcv/json_lite.hpp"
- #include "portcv/sha256.hpp"
- namespace {
- int g_pass = 0;
- int g_fail = 0;
- void check(bool ok, const std::string& what) {
- if (ok) {
- ++g_pass;
- std::printf(" [OK] %s\n", what.c_str());
- } else {
- ++g_fail;
- std::printf(" [FAIL] %s\n", what.c_str());
- }
- }
- void check_eq(long long got, long long want, const std::string& what) {
- if (got == want) {
- ++g_pass;
- std::printf(" [OK] %s (=%lld)\n", what.c_str(), got);
- } else {
- ++g_fail;
- std::printf(" [FAIL] %s: got=%lld want=%lld\n", what.c_str(), got, want);
- }
- }
- void check_str(const std::string& got, const std::string& want, const std::string& what) {
- if (got == want) {
- ++g_pass;
- std::printf(" [OK] %s (%s)\n", what.c_str(), got.c_str());
- } else {
- ++g_fail;
- std::printf(" [FAIL] %s: got=%s want=%s\n", what.c_str(), got.c_str(), want.c_str());
- }
- }
- bool has_reason(const portcv::CalibrationStatus& s, const char* reason) {
- for (const std::string& r : s.reasons) {
- if (r == reason) { return true; }
- }
- return false;
- }
- /// 2026-10-26T10:00:00+08:00 的 UTC epoch 秒(= 2026-10-26T02:00:00Z)。
- const long long kCalibratedAt = 1792980000LL;
- const long long kDay = 86400LL;
- // ---------------------------------------------------------------- 黄金向量
- // 由 Python 生成(`portcv.calibration.seal()`):记录文本 + 期望 sha256。
- // 只要两侧对"整数/浮点渲染、键排序、UTF-8 输出"有任何一处不一致,这里就会失败。
- const char* kGoldenRecord1 =
- "{"
- "\"schema\":\"portcv.calib/1\","
- "\"site\":\"CT01\","
- "\"camera_id\":\"front\","
- "\"calibrated_at\":\"2026-10-26T10:00:00+08:00\","
- "\"reviewer\":\"\\u5f20\\u5de5\","
- "\"method\":\"box_vertical_scale\","
- "\"px_per_m\":115.6,"
- "\"box_height_m\":2.591,"
- "\"intrinsics\":{\"fx\":1200.0,\"fy\":1200.0,\"cx\":960.0,\"cy\":540.0,\"dist\":[]},"
- "\"extrinsics\":{\"height_m\":6.5,\"pitch_deg\":-8.0,\"yaw_deg\":2.0},"
- "\"roi\":[[0.03,0.1],[0.97,0.1],[0.97,0.99],[0.03,0.99]],"
- "\"sha256\":\"735ab9e8f9e51abe32b17f0abe1976dd35b4a3d3a14aee5ad43674104211df82\""
- "}";
- const char* kGoldenHash1 = "735ab9e8f9e51abe32b17f0abe1976dd35b4a3d3a14aee5ad43674104211df82";
- /// 边界类型:整数 / 浮点 / 指数 / 控制符 / 布尔 / null / 非 ASCII 与非 ASCII 键。
- const char* kGoldenRecord2 =
- "{"
- "\"site\":\"S\","
- "\"camera_id\":\"c\","
- "\"calibrated_at\":\"2026-01-02\","
- "\"reviewer\":\"a\\nb\\tc\\\"d\\\\e\","
- "\"n_int\":180,"
- "\"n_float\":180.0,"
- "\"big\":1e+16,"
- "\"mid\":1000000000000000.0,"
- "\"small\":1e-05,"
- "\"tiny\":0.0001,"
- "\"neg_zero\":-0.0,"
- "\"prec\":1.2345678901234568e-300,"
- "\"flag\":true,"
- "\"none\":null,"
- "\"text\":\"\\u4e2d\\u6587 \\u00e9\","
- "\"\\u4e2d\":1,"
- "\"a\":2,"
- "\"arr\":[1,2.5,[]]"
- "}";
- const char* kGoldenHash2 = "b46564ddae4441f475f244a93e0be1bde69fc14bd253d926852a3f4c987117b7";
- portcv::json::Value parse_or_null(const std::string& text) {
- std::string error;
- const portcv::json::Value v = portcv::json::parse(text, &error);
- if (!error.empty()) { std::printf(" (parse error: %s)\n", error.c_str()); }
- return v;
- }
- void test_sha256() {
- std::printf("[1] sha256 (FIPS 180-4)\n");
- check_str(portcv::sha256_hex(""),
- "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
- "empty string");
- check_str(portcv::sha256_hex("abc"),
- "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", "\"abc\"");
- check_str(portcv::sha256_hex("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"),
- "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1",
- "56-byte sentence (2 blocks)");
- check_eq(static_cast<long long>(portcv::sha256_hex("abc").size()), 64, "hexdigest length");
- }
- void test_canonical() {
- std::printf("[2] canonical JSON == json.dumps(ensure_ascii=False, sort_keys=True, separators=(',',':'))\n");
- // 键排序(书写顺序打乱)
- check_str(portcv::canonical_json(parse_or_null("{\"b\":1,\"a\":\"x\"}")), "{\"a\":\"x\",\"b\":1}",
- "sort_keys");
- // 整数/浮点区分(Python: 180 vs 180.0)
- check_str(portcv::canonical_json(parse_or_null("{\"v\":180}")), "{\"v\":180}", "int literal stays int");
- check_str(portcv::canonical_json(parse_or_null("{\"v\":180.0}")), "{\"v\":180.0}",
- "float literal keeps .0");
- // 浮点渲染(Python repr 口径:1e15 定点、1e16 科学计数、指数至少两位)
- check_str(portcv::canonical_json(parse_or_null("{\"v\":1e15}")), "{\"v\":1000000000000000.0}",
- "1e15 -> fixed form");
- check_str(portcv::canonical_json(parse_or_null("{\"v\":1e16}")), "{\"v\":1e+16}", "1e16 -> exponent form");
- check_str(portcv::canonical_json(parse_or_null("{\"v\":0.0001}")), "{\"v\":0.0001}", "1e-4 stays fixed");
- check_str(portcv::canonical_json(parse_or_null("{\"v\":1e-5}")), "{\"v\":1e-05}", "1e-5 -> 2-digit exponent");
- check_str(portcv::canonical_json(parse_or_null("{\"v\":-0.0}")), "{\"v\":-0.0}", "negative zero");
- check_str(portcv::canonical_json(parse_or_null("{\"v\":5e-324}")), "{\"v\":5e-324}", "denormal min");
- check_str(portcv::canonical_json(parse_or_null("{\"v\":1.7976931348623157e308}")),
- "{\"v\":1.7976931348623157e+308}", "double max");
- // 整数前导 0 / "-0"
- check_str(portcv::canonical_json(parse_or_null("{\"v\":-0}")), "{\"v\":0}", "int -0 -> 0");
- // 字符串转义(控制符 + 引号 + 反斜杠;非 ASCII 原样)
- check_str(portcv::canonical_json(parse_or_null("{\"v\":\"a\\nb\\tc\\\"d\\\\e\"}")),
- "{\"v\":\"a\\nb\\tc\\\"d\\\\e\"}", "control/quote/backslash escapes");
- check_str(portcv::canonical_json(parse_or_null("{\"v\":\"\\u0001\"}")), "{\"v\":\"\\u0001\"}",
- "other control chars -> \\u00XX");
- // 顶层 sha256 剔除、嵌套 sha256 保留
- check_str(portcv::canonical_json(parse_or_null("{\"sha256\":\"dead\",\"a\":1}")), "{\"a\":1}",
- "top-level sha256 dropped");
- check_str(portcv::canonical_json(parse_or_null("{\"a\":{\"sha256\":\"x\"}}")),
- "{\"a\":{\"sha256\":\"x\"}}", "nested sha256 kept");
- // 数组/空容器
- check_str(portcv::canonical_json(parse_or_null("{\"a\":[],\"b\":{}}")), "{\"a\":[],\"b\":{}}",
- "empty containers");
- // 空对象
- check_str(portcv::canonical_json(parse_or_null("{}")), "{}", "empty object");
- }
- void test_golden_records() {
- std::printf("[3] cross-language golden records (sealed by Python)\n");
- const portcv::json::Value rec1 = parse_or_null(kGoldenRecord1);
- check(rec1.is_object(), "golden record 1 parses");
- check_str(portcv::compute_hash(rec1), kGoldenHash1, "record 1 sha256 matches Python");
- check(portcv::verify_hash(rec1), "record 1 hash verifies");
- // 改一个数(其余字段与哈希原样保留)→ 必须校验失败
- std::string tampered(kGoldenRecord1);
- const std::size_t pos = tampered.find("\"px_per_m\":115.6");
- if (pos != std::string::npos) { tampered.replace(pos, 16, "\"px_per_m\":120.0"); }
- const portcv::json::Value bad = parse_or_null(tampered);
- check(bad.is_object(), "tampered record parses");
- check(!portcv::verify_hash(bad), "tampered px_per_m fails verification");
- check_str(portcv::compute_hash(bad) != kGoldenHash1 ? "different" : "same", "different",
- "tampered hash differs");
- const portcv::json::Value rec2 = parse_or_null(kGoldenRecord2);
- check(rec2.is_object(), "golden record 2 parses (no sha256 field)");
- check_str(portcv::compute_hash(rec2), kGoldenHash2, "record 2 sha256 matches Python (int/float/unicode)");
- check(!portcv::verify_hash(rec2), "record without sha256 does not verify");
- }
- void test_iso8601() {
- std::printf("[4] ISO-8601 parsing\n");
- long long t1 = 0;
- long long t2 = 0;
- check(portcv::parse_iso8601("2026-10-26T10:00:00+08:00", &t1), "parse +08:00 form");
- check(portcv::parse_iso8601("2026-10-26T02:00:00Z", &t2), "parse Z form");
- check_eq(t1, t2, "same instant in different offsets");
- check_eq(t1, kCalibratedAt, "epoch value as expected");
- check(portcv::parse_iso8601("2026-10-26", &t1), "parse date-only form");
- check_eq(t1, kCalibratedAt - 2 * 3600, "date-only == midnight UTC");
- check(portcv::parse_iso8601("1970-01-01T00:00:00Z", &t1), "parse epoch origin");
- check_eq(t1, 0, "epoch origin is 0");
- check(portcv::parse_iso8601("2026-10-26T10:00:00.987+08:00", &t1), "parse fractional seconds");
- check_eq(t1, kCalibratedAt, "fraction is truncated to whole seconds");
- check(!portcv::parse_iso8601("2026-13-01T00:00:00Z", &t1), "reject month 13");
- check(!portcv::parse_iso8601("", &t1), "reject empty");
- check(!portcv::parse_iso8601("not-a-date", &t1), "reject garbage");
- check(!portcv::parse_iso8601("2026-10-26T25:00:00Z", &t1), "reject hour 25");
- check(!portcv::parse_iso8601("2026-10-26T02:00:00Zjunk", &t1), "reject trailing garbage");
- }
- /// 造一份"自洽封存"的记录(哈希由本实现算,只用于验证**非哈希**规则)。
- portcv::json::Value make_record(const std::string& reviewer, const std::string& calibrated_at,
- double px_per_m, double box_height_m) {
- using portcv::json::Type;
- using portcv::json::Value;
- Value record;
- record.type = Type::Object;
- const auto add_text = [&record](const char* key, const std::string& value) {
- Value v;
- v.type = Type::String;
- v.text = value;
- record.members.push_back({key, v});
- };
- const auto add_number = [&record](const char* key, double value) {
- Value v;
- v.type = Type::Number;
- v.number = value;
- record.members.push_back({key, v});
- };
- add_text("site", "CT01");
- add_text("camera_id", "front");
- add_text("calibrated_at", calibrated_at);
- add_text("reviewer", reviewer);
- add_number("px_per_m", px_per_m);
- add_number("box_height_m", box_height_m);
- Value hash;
- hash.type = Type::String;
- hash.text = portcv::compute_hash(record);
- record.members.push_back({"sha256", hash});
- return record;
- }
- portcv::CalibrationInputs inputs_of(const portcv::json::Value& record, double run_px, double run_box) {
- portcv::CalibrationInputs in;
- in.file_exists = true;
- in.json_ok = true;
- in.record = record;
- in.run_px_per_m = run_px;
- in.run_box_height_m = run_box;
- return in;
- }
- void test_evaluate() {
- std::printf("[5] evaluate (expiry / review / hash / drift / manual)\n");
- portcv::CalibrationConfig cfg; // 默认:180 天 / 10% / 0.02 m / hold_on_stale=true
- cfg.record = "configs/calib/ct01-front.json"; // 纯函数不读盘:路径只用于日志/原因
- const portcv::json::Value fresh = make_record("zhang", "2026-10-26T02:00:00Z", 115.6, 2.591);
- const long long now = kCalibratedAt + 10 * kDay;
- portcv::CalibrationStatus s =
- portcv::evaluate_calibration_values(cfg, inputs_of(fresh, 115.6, 2.591), now);
- check(!s.stale, "fresh reviewed record is usable");
- check(s.hash_ok, "hash_ok reported");
- check(s.has_record, "record parsed");
- check_str(s.record.site, "CT01", "site parsed");
- check_str(s.record.camera_id, "front", "camera_id parsed");
- check(s.hold, "hold_on_stale defaults to true");
- check(s.has_age && s.age_days > 9.9 && s.age_days < 10.1, "age_days ~ 10");
- check(s.reasons_text().empty(), "no reasons when usable");
- // 过期:超过 max_age_days
- s = portcv::evaluate_calibration_values(cfg, inputs_of(fresh, 115.6, 2.591),
- kCalibratedAt + 200 * kDay);
- check(s.stale && has_reason(s, "expired"), "expired -> reasons=expired");
- cfg.max_age_days = 0.0; // 0 = 关闭过期判定
- s = portcv::evaluate_calibration_values(cfg, inputs_of(fresh, 115.6, 2.591),
- kCalibratedAt + 200 * kDay);
- check(!s.stale, "max_age_days=0 disables expiry");
- cfg.max_age_days = 180.0;
- s = portcv::evaluate_calibration_values(cfg, inputs_of(fresh, 115.6, 2.591), kCalibratedAt - kDay);
- check(!s.stale, "future calibrated_at is not expired");
- // 未复核
- const portcv::json::Value unreviewed = make_record("", "2026-10-26T02:00:00Z", 115.6, 2.591);
- s = portcv::evaluate_calibration_values(cfg, inputs_of(unreviewed, 115.6, 2.591), now);
- check(s.stale && has_reason(s, "unreviewed"), "empty reviewer -> reasons=unreviewed");
- // 哈希不符(改了数但没重新封存)
- portcv::json::Value tampered = make_record("zhang", "2026-10-26T02:00:00Z", 115.6, 2.591);
- for (auto& member : tampered.members) {
- if (member.first == "px_per_m") { member.second.number = 120.0; }
- }
- s = portcv::evaluate_calibration_values(cfg, inputs_of(tampered, 120.0, 2.591), now);
- check(s.stale && has_reason(s, "hash_mismatch"), "tampered record -> reasons=hash_mismatch");
- }
- void test_evaluate_io_edges() {
- portcv::CalibrationConfig cfg;
- cfg.record = "configs/calib/ct01-front.json"; // 纯函数不读盘
- const portcv::json::Value fresh = make_record("zhang", "2026-10-26T02:00:00Z", 115.6, 2.591);
- const long long now = kCalibratedAt + 10 * kDay;
- // 文件缺失 / JSON 坏 / 记录不是对象 / 缺必填字段
- portcv::CalibrationInputs in = inputs_of(fresh, 115.6, 2.591);
- in.file_exists = false;
- portcv::CalibrationStatus s = portcv::evaluate_calibration_values(cfg, in, now);
- check(s.stale && has_reason(s, "missing") && !s.hash_ok, "missing file -> reasons=missing");
- in = inputs_of(fresh, 115.6, 2.591);
- in.json_ok = false;
- s = portcv::evaluate_calibration_values(cfg, in, now);
- check(s.stale && has_reason(s, "invalid:json"), "bad json -> reasons=invalid:json");
- in = inputs_of(fresh, 115.6, 2.591);
- in.record = portcv::json::Value(); // Null
- s = portcv::evaluate_calibration_values(cfg, in, now);
- check(s.stale && has_reason(s, "invalid:json"), "non-object record -> reasons=invalid:json");
- portcv::json::Value partial;
- partial.type = portcv::json::Type::Object;
- s = portcv::evaluate_calibration_values(cfg, inputs_of(partial, 115.6, 2.591), now);
- check(s.stale && has_reason(s, "invalid:fields"), "missing required fields -> invalid:fields");
- // 比例尺漂移(相对容差 10%)
- s = portcv::evaluate_calibration_values(cfg, inputs_of(fresh, 150.0, 2.591), now);
- check(s.stale && has_reason(s, "px_per_m_drift"), "px_per_m 150 vs 115.6 -> drift");
- s = portcv::evaluate_calibration_values(cfg, inputs_of(fresh, 120.0, 2.591), now);
- check(!s.stale, "px_per_m 120 vs 115.6 (3.8%) -> within tolerance");
- // 箱高漂移(绝对容差 0.02 m)
- s = portcv::evaluate_calibration_values(cfg, inputs_of(fresh, 115.6, 2.70), now);
- check(s.stale && has_reason(s, "box_height_drift"), "box_height 2.70 vs 2.591 -> drift");
- s = portcv::evaluate_calibration_values(cfg, inputs_of(fresh, 115.6, 2.60), now);
- check(!s.stale, "box_height 2.60 vs 2.591 -> within tolerance");
- // 运行期值为 0(未配置)时不判漂移
- s = portcv::evaluate_calibration_values(cfg, inputs_of(fresh, 0.0, 0.0), now);
- check(!s.stale, "run px_per_m=0 (unset) skips drift check");
- // 手工 stale(有记录时追加一条;无记录时单独成立)
- cfg.stale = true;
- s = portcv::evaluate_calibration_values(cfg, inputs_of(fresh, 115.6, 2.591), now);
- check(s.stale && has_reason(s, "manual_stale"), "manual stale flag raises CALIB_STALE");
- cfg.stale = false;
- cfg.record.clear(); // 未配记录
- s = portcv::evaluate_calibration_values(cfg, inputs_of(fresh, 115.6, 2.591), now);
- check(!s.stale && !s.has_record, "no record configured -> no judgement");
- cfg.stale = true;
- s = portcv::evaluate_calibration_values(cfg, inputs_of(fresh, 115.6, 2.591), now);
- check(s.stale && s.reasons_text() == "manual_stale", "manual stale without record");
- cfg.stale = false;
- cfg.record = "configs/calib/ct01-front.json"; // 恢复记录,供 hold 用例使用
- // hold_on_stale=false:只告警,不清 RESULT_VALID/STABLE
- cfg.hold_on_stale = false;
- s = portcv::evaluate_calibration_values(cfg, inputs_of(fresh, 150.0, 2.591), now);
- check(s.stale && !s.hold, "hold_on_stale=false is carried into status");
- }
- void test_file_io() {
- std::printf("[6] file I/O (real record on disk)\n");
- const char* path = "portcv_calib_selftest_tmp.json";
- std::FILE* fh = std::fopen(path, "wb");
- check(fh != nullptr, "temp record created");
- if (fh != nullptr) {
- std::fwrite(kGoldenRecord1, 1, std::strlen(kGoldenRecord1), fh);
- std::fclose(fh);
- }
- portcv::CalibrationConfig cfg;
- cfg.record = path;
- const long long now = kCalibratedAt + 3 * kDay;
- const portcv::CalibrationStatus s = portcv::evaluate_calibration(cfg, 115.6, 2.591, now);
- check(!s.stale, "record read from disk is usable");
- check(s.hash_ok, "disk record hash ok (same bytes as Python-sealed file)");
- check(s.has_age && s.age_days > 2.9 && s.age_days < 3.1, "disk record age ~ 3 days");
- check_str(s.record.calibrated_at, "2026-10-26T10:00:00+08:00", "calibrated_at kept verbatim");
- const std::string line = portcv::calibration_status_line(s);
- check(line.find("stale=no") != std::string::npos, "status line reports stale=no");
- check(line.find("hash=ok") != std::string::npos, "status line reports hash=ok");
- check(line.find("CT01") != std::string::npos, "status line shows site");
- std::remove(path);
- cfg.record = "definitely_missing_calib_record.json";
- const portcv::CalibrationStatus missing = portcv::evaluate_calibration(cfg, 115.6, 2.591, now);
- check(missing.stale && has_reason(missing, "missing"), "missing file on disk -> reasons=missing");
- check(!missing.hash_ok && !missing.has_record, "missing file: no hash / no record");
- cfg.record.clear();
- const portcv::CalibrationStatus none = portcv::evaluate_calibration(cfg, 0.0, 0.0, now);
- check(!none.stale, "empty record path is not stale");
- check(portcv::calibration_status_line(none).find("(未配置)") != std::string::npos,
- "status line marks unconfigured record");
- }
- void test_config() {
- std::printf("[7] calibration.* config binding\n");
- portcv::AppConfig cfg;
- std::string error;
- check(portcv::app_config_from_text("{\"calibration\":{}}", &cfg, &error), "empty block ok");
- check(cfg.calibration.max_age_days == 180.0, "default max_age_days=180 (same as Python DEFAULTS)");
- check(cfg.calibration.px_per_m_tolerance == 0.10, "default px_per_m_tolerance=0.10");
- check(cfg.calibration.box_height_tolerance_m == 0.02, "default box_height_tolerance_m=0.02");
- check(cfg.calibration.hold_on_stale, "default hold_on_stale=true");
- check(cfg.calibration.record.empty() && !cfg.calibration.stale, "default record empty / stale=false");
- const char* text =
- "{\"calibration\":{\"record\":\"configs/calib/ct01-front.json\",\"stale\":true,"
- "\"max_age_days\":90,\"px_per_m_tolerance\":0.05,"
- "\"box_height_tolerance_m\":0.01,\"hold_on_stale\":false}}";
- check(portcv::app_config_from_text(text, &cfg, &error), "full calibration block parses");
- check_str(cfg.calibration.record, "configs/calib/ct01-front.json", "record bound");
- check(cfg.calibration.stale, "stale bound");
- check(cfg.calibration.max_age_days == 90.0, "max_age_days bound");
- check(cfg.calibration.px_per_m_tolerance == 0.05, "px_per_m_tolerance bound");
- check(cfg.calibration.box_height_tolerance_m == 0.01, "box_height_tolerance_m bound");
- check(!cfg.calibration.hold_on_stale, "hold_on_stale bound");
- error.clear();
- check(!portcv::app_config_from_text("{\"calibration\":{\"max_age_days\":\"180\"}}", &cfg, &error),
- "string max_age_days is rejected");
- check(error.find("calibration.max_age_days") != std::string::npos, "error names the offending key");
- error.clear();
- check(!portcv::app_config_from_text("{\"calibration\":{\"px_per_m_tolerance\":-1}}", &cfg, &error),
- "negative tolerance rejected");
- error.clear();
- check(!portcv::app_config_from_text("{\"calibration\":{\"box_height_tolerance_m\":9}}", &cfg, &error),
- "absurd box tolerance rejected");
- error.clear();
- check(!portcv::app_config_from_text("{\"calibration\":{\"stale\":\"yes\"}}", &cfg, &error),
- "string stale is rejected");
- const std::string line = portcv::describe_app_config(cfg);
- check(line.find("calibration.record=") != std::string::npos, "describe shows calibration.record");
- check(line.find("hold_on_stale=") != std::string::npos, "describe shows hold_on_stale");
- check(line.find("max_age_days=") != std::string::npos, "describe shows max_age_days");
- }
- } // namespace
- int main() {
- std::printf("portcv 标定记录自检(P0-5):记录 / 哈希 / 过期 / 漂移 / 配置\n");
- test_sha256();
- test_canonical();
- test_golden_records();
- test_iso8601();
- test_evaluate();
- test_evaluate_io_edges();
- test_file_io();
- test_config();
- std::printf("\n结果: %d 通过 / %d 失败\n", g_pass, g_fail);
- if (g_fail == 0) { std::printf("全部通过(ctest 判据:退出码 0)。\n"); }
- return g_fail == 0 ? 0 : 1;
- }
|