summaryrefslogtreecommitdiffstats
path: root/tests/lib
diff options
context:
space:
mode:
authorDavid Lamparter <equinox@diac24.net>2019-05-20 21:04:14 +0200
committerDavid Lamparter <equinox@diac24.net>2019-05-21 05:42:10 +0200
commit2214f160bf30c21b96515287f06938fea4191848 (patch)
treecee8e8f16352422914c40ab5a08347634651a0fa /tests/lib
parentlib: add DECLARE_DLIST (double-linked list) (diff)
downloadfrr-2214f160bf30c21b96515287f06938fea4191848.tar.xz
frr-2214f160bf30c21b96515287f06938fea4191848.zip
tests: test DECLARE_HASH with good and bad hashfn
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>
Diffstat (limited to 'tests/lib')
-rw-r--r--tests/lib/test_typelist.c8
-rw-r--r--tests/lib/test_typelist.h25
-rw-r--r--tests/lib/test_typelist.py1
3 files changed, 26 insertions, 8 deletions
diff --git a/tests/lib/test_typelist.c b/tests/lib/test_typelist.c
index 68ea82ea4..df71da892 100644
--- a/tests/lib/test_typelist.c
+++ b/tests/lib/test_typelist.c
@@ -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();
diff --git a/tests/lib/test_typelist.h b/tests/lib/test_typelist.h
index 60a37e84e..196a85889 100644
--- a/tests/lib/test_typelist.h
+++ b/tests/lib/test_typelist.h
@@ -36,7 +36,11 @@
#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
diff --git a/tests/lib/test_typelist.py b/tests/lib/test_typelist.py
index 3b7373ceb..11cabecd2 100644
--- a/tests/lib/test_typelist.py
+++ b/tests/lib/test_typelist.py
@@ -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')