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
|
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2020, Intel Corporation */
#include "unittest.hpp"
#include <array>
#include <libpmemobj++/detail/pair.hpp>
#include <libpmemobj++/pool.hpp>
static int copy_ctor_called = 0;
static int move_ctor_called = 0;
struct A {
A(int a = 0, int b = 0, int c = 0) : a(a), b(b), c(c)
{
}
A(const A &rhs)
{
copy_ctor_called++;
a = rhs.a;
b = rhs.b;
c = rhs.c;
}
A(A &&rhs)
{
move_ctor_called++;
a = rhs.a;
b = rhs.b;
c = rhs.c;
}
A &operator=(const A &) = default;
int a;
int b;
int c;
};
void
verify_vals(pmem::detail::pair<A, A> &a, std::array<int, 6> expected)
{
UT_ASSERTeq(a.first.a, expected[0]);
UT_ASSERTeq(a.first.b, expected[1]);
UT_ASSERTeq(a.first.c, expected[2]);
UT_ASSERTeq(a.second.a, expected[3]);
UT_ASSERTeq(a.second.b, expected[4]);
UT_ASSERTeq(a.second.c, expected[5]);
}
void
construct_test()
{
{
A a1(1, 2, 3), a2(4, 5, 6);
copy_ctor_called = 0;
move_ctor_called = 0;
pmem::detail::pair<A, A> p(a1, a2);
UT_ASSERTeq(copy_ctor_called, 2);
UT_ASSERTeq(move_ctor_called, 0);
verify_vals(p, {1, 2, 3, 4, 5, 6});
}
{
A a1(1, 2, 3), a2(4, 5, 6);
copy_ctor_called = 0;
move_ctor_called = 0;
pmem::detail::pair<A, A> p(std::move(a1), std::move(a2));
UT_ASSERTeq(copy_ctor_called, 0);
UT_ASSERTeq(move_ctor_called, 2);
verify_vals(p, {1, 2, 3, 4, 5, 6});
}
{
A a1(1, 2, 3), a2(4, 5, 6);
copy_ctor_called = 0;
move_ctor_called = 0;
pmem::detail::pair<A, A> p(std::piecewise_construct,
std::forward_as_tuple(a1),
std::forward_as_tuple(a2));
UT_ASSERTeq(copy_ctor_called, 2);
UT_ASSERTeq(move_ctor_called, 0);
verify_vals(p, {1, 2, 3, 4, 5, 6});
}
{
A a1(1, 2, 3), a2(4, 5, 6);
copy_ctor_called = 0;
move_ctor_called = 0;
pmem::detail::pair<A, A> p(
std::piecewise_construct,
std::forward_as_tuple(std::move(a1)),
std::forward_as_tuple(std::move(a2)));
UT_ASSERTeq(copy_ctor_called, 0);
UT_ASSERTeq(move_ctor_called, 2);
verify_vals(p, {1, 2, 3, 4, 5, 6});
}
{
A a1(1, 2, 3), a2(4, 5, 6);
copy_ctor_called = 0;
move_ctor_called = 0;
pmem::detail::pair<A, A> p(std::piecewise_construct,
std::forward_as_tuple(1, 2),
std::forward_as_tuple(3));
UT_ASSERTeq(copy_ctor_called, 0);
UT_ASSERTeq(move_ctor_called, 0);
verify_vals(p, {1, 2, 0, 3, 0, 0});
}
}
static void
test(int argc, char *argv[])
{
construct_test();
}
int
main(int argc, char *argv[])
{
return run_test([&] { test(argc, argv); });
}
|