#include #include "vde/core/object_pool.h" #include #include #include #include #include using namespace vde::core; struct TestObj { int x = 0; double y = 0.0; }; TEST(ObjectPoolTest, AcquireRelease) { ObjectPool 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 pool; std::vector 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 pool; pool.release(nullptr); // should not crash EXPECT_EQ(pool.total_allocated(), 0u); } TEST(ObjectPoolTest, ExpandChunk) { ObjectPool pool; // Acquire all 25 without releasing to force expansion (3 chunks of 10) std::vector 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 pool; std::atomic 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); } // Regression: two live ThreadSafeObjectPool instances must never hand out each // other's memory. The previous implementation used one shared thread_local // ObjectPool for all instances, so a release() could push an instance-local // pointer into the shared per-thread free list and a later instance (or a // destroyed instance's chunk) could be handed back out. TEST(ThreadSafePoolTest, TwoInstancesAreIsolated) { ThreadSafeObjectPool pool_a; ThreadSafeObjectPool pool_b; std::vector a; std::vector b; for (int i = 0; i < 8; ++i) a.push_back(pool_a.acquire()); for (int i = 0; i < 8; ++i) b.push_back(pool_b.acquire()); const std::set b_set(b.begin(), b.end()); const std::set a_set(a.begin(), a.end()); ASSERT_EQ(a_set.size(), 8u); ASSERT_EQ(b_set.size(), 8u); for (auto* p : a) EXPECT_EQ(b_set.count(p), 0u); for (auto* p : b) EXPECT_EQ(a_set.count(p), 0u); for (auto* p : a) pool_a.release(p); for (auto* p : b) pool_b.release(p); // Re-acquiring from A must reuse A's own released objects only. for (int i = 0; i < 8; ++i) { TestObj* p = pool_a.acquire(); ASSERT_NE(p, nullptr); EXPECT_EQ(a_set.count(p), 1u); EXPECT_EQ(b_set.count(p), 0u); } EXPECT_EQ(pool_a.total_allocated(), 16u); EXPECT_EQ(pool_b.total_allocated(), 8u); } // Regression: repeatedly creating and destroying pools in the same thread must // not leave dangling free-list entries behind (the old thread_local pool // outlived each instance and kept pointers into freed chunks). TEST(ThreadSafePoolTest, RepeatedCreateDestroySameThread) { for (int round = 0; round < 200; ++round) { ThreadSafeObjectPool pool; std::vector objs; for (int i = 0; i < 40; ++i) { // forces several chunks auto* o = pool.acquire(); ASSERT_NE(o, nullptr); o->x = i; objs.push_back(o); } for (auto* o : objs) pool.release(o); // All 40 must be reusable after release, with a clean default state. for (int i = 0; i < 40; ++i) { auto* o = pool.acquire(); ASSERT_NE(o, nullptr); EXPECT_EQ(o->x, 0); o->x = i; pool.release(o); } } SUCCEED(); } // Pointers acquired by one thread may be released by another. TEST(ThreadSafePoolTest, ReleaseFromOtherThread) { ThreadSafeObjectPool pool; std::vector objs; for (int i = 0; i < 64; ++i) objs.push_back(pool.acquire()); constexpr int kThreads = 4; std::vector threads; for (int t = 0; t < kThreads; ++t) { threads.emplace_back([&pool, &objs, t]() { for (size_t i = static_cast(t); i < objs.size(); i += kThreads) pool.release(objs[i]); }); } for (auto& th : threads) th.join(); // Everything must be back in this pool and reusable. std::vector again; for (int i = 0; i < 64; ++i) { TestObj* p = pool.acquire(); ASSERT_NE(p, nullptr); again.push_back(p); } EXPECT_EQ(std::set(again.begin(), again.end()).size(), 64u); for (auto* p : again) pool.release(p); } TEST(CowPtrTest, ReadOnly) { CowPtr> cow(std::vector{1, 2, 3}); EXPECT_EQ(cow.read().size(), 3u); EXPECT_EQ(cow.read()[0], 1); } TEST(CowPtrTest, ShallowCopy) { CowPtr> cow(std::vector{1, 2, 3}); auto cow2 = cow.shallow_copy(); EXPECT_EQ(cow.use_count(), 2); EXPECT_EQ(cow2.use_count(), 2); } TEST(CowPtrTest, WriteDetaches) { CowPtr> cow(std::vector{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> cow(std::vector{1, 2, 3}); auto cow2 = cow.deep_copy(); EXPECT_EQ(cow.use_count(), 1); EXPECT_EQ(cow2.use_count(), 1); } TEST(CowPtrTest, UniqueCheck) { CowPtr cow(42); EXPECT_TRUE(cow.unique()); auto cow2 = cow.shallow_copy(); EXPECT_FALSE(cow.unique()); }