| 1 |
/* |
| 2 |
* Copyright 2012 Samy Al Bahra. |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyrights |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyrights |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 15 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 18 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 20 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 21 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 22 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 23 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 24 |
* SUCH DAMAGE. |
| 25 |
*/ |
| 26 |
|
| 27 |
#include <ck_hs.h> |
| 28 |
|
| 29 |
#include <assert.h> |
| 30 |
#include <ck_malloc.h> |
| 31 |
#include <errno.h> |
| 32 |
#include <stdio.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
|
| 36 |
#include "../../common.h" |
| 37 |
|
| 38 |
static void * |
| 39 |
hs_malloc(size_t r) |
| 40 |
{ |
| 41 |
|
| 42 |
return malloc(r); |
| 43 |
} |
| 44 |
|
| 45 |
static void |
| 46 |
hs_free(void *p, size_t b, bool r) |
| 47 |
{ |
| 48 |
|
| 49 |
(void)b; |
| 50 |
(void)r; |
| 51 |
free(p); |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
static struct ck_malloc my_allocator = { |
| 56 |
.malloc = hs_malloc, |
| 57 |
.free = hs_free |
| 58 |
}; |
| 59 |
|
| 60 |
static void |
| 61 |
stub_free(void *p, size_t b, bool r) |
| 62 |
{ |
| 63 |
|
| 64 |
(void)b; |
| 65 |
(void)r; |
| 66 |
|
| 67 |
fprintf(stderr, "Ignoring reclamation of %p\n", p); |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
static struct ck_malloc stub_allocator = { |
| 72 |
.malloc = hs_malloc, |
| 73 |
.free = stub_free |
| 74 |
}; |
| 75 |
|
| 76 |
const char *test[] = { "Samy", "Al", "Bahra", "dances", "in", "the", "wind.", "Once", |
| 77 |
"upon", "a", "time", "his", "gypsy", "ate", "one", "itsy", |
| 78 |
"bitsy", "spider.", "What", "goes", "up", "must", |
| 79 |
"come", "down.", "What", "is", "down", "stays", |
| 80 |
"down.", "A", "B", "C", "D", "E", "F", "G", "H", |
| 81 |
"I", "J", "K", "L", "M", "N", "O", "P", "Q" }; |
| 82 |
|
| 83 |
const char *negative = "negative"; |
| 84 |
|
| 85 |
/* Purposefully crappy hash function. */ |
| 86 |
static unsigned long |
| 87 |
hs_hash(const void *object, unsigned long seed) |
| 88 |
{ |
| 89 |
const char *c = object; |
| 90 |
unsigned long h; |
| 91 |
|
| 92 |
(void)seed; |
| 93 |
h = c[0]; |
| 94 |
return h; |
| 95 |
} |
| 96 |
|
| 97 |
static bool |
| 98 |
hs_compare(const void *previous, const void *compare) |
| 99 |
{ |
| 100 |
|
| 101 |
return strcmp(previous, compare) == 0; |
| 102 |
} |
| 103 |
|
| 104 |
static void * |
| 105 |
test_ip(void *key, void *closure) |
| 106 |
{ |
| 107 |
const char *a = key; |
| 108 |
const char *b = closure; |
| 109 |
|
| 110 |
if (strcmp(a, b) != 0) |
| 111 |
ck_error("Mismatch: %s != %s\n", a, b); |
| 112 |
|
| 113 |
return closure; |
| 114 |
} |
| 115 |
|
| 116 |
static void * |
| 117 |
test_negative(void *key, void *closure) |
| 118 |
{ |
| 119 |
|
| 120 |
(void)closure; |
| 121 |
if (key != NULL) |
| 122 |
ck_error("ERROR: Apply callback expects NULL argument instead of [%s]\n", key); |
| 123 |
|
| 124 |
return NULL; |
| 125 |
} |
| 126 |
|
| 127 |
static void * |
| 128 |
test_unique(void *key, void *closure) |
| 129 |
{ |
| 130 |
|
| 131 |
if (key != NULL) |
| 132 |
ck_error("ERROR: Apply callback expects NULL argument instead of [%s]\n", key); |
| 133 |
|
| 134 |
return closure; |
| 135 |
} |
| 136 |
|
| 137 |
static void * |
| 138 |
test_remove(void *key, void *closure) |
| 139 |
{ |
| 140 |
|
| 141 |
(void)key; |
| 142 |
(void)closure; |
| 143 |
|
| 144 |
return NULL; |
| 145 |
} |
| 146 |
|
| 147 |
static void |
| 148 |
run_test(unsigned int is, unsigned int ad) |
| 149 |
{ |
| 150 |
ck_hs_t hs[16]; |
| 151 |
const size_t size = sizeof(hs) / sizeof(*hs); |
| 152 |
size_t i, j; |
| 153 |
const char *blob = "#blobs"; |
| 154 |
unsigned long h; |
| 155 |
ck_hs_iterator_t it; |
| 156 |
|
| 157 |
if (ck_hs_init(&hs[0], CK_HS_MODE_SPMC | CK_HS_MODE_OBJECT | ad, hs_hash, hs_compare, &my_allocator, is, 6602834) == false) |
| 158 |
ck_error("ck_hs_init\n"); |
| 159 |
|
| 160 |
for (j = 0; j < size; j++) { |
| 161 |
for (i = 0; i < sizeof(test) / sizeof(*test); i++) { |
| 162 |
unsigned long h_1; |
| 163 |
|
| 164 |
h = CK_HS_HASH(&hs[j], hs_hash, test[i]); |
| 165 |
h_1 = ck_hs_hash(&hs[j], test[i]); |
| 166 |
|
| 167 |
if (h != h_1) |
| 168 |
ck_error("h != h_1 (%lu != %lu)\n", h, h_1); |
| 169 |
|
| 170 |
if (ck_hs_get(&hs[j], h, test[i]) != NULL) { |
| 171 |
continue; |
| 172 |
} |
| 173 |
|
| 174 |
if (i & 1) { |
| 175 |
if (ck_hs_put_unique(&hs[j], h, test[i]) == false) |
| 176 |
ck_error("ERROR [%zu]: Failed to insert unique (%s)\n", j, test[i]); |
| 177 |
} else if (ck_hs_apply(&hs[j], h, test[i], test_unique, (void *)(uintptr_t)test[i]) == false) { |
| 178 |
ck_error("ERROR: Failed to apply for insertion.\n"); |
| 179 |
} |
| 180 |
|
| 181 |
if (i & 1) { |
| 182 |
if (ck_hs_remove(&hs[j], h, test[i]) == false) |
| 183 |
ck_error("ERROR [%zu]: Failed to remove unique (%s)\n", j, test[i]); |
| 184 |
} else if (ck_hs_apply(&hs[j], h, test[i], test_remove, NULL) == false) { |
| 185 |
ck_error("ERROR: Failed to remove apply.\n"); |
| 186 |
} |
| 187 |
|
| 188 |
if (ck_hs_apply(&hs[j], h, test[i], test_negative, (char *)(uintptr_t)test[i]) == false) |
| 189 |
ck_error("ERROR: Failed to apply.\n"); |
| 190 |
|
| 191 |
break; |
| 192 |
} |
| 193 |
|
| 194 |
if (ck_hs_gc(&hs[j], 0, 0) == false) |
| 195 |
ck_error("ERROR: Failed to GC empty set.\n"); |
| 196 |
|
| 197 |
for (i = 0; i < sizeof(test) / sizeof(*test); i++) { |
| 198 |
h = test[i][0]; |
| 199 |
ck_hs_put(&hs[j], h, test[i]); |
| 200 |
if (ck_hs_put(&hs[j], h, test[i]) == true) { |
| 201 |
ck_error("ERROR [%u] [1]: put must fail on collision (%s).\n", is, test[i]); |
| 202 |
} |
| 203 |
if (ck_hs_get(&hs[j], h, test[i]) == NULL) { |
| 204 |
ck_error("ERROR [%u]: get must not fail after put\n", is); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/* Test iteration */ |
| 209 |
if (j == 0) { |
| 210 |
/* Avoid the blob stuff as it's not in the test array. */ |
| 211 |
ck_hs_iterator_init(&it); |
| 212 |
void *k = NULL; |
| 213 |
int matches = 0; |
| 214 |
int entries = 0; |
| 215 |
while (ck_hs_next(&hs[j], &it, &k) == true) { |
| 216 |
entries++; |
| 217 |
for (i = 0; i < sizeof(test) / sizeof(*test); i++) { |
| 218 |
int x = strcmp(test[i], (char *)k); |
| 219 |
if (x == 0) { |
| 220 |
matches++; |
| 221 |
break; |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
if (entries != matches) { |
| 227 |
ck_error("Iteration must match all elements, has: %d, matched: %d [%d]", entries, matches, is); |
| 228 |
} |
| 229 |
|
| 230 |
/* |
| 231 |
* Now test iteration in the face of grows (spmc). |
| 232 |
* In order to test usage after reclamation, we |
| 233 |
* stub the allocator. |
| 234 |
*/ |
| 235 |
ck_hs_iterator_init(&it); |
| 236 |
k = NULL; |
| 237 |
matches = 0; |
| 238 |
entries = 0; |
| 239 |
hs[j].m = &stub_allocator; |
| 240 |
while (ck_hs_next_spmc(&hs[j], &it, &k) == true) { |
| 241 |
entries++; |
| 242 |
for (i = 0; i < sizeof(test) / sizeof(*test); i++) { |
| 243 |
int x = strcmp(test[i], (char *)k); |
| 244 |
if (x == 0) { |
| 245 |
matches++; |
| 246 |
break; |
| 247 |
} |
| 248 |
} |
| 249 |
if (entries == 20) { |
| 250 |
ck_hs_grow(&hs[j], 128); |
| 251 |
} |
| 252 |
} |
| 253 |
hs[j].m = &my_allocator; |
| 254 |
|
| 255 |
if (entries != matches) { |
| 256 |
ck_error("After growth, iteration must match all elements, has: %d, matched: %d [%d]", entries, matches, is); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
/* Test grow semantics. */ |
| 261 |
ck_hs_grow(&hs[j], 128); |
| 262 |
for (i = 0; i < sizeof(test) / sizeof(*test); i++) { |
| 263 |
h = test[i][0]; |
| 264 |
if (ck_hs_put(&hs[j], h, test[i]) == true) { |
| 265 |
ck_error("ERROR [%u] [2]: put must fail on collision.\n", is); |
| 266 |
} |
| 267 |
|
| 268 |
if (ck_hs_get(&hs[j], h, test[i]) == NULL) { |
| 269 |
ck_error("ERROR [%u]: get must not fail\n", is); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
h = blob[0]; |
| 274 |
if (ck_hs_get(&hs[j], h, blob) == NULL) { |
| 275 |
if (j > 0) |
| 276 |
ck_error("ERROR [%u]: Blob must always exist after first.\n", is); |
| 277 |
|
| 278 |
if (ck_hs_put(&hs[j], h, blob) == false) { |
| 279 |
ck_error("ERROR [%u]: A unique blob put failed.\n", is); |
| 280 |
} |
| 281 |
} else { |
| 282 |
if (ck_hs_put(&hs[j], h, blob) == true) { |
| 283 |
ck_error("ERROR [%u]: Duplicate blob put succeeded.\n", is); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
/* Grow set and check get semantics. */ |
| 288 |
ck_hs_grow(&hs[j], 512); |
| 289 |
for (i = 0; i < sizeof(test) / sizeof(*test); i++) { |
| 290 |
h = test[i][0]; |
| 291 |
if (ck_hs_get(&hs[j], h, test[i]) == NULL) { |
| 292 |
ck_error("ERROR [%u]: get must not fail\n", is); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
/* Delete and check negative membership. */ |
| 297 |
for (i = 0; i < sizeof(test) / sizeof(*test); i++) { |
| 298 |
void *r; |
| 299 |
|
| 300 |
h = test[i][0]; |
| 301 |
if (ck_hs_get(&hs[j], h, test[i]) == NULL) |
| 302 |
continue; |
| 303 |
|
| 304 |
if (r = ck_hs_remove(&hs[j], h, test[i]), r == NULL) { |
| 305 |
ck_error("ERROR [%u]: remove must not fail\n", is); |
| 306 |
} |
| 307 |
|
| 308 |
if (strcmp(r, test[i]) != 0) { |
| 309 |
ck_error("ERROR [%u]: Removed incorrect node (%s != %s)\n", (char *)r, test[i], is); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
/* Test replacement semantics. */ |
| 314 |
for (i = 0; i < sizeof(test) / sizeof(*test); i++) { |
| 315 |
void *r; |
| 316 |
bool d; |
| 317 |
|
| 318 |
h = test[i][0]; |
| 319 |
d = ck_hs_get(&hs[j], h, test[i]) != NULL; |
| 320 |
if (ck_hs_set(&hs[j], h, test[i], &r) == false) { |
| 321 |
ck_error("ERROR [%u]: Failed to set\n", is); |
| 322 |
} |
| 323 |
|
| 324 |
/* Expected replacement. */ |
| 325 |
if (d == true && (r == NULL || strcmp(r, test[i]) != 0)) { |
| 326 |
ck_error("ERROR [%u]: Incorrect previous value: %s != %s\n", |
| 327 |
is, test[i], (char *)r); |
| 328 |
} |
| 329 |
|
| 330 |
/* Replacement should succeed. */ |
| 331 |
if (ck_hs_fas(&hs[j], h, test[i], &r) == false) |
| 332 |
ck_error("ERROR [%u]: ck_hs_fas must succeed.\n", is); |
| 333 |
|
| 334 |
if (strcmp(r, test[i]) != 0) { |
| 335 |
ck_error("ERROR [%u]: Incorrect replaced value: %s != %s\n", |
| 336 |
is, test[i], (char *)r); |
| 337 |
} |
| 338 |
|
| 339 |
if (ck_hs_fas(&hs[j], h, negative, &r) == true) |
| 340 |
ck_error("ERROR [%u]: Replacement of negative should fail.\n", is); |
| 341 |
|
| 342 |
if (ck_hs_set(&hs[j], h, test[i], &r) == false) { |
| 343 |
ck_error("ERROR [%u]: Failed to set [1]\n", is); |
| 344 |
} |
| 345 |
|
| 346 |
if (strcmp(r, test[i]) != 0) { |
| 347 |
ck_error("ERROR [%u]: Invalid &hs[j]: %s != %s\n", is, test[i], (char *)r); |
| 348 |
} |
| 349 |
|
| 350 |
/* Attempt in-place mutation. */ |
| 351 |
if (ck_hs_apply(&hs[j], h, test[i], test_ip, (void *)(uintptr_t)test[i]) == false) |
| 352 |
ck_error("ERROR [%u]: Failed to apply: %s != %s\n", is, (char *)r, test[i]); |
| 353 |
|
| 354 |
d = ck_hs_get(&hs[j], h, test[i]) != NULL; |
| 355 |
if (d == false) |
| 356 |
ck_error("ERROR [%u]: Expected [%s] to exist.\n", is, test[i]); |
| 357 |
} |
| 358 |
|
| 359 |
if (j == size - 1) |
| 360 |
break; |
| 361 |
|
| 362 |
if (ck_hs_move(&hs[j + 1], &hs[j], hs_hash, hs_compare, &my_allocator) == false) |
| 363 |
ck_error("Failed to move hash table"); |
| 364 |
|
| 365 |
if (j & 1) { |
| 366 |
ck_hs_gc(&hs[j + 1], 0, 0); |
| 367 |
} else { |
| 368 |
ck_hs_gc(&hs[j + 1], 26, 26); |
| 369 |
} |
| 370 |
|
| 371 |
if (ck_hs_rebuild(&hs[j + 1]) == false) |
| 372 |
ck_error("Failed to rebuild"); |
| 373 |
} |
| 374 |
|
| 375 |
return; |
| 376 |
} |
| 377 |
|
| 378 |
int |
| 379 |
main(void) |
| 380 |
{ |
| 381 |
unsigned int k; |
| 382 |
|
| 383 |
for (k = 16; k <= 64; k <<= 1) { |
| 384 |
run_test(k, 0); |
| 385 |
run_test(k, CK_HS_MODE_DELETE); |
| 386 |
break; |
| 387 |
} |
| 388 |
|
| 389 |
return 0; |
| 390 |
} |
| 391 |
|