/[ascend]/trunk/ascend/utilities/test/test_mem.c
ViewVC logotype

Contents of /trunk/ascend/utilities/test/test_mem.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2527 - (show annotations) (download) (as text)
Fri Jan 20 00:44:37 2012 UTC (10 years, 5 months ago) by jpye
File MIME type: text/x-csrc
File size: 26515 byte(s)
Disabled MMIO output functions when MMIO not built.
Rewrite error messages from mem.c to use ERROR_REPORTER_HERE.
Trying to track down source of errors in test_mem.c.
1 /*
2 * Unit test functions for ASCEND: utilities/mem.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 /* FIXME this test needs to move to 'general'. */
25
26 #include <stdio.h>
27 #include <ascend/general/platform.h>
28 #include <ascend/general/ascMalloc.h>
29 #include <ascend/general/mem.h>
30 #include <ascend/general/list.h>
31
32 #include <test/common.h>
33
34 #define STR_LEN 100
35
36 static void test_mem(void)
37 {
38 char str1[STR_LEN];
39 char str2[STR_LEN];
40 char str3[STR_LEN];
41 int str1_bad;
42 int str2_bad;
43 unsigned word;
44
45 mem_store_t ms;
46 struct mem_statistics stats;
47 int i;
48 int *pint_array[100];
49 double *pdoub_array[100];
50 char *pstr_array[100];
51 struct gl_list_t *pint_list;
52 #ifdef MALLOC_DEBUG
53 char str_error_msg[100];
54 #endif
55 int i_initialized_lists = FALSE;
56 unsigned long prior_meminuse;
57
58 prior_meminuse = ascmeminuse(); /* save meminuse() at start of test function */
59
60 #ifndef MALLOC_DEBUG
61 CU_FAIL("test_mem() compiled without MALLOC_DEBUG - memory management not tested.");
62 #endif
63
64 /* test mem_move_cast() */
65
66 memset(str1, '\0', STR_LEN);
67 memset(str2, '*', STR_LEN);
68
69 str1_bad = FALSE; /* test initial condition */
70 str2_bad = FALSE;
71 for (i=0 ; i<STR_LEN ; ++i) {
72 if (!str1_bad && (str1[i] != '\0')) {
73 SNPRINTF(str3, STR_LEN-1, "str1[%d] != '\\0' in test_mem().", i);
74 CU_FAIL(str3);
75 str1_bad = TRUE;
76 }
77 if (!str2_bad && (str2[i] != '*')) {
78 SNPRINTF(str3, STR_LEN-1, "str2[%d] != '*' in test_mem().", i);
79 CU_FAIL(str3);
80 str2_bad = TRUE;
81 }
82 }
83 if (!str1_bad && !str2_bad) CU_PASS("str1 and str2 check out.");
84
85 mem_move_cast(str2, str1, STR_LEN/2); /* copy part of a memory block */
86
87 str1_bad = FALSE;
88 str2_bad = FALSE;
89 for (i=0 ; i<STR_LEN/2 ; ++i) {
90 if (!str1_bad && (str1[i] != '*')) {
91 SNPRINTF(str3, STR_LEN-1, "str1[%d] != '*' in test_mem().", i);
92 CU_FAIL(str3);
93 str1_bad = TRUE;
94 }
95 }
96 for (i=STR_LEN/2 ; i<STR_LEN-1 ; ++i) {
97 if (!str1_bad && (str1[i] != '\0')) {
98 SNPRINTF(str3, STR_LEN-1, "str1[%d] != '\\0' in test_mem().", i);
99 CU_FAIL(str3);
100 str1_bad = TRUE;
101 }
102 }
103 for (i=0 ; i<STR_LEN ; ++i) {
104 if (!str2_bad && (str2[i] != '*')) {
105 SNPRINTF(str3, STR_LEN-1, "str2[%d] != '*' in test_mem().", i);
106 CU_FAIL(str3);
107 str2_bad = TRUE;
108 }
109 }
110 if (!str1_bad && !str2_bad) CU_PASS("str1 and str2 check out.");
111
112 memset(str1, '\0', STR_LEN);
113 memset(str2, '*', STR_LEN);
114
115 mem_move_cast(str2, str1, STR_LEN); /* copy all of a memory block */
116
117 str1_bad = FALSE;
118 str2_bad = FALSE;
119 for (i=0 ; i<STR_LEN ; ++i) {
120 if (!str1_bad && (str1[i] != '*')) {
121 SNPRINTF(str3, STR_LEN-1, "str1[%d] != '*' in test_mem().", i);
122 CU_FAIL(str3);
123 str1_bad = TRUE;
124 }
125 if (!str2_bad && (str2[i] != '*')) {
126 SNPRINTF(str3, STR_LEN-1, "str2[%d] != '*' in test_mem().", i);
127 CU_FAIL(str3);
128 str2_bad = TRUE;
129 }
130 }
131 if (!str1_bad && !str2_bad) CU_PASS("str1 and str2 check out.");
132
133 memset(str1, '~', 10);
134 memset(str1+10, '=', 10);
135 memset(str1+20, '|', 10);
136
137 mem_move_cast(str1, str1+10, 20); /* copy overlapping memory block */
138
139 str1_bad = FALSE;
140 for (i=0 ; i<20 ; ++i) {
141 if (!str1_bad && (str1[i] != '~')) {
142 SNPRINTF(str3, STR_LEN-1, "str1[%d] != '~' in test_mem().", i);
143 CU_FAIL(str3);
144 str1_bad = TRUE;
145 }
146 }
147 for (i=20 ; i<30 ; ++i) {
148 if (!str1_bad && (str1[i] != '=')) {
149 SNPRINTF(str3, STR_LEN-1, "str1[%d] != '=' in test_mem().", i);
150 CU_FAIL(str3);
151 str1_bad = TRUE;
152 }
153 }
154 if (!str1_bad) CU_PASS("str1 and str2 check out.");
155
156 memset(str1, '~', 10);
157 memset(str1+10, '=', 10);
158 memset(str1+20, '|', 10);
159
160 mem_move_cast(str1+10, str1, 20); /* copy overlapping memory block */
161
162 str1_bad = FALSE;
163 for (i=0 ; i<10 ; ++i) {
164 if (!str1_bad && (str1[i] != '=')) {
165 SNPRINTF(str3, STR_LEN-1, "str1[%d] != '~' in test_mem().", i);
166 CU_FAIL(str3);
167 str1_bad = TRUE;
168 }
169 }
170 for (i=10 ; i<30 ; ++i) {
171 if (!str1_bad && (str1[i] != '|')) {
172 SNPRINTF(str3, STR_LEN-1, "str1[%d] != '=' in test_mem().", i);
173 CU_FAIL(str3);
174 str1_bad = TRUE;
175 }
176 }
177 if (!str1_bad) CU_PASS("str1 and str2 check out.");
178
179 SNPRINTF(str1, STR_LEN-1, "This is yet another dumb string");
180 mem_move_cast(str1, str1, strlen(str1)); /* to == from */
181 CU_TEST(0 == strcmp(str1, "This is yet another dumb string"));
182
183 mem_move_cast(str2, str1, 0); /* n = 0 */
184 CU_TEST(0 == strcmp(str1, "This is yet another dumb string"));
185
186 /* test mem_copy_cast() */
187
188 memset(str1, '\0', STR_LEN);
189 memset(str2, '*', STR_LEN);
190
191 str1_bad = FALSE; /* test initial condition */
192 str2_bad = FALSE;
193 for (i=0 ; i<STR_LEN ; ++i) {
194 if (!str1_bad && (str1[i] != '\0')) {
195 SNPRINTF(str3, STR_LEN-1, "str1[%d] != '\\0' in test_mem().", i);
196 CU_FAIL(str3);
197 str1_bad = TRUE;
198 }
199 if (!str2_bad && (str2[i] != '*')) {
200 SNPRINTF(str3, STR_LEN-1, "str2[%d] != '*' in test_mem().", i);
201 CU_FAIL(str3);
202 str2_bad = TRUE;
203 }
204 }
205 if (!str1_bad && !str2_bad) CU_PASS("str1 and str2 check out.");
206
207 mem_copy_cast(str2, str1, STR_LEN/2); /* copy part of a memory block */
208
209 str1_bad = FALSE;
210 str2_bad = FALSE;
211 for (i=0 ; i<STR_LEN/2 ; ++i) {
212 if (!str1_bad && (str1[i] != '*')) {
213 SNPRINTF(str3, STR_LEN-1, "str1[%d] != '*' in test_mem().", i);
214 CU_FAIL(str3);
215 str1_bad = TRUE;
216 }
217 }
218 for (i=STR_LEN/2 ; i<STR_LEN-1 ; ++i) {
219 if (!str1_bad && (str1[i] != '\0')) {
220 SNPRINTF(str3, STR_LEN-1, "str1[%d] != '\\0' in test_mem().", i);
221 CU_FAIL(str3);
222 str1_bad = TRUE;
223 }
224 }
225 for (i=0 ; i<STR_LEN ; ++i) {
226 if (!str2_bad && (str2[i] != '*')) {
227 SNPRINTF(str3, STR_LEN-1, "str2[%d] != '*' in test_mem().", i);
228 CU_FAIL(str3);
229 str2_bad = TRUE;
230 }
231 }
232 if (!str1_bad && !str2_bad) CU_PASS("str1 and str2 check out.");
233
234 memset(str1, '\0', STR_LEN);
235 memset(str2, '*', STR_LEN);
236
237 mem_copy_cast(str2, str1, STR_LEN); /* copy all of a memory block */
238
239 str1_bad = FALSE;
240 str2_bad = FALSE;
241 for (i=0 ; i<STR_LEN ; ++i) {
242 if (!str1_bad && (str1[i] != '*')) {
243 SNPRINTF(str3, STR_LEN-1, "str1[%d] != '*' in test_mem().", i);
244 CU_FAIL(str3);
245 str1_bad = TRUE;
246 }
247 if (!str2_bad && (str2[i] != '*')) {
248 SNPRINTF(str3, STR_LEN-1, "str2[%d] != '*' in test_mem().", i);
249 CU_FAIL(str3);
250 str2_bad = TRUE;
251 }
252 }
253 if (!str1_bad && !str2_bad) CU_PASS("str1 and str2 check out.");
254
255 memset(str1, '~', 10);
256 memset(str1+10, '=', 10);
257 memset(str1+20, '|', 10);
258
259 SNPRINTF(str1, STR_LEN-1, "This is yet another dumb string");
260 mem_copy_cast(str1, str1, strlen(str1)); /* to == from */
261 CU_TEST(0 == strcmp(str1, "This is yet another dumb string"));
262
263 mem_copy_cast(str2, str1, 0); /* n = 0 */
264 CU_TEST(0 == strcmp(str1, "This is yet another dumb string"));
265
266 /* test mem_repl_byte_cast() */
267
268 memset(str1, '=', STR_LEN);
269
270 mem_repl_byte_cast(str1, '#', STR_LEN/2); /* replace part of a memory block */
271
272 str1_bad = FALSE;
273 for (i=0 ; i<STR_LEN/2 ; ++i) {
274 if (!str1_bad && (str1[i] != '#')) {
275 SNPRINTF(str3, STR_LEN-1, "str1[%d] != '\\0' in test_mem().", i);
276 CU_FAIL(str3);
277 str1_bad = TRUE;
278 }
279 }
280 for (i=STR_LEN/2 ; i<STR_LEN-1 ; ++i) {
281 if (!str1_bad && (str1[i] != '=')) {
282 SNPRINTF(str3, STR_LEN-1, "str1[%d] != '=' in test_mem().", i);
283 CU_FAIL(str3);
284 str1_bad = TRUE;
285 }
286 }
287 if (!str1_bad) CU_PASS("str1 checks out after mem_repl_byte_cast().");
288
289 memset(str1, '+', STR_LEN);
290
291 mem_zero_byte_cast(str1, '=', 0); /* 0 bytes processed */
292
293 str1_bad = FALSE;
294 for (i=0 ; i<STR_LEN ; ++i) {
295 if (!str1_bad && (str1[i] != '+')) {
296 SNPRINTF(str3, STR_LEN-1, "str1[%d] != '+' in test_mem().", i);
297 CU_FAIL(str3);
298 str1_bad = TRUE;
299 }
300 }
301 if (!str1_bad) CU_PASS("str1 checks out after mem_zero_byte_cast().");
302
303 /* test mem_zero_byte_cast() */
304
305 memset(str1, '=', STR_LEN);
306
307 mem_zero_byte_cast(str1, '#', STR_LEN/2); /* zero part of a memory block */
308
309 str1_bad = FALSE;
310 for (i=0 ; i<STR_LEN/2 ; ++i) {
311 if (!str1_bad && (str1[i] != '\0')) {
312 SNPRINTF(str3, STR_LEN-1, "str1[%d] != '\\0' in test_mem().", i);
313 CU_FAIL(str3);
314 str1_bad = TRUE;
315 }
316 }
317 for (i=STR_LEN/2 ; i<STR_LEN-1 ; ++i) {
318 if (!str1_bad && (str1[i] != '=')) {
319 SNPRINTF(str3, STR_LEN-1, "str1[%d] != '=' in test_mem().", i);
320 CU_FAIL(str3);
321 str1_bad = TRUE;
322 }
323 }
324 if (!str1_bad) CU_PASS("str1 checks out after mem_zero_byte_cast().");
325
326 memset(str1, '+', STR_LEN);
327
328 mem_zero_byte_cast(str1, '=', 0); /* 0 bytes processed */
329
330 str1_bad = FALSE;
331 for (i=0 ; i<STR_LEN ; ++i) {
332 if (!str1_bad && (str1[i] != '+')) {
333 SNPRINTF(str3, STR_LEN-1, "str1[%d] != '+' in test_mem().", i);
334 CU_FAIL(str3);
335 str1_bad = TRUE;
336 }
337 }
338 if (!str1_bad) CU_PASS("str1 checks out after mem_zero_byte_cast().");
339
340 /* test mem_repl_word_cast() */
341
342 word = 1234567890;
343
344 memset(str1, '=', STR_LEN);
345
346 mem_repl_word_cast(str1, word, 1); /* copy 1 word */
347
348 CU_TEST(*((unsigned *)str1) == word);
349
350 str1_bad = FALSE;
351 for (i=sizeof(unsigned) ; i<STR_LEN-1 ; ++i) {
352 if (!str1_bad && (str1[i] != '=')) {
353 SNPRINTF(str3, STR_LEN-1, "str1[%d] != '=' in test_mem().", i);
354 CU_FAIL(str3);
355 str1_bad = TRUE;
356 }
357 }
358 if (!str1_bad) CU_PASS("str1 checks out after mem_repl_word_cast().");
359
360 memset(str1, '~', STR_LEN);
361
362 mem_repl_word_cast(str1, word, 10); /* copy 5 words */
363
364 for (i=0 ; i<10 ; ++i) {
365 CU_TEST(*(((unsigned *)str1)+1) == word);
366 }
367
368 str1_bad = FALSE;
369 for (i=10*sizeof(unsigned) ; i<STR_LEN-1 ; ++i) {
370 if (!str1_bad && (str1[i] != '~')) {
371 SNPRINTF(str3, STR_LEN-1, "str1[%d] != '~' in test_mem().", i);
372 CU_FAIL(str3);
373 str1_bad = TRUE;
374 }
375 }
376 if (!str1_bad) CU_PASS("str1 checks out after mem_repl_word_cast().");
377
378 memset(str1, '?', STR_LEN);
379
380 mem_repl_word_cast(str1, word, 0); /* copy 0 words */
381
382 str1_bad = FALSE;
383 for (i=0 ; i<STR_LEN-1 ; ++i) {
384 if (!str1_bad && (str1[i] != '?')) {
385 SNPRINTF(str3, STR_LEN-1, "str1[%d] != '?' in test_mem().", i);
386 CU_FAIL(str3);
387 str1_bad = TRUE;
388 }
389 }
390 if (!str1_bad) CU_PASS("str1 checks out after mem_repl_word_cast().");
391
392 /* test mem_set_byte(), mem_get_byte() */
393
394 memset(str1, '\0', STR_LEN);
395 CU_TEST('\0' == mem_get_byte((asc_intptr_t)str1));
396
397 mem_set_byte((asc_intptr_t)str1, 0xff);
398 CU_TEST(0xff == mem_get_byte((POINTER)str1));
399
400 mem_set_byte((POINTER)str1, '\t');
401 CU_TEST('\t' == mem_get_byte((POINTER)str1));
402
403 mem_set_byte((POINTER)str1, '\0');
404 CU_TEST('\0' == mem_get_byte((POINTER)str1));
405
406 /* test mem_set_int(), mem_get_int() */
407
408 memset(str1, '\0', STR_LEN);
409 CU_TEST(0 == mem_get_int((POINTER)str1));
410
411 mem_set_int((POINTER)str1, 0xff72);
412 CU_TEST(0xff72 == mem_get_int((POINTER)str1));
413
414 mem_set_int((POINTER)str1, '\t');
415 CU_TEST('\t' == mem_get_int((POINTER)str1));
416
417 mem_set_int((POINTER)str1, 0);
418 CU_TEST(0 == mem_get_int((POINTER)str1));
419
420 /* test mem_set_long(), mem_get_long() */
421
422 memset(str1, '\0', STR_LEN);
423 CU_TEST(0 == mem_get_long((POINTER)str1));
424
425 mem_set_long((POINTER)str1, 0xff72);
426 CU_TEST(0xff72 == mem_get_long((POINTER)str1));
427
428 mem_set_long((POINTER)str1, '\t');
429 CU_TEST('\t' == mem_get_long((POINTER)str1));
430
431 mem_set_long((POINTER)str1, 0);
432 CU_TEST(0 == mem_get_long((POINTER)str1));
433
434 /* test mem_set_float(), mem_get_float() */
435
436 memset(str1, '\0', STR_LEN);
437 CU_ASSERT_DOUBLE_EQUAL(0.0, mem_get_float((POINTER)str1), 0.001);
438
439 mem_set_float((POINTER)str1, 1.501436);
440 CU_ASSERT_DOUBLE_EQUAL(1.501436, mem_get_float((POINTER)str1), 0.00001);
441
442 mem_set_float((POINTER)str1, 9.3e-10);
443 CU_ASSERT_DOUBLE_EQUAL(9.3e-10, mem_get_float((POINTER)str1), 0.001);
444
445 mem_set_float((POINTER)str1, 0.0);
446 CU_ASSERT_DOUBLE_EQUAL(0.0, mem_get_float((POINTER)str1), 0.001);
447
448 /* test mem_set_double(), mem_get_double() */
449
450 memset(str1, '\0', STR_LEN);
451 CU_ASSERT_DOUBLE_EQUAL(0.0, mem_get_double((POINTER)str1), 0.001);
452
453 mem_set_double((POINTER)str1, 1.501436872625);
454 CU_ASSERT_DOUBLE_EQUAL(1.501436872625, mem_get_double((POINTER)str1), 0.000000000001);
455
456 mem_set_double((POINTER)str1, 9.32626e-154);
457 CU_ASSERT_DOUBLE_EQUAL(9.32626e-154, mem_get_double((POINTER)str1), 0.00001);
458
459 mem_set_double((POINTER)str1, 0.0);
460 CU_ASSERT_DOUBLE_EQUAL(0.0, mem_get_double((POINTER)str1), 0.001);
461
462 /* test mem_set_unsigned(), mem_get_unsigned() */
463
464 memset(str1, '\0', STR_LEN);
465 CU_TEST(0 == mem_get_unsigned((POINTER)str1));
466
467 mem_set_unsigned((POINTER)str1, 0xff72);
468 CU_TEST(0xff72 == mem_get_unsigned((POINTER)str1));
469
470 mem_set_unsigned((POINTER)str1, '\t');
471 CU_TEST('\t' == mem_get_unsigned((POINTER)str1));
472
473 mem_set_unsigned((POINTER)str1, 0);
474 CU_TEST(0 == mem_get_unsigned((POINTER)str1));
475
476 /* test mem_create_store(), mem_destroy_store() */
477
478 ms = mem_create_store(0, 10, sizeof(int), 10, 10); /* create a store - length < 1 (error) */
479 CU_TEST(NULL == ms);
480 if(NULL != ms)mem_destroy_store(ms);
481
482 ms = mem_create_store(10, 0, sizeof(int), 10, 10); /* create a store - width < 1 (error) */
483 CU_TEST(NULL == ms);
484 if(NULL != ms)mem_destroy_store(ms);
485
486 ms = mem_create_store(10, 10, 0, 10, 10); /* create a store - eltsze < 1 (ok) */
487 CU_TEST(NULL != ms);
488 mem_get_stats(&stats, ms);
489 CU_TEST(100 == stats.elt_total);
490 CU_TEST(0 == stats.elt_taken);
491 CU_TEST(0 == stats.elt_inuse);
492 CU_TEST(0 == stats.elt_onlist);
493 CU_TEST(0 == stats.elt_size);
494 CU_TEST(10 == stats.str_len);
495 CU_TEST(10 == stats.str_wid);
496
497 if(NULL != ms)mem_destroy_store(ms);
498
499 ms = mem_create_store(10, 10, sizeof(int), 10, 10);/* create a store for ints */
500 CU_TEST(NULL != ms);
501 mem_get_stats(&stats, ms);
502 CU_TEST(100 == stats.elt_total);
503 CU_TEST(0 == stats.elt_taken);
504 CU_TEST(0 == stats.elt_inuse);
505 CU_TEST(0 == stats.elt_onlist);
506 CU_TEST(sizeof(int) == stats.elt_size);
507 CU_TEST(10 == stats.str_len);
508 CU_TEST(10 == stats.str_wid);
509
510 for (i=0 ; i<100 ; ++i) { /* check out some elements */
511 pint_array[i] = (int*)mem_get_element(ms);
512 #ifdef MALLOC_DEBUG
513 CU_TEST(0 != AllocatedMemory((VOIDPTR)pint_array[i], sizeof(VOIDPTR)));
514 #endif
515 }
516
517 if (NULL != ms) /* destroy the store */
518 mem_destroy_store(ms);
519
520 #ifdef MALLOC_DEBUG
521 for (i=0 ; i<100 ; ++i) { /* confirm destruction of the elements */
522 CU_TEST(0 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(VOIDPTR)));
523 }
524 #endif
525
526 /* test mem_get_element(), mem_free_element() */
527
528 ms = mem_create_store(10, 10, sizeof(int), 10, 10);/* create a store for ints */
529 CU_TEST(NULL != ms);
530
531 CU_TEST(NULL == mem_get_element(NULL)); /* requesting element of a NULL store has no effect */
532 CU_TEST(100 == stats.elt_total);
533 CU_TEST(0 == stats.elt_taken);
534 CU_TEST(0 == stats.elt_inuse);
535 CU_TEST(0 == stats.elt_onlist);
536 CU_TEST(sizeof(int) == stats.elt_size);
537 CU_TEST(10 == stats.str_len);
538 CU_TEST(10 == stats.str_wid);
539
540 for (i=0 ; i<50 ; ++i) { /* check out & assign some elements */
541 pint_array[i] = (int*)mem_get_element(ms);
542 *pint_array[i] = i;
543 }
544 mem_get_stats(&stats, ms);
545 CU_TEST(100 == stats.elt_total);
546 CU_TEST(50 == stats.elt_taken);
547 CU_TEST(50 == stats.elt_inuse);
548 CU_TEST(0 == stats.elt_onlist);
549 CU_TEST(sizeof(int) == stats.elt_size);
550 CU_TEST(10 == stats.str_len);
551 CU_TEST(10 == stats.str_wid);
552
553 for (i=50 ; i<100 ; ++i) { /* check out & assign more elements */
554 pint_array[i] = (int*)mem_get_element(ms);
555 *pint_array[i] = i;
556 }
557 mem_get_stats(&stats, ms);
558 CU_TEST(100 == stats.elt_total);
559 CU_TEST(100 == stats.elt_taken);
560 CU_TEST(100 == stats.elt_inuse);
561 CU_TEST(0 == stats.elt_onlist);
562 CU_TEST(sizeof(int) == stats.elt_size);
563 CU_TEST(10 == stats.str_len);
564 CU_TEST(10 == stats.str_wid);
565
566 for (i=0 ; i<100 ; ++i) {
567 CU_TEST(i == *pint_array[i]); /* make sure element values are ok */
568 }
569
570 mem_free_element(ms, NULL); /* returning a NULL element has no effect */
571 mem_get_stats(&stats, ms);
572 CU_TEST(100 == stats.elt_total);
573 CU_TEST(100 == stats.elt_taken);
574 CU_TEST(100 == stats.elt_inuse);
575 CU_TEST(0 == stats.elt_onlist);
576 CU_TEST(sizeof(int) == stats.elt_size);
577 CU_TEST(10 == stats.str_len);
578 CU_TEST(10 == stats.str_wid);
579
580 for (i=0 ; i<50 ; ++i) { /* return some elements */
581 mem_free_element(ms, pint_array[i]);
582 }
583 mem_get_stats(&stats, ms);
584 CU_TEST(100 == stats.elt_total);
585 CU_TEST(100 == stats.elt_taken);
586 CU_TEST(50 == stats.elt_inuse);
587 CU_TEST(50 == stats.elt_onlist);
588 CU_TEST(sizeof(int) == stats.elt_size);
589 CU_TEST(10 == stats.str_len);
590 CU_TEST(10 == stats.str_wid);
591 #ifdef MALLOC_DEBUG
592 for (i=0 ; i<100 ; ++i) { /* nothing should be deallocated yet */
593 CU_TEST(0 != AllocatedMemory((VOIDPTR)pint_array[i], sizeof(VOIDPTR)));
594 }
595 #endif
596
597 for (i=0 ; i<50 ; ++i) { /* check out & assign some elements */
598 pint_array[i] = (int*)mem_get_element(ms);
599 *pint_array[i] = i;
600 }
601 mem_get_stats(&stats, ms);
602 CU_TEST(100 == stats.elt_total);
603 CU_TEST(100 == stats.elt_taken);
604 CU_TEST(100 == stats.elt_inuse);
605 CU_TEST(0 == stats.elt_onlist);
606 CU_TEST(sizeof(int) == stats.elt_size);
607 CU_TEST(10 == stats.str_len);
608 CU_TEST(10 == stats.str_wid);
609
610 if (NULL != ms) /* destroy the store */
611 mem_destroy_store(ms);
612
613 #ifdef MALLOC_DEBUG
614 for (i=0 ; i<100 ; ++i) { /* confirm destruction of the elements */
615 CU_TEST(0 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(VOIDPTR)));
616 }
617 #endif
618
619 ms = mem_create_store(10, 10, sizeof(double), 10, 10);/* create a store for doubles */
620 CU_TEST(NULL != ms);
621
622 for (i=0 ; i<50 ; ++i) { /* check out & assign some elements */
623 pdoub_array[i] = (double*)mem_get_element(ms);
624 *pdoub_array[i] = (double)i;
625 }
626 mem_get_stats(&stats, ms);
627 CU_TEST(100 == stats.elt_total);
628 CU_TEST(50 == stats.elt_taken);
629 CU_TEST(50 == stats.elt_inuse);
630 CU_TEST(0 == stats.elt_onlist);
631 CU_TEST(sizeof(double) == stats.elt_size);
632 CU_TEST(10 == stats.str_len);
633 CU_TEST(10 == stats.str_wid);
634
635 for (i=50 ; i<100 ; ++i) { /* check out & assign more elements */
636 pdoub_array[i] = (double*)mem_get_element(ms);
637 *pdoub_array[i] = (double)i;
638 }
639 mem_get_stats(&stats, ms);
640 CU_TEST(100 == stats.elt_total);
641 CU_TEST(100 == stats.elt_taken);
642 CU_TEST(100 == stats.elt_inuse);
643 CU_TEST(0 == stats.elt_onlist);
644 CU_TEST(sizeof(double) == stats.elt_size);
645 CU_TEST(10 == stats.str_len);
646 CU_TEST(10 == stats.str_wid);
647
648 for (i=0 ; i<100 ; ++i) {
649 CU_ASSERT_DOUBLE_EQUAL((double)i, *pdoub_array[i], 0.001); /* make sure element values are ok */
650 }
651
652 for (i=0 ; i<50 ; ++i) { /* return some elements */
653 mem_free_element(ms, pdoub_array[i]);
654 }
655 mem_get_stats(&stats, ms);
656 CU_TEST(100 == stats.elt_total);
657 CU_TEST(100 == stats.elt_taken);
658 CU_TEST(50 == stats.elt_inuse);
659 CU_TEST(50 == stats.elt_onlist);
660 CU_TEST(sizeof(double) == stats.elt_size);
661 CU_TEST(10 == stats.str_len);
662 CU_TEST(10 == stats.str_wid);
663 #ifdef MALLOC_DEBUG
664 for (i=0 ; i<100 ; ++i) { /* nothing should be deallocated yet */
665 CU_TEST(0 != AllocatedMemory((VOIDPTR)pdoub_array[i], sizeof(VOIDPTR)));
666 }
667 #endif
668
669 for (i=0 ; i<50 ; ++i) { /* check out & assign some elements */
670 pdoub_array[i] = (double*)mem_get_element(ms);
671 *pdoub_array[i] = (double)i;
672 }
673 mem_get_stats(&stats, ms);
674 CU_TEST(100 == stats.elt_total);
675 CU_TEST(100 == stats.elt_taken);
676 CU_TEST(100 == stats.elt_inuse);
677 CU_TEST(0 == stats.elt_onlist);
678 CU_TEST(sizeof(double) == stats.elt_size);
679 CU_TEST(10 == stats.str_len);
680 CU_TEST(10 == stats.str_wid);
681
682 if (NULL != ms) /* destroy the store */
683 mem_destroy_store(ms);
684
685 #ifdef MALLOC_DEBUG
686 for (i=0 ; i<100 ; ++i) { /* confirm destruction of the elements */
687 CU_TEST(0 == AllocatedMemory((VOIDPTR)pdoub_array[i], sizeof(VOIDPTR)));
688 }
689 #endif
690
691 ms = mem_create_store(10, 10, 12*sizeof(char), 10, 10);/* create a store for strings */
692 CU_TEST(NULL != ms);
693
694 for (i=0 ; i<50 ; ++i) { /* check out & assign some elements */
695 pstr_array[i] = (char*)mem_get_element(ms);
696 strcpy(pstr_array[i], "01234567890");
697 }
698 mem_get_stats(&stats, ms);
699 CU_TEST(100 == stats.elt_total);
700 CU_TEST(50 == stats.elt_taken);
701 CU_TEST(50 == stats.elt_inuse);
702 CU_TEST(0 == stats.elt_onlist);
703 CU_TEST((12*sizeof(char) + (12*sizeof(char)%sizeof(void*))) == stats.elt_size);
704 CU_TEST(10 == stats.str_len);
705 CU_TEST(10 == stats.str_wid);
706
707 for (i=50 ; i<100 ; ++i) { /* check out & assign more elements */
708 pstr_array[i] = (char*)mem_get_element(ms);
709 strcpy(pstr_array[i], "01234567890");
710 }
711 mem_get_stats(&stats, ms);
712 CU_TEST(100 == stats.elt_total);
713 CU_TEST(100 == stats.elt_taken);
714 CU_TEST(100 == stats.elt_inuse);
715 CU_TEST(0 == stats.elt_onlist);
716 CU_TEST((12*sizeof(char) + (12*sizeof(char)%sizeof(void*))) == stats.elt_size);
717 CU_TEST(10 == stats.str_len);
718 CU_TEST(10 == stats.str_wid);
719
720 for (i=0 ; i<100 ; ++i) {
721 CU_ASSERT_STRING_EQUAL("01234567890", pstr_array[i]); /* make sure element values are ok */
722 }
723
724 for (i=0 ; i<50 ; ++i) { /* return some elements */
725 mem_free_element(ms, pstr_array[i]);
726 }
727 mem_get_stats(&stats, ms);
728 CU_TEST(100 == stats.elt_total);
729 CU_TEST(100 == stats.elt_taken);
730 CU_TEST(50 == stats.elt_inuse);
731 CU_TEST(50 == stats.elt_onlist);
732 CU_TEST((12*sizeof(char) + (12*sizeof(char)%sizeof(void*))) == stats.elt_size);
733 CU_TEST(10 == stats.str_len);
734 CU_TEST(10 == stats.str_wid);
735 #ifdef MALLOC_DEBUG
736 for (i=0 ; i<100 ; ++i) { /* nothing should be deallocated yet */
737 CU_TEST(0 != AllocatedMemory((VOIDPTR)pstr_array[i], sizeof(VOIDPTR)));
738 }
739 #endif
740
741 for (i=0 ; i<50 ; ++i) { /* check out & assign some elements */
742 pstr_array[i] = (char*)mem_get_element(ms);
743 strcpy(pstr_array[i], "01234567890");
744 }
745 mem_get_stats(&stats, ms);
746 CU_TEST(100 == stats.elt_total);
747 CU_TEST(100 == stats.elt_taken);
748 CU_TEST(100 == stats.elt_inuse);
749 CU_TEST(0 == stats.elt_onlist);
750 CU_TEST((12*sizeof(char) + (12*sizeof(char)%sizeof(void*))) == stats.elt_size);
751 CU_TEST(10 == stats.str_len);
752 CU_TEST(10 == stats.str_wid);
753
754 if (NULL != ms) /* destroy the store */
755 mem_destroy_store(ms);
756
757 #ifdef MALLOC_DEBUG
758 for (i=0 ; i<100 ; ++i) { /* confirm destruction of the elements */
759 if (0 != AllocatedMemory((VOIDPTR)pstr_array[i], sizeof(VOIDPTR))) {
760 SNPRINTF(str_error_msg, 100, "%s%d%s", "pstr_array[", i, "] not deallocated.");
761 CU_FAIL(str_error_msg);
762 }
763 }
764 #endif
765
766 /* test mem_clear_store() */
767
768 ms = mem_create_store(10, 10, sizeof(int), 10, 10);/* create a store for ints */
769 CU_TEST(NULL != ms);
770
771 /* set up pooling & recycling */
772 if (FALSE == gl_pool_initialized()) {
773 gl_init();
774 gl_init_pool();
775 i_initialized_lists = TRUE;
776 }
777 pint_list = gl_create(100);
778
779 for (i=0 ; i<100 ; ++i) { /* check out the elements */
780 gl_append_ptr(pint_list, mem_get_element(ms));
781 }
782 mem_get_stats(&stats, ms);
783 CU_TEST(100 == stats.elt_total);
784 CU_TEST(100 == stats.elt_taken);
785 CU_TEST(100 == stats.elt_inuse);
786 CU_TEST(0 == stats.elt_onlist);
787 CU_TEST(sizeof(int) == stats.elt_size);
788 CU_TEST(10 == stats.str_len);
789 CU_TEST(10 == stats.str_wid);
790
791 mem_clear_store(ms); /* clear the pool store */
792 mem_get_stats(&stats, ms);
793 CU_TEST(100 == stats.elt_total);
794 CU_TEST(0 == stats.elt_taken);
795 CU_TEST(0 == stats.elt_inuse);
796 CU_TEST(0 == stats.elt_onlist);
797 CU_TEST(sizeof(int) == stats.elt_size);
798 CU_TEST(10 == stats.str_len);
799 CU_TEST(10 == stats.str_wid);
800
801 CU_TEST(0 == gl_sorted(pint_list)); /* make sure list isn't sorted */
802 for (i=0 ; i<100 ; ++i) { /* check out the elements again & confirm they are the same */
803 pint_array[i] = (int*)mem_get_element(ms);
804 CU_TEST(0 != gl_ptr_search(pint_list, (VOIDPTR)pint_array[i], FALSE));
805 #ifdef MALLOC_DEBUG
806 CU_TEST(0 != AllocatedMemory((VOIDPTR)pint_array[i], sizeof(VOIDPTR)));
807 #endif
808 }
809
810 gl_destroy(pint_list);
811
812 if (TRUE == i_initialized_lists) {
813 gl_destroy_pool();
814 }
815
816 if (NULL != ms) /* destroy the store */
817 mem_destroy_store(ms);
818
819 #ifdef MALLOC_DEBUG
820 for (i=0 ; i<100 ; ++i) { /* confirm destruction of the elements */
821 CU_TEST(0 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(VOIDPTR)));
822 }
823 #endif
824
825 CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */
826 }
827
828 /*===========================================================================*/
829 /* Registration information */
830
831 #define TESTS(T) \
832 T(mem)
833
834 REGISTER_TESTS_SIMPLE(utilities_mem, TESTS)
835

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