1 /* ///////////////////////////////////////////////////////////////////////
2 * File: objects_pool_test.h
7 * Brief: objects_pool class unit testing
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
13 #ifndef EXTL_MEMORY_OBJECTS_POOL_TEST_H
14 #define EXTL_MEMORY_OBJECTS_POOL_TEST_H
16 #ifndef EXTL_MEMORY_OBJECTS_POOL_H
17 # error This file must be included of objects_pool.h
20 /* ///////////////////////////////////////////////////////////////////////
21 * ::unit_test namespace
23 EXTL_TEST_BEGIN_NAMESPACE
25 /* ///////////////////////////////////////////////////////////////////////
28 struct objects_pool_test
35 e_size_t
pool_rand(e_size_t
& seed
)
37 return (seed
= (seed
* 10807L) & 0x7fffffffL
);
41 EXTL_TEST_TRACE(_T("/* //////////////////////////////////////////////////////////////////// */"));
42 EXTL_TEST_TRACE(_T("objects_pool Test:\n"));
44 #ifdef EXTL_TEMPLATE_CLASS_DEFAULT_ARGUMENT_SUPPORT
45 objects_pool
<obj
> pool
;
48 , initialiser_selector
<obj
>::initialiser_type
52 clock_counter counter
;
53 e_size_t seed
= 0, cur_seed
= 100;
55 /* ///////////////////////////////////////////////////////////////////////
56 * deallocate memory to pool at once: alloc_size:(0 - 64K), alloc_count: 100000
58 EXTL_TEST_TRACE(_T("deallocate memory to pool at once: alloc_size:(0 - 64K), alloc_count: 1000"));
61 for(i
=0; i
< 1000; i
++)
63 obj
*p
= pool
.allocate(pool_rand(seed
)%(64 * 1024));
67 EXTL_TEST_TRACE(_T("objects_pool time:%d ms"), counter
.count());
68 EXTL_TEST_TRACE(_T("cur_alloc_size_from_system:%u"),pool
.get_snapshot().cur_alloc_size_from_system
);
69 EXTL_TEST_TRACE(_T("cur_alloc_size_from_pool:%u"),pool
.get_snapshot().cur_alloc_size_from_pool
);
73 for(i
=0; i
< 1000; i
++)
75 obj
*p
= new obj
[pool_rand(seed
)%(64 * 1024)];
79 EXTL_TEST_TRACE(_T("new time:%d ms\n"), counter
.count());
81 /* ///////////////////////////////////////////////////////////////////////
82 * deallocate memory to pool at once: alloc_size:(0 - 1024K), alloc_count: 100000
86 EXTL_TEST_TRACE(_T("deallocate memory to pool at once: alloc_size:(0 - 1024K), alloc_count: 1000"));
89 for(i
=0; i
< 1000; i
++)
91 obj
*p
= pool
.allocate(pool_rand(seed
)%(1024 * 1024));
95 EXTL_TEST_TRACE(_T("objects_pool time:%d ms"), counter
.count());
96 EXTL_TEST_TRACE(_T("cur_alloc_size_from_system:%u"),pool
.get_snapshot().cur_alloc_size_from_system
);
97 EXTL_TEST_TRACE(_T("cur_alloc_size_from_pool:%u"),pool
.get_snapshot().cur_alloc_size_from_pool
);
101 for(i
=0; i
< 1000; i
++)
103 obj
*p
= new obj
[pool_rand(seed
)%(1024 * 1024)];
107 EXTL_TEST_TRACE(_T("new time:%d ms\n"), counter
.count());
109 /* ///////////////////////////////////////////////////////////////////////
110 * delay to deallocate memory to pool: alloc_size:(0 - 8K), alloc_count: 20000
114 EXTL_TEST_TRACE(_T("delay to deallocate memory to pool: alloc_size:(0 - 4K), alloc_count: 1000"));
118 for(i
=0; i
< 10; i
++)
121 for(j
= 0; j
< 100; j
++)
122 pa
[j
] = pool
.allocate(pool_rand(seed
)%(4 * 1024));
123 for(j
= 0; j
< 100; j
++)
124 pool
.deallocate(pa
[j
]);
127 EXTL_TEST_TRACE(_T("objects_pool time:%d ms"), counter
.count());
128 EXTL_TEST_TRACE(_T("cur_alloc_size_from_system:%u"),pool
.get_snapshot().cur_alloc_size_from_system
);
129 EXTL_TEST_TRACE(_T("cur_alloc_size_from_pool:%u"),pool
.get_snapshot().cur_alloc_size_from_pool
);
133 for(i
=0; i
< 10; i
++)
136 for(j
= 0; j
< 100; j
++)
137 pa
[j
] = new obj
[pool_rand(seed
)%(4 * 1024)];
138 for(j
= 0; j
< 100; j
++)
142 EXTL_TEST_TRACE(_T("new time:%d ms\n"), counter
.count());
144 /* ///////////////////////////////////////////////////////////////////////
145 * delay to deallocate memory to pool: alloc_size:(0 - 64K), alloc_count: 20000
149 EXTL_TEST_TRACE(_T("delay to deallocate memory to pool: alloc_size:(0 - 64K), alloc_count: 1000"));
152 for(i
=0; i
< 10; i
++)
155 for(j
= 0; j
< 100; j
++)
156 pa
[j
] = pool
.allocate(pool_rand(seed
)%(64 * 1024));
157 for(j
= 0; j
< 100; j
++)
158 pool
.deallocate(pa
[j
]);
161 EXTL_TEST_TRACE(_T("objects_pool time:%d ms"), counter
.count());
162 EXTL_TEST_TRACE(_T("cur_alloc_size_from_system:%u"),pool
.get_snapshot().cur_alloc_size_from_system
);
163 EXTL_TEST_TRACE(_T("cur_alloc_size_from_pool:%u"),pool
.get_snapshot().cur_alloc_size_from_pool
);
167 for(i
=0; i
< 10; i
++)
170 for(j
= 0; j
< 100; j
++)
171 pa
[j
] = new obj
[pool_rand(seed
)%(64 * 1024)];
172 for(j
= 0; j
< 100; j
++)
176 EXTL_TEST_TRACE(_T("new time:%d ms\n"), counter
.count());
178 /* ///////////////////////////////////////////////////////////////////////
179 * delay to deallocate memory to pool: alloc_size:(0 - 1024K), alloc_count: 20000
183 EXTL_TEST_TRACE(_T("delay to deallocate memory to pool: alloc_size:(0 - 1024K), alloc_count: 1000"));
186 for(i
=0; i
< 10; i
++)
189 for(j
= 0; j
< 100; j
++)
190 pa
[j
] = pool
.allocate(pool_rand(seed
)%(1024 * 1024));
191 for(j
= 0; j
< 100; j
++)
192 pool
.deallocate(pa
[j
]);
195 EXTL_TEST_TRACE(_T("objects_pool time:%d ms"), counter
.count());
196 EXTL_TEST_TRACE(_T("cur_alloc_size_from_system:%u"),pool
.get_snapshot().cur_alloc_size_from_system
);
197 EXTL_TEST_TRACE(_T("cur_alloc_size_from_pool:%u"),pool
.get_snapshot().cur_alloc_size_from_pool
);
201 for(i
=0; i
< 10; i
++)
204 for(j
= 0; j
< 100; j
++)
205 pa
[j
] = new obj
[pool_rand(seed
)%(1024 * 1024)];
206 for(j
= 0; j
< 100; j
++)
210 EXTL_TEST_TRACE(_T("new time:%d ms\n"), counter
.count());
212 /* ///////////////////////////////////////////////////////////////////////
213 * reallocate memory: realloc_size:(0 - 64K), realloc_count: 50000
217 obj
* pr
= pool
.allocate(1);
218 EXTL_TEST_TRACE(_T("reallocate memory: realloc_size:(0 - 64K), realloc_count: 50000"));
221 for(i
=0; i
< 50000; i
++)
222 pr
= pool
.reallocate(pr
, pool_rand(seed
)%(64 * 1024));
225 EXTL_TEST_TRACE(_T("objects_pool time:%d ms"), counter
.count());
226 EXTL_TEST_TRACE(_T("cur_alloc_size_from_system:%u"),pool
.get_snapshot().cur_alloc_size_from_system
);
227 EXTL_TEST_TRACE(_T("cur_alloc_size_from_pool:%u"),pool
.get_snapshot().cur_alloc_size_from_pool
);
231 void* pm
= malloc(1);
232 for(i
=0; i
< 50000; i
++)
234 pm
= realloc(pm
, pool_rand(seed
)%(64 * 1024));
238 EXTL_TEST_TRACE(_T("realloc time:%d ms\n"), counter
.count());
241 objects_pool_test g_objects_pool_test
;
242 /* ///////////////////////////////////////////////////////////////////////
243 * ::unit_test namespace
245 EXTL_TEST_END_NAMESPACE
247 /* //////////////////////////////////////////////////////////////////// */
248 #endif /* EXTL_MEMORY_OBJECTS_POOL_TEST_H */
249 /* //////////////////////////////////////////////////////////////////// */