Added test for replacing a hashmap entry
This commit is contained in:
parent
bd820638b5
commit
4e63c251d8
3 changed files with 44 additions and 5 deletions
11
hmap.c
11
hmap.c
|
@ -32,6 +32,7 @@
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
@ -174,7 +175,7 @@ int hmap_expand(hmap *map) {
|
||||||
void* hmap_insert(hmap *map, void *key, void *data) {
|
void* hmap_insert(hmap *map, void *key, void *data) {
|
||||||
hmap_node *node;
|
hmap_node *node;
|
||||||
size_t idx;
|
size_t idx;
|
||||||
hmap_node *ret = NULL;
|
void *ret = NULL;
|
||||||
|
|
||||||
if (map->len >= map->capacity / 2) {
|
if (map->len >= map->capacity / 2) {
|
||||||
if (hmap_expand(map)) return NULL;
|
if (hmap_expand(map)) return NULL;
|
||||||
|
@ -187,11 +188,11 @@ void* hmap_insert(hmap *map, void *key, void *data) {
|
||||||
} else {
|
} else {
|
||||||
ret = hmap_node_attach(&map->buckets[idx], node, map->keysize);
|
ret = hmap_node_attach(&map->buckets[idx], node, map->keysize);
|
||||||
}
|
}
|
||||||
map->len++;
|
if (ret == NULL) {
|
||||||
if (ret == NULL)
|
map->len++;
|
||||||
return NULL;
|
return NULL;
|
||||||
else
|
} else
|
||||||
return ret->data;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* hmap_get(hmap *map, void *key) {
|
void* hmap_get(hmap *map, void *key) {
|
||||||
|
|
|
@ -64,6 +64,7 @@ tests += fnv1a_hash_str
|
||||||
tests += init_hmap
|
tests += init_hmap
|
||||||
tests += hmap_insert
|
tests += hmap_insert
|
||||||
tests += hmap_get
|
tests += hmap_get
|
||||||
|
tests += hmap_replace
|
||||||
|
|
||||||
total != echo $(tests) | wc -w | awk '{ print $$1 }'
|
total != echo $(tests) | wc -w | awk '{ print $$1 }'
|
||||||
|
|
||||||
|
|
37
test/hmap_replace.c
Normal file
37
test/hmap_replace.c
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
#include "haggis.h"
|
||||||
|
#include "haggis_private.h"
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdio.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 = NULL;
|
||||||
|
|
||||||
|
map = hmap_init(sizeof(ino_t));
|
||||||
|
assert(map != NULL);
|
||||||
|
data = "Makefile";
|
||||||
|
stat(data, &st);
|
||||||
|
nod.val = st.st_ino;
|
||||||
|
ret = hmap_insert(map, &nod.bytes[0], data);
|
||||||
|
assert(ret == NULL);
|
||||||
|
assert(map->len == 1);
|
||||||
|
ret = hmap_get(map, &nod.bytes[0]);
|
||||||
|
assert(memcmp(ret, data, 7) == 0);
|
||||||
|
ret = hmap_insert(map, &nod.bytes[0], "testdata");
|
||||||
|
assert(ret != NULL);
|
||||||
|
assert(memcmp(ret, data, 8) == 0);
|
||||||
|
assert(map->len == 1);
|
||||||
|
ret = hmap_get(map, &nod.bytes[0]);
|
||||||
|
assert(memcmp(ret, "testdata", 9) == 0);
|
||||||
|
hmap_deinit(map);
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue