remove \r
[extl.git] / extl / memory / unit_test / objects_pool_test.h
blob09cfa10d6a1343e6102a0c41e28ea73af59f3d55
1 /* ///////////////////////////////////////////////////////////////////////
2 * File: objects_pool_test.h
4 * Created: 08.04.14
5 * Updated: 08.04.28
7 * Brief: objects_pool class unit testing
9 * [<Home>]
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
18 #endif
20 /* ///////////////////////////////////////////////////////////////////////
21 * ::unit_test namespace
23 EXTL_TEST_BEGIN_NAMESPACE
25 /* ///////////////////////////////////////////////////////////////////////
26 * Unit-testing
28 struct objects_pool_test
30 struct obj
32 obj(){}
33 ~obj(){}
35 e_size_t pool_rand(e_size_t& seed)
37 return (seed = (seed * 10807L) & 0x7fffffffL);
39 objects_pool_test()
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;
46 #else
47 objects_pool< obj
48 , initialiser_selector<obj>::initialiser_type
49 > pool;
50 #endif
51 volatile int i = 0;
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"));
59 seed = cur_seed;
60 counter.begin();
61 for(i=0; i < 1000; i++)
63 obj *p = pool.allocate(pool_rand(seed)%(64 * 1024));
64 pool.deallocate(p);
66 counter.end();
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);
71 seed = cur_seed;
72 counter.begin();
73 for(i=0; i < 1000; i++)
75 obj *p = new obj[pool_rand(seed)%(64 * 1024)];
76 delete[] p;
78 counter.end();
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
84 //pool.destroy();
85 //pool.create();
86 EXTL_TEST_TRACE(_T("deallocate memory to pool at once: alloc_size:(0 - 1024K), alloc_count: 1000"));
87 seed = cur_seed;
88 counter.begin();
89 for(i=0; i < 1000; i++)
91 obj *p = pool.allocate(pool_rand(seed)%(1024 * 1024));
92 pool.deallocate(p);
94 counter.end();
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);
99 seed = cur_seed;
100 counter.begin();
101 for(i=0; i < 1000; i++)
103 obj *p = new obj[pool_rand(seed)%(1024 * 1024)];
104 delete[] p;
106 counter.end();
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
112 //pool.destroy();
113 //pool.create();
114 EXTL_TEST_TRACE(_T("delay to deallocate memory to pool: alloc_size:(0 - 4K), alloc_count: 1000"));
115 obj* pa[200];
116 seed = cur_seed;
117 counter.begin();
118 for(i=0; i < 10; i++)
120 volatile e_size_t j;
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]);
126 counter.end();
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);
131 seed = cur_seed;
132 counter.begin();
133 for(i=0; i < 10; i++)
135 volatile e_size_t j;
136 for(j = 0; j < 100; j++)
137 pa[j] = new obj[pool_rand(seed)%(4 * 1024)];
138 for(j = 0; j < 100; j++)
139 delete[] pa[j];
141 counter.end();
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
147 //pool.destroy();
148 //pool.create();
149 EXTL_TEST_TRACE(_T("delay to deallocate memory to pool: alloc_size:(0 - 64K), alloc_count: 1000"));
150 counter.begin();
151 seed = cur_seed;
152 for(i=0; i < 10; i++)
154 volatile e_size_t j;
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]);
160 counter.end();
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);
165 seed = cur_seed;
166 counter.begin();
167 for(i=0; i < 10; i++)
169 volatile e_size_t j;
170 for(j = 0; j < 100; j++)
171 pa[j] = new obj[pool_rand(seed)%(64 * 1024)];
172 for(j = 0; j < 100; j++)
173 delete[] pa[j];
175 counter.end();
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
181 //pool.destroy();
182 //pool.create();
183 EXTL_TEST_TRACE(_T("delay to deallocate memory to pool: alloc_size:(0 - 1024K), alloc_count: 1000"));
184 seed = cur_seed;
185 counter.begin();
186 for(i=0; i < 10; i++)
188 volatile e_size_t j;
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]);
194 counter.end();
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);
199 seed = cur_seed;
200 counter.begin();
201 for(i=0; i < 10; i++)
203 volatile e_size_t j;
204 for(j = 0; j < 100; j++)
205 pa[j] = new obj[pool_rand(seed)%(1024 * 1024)];
206 for(j = 0; j < 100; j++)
207 delete[] pa[j];
209 counter.end();
210 EXTL_TEST_TRACE(_T("new time:%d ms\n"), counter.count());
212 /* ///////////////////////////////////////////////////////////////////////
213 * reallocate memory: realloc_size:(0 - 64K), realloc_count: 50000
215 //pool.destroy();
216 //pool.create();
217 obj* pr = pool.allocate(1);
218 EXTL_TEST_TRACE(_T("reallocate memory: realloc_size:(0 - 64K), realloc_count: 50000"));
219 seed = cur_seed;
220 counter.begin();
221 for(i=0; i < 50000; i++)
222 pr = pool.reallocate(pr, pool_rand(seed)%(64 * 1024));
223 counter.end();
224 pool.deallocate(pr);
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);
229 seed = cur_seed;
230 counter.begin();
231 void* pm = malloc(1);
232 for(i=0; i < 50000; i++)
234 pm = realloc(pm, pool_rand(seed)%(64 * 1024));
236 counter.end();
237 free(pm);
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 /* //////////////////////////////////////////////////////////////////// */