| 1 | // { dg-do run { target c++11 } } |
| 2 | // { dg-require-effective-target hosted } |
| 3 | |
| 4 | // Copyright (C) 2005-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 | // 20.6.6.2 Template class shared_ptr [util.smartptr.shared] |
| 22 | |
| 23 | #include <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 | void deleter(A* p) { delete p; } |
| 47 | |
| 48 | struct reset_count_struct |
| 49 | { |
| 50 | ~reset_count_struct() |
| 51 | { |
| 52 | A::ctor_count = 0; |
| 53 | A::dtor_count = 0; |
| 54 | B::ctor_count = 0; |
| 55 | B::dtor_count = 0; |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | // 20.6.6.2.1 shared_ptr constructors [util.smartptr.shared.const] |
| 60 | |
| 61 | // Copy construction |
| 62 | int test01() |
| 63 | { |
| 64 | reset_count_struct __attribute__((unused)) reset; |
| 65 | |
| 66 | std::shared_ptr<A> a1; |
| 67 | std::shared_ptr<A> a2(a1); |
| 68 | VERIFY( a2.use_count() == 0 ); |
| 69 | VERIFY( A::ctor_count == 0 ); |
| 70 | VERIFY( A::dtor_count == 0 ); |
| 71 | VERIFY( B::ctor_count == 0 ); |
| 72 | VERIFY( B::dtor_count == 0 ); |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | int |
| 78 | test02() |
| 79 | { |
| 80 | reset_count_struct __attribute__((unused)) reset; |
| 81 | |
| 82 | std::shared_ptr<A> a1(new A); |
| 83 | std::shared_ptr<A> a2(a1); |
| 84 | VERIFY( a2.use_count() == 2 ); |
| 85 | VERIFY( A::ctor_count == 1 ); |
| 86 | VERIFY( A::dtor_count == 0 ); |
| 87 | VERIFY( B::ctor_count == 0 ); |
| 88 | VERIFY( B::dtor_count == 0 ); |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | int |
| 94 | test03() |
| 95 | { |
| 96 | reset_count_struct __attribute__((unused)) reset; |
| 97 | |
| 98 | std::shared_ptr<B> b(new B); |
| 99 | std::shared_ptr<A> a(b); |
| 100 | VERIFY( a.use_count() == 2 ); |
| 101 | VERIFY( A::ctor_count == 1 ); |
| 102 | VERIFY( A::dtor_count == 0 ); |
| 103 | VERIFY( B::ctor_count == 1 ); |
| 104 | VERIFY( B::dtor_count == 0 ); |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | int |
| 110 | test04() |
| 111 | { |
| 112 | reset_count_struct __attribute__((unused)) reset; |
| 113 | |
| 114 | std::shared_ptr<B> b(new B, &deleter); |
| 115 | std::shared_ptr<A> a(b); |
| 116 | VERIFY( a.use_count() == 2 ); |
| 117 | VERIFY( A::ctor_count == 1 ); |
| 118 | VERIFY( A::dtor_count == 0 ); |
| 119 | VERIFY( B::ctor_count == 1 ); |
| 120 | VERIFY( B::dtor_count == 0 ); |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | int |
| 126 | main() |
| 127 | { |
| 128 | test01(); |
| 129 | test02(); |
| 130 | test03(); |
| 131 | test04(); |
| 132 | return 0; |
| 133 | } |