Files
ViewDesignEngine/tests/core/test_material_library.cpp
T
hm a303114ef6 VDE-584~588: Feedback from ViewDesign Batch 254-258 panels
- VDE-584: Structural FEA mesh gen - Delaunay 3D tetrahedral meshing + quality optimization + CAE format export (Abaqus/Ansys/Nastran)

- VDE-585: FeatureWorks face extraction - mesh face clustering by normal direction, face type classification (planar/cylindrical/conical/toroidal)

- VDE-586: FeatureWorks measurement - mass properties (volume/centroid/inertia), surface area, bounding box from Shape geometry

- VDE-587: Python Shape module - pybind11 bindings for Shape creation, boolean ops, measurement, serialization to JSON

- VDE-588: PDM Shape validation - mesh validity check, watertightness, closed manifold detection, file stats from Shape
2026-08-07 15:39:39 +08:00

151 lines
4.4 KiB
C++

#include <gtest/gtest.h>
#include "vde/core/material_library.h"
#include <algorithm>
using namespace vde::core;
// ═══ MaterialDatabase 基础测试 ═══
TEST(MaterialLibraryTest, InitPopulatesDatabase) {
init_native_material_db();
EXPECT_GT(MaterialDatabase::instance().count(), 0u);
}
TEST(MaterialLibraryTest, Singleton_ReturnsSameReference) {
auto& a = MaterialDatabase::instance();
auto& b = MaterialDatabase::instance();
EXPECT_EQ(&a, &b);
}
TEST(MaterialLibraryTest, Count_GreaterThanZero) {
init_native_material_db();
EXPECT_GT(MaterialDatabase::instance().count(), 0u);
}
TEST(MaterialLibraryTest, Get_SteelReturnsValid) {
init_native_material_db();
auto mat = MaterialDatabase::instance().get("Steel A36");
ASSERT_TRUE(mat.has_value());
EXPECT_EQ(mat->name, "Steel A36");
EXPECT_FALSE(mat->category.empty());
}
TEST(MaterialLibraryTest, Get_NonExistentReturnsNullopt) {
init_native_material_db();
auto mat = MaterialDatabase::instance().get("Unobtanium");
EXPECT_FALSE(mat.has_value());
}
TEST(MaterialLibraryTest, All_ReturnsAllMaterials) {
init_native_material_db();
auto all = MaterialDatabase::instance().all();
EXPECT_EQ(all.size(), MaterialDatabase::instance().count());
EXPECT_FALSE(all.empty());
}
TEST(MaterialLibraryTest, Add_IncreasesCount) {
auto& db = MaterialDatabase::instance();
size_t before = db.count();
MaterialProperties mat;
mat.name = "Testium";
mat.category = "metal";
mat.density_kgm3 = 9999.0;
db.add(mat);
EXPECT_EQ(db.count(), before + 1);
}
TEST(MaterialLibraryTest, Add_CanRetrieveAdded) {
auto& db = MaterialDatabase::instance();
MaterialProperties mat;
mat.name = "CustomAlloy";
mat.category = "metal";
mat.density_kgm3 = 7200.0;
mat.yield_strength_mpa = 500.0;
db.add(mat);
auto retrieved = db.get("CustomAlloy");
ASSERT_TRUE(retrieved.has_value());
EXPECT_EQ(retrieved->name, "CustomAlloy");
EXPECT_DOUBLE_EQ(retrieved->density_kgm3, 7200.0);
}
TEST(MaterialLibraryTest, Clear_ResetsCount) {
auto& db = MaterialDatabase::instance();
db.clear();
EXPECT_EQ(db.count(), 0u);
}
// ═══ material_search 测试 ═══
TEST(MaterialLibraryTest, Search_ByCategory) {
init_native_material_db();
MaterialSearchQuery q;
q.category = "metal";
auto results = material_search(q);
EXPECT_FALSE(results.empty());
for (auto& m : results) {
EXPECT_EQ(m.category, "metal");
}
}
TEST(MaterialLibraryTest, Search_ByDensityRange) {
init_native_material_db();
MaterialSearchQuery q;
q.min_density = 7000.0;
q.max_density = 9000.0;
auto results = material_search(q);
EXPECT_FALSE(results.empty());
for (auto& m : results) {
EXPECT_GE(m.density_kgm3, 7000.0);
EXPECT_LE(m.density_kgm3, 9000.0);
}
}
TEST(MaterialLibraryTest, Search_ByNamePattern) {
init_native_material_db();
MaterialSearchQuery q;
q.name_pattern = "Steel";
auto results = material_search(q);
EXPECT_FALSE(results.empty());
bool found = false;
for (auto& m : results) {
if (m.name.find("Steel") != std::string::npos) found = true;
}
EXPECT_TRUE(found);
}
// ═══ material_properties 便捷函数 ═══
TEST(MaterialLibraryTest, MaterialProperties_ConvenienceFunction) {
init_native_material_db();
auto props = material_properties("Steel A36");
ASSERT_TRUE(props.has_value());
EXPECT_EQ(props->name, "Steel A36");
}
TEST(MaterialLibraryTest, MaterialProperties_NonExistent) {
auto props = material_properties("FairyDust");
EXPECT_FALSE(props.has_value());
}
TEST(MaterialLibraryTest, MaterialProperties_FieldsPopulated) {
init_native_material_db();
auto props = material_properties("Steel A36");
ASSERT_TRUE(props.has_value());
EXPECT_FALSE(props->name.empty());
EXPECT_FALSE(props->category.empty());
EXPECT_GT(props->density_kgm3, 0.0);
EXPECT_GT(props->youngs_modulus_gpa, 0.0);
EXPECT_GE(props->poisson_ratio, 0.0);
EXPECT_LE(props->poisson_ratio, 0.5);
EXPECT_GT(props->yield_strength_mpa, 0.0);
EXPECT_FALSE(props->standard.empty());
}
TEST(MaterialLibraryTest, MaterialProperties_DensityReasonable) {
init_native_material_db();
auto all = MaterialDatabase::instance().all();
for (auto& m : all) {
EXPECT_GT(m.density_kgm3, 0.0);
EXPECT_LT(m.density_kgm3, 100000.0);
}
}