Debian Codegraph @

dhash/examples/dhash_test.c

raw symbols SCIP
1 /*
2 Authors:
3 John Dennis <jdennis.redhat.com>
4
5 Copyright (C) 2009 Red Hat
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <stdio.h>
22 #include <assert.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <time.h>
26 #include <getopt.h>
27 #include "dhash.h"
28
29 #define BUF_SIZE 1024
30 #define DEFAULT_MAX_TEST (500)
31 hash_entry_t *iter_result_1 = NULL;
32 hash_entry_t *iter_result_2 = NULL;
33
34 unsigned long max_test = DEFAULT_MAX_TEST;
35 int verbose = 0;
36
37 static const char *error_string(int error)
38 {
39 const char *str;
40
41 if (IS_HASH_ERROR(error))
42 return hash_error_string(error);
43
44 if (error < 0) {
45 return "Negative error codes are not supported.";
46 }
47
48 str = strerror(error);
49 if (str == NULL) {
50 return "strerror() returned NULL.";
51 }
52
53 return str;
54 }
55
56 static char *key_string(hash_key_t *key)
57 {
58 static char buf[BUF_SIZE];
59
60 switch(key->type) {
61 case HASH_KEY_ULONG:
62 snprintf(buf, sizeof(buf), "key ulong = %lu", key->ul);
63 break;
64 case HASH_KEY_STRING:
65 snprintf(buf, sizeof(buf), "key string = \"%s\"", key->str);
66 break;
67 case HASH_KEY_CONST_STRING:
68 snprintf(buf, sizeof(buf), "key string = \"%s\"", key->c_str);
69 break;
70 default:
71 snprintf(buf, sizeof(buf), "unknown key type = %d", key->type);
72 break;
73 }
74 return buf;
75 }
76
77 static char *value_string(hash_value_t *value)
78 {
79 static char buf[BUF_SIZE];
80
81 switch(value->type) {
82 case HASH_VALUE_UNDEF:
83 snprintf(buf, sizeof(buf), "value undefined");
84 break;
85 case HASH_VALUE_PTR:
86 snprintf(buf, sizeof(buf), "value str = \"%s\"", (char *)value->ptr);
87 break;
88 case HASH_VALUE_INT:
89 snprintf(buf, sizeof(buf), "value int = %d", value->i);
90 break;
91 case HASH_VALUE_UINT:
92 snprintf(buf, sizeof(buf), "value unsigned int = %u", value->ui);
93 break;
94 case HASH_VALUE_LONG:
95 snprintf(buf, sizeof(buf), "value long = %ld", value->l);
96 break;
97 case HASH_VALUE_ULONG:
98 snprintf(buf, sizeof(buf), "value unsigned long = %lu", value->ul);
99 break;
100 case HASH_VALUE_FLOAT:
101 snprintf(buf, sizeof(buf), "value float = %f", value->f);
102 break;
103 case HASH_VALUE_DOUBLE:
104 snprintf(buf, sizeof(buf), "value double = %f", value->f);
105 break;
106 default:
107 snprintf(buf, sizeof(buf), "unknown value type = %d", value->type);
108 break;
109 }
110
111 return buf;
112 }
113
114 static char *entry_string(hash_entry_t *entry)
115 {
116 static char buf[BUF_SIZE];
117
118 snprintf(buf, sizeof(buf), "[%s] = [%s]", key_string(&entry->key), value_string(&entry->value));
119
120 return buf;
121
122 }
123
124 static bool callback(hash_entry_t *item, void *user_data)
125 {
126 unsigned long *callback_count = (unsigned long *)user_data;
127
128 iter_result_1[*callback_count] = *item;
129
130 (*callback_count)++;
131
132 if (verbose) printf("%s\n", entry_string(item));
133 return true;
134 }
135
136 static void delete_callback(hash_entry_t *item, hash_destroy_enum type,
137 void *pvt)
138 {
139 if (item->value.type == HASH_VALUE_PTR) free(item->value.ptr);
140 }
141
142 typedef struct test_val_t {
143 long val;
144 char *str;
145 } test_val_t;
146
147
148 int main(int argc, char **argv)
149 {
150 test_val_t *test = NULL;
151 long i, j, k;
152 bool duplicate;
153 long val;
154 int status;
155 hash_value_t value;
156 hash_value_t old_value;
157 hash_value_t new_value;
158 hash_key_t key;
159 char buf[BUF_SIZE];
160 hash_table_t *table = NULL;
161 unsigned long callback_count = 0;
162 unsigned long table_size = 0;
163 unsigned int seed = time(0);
164 unsigned int directory_bits = 0;
165 unsigned int segment_bits = 0;
166 unsigned long min_load_factor = HASH_DEFAULT_MIN_LOAD_FACTOR;
167 unsigned long max_load_factor = HASH_DEFAULT_MAX_LOAD_FACTOR;
168
169 while (1) {
170 int arg;
171 int option_index = 0;
172 static struct option long_options[] = {
173 {"count", 1, 0, 'c'},
174 {"verbose", 1, 0, 'v'},
175 {"quiet", 1, 0, 'q'},
176 {"table-size", 1, 0, 't'},
177 {"directory-bits", 1, 0, 'd'},
178 {"segment-bits", 1, 0, 's'},
179 {"min-load-factor", 1, 0, 'l'},
180 {"max-load-factor", 1, 0, 'h'},
181 {"seed", 1, 0, 'r'},
182 {0, 0, 0, 0}
183 };
184
185 arg = getopt_long(argc, argv, "c:vqt:d:s:l:h:r:",
186 long_options, &option_index);
187 if (arg == -1) break;
188
189 switch (arg) {
190 case 'c':
191 max_test = strtoul(optarg, NULL, 0);
192 break;
193 case 'v':
194 verbose = 1;
195 break;
196 case 'q':
197 verbose = 0;
198 break;
199 case 't':
200 table_size = strtoul(optarg, NULL, 0);
201 break;
202 case 'd':
203 directory_bits = atoi(optarg);
204 break;
205 case 's':
206 segment_bits = atoi(optarg);
207 break;
208 case 'l':
209 min_load_factor = strtoul(optarg, NULL, 0);
210 break;
211 case 'h':
212 max_load_factor = strtoul(optarg, NULL, 0);
213 break;
214 case 'r':
215 seed = strtoul(optarg, NULL, 0);
216 break;
217 }
218 }
219
220 if ((test = (test_val_t *) calloc(max_test, sizeof(test_val_t))) == NULL) {
221 fprintf(stderr, "Failed to allocate test array\n");
222 exit(1);
223 }
224 if ((iter_result_1 = (hash_entry_t *) calloc(max_test, sizeof(hash_entry_t))) == NULL) {
225 fprintf(stderr, "Failed to allocate iter_result_1 array\n");
226 exit(1);
227 }
228 if ((iter_result_2 = (hash_entry_t *) calloc(max_test, sizeof(hash_entry_t))) == NULL) {
229 fprintf(stderr, "Failed to allocate iter_result_2 array\n");
230 exit(1);
231 }
232
233
234 /* Initialize the random number generator */
235 srandom(seed);
236 printf("random seed: %#x\n", seed);
237
238 /* Create the hash table as small as possible to exercise growth */
239 if ((status = hash_create_ex(table_size, &table,
240 directory_bits, segment_bits,
241 min_load_factor, max_load_factor,
242 NULL, NULL, NULL,
243 delete_callback, NULL)) != HASH_SUCCESS) {
244 fprintf(stderr, "table creation failed at line %d (%s)\n", __LINE__, error_string(status));
245 exit(1);
246 }
247
248 /* Initialize the array of test values */
249 for (i = 0; i < max_test; i++) {
250 /* Get random value, make sure it's unique */
251 duplicate = true;
252 while (duplicate) {
253 duplicate = false;
254 val = random();
255 for (j = 0; j < i; j++) {
256 if (test[j].val == val) {
257 duplicate = true;
258 break;
259 }
260 }
261 }
262 test[i].val = val;
263 /* If the value is odd we'll use a string as the key,
264 * otherwise we'll use an unsigned long as the key */
265 if (test[i].val & 1) {
266 key.type = HASH_KEY_STRING;
267 snprintf(buf, BUF_SIZE, "%ld", test[i].val);
268 test[i].str = strdup(buf);
269 }
270 }
271
272 printf("Completed building test values, beginning test ...\n");
273
274 /* Enter all the test values into the hash table */
275 for (i = 0; i < max_test; i++) {
276 if (test[i].val & 1) {
277 key.type = HASH_KEY_STRING;
278 key.str = test[i].str;
279 value.type = HASH_VALUE_PTR;
280 value.ptr = (void *) strdup(test[i].str);
281 }
282 else {
283 key.type = HASH_KEY_ULONG;
284 key.ul = test[i].val;
285 value.type = HASH_VALUE_LONG;
286 value.l = test[i].val;
287 }
288
289 if (hash_has_key(table, &key)) {
290 fprintf(stderr, "Error: %ld already in table when inserting, i = %lu, at line %d\n",
291 test[i].val, i, __LINE__);
292 exit(1);
293 }
294 if ((status = hash_enter(table, &key, &value)) != HASH_SUCCESS) {
295 fprintf(stderr, "Error: %ld failed insertion at line %d (%s) \n",
296 test[i].val, __LINE__, error_string(status));
297 exit(1);
298 }
299 }
300
301 /* Now visit each entry in the table using a callback iterator,
302 * store what we found in iter_result_1 for testing the iterator object later on */
303 if (verbose) printf("callback iterate:\n");
304 callback_count = 0;
305 if ((status = hash_iterate(table, callback, &callback_count)) != HASH_SUCCESS) {
306 fprintf(stderr, "hash_iterate failed at line %d (%s)\n", __LINE__, error_string(status));
307 exit(1);
308 }
309 if (verbose) printf("hash_count=%ld, callback_count=%ld\n", hash_count(table), callback_count);
310
311 if (hash_count(table) != callback_count) {
312 fprintf(stderr, "Error: hash_count(%ld) != callback_count(%ld) at line %d\n",
313 hash_count(table), callback_count, __LINE__);
314 exit(1);
315 }
316
317 /* Now vist each entry in the table using an iterator object */
318 {
319 struct hash_iter_context_t *iter;
320 unsigned long n_items;
321 hash_entry_t *entry;
322
323 if (verbose) printf("iter iterate:\n");
324
325 n_items = 0;
326 iter = new_hash_iter_context(table);
327
328 while ((entry = iter->next(iter)) != NULL) {
329 if (verbose) printf("%s\n", entry_string(entry));
330 iter_result_2[n_items] = *entry;
331 n_items++;
332 }
333 if (verbose) printf("hash_count=%ld, n_items=%ld\n", hash_count(table), n_items);
334
335 if (hash_count(table) != n_items) {
336 fprintf(stderr, "Error: hash_count(%ld) != n_items(%ld) at line %d\n",
337 hash_count(table), n_items, __LINE__);
338 exit(1);
339 }
340 free(iter);
341 }
342
343 /* Both iterators should have visited each item in the same order, verify ... */
344 for (i = 0; i < max_test; i++) {
345 if (memcmp(&iter_result_1[i], &iter_result_2[i], sizeof(iter_result_1[0])) != 0) {
346 fprintf(stderr, "Error: iter_result_1[%lu] != iter_result_2[%lu] at line %d\n",
347 i, i, __LINE__);
348 exit(1);
349 }
350 }
351
352 /* Get an array of keys in the table, print them out */
353 {
354 unsigned long count;
355 hash_key_t *keys;
356
357 if (verbose) printf("\nhash_keys:\n");
358 if ((status = hash_keys(table, &count, &keys)) != HASH_SUCCESS) {
359 fprintf(stderr, "hash_keys failed at line %d (%s)\n",
360 __LINE__, error_string(status));
361 exit(1);
362 }
363
364 if (hash_count(table) != count) {
365 fprintf(stderr, "Error: hash_count(%ld) != hash_keys() count(%ld) at line %d\n",
366 hash_count(table), count, __LINE__);
367 exit(1);
368 }
369
370 for (i = 0; i < count; i++) {
371 if (verbose) printf("%s\n", key_string(&keys[i]));
372 }
373 free(keys);
374 }
375
376 /* Get an array of values in the table, print them out */
377 {
378 unsigned long count;
379 hash_value_t *values;
380
381 if (verbose) printf("\nhash_values:\n");
382 hash_values(table, &count, &values);
383
384 if (hash_count(table) != count) {
385 fprintf(stderr, "Error: hash_count(%ld) != hash_values() count(%ld) at line %d\n",
386 hash_count(table), count, __LINE__);
387 exit(1);
388 }
389
390 for (i = 0; i < count; i++) {
391 if (verbose) printf("%s\n", value_string(&values[i]));
392 }
393 free(values);
394 }
395
396 /* Get an array of items in the table, print them out */
397 {
398 unsigned long count;
399 hash_entry_t *entries;
400
401 if (verbose) printf("\nhash_entries:\n");
402 hash_entries(table, &count, &entries);
403
404 if (hash_count(table) != count) {
405 fprintf(stderr, "Error: hash_count(%ld) != hash_entries() count(%ld) at line %d\n",
406 hash_count(table), count, __LINE__);
407 exit(1);
408 }
409
410 for (i = 0; i < count; i++) {
411 if (verbose) printf("%s\n", entry_string(&entries[i]));
412 }
413 free(entries);
414 }
415
416 /* See if we can find every key */
417 for (i = max_test - 1; i >= 0; i--) {
418 if (test[i].val & 1) {
419 key.type = HASH_KEY_STRING;
420 key.str = test[i].str;
421 }
422 else {
423 key.type = HASH_KEY_ULONG;
424 key.ul = test[i].val;
425 }
426 if ((status = hash_lookup(table, &key, &value)) != HASH_SUCCESS) {
427 fprintf(stderr, "Error: failed first lookup for value %ld at index %ld at line %d (%s)\n",
428 test[i].val, i, __LINE__, error_string(status));
429 exit(1);
430 }
431 else {
432 switch(value.type) {
433 case HASH_VALUE_PTR:
434 if (strcmp((char *)value.ptr, test[i].str) != 0) {
435 fprintf(stderr, "Error: corrupt ptr data for %lu at line %d\n", i, __LINE__);
436 exit(1);
437 }
438 break;
439 case HASH_VALUE_LONG:
440 if (value.l != test[i].val) {
441 fprintf(stderr, "Error: corrupt long data for %lu at line %d\n", i, __LINE__);
442 exit(1);
443 }
444 break;
445 default:
446 fprintf(stderr, "Error: unknown value type for %lu\n", i);
447 break;
448 }
449 }
450 }
451
452 /* Update an entry */
453 if (test[0].val & 1) {
454 key.type = HASH_KEY_STRING;
455 key.str = test[0].str;
456 } else {
457 key.type = HASH_KEY_ULONG;
458 key.ul = test[0].val;
459 }
460
461 if ((status = hash_lookup(table, &key, &value)) != HASH_SUCCESS) {
462 fprintf(stderr, "Error: failed lookup for value %ld, at line %d (%s)\n",
463 test[0].val, __LINE__, error_string(status));
464 exit(1);
465 }
466
467 old_value.type = value.type;
468 switch (value.type) {
469 case HASH_VALUE_LONG:
470 old_value.ul = value.ul;
471 break;
472 case HASH_VALUE_PTR:
473 old_value.ptr = strdup(value.ptr);
474 break;
475 default:
476 fprintf(stderr, "Error: unsupported value type for update.\n");
477 exit(1);
478 }
479
480 value.type = HASH_VALUE_PTR;
481 value.ptr = (void *) strdup("Updated");
482
483 if ((status = hash_enter(table, &key, &value)) != HASH_SUCCESS) {
484 fprintf(stderr, "Error: %ld failed insertion at line %d (%s) \n",
485 test[0].val, __LINE__, error_string(status));
486 exit(1);
487 }
488
489 if ((status = hash_lookup(table, &key, &new_value)) != HASH_SUCCESS) {
490 fprintf(stderr, "Error: failed lookup for value %ld, at line %d (%s)\n",
491 test[0].val, __LINE__, error_string(status));
492 exit(1);
493 }
494
495 if (value.type == new_value.type) {
496 if (strcmp(value.ptr, new_value.ptr) != 0) {
497 fprintf(stderr, "Error: Updated value is not correct, "
498 "expected (%s) got (%s), at line %d\n",
499 (char *) value.ptr, (char *) new_value.ptr, __LINE__);
500 exit(1);
501 }
502 } else {
503 fprintf(stderr, "Error: Updated value is not correct, "
504 "expected type (%d) got (%d), at line %d\n",
505 value.type, new_value.type, __LINE__);
506 exit(1);
507 }
508
509 if ((status = hash_enter(table, &key, &old_value)) != HASH_SUCCESS) {
510 fprintf(stderr, "Error: %ld failed insertion at line %d (%s) \n",
511 test[0].val, __LINE__, error_string(status));
512 exit(1);
513 }
514
515
516 /*
517 * Delete a key, make sure we can't find it, assure we can find all other
518 * keys.
519 */
520 for (i = 0; i < max_test; i++) {
521 if (test[i].val & 1) {
522 key.type = HASH_KEY_STRING;
523 key.str = test[i].str;
524 }
525 else {
526 key.type = HASH_KEY_ULONG;
527 key.ul = test[i].val;
528 }
529
530 if ((status = hash_lookup(table, &key, &value)) != HASH_SUCCESS) {
531 fprintf(stderr, "Error: failed delete lookup for value %ld at index %ld at line %d (%s)\n",
532 test[i].val, i, __LINE__, error_string(status));
533 exit(1);
534 }
535
536 if ((status = hash_delete(table, &key)) != HASH_SUCCESS) {
537 fprintf(stderr, "Error: %ld not in table when deleting, i = %lu at line %d (%s)\n",
538 test[i].val, i, __LINE__, error_string(status));
539 exit(1);
540 }
541
542 if (hash_lookup(table, &key, &value) != HASH_ERROR_KEY_NOT_FOUND) {
543 fprintf(stderr, "Error: found in table after deletion, value = %ld at index %ld at line %d\n",
544 test[i].val, i, __LINE__);
545 exit(1);
546 }
547 /* See if we can find all remaining keys */
548 for (k = i + 1; k < max_test; k++) {
549 if (test[k].val & 1) {
550 key.type = HASH_KEY_STRING;
551 key.str = test[k].str;
552 } else {
553 key.type = HASH_KEY_ULONG;
554 key.ul = test[k].val;
555 }
556 if ((status = hash_lookup(table, &key, &value)) != HASH_SUCCESS) {
557 fprintf(stderr, "Error: failed second lookup for value %ld, i = %lu k = %lu at line %d (%s)\n",
558 test[k].val, i, k, __LINE__, error_string(status));
559 exit(1);
560 } else {
561 switch(value.type) {
562 case HASH_VALUE_PTR:
563 if (strcmp((char *)value.ptr, test[k].str) != 0) {
564 fprintf(stderr, "Error: corrupt ptr data for %lu at line %d\n", k, __LINE__);
565 exit(1);
566 }
567 break;
568 case HASH_VALUE_LONG:
569 if (value.l != test[k].val) {
570 fprintf(stderr, "Error: corrupt long data for %lu at line %d\n", k, __LINE__);
571 exit(1);
572 }
573 break;
574 default:
575 fprintf(stderr, "Error: unknown value type (%d) for %lu\n", value.type, k);
576 break;
577 }
578 }
579 }
580 }
581
582 if (verbose) printf("\n");
583
584 #ifdef HASH_STATISTICS
585 {
586 hash_statistics_t stats;
587
588 if ((status = hash_get_statistics(table, &stats)) != HASH_SUCCESS) {
589 fprintf(stderr, "Error: could not get statistics at line %d (%s)\n",
590 __LINE__, error_string(status));
591 exit(1);
592 }
593
594 printf("Statistics: Accesses = %ld, Collisions = %ld, Collision Rate = %.2f, Expansions = %ld, Contractions = %ld\n",
595 stats.hash_accesses, stats.hash_collisions,
596 ((float)stats.hash_collisions / (float)stats.hash_accesses),
597 stats.table_expansions, stats.table_contractions);
598 }
599 #endif
600
601 if ((status = hash_destroy(table)) != HASH_SUCCESS) {
602 fprintf(stderr, "table destruction failed at line %d (%s)\n",
603 __LINE__, error_string(status));
604 exit(1);
605 }
606
607 for (i = 0; i < max_test; i++) {
608 if (test[i].val & 1) {
609 free(test[i].str);
610 }
611 }
612 free(test);
613 free(iter_result_1);
614 free(iter_result_2);
615
616 printf("Successfully tested %lu values\n", max_test);
617 return 0;
618 }