| 1 |
#!/bin/sh |
| 2 |
# Unit Tests for GNU Datamash - perform simple calculation on input data |
| 3 |
|
| 4 |
# Copyright (C) 2014-2021 Assaf Gordon <[email protected]> |
| 5 |
# |
| 6 |
# This file is part of GNU Datamash. |
| 7 |
# |
| 8 |
# GNU Datamash is free software: you can redistribute it and/or modify |
| 9 |
# it under the terms of the GNU General Public License as published by |
| 10 |
# the Free Software Foundation, either version 3 of the License, or |
| 11 |
# (at your option) any later version. |
| 12 |
# |
| 13 |
# GNU Datamash is distributed in the hope that it will be useful, |
| 14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
# GNU General Public License for more details. |
| 17 |
# |
| 18 |
# You should have received a copy of the GNU General Public License |
| 19 |
# along with GNU Datamash. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
# |
| 21 |
# Written by Assaf Gordon |
| 22 |
|
| 23 |
## |
| 24 |
## This script tests the randomness of the 'rand' operation |
| 25 |
## |
| 26 |
|
| 27 |
. "${test_dir=.}/init.sh"; path_prepend_ ./src |
| 28 |
|
| 29 |
fail=0 |
| 30 |
|
| 31 |
require_paste_ |
| 32 |
|
| 33 |
## Ensure seq is useable |
| 34 |
openbsd_seq_replacement_ |
| 35 |
seq 10 >/dev/null 2>/dev/null || |
| 36 |
skip_ "requires a working seq" |
| 37 |
|
| 38 |
|
| 39 |
## |
| 40 |
## --- First test --- |
| 41 |
## |
| 42 |
## select a random number between 0 and 9, |
| 43 |
## repeat selection for 1000 times. |
| 44 |
## Each digit should be returned at least once |
| 45 |
## (unless we're extremely unlucky...) |
| 46 |
|
| 47 |
INPUT=$(seq 0 9) || framework_failure_ "generating INPUT failed" |
| 48 |
|
| 49 |
for i in $(seq 1000) ; |
| 50 |
do |
| 51 |
echo "$INPUT" | datamash rand 1 |
| 52 |
done > out_rand1 || framework_failure_ "test1 failed: datamash error" |
| 53 |
|
| 54 |
## First Check: each number should be there once |
| 55 |
RESULT=$(cat out_rand1 | sort -n | uniq | paste -d , -s -) || |
| 56 |
framework_failure_ "test1 failed: error preparing first check" |
| 57 |
|
| 58 |
[ "$RESULT" = "0,1,2,3,4,5,6,7,8,9" ] || |
| 59 |
{ warn_ "test1 failed. RESULT='$RESULT'." ; fail=1 ; } |
| 60 |
|
| 61 |
|
| 62 |
## Second check - we expect (hope?) the distribution is uniform, |
| 63 |
## and each number appears more-or-less equaly. |
| 64 |
## This is a poor-man's way of quasi-validation... |
| 65 |
## Using 'datamash', count how many times each number appears, |
| 66 |
## then, find the smallest count - in a uniform distribution, |
| 67 |
## we expect each number to appear close to 100 times (1000 draws of 10 items). |
| 68 |
## |
| 69 |
## NOTE: |
| 70 |
## We use 'datamash' to validate itself... but only after assuming the |
| 71 |
## basic operations (sort, group, count, min) have been already tested. |
| 72 |
RESULT=$(cat out_rand1 | |
| 73 |
datamash --sort --group 1 count 1 | |
| 74 |
datamash min 2) || |
| 75 |
framework_failure_ "test1 failed: error preparing second check" |
| 76 |
|
| 77 |
## We set the cut-off at 60 - if any number appeared less than 60 times, |
| 78 |
## we *might* have a problem in the uniform randomness in 'datamash'. |
| 79 |
if [ "$RESULT" -lt "60" ] ; then |
| 80 |
warn_ "Possible unifority problem in 'rand' operation." |
| 81 |
echo "--- distribution of numbers ---" |
| 82 |
cat out_rand1 | datamash --sort --group 1 count 1 |
| 83 |
echo "--- end ---" |
| 84 |
echo "--- 1000 random draws start here---" |
| 85 |
cat out_rand1 |
| 86 |
echo "---- end ----" |
| 87 |
fail=1 |
| 88 |
fi |
| 89 |
|
| 90 |
|
| 91 |
Exit $fail |