| 1 | // { dg-do run { target c++11 } } |
| 2 | // { dg-require-effective-target hosted } |
| 3 | |
| 4 | // Copyright (C) 2007-2025 Free Software Foundation, Inc. |
| 5 | // |
| 6 | // This file is part of the GNU ISO C++ Library. This library is free |
| 7 | // software; you can redistribute it and/or modify it under the |
| 8 | // terms of the GNU General Public License as published by the |
| 9 | // Free Software Foundation; either version 3, or (at your option) |
| 10 | // any later version. |
| 11 | |
| 12 | // This library is distributed in the hope that it will be useful, |
| 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | // GNU General Public License for more details. |
| 16 | |
| 17 | // You should have received a copy of the GNU General Public License along |
| 18 | // with this library; see the file COPYING3. If not see |
| 19 | // <http://www.gnu.org/licenses/>. |
| 20 | |
| 21 | // TR1 20.6.6.2 Template class shared_ptr [util.smartptr.shared] |
| 22 | |
| 23 | #include <memory> |
| 24 | #include <utility> |
| 25 | #include <testsuite_hooks.h> |
| 26 | |
| 27 | struct A |
| 28 | { |
| 29 | A() { ++ctor_count; } |
| 30 | virtual ~A() { ++dtor_count; } |
| 31 | static long ctor_count; |
| 32 | static long dtor_count; |
| 33 | }; |
| 34 | long A::ctor_count = 0; |
| 35 | long A::dtor_count = 0; |
| 36 | |
| 37 | struct B : A |
| 38 | { |
| 39 | B() { ++ctor_count; } |
| 40 | virtual ~B() { ++dtor_count; } |
| 41 | static long ctor_count; |
| 42 | static long dtor_count; |
| 43 | }; |
| 44 | long B::ctor_count = 0; |
| 45 | long B::dtor_count = 0; |
| 46 | |
| 47 | struct D |
| 48 | { |
| 49 | void operator()(B* p) const { delete p; ++delete_count; } |
| 50 | static long delete_count; |
| 51 | }; |
| 52 | long D::delete_count = 0; |
| 53 | |
| 54 | struct reset_count_struct |
| 55 | { |
| 56 | ~reset_count_struct() |
| 57 | { |
| 58 | A::ctor_count = 0; |
| 59 | A::dtor_count = 0; |
| 60 | B::ctor_count = 0; |
| 61 | B::dtor_count = 0; |
| 62 | D::delete_count = 0; |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | // 20.6.6.2.1 shared_ptr constructors [util.smartptr.shared.const] |
| 67 | |
| 68 | // Rvalue construction |
| 69 | int test01() |
| 70 | { |
| 71 | reset_count_struct __attribute__((unused)) reset; |
| 72 | |
| 73 | std::shared_ptr<A> a1; |
| 74 | std::shared_ptr<A> a2(std::move(a1)); |
| 75 | VERIFY( a1.use_count() == 0 ); |
| 76 | VERIFY( a2.use_count() == 0 ); |
| 77 | VERIFY( A::ctor_count == 0 ); |
| 78 | VERIFY( A::dtor_count == 0 ); |
| 79 | VERIFY( B::ctor_count == 0 ); |
| 80 | VERIFY( B::dtor_count == 0 ); |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | int |
| 86 | test02() |
| 87 | { |
| 88 | reset_count_struct __attribute__((unused)) reset; |
| 89 | |
| 90 | std::shared_ptr<A> a1(new A); |
| 91 | std::shared_ptr<A> a2(std::move(a1)); |
| 92 | VERIFY( a1.use_count() == 0 ); |
| 93 | VERIFY( a2.use_count() == 1 ); |
| 94 | VERIFY( A::ctor_count == 1 ); |
| 95 | VERIFY( A::dtor_count == 0 ); |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | int |
| 101 | test03() |
| 102 | { |
| 103 | reset_count_struct __attribute__((unused)) reset; |
| 104 | |
| 105 | std::shared_ptr<B> b(new B); |
| 106 | std::shared_ptr<A> a(std::move(b)); |
| 107 | VERIFY( b.use_count() == 0 ); |
| 108 | VERIFY( a.use_count() == 1 ); |
| 109 | VERIFY( A::ctor_count == 1 ); |
| 110 | VERIFY( A::dtor_count == 0 ); |
| 111 | VERIFY( B::ctor_count == 1 ); |
| 112 | VERIFY( B::dtor_count == 0 ); |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | int |
| 118 | test04() |
| 119 | { |
| 120 | reset_count_struct __attribute__((unused)) reset; |
| 121 | |
| 122 | std::shared_ptr<B> b(new B, D()); |
| 123 | std::shared_ptr<A> a(std::move(b)); |
| 124 | VERIFY( b.use_count() == 0 ); |
| 125 | VERIFY( a.use_count() == 1 ); |
| 126 | VERIFY( A::ctor_count == 1 ); |
| 127 | VERIFY( A::dtor_count == 0 ); |
| 128 | VERIFY( B::ctor_count == 1 ); |
| 129 | VERIFY( B::dtor_count == 0 ); |
| 130 | |
| 131 | a = std::move(std::shared_ptr<A>()); |
| 132 | VERIFY( D::delete_count == 1 ); |
| 133 | VERIFY( B::dtor_count == 1 ); |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | int |
| 139 | test05() |
| 140 | { |
| 141 | reset_count_struct __attribute__((unused)) reset; |
| 142 | |
| 143 | std::shared_ptr<A> a(std::move(std::shared_ptr<A>(new A))); |
| 144 | VERIFY( a.use_count() == 1 ); |
| 145 | VERIFY( A::ctor_count == 1 ); |
| 146 | VERIFY( A::dtor_count == 0 ); |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | int |
| 152 | main() |
| 153 | { |
| 154 | test01(); |
| 155 | test02(); |
| 156 | test03(); |
| 157 | test04(); |
| 158 | test05(); |
| 159 | return 0; |
| 160 | } |