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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 59 - (show annotations) (download) (as text)
Sun Oct 30 01:38:20 2005 UTC (18 years, 7 months ago) by jds
File MIME type: text/x-csrc
File size: 38792 byte(s)
- prototype unit test suite based on CUnit added.
- unit tests for base/generic/general and base/generic/utilites added.
- 2nd manual rework of doxygen documentation in general and utilities.
- bug fixes (mostly general & utilities) found during test development.
- added asc_assert prototype to redirect failures to Asc_Panic() and enable decoupling assertions from NDEBUG.
- some modifications of interface & implementation to facilitate testing.
- utilities/ascPrint & utilities/ascMalloc functions now always included in base libs to minimize recompilation when an interface chooses different options.
1 /*
2 * Unit test functions for ASCEND: utilities/ascMalloc.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 <io.h>
26 #include "utilities/ascConfig.h"
27 #include "utilities/ascMalloc.h"
28 #include "utilities/ascPanic.h"
29 #include "CUnit/CUnit.h"
30 #include "test_ascMalloc.h"
31 #include "assertimpl.h"
32
33 #undef STR_LEN
34 #define STR_LEN 100
35
36 /*
37 * ascMalloc.c is a challenging module to test. There are
38 * numerous different definitions for the function macros
39 * depending on the definitions of MOD_ASCMALLOC, MOD_REALLOC,
40 * MALLOC_DEBUG, and ALLOCATED_TESTS.
41 *
42 * As a first pass, we will only test a single set of definitions.
43 * This is whatever set is defined based on the settings of these
44 * macros at compilation time.
45 */
46 static void test_ascMalloc(void)
47 {
48 char str1[STR_LEN];
49 char str2[STR_LEN];
50 char str3[STR_LEN];
51 char *p_str1;
52 char *p_str2;
53 char *p_str3;
54 unsigned long i;
55 int str1_bad;
56 int str2_bad;
57 int *p_int1;
58 int *p_int2;
59 int *p_int3;
60 double *p_doub1;
61 double *p_doub2;
62 unsigned long prior_meminuse;
63
64 prior_meminuse = ascmeminuse(); /* save meminuse() at start of test function */
65
66 #ifdef NDEBUG
67 CU_FAIL("test_ascMalloc() compiled with NDEBUG - some features not tested.");
68 #endif
69 #ifndef MALLOC_DEBUG
70 CU_FAIL("test_ascMalloc() compiled without MALLOC_DEBUG - memory management not tested.");
71 #endif
72
73 /* test ascstrdup() */
74 CU_TEST(NULL == ascstrdup(NULL)); /* NULL str */
75
76 p_str1 = ascstrdup("Just a simple little string."); /* normal operation with literal*/
77 CU_TEST(NULL != p_str1);
78 CU_TEST(0 == strcmp(p_str1, "Just a simple little string."));
79 CU_TEST(0 != ascmeminuse());
80 ascfree(p_str1);
81 CU_TEST(0 == ascmeminuse());
82
83 snprintf(str1, STR_LEN-1, "I'm a simple string.");
84 p_str1 = ascstrdup(str1); /* normal operation with literal*/
85 CU_TEST(NULL != p_str1);
86 CU_TEST(0 == strcmp(p_str1, str1));
87 CU_TEST(0 != ascmeminuse());
88 ascfree(p_str1);
89 CU_TEST(0 == ascmeminuse());
90
91 /* test asc_memcpy() */
92
93 #ifndef ASC_NO_ASSERTIONS
94 asc_assert_catch(TRUE); /* prepare to test assertions */
95
96 asc_assert_reset();
97 if (0 == setjmp(g_asc_test_env))
98 asc_memcpy(NULL, str2, STR_LEN); /* error - NULL dest */
99 CU_TEST(TRUE == asc_assert_failed());
100
101 asc_assert_reset();
102 if (0 == setjmp(g_asc_test_env))
103 asc_memcpy(str1, NULL, STR_LEN); /* error - NULL src */
104 CU_TEST(TRUE == asc_assert_failed());
105
106 asc_assert_catch(FALSE); /* done testing assertions */
107 #endif /* !ASC_NO_ASSERTIONS */
108
109 memset(str1, '\0', STR_LEN);
110 memset(str2, '*', STR_LEN);
111
112 str1_bad = FALSE; /* test initial condition */
113 str2_bad = FALSE;
114 for (i=0 ; i<STR_LEN ; ++i) {
115 if (!str1_bad && (str1[i] != '\0')) {
116 snprintf(str3, STR_LEN-1, "str1[%lu] != '\\0' in test_ascMalloc().", i);
117 CU_FAIL(str3);
118 str1_bad = TRUE;
119 }
120 if (!str2_bad && (str2[i] != '*')) {
121 snprintf(str3, STR_LEN-1, "str2[%lu] != '*' in test_ascMalloc().", i);
122 CU_FAIL(str3);
123 str2_bad = TRUE;
124 }
125 }
126 if (!str1_bad && !str2_bad) CU_PASS("str1 and str2 check out.");
127
128 CU_TEST(str1 == asc_memcpy(str1, str2, STR_LEN/2)); /* copy part of a memory block */
129
130 str1_bad = FALSE;
131 str2_bad = FALSE;
132 for (i=0 ; i<STR_LEN/2 ; ++i) {
133 if (!str1_bad && (str1[i] != '*')) {
134 snprintf(str3, STR_LEN-1, "str1[%lu] != '*' in test_ascMalloc().", i);
135 CU_FAIL(str3);
136 str1_bad = TRUE;
137 }
138 }
139 for (i=STR_LEN/2 ; i<STR_LEN-1 ; ++i) {
140 if (!str1_bad && (str1[i] != '\0')) {
141 snprintf(str3, STR_LEN-1, "str1[%lu] != '\\0' in test_ascMalloc().", i);
142 CU_FAIL(str3);
143 str1_bad = TRUE;
144 }
145 }
146 for (i=0 ; i<STR_LEN ; ++i) {
147 if (!str2_bad && (str2[i] != '*')) {
148 snprintf(str3, STR_LEN-1, "str2[%lu] != '*' in test_ascMalloc().", i);
149 CU_FAIL(str3);
150 str2_bad = TRUE;
151 }
152 }
153 if (!str1_bad && !str2_bad) CU_PASS("str1 and str2 check out.");
154
155 memset(str1, '\0', STR_LEN);
156 memset(str2, '*', STR_LEN);
157
158 CU_TEST(str1 == asc_memcpy(str1, str2, STR_LEN)); /* copy all of a memory block */
159
160 str1_bad = FALSE;
161 str2_bad = FALSE;
162 for (i=0 ; i<STR_LEN ; ++i) {
163 if (!str1_bad && (str1[i] != '*')) {
164 snprintf(str3, STR_LEN-1, "str1[%lu] != '*' in test_ascMalloc().", i);
165 CU_FAIL(str3);
166 str1_bad = TRUE;
167 }
168 if (!str2_bad && (str2[i] != '*')) {
169 snprintf(str3, STR_LEN-1, "str2[%lu] != '*' in test_ascMalloc().", i);
170 CU_FAIL(str3);
171 str2_bad = TRUE;
172 }
173 }
174 if (!str1_bad && !str2_bad) CU_PASS("str1 and str2 check out.");
175
176 memset(str1, '~', 10);
177 memset(str1+10, '=', 10);
178 memset(str1+20, '|', 10);
179
180 CU_TEST((str1+10) == asc_memcpy(str1+10, str1, 20)); /* copy overlapping memory block */
181
182 str1_bad = FALSE;
183 for (i=0 ; i<20 ; ++i) {
184 if (!str1_bad && (str1[i] != '~')) {
185 snprintf(str3, STR_LEN-1, "str1[%lu] != '~' in test_ascMalloc().", i);
186 CU_FAIL(str3);
187 str1_bad = TRUE;
188 }
189 }
190 for (i=20 ; i<30 ; ++i) {
191 if (!str1_bad && (str1[i] != '=')) {
192 snprintf(str3, STR_LEN-1, "str1[%lu] != '=' in test_ascMalloc().", i);
193 CU_FAIL(str3);
194 str1_bad = TRUE;
195 }
196 }
197 if (!str1_bad) CU_PASS("str1 and str2 check out.");
198
199 memset(str1, '~', 10);
200 memset(str1+10, '=', 10);
201 memset(str1+20, '|', 10);
202
203 CU_TEST(str1 == asc_memcpy(str1, str1+10, 20)); /* copy overlapping memory block */
204
205 str1_bad = FALSE;
206 for (i=0 ; i<10 ; ++i) {
207 if (!str1_bad && (str1[i] != '=')) {
208 snprintf(str3, STR_LEN-1, "str1[%lu] != '~' in test_ascMalloc().", i);
209 CU_FAIL(str3);
210 str1_bad = TRUE;
211 }
212 }
213 for (i=10 ; i<30 ; ++i) {
214 if (!str1_bad && (str1[i] != '|')) {
215 snprintf(str3, STR_LEN-1, "str1[%lu] != '=' in test_ascMalloc().", i);
216 CU_FAIL(str3);
217 str1_bad = TRUE;
218 }
219 }
220 if (!str1_bad) CU_PASS("str1 and str2 check out.");
221
222 snprintf(str1, STR_LEN-1, "This is yet another dumb string");
223 CU_TEST(str1 == asc_memcpy(str1, str1, strlen(str1))); /* to == from */
224 CU_TEST(0 == strcmp(str1, "This is yet another dumb string"));
225
226 CU_TEST(str1 == asc_memcpy(str1, str2, 0)); /* n = 0 */
227 CU_TEST(0 == strcmp(str1, "This is yet another dumb string"));
228
229 /* test ascreallocPURE() */
230
231 p_str1 = (char *)ascmalloc(50); /* allocate a block & check status */
232 CU_TEST_FATAL(NULL != p_str1);
233 #ifdef MALLOC_DEBUG
234 CU_TEST(50 == ascmeminuse());
235 CU_TEST(2 == AllocatedMemory(p_str1, 50));
236 #else
237 CU_TEST(0 == ascmeminuse());
238 CU_TEST(1 == AllocatedMemory(p_str1, 50));
239 #endif
240
241 snprintf(p_str1, 49, "I should survive a reallocation!");
242 p_str1 = ascreallocPURE(p_str1, 50, 100);
243 CU_TEST_FATAL(NULL != p_str1);
244 #ifdef MALLOC_DEBUG
245 CU_TEST(100 == ascmeminuse());
246 CU_TEST(2 == AllocatedMemory(p_str1, 100));
247 #else
248 CU_TEST(0 == ascmeminuse());
249 CU_TEST(1 == AllocatedMemory(p_str1, 100));
250 #endif
251 CU_TEST(0 == strcmp(p_str1, "I should survive a reallocation!"));
252
253 ascfree(p_str1);
254 CU_TEST(0 == ascmeminuse());
255 #ifdef MALLOC_DEBUG
256 CU_TEST(0 == AllocatedMemory(p_str1, 0));
257 #else
258 CU_TEST(1 == AllocatedMemory(p_str1, 0));
259 #endif
260
261 /* ascstatus(), ascstatus_detail(), ascshutdown() - reporting functions, not tested */
262 /* ascmeminuse() - tested adequately by other tests */
263
264 /* test asccalloc() */
265
266 p_int1 = (int *)asccalloc(0, sizeof(int)); /* 0 elements requested */
267 CU_TEST_FATAL(NULL != p_int1);
268 #ifdef MALLOC_DEBUG
269 CU_TEST(AT_LEAST_1(0) == ascmeminuse());
270 CU_TEST(2 == AllocatedMemory(p_int1, 0));
271 #else
272 CU_TEST(0 == ascmeminuse());
273 CU_TEST(1 == AllocatedMemory(p_int1, 0));
274 #endif
275
276 ascfree(p_int1);
277 CU_TEST(0 == ascmeminuse());
278 #ifdef MALLOC_DEBUG
279 CU_TEST(0 == AllocatedMemory(p_int1, 0));
280 #else
281 CU_TEST(1 == AllocatedMemory(p_int1, 0));
282 #endif
283
284 p_int1 = (int *)asccalloc(100, 0); /* 0 size requested */
285 CU_TEST_FATAL(NULL != p_int1);
286 #ifdef MALLOC_DEBUG
287 CU_TEST(0 == ascmeminuse());
288 CU_TEST(2 == AllocatedMemory(p_int1, 0));
289 #else
290 CU_TEST(0 == ascmeminuse());
291 CU_TEST(1 == AllocatedMemory(p_int1, 0));
292 #endif
293
294 ascfree(p_int1);
295 CU_TEST(0 == ascmeminuse());
296 #ifdef MALLOC_DEBUG
297 CU_TEST(0 == AllocatedMemory(p_int1, 0));
298 #else
299 CU_TEST(1 == AllocatedMemory(p_int1, 0));
300 #endif
301
302 p_int1 = (int *)asccalloc(100, sizeof(int)); /* 100 elements requested */
303 CU_TEST_FATAL(NULL != p_int1);
304 #ifdef MALLOC_DEBUG
305 CU_TEST(100*sizeof(int) == ascmeminuse());
306 CU_TEST(2 == AllocatedMemory(p_int1, 100*sizeof(int)));
307 #else
308 CU_TEST(0 == ascmeminuse());
309 CU_TEST(1 == AllocatedMemory(p_int1, 0));
310 #endif
311
312 p_int2 = (int *)asccalloc(200, sizeof(int)); /* 200 more elements requested */
313 CU_TEST_FATAL(NULL != p_int2);
314 #ifdef MALLOC_DEBUG
315 CU_TEST(300*sizeof(int) == ascmeminuse());
316 CU_TEST(2 == AllocatedMemory(p_int2, 200*sizeof(int)));
317 #else
318 CU_TEST(0 == ascmeminuse());
319 CU_TEST(1 == AllocatedMemory(p_int2, 0));
320 #endif
321
322 p_int3 = (int *)asccalloc(10, sizeof(int)); /* 10 more elements requested */
323 CU_TEST_FATAL(NULL != p_int3);
324 #ifdef MALLOC_DEBUG
325 CU_TEST(310*sizeof(int) == ascmeminuse());
326 CU_TEST(2 == AllocatedMemory(p_int3, 10*sizeof(int)));
327 #else
328 CU_TEST(0 == ascmeminuse());
329 CU_TEST(1 == AllocatedMemory(p_int3, 0));
330 #endif
331
332 ascfree(p_int2);
333 #ifdef MALLOC_DEBUG
334 CU_TEST(110*sizeof(int) == ascmeminuse());
335 CU_TEST(2 == AllocatedMemory(p_int1, 100*sizeof(int)));
336 CU_TEST(0 == AllocatedMemory(p_int2, 200*sizeof(int)));
337 CU_TEST(2 == AllocatedMemory(p_int3, 10*sizeof(int)));
338 #else
339 CU_TEST(0 == ascmeminuse());
340 CU_TEST(1 == AllocatedMemory(p_int1, 100*sizeof(int)));
341 CU_TEST(1 == AllocatedMemory(p_int2, 200*sizeof(int)));
342 CU_TEST(1 == AllocatedMemory(p_int3, 10*sizeof(int)));
343 #endif
344
345 ascfree(p_int1);
346 #ifdef MALLOC_DEBUG
347 CU_TEST(10*sizeof(int) == ascmeminuse());
348 CU_TEST(0 == AllocatedMemory(p_int1, 100*sizeof(int)));
349 CU_TEST(0 == AllocatedMemory(p_int2, 200*sizeof(int)));
350 CU_TEST(2 == AllocatedMemory(p_int3, 10*sizeof(int)));
351 #else
352 CU_TEST(0 == ascmeminuse());
353 CU_TEST(1 == AllocatedMemory(p_int1, 100*sizeof(int)));
354 CU_TEST(1 == AllocatedMemory(p_int2, 200*sizeof(int)));
355 CU_TEST(1 == AllocatedMemory(p_int3, 10*sizeof(int)));
356 #endif
357
358 ascfree(p_int3);
359 #ifdef MALLOC_DEBUG
360 CU_TEST(0 == ascmeminuse());
361 CU_TEST(0 == AllocatedMemory(p_int1, 100*sizeof(int)));
362 CU_TEST(0 == AllocatedMemory(p_int2, 200*sizeof(int)));
363 CU_TEST(0 == AllocatedMemory(p_int3, 10*sizeof(int)));
364 #else
365 CU_TEST(0 == ascmeminuse());
366 CU_TEST(1 == AllocatedMemory(p_int1, 100*sizeof(int)));
367 CU_TEST(1 == AllocatedMemory(p_int2, 200*sizeof(int)));
368 CU_TEST(1 == AllocatedMemory(p_int3, 10*sizeof(int)));
369 #endif
370
371 /* test ascmalloc() */
372
373 p_int1 = (int *)ascmalloc(0); /* 0 bytes requested */
374 CU_TEST_FATAL(NULL != p_int1);
375 #ifdef MALLOC_DEBUG
376 CU_TEST(AT_LEAST_1(0) == ascmeminuse());
377 CU_TEST(2 == AllocatedMemory(p_int1, 0));
378 #else
379 CU_TEST(0 == ascmeminuse());
380 CU_TEST(1 == AllocatedMemory(p_int1, 0));
381 #endif
382
383 ascfree(p_int1);
384 CU_TEST(0 == ascmeminuse());
385 #ifdef MALLOC_DEBUG
386 CU_TEST(0 == AllocatedMemory(p_int1, 0));
387 #else
388 CU_TEST(1 == AllocatedMemory(p_int1, 0));
389 #endif
390
391 p_int1 = (int *)ascmalloc(100); /* 100 bytes requested */
392 CU_TEST_FATAL(NULL != p_int1);
393 #ifdef MALLOC_DEBUG
394 CU_TEST(100 == ascmeminuse());
395 CU_TEST(2 == AllocatedMemory(p_int1, 100));
396 #else
397 CU_TEST(0 == ascmeminuse());
398 CU_TEST(1 == AllocatedMemory(p_int1, 0));
399 #endif
400
401 p_int2 = (int *)ascmalloc(200); /* 200 more bytes requested */
402 CU_TEST_FATAL(NULL != p_int2);
403 #ifdef MALLOC_DEBUG
404 CU_TEST(300 == ascmeminuse());
405 CU_TEST(2 == AllocatedMemory(p_int2, 200));
406 #else
407 CU_TEST(0 == ascmeminuse());
408 CU_TEST(1 == AllocatedMemory(p_int2, 0));
409 #endif
410
411 p_int3 = (int *)ascmalloc(10); /* 10 more bytes requested */
412 CU_TEST_FATAL(NULL != p_int3);
413 #ifdef MALLOC_DEBUG
414 CU_TEST(310 == ascmeminuse());
415 CU_TEST(2 == AllocatedMemory(p_int3, 10));
416 #else
417 CU_TEST(0 == ascmeminuse());
418 CU_TEST(1 == AllocatedMemory(p_int3, 0));
419 #endif
420
421 ascfree(p_int2);
422 #ifdef MALLOC_DEBUG
423 CU_TEST(110 == ascmeminuse());
424 CU_TEST(2 == AllocatedMemory(p_int1, 100));
425 CU_TEST(0 == AllocatedMemory(p_int2, 200));
426 CU_TEST(2 == AllocatedMemory(p_int3, 10));
427 #else
428 CU_TEST(0 == ascmeminuse());
429 CU_TEST(1 == AllocatedMemory(p_int1, 100));
430 CU_TEST(1 == AllocatedMemory(p_int2, 200));
431 CU_TEST(1 == AllocatedMemory(p_int3, 10));
432 #endif
433
434 ascfree(p_int1);
435 #ifdef MALLOC_DEBUG
436 CU_TEST(10 == ascmeminuse());
437 CU_TEST(0 == AllocatedMemory(p_int1, 100));
438 CU_TEST(0 == AllocatedMemory(p_int2, 200));
439 CU_TEST(2 == AllocatedMemory(p_int3, 10));
440 #else
441 CU_TEST(0 == ascmeminuse());
442 CU_TEST(1 == AllocatedMemory(p_int1, 100));
443 CU_TEST(1 == AllocatedMemory(p_int2, 200));
444 CU_TEST(1 == AllocatedMemory(p_int3, 10));
445 #endif
446
447 ascfree(p_int3);
448 #ifdef MALLOC_DEBUG
449 CU_TEST(0 == ascmeminuse());
450 CU_TEST(0 == AllocatedMemory(p_int1, 100));
451 CU_TEST(0 == AllocatedMemory(p_int2, 200));
452 CU_TEST(0 == AllocatedMemory(p_int3, 10));
453 #else
454 CU_TEST(0 == ascmeminuse());
455 CU_TEST(1 == AllocatedMemory(p_int1, 100));
456 CU_TEST(1 == AllocatedMemory(p_int2, 200));
457 CU_TEST(1 == AllocatedMemory(p_int3, 10));
458 #endif
459
460 /* test ascrealloc() */
461
462 p_str1 = (char *)ascmalloc(50); /* allocate several blocks & check status */
463 p_str2 = (char *)ascmalloc(20);
464 p_str3 = (char *)ascmalloc(25);
465 CU_TEST_FATAL(NULL != p_str1);
466 CU_TEST_FATAL(NULL != p_str2);
467 CU_TEST_FATAL(NULL != p_str3);
468 #ifdef MALLOC_DEBUG
469 CU_TEST(95 == ascmeminuse());
470 CU_TEST(2 == AllocatedMemory(p_str1, 50));
471 CU_TEST(2 == AllocatedMemory(p_str2, 20));
472 CU_TEST(2 == AllocatedMemory(p_str3, 25));
473 #else
474 CU_TEST(0 == ascmeminuse());
475 CU_TEST(1 == AllocatedMemory(p_str1, 50));
476 CU_TEST(1 == AllocatedMemory(p_str2, 20));
477 CU_TEST(1 == AllocatedMemory(p_str3, 25));
478 #endif
479
480 snprintf(p_str1, 49, "I should survive a reallocation!");
481 snprintf(p_str2, 19, "Me too?");
482 snprintf(p_str3, 24, "Realloc me away.");
483
484 p_str1 = ascrealloc(p_str1, 100); /* realloc to larger size */
485 CU_TEST_FATAL(NULL != p_str1);
486 #ifdef MALLOC_DEBUG
487 CU_TEST(145 == ascmeminuse());
488 CU_TEST(2 == AllocatedMemory(p_str1, 100));
489 CU_TEST(2 == AllocatedMemory(p_str2, 20));
490 CU_TEST(2 == AllocatedMemory(p_str3, 25));
491 #else
492 CU_TEST(0 == ascmeminuse());
493 CU_TEST(1 == AllocatedMemory(p_str1, 100));
494 CU_TEST(1 == AllocatedMemory(p_str2, 20));
495 CU_TEST(1 == AllocatedMemory(p_str3, 25));
496 #endif
497 CU_TEST(0 == strcmp(p_str1, "I should survive a reallocation!"));
498 CU_TEST(0 == strcmp(p_str2, "Me too?"));
499 CU_TEST(0 == strcmp(p_str3, "Realloc me away."));
500
501 p_str2 = ascrealloc(p_str2, 10); /* realloc to smaller size */
502 CU_TEST_FATAL(NULL != p_str2);
503 #ifdef MALLOC_DEBUG
504 CU_TEST(135 == ascmeminuse());
505 CU_TEST(2 == AllocatedMemory(p_str1, 100));
506 CU_TEST(2 == AllocatedMemory(p_str2, 10));
507 CU_TEST(2 == AllocatedMemory(p_str3, 25));
508 #else
509 CU_TEST(0 == ascmeminuse());
510 CU_TEST(1 == AllocatedMemory(p_str1, 100));
511 CU_TEST(1 == AllocatedMemory(p_str2, 10));
512 CU_TEST(1 == AllocatedMemory(p_str3, 25));
513 #endif
514 CU_TEST(0 == strcmp(p_str1, "I should survive a reallocation!"));
515 CU_TEST(0 == strcmp(p_str2, "Me too?"));
516 CU_TEST(0 == strcmp(p_str3, "Realloc me away."));
517
518 p_str3 = ascrealloc(p_str3, 0); /* realloc to zero */
519 CU_TEST(NULL == p_str3);
520 #ifdef MALLOC_DEBUG
521 CU_TEST(110+AT_LEAST_1(0) == ascmeminuse());
522 CU_TEST(2 == AllocatedMemory(p_str1, 100));
523 CU_TEST(2 == AllocatedMemory(p_str2, 10));
524 CU_TEST(0 == AllocatedMemory(p_str3, 0));
525 #else
526 CU_TEST(0 == ascmeminuse());
527 CU_TEST(1 == AllocatedMemory(p_str1, 100));
528 CU_TEST(1 == AllocatedMemory(p_str2, 10));
529 CU_TEST(1 == AllocatedMemory(p_str3, 0));
530 #endif
531 CU_TEST(0 == strcmp(p_str1, "I should survive a reallocation!"));
532 CU_TEST(0 == strcmp(p_str2, "Me too?"));
533
534 ascfree(p_str3);
535 #ifdef MALLOC_DEBUG
536 CU_TEST(110 == ascmeminuse());
537 CU_TEST(2 == AllocatedMemory(p_str1, 100));
538 CU_TEST(2 == AllocatedMemory(p_str2, 10));
539 CU_TEST(0 == AllocatedMemory(p_str3, 0));
540 #else
541 CU_TEST(0 == ascmeminuse());
542 CU_TEST(1 == AllocatedMemory(p_str1, 100));
543 CU_TEST(1 == AllocatedMemory(p_str2, 10));
544 CU_TEST(1 == AllocatedMemory(p_str3, 0));
545 #endif
546
547 ascfree(p_str1);
548 #ifdef MALLOC_DEBUG
549 CU_TEST(10 == ascmeminuse());
550 CU_TEST(0 == AllocatedMemory(p_str1, 100));
551 CU_TEST(2 == AllocatedMemory(p_str2, 10));
552 CU_TEST(0 == AllocatedMemory(p_str3, 0));
553 #else
554 CU_TEST(0 == ascmeminuse());
555 CU_TEST(1 == AllocatedMemory(p_str1, 100));
556 CU_TEST(1 == AllocatedMemory(p_str2, 10));
557 CU_TEST(1 == AllocatedMemory(p_str3, 0));
558 #endif
559
560 ascfree(p_str2);
561 #ifdef MALLOC_DEBUG
562 CU_TEST(0 == ascmeminuse());
563 CU_TEST(0 == AllocatedMemory(p_str1, 100));
564 CU_TEST(0 == AllocatedMemory(p_str2, 10));
565 CU_TEST(0 == AllocatedMemory(p_str3, 0));
566 #else
567 CU_TEST(0 == ascmeminuse());
568 CU_TEST(1 == AllocatedMemory(p_str1, 100));
569 CU_TEST(1 == AllocatedMemory(p_str2, 10));
570 CU_TEST(1 == AllocatedMemory(p_str3, 0));
571 #endif
572
573 /* ascfree() tested adequately by other tests */
574
575 /* test ascbcopy() */
576
577 memset(str1, '\0', STR_LEN);
578 memset(str2, '*', STR_LEN);
579
580 CU_TEST(str2 == ascbcopy(str1, str2, STR_LEN/2)); /* copy part of a memory block */
581
582 str1_bad = FALSE;
583 str2_bad = FALSE;
584 for (i=0 ; i<STR_LEN/2 ; ++i) {
585 if (!str2_bad && (str2[i] != '\0')) {
586 snprintf(str3, STR_LEN-1, "str2[%lu] != '\\0' in test_ascMalloc().", i);
587 CU_FAIL(str3);
588 str2_bad = TRUE;
589 }
590 }
591 for (i=STR_LEN/2 ; i<STR_LEN-1 ; ++i) {
592 if (!str2_bad && (str2[i] != '*')) {
593 snprintf(str3, STR_LEN-1, "str2[%lu] != '*' in test_ascMalloc().", i);
594 CU_FAIL(str3);
595 str2_bad = TRUE;
596 }
597 }
598 for (i=0 ; i<STR_LEN ; ++i) {
599 if (!str1_bad && (str1[i] != '\0')) {
600 snprintf(str3, STR_LEN-1, "str1[%lu] != '\\0' in test_ascMalloc().", i);
601 CU_FAIL(str3);
602 str1_bad = TRUE;
603 }
604 }
605 if (!str1_bad && !str2_bad) CU_PASS("str1 and str2 check out.");
606
607 memset(str1, '+', STR_LEN);
608 memset(str2, '-', STR_LEN);
609
610 CU_TEST(str2 == ascbcopy(str1, str2, 0)); /* 0 bytes copied */
611
612 str1_bad = FALSE;
613 str2_bad = FALSE;
614 for (i=0 ; i<STR_LEN ; ++i) {
615 if (!str1_bad && (str1[i] != '+')) {
616 snprintf(str3, STR_LEN-1, "str1[%lu] != '+' in test_ascMalloc().", i);
617 CU_FAIL(str3);
618 str1_bad = TRUE;
619 }
620 if (!str2_bad && (str2[i] != '-')) {
621 snprintf(str3, STR_LEN-1, "str2[%lu] != '-' in test_ascMalloc().", i);
622 CU_FAIL(str3);
623 str2_bad = TRUE;
624 }
625 }
626 if (!str1_bad && !str2_bad) CU_PASS("str1 and str2 check out.");
627
628 /* test asczero() */
629
630 memset(str1, '=', STR_LEN);
631
632 CU_TEST(str1 == ascbzero(str1, STR_LEN/2)); /* zero part of a memory block */
633
634 str1_bad = FALSE;
635 for (i=0 ; i<STR_LEN/2 ; ++i) {
636 if (!str1_bad && (str1[i] != '\0')) {
637 snprintf(str3, STR_LEN-1, "str1[%lu] != '\\0' in test_ascMalloc().", i);
638 CU_FAIL(str3);
639 str1_bad = TRUE;
640 }
641 }
642 for (i=STR_LEN/2 ; i<STR_LEN-1 ; ++i) {
643 if (!str1_bad && (str1[i] != '=')) {
644 snprintf(str3, STR_LEN-1, "str1[%lu] != '=' in test_ascMalloc().", i);
645 CU_FAIL(str3);
646 str1_bad = TRUE;
647 }
648 }
649 if (!str1_bad) CU_PASS("str1 checks out after ascbzero().");
650
651 memset(str1, '+', STR_LEN);
652
653 CU_TEST(str1 == ascbzero(str1, 0)); /* 0 bytes processed */
654
655 str1_bad = FALSE;
656 for (i=0 ; i<STR_LEN ; ++i) {
657 if (!str1_bad && (str1[i] != '+')) {
658 snprintf(str3, STR_LEN-1, "str1[%lu] != '+' in test_ascMalloc().", i);
659 CU_FAIL(str3);
660 str1_bad = TRUE;
661 }
662 }
663 if (!str1_bad) CU_PASS("str1 checks out after ascbzero.");
664
665 /* test ascbfill() */
666
667 memset(str1, '@', STR_LEN);
668
669 CU_TEST(str1 == ascbfill(str1, STR_LEN/2)); /* fill part of a memory block */
670
671 str1_bad = FALSE;
672 for (i=0 ; i<STR_LEN/2 ; ++i) {
673 if (!str1_bad && (str1[i] != '\xff')) {
674 snprintf(str3, STR_LEN-1, "str1[%lu] != 255 in test_ascMalloc().", i);
675 CU_FAIL(str3);
676 str1_bad = TRUE;
677 }
678 }
679 for (i=STR_LEN/2 ; i<STR_LEN-1 ; ++i) {
680 if (!str1_bad && (str1[i] != '@')) {
681 snprintf(str3, STR_LEN-1, "str1[%lu] != '@' in test_ascMalloc().", i);
682 CU_FAIL(str3);
683 str1_bad = TRUE;
684 }
685 }
686 if (!str1_bad) CU_PASS("str1 checks out after ascbfill().");
687
688 memset(str1, '#', STR_LEN);
689
690 CU_TEST(str1 == ascbfill(str1, 0)); /* 0 bytes processed */
691
692 str1_bad = FALSE;
693 for (i=0 ; i<STR_LEN ; ++i) {
694 if (!str1_bad && (str1[i] != '#')) {
695 snprintf(str3, STR_LEN-1, "str1[%lu] != '#' in test_ascMalloc().", i);
696 CU_FAIL(str3);
697 str1_bad = TRUE;
698 }
699 }
700 if (!str1_bad) CU_PASS("str1 checks out after ascbfill.");
701
702 /* test AllocatedMemory() */
703
704 #ifdef MALLOC_DEBUG
705 CU_TEST(0 == AllocatedMemory(NULL, 0)); /* NULL pointer, nothing allocated */
706 CU_TEST(0 == AllocatedMemory(&str1_bad, 0)); /* non-NULL pointer, nothing allocated */
707 #else
708 CU_TEST(1 == AllocatedMemory(NULL, 0)); /* NULL pointer, nothing allocated */
709 CU_TEST(1 == AllocatedMemory(&str1_bad, 0)); /* non-NULL pointer, nothing allocated */
710 #endif
711
712 p_str1 = (char *)ascmalloc(100); /* allocate 1 block */
713 CU_TEST_FATAL(NULL != p_str1);
714
715 p_str2 = (char *)malloc(100); /* allocate another outside ascMalloc */
716 CU_TEST_FATAL(NULL != p_str2);
717
718 #ifdef MALLOC_DEBUG
719 CU_TEST(0 == AllocatedMemory(NULL, 0)); /* NULL pointer */
720
721 CU_TEST(0 == AllocatedMemory(p_str2, 0)); /* pointer allocated outside ascMalloc */
722
723 CU_TEST(2 == AllocatedMemory(p_str1, 100)); /* complete blocks */
724
725 CU_TEST(1 == AllocatedMemory(p_str1, 99)); /* contained blocks */
726 CU_TEST(1 == AllocatedMemory(p_str1+1, 99));
727
728 CU_TEST(-1 == AllocatedMemory(p_str1, 101)); /* overlapping blocks */
729 CU_TEST(-1 == AllocatedMemory(p_str1+1, 100));
730 CU_TEST(-1 == AllocatedMemory(p_str1-1, 2));
731 CU_TEST(-1 == AllocatedMemory(p_str1-1, 150));
732
733 CU_TEST(0 == AllocatedMemory(p_str1-10, 10)); /* non-overlapping blocks */
734 CU_TEST(0 == AllocatedMemory(p_str1-1, 1));
735
736 #else
737 CU_TEST(1 == AllocatedMemory(NULL, 0)); /* NULL pointer */
738
739 CU_TEST(1 == AllocatedMemory(p_str2, 0)); /* pointer allocated outside ascMalloc */
740
741 CU_TEST(1 == AllocatedMemory(p_str1, 100)); /* complete blocks */
742
743 CU_TEST(1 == AllocatedMemory(p_str1, 99)); /* contained blocks */
744 CU_TEST(1 == AllocatedMemory(p_str1+1, 99));
745
746 CU_TEST(1 == AllocatedMemory(p_str1, 101)); /* overlapping blocks */
747 CU_TEST(1 == AllocatedMemory(p_str1+1, 100));
748 CU_TEST(1 == AllocatedMemory(p_str1-1, 2));
749 CU_TEST(1 == AllocatedMemory(p_str1-1, 150));
750
751 CU_TEST(1 == AllocatedMemory(p_str1-10, 10)); /* non-overlapping blocks */
752 CU_TEST(1 == AllocatedMemory(p_str1-1, 1));
753
754 #endif
755
756 ascfree(p_str1);
757 free(p_str2);
758 CU_TEST(0 == ascmeminuse());
759
760 p_doub1 = (double *)ascmalloc(sizeof(double)); /* allocate 1 block */
761 CU_TEST_FATAL(NULL != p_doub1);
762
763 p_doub2 = (double *)malloc(sizeof(double)); /* allocate another outside ascMalloc */
764 CU_TEST_FATAL(NULL != p_doub2);
765
766 #ifdef MALLOC_DEBUG
767 CU_TEST(0 == AllocatedMemory(NULL, 0)); /* NULL pointer */
768
769 CU_TEST(0 == AllocatedMemory(p_doub2, 0)); /* pointer allocated outside ascMalloc */
770
771 CU_TEST(2 == AllocatedMemory(p_doub1, sizeof(double)));/* complete blocks */
772
773 CU_TEST(1 == AllocatedMemory(p_doub1, 0)); /* contained blocks */
774 CU_TEST(1 == AllocatedMemory((char *)p_doub1+1, sizeof(double)-1));
775
776 CU_TEST(-1 == AllocatedMemory(p_doub1, sizeof(double)+1)); /* overlapping blocks */
777 CU_TEST(-1 == AllocatedMemory((char *)p_doub1+1, sizeof(double)));
778 CU_TEST(-1 == AllocatedMemory((char *)p_doub1-1, 2));
779
780 CU_TEST(0 == AllocatedMemory((char *)p_doub1-1, 1)); /* non-overlapping blocks */
781 CU_TEST(0 == AllocatedMemory((char *)p_doub1-100, 100));
782
783 #else
784 CU_TEST(1 == AllocatedMemory(NULL, 0)); /* NULL pointer */
785
786 CU_TEST(1 == AllocatedMemory(p_doub2, 0)); /* pointer allocated outside ascMalloc */
787
788 CU_TEST(1 == AllocatedMemory(p_doub1, sizeof(double)));/* complete blocks */
789
790 CU_TEST(1 == AllocatedMemory(p_doub1, 0)); /* contained blocks */
791 CU_TEST(1 == AllocatedMemory((char *)p_doub1+1, sizeof(double)-1));
792
793 CU_TEST(1 == AllocatedMemory(p_doub1, sizeof(double)+1)); /* overlapping blocks */
794 CU_TEST(1 == AllocatedMemory((char *)p_doub1+1, sizeof(double)));
795 CU_TEST(1 == AllocatedMemory((char *)p_doub1-1, 2));
796
797 CU_TEST(1 == AllocatedMemory((char *)p_doub1-1, 1)); /* non-overlapping blocks */
798 CU_TEST(1 == AllocatedMemory((char *)p_doub1-100, 100));
799
800 #endif
801
802 ascfree(p_doub1);
803 free(p_doub2);
804 CU_TEST(0 == ascmeminuse());
805
806 /* test InMemoryBlock() */
807
808 p_str1 = (char *)ascmalloc(100); /* allocate 1 block */
809 CU_TEST_FATAL(NULL != p_str1);
810 CU_TEST(0 != InMemoryBlock(p_str1, p_str1));
811 CU_TEST(0 != InMemoryBlock(p_str1, p_str1+1));
812 CU_TEST(0 != InMemoryBlock(p_str1, p_str1+50));
813 CU_TEST(0 != InMemoryBlock(p_str1, p_str1+99));
814 #ifdef MALLOC_DEBUG
815 CU_TEST(0 == InMemoryBlock(p_str1, p_str1-1));
816 CU_TEST(0 == InMemoryBlock(p_str1, p_str1+100));
817 CU_TEST(0 == InMemoryBlock(p_str1, p_str1+101));
818 CU_TEST(0 == InMemoryBlock(p_str1, (VOIDPTR)0));
819 #else
820 CU_TEST(1 == InMemoryBlock(p_str1, p_str1-1));
821 CU_TEST(1 == InMemoryBlock(p_str1, p_str1+100));
822 CU_TEST(1 == InMemoryBlock(p_str1, p_str1+101));
823 CU_TEST(1 == InMemoryBlock(p_str1, (VOIDPTR)0));
824 #endif
825
826 ascfree(p_str1);
827 CU_TEST(0 == ascmeminuse());
828
829 /* test AssertAllocatedMemory() */
830
831 #ifndef ASC_NO_ASSERTIONS
832 asc_assert_catch(TRUE); /* prepare to test assertions */
833
834 asc_assert_reset();
835 if (0 == setjmp(g_asc_test_env))
836 AssertAllocatedMemory(p_str1, 100); /* error - no memory allocated */
837 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
838 CU_TEST(TRUE == asc_assert_failed());
839 #else
840 CU_TEST(FALSE == asc_assert_failed());
841 #endif
842
843 p_str1 = (char *)ascmalloc(100);
844 p_int1 = (int *)asccalloc(10, sizeof(int));
845
846 p_str2 = (char *)malloc(100); /* don't use asc*alloc! */
847 p_int2 = (int *)calloc(10, sizeof(int));
848
849 asc_assert_reset();
850 if (0 == setjmp(g_asc_test_env))
851 AssertAllocatedMemory(NULL, 100); /* error - NULL ptr */
852 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
853 CU_TEST(TRUE == asc_assert_failed());
854 #else
855 CU_TEST(FALSE == asc_assert_failed());
856 #endif
857
858 asc_assert_reset();
859 if (0 == setjmp(g_asc_test_env))
860 AssertAllocatedMemory(p_str1, 100); /* ok - allocated block, correct size*/
861 CU_TEST(FALSE == asc_assert_failed());
862
863 asc_assert_reset();
864 if (0 == setjmp(g_asc_test_env))
865 AssertAllocatedMemory(p_str1, 99); /* error - incorrect size */
866 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
867 CU_TEST(TRUE == asc_assert_failed());
868 #else
869 CU_TEST(FALSE == asc_assert_failed());
870 #endif
871
872 asc_assert_reset();
873 if (0 == setjmp(g_asc_test_env))
874 AssertAllocatedMemory(p_str1, 101); /* error - incorrect size */
875 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
876 CU_TEST(TRUE == asc_assert_failed());
877 #else
878 CU_TEST(FALSE == asc_assert_failed());
879 #endif
880
881 asc_assert_reset();
882 if (0 == setjmp(g_asc_test_env))
883 AssertAllocatedMemory(p_str2, 100); /* error - invalid ptr */
884 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
885 CU_TEST(TRUE == asc_assert_failed());
886 #else
887 CU_TEST(FALSE == asc_assert_failed());
888 #endif
889
890 asc_assert_reset();
891 if (0 == setjmp(g_asc_test_env))
892 AssertAllocatedMemory(p_int1, 10*sizeof(int)); /* ok - allocated block, correct size*/
893 CU_TEST(FALSE == asc_assert_failed());
894
895 asc_assert_reset();
896 if (0 == setjmp(g_asc_test_env))
897 AssertAllocatedMemory(p_int1, 10*sizeof(int)-1); /* error - incorrect size */
898 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
899 CU_TEST(TRUE == asc_assert_failed());
900 #else
901 CU_TEST(FALSE == asc_assert_failed());
902 #endif
903
904 asc_assert_reset();
905 if (0 == setjmp(g_asc_test_env))
906 AssertAllocatedMemory(p_int1, 10*sizeof(int)+1); /* error - incorrect size */
907 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
908 CU_TEST(TRUE == asc_assert_failed());
909 #else
910 CU_TEST(FALSE == asc_assert_failed());
911 #endif
912
913 asc_assert_reset();
914 if (0 == setjmp(g_asc_test_env))
915 AssertAllocatedMemory(p_int2, 10*sizeof(int)); /* error - invalid ptr */
916 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
917 CU_TEST(TRUE == asc_assert_failed());
918 #else
919 CU_TEST(FALSE == asc_assert_failed());
920 #endif
921
922 asc_assert_catch(FALSE); /* done testing assertions */
923 #endif /* !ASC_NO_ASSERTIONS */
924
925 ascfree(p_str1);
926 ascfree(p_int1);
927 free(p_str2);
928 free(p_int2);
929
930 /* test AssertMemory() */
931
932 #ifndef ASC_NO_ASSERTIONS
933 asc_assert_catch(TRUE); /* prepare to test assertions */
934
935 asc_assert_reset();
936 if (0 == setjmp(g_asc_test_env))
937 AssertMemory(p_str1); /* error - no memory allocated */
938 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
939 CU_TEST(TRUE == asc_assert_failed());
940 #else
941 CU_TEST(FALSE == asc_assert_failed());
942 #endif
943
944 p_str1 = (char *)ascmalloc(100);
945 p_int1 = (int *)asccalloc(10, sizeof(int));
946
947 p_str2 = (char *)malloc(100); /* don't use asc*alloc! */
948 p_int2 = (int *)calloc(10, sizeof(int));
949
950 asc_assert_reset();
951 if (0 == setjmp(g_asc_test_env))
952 AssertMemory(NULL); /* error - NULL ptr */
953 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
954 CU_TEST(TRUE == asc_assert_failed());
955 #else
956 CU_TEST(FALSE == asc_assert_failed());
957 #endif
958
959 asc_assert_reset();
960 if (0 == setjmp(g_asc_test_env))
961 AssertMemory(p_str1); /* ok - start of allocated block*/
962 CU_TEST(FALSE == asc_assert_failed());
963
964 asc_assert_reset();
965 if (0 == setjmp(g_asc_test_env))
966 AssertMemory(p_str1+10); /* ok - in allocated block*/
967 CU_TEST(FALSE == asc_assert_failed());
968
969 asc_assert_reset();
970 if (0 == setjmp(g_asc_test_env))
971 AssertMemory(p_int1); /* ok - start of allocated block */
972 CU_TEST(FALSE == asc_assert_failed());
973
974 asc_assert_reset();
975 if (0 == setjmp(g_asc_test_env))
976 AssertMemory(p_str2); /* error - not allocated using asc*alloc */
977 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
978 CU_TEST(TRUE == asc_assert_failed());
979 #else
980 CU_TEST(FALSE == asc_assert_failed());
981 #endif
982
983 asc_assert_reset();
984 if (0 == setjmp(g_asc_test_env))
985 AssertMemory(p_int2); /* error - not allocated using asc*alloc */
986 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
987 CU_TEST(TRUE == asc_assert_failed());
988 #else
989 CU_TEST(FALSE == asc_assert_failed());
990 #endif
991
992 asc_assert_catch(FALSE); /* done testing assertions */
993 #endif /* !ASC_NO_ASSERTIONS */
994
995 ascfree(p_str1);
996 ascfree(p_int1);
997 free(p_str2);
998 free(p_int2);
999
1000 /* test AssertContainedMemory() */
1001
1002 #ifndef ASC_NO_ASSERTIONS
1003 asc_assert_catch(TRUE); /* prepare to test assertions */
1004
1005 asc_assert_reset();
1006 if (0 == setjmp(g_asc_test_env))
1007 AssertContainedMemory(p_str1, 100); /* error - no memory allocated */
1008 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
1009 CU_TEST(TRUE == asc_assert_failed());
1010 #else
1011 CU_TEST(FALSE == asc_assert_failed());
1012 #endif
1013
1014 p_str1 = (char *)ascmalloc(100);
1015 p_int1 = (int *)asccalloc(10, sizeof(int));
1016
1017 p_str2 = (char *)malloc(100); /* don't use asc*alloc! */
1018 p_int2 = (int *)calloc(10, sizeof(int));
1019
1020 asc_assert_reset();
1021 if (0 == setjmp(g_asc_test_env))
1022 AssertContainedMemory(NULL, 100); /* error - NULL ptr */
1023 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
1024 CU_TEST(TRUE == asc_assert_failed());
1025 #else
1026 CU_TEST(FALSE == asc_assert_failed());
1027 #endif
1028
1029 asc_assert_reset();
1030 if (0 == setjmp(g_asc_test_env))
1031 AssertContainedMemory(p_str1, 100); /* ok - allocated block, correct size*/
1032 CU_TEST(FALSE == asc_assert_failed());
1033
1034 asc_assert_reset();
1035 if (0 == setjmp(g_asc_test_env))
1036 AssertContainedMemory(p_str1, 0); /* ok - contained in a block*/
1037 CU_TEST(FALSE == asc_assert_failed());
1038
1039 asc_assert_reset();
1040 if (0 == setjmp(g_asc_test_env))
1041 AssertContainedMemory(p_str1+10, 50); /* ok - contained in a block */
1042 CU_TEST(FALSE == asc_assert_failed());
1043
1044 asc_assert_reset();
1045 if (0 == setjmp(g_asc_test_env))
1046 AssertContainedMemory(p_str1, 101); /* error - incorrect size */
1047 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
1048 CU_TEST(TRUE == asc_assert_failed());
1049 #else
1050 CU_TEST(FALSE == asc_assert_failed());
1051 #endif
1052
1053 asc_assert_reset();
1054 if (0 == setjmp(g_asc_test_env))
1055 AssertContainedMemory(p_str2, 0); /* error - invalid ptr */
1056 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
1057 CU_TEST(TRUE == asc_assert_failed());
1058 #else
1059 CU_TEST(FALSE == asc_assert_failed());
1060 #endif
1061
1062 asc_assert_reset();
1063 if (0 == setjmp(g_asc_test_env))
1064 AssertContainedMemory(p_int1, 10*sizeof(int)); /* ok - allocated block, correct size*/
1065 CU_TEST(FALSE == asc_assert_failed());
1066
1067 asc_assert_reset();
1068 if (0 == setjmp(g_asc_test_env))
1069 AssertContainedMemory(p_int1, sizeof(int)-1); /* ok - incorrect size */
1070 CU_TEST(FALSE == asc_assert_failed());
1071
1072 asc_assert_reset();
1073 if (0 == setjmp(g_asc_test_env))
1074 AssertContainedMemory(p_int1, 10*sizeof(int)+1); /* error - incorrect size */
1075 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
1076 CU_TEST(TRUE == asc_assert_failed());
1077 #else
1078 CU_TEST(FALSE == asc_assert_failed());
1079 #endif
1080
1081 asc_assert_reset();
1082 if (0 == setjmp(g_asc_test_env))
1083 AssertContainedMemory(p_int2, 10*sizeof(int)); /* error - invalid ptr */
1084 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
1085 CU_TEST(TRUE == asc_assert_failed());
1086 #else
1087 CU_TEST(FALSE == asc_assert_failed());
1088 #endif
1089
1090 asc_assert_catch(FALSE); /* done testing assertions */
1091 #endif /* !ASC_NO_ASSERTIONS */
1092
1093 ascfree(p_str1);
1094 ascfree(p_int1);
1095 free(p_str2);
1096 free(p_int2);
1097
1098 /* test AssertContainedIn() */
1099
1100 #ifndef ASC_NO_ASSERTIONS
1101 asc_assert_catch(TRUE); /* prepare to test assertions */
1102
1103 asc_assert_reset();
1104 if (0 == setjmp(g_asc_test_env))
1105 AssertContainedIn(p_str1, p_str1); /* error - no memory allocated */
1106 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
1107 CU_TEST(TRUE == asc_assert_failed());
1108 #else
1109 CU_TEST(FALSE == asc_assert_failed());
1110 #endif
1111
1112 p_str1 = (char *)ascmalloc(100); /* allocate 1 block */
1113 CU_TEST_FATAL(NULL != p_str1);
1114
1115 asc_assert_reset();
1116 if (0 == setjmp(g_asc_test_env))
1117 AssertContainedIn(p_str1, p_str1); /* ok - same pointer */
1118 CU_TEST(FALSE == asc_assert_failed());
1119
1120 asc_assert_reset();
1121 if (0 == setjmp(g_asc_test_env))
1122 AssertContainedIn(p_str1, p_str1+1); /* ok - in block */
1123 CU_TEST(FALSE == asc_assert_failed());
1124
1125 asc_assert_reset();
1126 if (0 == setjmp(g_asc_test_env))
1127 AssertContainedIn(p_str1, p_str1+50); /* ok - in block */
1128 CU_TEST(FALSE == asc_assert_failed());
1129
1130 asc_assert_reset();
1131 if (0 == setjmp(g_asc_test_env))
1132 AssertContainedIn(p_str1, p_str1+99); /* ok - in block */
1133 CU_TEST(FALSE == asc_assert_failed());
1134
1135 asc_assert_reset();
1136 if (0 == setjmp(g_asc_test_env))
1137 AssertContainedIn(p_str1, p_str1-1); /* error - outside block */
1138 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
1139 CU_TEST(TRUE == asc_assert_failed());
1140 #else
1141 CU_TEST(FALSE == asc_assert_failed());
1142 #endif
1143
1144 asc_assert_reset();
1145 if (0 == setjmp(g_asc_test_env))
1146 AssertContainedIn(p_str1, p_str1+100); /* error - outside block */
1147 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
1148 CU_TEST(TRUE == asc_assert_failed());
1149 #else
1150 CU_TEST(FALSE == asc_assert_failed());
1151 #endif
1152
1153 asc_assert_reset();
1154 if (0 == setjmp(g_asc_test_env))
1155 AssertContainedIn(p_str1, p_str1+101); /* error - outside block */
1156 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
1157 CU_TEST(TRUE == asc_assert_failed());
1158 #else
1159 CU_TEST(FALSE == asc_assert_failed());
1160 #endif
1161
1162 asc_assert_reset();
1163 if (0 == setjmp(g_asc_test_env))
1164 AssertContainedIn(p_str1, NULL); /* error - NULL ptr */
1165 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
1166 CU_TEST(TRUE == asc_assert_failed());
1167 #else
1168 CU_TEST(FALSE == asc_assert_failed());
1169 #endif
1170
1171 asc_assert_reset();
1172 if (0 == setjmp(g_asc_test_env))
1173 AssertContainedIn(NULL, p_str1); /* error - NULL ptr */
1174 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
1175 CU_TEST(TRUE == asc_assert_failed());
1176 #else
1177 CU_TEST(FALSE == asc_assert_failed());
1178 #endif
1179
1180 asc_assert_catch(FALSE); /* done testing assertions */
1181 #endif /* !ASC_NO_ASSERTIONS */
1182
1183 ascfree(p_str1);
1184
1185 CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */
1186 }
1187
1188 /*===========================================================================*/
1189 /* Registration information */
1190
1191 static CU_TestInfo ascMalloc_test_list[] = {
1192 {"test_ascMalloc", test_ascMalloc},
1193 CU_TEST_INFO_NULL
1194 };
1195
1196 static CU_SuiteInfo suites[] = {
1197 {"test_utilities_ascMalloc", NULL, NULL, ascMalloc_test_list},
1198 CU_SUITE_INFO_NULL
1199 };
1200
1201 /*-------------------------------------------------------------------*/
1202 CU_ErrorCode test_register_utilities_ascMalloc(void)
1203 {
1204 return CU_register_suites(suites);
1205 }

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