3 #include "silcincludes.h"
5 typedef struct entry_struct {
11 bool auto_rehash = TRUE;
13 SilcHashTable t = NULL;
15 SilcUInt32 hash_entry(void *key, void *user_context)
18 return e->val + silc_hash_string(e->name, NULL);
21 bool hash_compare(void *key1, void *key2, void *user_context)
25 if (e->val == e2->val && !strcmp(e->name, e2->name))
30 void hash_destructor(void *key, void *context, void *user_context)
35 SILC_LOG_DEBUG(("e=%p, e->val=%d, e->name=%s, context=%s",
36 e, e->val, e->name, name));
37 memset(e, 'F', sizeof(*e));
46 SILC_LOG_DEBUG(("Adding %d entries", count));
48 for (i = 0; i < count; i++) {
49 e = silc_calloc(1, sizeof(*e));
52 snprintf(e->name, sizeof(e->name), "%d", i);
55 silc_hash_table_add(t, (void *)e, (void *)e->name);
58 SILC_LOG_DEBUG(("Hash table entry count: %d", silc_hash_table_count(t)));
63 bool del_entries_with_list()
65 SilcHashTableList htl;
69 SILC_LOG_DEBUG(("Deleting entries with SilcHashTableList"));
71 silc_hash_table_list(t, &htl);
72 while (silc_hash_table_get(&htl, (void **)&e, (void **)&name)) {
73 if (!silc_hash_table_del(t, e))
76 silc_hash_table_list_reset(&htl);
78 SILC_LOG_DEBUG(("Hash table entry count: %d", silc_hash_table_count(t)));
83 void del_foreach(void *key, void *context, void *user_context)
88 SILC_LOG_DEBUG(("del_foreach found e=%p, e->val=%d, e->name=%s, context=%s",
89 e, e->val, e->name, name));
90 silc_hash_table_del(t, key);
93 bool del_n_entries_foreach()
95 struct entry_struct f;
98 SILC_LOG_DEBUG(("Deleting keys 0-%d with foreach", count));
100 for (i = 0; i < count; i++) {
101 memset(&f, 0, sizeof(f));
102 snprintf(f.name, sizeof(f.name), "%d", i);
105 silc_hash_table_find_foreach(t, &f, del_foreach, NULL);
111 bool del_entries_foreach()
113 SILC_LOG_DEBUG(("Deleting all entries with foreach"));
114 silc_hash_table_foreach(t, del_foreach, NULL);
120 SILC_LOG_DEBUG(("Allocating hash table with %d entries (%s)",
121 count, auto_rehash ? "auto rehash" : "no auto rehash"));
123 t = silc_hash_table_alloc(0, hash_entry, NULL,
125 hash_destructor, NULL, auto_rehash);
130 SILC_LOG_DEBUG(("Hash table size: %d", silc_hash_table_size(t)));
132 if (silc_hash_table_count(t) != count) {
133 SILC_LOG_DEBUG(("Wrong table count %d", count));
140 bool delete_table_with_list()
143 SILC_LOG_DEBUG(("Deleting entries with SilcHashTableList"));
145 if (!del_entries_with_list())
148 SILC_LOG_DEBUG(("Hash table size: %d", silc_hash_table_size(t)));
150 if (silc_hash_table_count(t) != 0) {
151 SILC_LOG_DEBUG(("Wrong table count %d != 0", count));
155 silc_hash_table_free(t);
163 struct entry_struct f;
168 SILC_LOG_DEBUG(("Finding %d entries", count));
170 for (i = 0; i < count; i++) {
171 memset(&f, 0, sizeof(f));
172 snprintf(f.name, sizeof(f.name), "%d", i);
176 if (!silc_hash_table_find(t, &f, (void **)&e, (void **)&name))
179 /* Find itself with context */
180 if (!silc_hash_table_find_by_context(t, e, e->name, NULL))
189 SilcHashTableList htl;
192 bool dumpped = FALSE;
194 SILC_LOG_DEBUG(("Dumping hash table entries"));
196 silc_hash_table_list(t, &htl);
197 while (silc_hash_table_get(&htl, (void **)&e, (void **)&name)) {
200 SILC_LOG_DEBUG(("e=%p, e->val=%d, e->name=%s, context=%s",
201 e, e->val, e->name, name));
203 silc_hash_table_list_reset(&htl);
208 int main(int argc, char **argv)
210 bool success = FALSE;
213 if (argc > 1 && !strcmp(argv[1], "-d")) {
214 silc_log_debug(TRUE);
215 silc_log_debug_hexdump(TRUE);
216 silc_log_quick(TRUE);
217 silc_log_set_debug_string("*table*");
220 if (argc > 1 && !strcmp(argv[1], "-D")) {
221 silc_log_debug(TRUE);
223 silc_log_set_debug_string("*table*");
230 if (!delete_table_with_list())
239 SILC_LOG_DEBUG(("rehash"));
240 silc_hash_table_rehash(t, 0);
241 SILC_LOG_DEBUG(("Hash table entry count: %d", silc_hash_table_count(t)));
242 SILC_LOG_DEBUG(("Hash table size: %d", silc_hash_table_size(t)));
243 if (!delete_table_with_list())
253 SILC_LOG_DEBUG(("rehash"));
254 silc_hash_table_rehash(t, 0);
255 SILC_LOG_DEBUG(("Hash table entry count: %d", silc_hash_table_count(t)));
256 SILC_LOG_DEBUG(("Hash table size: %d", silc_hash_table_size(t)));
257 if (!del_entries_with_list())
259 SILC_LOG_DEBUG(("rehash"));
260 silc_hash_table_rehash(t, 0);
261 SILC_LOG_DEBUG(("Hash table entry count: %d", silc_hash_table_count(t)));
262 SILC_LOG_DEBUG(("Hash table size: %d", silc_hash_table_size(t)));
266 /* Adding duplicates */
267 for (i = 0; i < 30; i++) {
276 if (!del_n_entries_foreach())
286 if (!del_entries_foreach())
288 if (!delete_table_with_list())
294 SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
295 fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");