From 0b317c2727e55aa20f9b76e305844f4dceb06ea8 Mon Sep 17 00:00:00 2001 From: CismonX Date: Sun, 9 Mar 2025 11:35:17 +0800 Subject: [PATCH] test: misc refactor --- tests/check_hashmap.c | 23 +++++++++-------------- tests/check_lib.c | 13 ++++++------- tests/lib_hashmap.at | 1 + 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/tests/check_hashmap.c b/tests/check_hashmap.c index 614928c..e206b43 100644 --- a/tests/check_hashmap.c +++ b/tests/check_hashmap.c @@ -22,8 +22,6 @@ # include "config.h" #endif -#include - #include #include @@ -39,7 +37,7 @@ struct check_item { // Forward declaration start static int check_one_round (struct hashmap *, struct check_item *, size_t); -static int do_check_hashmap (int, int); +static int do_check_hashmap (size_t, int); static int item_comp_func (union hashmap_key, void const *); static unsigned long item_hash_func (void const *); @@ -81,14 +79,14 @@ check_one_round ( int do_check_hashmap ( - int n, - int rounds + size_t items_cnt, + int rounds ) { - size_t items_cnt = 1u << n; size_t buf_size = sizeof(struct check_item) * items_cnt; struct check_item *items = mmap(NULL, buf_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (items == MAP_FAILED) { + log_puts("failed to allocate memory"); return -1; } struct hashmap *map = hashmap_create(item_comp_func, item_hash_func); @@ -112,9 +110,8 @@ do_check_hashmap ( for (size_t i = 0; i < items_cnt; ++i) { struct check_item *item = items + i; - unsigned long id = item->id; - if (id & 1) { + if (item->id & 1) { --cnt; hashmap_delete(map, item, -1); } @@ -171,18 +168,16 @@ check_hashmap ( int argc, char *argv[] ) { - static uint64_t buf[4]; - uint64_t *seed = NULL; - + uint64_t seed_buf[4], *seed = NULL; int n = -1; int r = -1; getopt_foreach(argc, argv, ":s:n:r:") { case 's': - if (0 != prng_seed_from_hex(buf, optarg)) { + if (0 != prng_seed_from_hex(seed_buf, optarg)) { return -1; } - seed = buf; + seed = seed_buf; break; case 'n': @@ -209,5 +204,5 @@ check_hashmap ( if (0 != prng_seed(seed)) { return -1; } - return do_check_hashmap(n, r); + return do_check_hashmap(1u << n, r); } diff --git a/tests/check_lib.c b/tests/check_lib.c index a8fa08f..c4e0536 100644 --- a/tests/check_lib.c +++ b/tests/check_lib.c @@ -37,7 +37,7 @@ // Forward declaration start static int dispatch_subcmds (int, char *[]); -static size_t hash_cb (void *, void const **); +static size_t hash_check_cb (void *, void const **); static int subcmd_hash (int, char *[]); static int subcmd_prng (int, char *[]); // Forward declaration end @@ -70,7 +70,7 @@ dispatch_subcmds ( } static size_t -hash_cb ( +hash_check_cb ( void *UNUSED_VAR(user_data), void const **buf_ptr ) { @@ -98,7 +98,7 @@ subcmd_hash ( } hash_seed(seed); - printf("%016" PRIx64 "\n", hash_digestcb(hash_cb, NULL)); + printf("%016" PRIx64 "\n", hash_digestcb(hash_check_cb, NULL)); return 0; } @@ -107,16 +107,15 @@ subcmd_prng ( int argc, char *argv[] ) { - static uint64_t buf[4]; - uint64_t *seed = NULL; + uint64_t seed_buf[4], *seed = NULL; int n = 0; getopt_foreach(argc, argv, ":s:n:") { case 's': - if (0 != prng_seed_from_hex(buf, optarg)) { + if (0 != prng_seed_from_hex(seed_buf, optarg)) { return -1; } - seed = buf; + seed = seed_buf; break; case 'n': diff --git a/tests/lib_hashmap.at b/tests/lib_hashmap.at index 2518d7d..89d2c57 100644 --- a/tests/lib_hashmap.at +++ b/tests/lib_hashmap.at @@ -15,6 +15,7 @@ ATX_CHECK_LIB([ if test -z "$seed"; then seed=$(ath_fn_prng_seed) fi + echo "prng seed: $seed" size="${CHECK_HASHMAP_DATA_SIZE}" if test -z "$size"; then