/*! ======================================================================== ** Extended Template and Library Test Suite ** Handle Template Class Test ** $Id$ ** ** Copyright (c) 2002 Robert B. Quattlebaum Jr. ** ** This package is free software; you can redistribute it and/or ** modify it under the terms of the GNU General Public License as ** published by the Free Software Foundation; either version 2 of ** the License, or (at your option) any later version. ** ** This package is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ** General Public License for more details. ** ** === N O T E S =========================================================== ** ** ========================================================================= */ /* === H E A D E R S ======================================================= */ #include #include #include #include #include #include #include /* === M A C R O S ========================================================= */ #define NUMBER_OF_OBJECTS 40000 using namespace std; /* === C L A S S E S ======================================================= */ struct my_test_obj : public etl::rshared_object { static int instance_count; int my_id; my_test_obj(int my_id=0):my_id(my_id) { instance_count++; } virtual ~my_test_obj() { if(instance_count==0) printf("Error, instance count is going past zero!\n"); instance_count--; } bool operator<(const my_test_obj &rhs)const { return my_id obj_handle; typedef etl::rhandle robj_handle; typedef etl::handle other_obj_handle; typedef list< obj_handle > obj_list; typedef list< other_obj_handle > other_obj_list; typedef list< robj_handle > robj_list; int handle_basic_test() { printf("handle: Size of a handle: %u\n",(unsigned int)sizeof(etl::handle)); printf("handle: Size of a loose_handle: %u\n",(unsigned int)sizeof(etl::loose_handle)); printf("handle: Size of a rhandle: %u\n",(unsigned int)sizeof(etl::rhandle)); printf("handle: Size of a shared_object: %u\n",(unsigned int)sizeof(etl::shared_object)); printf("handle: Size of a rshared_object: %u\n",(unsigned int)sizeof(etl::rshared_object)); printf("handle: Basic test: "); my_test_obj::instance_count=0; { etl::handle obj_handle(new my_test_obj(rand())); } if(my_test_obj::instance_count!=0) { printf("FAILED!\n"); printf(__FILE__":%d: on create/destroy, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count); return 1; } { map > my_map; etl::handle obj_handle(new my_test_obj(rand())); my_map["bleh"]=obj_handle; } if(my_test_obj::instance_count!=0) { printf("FAILED!\n"); printf(__FILE__":%d: on create/destroy, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count); return 1; } etl::handle obj_handle(new my_test_obj(rand())); if(obj_handle != obj_handle.constant()) { printf("FAILED!\n"); printf(__FILE__":%d: on call to handle<>::constant().\n",__LINE__); return 1; } printf("PASSED\n"); return 0; } int handle_general_use_test(void) { printf("handle: General-use test: "); my_test_obj::instance_count=0; obj_list my_list, my_other_list; int i; for(i=0;imy_id != 42 || b->my_id != 27) { printf("FAILED!\n"); printf(__FILE__":%d: On swap (27,42) gave (%d,%d), should be (42,27).\n",__LINE__,a->my_id,b->my_id); return 1; } } my_other_list.clear(); if(my_test_obj::instance_count) { printf("FAILED!\n"); printf(__FILE__":%d: On clear, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count); return 1; } printf("PASSED\n"); return 0; } struct ListItem { robj_handle obj; int bleh; int blah; ListItem(robj_handle obj,int bleh=1, int blah=2): obj(obj),bleh(bleh),blah(blah) { } }; int rhandle_general_use_test(void) { printf("rhandle: General-use test: "); my_test_obj::instance_count=0; robj_list my_list; int i; robj_handle obj= new my_test_obj(rand()); for(i=0;i my_item_list; for(i=0;i=2); my_item_list.erase(my_item_list.begin()+src); assert(tmp.obj.rcount()>=1); my_item_list.insert(my_item_list.begin()+dest,tmp); assert(tmp.obj.rcount()>=2); } my_item_list.clear(); if(my_test_obj::instance_count) { printf("FAILED!\n"); printf(__FILE__":%d: On clear, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count); return 1; } printf("PASSED\n"); return 0; } int handle_inheritance_test(void) { printf("handle: Inheritance test: "); my_test_obj::instance_count=0; my_other_test_obj::instance_count=0; other_obj_list my_other_list; int i; for(i=0;i handle) { if(handle) { int i=handle.count(); i++; } } int loose_handle_test(void) { printf("handle: loose_handle test: "); my_test_obj::instance_count=0; etl::loose_handle obj_handle_loose; etl::handle obj_handle2; { etl::handle obj_handle(new my_test_obj(rand())); if(my_test_obj::instance_count!=1) { printf("FAILED!\n"); printf(__FILE__":%d: on handle assignment from new object, instance count=%d, should be 1.\n",__LINE__,my_test_obj::instance_count); return 1; } obj_handle_loose=obj_handle; if(obj_handle!=obj_handle_loose) { printf("FAILED!\n"); printf(__FILE__":%d: on loose_handle assignment\n",__LINE__); return 1; } obj_handle2=obj_handle_loose; if(my_test_obj::instance_count!=1) { printf("FAILED!\n"); printf(__FILE__":%d: on handle assignment from loose_handle, instance count=%d, should be 1.\n",__LINE__,my_test_obj::instance_count); return 1; } test_func(obj_handle_loose); if(my_test_obj::instance_count!=1) { printf("FAILED!\n"); printf(__FILE__":%d: on handle assignment from loose_handle, instance count=%d, should be 1.\n",__LINE__,my_test_obj::instance_count); return 1; } } { etl::loose_handle a(new my_test_obj(27)), b(new my_test_obj(42)); a.swap(b); if (a->my_id != 42 || b->my_id != 27) { printf("FAILED!\n"); printf(__FILE__":%d: on loose_handle swap (27,42) gave (%d,%d), should be (42,27).\n",__LINE__,a->my_id,b->my_id); return 1; } } if(my_test_obj::instance_count!=3) { printf("FAILED!\n"); printf(__FILE__":%d: on create/destroy, instance count=%d, should be 3.\n",__LINE__,my_test_obj::instance_count); return 1; } printf("PASSED\n"); return 0; } int handle_cast_test() { printf("handle: casting test: "); etl::handle obj; etl::handle other_obj; etl::loose_handle loose_obj; other_obj.spawn(); loose_obj=other_obj; obj=etl::handle::cast_dynamic(loose_obj); if(obj!=other_obj) { printf("FAILED!\n"); printf(__FILE__":%d: on handle assignment from loose_handle.\n",__LINE__); return 1; } printf("PASSED\n"); return 0; } /* === E N T R Y P O I N T ================================================= */ int main() { int error=0; error+=handle_basic_test(); error+=handle_cast_test(); error+=handle_general_use_test(); error+=handle_inheritance_test(); error+=loose_handle_test(); error+=rhandle_general_use_test(); return error; }