Skip to content

Commit

Permalink
Few more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
nolan-veed committed Dec 26, 2023
1 parent 0886bde commit c5ed77d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/internal_modules/roc_core/limited_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class LimitedPool : public NonCopyable<LimitedPool>, public IPool {
//! Get size of object allocated by pool.
size_t object_size() const;

//! Reserve memory for given number of objects, after checking with the memory
//! limiter.
//! Reserve memory for given number of objects.
//! @returns
//! false if allocation failed.
ROC_ATTR_NODISCARD bool reserve(size_t n_objects);
Expand Down
17 changes: 17 additions & 0 deletions src/tests/roc_core/test_limited_arena.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,22 @@ TEST(limited_arena, enforce_limit) {
}
}

TEST(limited_arena, size_of_and_overhead) {
HeapArena heapArena;
MemoryLimiter memoryLimiter("test", 256);

{
LimitedArena arena(heapArena, memoryLimiter);

void* pointer0 = arena.allocate(128);
CHECK(pointer0);

CHECK(arena.size_of(pointer0) == 128);

arena.deallocate(pointer0);

CHECK(arena.overhead() > 0);
}
}
} // namespace core
} // namespace roc
61 changes: 61 additions & 0 deletions src/tests/roc_core/test_limited_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,33 @@ TEST(limited_pool, enforce_limit) {

void* memory0 = pool.allocate();
CHECK(memory0);
CHECK(memoryLimiter.acquired() > 128);

void* memory1 = pool.allocate();
CHECK(memory1 == NULL);

pool.deallocate(memory0);

memory1 = pool.allocate();
CHECK(memory1);

pool.deallocate(memory1);

CHECK(memoryLimiter.acquired() == 0);
}
}

TEST(limited_pool, enforce_limit_despite_reserve) {
HeapArena arena;
SlabPool<TestObject> slabPool("test", arena);
MemoryLimiter memoryLimiter("test", 256);

{
LimitedPool pool(slabPool, memoryLimiter);
CHECK(pool.reserve(5));

void* memory0 = pool.allocate();
CHECK(memory0);
CHECK(memoryLimiter.acquired() > 128);

void* memory1 = pool.allocate();
Expand All @@ -55,5 +81,40 @@ TEST(limited_pool, enforce_limit) {
}
}

TEST(limited_pool, track_but_no_enforce_limit) {
HeapArena arena;
SlabPool<TestObject> slabPool("test", arena);
MemoryLimiter memoryLimiter("test", 0);

{
LimitedPool pool(slabPool, memoryLimiter);

void* memory0 = pool.allocate();
CHECK(memory0);
CHECK(memoryLimiter.acquired() > 128);

void* memory1 = pool.allocate();
CHECK(memory1);
CHECK(memoryLimiter.acquired() > 256);

pool.deallocate(memory0);
pool.deallocate(memory1);
CHECK(memoryLimiter.acquired() == 0);
}
}

TEST(limited_pool, object_size_and_overhead) {
HeapArena arena;
SlabPool<TestObject> slabPool("test", arena);
MemoryLimiter memoryLimiter("test", 256);

{
LimitedPool pool(slabPool, memoryLimiter);

CHECK(pool.object_size() == 128);
CHECK(pool.overhead() > 0);
}
}

} // namespace core
} // namespace roc

0 comments on commit c5ed77d

Please sign in to comment.