From 7e9ce072b7ffc0572e11e603b39b36a9a50f192a Mon Sep 17 00:00:00 2001 From: Pariksheet Nanda Date: Thu, 16 May 2019 16:59:10 -0400 Subject: [PATCH] TST: Thoroughly test prng for entropy and uniform values --- tests/prng.bats | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/tests/prng.bats b/tests/prng.bats index 7fc3ced..302d272 100644 --- a/tests/prng.bats +++ b/tests/prng.bats @@ -6,24 +6,37 @@ true # shellcheck source=../parallel_opts.sh . "${BATS_TEST_DIRNAME}/../examples/script_that_sometimes_fails.sh" -@test 'prng fails at least 4 times' { +@test 'prng produces a uniform distribution with a single zero, and has entropy' { # shellcheck disable=2034 SLURM_NTASKS=5 + not_sorted=0 for seed in {0..9} do result=() - sum=0 + num_zeros=0 for ID in {0..4} do x=$(prng) - if (( x % SLURM_NTASKS != 0 )) + if (( x % SLURM_NTASKS == 0 )) then - sum=$(( sum + 1 )) + num_zeros=$(( num_zeros + 1 )) fi result[ID]=$x done - echo "seed: $seed, prng: ${result[*]}, sum: $sum" - [[ $sum -ge 4 ]] + echo "seed: $seed, prng: ${result[*]}, num_zeros: $num_zeros" + [[ $num_zeros -eq 1 ]] + # Verify it contains all the values / is a uniform distribution. + expected=( $(seq 0 4) ) + IFS=$'\n' sorted=($(sort <<<"${result[*]}")) + unset IFS + [[ "${sorted[*]}" = "${expected[*]}" ]] + if [[ "${sorted[*]}" != "${result[*]}" ]] + then + not_sorted=$(( not_sorted + 1)) + fi done + # Check for entropy. + echo "not_sorted: $not_sorted" + [ $not_sorted -ge 6 ] }