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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 539 - (show annotations) (download) (as text)
Tue Apr 25 23:11:59 2006 UTC (16 years, 5 months ago) by johnpye
File MIME type: text/x-csrc
File size: 15860 byte(s)
Removed references to ascPrintType.h
1 /*
2 * Unit test functions for ASCEND: general/table.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/table.h>
31 #include "CUnit/CUnit.h"
32 #include "assertimpl.h"
33 #include "test_table.h"
34
35 /* transform function used in test_list(). */
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 /* transform function used in test_list(). */
44 static
45 void mult_by_2_and_add(VOIDPTR p, VOIDPTR add)
46 {
47 if ((NULL != p) && (NULL != add))
48 *((unsigned long*)p) = *((unsigned long*)p) * 2 + *((unsigned long*)add);
49 }
50
51
52 static void test_table(void)
53 {
54 struct Table *p_table1;
55 unsigned long i;
56 unsigned long incr;
57 unsigned long *pint_array[50];
58 char str50[50];
59 unsigned long prior_meminuse;
60
61 prior_meminuse = ascmeminuse(); /* save meminuse() at start of test function */
62
63 #ifdef NDEBUG
64 CU_FAIL("test_table() compiled with NDEBUG - some features not tested.");
65 #endif
66 #ifndef MALLOC_DEBUG
67 CU_FAIL("test_table() compiled without MALLOC_DEBUG - memory management not tested.");
68 #endif
69
70 /* NOTE: Each test section assumes that
71 * 1. the local Table* have been destroyed
72 * 2. pint_array[0..49] is allocated and initialized to [0..49]
73 *
74 * If a test section messes with any of these, then it must restore
75 * this state before finishing.
76 */
77
78 for (i=0 ; i<50 ; ++i) { /* create some test data */
79 pint_array[i] = (unsigned long*)ascmalloc(sizeof(unsigned long));
80 *pint_array[i] = i;
81 }
82
83 /* test CreateTable(), TableDestroy() */
84
85 p_table1 = CreateTable(0); /* create a table having initial capacity = 0 */
86 CU_TEST(0 == TableSize(p_table1));
87 CU_TEST(0 == TableHashSize(p_table1));
88 CU_TEST(NULL == TableLastFind(p_table1));
89
90 #ifdef MALLOC_DEBUG
91 CU_TEST(0 != AllocatedMemory((VOIDPTR)p_table1, sizeof(VOIDPTR)));
92 #endif
93
94 DestroyTable(p_table1, TRUE); /* destroy the table and check for deallocation */
95 #ifdef MALLOC_DEBUG
96 CU_TEST(0 == AllocatedMemory((VOIDPTR)p_table1, sizeof(VOIDPTR)));
97 #endif
98
99 p_table1 = CreateTable(31); /* create a more typical table */
100 CU_TEST(0 == TableSize(p_table1));
101 CU_TEST(31 == TableHashSize(p_table1));
102 CU_TEST(NULL == TableLastFind(p_table1));
103
104 for (i=0 ; i<10 ; ++i) { /* fill the list with data */
105 sprintf(str50, "string_%lu", i);
106 AddTableData(p_table1, pint_array[i], str50);
107 }
108
109 #ifdef MALLOC_DEBUG
110 for (i=0 ; i<10 ; ++i) { /* check that all pointers are still active */
111 CU_TEST(2 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long)));
112 }
113 #endif
114
115 DestroyTable(p_table1, FALSE); /* clean up, leaving data in tact */
116
117 #ifdef MALLOC_DEBUG
118 for (i=0 ; i<10 ; ++i) { /* check that data is still in tact */
119 CU_TEST(2 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long)));
120 }
121 #endif
122
123 p_table1 = CreateTable(31); /* create the table again, fill it with some of the data */
124 for (i=0 ; i<10 ; ++i) {
125 sprintf(str50, "string_%lu", i);
126 AddTableData(p_table1, pint_array[i], str50);
127 }
128
129 #ifdef MALLOC_DEBUG
130 for (i=0 ; i<20 ; ++i) { /* check that all pointers are still active */
131 CU_TEST(2 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long)));
132 }
133 #endif
134
135 DestroyTable(p_table1, TRUE); /* clean up, deallocating data stored in list */
136
137 #ifdef MALLOC_DEBUG
138 for (i=0 ; i<20 ; ++i) { /* check that some data was deallocated */
139 if (i < 10) {
140 CU_TEST(0 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long)));
141 }
142 else {
143 CU_TEST(2 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long)));
144 }
145 }
146 #endif
147
148 for (i=0 ; i<10 ; ++i) { /* restore test array */
149 pint_array[i] = (unsigned long*)ascmalloc(sizeof(unsigned long));
150 *pint_array[i] = i;
151 }
152
153 /* test AddTableData(), LookupTableData(), RemoveTableData() */
154
155 p_table1 = CreateTable(13); /* create and fill a table */
156
157 AddTableData(p_table1, NULL, "NULL data"); /* add a NULL data item */
158 CU_TEST(1 == TableSize(p_table1));
159 CU_TEST(13 == TableHashSize(p_table1));
160 CU_TEST(NULL == TableLastFind(p_table1));
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 AddTableData(NULL, pint_array[0], str50); /* error if NULL table */
168 CU_TEST(TRUE == asc_assert_failed());
169
170 asc_assert_reset();
171 if (0 == setjmp(g_asc_test_env))
172 AddTableData(p_table1, pint_array[0], NULL); /* error if NULL id */
173 CU_TEST(TRUE == asc_assert_failed());
174
175 asc_assert_reset();
176 if (0 == setjmp(g_asc_test_env))
177 (void)LookupTableData(NULL, str50); /* error if NULL table */
178 CU_TEST(TRUE == asc_assert_failed());
179
180 asc_assert_reset();
181 if (0 == setjmp(g_asc_test_env))
182 (void)LookupTableData(p_table1, NULL); /* error if NULL id */
183 CU_TEST(TRUE == asc_assert_failed());
184
185 asc_assert_reset();
186 if (0 == setjmp(g_asc_test_env))
187 (void)RemoveTableData(NULL, str50); /* error if NULL table */
188 CU_TEST(TRUE == asc_assert_failed());
189
190 asc_assert_reset();
191 if (0 == setjmp(g_asc_test_env))
192 (void)RemoveTableData(p_table1, NULL); /* error if NULL id */
193 CU_TEST(TRUE == asc_assert_failed());
194
195 asc_assert_catch(FALSE); /* done testing assertions */
196 #endif /* !ASC_NO_ASSERTIONS */
197
198 AddTableData(p_table1, pint_array[10], "NULL data"); /* can't add item with a duplicate id */
199 CU_TEST(1 == TableSize(p_table1));
200 CU_TEST(13 == TableHashSize(p_table1));
201 CU_TEST(NULL == TableLastFind(p_table1));
202
203 CU_TEST(NULL == LookupTableData(p_table1, "NULL data")); /* look up NULL item */
204 CU_TEST(1 == TableSize(p_table1));
205 CU_TEST(13 == TableHashSize(p_table1));
206 CU_TEST(NULL == TableLastFind(p_table1));
207
208 CU_TEST(NULL == RemoveTableData(p_table1, "NULL data")); /* remove NULL item */
209 CU_TEST(0 == TableSize(p_table1));
210 CU_TEST(13 == TableHashSize(p_table1));
211 CU_TEST(NULL == TableLastFind(p_table1));
212
213 for (i=0 ; i<50 ; ++i) { /* add data to the table */
214 sprintf(str50, "string_%lu", i);
215 AddTableData(p_table1, pint_array[i], str50);
216 }
217 CU_TEST(50 == TableSize(p_table1));
218 CU_TEST(13 == TableHashSize(p_table1));
219 CU_TEST(pint_array[49] == TableLastFind(p_table1));
220
221 for (i=0 ; i<50 ; ++i) { /* look up data in the table */
222 sprintf(str50, "string_%lu", i);
223 CU_TEST(pint_array[i] == (unsigned long*)LookupTableData(p_table1, str50));
224 CU_TEST(*pint_array[i] == *((unsigned long*)LookupTableData(p_table1, str50)));
225 }
226
227 sprintf(str50, "string_%d", 0); /* remove the first item added */
228 CU_TEST(pint_array[0] == (unsigned long*)RemoveTableData(p_table1, str50));
229 CU_TEST(49 == TableSize(p_table1));
230 CU_TEST(13 == TableHashSize(p_table1));
231 CU_TEST(pint_array[49] == TableLastFind(p_table1));
232
233 sprintf(str50, "string_%d", 49); /* remove the last item added */
234 CU_TEST(pint_array[49] == (unsigned long*)RemoveTableData(p_table1, str50));
235 CU_TEST(48 == TableSize(p_table1));
236 CU_TEST(13 == TableHashSize(p_table1));
237 CU_TEST(NULL == TableLastFind(p_table1));
238
239 sprintf(str50, "string_%d", 27); /* remove an item in the middle */
240 CU_TEST(pint_array[27] == (unsigned long*)RemoveTableData(p_table1, str50));
241 CU_TEST(47 == TableSize(p_table1));
242 CU_TEST(13 == TableHashSize(p_table1));
243 CU_TEST(NULL == TableLastFind(p_table1));
244
245 DestroyTable(p_table1, FALSE); /* clean up, deallocating data stored in list */
246
247 /* test TableApplyOne() */
248
249 p_table1 = CreateTable(13); /* create and fill a table */
250
251 for (i=0 ; i<50 ; ++i) {
252 sprintf(str50, "string_%lu", i);
253 AddTableData(p_table1, pint_array[i], str50);
254 }
255
256 #ifndef ASC_NO_ASSERTIONS
257 asc_assert_catch(TRUE); /* prepare to test assertions */
258
259 asc_assert_reset();
260 if (0 == setjmp(g_asc_test_env))
261 TableApplyOne(NULL, mult_by_2, str50); /* error if NULL table */
262 CU_TEST(TRUE == asc_assert_failed());
263
264 asc_assert_reset();
265 if (0 == setjmp(g_asc_test_env))
266 TableApplyOne(p_table1, NULL, str50); /* error if NULL applyfunc */
267 CU_TEST(TRUE == asc_assert_failed());
268
269 asc_assert_reset();
270 if (0 == setjmp(g_asc_test_env))
271 TableApplyOne(p_table1, mult_by_2, NULL); /* error if NULL id */
272 CU_TEST(TRUE == asc_assert_failed());
273
274 asc_assert_catch(FALSE); /* done testing assertions */
275 #endif /* !ASC_NO_ASSERTIONS */
276
277 sprintf(str50, "string_%d", 49); /* apply to the last item added */
278 TableApplyOne(p_table1, mult_by_2, str50);
279 CU_TEST(98 == *(unsigned long*)LookupTableData(p_table1, str50));
280 CU_TEST(98 == *pint_array[49]);
281
282 sprintf(str50, "string_%d", 0); /* apply to the first item added */
283 TableApplyOne(p_table1, mult_by_2, str50);
284 CU_TEST(0 == *(unsigned long*)LookupTableData(p_table1, str50));
285 CU_TEST(0 == *pint_array[0]);
286
287 sprintf(str50, "string_%d", 31); /* apply to something in the middle */
288 TableApplyOne(p_table1, mult_by_2, str50);
289 CU_TEST(62 == *(unsigned long*)LookupTableData(p_table1, str50));
290 CU_TEST(62 == *pint_array[31]);
291
292 for (i=0 ; i<50 ; ++i) { /* restore the test data */
293 *pint_array[i] = i;
294 }
295
296 DestroyTable(p_table1, FALSE); /* clean up, deallocating data stored in list */
297
298 /* test TableApplyAll() */
299
300 p_table1 = CreateTable(13); /* create and fill a table */
301
302 #ifndef ASC_NO_ASSERTIONS
303 asc_assert_catch(TRUE); /* prepare to test assertions */
304
305 asc_assert_reset();
306 if (0 == setjmp(g_asc_test_env))
307 TableApplyAll(NULL, mult_by_2); /* error if NULL table */
308 CU_TEST(TRUE == asc_assert_failed());
309
310 asc_assert_reset();
311 if (0 == setjmp(g_asc_test_env))
312 TableApplyAll(p_table1, NULL); /* error if NULL applyfunc */
313 CU_TEST(TRUE == asc_assert_failed());
314
315 asc_assert_catch(FALSE); /* done testing assertions */
316 #endif /* !ASC_NO_ASSERTIONS */
317
318 TableApplyAll(p_table1, mult_by_2); /* applying function to empty table */
319
320 for (i=0 ; i<50 ; ++i) { /* add data */
321 sprintf(str50, "string_%lu", i);
322 AddTableData(p_table1, pint_array[i], str50);
323 }
324
325 TableApplyAll(p_table1, mult_by_2);
326 for (i=0 ; i<50 ; ++i) {
327 sprintf(str50, "string_%lu", i);
328 CU_TEST((i*2) == *(unsigned long*)LookupTableData(p_table1, str50));
329 CU_TEST((i*2) == *pint_array[i]);
330 }
331
332 for (i=0 ; i<50 ; ++i) { /* restore the test data */
333 *pint_array[i] = i;
334 }
335
336 DestroyTable(p_table1, FALSE); /* clean up, deallocating data stored in list */
337
338 /* test TableApplyAllTwo() */
339
340 p_table1 = CreateTable(31); /* create and fill a table */
341 incr = 10;
342
343 #ifndef ASC_NO_ASSERTIONS
344 asc_assert_catch(TRUE); /* prepare to test assertions */
345
346 asc_assert_reset();
347 if (0 == setjmp(g_asc_test_env))
348 TableApplyAllTwo(NULL, mult_by_2_and_add, &incr); /* error if NULL table */
349 CU_TEST(TRUE == asc_assert_failed());
350
351 asc_assert_reset();
352 if (0 == setjmp(g_asc_test_env))
353 TableApplyAllTwo(p_table1, NULL, &incr); /* error if NULL applyfunc */
354 CU_TEST(TRUE == asc_assert_failed());
355
356 asc_assert_catch(FALSE); /* done testing assertions */
357 #endif /* !ASC_NO_ASSERTIONS */
358
359 TableApplyAllTwo(p_table1, mult_by_2_and_add, &incr); /* applying function to empty table */
360
361 for (i=0 ; i<50 ; ++i) { /* add data */
362 sprintf(str50, "string_%lu", i);
363 AddTableData(p_table1, pint_array[i], str50);
364 }
365
366 TableApplyAllTwo(p_table1, mult_by_2_and_add, &incr);
367 for (i=0 ; i<50 ; ++i) {
368 sprintf(str50, "string_%lu", i);
369 CU_TEST(((i*2)+10) == *(unsigned long*)LookupTableData(p_table1, str50));
370 CU_TEST(((i*2)+10) == *pint_array[i]);
371 }
372
373 for (i=0 ; i<50 ; ++i) { /* restore the test data */
374 *pint_array[i] = i;
375 }
376
377 DestroyTable(p_table1, FALSE); /* clean up, deallocating data stored in list */
378
379 /* test TableSize(), TableHashSize(), TableLastFind() */
380
381 p_table1 = CreateTable(13); /* create a table */
382
383 #ifndef ASC_NO_ASSERTIONS
384 asc_assert_catch(TRUE); /* prepare to test assertions */
385
386 asc_assert_reset();
387 if (0 == setjmp(g_asc_test_env))
388 (void)TableSize(NULL); /* error if NULL table */
389 CU_TEST(TRUE == asc_assert_failed());
390
391 asc_assert_reset();
392 if (0 == setjmp(g_asc_test_env))
393 (void)TableHashSize(NULL); /* error if NULL table */
394 CU_TEST(TRUE == asc_assert_failed());
395
396 asc_assert_reset();
397 if (0 == setjmp(g_asc_test_env))
398 (void)TableLastFind(NULL); /* error if NULL table */
399 CU_TEST(TRUE == asc_assert_failed());
400
401 asc_assert_catch(FALSE); /* done testing assertions */
402 #endif /* !ASC_NO_ASSERTIONS */
403
404 CU_TEST(0 == TableSize(p_table1));
405 CU_TEST(13 == TableHashSize(p_table1));
406 CU_TEST(NULL == TableLastFind(p_table1));
407
408 for (i=0 ; i<10 ; ++i) {
409 sprintf(str50, "string_%lu", i);
410 AddTableData(p_table1, pint_array[i], str50);
411 CU_TEST((i+1) == TableSize(p_table1));
412 CU_TEST(pint_array[i] == (unsigned long*)TableLastFind(p_table1));
413 }
414
415 for (i=0 ; i<10 ; ++i) {
416 sprintf(str50, "string_%lu", i);
417 CU_TEST(pint_array[i] == (unsigned long*)LookupTableData(p_table1, str50));
418 CU_TEST(pint_array[i] == (unsigned long*)TableLastFind(p_table1));
419 }
420
421 DestroyTable(p_table1, FALSE); /* clean up, deallocating data stored in list */
422
423 p_table1 = CreateTable(1); /* create a table */
424
425 CU_TEST(0 == TableSize(p_table1));
426 CU_TEST(1 == TableHashSize(p_table1));
427 CU_TEST(NULL == TableLastFind(p_table1));
428
429 for (i=0 ; i<10 ; ++i) {
430 sprintf(str50, "string_%lu", i);
431 AddTableData(p_table1, pint_array[i], str50);
432 CU_TEST((i+1) == TableSize(p_table1));
433 CU_TEST(pint_array[i] == (unsigned long*)TableLastFind(p_table1));
434 }
435
436 for (i=0 ; i<10 ; ++i) {
437 sprintf(str50, "string_%lu", i);
438 CU_TEST(pint_array[i] == (unsigned long*)LookupTableData(p_table1, str50));
439 CU_TEST(pint_array[i] == (unsigned long*)TableLastFind(p_table1));
440 }
441
442 DestroyTable(p_table1, FALSE); /* clean up, deallocating data stored in list */
443
444 /* not tested - PrintTable() */
445
446 /* clean up and exit */
447 for (i=0 ; i<50 ; ++i)
448 ascfree(pint_array[i]);
449
450 CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */
451 }
452
453 /*===========================================================================*/
454 /* Registration information */
455
456 static CU_TestInfo table_test_list[] = {
457 {"test_table", test_table},
458 CU_TEST_INFO_NULL
459 };
460
461 static CU_SuiteInfo suites[] = {
462 {"test_general_table", NULL, NULL, table_test_list},
463 CU_SUITE_INFO_NULL
464 };
465
466 /*-------------------------------------------------------------------*/
467 CU_ErrorCode test_register_general_table(void)
468 {
469 return CU_register_suites(suites);
470 }

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