Bugfixing: free our hashpositions...
[citadel.git] / libcitadel / tests / hashlist_test.c
1
2 /*
3  *  CUnit - A Unit testing framework library for C.
4  *  Copyright (C) 2001  Anil Kumar
5  *  
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Library General Public
8  *  License as published by the Free Software Foundation; either
9  *  version 2 of the License, or (at your option) any later version.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Library General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Library General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "stringbuf_test.h"
26 #include "../lib/libcitadel.h"
27
28
29 static HashList *GetFilledHash(int n, int stepwidth)
30 {
31         HashList* TestHash;
32         int i;
33         int *val;
34
35         TestHash = NewHash(1, Flathash);
36
37         for (i = 0; i < n;  i+=stepwidth)
38         {
39                 val = (int*) malloc(sizeof(int));
40                 *val = i;
41                 Put(TestHash, IKEY(i), val, NULL);
42         }
43         return TestHash;
44 }
45
46
47
48 static void test_iterate_hash(HashList *testh, int forward, int stepwidth)
49 {
50         int i = 0;
51         HashPos  *it;
52         void *vTest;
53         long len = 0;
54         const char *Key;
55         int dir = 1;
56
57         if (forward == 0)
58                 dir = -1;
59         it = GetNewHashPos(testh, dir * stepwidth);
60         while (GetNextHashPos(testh, it, &len, &Key, &vTest) &&
61                (vTest != NULL)) {
62
63                 printf("i: %d c: %d\n", i, *(int*) vTest);
64                 i+=stepwidth;
65         }
66         DeleteHashPos(&it);
67 }
68
69 static void ValidateBackAndForth(HashList *H)
70 {
71         test_iterate_hash(H, 1, 1);
72         printf("\n");
73
74         test_iterate_hash(H, 0, 1);
75         printf("\n");
76
77         test_iterate_hash(H, 1, 2);
78         printf("\n");
79
80         test_iterate_hash(H, 0, 2);
81         printf("\n");
82
83         test_iterate_hash(H, 1, 3);
84         printf("\n");
85
86         test_iterate_hash(H, 0, 3);
87         printf("\n");
88 }
89
90 static void TestHashlistIteratorForward (void)
91 {
92         HashList *H;
93
94         H = GetFilledHash (10, 1);
95
96         ValidateBackAndForth(H);
97
98         DeleteHash(&H);
99 }
100
101
102 static void TestHashlistAddDelete (void)
103 {
104         HashList *H;
105         HashPos *at;
106         int *val, i;
107
108         H = GetFilledHash (10, 1);
109
110         at = GetNewHashPos(H, 0);
111
112         printf("Remove first\n");
113         DeleteEntryFromHash(H, at);
114         DeleteHashPos(&at);
115
116         ValidateBackAndForth(H);
117
118         printf("Insert 15\n");
119         i = 15;
120         val = (int*) malloc(sizeof(int));
121         *val = i;
122         Put(H, IKEY(i), val, NULL);
123
124         ValidateBackAndForth(H);
125
126         printf("Remove third\n");
127         at = GetNewHashPos(H, 0);
128         NextHashPos(H, at);
129         NextHashPos(H, at);
130         NextHashPos(H, at);
131         DeleteEntryFromHash(H, at);
132
133         ValidateBackAndForth(H);
134         printf("Insert -15\n");
135
136         i = -15;
137         val = (int*) malloc(sizeof(int));
138         *val = i;
139         Put(H, IKEY(i), val, NULL);
140
141         ValidateBackAndForth(H);
142
143         DeleteHashPos(&at);
144         DeleteHash(&H);
145 }
146
147 /*
148 Some samples from the original...
149         CU_ASSERT_EQUAL(10, 10);
150         CU_ASSERT_EQUAL(0, -0);
151         CU_ASSERT_EQUAL(-12, -12);
152         CU_ASSERT_NOT_EQUAL(10, 11);
153         CU_ASSERT_NOT_EQUAL(0, -1);
154         CU_ASSERT_NOT_EQUAL(-12, -11);
155         CU_ASSERT_PTR_EQUAL((void*)0x100, (void*)0x100);
156         CU_ASSERT_PTR_NOT_EQUAL((void*)0x100, (void*)0x101);
157         CU_ASSERT_PTR_NULL(NULL);
158         CU_ASSERT_PTR_NULL(0x0);
159         CU_ASSERT_PTR_NOT_NULL((void*)0x23);
160         CU_ASSERT_STRING_EQUAL(str1, str2);
161         CU_ASSERT_STRING_NOT_EQUAL(str1, str2);
162         CU_ASSERT_NSTRING_EQUAL(str1, str2, strlen(str1));
163         CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1));
164         CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1) + 1);
165         CU_ASSERT_NSTRING_NOT_EQUAL(str1, str2, 3);
166         CU_ASSERT_NSTRING_NOT_EQUAL(str1, str3, strlen(str1) + 1);
167         CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, 0.0001);
168         CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, -0.0001);
169         CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, 0.0001);
170         CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, -0.0001);
171         CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, 0.0001);
172         CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, -0.0001);
173         CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, 0.0001);
174         CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, -0.0001);
175 */
176
177
178
179 const char *MSetStrings[] = {
180         "11:63",
181         "65",
182         "1,65,77",
183         "1:65,77,80:*",
184         NULL
185 };
186
187 #define InMSet 0
188 #define NotInMSet 1
189 const long MessageNumbers[5][2][10] = {
190 /* First MSet */
191         {
192                 {11, 59, 63, 0, 0, 0, 0, 0, 0, 0}, /* In */
193                 {2, 10, 64, 65, 0, 0, 0, 0, 0, 0} /* NotIn */
194         },
195         {
196                 {65, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* In */
197                 {1, 64, 66, 0, 0, 0, 0, 0, 0, 0} /* NotIn */
198         },
199
200         {
201                 {1, 65, 77, 0, 0, 0, 0, 0, 0, 0}, /* In */
202                 {2, 64, 66, 76, 78, 0, 0, 0, 0, 0} /* NotIn */
203         },
204         {
205                 {1, 2, 30, 64, 65, 77, 80, 81, 222222, 0}, /* In */
206                 {66, 76, 78, 79, 0, 0, 0, 0, 0, 0} /* NotIn */
207         },
208         {
209                 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* In */
210                 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} /* NotIn */
211         }
212 };
213
214
215
216 static void TestMSetHashlist (void)
217 {
218         int nTest = 0;
219         int j;
220         StrBuf *MSetStr;
221         StrBuf *Assert;
222         MSet *OneMSet;
223         
224
225         MSetStr = NewStrBuf();
226         Assert = NewStrBuf();
227         while (MSetStrings[nTest] != NULL)
228         {
229                 StrBufPlain(MSetStr, MSetStrings[nTest], -1);
230                 ParseMSet(&OneMSet, MSetStr);
231
232 //#ifdef VERBOSE_TEST
233                 printf("---%s---\n", ChrPtr(MSetStr));
234                 {
235                         const char *HashKey;
236                         long HKLen;
237                         HashList *ScanMe = (HashList*) OneMSet;
238                         HashPos *at;
239                         void *vMsg;
240                         long *end;
241
242                         at = GetNewHashPos(ScanMe, 0);
243                         while (GetNextHashPos(ScanMe, at, &HKLen, &HashKey, &vMsg)) {
244                                 /* Are you a new message, or an old message? */
245                                 end = (long*) vMsg;
246                                 printf("[%ld][%ld]\n", *(long*)HashKey, *end);
247                         }
248                         DeleteHashPos(&at);
249                 }
250 //#endif
251
252                 j = 0;
253                 while (MessageNumbers[nTest][InMSet][j] != 0)
254                 {
255                         if (!IsInMSetList(OneMSet, MessageNumbers[nTest][InMSet][j]))
256                         {
257                                 StrBufPrintf(Assert, "InFail: %s <-> %ld\n", 
258                                              ChrPtr(MSetStr), 
259                                              MessageNumbers[nTest][InMSet][j]);
260                                 CU_FAIL(ChrPtr(Assert));
261                                 printf(ChrPtr(Assert));
262                         }
263                         else
264                         {
265                                 StrBufPrintf(Assert, "InPass: %s <-> %ld\n", 
266                                              ChrPtr(MSetStr), 
267                                              MessageNumbers[nTest][InMSet][j]);
268                                 CU_PASS(ChrPtr(Assert));
269                         }
270                         j++;    
271                 }
272                 j = 0;
273                 while (MessageNumbers[nTest][NotInMSet][j] != 0)
274                 {
275                         if (IsInMSetList(OneMSet, MessageNumbers[nTest][NotInMSet][j]))
276                         {
277                                 StrBufPrintf(Assert, "NOT-InFail: %s <-> %ld\n", 
278                                              ChrPtr(MSetStr), 
279                                              MessageNumbers[nTest][NotInMSet][j]);
280                                 CU_FAIL(ChrPtr(Assert));
281                                 printf(ChrPtr(Assert));
282                         }
283                         else
284                         {
285                                 StrBufPrintf(Assert, "NOT-InPass: %s <-> %ld\n", 
286                                              ChrPtr(MSetStr), 
287                                              MessageNumbers[nTest][InMSet][j]);
288                                 CU_PASS(ChrPtr(Assert));
289                         }
290                         j++;
291                 }
292                 
293
294                 DeleteMSet(&OneMSet);
295                 nTest++;
296                 
297         }
298         FreeStrBuf(&MSetStr);
299         FreeStrBuf(&Assert);
300 }
301
302
303
304 static void AddHashlistTests(void)
305 {
306         CU_pSuite pGroup = NULL;
307         CU_pTest pTest = NULL;
308
309         pGroup = CU_add_suite("TestStringBufSimpleAppenders", NULL, NULL);
310         pTest = CU_add_test(pGroup, "TestHashListIteratorForward", TestHashlistIteratorForward);
311         pTest = CU_add_test(pGroup, "TestHashlistAddDelete", TestHashlistAddDelete);
312         pTest = CU_add_test(pGroup, "TestMSetHashlist", TestMSetHashlist);
313 }
314
315
316 int main(int argc, char* argv[])
317 {
318         setvbuf(stdout, NULL, _IONBF, 0);
319
320         StartLibCitadel(8);
321         CU_BOOL Run = CU_FALSE ;
322         
323         CU_set_output_filename("TestAutomated");
324         if (CU_initialize_registry()) {
325                 printf("\nInitialize of test Registry failed.");
326         }
327         
328         Run = CU_TRUE ;
329         AddHashlistTests();
330         
331         if (CU_TRUE == Run) {
332                 //CU_console_run_tests();
333     printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
334     
335     ///CU_automated_run_tests();
336         }
337         
338         CU_cleanup_registry();
339
340         return 0;
341 }