aboutsummaryrefslogtreecommitdiff
path: root/hmap/hmap-test.c
blob: bf3ebe2d10fa2dfc7d7e88824dbeb75f991ed8d6 (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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include "hash.h"
#include "hmap.h"
#include "../utest/utest.h"

#include <string.h>

struct hmap_test_s {
	const char		 *key;
	uint64_t		  data;
	struct hmap_entry entry;
};

#define entry_to_test_s(ent) hmap_item(ent, struct hmap_test_s, entry)

bool hmap_test_cmp(const struct hmap_entry *ent, const struct hmap_entry *key)
{
	struct hmap_test_s *a = entry_to_test_s(ent);
	struct hmap_test_s *b = entry_to_test_s(key);

	return !strcmp(a->key, b->key);
}

void hmap_test_hash(struct hmap_entry *ent)
{
	struct hmap_test_s *e = entry_to_test_s(ent);
	ent->hash			  = fnv1a_str_hash(e->key);
}

struct hmap *map;

/* clang-format off */
struct hmap_test_s inputs[] = {
	{ .key = "alpha",	.data = 10, },
	{ .key = "bravo",	.data = 24, },
	{ .key = "charlie",	.data = 30, },
	{ .key = "delta",	.data = 42, },
	{ .key = "echo",	.data = 50, },
	{ .key = "foxtrot",	.data = 69, },
	{ .key = "golf",	.data = 70, },
	{ .key = "hotel",	.data = 80, },
	{ .key = "india",	.data = 99, },
};

struct hmap_test_s inputs2[] = {
	{ .key = "aa" },
	{ .key = "ab" },
	{ .key = "ac" },
	{ .key = "ad" },
	{ .key = "ae" },
	{ .key = "af" },
	{ .key = "ag" },
	{ .key = "ah" },
	{ .key = "ai" },
	{ .key = "aj" },
	{ .key = "ak" },
	{ .key = "al" },
	{ .key = "am" },
	{ .key = "an" },
	{ .key = "ao" },
	{ .key = "ap" },
	{ .key = "aq" },
	{ .key = "ar" },
	{ .key = "as" },
	{ .key = "at" },
	{ .key = "au" },
	{ .key = "av" },
	{ .key = "aw" },
	{ .key = "ax" },
	{ .key = "ay" },
	{ .key = "az" },
	{ .key = "ba" },
	{ .key = "bb" },
	{ .key = "bc" },
	{ .key = "bd" },
	{ .key = "be" },
	{ .key = "bf" },
	{ .key = "bg" },
	{ .key = "bh" },
	{ .key = "bi" },
	{ .key = "bj" },
	{ .key = "bk" },
	{ .key = "bl" },
	{ .key = "bm" },
	{ .key = "bn" },
	{ .key = "bo" },
	{ .key = "bp" },
	{ .key = "bq" },
	{ .key = "br" },
	{ .key = "bs" },
	{ .key = "bt" },
	{ .key = "bu" },
	{ .key = "bv" },
	{ .key = "bw" },
	{ .key = "bx" },
	{ .key = "by" },
};
/* clang-format on */

struct hmap_test_s duplicate = { .key = "foxtrot", .data = 420 };

#ifndef HMAP_DEBUG

TEST_BEGIN(test_hmap_add)
{
	for (size_t i = 0; i < ARRAY_SIZE(inputs); i++) {
		hmap_add(map, &inputs[i].entry);
	}
	size_t cnt = hmap_entry_cnt(map);
	expect(cnt == ARRAY_SIZE(inputs),
		   "number of entries in hashmap (%lu) != number of entries added "
		   "(%lu)",
		   cnt, ARRAY_SIZE(inputs));
	TEST_OUT
}
TEST_END

TEST_BEGIN(test_hmap_get)
{
	for (size_t i = 0; i < ARRAY_SIZE(inputs); i++) {
		struct hmap_test_s	key	   = { .key = inputs[i].key };
		struct hmap_test_s *expect = inputs + i;
		struct hmap_entry  *ent	   = hmap_get(map, &key.entry);

		assertneq(ent, NULL);

		struct hmap_test_s *got = entry_to_test_s(ent);
		asserteq(expect, got);
	}
	TEST_OUT
}
TEST_END

TEST_BEGIN(test_hmap_get_next_dup)
{
	hmap_add(map, &duplicate.entry);
	struct hmap_test_s key = { .key = duplicate.key };
	struct hmap_entry *ent = hmap_get(map, &key.entry);

	assertneq(ent, NULL);

	struct hmap_test_s *got = entry_to_test_s(ent);

	struct hmap_entry *next = hmap_get_next_dup(map, ent);

	assertneq(next, NULL);

	struct hmap_test_s *got_dup = entry_to_test_s(next);
	if (got->data != duplicate.data) {
		asserteq(got_dup->data, duplicate.data);
	} else {
		assertneq(got_dup->data, duplicate.data);
	}
	TEST_OUT
}
TEST_END

TEST_BEGIN(test_hmap_remove)
{
	struct hmap_entry *removed;
	for (size_t i = 0; i < ARRAY_SIZE(inputs); i++) {
		struct hmap_test_s key = { .key = inputs[i].key };
		removed				   = hmap_remove(map, &key.entry);

		assertneq(removed, NULL);
	}

	expect(hmap_entry_cnt(map) == 1, "expected only duplicate entry left");
	removed = hmap_remove(map, &duplicate.entry);
	asserteq(removed, &duplicate.entry);

	TEST_OUT
}
TEST_END

TEST_BEGIN(test_hmap_realloc)
{
	/* This test case assumes default hmap settings */
	for (size_t i = 0; i < ARRAY_SIZE(inputs); i++) {
		hmap_add(map, &inputs[i].entry);
	}
	expect(hmap_cur_cap(map) <= 64, "hashmap shouldn't grow so fast");

	for (size_t i = 0; i < ARRAY_SIZE(inputs2); i++) {
		hmap_add(map, &inputs2[i].entry);
	}
	asserteq(hmap_entry_cnt(map), ARRAY_SIZE(inputs) + ARRAY_SIZE(inputs2));
	expect(hmap_cur_cap(map) > 64, "expected hashmap to grow");

	for (size_t i = 0; i < ARRAY_SIZE(inputs2); i++) {
		hmap_remove(map, &inputs2[i].entry);
	}
	expect(hmap_cur_cap(map) <= 64, "expected hashmap to shrink");

	TEST_OUT
	for (size_t i = 0; i < ARRAY_SIZE(inputs); i++) {
		hmap_remove(map, &inputs[i].entry);
	}
}
TEST_END

TEST_BEGIN(test_hmap_iter)
{
	struct hmap_iter   iter;
	struct hmap_entry *ent, *prev = NULL;
	size_t			   cnt = 0;

	hmap_iter_init(&iter, map);
	for (size_t i = 0; i < ARRAY_SIZE(inputs); i++) {
		hmap_add(map, &inputs[i].entry);
	}

	hmap_iter_foreach(&iter, ent) {
		cnt++;
		expect(ent != prev, "hmap_iter shouldn't return same entry on next");
	}
	asserteq(cnt, ARRAY_SIZE(inputs));

	TEST_OUT
}
TEST_END

TEST_BEGIN(test_hmap_iter_remove)
{
	struct hmap_iter   iter;
	struct hmap_entry *ent;

	for (size_t i = 0; i < ARRAY_SIZE(inputs2); i++) {
		hmap_add(map, &inputs2[i].entry);
	}

	hmap_dynamic_set(map, false);
	expect(!hmap_dynamic_get(map), "expected dynamic setting to be off");

	hmap_iter_init(&iter, map);
	hmap_iter_foreach(&iter, ent) {
		expect(hmap_remove(map, ent), "expected entry to be removed");
	}
	asserteq(hmap_entry_cnt(map), 0);

	hmap_dynamic_set(map, true);
	expect(hmap_dynamic_get(map), "expected dynamic setting to be on");

	TEST_OUT
}
TEST_END

#else

TEST_BEGIN(test_hmap_collisions)
{
	for (size_t i = 0; i < ARRAY_SIZE(inputs); i++) {
		hmap_add(map, &inputs[i].entry);
	}
	for (size_t i = 0; i < ARRAY_SIZE(inputs2); i++) {
		hmap_add(map, &inputs2[i].entry);
	}
	asserteq(hmap_entry_cnt(map), ARRAY_SIZE(inputs) + ARRAY_SIZE(inputs2));
	TEST_OUT
}
TEST_END

#endif /* HMAP_DEBUG */

void hmap_test_init(void)
{
#ifdef HMAP_DEBUG
#ifndef HMAP_DEBUG_CAP
#define HMAP_DEBUG_CAP 128
#endif /* HMAP_DEBUG_CAP */
	map = hmap_new_with(hmap_test_cmp, hmap_test_hash, HMAP_DEBUG_CAP, false);
#else
	map = hmap_new(hmap_test_cmp, hmap_test_hash);
#endif /* HMAP_DEBUG */
}

void hmap_test_destroy(void)
{
	hmap_free(map);
}

#ifdef HMAP_DEBUG

RUN_TEST_SUITE(hmap_test_init, hmap_test_destroy, test_hmap_collisions);

#else

RUN_TEST_SUITE(hmap_test_init, hmap_test_destroy, test_hmap_add, test_hmap_get,
			   test_hmap_get_next_dup, test_hmap_remove, test_hmap_realloc,
			   test_hmap_iter, test_hmap_iter_remove)

#endif /* HMAP_DEBUG */