1 // { dg-options "-std=gnu++11" }
3 // Copyright (C) 2007-2015 Free Software Foundation, Inc.
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)
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.
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/>.
20 // 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
23 #include <testsuite_hooks.h>
24 #include <testsuite_allocator.h>
26 using __gnu_test::tracker_allocator_counter
;
27 using __gnu_test::tracker_allocator
;
30 void deletefunc(A
* p
) { delete p
; }
33 void operator()(A
* p
) { delete p
; ++delete_count
; }
34 static long delete_count
;
36 long D::delete_count
= 0;
38 // 20.6.6.2.1 shared_ptr constructors [util.smartptr.shared.const]
40 // Construction with allocator
44 bool test
__attribute__((unused
)) = true;
45 tracker_allocator_counter::reset();
47 std::shared_ptr
<A
> p1(new A
, deletefunc
, tracker_allocator
<A
>());
48 std::size_t const sz
= tracker_allocator_counter::get_allocation_count();
51 std::shared_ptr
<A
> p2(p1
);
52 VERIFY( p2
.use_count() == 2 );
53 VERIFY( tracker_allocator_counter::get_allocation_count() == sz
);
54 VERIFY( tracker_allocator_counter::get_deallocation_count() == 0 );
56 VERIFY( p1
.use_count() == 1 );
57 VERIFY( tracker_allocator_counter::get_allocation_count() == sz
);
58 VERIFY( tracker_allocator_counter::get_deallocation_count() == 0 );
60 VERIFY( p1
.use_count() == 0 );
61 VERIFY( tracker_allocator_counter::get_allocation_count() == sz
);
62 VERIFY( tracker_allocator_counter::get_deallocation_count() == sz
);
67 // Construction with allocator
71 bool test
__attribute__((unused
)) = true;
72 tracker_allocator_counter::reset();
74 std::shared_ptr
<A
> p1(new A
, deletefunc
, tracker_allocator
<A
>());
75 std::size_t const sz1
= tracker_allocator_counter::get_allocation_count();
77 std::shared_ptr
<A
> p2(new A
, D(), tracker_allocator
<A
>());
78 std::size_t const sz2
= tracker_allocator_counter::get_allocation_count();
80 VERIFY( tracker_allocator_counter::get_deallocation_count() == 0 );
82 VERIFY( p2
.use_count() == 2 );
83 VERIFY( tracker_allocator_counter::get_allocation_count() == sz2
);
84 VERIFY( tracker_allocator_counter::get_deallocation_count() == sz1
);
86 VERIFY( p2
.use_count() == 1 );
87 VERIFY( tracker_allocator_counter::get_allocation_count() == sz2
);
88 VERIFY( tracker_allocator_counter::get_deallocation_count() == sz1
);
90 VERIFY( tracker_allocator_counter::get_allocation_count() == sz2
);
91 VERIFY( tracker_allocator_counter::get_deallocation_count() == sz2
);
92 VERIFY( D::delete_count
== 1 );