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
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// <memory>
// unique_ptr
// Test unique_ptr move ctor
#include <memory>
#include <utility>
#include <cassert>
#include "test_macros.h"
#include "unique_ptr_test_helper.h"
//=============================================================================
// TESTING unique_ptr(unique_ptr&&)
//
// Concerns
// 1 The moved from pointer is empty and the new pointer stores the old value.
// 2 The only requirement on the deleter is that it is MoveConstructible
// or a reference.
// 3 The constructor works for explicitly moved values (i.e. std::move(x))
// 4 The constructor works for true temporaries (e.g. a return value)
//
// Plan
// 1 Explicitly construct unique_ptr<T, D> for various deleter types 'D'.
// check that the value and deleter have been properly moved. (C-1,2,3)
//
// 2 Use the expression 'sink(source())' to move construct a unique_ptr<T, D>
// from a temporary. 'source' should return the unique_ptr by value and
// 'sink' should accept the unique_ptr by value. (C-1,2,4)
template <class VT>
std::unique_ptr<VT> source1() {
return std::unique_ptr<VT>(newValue<VT>(1));
}
template <class VT>
std::unique_ptr<VT, Deleter<VT> > source2() {
return std::unique_ptr<VT, Deleter<VT> >(newValue<VT>(1), Deleter<VT>(5));
}
template <class VT>
std::unique_ptr<VT, NCDeleter<VT>&> source3() {
static NCDeleter<VT> d(5);
return std::unique_ptr<VT, NCDeleter<VT>&>(newValue<VT>(1), d);
}
template <class VT>
void sink1(std::unique_ptr<VT> p) {
assert(p.get() != nullptr);
}
template <class VT>
void sink2(std::unique_ptr<VT, Deleter<VT> > p) {
assert(p.get() != nullptr);
assert(p.get_deleter().state() == 5);
}
template <class VT>
void sink3(std::unique_ptr<VT, NCDeleter<VT>&> p) {
assert(p.get() != nullptr);
assert(p.get_deleter().state() == 5);
assert(&p.get_deleter() == &source3<VT>().get_deleter());
}
template <class ValueT>
void test_sfinae() {
typedef std::unique_ptr<ValueT> U;
{ // Ensure unique_ptr is non-copyable
static_assert((!std::is_constructible<U, U const&>::value), "");
static_assert((!std::is_constructible<U, U&>::value), "");
}
}
template <bool IsArray>
void test_basic() {
typedef typename std::conditional<!IsArray, A, A[]>::type VT;
const int expect_alive = IsArray ? 5 : 1;
{
typedef std::unique_ptr<VT> APtr;
APtr s(newValue<VT>(expect_alive));
A* p = s.get();
APtr s2 = std::move(s);
assert(s2.get() == p);
assert(s.get() == 0);
assert(A::count == expect_alive);
}
assert(A::count == 0);
{
typedef Deleter<VT> MoveDel;
typedef std::unique_ptr<VT, MoveDel> APtr;
MoveDel d(5);
APtr s(newValue<VT>(expect_alive), std::move(d));
assert(d.state() == 0);
assert(s.get_deleter().state() == 5);
A* p = s.get();
APtr s2 = std::move(s);
assert(s2.get() == p);
assert(s.get() == 0);
assert(A::count == expect_alive);
assert(s2.get_deleter().state() == 5);
assert(s.get_deleter().state() == 0);
}
assert(A::count == 0);
{
typedef NCDeleter<VT> NonCopyDel;
typedef std::unique_ptr<VT, NonCopyDel&> APtr;
NonCopyDel d;
APtr s(newValue<VT>(expect_alive), d);
A* p = s.get();
APtr s2 = std::move(s);
assert(s2.get() == p);
assert(s.get() == 0);
assert(A::count == expect_alive);
d.set_state(6);
assert(s2.get_deleter().state() == d.state());
assert(s.get_deleter().state() == d.state());
}
assert(A::count == 0);
{
sink1<VT>(source1<VT>());
assert(A::count == 0);
sink2<VT>(source2<VT>());
assert(A::count == 0);
sink3<VT>(source3<VT>());
assert(A::count == 0);
}
assert(A::count == 0);
}
template <class VT>
void test_noexcept() {
#if TEST_STD_VER >= 11
{
typedef std::unique_ptr<VT> U;
static_assert(std::is_nothrow_move_constructible<U>::value, "");
}
{
typedef std::unique_ptr<VT, Deleter<VT> > U;
static_assert(std::is_nothrow_move_constructible<U>::value, "");
}
{
typedef std::unique_ptr<VT, NCDeleter<VT> &> U;
static_assert(std::is_nothrow_move_constructible<U>::value, "");
}
{
typedef std::unique_ptr<VT, const NCConstDeleter<VT> &> U;
static_assert(std::is_nothrow_move_constructible<U>::value, "");
}
#endif
}
int main(int, char**) {
{
test_basic</*IsArray*/ false>();
test_sfinae<int>();
test_noexcept<int>();
}
{
test_basic</*IsArray*/ true>();
test_sfinae<int[]>();
test_noexcept<int[]>();
}
return 0;
}
|