| 1 |
#!/bin/sh |
| 2 |
# Unit Tests for GNU Datamash - perform simple calculation on input data |
| 3 |
|
| 4 |
# Copyright (C) 2015-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 strbin (string binning/hashing) operator |
| 25 |
## |
| 26 |
|
| 27 |
. "${test_dir=.}/init.sh"; path_prepend_ ./src |
| 28 |
|
| 29 |
fail=0 |
| 30 |
|
| 31 |
## Ensure seq is useable |
| 32 |
openbsd_seq_replacement_ |
| 33 |
seq 10 >/dev/null 2>/dev/null \ |
| 34 |
|| skip_ "requires a working seq" |
| 35 |
|
| 36 |
|
| 37 |
# Generate input |
| 38 |
seq 1000 | sed 's/^/id-/' > in \ |
| 39 |
|| framework_failure_ "generating INPUT failed" |
| 40 |
|
| 41 |
# bin into 10 groups |
| 42 |
datamash strbin 1 < in > out1 \ |
| 43 |
|| { warn_ "'datamash strbin 1' failed" ; fail=1 ; } |
| 44 |
|
| 45 |
# Check output values |
| 46 |
sort -n -u < out1 > out2 || framework_failure_ "failed to sort out1" |
| 47 |
|
| 48 |
|
| 49 |
# Default binning to 10 bins, accept only single digits |
| 50 |
grep '^[^0-9]$' < out2 > /dev/null \ |
| 51 |
&& { warn_ "'datamash strbin 1' generated invalid output (out2):" ; |
| 52 |
cat out2 >&2 ; |
| 53 |
fail=1 ; } |
| 54 |
|
| 55 |
# Test binning into varying number of bins |
| 56 |
for i in 5 10 100 300 ; |
| 57 |
do |
| 58 |
datamash strbin:$i 1 < in > out-$i \ |
| 59 |
|| { warn_ "'datamash strbin:$i 1' failed" ; fail=1 ; break ; } |
| 60 |
|
| 61 |
# Check output values |
| 62 |
max=$(sort -n -u -r < out-$i | head -n1) |
| 63 |
|
| 64 |
test -n "$max" \ |
| 65 |
|| { warn_ "'datamash strbin:$i 1' failed - max output is empty" ; |
| 66 |
fail=1 ; |
| 67 |
break ; } |
| 68 |
|
| 69 |
test "$max" -gt 0 \ |
| 70 |
|| { warn_ "'datamash strbin:$i 1' failed - max value too small ($max)"; |
| 71 |
fail=1 ; |
| 72 |
break ; } |
| 73 |
|
| 74 |
test "$max" -lt "$i" \ |
| 75 |
|| { warn_ "'datamash strbin:$i 1' failed - max value too large ($max)"; |
| 76 |
fail=1 ; |
| 77 |
break ; } |
| 78 |
done |
| 79 |
|
| 80 |
|
| 81 |
# Same srting must result in the same bin, |
| 82 |
# in the same run and in different runs. |
| 83 |
# (the returned value, however, is machine-dependant) |
| 84 |
|
| 85 |
text="hello-42-world" |
| 86 |
for i in 5 10 100 300 ; |
| 87 |
do |
| 88 |
bin1=$(printf "%s\n%s\n%s\n" "$text" "$text" "$text" \ |
| 89 |
| datamash strbin:$i 1 | uniq) |
| 90 |
bin2=$(printf "%s\n" "$text" \ |
| 91 |
| datamash strbin:$i 1 | uniq) |
| 92 |
|
| 93 |
test -n "$bin1" \ |
| 94 |
|| { warn_ "'datamash strbin:$i 1' failed on text '$text' - empty"; |
| 95 |
fail=1 ; |
| 96 |
break ; } |
| 97 |
|
| 98 |
test "x$bin1" = "x$bin2" \ |
| 99 |
|| { warn_ "'datamash strbin:$i 1' failed on text '$text' - " \ |
| 100 |
"bin1 ($bin1) doesn't match bin2 ($bin2)" ; |
| 101 |
fail=1 ; |
| 102 |
break ; } |
| 103 |
done |
| 104 |
|
| 105 |
|
| 106 |
Exit $fail |