1 /*! ========================================================================
2 ** Extended Template and Library Test Suite
3 ** Smart Pointer Template Class Test
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** General Public License for more details.
18 ** === N O T E S ===========================================================
20 ** ========================================================================= */
22 //#define DEBUGPOINT() fprintf(stderr,__FILE__":%d: DEBUGPOINT\n",__LINE__)
25 /* === H E A D E R S ======================================================= */
27 #include <ETL/smart_ptr>
34 /* === M A C R O S ========================================================= */
36 #define NUMBER_OF_OBJECTS 40000
40 /* === C L A S S E S ======================================================= */
44 static int instance_count;
46 my_test_obj(int my_id=0):my_id(my_id)
51 virtual ~my_test_obj()
54 printf("Error, instance count is going past zero!\n");
58 bool operator<(const my_test_obj &rhs)const
60 return my_id<rhs.my_id;
64 struct my_other_test_obj : public my_test_obj
66 static int instance_count;
67 my_other_test_obj(int my_id=0):my_test_obj(my_id)
71 virtual ~my_other_test_obj()
74 printf("Error, instance count is going past zero!\n");
79 int my_test_obj::instance_count=0;
80 int my_other_test_obj::instance_count=0;
82 typedef etl::smart_ptr<my_test_obj> obj_smart_ptr;
83 typedef etl::smart_ptr<my_other_test_obj> other_obj_smart_ptr;
84 typedef list< obj_smart_ptr > obj_list;
85 typedef list< other_obj_smart_ptr > other_obj_list;
87 int smart_ptr_basic_test(void)
89 printf("smart_ptr: Size of a smart_ptr: %u\n",(unsigned int)sizeof(obj_smart_ptr));
90 printf("smart_ptr: Size of a reference_counter: %u\n",(unsigned int)sizeof(etl::reference_counter));
93 printf("smart_ptr: Basic test: ");
94 my_test_obj::instance_count=0;
97 etl::smart_ptr<my_test_obj> obj_smart_ptr(new my_test_obj(rand()));
100 if(my_test_obj::instance_count!=0)
103 printf(__FILE__":%d: on create/distroy, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count);
109 map<string,etl::smart_ptr<my_test_obj> > my_map;
111 //etl::smart_ptr<my_test_obj> obj_smart_ptr(new my_test_obj(rand()));
112 etl::smart_ptr<my_test_obj> temp;
121 if(my_test_obj::instance_count!=0)
124 printf(__FILE__":%d: on create/distroy, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count);
128 etl::smart_ptr<my_test_obj> obj_smart_ptr(new my_test_obj(rand()));
130 if(obj_smart_ptr != obj_smart_ptr.constant())
133 printf(__FILE__":%d: on call to smart_ptr<>::constant().\n",__LINE__);
142 int smart_ptr_general_use_test(void)
144 printf("smart_ptr: General-use test: ");
145 my_test_obj::instance_count=0;
147 obj_list my_list, my_other_list;
150 for(i=0;i<NUMBER_OF_OBJECTS;i++)
151 my_list.push_back( obj_smart_ptr(new my_test_obj(rand())) );
153 my_other_list=my_list;
154 if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS)
157 printf(__FILE__":%d: On copy, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS);
162 if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS)
165 printf(__FILE__":%d: On copy, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS);
170 if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS)
173 printf(__FILE__":%d: On copy's clear, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS);
177 my_other_list.clear();
178 if(my_test_obj::instance_count)
181 printf(__FILE__":%d: On clear, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count);
190 int smart_ptr_inheritance_test(void)
192 printf("smart_ptr: Inheritance test: ");
193 my_test_obj::instance_count=0;
194 my_other_test_obj::instance_count=0;
196 other_obj_list my_other_list;
199 for(i=0;i<NUMBER_OF_OBJECTS;i++)
200 my_other_list.push_back( other_obj_smart_ptr(new my_other_test_obj(rand())) );
202 obj_list my_list(my_other_list.begin(),my_other_list.end());
203 if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS)
206 printf(__FILE__":%d: On copy, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS);
210 for(i=0;i<NUMBER_OF_OBJECTS;i++)
211 my_list.push_back( other_obj_smart_ptr(new my_other_test_obj(rand())) );
212 if(my_other_test_obj::instance_count!=NUMBER_OF_OBJECTS*2 ||
213 my_test_obj::instance_count!=my_other_test_obj::instance_count)
216 printf(__FILE__":%d: On inherited copy, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS*2);
221 my_other_list.sort();
222 if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS*2)
225 printf(__FILE__":%d: On sort, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS*2);
230 if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS)
233 printf(__FILE__":%d: On clear, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS);
237 my_other_list.clear();
238 if(my_test_obj::instance_count)
241 printf(__FILE__":%d: On clear, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count);
250 void test_func(etl::smart_ptr<my_test_obj> smart_ptr)
254 int loose_smart_ptr_test(void)
256 printf("smart_ptr: loose_smart_ptr test: ");
257 my_test_obj::instance_count=0;
259 etl::loose_smart_ptr<my_test_obj> obj_smart_ptr_loose;
260 etl::smart_ptr<my_test_obj> obj_smart_ptr2;
263 etl::smart_ptr<my_test_obj> obj_smart_ptr(new my_test_obj(rand()));
264 if(my_test_obj::instance_count!=1)
267 printf(__FILE__":%d: on smart_ptr assignment from new object, instance count=%d, should be 1.\n",__LINE__,my_test_obj::instance_count);
271 obj_smart_ptr_loose=obj_smart_ptr;
272 if(obj_smart_ptr!=obj_smart_ptr_loose)
275 printf(__FILE__":%d: on loose_smart_ptr assignment\n",__LINE__);
279 obj_smart_ptr2=obj_smart_ptr_loose;
280 if(my_test_obj::instance_count!=1)
283 printf(__FILE__":%d: on smart_ptr assignment from loose_smart_ptr, instance count=%d, should be 1.\n",__LINE__,my_test_obj::instance_count);
287 test_func(obj_smart_ptr_loose);
288 if(my_test_obj::instance_count!=1)
291 printf(__FILE__":%d: on smart_ptr assignment from loose_smart_ptr, instance count=%d, should be 1.\n",__LINE__,my_test_obj::instance_count);
296 if(my_test_obj::instance_count!=1)
299 printf(__FILE__":%d: on create/destroy, instance count=%d, should be 1.\n",__LINE__,my_test_obj::instance_count);
307 /* === E N T R Y P O I N T ================================================= */
313 error+=smart_ptr_basic_test();
314 error+=smart_ptr_general_use_test();
315 error+=smart_ptr_inheritance_test();
316 error+=loose_smart_ptr_test();