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
|
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Copyright 2020, Intel Corporation
//
// Modified to test pmem::obj containers
//
// template <typename CharT, typename Traits>
// void basic_string<CharT, Traits>::swap(basic_string &other)
#include "unittest.hpp"
#include <libpmemobj++/container/string.hpp>
#include <libpmemobj++/make_persistent.hpp>
#include <string>
using S = pmem::obj::string;
struct root {
pmem::obj::persistent_ptr<S> lhs, rhs;
};
bool
invariants(const pmem::obj::persistent_ptr<S> &str)
{
if (str->size() > str->capacity())
return false;
if (str->capacity() < S::sso_capacity)
return false;
if (str->data() == 0)
return false;
if (str->data()[str->size()] != S::value_type(0))
return false;
return true;
}
template <class S>
void
test_swap(pmem::obj::persistent_ptr<S> &lhs, pmem::obj::persistent_ptr<S> &rhs,
std::string s1, std::string s2)
{
lhs->assign(s1);
rhs->assign(s2);
lhs->swap(*rhs);
UT_ASSERT(invariants(lhs));
UT_ASSERT(invariants(rhs));
UT_ASSERT(*rhs == s1);
UT_ASSERT(rhs->size() == s1.size());
UT_ASSERT(*lhs == s2);
UT_ASSERT(lhs->size() == s2.size());
}
static void
test(int argc, char *argv[])
{
if (argc < 2) {
UT_FATAL("usage: %s file-name", argv[0]);
}
auto path = argv[1];
auto pop = pmem::obj::pool<root>::create(
path, "string_swap_test", PMEMOBJ_MIN_POOL, S_IWUSR | S_IRUSR);
auto &lhs = pop.root()->lhs;
auto &rhs = pop.root()->rhs;
try {
pmem::obj::transaction::run(pop, [&] {
lhs = pmem::obj::make_persistent<S>();
rhs = pmem::obj::make_persistent<S>();
});
test_swap(lhs, rhs, "", "");
test_swap(lhs, rhs, "", "12345");
test_swap(lhs, rhs, "", "1234567890");
test_swap(lhs, rhs, "", "12345678901234567890");
test_swap(lhs, rhs, "abcde", "");
test_swap(lhs, rhs, "abcde", "12345");
test_swap(lhs, rhs, "abcde", "1234567890");
test_swap(lhs, rhs, "abcde", "12345678901234567890");
test_swap(lhs, rhs, "abcdefghij", "");
test_swap(lhs, rhs, "abcdefghij", "12345");
test_swap(lhs, rhs, "abcdefghij", "1234567890");
test_swap(lhs, rhs, "abcdefghij", "12345678901234567890");
test_swap(lhs, rhs, "abcdefghijklmnopqrst", "");
test_swap(lhs, rhs, "abcdefghijklmnopqrst", "12345");
test_swap(lhs, rhs, "abcdefghijklmnopqrst", "1234567890");
test_swap(lhs, rhs, "abcdefghijklmnopqrst",
"12345678901234567890");
test_swap(lhs, rhs, std::string(S::sso_capacity * 2, 'a'),
std::string(S::sso_capacity * 2, 'b'));
test_swap(lhs, rhs, std::string(S::sso_capacity * 4, 'a'),
std::string(S::sso_capacity * 2, 'b'));
test_swap(lhs, rhs, std::string(S::sso_capacity * 2, 'a'),
std::string(S::sso_capacity * 4, 'b'));
test_swap(lhs, rhs, std::string(S::sso_capacity * 4, 'a'),
std::string(S::sso_capacity * 4, 'b'));
/* XXX: add swap test cases when allocator template will be
* added to pmem::obj::basic_string */
pmem::obj::transaction::run(pop, [&] {
pmem::obj::delete_persistent<S>(lhs);
pmem::obj::delete_persistent<S>(rhs);
});
} catch (std::exception &e) {
UT_FATALexc(e);
}
pop.close();
}
int
main(int argc, char *argv[])
{
return run_test([&] { test(argc, argv); });
}
|