17dea50454afada083188d76e468b30b783cf698
[platform/upstream/coreutils.git] / tests / misc / shuf
1 #!/bin/sh
2 # Ensure that shuf randomizes its input.
3
4 if test "$VERBOSE" = yes; then
5   set -x
6   shuf --version
7 fi
8
9 pwd=`pwd`
10 t0=`echo "$0"|sed 's,.*/,,'`.tmp; tmp=$t0/$$
11 trap 'status=$?; cd $pwd; chmod -R u+rwx $t0; rm -rf $t0 && exit $status' 0
12 trap '(exit $?); exit $?' 1 2 13 15
13
14 framework_failure=0
15 mkdir -p $tmp || framework_failure=1
16 cd $tmp || framework_failure=1
17 seq 100 > in || framework_failure=1
18
19 if test $framework_failure = 1; then
20   echo "$0: failure in testing framework" 1>&2
21   (exit 1); exit 1
22 fi
23
24 fail=0
25
26 shuf in >out || fail=1
27
28 # Fail if the input is the same as the output.
29 # This is a probabilistic test :-)
30 # However, the odds of failure are very low: 1 in 100! (~ 1 in 10^158)
31 cmp in out > /dev/null && { fail=1; echo "not random?" 1>&2; }
32
33 # Fail if the sorted output is not the same as the input.
34 sort -n out > out1
35 cmp in out1 || { fail=1; echo "not a permutation" 1>&2; }
36
37 (exit $fail); exit $fail