File: map_find_lower_lower_eq.cpp

package info (click to toggle)
libpmemobj-cpp 1.12-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,388 kB
  • sloc: cpp: 136,076; sh: 1,022; perl: 381; ansic: 163; makefile: 13
file content (326 lines) | stat: -rw-r--r-- 8,635 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2020, Intel Corporation */

#include "../common/map_wrapper.hpp"
#include "../common/unittest.hpp"
#include "../container_generic/container_txabort.hpp"
#include "../external/libcxx/map/is_transparent.h"
#include "../external/libcxx/map/private_constructor.h"

#define LAYOUT "layout"

namespace nvobj = pmem::obj;

using map_type1 = container_t<int, nvobj::p<int>>;
using map_type2 =
	container_t<PrivateConstructor, nvobj::p<int>, transparent_less>;

const size_t TEST_ELEMENTS = 1024;

struct root {
	nvobj::persistent_ptr<map_type1> pptr1;
	nvobj::persistent_ptr<map_type2> pptr2;
};

/* Test lower() overloads */
template <bool IsConst>
void
test_find_lower(nvobj::pool<root> &pop)
{
	auto r = pop.root();

	{
		using M = typename std::conditional<IsConst, const map_type1 &,
						    map_type1 &>::type;
		using It = typename std::conditional<IsConst,
						     map_type1::const_iterator,
						     map_type1::iterator>::type;

		nvobj::transaction::run(pop, [&] {
			using V = typename map_type1::value_type;
			V ar[] = {V(1, 1), V(2, 2), V(3, 3), V(4, 4),
				  V(6, 6), V(8, 8), V(9, 9)};
			r->pptr1 = nvobj::make_persistent<map_type1>(
				ar, ar + sizeof(ar) / sizeof(ar[0]));
		});

		It it;
		M m = *(r->pptr1);

		it = m.find_lower(0);
		UT_ASSERT(it == m.end());
		it = m.find_lower(1);
		UT_ASSERT(it == m.end());
		it = m.find_lower(2);
		UT_ASSERT(it == m.begin());
		it = m.find_lower(3);
		UT_ASSERT(it == std::next(m.begin(), 1));
		it = m.find_lower(4);
		UT_ASSERT(it == std::next(m.begin(), 2));
		it = m.find_lower(5);
		UT_ASSERT(it == std::next(m.begin(), 3));
		it = m.find_lower(6);
		UT_ASSERT(it == std::next(m.begin(), 3));
		it = m.find_lower(7);
		UT_ASSERT(it == std::next(m.begin(), 4));
		it = m.find_lower(8);
		UT_ASSERT(it == std::next(m.begin(), 4));
		it = m.find_lower(9);
		UT_ASSERT(it == std::next(m.begin(), 5));
		it = m.find_lower(10);
		UT_ASSERT(it == std::next(m.begin(), 6));

		nvobj::transaction::run(pop, [&] {
			nvobj::delete_persistent<map_type1>(r->pptr1);
		});
	}

	{
		using M = typename std::conditional<IsConst, const map_type2 &,
						    map_type2 &>::type;
		using It = typename std::conditional<IsConst,
						     map_type2::const_iterator,
						     map_type2::iterator>::type;

		nvobj::transaction::run(pop, [&] {
			r->pptr2 = nvobj::make_persistent<map_type2>();
		});

		r->pptr2->insert({PrivateConstructor::make(1), 1});
		r->pptr2->insert({PrivateConstructor::make(2), 2});
		r->pptr2->insert({PrivateConstructor::make(3), 3});
		r->pptr2->insert({PrivateConstructor::make(4), 4});
		r->pptr2->insert({PrivateConstructor::make(6), 6});
		r->pptr2->insert({PrivateConstructor::make(8), 8});
		r->pptr2->insert({PrivateConstructor::make(9), 9});

		It it;
		M m = *(r->pptr2);

		it = m.find_lower(0);
		UT_ASSERT(it == m.end());
		it = m.find_lower(1);
		UT_ASSERT(it == m.end());
		it = m.find_lower(2);
		UT_ASSERT(it == m.begin());
		it = m.find_lower(3);
		UT_ASSERT(it == std::next(m.begin(), 1));
		it = m.find_lower(4);
		UT_ASSERT(it == std::next(m.begin(), 2));
		it = m.find_lower(5);
		UT_ASSERT(it == std::next(m.begin(), 3));
		it = m.find_lower(6);
		UT_ASSERT(it == std::next(m.begin(), 3));
		it = m.find_lower(7);
		UT_ASSERT(it == std::next(m.begin(), 4));
		it = m.find_lower(8);
		UT_ASSERT(it == std::next(m.begin(), 4));
		it = m.find_lower(9);
		UT_ASSERT(it == std::next(m.begin(), 5));
		it = m.find_lower(10);
		UT_ASSERT(it == std::next(m.begin(), 6));

		nvobj::transaction::run(pop, [&] {
			nvobj::delete_persistent<map_type2>(r->pptr2);
		});
	}
}

/* Test lower_eq() overloads */
template <bool IsConst>
void
test_find_lower_eq(nvobj::pool<root> &pop)
{
	auto r = pop.root();

	{
		using M = typename std::conditional<IsConst, const map_type1 &,
						    map_type1 &>::type;
		using It = typename std::conditional<IsConst,
						     map_type1::const_iterator,
						     map_type1::iterator>::type;

		nvobj::transaction::run(pop, [&] {
			using V = typename map_type1::value_type;
			V ar[] = {V(1, 1), V(2, 2), V(3, 3), V(4, 4),
				  V(6, 6), V(8, 8), V(9, 9)};
			r->pptr1 = nvobj::make_persistent<map_type1>(
				ar, ar + sizeof(ar) / sizeof(ar[0]));
		});

		It it;
		M m = *(r->pptr1);

		it = m.find_lower_eq(0);
		UT_ASSERT(it == m.end());
		it = m.find_lower_eq(1);
		UT_ASSERT(it == m.begin());
		it = m.find_lower_eq(2);
		UT_ASSERT(it == std::next(m.begin(), 1));
		it = m.find_lower_eq(3);
		UT_ASSERT(it == std::next(m.begin(), 2));
		it = m.find_lower_eq(4);
		UT_ASSERT(it == std::next(m.begin(), 3));
		it = m.find_lower_eq(5);
		UT_ASSERT(it == std::next(m.begin(), 3));
		it = m.find_lower_eq(6);
		UT_ASSERT(it == std::next(m.begin(), 4));
		it = m.find_lower_eq(7);
		UT_ASSERT(it == std::next(m.begin(), 4));
		it = m.find_lower_eq(8);
		UT_ASSERT(it == std::next(m.begin(), 5));
		it = m.find_lower_eq(9);
		UT_ASSERT(it == std::next(m.begin(), 6));
		it = m.find_lower_eq(10);
		UT_ASSERT(it == std::next(m.begin(), 6));

		nvobj::transaction::run(pop, [&] {
			nvobj::delete_persistent<map_type1>(r->pptr1);
		});
	}

	{
		using M = typename std::conditional<IsConst, const map_type2 &,
						    map_type2 &>::type;
		using It = typename std::conditional<IsConst,
						     map_type2::const_iterator,
						     map_type2::iterator>::type;

		nvobj::transaction::run(pop, [&] {
			r->pptr2 = nvobj::make_persistent<map_type2>();
		});

		r->pptr2->insert({PrivateConstructor::make(1), 1});
		r->pptr2->insert({PrivateConstructor::make(2), 2});
		r->pptr2->insert({PrivateConstructor::make(3), 3});
		r->pptr2->insert({PrivateConstructor::make(4), 4});
		r->pptr2->insert({PrivateConstructor::make(6), 6});
		r->pptr2->insert({PrivateConstructor::make(8), 8});
		r->pptr2->insert({PrivateConstructor::make(9), 9});

		It it;
		M m = *(r->pptr2);

		it = m.find_lower_eq(0);
		UT_ASSERT(it == m.end());
		it = m.find_lower_eq(1);
		UT_ASSERT(it == m.begin());
		it = m.find_lower_eq(2);
		UT_ASSERT(it == std::next(m.begin(), 1));
		it = m.find_lower_eq(3);
		UT_ASSERT(it == std::next(m.begin(), 2));
		it = m.find_lower_eq(4);
		UT_ASSERT(it == std::next(m.begin(), 3));
		it = m.find_lower_eq(5);
		UT_ASSERT(it == std::next(m.begin(), 3));
		it = m.find_lower_eq(6);
		UT_ASSERT(it == std::next(m.begin(), 4));
		it = m.find_lower_eq(7);
		UT_ASSERT(it == std::next(m.begin(), 4));
		it = m.find_lower_eq(8);
		UT_ASSERT(it == std::next(m.begin(), 5));
		it = m.find_lower_eq(9);
		UT_ASSERT(it == std::next(m.begin(), 6));
		it = m.find_lower_eq(10);
		UT_ASSERT(it == std::next(m.begin(), 6));

		nvobj::transaction::run(pop, [&] {
			nvobj::delete_persistent<map_type2>(r->pptr2);
		});
	}
}

/* Verify lower, lower_eq, higher, higher_eq, lower_bound and upper_bound
 * properties. */
template <bool IsConst>
void
test_properties(nvobj::pool<root> &pop)
{
	auto r = pop.root();

	{
		using M = typename std::conditional<IsConst, const map_type1 &,
						    map_type1 &>::type;
		using It = typename std::conditional<IsConst,
						     map_type1::const_iterator,
						     map_type1::iterator>::type;

		nvobj::transaction::run(pop, [&] {
			r->pptr1 = nvobj::make_persistent<map_type1>();
		});

		It it;
		M m = *(r->pptr1);

		for (size_t i = 0; i < TEST_ELEMENTS; i++)
			r->pptr1->insert({3 * i, 3 * i});

		for (size_t i = 2; i < TEST_ELEMENTS * 3; i++) {
			auto lo = m.find_lower(i);
			auto lq = m.find_lower_eq(i);
			auto lb = m.lower_bound(i);
			auto ub = m.upper_bound(i);
			auto hi = m.find_higher(i);
			auto he = m.find_higher_eq(i);

			UT_ASSERT(he == lb);
			UT_ASSERT(hi == ub);

			if (ub != m.end()) {
				UT_ASSERT(lo->first < lb->first);
				UT_ASSERT(lq->first < ub->first);

				if (lb == ub)
					UT_ASSERT(lq->first == lo->first);
				else
					UT_ASSERT(lq->first > lo->first);
			}

			++lo;
			UT_ASSERT(lo == lb);

			++lq;
			UT_ASSERT(lq == ub);
		}

		nvobj::transaction::run(pop, [&] {
			nvobj::delete_persistent<map_type1>(r->pptr1);
		});
	}
}

static void
test(int argc, char *argv[])
{
	if (argc < 2) {
		UT_FATAL("usage: %s file-name", argv[0]);
	}

	const char *path = argv[1];

	nvobj::pool<root> pop;

	try {
		pop = nvobj::pool<root>::create(
			path, LAYOUT, PMEMOBJ_MIN_POOL * 20, S_IWUSR | S_IRUSR);
	} catch (pmem::pool_error &pe) {
		UT_FATAL("!pool::create: %s %s", pe.what(), path);
	}

	test_find_lower<true>(pop);
	test_find_lower<false>(pop);

	test_find_lower_eq<true>(pop);
	test_find_lower_eq<false>(pop);

	test_properties<true>(pop);
	test_properties<false>(pop);

	pop.close();
}

int
main(int argc, char *argv[])
{
	return run_test([&] { test(argc, argv); });
}