673 lines
26 KiB
C++
673 lines
26 KiB
C++
// WP-P4-52 官方 IGES 插件写能力测试(确定性 mock 宿主 + 真实 C ABI 动态加载)。
|
||
//
|
||
// 覆盖:
|
||
// 1) vde_plugin_query / init / describe:id、能力(read+write)、extensions、ABI;
|
||
// 2) 写出:host body_tessellate → IGES 5.3 ASCII(80 列)faceted B-Rep:
|
||
// S/G/D/P/T 五段、Global 固定版本/单位/日期、D 段双 80 列(DE 指针/参数计数)、
|
||
// P 段 502 Vertex / 504 Edge / 510 Face(引用 DE + 方向)、T 段计数;
|
||
// 3) 多 body、顶点/边去重、write_file=write_memory、确定性输出;
|
||
// 4) 错误路径:空实体、空三角化、tessellate 失败、非法路径、宿主能力缺失、body 数超限;
|
||
// 5) 读能力保持(read_memory 仍 UNSUPPORTED;probe 80 列 S 头=95)。
|
||
//
|
||
// 本测试只依赖纯 C ABI(vde_plugin_api.h)+ gtest,不链接内核。
|
||
#include <gtest/gtest.h>
|
||
|
||
#include <algorithm>
|
||
#include <cctype>
|
||
#include <cmath>
|
||
#include <cstdint>
|
||
#include <cstdlib>
|
||
#include <cstring>
|
||
#include <fstream>
|
||
#include <map>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
#if defined(_WIN32)
|
||
#ifndef NOMINMAX
|
||
#define NOMINMAX
|
||
#endif
|
||
#include <windows.h>
|
||
#else
|
||
#include <dlfcn.h>
|
||
#endif
|
||
|
||
#include "vde/plugin/vde_plugin_api.h"
|
||
|
||
#ifndef VDE_PLUGIN_OFFICIAL_DIR
|
||
#define VDE_PLUGIN_OFFICIAL_DIR ""
|
||
#endif
|
||
#ifndef VDE_TEST_TEMP_DIR
|
||
#define VDE_TEST_TEMP_DIR ""
|
||
#endif
|
||
|
||
namespace {
|
||
|
||
constexpr const char* kPluginId = "com.vde.official.iges";
|
||
constexpr std::size_t kMaxBodyCount = 10000;
|
||
|
||
#if defined(_WIN32)
|
||
constexpr const char* kPluginRelativePath = "/iges/vde_official_iges.dll";
|
||
#elif defined(__APPLE__)
|
||
constexpr const char* kPluginRelativePath = "/iges/libvde_official_iges.dylib";
|
||
#else
|
||
constexpr const char* kPluginRelativePath = "/iges/libvde_official_iges.so";
|
||
#endif
|
||
|
||
// ── 动态加载(与宿主相同的纯 C ABI 入口)────────────────────────────
|
||
#if defined(_WIN32)
|
||
using LibraryHandle = HMODULE;
|
||
using RawSymbol = void (*)();
|
||
LibraryHandle open_library(const char* path) { return ::LoadLibraryA(path); }
|
||
RawSymbol find_symbol(LibraryHandle library, const char* name) {
|
||
return reinterpret_cast<RawSymbol>(::GetProcAddress(library, name));
|
||
}
|
||
void close_library(LibraryHandle library) {
|
||
if (library) ::FreeLibrary(library);
|
||
}
|
||
#else
|
||
using LibraryHandle = void*;
|
||
using RawSymbol = void (*)();
|
||
LibraryHandle open_library(const char* path) {
|
||
return ::dlopen(path, RTLD_NOW | RTLD_LOCAL);
|
||
}
|
||
RawSymbol find_symbol(LibraryHandle library, const char* name) {
|
||
return reinterpret_cast<RawSymbol>(::dlsym(library, name));
|
||
}
|
||
void close_library(LibraryHandle library) {
|
||
if (library) ::dlclose(library);
|
||
}
|
||
#endif
|
||
|
||
struct PluginApi {
|
||
LibraryHandle library = nullptr;
|
||
VdePluginQueryFn query = nullptr;
|
||
VdePluginInitFn init = nullptr;
|
||
VdePluginShutdownFn shutdown = nullptr;
|
||
const VdeFormatPlugin* format = nullptr;
|
||
|
||
~PluginApi() { close(); }
|
||
|
||
bool open(std::string* error) {
|
||
const std::string path = std::string(VDE_PLUGIN_OFFICIAL_DIR) + kPluginRelativePath;
|
||
library = open_library(path.c_str());
|
||
if (!library) {
|
||
if (error) *error = "无法加载插件动态库: " + path;
|
||
return false;
|
||
}
|
||
query = reinterpret_cast<VdePluginQueryFn>(find_symbol(library, "vde_plugin_query"));
|
||
init = reinterpret_cast<VdePluginInitFn>(find_symbol(library, "vde_plugin_init"));
|
||
shutdown = reinterpret_cast<VdePluginShutdownFn>(
|
||
find_symbol(library, "vde_plugin_shutdown"));
|
||
if (!query || !init || !shutdown) {
|
||
if (error) *error = "插件缺少 vde_plugin_query/init/shutdown 导出: " + path;
|
||
close();
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
void close() {
|
||
if (library) {
|
||
close_library(library);
|
||
library = nullptr;
|
||
}
|
||
query = nullptr;
|
||
init = nullptr;
|
||
shutdown = nullptr;
|
||
format = nullptr;
|
||
}
|
||
};
|
||
|
||
// ── 确定性 mock 宿主 ────────────────────────────────────────────────
|
||
std::string g_last_error;
|
||
int g_live_bodies = 0;
|
||
|
||
struct MockBody {
|
||
std::vector<double> positions; // 3×N
|
||
std::vector<std::uint32_t> indices; // 3×T
|
||
bool fail_tessellate = false;
|
||
bool empty_tessellate = false;
|
||
};
|
||
|
||
MockBody* as_mock(VdeBody body) { return reinterpret_cast<MockBody*>(body); }
|
||
|
||
void mock_log(int, const char*) {}
|
||
void mock_set_error(const char* message) { g_last_error = message ? message : ""; }
|
||
void* mock_alloc(std::size_t size) { return std::malloc(size == 0 ? 1 : size); }
|
||
void* mock_realloc(void* pointer, std::size_t size) {
|
||
return std::realloc(pointer, size == 0 ? 1 : size);
|
||
}
|
||
void mock_free(void* pointer) { std::free(pointer); }
|
||
void mock_free_buffer(void* pointer) { std::free(pointer); }
|
||
|
||
VdeBody mock_body_from_mesh(const double* positions, std::size_t num_vertices,
|
||
const std::uint32_t* indices, std::size_t num_triangles) {
|
||
if (!positions || num_vertices == 0) {
|
||
g_last_error = "mock body_from_mesh: 空网格";
|
||
return nullptr;
|
||
}
|
||
if (num_triangles > 0 && !indices) {
|
||
g_last_error = "mock body_from_mesh: 缺少索引";
|
||
return nullptr;
|
||
}
|
||
for (std::size_t i = 0; i < num_triangles * 3u; ++i) {
|
||
if (static_cast<std::size_t>(indices[i]) >= num_vertices) {
|
||
g_last_error = "mock body_from_mesh: 索引越界";
|
||
return nullptr;
|
||
}
|
||
}
|
||
auto* body = new MockBody();
|
||
body->positions.assign(positions, positions + num_vertices * 3u);
|
||
if (num_triangles > 0) body->indices.assign(indices, indices + num_triangles * 3u);
|
||
++g_live_bodies;
|
||
return reinterpret_cast<VdeBody>(body);
|
||
}
|
||
|
||
void mock_body_destroy(VdeBody handle) {
|
||
auto* body = as_mock(handle);
|
||
if (!body) return;
|
||
delete body;
|
||
--g_live_bodies;
|
||
}
|
||
|
||
int mock_body_tessellate(VdeBody handle, double, VdeMeshData* out) {
|
||
if (!out || !handle) {
|
||
g_last_error = "mock body_tessellate: 参数为空";
|
||
return VDE_PLUGIN_ERR_INVALID_ARG;
|
||
}
|
||
MockBody* body = as_mock(handle);
|
||
out->positions = nullptr;
|
||
out->num_vertices = 0;
|
||
out->indices = nullptr;
|
||
out->num_triangles = 0;
|
||
out->normals = nullptr;
|
||
if (body->fail_tessellate) {
|
||
g_last_error = "mock body_tessellate: 注入失败";
|
||
return VDE_PLUGIN_ERR_INTERNAL;
|
||
}
|
||
if (body->empty_tessellate || body->indices.empty()) return VDE_PLUGIN_OK;
|
||
const std::size_t triangles = body->indices.size() / 3u;
|
||
const std::size_t vertices = triangles * 3u;
|
||
auto* positions = static_cast<double*>(std::malloc(vertices * 3u * sizeof(double)));
|
||
auto* indices = static_cast<std::uint32_t*>(std::malloc(vertices * sizeof(std::uint32_t)));
|
||
if (!positions || !indices) {
|
||
std::free(positions);
|
||
std::free(indices);
|
||
g_last_error = "mock body_tessellate: 内存不足";
|
||
return VDE_PLUGIN_ERR_BUFFER;
|
||
}
|
||
for (std::size_t t = 0; t < triangles; ++t) {
|
||
for (std::size_t k = 0; k < 3; ++k) {
|
||
const std::uint32_t source = body->indices[3u * t + k];
|
||
positions[(3u * t + k) * 3u + 0u] = body->positions[3u * source + 0u];
|
||
positions[(3u * t + k) * 3u + 1u] = body->positions[3u * source + 1u];
|
||
positions[(3u * t + k) * 3u + 2u] = body->positions[3u * source + 2u];
|
||
indices[3u * t + k] = static_cast<std::uint32_t>(3u * t + k);
|
||
}
|
||
}
|
||
out->positions = positions;
|
||
out->num_vertices = vertices;
|
||
out->indices = indices;
|
||
out->num_triangles = triangles;
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
VdePluginHost make_mock_host() {
|
||
VdePluginHost host{};
|
||
host.struct_size = static_cast<std::uint32_t>(sizeof(VdePluginHost));
|
||
host.abi_major = VDE_PLUGIN_ABI_MAJOR;
|
||
host.abi_minor = VDE_PLUGIN_ABI_MINOR;
|
||
host.kernel_version = "mock";
|
||
host.alloc = &mock_alloc;
|
||
host.realloc = &mock_realloc;
|
||
host.free = &mock_free;
|
||
host.log = &mock_log;
|
||
host.set_error = &mock_set_error;
|
||
host.body_destroy = &mock_body_destroy;
|
||
host.free_buffer = &mock_free_buffer;
|
||
host.body_from_mesh = &mock_body_from_mesh;
|
||
host.body_tessellate = &mock_body_tessellate;
|
||
return host;
|
||
}
|
||
|
||
// ── IGES 80 列解析辅助 ─────────────────────────────────────────────
|
||
std::vector<std::string> split_records(const std::string& text) {
|
||
std::vector<std::string> lines;
|
||
std::size_t pos = 0;
|
||
while (pos < text.size()) {
|
||
const std::size_t end = text.find('\n', pos);
|
||
if (end == std::string::npos) {
|
||
if (pos < text.size()) lines.push_back(text.substr(pos));
|
||
break;
|
||
}
|
||
lines.push_back(text.substr(pos, end - pos));
|
||
pos = end + 1;
|
||
}
|
||
return lines;
|
||
}
|
||
|
||
int field_int(const std::string& record, std::size_t offset, std::size_t width) {
|
||
if (offset + width > record.size()) return 0;
|
||
return std::atoi(record.substr(offset, width).c_str());
|
||
}
|
||
|
||
int section_seq(const std::string& record) {
|
||
return record.size() >= 80 ? std::atoi(record.substr(73, 7).c_str()) : 0;
|
||
}
|
||
|
||
char section_char(const std::string& record) {
|
||
return record.size() >= 80 ? record[72] : '\0';
|
||
}
|
||
|
||
struct IgesFile {
|
||
std::vector<std::string> records;
|
||
std::vector<std::string> start;
|
||
std::vector<std::string> global;
|
||
std::vector<std::string> directory;
|
||
std::vector<std::string> parameters;
|
||
std::string terminate;
|
||
};
|
||
|
||
bool parse_iges(const std::string& text, IgesFile* file, std::string* error) {
|
||
file->records = split_records(text);
|
||
for (const auto& record : file->records) {
|
||
if (record.size() != 80) {
|
||
if (error) *error = "记录不是 80 列: " + std::to_string(record.size());
|
||
return false;
|
||
}
|
||
switch (section_char(record)) {
|
||
case 'S': file->start.push_back(record); break;
|
||
case 'G': file->global.push_back(record); break;
|
||
case 'D': file->directory.push_back(record); break;
|
||
case 'P': file->parameters.push_back(record); break;
|
||
case 'T': file->terminate = record; break;
|
||
default:
|
||
if (error) *error = "未知段字母";
|
||
return false;
|
||
}
|
||
}
|
||
if (file->terminate.empty()) {
|
||
if (error) *error = "缺少 Terminate 段";
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
int count_entities(const IgesFile& file, int entity_type) {
|
||
int count = 0;
|
||
for (std::size_t i = 0; i + 1 < file.directory.size(); i += 2) {
|
||
if (field_int(file.directory[i], 0, 8) == entity_type) ++count;
|
||
}
|
||
return count;
|
||
}
|
||
|
||
std::string temp_path(const char* name) { return std::string(VDE_TEST_TEMP_DIR) + name; }
|
||
|
||
std::string read_text_file(const std::string& path) {
|
||
std::ifstream in(path, std::ios::binary);
|
||
if (!in.good()) return {};
|
||
return std::string((std::istreambuf_iterator<char>(in)),
|
||
std::istreambuf_iterator<char>());
|
||
}
|
||
|
||
void remove_file(const std::string& path) { std::remove(path.c_str()); }
|
||
|
||
// ── 测试夹具 ────────────────────────────────────────────────────────
|
||
class IgesWriteFixture : public ::testing::Test {
|
||
protected:
|
||
void SetUp() override {
|
||
g_last_error.clear();
|
||
g_live_bodies = 0;
|
||
host_ = make_mock_host();
|
||
ASSERT_TRUE(api_.open(&load_error_)) << load_error_;
|
||
VdePluginInfo info{};
|
||
info.struct_size = static_cast<std::uint32_t>(sizeof(VdePluginInfo));
|
||
ASSERT_EQ(api_.query(VDE_PLUGIN_ABI_MAJOR, &info), VDE_PLUGIN_OK);
|
||
ASSERT_STREQ(info.id, kPluginId);
|
||
VdePluginRegistration registration{};
|
||
registration.struct_size = static_cast<std::uint32_t>(sizeof(VdePluginRegistration));
|
||
ASSERT_EQ(api_.init(&host_, ®istration), VDE_PLUGIN_OK) << g_last_error;
|
||
ASSERT_EQ(registration.kind, VDE_PLUGIN_KIND_FORMAT);
|
||
ASSERT_NE(registration.format, nullptr);
|
||
format_ = registration.format;
|
||
}
|
||
|
||
void TearDown() override {
|
||
if (api_.shutdown) api_.shutdown();
|
||
api_.close();
|
||
EXPECT_EQ(g_live_bodies, 0) << "mock 实体未全部销毁";
|
||
}
|
||
|
||
MockBody make_triangle(double offset = 0.0) {
|
||
MockBody body;
|
||
body.positions = {0.0 + offset, 0.0, 0.0, 1.0 + offset, 0.0, 0.0,
|
||
0.0 + offset, 1.0, 0.0};
|
||
body.indices = {0u, 1u, 2u};
|
||
return body;
|
||
}
|
||
|
||
MockBody make_quad() {
|
||
MockBody body;
|
||
body.positions = {0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0};
|
||
body.indices = {0u, 1u, 2u, 0u, 2u, 3u};
|
||
return body;
|
||
}
|
||
|
||
VdeBody make_body(const MockBody& source) {
|
||
return host_.body_from_mesh(source.positions.data(),
|
||
source.positions.size() / 3u,
|
||
source.indices.data(), source.indices.size() / 3u);
|
||
}
|
||
|
||
std::vector<VdeBody> make_triangle_bodies(std::size_t count, double step = 0.0) {
|
||
std::vector<VdeBody> bodies;
|
||
bodies.reserve(count);
|
||
for (std::size_t i = 0; i < count; ++i)
|
||
bodies.push_back(make_body(make_triangle(step * static_cast<double>(i))));
|
||
return bodies;
|
||
}
|
||
|
||
void destroy_bodies(std::vector<VdeBody>& bodies) {
|
||
for (VdeBody body : bodies) host_.body_destroy(body);
|
||
bodies.clear();
|
||
}
|
||
|
||
std::string write_memory_text(const std::vector<VdeBody>& bodies) {
|
||
VdeBuffer buffer{};
|
||
g_last_error.clear();
|
||
EXPECT_EQ(format_->write_memory(&host_, bodies.data(), bodies.size(), &buffer),
|
||
VDE_PLUGIN_OK) << g_last_error;
|
||
if (buffer.data == nullptr) return std::string();
|
||
const std::string text(static_cast<const char*>(buffer.data), buffer.size);
|
||
host_.free(buffer.data);
|
||
return text;
|
||
}
|
||
|
||
PluginApi api_;
|
||
std::string load_error_;
|
||
VdePluginHost host_{};
|
||
const VdeFormatPlugin* format_ = nullptr;
|
||
};
|
||
|
||
} // namespace
|
||
|
||
// ── 1. 元信息 / 注册 ───────────────────────────────────────────────
|
||
TEST_F(IgesWriteFixture, QueryAndRegistrationMetadata) {
|
||
VdePluginInfo info{};
|
||
info.struct_size = static_cast<std::uint32_t>(sizeof(VdePluginInfo));
|
||
ASSERT_EQ(api_.query(VDE_PLUGIN_ABI_MAJOR, &info), VDE_PLUGIN_OK);
|
||
EXPECT_STREQ(info.id, "com.vde.official.iges");
|
||
EXPECT_EQ(info.kind, VDE_PLUGIN_KIND_FORMAT);
|
||
EXPECT_EQ(info.abi_major, VDE_PLUGIN_ABI_MAJOR);
|
||
EXPECT_STREQ(info.capabilities, "format:iges.read;format:iges.write");
|
||
ASSERT_NE(format_, nullptr);
|
||
EXPECT_STREQ(format_->format_id, "iges");
|
||
EXPECT_STREQ(format_->extensions, ".iges;.igs");
|
||
EXPECT_NE(format_->probe, nullptr);
|
||
EXPECT_NE(format_->read_file, nullptr);
|
||
EXPECT_NE(format_->write_file, nullptr);
|
||
EXPECT_NE(format_->write_memory, nullptr);
|
||
ASSERT_NE(format_->describe, nullptr);
|
||
const std::string describe = format_->describe();
|
||
EXPECT_NE(describe.find("write"), std::string::npos) << describe;
|
||
EXPECT_NE(describe.find("faceted"), std::string::npos) << describe;
|
||
}
|
||
|
||
TEST_F(IgesWriteFixture, ProbeAndReadMemoryBehaviourKept) {
|
||
std::string head(80, ' ');
|
||
head[72] = 'S';
|
||
head[73] = '0';
|
||
head[74] = '0';
|
||
head[75] = '0';
|
||
head[76] = '0';
|
||
head[77] = '0';
|
||
head[78] = '0';
|
||
head[79] = '1';
|
||
EXPECT_EQ(format_->probe(head.data(), head.size()), 95);
|
||
VdeBody* bodies = nullptr;
|
||
std::size_t count = 0;
|
||
EXPECT_EQ(format_->read_memory(&host_, head.data(), head.size(), &bodies, &count),
|
||
VDE_PLUGIN_ERR_UNSUPPORTED);
|
||
}
|
||
|
||
// ── 2. 段结构与 80 列 ──────────────────────────────────────────────
|
||
TEST_F(IgesWriteFixture, SectionsAndColumnFormat) {
|
||
std::vector<VdeBody> bodies{make_body(make_triangle())};
|
||
ASSERT_NE(bodies[0], nullptr) << g_last_error;
|
||
const std::string text = write_memory_text(bodies);
|
||
destroy_bodies(bodies);
|
||
ASSERT_FALSE(text.empty());
|
||
|
||
IgesFile file;
|
||
std::string error;
|
||
ASSERT_TRUE(parse_iges(text, &file, &error)) << error;
|
||
EXPECT_FALSE(file.start.empty()) << "缺少 S 段";
|
||
EXPECT_FALSE(file.global.empty()) << "缺少 G 段";
|
||
EXPECT_FALSE(file.directory.empty()) << "缺少 D 段";
|
||
EXPECT_FALSE(file.parameters.empty()) << "缺少 P 段";
|
||
EXPECT_FALSE(file.terminate.empty()) << "缺少 T 段";
|
||
|
||
// S 段内容
|
||
std::string start_all;
|
||
for (const auto& record : file.start) start_all += record.substr(0, 72);
|
||
EXPECT_NE(start_all.find("ViewDesignEngine"), std::string::npos);
|
||
// G 段:固定版本/单位/日期,确定性
|
||
std::string global_all;
|
||
for (const auto& record : file.global) global_all += record.substr(0, 72);
|
||
EXPECT_NE(global_all.find("VDENGINE.IGS"), std::string::npos);
|
||
EXPECT_NE(global_all.find("2HMM"), std::string::npos);
|
||
EXPECT_NE(global_all.find("2026-10-31.001"), std::string::npos);
|
||
// 段内序号连续
|
||
for (std::size_t i = 0; i < file.start.size(); ++i)
|
||
EXPECT_EQ(section_seq(file.start[i]), static_cast<int>(i + 1));
|
||
for (std::size_t i = 0; i < file.global.size(); ++i)
|
||
EXPECT_EQ(section_seq(file.global[i]), static_cast<int>(i + 1));
|
||
for (std::size_t i = 0; i < file.parameters.size(); ++i)
|
||
EXPECT_EQ(section_seq(file.parameters[i]), static_cast<int>(i + 1));
|
||
// D 段双记录:每个实体两行、类型一致、DE 指针/参数计数完整
|
||
ASSERT_EQ(file.directory.size() % 2, 0u);
|
||
std::map<int, int> p_count_by_de;
|
||
for (std::size_t i = 0; i + 1 < file.directory.size(); i += 2) {
|
||
const int type1 = field_int(file.directory[i], 0, 8);
|
||
const int type2 = field_int(file.directory[i + 1], 0, 8);
|
||
EXPECT_EQ(type1, type2);
|
||
EXPECT_GT(field_int(file.directory[i], 8, 8), 0); // PD start
|
||
p_count_by_de[section_seq(file.directory[i])] =
|
||
field_int(file.directory[i + 1], 24, 8);
|
||
}
|
||
// P 段每行 DE 指针(65-72)与 P 计数一致
|
||
for (const auto& record : file.parameters) {
|
||
const int de = std::atoi(record.substr(64, 8).c_str());
|
||
EXPECT_GT(de, 0);
|
||
ASSERT_TRUE(p_count_by_de.count(de)) << "P 段 DE 指针无对应 D 段: " << de;
|
||
}
|
||
// T 段计数与实际一致
|
||
const std::string term = file.terminate.substr(0, 72);
|
||
ASSERT_GE(term.size(), 32u);
|
||
EXPECT_EQ(std::atoi(term.substr(1, 7).c_str()), static_cast<int>(file.start.size()));
|
||
EXPECT_EQ(std::atoi(term.substr(9, 7).c_str()), static_cast<int>(file.global.size()));
|
||
EXPECT_EQ(std::atoi(term.substr(17, 7).c_str()), static_cast<int>(file.directory.size()));
|
||
EXPECT_EQ(std::atoi(term.substr(25, 7).c_str()), static_cast<int>(file.parameters.size()));
|
||
}
|
||
|
||
TEST_F(IgesWriteFixture, SingleTriangleEntitiesAndReferences) {
|
||
std::vector<VdeBody> bodies{make_body(make_triangle())};
|
||
ASSERT_NE(bodies[0], nullptr);
|
||
const std::string text = write_memory_text(bodies);
|
||
destroy_bodies(bodies);
|
||
IgesFile file;
|
||
std::string error;
|
||
ASSERT_TRUE(parse_iges(text, &file, &error)) << error;
|
||
|
||
ASSERT_EQ(file.directory.size(), 14u); // (3×502 + 3×504 + 1×510) × 2 行
|
||
EXPECT_EQ(count_entities(file, 502), 3);
|
||
EXPECT_EQ(count_entities(file, 504), 3);
|
||
EXPECT_EQ(count_entities(file, 510), 1);
|
||
EXPECT_EQ(file.parameters.size(), 7u);
|
||
|
||
// 收集 DE 指针和参数
|
||
std::map<int, std::string> params_by_de;
|
||
for (const auto& record : file.parameters) {
|
||
const int de = std::atoi(record.substr(64, 8).c_str());
|
||
const std::string params = record.substr(0, 64);
|
||
params_by_de[de] += params;
|
||
}
|
||
std::map<int, int> type_by_de;
|
||
for (std::size_t i = 0; i + 1 < file.directory.size(); i += 2) {
|
||
const int de = section_seq(file.directory[i]);
|
||
type_by_de[de] = field_int(file.directory[i], 0, 8);
|
||
}
|
||
// 504 参数引用两个 502 DE;510 参数引用三个 504 DE 与方向 ±1
|
||
for (const auto& [de, type] : type_by_de) {
|
||
if (type != 504) continue;
|
||
const std::string& params = params_by_de[de];
|
||
// 0,0,<start>,1,<end>,1,0,0.0,0.0
|
||
const std::size_t p1 = params.find(',', params.find(',', params.find(',') + 1) + 1);
|
||
EXPECT_NE(p1, std::string::npos);
|
||
// 至少 5 个逗号字段
|
||
EXPECT_GE(std::count(params.begin(), params.end(), ','), 5);
|
||
}
|
||
int face_de_count = 0;
|
||
for (const auto& [de, type] : type_by_de) {
|
||
if (type != 510) continue;
|
||
++face_de_count;
|
||
const std::string& params = params_by_de[de];
|
||
const int edges = std::atoi(params.c_str() + params.find(',') + 1);
|
||
EXPECT_EQ(edges, 3);
|
||
// 引用 3 个 504 DE
|
||
std::size_t refs = 0;
|
||
std::size_t pos = 0;
|
||
while ((pos = params.find(',', pos)) != std::string::npos) {
|
||
++pos;
|
||
const int value = std::atoi(params.c_str() + pos);
|
||
if (value > 0 && type_by_de.count(value) && type_by_de[value] == 504) ++refs;
|
||
}
|
||
EXPECT_EQ(refs, 3u) << params;
|
||
}
|
||
EXPECT_EQ(face_de_count, 1);
|
||
}
|
||
|
||
TEST_F(IgesWriteFixture, QuadDedupesVerticesAndEdges) {
|
||
std::vector<VdeBody> bodies{make_body(make_quad())};
|
||
ASSERT_NE(bodies[0], nullptr);
|
||
const std::string text = write_memory_text(bodies);
|
||
destroy_bodies(bodies);
|
||
IgesFile file;
|
||
std::string error;
|
||
ASSERT_TRUE(parse_iges(text, &file, &error)) << error;
|
||
EXPECT_EQ(count_entities(file, 502), 4); // 4 个共享顶点
|
||
EXPECT_EQ(count_entities(file, 504), 5); // 2 三角共享对角线
|
||
EXPECT_EQ(count_entities(file, 510), 2); // 2 个三角面
|
||
}
|
||
|
||
// ── 3. 多 body / 确定性 / 文件一致 ─────────────────────────────────
|
||
TEST_F(IgesWriteFixture, MultiBodyDeterministicAndFileEqualsMemory) {
|
||
std::vector<VdeBody> bodies = make_triangle_bodies(2, 100.0);
|
||
for (const VdeBody body : bodies) ASSERT_NE(body, nullptr) << g_last_error;
|
||
const std::string first = write_memory_text(bodies);
|
||
const std::string second = write_memory_text(bodies);
|
||
EXPECT_EQ(first, second) << "同一输入写出应确定性一致";
|
||
ASSERT_FALSE(first.empty());
|
||
|
||
IgesFile file;
|
||
std::string error;
|
||
ASSERT_TRUE(parse_iges(first, &file, &error)) << error;
|
||
EXPECT_EQ(count_entities(file, 502), 6);
|
||
EXPECT_EQ(count_entities(file, 504), 6);
|
||
EXPECT_EQ(count_entities(file, 510), 2);
|
||
|
||
const std::string path = temp_path("p452_iges_equals.igs");
|
||
g_last_error.clear();
|
||
ASSERT_EQ(format_->write_file(&host_, path.c_str(), bodies.data(), bodies.size()),
|
||
VDE_PLUGIN_OK) << g_last_error;
|
||
EXPECT_EQ(read_text_file(path), first) << "write_file 与 write_memory 字节应一致";
|
||
remove_file(path);
|
||
destroy_bodies(bodies);
|
||
}
|
||
|
||
// ── 4. 错误路径 ────────────────────────────────────────────────────
|
||
TEST_F(IgesWriteFixture, EmptyAndInvalidWriteArguments) {
|
||
VdeBuffer buffer{};
|
||
EXPECT_EQ(format_->write_memory(&host_, nullptr, 0, &buffer),
|
||
VDE_PLUGIN_ERR_INVALID_ARG);
|
||
EXPECT_EQ(buffer.data, nullptr);
|
||
|
||
std::vector<VdeBody> bodies{make_body(make_triangle())};
|
||
ASSERT_NE(bodies[0], nullptr);
|
||
EXPECT_EQ(format_->write_memory(&host_, bodies.data(), 0, &buffer),
|
||
VDE_PLUGIN_ERR_INVALID_ARG);
|
||
EXPECT_EQ(format_->write_memory(&host_, bodies.data(), bodies.size(), nullptr),
|
||
VDE_PLUGIN_ERR_INVALID_ARG);
|
||
EXPECT_EQ(format_->write_file(&host_, nullptr, bodies.data(), bodies.size()),
|
||
VDE_PLUGIN_ERR_INVALID_ARG);
|
||
EXPECT_EQ(format_->write_file(&host_, temp_path("p452_nodata.igs").c_str(), nullptr, 0),
|
||
VDE_PLUGIN_ERR_INVALID_ARG);
|
||
destroy_bodies(bodies);
|
||
}
|
||
|
||
TEST_F(IgesWriteFixture, EmptyOrFailedTessellationRejected) {
|
||
MockBody triangle = make_triangle();
|
||
std::vector<VdeBody> empty_body{make_body(triangle)};
|
||
ASSERT_NE(empty_body[0], nullptr);
|
||
as_mock(empty_body[0])->empty_tessellate = true;
|
||
VdeBuffer buffer{};
|
||
g_last_error.clear();
|
||
EXPECT_EQ(format_->write_memory(&host_, empty_body.data(), empty_body.size(), &buffer),
|
||
VDE_PLUGIN_ERR_INTERNAL);
|
||
EXPECT_EQ(buffer.data, nullptr);
|
||
EXPECT_FALSE(g_last_error.empty());
|
||
destroy_bodies(empty_body);
|
||
|
||
std::vector<VdeBody> failing{make_body(triangle)};
|
||
ASSERT_NE(failing[0], nullptr);
|
||
as_mock(failing[0])->fail_tessellate = true;
|
||
g_last_error.clear();
|
||
EXPECT_EQ(format_->write_memory(&host_, failing.data(), failing.size(), &buffer),
|
||
VDE_PLUGIN_ERR_INTERNAL);
|
||
EXPECT_EQ(buffer.data, nullptr);
|
||
EXPECT_FALSE(g_last_error.empty());
|
||
destroy_bodies(failing);
|
||
}
|
||
|
||
TEST_F(IgesWriteFixture, MissingHostWriteServicesRejected) {
|
||
std::vector<VdeBody> bodies{make_body(make_triangle())};
|
||
ASSERT_NE(bodies[0], nullptr);
|
||
VdePluginHost no_tessellate = host_;
|
||
no_tessellate.body_tessellate = nullptr;
|
||
VdeBuffer buffer{};
|
||
EXPECT_EQ(format_->write_memory(&no_tessellate, bodies.data(), bodies.size(), &buffer),
|
||
VDE_PLUGIN_ERR_UNSUPPORTED);
|
||
VdePluginHost no_free_buffer = host_;
|
||
no_free_buffer.free_buffer = nullptr;
|
||
EXPECT_EQ(format_->write_memory(&no_free_buffer, bodies.data(), bodies.size(), &buffer),
|
||
VDE_PLUGIN_ERR_UNSUPPORTED);
|
||
destroy_bodies(bodies);
|
||
}
|
||
|
||
TEST_F(IgesWriteFixture, BodyCountLimitRejected) {
|
||
std::vector<VdeBody> bodies = make_triangle_bodies(kMaxBodyCount + 1);
|
||
for (const VdeBody body : bodies) ASSERT_NE(body, nullptr);
|
||
VdeBuffer buffer{};
|
||
g_last_error.clear();
|
||
EXPECT_EQ(format_->write_memory(&host_, bodies.data(), bodies.size(), &buffer),
|
||
VDE_PLUGIN_ERR_INVALID_ARG);
|
||
EXPECT_EQ(buffer.data, nullptr);
|
||
EXPECT_NE(g_last_error.find("上限"), std::string::npos) << g_last_error;
|
||
destroy_bodies(bodies);
|
||
}
|
||
|
||
TEST_F(IgesWriteFixture, NullAndBadEntryArguments) {
|
||
VdePluginInfo info{};
|
||
info.struct_size = static_cast<std::uint32_t>(sizeof(VdePluginInfo));
|
||
EXPECT_EQ(api_.query(VDE_PLUGIN_ABI_MAJOR + 1u, &info), VDE_PLUGIN_ERR_ABI);
|
||
EXPECT_EQ(api_.query(VDE_PLUGIN_ABI_MAJOR, nullptr), VDE_PLUGIN_ERR_ABI);
|
||
|
||
VdePluginRegistration registration{};
|
||
registration.struct_size = static_cast<std::uint32_t>(sizeof(VdePluginRegistration));
|
||
EXPECT_EQ(api_.init(nullptr, ®istration), VDE_PLUGIN_ERR_INVALID_ARG);
|
||
EXPECT_EQ(api_.init(&host_, nullptr), VDE_PLUGIN_ERR_ABI);
|
||
registration.struct_size = 1u;
|
||
EXPECT_EQ(api_.init(&host_, ®istration), VDE_PLUGIN_ERR_ABI);
|
||
}
|