STYLE: Nightly Date Stamp
[cmake.git] / Source / kwsys / testAutoPtr.cxx
blob5b2ed2854fb9bad3bc4cede069492e76baf062f7
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.
19 #if 0
20 # include "auto_ptr.hxx.in"
21 #endif
23 #include <stdio.h>
25 #define ASSERT(x,y) if (!(x)) { printf("FAIL: " y "\n"); status = 1; }
27 static int instances = 0;
29 struct A
31 A() { ++instances; }
32 ~A() { --instances; }
33 A* self() {return this; }
35 struct B: public A {};
37 static int function_call(kwsys::auto_ptr<A> a)
39 return a.get()? 1:0;
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*[])
56 int status = 0;
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());
66 A* ptr = get_A(*pa1);
67 ASSERT(ptr == pa1.get(),
68 "auto_ptr does not return correct object when dereferenced");
69 ptr = pa1->self();
70 ASSERT(ptr == pa1.get(),
71 "auto_ptr does not return correct pointer from operator->");
73 A* before = pa0.get();
74 pa0.reset(new A());
75 ASSERT(pa0.get() && pa0.get() != before,
76 "auto_ptr empty after reset(new A())");
78 before = pa0.get();
79 pa0.reset(new B());
80 ASSERT(pa0.get() && pa0.get() != before,
81 "auto_ptr empty after reset(new B())");
83 delete pa0.release();
84 ASSERT(!pa0.get(), "auto_ptr holds an object after release()");
86 kwsys::auto_ptr<A> pa3(pb1);
87 ASSERT(!pb1.get(),
88 "auto_ptr full after being used to construct another");
89 ASSERT(pa3.get(),
90 "auto_ptr empty after construction from another");
93 kwsys::auto_ptr<A> pa;
94 pa = pa3;
95 ASSERT(!pa3.get(),
96 "auto_ptr full after assignment to another");
97 ASSERT(pa.get(),
98 "auto_ptr empty after assignment from another");
102 kwsys::auto_ptr<A> pa;
103 pa = pb2;
104 ASSERT(!pb2.get(),
105 "auto_ptr full after assignment to compatible");
106 ASSERT(pa.get(),
107 "auto_ptr empty after assignment from compatible");
111 int receive = function_call(pa2);
112 ASSERT(receive,
113 "auto_ptr did not receive ownership in called function");
114 ASSERT(!pa2.get(),
115 "auto_ptr did not release ownership to called function");
119 int received = function_call(generate_auto_ptr_A());
120 ASSERT(received,
121 "auto_ptr in called function did not take ownership "
122 "from factory function");
125 #if 0
126 // Is this allowed by the standard?
128 int received = function_call(generate_auto_ptr_B());
129 ASSERT(received,
130 "auto_ptr in called function did not take ownership "
131 "from factory function with conversion");
133 #endif
136 kwsys::auto_ptr<A> pa(generate_auto_ptr_A());
137 ASSERT(pa.get(),
138 "auto_ptr empty after construction from factory function");
142 kwsys::auto_ptr<A> pa;
143 pa = generate_auto_ptr_A();
144 ASSERT(pa.get(),
145 "auto_ptr empty after assignment from factory function");
149 kwsys::auto_ptr<A> pa(generate_auto_ptr_B());
150 ASSERT(pa.get(),
151 "auto_ptr empty after construction from compatible factory function");
155 kwsys::auto_ptr<A> pa;
156 pa = generate_auto_ptr_B();
157 ASSERT(pa.get(),
158 "auto_ptr empty after assignment from compatible factory function");
162 ASSERT(instances == 0, "auto_ptr leaked an object");
164 return status;