Skip to content

Commit

Permalink
tests: test DECLARE_HASH with good and bad hashfn
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
eqvinox committed May 21, 2019
1 parent fdad523 commit 2214f16
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
8 changes: 8 additions & 0 deletions tests/lib/test_typelist.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "atomlist.h"
#include "memory.h"
#include "monotime.h"
#include "jhash.h"

#include "tests/helpers/c/prng.h"

Expand Down Expand Up @@ -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"

Expand All @@ -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();
Expand Down
25 changes: 17 additions & 8 deletions tests/lib/test_typelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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)
{
Expand Down Expand Up @@ -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
Expand All @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -269,4 +277,5 @@ static void concat(test_, TYPE)(void)
#undef list_del
#undef list_pop

#undef REALTYPE
#undef TYPE
1 change: 1 addition & 0 deletions tests/lib/test_typelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 2214f16

Please sign in to comment.