Skip to content
This repository was archived by the owner on Mar 22, 2023. It is now read-only.

Commit 7b8eea2

Browse files
tests: extend and cleanup radix_tree tests
1 parent 0ddba32 commit 7b8eea2

File tree

4 files changed

+328
-156
lines changed

4 files changed

+328
-156
lines changed

tests/radix_tree/radix.hpp

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
// SPDX-License-Identifier: BSD-3-Clause
22
/* Copyright 2020-2021, Intel Corporation */
33

4+
#include <functional>
5+
#include <libpmemobj.h>
6+
#include <random>
7+
48
#include "transaction_helpers.hpp"
59
#include "unittest.hpp"
610

711
#include <libpmemobj++/experimental/inline_string.hpp>
812
#include <libpmemobj++/experimental/radix_tree.hpp>
913

10-
#include <functional>
11-
12-
#include <libpmemobj.h>
14+
static std::mt19937_64 generator;
1315

1416
namespace nvobj = pmem::obj;
1517
namespace nvobjex = pmem::obj::experimental;
@@ -86,6 +88,7 @@ struct root {
8688
nvobj::persistent_ptr<container_inline_s_u8t_mt> radix_inline_s_u8t_mt;
8789
};
8890

91+
/* Helper functions to access key/value of different types */
8992
template <typename Container,
9093
typename Enable = typename std::enable_if<
9194
std::is_same<typename Container::mapped_type,
@@ -108,9 +111,9 @@ value(unsigned v, int repeats = 1)
108111
{
109112
using CharT = typename Container::mapped_type::value_type;
110113

114+
auto str = std::to_string(v);
111115
auto s = std::basic_string<CharT>{};
112116
for (int i = 0; i < repeats; i++) {
113-
auto str = std::to_string(v);
114117
s += std::basic_string<CharT>(str.begin(), str.end());
115118
}
116119

@@ -178,6 +181,7 @@ operator!=(pmem::obj::experimental::basic_inline_string<CharT, Traits> &lhs,
178181
.compare(rhs) != 0;
179182
}
180183

184+
/* verify all elements in container, using lower_/upper_bound and find */
181185
template <typename Container, typename K, typename F>
182186
void
183187
verify_elements(nvobj::persistent_ptr<Container> ptr, unsigned count, K &&key_f,
@@ -216,6 +220,8 @@ verify_elements(nvobj::persistent_ptr<Container> ptr, unsigned count, K &&key_f,
216220
}
217221
}
218222

223+
/* run 1 thread with modifications (erase/write/etc.) and multiple threads with
224+
* reads */
219225
template <typename ModifyF, typename ReadF>
220226
static void
221227
parallel_modify_read(ModifyF modifier, std::vector<ReadF> &readers,
@@ -230,16 +236,30 @@ parallel_modify_read(ModifyF modifier, std::vector<ReadF> &readers,
230236
});
231237
}
232238

239+
/* each test suite should initialize generator at the beginning */
240+
void
241+
init_random()
242+
{
243+
std::random_device rd;
244+
auto seed = rd();
245+
std::cout << "rand seed: " << seed << std::endl;
246+
generator = std::mt19937_64(seed);
247+
}
248+
233249
template <typename Container>
234250
static void
235251
init_container(nvobj::pool<root> &pop, nvobj::persistent_ptr<Container> &ptr,
236-
const size_t initial_elements, const size_t value_repeats = 1)
252+
const size_t initial_elements, const size_t value_repeats = 1,
253+
bool rand_keys = false)
237254
{
238255
nvobj::transaction::run(
239256
pop, [&] { ptr = nvobj::make_persistent<Container>(); });
240257

241258
for (size_t i = 0; i < initial_elements; ++i) {
242-
ptr->emplace(key<Container>(i),
243-
value<Container>(i, value_repeats));
259+
auto k = key<Container>(i);
260+
if (rand_keys) {
261+
k = key<Container>(static_cast<unsigned>(generator()));
262+
}
263+
ptr->emplace(k, value<Container>(i, value_repeats));
244264
}
245265
}

0 commit comments

Comments
 (0)