aboutsummaryrefslogtreecommitdiff
path: root/tests/test10.c
blob: 64f8035e2677502fa45f1356f3a52db61f3334fd (plain)
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
#include "uthash.h"
#include <stdlib.h>   /* malloc */
#include <stdio.h>    /* printf */

typedef struct example_user_t {
    int id;
    int cookie;
    UT_hash_handle hh;
    UT_hash_handle alth;
} example_user_t;

int main()
{
    int i;
    example_user_t *user, *tmp, *users=NULL, *altusers=NULL;

    /* create elements */
    for(i=0; i<1000; i++) {
        user = (example_user_t*)malloc(sizeof(example_user_t));
        if (user == NULL) {
            exit(-1);
        }
        user->id = i;
        user->cookie = i*i;
        if (i<10) {
            HASH_ADD_INT(users,id,user);
        }
        HASH_ADD(alth,altusers,id,sizeof(int),user);
    }

    /*
    printf("hh items: %d, alth items: %d\n",
            users->hh.tbl->num_items, users->alth.tbl->num_items);
    printf("hh buckets: %d, alth buckets: %d\n",
            users->hh.tbl->num_buckets, users->alth.tbl->num_buckets);
    */

    i=9;
    HASH_FIND_INT(users,&i,tmp);
    printf("%d %s in hh\n", i, (tmp != NULL) ? "found" : "not found");
    HASH_FIND(alth,altusers,&i,sizeof(int),tmp);
    printf("%d %s in alth\n", i, (tmp != NULL) ? "found" : "not found");

    i=10;
    HASH_FIND_INT(users,&i,tmp);
    printf("%d %s in hh\n", i, (tmp != NULL) ? "found" : "not found");
    HASH_FIND(alth,altusers,&i,sizeof(int),tmp);
    printf("%d %s in alth\n", i, (tmp != NULL) ? "found" : "not found");

    return 0;
}