From 5366100e4a804acc9f9c36b30b8da091483d8050 Mon Sep 17 00:00:00 2001 From: Ralph Ronnquist Date: Sun, 26 Jun 2022 15:46:59 +1000 Subject: [PATCH] added --- tests/example-hashvector.c | 64 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tests/example-hashvector.c diff --git a/tests/example-hashvector.c b/tests/example-hashvector.c new file mode 100644 index 0000000..63c5582 --- /dev/null +++ b/tests/example-hashvector.c @@ -0,0 +1,64 @@ +#include +#include +#include "hashvector.h" + +typedef struct _ipslot { + int family; + unsigned char data[32]; + unsigned int bits; +} ipslot; + +static unsigned long voidp_hashcode(void *key) { + return hashvector_hashcode( key, sizeof( ipslot ) ); +} + +static void* voidp_itemkey(void *item) { + return item; +} + +static int voidp_haskey(void *item,void *key) { + return memcmp( item, key, sizeof( ipslot ) ) == 0; +} + +static int shrink(vector *pv,unsigned long index,void *item,void *data) { + if ( item ) { + if ( item == HV_HOLE ) { + ((hashvector*) data)->holes--; + } else { + free( item ); + ((hashvector*) data)->fill--; + } + } + return 0; +} + +int main(int argc,char **argv) { + hashvector hv = { + .table = { 4, 0 }, + .fill = 0, + .holes = 0, + .keyhashcode = voidp_hashcode, + .itemkey = voidp_itemkey, + .haskey = voidp_haskey + }; + int i = 0; + for ( ; i < 259; i++ ) { + ipslot *item = (ipslot*) calloc( 1, sizeof( ipslot ) ); + if ( i > 250 ) { + int n = i; + i = n; + } + item->family = i; + memcpy( item->data, "10.10.10.1", 10 ); + hashvector_add( &hv, item ); + } + for ( i = 256; i < 260; i++ ) { + vector_index index = i; + void ** slot = vector_next_used( &hv.table, &index, 0, 0 ); + if ( slot && *slot != HV_HOLE ) { + hashvector_delete( &hv, *slot ); + } + } + vector_resize( &hv.table, 256, shrink, &hv ); + return 0; +} -- 2.39.2