2 * TSM - Hashtable Tests
4 * Copyright (c) 2012-2013 David Herrmann <dh.herrmann@gmail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files
8 * (the "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include "test_common.h"
29 static struct shl_htable ht = SHL_HTABLE_INIT_STR(ht);
30 static struct shl_htable uht = SHL_HTABLE_INIT_ULONG(uht);
33 char huge_padding[16384];
35 char paaaaaadding[16384];
38 char more_padding[32768];
42 #define to_node(_key) shl_htable_offsetof((_key), struct node, key)
43 #define ul_to_node(_key) shl_htable_offsetof((_key), struct node, ul)
45 static struct node o[] = {
46 { .v = 0, .key = "o0", .ul = 0 },
47 { .v = 1, .key = "o1", .ul = 1 },
48 { .v = 2, .key = "o2", .ul = 2 },
49 { .v = 3, .key = "o3", .ul = 3 },
50 { .v = 4, .key = "o4", .ul = 4 },
51 { .v = 5, .key = "o5", .ul = 5 },
52 { .v = 6, .key = "o6", .ul = 6 },
53 { .v = 7, .key = "o7", .ul = 7 },
56 static void test_htable_str_cb(char **k, void *ctx)
60 ck_assert(to_node(k)->v == to_node(k)->ul);
64 START_TEST(test_htable_str)
70 /* insert once, remove once, try removing again */
72 ck_assert(!o[0].hash);
73 r = shl_htable_insert_str(&ht, &o[0].key, &o[0].hash);
75 ck_assert(o[0].hash == shl_htable_rehash_str(&o[0].key, NULL));
77 b = shl_htable_remove_str(&ht, o[0].key, &o[0].hash, &k);
80 ck_assert(to_node(k)->v == 0);
83 b = shl_htable_remove_str(&ht, o[0].key, &o[0].hash, &k);
87 /* insert twice, remove twice, try removing again */
89 r = shl_htable_insert_str(&ht, &o[0].key, &o[0].hash);
91 ck_assert(o[0].hash == shl_htable_rehash_str(&o[0].key, NULL));
93 r = shl_htable_insert_str(&ht, &o[0].key, &o[0].hash);
95 ck_assert(o[0].hash == shl_htable_rehash_str(&o[0].key, NULL));
97 b = shl_htable_remove_str(&ht, o[0].key, &o[0].hash, &k);
100 ck_assert(to_node(k)->v == 0);
102 b = shl_htable_remove_str(&ht, o[0].key, &o[0].hash, &k);
104 ck_assert(k != NULL);
105 ck_assert(to_node(k)->v == 0);
108 b = shl_htable_remove_str(&ht, o[0].key, &o[0].hash, &k);
110 ck_assert(k == NULL);
112 /* same as before but without hash-cache */
114 r = shl_htable_insert_str(&ht, &o[0].key, NULL);
117 r = shl_htable_insert_str(&ht, &o[0].key, NULL);
120 b = shl_htable_remove_str(&ht, o[0].key, NULL, &k);
122 ck_assert(k != NULL);
123 ck_assert(to_node(k)->v == 0);
125 b = shl_htable_remove_str(&ht, o[0].key, NULL, &k);
127 ck_assert(k != NULL);
128 ck_assert(to_node(k)->v == 0);
131 b = shl_htable_remove_str(&ht, o[0].key, NULL, &k);
133 ck_assert(k == NULL);
135 /* insert all elements and verify empty hash-caches */
138 for (i = 0; i < 8; ++i) {
139 ck_assert(!o[i].hash);
140 r = shl_htable_insert_str(&ht, &o[i].key, &o[i].hash);
142 ck_assert(o[i].hash == shl_htable_rehash_str(&o[i].key, NULL));
147 for (i = 0; i < 8; ++i) {
149 b = shl_htable_lookup_str(&ht, o[i].key, NULL, &k);
151 ck_assert(k != NULL);
152 ck_assert(to_node(k)->v == i);
155 /* remove all elements again */
157 for (i = 0; i < 8; ++i) {
158 b = shl_htable_remove_str(&ht, o[i].key, NULL, &k);
160 ck_assert(k != NULL);
161 ck_assert(to_node(k)->v == i);
164 /* verify they're gone */
166 for (i = 0; i < 8; ++i) {
168 b = shl_htable_remove_str(&ht, o[i].key, NULL, &k);
170 ck_assert(k == NULL);
173 for (i = 0; i < 8; ++i) {
175 b = shl_htable_lookup_str(&ht, o[i].key, NULL, &k);
177 ck_assert(k == NULL);
181 shl_htable_visit_str(&ht, test_htable_str_cb, &num);
185 shl_htable_clear_str(&ht, test_htable_str_cb, &num);
188 /* test shl_htable_clear_str() */
190 for (i = 0; i < 8; ++i) {
191 r = shl_htable_insert_str(&ht, &o[i].key, &o[i].hash);
196 shl_htable_visit_str(&ht, test_htable_str_cb, &num);
200 shl_htable_clear_str(&ht, test_htable_str_cb, &num);
205 static void test_htable_ulong_cb(unsigned long *k, void *ctx)
209 ck_assert(ul_to_node(k)->v == ul_to_node(k)->ul);
213 START_TEST(test_htable_ulong)
219 /* insert once, remove once, try removing again */
221 r = shl_htable_insert_ulong(&uht, &o[0].ul);
223 ck_assert(o[0].ul == shl_htable_rehash_ulong(&o[0].ul, NULL));
225 b = shl_htable_remove_ulong(&uht, o[0].ul, &k);
227 ck_assert(k != NULL);
228 ck_assert(ul_to_node(k)->v == 0);
231 b = shl_htable_remove_ulong(&uht, o[0].ul, &k);
233 ck_assert(k == NULL);
237 for (i = 0; i < 8; ++i) {
238 r = shl_htable_insert_ulong(&uht, &o[i].ul);
244 for (i = 0; i < 8; ++i) {
246 b = shl_htable_lookup_ulong(&uht, o[i].ul, &k);
248 ck_assert(k != NULL);
251 /* remove all elements again */
253 for (i = 0; i < 8; ++i) {
254 b = shl_htable_remove_ulong(&uht, o[i].ul, &k);
256 ck_assert(k != NULL);
257 ck_assert(ul_to_node(k)->v == i);
260 /* verify they're gone */
262 for (i = 0; i < 8; ++i) {
264 b = shl_htable_remove_ulong(&uht, o[i].ul, &k);
266 ck_assert(k == NULL);
269 for (i = 0; i < 8; ++i) {
271 b = shl_htable_lookup_ulong(&uht, o[i].ul, &k);
273 ck_assert(k == NULL);
277 shl_htable_visit_ulong(&uht, test_htable_ulong_cb, &num);
281 shl_htable_clear_ulong(&uht, test_htable_ulong_cb, &num);
284 /* test shl_htable_clear_ulong() */
286 for (i = 0; i < 8; ++i) {
287 r = shl_htable_insert_ulong(&uht, &o[i].ul);
292 shl_htable_visit_ulong(&uht, test_htable_ulong_cb, &num);
296 shl_htable_clear_ulong(&uht, test_htable_ulong_cb, &num);
301 TEST_DEFINE_CASE(misc)
302 TEST(test_htable_str)
303 TEST(test_htable_ulong)
307 TEST_SUITE(hashtable,