Add some hashmap tests
This commit is contained in:
parent
1fb4e8d9a8
commit
b0c2c0c8ac
6 changed files with 100 additions and 19 deletions
22
hmap.c
22
hmap.c
|
@ -66,24 +66,6 @@ uint64_t hash_str_fnv1a_64(char * s) {
|
|||
return hash;
|
||||
}
|
||||
|
||||
struct _map_node {
|
||||
struct _map_node *next;
|
||||
void * key;
|
||||
uint64_t hash;
|
||||
void *data;
|
||||
};
|
||||
|
||||
typedef struct _map_node hmap_node;
|
||||
|
||||
typedef struct {
|
||||
size_t capacity;
|
||||
size_t len;
|
||||
size_t keysize;
|
||||
hmap_node *buckets;
|
||||
} hmap;
|
||||
|
||||
void* hmap_insert(hmap * map, void *key, void *data);
|
||||
|
||||
hmap_node* hmap_node_init(void *key, size_t keysize, void *data) {
|
||||
hmap_node *node;
|
||||
uint64_t hash;
|
||||
|
@ -242,13 +224,15 @@ void* hmap_remove(hmap *map, void *key) {
|
|||
ret = current->data;
|
||||
map->len--;
|
||||
if (previous != NULL) {
|
||||
previous->next = current;
|
||||
previous->next = current->next;
|
||||
} else {
|
||||
map->buckets[idx] = *current->next;
|
||||
}
|
||||
free(current);
|
||||
break;
|
||||
} else {
|
||||
current = current->next;
|
||||
previous = current;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -64,4 +64,31 @@ int haggis_store_filetype(FILE *stream, haggis_filetype *filetype);
|
|||
uint64_t hash_fnv1a_64(uint8_t *key, size_t len);
|
||||
uint64_t hash_str_fnv1a_64(char *s);
|
||||
|
||||
struct _map_node {
|
||||
struct _map_node *next;
|
||||
void * key;
|
||||
uint64_t hash;
|
||||
void *data;
|
||||
};
|
||||
|
||||
typedef struct _map_node hmap_node;
|
||||
|
||||
typedef struct {
|
||||
size_t capacity;
|
||||
size_t len;
|
||||
size_t keysize;
|
||||
hmap_node *buckets;
|
||||
} hmap;
|
||||
|
||||
hmap_node* hmap_node_init(void *key, size_t keysize, void *data);
|
||||
void hmap_node_deinit(hmap_node *node);
|
||||
void* hmap_node_attach(hmap_node *root, hmap_node *leaf, size_t keysize);
|
||||
void* hmap_node_search(hmap_node *root, void *key, size_t keysize);
|
||||
hmap* hmap_init(size_t keysize);
|
||||
void hmap_deinit(hmap *map);
|
||||
int hmap_expand(hmap *map);
|
||||
void* hmap_insert(hmap *map, void *key, void *data);
|
||||
void* hmap_get(hmap *map, void *key);
|
||||
void* hmap_remove(hmap *map, void *key);
|
||||
|
||||
#endif // !HAGGIS_PRIVATE_H
|
||||
|
|
|
@ -61,6 +61,9 @@ tests += store_file_sha256
|
|||
tests += load_file_sha256
|
||||
tests += fnv1a_hash_inode
|
||||
tests += fnv1a_hash_str
|
||||
tests += init_hmap
|
||||
tests += hmap_insert
|
||||
tests += hmap_get
|
||||
|
||||
total != echo $(tests) | wc -w | awk '{ print $$1 }'
|
||||
|
||||
|
|
29
test/hmap_get.c
Normal file
29
test/hmap_get.c
Normal file
|
@ -0,0 +1,29 @@
|
|||
#include "haggis.h"
|
||||
#include "haggis_private.h"
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
int main() {
|
||||
hmap *map;
|
||||
struct stat st;
|
||||
char *data;
|
||||
union {
|
||||
ino_t val;
|
||||
u8 bytes[sizeof(ino_t)];
|
||||
} nod;
|
||||
char *ret;
|
||||
|
||||
map = hmap_init(sizeof(ino_t));
|
||||
assert(map != NULL);
|
||||
data = "Makefile";
|
||||
stat(data, &st);
|
||||
nod.val = st.st_ino;
|
||||
hmap_insert(map, &nod.bytes[0], data);
|
||||
assert(map->len == 1);
|
||||
ret = hmap_get(map, &nod.bytes[0]);
|
||||
assert(memcmp(ret, data, 9) == 0);
|
||||
hmap_deinit(map);
|
||||
return 0;
|
||||
}
|
26
test/hmap_insert.c
Normal file
26
test/hmap_insert.c
Normal file
|
@ -0,0 +1,26 @@
|
|||
#include "haggis.h"
|
||||
#include "haggis_private.h"
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
int main() {
|
||||
hmap *map;
|
||||
struct stat st;
|
||||
char *data;
|
||||
union {
|
||||
ino_t val;
|
||||
u8 bytes[sizeof(ino_t)];
|
||||
} nod;
|
||||
|
||||
map = hmap_init(sizeof(ino_t));
|
||||
assert(map != NULL);
|
||||
data = "Makefile";
|
||||
stat(data, &st);
|
||||
nod.val = st.st_ino;
|
||||
hmap_insert(map, &nod.bytes[0], data);
|
||||
assert(map->len == 1);
|
||||
hmap_deinit(map);
|
||||
return 0;
|
||||
}
|
12
test/init_hmap.c
Normal file
12
test/init_hmap.c
Normal file
|
@ -0,0 +1,12 @@
|
|||
#include "haggis_private.h"
|
||||
#include <assert.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
int main() {
|
||||
hmap *map;
|
||||
|
||||
map = hmap_init(sizeof(ino_t));
|
||||
assert(map != NULL);
|
||||
hmap_deinit(map);
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Reference in a new issue