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