Skip to content

Commit

Permalink
[UMA] add more tests for allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian Duy committed May 23, 2023
1 parent 6b54e75 commit 1e6efd8
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions test/unified_memory_allocation/memoryPool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
#include <cstring>
#include <functional>
#include <random>
#include <thread>

#ifndef UMA_TEST_MEMORY_POOL_OPS_HPP
#define UMA_TEST_MEMORY_POOL_OPS_HPP

#define NTHREADS 5

struct umaPoolTest : uma_test::test,
::testing::WithParamInterface<
std::function<uma::pool_unique_handle_t(void)>> {
Expand Down Expand Up @@ -73,6 +76,56 @@ TEST_P(umaPoolTest, pow2AlignedAlloc) {
}
}

TEST_P(umaPoolTest, allocZeroSize) {
static constexpr size_t allocSize = 0;
auto *ptr = umaPoolMalloc(pool.get(), allocSize);
umaPoolFree(pool.get(), ptr);
}

TEST_P(umaPoolTest, freeNullptr) {
void *ptr = nullptr;
umaPoolFree(pool.get(), ptr);
ASSERT_EQ(ptr, nullptr);
}

TEST_P(umaPoolTest, allocOutOfMem) {
// test whether memory is kept in a pool accordingly to MaxSize
static constexpr size_t allocSize = 16;
// MaxSize equals 16 * 1024 * 1024;
static constexpr size_t maxAllocSizeMultiplier = 1024 * 1024;

// allocate until oom
auto *ptr = umaPoolMalloc(pool.get(), allocSize);
ASSERT_NE(ptr, nullptr);

for (size_t i = 0; i <= maxAllocSizeMultiplier; ++i) {
ptr = umaPoolMalloc(pool.get(), allocSize);
ASSERT_NE(ptr, nullptr);
}

umaPoolFree(pool.get(), ptr);

ptr = umaPoolMalloc(pool.get(), allocSize);
ASSERT_NE(ptr, nullptr);
}

TEST_P(umaPoolTest, multiThreadedMallocFree) {
auto poolMalloc = [](size_t allocSize, uma_memory_pool_handle_t pool) {
auto *ptr = umaPoolMalloc(pool, allocSize);
ASSERT_NE(ptr, nullptr);
umaPoolFree(pool, ptr);
};

std::vector<std::thread> threads;
for (int i = 0; i < NTHREADS; i++) {
threads.push_back(std::thread(poolMalloc, 64, pool.get()));
}

for (auto &thread : threads) {
thread.join();
}
}

// TODO: add similar tests for realloc/aligned_alloc, etc.
// TODO: add multithreaded tests
TEST_P(umaMultiPoolTest, memoryTracking) {
Expand Down

0 comments on commit 1e6efd8

Please sign in to comment.