hash_map iter fn: const key-value, update unittest
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 52s
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 52s
This commit is contained in:
parent
aeb8eff350
commit
dd1a8abdd4
3 changed files with 39 additions and 19 deletions
|
@ -37,7 +37,7 @@ typedef struct SDArchiverHashMapKeyData {
|
|||
} SDArchiverHashMapKeyData;
|
||||
|
||||
typedef struct SDArchiverInternalIterContext {
|
||||
int (*iter_check_fn)(void *, unsigned int, void *, void *);
|
||||
int (*iter_check_fn)(const void *, unsigned int, const void *, void *);
|
||||
int ret;
|
||||
void *user_data;
|
||||
} SDArchiverInternalIterContext;
|
||||
|
@ -269,17 +269,20 @@ int simple_archiver_internal_hash_map_bucket_iter_fn(void *data, void *ud) {
|
|||
}
|
||||
|
||||
int simple_archiver_hash_map_iter(const SDArchiverHashMap *hash_map,
|
||||
int (*iter_check_fn)(void *, unsigned int,
|
||||
void *, void *),
|
||||
int (*iter_check_fn)(const void *,
|
||||
unsigned int,
|
||||
const void *, void *),
|
||||
void *user_data) {
|
||||
SDArchiverInternalIterContext ctx;
|
||||
ctx.iter_check_fn = iter_check_fn;
|
||||
ctx.ret = 0;
|
||||
ctx.user_data = user_data;
|
||||
for (unsigned int idx = 0; idx < hash_map->buckets_size; ++idx) {
|
||||
simple_archiver_list_get(hash_map->buckets[idx],
|
||||
simple_archiver_internal_hash_map_bucket_iter_fn,
|
||||
&ctx);
|
||||
if (simple_archiver_list_get(
|
||||
hash_map->buckets[idx],
|
||||
simple_archiver_internal_hash_map_bucket_iter_fn, &ctx) != 0) {
|
||||
return ctx.ret;
|
||||
}
|
||||
}
|
||||
return ctx.ret;
|
||||
}
|
||||
|
|
|
@ -60,8 +60,9 @@ int simple_archiver_hash_map_remove(SDArchiverHashMap *hash_map, void *key,
|
|||
/// "iter_check_fn" returns zero for every call, then this function will return
|
||||
/// zero after having iterated through every key-value pair.
|
||||
int simple_archiver_hash_map_iter(const SDArchiverHashMap *hash_map,
|
||||
int (*iter_check_fn)(void *, unsigned int,
|
||||
void *, void *),
|
||||
int (*iter_check_fn)(const void *,
|
||||
unsigned int,
|
||||
const void *, void *),
|
||||
void *user_data);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -25,6 +25,8 @@
|
|||
#include "linked_list.h"
|
||||
#include "priority_heap.h"
|
||||
|
||||
#define SDARCHIVER_DS_TEST_HASH_MAP_ITER_SIZE 100
|
||||
|
||||
static int checks_checked = 0;
|
||||
static int checks_passed = 0;
|
||||
|
||||
|
@ -63,12 +65,12 @@ int get_three_fn(void *data, __attribute__((unused)) void *ud) {
|
|||
|
||||
int more_fn(long long a, long long b) { return a > b ? 1 : 0; }
|
||||
|
||||
int hash_map_iter_check_fn(__attribute__((unused)) void *key,
|
||||
int hash_map_iter_check_fn(__attribute__((unused)) const void *key,
|
||||
__attribute__((unused)) unsigned int key_size,
|
||||
void *value, void *ud) {
|
||||
const void *value, void *ud) {
|
||||
char *found_buf = ud;
|
||||
size_t real_value = (size_t)value;
|
||||
if (real_value < 5) {
|
||||
const size_t real_value = (const size_t)value;
|
||||
if (real_value < SDARCHIVER_DS_TEST_HASH_MAP_ITER_SIZE) {
|
||||
found_buf[real_value] += 1;
|
||||
return 0;
|
||||
} else {
|
||||
|
@ -76,6 +78,13 @@ int hash_map_iter_check_fn(__attribute__((unused)) void *key,
|
|||
}
|
||||
}
|
||||
|
||||
int hash_map_iter_check_fn2(__attribute__((unused)) const void *key,
|
||||
__attribute__((unused)) unsigned int key_size,
|
||||
__attribute__((unused)) const void *value,
|
||||
__attribute__((unused)) void *ud) {
|
||||
return 2;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
// Test LinkedList.
|
||||
{
|
||||
|
@ -183,21 +192,28 @@ int main(void) {
|
|||
// Hash map iter test.
|
||||
hash_map = simple_archiver_hash_map_init();
|
||||
|
||||
for (size_t idx = 0; idx < 5; ++idx) {
|
||||
for (size_t idx = 0; idx < SDARCHIVER_DS_TEST_HASH_MAP_ITER_SIZE; ++idx) {
|
||||
simple_archiver_hash_map_insert(&hash_map, (void *)idx, &idx,
|
||||
sizeof(size_t), no_free_fn, no_free_fn);
|
||||
}
|
||||
|
||||
char found[5] = {0, 0, 0, 0, 0};
|
||||
char found[SDARCHIVER_DS_TEST_HASH_MAP_ITER_SIZE] = {0};
|
||||
|
||||
CHECK_TRUE(simple_archiver_hash_map_iter(hash_map, hash_map_iter_check_fn,
|
||||
found) == 0);
|
||||
|
||||
CHECK_TRUE(found[0] == 1);
|
||||
CHECK_TRUE(found[1] == 1);
|
||||
CHECK_TRUE(found[2] == 1);
|
||||
CHECK_TRUE(found[3] == 1);
|
||||
CHECK_TRUE(found[4] == 1);
|
||||
for (unsigned int idx = 0; idx < SDARCHIVER_DS_TEST_HASH_MAP_ITER_SIZE;
|
||||
++idx) {
|
||||
CHECK_TRUE(found[idx] == 1);
|
||||
}
|
||||
|
||||
CHECK_TRUE(simple_archiver_hash_map_iter(hash_map, hash_map_iter_check_fn2,
|
||||
found) == 2);
|
||||
|
||||
for (unsigned int idx = 0; idx < SDARCHIVER_DS_TEST_HASH_MAP_ITER_SIZE;
|
||||
++idx) {
|
||||
CHECK_TRUE(found[idx] == 1);
|
||||
}
|
||||
|
||||
simple_archiver_hash_map_free(&hash_map);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue