/*************************************************************************** * * Copyright (c) 2008 Baidu.com, Inc. All Rights Reserved * $Id: test_bsl_wrappers.cpp,v 1.2 2009/03/09 04:56:42 xiaowei Exp $ * **************************************************************************/ /** * @file test_bsl_wrappers.cpp * @author chenxm(chenxiaoming@baidu.com) * @date 2008/08/14 10:44:47 * @version $Revision: 1.2 $ * @brief * **/ #include"bsl/ResourcePool.h" #include #include #include using namespace std; using namespace bsl; #define see(x) do{ \ cerr<<__FILE__<<":"<<__LINE__<<": " <<(#x)<<" = "<<(x)<<" ("< // #define NG_TEST_DEBUG /* * __construct_aux [bsl] __construct_aux [bsl] bsl::construct [bsl] bsl::construct [bsl] bsl::destroy [bsl] __construct_array_aux [bsl] __construct_array_aux [bsl] bsl::construct_array [bsl] bsl::construct_array [bsl] default_destroy_array [bsl] bsl::destroy_array [bsl] deallocate [bsl] bsl::destroy_and_deallocate [bsl] bsl::destroy_and_deallocate_array [bsl] bsl_delete [bsl] bsl_delete_array [bsl] bsl_free [bsl] bsl_fclose [bsl] */ /** * @brief 好用的测试类 * * */ class C{ public: static int s_default_constructor_count; static int s_copy_constructor_count; static int s_int_constructor_count; static int s_destructor_count; static int alive_count(){ return s_default_constructor_count + s_copy_constructor_count + s_int_constructor_count - s_destructor_count; } static void print_count(){ see( s_default_constructor_count ); see( s_copy_constructor_count ); see( s_int_constructor_count ); see( s_destructor_count ); see( alive_count() ); } static void clear_count(){ s_default_constructor_count = 0; s_copy_constructor_count = 0; s_int_constructor_count = 0; s_destructor_count = 0; } C(){ #ifdef NG_TEST_DEBUG cerr<<"C() this ="<(buf); bsl::construct(buf, &iarg); bsl::construct(buf, &carg); assert( C::s_default_constructor_count == 1 ); assert( C::s_copy_constructor_count == 1 ); assert( C::s_int_constructor_count == 1 ); assert( C::s_destructor_count == 0 ); assert( C::alive_count() == 3 ); //for non-pod array C::clear_count(); char arr_buf[sizeof(C)*100]; bsl::construct_array(arr_buf,arr_buf+0); bsl::construct_array(arr_buf,arr_buf+3); bsl::construct_array(arr_buf, arr_buf+5, &iarg); bsl::construct_array(arr_buf, arr_buf+7, &carg); #ifdef NGUI_CONFIG_DO_NOT_COPY_FOR_DEFAULT_CONSTRUCTION assert( C::s_default_constructor_count == 3 ); assert( C::s_copy_constructor_count == 7 ); assert( C::s_int_constructor_count == 5 ); assert( C::s_destructor_count == 0 ); assert( C::alive_count() == 15 ); #else assert( C::s_default_constructor_count == 2 ); assert( C::s_copy_constructor_count == 10 ); assert( C::s_int_constructor_count == 5 ); assert( C::s_destructor_count == 2 ); assert( C::alive_count() == 15 ); #endif //for pod float farg = 4670.9394; float fbuf[1]; void * vfbuf = fbuf; bsl::construct(fbuf); #ifndef NGUI_CONFIG_SKIP_POD_INITIALIAZION assert(*fbuf == 0.0 ); #endif bsl::construct(vfbuf, &farg);//用fbuf代替vfbuf当然也是没问题的 assert(*fbuf == farg ); bsl::construct(vfbuf, &iarg); assert( *fbuf == float(iarg) ); //for pod array float farr[100]; bsl::construct_array(farr, farr+3); #ifndef NGUI_CONFIG_SKIP_POD_INITIALIAZION for( int i = 0; i < 3; ++ i){ assert(farr[i] == 0.0); } #endif bsl::construct_array(farr, farr+5, &farg);//用farr代替vfarr当然也是没问题的 for( int i = 0; i < 5; ++ i){ assert(farr[i] == farg ); } bsl::construct_array(farr, farr+7, &iarg); for( int i = 0; i < 7; ++ i){ assert(farr[i] == float(iarg)); } } void test_destroy(){ cout<<"enter "<<__func__<<"()"<(buf); bsl::destroy(buf); char arr[sizeof(C)*100]; bsl::construct_array(arr,arr+sizeof(C)*3, &iarg ); bsl::destroy_array(arr,arr+sizeof(C)*3); bsl::construct_array(arr,arr+sizeof(C)*11, &iarg ); default_destroy_array((C*)arr, ((C*)arr)+11); assert( C::s_destructor_count == 15 ); assert( C::alive_count() == 0 ); //for POD, just test compilation bsl::destroy(arr); bsl::destroy(arr); bsl::destroy(arr); bsl::destroy_array(arr,arr+3); bsl::destroy_array(arr,arr+5); bsl::destroy_array(arr,arr+7); default_destroy_array(arr,arr+11); } /* void test_deallocate(){ cout<<"enter "<<__func__<<"()"<().allocate(46709394); deallocate >( buf, buf + 46709394 ); } for( int i = 0; i< times; ++ i){ C * buf = NgAlloc().allocate(46709394); deallocate >( buf, buf + 46709394 ); } //POD for( int i = 0; i< times; ++ i){ char * buf = std::allocator().allocate(46709394); deallocate >( buf, buf + 46709394 ); } for( int i = 0; i< times; ++ i){ char * buf = NgAlloc().allocate(46709394); deallocate >( buf, buf+46709394 ); } } void test_destroy_and_deallocate(){ cout<<"enter "<<__func__<<"()"<().allocate(1); bsl::destroy_and_deallocate >(dbl); C::clear_count(); C* pc = bsl::NgAlloc().allocate(10); bsl::NgAlloc().construct(pc,carg); bsl::destroy_and_deallocate >(pc); assert( C::s_default_constructor_count == 0 ); assert( C::s_copy_constructor_count == 1 ); assert( C::s_int_constructor_count == 0 ); assert( C::s_destructor_count == 1 ); assert( C::alive_count() == 0 ); //destroy_and_deallocate_array //POD for( int i = 0; i< times; ++ i){ float * buf = std::allocator().allocate(7336); for( int j = 0; j < 7336; ++ j){ std::allocator().construct(&buf[j], farg ); } bsl::destroy_and_deallocate_array >( buf, buf+7336 ); } for( int i = 0; i< times; ++ i){ float * buf = bsl::NgAlloc().allocate(7336); for( int j = 0; j < 7336; ++ j){ bsl::NgAlloc().construct(&buf[j], farg ); } bsl::destroy_and_deallocate_array >( buf, buf+7336 ); } //non-POD C::clear_count(); for( int i = 0; i< times; ++ i){ C * buf = std::allocator().allocate(7336); for( int j = 0; j < 7336; ++ j){ std::allocator().construct(&buf[j], carg ); } bsl::destroy_and_deallocate_array >( buf, buf+7336 ); } for( int i = 0; i< times; ++ i){ C * buf = bsl::NgAlloc().allocate(7336); for( int j = 0; j < 7336; ++ j){ bsl::NgAlloc().construct(&buf[j], carg ); } bsl::destroy_and_deallocate_array >( buf, buf+7336 ); } assert( C::s_default_constructor_count == 0 ); assert( C::s_copy_constructor_count == 7336+7336 ); assert( C::s_int_constructor_count == 0 ); assert( C::s_destructor_count == 7336+7336 ); assert( C::alive_count() == 0 ); } */ void test_bsl_delete(){ cout<<"enter "<<__func__<<"()"<(pc); C* arr_c = new C[123]; bsl_delete_array(arr_c); assert( C::s_default_constructor_count == 1+123 ); assert( C::s_copy_constructor_count == 0 ); assert( C::s_int_constructor_count == 0 ); assert( C::s_destructor_count == 1+123 ); assert( C::alive_count() == 0 ); } void test_bsl_fclose(){ cout<<"enter "<<__func__<<"()"<