-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint_hash_test.c
65 lines (56 loc) · 1.28 KB
/
int_hash_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "dhash.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define SIZE 128
static elem_t copy_elem_func(elem_t elem)
{
int *copy = malloc(sizeof(int));
*copy = *(int *)elem;
return copy;
}
static int elem_equal_func(elem_t *a, elem_t *b)
{
return (*(int *)a) == (*(int *)b);
}
static void free_elem_func(elem_t elem)
{
free(elem);
}
static int hash_func(elem_t elem)
{
return *(int *)elem;
}
static void show_elem_func(elem_t elem)
{
printf("%d\t", *(int *)elem);
}
int main()
{
dhash_t *dhash;
int elem[SIZE];
int i;
dhash = dhash_init(0);
if (!dhash)
return -1;
set_hash_func(dhash, hash_func);
set_free_elem_func(dhash, free_elem_func);
set_elem_equal_func(dhash, elem_equal_func);
set_copy_elem_func(dhash, copy_elem_func);
set_show_elem_func(dhash, show_elem_func);
srand(time(NULL));
for (i = 0; i < SIZE; i++) {
elem[i] = rand();
if (dhash_insert(dhash, &elem[i]) < 0)
printf("insert %d failed\n", elem[i]);
}
printf("hash table:\n");
dhash_show(dhash);
for (i = 0; i < SIZE; i++) {
assert(NULL != dhash_query(dhash, &elem[i]));
dhash_delete(dhash, &elem[i]);
}
dhash_destroy(dhash);
return 0;
}