/[ascend]/trunk/base/generic/general/test/test_pool.c
ViewVC logotype

Contents of /trunk/base/generic/general/test/test_pool.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 60 - (show annotations) (download) (as text)
Mon Oct 31 03:39:15 2005 UTC (18 years, 7 months ago) by jds
File MIME type: text/x-csrc
File size: 14521 byte(s)
- jam build up and limping on Linux (finally).
- fixes to CUnit test suite to compile on Linux (still not operable, though).
1 /*
2 * Unit test functions for ASCEND: general/pool.c
3 *
4 * Copyright (C) 2005 Jerry St.Clair
5 *
6 * This file is part of the Ascend Environment.
7 *
8 * The Ascend Environment is free software; you can redistribute it
9 * and/or modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * The Ascend Environment is distributed in hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with the program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check the file named
21 * COPYING.
22 */
23
24 #include <stdio.h>
25 #include "utilities/ascConfig.h"
26 #include "utilities/ascMalloc.h"
27 #include "general/pool.h"
28 #include "general/list.h"
29 #include "CUnit/CUnit.h"
30 #include "test_pool.h"
31
32 static void test_pool(void)
33 {
34 pool_store_t ps;
35 struct pool_statistics stats;
36 int i;
37 int *pint_array[100];
38 double *pdoub_array[100];
39 char *pstr_array[100];
40 struct gl_list_t *pint_list;
41 #ifdef MALLOC_DEBUG
42 char str_error_msg[100];
43 #endif
44 int i_initialized_lists = FALSE;
45 unsigned long prior_meminuse;
46
47 prior_meminuse = ascmeminuse(); /* save meminuse() at start of test function */
48
49 #ifndef MALLOC_DEBUG
50 CU_FAIL("test_pool() compiled without MALLOC_DEBUG - memory management not tested.");
51 #endif
52
53 /* test pool_create_store(), pool_destroy_store() */
54
55 ps = pool_create_store(0, 10, sizeof(int), 10, 10); /* create a store - length < 1 (error) */
56 CU_TEST(NULL == ps);
57 if (NULL != ps)
58 pool_destroy_store(ps);
59
60 ps = pool_create_store(10, 0, sizeof(int), 10, 10); /* create a store - width < 1 (error) */
61 CU_TEST(NULL == ps);
62 if (NULL != ps)
63 pool_destroy_store(ps);
64
65 ps = pool_create_store(10, 10, 0, 10, 10); /* create a store - eltsze < 1 (ok) */
66 CU_TEST(NULL != ps);
67 pool_get_stats(&stats, ps);
68 CU_TEST(100 == stats.elt_total);
69 CU_TEST(0 == stats.elt_taken);
70 CU_TEST(0 == stats.elt_inuse);
71 CU_TEST(0 == stats.elt_onlist);
72 CU_TEST(0 == stats.elt_size);
73 CU_TEST(10 == stats.str_len);
74 CU_TEST(10 == stats.str_wid);
75
76 if (NULL != ps)
77 pool_destroy_store(ps);
78
79 ps = pool_create_store(10, 10, sizeof(int), 10, 10);/* create a store for ints */
80 CU_TEST(NULL != ps);
81 pool_get_stats(&stats, ps);
82 CU_TEST(100 == stats.elt_total);
83 CU_TEST(0 == stats.elt_taken);
84 CU_TEST(0 == stats.elt_inuse);
85 CU_TEST(0 == stats.elt_onlist);
86 CU_TEST(sizeof(int) == stats.elt_size);
87 CU_TEST(10 == stats.str_len);
88 CU_TEST(10 == stats.str_wid);
89
90 for (i=0 ; i<100 ; ++i) { /* check out some elements */
91 pint_array[i] = (int*)pool_get_element(ps);
92 #ifdef MALLOC_DEBUG
93 CU_TEST(0 != AllocatedMemory((VOIDPTR)pint_array[i], sizeof(VOIDPTR)));
94 #endif
95 }
96
97 if (NULL != ps) /* destroy the store */
98 pool_destroy_store(ps);
99
100 #ifdef MALLOC_DEBUG
101 for (i=0 ; i<100 ; ++i) { /* confirm destruction of the elements */
102 CU_TEST(0 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(VOIDPTR)));
103 }
104 #endif
105
106 /* test pool_get_element(), pool_free_element() */
107
108 ps = pool_create_store(10, 10, sizeof(int), 10, 10);/* create a store for ints */
109 CU_TEST(NULL != ps);
110
111 CU_TEST(NULL == pool_get_element(NULL)); /* requesting element of a NULL store has no effect */
112 CU_TEST(100 == stats.elt_total);
113 CU_TEST(0 == stats.elt_taken);
114 CU_TEST(0 == stats.elt_inuse);
115 CU_TEST(0 == stats.elt_onlist);
116 CU_TEST(sizeof(int) == stats.elt_size);
117 CU_TEST(10 == stats.str_len);
118 CU_TEST(10 == stats.str_wid);
119
120 for (i=0 ; i<50 ; ++i) { /* check out & assign some elements */
121 pint_array[i] = (int*)pool_get_element(ps);
122 *pint_array[i] = i;
123 }
124 pool_get_stats(&stats, ps);
125 CU_TEST(100 == stats.elt_total);
126 CU_TEST(50 == stats.elt_taken);
127 CU_TEST(50 == stats.elt_inuse);
128 CU_TEST(0 == stats.elt_onlist);
129 CU_TEST(sizeof(int) == stats.elt_size);
130 CU_TEST(10 == stats.str_len);
131 CU_TEST(10 == stats.str_wid);
132
133 for (i=50 ; i<100 ; ++i) { /* check out & assign more elements */
134 pint_array[i] = (int*)pool_get_element(ps);
135 *pint_array[i] = i;
136 }
137 pool_get_stats(&stats, ps);
138 CU_TEST(100 == stats.elt_total);
139 CU_TEST(100 == stats.elt_taken);
140 CU_TEST(100 == stats.elt_inuse);
141 CU_TEST(0 == stats.elt_onlist);
142 CU_TEST(sizeof(int) == stats.elt_size);
143 CU_TEST(10 == stats.str_len);
144 CU_TEST(10 == stats.str_wid);
145
146 for (i=0 ; i<100 ; ++i) {
147 CU_TEST(i == *pint_array[i]); /* make sure element values are ok */
148 }
149
150 pool_free_element(ps, NULL); /* returning a NULL element has no effect */
151 pool_get_stats(&stats, ps);
152 CU_TEST(100 == stats.elt_total);
153 CU_TEST(100 == stats.elt_taken);
154 CU_TEST(100 == stats.elt_inuse);
155 CU_TEST(0 == stats.elt_onlist);
156 CU_TEST(sizeof(int) == stats.elt_size);
157 CU_TEST(10 == stats.str_len);
158 CU_TEST(10 == stats.str_wid);
159
160 for (i=0 ; i<50 ; ++i) { /* return some elements */
161 pool_free_element(ps, pint_array[i]);
162 }
163 pool_get_stats(&stats, ps);
164 CU_TEST(100 == stats.elt_total);
165 CU_TEST(100 == stats.elt_taken);
166 CU_TEST(50 == stats.elt_inuse);
167 CU_TEST(50 == stats.elt_onlist);
168 CU_TEST(sizeof(int) == stats.elt_size);
169 CU_TEST(10 == stats.str_len);
170 CU_TEST(10 == stats.str_wid);
171 #ifdef MALLOC_DEBUG
172 for (i=0 ; i<100 ; ++i) { /* nothing should be deallocated yet */
173 CU_TEST(0 != AllocatedMemory((VOIDPTR)pint_array[i], sizeof(VOIDPTR)));
174 }
175 #endif
176
177 for (i=0 ; i<50 ; ++i) { /* check out & assign some elements */
178 pint_array[i] = (int*)pool_get_element(ps);
179 *pint_array[i] = i;
180 }
181 pool_get_stats(&stats, ps);
182 CU_TEST(100 == stats.elt_total);
183 CU_TEST(100 == stats.elt_taken);
184 CU_TEST(100 == stats.elt_inuse);
185 CU_TEST(0 == stats.elt_onlist);
186 CU_TEST(sizeof(int) == stats.elt_size);
187 CU_TEST(10 == stats.str_len);
188 CU_TEST(10 == stats.str_wid);
189
190 if (NULL != ps) /* destroy the store */
191 pool_destroy_store(ps);
192
193 #ifdef MALLOC_DEBUG
194 for (i=0 ; i<100 ; ++i) { /* confirm destruction of the elements */
195 CU_TEST(0 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(VOIDPTR)));
196 }
197 #endif
198
199 ps = pool_create_store(10, 10, sizeof(double), 10, 10);/* create a store for doubles */
200 CU_TEST(NULL != ps);
201
202 for (i=0 ; i<50 ; ++i) { /* check out & assign some elements */
203 pdoub_array[i] = (double*)pool_get_element(ps);
204 *pdoub_array[i] = (double)i;
205 }
206 pool_get_stats(&stats, ps);
207 CU_TEST(100 == stats.elt_total);
208 CU_TEST(50 == stats.elt_taken);
209 CU_TEST(50 == stats.elt_inuse);
210 CU_TEST(0 == stats.elt_onlist);
211 CU_TEST(sizeof(double) == stats.elt_size);
212 CU_TEST(10 == stats.str_len);
213 CU_TEST(10 == stats.str_wid);
214
215 for (i=50 ; i<100 ; ++i) { /* check out & assign more elements */
216 pdoub_array[i] = (double*)pool_get_element(ps);
217 *pdoub_array[i] = (double)i;
218 }
219 pool_get_stats(&stats, ps);
220 CU_TEST(100 == stats.elt_total);
221 CU_TEST(100 == stats.elt_taken);
222 CU_TEST(100 == stats.elt_inuse);
223 CU_TEST(0 == stats.elt_onlist);
224 CU_TEST(sizeof(double) == stats.elt_size);
225 CU_TEST(10 == stats.str_len);
226 CU_TEST(10 == stats.str_wid);
227
228 for (i=0 ; i<100 ; ++i) {
229 CU_ASSERT_DOUBLE_EQUAL((double)i, *pdoub_array[i], 0.001); /* make sure element values are ok */
230 }
231
232 for (i=0 ; i<50 ; ++i) { /* return some elements */
233 pool_free_element(ps, pdoub_array[i]);
234 }
235 pool_get_stats(&stats, ps);
236 CU_TEST(100 == stats.elt_total);
237 CU_TEST(100 == stats.elt_taken);
238 CU_TEST(50 == stats.elt_inuse);
239 CU_TEST(50 == stats.elt_onlist);
240 CU_TEST(sizeof(double) == stats.elt_size);
241 CU_TEST(10 == stats.str_len);
242 CU_TEST(10 == stats.str_wid);
243 #ifdef MALLOC_DEBUG
244 for (i=0 ; i<100 ; ++i) { /* nothing should be deallocated yet */
245 CU_TEST(0 != AllocatedMemory((VOIDPTR)pdoub_array[i], sizeof(VOIDPTR)));
246 }
247 #endif
248
249 for (i=0 ; i<50 ; ++i) { /* check out & assign some elements */
250 pdoub_array[i] = (double*)pool_get_element(ps);
251 *pdoub_array[i] = (double)i;
252 }
253 pool_get_stats(&stats, ps);
254 CU_TEST(100 == stats.elt_total);
255 CU_TEST(100 == stats.elt_taken);
256 CU_TEST(100 == stats.elt_inuse);
257 CU_TEST(0 == stats.elt_onlist);
258 CU_TEST(sizeof(double) == stats.elt_size);
259 CU_TEST(10 == stats.str_len);
260 CU_TEST(10 == stats.str_wid);
261
262 if (NULL != ps) /* destroy the store */
263 pool_destroy_store(ps);
264
265 #ifdef MALLOC_DEBUG
266 for (i=0 ; i<100 ; ++i) { /* confirm destruction of the elements */
267 CU_TEST(0 == AllocatedMemory((VOIDPTR)pdoub_array[i], sizeof(VOIDPTR)));
268 }
269 #endif
270
271 ps = pool_create_store(10, 10, 12*sizeof(char), 10, 10);/* create a store for strings */
272 CU_TEST(NULL != ps);
273
274 for (i=0 ; i<50 ; ++i) { /* check out & assign some elements */
275 pstr_array[i] = (char*)pool_get_element(ps);
276 strcpy(pstr_array[i], "01234567890");
277 }
278 pool_get_stats(&stats, ps);
279 CU_TEST(100 == stats.elt_total);
280 CU_TEST(50 == stats.elt_taken);
281 CU_TEST(50 == stats.elt_inuse);
282 CU_TEST(0 == stats.elt_onlist);
283 CU_TEST((12*sizeof(char) + (12*sizeof(char)%sizeof(void*))) == stats.elt_size);
284 CU_TEST(10 == stats.str_len);
285 CU_TEST(10 == stats.str_wid);
286
287 for (i=50 ; i<100 ; ++i) { /* check out & assign more elements */
288 pstr_array[i] = (char*)pool_get_element(ps);
289 strcpy(pstr_array[i], "01234567890");
290 }
291 pool_get_stats(&stats, ps);
292 CU_TEST(100 == stats.elt_total);
293 CU_TEST(100 == stats.elt_taken);
294 CU_TEST(100 == stats.elt_inuse);
295 CU_TEST(0 == stats.elt_onlist);
296 CU_TEST((12*sizeof(char) + (12*sizeof(char)%sizeof(void*))) == stats.elt_size);
297 CU_TEST(10 == stats.str_len);
298 CU_TEST(10 == stats.str_wid);
299
300 for (i=0 ; i<100 ; ++i) {
301 CU_ASSERT_STRING_EQUAL("01234567890", pstr_array[i]); /* make sure element values are ok */
302 }
303
304 for (i=0 ; i<50 ; ++i) { /* return some elements */
305 pool_free_element(ps, pstr_array[i]);
306 }
307 pool_get_stats(&stats, ps);
308 CU_TEST(100 == stats.elt_total);
309 CU_TEST(100 == stats.elt_taken);
310 CU_TEST(50 == stats.elt_inuse);
311 CU_TEST(50 == stats.elt_onlist);
312 CU_TEST((12*sizeof(char) + (12*sizeof(char)%sizeof(void*))) == stats.elt_size);
313 CU_TEST(10 == stats.str_len);
314 CU_TEST(10 == stats.str_wid);
315 #ifdef MALLOC_DEBUG
316 for (i=0 ; i<100 ; ++i) { /* nothing should be deallocated yet */
317 CU_TEST(0 != AllocatedMemory((VOIDPTR)pstr_array[i], sizeof(VOIDPTR)));
318 }
319 #endif
320
321 for (i=0 ; i<50 ; ++i) { /* check out & assign some elements */
322 pstr_array[i] = (char*)pool_get_element(ps);
323 strcpy(pstr_array[i], "01234567890");
324 }
325 pool_get_stats(&stats, ps);
326 CU_TEST(100 == stats.elt_total);
327 CU_TEST(100 == stats.elt_taken);
328 CU_TEST(100 == stats.elt_inuse);
329 CU_TEST(0 == stats.elt_onlist);
330 CU_TEST((12*sizeof(char) + (12*sizeof(char)%sizeof(void*))) == stats.elt_size);
331 CU_TEST(10 == stats.str_len);
332 CU_TEST(10 == stats.str_wid);
333
334 if (NULL != ps) /* destroy the store */
335 pool_destroy_store(ps);
336
337 #ifdef MALLOC_DEBUG
338 for (i=0 ; i<100 ; ++i) { /* confirm destruction of the elements */
339 if (0 != AllocatedMemory((VOIDPTR)pstr_array[i], sizeof(VOIDPTR))) {
340 snprintf(str_error_msg, 100, "%s%d%s", "pstr_array[", i, "] not deallocated.");
341 CU_FAIL(str_error_msg);
342 }
343 }
344 #endif
345
346 /* test pool_clear_store() */
347
348 ps = pool_create_store(10, 10, sizeof(int), 10, 10);/* create a store for ints */
349 CU_TEST(NULL != ps);
350
351 /* set up pooling & recycling */
352 if (FALSE == gl_pool_initialized()) {
353 gl_init();
354 gl_init_pool();
355 i_initialized_lists = TRUE;
356 }
357 pint_list = gl_create(100);
358
359 for (i=0 ; i<100 ; ++i) { /* check out the elements */
360 gl_append_ptr(pint_list, pool_get_element(ps));
361 }
362 pool_get_stats(&stats, ps);
363 CU_TEST(100 == stats.elt_total);
364 CU_TEST(100 == stats.elt_taken);
365 CU_TEST(100 == stats.elt_inuse);
366 CU_TEST(0 == stats.elt_onlist);
367 CU_TEST(sizeof(int) == stats.elt_size);
368 CU_TEST(10 == stats.str_len);
369 CU_TEST(10 == stats.str_wid);
370
371 pool_clear_store(ps); /* clear the pool store */
372 pool_get_stats(&stats, ps);
373 CU_TEST(100 == stats.elt_total);
374 CU_TEST(0 == stats.elt_taken);
375 CU_TEST(0 == stats.elt_inuse);
376 CU_TEST(0 == stats.elt_onlist);
377 CU_TEST(sizeof(int) == stats.elt_size);
378 CU_TEST(10 == stats.str_len);
379 CU_TEST(10 == stats.str_wid);
380
381 CU_TEST(0 == gl_sorted(pint_list)); /* make sure list isn't sorted */
382 for (i=0 ; i<100 ; ++i) { /* check out the elements again & confirm they are the same */
383 pint_array[i] = (int*)pool_get_element(ps);
384 CU_TEST(0 != gl_ptr_search(pint_list, (VOIDPTR)pint_array[i], FALSE));
385 #ifdef MALLOC_DEBUG
386 CU_TEST(0 != AllocatedMemory((VOIDPTR)pint_array[i], sizeof(VOIDPTR)));
387 #endif
388 }
389
390 gl_destroy(pint_list);
391 if (TRUE == i_initialized_lists) {
392 gl_destroy_pool();
393 }
394
395 if (NULL != ps) /* destroy the store */
396 pool_destroy_store(ps);
397
398 #ifdef MALLOC_DEBUG
399 for (i=0 ; i<100 ; ++i) { /* confirm destruction of the elements */
400 CU_TEST(0 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(VOIDPTR)));
401 }
402 #endif
403
404 CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */
405 }
406
407 /*===========================================================================*/
408 /* Registration information */
409
410 static CU_TestInfo list_test_pool[] = {
411 {"test_pool", test_pool},
412 CU_TEST_INFO_NULL
413 };
414
415 static CU_SuiteInfo suites[] = {
416 {"test_general_pool", NULL, NULL, list_test_pool},
417 CU_SUITE_INFO_NULL
418 };
419
420 /*-------------------------------------------------------------------*/
421 CU_ErrorCode test_register_general_pool(void)
422 {
423 return CU_register_suites(suites);
424 }

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22