]> git.puffer.fish Git - matthieu/frr.git/commitdiff
tests: test DECLARE_HASH with good and bad hashfn
authorDavid Lamparter <equinox@diac24.net>
Mon, 20 May 2019 19:04:14 +0000 (21:04 +0200)
committerDavid Lamparter <equinox@diac24.net>
Tue, 21 May 2019 03:42:10 +0000 (05:42 +0200)
The hash table test was previously (intentionally) using a bad hash
function to test the code in the face of hash collisions.  Add a test
with a good hash function to see some performance numbers.

Signed-off-by: David Lamparter <equinox@diac24.net>
tests/lib/test_typelist.c
tests/lib/test_typelist.h
tests/lib/test_typelist.py

index 68ea82ea4164010d628efd674240a26d04fd6e92..df71da8921cf9c5644ab8984e8dfa73f568d8bec 100644 (file)
@@ -32,6 +32,7 @@
 #include "atomlist.h"
 #include "memory.h"
 #include "monotime.h"
+#include "jhash.h"
 
 #include "tests/helpers/c/prng.h"
 
@@ -105,6 +106,12 @@ static void ts_end(void)
 #define TYPE HASH
 #include "test_typelist.h"
 
+#define TYPE HASH_collisions
+#define REALTYPE HASH
+#define SHITTY_HASH
+#include "test_typelist.h"
+#undef SHITTY_HASH
+
 #define TYPE SKIPLIST_UNIQ
 #include "test_typelist.h"
 
@@ -130,6 +137,7 @@ int main(int argc, char **argv)
        test_SORTLIST_UNIQ();
        test_SORTLIST_NONUNIQ();
        test_HASH();
+       test_HASH_collisions();
        test_SKIPLIST_UNIQ();
        test_SKIPLIST_NONUNIQ();
        test_RBTREE_UNIQ();
index 60a37e84edd326250fdac47110f3a65892e1f47b..196a85889d982153032ec9affe9e8c1e0327641e 100644 (file)
 #define list_del       concat(TYPE, _del)
 #define list_pop       concat(TYPE, _pop)
 
-PREDECL(TYPE, list)
+#ifndef REALTYPE
+#define REALTYPE TYPE
+#endif
+
+PREDECL(REALTYPE, list)
 struct item {
        uint64_t val;
        struct list_item itm;
@@ -45,18 +49,22 @@ struct item {
 
 static int list_cmp(const struct item *a, const struct item *b);
 
-#if IS_HASH(TYPE)
+#if IS_HASH(REALTYPE)
 static uint32_t list_hash(const struct item *a);
-DECLARE(TYPE, list, struct item, itm, list_cmp, list_hash)
+DECLARE(REALTYPE, list, struct item, itm, list_cmp, list_hash)
 
 static uint32_t list_hash(const struct item *a)
 {
+#ifdef SHITTY_HASH
        /* crappy hash to get some hash collisions */
        return a->val ^ (a->val << 29) ^ 0x55AA0000U;
+#else
+       return jhash_1word(a->val, 0xdeadbeef);
+#endif
 }
 
 #else
-DECLARE(TYPE, list, struct item, itm, list_cmp)
+DECLARE(REALTYPE, list, struct item, itm, list_cmp)
 #endif
 
 static int list_cmp(const struct item *a, const struct item *b)
@@ -70,7 +78,7 @@ static int list_cmp(const struct item *a, const struct item *b)
 
 #define NITEM 10000
 struct item itm[NITEM];
-static struct list_head head = concat(INIT_, TYPE)(head);
+static struct list_head head = concat(INIT_, REALTYPE)(head);
 
 static void concat(test_, TYPE)(void)
 {
@@ -109,7 +117,7 @@ static void concat(test_, TYPE)(void)
        k = 0;
        prev = NULL;
        for_each(list, &head, item) {
-#if IS_HASH(TYPE)
+#if IS_HASH(REALTYPE)
                /* hash table doesn't give sorting */
                (void)prev;
 #else
@@ -121,7 +129,7 @@ static void concat(test_, TYPE)(void)
        assert(list_count(&head) == k);
        ts_ref("walk");
 
-#if IS_UNIQ(TYPE)
+#if IS_UNIQ(REALTYPE)
        prng_free(prng);
        prng = prng_new(0);
 
@@ -175,7 +183,7 @@ static void concat(test_, TYPE)(void)
        }
        ts_ref("add-dup+find_{lt,gteq}");
 #endif
-#if !IS_HASH(TYPE)
+#if !IS_HASH(REALTYPE)
        prng_free(prng);
        prng = prng_new(123456);
 
@@ -269,4 +277,5 @@ static void concat(test_, TYPE)(void)
 #undef list_del
 #undef list_pop
 
+#undef REALTYPE
 #undef TYPE
index 3b7373ceb8797d84bc1e7e5f5d6a0fb987529434..11cabecd228ab83e5fd650e7620e4ccd2c71a41d 100644 (file)
@@ -6,6 +6,7 @@ class TestTypelist(frrtest.TestMultiOut):
 TestTypelist.onesimple('SORTLIST_UNIQ end')
 TestTypelist.onesimple('SORTLIST_NONUNIQ end')
 TestTypelist.onesimple('HASH end')
+TestTypelist.onesimple('HASH_collisions end')
 TestTypelist.onesimple('SKIPLIST_UNIQ end')
 TestTypelist.onesimple('SKIPLIST_NONUNIQ end')
 TestTypelist.onesimple('RBTREE_UNIQ end')