311 lines
13 KiB
C++
311 lines
13 KiB
C++
// P3-8 插件 ABI 1.3:GCS(几何约束求解)扩展点测试。
|
||
//
|
||
// 覆盖:
|
||
// 1) 插件发现/加载/注册:kind=gcs_solver、capability gcs:solve、gcs_solvers() 查询、
|
||
// manifest abi.minor=3;
|
||
// 2) mock 求解成功:distance/horizontal/fix 约束(位置收敛、dof/残差诊断);
|
||
// 3) 参数传递:max_iterations 过小 → 未完全收敛(验证 params_json 生效);
|
||
// 4) 失败隔离:未知 solver / 坏 JSON / 不支持的约束 / 空草图 → false+error,之后正常求解不受影响;
|
||
// 5) 卸载清理:unload 后 gcs_solvers()/能力清空;
|
||
// 6) 旧 ABI 兼容:abi11_compat(manifest ABI 1.1)在新宿主(1.3)仍可加载;
|
||
// 7) 直接加载 DLL 校验 query 元信息(abi_minor=3、kind=GCS_SOLVER)。
|
||
#include <gtest/gtest.h>
|
||
|
||
#include <cmath>
|
||
#include <cstdio>
|
||
#include <cstring>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
#include "vde/plugin/geometry_host_services.h"
|
||
#include "vde/plugin/plugin_manager.h"
|
||
#include "vde/plugin/vde_plugin_api.h"
|
||
#include "vde_platform/platform.h"
|
||
|
||
#ifndef VDE_PLUGIN_DIRS
|
||
#define VDE_PLUGIN_DIRS ""
|
||
#endif
|
||
|
||
namespace {
|
||
|
||
using vde::plugin::GcsSolverInfo;
|
||
using vde::plugin::PluginManager;
|
||
|
||
constexpr const char* kGcsPluginId = "com.vde.sample.gcs";
|
||
constexpr const char* kGcsSolverId = "gcs.2d.mock";
|
||
constexpr const char* kLegacyPluginId = "com.vde.test.abi11";
|
||
|
||
std::string temp_path(const char* name) { return std::string(VDE_TEST_TEMP_DIR) + name; }
|
||
|
||
bool write_text_file(const std::string& path, const std::string& text) {
|
||
std::FILE* file = std::fopen(path.c_str(), "wb");
|
||
if (!file) return false;
|
||
const std::size_t written =
|
||
text.empty() ? 0u : std::fwrite(text.data(), 1, text.size(), file);
|
||
std::fclose(file);
|
||
return written == text.size();
|
||
}
|
||
|
||
/// 从结果 JSON 中提取首个 "key": <number> 的数值
|
||
bool extract_number(const std::string& json, const std::string& key, double* out) {
|
||
const std::string pattern = "\"" + key + "\":";
|
||
const std::size_t pos = json.find(pattern);
|
||
if (pos == std::string::npos) return false;
|
||
char* end = nullptr;
|
||
const double value = std::strtod(json.c_str() + pos + pattern.size(), &end);
|
||
if (end == json.c_str() + pos + pattern.size()) return false;
|
||
*out = value;
|
||
return true;
|
||
}
|
||
|
||
/// 从结果 JSON 的 points 数组中提取指定 id 的 x/y
|
||
bool extract_point(const std::string& json, int id, double* x, double* y) {
|
||
const std::string pattern =
|
||
"{\"id\":" + std::to_string(id) + ",\"x\":";
|
||
const std::size_t pos = json.find(pattern);
|
||
if (pos == std::string::npos) return false;
|
||
const char* begin = json.c_str() + pos + pattern.size();
|
||
return std::sscanf(begin, "%lf,\"y\":%lf", x, y) == 2;
|
||
}
|
||
|
||
class GcsExtensionFixture : public ::testing::Test {
|
||
protected:
|
||
void SetUp() override {
|
||
host_ = vde::plugin::make_geometry_host_services();
|
||
PluginManager& manager = PluginManager::instance();
|
||
manager.clear();
|
||
manager.set_host_services(host_);
|
||
manager.clear_search_paths();
|
||
if (VDE_PLUGIN_DIRS[0] != '\0') manager.add_search_path(VDE_PLUGIN_DIRS);
|
||
manager.scan_search_paths();
|
||
}
|
||
void TearDown() override {
|
||
PluginManager::instance().clear();
|
||
vde::plugin::release_geometry_host_services(&host_);
|
||
}
|
||
|
||
bool load_gcs(std::string* error) {
|
||
PluginManager& manager = PluginManager::instance();
|
||
if (manager.is_loaded(kGcsPluginId)) return true;
|
||
return manager.load(kGcsPluginId, error);
|
||
}
|
||
|
||
bool solve(const std::string& sketch_json, std::string* result,
|
||
std::string* error = nullptr,
|
||
const std::string& params_json = std::string()) {
|
||
result->clear();
|
||
return PluginManager::instance().solve_gcs(
|
||
kGcsSolverId, sketch_json.data(), sketch_json.size(), params_json, result,
|
||
error);
|
||
}
|
||
|
||
/// 两点草图:p0 固定原点;p1 初始 (10,0);
|
||
/// 约束:distance(p0,p1)=20 + horizontal(p0,p1) → 期望 p1=(20,0)、dof=0。
|
||
std::string two_point_sketch() const {
|
||
return "{"
|
||
"\"points\":[{\"id\":0,\"x\":0,\"y\":0,\"fixed\":true},"
|
||
"{\"id\":1,\"x\":10,\"y\":0}],"
|
||
"\"constraints\":["
|
||
"{\"type\":\"distance\",\"a\":0,\"b\":1,\"value\":20.0},"
|
||
"{\"type\":\"horizontal\",\"a\":0,\"b\":1}]}";
|
||
}
|
||
|
||
VdePluginHost host_{};
|
||
};
|
||
|
||
} // namespace
|
||
|
||
TEST_F(GcsExtensionFixture, LoadAndQueryGcsSolvers) {
|
||
ASSERT_EQ(VDE_PLUGIN_ABI_MAJOR, 1);
|
||
ASSERT_EQ(VDE_PLUGIN_ABI_MINOR, 8);
|
||
std::string error;
|
||
ASSERT_TRUE(load_gcs(&error)) << error;
|
||
PluginManager& manager = PluginManager::instance();
|
||
EXPECT_TRUE(manager.is_loaded(kGcsPluginId));
|
||
const auto manifest = manager.manifest(kGcsPluginId);
|
||
ASSERT_TRUE(manifest.has_value());
|
||
EXPECT_EQ(manifest->abi_major, 1);
|
||
EXPECT_EQ(manifest->abi_minor, 3);
|
||
EXPECT_STREQ(vde::plugin::plugin_kind_name(manifest->kind), "gcs_solver");
|
||
|
||
const std::vector<GcsSolverInfo> solvers = manager.gcs_solvers();
|
||
ASSERT_EQ(solvers.size(), 1u);
|
||
EXPECT_EQ(solvers[0].plugin_id, kGcsPluginId);
|
||
EXPECT_EQ(solvers[0].solver_id, kGcsSolverId);
|
||
EXPECT_NE(solvers[0].capabilities_json.find("constraint_types"), std::string::npos);
|
||
EXPECT_TRUE(manager.capabilities().has("gcs:solve"));
|
||
}
|
||
|
||
TEST_F(GcsExtensionFixture, SolveDistanceHorizontalConverges) {
|
||
std::string error;
|
||
ASSERT_TRUE(load_gcs(&error)) << error;
|
||
std::string result;
|
||
ASSERT_TRUE(solve(two_point_sketch(), &result, &error)) << error;
|
||
EXPECT_NE(result.find("\"converged\":true"), std::string::npos) << result;
|
||
EXPECT_NE(result.find("\"solver_id\":\"gcs.2d.mock\""), std::string::npos) << result;
|
||
const auto& plugin = PluginManager::instance();
|
||
(void)plugin;
|
||
double dof = -1.0;
|
||
ASSERT_TRUE(extract_number(result, "dof", &dof));
|
||
EXPECT_EQ(dof, 0.0);
|
||
double residual = 1.0;
|
||
ASSERT_TRUE(extract_number(result, "residual", &residual));
|
||
EXPECT_LE(residual, 1e-6);
|
||
double x0 = 99.0, y0 = 99.0, x1 = 0.0, y1 = 99.0;
|
||
ASSERT_TRUE(extract_point(result, 0, &x0, &y0)) << result;
|
||
ASSERT_TRUE(extract_point(result, 1, &x1, &y1)) << result;
|
||
EXPECT_NEAR(x0, 0.0, 1e-9);
|
||
EXPECT_NEAR(y0, 0.0, 1e-9);
|
||
EXPECT_NEAR(x1, 20.0, 1e-6);
|
||
EXPECT_NEAR(y1, 0.0, 1e-6);
|
||
}
|
||
|
||
TEST_F(GcsExtensionFixture, SolveTriangleDofAndConstraintStatus) {
|
||
std::string error;
|
||
ASSERT_TRUE(load_gcs(&error)) << error;
|
||
const std::string sketch =
|
||
"{"
|
||
"\"points\":[{\"id\":0,\"x\":0,\"y\":0,\"fixed\":true},"
|
||
"{\"id\":1,\"x\":10,\"y\":0},{\"id\":2,\"x\":0,\"y\":10}],"
|
||
"\"constraints\":["
|
||
"{\"type\":\"distance\",\"a\":0,\"b\":1,\"value\":20.0},"
|
||
"{\"type\":\"distance\",\"a\":0,\"b\":2,\"value\":15.0},"
|
||
"{\"type\":\"horizontal\",\"a\":1,\"b\":2}]}";
|
||
std::string result;
|
||
ASSERT_TRUE(solve(sketch, &result, &error)) << error;
|
||
EXPECT_NE(result.find("\"converged\":true"), std::string::npos) << result;
|
||
EXPECT_NE(result.find("\"constraint_status\""), std::string::npos) << result;
|
||
double dof = -1.0;
|
||
ASSERT_TRUE(extract_number(result, "dof", &dof));
|
||
EXPECT_EQ(dof, 1.0); // 2*3 - (2 fixed + 1 + 1 + 1) = 1
|
||
double x0 = 1.0, y0 = 1.0;
|
||
ASSERT_TRUE(extract_point(result, 0, &x0, &y0)) << result;
|
||
EXPECT_NEAR(x0, 0.0, 1e-9);
|
||
EXPECT_NEAR(y0, 0.0, 1e-9);
|
||
double x1 = 0.0, y1 = 99.0, x2 = 0.0, y2 = 99.0;
|
||
ASSERT_TRUE(extract_point(result, 1, &x1, &y1)) << result;
|
||
ASSERT_TRUE(extract_point(result, 2, &x2, &y2)) << result;
|
||
EXPECT_NEAR(std::sqrt(x1 * x1 + y1 * y1), 20.0, 1e-5);
|
||
EXPECT_NEAR(std::sqrt(x2 * x2 + y2 * y2), 15.0, 1e-5);
|
||
EXPECT_NEAR(y1, y2, 1e-5);
|
||
}
|
||
|
||
TEST_F(GcsExtensionFixture, ParamsMaxIterationsIsForwarded) {
|
||
std::string error;
|
||
ASSERT_TRUE(load_gcs(&error)) << error;
|
||
// 三点耦合夹具:1 次迭代不足以把残差降到 1e-12(验证 params_json 透传)
|
||
const std::string sketch =
|
||
"{"
|
||
"\"points\":[{\"id\":0,\"x\":0,\"y\":0,\"fixed\":true},"
|
||
"{\"id\":1,\"x\":10,\"y\":0},{\"id\":2,\"x\":0,\"y\":10}],"
|
||
"\"constraints\":["
|
||
"{\"type\":\"distance\",\"a\":0,\"b\":1,\"value\":20.0},"
|
||
"{\"type\":\"distance\",\"a\":0,\"b\":2,\"value\":15.0},"
|
||
"{\"type\":\"horizontal\",\"a\":1,\"b\":2}]}";
|
||
std::string result;
|
||
ASSERT_TRUE(solve(sketch, &result, &error,
|
||
"{\"max_iterations\":1,\"tolerance\":1e-12}"))
|
||
<< error;
|
||
EXPECT_NE(result.find("\"converged\":false"), std::string::npos) << result;
|
||
// 默认参数(500 次迭代)可收敛
|
||
std::string converged;
|
||
ASSERT_TRUE(solve(sketch, &converged, &error)) << error;
|
||
EXPECT_NE(converged.find("\"converged\":true"), std::string::npos) << converged;
|
||
double residual = 1.0;
|
||
ASSERT_TRUE(extract_number(converged, "residual", &residual));
|
||
EXPECT_LE(residual, 1e-6);
|
||
}
|
||
|
||
TEST_F(GcsExtensionFixture, FailureIsolation) {
|
||
std::string error;
|
||
ASSERT_TRUE(load_gcs(&error)) << error;
|
||
std::string result;
|
||
|
||
// 未知 solver id
|
||
EXPECT_FALSE(PluginManager::instance().solve_gcs(
|
||
"gcs.does_not_exist", two_point_sketch().data(), two_point_sketch().size(), "",
|
||
&result, &error));
|
||
EXPECT_FALSE(error.empty());
|
||
EXPECT_NE(error.find("not registered"), std::string::npos) << error;
|
||
|
||
// 坏 JSON
|
||
error.clear();
|
||
const std::string bad_json = "{not a json";
|
||
EXPECT_FALSE(PluginManager::instance().solve_gcs(
|
||
kGcsSolverId, bad_json.data(), bad_json.size(), "", &result, &error));
|
||
EXPECT_FALSE(error.empty());
|
||
|
||
// 不支持的约束类型
|
||
error.clear();
|
||
const std::string bad_constraint =
|
||
"{\"points\":[{\"id\":0,\"x\":0,\"y\":0},{\"id\":1,\"x\":1,\"y\":0}],"
|
||
"\"constraints\":[{\"type\":\"perpendicular\",\"a\":0,\"b\":1}]}";
|
||
EXPECT_FALSE(PluginManager::instance().solve_gcs(
|
||
kGcsSolverId, bad_constraint.data(), bad_constraint.size(), "", &result, &error));
|
||
EXPECT_FALSE(error.empty());
|
||
|
||
// 空草图
|
||
error.clear();
|
||
EXPECT_FALSE(PluginManager::instance().solve_gcs(kGcsSolverId, nullptr, 0, "", &result,
|
||
&error));
|
||
EXPECT_FALSE(error.empty());
|
||
|
||
// 失败后正常求解仍可用
|
||
error.clear();
|
||
ASSERT_TRUE(solve(two_point_sketch(), &result, &error)) << error;
|
||
EXPECT_NE(result.find("\"converged\":true"), std::string::npos);
|
||
}
|
||
|
||
TEST_F(GcsExtensionFixture, UnloadClearsGcsSolvers) {
|
||
std::string error;
|
||
ASSERT_TRUE(load_gcs(&error)) << error;
|
||
ASSERT_EQ(PluginManager::instance().gcs_solvers().size(), 1u);
|
||
ASSERT_TRUE(PluginManager::instance().unload(kGcsPluginId, &error)) << error;
|
||
EXPECT_TRUE(PluginManager::instance().gcs_solvers().empty());
|
||
EXPECT_FALSE(PluginManager::instance().capabilities().has("gcs:solve"));
|
||
std::string result;
|
||
EXPECT_FALSE(PluginManager::instance().solve_gcs(
|
||
kGcsSolverId, two_point_sketch().data(), two_point_sketch().size(), "", &result,
|
||
&error));
|
||
EXPECT_FALSE(error.empty());
|
||
}
|
||
|
||
TEST_F(GcsExtensionFixture, LegacyAbiPluginStillLoads) {
|
||
PluginManager& manager = PluginManager::instance();
|
||
const auto manifest = manager.manifest(kLegacyPluginId);
|
||
ASSERT_TRUE(manifest.has_value()) << "abi11_compat 插件未发现(构建产物缺失?)";
|
||
EXPECT_EQ(manifest->abi_major, 1);
|
||
EXPECT_EQ(manifest->abi_minor, 1);
|
||
std::string error;
|
||
ASSERT_TRUE(manager.load(kLegacyPluginId, &error)) << error;
|
||
EXPECT_TRUE(manager.is_loaded(kLegacyPluginId));
|
||
bool found_format = false;
|
||
for (const auto& format : manager.formats()) {
|
||
if (format.format_id == "ab11") found_format = true;
|
||
}
|
||
EXPECT_TRUE(found_format);
|
||
}
|
||
|
||
TEST_F(GcsExtensionFixture, DirectDllReportsAbi13AndGcsKind) {
|
||
#ifdef _WIN32
|
||
const std::string library_path =
|
||
std::string(VDE_PLUGIN_DIRS) + "/sample_gcs/vde_sample_gcs.dll";
|
||
#else
|
||
const std::string library_path =
|
||
std::string(VDE_PLUGIN_DIRS) + "/sample_gcs/libvde_sample_gcs.so";
|
||
#endif
|
||
vde::platform::LibHandle library = vde::platform::load_library(library_path);
|
||
ASSERT_NE(library, nullptr) << vde::platform::last_error();
|
||
auto query = reinterpret_cast<VdePluginQueryFn>(
|
||
vde::platform::find_symbol(library, VDE_PLUGIN_SYMBOL_QUERY));
|
||
ASSERT_NE(query, nullptr);
|
||
VdePluginInfo info{};
|
||
info.struct_size = sizeof(VdePluginInfo);
|
||
ASSERT_EQ(query(VDE_PLUGIN_ABI_MAJOR, &info), VDE_PLUGIN_OK);
|
||
EXPECT_STREQ(info.id, kGcsPluginId);
|
||
EXPECT_EQ(info.kind, static_cast<uint32_t>(VDE_PLUGIN_KIND_GCS_SOLVER));
|
||
EXPECT_EQ(info.abi_major, static_cast<uint32_t>(VDE_PLUGIN_ABI_MAJOR));
|
||
EXPECT_EQ(info.abi_minor, static_cast<uint32_t>(VDE_PLUGIN_ABI_MINOR));
|
||
vde::platform::unload_library(library);
|
||
}
|