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
|
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2016-2020, Intel Corporation */
/*
* allocator.cpp -- cpp bindings test
*
*/
#include "unittest.hpp"
#include <libpmemobj++/allocator.hpp>
#include <libpmemobj++/p.hpp>
#include <libpmemobj++/persistent_ptr.hpp>
#include <libpmemobj++/pool.hpp>
#include <libpmemobj++/transaction.hpp>
#define LAYOUT "cpp"
namespace nvobj = pmem::obj;
namespace
{
const int TEST_ARR_SIZE = 10;
struct foo {
/*
* Default constructor.
*/
foo()
{
bar = 1;
for (int i = 0; i < TEST_ARR_SIZE; ++i)
arr[i] = static_cast<char>(i);
}
/*
* Copy constructible.
*/
foo(const foo &rhs) = default;
/*
* Check foo values.
*/
void
test_foo()
{
UT_ASSERTeq(bar, 1);
for (int i = 0; i < TEST_ARR_SIZE; ++i)
UT_ASSERTeq(arr[i], i);
}
nvobj::p<int> bar;
nvobj::p<char> arr[TEST_ARR_SIZE];
};
/*
* test_alloc_valid -- (internal) test an allocation within a transaction
*/
void
test_alloc_valid(nvobj::pool_base &pop)
{
nvobj::allocator<foo> al;
try {
nvobj::transaction::run(pop, [&] {
auto fooptr = al.allocate(1);
UT_ASSERT(pmemobj_alloc_usable_size(fooptr.raw()) >=
sizeof(foo));
al.construct(fooptr, foo());
fooptr->test_foo();
al.destroy(fooptr);
al.deallocate(fooptr);
});
} catch (...) {
UT_ASSERT(0);
}
}
/*
* test_alloc_invalid -- (internal) test an allocation outside of a transaction
*/
void
test_alloc_invalid()
{
nvobj::allocator<foo> al;
bool thrown = false;
try {
auto fooptr = al.allocate(1);
al.construct(fooptr, foo());
} catch (pmem::transaction_scope_error &) {
thrown = true;
} catch (...) {
UT_ASSERT(0);
}
UT_ASSERT(thrown);
}
/*
* test_dealloc_invalid -- (internal) test an deallocation outside of a
* transaction
*/
void
test_dealloc_invalid(nvobj::pool_base &pop)
{
nvobj::allocator<foo> al;
nvobj::persistent_ptr<foo> fooptr;
bool thrown = false;
try {
nvobj::transaction::run(pop, [&] { fooptr = al.allocate(1); });
al.deallocate(fooptr);
} catch (pmem::transaction_scope_error &) {
thrown = true;
} catch (...) {
UT_ASSERT(0);
}
UT_ASSERT(thrown);
try {
nvobj::transaction::run(pop, [&] { al.deallocate(fooptr); });
} catch (...) {
UT_ASSERT(0);
}
}
/*
* test_alloc_equal -- (internal) test allocator equality/inequality operators
*/
void
test_alloc_equal()
{
nvobj::allocator<foo> fooal;
nvobj::allocator<int> intal;
std::allocator<foo> stdfooal;
std::allocator<int> stdintal;
std::allocator<double> stddblal;
UT_ASSERT(fooal == fooal);
UT_ASSERT(intal == fooal);
UT_ASSERT(!(fooal != fooal));
UT_ASSERT(!(intal != fooal));
UT_ASSERT(fooal != stdfooal);
UT_ASSERT(fooal != stdintal);
UT_ASSERT(fooal != stddblal);
UT_ASSERT(intal != stdfooal);
UT_ASSERT(intal != stdintal);
UT_ASSERT(intal != stddblal);
UT_ASSERT(!(fooal == stdfooal));
UT_ASSERT(!(fooal == stdintal));
UT_ASSERT(!(fooal == stddblal));
UT_ASSERT(!(intal == stdfooal));
UT_ASSERT(!(intal == stdintal));
UT_ASSERT(!(intal == stddblal));
}
}
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_base pop;
try {
pop = nvobj::pool_base::create(path, LAYOUT, PMEMOBJ_MIN_POOL,
S_IWUSR | S_IRUSR);
} catch (pmem::pool_error &pe) {
UT_FATAL("!pool::create: %s %s", pe.what(), path);
}
test_alloc_valid(pop);
test_alloc_invalid();
test_dealloc_invalid(pop);
test_alloc_equal();
pop.close();
}
int
main(int argc, char *argv[])
{
return run_test([&] { test(argc, argv); });
}
|