/*************************************************************************** * * Copyright (c) 2008 Baidu.com, Inc. All Rights Reserved * $Id: bsl_test_pool.h,v 1.14 2009/03/09 04:56:42 xiaowei Exp $ * **************************************************************************/ /** * @file bsl_test_pool.h * @author xiaowei(com@baidu.com) * @date 2008/12/08 19:52:09 * @version $Revision: 1.14 $ * @brief * **/ #ifndef __BSL_TEST_POOL_H_ #define __BSL_TEST_POOL_H_ #include #include #include #include #include #include #include #include #include #define __XASSERT(flag, fmt, arg...) \ {\ bool ___bsl_flag = flag; \ if (!(___bsl_flag)) { \ fprintf(stdout, "\n[error][%s:%d][%s]"fmt"\n", __FILE__, __LINE__, #flag, ##arg); \ return false; \ }\ } #define __XASSERT2(flag) __XASSERT(flag, "") typedef bool (*fun_t)(bsl::mempool *); bool test_normal_pool (bsl::mempool *pool) { std::vector vec; std::vector vec2; //size_t sizv[] = {0, 4, 16, 1<<21, 1<<19}; size_t vsize = 1000; size_t cnt = 0; for (size_t i=0; imalloc(size); if (ptr) { memset(ptr, 0, size); cnt += size; vec.push_back(ptr); vec2.push_back(size); void *ptr2 = vec.back(); size_t ptr2siz = vec2.back(); pool->free (ptr2, ptr2siz); } } std::cout<<(cnt>>20)<malloc (size); if (ptr) { memset (ptr, 0, size); cnt += size; vec.push_back(ptr); vec2.push_back(size); } } for (size_t i=0; ifree (vec[i], vec2[i]); } return true; } bool test_xmempool (fun_t op) { bsl::xmempool *pool = new bsl::xmempool; size_t size = 1<<24; void *dat = malloc (size); pool->create(dat, size, 2, 1<<16, 1.8); __XASSERT2(op((bsl::mempool *)pool)); pool->clear(); for (int i=0; i<1<<16; i = int(float(i)*1.5) + 1) std::cout<goodsize(i) <<" "<goodsize(pool->goodsize(i))<<" " <goodsize(pool->goodsize(i)+1)<destroy(); delete pool; free (dat); { bsl::xmempool pool2; } return true; } bool test_xcompool (fun_t op) { bsl::xcompool *pool = new bsl::xcompool; pool->create(1<<20, 2, 1<<16, 2.0f); __XASSERT2(op((bsl::mempool *)pool)); pool->destroy(); delete pool; { bsl::xcompool pool2; } return true; }; bool test_debugpool (fun_t op) { bsl::debugpool *pool = new bsl::debugpool; __XASSERT2(op((bsl::mempool *)pool)); pool->free(pool->malloc(10), 10); delete pool; return true; } bool g_exp = true; template bool test_stl (bsl::mempool *pool) { #ifndef __i386 typedef bsl::pool_allocator alloc; alloc a(pool); typedef std::vector vector; vector v(a); typedef std::set, alloc> set; set s(std::less(), a); size_t num = 10000; try { for (size_t i=0; i()); for (size_t i=0; i alloc_c; typedef std::basic_string, alloc_c> string; typedef bsl::pool_allocator alloc_s; typedef std::vector vector; typedef std::set, alloc_s> set; alloc_c ca(pool); alloc_s cs(pool); string a(ca); a = "helloa"; string b(ca); b = "hellob"; string c(ca); c = "helloc"; size_t slen = 1000; vector v(cs); for (size_t i=0; i()); set s(std::less(), cs); for (size_t i=0; imalloc(rand()%16+1) != NULL); } return true; } class bsl_test_pool : public CxxTest::TestSuite { public: void test_normal_pool_ () { TSM_ASSERT(0, test_xmempool(test_normal_pool)); TSM_ASSERT(0, test_xcompool(test_normal_pool)); TSM_ASSERT(0, test_xcompool(test_compool)); } void test_stl_pool_ () { g_exp = true; TSM_ASSERT(1, test_xmempool(test_stl)); TSM_ASSERT(1, test_xmempool(test_stl)); TSM_ASSERT(1, test_xmempool(test_stl)); TSM_ASSERT(1, test_xmempool(test_stl)); g_exp = false; TSM_ASSERT(1, test_xcompool(test_stl)); TSM_ASSERT(1, test_xcompool(test_stl)); TSM_ASSERT(1, test_xcompool(test_stl)); TSM_ASSERT(1, test_xcompool(test_stl)); TSM_ASSERT(1, test_debugpool(test_stl)); } void test_stl_string_ () { TSM_ASSERT(2, test_xmempool(test_comp)); TSM_ASSERT(2, test_xcompool(test_comp)); } void test_pool_allocator_() { { bsl::syspool pool; bsl::pool_allocator alloc((bsl::mempool *)&pool); try { alloc.allocate(1<<30); alloc.allocate(1<<30); alloc.allocate(1<<30); alloc.allocate(1<<30); } catch (...) { std::cout<<"Exception"< alloc((bsl::mempool *) &pool); try { alloc.allocate(1<<30); alloc.allocate(1<<30); alloc.allocate(1<<30); alloc.allocate(1<<30); } catch(bsl::BadAllocException &e){ std::cout<<"Exception"<create(); bsl::pool_allocator alloc(pool); int size = 1<<30; alloc.allocate(size); delete pool; } catch(bsl::BadAllocException &e){ std::cout << "bad alloc!\n" << std::endl; } } { bsl::xcompool pool; } { typedef bsl::pool_allocator _Alloc; typedef _Alloc::pointer pointer; typedef _Alloc::const_reference const_reference; int size = 1000; int size2 = 1073741824; char *buf = (char*)malloc(sizeof(char)*size); bsl::xmempool *pool = new bsl::xmempool; int ret = pool->create(buf, size); if (ret != 0) return; _Alloc alloc(pool); pointer p; try{ p = alloc.allocate(size2); TSM_ASSERT(p, p != NULL); delete pool; } catch(bsl::BadAllocException &e){ std::cout << "bad alloc!\n" << std::endl; free(buf); } } { bsl::xcompool pool; assert (pool.create() == 0); assert (pool.create(0) != 0); assert (pool.create(1<<10) == 0); assert (pool.malloc(1<<11) != 0); assert (pool.create(1<<16) == 0); assert (pool.malloc(1<<17) != 0); assert (pool.create(1<<17) == 0); } { bsl::xcompool pool; assert (pool.create(1<<10) == 0); for (int k=0; k<2; ++k) { for (int i=0; i<(1<<22); ++i) { void * ret = (void *)pool.malloc(i); assert (ret != 0); pool.free (ret, i); if (i%(1<<20) == 0) { std::cout<