aboutsummaryrefslogtreecommitdiff
path: root/test/test-htab.c
diff options
context:
space:
mode:
authorcomex2015-07-12 19:25:22 -0400
committercomex2015-07-12 19:25:22 -0400
commitd07d1dd683bcdc70edb26fc38719f38a591f1468 (patch)
treeaf0724c32b483ea89a646d26b77f08994b9c4505 /test/test-htab.c
parentfixes (diff)
downloadsubstitute-d07d1dd683bcdc70edb26fc38719f38a591f1468.tar.gz
fix my hash table algorithm - argh
Diffstat (limited to 'test/test-htab.c')
-rw-r--r--test/test-htab.c32
1 files changed, 31 insertions, 1 deletions
diff --git a/test/test-htab.c b/test/test-htab.c
index c85d620..57886f7 100644
--- a/test/test-htab.c
+++ b/test/test-htab.c
@@ -13,6 +13,13 @@ struct teststr {
DECL_EXTERN_HTAB_KEY(teststr, const char *);
DECL_HTAB(teststr_int, teststr, int);
+#define u32_hash(up) (*(up) % 100)
+#define u32_null(up) (!*(up))
+#define u32_eq(u1p, u2p) (*(u1p) == *(u2p))
+
+DECL_STATIC_HTAB_KEY(u32, uint32_t, u32_hash, u32_eq, u32_null, 0);
+DECL_HTAB(u32_u32, u32, uint32_t);
+
int main() {
/* test loop crap */
LET(int y = 5)
@@ -31,7 +38,6 @@ int main() {
struct htab_teststr_int *hp;
HTAB_STORAGE(teststr_int) stor = HTAB_STORAGE_INIT_STATIC(&stor, teststr_int);
- /*HTAB_STORAGE_INIT(&stor, teststr_int);*/
hp = &stor.h;
for(int i = 0; i < 100; i++) {
const char *k;
@@ -55,6 +61,30 @@ int main() {
printf("%s -> %d\n", *k, *v);
}
htab_free_storage_teststr_int(hp);
+
+ HTAB_STORAGE(u32_u32) h2;
+ HTAB_STORAGE_INIT(&h2, u32_u32);
+ uint32_t h2_raw[501];
+ memset(h2_raw, 0xff, sizeof(h2_raw));
+ for (int i = 0; i < 3000; i++) {
+ uint32_t op = arc4random() & 1;
+ uint32_t key = (arc4random() % 500) + 1;
+ if (op == 0) { /* set */
+ uint32_t val = arc4random() & 0x7fffffff;
+ *htab_setp_u32_u32(&h2.h, &key, NULL) = val;
+ h2_raw[key] = val;
+ } else { /* delete */
+ htab_remove_u32_u32(&h2.h, &key);
+ h2_raw[key] = -1;
+ }
+ }
+ for (uint32_t k = 1; k <= 500; k++) {
+ uint32_t raw = h2_raw[k];
+ uint32_t *hashedp = htab_getp_u32_u32(&h2.h, &k);
+ uint32_t hashed = hashedp ? *hashedp : -1;
+ /* printf("%d %x %x\n", k, raw, hashed); */
+ assert(hashed == raw);
+ }
}
/*