From b20d9da5f294e19acdd1341388377be27cf11c46 Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 13 Sep 2021 19:58:01 -0700 Subject: [PATCH] ... --- inc/shutdown.h | 4 +- src/hashmap.c | 15 ++-- src/shutdown.c | 4 +- test/unit/hashmap.tests.c | 166 +++++++++++++++++++++++++++++++++++++- 4 files changed, 178 insertions(+), 11 deletions(-) diff --git a/inc/shutdown.h b/inc/shutdown.h index 1ab723e..5191354 100644 --- a/inc/shutdown.h +++ b/inc/shutdown.h @@ -7,8 +7,8 @@ #include -#define SHUTDOWN_MESSAGE_FAILED "graceful shutdown failed\n" -#define SHUTDOWN_MESSAGE_SUCCESS "shutdown successful\n" +#define SHUTDOWN_MESSAGE_LOGGING_FAILED "graceful shutdown of logging thread failed\n" +#define SHUTDOWN_MESSAGE_LOGGING_SUCCESS "shutdown logging thread successfully\n" int shutdown_register(); void shutdown(); diff --git a/src/hashmap.c b/src/hashmap.c index 0f38931..fba67af 100644 --- a/src/hashmap.c +++ b/src/hashmap.c @@ -1,12 +1,12 @@ #include void *hashmap_find(struct hash_map *p, void *key, size_t key_size) { - unsigned char hash[crypto_shorthash_KEYBYTES]; + unsigned char hash[crypto_shorthash_BYTES]; size_t index; if(crypto_shorthash(hash,key,key_size,p->key)<0) { return NULL; } - index = ((uint64_t) hash)%p->size; + index = (*(uint64_t*) hash)%p->size; return p->map[index]; } @@ -20,6 +20,9 @@ void hashmap_free(struct hash_map *p) { } int hashmap_init(struct hash_map **p, size_t initial_size) { + if(p==NULL) { return -1; } + if(initial_size==0) { return -1; } + if(sodium_init()<0) { perror("sodium_init"); return -1; @@ -44,12 +47,12 @@ int hashmap_init(struct hash_map **p, size_t initial_size) { } int hashmap_insert(struct hash_map *p, void *key, size_t key_size, void *value) { - unsigned char hash[crypto_shorthash_KEYBYTES]; + unsigned char hash[crypto_shorthash_BYTES]; size_t index; if(crypto_shorthash(hash,key,key_size,p->key)<0) { return -1; } - index = ((uint64_t) hash)%p->size; + index = (*(uint64_t*) hash)%p->size; if(p->map[index]!=NULL) { return 0; } p->map[index] = value; @@ -58,13 +61,13 @@ int hashmap_insert(struct hash_map *p, void *key, size_t key_size, void *value) } void *hashmap_remove(struct hash_map *p, void *key, size_t key_size) { - unsigned char hash[crypto_shorthash_KEYBYTES]; + unsigned char hash[crypto_shorthash_BYTES]; size_t index; void *removed; if(crypto_shorthash(hash,key,key_size,p->key)<0) { return NULL; } - index = ((uint64_t) hash)%p->size; + index = (*(uint64_t*) hash)%p->size; removed = p->map[index]; p->map[index] = NULL; diff --git a/src/shutdown.c b/src/shutdown.c index c3ee5d1..4bd9a32 100644 --- a/src/shutdown.c +++ b/src/shutdown.c @@ -24,8 +24,8 @@ static void shutdown_logging() { logging_thread = pthread_self(); if((ret_cancel!=0)||(ret_join!=0)||(res!=PTHREAD_CANCELED)) { - log_err(SHUTDOWN_MESSAGE_FAILED); + log_err(SHUTDOWN_MESSAGE_LOGGING_FAILED); } else { - log_msg(SHUTDOWN_MESSAGE_SUCCESS); + log_msg(SHUTDOWN_MESSAGE_LOGGING_SUCCESS); } } diff --git a/test/unit/hashmap.tests.c b/test/unit/hashmap.tests.c index 3699336..2ec0e9b 100644 --- a/test/unit/hashmap.tests.c +++ b/test/unit/hashmap.tests.c @@ -3,18 +3,182 @@ #include int main(); +static void hashmap_find_basic_tests(); static void hashmap_init_basic_tests(); +static void hashmap_insert_basic_tests(); +static void hashmap_remove_basic_tests(); int main() { setup_env(); hashmap_init_basic_tests(); + hashmap_insert_basic_tests(); + hashmap_find_basic_tests(); + hashmap_remove_basic_tests(); clean_env(); return EXIT_SUCCESS; } +static void hashmap_find_basic_tests() { + struct hash_map *p; + int i,j,k,test; + int *a,*b,*c; + + assert(hashmap_init(&p,8)==1); + memset(p->key,0,crypto_shorthash_KEYBYTES); + + a = malloc(sizeof(int)); + assert(NULL!=a); + b = malloc(sizeof(int)); + assert(NULL!=b); + c = malloc(sizeof(int)); + assert(NULL!=c); + + i = 10; + *a = 51; + j = 11; + *b = 50; + k = 69; + *c = 1001; + + assert(hashmap_insert(p,&i,sizeof(int),a)==1); + assert(hashmap_insert(p,&j,sizeof(int),b)==1); + assert(hashmap_insert(p,&k,sizeof(int),c)==1); + + test = 5; + assert(NULL==hashmap_find(p,&test,sizeof(int))); + + test = 10; + assert(a==hashmap_find(p,&test,sizeof(int))); + + test = 11; + assert(b==hashmap_find(p,&test,sizeof(int))); + + test = 500; + assert(NULL==hashmap_find(p,&test,sizeof(int))); + + test = 69; + assert(c==hashmap_find(p,&test,sizeof(int))); + + hashmap_free(p); +} + static void hashmap_init_basic_tests() { - assert(0); + struct hash_map *p; + + assert(hashmap_init(NULL,0)==-1); + assert(hashmap_init(&p,0)==-1); + + assert(hashmap_init(&p,8)==1); + + assert(8==p->size); + for(int i=0;i<8;i++) { + assert(NULL==p->map[i]); + } + + hashmap_free(p); +} + +static void hashmap_insert_basic_tests() { + struct hash_map *p; + int i,j,k,m; + int *a,*b,*c,*d; + + assert(hashmap_init(&p,8)==1); + memset(p->key,0,crypto_shorthash_KEYBYTES); + + a = malloc(sizeof(int)); + assert(NULL!=a); + b = malloc(sizeof(int)); + assert(NULL!=b); + c = malloc(sizeof(int)); + assert(NULL!=c); + d = malloc(sizeof(int)); + assert(NULL!=d); + + i = 10; + *a = 51; + j = 11; + *b = 50; + k = 69; + *c = 1001; + m = 50; + *d = 17; + + assert(hashmap_insert(p,&i,sizeof(int),a)==1); + assert(hashmap_insert(p,&j,sizeof(int),b)==1); + assert(hashmap_insert(p,&k,sizeof(int),c)==1); + assert(hashmap_insert(p,&m,sizeof(int),d)==0); + + assert(NULL==p->map[0]); + assert(NULL==p->map[1]); + assert(c==p->map[2]); + assert(a==p->map[3]); + assert(b==p->map[4]); + assert(NULL==p->map[5]); + assert(NULL==p->map[6]); + assert(NULL==p->map[7]); + + hashmap_free(p); + + free(d); +} + +static void hashmap_remove_basic_tests() { + struct hash_map *p; + int i,j,k,test; + int *a,*b,*c,*ret; + + assert(hashmap_init(&p,8)==1); + memset(p->key,0,crypto_shorthash_KEYBYTES); + + a = malloc(sizeof(int)); + assert(NULL!=a); + b = malloc(sizeof(int)); + assert(NULL!=b); + c = malloc(sizeof(int)); + assert(NULL!=c); + + i = 10; + *a = 51; + j = 11; + *b = 50; + k = 69; + *c = 1001; + + assert(hashmap_insert(p,&i,sizeof(int),a)==1); + assert(hashmap_insert(p,&j,sizeof(int),b)==1); + assert(hashmap_insert(p,&k,sizeof(int),c)==1); + + assert(NULL==p->map[0]); + assert(NULL==p->map[1]); + assert(c==p->map[2]); + assert(a==p->map[3]); + assert(b==p->map[4]); + assert(NULL==p->map[5]); + assert(NULL==p->map[6]); + assert(NULL==p->map[7]); + + test = 500; + ret = hashmap_remove(p,&test,sizeof(int)); + assert(NULL==ret); + + test = 10; + ret = hashmap_remove(p,&test,sizeof(int)); + assert(ret==a); + + assert(NULL==p->map[0]); + assert(NULL==p->map[1]); + assert(c==p->map[2]); + assert(NULL==p->map[3]); + assert(b==p->map[4]); + assert(NULL==p->map[5]); + assert(NULL==p->map[6]); + assert(NULL==p->map[7]); + + free(ret); + + hashmap_free(p); } -- 2.39.5