summaryrefslogtreecommitdiffstats
path: root/nserver/tests/hashmap_tests.c
blob: d4835dce28e695ba1938d29c49cb2c280455f4e1 (plain) (blame)
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* SPDX-License-Identifier: BSD-3-Clause */
/*
 * Copyright © 2010, Zed A. Shaw.
 * Copyright © 2020 rsiddharth <s@ricketyspace.net>
 */

#include "minunit.h"
#include <hashmap.h>
#include <assert.h>
#include <bstrlib.h>

Hashmap *map = NULL;
Hashmap *map_alt = NULL;
static int traverse_called = 0;
static int traverse_duplicates = 0;
struct tagbstring test0 = bsStatic("test data 0");
struct tagbstring test1 = bsStatic("test data 1");
struct tagbstring test2 = bsStatic("test data 2");
struct tagbstring test3 = bsStatic("xest data 3");
struct tagbstring expect0 = bsStatic("THE VALUE 0");
struct tagbstring expect1 = bsStatic("THE VALUE 1");
struct tagbstring expect2 = bsStatic("THE VALUE 2");
struct tagbstring expect3 = bsStatic("THE VALUE 3");

static int traverse_good_cb(HashmapNode *node)
{
    debug("KEY: %s", bdata((bstring) node->key));
    traverse_called++;
    return 0;
}

static int traverse_fail_cb(HashmapNode *node)
{
    debug("KEY %s", bdata((bstring) node->key));
    traverse_called++;

    if (traverse_called == 2) {
        return 1;
    } else {
        return 0;
    }
}

static int traverse_duplicates_cb(HashmapNode *node)
{
    if ((bstring) node->data == &expect1) {
        ++traverse_duplicates;
    }

    return 0;
}

int non_empty_buckets(Hashmap *map)
{
    int i = 0, non_empty = 0;

    for (i = 0; i < DArray_count(map->buckets); i++) {
        DArray *bucket = DArray_get(map->buckets, i);
        if (bucket) {
            non_empty += 1;
        }
    }

    return non_empty;
}

char *test_create()
{
    map = Hashmap_create(NULL, NULL);
    mu_assert(map != NULL, "Failed to create map.");

    map_alt = Hashmap_create(NULL, (Hashmap_hash) fnv_hash);
    mu_assert(map != NULL, "Failde to create map_alt.");

    return NULL;
}

char *test_destroy()
{
    Hashmap_destroy(map);
    Hashmap_destroy(map_alt);
    return NULL;
}

char *test_get_test()
{
    int rc = Hashmap_set(map, &test1, &expect1);
    mu_assert(rc == 0, "Failed to set &test1");
    bstring result = Hashmap_get(map, &test1);
    mu_assert(result == &expect1, "Wron value for test1.");

    rc = Hashmap_set(map, &test2, &expect2);
    mu_assert(rc == 0, "Failed to test test2");
    result = Hashmap_get(map, &test2);
    mu_assert(result == &expect2, "Wrong value for test2.");

    rc = Hashmap_set(map, &test3, &expect3);
    mu_assert(rc == 0, "Failed to set test3");
    result = Hashmap_get(map, &test3);
    mu_assert(result == &expect3, "Wrong value for test3");

    return NULL;
}

char *test_traverse()
{
    int rc = Hashmap_traverse(map, traverse_good_cb);
    mu_assert(rc == 0, "Failed to traverse");
    mu_assert(traverse_called == 3, "Wrong count traverse");

    traverse_called = 0;
    rc = Hashmap_traverse(map, traverse_fail_cb);
    mu_assert(rc == 1, "Failed to traverse.");
    mu_assert(traverse_called == 2, "Wrong count traverse for fail.");

    return NULL;
}

char *test_keys()
{
    DArray *keys = Hashmap_keys(map);
    mu_assert(keys != NULL, "Unable to get hashmap's keys");
    mu_assert(DArray_count(keys) == 3, "Expected three keys");

    for (int i = 0; i < DArray_count(keys); i++) {
        char *k = bdata((bstring) DArray_get(keys, i));

        debug("KEY: %s", k);
    }

    // clean up.
    DArray_destroy(keys);

    return NULL;
}

char *test_delete()
{
    bstring deleted = (bstring) Hashmap_delete(map, &test1);
    mu_assert(deleted != NULL, "Got NULL on delete");
    mu_assert(deleted == &expect1, "Should get test1");
    bstring result = Hashmap_get(map, &test1);
    mu_assert(result == NULL, "Should delete");

    deleted = (bstring) Hashmap_delete(map, &test2);
    mu_assert(deleted != NULL, "Got NULL on delete");
    mu_assert(deleted == &expect2, "Should get test2");
    result = Hashmap_get(map, &test2);
    mu_assert(result == NULL, "Should delete");

    deleted = (bstring) Hashmap_delete(map, &test3);
    mu_assert(deleted != NULL, "Got NULL on delete.");
    mu_assert(deleted == &expect3, "Should get test3");
    result = Hashmap_get(map, &test3);
    mu_assert(result == NULL, "Should delete.");

    return NULL;
}

char *test_bucket_destruction()
{
    int rc = 0, non_empty = 0;
    bstring deleted  = NULL;

    // Insert some elements.
    rc = Hashmap_set(map, &test1, &expect1);
    mu_assert(rc == 0, "Failed to set &test1 0");
    rc = Hashmap_set(map, &test2, &expect2);
    mu_assert(rc == 0, "Failed to test test2");

    // Non-empty buckets test.
    non_empty = non_empty_buckets(map);
    mu_assert(non_empty == 2, "Expected two non-empty buckets");

    // Remove test1
    deleted = Hashmap_delete(map, &test1);
    mu_assert(deleted != NULL, "Error deleting test1");

    // Non-empty buckets test.
    non_empty = non_empty_buckets(map);
    mu_assert(non_empty == 1, "Expected one non-empty buckets");

    // Remove test2
    deleted = Hashmap_delete(map, &test2);
    mu_assert(deleted != NULL, "Error deleting test2");

    // Non-empty buckets test.
    non_empty = non_empty_buckets(map);
    mu_assert(non_empty == 0, "Expected one non-empty buckets");

    return NULL;
}

char *test_set_duplicates()
{
    int rc = 0;

    // Insert test1 two times.
    rc = Hashmap_set(map, &test1, &expect1);
    mu_assert(rc == 0, "Failed to set &test1 0");
    rc = Hashmap_set(map, &test1, &expect1);
    mu_assert(rc == 0, "Failed to set &test1 1");

    // Insert test2 one time.
    rc = Hashmap_set(map, &test2, &expect2);
    mu_assert(rc == 0, "Failed to test test2");

    // Insert test3 one time.
    rc = Hashmap_set(map, &test3, &expect3);
    mu_assert(rc == 0, "Failed to set test3");

    // Test there are two test1 nodes.
    traverse_duplicates = 0;
    rc = Hashmap_traverse(map, traverse_duplicates_cb);
    mu_assert(traverse_duplicates == 2,
              "traverse_duplicates must be 2");

    // Cleanup
    bstring deleted  = NULL;
    deleted = Hashmap_delete(map, &test1);
    mu_assert(deleted != NULL, "Error deleting test1 0");
    deleted  = Hashmap_delete(map, &test1);
    mu_assert(deleted != NULL, "Error deleting test1 1");
    deleted  = Hashmap_delete(map, &test2);
    mu_assert(deleted != NULL, "Error deleting test2");
    deleted  = Hashmap_delete(map, &test3);
    mu_assert(deleted != NULL, "Error deleting test3");

    return NULL;
}

char *test_set_fucked()
{
    int rc = 0;

    // Insert test1 three times.
    rc = Hashmap_set_fucked(map, &test1, &expect1);
    mu_assert(rc == 1, "Failed to set fuck &test1 0");
    rc = Hashmap_set_fucked(map, &test1, &expect1);
    mu_assert(rc == 0, "Failed to set fuck &test1 1");
    rc = Hashmap_set_fucked(map, &test1, &expect1);
    mu_assert(rc == 0, "Failed to set fuck &test1 2");

    // Insert test2 one time.
    rc = Hashmap_set_fucked(map, &test2, &expect2);
    mu_assert(rc == 1, "Failed to test test2");

    // Insert test3 one time.
    rc = Hashmap_set_fucked(map, &test3, &expect3);
    mu_assert(rc == 1, "Failed to set test3");

    // Test there are two test1 nodes.
    traverse_duplicates = 0;
    rc = Hashmap_traverse(map, traverse_duplicates_cb);
    mu_assert(traverse_duplicates == 1,
              "traverse_duplicates must be 1");

    // Cleanup
    bstring deleted  = NULL;
    deleted = Hashmap_delete(map, &test1);
    mu_assert(deleted != NULL, "Error deleting test1");
    deleted  = Hashmap_delete(map, &test1);
    mu_assert(deleted == NULL, "Error test1 must be already deleted");
    deleted  = Hashmap_delete(map, &test2);
    mu_assert(deleted != NULL, "Error deleting test2");
    deleted  = Hashmap_delete(map, &test3);
    mu_assert(deleted != NULL, "Error deleting test3");

    return NULL;
}

char *test_fnv_hash()
{
    uint32_t hash[3];

    hash[0] = fnv_hash(&test0);
    mu_assert(hash[0] > 0, "hash not set correctly for key test0");

    hash[1] = fnv_hash(&test1);
    mu_assert(hash[1] > 0, "hash not set correctly for key test1");

    hash[2] = fnv_hash(&test2);
    mu_assert(hash[2] > 0, "hash not set correctly for key test2");

    // Check all three hashes are different.
    mu_assert(hash[0] != hash[1], "hash for test0 = hash for test1");
    mu_assert(hash[0] != hash[2], "hash for test0 = hash for test2");
    mu_assert(hash[1] != hash[2], "hash for test1 = hash for test2");

    return NULL;
}

char *all_tests()
{
    mu_suite_start();

    mu_run_test(test_create);
    mu_run_test(test_get_test);
    mu_run_test(test_traverse);
    mu_run_test(test_keys);
    mu_run_test(test_delete);
    mu_run_test(test_bucket_destruction);
    mu_run_test(test_set_duplicates);
    mu_run_test(test_set_fucked);
    mu_run_test(test_fnv_hash);
    mu_run_test(test_destroy);

    return NULL;
}

RUN_TESTS(all_tests);