bsl_test_pool.h 7.3 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
/***************************************************************************
 * 
 * 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 <bsl/pool/bsl_pool.h>
#include <bsl/pool/bsl_poolalloc.h>
#include <bsl/pool/bsl_xmempool.h>
#include <bsl/pool/bsl_xcompool.h>
#include <bsl/pool/bsl_debugpool.h>
#include <vector>
#include <cxxtest/TestSuite.h>
#include <vector>
#include <set>

#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<void *> vec;
	std::vector<size_t> vec2;
	//size_t sizv[] = {0, 4, 16, 1<<21, 1<<19};
	size_t vsize = 1000;
	size_t cnt = 0;
	for (size_t i=0; i<vsize; ++i) {
		size_t size = rand()%(1<<20)*2;//sizv[i];
		void * ptr = pool->malloc(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)<<std::endl;
	vec.clear();
	vec2.clear();

	for (size_t i=0; i<vsize; ++i) {
		size_t size = rand()%(1<<20) * 2;
		void *ptr = pool->malloc (size);
		if (ptr) {
			memset (ptr, 0, size);
			cnt += size;
			vec.push_back(ptr);
			vec2.push_back(size);
		}
	}
	for (size_t i=0; i<vec.size(); ++i) {
		pool->free (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<<i<<" "<< pool->goodsize(i) <<" "<<pool->goodsize(pool->goodsize(i))<<" "
			<<pool->goodsize(pool->goodsize(i)+1)<<std::endl;

	//pool->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 <typename Tp>
bool test_stl (bsl::mempool *pool)
{
#ifndef __i386
	typedef bsl::pool_allocator<Tp> alloc;
	alloc a(pool);
	typedef std::vector<Tp, alloc> vector;
	vector v(a);
	typedef std::set<Tp, std::less<Tp>, alloc> set;
	set s(std::less<Tp>(), a);
	size_t num = 10000;
	try {
		for (size_t i=0; i<num; ++i) {
			v.push_back((Tp)(i));
			s.insert((Tp)(i));
		}
	} catch (...) {
		__XASSERT(g_exp, "exp");
	}
	std::sort(v.begin(), v.end(), std::greater<Tp>());
	for (size_t i=0; i<v.size(); ++i) {
		if (s.find((Tp)i) == s.end()) {
			__XASSERT(false, "not find");
		}
	}
#endif
	return true;
}

bool test_comp (bsl::mempool *pool)
{
#ifndef __i386
	typedef bsl::pool_allocator<char> alloc_c;
	typedef std::basic_string<char, std::char_traits<char>, alloc_c> string;
	typedef bsl::pool_allocator<string> alloc_s;
	typedef std::vector<string, alloc_s> vector;
	typedef std::set<string, std::less<string>, 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<slen; ++i) {
		v.push_back(a);
		v.push_back(b);
		v.push_back(c);
	}
	std::sort(v.begin(), v.end(), std::greater<string>());
	set s(std::less<string>(), cs);
	for (size_t i=0; i<v.size(); ++i) {
		s.insert(v[i]);
	}
#endif

	return true;
}

bool test_compool (bsl::mempool *pool)
{
	for (int i=0; i<1<<24; ++i) {
		__XASSERT2(pool->malloc(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<int>));
		TSM_ASSERT(1, test_xmempool(test_stl<char>));
		TSM_ASSERT(1, test_xmempool(test_stl<float>));
		TSM_ASSERT(1, test_xmempool(test_stl<double>));

		g_exp = false;
		TSM_ASSERT(1, test_xcompool(test_stl<int>));
		TSM_ASSERT(1, test_xcompool(test_stl<char>));
		TSM_ASSERT(1, test_xcompool(test_stl<float>));
		TSM_ASSERT(1, test_xcompool(test_stl<double>));

		TSM_ASSERT(1, test_debugpool(test_stl<int>));
	}
	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<char> 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"<<std::endl;
			}
		}

		{
			bsl::xcompool pool;
			pool.create ();
			bsl::pool_allocator<char> 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"<<std::endl;
			}
		}
		{
			try{
				bsl::xcompool *pool = new bsl::xcompool;
				pool->create();
				bsl::pool_allocator<double>  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<char> _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<<i<<std::endl;
					}
				}
				pool.clear();
			}
		}

		{
			bsl::xcompool pool;
			pool.create(0);
		}
	}
};




#endif  //__BSL_TEST_POOL_H_

/* vim: set ts=4 sw=4 sts=4 tw=100 */