33 #include <Base/Common/SharedPtr.hh>
42 const bool verbose =
false;
49 parent() : data(0.0) {}
50 parent(
const double d) : data(d) {}
53 virtual void printData()
54 { cout <<
"parent::printData: "<<data<<endl; }
58 class child :
public parent
62 child(
const double d) : parent(d) {}
64 virtual void printData()
65 { cout <<
"child::printData: "<<data<<endl; }
69 ostream&
operator<<(ostream& os,
const parent & f)
70 { os <<
"parent = "<<f.data;
return os; }
73 ostream&
operator<<(ostream& os,
const child & f)
74 { os <<
"child = "<<f.data;
return os; }
79 void TestConstructors()
87 #ifdef TEST_COMPILE_TIME_ERROR_Delete
96 const double value(2.0);
98 if ((*sp).data != value) { BIASABORT; }
99 if (sp->data != value) { BIASABORT; }
101 cout <<
"Get: "<<
Get(sp)<<endl;
102 cout <<
"RefCount: "<<
RefCount(sp)<<endl;
106 if (verbose) cout << foo <<
"\t" << foo <<
" rc "<<
RefCount(foo)<<endl;
108 if (verbose) cout << foo <<
"\t" << *foo <<
" rc "<<
RefCount(foo)<<endl;
110 if (verbose) cout << foo <<
"\t" << *foo <<
" rc "<<
RefCount(foo)<<endl;
115 void TestConversionToBool()
121 if (parent1){ BIASABORT; }
122 if (parent2){}
else { BIASABORT; }
125 if (!parent1){}
else { BIASABORT; }
126 if (!parent2){ BIASABORT; }
130 void TestComparison()
132 parent *raw_1 =
new parent(1.0);
133 parent *raw_2 =
new parent(2.0);
135 child *raw_child =
new child(3.0);
144 if (shared_1 == shared_1b) {}
else { BIASABORT; }
145 if (shared_1 != shared_1b) { BIASABORT; }
146 if (shared_1 == shared_2) { BIASABORT; }
147 if (shared_1 != shared_2) {}
else { BIASABORT; }
150 if (raw_1 ==
Get(shared_1)) {}
else { BIASABORT; }
151 if (raw_1 !=
Get(shared_1)) { BIASABORT; }
154 #ifdef TEST_COMPILE_TIME_ERROR_CompareApplesAnOrangesEqual
155 if (shared_int == shared_1) { BIASABORT; }
157 #ifdef TEST_COMPILE_TIME_ERROR_CompareApplesAnOrangesNotEqual
158 if (shared_int != shared_1) {}
else { BIASABORT; }
171 if (
Get(ptr1)==
Get(ptr2)){ BIASABORT; }
173 if (!(ptr1<ptr2)) { BIASABORT; }
174 if (ptr1>=ptr2) { BIASABORT; }
176 if (!(ptr1>ptr2)) { BIASABORT; }
177 if (ptr1<=ptr2) { BIASABORT; }
181 if (*ptr1>=*ptr2) { BIASABORT; }
185 void TestReferenceCounting()
191 if (verbose) cout <<
"reference count is "<<
RefCount(ptr2)<<
" expected 1\n";
192 if (
RefCount(ptr2)!=1u) { BIASABORT; }
195 if (
RefCount(ptr2)!=2u) { BIASABORT; }
196 if (
RefCount(ptr1)!=2u) { BIASABORT; }
199 if (
RefCount(ptr2)!=3u) { BIASABORT; }
200 if (
RefCount(ptr1)!=3u) { BIASABORT; }
204 if (
RefCount(ptr1)!=1u) { BIASABORT; }
212 cout <<
"ptr1: "<<ptr1<<
" = "<<*ptr1<<endl;
214 cout <<
"ptr2: "<<ptr2<<endl;
228 #ifdef TEST_COMPILE_TIME_ERROR_DereferencePtr2Const
230 *var_ptr_2_const = 8;
235 #ifdef TEST_COMPILE_TIME_ERROR_InitFromGenericPointer
237 const_ptr_2_var =
new int(7);
239 *const_ptr_2_var = 8;
243 #ifdef TEST_COMPILE_TIME_ERROR_InitFromGenericPointerConst
245 const_ptr_2_const =
new int(6);
247 #ifdef TEST_COMPILE_TIME_ERROR_DereferenceConstPtr2Const
249 *const_ptr_2_const = 7;
256 #ifdef TEST_COMPILE_TIME_ERROR_ConstCast
265 std::vector<SharedPtr<int> > vec(1);
270 for (
unsigned i=0; i<vec.size(); i++){
271 cout << i <<
": "<<(vec[i])<<
"\tref count: "<<
RefCount(vec[i])<<endl;
274 std::vector<SharedPtr<int> > vec2(5, foo);
277 for (
unsigned i=0; i<vec2.size(); i++){
278 cout << i <<
": "<<(vec2[i])<<
"\tref count: "<<
RefCount(vec2[i])<<endl;
287 virtual double get() {
return a;}
294 class Child :
public Father {
296 virtual double get() {
return b;}
302 class Child2 :
public Father {
304 virtual double get() {
return c;}
316 f2 = DynamicCast<Father>(c);
317 if (f2) {}
else { BIASABORT; }
319 c2 = DynamicCast<Child>(cc);
320 if (c2) { BIASABORT; }
323 cout <<
"ref count cast f2: "<<
RefCount(f2)<<
" (expected 2)"<<endl;
324 cout <<
"ref count cast c : "<<
RefCount(c) <<
" (expected 2)"<<endl;
325 cout <<
"ref count cast f : "<<
RefCount(f) <<
" (expected 1)"<<endl;
326 cout <<
"ref count cast c2: "<<
RefCount(c2)<<
" (expected 0)"<<endl;
327 cout <<
"ref count cast c : "<<
RefCount(cc)<<
" (expected 1)"<<endl;
332 f2 = ConstCast<Father>(f3);
333 if (f2) {}
else { BIASABORT; };
334 f3 = ConstCast<const Father>(f);
335 if (f3) {}
else { BIASABORT; };
337 #ifdef TEST_COMPILE_TIME_ERROR_ConstCastTypeMismatch
339 f2 = ConstCast<Father>(c);
344 void TestAutomaticCast()
352 const_father = child;
353 const_father = const_child;
355 #ifdef TEST_COMPILE_TIME_ERROR_AutocastConst
356 child = const_father;
365 TestConversionToBool();
368 TestReferenceCounting();
T * Get(SharedPtr< T > &t)
pointer with reference count and automatic deletion
unsigned RefCount(const SharedPtr< T > &t)
std::ostream & operator<<(std::ostream &os, const Array2D< T > &arg)