| 1 | // { dg-do run { target c++11 } } |
| 2 | // { dg-require-effective-target hosted } |
| 3 | |
| 4 | // Copyright (C) 2010-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.9.11.2 Class template shared_ptr [util.smartptr.shared] |
| 22 | |
| 23 | #include <memory> |
| 24 | #include <cstddef> |
| 25 | #include <testsuite_hooks.h> |
| 26 | #include <testsuite_allocator.h> |
| 27 | |
| 28 | // 20.9.11.2.1 shared_ptr constructors [util.smartptr.shared.const] |
| 29 | |
| 30 | // Construction from nullptr |
| 31 | |
| 32 | struct deleter |
| 33 | { |
| 34 | int count; |
| 35 | deleter() : count(0) { } |
| 36 | void operator()(std::nullptr_t) { ++count; } |
| 37 | void operator()(int*) const { throw "wrong type passed to deleter"; } |
| 38 | }; |
| 39 | |
| 40 | void |
| 41 | test01() |
| 42 | { |
| 43 | std::shared_ptr<int> p = nullptr; |
| 44 | VERIFY( p.get() == nullptr ); |
| 45 | VERIFY( p.use_count() == 0 ); |
| 46 | |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | test02() |
| 51 | { |
| 52 | deleter d; |
| 53 | std::shared_ptr<int> p(nullptr, std::ref(d)); |
| 54 | VERIFY( p.get() == nullptr ); |
| 55 | VERIFY( p.use_count() == 1 ); |
| 56 | |
| 57 | p = nullptr; |
| 58 | VERIFY( p.use_count() == 0 ); |
| 59 | VERIFY( d.count == 1 ); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | void |
| 64 | test03() |
| 65 | { |
| 66 | deleter d; |
| 67 | __gnu_test::tracker_allocator<int> a; |
| 68 | std::shared_ptr<int> p(nullptr, std::ref(d), a); |
| 69 | VERIFY( p.get() == nullptr ); |
| 70 | VERIFY( p.use_count() == 1 ); |
| 71 | |
| 72 | p = nullptr; |
| 73 | VERIFY( p.use_count() == 0 ); |
| 74 | VERIFY( d.count == 1 ); |
| 75 | |
| 76 | typedef __gnu_test::tracker_allocator_counter c; |
| 77 | VERIFY( c::get_destruct_count() == c::get_construct_count() ); |
| 78 | VERIFY( c::get_deallocation_count() == c::get_allocation_count() ); |
| 79 | } |
| 80 | |
| 81 | int |
| 82 | main() |
| 83 | { |
| 84 | test01(); |
| 85 | test02(); |
| 86 | test03(); |
| 87 | return 0; |
| 88 | } |