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

Contents of /trunk/base/generic/utilities/test/test_mem.c

Parent Directory Parent Directory | Revision Log Revision Log


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

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