forked from fmela/libdict
/
unit_tests.c
373 lines (329 loc) · 10.1 KB
/
unit_tests.c
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* unit_tests.c
* Copyright (C) 2012 Farooq Mela. All rights reserved. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <limits.h>
#include <float.h>
#include <time.h>
#include <CUnit/CUnit.h>
#include <CUnit/Basic.h>
#include "dict.h"
#define TEST_FUNC(func) { #func, func }
struct key_info {
char *key, *value, *alt;
};
void test_basic(dict *dct, const struct key_info *keys, const unsigned nkeys);
void test_basic_hashtable_1bucket();
void test_basic_hashtable_nbuckets();
void test_basic_height_balanced_tree();
void test_basic_path_reduction_tree();
void test_basic_red_black_tree();
void test_basic_skiplist();
void test_basic_splay_tree();
void test_basic_treap();
void test_basic_weight_balanced_tree();
void test_version_string();
CU_TestInfo basic_tests[] = {
TEST_FUNC(test_basic_hashtable_1bucket),
TEST_FUNC(test_basic_hashtable_nbuckets),
TEST_FUNC(test_basic_height_balanced_tree),
TEST_FUNC(test_basic_path_reduction_tree),
TEST_FUNC(test_basic_red_black_tree),
TEST_FUNC(test_basic_skiplist),
TEST_FUNC(test_basic_splay_tree),
TEST_FUNC(test_basic_treap),
TEST_FUNC(test_basic_weight_balanced_tree),
TEST_FUNC(test_version_string),
CU_TEST_INFO_NULL
};
#define TEST_SUITE(suite) { #suite, NULL, NULL, suite }
CU_SuiteInfo test_suites[] = {
TEST_SUITE(basic_tests),
CU_SUITE_INFO_NULL
};
int
main()
{
CU_initialize_registry();
CU_register_suites(test_suites);
CU_basic_set_mode(CU_BRM_NORMAL);
CU_basic_run_tests();
CU_cleanup_registry();
return 0;
}
void
shuffle(char **p, unsigned size)
{
for (unsigned i = 0; i < size - 1; i++) {
unsigned n = rand() % (size - i);
char *t = p[i+n]; p[i+n] = p[i]; p[i] = t;
}
}
static const struct key_info keys1[] = {
{ "d", "D", "d" },
{ "b", "B", "b" },
{ "a", "A", "a" },
{ "c", "C", "c" },
{ "g", "G", "g" },
{ "f", "F", "f" },
{ "h", "H", "h" },
{ "y", "Y", "y" },
{ "z", "Z", "z" },
{ "x", "X", "x" },
{ "j", "J", "j" },
{ "r", "R", "r" },
{ "q", "Q", "q" },
{ "p", "P", "p" },
{ "l", "L", "l" },
{ "m", "M", "m" },
{ "s", "S", "s" },
{ "t", "T", "t" },
{ "u", "U", "u" },
{ "da", "DA", "da" },
{ "ba", "BA", "ba" },
{ "aa", "AA", "aa" },
{ "ca", "CA", "ca" },
{ "ga", "GA", "ga" },
{ "fa", "FA", "fa" },
{ "ha", "HA", "ha" },
{ "ya", "YA", "ya" },
{ "za", "ZA", "za" },
{ "xa", "XA", "xa" },
{ "ja", "JA", "ja" },
{ "ra", "RA", "ra" },
{ "qa", "QA", "qa" },
{ "pa", "PA", "pa" },
{ "la", "LA", "la" },
{ "ma", "MA", "ma" },
{ "sa", "SA", "sa" },
{ "ta", "TA", "ta" },
{ "ua", "UA", "ua" },
};
#define NKEYS1 (sizeof(keys1) / sizeof(keys1[0]))
static const struct key_info keys2[] = {
{ "a", "A", "a" },
{ "aa", "AA", "aa" },
{ "b", "B", "b" },
{ "ba", "BA", "ba" },
{ "c", "C", "c" },
{ "ca", "CA", "ca" },
{ "d", "D", "d" },
{ "da", "DA", "da" },
{ "f", "F", "f" },
{ "fa", "FA", "fa" },
{ "g", "G", "g" },
{ "ga", "GA", "ga" },
{ "h", "H", "h" },
{ "ha", "HA", "ha" },
{ "j", "J", "j" },
{ "ja", "JA", "ja" },
{ "l", "L", "l" },
{ "la", "LA", "la" },
{ "m", "M", "m" },
{ "ma", "MA", "ma" },
{ "p", "P", "p" },
{ "pa", "PA", "pa" },
{ "q", "Q", "q" },
{ "qa", "QA", "qa" },
{ "r", "R", "r" },
{ "ra", "RA", "ra" },
{ "s", "S", "s" },
{ "sa", "SA", "sa" },
{ "t", "T", "t" },
{ "ta", "TA", "ta" },
{ "u", "U", "u" },
{ "ua", "UA", "ua" },
{ "x", "X", "x" },
{ "xa", "XA", "xa" },
{ "y", "Y", "y" },
{ "ya", "YA", "ya" },
{ "z", "Z", "z" },
{ "za", "ZA", "za" },
};
#define NKEYS2 (sizeof(keys2) / sizeof(keys2[0]))
void test_basic(dict *dct, const struct key_info *keys, const unsigned nkeys) {
CU_ASSERT_TRUE(dict_verify(dct));
for (unsigned i = 0; i < nkeys; ++i) {
void **datum_location = NULL;
CU_ASSERT_TRUE(dict_insert(dct, keys[i].key, &datum_location));
CU_ASSERT_PTR_NOT_NULL(datum_location);
*datum_location = keys[i].value;
CU_ASSERT_TRUE(dict_verify(dct));
for (unsigned j = 0; j <= i; ++j)
CU_ASSERT_EQUAL(dict_search(dct, keys[j].key), keys[j].value);
for (unsigned j = i + 1; j < nkeys; ++j)
CU_ASSERT_EQUAL(dict_search(dct, keys[j].key), NULL);
}
CU_ASSERT_EQUAL(dict_count(dct), nkeys);
if (dct->_vtable->insert == (dict_insert_func)hashtable_insert) {
/* Verify that hashtable_resize works as expected. */
dict *clone = dict_clone(dct, NULL);
CU_ASSERT_TRUE(dict_verify(dct));
CU_ASSERT_TRUE(hashtable_resize(dict_private(clone), 3));
CU_ASSERT_TRUE(dict_verify(dct));
for (unsigned j = 0; j < nkeys; ++j)
CU_ASSERT_EQUAL(dict_search(clone, keys[j].key), keys[j].value);
dict_free(clone);
}
if (dct->_vtable->clone) {
dict *clone = dict_clone(dct, NULL);
CU_ASSERT_PTR_NOT_NULL(clone);
CU_ASSERT_TRUE(dict_verify(clone));
CU_ASSERT_EQUAL(dict_count(clone), nkeys);
for (unsigned i = 0; i < nkeys; ++i) {
CU_ASSERT_EQUAL(dict_search(clone, keys[i].key), keys[i].value);
}
for (unsigned i = 0; i < nkeys; ++i) {
CU_ASSERT_TRUE(dict_remove(clone, keys[i].key));
}
dict_free(clone);
}
for (unsigned i = 0; i < nkeys; ++i)
CU_ASSERT_EQUAL(dict_search(dct, keys[i].key), keys[i].value);
for (unsigned i = 0; i < nkeys; ++i) {
void **datum_location = NULL;
CU_ASSERT_FALSE(dict_insert(dct, keys[i].key, &datum_location));
CU_ASSERT_PTR_NOT_NULL(datum_location);
CU_ASSERT_EQUAL(*datum_location, keys[i].value);
CU_ASSERT_TRUE(dict_verify(dct));
}
CU_ASSERT_EQUAL(dict_count(dct), nkeys);
dict_itor *itor = dict_itor_new(dct);
CU_ASSERT_PTR_NOT_NULL(itor);
char *last_key = NULL;
unsigned n = 0;
for (dict_itor_first(itor); dict_itor_valid(itor); dict_itor_next(itor)) {
CU_ASSERT_PTR_NOT_NULL(dict_itor_key(itor));
CU_ASSERT_PTR_NOT_NULL(dict_itor_data(itor));
++n;
if (dct->_vtable->insert != (dict_insert_func)hashtable_insert) {
if (last_key) {
CU_ASSERT_TRUE(strcmp(last_key, dict_itor_key(itor)) < 0);
}
last_key = dict_itor_key(itor);
}
}
CU_ASSERT_EQUAL(n, nkeys);
last_key = NULL;
n = 0;
for (dict_itor_last(itor); dict_itor_valid(itor); dict_itor_prev(itor)) {
CU_ASSERT_PTR_NOT_NULL(dict_itor_key(itor));
CU_ASSERT_PTR_NOT_NULL(dict_itor_data(itor));
++n;
if (dct->_vtable->insert != (dict_insert_func)hashtable_insert) {
if (last_key) {
CU_ASSERT_TRUE(strcmp(last_key, dict_itor_key(itor)) > 0);
}
last_key = dict_itor_key(itor);
}
}
CU_ASSERT_EQUAL(n, nkeys);
dict_itor_free(itor);
for (unsigned i = 0; i < nkeys; ++i) {
void **datum_location = NULL;
CU_ASSERT_FALSE(dict_insert(dct, keys[i].key, &datum_location));
CU_ASSERT_PTR_NOT_NULL(datum_location);
*datum_location = keys[i].alt;
CU_ASSERT_TRUE(dict_verify(dct));
}
CU_ASSERT_EQUAL(dict_count(dct), nkeys);
for (unsigned i = 0; i < nkeys; ++i)
CU_ASSERT_EQUAL(dict_search(dct, keys[i].key), keys[i].alt);
for (unsigned i = 0; i < nkeys; ++i) {
CU_ASSERT_EQUAL(dict_search(dct, keys[i].key), keys[i].alt);
CU_ASSERT_TRUE(dict_remove(dct, keys[i].key));
CU_ASSERT_TRUE(dict_verify(dct));
CU_ASSERT_EQUAL(dict_remove(dct, keys[i].key), false);
for (unsigned j = 0; j <= i; ++j) {
CU_ASSERT_EQUAL(dict_search(dct, keys[j].key), NULL);
}
for (unsigned j = i + 1; j < nkeys; ++j) {
CU_ASSERT_EQUAL(dict_search(dct, keys[j].key), keys[j].alt);
}
}
for (unsigned i = 0; i < nkeys; ++i) {
void **datum_location = NULL;
CU_ASSERT_TRUE(dict_insert(dct, keys[i].key, &datum_location));
CU_ASSERT_PTR_NOT_NULL(datum_location);
*datum_location = keys[i].value;
CU_ASSERT_TRUE(dict_verify(dct));
}
CU_ASSERT_EQUAL(dict_count(dct), nkeys);
CU_ASSERT_EQUAL(dict_clear(dct), nkeys);
for (unsigned i = 0; i < nkeys; ++i) {
void **datum_location = NULL;
CU_ASSERT_TRUE(dict_insert(dct, keys[i].key, &datum_location));
CU_ASSERT_PTR_NOT_NULL(datum_location);
*datum_location = keys[i].value;
CU_ASSERT_TRUE(dict_verify(dct));
}
CU_ASSERT_EQUAL(dict_count(dct), nkeys);
CU_ASSERT_EQUAL(dict_free(dct), nkeys);
}
unsigned
strhash(const void *p)
{
unsigned hash = 2166136261U;
for (const uint8_t *ptr = p; *ptr;) {
hash = (hash ^ *ptr++) * 16777619U;
}
return hash;
}
void test_basic_hashtable_1bucket()
{
test_basic(hashtable_dict_new(dict_str_cmp, strhash, NULL, 1),
keys1, NKEYS1);
test_basic(hashtable_dict_new(dict_str_cmp, strhash, NULL, 1),
keys2, NKEYS2);
}
void test_basic_hashtable_nbuckets()
{
test_basic(hashtable_dict_new(dict_str_cmp, strhash, NULL, 7),
keys1, NKEYS1);
test_basic(hashtable_dict_new(dict_str_cmp, strhash, NULL, 7),
keys2, NKEYS2);
}
void test_basic_height_balanced_tree()
{
test_basic(hb_dict_new(dict_str_cmp, NULL), keys1, NKEYS1);
test_basic(hb_dict_new(dict_str_cmp, NULL), keys2, NKEYS2);
}
void test_basic_path_reduction_tree()
{
test_basic(pr_dict_new(dict_str_cmp, NULL), keys1, NKEYS1);
test_basic(pr_dict_new(dict_str_cmp, NULL), keys2, NKEYS2);
}
void test_basic_red_black_tree()
{
test_basic(rb_dict_new(dict_str_cmp, NULL), keys1, NKEYS1);
test_basic(rb_dict_new(dict_str_cmp, NULL), keys2, NKEYS2);
}
void test_basic_skiplist()
{
test_basic(skiplist_dict_new(dict_str_cmp, NULL, 13), keys1, NKEYS1);
test_basic(skiplist_dict_new(dict_str_cmp, NULL, 13), keys2, NKEYS2);
}
void test_basic_splay_tree()
{
test_basic(sp_dict_new(dict_str_cmp, NULL), keys1, NKEYS1);
test_basic(sp_dict_new(dict_str_cmp, NULL), keys2, NKEYS2);
}
void test_basic_treap()
{
test_basic(tr_dict_new(dict_str_cmp, NULL, NULL), keys1, NKEYS1);
test_basic(tr_dict_new(dict_str_cmp, NULL, NULL), keys2, NKEYS2);
}
void test_basic_weight_balanced_tree()
{
test_basic(wb_dict_new(dict_str_cmp, NULL), keys1, NKEYS1);
test_basic(wb_dict_new(dict_str_cmp, NULL), keys2, NKEYS2);
}
void test_version_string()
{
char version_string[32];
snprintf(version_string, sizeof(version_string), "%d.%d.%d",
DICT_VERSION_MAJOR, DICT_VERSION_MINOR, DICT_VERSION_PATCH);
CU_ASSERT_STRING_EQUAL(kDictVersionString, version_string);
}