Files

189 lines
6.9 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.
// VDCS-016 请求 3:拓扑变更后的约束引用重映射(vdcs_replace_entity2d)。
// 覆盖:多约束整体迁移与计数、类型/存在性/自引用/双引用拒绝与原子性、
// 快照型约束(Fixed2D)目标刷新、迁移后求解仍收敛、C ABI 误用面。
#include <gtest/gtest.h>
#include <cmath>
#include "vdcs/c_api/vdcs_c_api.h"
#include "vdcs/model/constraint.h"
#include "vdcs/model/model.h"
using vdcs::Constraint;
using vdcs::Entity;
using vdcs::ErrorCode;
using vdcs::Fixed2D;
using vdcs::Model;
using vdcs::Uid;
namespace {
bool References(const Model& model, Uid cid, Uid entity) {
const Constraint* c = model.FindConstraint(cid);
if (c == nullptr) {
return false;
}
for (const Uid e : c->Entities()) {
if (e == entity) {
return true;
}
}
return false;
}
void Params(const Model& model, Uid id, double* out) {
const Entity* e = model.FindEntity(id);
ASSERT_NE(e, nullptr);
e->WriteParams(out);
}
} // namespace
TEST(ReplaceEntity2D, RemapsAllReferencingConstraints) {
Model model;
const auto p1 = model.AddPoint2D("p1", 0.0, 0.0);
const auto p2 = model.AddPoint2D("p2", 1.0, 0.0);
const auto p3 = model.AddPoint2D("p3", 4.0, 0.0);
const auto p4 = model.AddPoint2D("p4", 0.0, 0.0);
const auto c1 = model.AddCoincident2D("c1", p1.Value(), p2.Value());
const auto c2 = model.AddDistance2D("c2", p1.Value(), p3.Value(), 5.0);
const auto c3 = model.AddHorizontal2D("c3", p2.Value(), p3.Value());
ASSERT_TRUE(p1.Ok() && p2.Ok() && p3.Ok() && p4.Ok());
ASSERT_TRUE(c1.Ok() && c2.Ok() && c3.Ok());
const auto r = model.ReplaceEntityReferences(p1.Value(), p4.Value());
ASSERT_TRUE(r.Ok());
EXPECT_EQ(r.Value(), 2); // c1、c2 引用 p1;c3 不引用
EXPECT_TRUE(References(model, c1.Value(), p4.Value()));
EXPECT_TRUE(References(model, c2.Value(), p4.Value()));
EXPECT_FALSE(References(model, c1.Value(), p1.Value()));
EXPECT_FALSE(References(model, c2.Value(), p1.Value()));
}
TEST(ReplaceEntity2D, RejectsTypeMismatchAndUnknown) {
Model model;
const auto p = model.AddPoint2D("p", 0.0, 0.0);
const auto line = model.AddLine2D("l", 0.0, 1.0);
ASSERT_TRUE(p.Ok() && line.Ok());
const auto bad_type =
model.ReplaceEntityReferences(p.Value(), line.Value());
ASSERT_FALSE(bad_type.Ok());
EXPECT_EQ(bad_type.Err().Code(), ErrorCode::kInvalidInput);
const auto missing =
model.ReplaceEntityReferences(p.Value(), static_cast<Uid>(999999));
ASSERT_FALSE(missing.Ok());
EXPECT_EQ(missing.Err().Code(), ErrorCode::kInvalidReference);
const auto same = model.ReplaceEntityReferences(p.Value(), p.Value());
ASSERT_FALSE(same.Ok());
EXPECT_EQ(same.Err().Code(), ErrorCode::kInvalidInput);
}
TEST(ReplaceEntity2D, RejectsConstraintReferencingBothAtomically) {
Model model;
const auto p1 = model.AddPoint2D("p1", 0.0, 0.0);
const auto p2 = model.AddPoint2D("p2", 1.0, 1.0);
const auto pair = model.AddCoincident2D("pair", p1.Value(), p2.Value());
const auto fixed = model.AddFixed2D("fx", p1.Value());
ASSERT_TRUE(pair.Ok() && fixed.Ok());
const auto r = model.ReplaceEntityReferences(p1.Value(), p2.Value());
ASSERT_FALSE(r.Ok());
EXPECT_EQ(r.Err().Code(), ErrorCode::kInvalidInput);
// 整单原子:失败的调用不得留下半迁移状态
EXPECT_TRUE(References(model, pair.Value(), p1.Value()));
EXPECT_TRUE(References(model, fixed.Value(), p1.Value()));
}
TEST(ReplaceEntity2D, NoReferencesReturnsZero) {
Model model;
const auto p1 = model.AddPoint2D("p1", 0.0, 0.0);
const auto p2 = model.AddPoint2D("p2", 1.0, 0.0);
const auto p3 = model.AddPoint2D("p3", 3.0, 0.0);
const auto c = model.AddCoincident2D("c", p1.Value(), p2.Value());
ASSERT_TRUE(c.Ok());
const auto r = model.ReplaceEntityReferences(p2.Value(), p3.Value());
ASSERT_TRUE(r.Ok());
EXPECT_EQ(r.Value(), 1);
const auto none = model.ReplaceEntityReferences(p2.Value(), p3.Value());
ASSERT_TRUE(none.Ok());
EXPECT_EQ(none.Value(), 0); // p2 已无引用:空迁移不报错
}
TEST(ReplaceEntity2D, FixedSnapshotRefreshedToNewEntity) {
Model model;
const auto p1 = model.AddPoint2D("p1", 1.0, 2.0);
const auto p2 = model.AddPoint2D("p2", 7.0, 8.0);
const auto fx = model.AddFixed2D("fx", p1.Value());
ASSERT_TRUE(fx.Ok());
const auto r = model.ReplaceEntityReferences(p1.Value(), p2.Value());
ASSERT_TRUE(r.Ok());
EXPECT_EQ(r.Value(), 1);
const auto* c = model.FindConstraint(fx.Value());
ASSERT_NE(c, nullptr);
const auto* f = dynamic_cast<const Fixed2D*>(c);
ASSERT_NE(f, nullptr);
ASSERT_EQ(f->RefParams().size(), 2);
EXPECT_DOUBLE_EQ(f->RefParams()[0], 7.0);
EXPECT_DOUBLE_EQ(f->RefParams()[1], 8.0);
// 求解后仍钉在新实体现态
ASSERT_TRUE(model.Solve().Ok());
double p[2];
Params(model, p2.Value(), p);
EXPECT_NEAR(p[0], 7.0, 1e-9);
EXPECT_NEAR(p[1], 8.0, 1e-9);
}
TEST(ReplaceEntity2D, SolveStillConvergesAfterRemap) {
Model model;
const auto p1 = model.AddPoint2D("p1", 0.0, 0.0);
const auto p2 = model.AddPoint2D("p2", 0.5, 3.0);
const auto p3 = model.AddPoint2D("p3", 2.0, 4.0);
const auto h = model.AddHorizontal2D("h", p1.Value(), p2.Value());
ASSERT_TRUE(h.Ok());
// p2 → p3:水平约束改挂到 p3,p3 现态 y=4 与 p1.y=0 不一致
const auto r = model.ReplaceEntityReferences(p2.Value(), p3.Value());
ASSERT_TRUE(r.Ok());
EXPECT_EQ(r.Value(), 1);
const auto solved = model.Solve();
ASSERT_TRUE(solved.Ok());
EXPECT_TRUE(solved.Value().converged);
double a[2];
double b[2];
Params(model, p1.Value(), a);
Params(model, p3.Value(), b);
EXPECT_NEAR(a[1], b[1], 1e-9);
}
TEST(ReplaceEntity2D, CApiSmokeAndMisuse) {
vdcs_model* m = nullptr;
ASSERT_EQ(vdcs_model_create(&m), VDCS_OK);
vdcs_uid p1 = 0;
vdcs_uid p2 = 0;
vdcs_uid c = 0;
ASSERT_EQ(vdcs_add_point2d(m, "p1", 0.0, 0.0, &p1), VDCS_OK);
ASSERT_EQ(vdcs_add_point2d(m, "p2", 1.0, 0.0, &p2), VDCS_OK);
ASSERT_EQ(vdcs_add_coincident2d(m, "c", p1, p2, &c), VDCS_OK);
int remapped = -1;
EXPECT_EQ(vdcs_replace_entity2d(nullptr, p1, p2, &remapped),
VDCS_E_INVALID_INPUT);
// out_remapped 可选
EXPECT_EQ(vdcs_replace_entity2d(m, p1, p2, nullptr),
VDCS_E_INVALID_INPUT); // 自引用:约束 c 同时引用两者
EXPECT_EQ(vdcs_replace_entity2d(m, p1, static_cast<vdcs_uid>(999999),
&remapped),
VDCS_E_INVALID_REFERENCE);
vdcs_uid p3 = 0;
vdcs_uid d = 0;
ASSERT_EQ(vdcs_add_point2d(m, "p3", 5.0, 0.0, &p3), VDCS_OK);
ASSERT_EQ(vdcs_add_distance2d(m, "d", p3, p1, 5.0, &d), VDCS_OK);
ASSERT_EQ(vdcs_replace_entity2d(m, p3, p2, &remapped), VDCS_OK);
EXPECT_EQ(remapped, 1);
vdcs_model_destroy(m);
}