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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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