Files
ViewDesignEngine/tests/plugin/test_official_step_write.cpp
T

692 lines
29 KiB
C++
Raw 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.
// WP-P4-50 官方 STEP 插件写能力测试(确定性 mock 宿主 + 真实 C ABI 动态加载)。
//
// 覆盖:
// 1) vde_plugin_query / init / describe:id、能力(read+write)、extensions、ABI;
// 2) 写出:host body_tessellate → ISO-10303-21 AP203 FACETED_BREP:
// HEADER(FILE_DESCRIPTION/FILE_NAME/FILE_SCHEMA)、DATA 段单位/上下文、
// CARTESIAN_POINT(全局去重)/POLY_LOOP/FACE_OUTER_BOUND/FACE/CLOSED_SHELL/FACETED_BREP、
// SHAPE_REPRESENTATION、END-ISO-10303-21;实体 id 唯一且递增;
// 3) 多 body → 多个 FACETED_BREP;write_file 与 write_memory 字节一致、输出确定性;
// 4) 错误路径:空实体、空三角化、tessellate 失败、非法路径、宿主能力缺失、body 数超限;
// 5) 读能力保持(read_memory 仍 UNSUPPORTED;probe ISO 头=100)。
//
// 本测试只依赖纯 C ABI(vde_plugin_api.h)+ gtest,不链接内核。
#include <gtest/gtest.h>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <chrono>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <fstream>
#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.step";
constexpr std::size_t kMaxBodyCount = 10000;
#if defined(_WIN32)
constexpr const char* kPluginRelativePath = "/step/vde_official_step.dll";
#elif defined(__APPLE__)
constexpr const char* kPluginRelativePath = "/step/libvde_official_step.dylib";
#else
constexpr const char* kPluginRelativePath = "/step/libvde_official_step.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;
// P4-65 纯格式化基准:fast_tessellate 复用 body 内缓存 mesh,下列指针由
// mock_free_buffer 跳过释放(模拟宿主无 tessellate 开销的格式化基准)。
const void* g_fast_positions = nullptr;
const void* g_fast_indices = nullptr;
struct MockBody {
std::vector<double> positions; // 3×N
std::vector<std::uint32_t> indices; // 3×T
bool fail_tessellate = false;
bool empty_tessellate = false;
bool fast_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) {
if (pointer == nullptr) return;
if (pointer == g_fast_positions || pointer == g_fast_indices) return;
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;
if (body->fast_tessellate) { // P4-65:返回 body 缓存 mesh,零拷贝/零 tessellate
out->positions = const_cast<double*>(body->positions.data());
out->num_vertices = body->positions.size() / 3u;
out->indices = const_cast<std::uint32_t*>(body->indices.data());
out->num_triangles = body->indices.size() / 3u;
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;
}
// ── 文本/实体断言辅助 ──────────────────────────────────────────────
std::size_t count_substring(const std::string& text, const std::string& needle) {
std::size_t count = 0;
for (std::size_t pos = text.find(needle); pos != std::string::npos;
pos = text.find(needle, pos + needle.size()))
++count;
return count;
}
bool contains(const std::string& text, const std::string& needle) {
return text.find(needle) != std::string::npos;
}
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()); }
/// 扫描 "#<id>=" 实体 id(保持出现顺序)。
std::vector<long long> entity_ids_in_order(const std::string& text) {
std::vector<long long> ids;
for (std::size_t pos = 0; pos < text.size(); ++pos) {
if (text[pos] != '#') continue;
if (pos + 1 >= text.size() || text[pos + 1] < '0' || text[pos + 1] > '9') continue;
if (pos > 0 && (std::isalnum(static_cast<unsigned char>(text[pos - 1])) ||
text[pos - 1] == '_'))
continue;
char* end = nullptr;
const long long id = std::strtoll(text.c_str() + pos + 1, &end, 10);
const std::size_t after = static_cast<std::size_t>(end - text.c_str());
if (id <= 0 || after >= text.size() || text[after] != '=') continue;
ids.push_back(id);
}
return ids;
}
// ── 测试夹具 ────────────────────────────────────────────────────────
class StepWriteFixture : 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_, &registration), 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;
}
MockBody make_grid(std::size_t cells_x, std::size_t cells_y) {
MockBody body;
const std::size_t nx = cells_x + 1;
const std::size_t ny = cells_y + 1;
body.positions.reserve(nx * ny * 3);
for (std::size_t y = 0; y < ny; ++y) {
for (std::size_t x = 0; x < nx; ++x) {
body.positions.push_back(static_cast<double>(x) * 0.001);
body.positions.push_back(static_cast<double>(y) * 0.001);
body.positions.push_back(0.0);
}
}
body.indices.reserve(cells_x * cells_y * 6);
for (std::size_t y = 0; y < cells_y; ++y) {
for (std::size_t x = 0; x < cells_x; ++x) {
const std::uint32_t i0 = static_cast<std::uint32_t>(y * nx + x);
const std::uint32_t i1 = i0 + 1;
const std::uint32_t i2 = static_cast<std::uint32_t>((y + 1) * nx + x);
const std::uint32_t i3 = i2 + 1;
body.indices.push_back(i0);
body.indices.push_back(i1);
body.indices.push_back(i2);
body.indices.push_back(i1);
body.indices.push_back(i3);
body.indices.push_back(i2);
}
}
return body;
}
PluginApi api_;
std::string load_error_;
VdePluginHost host_{};
const VdeFormatPlugin* format_ = nullptr;
};
// P4-63 大模型基准:100k 三角 write_memory/write_file 字节一致 + 宽松时间上限。
TEST_F(StepWriteFixture, LargeModelStreamingBytesEqualAndFast) {
MockBody grid = make_grid(200, 250); // 100,000 三角形(50,100 顶点去重)
std::vector<VdeBody> bodies{make_body(grid)};
ASSERT_NE(bodies[0], nullptr) << g_last_error;
using Clock = std::chrono::steady_clock;
const auto memory_begin = Clock::now();
const std::string memory_text = write_memory_text(bodies);
const auto memory_end = Clock::now();
ASSERT_FALSE(memory_text.empty());
const std::string path = temp_path("p463_step_large.step");
const auto file_begin = Clock::now();
ASSERT_EQ(format_->write_file(&host_, path.c_str(), bodies.data(), bodies.size()),
VDE_PLUGIN_OK) << g_last_error;
const auto file_end = Clock::now();
const std::string file_text = read_text_file(path);
remove_file(path);
destroy_bodies(bodies);
const auto memory_ms =
std::chrono::duration_cast<std::chrono::milliseconds>(memory_end - memory_begin).count();
const auto file_ms =
std::chrono::duration_cast<std::chrono::milliseconds>(file_end - file_begin).count();
std::cout << "[perf] STEP 100k tris: memory=" << memory_ms << "ms file=" << file_ms
<< "ms bytes=" << memory_text.size() << std::endl;
EXPECT_EQ(file_text, memory_text) << "write_file 与 write_memory 大模型字节应一致";
EXPECT_GT(memory_text.size(), 8u * 1024u * 1024u);
EXPECT_LT(memory_ms, 20000);
EXPECT_LT(file_ms, 20000);
}
// P4-65 纯格式化基准(排除 host tessellate):fast_tessellate 直接返回缓存 mesh,
// 只计时 write_memory/write_file 的格式化与 sink 写出路径;输出字节必须一致。
TEST_F(StepWriteFixture, PureFormattingBenchmarkExcludingTessellate) {
MockBody grid = make_grid(158, 158); // 49,928 三角形
std::vector<VdeBody> bodies{make_body(grid)};
ASSERT_NE(bodies[0], nullptr) << g_last_error;
MockBody* mock = as_mock(bodies[0]);
mock->fast_tessellate = true;
g_fast_positions = mock->positions.data();
g_fast_indices = mock->indices.data();
using Clock = std::chrono::steady_clock;
const auto memory_begin = Clock::now();
const std::string memory_text = write_memory_text(bodies);
const auto memory_end = Clock::now();
const std::string path = temp_path("p465_step_pure.step");
const auto file_begin = Clock::now();
ASSERT_EQ(format_->write_file(&host_, path.c_str(), bodies.data(), bodies.size()),
VDE_PLUGIN_OK) << g_last_error;
const auto file_end = Clock::now();
const std::string file_text = read_text_file(path);
remove_file(path);
g_fast_positions = nullptr;
g_fast_indices = nullptr;
destroy_bodies(bodies);
const auto memory_ms =
std::chrono::duration_cast<std::chrono::milliseconds>(memory_end - memory_begin).count();
const auto file_ms =
std::chrono::duration_cast<std::chrono::milliseconds>(file_end - file_begin).count();
std::cout << "[perf-pure] STEP 50k tris: memory=" << memory_ms << "ms file=" << file_ms
<< "ms bytes=" << memory_text.size() << std::endl;
EXPECT_EQ(file_text, memory_text) << "纯格式化路径 write_file 与 write_memory 字节应一致";
EXPECT_GT(memory_text.size(), 1024u * 1024u);
EXPECT_LT(memory_ms, 20000);
EXPECT_LT(file_ms, 20000);
}
} // namespace
// ── 1. 元信息 / 注册 ───────────────────────────────────────────────
TEST_F(StepWriteFixture, 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.step");
EXPECT_EQ(info.kind, VDE_PLUGIN_KIND_FORMAT);
EXPECT_EQ(info.abi_major, VDE_PLUGIN_ABI_MAJOR);
EXPECT_STREQ(info.capabilities, "format:step.read;format:step.write");
ASSERT_NE(format_, nullptr);
EXPECT_STREQ(format_->format_id, "step");
EXPECT_STREQ(format_->extensions, ".step;.stp");
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_brep"), std::string::npos) << describe;
}
TEST_F(StepWriteFixture, ProbeAndReadMemoryBehaviourKept) {
const char* iso_head = "ISO-10303-21;\nHEADER;";
EXPECT_EQ(format_->probe(iso_head, std::strlen(iso_head)), 100);
const char* not_step = "hello world";
EXPECT_EQ(format_->probe(not_step, std::strlen(not_step)), 0);
VdeBody* bodies = nullptr;
std::size_t count = 0;
EXPECT_EQ(format_->read_memory(&host_, iso_head, std::strlen(iso_head), &bodies, &count),
VDE_PLUGIN_ERR_UNSUPPORTED);
}
// ── 2. 写出结构 ────────────────────────────────────────────────────
TEST_F(StepWriteFixture, WriteMemorySingleTriangleStructure) {
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());
EXPECT_EQ(text.rfind("ISO-10303-21;", 0), 0u) << "缺少 ISO-10303-21 文件头";
EXPECT_TRUE(contains(text, "HEADER;"));
EXPECT_TRUE(contains(text, "FILE_DESCRIPTION(('VDE faceted BRep export'),'2;1')"));
EXPECT_TRUE(contains(text, "FILE_NAME('vde_faceted.step'"));
EXPECT_TRUE(contains(text, "FILE_SCHEMA(('CONFIG_CONTROL_DESIGN'))"));
EXPECT_TRUE(contains(text, "DATA;"));
EXPECT_TRUE(contains(text, "END-ISO-10303-21;"));
// 最小单位/上下文
EXPECT_TRUE(contains(text, "SI_UNIT(.MILLI.,.METRE.)"));
EXPECT_TRUE(contains(text, "UNCERTAINTY_MEASURE_WITH_UNIT"));
EXPECT_TRUE(contains(text, "GEOMETRIC_REPRESENTATION_CONTEXT(3)"));
EXPECT_TRUE(contains(text, "SHAPE_REPRESENTATION('VDE',"));
// 几何实体
EXPECT_EQ(count_substring(text, "CARTESIAN_POINT('',("), 3u);
EXPECT_EQ(count_substring(text, "POLY_LOOP('',("), 1u);
EXPECT_EQ(count_substring(text, "FACE_OUTER_BOUND('',#"), 1u);
EXPECT_EQ(count_substring(text, "FACE('',(#"), 1u);
EXPECT_EQ(count_substring(text, "CLOSED_SHELL('',("), 1u);
EXPECT_EQ(count_substring(text, "FACETED_BREP('VDE body 1',#"), 1u);
// 三角形:POLY_LOOP 引用 3 个点
const std::size_t loop_pos = text.find("POLY_LOOP('',(");
ASSERT_NE(loop_pos, std::string::npos);
const std::size_t loop_end = text.find("))", loop_pos);
const std::string loop = text.substr(loop_pos, loop_end - loop_pos);
EXPECT_EQ(count_substring(loop, "#"), 3u);
// 实体 id 唯一且递增(确定性 #1..#N)
const std::vector<long long> ids = entity_ids_in_order(text);
ASSERT_GE(ids.size(), 12u);
for (std::size_t i = 1; i < ids.size(); ++i) EXPECT_LT(ids[i - 1], ids[i]) << i;
std::vector<long long> unique = ids;
std::sort(unique.begin(), unique.end());
EXPECT_EQ(std::adjacent_find(unique.begin(), unique.end()), unique.end());
}
TEST_F(StepWriteFixture, WriteMemoryQuadSharesVertices) {
std::vector<VdeBody> bodies{make_body(make_quad())};
ASSERT_NE(bodies[0], nullptr);
const std::string text = write_memory_text(bodies);
destroy_bodies(bodies);
// 四边形 4 个顶点、2 个三角面;共享顶点去重
EXPECT_EQ(count_substring(text, "CARTESIAN_POINT('',("), 4u);
EXPECT_EQ(count_substring(text, "POLY_LOOP('',("), 2u);
EXPECT_EQ(count_substring(text, "FACE_OUTER_BOUND('',#"), 2u);
EXPECT_EQ(count_substring(text, "FACE('',(#"), 2u);
EXPECT_EQ(count_substring(text, "CLOSED_SHELL('',("), 1u);
EXPECT_EQ(count_substring(text, "FACETED_BREP('VDE body 1',#"), 1u);
}
TEST_F(StepWriteFixture, StepRealFormattingUsesDecimalOrUpperExponent) {
MockBody tiny = make_triangle();
tiny.positions[0] = 1.0e-7; // 触发指数格式
tiny.positions[1] = 0.0;
std::vector<VdeBody> bodies{make_body(tiny)};
ASSERT_NE(bodies[0], nullptr);
const std::string text = write_memory_text(bodies);
destroy_bodies(bodies);
EXPECT_NE(text.find("E-07"), std::string::npos) << "STEP 实数指数应大写 E";
EXPECT_NE(text.find("CARTESIAN_POINT('',(1E-07,0.,0.))"), std::string::npos) << text;
}
TEST_F(StepWriteFixture, WriteFileEqualsMemoryAndDeterministic) {
std::vector<VdeBody> bodies{make_body(make_triangle())};
ASSERT_NE(bodies[0], nullptr);
const std::string first = write_memory_text(bodies);
ASSERT_FALSE(first.empty());
const std::string second = write_memory_text(bodies);
EXPECT_EQ(first, second) << "同一输入的写出应确定性一致";
const std::string path = temp_path("p450_step_equals.step");
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);
}
// ── 3. 多 body ─────────────────────────────────────────────────────
TEST_F(StepWriteFixture, WriteMemoryMultipleBodiesMultipleBreps) {
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 text = write_memory_text(bodies);
destroy_bodies(bodies);
EXPECT_EQ(count_substring(text, "FACETED_BREP("), 2u);
EXPECT_EQ(count_substring(text, "CLOSED_SHELL('',("), 2u);
EXPECT_EQ(count_substring(text, "FACE('',(#"), 2u);
EXPECT_EQ(count_substring(text, "CARTESIAN_POINT('',("), 6u);
// SHAPE_REPRESENTATION 同时引用两个 BREP
const std::size_t shape_pos = text.find("SHAPE_REPRESENTATION('VDE',(");
ASSERT_NE(shape_pos, std::string::npos);
const std::size_t shape_end = text.find("),#", shape_pos);
ASSERT_NE(shape_end, std::string::npos);
const std::string items = text.substr(shape_pos, shape_end - shape_pos);
EXPECT_EQ(count_substring(items, "#"), 2u);
}
// ── 4. 错误路径 ────────────────────────────────────────────────────
TEST_F(StepWriteFixture, 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("p450_nodata.step").c_str(), nullptr, 0),
VDE_PLUGIN_ERR_INVALID_ARG);
destroy_bodies(bodies);
}
TEST_F(StepWriteFixture, 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(StepWriteFixture, 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(StepWriteFixture, 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(StepWriteFixture, 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, &registration), VDE_PLUGIN_ERR_INVALID_ARG);
EXPECT_EQ(api_.init(&host_, nullptr), VDE_PLUGIN_ERR_ABI);
registration.struct_size = 1u;
EXPECT_EQ(api_.init(&host_, &registration), VDE_PLUGIN_ERR_ABI);
}