Files
ViewDesignEngine/tests/core/test_object_pool.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

102 lines
2.8 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.
#include <gtest/gtest.h>
#include "vde/core/object_pool.h"
#include <thread>
#include <vector>
using namespace vde::core;
struct TestObj { int x = 0; double y = 0.0; };
TEST(ObjectPoolTest, AcquireRelease) {
ObjectPool<TestObj> pool;
auto* obj = pool.acquire();
ASSERT_NE(obj, nullptr);
obj->x = 42;
pool.release(obj);
auto* obj2 = pool.acquire(); // should reuse
EXPECT_EQ(obj2->x, 0); // reset to default
EXPECT_GT(pool.reuse_rate(), 0.0);
}
TEST(ObjectPoolTest, AcquireMany) {
ObjectPool<TestObj, 64> pool;
std::vector<TestObj*> objs;
for (int i = 0; i < 1000; ++i) objs.push_back(pool.acquire());
for (auto* o : objs) pool.release(o);
EXPECT_EQ(pool.total_allocated(), 1000u);
EXPECT_GT(pool.total_reused(), 0u);
}
TEST(ObjectPoolTest, ReleaseNullptr) {
ObjectPool<TestObj> pool;
pool.release(nullptr); // should not crash
EXPECT_EQ(pool.total_allocated(), 0u);
}
TEST(ObjectPoolTest, ExpandChunk) {
ObjectPool<TestObj, 10> pool;
// Acquire all 25 without releasing to force expansion (3 chunks of 10)
std::vector<TestObj*> objs;
for (int i = 0; i < 25; ++i) {
objs.push_back(pool.acquire());
}
EXPECT_GE(pool.pool_size(), 30u); // 3 chunks × 10 = 30
for (auto* o : objs) pool.release(o);
}
TEST(ThreadSafePoolTest, ConcurrentAcquire) {
ThreadSafeObjectPool<TestObj, 64> pool;
std::atomic<int> count{0};
auto worker = [&]() {
for (int i = 0; i < 100; ++i) {
auto* o = pool.acquire();
o->x = i;
pool.release(o);
count++;
}
};
std::thread t1(worker), t2(worker), t3(worker), t4(worker);
t1.join(); t2.join(); t3.join(); t4.join();
EXPECT_EQ(count, 400);
}
TEST(CowPtrTest, ReadOnly) {
CowPtr<std::vector<int>> cow(std::vector<int>{1, 2, 3});
EXPECT_EQ(cow.read().size(), 3u);
EXPECT_EQ(cow.read()[0], 1);
}
TEST(CowPtrTest, ShallowCopy) {
CowPtr<std::vector<int>> cow(std::vector<int>{1, 2, 3});
auto cow2 = cow.shallow_copy();
EXPECT_EQ(cow.use_count(), 2);
EXPECT_EQ(cow2.use_count(), 2);
}
TEST(CowPtrTest, WriteDetaches) {
CowPtr<std::vector<int>> cow(std::vector<int>{1, 2, 3});
auto cow2 = cow.shallow_copy();
EXPECT_EQ(cow.use_count(), 2);
cow.write().push_back(4);
EXPECT_EQ(cow.use_count(), 1); // detached
EXPECT_EQ(cow2.use_count(), 1);
EXPECT_EQ(cow2.read().size(), 3u); // cow2 unchanged
}
TEST(CowPtrTest, DeepCopy) {
CowPtr<std::vector<int>> cow(std::vector<int>{1, 2, 3});
auto cow2 = cow.deep_copy();
EXPECT_EQ(cow.use_count(), 1);
EXPECT_EQ(cow2.use_count(), 1);
}
TEST(CowPtrTest, UniqueCheck) {
CowPtr<int> cow(42);
EXPECT_TRUE(cow.unique());
auto cow2 = cow.shallow_copy();
EXPECT_FALSE(cow.unique());
}