Add unit tests for block_pool

This commit is contained in:
mutouyun 2023-12-24 12:54:43 +08:00
parent c10d84a8f2
commit d995c693f3
2 changed files with 27 additions and 1 deletions

View File

@ -18,7 +18,7 @@ LIBPMR_NAMESPACE_BEG_
* \brief `synchronized_pool_resource` may be accessed from multiple threads without external synchronization,
* and have thread-specific pools to reduce synchronization costs.
* \note Unlike the standard library implementation, `synchronized_pool_resource` automatically manages
* the block size and reclaims all allocated memory to the central heap when the thread is destroyed,
* the block size and reclaims all deallocated memory to the central heap when the thread is destroyed,
* rather than being destructed on its own.
* \see https://en.cppreference.com/w/cpp/memory/synchronized_pool_resource
*/

View File

@ -61,3 +61,29 @@ TEST(block_pool, central_cache_pool_ctor) {
EXPECT_NE(b1, b4);
}
}
TEST(block_pool, ctor) {
ASSERT_TRUE ((std::is_default_constructible<pmr::block_pool<1, 1>>::value));
ASSERT_FALSE((std::is_copy_constructible<pmr::block_pool<1, 1>>::value));
ASSERT_FALSE((std::is_move_constructible<pmr::block_pool<1, 1>>::value));
ASSERT_FALSE((std::is_copy_assignable<pmr::block_pool<1, 1>>::value));
ASSERT_FALSE((std::is_move_assignable<pmr::block_pool<1, 1>>::value));
}
TEST(block_pool, allocate) {
std::vector<void *> v;
pmr::block_pool<1, 1> pool;
for (int i = 0; i < 100; ++i) {
v.push_back(pool.allocate());
}
for (void *p: v) {
ASSERT_FALSE(nullptr == p);
pool.deallocate(p);
}
for (int i = 0; i < 100; ++i) {
ASSERT_EQ(v[v.size() - i - 1], pool.allocate());
}
for (void *p: v) {
pool.deallocate(p);
}
}