* fix hashlist iterator
[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
67 }
68
69 static void TestHashlistIteratorForward (void)
70 {
71         HashList *H;
72
73         H = GetFilledHash (10, 1);
74
75         test_iterate_hash(H, 1, 1);
76         printf("\n");
77
78         test_iterate_hash(H, 0, 1);
79         printf("\n");
80
81         test_iterate_hash(H, 1, 2);
82         printf("\n");
83
84         test_iterate_hash(H, 0, 2);
85         printf("\n");
86
87         test_iterate_hash(H, 1, 3);
88         printf("\n");
89
90         test_iterate_hash(H, 0, 3);
91         printf("\n");
92
93         DeleteHash(&H);
94 }
95 /*
96 Some samples from the original...
97         CU_ASSERT_EQUAL(10, 10);
98         CU_ASSERT_EQUAL(0, -0);
99         CU_ASSERT_EQUAL(-12, -12);
100         CU_ASSERT_NOT_EQUAL(10, 11);
101         CU_ASSERT_NOT_EQUAL(0, -1);
102         CU_ASSERT_NOT_EQUAL(-12, -11);
103         CU_ASSERT_PTR_EQUAL((void*)0x100, (void*)0x100);
104         CU_ASSERT_PTR_NOT_EQUAL((void*)0x100, (void*)0x101);
105         CU_ASSERT_PTR_NULL(NULL);
106         CU_ASSERT_PTR_NULL(0x0);
107         CU_ASSERT_PTR_NOT_NULL((void*)0x23);
108         CU_ASSERT_STRING_EQUAL(str1, str2);
109         CU_ASSERT_STRING_NOT_EQUAL(str1, str2);
110         CU_ASSERT_NSTRING_EQUAL(str1, str2, strlen(str1));
111         CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1));
112         CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1) + 1);
113         CU_ASSERT_NSTRING_NOT_EQUAL(str1, str2, 3);
114         CU_ASSERT_NSTRING_NOT_EQUAL(str1, str3, strlen(str1) + 1);
115         CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, 0.0001);
116         CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, -0.0001);
117         CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, 0.0001);
118         CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, -0.0001);
119         CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, 0.0001);
120         CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, -0.0001);
121         CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, 0.0001);
122         CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, -0.0001);
123 */
124
125
126
127
128
129 static void AddHashlistTests(void)
130 {
131         CU_pSuite pGroup = NULL;
132         CU_pTest pTest = NULL;
133
134         pGroup = CU_add_suite("TestStringBufSimpleAppenders", NULL, NULL);
135         pTest = CU_add_test(pGroup, "TestHashListIteratorForward", TestHashlistIteratorForward);
136
137 }
138
139
140 int main(int argc, char* argv[])
141 {
142         setvbuf(stdout, NULL, _IONBF, 0);
143
144         StartLibCitadel(8);
145         CU_BOOL Run = CU_FALSE ;
146         
147         CU_set_output_filename("TestAutomated");
148         if (CU_initialize_registry()) {
149                 printf("\nInitialize of test Registry failed.");
150         }
151         
152         Run = CU_TRUE ;
153         AddHashlistTests();
154         
155         if (CU_TRUE == Run) {
156                 //CU_console_run_tests();
157     printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
158     
159     ///CU_automated_run_tests();
160         }
161         
162         CU_cleanup_registry();
163
164         return 0;
165 }