From 9da346753d69a9c06b863444aeb20d4b0b8e9dd7 Mon Sep 17 00:00:00 2001 From: CismonX Date: Fri, 28 Mar 2025 14:29:18 +0800 Subject: [PATCH] test: misc refactor - Repeat simple tests that may fail upon multiple invocations. - ... --- tests/check_watcher.c | 12 ++---------- tests/lib_hash.at | 31 +++++++++++++++++-------------- tests/lib_prng.at | 31 +++++++++++++++++-------------- tests/lib_watcher.at | 2 +- tests/testsuite.at | 17 +++++++++++++++-- 5 files changed, 52 insertions(+), 41 deletions(-) diff --git a/tests/check_watcher.c b/tests/check_watcher.c index 7740b84..a7128c8 100644 --- a/tests/check_watcher.c +++ b/tests/check_watcher.c @@ -154,19 +154,11 @@ check_watcher ( int argc, char *argv[] ) { - char const *path = NULL; - - OPT_START(argc, argv, "d:") - OPT_OPT('d') { - path = optarg; - break; - } - OPT_END - - if (path == NULL) { + if (--argc < 1) { log_puts("path not specified"); return -1; } + char const *path = *(++argv); int dirfd = open(path, O_RDONLY | O_DIRECTORY); if (dirfd < 0) { diff --git a/tests/lib_hash.at b/tests/lib_hash.at index ca9f789..b29c29b 100644 --- a/tests/lib_hash.at +++ b/tests/lib_hash.at @@ -19,23 +19,26 @@ ATX_CHECK_LIB([ echo "$2" | check-util-lib hash -s$1 } - seed=$(ath_fn_rand_u64_hex) - input=$(ath_fn_rand_base64 300) + ATX_RUN_REPEAT([16], [ + seed=$(ath_fn_rand_u64_hex) + input=$(ath_fn_rand_base64 300) + echo "hash seed: $seed; input: $input" - hash_1=$(calc_digest $seed $input) - hash_2=$(calc_digest $seed $input) - hash_3=$(calc_digest $seed $input) - hash_x=$(calc_digest $seed $input+) + hash_1=$(calc_digest $seed $input) + hash_2=$(calc_digest $seed $input) + hash_3=$(calc_digest $seed $input) + hash_x=$(calc_digest $seed $input+) - if test "$hash_1" != "$hash_2" -o "$hash_2" != "$hash_3"; then - echo 'bad hash function: inconsistent digest' - exit 1 - fi + if test "$hash_1" != "$hash_2" -o "$hash_2" != "$hash_3"; then + echo 'bad hash function: inconsistent digest' + exit 1 + fi - if test "$hash_1" = "$hash_x"; then - echo 'bad hash function: collision' - exit 1 - fi + if test "$hash_1" = "$hash_x"; then + echo 'bad hash function: collision' + exit 1 + fi + ]) ]) AT_CLEANUP diff --git a/tests/lib_prng.at b/tests/lib_prng.at index c0b5589..0ff29af 100644 --- a/tests/lib_prng.at +++ b/tests/lib_prng.at @@ -18,23 +18,26 @@ ATX_CHECK_LIB([ check-util-lib prng -s$1 -n$2 } - seed=$(ath_fn_prng_seed) - count=32 + ATX_RUN_REPEAT([16], [ + seed=$(ath_fn_prng_seed) + count=32 + echo "prng seed: $seed" - num_1=$(gen_num $seed $count) - num_2=$(gen_num $seed $count) - num_3=$(gen_num $seed $count) + num_1=$(gen_num $seed $count) + num_2=$(gen_num $seed $count) + num_3=$(gen_num $seed $count) - if test "$num_1" != "$num_2" -o "$num_2" != "$num_3"; then - echo 'bad prng: inconsistent number sequence' - exit 1 - fi + if test "$num_1" != "$num_2" -o "$num_2" != "$num_3"; then + echo 'bad prng: inconsistent number sequence' + exit 1 + fi - uniq_count=$(echo "$num_1" | sort | uniq | wc -l) - if test $uniq_count -ne $count; then - echo 'bad prng: getting duplicates too soon' - exit 1 - fi + uniq_count=$(echo "$num_1" | sort | uniq | wc -l) + if test $uniq_count -ne $count; then + echo 'bad prng: getting duplicates too soon' + exit 1 + fi + ]) ]) AT_CLEANUP diff --git a/tests/lib_watcher.at b/tests/lib_watcher.at index 00a6a23..e87eb83 100644 --- a/tests/lib_watcher.at +++ b/tests/lib_watcher.at @@ -16,7 +16,7 @@ ATX_CHECK_LIB([ mkdir $tmpdir ATX_RUN([ - check-util-lib watcher -d $tmpdir + check-util-lib watcher $tmpdir ]) ]) diff --git a/tests/testsuite.at b/tests/testsuite.at index b959729..5222be4 100644 --- a/tests/testsuite.at +++ b/tests/testsuite.at @@ -50,6 +50,19 @@ m4_define([ATX_FEAT_PREREQ], [ fi ]) +dnl +dnl ATX_RUN_REPEAT(n, script) +dnl +dnl Run script n times. Cannot be nested. +dnl +m4_define([ATX_RUN_REPEAT], [ + atx_counter_=$1 + while test $atx_counter_ -gt 0; do + $2 + atx_counter_=$(($atx_counter_ - 1)) + done +]) + dnl dnl ATX_RUN_ONE(cmd) dnl @@ -71,10 +84,10 @@ m4_define([ATX_RUN], [ ]) dnl -dnl ATX_CHECK_SIMPLE(script) +dnl ATX_CHECK_SIMPLE(script, [cleanup]) dnl m4_define([ATX_CHECK_SIMPLE], [ - AT_CHECK([$1], , [ignore], [ignore]) + AT_CHECK([$1], , [ignore], [ignore], [$2], [$2]) ]) dnl