718 lines
29 KiB
C++
718 lines
29 KiB
C++
// WP-P4-21 官方 ASCII OFF 格式插件测试(确定性 mock 宿主 + 真实 C ABI 动态加载)。
|
||
//
|
||
// 覆盖:
|
||
// 1) vde_plugin_query / init / describe / manifest 的 id、能力与扩展名;
|
||
// 2) OFF/COFF 读取:注释、空行、行内注释、同行 counts、顶点附加字段、
|
||
// 面附加字段、k>=3 扇形三角化;
|
||
// 3) 错误路径:空文件、坏头、counts 非法/超限、截断、索引越界、多余数据;
|
||
// 4) 写出:mock 宿主 body_tessellate -> 合并多实体 ASCII OFF,计数/索引正确,
|
||
// write_file 与 write_memory 字节一致,写后重读一致;
|
||
// 5) 宿主能力缺失、空实体、三角化失败、非法路径等明确错误。
|
||
//
|
||
// 本测试不依赖 vde_brep:mock VdePluginHost 用内存三角网格实现
|
||
// body_from_mesh/body_tessellate,插件通过构建产物动态加载(纯 C ABI)。
|
||
#include <gtest/gtest.h>
|
||
|
||
#include <cmath>
|
||
#include <cstdint>
|
||
#include <cstdlib>
|
||
#include <cstring>
|
||
#include <fstream>
|
||
#include <sstream>
|
||
#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.off";
|
||
|
||
#if defined(_WIN32)
|
||
constexpr const char* kPluginRelativePath = "/off/vde_official_off.dll";
|
||
#elif defined(__APPLE__)
|
||
constexpr const char* kPluginRelativePath = "/off/libvde_official_off.dylib";
|
||
#else
|
||
constexpr const char* kPluginRelativePath = "/off/libvde_official_off.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); }
|
||
const MockBody* as_mock_const(VdeBody body) {
|
||
return reinterpret_cast<const 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;
|
||
}
|
||
|
||
// ── 几何/文件辅助 ────────────────────────────────────────
|
||
struct Cube {
|
||
std::vector<double> positions;
|
||
std::vector<std::uint32_t> indices;
|
||
};
|
||
|
||
Cube make_cube(double width, double height, double depth) {
|
||
const double x = width * 0.5;
|
||
const double y = height * 0.5;
|
||
const double z = depth * 0.5;
|
||
Cube cube;
|
||
cube.positions = {-x, -y, -z, x, -y, -z, x, y, -z, -x, y, -z,
|
||
-x, -y, z, x, -y, z, x, y, z, -x, y, z};
|
||
cube.indices = {0, 2, 1, 0, 3, 2, 4, 5, 6, 4, 6, 7, 0, 1, 5, 0, 5, 4,
|
||
1, 2, 6, 1, 6, 5, 2, 3, 7, 2, 7, 6, 3, 0, 4, 3, 4, 7};
|
||
return cube;
|
||
}
|
||
|
||
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>());
|
||
}
|
||
|
||
bool write_text_file(const std::string& path, const std::string& text) {
|
||
std::ofstream out(path, std::ios::binary | std::ios::trunc);
|
||
if (!out.good()) return false;
|
||
out.write(text.data(), static_cast<std::streamsize>(text.size()));
|
||
out.close();
|
||
return out.good();
|
||
}
|
||
|
||
// ── 测试夹具 ─────────────────────────────────────────────
|
||
class OffPluginFixture : 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 实体未全部销毁";
|
||
}
|
||
|
||
int read_memory_text(const std::string& text, VdeBody** bodies, std::size_t* count) {
|
||
g_last_error.clear();
|
||
return format_->read_memory(&host_, text.empty() ? nullptr : text.data(),
|
||
text.size(), bodies, count);
|
||
}
|
||
|
||
void release(VdeBody* bodies, std::size_t count) {
|
||
if (!bodies) return;
|
||
for (std::size_t i = 0; i < count; ++i) {
|
||
if (bodies[i]) host_.body_destroy(bodies[i]);
|
||
}
|
||
host_.free(bodies);
|
||
}
|
||
|
||
VdeBody make_body(const Cube& cube) {
|
||
return host_.body_from_mesh(cube.positions.data(), cube.positions.size() / 3u,
|
||
cube.indices.data(), cube.indices.size() / 3u);
|
||
}
|
||
|
||
VdePluginHost host_{};
|
||
PluginApi api_;
|
||
const VdeFormatPlugin* format_ = nullptr;
|
||
std::string load_error_;
|
||
};
|
||
|
||
} // namespace
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 1. 元信息 / probe / manifest
|
||
// ═══════════════════════════════════════════════════════════
|
||
TEST_F(OffPluginFixture, QueryDescribeAndProbe) {
|
||
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.off");
|
||
EXPECT_STREQ(info.name, "VDE Official ASCII OFF Format");
|
||
EXPECT_STREQ(info.version, "1.0.0");
|
||
EXPECT_EQ(info.kind, VDE_PLUGIN_KIND_FORMAT);
|
||
EXPECT_EQ(info.abi_major, static_cast<std::uint32_t>(VDE_PLUGIN_ABI_MAJOR));
|
||
EXPECT_EQ(info.abi_minor, static_cast<std::uint32_t>(VDE_PLUGIN_ABI_MINOR));
|
||
EXPECT_STREQ(info.capabilities, "format:off.read;format:off.write");
|
||
EXPECT_STREQ(info.license, "Apache-2.0");
|
||
|
||
ASSERT_NE(format_, nullptr);
|
||
EXPECT_STREQ(format_->format_id, "off");
|
||
EXPECT_STREQ(format_->extensions, ".off");
|
||
EXPECT_NE(format_->probe, nullptr);
|
||
EXPECT_NE(format_->read_file, nullptr);
|
||
EXPECT_NE(format_->read_memory, nullptr);
|
||
EXPECT_NE(format_->write_file, nullptr);
|
||
EXPECT_NE(format_->write_memory, nullptr);
|
||
ASSERT_NE(format_->describe, nullptr);
|
||
EXPECT_NE(std::string(format_->describe()).find("OFF"), std::string::npos);
|
||
|
||
const auto probe_text = [this](const char* text) {
|
||
return format_->probe(text, std::strlen(text));
|
||
};
|
||
EXPECT_EQ(format_->probe(nullptr, 0), 0);
|
||
EXPECT_EQ(probe_text("OFF\n"), 100);
|
||
EXPECT_EQ(probe_text("COFF\n"), 100);
|
||
EXPECT_EQ(probe_text("off 3 1 0\n"), 100);
|
||
EXPECT_EQ(probe_text("\n # header comment\n\tOFF\n"), 100);
|
||
EXPECT_EQ(probe_text("\xEF\xBB\xBF OFF\n"), 100);
|
||
EXPECT_EQ(probe_text("PLY\n"), 0);
|
||
EXPECT_EQ(probe_text("OFFICE\n"), 0);
|
||
EXPECT_EQ(probe_text("OFFY\n"), 0);
|
||
}
|
||
|
||
TEST_F(OffPluginFixture, ManifestDeclaresIdCapabilitiesAndAbi) {
|
||
const std::string manifest =
|
||
std::string(VDE_PLUGIN_OFFICIAL_DIR) + "/off/vde-plugin.json";
|
||
const std::string text = read_text_file(manifest);
|
||
ASSERT_FALSE(text.empty()) << manifest;
|
||
EXPECT_NE(text.find("\"id\": \"com.vde.official.off\""), std::string::npos);
|
||
EXPECT_NE(text.find("\"kind\": \"format\""), std::string::npos);
|
||
EXPECT_NE(text.find("\"major\": 1"), std::string::npos);
|
||
EXPECT_NE(text.find("\"minor\": 3"), std::string::npos);
|
||
EXPECT_NE(text.find("format:off.read"), std::string::npos);
|
||
EXPECT_NE(text.find("format:off.write"), std::string::npos);
|
||
EXPECT_NE(text.find("vde_official_off.dll"), std::string::npos);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 2. 读取 OFF / COFF
|
||
// ═══════════════════════════════════════════════════════════
|
||
TEST_F(OffPluginFixture, ReadOffWithCommentsBlankLinesAndFanTriangulation) {
|
||
const std::string text =
|
||
"# leading comment\n"
|
||
"\n"
|
||
"OFF\n"
|
||
"# counts: 5 vertices, 2 faces, 0 edges\n"
|
||
"5 2 0\n"
|
||
"\n"
|
||
"0 0 0\n"
|
||
"1 0 0\n"
|
||
"1 1 0\n"
|
||
"0 1 0 # inline comment, quad corner\n"
|
||
"0 0 1\n"
|
||
"\n"
|
||
"4 0 1 2 3\n"
|
||
"3 0 2 4\n";
|
||
VdeBody* bodies = nullptr;
|
||
std::size_t count = 0;
|
||
ASSERT_EQ(read_memory_text(text, &bodies, &count), VDE_PLUGIN_OK) << g_last_error;
|
||
ASSERT_EQ(count, 1u);
|
||
ASSERT_NE(bodies, nullptr);
|
||
const MockBody* body = as_mock_const(bodies[0]);
|
||
ASSERT_NE(body, nullptr);
|
||
EXPECT_EQ(body->positions.size(), 15u);
|
||
const std::vector<std::uint32_t> expected = {0, 1, 2, 0, 2, 3, 0, 2, 4};
|
||
EXPECT_EQ(body->indices, expected);
|
||
EXPECT_DOUBLE_EQ(body->positions[0], 0.0);
|
||
EXPECT_DOUBLE_EQ(body->positions[9], 0.0); // vertex 3: 0 1 0
|
||
EXPECT_DOUBLE_EQ(body->positions[10], 1.0);
|
||
release(bodies, count);
|
||
}
|
||
|
||
TEST_F(OffPluginFixture, ReadCoffInlineCountsAndExtraColorFields) {
|
||
const std::string text =
|
||
"COFF 3 1 0\n"
|
||
"0 0 0 255 0 0 255\n"
|
||
"1 0 0 0 255 0 255\n"
|
||
"0 1 0 0 0 255 255\n"
|
||
"3 0 1 2 10 20 30 40\n";
|
||
VdeBody* bodies = nullptr;
|
||
std::size_t count = 0;
|
||
ASSERT_EQ(read_memory_text(text, &bodies, &count), VDE_PLUGIN_OK) << g_last_error;
|
||
ASSERT_EQ(count, 1u);
|
||
const MockBody* body = as_mock_const(bodies[0]);
|
||
ASSERT_NE(body, nullptr);
|
||
EXPECT_EQ(body->positions.size(), 9u);
|
||
EXPECT_EQ(body->indices, (std::vector<std::uint32_t>{0, 1, 2}));
|
||
EXPECT_DOUBLE_EQ(body->positions[3], 1.0);
|
||
release(bodies, count);
|
||
}
|
||
|
||
TEST_F(OffPluginFixture, ReadFileMatchesReadMemory) {
|
||
const std::string text =
|
||
"OFF\n"
|
||
"3 1 0\n"
|
||
"0 0 0\n"
|
||
"2 0 0\n"
|
||
"0 3 0\n"
|
||
"3 0 1 2\n";
|
||
const std::string path = temp_path("vde_off_read.off");
|
||
ASSERT_TRUE(write_text_file(path, text));
|
||
VdeBody* bodies = nullptr;
|
||
std::size_t count = 0;
|
||
g_last_error.clear();
|
||
ASSERT_EQ(format_->read_file(&host_, path.c_str(), &bodies, &count), VDE_PLUGIN_OK)
|
||
<< g_last_error;
|
||
ASSERT_EQ(count, 1u);
|
||
const MockBody* body = as_mock_const(bodies[0]);
|
||
ASSERT_NE(body, nullptr);
|
||
EXPECT_EQ(body->positions.size(), 9u);
|
||
EXPECT_EQ(body->indices, (std::vector<std::uint32_t>{0, 1, 2}));
|
||
release(bodies, count);
|
||
std::remove(path.c_str());
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 3. 读取错误路径
|
||
// ═══════════════════════════════════════════════════════════
|
||
TEST_F(OffPluginFixture, ReadRejectsMalformedTruncatedAndOutOfRangeInput) {
|
||
struct BadCase {
|
||
const char* name;
|
||
std::string text;
|
||
int expected;
|
||
};
|
||
const std::vector<BadCase> cases = {
|
||
{"empty-buffer", "", VDE_PLUGIN_ERR_INVALID_ARG},
|
||
{"blank-comments-only", "\n# only comment\n\t\n", VDE_PLUGIN_ERR_PARSE},
|
||
{"bad-magic", "PLY\n3 1 0\n", VDE_PLUGIN_ERR_PARSE},
|
||
{"missing-counts", "OFF\n", VDE_PLUGIN_ERR_PARSE},
|
||
{"too-many-counts", "OFF\n3 1 0 0\n", VDE_PLUGIN_ERR_PARSE},
|
||
{"bad-counts", "OFF\nx 1 0\n", VDE_PLUGIN_ERR_PARSE},
|
||
{"negative-counts", "OFF\n3 -1 0\n", VDE_PLUGIN_ERR_PARSE},
|
||
{"overflow-counts", "OFF\n999999999999999999999 0 0\n", VDE_PLUGIN_ERR_PARSE},
|
||
{"vertex-over-limit", "OFF\n20000001 0 0\n", VDE_PLUGIN_ERR_PARSE},
|
||
{"face-over-limit", "OFF\n3 20000001 0\n", VDE_PLUGIN_ERR_PARSE},
|
||
{"truncated-vertices", "OFF\n4 1 0\n0 0 0\n1 0 0\n", VDE_PLUGIN_ERR_PARSE},
|
||
{"short-vertex", "OFF\n3 1 0\n0 0\n1 0 0\n0 1 0\n3 0 1 2\n",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"bad-vertex-number", "OFF\n3 1 0\n0 0 x\n1 0 0\n0 1 0\n3 0 1 2\n",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"nan-vertex", "OFF\n3 1 0\n0 0 nan\n1 0 0\n0 1 0\n3 0 1 2\n",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"truncated-face", "OFF\n3 1 0\n0 0 0\n1 0 0\n0 1 0\n3 0 1\n",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"face-k-too-small", "OFF\n3 1 0\n0 0 0\n1 0 0\n0 1 0\n2 0 1\n",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"face-k-over-limit", "OFF\n3 1 0\n0 0 0\n1 0 0\n0 1 0\n1000001 0 1 2\n",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"face-k-exceeds-vertices",
|
||
"OFF\n3 1 0\n0 0 0\n1 0 0\n0 1 0\n4 0 1 2 2\n", VDE_PLUGIN_ERR_PARSE},
|
||
{"index-out-of-range", "OFF\n3 1 0\n0 0 0\n1 0 0\n0 1 0\n3 0 1 3\n",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"negative-index", "OFF\n3 1 0\n0 0 0\n1 0 0\n0 1 0\n3 0 1 -1\n",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"trailing-data", "OFF\n3 1 0\n0 0 0\n1 0 0\n0 1 0\n3 0 1 2\n9 9 9\n",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
{"degenerate-face-only", "OFF\n3 1 0\n0 0 0\n1 0 0\n0 1 0\n3 0 0 0\n",
|
||
VDE_PLUGIN_ERR_PARSE},
|
||
};
|
||
for (const BadCase& test_case : cases) {
|
||
VdeBody* bodies = reinterpret_cast<VdeBody*>(0x1);
|
||
std::size_t count = 123;
|
||
const int rc = read_memory_text(test_case.text, &bodies, &count);
|
||
EXPECT_EQ(rc, test_case.expected) << test_case.name;
|
||
EXPECT_EQ(bodies, nullptr) << test_case.name;
|
||
EXPECT_EQ(count, 0u) << test_case.name;
|
||
EXPECT_FALSE(g_last_error.empty()) << test_case.name;
|
||
}
|
||
}
|
||
|
||
TEST_F(OffPluginFixture, ReadFileMissingReturnsIoError) {
|
||
const std::string path = temp_path("vde_off_does_not_exist.off");
|
||
std::remove(path.c_str());
|
||
VdeBody* bodies = nullptr;
|
||
std::size_t count = 0;
|
||
g_last_error.clear();
|
||
EXPECT_EQ(format_->read_file(&host_, path.c_str(), &bodies, &count),
|
||
VDE_PLUGIN_ERR_IO);
|
||
EXPECT_EQ(bodies, nullptr);
|
||
EXPECT_EQ(count, 0u);
|
||
EXPECT_FALSE(g_last_error.empty());
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 4. 写出:合并多实体 / 计数 / 重读
|
||
// ═══════════════════════════════════════════════════════════
|
||
TEST_F(OffPluginFixture, WriteFileCombinesBodiesWithCorrectCounts) {
|
||
VdeBody body_a = make_body(make_cube(2.0, 3.0, 4.0));
|
||
VdeBody body_b = make_body(make_cube(1.0, 1.0, 1.0));
|
||
ASSERT_NE(body_a, nullptr);
|
||
ASSERT_NE(body_b, nullptr);
|
||
const std::vector<VdeBody> bodies = {body_a, body_b};
|
||
const std::string path = temp_path("vde_off_write.off");
|
||
std::remove(path.c_str());
|
||
g_last_error.clear();
|
||
ASSERT_EQ(format_->write_file(&host_, path.c_str(), bodies.data(), bodies.size()),
|
||
VDE_PLUGIN_OK)
|
||
<< g_last_error;
|
||
|
||
const std::string text = read_text_file(path);
|
||
ASSERT_FALSE(text.empty());
|
||
std::istringstream stream(text);
|
||
std::string magic;
|
||
std::size_t vertices = 0;
|
||
std::size_t faces = 0;
|
||
std::size_t edges = 0;
|
||
ASSERT_TRUE(static_cast<bool>(stream >> magic));
|
||
EXPECT_EQ(magic, "OFF");
|
||
ASSERT_TRUE(static_cast<bool>(stream >> vertices >> faces >> edges));
|
||
// mock body_tessellate 以三角面展开:每立方体 12 面 -> 36 顶点;两体 72/24。
|
||
EXPECT_EQ(vertices, 72u);
|
||
EXPECT_EQ(faces, 24u);
|
||
EXPECT_EQ(edges, 0u);
|
||
for (std::size_t i = 0; i < vertices * 3u; ++i) {
|
||
double value = 0.0;
|
||
ASSERT_TRUE(static_cast<bool>(stream >> value));
|
||
EXPECT_TRUE(std::isfinite(value));
|
||
}
|
||
for (std::size_t f = 0; f < faces; ++f) {
|
||
std::size_t k = 0;
|
||
ASSERT_TRUE(static_cast<bool>(stream >> k));
|
||
EXPECT_EQ(k, 3u);
|
||
std::uint32_t a = 0, b = 0, c = 0;
|
||
ASSERT_TRUE(static_cast<bool>(stream >> a >> b >> c));
|
||
EXPECT_LT(a, vertices);
|
||
EXPECT_LT(b, vertices);
|
||
EXPECT_LT(c, vertices);
|
||
}
|
||
std::string trailing;
|
||
EXPECT_FALSE(static_cast<bool>(stream >> trailing));
|
||
|
||
// 写出的文件必须能被同一插件重读为一个三角网格实体。
|
||
VdeBody* reimported = nullptr;
|
||
std::size_t reimported_count = 0;
|
||
g_last_error.clear();
|
||
ASSERT_EQ(format_->read_file(&host_, path.c_str(), &reimported, &reimported_count),
|
||
VDE_PLUGIN_OK)
|
||
<< g_last_error;
|
||
ASSERT_EQ(reimported_count, 1u);
|
||
const MockBody* roundtrip = as_mock_const(reimported[0]);
|
||
ASSERT_NE(roundtrip, nullptr);
|
||
EXPECT_EQ(roundtrip->positions.size(), vertices * 3u);
|
||
EXPECT_EQ(roundtrip->indices.size(), faces * 3u);
|
||
release(reimported, reimported_count);
|
||
|
||
host_.body_destroy(body_a);
|
||
host_.body_destroy(body_b);
|
||
std::remove(path.c_str());
|
||
}
|
||
|
||
TEST_F(OffPluginFixture, WriteMemoryMatchesWriteFile) {
|
||
VdeBody body = make_body(make_cube(2.0, 2.0, 2.0));
|
||
ASSERT_NE(body, nullptr);
|
||
VdeBuffer buffer{};
|
||
g_last_error.clear();
|
||
ASSERT_EQ(format_->write_memory(&host_, &body, 1, &buffer), VDE_PLUGIN_OK)
|
||
<< g_last_error;
|
||
ASSERT_NE(buffer.data, nullptr);
|
||
const std::string memory_text(static_cast<const char*>(buffer.data), buffer.size);
|
||
host_.free(buffer.data);
|
||
|
||
const std::string path = temp_path("vde_off_memory.off");
|
||
std::remove(path.c_str());
|
||
ASSERT_EQ(format_->write_file(&host_, path.c_str(), &body, 1), VDE_PLUGIN_OK)
|
||
<< g_last_error;
|
||
const std::string file_text = read_text_file(path);
|
||
EXPECT_EQ(memory_text, file_text);
|
||
EXPECT_EQ(memory_text.rfind("OFF\n36 12 0\n", 0), 0u);
|
||
host_.body_destroy(body);
|
||
std::remove(path.c_str());
|
||
}
|
||
|
||
TEST_F(OffPluginFixture, WriteEmptyEntityListIsRejected) {
|
||
VdeBody* no_bodies = nullptr;
|
||
VdeBuffer buffer{};
|
||
g_last_error.clear();
|
||
EXPECT_EQ(format_->write_memory(&host_, no_bodies, 0, &buffer),
|
||
VDE_PLUGIN_ERR_INTERNAL);
|
||
EXPECT_EQ(buffer.data, nullptr);
|
||
EXPECT_EQ(buffer.size, 0u);
|
||
EXPECT_FALSE(g_last_error.empty());
|
||
|
||
const std::string path = temp_path("vde_off_empty_list.off");
|
||
g_last_error.clear();
|
||
EXPECT_EQ(format_->write_file(&host_, path.c_str(), no_bodies, 0),
|
||
VDE_PLUGIN_ERR_INTERNAL);
|
||
EXPECT_FALSE(g_last_error.empty());
|
||
std::remove(path.c_str());
|
||
}
|
||
|
||
TEST_F(OffPluginFixture, WriteReportsTessellationAndPathFailures) {
|
||
VdeBody failing = make_body(make_cube(1.0, 1.0, 1.0));
|
||
ASSERT_NE(failing, nullptr);
|
||
as_mock(failing)->fail_tessellate = true;
|
||
const std::string path = temp_path("vde_off_tess_fail.off");
|
||
g_last_error.clear();
|
||
EXPECT_EQ(format_->write_file(&host_, path.c_str(), &failing, 1),
|
||
VDE_PLUGIN_ERR_INTERNAL);
|
||
EXPECT_FALSE(g_last_error.empty());
|
||
|
||
as_mock(failing)->fail_tessellate = false;
|
||
as_mock(failing)->empty_tessellate = true;
|
||
g_last_error.clear();
|
||
EXPECT_EQ(format_->write_file(&host_, path.c_str(), &failing, 1),
|
||
VDE_PLUGIN_ERR_INTERNAL);
|
||
EXPECT_FALSE(g_last_error.empty());
|
||
host_.body_destroy(failing);
|
||
|
||
// 不存在的输出目录 -> IO。
|
||
VdeBody body = make_body(make_cube(1.0, 1.0, 1.0));
|
||
ASSERT_NE(body, nullptr);
|
||
const std::string bad_path = temp_path("vde_off_no_such_dir/out.off");
|
||
g_last_error.clear();
|
||
EXPECT_EQ(format_->write_file(&host_, bad_path.c_str(), &body, 1),
|
||
VDE_PLUGIN_ERR_IO);
|
||
EXPECT_FALSE(g_last_error.empty());
|
||
host_.body_destroy(body);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 5. 宿主能力 / 参数校验
|
||
// ═══════════════════════════════════════════════════════════
|
||
TEST_F(OffPluginFixture, MissingHostServicesReturnUnsupported) {
|
||
const std::string text = "OFF\n3 1 0\n0 0 0\n1 0 0\n0 1 0\n3 0 1 2\n";
|
||
VdePluginHost read_host = host_;
|
||
read_host.body_from_mesh = nullptr;
|
||
VdeBody* bodies = nullptr;
|
||
std::size_t count = 0;
|
||
g_last_error.clear();
|
||
EXPECT_EQ(format_->read_memory(&read_host, text.data(), text.size(), &bodies, &count),
|
||
VDE_PLUGIN_ERR_UNSUPPORTED);
|
||
EXPECT_EQ(bodies, nullptr);
|
||
EXPECT_EQ(count, 0u);
|
||
EXPECT_FALSE(g_last_error.empty());
|
||
|
||
VdeBody body = make_body(make_cube(1.0, 1.0, 1.0));
|
||
ASSERT_NE(body, nullptr);
|
||
VdePluginHost write_host = host_;
|
||
write_host.body_tessellate = nullptr;
|
||
VdeBuffer buffer{};
|
||
g_last_error.clear();
|
||
EXPECT_EQ(format_->write_memory(&write_host, &body, 1, &buffer),
|
||
VDE_PLUGIN_ERR_UNSUPPORTED);
|
||
EXPECT_EQ(buffer.data, nullptr);
|
||
EXPECT_FALSE(g_last_error.empty());
|
||
host_.body_destroy(body);
|
||
}
|
||
|
||
TEST_F(OffPluginFixture, EntryPointArgumentValidation) {
|
||
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);
|
||
|
||
EXPECT_EQ(format_->write_memory(&host_, nullptr, 0, nullptr),
|
||
VDE_PLUGIN_ERR_INVALID_ARG);
|
||
EXPECT_EQ(format_->write_file(&host_, nullptr, nullptr, 0),
|
||
VDE_PLUGIN_ERR_INVALID_ARG);
|
||
VdeBody* bodies = nullptr;
|
||
std::size_t count = 0;
|
||
EXPECT_EQ(format_->read_file(&host_, nullptr, &bodies, &count),
|
||
VDE_PLUGIN_ERR_INVALID_ARG);
|
||
EXPECT_EQ(format_->read_memory(&host_, nullptr, 0, &bodies, &count),
|
||
VDE_PLUGIN_ERR_INVALID_ARG);
|
||
EXPECT_EQ(format_->read_memory(&host_, "OFF\n", 4, nullptr, &count),
|
||
VDE_PLUGIN_ERR_INVALID_ARG);
|
||
EXPECT_EQ(format_->read_memory(&host_, "OFF\n", 4, &bodies, nullptr),
|
||
VDE_PLUGIN_ERR_INVALID_ARG);
|
||
}
|