This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/blenlib/tests/BLI_pool_test.cc
Clément Foucault e047b2618a BLI: Add new blender::Pool container
A `blender::Pool` can construct and destruct elements without reordering. Freed items memory
will be reused by next allocations.

Elements are allocated in chunks to reduce memory fragmentation and avoid reallocation.

Reviewed By: JacquesLucke

Differential Revision: https://developer.blender.org/D15894
2022-09-06 17:51:25 +02:00

52 lines
891 B
C++

/* SPDX-License-Identifier: Apache-2.0 */
#include "BLI_exception_safety_test_utils.hh"
#include "BLI_pool.hh"
#include "BLI_strict_flags.h"
#include "testing/testing.h"
namespace blender::tests {
TEST(pool, DefaultConstructor)
{
Pool<int> pool;
EXPECT_EQ(pool.size(), 0);
}
TEST(pool, Allocation)
{
Vector<int *> ptrs;
Pool<int> pool;
for (int i = 0; i < 100; i++) {
ptrs.append(&pool.construct(i));
}
EXPECT_EQ(pool.size(), 100);
for (int *ptr : ptrs) {
pool.destruct(*ptr);
}
EXPECT_EQ(pool.size(), 0);
}
TEST(pool, Reuse)
{
Vector<int *> ptrs;
Pool<int> pool;
for (int i = 0; i < 32; i++) {
ptrs.append(&pool.construct(i));
}
int *freed_ptr = ptrs[6];
pool.destruct(*freed_ptr);
ptrs[6] = &pool.construct(0);
EXPECT_EQ(ptrs[6], freed_ptr);
for (int *ptr : ptrs) {
pool.destruct(*ptr);
}
}
} // namespace blender::tests