Files
hm ddc46b63fb
ci / ubuntu-latest / clang (push) Failing after 38s
ci / ubuntu-latest / gcc (push) Failing after 43s
ci / coverage / gcc (push) Failing after 52s
ci / macos-latest / apple-clang (push) Canceled after 0s
ci / windows-latest / mingw (push) Canceled after 0s
ci / windows-latest / msvc (push) Canceled after 0s
test(g700): 拖拽错误传播/驱动约束装配/冗余归因/0-DOF 分解回填 可达补测覆盖收口
- drag_controller L74/L115/L158 错误传播三门(Begin 非有限钉扎拒绝 / Move-End 钉扎丢失 kInvalidReference)
- sparse_jacobian L32/L189 driven 约束跳过门(JacobianLayout vs AssembleJacobian 双口径同判)
- diagnostic L128/L163 欠约束与名义完整分量的部分冗余消息真值支
- decomposer L520-527 0-DOF 实体+0-RemovedDof 约束的 0↔n 一致性回填
- 纯测试轮零 src 变更(ABI 171/0);ctest build_cov 60/60 exit 0(638.45s);coverage src 95.9%
- line_scan src gap 530→518(Δ −12 行 / 0 新开,连续第十二轮真实覆盖率闭合)
2026-10-07 05:26:12 +08:00

410 lines
18 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <gtest/gtest.h>
#include "vdcs/core/diagnostic.h"
#include "vdcs/core/dof_analyzer.h"
#include "vdcs/model/model.h"
using vdcs::Diagnostic;
using vdcs::DiagnosticCode;
using vdcs::DiagnosticLevel;
using vdcs::DofAnalyzer;
using vdcs::Model;
using vdcs::SolveDiagnostics;
using vdcs::SolveOptions;
using vdcs::SolveStatus;
namespace {
int CountCode(const vdcs::SolveResult& r, DiagnosticCode code) {
int n = 0;
for (const auto& d : r.diagnostics) {
if (d.code == code) {
++n;
}
}
return n;
}
/// 完整约束三角形:A 固定 + 三距离 + AB 水平
Model BuildTriangle() {
Model m;
auto a = m.AddPoint2D("P1", 0.0, 0.0);
auto b = m.AddPoint2D("P2", 4.0, 0.0);
auto c = m.AddPoint2D("P3", 1.0, 2.0);
(void)m.AddFixed2D("F1", a.Value());
(void)m.AddDistance2D("D1", a.Value(), b.Value(), 5.0);
(void)m.AddDistance2D("D2", a.Value(), c.Value(), 4.0);
(void)m.AddDistance2D("D3", b.Value(), c.Value(), 3.0);
(void)m.AddHorizontal2D("H1", a.Value(), b.Value());
return m;
}
} // namespace
TEST(SolveDiagnosticsTest, WellConstrainedSolved) {
Model model = BuildTriangle();
auto report = model.Solve(SolveOptions{});
ASSERT_TRUE(report.Ok());
auto dof = DofAnalyzer::Analyze(model);
auto result = SolveDiagnostics::Build(report.Value(), dof);
EXPECT_EQ(result.status, SolveStatus::kSolved);
EXPECT_TRUE(result.diagnostics.empty());
EXPECT_TRUE(result.fully_constrained);
EXPECT_FALSE(result.has_redundancy);
EXPECT_EQ(result.mobility, 0);
}
TEST(SolveDiagnosticsTest, UnderConstrainedWarnsButSolves) {
// A 固定 + |AB|:M=1,欠约束但仍收敛 → kSolved + 警告
Model model;
auto a = model.AddPoint2D("P1", 0.0, 0.0);
auto b = model.AddPoint2D("P2", 10.0, 0.1);
(void)model.AddFixed2D("F1", a.Value());
(void)model.AddDistance2D("D1", a.Value(), b.Value(), 12.5);
auto report = model.Solve(SolveOptions{});
ASSERT_TRUE(report.Ok());
auto dof = DofAnalyzer::Analyze(model);
auto result = SolveDiagnostics::Build(report.Value(), dof);
EXPECT_EQ(result.status, SolveStatus::kSolved);
EXPECT_EQ(CountCode(result, DiagnosticCode::kUnderConstrained), 1);
EXPECT_GT(result.mobility, 0);
EXPECT_GT(result.residual_dof, 0);
EXPECT_FALSE(result.fully_constrained);
// 分量索引可定位
const auto& d = result.diagnostics[0];
EXPECT_EQ(d.level, DiagnosticLevel::kWarning);
EXPECT_EQ(d.component, 0);
}
TEST(SolveDiagnosticsTest, OverConstrainedInconsistent) {
// 双固定 + 矛盾距离:计数过约束 → kInconsistent + Error + 冗余候选
Model model;
auto a = model.AddPoint2D("P1", 0.0, 0.0);
auto b = model.AddPoint2D("P2", 10.0, 0.0);
(void)model.AddFixed2D("F1", a.Value());
(void)model.AddFixed2D("F2", b.Value());
auto d1 = model.AddDistance2D("D1", a.Value(), b.Value(), 5.0);
ASSERT_TRUE(d1.Ok());
auto report = model.Solve(SolveOptions{});
ASSERT_TRUE(report.Ok());
auto dof = DofAnalyzer::Analyze(model);
auto result = SolveDiagnostics::Build(report.Value(), dof);
EXPECT_EQ(result.status, SolveStatus::kInconsistent);
EXPECT_EQ(CountCode(result, DiagnosticCode::kOverConstrained), 1);
// 过约束分量内每个约束都是冗余候选(2 Fixed + 1 Distance)
EXPECT_EQ(CountCode(result, DiagnosticCode::kRedundantConstraint), 3);
EXPECT_TRUE(result.has_redundancy);
// 冗余候选引用约束 UID(可定位)
for (const auto& d : result.diagnostics) {
if (d.code == DiagnosticCode::kRedundantConstraint) {
EXPECT_NE(d.constraint, vdcs::kInvalidUid);
}
}
}
TEST(SolveDiagnosticsTest, HiddenRedundancyDetected) {
// A 固定 + 两条 |AB| 距离:计数 M=0 名义完整,梯度平行 → 秩亏
Model model;
auto a = model.AddPoint2D("P1", 0.0, 0.0);
auto b = model.AddPoint2D("P2", 3.0, 4.0);
(void)model.AddFixed2D("F1", a.Value());
(void)model.AddDistance2D("D1", a.Value(), b.Value(), 5.0);
(void)model.AddDistance2D("D2", a.Value(), b.Value(), 5.0);
auto dof = DofAnalyzer::Analyze(model);
vdcs::SolveReport solved; // 只验证结构诊断:converged 避免注入数值噪声
solved.converged = true;
auto result = SolveDiagnostics::Build(solved, dof);
EXPECT_EQ(result.status, SolveStatus::kInconsistent);
EXPECT_TRUE(result.has_redundancy);
// 分量内全部约束(F1/D1/D2)均为冗余候选
EXPECT_EQ(CountCode(result, DiagnosticCode::kRedundantConstraint), 3);
EXPECT_EQ(CountCode(result, DiagnosticCode::kOverConstrained), 0);
}
TEST(SolveDiagnosticsTest, PartialRedundancyLocalizedToConstraint) {
// 部分冗余(g23,对标 planeGCS partiallyRedundant):
// Coincident(2 行) 与 DistanceX=0 并存——前者 dx 行与后者线性相关、
// dy 行独立 → 秩贡献 1 ∈ (0,2) → kPartiallyRedundantConstraint;
// Fixed(贡献 2=行数) 与 DistanceX(贡献 0) 均不属部分冗余
Model model;
auto a = model.AddPoint2D("P1", 0.0, 0.0);
auto b = model.AddPoint2D("P2", 1.0, 2.0);
(void)model.AddFixed2D("F1", a.Value());
auto c1 = model.AddCoincident2D("C1", a.Value(), b.Value());
(void)model.AddDistanceX2D("X1", a.Value(), b.Value(), 0.0);
ASSERT_TRUE(c1.Ok());
auto report = model.Solve(SolveOptions{});
ASSERT_TRUE(report.Ok());
EXPECT_TRUE(report.Value().converged); // 一致系统(b → a)
auto dof = DofAnalyzer::Analyze(model);
ASSERT_EQ(dof.components.size(), 1u);
EXPECT_TRUE(dof.components[0].rank_deficient);
ASSERT_EQ(dof.components[0].partial_constraints.size(), 1u);
EXPECT_EQ(dof.components[0].partial_constraints[0], c1.Value());
// 2 参 Build(数值层候选精化)
auto result = SolveDiagnostics::Build(report.Value(), dof);
EXPECT_EQ(CountCode(result,
DiagnosticCode::kPartiallyRedundantConstraint),
1);
EXPECT_EQ(CountCode(result, DiagnosticCode::kRedundantConstraint), 2);
for (const auto& d : result.diagnostics) {
if (d.code == DiagnosticCode::kPartiallyRedundantConstraint) {
EXPECT_EQ(d.constraint, c1.Value());
EXPECT_EQ(d.level, DiagnosticLevel::kWarning);
}
}
// 3 参 Build(结构层收敛分支精化)同口径
auto decomp = vdcs::Decomposer::Decompose(model);
auto result3 = SolveDiagnostics::Build(report.Value(), dof, decomp);
EXPECT_EQ(CountCode(result3,
DiagnosticCode::kPartiallyRedundantConstraint),
1);
for (const auto& d : result3.diagnostics) {
if (d.code == DiagnosticCode::kPartiallyRedundantConstraint) {
EXPECT_EQ(d.constraint, c1.Value());
}
}
// JSON 往返保持部分冗余码
auto back = SolveDiagnostics::FromJson(SolveDiagnostics::ToJson(result));
ASSERT_TRUE(back.Ok());
EXPECT_EQ(CountCode(back.Value(),
DiagnosticCode::kPartiallyRedundantConstraint),
1);
EXPECT_STREQ(SolveDiagnostics::ToString(
DiagnosticCode::kPartiallyRedundantConstraint),
"partially_redundant_constraint");
}
TEST(SolveDiagnosticsTest, PartialRedundancyInUnderConstrainedComponent) {
// g700:欠约束 + 秩亏分量的部分冗余分支(diagnostic 消息真值支)。
// 四自由点链 F(a)+D(a,b)+D(b,c)+C(c,d)+X(c,d)=0:rows=7 < cols=8
// → kUnderConstrained;X 行依赖 C 的 dx 行 → 秩亏 6<7;
// C 留一后秩降 1 ∈ (0,2) → 部分冗余 → 走 rank-deficient 真支
Model model;
auto a = model.AddPoint2D("P1", 0.0, 0.0);
auto b = model.AddPoint2D("P2", 1.0, 0.2);
auto c = model.AddPoint2D("P3", 2.0, 0.3);
auto d = model.AddPoint2D("P4", 2.5, 0.4);
(void)model.AddFixed2D("F1", a.Value());
(void)model.AddDistance2D("D1", a.Value(), b.Value(), 1.0);
(void)model.AddDistance2D("D2", b.Value(), c.Value(), 1.0);
auto c1 = model.AddCoincident2D("C1", c.Value(), d.Value());
(void)model.AddDistanceX2D("X1", c.Value(), d.Value(), 0.0);
ASSERT_TRUE(c1.Ok());
auto report = model.Solve(SolveOptions{});
ASSERT_TRUE(report.Ok());
EXPECT_TRUE(report.Value().converged);
auto dof = DofAnalyzer::Analyze(model);
ASSERT_EQ(dof.components.size(), 1u);
EXPECT_EQ(dof.components[0].status, vdcs::DofStatus::kUnderConstrained);
EXPECT_TRUE(dof.components[0].rank_deficient);
ASSERT_EQ(dof.components[0].partial_constraints.size(), 1u);
EXPECT_EQ(dof.components[0].partial_constraints[0], c1.Value());
auto result = SolveDiagnostics::Build(report.Value(), dof);
EXPECT_EQ(
CountCode(result, DiagnosticCode::kPartiallyRedundantConstraint), 1);
for (const auto& d : result.diagnostics) {
if (d.code == DiagnosticCode::kPartiallyRedundantConstraint) {
EXPECT_EQ(d.constraint, c1.Value());
EXPECT_NE(d.message.find("rank-deficient component"),
std::string::npos);
}
}
}
TEST(SolveDiagnosticsTest, PartialRedundancyInWellConstrainedComponent) {
// g700:名义完整 + 秩亏分量的部分冗余分支(隐藏冗余的部分归因)。
// Fixed(a,2) + Coincident(b,c)(2) + Distance(a,b)(1) + DistanceX(b,c)=0(1)
// = 6 行 = 6 DOF → kWellConstrained;DistanceX 行依赖 Coincident 的
// dx 行 → 秩亏;Coincident 留一后秩降 1 ∈ (0,2) → 部分冗余
Model model;
auto a = model.AddPoint2D("P1", 0.0, 0.0);
auto b = model.AddPoint2D("P2", 1.0, 0.0);
auto c = model.AddPoint2D("P3", 3.0, 2.0);
(void)model.AddFixed2D("F1", a.Value());
auto c1 = model.AddCoincident2D("C1", b.Value(), c.Value());
(void)model.AddDistance2D("D1", a.Value(), b.Value(), 1.0);
(void)model.AddDistanceX2D("X1", b.Value(), c.Value(), 0.0);
ASSERT_TRUE(c1.Ok());
auto report = model.Solve(SolveOptions{});
ASSERT_TRUE(report.Ok());
EXPECT_TRUE(report.Value().converged);
auto dof = DofAnalyzer::Analyze(model);
ASSERT_EQ(dof.components.size(), 1u);
EXPECT_EQ(dof.components[0].status, vdcs::DofStatus::kWellConstrained);
EXPECT_TRUE(dof.components[0].rank_deficient);
ASSERT_EQ(dof.components[0].partial_constraints.size(), 1u);
EXPECT_EQ(dof.components[0].partial_constraints[0], c1.Value());
auto result = SolveDiagnostics::Build(report.Value(), dof);
EXPECT_EQ(
CountCode(result, DiagnosticCode::kPartiallyRedundantConstraint), 1);
for (const auto& d : result.diagnostics) {
if (d.code == DiagnosticCode::kPartiallyRedundantConstraint) {
EXPECT_EQ(d.constraint, c1.Value());
EXPECT_NE(d.message.find("nominally complete component"),
std::string::npos);
}
}
}
TEST(SolveDiagnosticsTest, ConcentricTangentEqualRadiiNotRedundant) {
// g38(上游 7023e83):internal 同心 + 等半径——约束已满足,但距离差
// 口径的零梯度(中心 dx/d、半径 sgn=0)会使雅可比整行归零 → 秩贡献 0
// → 误报冗余;同心分支的常数梯度 ±1 使秩贡献 1 = 行数,不冗余
Model model;
auto c1 = model.AddCircle2D("C1", 0.0, 0.0, 3.0);
auto c2 = model.AddCircle2D("C2", 0.0, 0.0, 3.0); // 同心等半径
(void)model.AddFixed2D("F1", c1.Value());
(void)model.AddTangent2D("TAN", c1.Value(), c2.Value(), false);
auto dof = DofAnalyzer::Analyze(model);
vdcs::SolveReport solved; // 只验证结构诊断:converged 避免注入数值噪声
solved.converged = true;
auto result = SolveDiagnostics::Build(solved, dof);
EXPECT_EQ(CountCode(result, DiagnosticCode::kRedundantConstraint), 0);
EXPECT_FALSE(result.has_redundancy);
// mobility:6 − 3(Fixed) − 1(相切满秩行) = 2(c2 中心自由)
EXPECT_EQ(result.mobility, 2);
}
TEST(SolveDiagnosticsTest, NotConvergedReported) {
// 矛盾模型不收敛:λ 饱和停滞 → kStalled Error(P1-S3-04 语义)
Model model;
auto a = model.AddPoint2D("P1", 0.0, 0.0);
auto b = model.AddPoint2D("P2", 10.0, 0.0);
(void)model.AddFixed2D("F1", a.Value());
(void)model.AddFixed2D("F2", b.Value());
(void)model.AddDistance2D("D1", a.Value(), b.Value(), 5.0);
auto report = model.Solve(SolveOptions{});
ASSERT_TRUE(report.Ok());
auto dof = DofAnalyzer::Analyze(model);
auto result = SolveDiagnostics::Build(report.Value(), dof);
// 过约束 + 停滞:结构性矛盾优先
EXPECT_EQ(result.status, SolveStatus::kInconsistent);
EXPECT_EQ(CountCode(result, DiagnosticCode::kStalled), 1);
// 仅数值失败(结构正常):手工构造未收敛报告
vdcs::SolveReport failing;
failing.converged = false;
failing.residual_norm = 1.0;
failing.iterations = 7;
auto result2 = SolveDiagnostics::Build(failing, DofAnalyzer::Analyze(model));
EXPECT_EQ(result2.status, SolveStatus::kInconsistent); // 同模型仍矛盾
Model tri = BuildTriangle();
auto dof_tri = DofAnalyzer::Analyze(tri);
auto result3 = SolveDiagnostics::Build(failing, dof_tri);
EXPECT_EQ(result3.status, SolveStatus::kNotConverged);
EXPECT_EQ(CountCode(result3, DiagnosticCode::kNotConverged), 1);
}
TEST(SolveDiagnosticsTest, JsonRoundtrip) {
// 欠约束 + 未收敛混合诊断的往返保持
Model model;
auto a = model.AddPoint2D("P1", 0.0, 0.0);
auto b = model.AddPoint2D("P2", 10.0, 0.1);
(void)model.AddFixed2D("F1", a.Value());
(void)model.AddDistance2D("D1", a.Value(), b.Value(), 12.5);
auto report = model.Solve(SolveOptions{});
ASSERT_TRUE(report.Ok());
auto dof = DofAnalyzer::Analyze(model);
auto result = SolveDiagnostics::Build(report.Value(), dof);
result.diagnostics.push_back(Diagnostic{
DiagnosticLevel::kError, DiagnosticCode::kNotConverged,
42, 7, 0, 3, "manual entry"});
std::string json = SolveDiagnostics::ToJson(result);
auto back = SolveDiagnostics::FromJson(json);
ASSERT_TRUE(back.Ok());
const auto& r = back.Value();
EXPECT_EQ(r.status, result.status);
EXPECT_EQ(r.residual_dof, result.residual_dof);
EXPECT_EQ(r.mobility, result.mobility);
EXPECT_EQ(r.has_redundancy, result.has_redundancy);
EXPECT_EQ(r.fully_constrained, result.fully_constrained);
EXPECT_EQ(r.report.converged, result.report.converged);
EXPECT_EQ(r.report.iterations, result.report.iterations);
EXPECT_DOUBLE_EQ(r.report.residual_norm, result.report.residual_norm);
ASSERT_EQ(r.diagnostics.size(), result.diagnostics.size());
for (size_t i = 0; i < r.diagnostics.size(); ++i) {
EXPECT_EQ(r.diagnostics[i].level, result.diagnostics[i].level);
EXPECT_EQ(r.diagnostics[i].code, result.diagnostics[i].code);
EXPECT_EQ(r.diagnostics[i].entity, result.diagnostics[i].entity);
EXPECT_EQ(r.diagnostics[i].constraint,
result.diagnostics[i].constraint);
EXPECT_EQ(r.diagnostics[i].component, result.diagnostics[i].component);
EXPECT_EQ(r.diagnostics[i].message, result.diagnostics[i].message);
}
}
TEST(SolveDiagnosticsTest, JsonInvalidRejected) {
auto r1 = SolveDiagnostics::FromJson("not json");
ASSERT_FALSE(r1.Ok());
EXPECT_EQ(r1.Err().Code(), vdcs::ErrorCode::kInvalidInput);
auto r2 = SolveDiagnostics::FromJson(R"({"status":"bogus"})");
ASSERT_FALSE(r2.Ok());
// 缺字段
auto r3 = SolveDiagnostics::FromJson(
R"({"status":"solved","residual_dof":0})");
ASSERT_FALSE(r3.Ok());
}
TEST(SolveDiagnosticsTest, EnumStringsStable) {
EXPECT_STREQ(SolveDiagnostics::ToString(SolveStatus::kSolved), "solved");
EXPECT_STREQ(SolveDiagnostics::ToString(SolveStatus::kNotConverged),
"not_converged");
EXPECT_STREQ(SolveDiagnostics::ToString(SolveStatus::kInconsistent),
"inconsistent");
EXPECT_STREQ(SolveDiagnostics::ToString(DiagnosticLevel::kWarning),
"warning");
EXPECT_STREQ(SolveDiagnostics::ToString(DiagnosticCode::kOverConstrained),
"over_constrained");
EXPECT_STREQ(
SolveDiagnostics::ToString(DiagnosticCode::kRedundantConstraint),
"redundant_constraint");
}
TEST(SolveDiagnosticsTest, JsonLevelCodeEdgesAndInfoLevel) {
// g611:覆盖率缺口填充——FromJson 的 info 层解析(此前仅 warning/
// error 经 Build 产生,kInfo 分支从未走到)、未知 level/code 拒绝
// (此前只测了未知 status)、非对象 JSON 拒绝;并直接行使
// ToString(DiagnosticLevel::kInfo)(kInfo 不参与诊断生成路径)
const auto parse = [](const std::string& diags) {
return SolveDiagnostics::FromJson(
R"({"status":"solved","residual_dof":0,"mobility":0,)"
R"("has_redundancy":false,"fully_constrained":true,)"
R"("report":{"converged":true,"iterations":1,)"
R"("residual_norm":0.0,"total_dof":3,"removed_dof":0,)"
R"("mobility":0,"used_lm":false,"sparse":false,)"
R"("jacobian_nnz":0},"diagnostics":)" +
diags + "}");
};
// info 层解析成功 + 逐位读回
auto ok = parse(R"([{"level":"info","code":"under_constrained",)"
R"("message":"m"}])");
ASSERT_TRUE(ok.Ok());
ASSERT_EQ(ok.Value().diagnostics.size(), 1u);
EXPECT_EQ(ok.Value().diagnostics[0].level, DiagnosticLevel::kInfo);
EXPECT_EQ(ok.Value().diagnostics[0].code,
DiagnosticCode::kUnderConstrained);
// 未知 level / 未知 code → kInvalidInput
auto bad_level = parse(R"([{"level":"bogus","code":"under_constrained",)"
R"("message":"m"}])");
ASSERT_FALSE(bad_level.Ok());
EXPECT_EQ(bad_level.Err().Code(), vdcs::ErrorCode::kInvalidInput);
auto bad_code = parse(R"([{"level":"info","code":"bogus",)"
R"("message":"m"}])");
ASSERT_FALSE(bad_code.Ok());
EXPECT_EQ(bad_code.Err().Code(), vdcs::ErrorCode::kInvalidInput);
// 合法 JSON 但非对象(数组)→ kInvalidInput
auto not_obj = SolveDiagnostics::FromJson("[1,2,3]");
ASSERT_FALSE(not_obj.Ok());
EXPECT_EQ(not_obj.Err().Code(), vdcs::ErrorCode::kInvalidInput);
// ToString(kInfo) 直接行使
EXPECT_STREQ(SolveDiagnostics::ToString(DiagnosticLevel::kInfo), "info");
}