1 // Copyright (C) 2000, 2002, 2003 Free Software Foundation
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
6 // Free Software Foundation; either version 2, or (at your option)
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.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING. If not, write to the Free
16 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // 20.4.5 Template class auto_ptr [lib.auto.ptr]
22 #include <testsuite_hooks.h>
27 virtual ~A() { ++dtor_count
; }
28 static long ctor_count
;
29 static long dtor_count
;
31 long A::ctor_count
= 0;
32 long A::dtor_count
= 0;
37 virtual ~B() { ++dtor_count
; }
38 static long ctor_count
;
39 static long dtor_count
;
41 long B::ctor_count
= 0;
42 long B::dtor_count
= 0;
45 struct reset_count_struct
57 // 20.4.5.1 auto_ptr constructors [lib.auto.ptr.cons]
59 // Construction from pointer
63 reset_count_struct
__attribute__((unused
)) reset
;
64 bool test
__attribute__((unused
)) = true;
66 std::auto_ptr
<A
> A_default
;
67 VERIFY( A_default
.get() == 0 );
68 VERIFY( A::ctor_count
== 0 );
69 VERIFY( A::dtor_count
== 0 );
70 VERIFY( B::ctor_count
== 0 );
71 VERIFY( B::dtor_count
== 0 );
73 std::auto_ptr
<A
> A_from_A(new A
);
74 VERIFY( A_from_A
.get() != 0 );
75 VERIFY( A::ctor_count
== 1 );
76 VERIFY( A::dtor_count
== 0 );
77 VERIFY( B::ctor_count
== 0 );
78 VERIFY( B::dtor_count
== 0 );
80 std::auto_ptr
<A
> A_from_B(new B
);
81 VERIFY( A_from_B
.get() != 0 );
82 VERIFY( A::ctor_count
== 2 );
83 VERIFY( A::dtor_count
== 0 );
84 VERIFY( B::ctor_count
== 1 );
85 VERIFY( B::dtor_count
== 0 );
90 // Construction from std::auto_ptr
94 reset_count_struct
__attribute__((unused
)) reset
;
95 bool test
__attribute__((unused
)) = true;
97 std::auto_ptr
<A
> A_from_A(new A
);
98 std::auto_ptr
<B
> B_from_B(new B
);
100 std::auto_ptr
<A
> A_from_ptr_A(A_from_A
);
101 std::auto_ptr
<A
> A_from_ptr_B(B_from_B
);
102 VERIFY( A_from_A
.get() == 0 );
103 VERIFY( B_from_B
.get() == 0 );
104 VERIFY( A_from_ptr_A
.get() != 0 );
105 VERIFY( A_from_ptr_B
.get() != 0 );
106 VERIFY( A::ctor_count
== 2 );
107 VERIFY( A::dtor_count
== 0 );
108 VERIFY( B::ctor_count
== 1 );
109 VERIFY( B::dtor_count
== 0 );
114 // Assignment from std::auto_ptr
118 reset_count_struct
__attribute__((unused
)) reset
;
119 bool test
__attribute__((unused
)) = true;
121 std::auto_ptr
<A
> A_from_ptr_A
;
122 std::auto_ptr
<A
> A_from_ptr_B
;
123 std::auto_ptr
<A
> A_from_A(new A
);
124 std::auto_ptr
<B
> B_from_B(new B
);
126 A_from_ptr_A
= A_from_A
;
127 A_from_ptr_B
= B_from_B
;
128 VERIFY( A_from_A
.get() == 0 );
129 VERIFY( B_from_B
.get() == 0 );
130 VERIFY( A_from_ptr_A
.get() != 0 );
131 VERIFY( A_from_ptr_B
.get() != 0 );
132 VERIFY( A::ctor_count
== 2 );
133 VERIFY( A::dtor_count
== 0 );
134 VERIFY( B::ctor_count
== 1 );
135 VERIFY( B::dtor_count
== 0 );
144 reset_count_struct
__attribute__((unused
)) reset
;
145 bool test
__attribute__((unused
)) = true;
148 std::auto_ptr
<A
> A_from_A(new A
);
149 std::auto_ptr
<A
> A_from_B(new B
);
150 std::auto_ptr
<B
> B_from_B(new B
);
151 }/*destructors called here*/
153 VERIFY( A::ctor_count
== 3 );
154 VERIFY( A::dtor_count
== 3 );
155 VERIFY( B::ctor_count
== 2 );
156 VERIFY( B::dtor_count
== 2 );
161 // Class member construction/destruction
162 template <typename T
>
166 pimpl() : p_impl(new T
) {}
168 std::auto_ptr
<T
> p_impl
;
174 bool test
__attribute__((unused
)) = true;
175 reset_count_struct
__attribute__((unused
)) reset
;
179 VERIFY( A::ctor_count
== 2 );
180 VERIFY( A::dtor_count
== 2 );
181 VERIFY( B::ctor_count
== 1 );
182 VERIFY( B::dtor_count
== 1 );
187 // 20.4.5.2 auto_ptr members [lib.auto.ptr.members]
193 reset_count_struct
__attribute__((unused
)) reset
;
194 bool test
__attribute__((unused
)) = true;
196 std::auto_ptr
<A
> A_from_A(new A
);
197 std::auto_ptr
<A
> A_from_A_ptr(A_from_A
.release());
198 VERIFY( A_from_A
.get() == 0 );
199 VERIFY( A_from_A_ptr
.get() != 0 );
200 VERIFY( A_from_A_ptr
->ctor_count
== 1 );
201 VERIFY( (*A_from_A_ptr
).dtor_count
== 0 );
203 A
* A_ptr
= A_from_A_ptr
.get();
205 A_from_A_ptr
.reset(A_ptr
);
206 VERIFY( A_from_A_ptr
.get() == A_ptr
);
207 VERIFY( A_from_A_ptr
->ctor_count
== 1 );
208 VERIFY( (*A_from_A_ptr
).dtor_count
== 0 );
210 A_from_A_ptr
.reset(new A
);
211 VERIFY( A_from_A_ptr
.get() != A_ptr
);
212 VERIFY( A_from_A_ptr
->ctor_count
== 2 );
213 VERIFY( (*A_from_A_ptr
).dtor_count
== 1 );
218 // 20.4.5.3 auto_ptr conversions [lib.auto.ptr.conv]
220 // Parameters and return values
221 template <typename T
>
222 static std::auto_ptr
<T
> source()
224 return std::auto_ptr
<T
>(new T
);
227 template <typename T
>
228 static void drain(std::auto_ptr
<T
>)
234 bool test
__attribute__((unused
)) = true;
235 reset_count_struct
__attribute__((unused
)) reset
;
238 // The resolution of core issue 84, now a DR, breaks this call.
239 // drain<A>(source<B>());
241 VERIFY( A::ctor_count
== 2 );
242 VERIFY( A::dtor_count
== 2 );
243 VERIFY( B::ctor_count
== 1 );
244 VERIFY( B::dtor_count
== 1 );
249 // http://gcc.gnu.org/ml/libstdc++/2002-07/msg00024.html
251 struct Derived
: public Base
{ };
253 std::auto_ptr
<Derived
>
254 conversiontest08() { return std::auto_ptr
<Derived
>(new Derived
); }
259 std::auto_ptr
<Base
> ptr
;
260 ptr
= conversiontest08();