// WP-P4-24 官方 COLLADA(.dae)格式插件测试(确定性 mock 宿主 + 真实 C ABI 动态加载)。 // // 覆盖: // 1) vde_plugin_query / init / describe / manifest 的 id、能力、扩展名与 ABI; // 2) COLLADA 1.4/1.5 XML 读取:单 geometry、多 geometry、多个 合并、offset/stride; // 2b) P4-31:/(vcount/p、3/4/>4 边形扇形三角化、多 input offset/stride、 // 混合 primitive、多 geometry→多 body、vcount 不匹配/索引越界/不支持 primitive 负例); // 3) 写出:mock 宿主 body_tessellate -> 每体一个 ;write_file 与 write_memory 一致,写后重读; // 4) 错误路径:空文件、缺少 //positions source、越界索引、 // 非法版本、标签截断、超限; // 5) 宿主能力缺失、入口参数校验、空实体、三角化失败等明确错误。 // // 本测试只依赖纯 C ABI(vde_plugin_api.h)+ gtest,不链接内核。 #include #include #include #include #include #include #include #include #include #if defined(_WIN32) #ifndef NOMINMAX #define NOMINMAX #endif #include #else #include #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.collada"; #if defined(_WIN32) constexpr const char* kPluginRelativePath = "/collada/vde_official_collada.dll"; #elif defined(__APPLE__) constexpr const char* kPluginRelativePath = "/collada/libvde_official_collada.dylib"; #else constexpr const char* kPluginRelativePath = "/collada/libvde_official_collada.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(::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(::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(find_symbol(library, "vde_plugin_query")); init = reinterpret_cast(find_symbol(library, "vde_plugin_init")); shutdown = reinterpret_cast( 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 positions; // 3×N std::vector indices; // 3×T bool fail_tessellate = false; bool empty_tessellate = false; }; MockBody* as_mock(VdeBody body) { return reinterpret_cast(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(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(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(std::malloc(vertices * 3u * sizeof(double))); auto* indices = static_cast(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(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(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; } // ── 夹具文本 ──────────────────────────────────────────────────────── const char* kSimpleCollada = "\n" "\n" " Y_UP\n" " \n" " \n" " \n" " \n" " " "0 0 0 10 0 0 0 10 0\n" " \n" " " "\n" " \n" " \n" " \n" " \n" " \n" "

0 1 2

\n" "
\n" "
\n" "
\n" "
\n" "
\n"; std::string multi_collada(const char* version) { return std::string("\n") + "\n \n" + " \n" + " " "0 0 0 1 0 0 0 1 0\n" + " \n" + "

0 1 2

\n" + "
\n" + " \n" + " " "20 0 0 21 0 0 20 1 0 20 1 1\n" + " \n" + "

0 1 2 0 2 3

\n" + "
\n" + "
\n
\n"; } // 多 input(offset 0=VERTEX, 1=NORMAL)的 stride 解析样例。 const char* kOffsetStrideCollada = "\n" "\n" " 0 0 0 1 0 0 0 1 0" "\n" " \n" " \n" " \n" " \n" "

0 0 1 1 2 2

\n" "
\n" "
\n"; 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(in)), std::istreambuf_iterator()); } 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(text.size())); out.close(); return out.good(); } std::size_t count_occurrences(const std::string& text, const std::string& needle) { std::size_t count = 0; for (std::size_t p = text.find(needle); p != std::string::npos; p = text.find(needle, p + needle.size())) { ++count; } return count; } // ── 测试夹具 ──────────────────────────────────────────────────────── class ColladaPluginFixture : 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(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(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_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); } 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); } MockBody make_triangle() { MockBody body; body.positions = {0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0}; body.indices = {0u, 1u, 2u}; return body; } void destroy_bodies(std::vector& bodies) { for (VdeBody body : bodies) host_.body_destroy(body); bodies.clear(); } PluginApi api_; std::string load_error_; VdePluginHost host_{}; const VdeFormatPlugin* format_ = nullptr; }; } // namespace // ── 1. 插件元信息 / 注册 ─────────────────────────────────────────── TEST_F(ColladaPluginFixture, QueryAndRegistrationMetadata) { VdePluginInfo info{}; info.struct_size = static_cast(sizeof(VdePluginInfo)); ASSERT_EQ(api_.query(VDE_PLUGIN_ABI_MAJOR, &info), VDE_PLUGIN_OK); EXPECT_STREQ(info.id, "com.vde.official.collada"); EXPECT_EQ(info.kind, VDE_PLUGIN_KIND_FORMAT); EXPECT_EQ(info.abi_major, VDE_PLUGIN_ABI_MAJOR); EXPECT_EQ(info.abi_minor, VDE_PLUGIN_ABI_MINOR); EXPECT_STREQ(info.capabilities, "format:dae.read;format:dae.write"); ASSERT_NE(format_, nullptr); EXPECT_STREQ(format_->format_id, "dae"); EXPECT_STREQ(format_->extensions, ".dae"); 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); } TEST_F(ColladaPluginFixture, ProbeRecognizesColladaOnly) { const std::string collada = kSimpleCollada; EXPECT_EQ(format_->probe(collada.data(), collada.size()), 92); const char* other = ""; EXPECT_EQ(format_->probe(other, std::strlen(other)), 0); EXPECT_EQ(format_->probe(nullptr, 0), 0); } // ── 2. 读取 ──────────────────────────────────────────────────────── TEST_F(ColladaPluginFixture, ReadSingleGeometryEntityCount) { VdeBody* bodies = nullptr; std::size_t count = 0; ASSERT_EQ(read_text(kSimpleCollada, &bodies, &count), VDE_PLUGIN_OK) << g_last_error; ASSERT_NE(bodies, nullptr); ASSERT_EQ(count, 1u); const MockBody* body = as_mock(bodies[0]); ASSERT_NE(body, nullptr); EXPECT_EQ(body->positions.size(), 9u); EXPECT_EQ(body->indices.size(), 3u); host_.body_destroy(bodies[0]); host_.free(bodies); } TEST_F(ColladaPluginFixture, ReadMultiGeometryAndVersion15) { for (const char* version : {"1.4.1", "1.5.0"}) { const std::string text = multi_collada(version); VdeBody* bodies = nullptr; std::size_t count = 0; ASSERT_EQ(read_text(text, &bodies, &count), VDE_PLUGIN_OK) << version << " / " << g_last_error; ASSERT_NE(bodies, nullptr); ASSERT_EQ(count, 2u) << "多 geometry 应生成 2 个实体 (" << version << ")"; const MockBody* first = as_mock(bodies[0]); const MockBody* second = as_mock(bodies[1]); EXPECT_EQ(first->indices.size(), 3u); EXPECT_EQ(second->indices.size(), 6u); host_.body_destroy(bodies[0]); host_.body_destroy(bodies[1]); host_.free(bodies); } } TEST_F(ColladaPluginFixture, ReadOffsetStrideInputs) { VdeBody* bodies = nullptr; std::size_t count = 0; ASSERT_EQ(read_text(kOffsetStrideCollada, &bodies, &count), VDE_PLUGIN_OK) << g_last_error; ASSERT_EQ(count, 1u); const MockBody* body = as_mock(bodies[0]); ASSERT_NE(body, nullptr); EXPECT_EQ(body->indices.size(), 3u); host_.body_destroy(bodies[0]); host_.free(bodies); } // ── 3. 写出与往返 ────────────────────────────────────────────────── TEST_F(ColladaPluginFixture, WriteMemorySingleBody) { MockBody triangle = make_triangle(); std::vector bodies{make_body(triangle)}; ASSERT_NE(bodies[0], nullptr) << g_last_error; VdeBuffer buffer{}; g_last_error.clear(); ASSERT_EQ(format_->write_memory(&host_, bodies.data(), bodies.size(), &buffer), VDE_PLUGIN_OK) << g_last_error; ASSERT_NE(buffer.data, nullptr); ASSERT_GT(buffer.size, 0u); const std::string xml(static_cast(buffer.data), buffer.size); EXPECT_NE(xml.find(""), std::string::npos); EXPECT_NE(xml.find(""), std::string::npos); EXPECT_NE(xml.find("

0 1 2

"), std::string::npos); EXPECT_EQ(count_occurrences(xml, " bodies{make_body(triangle), make_body(triangle)}; ASSERT_NE(bodies[0], nullptr); ASSERT_NE(bodies[1], nullptr); VdeBuffer buffer{}; ASSERT_EQ(format_->write_memory(&host_, bodies.data(), bodies.size(), &buffer), VDE_PLUGIN_OK) << g_last_error; ASSERT_NE(buffer.data, nullptr); const std::string xml(static_cast(buffer.data), buffer.size); EXPECT_EQ(count_occurrences(xml, " bodies{make_body(triangle)}; ASSERT_NE(bodies[0], nullptr); const std::string path = temp_path("p424_roundtrip.dae"); 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 written = read_text_file(path); ASSERT_FALSE(written.empty()); EXPECT_NE(written.find("read_file(&host_, path.c_str(), &again, &again_count), VDE_PLUGIN_OK) << g_last_error; ASSERT_EQ(again_count, 1u); const MockBody* body = as_mock(again[0]); ASSERT_NE(body, nullptr); EXPECT_EQ(body->positions.size(), 9u); EXPECT_EQ(body->indices.size(), 3u); host_.body_destroy(again[0]); host_.free(again); destroy_bodies(bodies); std::remove(path.c_str()); } // ── 4. 错误路径 ──────────────────────────────────────────────────── TEST_F(ColladaPluginFixture, InvalidInputsRejectedWithParse) { struct InvalidFixture { const char* name; std::string text; } cases[] = { {"empty", std::string()}, {"blank", " \n\t "}, {"no-COLLADA-root", ""}, {"no-library-geometries", ""}, {"no-geometry", "" ""}, {"missing-positions-source", "" "" "

0 1 2

" "
"}, {"index-out-of-range", "" "0 0 0 1 0 0 0 1 0" "" "

0 1 9

" "
"}, {"unsupported-version", "" ""}, {"truncated-tag", ""}, {"missing-triangle-p", "" "0 0 0 1 0 0 0 1 0" "" "" ""}, }; for (const auto& test : cases) { VdeBody* bodies = nullptr; std::size_t count = 0; EXPECT_EQ(read_text(test.text, &bodies, &count), VDE_PLUGIN_ERR_PARSE) << "应拒绝: " << test.name; EXPECT_EQ(bodies, nullptr) << test.name; EXPECT_EQ(count, 0u) << test.name; EXPECT_FALSE(g_last_error.empty()) << test.name; } } TEST_F(ColladaPluginFixture, OversizeFileRejected) { VdeBody* bodies = nullptr; std::size_t count = 0; const char tiny[] = ""; EXPECT_EQ(format_->read_memory(&host_, tiny, 600u * 1024u * 1024u, &bodies, &count), VDE_PLUGIN_ERR_PARSE); EXPECT_EQ(bodies, nullptr); } TEST_F(ColladaPluginFixture, GeometryCountLimitRejected) { std::string text = ""; text.reserve(4097 * 250); for (int i = 0; i < 4097; ++i) { text += "" "0 0 0 1 0 0 0 1 0" "" "

0 1 2

" "
"; } text += "
"; VdeBody* bodies = nullptr; std::size_t count = 0; EXPECT_EQ(read_text(text, &bodies, &count), VDE_PLUGIN_ERR_PARSE); EXPECT_EQ(bodies, nullptr); EXPECT_FALSE(g_last_error.empty()); } // ── 5. 宿主能力 / 参数校验 ───────────────────────────────────────── TEST_F(ColladaPluginFixture, MissingHostServicesReturnUnsupported) { VdePluginHost read_host = host_; read_host.body_from_mesh = nullptr; VdeBody* bodies = nullptr; std::size_t count = 0; EXPECT_EQ(format_->read_memory(&read_host, kSimpleCollada, std::strlen(kSimpleCollada), &bodies, &count), VDE_PLUGIN_ERR_UNSUPPORTED); EXPECT_EQ(bodies, nullptr); MockBody triangle = make_triangle(); std::vector body_list{make_body(triangle)}; VdePluginHost write_host = host_; write_host.body_tessellate = nullptr; VdeBuffer buffer{}; EXPECT_EQ(format_->write_memory(&write_host, body_list.data(), body_list.size(), &buffer), VDE_PLUGIN_ERR_UNSUPPORTED); EXPECT_EQ(buffer.data, nullptr); destroy_bodies(body_list); } TEST_F(ColladaPluginFixture, NullAndBadArguments) { VdePluginInfo info{}; info.struct_size = static_cast(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(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); VdeBody* bodies = nullptr; std::size_t count = 0; EXPECT_EQ(format_->read_memory(&host_, nullptr, 0, &bodies, &count), VDE_PLUGIN_ERR_PARSE); EXPECT_EQ(format_->read_memory(&host_, kSimpleCollada, std::strlen(kSimpleCollada), nullptr, &count), VDE_PLUGIN_ERR_INVALID_ARG); EXPECT_EQ(format_->read_memory(&host_, kSimpleCollada, std::strlen(kSimpleCollada), &bodies, nullptr), VDE_PLUGIN_ERR_INVALID_ARG); EXPECT_EQ(format_->read_file(&host_, nullptr, &bodies, &count), VDE_PLUGIN_ERR_INVALID_ARG); 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); VdeBuffer buffer{}; EXPECT_EQ(format_->write_memory(&host_, nullptr, 0, &buffer), VDE_PLUGIN_ERR_INVALID_ARG); std::vector empty; MockBody triangle = make_triangle(); std::vector one{make_body(triangle)}; EXPECT_EQ(format_->write_memory(&host_, empty.data(), 0, &buffer), VDE_PLUGIN_ERR_INVALID_ARG); EXPECT_EQ(format_->write_file(&host_, temp_path("p424_empty.dae").c_str(), empty.data(), 0), VDE_PLUGIN_ERR_INVALID_ARG); destroy_bodies(one); } TEST_F(ColladaPluginFixture, TessellateFailureAndEmptyTessellation) { MockBody triangle = make_triangle(); std::vector failing{make_body(triangle)}; ASSERT_NE(failing[0], nullptr); as_mock(failing[0])->fail_tessellate = true; VdeBuffer buffer{}; EXPECT_EQ(format_->write_memory(&host_, failing.data(), failing.size(), &buffer), VDE_PLUGIN_ERR_INTERNAL); EXPECT_EQ(buffer.data, nullptr); destroy_bodies(failing); MockBody empty = make_triangle(); std::vector empty_bodies{make_body(empty)}; ASSERT_NE(empty_bodies[0], nullptr); as_mock(empty_bodies[0])->empty_tessellate = true; EXPECT_EQ(format_->write_memory(&host_, empty_bodies.data(), empty_bodies.size(), &buffer), VDE_PLUGIN_ERR_INTERNAL); destroy_bodies(empty_bodies); } // ── P4-31:polylist / polygons / 混合 / vcount 不匹配 ───────────── namespace { std::string collada_mesh_with(const std::string& primitives, const std::string& positions, std::size_t position_count) { return std::string("\n") + "\n" + " " + positions + "\n" " \n" + primitives + "\n"; } std::string collada_polylist(const std::string& vcount, const std::string& p, const std::string& inputs = " \n") { return " \n" + inputs + " " + vcount + "\n

" + p + "

\n
\n"; } } // namespace TEST_F(ColladaPluginFixture, PolylistTriangleQuadPentagon) { // 12 顶点;vcount 3/4/5 → 1 + 2 + 3 = 6 个三角形 const std::string positions = "0 0 0 1 0 0 0 1 0 " "2 0 0 3 0 0 3 1 0 2 1 0 " "4 0 0 5 0 0 5 1 0 4 1 0 4.5 1.5 0"; const std::string text = collada_mesh_with( collada_polylist("3 4 5", "0 1 2 3 4 5 6 7 8 9 10 11"), positions, 12); VdeBody* bodies = nullptr; std::size_t count = 0; ASSERT_EQ(read_text(text, &bodies, &count), VDE_PLUGIN_OK) << g_last_error; ASSERT_EQ(count, 1u); MockBody* body = as_mock(bodies[0]); EXPECT_EQ(body->positions.size() / 3u, 12u); EXPECT_EQ(body->indices.size() / 3u, 6u); std::vector owned(bodies, bodies + count); destroy_bodies(owned); } TEST_F(ColladaPluginFixture, PolygonsTriangleQuadAndIgnoreHoles) { const std::string positions = "0 0 0 1 0 0 0 1 0 2 0 0 3 0 0 3 1 0 2 1 0"; const std::string text = collada_mesh_with( " \n" " \n" "

0 1 2

\n" " 0 1\n" "

3 4 5 6

\n" "
\n", positions, 7); VdeBody* bodies = nullptr; std::size_t count = 0; ASSERT_EQ(read_text(text, &bodies, &count), VDE_PLUGIN_OK) << g_last_error; ASSERT_EQ(count, 1u); MockBody* body = as_mock(bodies[0]); EXPECT_EQ(body->positions.size() / 3u, 7u); EXPECT_EQ(body->indices.size() / 3u, 3u); // 三角形 1 + 四边形 2 std::vector owned(bodies, bodies + count); destroy_bodies(owned); } TEST_F(ColladaPluginFixture, MixedTrianglesAndPolylist) { const std::string positions = "0 0 0 1 0 0 0 1 0 1 1 0"; const std::string primitives = " \n" " \n" "

0 1 2

\n" "
\n" + collada_polylist("4", "0 1 2 3"); const std::string text = collada_mesh_with(primitives, positions, 4); VdeBody* bodies = nullptr; std::size_t count = 0; ASSERT_EQ(read_text(text, &bodies, &count), VDE_PLUGIN_OK) << g_last_error; ASSERT_EQ(count, 1u); MockBody* body = as_mock(bodies[0]); EXPECT_EQ(body->indices.size() / 3u, 3u); // 1 triangle + quad→2 std::vector owned(bodies, bodies + count); destroy_bodies(owned); } TEST_F(ColladaPluginFixture, PolylistWithInputOffsets) { const std::string positions = "0 0 0 1 0 0 0 1 0"; // VERTEX offset=1,NORMAL offset=0;p 交错:n0 v0 n0 v1 n0 v2 const std::string text = collada_mesh_with( collada_polylist("3", "0 0 0 1 0 2", " \n" " \n"), positions, 3); VdeBody* bodies = nullptr; std::size_t count = 0; ASSERT_EQ(read_text(text, &bodies, &count), VDE_PLUGIN_OK) << g_last_error; ASSERT_EQ(count, 1u); MockBody* body = as_mock(bodies[0]); EXPECT_EQ(body->positions.size() / 3u, 3u); EXPECT_EQ(body->indices.size() / 3u, 1u); ASSERT_EQ(body->indices, (std::vector{0u, 1u, 2u})); std::vector owned(bodies, bodies + count); destroy_bodies(owned); } TEST_F(ColladaPluginFixture, PolylistMultipleGeometriesToMultipleBodies) { const std::string text = "\n" "\n" " \n" " 0 0 0 1 0 0 0 1 0" "\n" " \n" " " "3

0 1 2

\n" "
\n" " \n" " 20 0 0 21 0 0 21 1 0 " "20 1 0\n" " \n" " " "4

0 1 2 3

\n" "
\n" "
\n"; VdeBody* bodies = nullptr; std::size_t count = 0; ASSERT_EQ(read_text(text, &bodies, &count), VDE_PLUGIN_OK) << g_last_error; ASSERT_EQ(count, 2u); EXPECT_EQ(as_mock(bodies[0])->indices.size() / 3u, 1u); EXPECT_EQ(as_mock(bodies[1])->indices.size() / 3u, 2u); std::vector owned(bodies, bodies + count); destroy_bodies(owned); } TEST_F(ColladaPluginFixture, RejectsVcountMismatchAndBadIndex) { const std::string positions = "0 0 0 1 0 0 0 1 0 1 1 0"; { // vcount 声明 3 4 共 7 顶点,但 p 只有 6 个索引 const std::string text = collada_mesh_with( collada_polylist("3 4", "0 1 2 0 1 2"), positions, 4); VdeBody* bodies = nullptr; std::size_t count = 0; g_last_error.clear(); EXPECT_EQ(read_text(text, &bodies, &count), VDE_PLUGIN_ERR_PARSE); EXPECT_NE(g_last_error.find("vcount"), std::string::npos) << g_last_error; } { // 索引越界(位置只有 4 个) const std::string text = collada_mesh_with( collada_polylist("3", "0 1 9"), positions, 4); VdeBody* bodies = nullptr; std::size_t count = 0; g_last_error.clear(); EXPECT_EQ(read_text(text, &bodies, &count), VDE_PLUGIN_ERR_PARSE); EXPECT_FALSE(g_last_error.empty()); } { // polygons 单面只有 2 个顶点(<3 → 非法) const std::string text = collada_mesh_with( " " "

0 1

\n", positions, 4); VdeBody* bodies = nullptr; std::size_t count = 0; g_last_error.clear(); EXPECT_EQ(read_text(text, &bodies, &count), VDE_PLUGIN_ERR_PARSE); EXPECT_FALSE(g_last_error.empty()); } } TEST_F(ColladaPluginFixture, RejectsUnsupportedPrimitive) { const std::string text = collada_mesh_with( " " "

0 1 2

\n", "0 0 0 1 0 0 0 1 0", 3); VdeBody* bodies = nullptr; std::size_t count = 0; g_last_error.clear(); EXPECT_EQ(read_text(text, &bodies, &count), VDE_PLUGIN_ERR_PARSE); EXPECT_NE(g_last_error.find("tristrips"), std::string::npos) << g_last_error; }