aboutsummaryrefslogtreecommitdiff
path: root/tests/test6.c
blob: 55ce36bfba4161a698eca774c932ebe1d0093cc2 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "uthash.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Set up macros for alternative malloc/free functions  */
#undef uthash_malloc
#undef uthash_free
#undef uthash_memcmp
#undef uthash_strlen
#undef uthash_bzero
#define uthash_malloc(sz) alt_malloc(sz)
#define uthash_free(ptr,sz) alt_free(ptr,sz)
#define uthash_memcmp(a,b,n) alt_memcmp(a,b,n)
#define uthash_strlen(s) ..fail_to_compile..
#define uthash_bzero(a,n) alt_bzero(a,n)

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

static size_t alt_malloc_sizes[10];
static int alt_malloc_balance = 0;
static void *alt_malloc(size_t sz)
{
    alt_malloc_sizes[alt_malloc_balance++] = sz;
    if (alt_malloc_balance == 1) {
        assert(sz == sizeof(UT_hash_table));
    }
    return malloc(sz);
}
static void alt_free(void *ptr, size_t sz)
{
    size_t expected = alt_malloc_sizes[--alt_malloc_balance];
    if (sz != expected) {
        printf("expected free of size %d, got %d\n", (int)expected, (int)sz);
    }
    free(ptr);
}

static int alt_memcmp_count = 0;
static int alt_memcmp(const void *a, const void *b, size_t n)
{
    ++alt_memcmp_count;
    return memcmp(a,b,n);
}

static int alt_bzero_count = 0;
static void alt_bzero(void *a, size_t n)
{
    ++alt_bzero_count;
    memset(a,0,n);
}

static void *real_malloc(size_t n)
{
    return malloc(n);
}

static void real_free(void *p)
{
    free(p);
}

#undef malloc
#undef realloc
#undef free
#undef memset
#undef memcmp
#undef strlen
#define malloc ..fail_to_compile..
#define realloc ..fail_to_compile..
#define free ..fail_to_compile..
#define memset ..fail_to_compile..
#define memcmp ..fail_to_compile..
#define strlen ..fail_to_compile..

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

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

    /* delete each ID */
    for(i=0; i<10; i++) {
        HASH_FIND_INT(users,&i,tmp);
        if (tmp != NULL) {
            HASH_DEL(users,tmp);
            real_free(tmp);
        } else {
            printf("user id %d not found\n", i);
        }
    }

    /* show the hash */
    for(user=users; user != NULL; user=(example_user_t*)(user->hh.next)) {
        printf("user %d, cookie %d\n", user->id, user->cookie);
    }

#ifdef HASH_BLOOM
    assert(alt_bzero_count == 3);
#else
    assert(alt_bzero_count == 2);
#endif
    assert(alt_memcmp_count == 10);
    assert(alt_malloc_balance == 0);
    return 0;
}