2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libstdc++-v3 / testsuite / 20_util / auto_ptr.cc
blob110d4b2d4bf055a20838979be58fc48a5faa8f6b
1 // Copyright (C) 2000, 2002, 2003 Free Software Foundation
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
6 // Free Software Foundation; either version 2, or (at your option)
7 // any later version.
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,
17 // USA.
19 // 20.4.5 Template class auto_ptr [lib.auto.ptr]
21 #include <memory>
22 #include <testsuite_hooks.h>
24 struct A
26 A() { ++ctor_count; }
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;
34 struct B : A
36 B() { ++ctor_count; }
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
47 ~reset_count_struct()
49 A::ctor_count = 0;
50 A::dtor_count = 0;
51 B::ctor_count = 0;
52 B::dtor_count = 0;
57 // 20.4.5.1 auto_ptr constructors [lib.auto.ptr.cons]
59 // Construction from pointer
60 int
61 test01()
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 );
87 return 0;
90 // Construction from std::auto_ptr
91 int
92 test02()
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 );
111 return 0;
114 // Assignment from std::auto_ptr
116 test03()
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 );
137 return 0;
140 // Destruction
142 test04()
144 reset_count_struct __attribute__((unused)) reset;
145 bool test __attribute__((unused)) = true;
147 {/*lifetine scope*/
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 );
158 return 0;
161 // Class member construction/destruction
162 template <typename T>
163 class pimpl
165 public:
166 pimpl() : p_impl(new T) {}
167 private:
168 std::auto_ptr<T> p_impl;
171 int
172 test05()
174 bool test __attribute__((unused)) = true;
175 reset_count_struct __attribute__((unused)) reset;
177 pimpl<A>();
178 pimpl<B>();
179 VERIFY( A::ctor_count == 2 );
180 VERIFY( A::dtor_count == 2 );
181 VERIFY( B::ctor_count == 1 );
182 VERIFY( B::dtor_count == 1 );
183 return 0;
187 // 20.4.5.2 auto_ptr members [lib.auto.ptr.members]
189 // Member access
191 test06()
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 );
214 return 0;
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>)
232 test07()
234 bool test __attribute__((unused)) = true;
235 reset_count_struct __attribute__((unused)) reset;
237 drain(source<A>());
238 // The resolution of core issue 84, now a DR, breaks this call.
239 // drain<A>(source<B>());
240 drain(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 );
245 return 0;
248 // libstdc++/3946
249 // http://gcc.gnu.org/ml/libstdc++/2002-07/msg00024.html
250 struct Base { };
251 struct Derived : public Base { };
253 std::auto_ptr<Derived>
254 conversiontest08() { return std::auto_ptr<Derived>(new Derived); }
256 void
257 test08()
259 std::auto_ptr<Base> ptr;
260 ptr = conversiontest08();
264 int
265 main()
267 test01();
268 test02();
269 test03();
270 test04();
271 test05();
272 test06();
273 test07();
274 test08();
276 return 0;