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