]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Tue, 14 Sep 2021 02:58:01 +0000 (19:58 -0700)
committeralex <[email protected]>
Tue, 14 Sep 2021 02:58:01 +0000 (19:58 -0700)
inc/shutdown.h
src/hashmap.c
src/shutdown.c
test/unit/hashmap.tests.c

index 1ab723eafa15770bc405f554c9cebb2a47729d6c..5191354cd8f4ed293b5ea65cfadfbe74bb603a2b 100644 (file)
@@ -7,8 +7,8 @@
 
 #include<log.h>
 
-#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();
index 0f3893184dbd5bb17f4d61926c4002976dac2eea..fba67af24e6a0ffbf489909a28faee2c6aa50cfb 100644 (file)
@@ -1,12 +1,12 @@
 #include<hashmap.h>
 
 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;
index c3ee5d1017af1afd2f79122eae884cefb16b5721..4bd9a3292f58e4691b6ab2a26005ea3cfeb5f712 100644 (file)
@@ -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);
        }
 }
index 3699336cf5b57fa3f01c88e4479697d769bcdf4a..2ec0e9b624b00bdde72e0bb628b26f3ab6a03daf 100644 (file)
 #include<hashmap.h>
 
 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);
 }