| 1 |
// { dg-do run { target c++14 } } |
| 2 |
|
| 3 |
// Copyright (C) 2015-2025 Free Software Foundation, Inc. |
| 4 |
// |
| 5 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 6 |
// software; you can redistribute it and/or modify it under the |
| 7 |
// terms of the GNU General Public License as published by the |
| 8 |
// Free Software Foundation; either version 3, or (at your option) |
| 9 |
// any later version. |
| 10 |
|
| 11 |
// This library is distributed in the hope that it will be useful, |
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
// GNU General Public License for more details. |
| 15 |
|
| 16 |
// You should have received a copy of the GNU General Public License along |
| 17 |
// with this library; see the file COPYING3. If not see |
| 18 |
// <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
// 8.2.1 Class template shared_ptr [memory.smartptr.shared] |
| 21 |
|
| 22 |
|
| 23 |
#include <experimental/memory> |
| 24 |
#include <testsuite_hooks.h> |
| 25 |
|
| 26 |
struct A |
| 27 |
{ |
| 28 |
A() { ++ctor_count; } |
| 29 |
virtual ~A() { ++dtor_count; } |
| 30 |
static long ctor_count; |
| 31 |
static long dtor_count; |
| 32 |
}; |
| 33 |
long A::ctor_count = 0; |
| 34 |
long A::dtor_count = 0; |
| 35 |
|
| 36 |
struct B : A |
| 37 |
{ |
| 38 |
B() { ++ctor_count; } |
| 39 |
virtual ~B() { ++dtor_count; } |
| 40 |
static long ctor_count; |
| 41 |
static long dtor_count; |
| 42 |
}; |
| 43 |
long B::ctor_count = 0; |
| 44 |
long B::dtor_count = 0; |
| 45 |
|
| 46 |
struct reset_count_struct |
| 47 |
{ |
| 48 |
~reset_count_struct() |
| 49 |
{ |
| 50 |
A::ctor_count = 0; |
| 51 |
A::dtor_count = 0; |
| 52 |
B::ctor_count = 0; |
| 53 |
B::dtor_count = 0; |
| 54 |
} |
| 55 |
}; |
| 56 |
|
| 57 |
// C++14 ยง20.8.2.2.3 shared_ptr assignment |
| 58 |
|
| 59 |
void |
| 60 |
test01() |
| 61 |
{ |
| 62 |
reset_count_struct __attribute__((unused)) reset; |
| 63 |
|
| 64 |
std::experimental::shared_ptr<A[5]> a; |
| 65 |
std::experimental::shared_ptr<A[]> a1; |
| 66 |
std::experimental::shared_ptr<B[5]> a2; |
| 67 |
|
| 68 |
a = std::experimental::shared_ptr<A[5]> (); |
| 69 |
VERIFY( a.get() == 0 ); |
| 70 |
VERIFY( A::ctor_count == 0 ); |
| 71 |
VERIFY( A::dtor_count == 0 ); |
| 72 |
VERIFY( B::ctor_count == 0 ); |
| 73 |
VERIFY( B::dtor_count == 0 ); |
| 74 |
|
| 75 |
a = std::experimental::shared_ptr<A[5]> (new A[5]); |
| 76 |
VERIFY( a.get() != 0 ); |
| 77 |
VERIFY( A::ctor_count == 5 ); |
| 78 |
VERIFY( A::dtor_count == 0 ); |
| 79 |
VERIFY( B::ctor_count == 0 ); |
| 80 |
VERIFY( B::dtor_count == 0 ); |
| 81 |
|
| 82 |
a1 = std::experimental::shared_ptr<A[5]> (new A[5]); |
| 83 |
VERIFY( a1.get() != 0 ); |
| 84 |
VERIFY( A::ctor_count == 10 ); |
| 85 |
VERIFY( A::dtor_count == 0 ); |
| 86 |
VERIFY( B::ctor_count == 0 ); |
| 87 |
VERIFY( B::dtor_count == 0 ); |
| 88 |
|
| 89 |
a2 = std::experimental::shared_ptr<B[5]> (new B[5]); |
| 90 |
VERIFY( a2.get() != 0 ); |
| 91 |
VERIFY( A::ctor_count == 15 ); |
| 92 |
VERIFY( A::dtor_count == 0 ); |
| 93 |
VERIFY( B::ctor_count == 5 ); |
| 94 |
VERIFY( B::dtor_count == 0 ); |
| 95 |
} |
| 96 |
|
| 97 |
void |
| 98 |
test02() |
| 99 |
{ |
| 100 |
std::experimental::shared_ptr<A[5]> p(new A[5]); |
| 101 |
std::experimental::shared_ptr<A[5]> p1; |
| 102 |
std::experimental::shared_ptr<A[]> p2; |
| 103 |
|
| 104 |
p1 = p; |
| 105 |
VERIFY( p.get() == p1.get() ); |
| 106 |
|
| 107 |
p2 = p1; |
| 108 |
VERIFY( p1.get() == p2.get() ); |
| 109 |
} |
| 110 |
|
| 111 |
int |
| 112 |
main() |
| 113 |
{ |
| 114 |
test01(); |
| 115 |
test02(); |
| 116 |
return 0; |
| 117 |
} |