| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
// <memory> |
| 10 |
|
| 11 |
// unique_ptr |
| 12 |
|
| 13 |
// test swap |
| 14 |
|
| 15 |
#include <memory> |
| 16 |
#include <cassert> |
| 17 |
|
| 18 |
#include "test_macros.h" |
| 19 |
#include "unique_ptr_test_helper.h" |
| 20 |
|
| 21 |
struct TT { |
| 22 |
int state_; |
| 23 |
static int count; |
| 24 |
TEST_CONSTEXPR_CXX23 TT() : state_(-1) { |
| 25 |
if (!TEST_IS_CONSTANT_EVALUATED) |
| 26 |
++count; |
| 27 |
} |
| 28 |
TEST_CONSTEXPR_CXX23 explicit TT(int i) : state_(i) { |
| 29 |
if (!TEST_IS_CONSTANT_EVALUATED) |
| 30 |
++count; |
| 31 |
} |
| 32 |
TEST_CONSTEXPR_CXX23 TT(const TT& a) : state_(a.state_) { |
| 33 |
if (!TEST_IS_CONSTANT_EVALUATED) |
| 34 |
++count; |
| 35 |
} |
| 36 |
TEST_CONSTEXPR_CXX23 TT& operator=(const TT& a) { |
| 37 |
state_ = a.state_; |
| 38 |
return *this; |
| 39 |
} |
| 40 |
TEST_CONSTEXPR_CXX23 ~TT() { |
| 41 |
if (!TEST_IS_CONSTANT_EVALUATED) |
| 42 |
--count; |
| 43 |
} |
| 44 |
|
| 45 |
friend TEST_CONSTEXPR_CXX23 bool operator==(const TT& x, const TT& y) { return x.state_ == y.state_; } |
| 46 |
}; |
| 47 |
|
| 48 |
int TT::count = 0; |
| 49 |
|
| 50 |
template <class T> |
| 51 |
TEST_CONSTEXPR_CXX23 typename std::remove_all_extents<T>::type* newValueInit(int size, int new_value) { |
| 52 |
typedef typename std::remove_all_extents<T>::type VT; |
| 53 |
VT* p = newValue<T>(size); |
| 54 |
for (int i = 0; i < size; ++i) |
| 55 |
(p + i)->state_ = new_value; |
| 56 |
return p; |
| 57 |
} |
| 58 |
|
| 59 |
template <bool IsArray> |
| 60 |
TEST_CONSTEXPR_CXX23 void test_basic() { |
| 61 |
typedef typename std::conditional<IsArray, TT[], TT>::type VT; |
| 62 |
const int expect_alive = IsArray ? 5 : 1; |
| 63 |
#if TEST_STD_VER >= 11 |
| 64 |
{ |
| 65 |
using U = std::unique_ptr<VT, Deleter<VT> >; |
| 66 |
U u; ((void)u); |
| 67 |
ASSERT_NOEXCEPT(u.swap(u)); |
| 68 |
} |
| 69 |
#endif |
| 70 |
{ |
| 71 |
TT* p1 = newValueInit<VT>(expect_alive, 1); |
| 72 |
std::unique_ptr<VT, Deleter<VT> > s1(p1, Deleter<VT>(1)); |
| 73 |
TT* p2 = newValueInit<VT>(expect_alive, 2); |
| 74 |
std::unique_ptr<VT, Deleter<VT> > s2(p2, Deleter<VT>(2)); |
| 75 |
assert(s1.get() == p1); |
| 76 |
assert(*s1.get() == TT(1)); |
| 77 |
assert(s1.get_deleter().state() == 1); |
| 78 |
assert(s2.get() == p2); |
| 79 |
assert(*s2.get() == TT(2)); |
| 80 |
assert(s2.get_deleter().state() == 2); |
| 81 |
s1.swap(s2); |
| 82 |
assert(s1.get() == p2); |
| 83 |
assert(*s1.get() == TT(2)); |
| 84 |
assert(s1.get_deleter().state() == 2); |
| 85 |
assert(s2.get() == p1); |
| 86 |
assert(*s2.get() == TT(1)); |
| 87 |
assert(s2.get_deleter().state() == 1); |
| 88 |
if (!TEST_IS_CONSTANT_EVALUATED) |
| 89 |
assert(TT::count == (expect_alive * 2)); |
| 90 |
} |
| 91 |
if (!TEST_IS_CONSTANT_EVALUATED) |
| 92 |
assert(TT::count == 0); |
| 93 |
} |
| 94 |
|
| 95 |
TEST_CONSTEXPR_CXX23 bool test() { |
| 96 |
test_basic</*IsArray*/ false>(); |
| 97 |
test_basic<true>(); |
| 98 |
|
| 99 |
return true; |
| 100 |
} |
| 101 |
|
| 102 |
int main(int, char**) { |
| 103 |
test(); |
| 104 |
#if TEST_STD_VER >= 23 |
| 105 |
static_assert(test()); |
| 106 |
#endif |
| 107 |
|
| 108 |
return 0; |
| 109 |
} |