/[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 100 - (show annotations) (download) (as text)
Fri Dec 9 23:25:15 2005 UTC (18 years, 9 months ago) by jds
File MIME type: text/x-csrc
File size: 38887 byte(s)
ascMalloc now working on Linux.
Fixed minor bugs in ascPanic.c, test suite functions.
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 "utilities/ascConfig.h"
26 #ifdef __WIN32__
27 #include <io.h>
28 #endif
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() - reporting functions, not tested */
264 /* ascshutdown() - do not call here or will result in closure of test memory log */
265 /* ascmeminuse() - tested adequately by other tests */
266
267 /* test asccalloc() */
268
269 p_int1 = (int *)asccalloc(0, sizeof(int)); /* 0 elements requested */
270 CU_TEST_FATAL(NULL != p_int1);
271 #ifdef MALLOC_DEBUG
272 CU_TEST(AT_LEAST_1(0) == ascmeminuse());
273 CU_TEST(2 == AllocatedMemory(p_int1, 0));
274 #else
275 CU_TEST(0 == ascmeminuse());
276 CU_TEST(1 == AllocatedMemory(p_int1, 0));
277 #endif
278
279 ascfree(p_int1);
280 CU_TEST(0 == ascmeminuse());
281 #ifdef MALLOC_DEBUG
282 CU_TEST(0 == AllocatedMemory(p_int1, 0));
283 #else
284 CU_TEST(1 == AllocatedMemory(p_int1, 0));
285 #endif
286
287 p_int1 = (int *)asccalloc(100, 0); /* 0 size requested */
288 CU_TEST_FATAL(NULL != p_int1);
289 #ifdef MALLOC_DEBUG
290 CU_TEST(0 == ascmeminuse());
291 CU_TEST(2 == AllocatedMemory(p_int1, 0));
292 #else
293 CU_TEST(0 == ascmeminuse());
294 CU_TEST(1 == AllocatedMemory(p_int1, 0));
295 #endif
296
297 ascfree(p_int1);
298 CU_TEST(0 == ascmeminuse());
299 #ifdef MALLOC_DEBUG
300 CU_TEST(0 == AllocatedMemory(p_int1, 0));
301 #else
302 CU_TEST(1 == AllocatedMemory(p_int1, 0));
303 #endif
304
305 p_int1 = (int *)asccalloc(100, sizeof(int)); /* 100 elements requested */
306 CU_TEST_FATAL(NULL != p_int1);
307 #ifdef MALLOC_DEBUG
308 CU_TEST(100*sizeof(int) == ascmeminuse());
309 CU_TEST(2 == AllocatedMemory(p_int1, 100*sizeof(int)));
310 #else
311 CU_TEST(0 == ascmeminuse());
312 CU_TEST(1 == AllocatedMemory(p_int1, 0));
313 #endif
314
315 p_int2 = (int *)asccalloc(200, sizeof(int)); /* 200 more elements requested */
316 CU_TEST_FATAL(NULL != p_int2);
317 #ifdef MALLOC_DEBUG
318 CU_TEST(300*sizeof(int) == ascmeminuse());
319 CU_TEST(2 == AllocatedMemory(p_int2, 200*sizeof(int)));
320 #else
321 CU_TEST(0 == ascmeminuse());
322 CU_TEST(1 == AllocatedMemory(p_int2, 0));
323 #endif
324
325 p_int3 = (int *)asccalloc(10, sizeof(int)); /* 10 more elements requested */
326 CU_TEST_FATAL(NULL != p_int3);
327 #ifdef MALLOC_DEBUG
328 CU_TEST(310*sizeof(int) == ascmeminuse());
329 CU_TEST(2 == AllocatedMemory(p_int3, 10*sizeof(int)));
330 #else
331 CU_TEST(0 == ascmeminuse());
332 CU_TEST(1 == AllocatedMemory(p_int3, 0));
333 #endif
334
335 ascfree(p_int2);
336 #ifdef MALLOC_DEBUG
337 CU_TEST(110*sizeof(int) == ascmeminuse());
338 CU_TEST(2 == AllocatedMemory(p_int1, 100*sizeof(int)));
339 CU_TEST(0 == AllocatedMemory(p_int2, 200*sizeof(int)));
340 CU_TEST(2 == AllocatedMemory(p_int3, 10*sizeof(int)));
341 #else
342 CU_TEST(0 == ascmeminuse());
343 CU_TEST(1 == AllocatedMemory(p_int1, 100*sizeof(int)));
344 CU_TEST(1 == AllocatedMemory(p_int2, 200*sizeof(int)));
345 CU_TEST(1 == AllocatedMemory(p_int3, 10*sizeof(int)));
346 #endif
347
348 ascfree(p_int1);
349 #ifdef MALLOC_DEBUG
350 CU_TEST(10*sizeof(int) == ascmeminuse());
351 CU_TEST(0 == AllocatedMemory(p_int1, 100*sizeof(int)));
352 CU_TEST(0 == AllocatedMemory(p_int2, 200*sizeof(int)));
353 CU_TEST(2 == AllocatedMemory(p_int3, 10*sizeof(int)));
354 #else
355 CU_TEST(0 == ascmeminuse());
356 CU_TEST(1 == AllocatedMemory(p_int1, 100*sizeof(int)));
357 CU_TEST(1 == AllocatedMemory(p_int2, 200*sizeof(int)));
358 CU_TEST(1 == AllocatedMemory(p_int3, 10*sizeof(int)));
359 #endif
360
361 ascfree(p_int3);
362 #ifdef MALLOC_DEBUG
363 CU_TEST(0 == ascmeminuse());
364 CU_TEST(0 == AllocatedMemory(p_int1, 100*sizeof(int)));
365 CU_TEST(0 == AllocatedMemory(p_int2, 200*sizeof(int)));
366 CU_TEST(0 == AllocatedMemory(p_int3, 10*sizeof(int)));
367 #else
368 CU_TEST(0 == ascmeminuse());
369 CU_TEST(1 == AllocatedMemory(p_int1, 100*sizeof(int)));
370 CU_TEST(1 == AllocatedMemory(p_int2, 200*sizeof(int)));
371 CU_TEST(1 == AllocatedMemory(p_int3, 10*sizeof(int)));
372 #endif
373
374 /* test ascmalloc() */
375
376 p_int1 = (int *)ascmalloc(0); /* 0 bytes requested */
377 CU_TEST_FATAL(NULL != p_int1);
378 #ifdef MALLOC_DEBUG
379 CU_TEST(AT_LEAST_1(0) == ascmeminuse());
380 CU_TEST(2 == AllocatedMemory(p_int1, 0));
381 #else
382 CU_TEST(0 == ascmeminuse());
383 CU_TEST(1 == AllocatedMemory(p_int1, 0));
384 #endif
385
386 ascfree(p_int1);
387 CU_TEST(0 == ascmeminuse());
388 #ifdef MALLOC_DEBUG
389 CU_TEST(0 == AllocatedMemory(p_int1, 0));
390 #else
391 CU_TEST(1 == AllocatedMemory(p_int1, 0));
392 #endif
393
394 p_int1 = (int *)ascmalloc(100); /* 100 bytes requested */
395 CU_TEST_FATAL(NULL != p_int1);
396 #ifdef MALLOC_DEBUG
397 CU_TEST(100 == ascmeminuse());
398 CU_TEST(2 == AllocatedMemory(p_int1, 100));
399 #else
400 CU_TEST(0 == ascmeminuse());
401 CU_TEST(1 == AllocatedMemory(p_int1, 0));
402 #endif
403
404 p_int2 = (int *)ascmalloc(200); /* 200 more bytes requested */
405 CU_TEST_FATAL(NULL != p_int2);
406 #ifdef MALLOC_DEBUG
407 CU_TEST(300 == ascmeminuse());
408 CU_TEST(2 == AllocatedMemory(p_int2, 200));
409 #else
410 CU_TEST(0 == ascmeminuse());
411 CU_TEST(1 == AllocatedMemory(p_int2, 0));
412 #endif
413
414 p_int3 = (int *)ascmalloc(10); /* 10 more bytes requested */
415 CU_TEST_FATAL(NULL != p_int3);
416 #ifdef MALLOC_DEBUG
417 CU_TEST(310 == ascmeminuse());
418 CU_TEST(2 == AllocatedMemory(p_int3, 10));
419 #else
420 CU_TEST(0 == ascmeminuse());
421 CU_TEST(1 == AllocatedMemory(p_int3, 0));
422 #endif
423
424 ascfree(p_int2);
425 #ifdef MALLOC_DEBUG
426 CU_TEST(110 == ascmeminuse());
427 CU_TEST(2 == AllocatedMemory(p_int1, 100));
428 CU_TEST(0 == AllocatedMemory(p_int2, 200));
429 CU_TEST(2 == AllocatedMemory(p_int3, 10));
430 #else
431 CU_TEST(0 == ascmeminuse());
432 CU_TEST(1 == AllocatedMemory(p_int1, 100));
433 CU_TEST(1 == AllocatedMemory(p_int2, 200));
434 CU_TEST(1 == AllocatedMemory(p_int3, 10));
435 #endif
436
437 ascfree(p_int1);
438 #ifdef MALLOC_DEBUG
439 CU_TEST(10 == ascmeminuse());
440 CU_TEST(0 == AllocatedMemory(p_int1, 100));
441 CU_TEST(0 == AllocatedMemory(p_int2, 200));
442 CU_TEST(2 == AllocatedMemory(p_int3, 10));
443 #else
444 CU_TEST(0 == ascmeminuse());
445 CU_TEST(1 == AllocatedMemory(p_int1, 100));
446 CU_TEST(1 == AllocatedMemory(p_int2, 200));
447 CU_TEST(1 == AllocatedMemory(p_int3, 10));
448 #endif
449
450 ascfree(p_int3);
451 #ifdef MALLOC_DEBUG
452 CU_TEST(0 == ascmeminuse());
453 CU_TEST(0 == AllocatedMemory(p_int1, 100));
454 CU_TEST(0 == AllocatedMemory(p_int2, 200));
455 CU_TEST(0 == AllocatedMemory(p_int3, 10));
456 #else
457 CU_TEST(0 == ascmeminuse());
458 CU_TEST(1 == AllocatedMemory(p_int1, 100));
459 CU_TEST(1 == AllocatedMemory(p_int2, 200));
460 CU_TEST(1 == AllocatedMemory(p_int3, 10));
461 #endif
462
463 /* test ascrealloc() */
464
465 p_str1 = (char *)ascmalloc(50); /* allocate several blocks & check status */
466 p_str2 = (char *)ascmalloc(20);
467 p_str3 = (char *)ascmalloc(25);
468 CU_TEST_FATAL(NULL != p_str1);
469 CU_TEST_FATAL(NULL != p_str2);
470 CU_TEST_FATAL(NULL != p_str3);
471 #ifdef MALLOC_DEBUG
472 CU_TEST(95 == ascmeminuse());
473 CU_TEST(2 == AllocatedMemory(p_str1, 50));
474 CU_TEST(2 == AllocatedMemory(p_str2, 20));
475 CU_TEST(2 == AllocatedMemory(p_str3, 25));
476 #else
477 CU_TEST(0 == ascmeminuse());
478 CU_TEST(1 == AllocatedMemory(p_str1, 50));
479 CU_TEST(1 == AllocatedMemory(p_str2, 20));
480 CU_TEST(1 == AllocatedMemory(p_str3, 25));
481 #endif
482
483 snprintf(p_str1, 49, "I should survive a reallocation!");
484 snprintf(p_str2, 19, "Me too?");
485 snprintf(p_str3, 24, "Realloc me away.");
486
487 p_str1 = ascrealloc(p_str1, 100); /* realloc to larger size */
488 CU_TEST_FATAL(NULL != p_str1);
489 #ifdef MALLOC_DEBUG
490 CU_TEST(145 == ascmeminuse());
491 CU_TEST(2 == AllocatedMemory(p_str1, 100));
492 CU_TEST(2 == AllocatedMemory(p_str2, 20));
493 CU_TEST(2 == AllocatedMemory(p_str3, 25));
494 #else
495 CU_TEST(0 == ascmeminuse());
496 CU_TEST(1 == AllocatedMemory(p_str1, 100));
497 CU_TEST(1 == AllocatedMemory(p_str2, 20));
498 CU_TEST(1 == AllocatedMemory(p_str3, 25));
499 #endif
500 CU_TEST(0 == strcmp(p_str1, "I should survive a reallocation!"));
501 CU_TEST(0 == strcmp(p_str2, "Me too?"));
502 CU_TEST(0 == strcmp(p_str3, "Realloc me away."));
503
504 p_str2 = ascrealloc(p_str2, 10); /* realloc to smaller size */
505 CU_TEST_FATAL(NULL != p_str2);
506 #ifdef MALLOC_DEBUG
507 CU_TEST(135 == ascmeminuse());
508 CU_TEST(2 == AllocatedMemory(p_str1, 100));
509 CU_TEST(2 == AllocatedMemory(p_str2, 10));
510 CU_TEST(2 == AllocatedMemory(p_str3, 25));
511 #else
512 CU_TEST(0 == ascmeminuse());
513 CU_TEST(1 == AllocatedMemory(p_str1, 100));
514 CU_TEST(1 == AllocatedMemory(p_str2, 10));
515 CU_TEST(1 == AllocatedMemory(p_str3, 25));
516 #endif
517 CU_TEST(0 == strcmp(p_str1, "I should survive a reallocation!"));
518 CU_TEST(0 == strcmp(p_str2, "Me too?"));
519 CU_TEST(0 == strcmp(p_str3, "Realloc me away."));
520
521 p_str3 = ascrealloc(p_str3, 0); /* realloc to zero */
522 CU_TEST(NULL == p_str3);
523 #ifdef MALLOC_DEBUG
524 CU_TEST(110+AT_LEAST_1(0) == ascmeminuse());
525 CU_TEST(2 == AllocatedMemory(p_str1, 100));
526 CU_TEST(2 == AllocatedMemory(p_str2, 10));
527 CU_TEST(0 == AllocatedMemory(p_str3, 0));
528 #else
529 CU_TEST(0 == ascmeminuse());
530 CU_TEST(1 == AllocatedMemory(p_str1, 100));
531 CU_TEST(1 == AllocatedMemory(p_str2, 10));
532 CU_TEST(1 == AllocatedMemory(p_str3, 0));
533 #endif
534 CU_TEST(0 == strcmp(p_str1, "I should survive a reallocation!"));
535 CU_TEST(0 == strcmp(p_str2, "Me too?"));
536
537 ascfree(p_str3);
538 #ifdef MALLOC_DEBUG
539 CU_TEST(110 == ascmeminuse());
540 CU_TEST(2 == AllocatedMemory(p_str1, 100));
541 CU_TEST(2 == AllocatedMemory(p_str2, 10));
542 CU_TEST(0 == AllocatedMemory(p_str3, 0));
543 #else
544 CU_TEST(0 == ascmeminuse());
545 CU_TEST(1 == AllocatedMemory(p_str1, 100));
546 CU_TEST(1 == AllocatedMemory(p_str2, 10));
547 CU_TEST(1 == AllocatedMemory(p_str3, 0));
548 #endif
549
550 ascfree(p_str1);
551 #ifdef MALLOC_DEBUG
552 CU_TEST(10 == ascmeminuse());
553 CU_TEST(0 == AllocatedMemory(p_str1, 100));
554 CU_TEST(2 == AllocatedMemory(p_str2, 10));
555 CU_TEST(0 == AllocatedMemory(p_str3, 0));
556 #else
557 CU_TEST(0 == ascmeminuse());
558 CU_TEST(1 == AllocatedMemory(p_str1, 100));
559 CU_TEST(1 == AllocatedMemory(p_str2, 10));
560 CU_TEST(1 == AllocatedMemory(p_str3, 0));
561 #endif
562
563 ascfree(p_str2);
564 #ifdef MALLOC_DEBUG
565 CU_TEST(0 == ascmeminuse());
566 CU_TEST(0 == AllocatedMemory(p_str1, 100));
567 CU_TEST(0 == AllocatedMemory(p_str2, 10));
568 CU_TEST(0 == AllocatedMemory(p_str3, 0));
569 #else
570 CU_TEST(0 == ascmeminuse());
571 CU_TEST(1 == AllocatedMemory(p_str1, 100));
572 CU_TEST(1 == AllocatedMemory(p_str2, 10));
573 CU_TEST(1 == AllocatedMemory(p_str3, 0));
574 #endif
575
576 /* ascfree() tested adequately by other tests */
577
578 /* test ascbcopy() */
579
580 memset(str1, '\0', STR_LEN);
581 memset(str2, '*', STR_LEN);
582
583 CU_TEST(str2 == ascbcopy(str1, str2, STR_LEN/2)); /* copy part of a memory block */
584
585 str1_bad = FALSE;
586 str2_bad = FALSE;
587 for (i=0 ; i<STR_LEN/2 ; ++i) {
588 if (!str2_bad && (str2[i] != '\0')) {
589 snprintf(str3, STR_LEN-1, "str2[%lu] != '\\0' in test_ascMalloc().", i);
590 CU_FAIL(str3);
591 str2_bad = TRUE;
592 }
593 }
594 for (i=STR_LEN/2 ; i<STR_LEN-1 ; ++i) {
595 if (!str2_bad && (str2[i] != '*')) {
596 snprintf(str3, STR_LEN-1, "str2[%lu] != '*' in test_ascMalloc().", i);
597 CU_FAIL(str3);
598 str2_bad = TRUE;
599 }
600 }
601 for (i=0 ; i<STR_LEN ; ++i) {
602 if (!str1_bad && (str1[i] != '\0')) {
603 snprintf(str3, STR_LEN-1, "str1[%lu] != '\\0' in test_ascMalloc().", i);
604 CU_FAIL(str3);
605 str1_bad = TRUE;
606 }
607 }
608 if (!str1_bad && !str2_bad) CU_PASS("str1 and str2 check out.");
609
610 memset(str1, '+', STR_LEN);
611 memset(str2, '-', STR_LEN);
612
613 CU_TEST(str2 == ascbcopy(str1, str2, 0)); /* 0 bytes copied */
614
615 str1_bad = FALSE;
616 str2_bad = FALSE;
617 for (i=0 ; i<STR_LEN ; ++i) {
618 if (!str1_bad && (str1[i] != '+')) {
619 snprintf(str3, STR_LEN-1, "str1[%lu] != '+' in test_ascMalloc().", i);
620 CU_FAIL(str3);
621 str1_bad = TRUE;
622 }
623 if (!str2_bad && (str2[i] != '-')) {
624 snprintf(str3, STR_LEN-1, "str2[%lu] != '-' in test_ascMalloc().", i);
625 CU_FAIL(str3);
626 str2_bad = TRUE;
627 }
628 }
629 if (!str1_bad && !str2_bad) CU_PASS("str1 and str2 check out.");
630
631 /* test asczero() */
632
633 memset(str1, '=', STR_LEN);
634
635 CU_TEST(str1 == ascbzero(str1, STR_LEN/2)); /* zero part of a memory block */
636
637 str1_bad = FALSE;
638 for (i=0 ; i<STR_LEN/2 ; ++i) {
639 if (!str1_bad && (str1[i] != '\0')) {
640 snprintf(str3, STR_LEN-1, "str1[%lu] != '\\0' in test_ascMalloc().", i);
641 CU_FAIL(str3);
642 str1_bad = TRUE;
643 }
644 }
645 for (i=STR_LEN/2 ; i<STR_LEN-1 ; ++i) {
646 if (!str1_bad && (str1[i] != '=')) {
647 snprintf(str3, STR_LEN-1, "str1[%lu] != '=' in test_ascMalloc().", i);
648 CU_FAIL(str3);
649 str1_bad = TRUE;
650 }
651 }
652 if (!str1_bad) CU_PASS("str1 checks out after ascbzero().");
653
654 memset(str1, '+', STR_LEN);
655
656 CU_TEST(str1 == ascbzero(str1, 0)); /* 0 bytes processed */
657
658 str1_bad = FALSE;
659 for (i=0 ; i<STR_LEN ; ++i) {
660 if (!str1_bad && (str1[i] != '+')) {
661 snprintf(str3, STR_LEN-1, "str1[%lu] != '+' in test_ascMalloc().", i);
662 CU_FAIL(str3);
663 str1_bad = TRUE;
664 }
665 }
666 if (!str1_bad) CU_PASS("str1 checks out after ascbzero.");
667
668 /* test ascbfill() */
669
670 memset(str1, '@', STR_LEN);
671
672 CU_TEST(str1 == ascbfill(str1, STR_LEN/2)); /* fill part of a memory block */
673
674 str1_bad = FALSE;
675 for (i=0 ; i<STR_LEN/2 ; ++i) {
676 if (!str1_bad && (str1[i] != '\xff')) {
677 snprintf(str3, STR_LEN-1, "str1[%lu] != 255 in test_ascMalloc().", i);
678 CU_FAIL(str3);
679 str1_bad = TRUE;
680 }
681 }
682 for (i=STR_LEN/2 ; i<STR_LEN-1 ; ++i) {
683 if (!str1_bad && (str1[i] != '@')) {
684 snprintf(str3, STR_LEN-1, "str1[%lu] != '@' in test_ascMalloc().", i);
685 CU_FAIL(str3);
686 str1_bad = TRUE;
687 }
688 }
689 if (!str1_bad) CU_PASS("str1 checks out after ascbfill().");
690
691 memset(str1, '#', STR_LEN);
692
693 CU_TEST(str1 == ascbfill(str1, 0)); /* 0 bytes processed */
694
695 str1_bad = FALSE;
696 for (i=0 ; i<STR_LEN ; ++i) {
697 if (!str1_bad && (str1[i] != '#')) {
698 snprintf(str3, STR_LEN-1, "str1[%lu] != '#' in test_ascMalloc().", i);
699 CU_FAIL(str3);
700 str1_bad = TRUE;
701 }
702 }
703 if (!str1_bad) CU_PASS("str1 checks out after ascbfill.");
704
705 /* test AllocatedMemory() */
706
707 #ifdef MALLOC_DEBUG
708 CU_TEST(0 == AllocatedMemory(NULL, 0)); /* NULL pointer, nothing allocated */
709 CU_TEST(0 == AllocatedMemory(&str1_bad, 0)); /* non-NULL pointer, nothing allocated */
710 #else
711 CU_TEST(1 == AllocatedMemory(NULL, 0)); /* NULL pointer, nothing allocated */
712 CU_TEST(1 == AllocatedMemory(&str1_bad, 0)); /* non-NULL pointer, nothing allocated */
713 #endif
714
715 p_str1 = (char *)ascmalloc(100); /* allocate 1 block */
716 CU_TEST_FATAL(NULL != p_str1);
717
718 p_str2 = (char *)malloc(100); /* allocate another outside ascMalloc */
719 CU_TEST_FATAL(NULL != p_str2);
720
721 #ifdef MALLOC_DEBUG
722 CU_TEST(0 == AllocatedMemory(NULL, 0)); /* NULL pointer */
723
724 CU_TEST(0 == AllocatedMemory(p_str2, 0)); /* pointer allocated outside ascMalloc */
725
726 CU_TEST(2 == AllocatedMemory(p_str1, 100)); /* complete blocks */
727
728 CU_TEST(1 == AllocatedMemory(p_str1, 99)); /* contained blocks */
729 CU_TEST(1 == AllocatedMemory(p_str1+1, 99));
730
731 CU_TEST(-1 == AllocatedMemory(p_str1, 101)); /* overlapping blocks */
732 CU_TEST(-1 == AllocatedMemory(p_str1+1, 100));
733 CU_TEST(-1 == AllocatedMemory(p_str1-1, 2));
734 CU_TEST(-1 == AllocatedMemory(p_str1-1, 150));
735
736 CU_TEST(0 == AllocatedMemory(p_str1-10, 10)); /* non-overlapping blocks */
737 CU_TEST(0 == AllocatedMemory(p_str1-1, 1));
738
739 #else
740 CU_TEST(1 == AllocatedMemory(NULL, 0)); /* NULL pointer */
741
742 CU_TEST(1 == AllocatedMemory(p_str2, 0)); /* pointer allocated outside ascMalloc */
743
744 CU_TEST(1 == AllocatedMemory(p_str1, 100)); /* complete blocks */
745
746 CU_TEST(1 == AllocatedMemory(p_str1, 99)); /* contained blocks */
747 CU_TEST(1 == AllocatedMemory(p_str1+1, 99));
748
749 CU_TEST(1 == AllocatedMemory(p_str1, 101)); /* overlapping blocks */
750 CU_TEST(1 == AllocatedMemory(p_str1+1, 100));
751 CU_TEST(1 == AllocatedMemory(p_str1-1, 2));
752 CU_TEST(1 == AllocatedMemory(p_str1-1, 150));
753
754 CU_TEST(1 == AllocatedMemory(p_str1-10, 10)); /* non-overlapping blocks */
755 CU_TEST(1 == AllocatedMemory(p_str1-1, 1));
756
757 #endif
758
759 ascfree(p_str1);
760 free(p_str2);
761 CU_TEST(0 == ascmeminuse());
762
763 p_doub1 = (double *)ascmalloc(sizeof(double)); /* allocate 1 block */
764 CU_TEST_FATAL(NULL != p_doub1);
765
766 p_doub2 = (double *)malloc(sizeof(double)); /* allocate another outside ascMalloc */
767 CU_TEST_FATAL(NULL != p_doub2);
768
769 #ifdef MALLOC_DEBUG
770 CU_TEST(0 == AllocatedMemory(NULL, 0)); /* NULL pointer */
771
772 CU_TEST(0 == AllocatedMemory(p_doub2, 0)); /* pointer allocated outside ascMalloc */
773
774 CU_TEST(2 == AllocatedMemory(p_doub1, sizeof(double)));/* complete blocks */
775
776 CU_TEST(1 == AllocatedMemory(p_doub1, 0)); /* contained blocks */
777 CU_TEST(1 == AllocatedMemory((char *)p_doub1+1, sizeof(double)-1));
778
779 CU_TEST(-1 == AllocatedMemory(p_doub1, sizeof(double)+1)); /* overlapping blocks */
780 CU_TEST(-1 == AllocatedMemory((char *)p_doub1+1, sizeof(double)));
781 CU_TEST(-1 == AllocatedMemory((char *)p_doub1-1, 2));
782
783 CU_TEST(0 == AllocatedMemory((char *)p_doub1-1, 1)); /* non-overlapping blocks */
784 CU_TEST(0 == AllocatedMemory((char *)p_doub1-100, 100));
785
786 #else
787 CU_TEST(1 == AllocatedMemory(NULL, 0)); /* NULL pointer */
788
789 CU_TEST(1 == AllocatedMemory(p_doub2, 0)); /* pointer allocated outside ascMalloc */
790
791 CU_TEST(1 == AllocatedMemory(p_doub1, sizeof(double)));/* complete blocks */
792
793 CU_TEST(1 == AllocatedMemory(p_doub1, 0)); /* contained blocks */
794 CU_TEST(1 == AllocatedMemory((char *)p_doub1+1, sizeof(double)-1));
795
796 CU_TEST(1 == AllocatedMemory(p_doub1, sizeof(double)+1)); /* overlapping blocks */
797 CU_TEST(1 == AllocatedMemory((char *)p_doub1+1, sizeof(double)));
798 CU_TEST(1 == AllocatedMemory((char *)p_doub1-1, 2));
799
800 CU_TEST(1 == AllocatedMemory((char *)p_doub1-1, 1)); /* non-overlapping blocks */
801 CU_TEST(1 == AllocatedMemory((char *)p_doub1-100, 100));
802
803 #endif
804
805 ascfree(p_doub1);
806 free(p_doub2);
807 CU_TEST(0 == ascmeminuse());
808
809 /* test InMemoryBlock() */
810
811 p_str1 = (char *)ascmalloc(100); /* allocate 1 block */
812 CU_TEST_FATAL(NULL != p_str1);
813 CU_TEST(0 != InMemoryBlock(p_str1, p_str1));
814 CU_TEST(0 != InMemoryBlock(p_str1, p_str1+1));
815 CU_TEST(0 != InMemoryBlock(p_str1, p_str1+50));
816 CU_TEST(0 != InMemoryBlock(p_str1, p_str1+99));
817 #ifdef MALLOC_DEBUG
818 CU_TEST(0 == InMemoryBlock(p_str1, p_str1-1));
819 CU_TEST(0 == InMemoryBlock(p_str1, p_str1+100));
820 CU_TEST(0 == InMemoryBlock(p_str1, p_str1+101));
821 CU_TEST(0 == InMemoryBlock(p_str1, (VOIDPTR)0));
822 #else
823 CU_TEST(1 == InMemoryBlock(p_str1, p_str1-1));
824 CU_TEST(1 == InMemoryBlock(p_str1, p_str1+100));
825 CU_TEST(1 == InMemoryBlock(p_str1, p_str1+101));
826 CU_TEST(1 == InMemoryBlock(p_str1, (VOIDPTR)0));
827 #endif
828
829 ascfree(p_str1);
830 CU_TEST(0 == ascmeminuse());
831
832 /* test AssertAllocatedMemory() */
833
834 #ifndef ASC_NO_ASSERTIONS
835 asc_assert_catch(TRUE); /* prepare to test assertions */
836
837 asc_assert_reset();
838 if (0 == setjmp(g_asc_test_env))
839 AssertAllocatedMemory(p_str1, 100); /* error - no memory allocated */
840 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
841 CU_TEST(TRUE == asc_assert_failed());
842 #else
843 CU_TEST(FALSE == asc_assert_failed());
844 #endif
845
846 p_str1 = (char *)ascmalloc(100);
847 p_int1 = (int *)asccalloc(10, sizeof(int));
848
849 p_str2 = (char *)malloc(100); /* don't use asc*alloc! */
850 p_int2 = (int *)calloc(10, sizeof(int));
851
852 asc_assert_reset();
853 if (0 == setjmp(g_asc_test_env))
854 AssertAllocatedMemory(NULL, 100); /* error - NULL ptr */
855 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
856 CU_TEST(TRUE == asc_assert_failed());
857 #else
858 CU_TEST(FALSE == asc_assert_failed());
859 #endif
860
861 asc_assert_reset();
862 if (0 == setjmp(g_asc_test_env))
863 AssertAllocatedMemory(p_str1, 100); /* ok - allocated block, correct size*/
864 CU_TEST(FALSE == asc_assert_failed());
865
866 asc_assert_reset();
867 if (0 == setjmp(g_asc_test_env))
868 AssertAllocatedMemory(p_str1, 99); /* error - incorrect size */
869 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
870 CU_TEST(TRUE == asc_assert_failed());
871 #else
872 CU_TEST(FALSE == asc_assert_failed());
873 #endif
874
875 asc_assert_reset();
876 if (0 == setjmp(g_asc_test_env))
877 AssertAllocatedMemory(p_str1, 101); /* error - incorrect size */
878 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
879 CU_TEST(TRUE == asc_assert_failed());
880 #else
881 CU_TEST(FALSE == asc_assert_failed());
882 #endif
883
884 asc_assert_reset();
885 if (0 == setjmp(g_asc_test_env))
886 AssertAllocatedMemory(p_str2, 100); /* error - invalid ptr */
887 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
888 CU_TEST(TRUE == asc_assert_failed());
889 #else
890 CU_TEST(FALSE == asc_assert_failed());
891 #endif
892
893 asc_assert_reset();
894 if (0 == setjmp(g_asc_test_env))
895 AssertAllocatedMemory(p_int1, 10*sizeof(int)); /* ok - allocated block, correct size*/
896 CU_TEST(FALSE == asc_assert_failed());
897
898 asc_assert_reset();
899 if (0 == setjmp(g_asc_test_env))
900 AssertAllocatedMemory(p_int1, 10*sizeof(int)-1); /* error - incorrect size */
901 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
902 CU_TEST(TRUE == asc_assert_failed());
903 #else
904 CU_TEST(FALSE == asc_assert_failed());
905 #endif
906
907 asc_assert_reset();
908 if (0 == setjmp(g_asc_test_env))
909 AssertAllocatedMemory(p_int1, 10*sizeof(int)+1); /* error - incorrect size */
910 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
911 CU_TEST(TRUE == asc_assert_failed());
912 #else
913 CU_TEST(FALSE == asc_assert_failed());
914 #endif
915
916 asc_assert_reset();
917 if (0 == setjmp(g_asc_test_env))
918 AssertAllocatedMemory(p_int2, 10*sizeof(int)); /* error - invalid ptr */
919 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
920 CU_TEST(TRUE == asc_assert_failed());
921 #else
922 CU_TEST(FALSE == asc_assert_failed());
923 #endif
924
925 asc_assert_catch(FALSE); /* done testing assertions */
926 #endif /* !ASC_NO_ASSERTIONS */
927
928 ascfree(p_str1);
929 ascfree(p_int1);
930 free(p_str2);
931 free(p_int2);
932
933 /* test AssertMemory() */
934
935 #ifndef ASC_NO_ASSERTIONS
936 asc_assert_catch(TRUE); /* prepare to test assertions */
937
938 asc_assert_reset();
939 if (0 == setjmp(g_asc_test_env))
940 AssertMemory(p_str1); /* error - no memory allocated */
941 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
942 CU_TEST(TRUE == asc_assert_failed());
943 #else
944 CU_TEST(FALSE == asc_assert_failed());
945 #endif
946
947 p_str1 = (char *)ascmalloc(100);
948 p_int1 = (int *)asccalloc(10, sizeof(int));
949
950 p_str2 = (char *)malloc(100); /* don't use asc*alloc! */
951 p_int2 = (int *)calloc(10, sizeof(int));
952
953 asc_assert_reset();
954 if (0 == setjmp(g_asc_test_env))
955 AssertMemory(NULL); /* error - NULL ptr */
956 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
957 CU_TEST(TRUE == asc_assert_failed());
958 #else
959 CU_TEST(FALSE == asc_assert_failed());
960 #endif
961
962 asc_assert_reset();
963 if (0 == setjmp(g_asc_test_env))
964 AssertMemory(p_str1); /* ok - start of allocated block*/
965 CU_TEST(FALSE == asc_assert_failed());
966
967 asc_assert_reset();
968 if (0 == setjmp(g_asc_test_env))
969 AssertMemory(p_str1+10); /* ok - in allocated block*/
970 CU_TEST(FALSE == asc_assert_failed());
971
972 asc_assert_reset();
973 if (0 == setjmp(g_asc_test_env))
974 AssertMemory(p_int1); /* ok - start of allocated block */
975 CU_TEST(FALSE == asc_assert_failed());
976
977 asc_assert_reset();
978 if (0 == setjmp(g_asc_test_env))
979 AssertMemory(p_str2); /* error - not allocated using asc*alloc */
980 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
981 CU_TEST(TRUE == asc_assert_failed());
982 #else
983 CU_TEST(FALSE == asc_assert_failed());
984 #endif
985
986 asc_assert_reset();
987 if (0 == setjmp(g_asc_test_env))
988 AssertMemory(p_int2); /* error - not allocated using asc*alloc */
989 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
990 CU_TEST(TRUE == asc_assert_failed());
991 #else
992 CU_TEST(FALSE == asc_assert_failed());
993 #endif
994
995 asc_assert_catch(FALSE); /* done testing assertions */
996 #endif /* !ASC_NO_ASSERTIONS */
997
998 ascfree(p_str1);
999 ascfree(p_int1);
1000 free(p_str2);
1001 free(p_int2);
1002
1003 /* test AssertContainedMemory() */
1004
1005 #ifndef ASC_NO_ASSERTIONS
1006 asc_assert_catch(TRUE); /* prepare to test assertions */
1007
1008 asc_assert_reset();
1009 if (0 == setjmp(g_asc_test_env))
1010 AssertContainedMemory(p_str1, 100); /* error - no memory allocated */
1011 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
1012 CU_TEST(TRUE == asc_assert_failed());
1013 #else
1014 CU_TEST(FALSE == asc_assert_failed());
1015 #endif
1016
1017 p_str1 = (char *)ascmalloc(100);
1018 p_int1 = (int *)asccalloc(10, sizeof(int));
1019
1020 p_str2 = (char *)malloc(100); /* don't use asc*alloc! */
1021 p_int2 = (int *)calloc(10, sizeof(int));
1022
1023 asc_assert_reset();
1024 if (0 == setjmp(g_asc_test_env))
1025 AssertContainedMemory(NULL, 100); /* error - NULL ptr */
1026 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
1027 CU_TEST(TRUE == asc_assert_failed());
1028 #else
1029 CU_TEST(FALSE == asc_assert_failed());
1030 #endif
1031
1032 asc_assert_reset();
1033 if (0 == setjmp(g_asc_test_env))
1034 AssertContainedMemory(p_str1, 100); /* ok - allocated block, correct size*/
1035 CU_TEST(FALSE == asc_assert_failed());
1036
1037 asc_assert_reset();
1038 if (0 == setjmp(g_asc_test_env))
1039 AssertContainedMemory(p_str1, 0); /* ok - contained in a block*/
1040 CU_TEST(FALSE == asc_assert_failed());
1041
1042 asc_assert_reset();
1043 if (0 == setjmp(g_asc_test_env))
1044 AssertContainedMemory(p_str1+10, 50); /* ok - contained in a block */
1045 CU_TEST(FALSE == asc_assert_failed());
1046
1047 asc_assert_reset();
1048 if (0 == setjmp(g_asc_test_env))
1049 AssertContainedMemory(p_str1, 101); /* error - incorrect size */
1050 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
1051 CU_TEST(TRUE == asc_assert_failed());
1052 #else
1053 CU_TEST(FALSE == asc_assert_failed());
1054 #endif
1055
1056 asc_assert_reset();
1057 if (0 == setjmp(g_asc_test_env))
1058 AssertContainedMemory(p_str2, 0); /* error - invalid ptr */
1059 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
1060 CU_TEST(TRUE == asc_assert_failed());
1061 #else
1062 CU_TEST(FALSE == asc_assert_failed());
1063 #endif
1064
1065 asc_assert_reset();
1066 if (0 == setjmp(g_asc_test_env))
1067 AssertContainedMemory(p_int1, 10*sizeof(int)); /* ok - allocated block, correct size*/
1068 CU_TEST(FALSE == asc_assert_failed());
1069
1070 asc_assert_reset();
1071 if (0 == setjmp(g_asc_test_env))
1072 AssertContainedMemory(p_int1, sizeof(int)-1); /* ok - incorrect size */
1073 CU_TEST(FALSE == asc_assert_failed());
1074
1075 asc_assert_reset();
1076 if (0 == setjmp(g_asc_test_env))
1077 AssertContainedMemory(p_int1, 10*sizeof(int)+1); /* error - incorrect size */
1078 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
1079 CU_TEST(TRUE == asc_assert_failed());
1080 #else
1081 CU_TEST(FALSE == asc_assert_failed());
1082 #endif
1083
1084 asc_assert_reset();
1085 if (0 == setjmp(g_asc_test_env))
1086 AssertContainedMemory(p_int2, 10*sizeof(int)); /* error - invalid ptr */
1087 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
1088 CU_TEST(TRUE == asc_assert_failed());
1089 #else
1090 CU_TEST(FALSE == asc_assert_failed());
1091 #endif
1092
1093 asc_assert_catch(FALSE); /* done testing assertions */
1094 #endif /* !ASC_NO_ASSERTIONS */
1095
1096 ascfree(p_str1);
1097 ascfree(p_int1);
1098 free(p_str2);
1099 free(p_int2);
1100
1101 /* test AssertContainedIn() */
1102
1103 #ifndef ASC_NO_ASSERTIONS
1104 asc_assert_catch(TRUE); /* prepare to test assertions */
1105
1106 asc_assert_reset();
1107 if (0 == setjmp(g_asc_test_env))
1108 AssertContainedIn(p_str1, p_str1); /* error - no memory allocated */
1109 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
1110 CU_TEST(TRUE == asc_assert_failed());
1111 #else
1112 CU_TEST(FALSE == asc_assert_failed());
1113 #endif
1114
1115 p_str1 = (char *)ascmalloc(100); /* allocate 1 block */
1116 CU_TEST_FATAL(NULL != p_str1);
1117
1118 asc_assert_reset();
1119 if (0 == setjmp(g_asc_test_env))
1120 AssertContainedIn(p_str1, p_str1); /* ok - same pointer */
1121 CU_TEST(FALSE == asc_assert_failed());
1122
1123 asc_assert_reset();
1124 if (0 == setjmp(g_asc_test_env))
1125 AssertContainedIn(p_str1, p_str1+1); /* ok - in block */
1126 CU_TEST(FALSE == asc_assert_failed());
1127
1128 asc_assert_reset();
1129 if (0 == setjmp(g_asc_test_env))
1130 AssertContainedIn(p_str1, p_str1+50); /* ok - in block */
1131 CU_TEST(FALSE == asc_assert_failed());
1132
1133 asc_assert_reset();
1134 if (0 == setjmp(g_asc_test_env))
1135 AssertContainedIn(p_str1, p_str1+99); /* ok - in block */
1136 CU_TEST(FALSE == asc_assert_failed());
1137
1138 asc_assert_reset();
1139 if (0 == setjmp(g_asc_test_env))
1140 AssertContainedIn(p_str1, p_str1-1); /* error - outside block */
1141 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
1142 CU_TEST(TRUE == asc_assert_failed());
1143 #else
1144 CU_TEST(FALSE == asc_assert_failed());
1145 #endif
1146
1147 asc_assert_reset();
1148 if (0 == setjmp(g_asc_test_env))
1149 AssertContainedIn(p_str1, p_str1+100); /* error - outside block */
1150 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
1151 CU_TEST(TRUE == asc_assert_failed());
1152 #else
1153 CU_TEST(FALSE == asc_assert_failed());
1154 #endif
1155
1156 asc_assert_reset();
1157 if (0 == setjmp(g_asc_test_env))
1158 AssertContainedIn(p_str1, p_str1+101); /* error - outside block */
1159 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
1160 CU_TEST(TRUE == asc_assert_failed());
1161 #else
1162 CU_TEST(FALSE == asc_assert_failed());
1163 #endif
1164
1165 asc_assert_reset();
1166 if (0 == setjmp(g_asc_test_env))
1167 AssertContainedIn(p_str1, NULL); /* error - NULL ptr */
1168 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
1169 CU_TEST(TRUE == asc_assert_failed());
1170 #else
1171 CU_TEST(FALSE == asc_assert_failed());
1172 #endif
1173
1174 asc_assert_reset();
1175 if (0 == setjmp(g_asc_test_env))
1176 AssertContainedIn(NULL, p_str1); /* error - NULL ptr */
1177 #if defined(MALLOC_DEBUG) && defined(ALLOCATED_TESTS)
1178 CU_TEST(TRUE == asc_assert_failed());
1179 #else
1180 CU_TEST(FALSE == asc_assert_failed());
1181 #endif
1182
1183 asc_assert_catch(FALSE); /* done testing assertions */
1184 #endif /* !ASC_NO_ASSERTIONS */
1185
1186 ascfree(p_str1);
1187
1188 CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */
1189 }
1190
1191 /*===========================================================================*/
1192 /* Registration information */
1193
1194 static CU_TestInfo ascMalloc_test_list[] = {
1195 {"test_ascMalloc", test_ascMalloc},
1196 CU_TEST_INFO_NULL
1197 };
1198
1199 static CU_SuiteInfo suites[] = {
1200 {"test_utilities_ascMalloc", NULL, NULL, ascMalloc_test_list},
1201 CU_SUITE_INFO_NULL
1202 };
1203
1204 /*-------------------------------------------------------------------*/
1205 CU_ErrorCode test_register_utilities_ascMalloc(void)
1206 {
1207 return CU_register_suites(suites);
1208 }

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