File: ctor_and_assignment.hpp

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 (153 lines) | stat: -rw-r--r-- 3,708 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
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2020, Intel Corporation */

#ifndef LIBPMEMOBJ_CPP_TESTS_CTOR_AND_ASSIGNMENT
#define LIBPMEMOBJ_CPP_TESTS_CTOR_AND_ASSIGNMENT

#include "../common/unittest.hpp"

namespace nvobj = pmem::obj;

/**
 * Wrapper around PMDK allocator
 * @throw std::bad_alloc on allocation failure.
 */
template <typename T, typename U, typename... Args>
void
tx_alloc_wrapper(nvobj::pool_base &pop, nvobj::persistent_ptr<U> &ptr,
		 Args &&... args)
{
	try {
		nvobj::transaction::manual tx(pop);
		ptr = nvobj::make_persistent<T>(std::forward<Args>(args)...);
		nvobj::transaction::commit();
	} catch (...) {
		throw std::bad_alloc();
	}
}

template <typename T>
void
verify_elements(T &container, size_t elements)
{
	UT_ASSERT(container.size() == elements);

	for (int i = 0; i < static_cast<int>(elements); i++) {
		UT_ASSERT(container.count(i) == 1);
	}
}

template <typename T>
void
ctor_test(nvobj::pool_base &pop, nvobj::persistent_ptr<T> &container1,
	  nvobj::persistent_ptr<T> &container2)
{
	using V = typename T::value_type;

	tx_alloc_wrapper<T>(pop, container1);
	UT_ASSERT(container1->empty());
	UT_ASSERT(container1->size() == size_t(0));

	for (int i = 0; i < 300; i++) {
		auto ret = container1->insert(V(i, i));
		UT_ASSERT(ret.second == true);
		UT_ASSERT(ret.first->MAP_KEY == i);
		UT_ASSERT(ret.first->MAP_VALUE == i);
	}

	tx_alloc_wrapper<T>(pop, container2, container1->begin(),
			    container1->end());

	UT_ASSERT(!container2->empty());
	UT_ASSERT(container1->size() == container2->size());

	verify_elements<T>(*container2, 300);

	pmem::detail::destroy<T>(*container2);
	tx_alloc_wrapper<T>(pop, container2, *container1);

	UT_ASSERT(container1->size() == container2->size());

	verify_elements<T>(*container2, 300);

	pmem::detail::destroy<T>(*container2);
	tx_alloc_wrapper<T>(pop, container2, std::move(*container1));

	verify_elements<T>(*container2, 300);

	pmem::detail::destroy<T>(*container2);
	tx_alloc_wrapper<T>(pop, container2,
			    std::initializer_list<V>{V(0, 0), V(1, 1)});

	verify_elements<T>(*container2, 2);

	pmem::detail::destroy<T>(*container1);
	pmem::detail::destroy<T>(*container2);
}

template <typename T>
void
assignment_test(nvobj::pool_base &pop, nvobj::persistent_ptr<T> &container1,
		nvobj::persistent_ptr<T> &container2)
{
	using V = typename T::value_type;

	tx_alloc_wrapper<T>(pop, container1);
	tx_alloc_wrapper<T>(pop, container2);

	UT_ASSERT(container1->empty());

	for (int i = 0; i < 50; i++) {
		auto ret = container1->insert(V(i, i));
		UT_ASSERT(ret.second == true);
	}

	verify_elements<T>(*container1, 50);

	for (int i = 0; i < 300; i++) {
		auto ret = container2->insert(V(i, i));
		UT_ASSERT(ret.second == true);
	}

	*container1 = *container2;

	verify_elements<T>(*container1, 300);

	for (int i = 300; i < 350; i++) {
		auto ret = container1->insert(V(i, i));
		UT_ASSERT(ret.second == true);
	}

	verify_elements<T>(*container1, 350);
	verify_elements<T>(*container2, 300);

	container2->clear();

	*container1 = *container2;

	UT_ASSERT(container1->size() == 0);
	UT_ASSERT(std::distance(container1->begin(), container1->end()) == 0);
	UT_ASSERT(container2->size() == 0);
	UT_ASSERT(std::distance(container2->begin(), container2->end()) == 0);

	for (int i = 0; i < 350; i++) {
		UT_ASSERT(container1->count(i) == 0);
		UT_ASSERT(container2->count(i) == 0);
	}

	for (int i = 0; i < 100; i++) {
		auto ret = container1->insert(V(i, i));
		UT_ASSERT(ret.second == true);
	}

	verify_elements<T>(*container1, 100);

	*container2 = std::move(*container1);

	verify_elements<T>(*container2, 100);

	pmem::detail::destroy<T>(*container1);
	pmem::detail::destroy<T>(*container2);
}

#endif