#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];
}
}
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;
}
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;
}
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;
#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);
}