Files
ViewDesignEngine/tests/capi/test_vde_capi.cpp
T
hm b98ca6373c feat(brep): entity/curve/mesh audit fixes, robust numeric inputs (VDE-1329..1340, 1.0.175)
- entity/feature layer: split_body shell IDs, shell_face_ids bounds, empty
  FeatureHistory, multi-body Euler warning, config dependency cycles, undo off-by-one
- VDE-1335: serialize assembly flex joints in additive section 16 with validation
- curves: P0 crashes (degree=0 derivative, empty Bezier, negative index/degree,
  weights mismatch, empty domain), k-order derivative, repeated-knot NaN,
  degenerate self-intersection; stricter constructors and C API boundary guards
- VDE-1337: exact planar trim handles curves enclosing/inside existing holes and
  repeated trims of single-closed-edge outer loops
- mesh: O(1) vertex queries, O(F+V) update_normals (~2035x/36588x), validated
  construction, empty-operand boolean set semantics, save_ply length checks
- VDE-1339/1340: finite guards in primitive builders (NaN/Inf return empty body)
  and in intersection curve fitting (never throw inside OpenMP regions)
- tests: 8,290 -> 8,339; full CTest 8,339/8,339 passed, 0 failed, 35 skipped
- reports: 219, 221 (independent verification), 222 (self verification)
2026-11-06 23:49:19 +08:00

163 lines
6.0 KiB
C++

#include <gtest/gtest.h>
// C API 包含在 extern "C" 块中
extern "C" {
#include "vde/capi/vde_capi.h"
}
#include <limits>
// ═══════════════════════════════════════════════════════════
// VDE C API 基础测试
// 测试引擎生命周期、网格操作和求解器功能
// ═══════════════════════════════════════════════════════════
TEST(VdeCapiTest, CreateAndDestroy) {
VdeHandle ctx = vde_create();
ASSERT_NE(ctx, nullptr);
vde_destroy(ctx);
}
TEST(VdeCapiTest, Version) {
const char* ver = vde_version();
ASSERT_NE(ver, nullptr);
EXPECT_GT(strlen(ver), 0u);
}
TEST(VdeCapiTest, LastErrorInitiallyEmpty) {
VdeHandle ctx = vde_create();
const char* err = vde_last_error(ctx);
// 初始状态:错误串可能为空或 "(none)"
ASSERT_NE(err, nullptr);
vde_destroy(ctx);
}
TEST(VdeCapiTest, LoadMeshInvalidPath) {
VdeHandle ctx = vde_create();
VdeMeshHandle mesh = vde_load_mesh(ctx, "/nonexistent/file.obj");
// 无效路径应返回 NULL
EXPECT_EQ(mesh, nullptr);
const char* err = vde_last_error(ctx);
EXPECT_NE(err, nullptr);
vde_destroy(ctx);
}
TEST(VdeCapiTest, MeshFreeNullSafe) {
// 释放 NULL 句柄应安全(不崩溃)
vde_mesh_free(nullptr);
}
TEST(VdeCapiTest, CreateBezierCurve) {
VdeHandle ctx = vde_create();
double cp[] = {0.0, 0.0, 0.0, 1.0, 2.0, 0.0, 3.0, 0.0, 0.0};
VdeCurveHandle curve = vde_curve_create_bezier(ctx, cp, 3, 3);
ASSERT_NE(curve, nullptr);
// 求值 t=0(应在第一个控制点)
double x, y, z;
vde_curve_evaluate(curve, 0.0, &x, &y, &z);
EXPECT_NEAR(x, 0.0, 1e-9);
EXPECT_NEAR(y, 0.0, 1e-9);
EXPECT_NEAR(z, 0.0, 1e-9);
// 次数应为 n-1 = 2
EXPECT_EQ(vde_curve_degree(curve), 2);
vde_curve_free(curve);
vde_destroy(ctx);
}
TEST(VdeCapiTest, CreateBezierRejectsInvalidInputWithoutThrowing) {
// C ABI 边界:非法参数返回 NULL 而不是抛 C++ 异常。
VdeHandle ctx = vde_create();
double cp[] = {0.0, 0.0, 0.0, 1.0, 2.0, 0.0};
EXPECT_EQ(vde_curve_create_bezier(ctx, cp, 0, 3), nullptr);
EXPECT_EQ(vde_curve_create_bezier(ctx, cp, 1, 3), nullptr);
EXPECT_EQ(vde_curve_create_bezier(ctx, nullptr, 2, 3), nullptr);
double bad[] = {0.0, 0.0, 0.0, 1.0, std::numeric_limits<double>::quiet_NaN(), 0.0};
EXPECT_EQ(vde_curve_create_bezier(ctx, bad, 2, 3), nullptr);
// 2 点 = 一次 Bézier,合法
VdeCurveHandle line = vde_curve_create_bezier(ctx, cp, 2, 3);
ASSERT_NE(line, nullptr);
EXPECT_EQ(vde_curve_degree(line), 1);
vde_curve_free(line);
vde_destroy(ctx);
}
// ═══════════════════════════════════════════════════════════
// A-9: C API mesh format extension (3mf/gltf/glb/off/ply/stl)
// ═══════════════════════════════════════════════════════════
#include <cstdio>
#include <fstream>
#include <string>
namespace {
std::string a9_capi_path(const char* name) {
return std::string(VDE_TEST_TEMP_DIR) + "/" + name;
}
void a9_write_tetra_obj(const std::string& path) {
std::ofstream f(path);
f << "v 0 0 0\nv 1 0 0\nv 0 1 0\nv 0 0 1\n"
"f 1 2 3\nf 1 4 2\nf 2 4 3\nf 1 3 4\n";
}
} // namespace
TEST(VdeCapiFormatA9, MeshSaveLoadRoundTripAllFormats) {
VdeHandle ctx = vde_create();
ASSERT_NE(ctx, nullptr);
const std::string obj = a9_capi_path("a9_capi_src.obj");
a9_write_tetra_obj(obj);
VdeMeshHandle m = vde_load_mesh(ctx, obj.c_str());
ASSERT_NE(m, nullptr) << vde_last_error(ctx);
const unsigned nv = static_cast<unsigned>(vde_mesh_num_vertices(m));
EXPECT_GT(nv, 0u);
struct Fmt { const char* fmt; const char* ext; };
const Fmt formats[] = {{"ply", "ply"}, {"stl", "stl"}, {"obj", "obj"},
{"gltf", "gltf"}, {"glb", "glb"}, {"3mf", "3mf"},
{"off", "off"}};
for (const auto& f : formats) {
const std::string path = a9_capi_path((std::string("a9_capi_out.") + f.ext).c_str());
std::remove(path.c_str());
EXPECT_EQ(vde_save_mesh(ctx, m, path.c_str(), f.fmt), 1)
<< "fmt=" << f.fmt << " err=" << vde_last_error(ctx);
EXPECT_TRUE(std::ifstream(path, std::ios::binary).good()) << "fmt=" << f.fmt;
VdeMeshHandle back = vde_load_mesh(ctx, path.c_str());
EXPECT_NE(back, nullptr) << "fmt=" << f.fmt << " err=" << vde_last_error(ctx);
if (back) {
EXPECT_GT(vde_mesh_num_vertices(back), 0u) << "fmt=" << f.fmt;
vde_mesh_free(back);
}
}
// format string is case-insensitive ("PLY")
const std::string upper = a9_capi_path("a9_capi_upper.PLY");
EXPECT_EQ(vde_save_mesh(ctx, m, upper.c_str(), "PLY"), 1) << vde_last_error(ctx);
vde_mesh_free(m);
vde_destroy(ctx);
}
TEST(VdeCapiFormatA9, UnsupportedFormatsAreExplicitErrors) {
VdeHandle ctx = vde_create();
const std::string obj = a9_capi_path("a9_capi_src2.obj");
a9_write_tetra_obj(obj);
VdeMeshHandle m = vde_load_mesh(ctx, obj.c_str());
ASSERT_NE(m, nullptr) << vde_last_error(ctx);
const std::string out = a9_capi_path("a9_capi_unsupported.xyz");
std::remove(out.c_str());
EXPECT_EQ(vde_save_mesh(ctx, m, out.c_str(), "xyz"), 0);
const std::string err = vde_last_error(ctx);
EXPECT_NE(err.find("不支持"), std::string::npos) << err;
EXPECT_FALSE(std::ifstream(out).good());
const std::string bogus = a9_capi_path("a9_capi_bogus.xyz");
std::ofstream(bogus) << "junk";
EXPECT_EQ(vde_load_mesh(ctx, bogus.c_str()), nullptr);
const std::string lerr = vde_last_error(ctx);
EXPECT_NE(lerr.find("不支持"), std::string::npos) << lerr;
vde_mesh_free(m);
vde_destroy(ctx);
}