Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
TST: Thoroughly test prng for entropy and uniform values
  • Loading branch information
pan14001 committed May 16, 2019
1 parent 81eebc5 commit 7e9ce07
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions tests/prng.bats
Expand Up @@ -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 ]
}

0 comments on commit 7e9ce07

Please sign in to comment.