/[ascend]/trunk/base/generic/general/test/test_stack.c
ViewVC logotype

Contents of /trunk/base/generic/general/test/test_stack.c

Parent Directory Parent Directory | Revision Log Revision Log


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

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