Skip to content

Commit 2572937

Browse files
authored
Merge pull request #208 from igchor/cpp_test_ba
Reimplement base_alloc test in cpp
2 parents 4c95ea8 + 5ee1864 commit 2572937

File tree

5 files changed

+124
-163
lines changed

5 files changed

+124
-163
lines changed

test/CMakeLists.txt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,14 @@ if(UMF_BUILD_OS_MEMORY_PROVIDER AND LINUX) # OS-specific functions are implement
114114
LIBS umf_utils numa)
115115
endif()
116116

117-
# TODO: add Windows tests
118-
if(LINUX)
119-
# the base_alloc test uses linux pthreads
120-
add_umf_test(NAME base_alloc
121-
SRCS ${BA_SOURCES} test_base_alloc.c
122-
LIBS umf_utils)
123-
add_umf_test(NAME base_alloc_linear
124-
SRCS ${BA_SOURCES} test_base_alloc_linear.c
125-
LIBS umf_utils)
117+
if(UMF_BUILD_SHARED_LIBRARY)
118+
# if build as shared library, ba symbols won't be visible in tests
119+
set(BA_SOURCES_FOR_TEST ${BA_SOURCES})
126120
endif()
121+
122+
add_umf_test(NAME base_alloc
123+
SRCS ${BA_SOURCES_FOR_TEST} test_base_alloc.cpp
124+
LIBS umf_utils)
125+
add_umf_test(NAME base_alloc_linear
126+
SRCS ${BA_SOURCES_FOR_TEST} test_base_alloc_linear.cpp
127+
LIBS umf_utils)

test/test_base_alloc.c

Lines changed: 0 additions & 73 deletions
This file was deleted.

test/test_base_alloc.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (C) 2024 Intel Corporation
3+
*
4+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
5+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
*/
7+
8+
#include <cstdio>
9+
#include <cstdlib>
10+
#include <thread>
11+
12+
#include "base_alloc.h"
13+
14+
#include "base.hpp"
15+
#include "test_helpers.h"
16+
17+
using umf_test::test;
18+
19+
TEST_F(test, baseAllocMultiThreadedAllocMemset) {
20+
static constexpr int NTHREADS = 10;
21+
static constexpr int ITERATIONS = 1000;
22+
static constexpr int ALLOCATION_SIZE = 16;
23+
24+
auto pool = std::shared_ptr<umf_ba_pool_t>(umf_ba_create(ALLOCATION_SIZE),
25+
umf_ba_destroy);
26+
27+
auto poolAlloc = [](int TID, umf_ba_pool_t *pool) {
28+
std::vector<std::shared_ptr<unsigned char>> ptrs;
29+
30+
for (int i = 0; i < ITERATIONS; i++) {
31+
ptrs.emplace_back(
32+
(unsigned char *)umf_ba_alloc(pool),
33+
[pool = pool](unsigned char *ptr) { umf_ba_free(pool, ptr); });
34+
35+
memset(ptrs.back().get(), (i + TID) & 0xFF, ALLOCATION_SIZE);
36+
}
37+
38+
for (int i = 0; i < ITERATIONS; i++) {
39+
for (int k = 0; k < ALLOCATION_SIZE; k++) {
40+
UT_ASSERTeq(*(ptrs[i].get() + k), ((i + TID) & 0xFF));
41+
}
42+
}
43+
};
44+
45+
std::vector<std::thread> threads;
46+
for (int i = 0; i < NTHREADS; i++) {
47+
threads.emplace_back(poolAlloc, i, pool.get());
48+
}
49+
50+
for (auto &thread : threads) {
51+
thread.join();
52+
}
53+
}

test/test_base_alloc_linear.c

Lines changed: 0 additions & 81 deletions
This file was deleted.

test/test_base_alloc_linear.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (C) 2024 Intel Corporation
3+
*
4+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
5+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
*/
7+
8+
#include <cstdio>
9+
#include <cstdlib>
10+
#include <thread>
11+
12+
#include "base_alloc_linear.h"
13+
14+
#include "base.hpp"
15+
#include "test_helpers.h"
16+
17+
using umf_test::test;
18+
19+
TEST_F(test, baseAllocLinearMultiThreadedAllocMemset) {
20+
static constexpr int NTHREADS = 10;
21+
static constexpr int ITERATIONS = 1000;
22+
static constexpr int MAX_ALLOCATION_SIZE = 1024;
23+
24+
srand(0);
25+
26+
auto pool = std::shared_ptr<umf_ba_linear_pool_t>(
27+
umf_ba_linear_create(NTHREADS * ITERATIONS * MAX_ALLOCATION_SIZE),
28+
umf_ba_linear_destroy);
29+
30+
auto poolAlloc = [](int TID, umf_ba_linear_pool_t *pool) {
31+
struct buffer_t {
32+
unsigned char *ptr;
33+
size_t size;
34+
} buffer[ITERATIONS];
35+
36+
for (int i = 0; i < ITERATIONS; i++) {
37+
buffer[i].size =
38+
(size_t)((rand() / (double)RAND_MAX) * MAX_ALLOCATION_SIZE);
39+
buffer[i].ptr =
40+
(unsigned char *)umf_ba_linear_alloc(pool, buffer[i].size);
41+
UT_ASSERTne(buffer[i].ptr, NULL);
42+
memset(buffer[i].ptr, (i + TID) & 0xFF, buffer[i].size);
43+
}
44+
45+
for (int i = 0; i < ITERATIONS; i++) {
46+
UT_ASSERTne(buffer[i].ptr, NULL);
47+
for (size_t k = 0; k < buffer[i].size; k++) {
48+
UT_ASSERTeq(*(buffer[i].ptr + k), (i + TID) & 0xFF);
49+
}
50+
}
51+
};
52+
53+
std::vector<std::thread> threads;
54+
for (int i = 0; i < NTHREADS; i++) {
55+
threads.emplace_back(poolAlloc, i, pool.get());
56+
}
57+
58+
for (auto &thread : threads) {
59+
thread.join();
60+
}
61+
}

0 commit comments

Comments
 (0)