]>
Commit | Line | Data |
---|---|---|
f1717362 | 1 | // Copyright (C) 2005-2016 Free Software Foundation, Inc. |
cf8910b0 | 2 | // |
3 | // This file is part of the GNU ISO C++ Library. This library is free | |
4 | // software; you can redistribute it and/or modify it under the | |
5 | // terms of the GNU General Public License as published by the | |
6bc9506f | 6 | // Free Software Foundation; either version 3, or (at your option) |
cf8910b0 | 7 | // any later version. |
8 | ||
9 | // This library is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | ||
14 | // You should have received a copy of the GNU General Public License along | |
6bc9506f | 15 | // with this library; see the file COPYING3. If not see |
16 | // <http://www.gnu.org/licenses/>. | |
cf8910b0 | 17 | |
18 | // TR1 2.2.2 Template class shared_ptr [tr.util.smartptr.shared] | |
19 | ||
20 | #include <tr1/memory> | |
21 | #include <testsuite_hooks.h> | |
22 | ||
23 | struct A | |
24 | { | |
25 | A() { ++ctor_count; } | |
26 | ~A() { ++dtor_count; } | |
27 | static long ctor_count; | |
28 | static long dtor_count; | |
29 | }; | |
30 | long A::ctor_count = 0; | |
31 | long A::dtor_count = 0; | |
32 | ||
33 | struct B : A | |
34 | { | |
35 | B() { ++ctor_count; } | |
36 | ~B() { ++dtor_count; } | |
37 | static long ctor_count; | |
38 | static long dtor_count; | |
39 | }; | |
40 | long B::ctor_count = 0; | |
41 | long B::dtor_count = 0; | |
42 | ||
43 | struct D | |
44 | { | |
45 | void operator()(const B* p) { delete p; ++delete_count; } | |
46 | static long delete_count; | |
47 | }; | |
48 | long D::delete_count = 0; | |
49 | ||
50 | struct reset_count_struct | |
51 | { | |
52 | ~reset_count_struct() | |
53 | { | |
54 | A::ctor_count = 0; | |
55 | A::dtor_count = 0; | |
56 | B::ctor_count = 0; | |
57 | B::dtor_count = 0; | |
58 | D::delete_count = 0; | |
59 | } | |
60 | }; | |
61 | ||
62 | ||
63 | // 2.2.3.2 shared_ptr destructor [tr.util.smartptr.shared.dest] | |
64 | ||
65 | // empty shared_ptr | |
66 | int | |
67 | test01() | |
68 | { | |
69 | reset_count_struct __attribute__((unused)) reset; | |
70 | bool test __attribute__((unused)) = true; | |
71 | ||
72 | { | |
73 | std::tr1::shared_ptr<A> a; | |
74 | } | |
75 | VERIFY( A::ctor_count == 0 ); | |
76 | VERIFY( A::dtor_count == 0 ); | |
77 | VERIFY( B::ctor_count == 0 ); | |
78 | VERIFY( B::dtor_count == 0 ); | |
79 | VERIFY( D::delete_count == 0 ); | |
80 | ||
81 | return 0; | |
82 | } | |
83 | ||
84 | // shared ownership | |
85 | int | |
86 | test02() | |
87 | { | |
88 | reset_count_struct __attribute__((unused)) reset; | |
89 | bool test __attribute__((unused)) = true; | |
90 | ||
91 | std::tr1::shared_ptr<A> a; | |
92 | { | |
93 | a = std::tr1::shared_ptr<A>(new B, D()); | |
94 | } | |
95 | VERIFY( A::ctor_count == 1 ); | |
96 | VERIFY( A::dtor_count == 0 ); | |
97 | VERIFY( B::ctor_count == 1 ); | |
98 | VERIFY( B::dtor_count == 0 ); | |
99 | VERIFY( D::delete_count == 0 ); | |
100 | ||
101 | return 0; | |
102 | } | |
103 | ||
104 | // exclusive ownership | |
105 | int | |
106 | test03() | |
107 | { | |
108 | reset_count_struct __attribute__((unused)) reset; | |
109 | bool test __attribute__((unused)) = true; | |
110 | ||
111 | { | |
112 | std::tr1::shared_ptr<A> a1(new B); | |
113 | std::tr1::shared_ptr<A> a2(new B, D()); | |
114 | } | |
115 | VERIFY( A::ctor_count == 2 ); | |
116 | VERIFY( A::dtor_count == 2 ); | |
117 | VERIFY( B::ctor_count == 2 ); | |
118 | VERIFY( B::dtor_count == 2 ); | |
119 | VERIFY( D::delete_count == 1 ); | |
120 | ||
121 | return 0; | |
122 | } | |
123 | ||
124 | ||
125 | int | |
126 | main() | |
127 | { | |
128 | test01(); | |
129 | test02(); | |
130 | test03(); | |
131 | return 0; | |
132 | } |