summaryrefslogtreecommitdiffstats
path: root/runtime/hashtable/tester.c
blob: 4678ffa85e2b146526924157bab641e0a996cb2a (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* Copyright (C) 2002, 2004 Christopher Clark <firstname.lastname@cl.cam.ac.uk> */

#include "hashtable.h"
#include "hashtable_itr.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h> /* for memcmp */

static const int ITEM_COUNT = 4000;

typedef unsigned int uint32_t;
typedef unsigned short uint16_t;

/*****************************************************************************/
struct key
{
    uint32_t one_ip; uint32_t two_ip; uint16_t one_port; uint16_t two_port;
};

struct value
{
    char *id;
};

DEFINE_HASHTABLE_INSERT(insert_some, struct key, struct value);
DEFINE_HASHTABLE_SEARCH(search_some, struct key, struct value);
DEFINE_HASHTABLE_REMOVE(remove_some, struct key, struct value);
DEFINE_HASHTABLE_ITERATOR_SEARCH(search_itr_some, struct key);


/*****************************************************************************/
static unsigned int
hashfromkey(void *ky)
{
    struct key *k = (struct key *)ky;
    return (((k->one_ip << 17) | (k->one_ip >> 15)) ^ k->two_ip) +
            (k->one_port * 17) + (k->two_port * 13 * 29);
}

static int
equalkeys(void *k1, void *k2)
{
    return (0 == memcmp(k1,k2,sizeof(struct key)));
}

/*****************************************************************************/
int
main(int argc, char **argv)
{
    struct key *k, *kk;
    struct value *v, *found;
    struct hashtable *h;
    struct hashtable_itr *itr;
    int i;

    h = create_hashtable(16, hashfromkey, equalkeys);
    if (NULL == h) exit(-1); /*oom*/


/*****************************************************************************/
/* Insertion */
    for (i = 0; i < ITEM_COUNT; i++)
    {
        k = (struct key *)malloc(sizeof(struct key));
        if (NULL == k) {
            printf("ran out of memory allocating a key\n");
            return 1;
        }
        k->one_ip = 0xcfccee40 + i;
        k->two_ip = 0xcf0cee67 - (5 * i);
        k->one_port = 22 + (7 * i);
        k->two_port = 5522 - (3 * i);
        
        v = (struct value *)malloc(sizeof(struct value));
        v->id = "a value";
        
        if (!insert_some(h,k,v)) exit(-1); /*oom*/
    }
    printf("After insertion, hashtable contains %u items.\n",
            hashtable_count(h));

/*****************************************************************************/
/* Hashtable search */
    k = (struct key *)malloc(sizeof(struct key));
    if (NULL == k) {
        printf("ran out of memory allocating a key\n");
        return 1;
    }
    
    for (i = 0; i < ITEM_COUNT; i++)
    {
        k->one_ip = 0xcfccee40 + i;
        k->two_ip = 0xcf0cee67 - (5 * i);
        k->one_port = 22 + (7 * i);
        k->two_port = 5522 - (3 * i);
        
        if (NULL == (found = search_some(h,k))) {
            printf("BUG: key not found\n");
        }
    }

/*****************************************************************************/
/* Hashtable iteration */
    /* Iterator constructor only returns a valid iterator if
     * the hashtable is not empty */
    itr = hashtable_iterator(h);
    i = 0;
    if (hashtable_count(h) > 0)
    {
        do {
            kk = hashtable_iterator_key(itr);
            v = hashtable_iterator_value(itr);
            /* here (kk,v) are a valid (key, value) pair */
            /* We could call 'hashtable_remove(h,kk)' - and this operation
             * 'free's kk. However, the iterator is then broken.
             * This is why hashtable_iterator_remove exists - see below.
             */
            i++;

        } while (hashtable_iterator_advance(itr));
    }
    printf("Iterated through %u entries.\n", i);

/*****************************************************************************/
/* Hashtable iterator search */

    /* Try the search some method */
    for (i = 0; i < ITEM_COUNT; i++)
    {
        k->one_ip = 0xcfccee40 + i;
        k->two_ip = 0xcf0cee67 - (5 * i);
        k->one_port = 22 + (7 * i);
        k->two_port = 5522 - (3 * i);
        
        if (0 == search_itr_some(itr,h,k)) {
            printf("BUG: key not found searching with iterator");
        }
    }

/*****************************************************************************/
/* Hashtable removal */

    for (i = 0; i < ITEM_COUNT; i++)
    {
        k->one_ip = 0xcfccee40 + i;
        k->two_ip = 0xcf0cee67 - (5 * i);
        k->one_port = 22 + (7 * i);
        k->two_port = 5522 - (3 * i);
        
        if (NULL == (found = remove_some(h,k))) {
            printf("BUG: key not found for removal\n");
        }
    }
    printf("After removal, hashtable contains %u items.\n",
            hashtable_count(h));

/*****************************************************************************/
/* Hashtable destroy and create */

    hashtable_destroy(h, 1);
    h = NULL;
    free(k);

    h = create_hashtable(160, hashfromkey, equalkeys);
    if (NULL == h) {
        printf("out of memory allocating second hashtable\n");
        return 1;
    }

/*****************************************************************************/
/* Hashtable insertion */

    for (i = 0; i < ITEM_COUNT; i++)
    {
        k = (struct key *)malloc(sizeof(struct key));
        k->one_ip = 0xcfccee40 + i;
        k->two_ip = 0xcf0cee67 - (5 * i);
        k->one_port = 22 + (7 * i);
        k->two_port = 5522 - (3 * i);
        
        v = (struct value *)malloc(sizeof(struct value));
        v->id = "a value";
        
        if (!insert_some(h,k,v))
        {
            printf("out of memory inserting into second hashtable\n");
            return 1;
        }
    }
    printf("After insertion, hashtable contains %u items.\n",
            hashtable_count(h));

/*****************************************************************************/
/* Hashtable iterator search and iterator remove */

    k = (struct key *)malloc(sizeof(struct key));
    if (NULL == k) {
        printf("ran out of memory allocating a key\n");
        return 1;
    }
    
    for (i = ITEM_COUNT - 1; i >= 0; i = i - 7)
    {
        k->one_ip = 0xcfccee40 + i;
        k->two_ip = 0xcf0cee67 - (5 * i);
        k->one_port = 22 + (7 * i);
        k->two_port = 5522 - (3 * i);
        
        if (0 == search_itr_some(itr, h, k)) {
            printf("BUG: key %u not found for search preremoval using iterator\n", i);
            return 1;
        }
        if (0 == hashtable_iterator_remove(itr)) {
            printf("BUG: key not found for removal using iterator\n");
            return 1;
        }
    }
    free(itr);

/*****************************************************************************/
/* Hashtable iterator remove and advance */

    for (itr = hashtable_iterator(h);
         hashtable_iterator_remove(itr) != 0; ) {
        ;
    }
    free(itr);
    printf("After removal, hashtable contains %u items.\n",
            hashtable_count(h));

/*****************************************************************************/
/* Hashtable destroy */

    hashtable_destroy(h, 1);
    free(k);
    return 0;
}

/*
 * Copyright (c) 2002, 2004, Christopher Clark
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * * Neither the name of the original author; nor the names of any contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 * 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/