| 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_rhs.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 |
const char *test[] = { "Samy", "Al", "Bahra", "dances", "in", "the", "wind.", "Once", |
| 61 |
"upon", "a", "time", "his", "gypsy", "ate", "one", "itsy", |
| 62 |
"bitsy", "spider.", "What", "goes", "up", "must", |
| 63 |
"come", "down.", "What", "is", "down", "stays", |
| 64 |
"down.", "A", "B", "C", "D", "E", "F", "G", "H", |
| 65 |
"I", "J", "K", "L", "M", "N", "O", "P", "Q" }; |
| 66 |
|
| 67 |
const char *negative = "negative"; |
| 68 |
|
| 69 |
/* Purposefully crappy hash function. */ |
| 70 |
static unsigned long |
| 71 |
hs_hash(const void *object, unsigned long seed) |
| 72 |
{ |
| 73 |
const char *c = object; |
| 74 |
unsigned long h; |
| 75 |
|
| 76 |
(void)seed; |
| 77 |
h = c[0]; |
| 78 |
return h; |
| 79 |
} |
| 80 |
|
| 81 |
static bool |
| 82 |
hs_compare(const void *previous, const void *compare) |
| 83 |
{ |
| 84 |
|
| 85 |
return strcmp(previous, compare) == 0; |
| 86 |
} |
| 87 |
|
| 88 |
static void * |
| 89 |
test_ip(void *key, void *closure) |
| 90 |
{ |
| 91 |
const char *a = key; |
| 92 |
const char *b = closure; |
| 93 |
|
| 94 |
if (strcmp(a, b) != 0) |
| 95 |
ck_error("Mismatch: %s != %s\n", a, b); |
| 96 |
|
| 97 |
return closure; |
| 98 |
} |
| 99 |
|
| 100 |
static void * |
| 101 |
test_negative(void *key, void *closure) |
| 102 |
{ |
| 103 |
|
| 104 |
(void)closure; |
| 105 |
if (key != NULL) |
| 106 |
ck_error("ERROR: Apply callback expects NULL argument instead of [%s]\n", key); |
| 107 |
|
| 108 |
return NULL; |
| 109 |
} |
| 110 |
|
| 111 |
static void * |
| 112 |
test_unique(void *key, void *closure) |
| 113 |
{ |
| 114 |
|
| 115 |
if (key != NULL) |
| 116 |
ck_error("ERROR: Apply callback expects NULL argument instead of [%s]\n", key); |
| 117 |
|
| 118 |
return closure; |
| 119 |
} |
| 120 |
|
| 121 |
static void * |
| 122 |
test_remove(void *key, void *closure) |
| 123 |
{ |
| 124 |
|
| 125 |
(void)key; |
| 126 |
(void)closure; |
| 127 |
|
| 128 |
return NULL; |
| 129 |
} |
| 130 |
|
| 131 |
static void |
| 132 |
run_test(unsigned int is, unsigned int ad) |
| 133 |
{ |
| 134 |
ck_rhs_t hs[16]; |
| 135 |
const size_t size = sizeof(hs) / sizeof(*hs); |
| 136 |
size_t i, j; |
| 137 |
const char *blob = "#blobs"; |
| 138 |
unsigned long h; |
| 139 |
|
| 140 |
if (ck_rhs_init(&hs[0], CK_RHS_MODE_SPMC | CK_RHS_MODE_OBJECT | ad, hs_hash, hs_compare, &my_allocator, is, 6602834) == false) |
| 141 |
ck_error("ck_rhs_init\n"); |
| 142 |
|
| 143 |
for (j = 0; j < size; j++) { |
| 144 |
for (i = 0; i < sizeof(test) / sizeof(*test); i++) { |
| 145 |
h = test[i][0]; |
| 146 |
if (ck_rhs_get(&hs[j], h, test[i]) != NULL) { |
| 147 |
continue; |
| 148 |
} |
| 149 |
|
| 150 |
if (i & 1) { |
| 151 |
if (ck_rhs_put_unique(&hs[j], h, test[i]) == false) |
| 152 |
ck_error("ERROR [%zu]: Failed to insert unique (%s)\n", j, test[i]); |
| 153 |
} else if (ck_rhs_apply(&hs[j], h, test[i], test_unique, |
| 154 |
(void *)(uintptr_t)test[i]) == false) { |
| 155 |
ck_error("ERROR: Failed to apply for insertion.\n"); |
| 156 |
} |
| 157 |
|
| 158 |
if (i & 1) { |
| 159 |
if (ck_rhs_remove(&hs[j], h, test[i]) == false) |
| 160 |
ck_error("ERROR [%zu]: Failed to remove unique (%s)\n", j, test[i]); |
| 161 |
} else if (ck_rhs_apply(&hs[j], h, test[i], test_remove, NULL) == false) { |
| 162 |
ck_error("ERROR: Failed to remove apply.\n"); |
| 163 |
} |
| 164 |
|
| 165 |
if (ck_rhs_apply(&hs[j], h, test[i], test_negative, |
| 166 |
(void *)(uintptr_t)test[i]) == false) |
| 167 |
ck_error("ERROR: Failed to apply.\n"); |
| 168 |
|
| 169 |
break; |
| 170 |
} |
| 171 |
|
| 172 |
for (i = 0; i < sizeof(test) / sizeof(*test); i++) { |
| 173 |
h = test[i][0]; |
| 174 |
ck_rhs_put(&hs[j], h, test[i]); |
| 175 |
if (ck_rhs_put(&hs[j], h, test[i]) == true) { |
| 176 |
ck_error("ERROR [%u] [1]: put must fail on collision (%s).\n", is, test[i]); |
| 177 |
} |
| 178 |
if (ck_rhs_get(&hs[j], h, test[i]) == NULL) { |
| 179 |
ck_error("ERROR [%u]: get must not fail after put\n", is); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
/* Test grow semantics. */ |
| 184 |
ck_rhs_grow(&hs[j], 128); |
| 185 |
for (i = 0; i < sizeof(test) / sizeof(*test); i++) { |
| 186 |
h = test[i][0]; |
| 187 |
if (ck_rhs_put(&hs[j], h, test[i]) == true) { |
| 188 |
ck_error("ERROR [%u] [2]: put must fail on collision.\n", is); |
| 189 |
} |
| 190 |
|
| 191 |
if (ck_rhs_get(&hs[j], h, test[i]) == NULL) { |
| 192 |
ck_error("ERROR [%u]: get must not fail\n", is); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
h = blob[0]; |
| 197 |
if (ck_rhs_get(&hs[j], h, blob) == NULL) { |
| 198 |
if (j > 0) |
| 199 |
ck_error("ERROR [%u]: Blob must always exist after first.\n", is); |
| 200 |
|
| 201 |
if (ck_rhs_put(&hs[j], h, blob) == false) { |
| 202 |
ck_error("ERROR [%u]: A unique blob put failed.\n", is); |
| 203 |
} |
| 204 |
} else { |
| 205 |
if (ck_rhs_put(&hs[j], h, blob) == true) { |
| 206 |
ck_error("ERROR [%u]: Duplicate blob put succeeded.\n", is); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
/* Grow set and check get semantics. */ |
| 211 |
ck_rhs_grow(&hs[j], 512); |
| 212 |
for (i = 0; i < sizeof(test) / sizeof(*test); i++) { |
| 213 |
h = test[i][0]; |
| 214 |
if (ck_rhs_get(&hs[j], h, test[i]) == NULL) { |
| 215 |
ck_error("ERROR [%u]: get must not fail\n", is); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
/* Delete and check negative membership. */ |
| 220 |
for (i = 0; i < sizeof(test) / sizeof(*test); i++) { |
| 221 |
void *r; |
| 222 |
|
| 223 |
h = test[i][0]; |
| 224 |
if (ck_rhs_get(&hs[j], h, test[i]) == NULL) |
| 225 |
continue; |
| 226 |
|
| 227 |
if (r = ck_rhs_remove(&hs[j], h, test[i]), r == NULL) { |
| 228 |
ck_error("ERROR [%u]: remove must not fail\n", is); |
| 229 |
} |
| 230 |
|
| 231 |
if (strcmp(r, test[i]) != 0) { |
| 232 |
ck_error("ERROR [%u]: Removed incorrect node (%s != %s)\n", (char *)r, test[i], is); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
/* Test replacement semantics. */ |
| 237 |
for (i = 0; i < sizeof(test) / sizeof(*test); i++) { |
| 238 |
void *r; |
| 239 |
bool d; |
| 240 |
|
| 241 |
h = test[i][0]; |
| 242 |
d = ck_rhs_get(&hs[j], h, test[i]) != NULL; |
| 243 |
if (ck_rhs_set(&hs[j], h, test[i], &r) == false) { |
| 244 |
ck_error("ERROR [%u]: Failed to set\n", is); |
| 245 |
} |
| 246 |
|
| 247 |
/* Expected replacement. */ |
| 248 |
if (d == true && (r == NULL || strcmp(r, test[i]) != 0)) { |
| 249 |
ck_error("ERROR [%u]: Incorrect previous value: %s != %s\n", |
| 250 |
is, test[i], (char *)r); |
| 251 |
} |
| 252 |
|
| 253 |
/* Replacement should succeed. */ |
| 254 |
if (ck_rhs_fas(&hs[j], h, test[i], &r) == false) |
| 255 |
ck_error("ERROR [%u]: ck_rhs_fas must succeed.\n", is); |
| 256 |
|
| 257 |
if (strcmp(r, test[i]) != 0) { |
| 258 |
ck_error("ERROR [%u]: Incorrect replaced value: %s != %s\n", |
| 259 |
is, test[i], (char *)r); |
| 260 |
} |
| 261 |
|
| 262 |
if (ck_rhs_fas(&hs[j], h, negative, &r) == true) |
| 263 |
ck_error("ERROR [%u]: Replacement of negative should fail.\n", is); |
| 264 |
|
| 265 |
if (ck_rhs_set(&hs[j], h, test[i], &r) == false) { |
| 266 |
ck_error("ERROR [%u]: Failed to set [1]\n", is); |
| 267 |
} |
| 268 |
|
| 269 |
if (strcmp(r, test[i]) != 0) { |
| 270 |
ck_error("ERROR [%u]: Invalid &hs[j]: %s != %s\n", (char *)r, test[i], is); |
| 271 |
} |
| 272 |
/* Attempt in-place mutation. */ |
| 273 |
if (ck_rhs_apply(&hs[j], h, test[i], test_ip, |
| 274 |
(void *)(uintptr_t)test[i]) == false) { |
| 275 |
ck_error("ERROR [%u]: Failed to apply: %s != %s\n", is, (char *)r, test[i]); |
| 276 |
} |
| 277 |
|
| 278 |
d = ck_rhs_get(&hs[j], h, test[i]) != NULL; |
| 279 |
if (d == false) |
| 280 |
ck_error("ERROR [%u]: Expected [%s] to exist.\n", is, test[i]); |
| 281 |
} |
| 282 |
|
| 283 |
if (j == size - 1) |
| 284 |
break; |
| 285 |
|
| 286 |
if (ck_rhs_move(&hs[j + 1], &hs[j], hs_hash, hs_compare, &my_allocator) == false) |
| 287 |
ck_error("Failed to move hash table"); |
| 288 |
|
| 289 |
ck_rhs_gc(&hs[j + 1]); |
| 290 |
|
| 291 |
if (ck_rhs_rebuild(&hs[j + 1]) == false) |
| 292 |
ck_error("Failed to rebuild"); |
| 293 |
} |
| 294 |
|
| 295 |
return; |
| 296 |
} |
| 297 |
|
| 298 |
int |
| 299 |
main(void) |
| 300 |
{ |
| 301 |
unsigned int k; |
| 302 |
|
| 303 |
for (k = 16; k <= 64; k <<= 1) { |
| 304 |
run_test(k, 0); |
| 305 |
break; |
| 306 |
} |
| 307 |
|
| 308 |
return 0; |
| 309 |
} |
| 310 |
|