test: extract common prng helpers for future use

This commit is contained in:
CismonX 2025-03-08 13:50:47 +08:00
parent 1ed607de69
commit aed846b286
No known key found for this signature in database
GPG key ID: 3094873E29A482FB
4 changed files with 38 additions and 18 deletions

View file

@ -105,19 +105,16 @@ subcmd_prng (
int argc, int argc,
char *argv[] char *argv[]
) { ) {
static uint64_t seed[4]; static uint64_t buf[4];
int cnt = 0; 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':
cnt = sscanf(optarg, if (0 != prng_seed_from_hex(buf, optarg)) {
"%16" SCNx64 "%16" SCNx64 "%16" SCNx64 "%16" SCNx64,
&seed[0], &seed[1], &seed[2], &seed[3]);
if (cnt != 4) {
log_printf("bad seed '%s'", optarg);
return -1; return -1;
} }
seed = buf;
break; break;
case 'n': case 'n':
@ -129,7 +126,7 @@ subcmd_prng (
return -1; return -1;
} }
if (0 != prng_seed(cnt == 0 ? NULL : seed)) { if (0 != prng_seed(seed)) {
return -1; return -1;
} }
for (; n > 0; --n) { for (; n > 0; --n) {
@ -138,6 +135,21 @@ subcmd_prng (
return 0; return 0;
} }
int
prng_seed_from_hex (
uint64_t *buf,
char const *str
) {
int cnt = sscanf(str,
"%16" SCNx64 "%16" SCNx64 "%16" SCNx64 "%16" SCNx64,
&buf[0], &buf[1], &buf[2], &buf[3]);
if (cnt != 4) {
log_printf("bad seed '%s'", optarg);
return -1;
}
return 0;
}
int int
main ( main (
int argc, int argc,

View file

@ -21,6 +21,8 @@
#ifndef BOOKMARKFS_CHECK_LIB_H_ #ifndef BOOKMARKFS_CHECK_LIB_H_
#define BOOKMARKFS_CHECK_LIB_H_ #define BOOKMARKFS_CHECK_LIB_H_
#include <stdint.h>
#include "xstd.h" #include "xstd.h"
#define ASSERT_EXPR_INT(expr, r, cond, action_if_false) \ #define ASSERT_EXPR_INT(expr, r, cond, action_if_false) \
@ -45,4 +47,10 @@ check_watcher (
char *argv[] char *argv[]
); );
int
prng_seed_from_hex (
uint64_t *buf,
char const *str
);
#endif /* !defined(BOOKMARKFS_CHECK_LIB_H_) */ #endif /* !defined(BOOKMARKFS_CHECK_LIB_H_) */

View file

@ -18,16 +18,7 @@ ATX_CHECK_LIB([
check-util-lib prng -s$1 -n$2 check-util-lib prng -s$1 -n$2
} }
gen_seed() { seed=$(ath_fn_prng_seed)
seed_1=$(ath_fn_rand_u64_hex)
seed_2=$(ath_fn_rand_u64_hex)
seed_3=$(ath_fn_rand_u64_hex)
seed_4=$(ath_fn_rand_u64_hex)
echo $seed_1$seed_2$seed_3$seed_4
}
seed=$(gen_seed)
count=32 count=32
num_1=$(gen_num $seed $count) num_1=$(gen_num $seed $count)

View file

@ -93,6 +93,15 @@ AT_TEST_HELPER_FN([rand_base64], , , [
echo $(head -c$1 /dev/urandom | base64 -w0) echo $(head -c$1 /dev/urandom | base64 -w0)
]) ])
AT_TEST_HELPER_FN([prng_seed], , , [
seed_1=$(ath_fn_rand_u64_hex)
seed_2=$(ath_fn_rand_u64_hex)
seed_3=$(ath_fn_rand_u64_hex)
seed_4=$(ath_fn_rand_u64_hex)
echo $seed_1$seed_2$seed_3$seed_4
])
AT_BANNER([The Utility Library]) AT_BANNER([The Utility Library])
m4_include([lib_hash.at]) m4_include([lib_hash.at])
m4_include([lib_prng.at]) m4_include([lib_prng.at])