1 /*=========================================================================
3 Program: KWSys - Kitware System Library
4 Module: $RCSfile: testAutoPtr.cxx,v $
6 Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
7 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notices for more information.
13 =========================================================================*/
14 #include "kwsysPrivate.h"
15 #include KWSYS_HEADER(auto_ptr.hxx)
17 // Work-around CMake dependency scanning limitation. This must
18 // duplicate the above list of headers.
20 # include "auto_ptr.hxx.in"
25 #define ASSERT(x,y) if (!(x)) { printf("FAIL: " y "\n"); status = 1; }
27 static int instances
= 0;
33 A
* self() {return this; }
35 struct B
: public A
{};
37 static int function_call(kwsys::auto_ptr
<A
> a
)
42 static A
* get_A(A
& a
) { return &a
; }
44 static kwsys::auto_ptr
<A
> generate_auto_ptr_A()
46 return kwsys::auto_ptr
<A
>(new A
);
49 static kwsys::auto_ptr
<B
> generate_auto_ptr_B()
51 return kwsys::auto_ptr
<B
>(new B
);
54 int testAutoPtr(int, char*[])
58 // Keep everything in a subscope so we can detect leaks.
60 kwsys::auto_ptr
<A
> pa0
;
61 kwsys::auto_ptr
<A
> pa1(new A());
62 kwsys::auto_ptr
<B
> pb1(new B());
63 kwsys::auto_ptr
<B
> pb2(new B());
64 kwsys::auto_ptr
<A
> pa2(new B());
67 ASSERT(ptr
== pa1
.get(),
68 "auto_ptr does not return correct object when dereferenced");
70 ASSERT(ptr
== pa1
.get(),
71 "auto_ptr does not return correct pointer from operator->");
73 A
* before
= pa0
.get();
75 ASSERT(pa0
.get() && pa0
.get() != before
,
76 "auto_ptr empty after reset(new A())");
80 ASSERT(pa0
.get() && pa0
.get() != before
,
81 "auto_ptr empty after reset(new B())");
84 ASSERT(!pa0
.get(), "auto_ptr holds an object after release()");
86 kwsys::auto_ptr
<A
> pa3(pb1
);
88 "auto_ptr full after being used to construct another");
90 "auto_ptr empty after construction from another");
93 kwsys::auto_ptr
<A
> pa
;
96 "auto_ptr full after assignment to another");
98 "auto_ptr empty after assignment from another");
102 kwsys::auto_ptr
<A
> pa
;
105 "auto_ptr full after assignment to compatible");
107 "auto_ptr empty after assignment from compatible");
111 int receive
= function_call(pa2
);
113 "auto_ptr did not receive ownership in called function");
115 "auto_ptr did not release ownership to called function");
119 int received
= function_call(generate_auto_ptr_A());
121 "auto_ptr in called function did not take ownership "
122 "from factory function");
126 // Is this allowed by the standard?
128 int received
= function_call(generate_auto_ptr_B());
130 "auto_ptr in called function did not take ownership "
131 "from factory function with conversion");
136 kwsys::auto_ptr
<A
> pa(generate_auto_ptr_A());
138 "auto_ptr empty after construction from factory function");
142 kwsys::auto_ptr
<A
> pa
;
143 pa
= generate_auto_ptr_A();
145 "auto_ptr empty after assignment from factory function");
149 kwsys::auto_ptr
<A
> pa(generate_auto_ptr_B());
151 "auto_ptr empty after construction from compatible factory function");
155 kwsys::auto_ptr
<A
> pa
;
156 pa
= generate_auto_ptr_B();
158 "auto_ptr empty after assignment from compatible factory function");
162 ASSERT(instances
== 0, "auto_ptr leaked an object");