| 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 | #include <experimental/memory> |
| 23 | #include <testsuite_hooks.h> |
| 24 | |
| 25 | struct A |
| 26 | { |
| 27 | A() { ++ctor_count; } |
| 28 | virtual ~A() { ++dtor_count; } |
| 29 | static long ctor_count; |
| 30 | static long dtor_count; |
| 31 | }; |
| 32 | long A::ctor_count = 0; |
| 33 | long A::dtor_count = 0; |
| 34 | |
| 35 | struct B : A |
| 36 | { |
| 37 | B() { ++ctor_count; } |
| 38 | virtual ~B() { ++dtor_count; } |
| 39 | static long ctor_count; |
| 40 | static long dtor_count; |
| 41 | }; |
| 42 | long B::ctor_count = 0; |
| 43 | long B::dtor_count = 0; |
| 44 | |
| 45 | void deleter(A* p) { delete [] p; } |
| 46 | |
| 47 | struct reset_count_struct |
| 48 | { |
| 49 | ~reset_count_struct() |
| 50 | { |
| 51 | A::ctor_count = 0; |
| 52 | A::dtor_count = 0; |
| 53 | B::ctor_count = 0; |
| 54 | B::dtor_count = 0; |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | // 8.2.1.1 shared_ptr constructors [memory.smartptr.shared.const] |
| 59 | |
| 60 | // Copy construction |
| 61 | |
| 62 | int |
| 63 | test01() |
| 64 | { |
| 65 | reset_count_struct __attribute__((unused)) reset; |
| 66 | |
| 67 | std::experimental::shared_ptr<A[5]> a1; |
| 68 | std::experimental::shared_ptr<A[5]> a2(a1); |
| 69 | VERIFY( a2.use_count() == 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 | return 0; |
| 76 | } |
| 77 | |
| 78 | int |
| 79 | test02() |
| 80 | { |
| 81 | reset_count_struct __attribute__((unused)) reset; |
| 82 | |
| 83 | std::experimental::shared_ptr<A[5]> a1(new A[5]); |
| 84 | std::experimental::shared_ptr<A[5]> a2(a1); |
| 85 | VERIFY( a2.use_count() == 2 ); |
| 86 | VERIFY( A::ctor_count == 5 ); |
| 87 | VERIFY( A::dtor_count == 0 ); |
| 88 | VERIFY( B::ctor_count == 0 ); |
| 89 | VERIFY( B::dtor_count == 0 ); |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | int |
| 95 | test03() |
| 96 | { |
| 97 | reset_count_struct __attribute__((unused)) reset; |
| 98 | |
| 99 | std::experimental::shared_ptr<A[5]> a1(new A[5], &deleter); |
| 100 | std::experimental::shared_ptr<A[5]> a2(a1); |
| 101 | VERIFY( a2.use_count() == 2 ); |
| 102 | VERIFY( A::ctor_count == 5 ); |
| 103 | VERIFY( A::dtor_count == 0 ); |
| 104 | VERIFY( B::ctor_count == 0 ); |
| 105 | VERIFY( B::dtor_count == 0 ); |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | int |
| 111 | test04() |
| 112 | { |
| 113 | reset_count_struct __attribute__((unused)) reset; |
| 114 | |
| 115 | std::experimental::shared_ptr<A[5]> a1(std::experimental::shared_ptr<A[5]> |
| 116 | (new A[5])); |
| 117 | VERIFY( a1.use_count() == 1 ); |
| 118 | VERIFY( A::ctor_count == 5 ); |
| 119 | VERIFY( A::dtor_count == 0 ); |
| 120 | VERIFY( B::ctor_count == 0 ); |
| 121 | VERIFY( B::dtor_count == 0 ); |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | int |
| 127 | test05() |
| 128 | { |
| 129 | reset_count_struct __attribute__((unused)) reset; |
| 130 | |
| 131 | std::experimental::shared_ptr<A[5]> a1(new A[5]); |
| 132 | std::experimental::shared_ptr<A[]> a2(a1); |
| 133 | |
| 134 | VERIFY( a2.use_count() == 2 ); |
| 135 | VERIFY( a2.get() == a1.get() ); |
| 136 | VERIFY( A::ctor_count == 5 ); |
| 137 | VERIFY( A::dtor_count == 0 ); |
| 138 | VERIFY( B::ctor_count == 0 ); |
| 139 | VERIFY( B::dtor_count == 0 ); |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | int |
| 145 | test06() |
| 146 | { |
| 147 | reset_count_struct __attribute__((unused)) reset; |
| 148 | |
| 149 | std::experimental::shared_ptr<B> a1(new B); |
| 150 | std::experimental::shared_ptr<A> a2(a1); |
| 151 | |
| 152 | VERIFY( a2.use_count() == 2 ); |
| 153 | VERIFY( a2.get() == a1.get() ); |
| 154 | VERIFY( A::ctor_count == 1 ); |
| 155 | VERIFY( A::dtor_count == 0 ); |
| 156 | VERIFY( B::ctor_count == 1 ); |
| 157 | VERIFY( B::dtor_count == 0 ); |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | int |
| 163 | main() |
| 164 | { |
| 165 | test01(); |
| 166 | test02(); |
| 167 | test03(); |
| 168 | test04(); |
| 169 | test05(); |
| 170 | test06(); |
| 171 | return 0; |
| 172 | } |