Skip to content

Commit b1da922

Browse files
committed
Add tests for double allocation
Add tests for double allocation to: - OS provider - file provider - devdax provider Signed-off-by: Lukasz Dorau <lukasz.dorau@intel.com>
1 parent ad49271 commit b1da922

6 files changed

+102
-105
lines changed

test/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ if(LINUX AND (NOT UMF_DISABLE_HWLOC)) # OS-specific functions are implemented
201201

202202
add_umf_test(
203203
NAME provider_os_memory
204-
SRCS provider_os_memory.cpp
204+
SRCS provider_os_memory.cpp common/test_helpers.cpp
205205
LIBS ${UMF_UTILS_FOR_TEST})
206206
if(UMF_BUILD_LIBUMF_POOL_DISJOINT)
207207
target_compile_definitions(umf_test-provider_os_memory
@@ -251,11 +251,11 @@ if(LINUX AND (NOT UMF_DISABLE_HWLOC)) # OS-specific functions are implemented
251251
LIBS ${LIBNUMA_LIBRARIES} ${LIBHWLOC_LIBRARIES})
252252
add_umf_test(
253253
NAME provider_devdax_memory
254-
SRCS provider_devdax_memory.cpp
254+
SRCS provider_devdax_memory.cpp common/test_helpers.cpp
255255
LIBS ${UMF_UTILS_FOR_TEST})
256256
add_umf_test(
257257
NAME provider_file_memory
258-
SRCS provider_file_memory.cpp
258+
SRCS provider_file_memory.cpp common/test_helpers.cpp
259259
LIBS ${UMF_UTILS_FOR_TEST})
260260
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND UMF_BUILD_FUZZTESTS)
261261
add_subdirectory(fuzz)

test/common/test_helpers.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (C) 2024 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+
// This file contains tests for UMF pool API
5+
6+
#include "test_helpers.hpp"
7+
#include "base.hpp"
8+
#include "umf/memory_provider.h"
9+
10+
static void
11+
test_alloc_success_with_result(umf_memory_provider_handle_t provider,
12+
size_t size, size_t alignment, purge_t purge,
13+
umf_result_t expected_result) {
14+
umf_result_t umf_result;
15+
void *ptr = nullptr;
16+
void *ptr2 = nullptr;
17+
18+
umf_result = umfMemoryProviderAlloc(provider, size, alignment, &ptr);
19+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
20+
ASSERT_NE(ptr, nullptr);
21+
22+
umf_result = umfMemoryProviderAlloc(provider, size, alignment, &ptr2);
23+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
24+
ASSERT_NE(ptr, nullptr);
25+
26+
ASSERT_NE(ptr, ptr2);
27+
28+
memset(ptr, 0xFF, size);
29+
memset(ptr2, 0xFF, size);
30+
31+
if (purge == PURGE_LAZY) {
32+
umf_result = umfMemoryProviderPurgeLazy(provider, ptr, size);
33+
ASSERT_EQ(umf_result, expected_result);
34+
} else if (purge == PURGE_FORCE) {
35+
umf_result = umfMemoryProviderPurgeForce(provider, ptr, size);
36+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
37+
}
38+
39+
umf_result = umfMemoryProviderFree(provider, ptr, size);
40+
ASSERT_EQ(umf_result, expected_result);
41+
42+
umf_result = umfMemoryProviderFree(provider, ptr2, size);
43+
ASSERT_EQ(umf_result, expected_result);
44+
}
45+
46+
void test_alloc_free_success(umf_memory_provider_handle_t provider, size_t size,
47+
size_t alignment, purge_t purge) {
48+
test_alloc_success_with_result(provider, size, alignment, purge,
49+
UMF_RESULT_SUCCESS);
50+
}
51+
52+
// test used only in providers that do not support the free() operation (file and devdax provider)
53+
void test_alloc_success_not_free(umf_memory_provider_handle_t provider,
54+
size_t size, size_t alignment, purge_t purge) {
55+
test_alloc_success_with_result(provider, size, alignment, purge,
56+
UMF_RESULT_ERROR_NOT_SUPPORTED);
57+
}

test/common/test_helpers.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (C) 2023-2024 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+
// This file contains helpers for tests for UMF pool API
5+
6+
#ifndef UMF_TEST_HELPERS_HPP
7+
#define UMF_TEST_HELPERS_HPP 1
8+
9+
#include <umf/base.h>
10+
#include <umf/memory_pool.h>
11+
#include <umf/memory_provider_ops.h>
12+
13+
typedef enum purge_t {
14+
PURGE_NONE = 0,
15+
PURGE_LAZY = 1,
16+
PURGE_FORCE = 2,
17+
} purge_t;
18+
19+
void test_alloc_free_success(umf_memory_provider_handle_t provider, size_t size,
20+
size_t alignment, purge_t purge);
21+
22+
// test used only in providers that do not support the free() operation (file and devdax provider)
23+
void test_alloc_success_not_free(umf_memory_provider_handle_t provider,
24+
size_t size, size_t alignment, purge_t purge);
25+
26+
#endif /* UMF_TEST_HELPERS_HPP */

test/provider_devdax_memory.cpp

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "cpp_helpers.hpp"
1515
#include "test_helpers.h"
16+
#include "test_helpers.hpp"
1617

1718
#include <umf/memory_provider.h>
1819
#include <umf/providers/provider_devdax_memory.h>
@@ -21,12 +22,6 @@ using umf_test::test;
2122

2223
#define INVALID_PTR ((void *)0x01)
2324

24-
typedef enum purge_t {
25-
PURGE_NONE = 0,
26-
PURGE_LAZY = 1,
27-
PURGE_FORCE = 2,
28-
} purge_t;
29-
3025
static const char *Native_error_str[] = {
3126
"success", // UMF_DEVDAX_RESULT_SUCCESS
3227
"memory allocation failed", // UMF_DEVDAX_RESULT_ERROR_ALLOC_FAILED
@@ -89,30 +84,6 @@ struct umfProviderTest
8984
size_t page_plus_64;
9085
};
9186

92-
static void test_alloc_free_success(umf_memory_provider_handle_t provider,
93-
size_t size, size_t alignment,
94-
purge_t purge) {
95-
void *ptr = nullptr;
96-
97-
umf_result_t umf_result =
98-
umfMemoryProviderAlloc(provider, size, alignment, &ptr);
99-
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
100-
ASSERT_NE(ptr, nullptr);
101-
102-
memset(ptr, 0xFF, size);
103-
104-
if (purge == PURGE_LAZY) {
105-
umf_result = umfMemoryProviderPurgeLazy(provider, ptr, size);
106-
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_NOT_SUPPORTED);
107-
} else if (purge == PURGE_FORCE) {
108-
umf_result = umfMemoryProviderPurgeForce(provider, ptr, size);
109-
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
110-
}
111-
112-
umf_result = umfMemoryProviderFree(provider, ptr, size);
113-
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_NOT_SUPPORTED);
114-
}
115-
11687
static void verify_last_native_error(umf_memory_provider_handle_t provider,
11788
int32_t err) {
11889
const char *message;
@@ -177,7 +148,7 @@ TEST_F(test, test_if_mapped_with_MAP_SYNC) {
177148
ASSERT_EQ(flag_found, true);
178149
}
179150

180-
// positive tests using test_alloc_free_success
151+
// positive tests using test_alloc_success_not_free
181152

182153
auto defaultParams = umfDevDaxMemoryProviderParamsDefault(
183154
getenv("UMF_TESTS_DEVDAX_PATH"),
@@ -191,20 +162,20 @@ INSTANTIATE_TEST_SUITE_P(devdaxProviderTest, umfProviderTest,
191162
TEST_P(umfProviderTest, create_destroy) {}
192163

193164
TEST_P(umfProviderTest, alloc_page64_align_0) {
194-
test_alloc_free_success(provider.get(), page_plus_64, 0, PURGE_NONE);
165+
test_alloc_success_not_free(provider.get(), page_plus_64, 0, PURGE_NONE);
195166
}
196167

197168
TEST_P(umfProviderTest, alloc_page64_align_page_div_2) {
198-
test_alloc_free_success(provider.get(), page_plus_64, page_size / 2,
199-
PURGE_NONE);
169+
test_alloc_success_not_free(provider.get(), page_plus_64, page_size / 2,
170+
PURGE_NONE);
200171
}
201172

202173
TEST_P(umfProviderTest, purge_lazy) {
203-
test_alloc_free_success(provider.get(), page_plus_64, 0, PURGE_LAZY);
174+
test_alloc_success_not_free(provider.get(), page_plus_64, 0, PURGE_LAZY);
204175
}
205176

206177
TEST_P(umfProviderTest, purge_force) {
207-
test_alloc_free_success(provider.get(), page_plus_64, 0, PURGE_FORCE);
178+
test_alloc_success_not_free(provider.get(), page_plus_64, 0, PURGE_FORCE);
208179
}
209180

210181
// negative tests using test_alloc_failure

test/provider_file_memory.cpp

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include "cpp_helpers.hpp"
88
#include "test_helpers.h"
9+
#include "test_helpers.hpp"
10+
911
#ifndef _WIN32
1012
#include "test_helpers_linux.h"
1113
#endif
@@ -18,12 +20,6 @@ using umf_test::test;
1820
#define FILE_PATH ((char *)"tmp_file")
1921
#define INVALID_PTR ((void *)0x01)
2022

21-
typedef enum purge_t {
22-
PURGE_NONE = 0,
23-
PURGE_LAZY = 1,
24-
PURGE_FORCE = 2,
25-
} purge_t;
26-
2723
static const char *Native_error_str[] = {
2824
"success", // UMF_FILE_RESULT_SUCCESS
2925
"memory allocation failed", // UMF_FILE_RESULT_ERROR_ALLOC_FAILED
@@ -77,30 +73,6 @@ struct FileProviderParamsDefault
7773

7874
struct FileProviderParamsShared : FileProviderParamsDefault {};
7975

80-
static void test_alloc_free_success(umf_memory_provider_handle_t provider,
81-
size_t size, size_t alignment,
82-
purge_t purge) {
83-
void *ptr = nullptr;
84-
85-
umf_result_t umf_result =
86-
umfMemoryProviderAlloc(provider, size, alignment, &ptr);
87-
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
88-
ASSERT_NE(ptr, nullptr);
89-
90-
memset(ptr, 0xFF, size);
91-
92-
if (purge == PURGE_LAZY) {
93-
umf_result = umfMemoryProviderPurgeLazy(provider, ptr, size);
94-
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_NOT_SUPPORTED);
95-
} else if (purge == PURGE_FORCE) {
96-
umf_result = umfMemoryProviderPurgeForce(provider, ptr, size);
97-
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
98-
}
99-
100-
umf_result = umfMemoryProviderFree(provider, ptr, size);
101-
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_NOT_SUPPORTED);
102-
}
103-
10476
static void verify_last_native_error(umf_memory_provider_handle_t provider,
10577
int32_t err) {
10678
const char *message;
@@ -161,7 +133,7 @@ TEST_F(test, test_if_mapped_with_MAP_SYNC) {
161133
ASSERT_EQ(flag_found, true);
162134
}
163135

164-
// positive tests using test_alloc_free_success
136+
// positive tests using test_alloc_success_not_free
165137

166138
umf_file_memory_provider_params_t file_params_default =
167139
umfFileMemoryProviderParamsDefault(FILE_PATH);
@@ -184,20 +156,20 @@ INSTANTIATE_TEST_SUITE_P(fileProviderTest, FileProviderParamsDefault,
184156
TEST_P(FileProviderParamsDefault, create_destroy) {}
185157

186158
TEST_P(FileProviderParamsDefault, alloc_page64_align_0) {
187-
test_alloc_free_success(provider.get(), page_plus_64, 0, PURGE_NONE);
159+
test_alloc_success_not_free(provider.get(), page_plus_64, 0, PURGE_NONE);
188160
}
189161

190162
TEST_P(FileProviderParamsDefault, alloc_page64_align_page_div_2) {
191-
test_alloc_free_success(provider.get(), page_plus_64, page_size / 2,
192-
PURGE_NONE);
163+
test_alloc_success_not_free(provider.get(), page_plus_64, page_size / 2,
164+
PURGE_NONE);
193165
}
194166

195167
TEST_P(FileProviderParamsDefault, purge_lazy) {
196-
test_alloc_free_success(provider.get(), page_plus_64, 0, PURGE_LAZY);
168+
test_alloc_success_not_free(provider.get(), page_plus_64, 0, PURGE_LAZY);
197169
}
198170

199171
TEST_P(FileProviderParamsDefault, purge_force) {
200-
test_alloc_free_success(provider.get(), page_plus_64, 0, PURGE_FORCE);
172+
test_alloc_success_not_free(provider.get(), page_plus_64, 0, PURGE_FORCE);
201173
}
202174

203175
// negative tests using test_alloc_failure

test/provider_os_memory.cpp

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "cpp_helpers.hpp"
88
#include "ipcFixtures.hpp"
99
#include "test_helpers.h"
10+
#include "test_helpers.hpp"
1011

1112
#include <umf/memory_provider.h>
1213
#include <umf/pools/pool_disjoint.h>
@@ -16,12 +17,6 @@ using umf_test::test;
1617

1718
#define INVALID_PTR ((void *)0x01)
1819

19-
typedef enum purge_t {
20-
PURGE_NONE = 0,
21-
PURGE_LAZY = 1,
22-
PURGE_FORCE = 2,
23-
} purge_t;
24-
2520
static const char *Native_error_str[] = {
2621
"success", // UMF_OS_RESULT_SUCCESS
2722
"memory allocation failed", // UMF_OS_RESULT_ERROR_ALLOC_FAILED
@@ -77,30 +72,6 @@ struct umfProviderTest
7772
size_t page_plus_64;
7873
};
7974

80-
static void test_alloc_free_success(umf_memory_provider_handle_t provider,
81-
size_t size, size_t alignment,
82-
purge_t purge) {
83-
void *ptr = nullptr;
84-
85-
umf_result_t umf_result =
86-
umfMemoryProviderAlloc(provider, size, alignment, &ptr);
87-
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
88-
ASSERT_NE(ptr, nullptr);
89-
90-
memset(ptr, 0xFF, size);
91-
92-
if (purge == PURGE_LAZY) {
93-
umf_result = umfMemoryProviderPurgeLazy(provider, ptr, size);
94-
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
95-
} else if (purge == PURGE_FORCE) {
96-
umf_result = umfMemoryProviderPurgeForce(provider, ptr, size);
97-
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
98-
}
99-
100-
umf_result = umfMemoryProviderFree(provider, ptr, size);
101-
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
102-
}
103-
10475
static void verify_last_native_error(umf_memory_provider_handle_t provider,
10576
int32_t err) {
10677
const char *message;

0 commit comments

Comments
 (0)