File: array_slice_pmreorder.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 (135 lines) | stat: -rw-r--r-- 2,392 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
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2018-2020, Intel Corporation */

/*
 * array_slice_pmreorder.cpp -- pmem::obj::array test under
 * pmreorder
 *
 */

#include "unittest.hpp"

#include <libpmemobj++/container/array.hpp>
#include <libpmemobj++/make_persistent.hpp>
#include <libpmemobj++/p.hpp>
#include <libpmemobj++/persistent_ptr.hpp>
#include <libpmemobj++/pool.hpp>
#include <libpmemobj++/transaction.hpp>

#include <iostream>

#define LAYOUT "pmreorder"

namespace nvobj = pmem::obj;

struct Data {
	void
	increase_elements()
	{
		auto slice = array.range(0, array.size());

		for (auto &e : slice) {
			e++;
		}
	}

	nvobj::array<int, 5> array = {{1, 2, 3, 4, 5}};
};

struct root {
	nvobj::persistent_ptr<Data> ptr;
};

void
init(nvobj::pool<root> &pop)
{
	auto r = pop.root();

	try {
		nvobj::transaction::run(
			pop, [&] { r->ptr = nvobj::make_persistent<Data>(); });
	} catch (...) {
		UT_ASSERT(0);
	}
}

void
run_consistent(nvobj::pool<root> &pop)
{
	auto r = pop.root();

	try {
		nvobj::transaction::run(pop,
					[&] { r->ptr->increase_elements(); });
	} catch (...) {
		UT_ASSERT(0);
	}
}

void
run_inconsistent(nvobj::pool<root> &pop)
{
	auto r = pop.root();

	r->ptr->increase_elements();
	r->ptr.persist();
}

void
check_consistency(nvobj::pool<root> &pop)
{
	auto r = pop.root();

	if (r->ptr->array[0] == 1) {
		auto i = 1;
		for (auto e : r->ptr->array)
			UT_ASSERT(e == i++);
	} else {
		auto i = 2;
		for (auto e : r->ptr->array)
			UT_ASSERT(e == i++);
	}
}

static void
test(int argc, char *argv[])
{
	if (argc != 3 || strchr("coxi", argv[1][0]) == nullptr)
		UT_FATAL("usage: %s <c|o|x|i> file-name", argv[0]);

	const char *path = argv[2];

	nvobj::pool<root> pop;

	try {
		if (argv[1][0] == 'o') {
			pop = nvobj::pool<root>::open(path, LAYOUT);

			check_consistency(pop);
		} else if (argv[1][0] == 'c') {
			pop = nvobj::pool<root>::create(path, LAYOUT,
							PMEMOBJ_MIN_POOL * 20,
							S_IWUSR | S_IRUSR);

			init(pop);
		} else if (argv[1][0] == 'i') {
			pop = nvobj::pool<root>::open(path, LAYOUT);

			run_inconsistent(pop);
		} else if (argv[1][0] == 'x') {
			pop = nvobj::pool<root>::open(path, LAYOUT);

			run_consistent(pop);
		}
	} catch (pmem::pool_error &pe) {
		UT_FATAL("!pool::create: %s %s", pe.what(), path);
	}

	pop.close();
}

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