1 // { dg-options "-std=gnu++0x" }
3 // Copyright (C) 2007, 2008 Free Software Foundation
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 2, 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 COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
24 #include <testsuite_hooks.h>
25 #include <testsuite_allocator.h>
27 using __gnu_test::tracker_allocator_counter
;
28 using __gnu_test::tracker_allocator
;
32 A(int i
, double d
, char c
= '\0') : i(i
), d(d
), c(c
) { ++ctor_count
; }
33 explicit A(int i
) : i(i
), d(), c() { ++ctor_count
; }
34 A() : i(), d(), c() { ++ctor_count
; }
35 ~A() { ++dtor_count
; }
39 static int ctor_count
;
40 static int dtor_count
;
42 int A::ctor_count
= 0;
43 int A::dtor_count
= 0;
45 struct reset_count_struct
51 tracker_allocator_counter::reset();
55 // 20.6.6.2.6 shared_ptr creation [util.smartptr.shared.create]
60 bool test
__attribute__((unused
)) = true;
61 reset_count_struct
__attribute__((unused
)) reset
;
64 std::shared_ptr
<A
> p1
= std::allocate_shared
<A
>(tracker_allocator
<A
>());
65 VERIFY( p1
.get() != 0 );
66 VERIFY( p1
.use_count() == 1 );
67 VERIFY( A::ctor_count
== 1 );
68 VERIFY( tracker_allocator_counter::get_allocation_count() > 0 );
70 VERIFY( A::ctor_count
== A::dtor_count
);
71 VERIFY( tracker_allocator_counter::get_allocation_count()
72 == tracker_allocator_counter::get_deallocation_count() );
78 bool test
__attribute__((unused
)) = true;
79 reset_count_struct
__attribute__((unused
)) reset
;
81 std::shared_ptr
<A
> p1
;
83 p1
= std::allocate_shared
<A
>(tracker_allocator
<A
>(), 1);
84 VERIFY( A::ctor_count
== 1 );
85 VERIFY( tracker_allocator_counter::get_allocation_count() > 0 );
87 p1
= std::allocate_shared
<A
>(tracker_allocator
<A
>(), 1, 2.0);
88 VERIFY( A::ctor_count
== 2 );
89 VERIFY( A::dtor_count
== 1 );
90 VERIFY( tracker_allocator_counter::get_deallocation_count() > 0 );
92 p1
= std::allocate_shared
<A
>(tracker_allocator
<A
>(), 1, 2.0, '3');
93 VERIFY( A::ctor_count
== 3 );
94 VERIFY( A::dtor_count
== 2 );
96 VERIFY( p1
->d
== 2.0 );
97 VERIFY( p1
->c
== '3' );
99 p1
= std::shared_ptr
<A
>();
100 VERIFY( A::ctor_count
== A::dtor_count
);
101 VERIFY( tracker_allocator_counter::get_allocation_count()
102 == tracker_allocator_counter::get_deallocation_count() );