1 |
/* |
2 |
* Unit test functions for ASCEND: general/stack.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 <stdlib.h> |
25 |
#include <stdio.h> |
26 |
#include <stdarg.h> |
27 |
#include <utilities/ascConfig.h> |
28 |
#include <utilities/ascMalloc.h> |
29 |
#include <utilities/ascPrint.h> |
30 |
#include <general/stack.h> |
31 |
#include "CUnit/CUnit.h" |
32 |
#include "assertimpl.h" |
33 |
#include "test_stack.h" |
34 |
|
35 |
/* transform function used in test_stack(). */ |
36 |
static |
37 |
void mult_by_2(VOIDPTR p) |
38 |
{ |
39 |
if (NULL != p) |
40 |
*((unsigned long*)p) = *((unsigned long*)p) * 2; |
41 |
} |
42 |
|
43 |
static void test_stack(void) |
44 |
{ |
45 |
struct gs_stack_t *p_stack1; |
46 |
unsigned long i; |
47 |
unsigned long *pint_array[20]; |
48 |
unsigned long *pint; |
49 |
unsigned long prior_meminuse; |
50 |
|
51 |
prior_meminuse = ascmeminuse(); /* save meminuse() at start of test function */ |
52 |
|
53 |
#ifdef NDEBUG |
54 |
CU_FAIL("test_stack() compiled with NDEBUG - some features not tested."); |
55 |
#endif |
56 |
#ifndef MALLOC_DEBUG |
57 |
CU_FAIL("test_stack() compiled without MALLOC_DEBUG - memory management not tested."); |
58 |
#endif |
59 |
|
60 |
/* NOTE: Each test section assumes that |
61 |
* 1. the local gs_stack_t* have been destroyed |
62 |
* 2. pint_array[0..19] is allocated and initialized to [0..19] |
63 |
* |
64 |
* If a test section messes with any of these, then it must restore |
65 |
* this state before finishing. |
66 |
*/ |
67 |
|
68 |
for (i=0 ; i<20 ; ++i) { /* create some test data */ |
69 |
pint_array[i] = (unsigned long*)ascmalloc(sizeof(unsigned long)); |
70 |
*pint_array[i] = i; |
71 |
} |
72 |
|
73 |
/* test gs_stack_create(), gs_stack_destroy() */ |
74 |
|
75 |
p_stack1 = gs_stack_create(0); /* create a stack having initial capacity = 0 */ |
76 |
CU_TEST(0 == gs_stack_size(p_stack1)); |
77 |
CU_TEST(0 != gs_stack_empty(p_stack1)); |
78 |
|
79 |
#ifdef MALLOC_DEBUG |
80 |
CU_TEST(0 != AllocatedMemory((VOIDPTR)p_stack1, sizeof(VOIDPTR))); |
81 |
#endif |
82 |
|
83 |
gs_stack_destroy(p_stack1, TRUE); /* destroy the stack and check for deallocation */ |
84 |
|
85 |
#ifdef MALLOC_DEBUG |
86 |
CU_TEST(0 == AllocatedMemory((VOIDPTR)p_stack1, sizeof(VOIDPTR))); |
87 |
#endif |
88 |
|
89 |
p_stack1 = gs_stack_create(10); /* create a new stack with capacity = 10 */ |
90 |
CU_TEST(0 == gs_stack_size(p_stack1)); |
91 |
CU_TEST(0 != gs_stack_empty(p_stack1)); |
92 |
|
93 |
for (i=0 ; i<10 ; ++i) { /* push some data onto the stack */ |
94 |
gs_stack_push(p_stack1, pint_array[i]); |
95 |
} |
96 |
CU_TEST(10 == gs_stack_size(p_stack1)); |
97 |
CU_TEST(0 == gs_stack_empty(p_stack1)); |
98 |
|
99 |
#ifdef MALLOC_DEBUG |
100 |
for (i=0 ; i<20 ; ++i) { /* check that all pointers are still active */ |
101 |
CU_TEST(2 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long))); |
102 |
} |
103 |
#endif |
104 |
|
105 |
gs_stack_destroy(p_stack1, FALSE); /* clean up, leaving data in tact */ |
106 |
|
107 |
#ifdef MALLOC_DEBUG |
108 |
for (i=0 ; i<10 ; ++i) { /* check that data is still in tact */ |
109 |
CU_TEST(2 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long))); |
110 |
} |
111 |
#endif |
112 |
|
113 |
p_stack1 = gs_stack_create(5); /* create the stack again, fill it with half the data */ |
114 |
for (i=0 ; i<5 ; ++i) { |
115 |
gs_stack_push(p_stack1, pint_array[i]); |
116 |
} |
117 |
|
118 |
#ifdef MALLOC_DEBUG |
119 |
for (i=0 ; i<10 ; ++i) { /* check that all pointers are still active */ |
120 |
CU_TEST(2 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long))); |
121 |
} |
122 |
#endif |
123 |
|
124 |
gs_stack_destroy(p_stack1, TRUE); /* clean up, deallocating data stored in stack */ |
125 |
|
126 |
#ifdef MALLOC_DEBUG |
127 |
for (i=0 ; i<10 ; ++i) { /* check that some data was deallocated */ |
128 |
if (i < 5) { |
129 |
CU_TEST(0 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long))); |
130 |
} |
131 |
else { |
132 |
CU_TEST(2 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long))); |
133 |
} |
134 |
} |
135 |
#endif |
136 |
|
137 |
for (i=5 ; i<10 ; ++i) { /* deallocate the rest of the data */ |
138 |
ascfree(pint_array[i]); |
139 |
} |
140 |
|
141 |
#ifdef MALLOC_DEBUG |
142 |
for (i=0 ; i<20 ; ++i) { /* confirm that all data is now deallocated */ |
143 |
if (i < 10) { |
144 |
CU_TEST(0 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long))); |
145 |
} |
146 |
else { |
147 |
CU_TEST(2 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long))); |
148 |
} |
149 |
} |
150 |
#endif |
151 |
|
152 |
for (i=0 ; i<10 ; ++i) { /* restore test array */ |
153 |
pint_array[i] = (unsigned long*)ascmalloc(sizeof(unsigned long)); |
154 |
*pint_array[i] = i; |
155 |
} |
156 |
/* test gs_stack_push(), gs_stack_pop() */ |
157 |
|
158 |
p_stack1 = gs_stack_create(10); /* create and fill a stack */ |
159 |
|
160 |
gs_stack_push(p_stack1, pint_array[15]); |
161 |
|
162 |
#ifndef ASC_NO_ASSERTIONS |
163 |
asc_assert_catch(TRUE); /* prepare to test assertions */ |
164 |
|
165 |
asc_assert_reset(); |
166 |
if (0 == setjmp(g_asc_test_env)) |
167 |
gs_stack_push(NULL, pint_array[15]);/* error if NULL stack* */ |
168 |
CU_TEST(TRUE == asc_assert_failed()); |
169 |
|
170 |
asc_assert_reset(); |
171 |
if (0 == setjmp(g_asc_test_env)) |
172 |
(void)gs_stack_pop(NULL); /* error if NULL stack* */ |
173 |
CU_TEST(TRUE == asc_assert_failed()); |
174 |
|
175 |
asc_assert_catch(FALSE); /* done testing assertions */ |
176 |
#endif /* !ASC_NO_ASSERTIONS */ |
177 |
|
178 |
pint = (unsigned long*)gs_stack_pop(p_stack1); |
179 |
CU_TEST(pint_array[15] == pint); |
180 |
CU_TEST(*pint_array[15] == *pint); |
181 |
CU_TEST(0 != gs_stack_empty(p_stack1)); /* stack should be empty */ |
182 |
|
183 |
CU_TEST(NULL == gs_stack_pop(p_stack1)); /* popping an empty stack should be ok */ |
184 |
CU_TEST(NULL == gs_stack_pop(p_stack1)); |
185 |
CU_TEST(NULL == gs_stack_pop(p_stack1)); |
186 |
CU_TEST(NULL == gs_stack_pop(p_stack1)); |
187 |
|
188 |
gs_stack_clear(p_stack1); |
189 |
|
190 |
for (i=0 ; i<10 ; ++i) { /* push some data onto the stack */ |
191 |
gs_stack_push(p_stack1, pint_array[i]); |
192 |
} |
193 |
|
194 |
for (i=0 ; i<10 ; ++i) { |
195 |
pint = (unsigned long*)gs_stack_pop(p_stack1); |
196 |
CU_TEST(pint_array[9-i] == pint); |
197 |
CU_TEST(*pint_array[9-i] == *pint); |
198 |
if (i < 9) { |
199 |
CU_TEST(0 == gs_stack_empty(p_stack1)); |
200 |
} |
201 |
else { |
202 |
CU_TEST(0 != gs_stack_empty(p_stack1)); |
203 |
} |
204 |
} |
205 |
|
206 |
CU_TEST(NULL == gs_stack_pop(p_stack1)); /* popping an empty stack should be ok */ |
207 |
CU_TEST(NULL == gs_stack_pop(p_stack1)); |
208 |
CU_TEST(NULL == gs_stack_pop(p_stack1)); |
209 |
CU_TEST(NULL == gs_stack_pop(p_stack1)); |
210 |
|
211 |
gs_stack_destroy(p_stack1, FALSE); /* clean up the stack, preserving data */ |
212 |
|
213 |
/* test gs_stack_size(), gs_stack_empty() */ |
214 |
|
215 |
p_stack1 = gs_stack_create(10); /* create a stack */ |
216 |
|
217 |
#ifndef ASC_NO_ASSERTIONS |
218 |
asc_assert_catch(TRUE); /* prepare to test assertions */ |
219 |
|
220 |
asc_assert_reset(); |
221 |
if (0 == setjmp(g_asc_test_env)) |
222 |
(void)gs_stack_size(NULL); /* error if NULL stack* */ |
223 |
CU_TEST(TRUE == asc_assert_failed()); |
224 |
|
225 |
asc_assert_reset(); |
226 |
if (0 == setjmp(g_asc_test_env)) |
227 |
(void)gs_stack_empty(NULL); /* error if NULL stack* */ |
228 |
CU_TEST(TRUE == asc_assert_failed()); |
229 |
|
230 |
asc_assert_catch(FALSE); /* done testing assertions */ |
231 |
#endif /* !ASC_NO_ASSERTIONS */ |
232 |
|
233 |
CU_TEST(0 == gs_stack_size(p_stack1)); |
234 |
CU_TEST(0 != gs_stack_empty(p_stack1)); |
235 |
for (i=0 ; i<10 ; ++i) { |
236 |
gs_stack_push(p_stack1, pint_array[i]); |
237 |
CU_TEST((i+1) == gs_stack_size(p_stack1)); |
238 |
CU_TEST(0 == gs_stack_empty(p_stack1)); |
239 |
} |
240 |
|
241 |
gs_stack_destroy(p_stack1, FALSE); /* clean up the stack, preserving data */ |
242 |
|
243 |
/* test gs_stack_apply() */ |
244 |
|
245 |
p_stack1 = gs_stack_create(10); /* create a stack and fill with data */ |
246 |
for (i=0 ; i<10 ; ++i) { |
247 |
gs_stack_push(p_stack1, pint_array[i]); |
248 |
} |
249 |
|
250 |
#ifndef ASC_NO_ASSERTIONS |
251 |
asc_assert_catch(TRUE); /* prepare to test assertions */ |
252 |
|
253 |
asc_assert_reset(); |
254 |
if (0 == setjmp(g_asc_test_env)) |
255 |
gs_stack_apply(NULL, mult_by_2); /* error if NULL stack* */ |
256 |
CU_TEST(TRUE == asc_assert_failed()); |
257 |
|
258 |
asc_assert_reset(); |
259 |
if (0 == setjmp(g_asc_test_env)) |
260 |
gs_stack_apply(p_stack1, NULL); /* error if NULL func* */ |
261 |
CU_TEST(TRUE == asc_assert_failed()); |
262 |
|
263 |
asc_assert_catch(FALSE); /* done testing assertions */ |
264 |
#endif /* !ASC_NO_ASSERTIONS */ |
265 |
|
266 |
gs_stack_apply(p_stack1, mult_by_2); /* execute function on each data element */ |
267 |
|
268 |
for (i=0 ; i<10 ; ++i) { |
269 |
pint = (unsigned long*)gs_stack_pop(p_stack1); |
270 |
CU_TEST((2*(9-i)) == *pint); |
271 |
} |
272 |
|
273 |
gs_stack_destroy(p_stack1, FALSE); /* clean up the stack, preserving data */ |
274 |
for (i=0 ; i<20 ; ++i) /* need to restore our integer array */ |
275 |
*pint_array[i] = i; |
276 |
|
277 |
/* test gs_stack_clear() */ |
278 |
|
279 |
p_stack1 = gs_stack_create(0); /* create an empty stack */ |
280 |
|
281 |
#ifndef ASC_NO_ASSERTIONS |
282 |
asc_assert_catch(TRUE); /* prepare to test assertions */ |
283 |
|
284 |
asc_assert_reset(); |
285 |
if (0 == setjmp(g_asc_test_env)) |
286 |
gs_stack_clear(NULL); /* error if NULL stack* */ |
287 |
CU_TEST(TRUE == asc_assert_failed()); |
288 |
|
289 |
asc_assert_catch(FALSE); /* done testing assertions */ |
290 |
#endif /* !ASC_NO_ASSERTIONS */ |
291 |
|
292 |
gs_stack_clear(p_stack1); /* reset an empty stack */ |
293 |
gs_stack_destroy(p_stack1, FALSE); /* clean up the stack, preserving data */ |
294 |
|
295 |
p_stack1 = gs_stack_create(10); /* create and fill a stack */ |
296 |
for (i=0 ; i<10 ; ++i) { |
297 |
gs_stack_push(p_stack1, pint_array[i]); |
298 |
} |
299 |
|
300 |
gs_stack_clear(p_stack1); /* reset the stack */ |
301 |
CU_TEST(0 == gs_stack_size(p_stack1)); |
302 |
#ifdef MALLOC_DEBUG |
303 |
for (i=0 ; i<20 ; ++i) { /* all pointers should still be active */ |
304 |
CU_TEST(2 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long))); |
305 |
} |
306 |
#endif |
307 |
|
308 |
gs_stack_destroy(p_stack1, FALSE); /* clean up the stack, preserving data */ |
309 |
|
310 |
/* clean up and exit */ |
311 |
for (i=0 ; i<20 ; ++i) { |
312 |
ascfree(pint_array[i]); |
313 |
} |
314 |
|
315 |
CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */ |
316 |
} |
317 |
|
318 |
/*===========================================================================*/ |
319 |
/* Registration information */ |
320 |
|
321 |
static CU_TestInfo stack_test_list[] = { |
322 |
{"test_stack", test_stack}, |
323 |
CU_TEST_INFO_NULL |
324 |
}; |
325 |
|
326 |
static CU_SuiteInfo suites[] = { |
327 |
{"test_general_stack", NULL, NULL, stack_test_list}, |
328 |
CU_SUITE_INFO_NULL |
329 |
}; |
330 |
|
331 |
/*-------------------------------------------------------------------*/ |
332 |
CU_ErrorCode test_register_general_stack(void) |
333 |
{ |
334 |
return CU_register_suites(suites); |
335 |
} |