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

This commit is contained in:
Stephen Seo 2024-09-01 15:40:17 +09:00
parent aeb8eff350
commit dd1a8abdd4
3 changed files with 39 additions and 19 deletions

View file

@ -37,7 +37,7 @@ typedef struct SDArchiverHashMapKeyData {
} SDArchiverHashMapKeyData; } SDArchiverHashMapKeyData;
typedef struct SDArchiverInternalIterContext { 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; int ret;
void *user_data; void *user_data;
} SDArchiverInternalIterContext; } 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 simple_archiver_hash_map_iter(const SDArchiverHashMap *hash_map,
int (*iter_check_fn)(void *, unsigned int, int (*iter_check_fn)(const void *,
void *, void *), unsigned int,
const void *, void *),
void *user_data) { void *user_data) {
SDArchiverInternalIterContext ctx; SDArchiverInternalIterContext ctx;
ctx.iter_check_fn = iter_check_fn; ctx.iter_check_fn = iter_check_fn;
ctx.ret = 0; ctx.ret = 0;
ctx.user_data = user_data; ctx.user_data = user_data;
for (unsigned int idx = 0; idx < hash_map->buckets_size; ++idx) { for (unsigned int idx = 0; idx < hash_map->buckets_size; ++idx) {
simple_archiver_list_get(hash_map->buckets[idx], if (simple_archiver_list_get(
simple_archiver_internal_hash_map_bucket_iter_fn, hash_map->buckets[idx],
&ctx); simple_archiver_internal_hash_map_bucket_iter_fn, &ctx) != 0) {
return ctx.ret;
}
} }
return ctx.ret; return ctx.ret;
} }

View file

@ -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 /// "iter_check_fn" returns zero for every call, then this function will return
/// zero after having iterated through every key-value pair. /// zero after having iterated through every key-value pair.
int simple_archiver_hash_map_iter(const SDArchiverHashMap *hash_map, int simple_archiver_hash_map_iter(const SDArchiverHashMap *hash_map,
int (*iter_check_fn)(void *, unsigned int, int (*iter_check_fn)(const void *,
void *, void *), unsigned int,
const void *, void *),
void *user_data); void *user_data);
#endif #endif

View file

@ -25,6 +25,8 @@
#include "linked_list.h" #include "linked_list.h"
#include "priority_heap.h" #include "priority_heap.h"
#define SDARCHIVER_DS_TEST_HASH_MAP_ITER_SIZE 100
static int checks_checked = 0; static int checks_checked = 0;
static int checks_passed = 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 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, __attribute__((unused)) unsigned int key_size,
void *value, void *ud) { const void *value, void *ud) {
char *found_buf = ud; char *found_buf = ud;
size_t real_value = (size_t)value; const size_t real_value = (const size_t)value;
if (real_value < 5) { if (real_value < SDARCHIVER_DS_TEST_HASH_MAP_ITER_SIZE) {
found_buf[real_value] += 1; found_buf[real_value] += 1;
return 0; return 0;
} else { } 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) { int main(void) {
// Test LinkedList. // Test LinkedList.
{ {
@ -183,21 +192,28 @@ int main(void) {
// Hash map iter test. // Hash map iter test.
hash_map = simple_archiver_hash_map_init(); 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, simple_archiver_hash_map_insert(&hash_map, (void *)idx, &idx,
sizeof(size_t), no_free_fn, no_free_fn); 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, CHECK_TRUE(simple_archiver_hash_map_iter(hash_map, hash_map_iter_check_fn,
found) == 0); found) == 0);
CHECK_TRUE(found[0] == 1); for (unsigned int idx = 0; idx < SDARCHIVER_DS_TEST_HASH_MAP_ITER_SIZE;
CHECK_TRUE(found[1] == 1); ++idx) {
CHECK_TRUE(found[2] == 1); CHECK_TRUE(found[idx] == 1);
CHECK_TRUE(found[3] == 1); }
CHECK_TRUE(found[4] == 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); simple_archiver_hash_map_free(&hash_map);
} }