summaryrefslogtreecommitdiff
path: root/test/hashtable.c
blob: 11c58c625af5d881c523f99e1a72a28d5200cb2b (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 * Copyright 2015 Vincent Sanders <vince@netsurf-browser.org>
 * Copyright 2006 Rob Kendrick <rjek@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * \file
 * Test hash table operations.
 *
 * Implementation taken from original test rig in bloom filter code
 */

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <check.h>

#include "utils/hashtable.h"

/* Limit for hash table tests which use /usr/share/dict/words */
#define DICT_TEST_WORD_COUNT 100000

#define NELEMS(x)  (sizeof(x) / sizeof((x)[0]))

struct test_pairs {
	const char* test;
	const char* res;
};

static struct hash_table *match_hash_a;
static struct hash_table *match_hash_b;

static struct hash_table *dict_hash;

static const struct test_pairs match_tests[] = {
	{ "cow", "moo" },
	{ "pig", "oink" },
	{ "chicken", "cluck" },
	{ "dog", "woof" },
	{ "sheep", "baaa" },
};

/* Fixtures */

static void match_hashtable_create(void)
{
	unsigned int idx;

	match_hash_a = hash_create(79);
	ck_assert(match_hash_a != NULL);

	match_hash_b = hash_create(103);
	ck_assert(match_hash_b != NULL);

	for (idx = 0; idx < NELEMS(match_tests); idx++) {
		hash_add(match_hash_a,
			 match_tests[idx].test,
			 match_tests[idx].res);
		hash_add(match_hash_b,
			 match_tests[idx].res,
			 match_tests[idx].test);
	}
}

static void match_hashtable_teardown(void)
{
	hash_destroy(match_hash_a);
	hash_destroy(match_hash_b);
}


/**
 * create dictionary hashtable
 *
 * hashtable constructed from the odd/even rows of the
 * dictionary
 */
static void dict_hashtable_create(int dict_hash_size)
{
	FILE *dictf;
	char keybuf[BUFSIZ], valbuf[BUFSIZ];
	uint32_t counter = 0;

	dictf = fopen("/usr/share/dict/words", "r");
	ck_assert(dictf != NULL);

	dict_hash = hash_create(dict_hash_size);
	ck_assert(dict_hash != NULL);

	while (!feof(dictf)) {
		fscanf(dictf, "%s", keybuf);
		fscanf(dictf, "%s", valbuf);
		hash_add(dict_hash, keybuf, valbuf);
		if (counter++ > DICT_TEST_WORD_COUNT) break;
	}

	fclose(dictf);
}

static void dicts_hashtable_create(void)
{
	dict_hashtable_create(1031);
}

static void dictl_hashtable_create(void)
{
	dict_hashtable_create(7919);
}

static void dict_hashtable_teardown(void)
{
	hash_destroy(dict_hash);
}

/* Tests */

/**
 * Test hash table creation
 *
 * Create a hash table, add a single entry and test for value retival
 * from key.
 *
 */
START_TEST(hashtable_create_test)
{
	struct hash_table *ht;

	ht = hash_create(42);
	ck_assert(ht != NULL);

	hash_destroy(ht);
}
END_TEST

/**
 * Test hash table simple operation
 *
 * Create a hash table, add a single entry and test for failed retival
 * from not present key.
 *
 */
START_TEST(hashtable_negative_test)
{
	struct hash_table *ht;
	bool added;
	const char *res;

	/* create hash */
	ht = hash_create(42);
	ck_assert(ht != NULL);

	/* add entry */
	added = hash_add(ht, "cow", "moo");
	ck_assert(added == true);

	res = hash_get(ht, "sheep");
	ck_assert(res == NULL);

	hash_destroy(ht);
}
END_TEST

/**
 * Test hash table simple operation
 *
 * Create a hash table, add a single entry and test for sucessful
 * retrival of key.
 *
 */
START_TEST(hashtable_positive_test)
{
	struct hash_table *ht;
	bool added;
	const char *res;

	/* create hash */
	ht = hash_create(42);
	ck_assert(ht != NULL);

	/* add entry */
	added = hash_add(ht, "cow", "moo");
	ck_assert(added == true);

	res = hash_get(ht, "cow");
	ck_assert(res != NULL);
	ck_assert_str_eq(res, "moo");

	hash_destroy(ht);
}
END_TEST


START_TEST(hashtable_matcha_test)
{
	const char *res;

	res = hash_get(match_hash_a, match_tests[_i].test);
	ck_assert(res != NULL);
	ck_assert_str_eq(res, match_tests[_i].res);
}
END_TEST


START_TEST(hashtable_matchb_test)
{
	const char *res;

	res = hash_get(match_hash_b, match_tests[_i].res);
	ck_assert(res != NULL);
	ck_assert_str_eq(res, match_tests[_i].test);
}
END_TEST


START_TEST(hashtable_dict_test)
{
	FILE *dictf;
	char keybuf[BUFSIZ], valbuf[BUFSIZ];
	const char *res;
	uint32_t counter = 0;

	dictf = fopen("/usr/share/dict/words", "r");
	ck_assert(dictf != NULL);

	while (!feof(dictf)) {
		fscanf(dictf, "%s", keybuf);
		fscanf(dictf, "%s", valbuf);

		res = hash_get(dict_hash, keybuf);
		ck_assert(res != NULL);
		ck_assert_str_eq(res, valbuf);
		if (counter++ > DICT_TEST_WORD_COUNT) break;
	}

	fclose(dictf);
}
END_TEST

/* Suite */

static Suite *hashtable_suite(void)
{
	Suite *s;
	TCase *tc_create;
	TCase *tc_match;
	TCase *tc_dict_s;
	TCase *tc_dict_l;

	s = suite_create("hash table filter");

	/* Core API */
	tc_create = tcase_create("Core");

	tcase_add_test(tc_create, hashtable_create_test);
	tcase_add_test(tc_create, hashtable_negative_test);
	tcase_add_test(tc_create, hashtable_positive_test);

	suite_add_tcase(s, tc_create);

	/* Matching entry tests */
	tc_match = tcase_create("Match");

	tcase_add_checked_fixture(tc_match,
				  match_hashtable_create,
				  match_hashtable_teardown);

	tcase_add_loop_test(tc_match,
			    hashtable_matcha_test,
			    0, NELEMS(match_tests));
	tcase_add_loop_test(tc_match,
			    hashtable_matchb_test,
			    0, NELEMS(match_tests));

	suite_add_tcase(s, tc_match);

	/* small table dictionary test */
	tc_dict_s = tcase_create("small table dictionary");
	tcase_add_checked_fixture(tc_dict_s,
				  dicts_hashtable_create,
				  dict_hashtable_teardown);

	tcase_add_test(tc_dict_s, hashtable_dict_test);

	suite_add_tcase(s, tc_dict_s);


	/* large table dictionary test */
	tc_dict_l = tcase_create("large table dictionary");
	tcase_add_checked_fixture(tc_dict_l,
				  dictl_hashtable_create,
				  dict_hashtable_teardown);

	tcase_add_test(tc_dict_l, hashtable_dict_test);

	suite_add_tcase(s, tc_dict_l);

	return s;
}

int main(int argc, char **argv)
{
	int number_failed;
	Suite *s;
	SRunner *sr;

	s = hashtable_suite();

	sr = srunner_create(s);
	srunner_run_all(sr, CK_ENV);

	number_failed = srunner_ntests_failed(sr);
	srunner_free(sr);

	return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}