Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 580 lines (520 sloc) 17.922 kb
44b9934 Farooq Mela Initial commit.
authored
1 /* unit_tests.c
2 * Copyright (C) 2012 Farooq Mela. All rights reserved. */
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <stdarg.h>
8 #include <limits.h>
9 #include <float.h>
10 #include <time.h>
11
12 #include <CUnit/CUnit.h>
13 #include <CUnit/Basic.h>
14
15 #include "dict.h"
16
17 #define TEST_FUNC(func) { #func, func }
18
ff43c12 Farooq Mela Expand unit test data.
authored
19 struct key_info {
20 char *key, *value, *alt;
21 };
22
594fca6 Alexander Drozdov cunit: Add tests of closest search functions.
sorc1 authored
23 struct closest_lookup_info {
24 const char *key;
25 const char *le_key, *le_val;
26 const char *lt_key, *lt_val;
27 const char *ge_key, *ge_val;
28 const char *gt_key, *gt_val;
29 };
30
31 void test_basic(dict *dct, const struct key_info *keys, const unsigned nkeys,
32 const struct closest_lookup_info *cl_infos,
33 unsigned n_cl_infos);
cbf15a1 Farooq Mela More descriptive names for tests.
authored
34 void test_basic_hashtable_1bucket();
b5a4327 Farooq Mela New hash table implementation based on open addressing and linear probin...
authored
35 void test_basic_hashtable2_1bucket();
cbf15a1 Farooq Mela More descriptive names for tests.
authored
36 void test_basic_hashtable_nbuckets();
b5a4327 Farooq Mela New hash table implementation based on open addressing and linear probin...
authored
37 void test_basic_hashtable2_nbuckets();
cbf15a1 Farooq Mela More descriptive names for tests.
authored
38 void test_basic_height_balanced_tree();
39 void test_basic_path_reduction_tree();
40 void test_basic_red_black_tree();
41 void test_basic_skiplist();
42 void test_basic_splay_tree();
43 void test_basic_treap();
44 void test_basic_weight_balanced_tree();
ef8b363 Farooq Mela Add a version string constant with unit test.
authored
45 void test_version_string();
44b9934 Farooq Mela Initial commit.
authored
46
47 CU_TestInfo basic_tests[] = {
cbf15a1 Farooq Mela More descriptive names for tests.
authored
48 TEST_FUNC(test_basic_hashtable_1bucket),
b5a4327 Farooq Mela New hash table implementation based on open addressing and linear probin...
authored
49 TEST_FUNC(test_basic_hashtable2_1bucket),
cbf15a1 Farooq Mela More descriptive names for tests.
authored
50 TEST_FUNC(test_basic_hashtable_nbuckets),
b5a4327 Farooq Mela New hash table implementation based on open addressing and linear probin...
authored
51 TEST_FUNC(test_basic_hashtable2_nbuckets),
cbf15a1 Farooq Mela More descriptive names for tests.
authored
52 TEST_FUNC(test_basic_height_balanced_tree),
53 TEST_FUNC(test_basic_path_reduction_tree),
54 TEST_FUNC(test_basic_red_black_tree),
55 TEST_FUNC(test_basic_skiplist),
56 TEST_FUNC(test_basic_splay_tree),
57 TEST_FUNC(test_basic_treap),
58 TEST_FUNC(test_basic_weight_balanced_tree),
ef8b363 Farooq Mela Add a version string constant with unit test.
authored
59 TEST_FUNC(test_version_string),
44b9934 Farooq Mela Initial commit.
authored
60 CU_TEST_INFO_NULL
61 };
62
63 #define TEST_SUITE(suite) { #suite, NULL, NULL, suite }
64
65 CU_SuiteInfo test_suites[] = {
66 TEST_SUITE(basic_tests),
67 CU_SUITE_INFO_NULL
68 };
69
70 int
b223f49 Farooq Mela First set of unit tests.
authored
71 main()
44b9934 Farooq Mela Initial commit.
authored
72 {
73 CU_initialize_registry();
74 CU_register_suites(test_suites);
75 CU_basic_set_mode(CU_BRM_NORMAL);
76 CU_basic_run_tests();
77 CU_cleanup_registry();
78
79 return 0;
80 }
81
c36a5dd Farooq Mela Add more data for tests.
authored
82 void
83 shuffle(char **p, unsigned size)
84 {
85 for (unsigned i = 0; i < size - 1; i++) {
86 unsigned n = rand() % (size - i);
87 char *t = p[i+n]; p[i+n] = p[i]; p[i] = t;
88 }
89 }
90
ff43c12 Farooq Mela Expand unit test data.
authored
91 static const struct key_info keys1[] = {
b223f49 Farooq Mela First set of unit tests.
authored
92 { "d", "D", "d" },
93 { "b", "B", "b" },
94 { "a", "A", "a" },
95 { "c", "C", "c" },
96 { "g", "G", "g" },
97 { "f", "F", "f" },
98 { "h", "H", "h" },
99 { "y", "Y", "y" },
100 { "z", "Z", "z" },
101 { "x", "X", "x" },
c36a5dd Farooq Mela Add more data for tests.
authored
102 { "j", "J", "j" },
103 { "r", "R", "r" },
104 { "q", "Q", "q" },
105 { "p", "P", "p" },
106 { "l", "L", "l" },
107 { "m", "M", "m" },
108 { "s", "S", "s" },
109 { "t", "T", "t" },
110 { "u", "U", "u" },
ff43c12 Farooq Mela Expand unit test data.
authored
111 { "da", "DA", "da" },
112 { "ba", "BA", "ba" },
113 { "aa", "AA", "aa" },
114 { "ca", "CA", "ca" },
115 { "ga", "GA", "ga" },
116 { "fa", "FA", "fa" },
117 { "ha", "HA", "ha" },
118 { "ya", "YA", "ya" },
119 { "za", "ZA", "za" },
120 { "xa", "XA", "xa" },
121 { "ja", "JA", "ja" },
122 { "ra", "RA", "ra" },
123 { "qa", "QA", "qa" },
124 { "pa", "PA", "pa" },
125 { "la", "LA", "la" },
126 { "ma", "MA", "ma" },
127 { "sa", "SA", "sa" },
128 { "ta", "TA", "ta" },
129 { "ua", "UA", "ua" },
b223f49 Farooq Mela First set of unit tests.
authored
130 };
ff43c12 Farooq Mela Expand unit test data.
authored
131 #define NKEYS1 (sizeof(keys1) / sizeof(keys1[0]))
b223f49 Farooq Mela First set of unit tests.
authored
132
ff43c12 Farooq Mela Expand unit test data.
authored
133 static const struct key_info keys2[] = {
134 { "a", "A", "a" },
135 { "aa", "AA", "aa" },
136 { "b", "B", "b" },
137 { "ba", "BA", "ba" },
138 { "c", "C", "c" },
139 { "ca", "CA", "ca" },
140 { "d", "D", "d" },
141 { "da", "DA", "da" },
142 { "f", "F", "f" },
143 { "fa", "FA", "fa" },
144 { "g", "G", "g" },
145 { "ga", "GA", "ga" },
146 { "h", "H", "h" },
147 { "ha", "HA", "ha" },
148 { "j", "J", "j" },
149 { "ja", "JA", "ja" },
150 { "l", "L", "l" },
151 { "la", "LA", "la" },
152 { "m", "M", "m" },
153 { "ma", "MA", "ma" },
154 { "p", "P", "p" },
155 { "pa", "PA", "pa" },
156 { "q", "Q", "q" },
157 { "qa", "QA", "qa" },
158 { "r", "R", "r" },
159 { "ra", "RA", "ra" },
160 { "s", "S", "s" },
161 { "sa", "SA", "sa" },
162 { "t", "T", "t" },
163 { "ta", "TA", "ta" },
164 { "u", "U", "u" },
165 { "ua", "UA", "ua" },
166 { "x", "X", "x" },
167 { "xa", "XA", "xa" },
168 { "y", "Y", "y" },
169 { "ya", "YA", "ya" },
170 { "z", "Z", "z" },
171 { "za", "ZA", "za" },
172 };
173 #define NKEYS2 (sizeof(keys2) / sizeof(keys2[0]))
174
d2a5a82 Farooq Mela Fix shadowed variable warnings.
authored
175 static const struct closest_lookup_info closest_lookup_infos[] = {
594fca6 Alexander Drozdov cunit: Add tests of closest search functions.
sorc1 authored
176 {.key = "_",
177 .ge_key = "a", .ge_val = "A",
178 .gt_key = "a", .gt_val = "A"},
179 {.key = "a",
180 .le_key = "a", .le_val = "A",
181 .ge_key = "a", .ge_val = "A",
182 .gt_key = "aa", .gt_val = "AA"},
183 {.key = "aa",
184 .le_key = "aa", .le_val = "AA",
185 .lt_key = "a", .lt_val = "A",
186 .ge_key = "aa", .ge_val = "AA",
187 .gt_key = "b", .gt_val = "B"},
188 {.key = "ab",
189 .le_key = "aa", .le_val = "AA",
190 .lt_key = "aa", .lt_val = "AA",
191 .ge_key = "b", .ge_val = "B",
192 .gt_key = "b", .gt_val = "B"},
193 {.key = "m",
194 .le_key = "m", .le_val = "M",
195 .lt_key = "la", .lt_val = "LA",
196 .ge_key = "m", .ge_val = "M",
197 .gt_key = "ma", .gt_val = "MA"},
198 {.key = "n",
199 .le_key = "ma", .le_val = "MA",
200 .lt_key = "ma", .lt_val = "MA",
201 .ge_key = "p", .ge_val = "P",
202 .gt_key = "p", .gt_val = "P"},
203 {.key = "za",
204 .le_key = "za", .le_val = "ZA",
205 .lt_key = "z", .lt_val = "Z",
206 .ge_key = "za", .ge_val = "ZA"},
207 {.key = "zb",
208 .le_key = "za", .le_val = "ZA",
209 .lt_key = "za", .lt_val = "ZA"},
210 };
211
d2a5a82 Farooq Mela Fix shadowed variable warnings.
authored
212 #define NUM_CLOSEST_LOOKUP_INFOS (sizeof(closest_lookup_infos) / sizeof(closest_lookup_infos[0]))
594fca6 Alexander Drozdov cunit: Add tests of closest search functions.
sorc1 authored
213
4e6823f Alexander Drozdov cunit: add dict_itor_search() tests
sorc1 authored
214 void
215 test_search(dict *dct, dict_itor *itor, const char *key, const char *value)
216 {
217 CU_ASSERT_EQUAL(dict_search(dct, key), value);
218 if (itor != NULL) {
219 if (value == NULL) {
220 CU_ASSERT_EQUAL(dict_itor_search(itor, key), false);
221 } else {
222 CU_ASSERT_EQUAL(dict_itor_search(itor, key), true);
223 CU_ASSERT_EQUAL(dict_itor_key(itor), key);
224 CU_ASSERT_EQUAL(*dict_itor_data(itor), value);
225 }
226 }
227 }
228
594fca6 Alexander Drozdov cunit: Add tests of closest search functions.
sorc1 authored
229 static void
6a9aff6 Farooq Mela Style/formatting fixes.
authored
230 test_closest_lookup(dict *dct, const struct closest_lookup_info *cl_infos, unsigned n_cl_infos)
594fca6 Alexander Drozdov cunit: Add tests of closest search functions.
sorc1 authored
231 {
5607394 Farooq Mela Add dict_has_near_search macro to test if a dict supports search le/lt/g...
authored
232 if (!dict_has_near_search(dct))
594fca6 Alexander Drozdov cunit: Add tests of closest search functions.
sorc1 authored
233 return;
234
6a9aff6 Farooq Mela Style/formatting fixes.
authored
235 dict_itor* itor = dict_itor_new(dct);
236 for (unsigned i = 0; i < n_cl_infos; i++) {
594fca6 Alexander Drozdov cunit: Add tests of closest search functions.
sorc1 authored
237 if (cl_infos[i].le_key) {
238 CU_ASSERT_STRING_EQUAL(dict_search_le(dct, cl_infos[i].key),
239 cl_infos[i].le_val);
240 CU_ASSERT_EQUAL(dict_itor_search_le(itor, cl_infos[i].key), true);
241 CU_ASSERT_STRING_EQUAL(dict_itor_key(itor), cl_infos[i].le_key);
242 CU_ASSERT_STRING_EQUAL(*dict_itor_data(itor), cl_infos[i].le_val);
243 } else {
244 CU_ASSERT_PTR_NULL(dict_search_le(dct, cl_infos[i].key));
245 CU_ASSERT_EQUAL(dict_itor_search_le(itor, cl_infos[i].key), false);
246 CU_ASSERT_PTR_NULL(dict_itor_key(itor));
247 CU_ASSERT_PTR_NULL(dict_itor_data(itor));
248 }
249 if (cl_infos[i].lt_key) {
250 CU_ASSERT_STRING_EQUAL(dict_search_lt(dct, cl_infos[i].key),
251 cl_infos[i].lt_val);
252 CU_ASSERT_EQUAL(dict_itor_search_lt(itor, cl_infos[i].key), true);
253 CU_ASSERT_STRING_EQUAL(dict_itor_key(itor), cl_infos[i].lt_key);
254 CU_ASSERT_STRING_EQUAL(*dict_itor_data(itor), cl_infos[i].lt_val);
255 } else {
256 CU_ASSERT_PTR_NULL(dict_search_lt(dct, cl_infos[i].key));
257 CU_ASSERT_EQUAL(dict_itor_search_lt(itor, cl_infos[i].key), false);
258 CU_ASSERT_PTR_NULL(dict_itor_key(itor));
259 CU_ASSERT_PTR_NULL(dict_itor_data(itor));
260 }
261 if (cl_infos[i].ge_key) {
262 CU_ASSERT_STRING_EQUAL(dict_search_ge(dct, cl_infos[i].key),
263 cl_infos[i].ge_val);
264 CU_ASSERT_EQUAL(dict_itor_search_ge(itor, cl_infos[i].key), true);
265 CU_ASSERT_STRING_EQUAL(dict_itor_key(itor), cl_infos[i].ge_key);
266 CU_ASSERT_STRING_EQUAL(*dict_itor_data(itor), cl_infos[i].ge_val);
267 } else {
268 CU_ASSERT_PTR_NULL(dict_search_ge(dct, cl_infos[i].key));
269 CU_ASSERT_EQUAL(dict_itor_search_ge(itor, cl_infos[i].key), false);
270 CU_ASSERT_PTR_NULL(dict_itor_key(itor));
271 CU_ASSERT_PTR_NULL(dict_itor_data(itor));
272 }
273 if (cl_infos[i].gt_key) {
274 CU_ASSERT_STRING_EQUAL(dict_search_gt(dct, cl_infos[i].key),
275 cl_infos[i].gt_val);
276 CU_ASSERT_EQUAL(dict_itor_search_gt(itor, cl_infos[i].key), true);
277 CU_ASSERT_STRING_EQUAL(dict_itor_key(itor), cl_infos[i].gt_key);
278 CU_ASSERT_STRING_EQUAL(*dict_itor_data(itor), cl_infos[i].gt_val);
279 } else {
280 CU_ASSERT_PTR_NULL(dict_search_gt(dct, cl_infos[i].key));
281 CU_ASSERT_EQUAL(dict_itor_search_gt(itor, cl_infos[i].key), false);
282 CU_ASSERT_PTR_NULL(dict_itor_key(itor));
283 CU_ASSERT_PTR_NULL(dict_itor_data(itor));
284 }
285 }
286 dict_itor_free(itor);
287 }
288
289 void test_basic(dict *dct, const struct key_info *keys, const unsigned nkeys,
290 const struct closest_lookup_info *cl_infos,
291 unsigned n_cl_infos) {
4e6823f Alexander Drozdov cunit: add dict_itor_search() tests
sorc1 authored
292 dict_itor *itor = dict_itor_new(dct);
293
a8ecf00 Farooq Mela Verification returns a boolean indicating success or failure, rather tha...
authored
294 CU_ASSERT_TRUE(dict_verify(dct));
b7c78c8 Farooq Mela Implement verification functions for all containers.
authored
295
ff43c12 Farooq Mela Expand unit test data.
authored
296 for (unsigned i = 0; i < nkeys; ++i) {
b27c1ed Farooq Mela Change signature of insert method.
authored
297 bool inserted = false;
298 void **datum_location = dict_insert(dct, keys[i].key, &inserted);
299 CU_ASSERT_TRUE(inserted);
e6a2847 Farooq Mela Change insert interface to be more useful (also eliminates the need for ...
authored
300 CU_ASSERT_PTR_NOT_NULL(datum_location);
b27c1ed Farooq Mela Change signature of insert method.
authored
301 CU_ASSERT_PTR_NULL(*datum_location);
e6a2847 Farooq Mela Change insert interface to be more useful (also eliminates the need for ...
authored
302 *datum_location = keys[i].value;
b7c78c8 Farooq Mela Implement verification functions for all containers.
authored
303
a8ecf00 Farooq Mela Verification returns a boolean indicating success or failure, rather tha...
authored
304 CU_ASSERT_TRUE(dict_verify(dct));
1e4067d Farooq Mela Test that search works as expected after each insert.
authored
305
306 for (unsigned j = 0; j <= i; ++j)
4e6823f Alexander Drozdov cunit: add dict_itor_search() tests
sorc1 authored
307 test_search(dct, itor, keys[j].key, keys[j].value);
1e4067d Farooq Mela Test that search works as expected after each insert.
authored
308 for (unsigned j = i + 1; j < nkeys; ++j)
4e6823f Alexander Drozdov cunit: add dict_itor_search() tests
sorc1 authored
309 test_search(dct, itor, keys[j].key, NULL);
e6a2847 Farooq Mela Change insert interface to be more useful (also eliminates the need for ...
authored
310 }
ff43c12 Farooq Mela Expand unit test data.
authored
311 CU_ASSERT_EQUAL(dict_count(dct), nkeys);
b223f49 Farooq Mela First set of unit tests.
authored
312
b5a4327 Farooq Mela New hash table implementation based on open addressing and linear probin...
authored
313 if (dct->_vtable->insert == (dict_insert_func)hashtable_insert ||
314 dct->_vtable->insert == (dict_insert_func)hashtable2_insert) {
34e37e8 Farooq Mela Implement clone functionality for all dictionaries.
authored
315 /* Verify that hashtable_resize works as expected. */
316 dict *clone = dict_clone(dct, NULL);
317 CU_ASSERT_TRUE(dict_verify(dct));
b5a4327 Farooq Mela New hash table implementation based on open addressing and linear probin...
authored
318 if (dct->_vtable->insert == (dict_insert_func)hashtable_insert) {
319 CU_ASSERT_TRUE(hashtable_resize(dict_private(clone), 3));
320 } else {
321 CU_ASSERT_TRUE(hashtable2_resize(dict_private(clone), 3));
322 }
34e37e8 Farooq Mela Implement clone functionality for all dictionaries.
authored
323 CU_ASSERT_TRUE(dict_verify(dct));
324 for (unsigned j = 0; j < nkeys; ++j)
4e6823f Alexander Drozdov cunit: add dict_itor_search() tests
sorc1 authored
325 test_search(clone, NULL, keys[j].key, keys[j].value);
34e37e8 Farooq Mela Implement clone functionality for all dictionaries.
authored
326 dict_free(clone);
327 }
328
329 if (dct->_vtable->clone) {
330 dict *clone = dict_clone(dct, NULL);
331 CU_ASSERT_PTR_NOT_NULL(clone);
332 CU_ASSERT_TRUE(dict_verify(clone));
333 CU_ASSERT_EQUAL(dict_count(clone), nkeys);
334 for (unsigned i = 0; i < nkeys; ++i) {
4e6823f Alexander Drozdov cunit: add dict_itor_search() tests
sorc1 authored
335 test_search(clone, itor, keys[i].key, keys[i].value);
34e37e8 Farooq Mela Implement clone functionality for all dictionaries.
authored
336 }
337 for (unsigned i = 0; i < nkeys; ++i) {
338 CU_ASSERT_TRUE(dict_remove(clone, keys[i].key));
339 }
340 dict_free(clone);
341 }
342
ff43c12 Farooq Mela Expand unit test data.
authored
343 for (unsigned i = 0; i < nkeys; ++i)
4e6823f Alexander Drozdov cunit: add dict_itor_search() tests
sorc1 authored
344 test_search(dct, itor, keys[i].key, keys[i].value);
b223f49 Farooq Mela First set of unit tests.
authored
345
ff43c12 Farooq Mela Expand unit test data.
authored
346 for (unsigned i = 0; i < nkeys; ++i) {
b27c1ed Farooq Mela Change signature of insert method.
authored
347 bool inserted = false;
348 void **datum_location = dict_insert(dct, keys[i].key, &inserted);
349 CU_ASSERT_FALSE(inserted);
e6a2847 Farooq Mela Change insert interface to be more useful (also eliminates the need for ...
authored
350 CU_ASSERT_PTR_NOT_NULL(datum_location);
1e4067d Farooq Mela Test that search works as expected after each insert.
authored
351 CU_ASSERT_EQUAL(*datum_location, keys[i].value);
b7c78c8 Farooq Mela Implement verification functions for all containers.
authored
352
a8ecf00 Farooq Mela Verification returns a boolean indicating success or failure, rather tha...
authored
353 CU_ASSERT_TRUE(dict_verify(dct));
e6a2847 Farooq Mela Change insert interface to be more useful (also eliminates the need for ...
authored
354 }
ff43c12 Farooq Mela Expand unit test data.
authored
355 CU_ASSERT_EQUAL(dict_count(dct), nkeys);
b223f49 Farooq Mela First set of unit tests.
authored
356
68a918f Farooq Mela Add basic iterator tests.
authored
357 CU_ASSERT_PTR_NOT_NULL(itor);
358 char *last_key = NULL;
359 unsigned n = 0;
360 for (dict_itor_first(itor); dict_itor_valid(itor); dict_itor_next(itor)) {
361 CU_ASSERT_PTR_NOT_NULL(dict_itor_key(itor));
362 CU_ASSERT_PTR_NOT_NULL(dict_itor_data(itor));
b27c1ed Farooq Mela Change signature of insert method.
authored
363 CU_ASSERT_PTR_NOT_NULL(*dict_itor_data(itor));
cae47e3 Farooq Mela Better iterator verification in unit tests.
authored
364
365 char *key = dict_itor_key(itor);
366 bool key_matched = false;
367 for (unsigned i = 0; i < nkeys; ++i) {
368 if (keys[i].key == key) {
369 CU_ASSERT_EQUAL(*dict_itor_data(itor), keys[i].value);
370 key_matched = true;
371 break;
372 }
373 }
374 CU_ASSERT_TRUE(key_matched);
375
b5a4327 Farooq Mela New hash table implementation based on open addressing and linear probin...
authored
376 if (dct->_vtable->insert != (dict_insert_func)hashtable_insert &&
377 dct->_vtable->insert != (dict_insert_func)hashtable2_insert) {
68a918f Farooq Mela Add basic iterator tests.
authored
378 if (last_key) {
379 CU_ASSERT_TRUE(strcmp(last_key, dict_itor_key(itor)) < 0);
380 }
381 last_key = dict_itor_key(itor);
382 }
cae47e3 Farooq Mela Better iterator verification in unit tests.
authored
383
384 ++n;
68a918f Farooq Mela Add basic iterator tests.
authored
385 }
ff43c12 Farooq Mela Expand unit test data.
authored
386 CU_ASSERT_EQUAL(n, nkeys);
68a918f Farooq Mela Add basic iterator tests.
authored
387 last_key = NULL;
388 n = 0;
389 for (dict_itor_last(itor); dict_itor_valid(itor); dict_itor_prev(itor)) {
390 CU_ASSERT_PTR_NOT_NULL(dict_itor_key(itor));
391 CU_ASSERT_PTR_NOT_NULL(dict_itor_data(itor));
b27c1ed Farooq Mela Change signature of insert method.
authored
392 CU_ASSERT_PTR_NOT_NULL(*dict_itor_data(itor));
cae47e3 Farooq Mela Better iterator verification in unit tests.
authored
393
394 char *key = dict_itor_key(itor);
395 bool key_matched = false;
396 for (unsigned i = 0; i < nkeys; ++i) {
397 if (keys[i].key == key) {
398 CU_ASSERT_EQUAL(*dict_itor_data(itor), keys[i].value);
399 key_matched = true;
400 break;
401 }
402 }
403 CU_ASSERT_TRUE(key_matched);
404
b5a4327 Farooq Mela New hash table implementation based on open addressing and linear probin...
authored
405 if (dct->_vtable->insert != (dict_insert_func)hashtable_insert &&
406 dct->_vtable->insert != (dict_insert_func)hashtable2_insert) {
68a918f Farooq Mela Add basic iterator tests.
authored
407 if (last_key) {
408 CU_ASSERT_TRUE(strcmp(last_key, dict_itor_key(itor)) > 0);
409 }
410 last_key = dict_itor_key(itor);
411 }
cae47e3 Farooq Mela Better iterator verification in unit tests.
authored
412
413 ++n;
68a918f Farooq Mela Add basic iterator tests.
authored
414 }
ff43c12 Farooq Mela Expand unit test data.
authored
415 CU_ASSERT_EQUAL(n, nkeys);
68a918f Farooq Mela Add basic iterator tests.
authored
416
ff43c12 Farooq Mela Expand unit test data.
authored
417 for (unsigned i = 0; i < nkeys; ++i) {
b27c1ed Farooq Mela Change signature of insert method.
authored
418 bool inserted = false;
419 void **datum_location = dict_insert(dct, keys[i].key, &inserted);
420 CU_ASSERT_FALSE(inserted);
e6a2847 Farooq Mela Change insert interface to be more useful (also eliminates the need for ...
authored
421 CU_ASSERT_PTR_NOT_NULL(datum_location);
b27c1ed Farooq Mela Change signature of insert method.
authored
422 CU_ASSERT_PTR_NOT_NULL(*datum_location);
e6a2847 Farooq Mela Change insert interface to be more useful (also eliminates the need for ...
authored
423 *datum_location = keys[i].alt;
b7c78c8 Farooq Mela Implement verification functions for all containers.
authored
424
a8ecf00 Farooq Mela Verification returns a boolean indicating success or failure, rather tha...
authored
425 CU_ASSERT_TRUE(dict_verify(dct));
e6a2847 Farooq Mela Change insert interface to be more useful (also eliminates the need for ...
authored
426 }
ff43c12 Farooq Mela Expand unit test data.
authored
427 CU_ASSERT_EQUAL(dict_count(dct), nkeys);
b223f49 Farooq Mela First set of unit tests.
authored
428
ff43c12 Farooq Mela Expand unit test data.
authored
429 for (unsigned i = 0; i < nkeys; ++i)
4e6823f Alexander Drozdov cunit: add dict_itor_search() tests
sorc1 authored
430 test_search(dct, itor, keys[i].key, keys[i].alt);
b223f49 Farooq Mela First set of unit tests.
authored
431
ff43c12 Farooq Mela Expand unit test data.
authored
432 for (unsigned i = 0; i < nkeys; ++i) {
4e6823f Alexander Drozdov cunit: add dict_itor_search() tests
sorc1 authored
433 test_search(dct, itor, keys[i].key, keys[i].alt);
34e37e8 Farooq Mela Implement clone functionality for all dictionaries.
authored
434 CU_ASSERT_TRUE(dict_remove(dct, keys[i].key));
a8ecf00 Farooq Mela Verification returns a boolean indicating success or failure, rather tha...
authored
435 CU_ASSERT_TRUE(dict_verify(dct));
1e4067d Farooq Mela Test that search works as expected after each insert.
authored
436
b223f49 Farooq Mela First set of unit tests.
authored
437 CU_ASSERT_EQUAL(dict_remove(dct, keys[i].key), false);
1e4067d Farooq Mela Test that search works as expected after each insert.
authored
438 for (unsigned j = 0; j <= i; ++j) {
4e6823f Alexander Drozdov cunit: add dict_itor_search() tests
sorc1 authored
439 test_search(dct, itor, keys[j].key, NULL);
1e4067d Farooq Mela Test that search works as expected after each insert.
authored
440 }
ff43c12 Farooq Mela Expand unit test data.
authored
441 for (unsigned j = i + 1; j < nkeys; ++j) {
4e6823f Alexander Drozdov cunit: add dict_itor_search() tests
sorc1 authored
442 test_search(dct, itor, keys[j].key, keys[j].alt);
b223f49 Farooq Mela First set of unit tests.
authored
443 }
444 }
445
ff43c12 Farooq Mela Expand unit test data.
authored
446 for (unsigned i = 0; i < nkeys; ++i) {
b27c1ed Farooq Mela Change signature of insert method.
authored
447 bool inserted = false;
448 void **datum_location = dict_insert(dct, keys[i].key, &inserted);
449 CU_ASSERT_TRUE(inserted);
ff43c12 Farooq Mela Expand unit test data.
authored
450 CU_ASSERT_PTR_NOT_NULL(datum_location);
b27c1ed Farooq Mela Change signature of insert method.
authored
451 CU_ASSERT_PTR_NULL(*datum_location);
ff43c12 Farooq Mela Expand unit test data.
authored
452 *datum_location = keys[i].value;
453
a8ecf00 Farooq Mela Verification returns a boolean indicating success or failure, rather tha...
authored
454 CU_ASSERT_TRUE(dict_verify(dct));
ff43c12 Farooq Mela Expand unit test data.
authored
455 }
456 CU_ASSERT_EQUAL(dict_count(dct), nkeys);
457 CU_ASSERT_EQUAL(dict_clear(dct), nkeys);
458
459 for (unsigned i = 0; i < nkeys; ++i) {
b27c1ed Farooq Mela Change signature of insert method.
authored
460 bool inserted = false;
461 void **datum_location = dict_insert(dct, keys[i].key, &inserted);
462 CU_ASSERT_TRUE(inserted);
ff43c12 Farooq Mela Expand unit test data.
authored
463 CU_ASSERT_PTR_NOT_NULL(datum_location);
b27c1ed Farooq Mela Change signature of insert method.
authored
464 CU_ASSERT_PTR_NULL(*datum_location);
ff43c12 Farooq Mela Expand unit test data.
authored
465 *datum_location = keys[i].value;
466
a8ecf00 Farooq Mela Verification returns a boolean indicating success or failure, rather tha...
authored
467 CU_ASSERT_TRUE(dict_verify(dct));
ff43c12 Farooq Mela Expand unit test data.
authored
468 }
594fca6 Alexander Drozdov cunit: Add tests of closest search functions.
sorc1 authored
469 test_closest_lookup(dct, cl_infos, n_cl_infos);
4e6823f Alexander Drozdov cunit: add dict_itor_search() tests
sorc1 authored
470 dict_itor_free(itor);
ff43c12 Farooq Mela Expand unit test data.
authored
471 CU_ASSERT_EQUAL(dict_count(dct), nkeys);
472 CU_ASSERT_EQUAL(dict_free(dct), nkeys);
b223f49 Farooq Mela First set of unit tests.
authored
473 }
474
475 unsigned
476 strhash(const void *p)
477 {
478 unsigned hash = 2166136261U;
479 for (const uint8_t *ptr = p; *ptr;) {
480 hash = (hash ^ *ptr++) * 16777619U;
481 }
482 return hash;
483 }
e14d859 Farooq Mela Format.
authored
484
cbf15a1 Farooq Mela More descriptive names for tests.
authored
485 void test_basic_hashtable_1bucket()
b223f49 Farooq Mela First set of unit tests.
authored
486 {
ff43c12 Farooq Mela Expand unit test data.
authored
487 test_basic(hashtable_dict_new(dict_str_cmp, strhash, NULL, 1),
d2a5a82 Farooq Mela Fix shadowed variable warnings.
authored
488 keys1, NKEYS1, closest_lookup_infos, NUM_CLOSEST_LOOKUP_INFOS);
ff43c12 Farooq Mela Expand unit test data.
authored
489 test_basic(hashtable_dict_new(dict_str_cmp, strhash, NULL, 1),
d2a5a82 Farooq Mela Fix shadowed variable warnings.
authored
490 keys2, NKEYS2, closest_lookup_infos, NUM_CLOSEST_LOOKUP_INFOS);
b223f49 Farooq Mela First set of unit tests.
authored
491 }
492
b5a4327 Farooq Mela New hash table implementation based on open addressing and linear probin...
authored
493 void test_basic_hashtable2_1bucket()
494 {
495 test_basic(hashtable2_dict_new(dict_str_cmp, strhash, NULL, 1),
496 keys1, NKEYS1, closest_lookup_infos, NUM_CLOSEST_LOOKUP_INFOS);
497 test_basic(hashtable2_dict_new(dict_str_cmp, strhash, NULL, 1),
498 keys2, NKEYS2, closest_lookup_infos, NUM_CLOSEST_LOOKUP_INFOS);
499 }
500
cbf15a1 Farooq Mela More descriptive names for tests.
authored
501 void test_basic_hashtable_nbuckets()
b223f49 Farooq Mela First set of unit tests.
authored
502 {
ff43c12 Farooq Mela Expand unit test data.
authored
503 test_basic(hashtable_dict_new(dict_str_cmp, strhash, NULL, 7),
d2a5a82 Farooq Mela Fix shadowed variable warnings.
authored
504 keys1, NKEYS1, closest_lookup_infos, NUM_CLOSEST_LOOKUP_INFOS);
ff43c12 Farooq Mela Expand unit test data.
authored
505 test_basic(hashtable_dict_new(dict_str_cmp, strhash, NULL, 7),
d2a5a82 Farooq Mela Fix shadowed variable warnings.
authored
506 keys2, NKEYS2, closest_lookup_infos, NUM_CLOSEST_LOOKUP_INFOS);
b223f49 Farooq Mela First set of unit tests.
authored
507 }
508
b5a4327 Farooq Mela New hash table implementation based on open addressing and linear probin...
authored
509 void test_basic_hashtable2_nbuckets()
510 {
511 test_basic(hashtable2_dict_new(dict_str_cmp, strhash, NULL, 7),
512 keys1, NKEYS1, closest_lookup_infos, NUM_CLOSEST_LOOKUP_INFOS);
513 test_basic(hashtable2_dict_new(dict_str_cmp, strhash, NULL, 7),
514 keys2, NKEYS2, closest_lookup_infos, NUM_CLOSEST_LOOKUP_INFOS);
515 }
516
cbf15a1 Farooq Mela More descriptive names for tests.
authored
517 void test_basic_height_balanced_tree()
b223f49 Farooq Mela First set of unit tests.
authored
518 {
594fca6 Alexander Drozdov cunit: Add tests of closest search functions.
sorc1 authored
519 test_basic(hb_dict_new(dict_str_cmp, NULL), keys1, NKEYS1,
d2a5a82 Farooq Mela Fix shadowed variable warnings.
authored
520 closest_lookup_infos, NUM_CLOSEST_LOOKUP_INFOS);
594fca6 Alexander Drozdov cunit: Add tests of closest search functions.
sorc1 authored
521 test_basic(hb_dict_new(dict_str_cmp, NULL), keys2, NKEYS2,
d2a5a82 Farooq Mela Fix shadowed variable warnings.
authored
522 closest_lookup_infos, NUM_CLOSEST_LOOKUP_INFOS);
b223f49 Farooq Mela First set of unit tests.
authored
523 }
524
cbf15a1 Farooq Mela More descriptive names for tests.
authored
525 void test_basic_path_reduction_tree()
b223f49 Farooq Mela First set of unit tests.
authored
526 {
594fca6 Alexander Drozdov cunit: Add tests of closest search functions.
sorc1 authored
527 test_basic(pr_dict_new(dict_str_cmp, NULL), keys1, NKEYS1,
d2a5a82 Farooq Mela Fix shadowed variable warnings.
authored
528 closest_lookup_infos, NUM_CLOSEST_LOOKUP_INFOS);
594fca6 Alexander Drozdov cunit: Add tests of closest search functions.
sorc1 authored
529 test_basic(pr_dict_new(dict_str_cmp, NULL), keys2, NKEYS2,
d2a5a82 Farooq Mela Fix shadowed variable warnings.
authored
530 closest_lookup_infos, NUM_CLOSEST_LOOKUP_INFOS);
b223f49 Farooq Mela First set of unit tests.
authored
531 }
532
cbf15a1 Farooq Mela More descriptive names for tests.
authored
533 void test_basic_red_black_tree()
b223f49 Farooq Mela First set of unit tests.
authored
534 {
594fca6 Alexander Drozdov cunit: Add tests of closest search functions.
sorc1 authored
535 test_basic(rb_dict_new(dict_str_cmp, NULL), keys1, NKEYS1,
d2a5a82 Farooq Mela Fix shadowed variable warnings.
authored
536 closest_lookup_infos, NUM_CLOSEST_LOOKUP_INFOS);
594fca6 Alexander Drozdov cunit: Add tests of closest search functions.
sorc1 authored
537 test_basic(rb_dict_new(dict_str_cmp, NULL), keys2, NKEYS2,
d2a5a82 Farooq Mela Fix shadowed variable warnings.
authored
538 closest_lookup_infos, NUM_CLOSEST_LOOKUP_INFOS);
b223f49 Farooq Mela First set of unit tests.
authored
539 }
540
cbf15a1 Farooq Mela More descriptive names for tests.
authored
541 void test_basic_skiplist()
e6a2847 Farooq Mela Change insert interface to be more useful (also eliminates the need for ...
authored
542 {
594fca6 Alexander Drozdov cunit: Add tests of closest search functions.
sorc1 authored
543 test_basic(skiplist_dict_new(dict_str_cmp, NULL, 13), keys1, NKEYS1,
d2a5a82 Farooq Mela Fix shadowed variable warnings.
authored
544 closest_lookup_infos, NUM_CLOSEST_LOOKUP_INFOS);
594fca6 Alexander Drozdov cunit: Add tests of closest search functions.
sorc1 authored
545 test_basic(skiplist_dict_new(dict_str_cmp, NULL, 13), keys2, NKEYS2,
d2a5a82 Farooq Mela Fix shadowed variable warnings.
authored
546 closest_lookup_infos, NUM_CLOSEST_LOOKUP_INFOS);
e6a2847 Farooq Mela Change insert interface to be more useful (also eliminates the need for ...
authored
547 }
548
cbf15a1 Farooq Mela More descriptive names for tests.
authored
549 void test_basic_splay_tree()
b223f49 Farooq Mela First set of unit tests.
authored
550 {
594fca6 Alexander Drozdov cunit: Add tests of closest search functions.
sorc1 authored
551 test_basic(sp_dict_new(dict_str_cmp, NULL), keys1, NKEYS1,
d2a5a82 Farooq Mela Fix shadowed variable warnings.
authored
552 closest_lookup_infos, NUM_CLOSEST_LOOKUP_INFOS);
594fca6 Alexander Drozdov cunit: Add tests of closest search functions.
sorc1 authored
553 test_basic(sp_dict_new(dict_str_cmp, NULL), keys2, NKEYS2,
d2a5a82 Farooq Mela Fix shadowed variable warnings.
authored
554 closest_lookup_infos, NUM_CLOSEST_LOOKUP_INFOS);
b223f49 Farooq Mela First set of unit tests.
authored
555 }
556
cbf15a1 Farooq Mela More descriptive names for tests.
authored
557 void test_basic_treap()
b223f49 Farooq Mela First set of unit tests.
authored
558 {
594fca6 Alexander Drozdov cunit: Add tests of closest search functions.
sorc1 authored
559 test_basic(tr_dict_new(dict_str_cmp, NULL, NULL), keys1, NKEYS1,
d2a5a82 Farooq Mela Fix shadowed variable warnings.
authored
560 closest_lookup_infos, NUM_CLOSEST_LOOKUP_INFOS);
594fca6 Alexander Drozdov cunit: Add tests of closest search functions.
sorc1 authored
561 test_basic(tr_dict_new(dict_str_cmp, NULL, NULL), keys2, NKEYS2,
d2a5a82 Farooq Mela Fix shadowed variable warnings.
authored
562 closest_lookup_infos, NUM_CLOSEST_LOOKUP_INFOS);
b223f49 Farooq Mela First set of unit tests.
authored
563 }
564
cbf15a1 Farooq Mela More descriptive names for tests.
authored
565 void test_basic_weight_balanced_tree()
44b9934 Farooq Mela Initial commit.
authored
566 {
594fca6 Alexander Drozdov cunit: Add tests of closest search functions.
sorc1 authored
567 test_basic(wb_dict_new(dict_str_cmp, NULL), keys1, NKEYS1,
d2a5a82 Farooq Mela Fix shadowed variable warnings.
authored
568 closest_lookup_infos, NUM_CLOSEST_LOOKUP_INFOS);
594fca6 Alexander Drozdov cunit: Add tests of closest search functions.
sorc1 authored
569 test_basic(wb_dict_new(dict_str_cmp, NULL), keys2, NKEYS2,
d2a5a82 Farooq Mela Fix shadowed variable warnings.
authored
570 closest_lookup_infos, NUM_CLOSEST_LOOKUP_INFOS);
44b9934 Farooq Mela Initial commit.
authored
571 }
ef8b363 Farooq Mela Add a version string constant with unit test.
authored
572
573 void test_version_string()
574 {
575 char version_string[32];
576 snprintf(version_string, sizeof(version_string), "%d.%d.%d",
577 DICT_VERSION_MAJOR, DICT_VERSION_MINOR, DICT_VERSION_PATCH);
578 CU_ASSERT_STRING_EQUAL(kDictVersionString, version_string);
579 }
Something went wrong with that request. Please try again.