/*************************************************************************** * * Copyright (c) 2008 Baidu.com, Inc. All Rights Reserved * $Id: test_ResourcePool.cpp,v 1.5 2010/04/28 12:45:33 scmpf Exp $ * **************************************************************************/ /** * @file test_ResourcePool.cpp * @author chenxm(chenxiaoming@baidu.com) * @date 2008/08/06 20:23:01 * @version $Revision: 1.5 $ * @brief * **/ //#define NGUI_CONFIG_DO_NOT_COPY_FOR_DEFAULT_CONSTRUCTION //#define NG_TEST_DEBUG #include #include #include #include #include "bsl/ResourcePool.h" #include "bsl/pool/bsl_debugpool.h" #include "bsl/pool/bsl_xcompool.h" #include "bsl/var/implement.h" using namespace std; #include #include using namespace std; #define see(x) do{ \ cerr<<__FILE__<<":"<<__LINE__<<": " <<(#x)<<" = "<<(x)<<" ("<(p); assert( C::s_default_constructor_count == 1); assert( C::alive_count() == 1 ); bsl::destroy(p); assert( C::s_destructor_count == 1 ); assert( C::alive_count() == 0 ); //test copy constructor char buf2[sizeof(C)]; void *q = buf2; bsl::construct(q,p); int aa = 4; bsl::construct(q,&aa); //call C::C(int) assert( C::s_copy_constructor_count == 1 ); assert( C::s_int_constructor_count == 1 ); assert( C::alive_count() == 2 ); bsl::destroy(q); assert( C::s_destructor_count == 2 ); assert( C::alive_count() == 1 ); } void test_attach(){ C::clear_count(); cout< ); C *pc_arr = new C[2]; rp.attach( pc_arr, bsl::bsl_delete_array ); }// rp destroyed here, “没有rp,就什么都没有了” assert( C::s_default_constructor_count == 3 ); assert( C::s_destructor_count == 3 ); assert( C::alive_count() == 0 ); } void test_create(){ cout<(); //test create(arg) rp.create(c); //test create(arg) with int rp.create(123); C* pc = rp.createp(456); assert(NULL != pc); }// rp destroyed here, “没有rp,就什么都没有了” assert( C::s_default_constructor_count == 1 ); assert( C::s_copy_constructor_count == 1 ); assert( C::s_int_constructor_count == 2 ); assert( C::s_destructor_count == 4 ); assert( C::alive_count() == 0 ); //POD { bsl::ResourcePool rp; void * p = rp.create(); void * p1 = rp.create(p); assert( p1 == p ); void * p2 = rp.create(p); assert( p2 == p ); const char * str = "str"; const void * p3 = rp.create(str); assert( p3 == str ); //注意: void * p4 = rp.create(str); 是不可以的 //因为const char *& 不能转化成void * }// rp destroyed here } void test_create_array(){ cout<(2);// default = 1 copy = 2 int = 0 destruct = 1 //test create(arg) C* c_arr2 = rp.create_array(3, c_arr[0]); // default = 1 copy = 5 int = 0 destruct = 1 //test create(arg) with arg is int C* c_arr3 = rp.create_array(4, 7336 );// default = 1 copy = 5 int = 4 destruct = 1 // you can dump them casually c_arr = NULL; c_arr2 = NULL; c_arr3 = NULL; }// rp destroyed here, “没有rp,就什么都没有了” #ifdef NGUI_CONFIG_DO_NOT_COPY_FOR_DEFAULT_CONSTRUCTION //这个没什么好说的,正常版本 assert( C::s_default_constructor_count == 2 ); assert( C::s_copy_constructor_count == 3 ); assert( C::s_int_constructor_count == 4 ); assert( C::alive_count() == 0 ); #else //这个看请面的注释 assert( C::s_default_constructor_count == 1 ); assert( C::s_copy_constructor_count == 5 ); assert( C::s_int_constructor_count == 4 ); assert( C::alive_count() == 0 ); #endif //pod; { bsl::ResourcePool rp; void ** p_arr = rp.create_array(1); void ** p1_arr = rp.create_array(3,p_arr[0]); char str[] = "str"; const void ** p2_arr = rp.create_array(3,str); for( int i = 0; i< 3; ++ i ){ assert(p2_arr[i] == str ); } void ** p3_arr = rp.create_array(7,(void*)str); for( int i = 0; i< 7; ++ i ){ assert(p3_arr[i] == str ); } } } void test_clone(){ cout<(src[1]); rp.clone(src[1]); //test clone_array() C* pc3 = rp.clone_array(src, 3); assert(NULL != pc3); //test create_raw() void * p1 = rp.create_raw( 123 ); assert(NULL != p1); void * p2 = rp.clone_raw( p1, 123 ); assert(NULL != p2); //test create_cstring() char * str = rp.clone_cstring( "没有rp,就什么都没有了" ); assert( 0 == strcmp( str, "没有rp,就什么都没有了" ) ); }// rp destroyed here, “没有rp,就什么都没有了” assert( C::s_default_constructor_count == 0 ); assert( C::s_copy_constructor_count == 5 ); assert( C::s_int_constructor_count == 0 ); assert( C::s_destructor_count == 5 ); assert( C::alive_count() == 0 ); } #include #include #include #include #include /* void test_allocator(){ cout< > v; map >m; set > s; list > l; deque > di; deque >, bsl::NgAlloc > > > dv; // todo: 功能测试 } */ void test_with_mempool(){ { bsl::debugpool pool; bsl::ResourcePool rp(pool); rp.create(); rp.create_array(123); } { bsl::debugpool pool; bsl::ResourcePool rp(pool); bsl::pool_allocator alloc = rp.get_allocator(); char *p = alloc.allocate(2); alloc.deallocate(p, 2); bsl::pool_allocator alloc2 = rp.get_allocator(); C* pc = alloc2.allocate(1); alloc2.deallocate(pc, 1); } { bsl::debugpool pool; bsl::ResourcePool rp(pool); char * p = static_cast(rp.create_raw(1000)); p[999] = 'A'; } { bsl::debugpool pool; bsl::ResourcePool rp(pool); const char * cs = "iwegoewgewobjojqwnn q qoeqbn w%$%#^@$#@^@$%@!@$&&*%$*%$"; assert( 0 == strcmp( cs, rp.clone_cstring(cs) ) ); assert( 0 == strcmp( "", rp.clone_cstring("") ) ); assert( 0 == strncmp( cs, rp.clone_cstring(cs, strlen(cs)), strlen(cs) ) ); assert( 0 == strncmp( cs, rp.clone_cstring(cs, 5), 5 ) ); assert( '\0' == * rp.clone_cstring(cs, 0) ) ; } { bsl::xcompool pool; pool.create(); bsl::ResourcePool rp(pool); bsl::var::String str = rp.create(bsl::var::String::allocator_type( &rp.get_mempool() )); str = "hello word"; printf("%s\n",str.c_str()); } } class EException: public bsl::BasicException{}; class E{ public: E(){ throw EException()<(buf)), _count(0), _size(size) {} virtual void * malloc(size_t size){ if ( _count + size > _size ){ throw bsl::BadAllocException()<::iterator iter = _map.find(static_cast(p)); if ( iter == _map.end() || p != iter->first || len != iter->second ){ throw bsl::BadArgumentException()< _map; char * _buf; size_t _count; size_t _size; }; void test_except(){ { bsl::ResourcePool rp; ASSERT_THROW( rp.create(), EException ); assert( NULL == rp.createp() ); } { bsl::ResourcePool rp; const E& ee = rp.create(std::nothrow); ASSERT_THROW( rp.create(ee), EException ); assert( NULL == rp.createp(ee) ); } { BufPool throw_pool(NULL, 0); bsl::ResourcePool rp(throw_pool); ASSERT_THROW( rp.create(), bsl::BadAllocException ); ASSERT_THROW( rp.create(123), bsl::BadAllocException ); assert( NULL == rp.createp() ); assert( NULL == rp.createp(123) ); C *pc = new C(); ASSERT_THROW( rp.attach( pc, bsl::bsl_delete ), bsl::BadAllocException ); } } void test_valist(const char * format, ...) { va_list ap; va_start(ap,format); bsl::ResourcePool rp; const char * str = rp.vcrcprintf( format, ap ); printf("%s\n",str); const char * str2 = rp.vcrcprintf_hint( 5, format, ap ); printf("%s\n",str2); va_end(ap); assert( 0 == strcmp( str, rp.clone_cstring(str2) ) ); return ; } void test_print() { test_valist("%d,%d,%s,%lf",200,3,"wahaha",1.0); bsl::ResourcePool rp; const char * str2 = rp.crcprintf("%d,%d,%s,%lf",200,3,"wahaha",1.0); const char * str3 = rp.crcprintf_hint(5,"%d,%d,%s,%lf",200,3,"wahaha",1.0); assert( 0 == strcmp(str2,rp.clone_cstring(str3)) ); } int main(){ try{ test_construct_and_destroy(); test_attach(); test_create(); test_create_array(); test_clone(); test_with_mempool(); test_except(); test_print(); }catch(bsl::Exception& e){ fprintf( stderr, "Exception: %s", e.what() ); } return 0; } /* vim: set ts=4 sw=4 sts=4 tw=100 */