Accept topic changes from servers that do not send topic-set timestamps (fixes SF...
[ircu2.10.12-pk.git] / libs / dbprim / tests / t_ht_init.c
1 /*
2 ** Copyright (C) 2002 by Kevin L. Mitchell <klmitch@mit.edu>
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Library General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 ** Library General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Library General Public
15 ** License along with this library; if not, write to the Free
16 ** Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
17 ** MA 02111-1307, USA
18 **
19 ** @(#)$Id$
20 */
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #include "dbprim.h"
25
26 #define DEADINT 0xdeadbeef
27 #define DEADPTR (void *)0xdeadbeef
28
29 static void
30 check_init(hash_table_t *table, unsigned long flags, unsigned long mod,
31            unsigned long over, unsigned long under, hash_func_t func,
32            hash_comp_t comp, void *extra, char *how)
33 {
34   if (table->ht_magic != HASH_TABLE_MAGIC) /* Verify magic was set */
35     printf("FAIL/%s_magic:Initialization failed to set magic number\n", how);
36   else
37     printf("PASS/%s_magic:Initialization set magic number properly\n", how);
38
39   if (table->ht_flags != flags) /* verify flags were set */
40     printf("FAIL/%s_flags:Initialization failed to set flags\n", how);
41   else
42     printf("PASS/%s_flags:Initialization set flags properly\n", how);
43
44   if (table->ht_modulus != mod) /* verify modulus was set */
45     printf("FAIL/%s_modulus:Initialization failed to set modulus to %ld "
46            "(%ld instead)\n", how, mod, table->ht_modulus);
47   else
48     printf("PASS/%s_modulus:Initialization set modulus to %ld\n", how, mod);
49
50   if (table->ht_count != 0) /* verify count was set */
51     printf("FAIL/%s_count:Initialization failed to clear count\n", how);
52   else
53     printf("PASS/%s_count:Initialization set count to 0\n", how);
54
55   if (table->ht_rollover != over) /* verify rollover was set */
56     printf("FAIL/%s_rollover:Initialization failed to set rollover to %ld "
57            "(%ld instead)\n", how, over, table->ht_rollover);
58   else
59     printf("PASS/%s_rollover:Initialization set rollover to %ld\n", how, over);
60
61   if (table->ht_rollunder != under) /* verify rollunder was set */
62     printf("FAIL/%s_rollunder:Initialization failed to set rollunder to %ld "
63            "(%ld instead)\n", how, under, table->ht_rollunder);
64   else
65     printf("PASS/%s_rollunder:Initialization set rollunder to %ld\n", how,
66            under);
67
68   if (table->ht_func != func) /* verify func was set */
69     printf("FAIL/%s_func:Initialization failed to set func\n", how);
70   else
71     printf("PASS/%s_func:Initialization set func properly\n", how);
72
73   if (table->ht_comp != comp) /* verify comp was set */
74     printf("FAIL/%s_comp:Initialization failed to set comp\n", how);
75   else
76     printf("PASS/%s_comp:Initialization set comp properly\n", how);
77
78   if (table->ht_resize != 0) /* verify resize was set */
79     printf("FAIL/%s_rsize:Initialization failed to set resize\n", how);
80   else
81     printf("PASS/%s_rsize:Initialization set resize properly\n", how);
82
83   if (table->ht_extra != extra) /* verify extra was set */
84     printf("FAIL/%s_extra:Initialization failed to set extra\n", how);
85   else
86     printf("PASS/%s_extra:Initialization set extra properly\n", how);
87
88   if (mod == 0) {
89     if (table->ht_table != 0) /* verify that table was not allocated */
90       printf("FAIL/%s_table:Initialization failed to clear table\n", how);
91     else
92       printf("PASS/%s_table:Initialization set table to 0\n", how);
93   } else {
94     int i;
95
96     if (table->ht_table == 0) /* verify that table was not allocated */
97       printf("FAIL/%s_table:Initialization failed to set table\n", how);
98     else
99       printf("PASS/%s_table:Initialization set table properly\n", how);
100
101     for (i = 0; i < mod; i++) /* verify buckets initialized */
102       if (table->ht_table == 0 || i >= table->ht_modulus ||
103           !ll_verify(&table->ht_table[i]))
104         printf("FAIL/%s_bucket%d:Initialization failed to initialize "
105                "bucket %d\n", how, i, i);
106       else
107         printf("PASS/%s_bucket%d:Initialization initialized bucket "
108                "%d properly\n", how, i, i);
109   }
110 }
111
112 /* Check return value of operation and report PASS/FAIL */
113 static void
114 check_result(unsigned long result, unsigned long expected, char *test,
115              char *info, int die)
116 {
117   if (result != expected) {
118     printf("FAIL/%s:%s incorrectly returned %lu (expected %lu)\n", test, info,
119            result, expected);
120     if (die)
121       exit(0);
122   } else
123     printf("PASS/%s:%s correctly returned %lu\n", test, info, result);
124 }
125
126 /* Scramble the table */
127 static void
128 scramble(hash_table_t *table)
129 {
130   table->ht_magic = DEADINT;
131   table->ht_flags = DEADINT;
132   table->ht_modulus = DEADINT;
133   table->ht_count = DEADINT;
134   table->ht_rollover = DEADINT;
135   table->ht_rollunder = DEADINT;
136   table->ht_table = DEADPTR;
137   table->ht_func = (hash_func_t)DEADPTR;
138   table->ht_comp = (hash_comp_t)DEADPTR;
139   table->ht_extra = DEADPTR;
140 }
141
142 static unsigned long
143 check_func(hash_table_t *table, db_key_t *key)
144 {
145   return 0;
146 }
147
148 static unsigned long
149 check_comp(hash_table_t *table, db_key_t *key1, db_key_t *key2)
150 {
151   return 0;
152 }
153
154 int
155 main(int argc, char **argv)
156 {
157   unsigned long mod;
158   hash_table_t table = HASH_TABLE_INIT(0x80010000 | HASH_FLAG_AUTOGROW,
159                                        check_func, check_comp, 0, 0);
160
161   /* Check that the static initializer produces a passable structure */
162   check_init(&table, HASH_FLAG_AUTOGROW, 0, 0, 0, check_func, check_comp,
163              0, "ht_static");
164
165   /* now, check what ht_init does with bad arguments */
166   check_result(ht_init(0, 0, 0, 0, 0, 0, 0), DB_ERR_BADARGS, "ht_init_noargs",
167                "ht_init() with no valid arguments", 0);
168   check_result(ht_init(0, 0, check_func, check_comp, 0, 0, 0), DB_ERR_BADARGS,
169                "ht_init_notable", "ht_init() with no table", 0);
170   check_result(ht_init(&table, 0, 0, check_comp, 0, 0, 0), DB_ERR_BADARGS,
171                "ht_init_nofunc", "ht_init() with no hash function", 0);
172   check_result(ht_init(&table, 0, check_func, 0, 0, 0, 0), DB_ERR_BADARGS,
173                "ht_init_nocomp", "ht_init() with no comparison function", 0);
174
175   /* Scramble the structure */
176   scramble(&table);
177
178   /* Now try to initialize our structure with a 0 mod and see what happens */
179   check_result(ht_init(&table, 0x80010000 | HASH_FLAG_AUTOGROW, check_func,
180                        check_comp, 0, 0, 0), 0, "ht_dynamic_nomod",
181                "ht_init() with zero modulus", 0);
182   check_init(&table, HASH_FLAG_AUTOGROW, 0, 0, 0, check_func, check_comp,
183              0, "ht_dynamic_nomod");
184
185   /* Scramble the structure again */
186   scramble(&table);
187
188   /* Now try to initialize our structure with a non-0 mod and see what
189    * happens
190    */
191   check_result(ht_init(&table, 0x80010000 | HASH_FLAG_AUTOGROW, check_func,
192                        check_comp, 0, 0, 6), 0, "ht_dynamic_mod6",
193                "ht_init() with non-zero modulus", 0);
194   mod = 7; /* next prime after 6 */
195   check_init(&table, HASH_FLAG_AUTOGROW, mod, (mod * 4) / 3, (mod * 3) / 4,
196              check_func, check_comp, 0, "ht_dynamic_mod6");
197
198   return 0;
199 }