2 ** Copyright (C) 2002 by Kevin L. Mitchell <klmitch@mit.edu>
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.
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.
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,
26 #define DEADINT 0xdeadbeef
27 #define DEADPTR (void *)0xdeadbeef
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)
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);
37 printf("PASS/%s_magic:Initialization set magic number properly\n", how);
39 if (table->ht_flags != flags) /* verify flags were set */
40 printf("FAIL/%s_flags:Initialization failed to set flags\n", how);
42 printf("PASS/%s_flags:Initialization set flags properly\n", how);
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);
48 printf("PASS/%s_modulus:Initialization set modulus to %ld\n", how, mod);
50 if (table->ht_count != 0) /* verify count was set */
51 printf("FAIL/%s_count:Initialization failed to clear count\n", how);
53 printf("PASS/%s_count:Initialization set count to 0\n", how);
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);
59 printf("PASS/%s_rollover:Initialization set rollover to %ld\n", how, over);
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);
65 printf("PASS/%s_rollunder:Initialization set rollunder to %ld\n", how,
68 if (table->ht_func != func) /* verify func was set */
69 printf("FAIL/%s_func:Initialization failed to set func\n", how);
71 printf("PASS/%s_func:Initialization set func properly\n", how);
73 if (table->ht_comp != comp) /* verify comp was set */
74 printf("FAIL/%s_comp:Initialization failed to set comp\n", how);
76 printf("PASS/%s_comp:Initialization set comp properly\n", how);
78 if (table->ht_resize != 0) /* verify resize was set */
79 printf("FAIL/%s_rsize:Initialization failed to set resize\n", how);
81 printf("PASS/%s_rsize:Initialization set resize properly\n", how);
83 if (table->ht_extra != extra) /* verify extra was set */
84 printf("FAIL/%s_extra:Initialization failed to set extra\n", how);
86 printf("PASS/%s_extra:Initialization set extra properly\n", how);
89 if (table->ht_table != 0) /* verify that table was not allocated */
90 printf("FAIL/%s_table:Initialization failed to clear table\n", how);
92 printf("PASS/%s_table:Initialization set table to 0\n", how);
96 if (table->ht_table == 0) /* verify that table was not allocated */
97 printf("FAIL/%s_table:Initialization failed to set table\n", how);
99 printf("PASS/%s_table:Initialization set table properly\n", how);
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);
107 printf("PASS/%s_bucket%d:Initialization initialized bucket "
108 "%d properly\n", how, i, i);
112 /* Check return value of operation and report PASS/FAIL */
114 check_result(unsigned long result, unsigned long expected, char *test,
117 if (result != expected) {
118 printf("FAIL/%s:%s incorrectly returned %lu (expected %lu)\n", test, info,
123 printf("PASS/%s:%s correctly returned %lu\n", test, info, result);
126 /* Scramble the table */
128 scramble(hash_table_t *table)
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;
143 check_func(hash_table_t *table, db_key_t *key)
149 check_comp(hash_table_t *table, db_key_t *key1, db_key_t *key2)
155 main(int argc, char **argv)
158 hash_table_t table = HASH_TABLE_INIT(0x80010000 | HASH_FLAG_AUTOGROW,
159 check_func, check_comp, 0, 0);
161 /* Check that the static initializer produces a passable structure */
162 check_init(&table, HASH_FLAG_AUTOGROW, 0, 0, 0, check_func, check_comp,
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);
175 /* Scramble the structure */
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");
185 /* Scramble the structure again */
188 /* Now try to initialize our structure with a non-0 mod and see what
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");