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/destroy, 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/destroy, 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);
171 obj_handle a(new my_test_obj(27)), b(new my_test_obj(42));
173 if (a->my_id != 42 || b->my_id != 27)
176 printf(__FILE__":%d: On swap (27,42) gave (%d,%d), should be (42,27).\n",__LINE__,a->my_id,b->my_id);
181 my_other_list.clear();
182 if(my_test_obj::instance_count)
185 printf(__FILE__":%d: On clear, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count);
199 ListItem(robj_handle obj,int bleh=1, int blah=2):
200 obj(obj),bleh(bleh),blah(blah) { }
203 int rhandle_general_use_test(void)
207 printf("rhandle: General-use test: ");
208 my_test_obj::instance_count=0;
213 robj_handle obj= new my_test_obj(rand());
214 for(i=0;i<NUMBER_OF_OBJECTS;i++)
215 my_list.push_back(obj);
217 obj_list my_other_list(my_list.begin(),my_list.end());
221 if(obj.count()!=NUMBER_OF_OBJECTS*2+1)
224 printf(__FILE__":%d: On copy, handle count=%d, should be %d.\n",__LINE__,obj.count(),NUMBER_OF_OBJECTS*2+1);
228 if(obj.rcount()!=NUMBER_OF_OBJECTS+1)
231 printf(__FILE__":%d: On copy, rhandle count=%d, should be %d.\n",__LINE__,obj.rcount(),NUMBER_OF_OBJECTS+1);
236 if(obj.rcount()!=NUMBER_OF_OBJECTS+1)
239 printf(__FILE__":%d: On copy, instance count=%d, should be %d.\n",__LINE__,obj.rcount(),NUMBER_OF_OBJECTS+1);
243 {robj_handle bleh(obj);}
245 if(obj.rcount()!=NUMBER_OF_OBJECTS+1)
248 printf(__FILE__":%d: On copy, instance count=%d, should be %d.\n",__LINE__,obj.rcount(),NUMBER_OF_OBJECTS+1);
252 my_other_list.clear();
254 if(obj.rcount()!=obj.count())
257 printf(__FILE__":%d: On copy's clear, handle count (%d) != rhandle count (%d)\n",__LINE__,obj.count(),obj.rcount());
261 if(obj.rcount()!=NUMBER_OF_OBJECTS+1)
264 printf(__FILE__":%d: On copy's clear, instance count=%d, should be %d.\n",__LINE__,obj.rcount(),NUMBER_OF_OBJECTS+1);
268 robj_handle new_obj= new my_test_obj(rand());
270 int replacements=obj.replace(new_obj);
272 if(replacements!=NUMBER_OF_OBJECTS+1)
275 printf(__FILE__":%d: Only managed to replace %d, should have replaced %d\n",__LINE__,replacements,NUMBER_OF_OBJECTS+1);
282 printf(__FILE__":%d: On replace, handles should be equal.\n",__LINE__);
287 robj_handle bleh(obj);
288 robj_handle blah(obj.get());
296 if(my_test_obj::instance_count)
299 printf(__FILE__":%d: On clear, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count);
305 std::vector<ListItem> my_item_list;
306 for(i=0;i<NUMBER_OF_OBJECTS;i++)
307 my_item_list.push_back(ListItem(new my_test_obj(rand()),3,4));
313 src=rand()%NUMBER_OF_OBJECTS;
314 dest=rand()%NUMBER_OF_OBJECTS;
315 ListItem tmp(my_item_list[src]);
316 assert(tmp.obj.rcount()>=2);
317 my_item_list.erase(my_item_list.begin()+src);
318 assert(tmp.obj.rcount()>=1);
319 my_item_list.insert(my_item_list.begin()+dest,tmp);
320 assert(tmp.obj.rcount()>=2);
323 my_item_list.clear();
325 if(my_test_obj::instance_count)
328 printf(__FILE__":%d: On clear, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count);
337 int handle_inheritance_test(void)
339 printf("handle: Inheritance test: ");
340 my_test_obj::instance_count=0;
341 my_other_test_obj::instance_count=0;
343 other_obj_list my_other_list;
346 for(i=0;i<NUMBER_OF_OBJECTS;i++)
347 my_other_list.push_back( other_obj_handle(new my_other_test_obj(rand())) );
349 obj_list my_list(my_other_list.begin(),my_other_list.end());
350 if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS)
353 printf(__FILE__":%d: On copy, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS);
357 for(i=0;i<NUMBER_OF_OBJECTS;i++)
358 my_list.push_back( other_obj_handle(new my_other_test_obj(rand())) );
359 if(my_other_test_obj::instance_count!=NUMBER_OF_OBJECTS*2 ||
360 my_test_obj::instance_count!=my_other_test_obj::instance_count)
363 printf(__FILE__":%d: On inherited copy, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS*2);
368 my_other_list.sort();
369 if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS*2)
372 printf(__FILE__":%d: On sort, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS*2);
377 if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS)
380 printf(__FILE__":%d: On clear, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS);
384 my_other_list.clear();
385 if(my_test_obj::instance_count)
388 printf(__FILE__":%d: On clear, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count);
397 void test_func(etl::handle<my_test_obj> handle)
399 if(handle) { int i=handle.count(); i++; }
402 int loose_handle_test(void)
404 printf("handle: loose_handle test: ");
405 my_test_obj::instance_count=0;
407 etl::loose_handle<my_test_obj> obj_handle_loose;
408 etl::handle<my_test_obj> obj_handle2;
411 etl::handle<my_test_obj> obj_handle(new my_test_obj(rand()));
412 if(my_test_obj::instance_count!=1)
415 printf(__FILE__":%d: on handle assignment from new object, instance count=%d, should be 1.\n",__LINE__,my_test_obj::instance_count);
419 obj_handle_loose=obj_handle;
420 if(obj_handle!=obj_handle_loose)
423 printf(__FILE__":%d: on loose_handle assignment\n",__LINE__);
427 obj_handle2=obj_handle_loose;
428 if(my_test_obj::instance_count!=1)
431 printf(__FILE__":%d: on handle assignment from loose_handle, instance count=%d, should be 1.\n",__LINE__,my_test_obj::instance_count);
435 test_func(obj_handle_loose);
436 if(my_test_obj::instance_count!=1)
439 printf(__FILE__":%d: on handle assignment from loose_handle, instance count=%d, should be 1.\n",__LINE__,my_test_obj::instance_count);
445 etl::loose_handle<my_test_obj> a(new my_test_obj(27)), b(new my_test_obj(42));
447 if (a->my_id != 42 || b->my_id != 27)
450 printf(__FILE__":%d: on loose_handle swap (27,42) gave (%d,%d), should be (42,27).\n",__LINE__,a->my_id,b->my_id);
455 if(my_test_obj::instance_count!=3)
458 printf(__FILE__":%d: on create/destroy, instance count=%d, should be 3.\n",__LINE__,my_test_obj::instance_count);
466 int handle_cast_test()
468 printf("handle: casting test: ");
470 etl::handle<my_test_obj> obj;
471 etl::handle<my_other_test_obj> other_obj;
472 etl::loose_handle<my_other_test_obj> loose_obj;
477 obj=etl::handle<my_test_obj>::cast_dynamic(loose_obj);
482 printf(__FILE__":%d: on handle assignment from loose_handle.\n",__LINE__);
490 /* === E N T R Y P O I N T ================================================= */
496 error+=handle_basic_test();
497 error+=handle_cast_test();
498 error+=handle_general_use_test();
499 error+=handle_inheritance_test();
500 error+=loose_handle_test();
501 error+=rhandle_general_use_test();