test: misc refactor

This commit is contained in:
CismonX 2025-03-09 11:35:17 +08:00
parent ca43bc6939
commit 0b317c2727
No known key found for this signature in database
GPG key ID: 3094873E29A482FB
3 changed files with 16 additions and 21 deletions

View file

@ -22,8 +22,6 @@
# include "config.h" # include "config.h"
#endif #endif
#include <limits.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <unistd.h> #include <unistd.h>
@ -39,7 +37,7 @@ struct check_item {
// Forward declaration start // Forward declaration start
static int check_one_round (struct hashmap *, struct check_item *, size_t); 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 int item_comp_func (union hashmap_key, void const *);
static unsigned long static unsigned long
item_hash_func (void const *); item_hash_func (void const *);
@ -81,14 +79,14 @@ check_one_round (
int int
do_check_hashmap ( do_check_hashmap (
int n, size_t items_cnt,
int rounds int rounds
) { ) {
size_t items_cnt = 1u << n;
size_t buf_size = sizeof(struct check_item) * items_cnt; size_t buf_size = sizeof(struct check_item) * items_cnt;
struct check_item *items = mmap(NULL, buf_size, PROT_READ | PROT_WRITE, struct check_item *items = mmap(NULL, buf_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (items == MAP_FAILED) { if (items == MAP_FAILED) {
log_puts("failed to allocate memory");
return -1; return -1;
} }
struct hashmap *map = hashmap_create(item_comp_func, item_hash_func); 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) { for (size_t i = 0; i < items_cnt; ++i) {
struct check_item *item = items + i; struct check_item *item = items + i;
unsigned long id = item->id;
if (id & 1) { if (item->id & 1) {
--cnt; --cnt;
hashmap_delete(map, item, -1); hashmap_delete(map, item, -1);
} }
@ -171,18 +168,16 @@ check_hashmap (
int argc, int argc,
char *argv[] char *argv[]
) { ) {
static uint64_t buf[4]; uint64_t seed_buf[4], *seed = NULL;
uint64_t *seed = NULL;
int n = -1; int n = -1;
int r = -1; int r = -1;
getopt_foreach(argc, argv, ":s:n:r:") { getopt_foreach(argc, argv, ":s:n:r:") {
case 's': case 's':
if (0 != prng_seed_from_hex(buf, optarg)) { if (0 != prng_seed_from_hex(seed_buf, optarg)) {
return -1; return -1;
} }
seed = buf; seed = seed_buf;
break; break;
case 'n': case 'n':
@ -209,5 +204,5 @@ check_hashmap (
if (0 != prng_seed(seed)) { if (0 != prng_seed(seed)) {
return -1; return -1;
} }
return do_check_hashmap(n, r); return do_check_hashmap(1u << n, r);
} }

View file

@ -37,7 +37,7 @@
// Forward declaration start // Forward declaration start
static int dispatch_subcmds (int, char *[]); 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_hash (int, char *[]);
static int subcmd_prng (int, char *[]); static int subcmd_prng (int, char *[]);
// Forward declaration end // Forward declaration end
@ -70,7 +70,7 @@ dispatch_subcmds (
} }
static size_t static size_t
hash_cb ( hash_check_cb (
void *UNUSED_VAR(user_data), void *UNUSED_VAR(user_data),
void const **buf_ptr void const **buf_ptr
) { ) {
@ -98,7 +98,7 @@ subcmd_hash (
} }
hash_seed(seed); hash_seed(seed);
printf("%016" PRIx64 "\n", hash_digestcb(hash_cb, NULL)); printf("%016" PRIx64 "\n", hash_digestcb(hash_check_cb, NULL));
return 0; return 0;
} }
@ -107,16 +107,15 @@ subcmd_prng (
int argc, int argc,
char *argv[] char *argv[]
) { ) {
static uint64_t buf[4]; uint64_t seed_buf[4], *seed = NULL;
uint64_t *seed = NULL;
int n = 0; int n = 0;
getopt_foreach(argc, argv, ":s:n:") { getopt_foreach(argc, argv, ":s:n:") {
case 's': case 's':
if (0 != prng_seed_from_hex(buf, optarg)) { if (0 != prng_seed_from_hex(seed_buf, optarg)) {
return -1; return -1;
} }
seed = buf; seed = seed_buf;
break; break;
case 'n': case 'n':

View file

@ -15,6 +15,7 @@ ATX_CHECK_LIB([
if test -z "$seed"; then if test -z "$seed"; then
seed=$(ath_fn_prng_seed) seed=$(ath_fn_prng_seed)
fi fi
echo "prng seed: $seed"
size="${CHECK_HASHMAP_DATA_SIZE}" size="${CHECK_HASHMAP_DATA_SIZE}"
if test -z "$size"; then if test -z "$size"; then