diff options
author | Assaf Gordon <assafgordon@gmail.com> | 2013-07-04 13:26:45 -0600 |
---|---|---|
committer | Pádraig Brady <P@draigBrady.com> | 2013-07-11 01:17:31 +0100 |
commit | 3a84293987bd21a92071a3d1c605ec9a2b3af1b4 (patch) | |
tree | 66aedec3a3800ba35045921fadb63fea557077e6 /tests | |
parent | f3fa3b2990c13623b80439039a92f72e08bb42be (diff) | |
download | coreutils-3a84293987bd21a92071a3d1c605ec9a2b3af1b4.tar.xz |
shuf: add --repetition to support repetition in output
main(): Process new option. Replace input_numbers_option_used()
with a local variable. Re-organize argument processing.
usage(): Describe the new option.
(write_random_numbers): A new function to generate a
permutation of the specified input range with repetition.
(write_random_lines): Likewise for stdin and --echo.
(write_permuted_numbers): New function refactored from
write_permuted_output().
(write_permuted_lines): Likewise.
* tests/misc/shuf.sh: Add tests for --repetitions option.
* doc/coreutils.texi: Mention --repetitions, add examples.
* TODO: Mention an optimization to avoid needing to
read all of the input into memory with --repetitions.
* NEWS: Mention new shuf option.
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/misc/shuf.sh | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/tests/misc/shuf.sh b/tests/misc/shuf.sh index 492fd4186..a25a6f8ad 100755 --- a/tests/misc/shuf.sh +++ b/tests/misc/shuf.sh @@ -94,4 +94,66 @@ shuf -i0-9 -o A -o B && shuf -i0-9 --random-source A --random-source B && { fail=1; echo "shuf did not detect multiple --random-source usage.">&2 ; } +# Test --repetition option + +# --repetition without count should return one line +shuf --rep -i0-10 > exp || framework_failure_ +c=$(wc -l < exp) || framework_failure_ +test "$c" -eq 1 || { fail=1; echo "--repetition default count is not 1">&2 ; } + +# --repetition can output more values than the input range +shuf --rep -i0-9 -n1000 > exp || framework_failure_ +c=$(wc -l < exp) || framework_failure_ +test "$c" -eq 1000 || { fail=1; echo "--repetition with --count failed">&2 ; } + +# Check output values (this is not bullet-proof, but drawing 1000 values +# between 0 and 9 should produce all values, unless there's a bug in shuf +# or a very poor random source, or extremely bad luck) +c=$(sort -nu exp | paste -s -d ' ') || framework_failure_ +test "$c" = "0 1 2 3 4 5 6 7 8 9" || + { fail=1; echo "--repetition produced bad output">&2 ; } + +# check --repetition with non-zero low value +shuf --rep -i222-233 -n2000 > exp || framework_failure_ +c=$(cat exp | sort -nu | paste -s -d ' ') || framework_failure_ +test "$c" = "222 223 224 225 226 227 228 229 230 231 232 233" || + { fail=1; echo "--repetition produced bad output with non-zero low">&2 ; } + +# --repetition,-i,count=0 should not fail and produce no output +shuf --rep -i0-9 -n0 > exp || framework_failure_ +# file size should be zero (no output from shuf) +test \! -s exp || + { fail=1; echo "--repetition,-i0-9,-n0 produced bad output">&2 ; } + +# --repetition with -e, without count, should return one line +shuf --rep -e A B C D > exp || framework_failure_ +c=$(cat exp | wc -l) || framework_failure_ +test "$c" -eq 1 || + { fail=1; echo "--repetition,-e default count is not 1">&2 ; } + +# --repetition with STDIN, without count, should return one line +printf "A\nB\nC\nD\nE\n" | shuf --rep > exp || framework_failure_ +c=$(wc -l < exp) || framework_failure_ +test "$c" -eq 1 || + { fail=1; echo "--repetition,STDIN default count is not 1">&2 ; } + +# --repetition with STDIN,count - can return move values than input lines +printf "A\nB\nC\nD\nE\n" | shuf --rep -n2000 > exp || framework_failure_ +c=$(wc -l < exp) || framework_failure_ +test "$c" -eq 2000 || + { fail=1; echo "--repetition,STDIN,count failed">&2 ; } + +# Check output values (this is not bullet-proof, but drawing 2000 values +# between A and E should produce all values, unless there's a bug in shuf +# or a very poor random source, or extremely bad luck) +c=$(sort -u exp | paste -s -d ' ') || framework_failure_ +test "$c" = "A B C D E" || + { fail=1; echo "--repetition,STDIN,count produced bad output">&2 ; } + +# --repetition,stdin,count=0 should not fail and produce no output +printf "A\nB\nC\nD\nE\n" | shuf --rep -n0 > exp || framework_failure_ +# file size should be zero (no output from shuf) +test \! -s exp || + { fail=1; echo "--repetition,STDIN,-n0 produced bad output">&2 ; } + Exit $fail |