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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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