1 /*! ========================================================================
2 ** Extended Template and Library Test Suite
3 ** Handle 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 /* === H E A D E R S ======================================================= */
32 /* === M A C R O S ========================================================= */
34 #define NUMBER_OF_OBJECTS 40000
37 /* === C L A S S E S ======================================================= */
39 struct my_test_obj : public etl::rshared_object
41 static int instance_count;
43 my_test_obj(int my_id=0):my_id(my_id)
48 virtual ~my_test_obj()
51 printf("Error, instance count is going past zero!\n");
55 bool operator<(const my_test_obj &rhs)const
57 return my_id<rhs.my_id;
61 struct my_other_test_obj : public my_test_obj
63 static int instance_count;
64 my_other_test_obj(int my_id=0):my_test_obj(my_id)
68 virtual ~my_other_test_obj()
71 printf("Error, instance count is going past zero!\n");
76 int my_test_obj::instance_count=0;
77 int my_other_test_obj::instance_count=0;
79 typedef etl::handle<my_test_obj> obj_handle;
80 typedef etl::rhandle<my_test_obj> robj_handle;
81 typedef etl::handle<my_other_test_obj> other_obj_handle;
82 typedef list< obj_handle > obj_list;
83 typedef list< other_obj_handle > other_obj_list;
84 typedef list< robj_handle > robj_list;
86 int handle_basic_test()
88 printf("handle: Size of a handle: %u\n",(unsigned int)sizeof(etl::handle<int>));
89 printf("handle: Size of a loose_handle: %u\n",(unsigned int)sizeof(etl::loose_handle<int>));
90 printf("handle: Size of a rhandle: %u\n",(unsigned int)sizeof(etl::rhandle<int>));
91 printf("handle: Size of a shared_object: %u\n",(unsigned int)sizeof(etl::shared_object));
92 printf("handle: Size of a rshared_object: %u\n",(unsigned int)sizeof(etl::rshared_object));
94 printf("handle: Basic test: ");
95 my_test_obj::instance_count=0;
98 etl::handle<my_test_obj> obj_handle(new my_test_obj(rand()));
101 if(my_test_obj::instance_count!=0)
104 printf(__FILE__":%d: on create/distroy, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count);
109 map<string,etl::handle<my_test_obj> > my_map;
110 etl::handle<my_test_obj> obj_handle(new my_test_obj(rand()));
111 my_map["bleh"]=obj_handle;
114 if(my_test_obj::instance_count!=0)
117 printf(__FILE__":%d: on create/distroy, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count);
121 etl::handle<my_test_obj> obj_handle(new my_test_obj(rand()));
123 if(obj_handle != obj_handle.constant())
126 printf(__FILE__":%d: on call to handle<>::constant().\n",__LINE__);
135 int handle_general_use_test(void)
137 printf("handle: General-use test: ");
138 my_test_obj::instance_count=0;
140 obj_list my_list, my_other_list;
143 for(i=0;i<NUMBER_OF_OBJECTS;i++)
144 my_list.push_back( obj_handle(new my_test_obj(rand())) );
146 my_other_list=my_list;
147 if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS)
150 printf(__FILE__":%d: On copy, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS);
155 if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS)
158 printf(__FILE__":%d: On copy, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS);
163 if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS)
166 printf(__FILE__":%d: On copy's clear, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS);
170 my_other_list.clear();
171 if(my_test_obj::instance_count)
174 printf(__FILE__":%d: On clear, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count);
188 ListItem(robj_handle obj,int bleh=1, int blah=2):
189 obj(obj),bleh(bleh),blah(blah) { }
192 int rhandle_general_use_test(void)
196 printf("rhandle: General-use test: ");
197 my_test_obj::instance_count=0;
202 robj_handle obj= new my_test_obj(rand());
203 for(i=0;i<NUMBER_OF_OBJECTS;i++)
204 my_list.push_back(obj);
206 obj_list my_other_list(my_list.begin(),my_list.end());
210 if(obj.count()!=NUMBER_OF_OBJECTS*2+1)
213 printf(__FILE__":%d: On copy, handle count=%d, should be %d.\n",__LINE__,obj.count(),NUMBER_OF_OBJECTS*2+1);
217 if(obj.rcount()!=NUMBER_OF_OBJECTS+1)
220 printf(__FILE__":%d: On copy, rhandle count=%d, should be %d.\n",__LINE__,obj.rcount(),NUMBER_OF_OBJECTS+1);
225 if(obj.rcount()!=NUMBER_OF_OBJECTS+1)
228 printf(__FILE__":%d: On copy, instance count=%d, should be %d.\n",__LINE__,obj.rcount(),NUMBER_OF_OBJECTS+1);
232 {robj_handle bleh(obj);}
234 if(obj.rcount()!=NUMBER_OF_OBJECTS+1)
237 printf(__FILE__":%d: On copy, instance count=%d, should be %d.\n",__LINE__,obj.rcount(),NUMBER_OF_OBJECTS+1);
241 my_other_list.clear();
243 if(obj.rcount()!=obj.count())
246 printf(__FILE__":%d: On copy's clear, handle count (%d) != rhandle count (%d)\n",__LINE__,obj.count(),obj.rcount());
250 if(obj.rcount()!=NUMBER_OF_OBJECTS+1)
253 printf(__FILE__":%d: On copy's clear, instance count=%d, should be %d.\n",__LINE__,obj.rcount(),NUMBER_OF_OBJECTS+1);
257 robj_handle new_obj= new my_test_obj(rand());
259 int replacements=obj.replace(new_obj);
261 if(replacements!=NUMBER_OF_OBJECTS+1)
264 printf(__FILE__":%d: Only managed to replace %d, should have replaced %d\n",__LINE__,replacements,NUMBER_OF_OBJECTS+1);
271 printf(__FILE__":%d: On replace, handles should be equal.\n",__LINE__);
276 robj_handle bleh(obj);
277 robj_handle blah(obj.get());
285 if(my_test_obj::instance_count)
288 printf(__FILE__":%d: On clear, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count);
294 std::vector<ListItem> my_item_list;
295 for(i=0;i<NUMBER_OF_OBJECTS;i++)
296 my_item_list.push_back(ListItem(new my_test_obj(rand()),3,4));
302 src=rand()%NUMBER_OF_OBJECTS;
303 dest=rand()%NUMBER_OF_OBJECTS;
304 ListItem tmp(my_item_list[src]);
305 assert(tmp.obj.rcount()>=2);
306 my_item_list.erase(my_item_list.begin()+src);
307 assert(tmp.obj.rcount()>=1);
308 my_item_list.insert(my_item_list.begin()+dest,tmp);
309 assert(tmp.obj.rcount()>=2);
312 my_item_list.clear();
314 if(my_test_obj::instance_count)
317 printf(__FILE__":%d: On clear, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count);
326 int handle_inheritance_test(void)
328 printf("handle: Inheritance test: ");
329 my_test_obj::instance_count=0;
330 my_other_test_obj::instance_count=0;
332 other_obj_list my_other_list;
335 for(i=0;i<NUMBER_OF_OBJECTS;i++)
336 my_other_list.push_back( other_obj_handle(new my_other_test_obj(rand())) );
338 obj_list my_list(my_other_list.begin(),my_other_list.end());
339 if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS)
342 printf(__FILE__":%d: On copy, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS);
346 for(i=0;i<NUMBER_OF_OBJECTS;i++)
347 my_list.push_back( other_obj_handle(new my_other_test_obj(rand())) );
348 if(my_other_test_obj::instance_count!=NUMBER_OF_OBJECTS*2 ||
349 my_test_obj::instance_count!=my_other_test_obj::instance_count)
352 printf(__FILE__":%d: On inherited copy, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS*2);
357 my_other_list.sort();
358 if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS*2)
361 printf(__FILE__":%d: On sort, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS*2);
366 if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS)
369 printf(__FILE__":%d: On clear, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS);
373 my_other_list.clear();
374 if(my_test_obj::instance_count)
377 printf(__FILE__":%d: On clear, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count);
386 void test_func(etl::handle<my_test_obj> handle)
388 if(handle) { int i=handle.count(); i++; }
391 int loose_handle_test(void)
393 printf("handle: loose_handle test: ");
394 my_test_obj::instance_count=0;
396 etl::loose_handle<my_test_obj> obj_handle_loose;
397 etl::handle<my_test_obj> obj_handle2;
400 etl::handle<my_test_obj> obj_handle(new my_test_obj(rand()));
401 if(my_test_obj::instance_count!=1)
404 printf(__FILE__":%d: on handle assignment from new object, instance count=%d, should be 1.\n",__LINE__,my_test_obj::instance_count);
408 obj_handle_loose=obj_handle;
409 if(obj_handle!=obj_handle_loose)
412 printf(__FILE__":%d: on loose_handle assignment\n",__LINE__);
416 obj_handle2=obj_handle_loose;
417 if(my_test_obj::instance_count!=1)
420 printf(__FILE__":%d: on handle assignment from loose_handle, instance count=%d, should be 1.\n",__LINE__,my_test_obj::instance_count);
424 test_func(obj_handle_loose);
425 if(my_test_obj::instance_count!=1)
428 printf(__FILE__":%d: on handle assignment from loose_handle, instance count=%d, should be 1.\n",__LINE__,my_test_obj::instance_count);
433 if(my_test_obj::instance_count!=1)
436 printf(__FILE__":%d: on create/destroy, instance count=%d, should be 1.\n",__LINE__,my_test_obj::instance_count);
444 int handle_cast_test()
446 printf("handle: casting test: ");
448 etl::handle<my_test_obj> obj;
449 etl::handle<my_other_test_obj> other_obj;
450 etl::loose_handle<my_other_test_obj> loose_obj;
455 obj=etl::handle<my_test_obj>::cast_dynamic(loose_obj);
460 printf(__FILE__":%d: on handle assignment from loose_handle.\n",__LINE__);
468 /* === E N T R Y P O I N T ================================================= */
474 error+=handle_basic_test();
475 error+=handle_cast_test();
476 error+=handle_general_use_test();
477 error+=handle_inheritance_test();
478 error+=loose_handle_test();
479 error+=rhandle_general_use_test();