Files
ViewDesignEngine/tests/plugin/test_official_io_batch_mesh.cpp
T

898 lines
40 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.
// P4-33/P4-38 官方 IO_BATCH 网格转换插件测试(确定性 mock 宿主 + 真实 C ABI 动态加载;ABI 1.6 run_v2)。
//
// 覆盖:
// 1) query/init/describe/batch_id/能力与 struct_size;
// 2) STL(ASCII)/OBJ/PLY(ASCII) 批量互转(3 项一次请求),输出文件可读、triangles=1;
// 2b) P4-56:OFF(ASCII v/f)与 PLY binary_little_endian(float/double + 索引类型)读写、
// 混合格式矩阵、partial/坏输入/上限/确定性;
// 3) STL binary 读取 → OBJ 写出;
// 4) 混合成功/失败:合法项 + 不存在输入 + 不支持格式 → status=-4(PARTIAL)/processed/failed
// 逐项记录,响应 JSON 诊断;
// 4b) P4-62:大文件(200k 三角 binary STL)流式读(binary 块/ASCII 行)与流式写
// (STL→OBJ/PLY/PLY binary/OFF,再流式回读),记录本地基准耗时;
// 4c) P4-64:PLY ASCII token 流(多记录同行/记录跨行/任意空白)、options.stream
// auto/always/never + 环境变量、非法 stream 值 INVALID_ARG;
// 5) 非法 options(flags)/畸形 request/空 items/非法 options.format;
// 6) 坏输入文件(非 STL)→ 单项失败,不产生输出。
//
// 注:shared CMake 由 Lead 注册;本文件按现有官方插件测试模式编写(纯 C ABI,不链接内核)。
#include <gtest/gtest.h>
#include <chrono>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#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.io_batch.mesh";
constexpr const char* kBatchId = "io_batch.mesh";
#if defined(_WIN32)
constexpr const char* kPluginRelativePath = "/io_batch_mesh/vde_official_io_batch_mesh.dll";
#elif defined(__APPLE__)
constexpr const char* kPluginRelativePath = "/io_batch_mesh/libvde_official_io_batch_mesh.dylib";
#else
constexpr const char* kPluginRelativePath = "/io_batch_mesh/libvde_official_io_batch_mesh.so";
#endif
#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
std::string g_last_error;
// ── P4-62:大文件/流式辅助 ───────────────────────────────────────────
void write_large_binary_stl(const std::string& path, std::size_t triangle_count) {
std::ofstream out(path, std::ios::binary | std::ios::trunc);
ASSERT_TRUE(out.good());
char header[80] = {};
out.write(header, sizeof(header));
const std::uint32_t count = static_cast<std::uint32_t>(triangle_count);
out.write(reinterpret_cast<const char*>(&count), sizeof(count));
for (std::size_t i = 0; i < triangle_count; ++i) {
const float normal[3] = {0.0f, 0.0f, 1.0f};
out.write(reinterpret_cast<const char*>(normal), sizeof(normal));
const float x = static_cast<float>(i % 1000u);
const float vertices[3][3] = {{x, 0.0f, 0.0f},
{x + 1.0f, 0.0f, 0.0f},
{x, 1.0f, 0.0f}};
for (const auto& vertex : vertices)
out.write(reinterpret_cast<const char*>(vertex), sizeof(vertex));
const char attributes[2] = {0, 0};
out.write(attributes, sizeof(attributes));
}
}
std::size_t count_file_lines_with_prefix(const std::string& path, const std::string& prefix) {
std::ifstream in(path, std::ios::binary);
std::string line;
std::size_t count = 0;
while (std::getline(in, line)) {
if (line.rfind(prefix, 0) == 0) ++count;
}
return count;
}
std::uint64_t file_size_of(const std::string& path) {
std::ifstream in(path, std::ios::binary | std::ios::ate);
if (!in.good()) return 0;
const std::streamoff end = in.tellg();
return end > 0 ? static_cast<std::uint64_t>(end) : 0;
}
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_log(int, const char*) {}
void mock_set_error(const char* message) { g_last_error = message ? message : ""; }
void mock_body_destroy(VdeBody) {}
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;
return host;
}
std::string env_or_macro(const char* macro_value, const char* env_name) {
if (macro_value && macro_value[0] != '\0') return std::string(macro_value);
const char* env = std::getenv(env_name);
return env ? std::string(env) : std::string();
}
std::string official_dir() {
return env_or_macro(VDE_PLUGIN_OFFICIAL_DIR, "VDE_PLUGIN_OFFICIAL_DIR");
}
std::string temp_dir() {
return env_or_macro(VDE_TEST_TEMP_DIR, "VDE_TEST_TEMP_DIR");
}
// 每个测试进程使用独立临时目录,避免 CTest 并行时共享路径互相覆盖。
std::string g_temp_dir;
std::string temp_path(const char* name) {
if (!g_temp_dir.empty()) return g_temp_dir + "/" + name;
return temp_dir() + name;
}
bool write_file(const std::string& path, const std::string& data) {
std::ofstream out(path, std::ios::binary | std::ios::trunc);
if (!out.good()) return false;
out.write(data.data(), static_cast<std::streamsize>(data.size()));
return out.good();
}
std::string read_file(const std::string& path) {
std::ifstream in(path, std::ios::binary);
return std::string((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
}
bool contains(const std::string& text, const std::string& needle) {
return text.find(needle) != std::string::npos;
}
const char* kStlAscii =
"solid tri\n"
" facet normal 0 0 1\n"
" outer loop\n"
" vertex 0 0 0\n"
" vertex 1 0 0\n"
" vertex 0 1 0\n"
" endloop\n"
" endfacet\n"
"endsolid tri\n";
const char* kObj =
"# obj test\n"
"v 0 0 0\nv 1 0 0\nv 0 1 0\n"
"f 1 2 3\n";
const char* kPly =
"ply\n"
"format ascii 1.0\n"
"element vertex 3\n"
"property float x\nproperty float y\nproperty float z\n"
"element face 1\n"
"property list uchar int vertex_indices\n"
"end_header\n"
"0 0 0\n1 0 0\n0 1 0\n"
"3 0 1 2\n";
const char* kPlyCompact =
"ply\n"
"format ascii 1.0\n"
"element vertex 4\n"
"property float x\nproperty float y\nproperty float z\n"
"element face 1\n"
"property list uchar int vertex_indices\n"
"end_header\n"
"0 0\n0 1\n0 0\n0 1\n0 1 1 0 4 0 1 3 2\n";
const char* kOff =
"OFF\n"
"3 1 0\n"
"0 0 0\n1 0 0\n0 1 0\n"
"3 0 1 2\n";
void append_float_le(std::string* out, float value) {
unsigned char bytes[4];
std::memcpy(bytes, &value, 4);
out->append(reinterpret_cast<const char*>(bytes), 4);
}
void append_double_le(std::string* out, double value) {
unsigned char bytes[8];
std::memcpy(bytes, &value, 8);
out->append(reinterpret_cast<const char*>(bytes), 8);
}
void append_uint_le(std::string* out, std::uint64_t value, std::size_t width) {
unsigned char bytes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
for (std::size_t i = 0; i < width; ++i)
bytes[i] = static_cast<unsigned char>((value >> (8 * i)) & 0xFFu);
out->append(reinterpret_cast<const char*>(bytes), width);
}
std::string binary_ply_float_int() {
std::string out =
"ply\nformat binary_little_endian 1.0\n"
"element vertex 3\nproperty float x\nproperty float y\nproperty float z\n"
"element face 1\nproperty list uchar int vertex_indices\nend_header\n";
const float vertices[3][3] = {{0, 0, 0}, {1, 0, 0}, {0, 1, 0}};
for (const auto& vertex : vertices)
for (float value : vertex) append_float_le(&out, value);
append_uint_le(&out, 3, 1);
append_uint_le(&out, 0, 4);
append_uint_le(&out, 1, 4);
append_uint_le(&out, 2, 4);
return out;
}
std::string binary_ply_double_ushort() {
std::string out =
"ply\nformat binary_little_endian 1.0\n"
"element vertex 4\nproperty double x\nproperty double y\nproperty double z\n"
"property uchar red\n"
"element face 1\nproperty list ushort ushort vertex_indices\nend_header\n";
const double vertices[4][3] = {{0, 0, 0}, {2, 0, 0}, {2, 1, 0}, {0, 1, 0}};
for (const auto& vertex : vertices) {
for (double value : vertex) append_double_le(&out, value);
out.push_back(static_cast<char>(7));
}
append_uint_le(&out, 4, 2);
for (std::uint64_t index : {0ull, 1ull, 2ull, 3ull}) append_uint_le(&out, index, 2);
return out;
}
std::string binary_stl_one_triangle() {
std::string data(80, '\0');
const std::uint32_t count = 1;
data.append(reinterpret_cast<const char*>(&count), sizeof(count));
const float vertices[3][3] = {{0, 0, 0}, {1, 0, 0}, {0, 1, 0}};
const float normal[3] = {0, 0, 1};
data.append(reinterpret_cast<const char*>(normal), 12);
for (const auto& vertex : vertices) {
data.append(reinterpret_cast<const char*>(vertex), 12);
}
const char attributes[2] = {0, 0};
data.append(attributes, 2);
return data;
}
class IoBatchMeshFixture : public ::testing::Test {
protected:
void SetUp() override {
g_last_error.clear();
host_ = make_mock_host();
path_ = official_dir() + kPluginRelativePath;
library_ = open_library(path_.c_str());
ASSERT_NE(library_, nullptr) << "无法加载插件: " << path_;
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"));
ASSERT_NE(query_, nullptr);
ASSERT_NE(init_, nullptr);
VdePluginInfo info{};
info.struct_size = static_cast<std::uint32_t>(sizeof(VdePluginInfo));
ASSERT_EQ(query_(VDE_PLUGIN_ABI_MAJOR, &info), VDE_PLUGIN_OK);
VdePluginRegistration registration{};
registration.struct_size =
static_cast<std::uint32_t>(sizeof(VdePluginRegistration));
ASSERT_EQ(init_(&host_, &registration), VDE_PLUGIN_OK) << g_last_error;
ASSERT_EQ(registration.kind, VDE_PLUGIN_KIND_IO_BATCH);
ASSERT_NE(registration.io_batch, nullptr);
batch_ = registration.io_batch;
ASSERT_EQ(std::string(batch_->batch_id), kBatchId);
const auto unique = std::chrono::high_resolution_clock::now().time_since_epoch().count();
g_temp_dir = (std::filesystem::path(temp_dir()) /
("io_batch_mesh_" + std::to_string(unique))).string();
temp_dir_ = std::filesystem::path(g_temp_dir);
std::filesystem::create_directories(temp_dir_);
write_file(temp_path("tri.stl"), kStlAscii);
write_file(temp_path("tri.obj"), kObj);
write_file(temp_path("tri.ply"), kPly);
write_file(temp_path("tri.off"), kOff);
write_file(temp_path("tri_compact.ply"), kPlyCompact);
write_file(temp_path("tri_binary.plyb"), binary_ply_float_int());
write_file(temp_path("tri_double.plyb"), binary_ply_double_ushort());
write_file(temp_path("tri_binary.stl"), binary_stl_one_triangle());
}
void TearDown() override {
if (shutdown_) shutdown_();
if (library_) close_library(library_);
if (!g_temp_dir.empty()) std::filesystem::remove_all(g_temp_dir);
}
int run(const std::string& options, const std::string& request, std::string* response,
std::string* error) {
VdeBuffer buffer{};
char error_message[512] = {};
g_last_error.clear();
if (response) response->clear();
if (error) error->clear();
const int rc = batch_->run(&host_, options.empty() ? nullptr : options.c_str(),
request.data(), request.size(), &buffer, error_message,
sizeof(error_message));
if (buffer.data && buffer.size > 0) {
if (response) response->assign(static_cast<const char*>(buffer.data),
buffer.size);
host_.free_buffer(buffer.data);
}
if (error) *error = error_message;
return rc;
}
int run_v2_batch(const std::string& options, const std::string& request,
VdeIoBatchResult* result, std::string* response, std::string* error) {
VdeBuffer buffer{};
char error_message[512] = {};
result->struct_size = static_cast<std::uint32_t>(sizeof(VdeIoBatchResult));
result->status = static_cast<std::uint32_t>(VDE_IO_BATCH_ERR_INTERNAL);
result->processed = 0;
result->failed = 0;
g_last_error.clear();
if (response) response->clear();
if (error) error->clear();
const int rc = batch_->run_v2(&host_, options.empty() ? nullptr : options.c_str(),
request.data(), request.size(), result, &buffer,
error_message, sizeof(error_message));
if (buffer.data && buffer.size > 0) {
if (response) response->assign(static_cast<const char*>(buffer.data),
buffer.size);
host_.free_buffer(buffer.data);
}
if (error) *error = error_message;
return rc;
}
VdePluginHost host_{};
LibraryHandle library_ = nullptr;
VdePluginQueryFn query_ = nullptr;
VdePluginInitFn init_ = nullptr;
VdePluginShutdownFn shutdown_ = nullptr;
const VdeIoBatchPlugin* batch_ = nullptr;
std::string path_;
std::filesystem::path temp_dir_;
};
TEST_F(IoBatchMeshFixture, MetadataAndRegistration) {
VdePluginInfo info{};
info.struct_size = static_cast<std::uint32_t>(sizeof(VdePluginInfo));
ASSERT_EQ(query_(VDE_PLUGIN_ABI_MAJOR, &info), VDE_PLUGIN_OK);
EXPECT_STREQ(info.id, kPluginId);
EXPECT_EQ(info.kind, VDE_PLUGIN_KIND_IO_BATCH);
EXPECT_EQ(info.abi_major, VDE_PLUGIN_ABI_MAJOR);
EXPECT_EQ(info.abi_minor, VDE_PLUGIN_ABI_MINOR);
EXPECT_STREQ(info.capabilities, "io_batch:mesh_convert");
ASSERT_NE(batch_, nullptr);
EXPECT_STREQ(batch_->batch_id, kBatchId);
EXPECT_GE(batch_->struct_size, sizeof(VdeIoBatchPlugin));
EXPECT_NE(batch_->run_v2, nullptr);
ASSERT_NE(batch_->capabilities_json, nullptr);
EXPECT_NE(std::string(batch_->capabilities_json).find("\"stl\""), std::string::npos);
}
TEST_F(IoBatchMeshFixture, BatchConvertsStlObjPly) {
const std::string request =
"{\"items\":["
"{\"input\":\"" + temp_path("tri.stl") + "\",\"output\":\"" + temp_path("a.obj") +
"\",\"from\":\"stl\",\"to\":\"obj\"},"
"{\"input\":\"" + temp_path("tri.obj") + "\",\"output\":\"" + temp_path("b.ply") +
"\",\"from\":\"obj\",\"to\":\"ply\"},"
"{\"input\":\"" + temp_path("tri.ply") + "\",\"output\":\"" + temp_path("c.stl") +
"\",\"from\":\"ply\",\"to\":\"stl\"}]}";
std::string response;
std::string error;
const int rc = run("", request, &response, &error);
ASSERT_EQ(rc, VDE_PLUGIN_OK) << error << g_last_error;
EXPECT_TRUE(contains(response, "\"batch_id\":\"io_batch.mesh\""));
EXPECT_TRUE(contains(response, "\"ok\":true"));
EXPECT_TRUE(contains(response, "\"processed\":3")) << response;
EXPECT_TRUE(contains(response, "\"failed\":0"));
EXPECT_TRUE(contains(response, "\"triangles\":1"));
const std::string obj = read_file(temp_path("a.obj"));
EXPECT_TRUE(contains(obj, "v "));
EXPECT_TRUE(contains(obj, "f "));
const std::string ply = read_file(temp_path("b.ply"));
EXPECT_TRUE(ply.rfind("ply", 0) == 0);
EXPECT_TRUE(contains(ply, "element vertex 3"));
const std::string stl = read_file(temp_path("c.stl"));
EXPECT_TRUE(stl.rfind("solid", 0) == 0);
}
TEST_F(IoBatchMeshFixture, ReadsBinaryStl) {
const std::string request =
"{\"items\":[{\"input\":\"" + temp_path("tri_binary.stl") + "\",\"output\":\"" +
temp_path("binary.obj") + "\",\"from\":\"stl\",\"to\":\"obj\"}]}";
std::string response;
std::string error;
ASSERT_EQ(run("", request, &response, &error), VDE_PLUGIN_OK) << error;
EXPECT_TRUE(contains(response, "\"triangles\":1"));
const std::string obj = read_file(temp_path("binary.obj"));
EXPECT_TRUE(contains(obj, "v "));
EXPECT_TRUE(contains(obj, "f 1 2 3"));
}
TEST_F(IoBatchMeshFixture, PartialFailureReportsAndContinues) {
const std::string request =
"{\"items\":["
"{\"input\":\"" + temp_path("tri.stl") + "\",\"output\":\"" + temp_path("ok.obj") +
"\",\"from\":\"stl\",\"to\":\"obj\"},"
"{\"input\":\"" + temp_path("missing.stl") + "\",\"output\":\"" +
temp_path("missing.obj") + "\",\"from\":\"stl\",\"to\":\"obj\"},"
"{\"input\":\"" + temp_path("tri.obj") + "\",\"output\":\"" + temp_path("bad.xyz") +
"\",\"from\":\"obj\",\"to\":\"xyz\"}]}";
std::string response;
std::string error;
ASSERT_EQ(run("", request, &response, &error), VDE_PLUGIN_OK) << error;
EXPECT_TRUE(contains(response, "\"status\":-4"));
EXPECT_TRUE(contains(response, "\"ok\":false"));
EXPECT_TRUE(contains(response, "\"processed\":1"));
EXPECT_TRUE(contains(response, "\"failed\":2"));
EXPECT_TRUE(contains(response, "读取失败"));
EXPECT_TRUE(contains(response, "不支持的 to"));
EXPECT_TRUE(std::filesystem::exists(temp_path("ok.obj")));
EXPECT_FALSE(std::filesystem::exists(temp_path("missing.obj")));
EXPECT_FALSE(std::filesystem::exists(temp_path("bad.xyz")));
}
TEST_F(IoBatchMeshFixture, InvalidOptionsAndRequestsRejected) {
std::string response;
std::string error;
EXPECT_EQ(run("{\"flags\":1}", "{\"items\":[]}", &response, &error),
VDE_PLUGIN_ERR_INVALID_ARG);
EXPECT_EQ(run("", "{bad json", &response, &error), VDE_PLUGIN_ERR_INVALID_ARG);
EXPECT_EQ(run("", "{\"items\":[]}", &response, &error), VDE_PLUGIN_ERR_INVALID_ARG);
EXPECT_EQ(run("{\"format\":\"step\"}", "{\"items\":[]}", &response, &error),
VDE_PLUGIN_ERR_INVALID_ARG);
EXPECT_EQ(run("", "", &response, &error), VDE_PLUGIN_ERR_INVALID_ARG);
}
TEST_F(IoBatchMeshFixture, MalformedInputItemFailsWithoutOutput) {
write_file(temp_path("broken.stl"), "not a stl file\n");
const std::string request =
"{\"items\":[{\"input\":\"" + temp_path("broken.stl") + "\",\"output\":\"" +
temp_path("broken.obj") + "\",\"from\":\"stl\",\"to\":\"obj\"}]}";
std::string response;
std::string error;
ASSERT_EQ(run("", request, &response, &error), VDE_PLUGIN_OK) << error;
EXPECT_TRUE(contains(response, "\"status\":-4"));
EXPECT_TRUE(contains(response, "\"failed\":1"));
EXPECT_TRUE(contains(response, "读取失败"));
EXPECT_FALSE(std::filesystem::exists(temp_path("broken.obj")));
}
} // namespace
TEST_F(IoBatchMeshFixture, RunV2BackfillsResultSuccess) {
ASSERT_NE(batch_->run_v2, nullptr);
const std::string request =
"{\"items\":[{\"input\":\"" + temp_path("tri.stl") +
"\",\"output\":\"" + temp_path("v2.obj") +
"\",\"from\":\"stl\",\"to\":\"obj\"}]}";
VdeIoBatchResult result{};
std::string response;
std::string error;
ASSERT_EQ(run_v2_batch("", request, &result, &response, &error), VDE_PLUGIN_OK)
<< error << g_last_error;
EXPECT_EQ(result.status, static_cast<std::uint32_t>(VDE_IO_BATCH_OK));
EXPECT_EQ(result.processed, 1u);
EXPECT_EQ(result.failed, 0u);
EXPECT_TRUE(contains(response, "\"processed\":1"));
EXPECT_TRUE(contains(response, "\"failed\":0"));
}
TEST_F(IoBatchMeshFixture, RunV2BackfillsPartialFailureStatus) {
ASSERT_NE(batch_->run_v2, nullptr);
const std::string request =
"{\"items\":["
"{\"input\":\"" + temp_path("tri.stl") +
"\",\"output\":\"" + temp_path("v2_ok.obj") +
"\",\"from\":\"stl\",\"to\":\"obj\"},"
"{\"input\":\"" + temp_path("missing.stl") +
"\",\"output\":\"" + temp_path("v2_missing.obj") +
"\",\"from\":\"stl\",\"to\":\"obj\"}]}";
VdeIoBatchResult result{};
std::string response;
std::string error;
ASSERT_EQ(run_v2_batch("", request, &result, &response, &error), VDE_PLUGIN_OK)
<< error << g_last_error;
EXPECT_EQ(result.status, static_cast<std::uint32_t>(VDE_IO_BATCH_ERR_PARTIAL));
EXPECT_EQ(result.processed, 1u);
EXPECT_EQ(result.failed, 1u);
EXPECT_TRUE(contains(response, "\"status\":-4"));
}
TEST_F(IoBatchMeshFixture, BatchConvertsOffAndPlyBinary) {
const std::string request =
"{\"items\":["
"{\"input\":\"" + temp_path("tri.off") + "\",\"output\":\"" + temp_path("from_off.obj") +
"\",\"from\":\"off\",\"to\":\"obj\"},"
"{\"input\":\"" + temp_path("tri_binary.plyb") + "\",\"output\":\"" +
temp_path("from_binary.off") + "\",\"from\":\"ply\",\"to\":\"off\"},"
"{\"input\":\"" + temp_path("tri.obj") + "\",\"output\":\"" +
temp_path("to_binary.plyb") + "\",\"from\":\"obj\",\"to\":\"ply\"},"
"{\"input\":\"" + temp_path("tri_double.plyb") + "\",\"output\":\"" +
temp_path("double.obj") + "\",\"from\":\"plyb\",\"to\":\"obj\"}]}";
std::string response;
std::string error;
ASSERT_EQ(run("", request, &response, &error), VDE_PLUGIN_OK) << error << g_last_error;
EXPECT_TRUE(contains(response, "\"ok\":true")) << response;
EXPECT_TRUE(contains(response, "\"processed\":4")) << response;
EXPECT_TRUE(contains(response, "\"failed\":0"));
const std::string obj = read_file(temp_path("from_off.obj"));
EXPECT_TRUE(contains(obj, "v "));
EXPECT_TRUE(contains(obj, "f 1 2 3"));
const std::string off = read_file(temp_path("from_binary.off"));
EXPECT_TRUE(off.rfind("OFF", 0) == 0);
EXPECT_TRUE(contains(off, "3 1 0"));
const std::string binary = read_file(temp_path("to_binary.plyb"));
EXPECT_TRUE(contains(binary, "format binary_little_endian 1.0"));
EXPECT_TRUE(contains(binary, "property float x"));
const std::string double_obj = read_file(temp_path("double.obj"));
EXPECT_TRUE(contains(double_obj, "v "));
EXPECT_TRUE(contains(double_obj, "f 1 2 3"));
}
TEST_F(IoBatchMeshFixture, FormatMatrixOffPlyAsciiPlyBinary) {
const char* inputs[] = {"tri.stl", "tri.obj", "tri.ply", "tri.off", "tri_binary.plyb"};
const char* inputs_format[] = {"stl", "obj", "ply", "off", "ply"};
const char* outputs[] = {"matrix.off", "matrix.ply", "matrix.plyb"};
const char* outputs_format[] = {"off", "ply", "ply"};
std::string request = "{\"items\":[";
bool first = true;
for (int i = 0; i < 5; ++i) {
for (int o = 0; o < 3; ++o) {
if (!first) request += ",";
first = false;
request += "{\"input\":\"" + temp_path(inputs[i]) + "\",\"output\":\"" +
temp_path((std::string("matrix_") + std::to_string(i) + "_" +
outputs[o]).c_str()) +
"\",\"from\":\"" + inputs_format[i] + "\",\"to\":\"" +
outputs_format[o] + "\"}";
}
}
request += "]}";
std::string response;
std::string error;
ASSERT_EQ(run("", request, &response, &error), VDE_PLUGIN_OK) << error << g_last_error;
EXPECT_TRUE(contains(response, "\"processed\":15")) << response;
EXPECT_TRUE(contains(response, "\"failed\":0"));
for (int i = 0; i < 5; ++i) {
for (int o = 0; o < 3; ++o) {
const std::string name = std::string("matrix_") + std::to_string(i) + "_" +
outputs[o];
const std::string text = read_file(temp_path(name.c_str()));
EXPECT_FALSE(text.empty()) << name;
if (o == 2) {
EXPECT_TRUE(contains(text, "format binary_little_endian 1.0"));
}
}
}
}
TEST_F(IoBatchMeshFixture, PartialFailuresForBadOffAndBadPly) {
write_file(temp_path("bad.off"), "COFF\n3 1 0\n0 0 0\n1 0 0\n0 1 0\n3 0 1 2\n");
write_file(temp_path("truncated.plyb"), binary_ply_float_int().substr(0, 200));
const std::string request =
"{\"items\":["
"{\"input\":\"" + temp_path("tri.stl") + "\",\"output\":\"" + temp_path("ok2.obj") +
"\",\"from\":\"stl\",\"to\":\"obj\"},"
"{\"input\":\"" + temp_path("bad.off") + "\",\"output\":\"" + temp_path("bad2.obj") +
"\",\"from\":\"off\",\"to\":\"obj\"},"
"{\"input\":\"" + temp_path("truncated.plyb") + "\",\"output\":\"" +
temp_path("truncated.obj") + "\",\"from\":\"ply\",\"to\":\"obj\"}]}";
std::string response;
std::string error;
ASSERT_EQ(run("", request, &response, &error), VDE_PLUGIN_OK) << error;
EXPECT_TRUE(contains(response, "\"status\":-4"));
EXPECT_TRUE(contains(response, "\"processed\":1"));
EXPECT_TRUE(contains(response, "\"failed\":2"));
EXPECT_TRUE(contains(response, "COFF")) << response;
EXPECT_TRUE(contains(response, "读取失败"));
EXPECT_FALSE(std::filesystem::exists(temp_path("bad2.obj")));
EXPECT_FALSE(std::filesystem::exists(temp_path("truncated.obj")));
}
TEST_F(IoBatchMeshFixture, RejectsOffAndPlyBinaryLimitAndBigEndian) {
write_file(temp_path("huge.off"),
"OFF\n20000001 0 0\n");
write_file(temp_path("big_endian.plyb"),
"ply\nformat binary_big_endian 1.0\nelement vertex 3\nproperty float x\n"
"property float y\nproperty float z\nelement face 1\n"
"property list uchar int vertex_indices\nend_header\n");
const std::string request =
"{\"items\":["
"{\"input\":\"" + temp_path("huge.off") + "\",\"output\":\"" + temp_path("huge.obj") +
"\",\"from\":\"off\",\"to\":\"obj\"},"
"{\"input\":\"" + temp_path("big_endian.plyb") + "\",\"output\":\"" +
temp_path("be.obj") + "\",\"from\":\"ply\",\"to\":\"obj\"}]}";
std::string response;
std::string error;
ASSERT_EQ(run("", request, &response, &error), VDE_PLUGIN_OK) << error;
EXPECT_TRUE(contains(response, "\"failed\":2"));
EXPECT_TRUE(contains(response, "上限")) << response;
EXPECT_TRUE(contains(response, "big_endian")) << response;
}
TEST_F(IoBatchMeshFixture, DeterministicOffAndPlyBinaryOutput) {
const auto make_request = [&](const std::string& suffix) {
return std::string("{\"items\":["
"{\"input\":\"") + temp_path("tri.off") + "\",\"output\":\"" +
temp_path((std::string("det_off_") + suffix + ".off").c_str()) +
"\",\"from\":\"off\",\"to\":\"off\"},"
"{\"input\":\"" + temp_path("tri.obj") + "\",\"output\":\"" +
temp_path((std::string("det_ply_") + suffix + ".plyb").c_str()) +
"\",\"from\":\"obj\",\"to\":\"ply\"}]}";
};
std::string response;
std::string error;
ASSERT_EQ(run("", make_request("a"), &response, &error), VDE_PLUGIN_OK) << error;
ASSERT_EQ(run("", make_request("b"), &response, &error), VDE_PLUGIN_OK) << error;
EXPECT_EQ(read_file(temp_path("det_off_a.off")), read_file(temp_path("det_off_b.off")));
EXPECT_EQ(read_file(temp_path("det_ply_a.plyb")), read_file(temp_path("det_ply_b.plyb")));
}
TEST_F(IoBatchMeshFixture, LargeFilesStreamingPipeline) {
constexpr std::size_t kTriangles = 200000;
const std::string large_stl = temp_path("large_200k.stl");
write_large_binary_stl(large_stl, kTriangles);
const std::uint64_t input_size = file_size_of(large_stl);
ASSERT_GT(input_size, 8ull * 1024ull * 1024ull);
ASSERT_GT(input_size, 0u);
const auto begin = std::chrono::steady_clock::now();
const std::string request =
"{\"items\":["
"{\"input\":\"" + large_stl + "\",\"output\":\"" + temp_path("large_200k.obj") +
"\",\"from\":\"stl\",\"to\":\"obj\"},"
"{\"input\":\"" + large_stl + "\",\"output\":\"" + temp_path("large_200k.ply") +
"\",\"from\":\"stl\",\"to\":\"ply\"},"
"{\"input\":\"" + large_stl + "\",\"output\":\"" + temp_path("large_200k.plyb") +
"\",\"from\":\"stl\",\"to\":\"ply\"},"
"{\"input\":\"" + large_stl + "\",\"output\":\"" + temp_path("large_200k.off") +
"\",\"from\":\"stl\",\"to\":\"off\"}]}";
std::string response;
std::string error;
ASSERT_EQ(run("", request, &response, &error), VDE_PLUGIN_OK) << error << g_last_error;
const auto end = std::chrono::steady_clock::now();
const auto elapsed_ms =
std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
EXPECT_TRUE(contains(response, "\"processed\":4")) << response;
EXPECT_TRUE(contains(response, "\"failed\":0"));
EXPECT_TRUE(contains(response, "\"triangles\":200000")) << response;
std::cout << "[bench] P4-62 large STL(200k) -> OBJ/PLY/PLY-binary/OFF: " << elapsed_ms
<< " ms, input=" << input_size << " bytes\n";
ASSERT_GT(file_size_of(temp_path("large_200k.obj")), 4ull * 1024ull * 1024ull);
ASSERT_GT(file_size_of(temp_path("large_200k.ply")), 8ull * 1024ull * 1024ull);
ASSERT_GT(file_size_of(temp_path("large_200k.plyb")), 6ull * 1024ull * 1024ull);
ASSERT_GT(file_size_of(temp_path("large_200k.off")), 4ull * 1024ull * 1024ull);
EXPECT_EQ(count_file_lines_with_prefix(temp_path("large_200k.obj"), "f "), kTriangles);
EXPECT_TRUE(contains(read_file(temp_path("large_200k.ply")),
"element face 200000"));
EXPECT_TRUE(contains(read_file(temp_path("large_200k.plyb")),
"format binary_little_endian 1.0"));
EXPECT_TRUE(read_file(temp_path("large_200k.off")).rfind("OFF", 0) == 0);
// 回读:OBJ(流式 ASCII 行)/PLY ascii/PLY binary/OFF → STL(流式写出)
const auto begin2 = std::chrono::steady_clock::now();
const std::string readback =
"{\"items\":["
"{\"input\":\"" + temp_path("large_200k.obj") + "\",\"output\":\"" +
temp_path("readback_obj.stl") + "\",\"from\":\"obj\",\"to\":\"stl\"},"
"{\"input\":\"" + temp_path("large_200k.ply") + "\",\"output\":\"" +
temp_path("readback_ply.obj") + "\",\"from\":\"ply\",\"to\":\"obj\"},"
"{\"input\":\"" + temp_path("large_200k.plyb") + "\",\"output\":\"" +
temp_path("readback_plyb.obj") + "\",\"from\":\"ply\",\"to\":\"obj\"},"
"{\"input\":\"" + temp_path("large_200k.off") + "\",\"output\":\"" +
temp_path("readback_off.obj") + "\",\"from\":\"off\",\"to\":\"obj\"}]}";
ASSERT_EQ(run("", readback, &response, &error), VDE_PLUGIN_OK) << error << g_last_error;
const auto end2 = std::chrono::steady_clock::now();
EXPECT_TRUE(contains(response, "\"processed\":4")) << response;
EXPECT_TRUE(contains(response, "\"failed\":0"));
EXPECT_TRUE(contains(response, "\"triangles\":200000"));
EXPECT_EQ(count_file_lines_with_prefix(temp_path("readback_obj.stl"), " vertex"),
kTriangles * 3u);
EXPECT_EQ(count_file_lines_with_prefix(temp_path("readback_ply.obj"), "f "), kTriangles);
EXPECT_EQ(count_file_lines_with_prefix(temp_path("readback_plyb.obj"), "f "), kTriangles);
EXPECT_EQ(count_file_lines_with_prefix(temp_path("readback_off.obj"), "f "), kTriangles);
std::cout << "[bench] P4-62 large stream readback: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(end2 - begin2).count()
<< " ms\n";
}
TEST_F(IoBatchMeshFixture, StreamingAndLegacyPathsByteIdentical) {
const auto make_request = [&](const std::string& suffix) {
return std::string("{\"items\":["
"{\"input\":\"") + temp_path("tri.stl") + "\",\"output\":\"" +
temp_path((std::string("identical_") + suffix + ".obj").c_str()) +
"\",\"from\":\"stl\",\"to\":\"obj\"},"
"{\"input\":\"" + temp_path("tri.obj") + "\",\"output\":\"" +
temp_path((std::string("identical_") + suffix + ".ply").c_str()) +
"\",\"from\":\"obj\",\"to\":\"ply\"},"
"{\"input\":\"" + temp_path("tri.off") + "\",\"output\":\"" +
temp_path((std::string("identical_") + suffix + ".stl").c_str()) +
"\",\"from\":\"off\",\"to\":\"stl\"},"
"{\"input\":\"" + temp_path("tri_binary.plyb") + "\",\"output\":\"" +
temp_path((std::string("identical_") + suffix + ".plyb").c_str()) +
"\",\"from\":\"ply\",\"to\":\"ply\"},"
"{\"input\":\"" + temp_path("tri.ply") + "\",\"output\":\"" +
temp_path((std::string("identical_") + suffix + ".off").c_str()) +
"\",\"from\":\"ply\",\"to\":\"off\"}]}";
};
std::string response;
std::string error;
#if defined(_WIN32)
_putenv_s("VDE_IO_BATCH_FORCE_STREAM", "");
#else
unsetenv("VDE_IO_BATCH_FORCE_STREAM");
#endif
ASSERT_EQ(run("", make_request("legacy"), &response, &error), VDE_PLUGIN_OK)
<< error << g_last_error;
EXPECT_TRUE(contains(response, "\"processed\":5")) << response;
#if defined(_WIN32)
_putenv_s("VDE_IO_BATCH_FORCE_STREAM", "1");
#else
setenv("VDE_IO_BATCH_FORCE_STREAM", "1", 1);
#endif
ASSERT_EQ(run("", make_request("stream"), &response, &error), VDE_PLUGIN_OK)
<< error << g_last_error;
EXPECT_TRUE(contains(response, "\"processed\":5")) << response;
#if defined(_WIN32)
_putenv_s("VDE_IO_BATCH_FORCE_STREAM", "");
#else
unsetenv("VDE_IO_BATCH_FORCE_STREAM");
#endif
const char* extensions[] = {".obj", ".ply", ".stl", ".plyb", ".off"};
for (const char* extension : extensions) {
const std::string legacy = read_file(temp_path(
(std::string("identical_legacy") + extension).c_str()));
const std::string streaming = read_file(temp_path(
(std::string("identical_stream") + extension).c_str()));
EXPECT_FALSE(legacy.empty()) << extension;
EXPECT_EQ(legacy, streaming) << "streaming/legacy 输出不一致: " << extension;
}
}
TEST_F(IoBatchMeshFixture, PlyAsciiTokenStreamMultiRecordAndStreamModes) {
const std::string compact = temp_path("tri_compact.ply");
const auto make_request = [&](const std::string& options, const std::string& suffix) {
return std::string("{\"items\":["
"{\"input\":\"") + compact + "\",\"output\":\"" +
temp_path((std::string("compact_") + suffix + ".obj").c_str()) +
"\",\"from\":\"ply\",\"to\":\"obj\"}]}";
};
std::string response;
std::string error;
ASSERT_EQ(run("{\"stream\":\"always\"}", make_request("always", "always"), &response,
&error),
VDE_PLUGIN_OK) << error << g_last_error;
EXPECT_TRUE(contains(response, "\"processed\":1")) << response;
EXPECT_TRUE(contains(response, "\"triangles\":2")) << response;
EXPECT_EQ(count_file_lines_with_prefix(temp_path("compact_always.obj"), "f "), 2u);
// auto 下 PLY ASCII 同样走 token 流(多记录/跨行)
ASSERT_EQ(run("", make_request("auto", "auto"), &response, &error), VDE_PLUGIN_OK)
<< error;
EXPECT_EQ(read_file(temp_path("compact_auto.obj")),
read_file(temp_path("compact_always.obj")));
// never:旧内存路径;当前实现对紧凑 token 文本也能解析,输出与 always 一致。
ASSERT_EQ(run("{\"stream\":\"never\"}", make_request("never", "never"), &response,
&error),
VDE_PLUGIN_OK) << error;
EXPECT_TRUE(contains(response, "\"processed\":1")) << response;
EXPECT_TRUE(contains(response, "\"triangles\":2")) << response;
// 非法 options.stream
EXPECT_EQ(run("{\"stream\":\"sometimes\"}", make_request("bad", "bad"), &response,
&error),
VDE_PLUGIN_ERR_INVALID_ARG);
EXPECT_TRUE(contains(g_last_error, "options.stream")) << g_last_error;
// 环境变量模式 + FORCE_STREAM 旧别名
#if defined(_WIN32)
_putenv_s("VDE_IO_BATCH_STREAM_MODE", "always");
_putenv_s("VDE_IO_BATCH_FORCE_STREAM", "");
#else
setenv("VDE_IO_BATCH_STREAM_MODE", "always", 1);
unsetenv("VDE_IO_BATCH_FORCE_STREAM");
#endif
ASSERT_EQ(run("", make_request("env", "env"), &response, &error), VDE_PLUGIN_OK)
<< error << g_last_error;
EXPECT_TRUE(contains(response, "\"triangles\":2")) << response;
EXPECT_EQ(read_file(temp_path("compact_env.obj")),
read_file(temp_path("compact_always.obj")));
#if defined(_WIN32)
_putenv_s("VDE_IO_BATCH_STREAM_MODE", "bogus");
#else
setenv("VDE_IO_BATCH_STREAM_MODE", "bogus", 1);
#endif
EXPECT_EQ(run("", make_request("badenv", "badenv"), &response, &error),
VDE_PLUGIN_ERR_INVALID_ARG);
#if defined(_WIN32)
_putenv_s("VDE_IO_BATCH_STREAM_MODE", "");
#else
unsetenv("VDE_IO_BATCH_STREAM_MODE");
#endif
}
TEST_F(IoBatchMeshFixture, StreamModesProduceIdenticalSmallFileBytes) {
const auto make_request = [&](const std::string& suffix) {
return std::string("{\"items\":["
"{\"input\":\"") + temp_path("tri.stl") + "\",\"output\":\"" +
temp_path((std::string("modes_") + suffix + ".obj").c_str()) +
"\",\"from\":\"stl\",\"to\":\"obj\"},"
"{\"input\":\"" + temp_path("tri.obj") + "\",\"output\":\"" +
temp_path((std::string("modes_") + suffix + ".ply").c_str()) +
"\",\"from\":\"obj\",\"to\":\"ply\"},"
"{\"input\":\"" + temp_path("tri.off") + "\",\"output\":\"" +
temp_path((std::string("modes_") + suffix + ".plyb").c_str()) +
"\",\"from\":\"off\",\"to\":\"ply\"},"
"{\"input\":\"" + temp_path("tri.ply") + "\",\"output\":\"" +
temp_path((std::string("modes_") + suffix + ".stl").c_str()) +
"\",\"from\":\"ply\",\"to\":\"stl\"}]}";
};
std::string response;
std::string error;
const char* modes[] = {"auto", "always", "never"};
for (const char* mode : modes) {
const std::string options = std::string("{\"stream\":\"") + mode + "\"}";
ASSERT_EQ(run(options, make_request(mode), &response, &error), VDE_PLUGIN_OK)
<< mode << ": " << error << g_last_error;
EXPECT_TRUE(contains(response, "\"processed\":4")) << mode << ": " << response;
}
const char* extensions[] = {".obj", ".ply", ".plyb", ".stl"};
for (const char* extension : extensions) {
const std::string auto_text = read_file(
temp_path((std::string("modes_auto") + extension).c_str()));
const std::string always_text = read_file(
temp_path((std::string("modes_always") + extension).c_str()));
const std::string never_text = read_file(
temp_path((std::string("modes_never") + extension).c_str()));
EXPECT_FALSE(auto_text.empty()) << extension;
EXPECT_EQ(auto_text, always_text) << extension;
EXPECT_EQ(auto_text, never_text) << extension;
}
}