|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// The LLVM Compiler Infrastructure |
| 4 | +// |
| 5 | +// This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | +// Source Licenses. See LICENSE.TXT for details. |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | +// |
| 10 | +// Copyright 2018-2021, Intel Corporation |
| 11 | +// |
| 12 | +// Modified to test pmem::obj containers |
| 13 | +// |
| 14 | + |
| 15 | +#include "unittest.hpp" |
| 16 | + |
| 17 | +#include <libpmemobj++/container/array.hpp> |
| 18 | +#include <libpmemobj++/make_persistent.hpp> |
| 19 | +#include <libpmemobj++/persistent_ptr.hpp> |
| 20 | +#include <libpmemobj++/pool.hpp> |
| 21 | +#include <libpmemobj++/transaction.hpp> |
| 22 | + |
| 23 | +struct NoDefault { |
| 24 | + NoDefault(int) |
| 25 | + { |
| 26 | + } |
| 27 | +}; |
| 28 | + |
| 29 | +struct Testcase1 { |
| 30 | + typedef double T; |
| 31 | + typedef pmem::obj::array<const T, 3> C; |
| 32 | + C c = {{1.1, 2.2, 3.3}}; |
| 33 | + C c2 = c; |
| 34 | + |
| 35 | + void |
| 36 | + run() |
| 37 | + { |
| 38 | + (void)c2; |
| 39 | + static_assert(std::is_copy_constructible<C>::value, ""); |
| 40 | + } |
| 41 | +}; |
| 42 | + |
| 43 | +struct Testcase2 { |
| 44 | + // const arrays of size 0 should disable the implicit copy |
| 45 | + // assignment operator. |
| 46 | + typedef double T; |
| 47 | + typedef pmem::obj::array<const T, 0> C; |
| 48 | + C c = {{}}; |
| 49 | + C c2 = c; |
| 50 | + |
| 51 | + void |
| 52 | + run() |
| 53 | + { |
| 54 | + (void)c2; |
| 55 | + static_assert(std::is_copy_constructible<C>::value, ""); |
| 56 | + } |
| 57 | +}; |
| 58 | + |
| 59 | +struct Testcase3 { |
| 60 | + typedef NoDefault T; |
| 61 | + typedef pmem::obj::array<const T, 0> C; |
| 62 | + C c = {{}}; |
| 63 | + C c2 = c; |
| 64 | + void |
| 65 | + run() |
| 66 | + { |
| 67 | + (void)c2; |
| 68 | + static_assert(std::is_copy_constructible<C>::value, ""); |
| 69 | + } |
| 70 | +}; |
| 71 | + |
| 72 | +struct root { |
| 73 | + pmem::obj::persistent_ptr<Testcase1> r1; |
| 74 | + pmem::obj::persistent_ptr<Testcase2> r2; |
| 75 | + pmem::obj::persistent_ptr<Testcase3> r3; |
| 76 | +}; |
| 77 | + |
| 78 | +void |
| 79 | +run(pmem::obj::pool<root> &pop) |
| 80 | +{ |
| 81 | + try { |
| 82 | + pmem::obj::transaction::run(pop, [&] { |
| 83 | + pop.root()->r1 = |
| 84 | + pmem::obj::make_persistent<Testcase1>(); |
| 85 | + pop.root()->r2 = |
| 86 | + pmem::obj::make_persistent<Testcase2>(); |
| 87 | + pop.root()->r3 = |
| 88 | + pmem::obj::make_persistent<Testcase3>(); |
| 89 | + }); |
| 90 | + |
| 91 | + pop.root()->r1->run(); |
| 92 | + pop.root()->r2->run(); |
| 93 | + pop.root()->r3->run(); |
| 94 | + } catch (...) { |
| 95 | + UT_ASSERT(0); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +static void |
| 100 | +test(int argc, char *argv[]) |
| 101 | +{ |
| 102 | + if (argc != 2) |
| 103 | + UT_FATAL("usage: %s file-name", argv[0]); |
| 104 | + |
| 105 | + const char *path = argv[1]; |
| 106 | + |
| 107 | + pmem::obj::pool<root> pop; |
| 108 | + try { |
| 109 | + pop = pmem::obj::pool<root>::create(path, "implicit_copy.pass", |
| 110 | + PMEMOBJ_MIN_POOL, |
| 111 | + S_IWUSR | S_IRUSR); |
| 112 | + } catch (...) { |
| 113 | + UT_FATAL("!pmemobj_create: %s", path); |
| 114 | + } |
| 115 | + |
| 116 | + run(pop); |
| 117 | + |
| 118 | + pop.close(); |
| 119 | +} |
| 120 | + |
| 121 | +int |
| 122 | +main(int argc, char *argv[]) |
| 123 | +{ |
| 124 | + return run_test([&] { test(argc, argv); }); |
| 125 | +} |
0 commit comments