Skip to content

Commit 528e96d

Browse files
committed
Add tests for umfPoolCreateEx
1 parent 50fe758 commit 528e96d

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ add_umf_test(NAME memoryPool SRCS memoryPoolAPI.cpp)
5959
add_umf_test(NAME memoryProvider SRCS memoryProviderAPI.cpp)
6060
add_umf_test(NAME disjointPool SRCS disjoint_pool.cpp)
6161
add_umf_test(NAME c_api_disjoint_pool SRCS c_api/disjoint_pool.c)
62+
add_umf_test(NAME memory_pool_internal SRCS memory_pool_internal.cpp)
6263

6364
if(LINUX) # OS-specific functions are implemented only for Linux now
6465
add_umf_test(NAME provider_os_memory SRCS provider_os_memory.cpp LIBS numa)

test/memory_pool_internal.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright (C) 2023 Intel Corporation
2+
// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
3+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
#include "../src/memory_pool_internal.h"
6+
#include "base.hpp"
7+
#include "common/provider_null.h"
8+
#include "pool.hpp"
9+
#include "provider_trace.h"
10+
#include "test_helpers.h"
11+
12+
#include <unordered_map>
13+
14+
using umf_test::test;
15+
16+
template <typename T> umf_memory_pool_ops_t poolNoParamsMakeCOps() {
17+
umf_memory_pool_ops_t ops = umf::detail::poolOpsBase<T>();
18+
19+
ops.initialize = [](umf_memory_provider_handle_t provider, void *params,
20+
void **obj) {
21+
(void)params;
22+
try {
23+
*obj = new T;
24+
} catch (...) {
25+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
26+
}
27+
28+
return umf::detail::initialize<T>(reinterpret_cast<T *>(*obj),
29+
std::make_tuple(provider));
30+
};
31+
32+
return ops;
33+
}
34+
35+
umf_memory_pool_ops_t PROXY_POOL_OPS =
36+
poolNoParamsMakeCOps<umf_test::proxy_pool>();
37+
38+
TEST_F(test, poolCreateExSuccess) {
39+
umf_memory_pool_handle_t pool = nullptr;
40+
auto ret = umfPoolCreateEx(&PROXY_POOL_OPS, nullptr, &UMF_NULL_PROVIDER_OPS,
41+
nullptr, &pool);
42+
ASSERT_EQ(ret, UMF_RESULT_SUCCESS);
43+
ASSERT_NE(pool, nullptr);
44+
45+
umfPoolDestroy(pool);
46+
}
47+
48+
TEST_F(test, poolCreateExNullPoolOps) {
49+
umf_memory_pool_handle_t pool = nullptr;
50+
auto ret = umfPoolCreateEx(nullptr, nullptr, &UMF_NULL_PROVIDER_OPS,
51+
nullptr, &pool);
52+
ASSERT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT);
53+
}
54+
55+
TEST_F(test, poolCreateExNullProviderOps) {
56+
umf_memory_pool_handle_t pool = nullptr;
57+
auto ret =
58+
umfPoolCreateEx(&PROXY_POOL_OPS, nullptr, nullptr, nullptr, &pool);
59+
ASSERT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT);
60+
}
61+
62+
TEST_F(test, poolCreateExNullPoolHandle) {
63+
auto ret = umfPoolCreateEx(&PROXY_POOL_OPS, nullptr, &UMF_NULL_PROVIDER_OPS,
64+
nullptr, nullptr);
65+
ASSERT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT);
66+
}
67+
68+
TEST_F(test, poolCreateExCountProviderCalls) {
69+
auto nullProvider = umf_test::wrapProviderUnique(nullProviderCreate());
70+
71+
static std::unordered_map<std::string, size_t> providerCalls;
72+
auto traceCb = [](const char *name) { providerCalls[name]++; };
73+
74+
umf_provider_trace_params_t provider_params = {nullProvider.get(), traceCb};
75+
76+
umf_memory_pool_handle_t pool = nullptr;
77+
umf_result_t ret =
78+
umfPoolCreateEx(&PROXY_POOL_OPS, nullptr, &UMF_TRACE_PROVIDER_OPS,
79+
&provider_params, &pool);
80+
ASSERT_EQ(ret, UMF_RESULT_SUCCESS);
81+
ASSERT_NE(pool, nullptr);
82+
83+
size_t provider_call_count = 0;
84+
85+
umfPoolMalloc(pool, 0);
86+
ASSERT_EQ(providerCalls["alloc"], 1);
87+
ASSERT_EQ(providerCalls.size(), ++provider_call_count);
88+
89+
umfPoolFree(pool, 0);
90+
ASSERT_EQ(providerCalls["free"], 1);
91+
ASSERT_EQ(providerCalls.size(), ++provider_call_count);
92+
93+
umfPoolCalloc(pool, 0, 0);
94+
ASSERT_EQ(providerCalls["alloc"], 2);
95+
ASSERT_EQ(providerCalls.size(), provider_call_count);
96+
97+
umfPoolAlignedMalloc(pool, 0, 0);
98+
ASSERT_EQ(providerCalls["alloc"], 3);
99+
ASSERT_EQ(providerCalls.size(), provider_call_count);
100+
101+
umfPoolDestroy(pool);
102+
}

0 commit comments

Comments
 (0)