| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
# Unit Tests for GNU Datamash - perform simple calculation on input data |
| 4 |
|
| 5 |
# Copyright (C) 2014-2021 Assaf Gordon <[email protected]> |
| 6 |
# |
| 7 |
# This file is part of GNU Datamash. |
| 8 |
# |
| 9 |
# GNU Datamash is free software: you can redistribute it and/or modify |
| 10 |
# it under the terms of the GNU General Public License as published by |
| 11 |
# the Free Software Foundation, either version 3 of the License, or |
| 12 |
# (at your option) any later version. |
| 13 |
# |
| 14 |
# GNU Datamash is distributed in the hope that it will be useful, |
| 15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
# GNU General Public License for more details. |
| 18 |
# |
| 19 |
# You should have received a copy of the GNU General Public License |
| 20 |
# along with GNU Datamash If not, see <https://www.gnu.org/licenses/>. |
| 21 |
# |
| 22 |
# Written by Assaf Gordon. |
| 23 |
|
| 24 |
. "${test_dir=.}/init.sh"; path_prepend_ ./src |
| 25 |
|
| 26 |
expensive_ |
| 27 |
require_valgrind_ |
| 28 |
|
| 29 |
## Don't use valgrind on statically-compiled binary |
| 30 |
## (it gives some false-positives and the test fails). |
| 31 |
if which ldd >/dev/null ; then |
| 32 |
## Tricky implicit assumption: |
| 33 |
## If the system has "ldd" - we can test if this is a static binary. |
| 34 |
## If the system doesn't have "ldd", we can't test it, and we'll assume |
| 35 |
## we can valgrind without false-positives. |
| 36 |
## This is relevant for Mac OS X, where static binaries are discouraged and |
| 37 |
## difficult to create |
| 38 |
## (https://developer.apple.com/library/mac/qa/qa1118/_index.html) |
| 39 |
ldd $(which datamash) >/dev/null 2>/dev/null || |
| 40 |
skip_ "skipping valgrind test for a non-dynamic-binary datamash" |
| 41 |
fi |
| 42 |
|
| 43 |
|
| 44 |
## Prepare file with long fields - for first/last operations. |
| 45 |
( printf "A " ; seq 100 | paste -s -d "" ; |
| 46 |
printf "A " ; seq 1000 | paste -s -d "" ; |
| 47 |
printf "A " ; seq 2000 | paste -s -d "" ; |
| 48 |
printf "A " ; seq 3000 | paste -s -d "" ; |
| 49 |
printf "A 1\n" ; |
| 50 |
printf "B " ; seq 1000 | paste -s -d "" ; |
| 51 |
printf "B " ; seq 2000 | paste -s -d "" ; |
| 52 |
printf "B " ; seq 3000 | paste -s -d "" ; |
| 53 |
printf "B " ; seq 100 | paste -s -d "" ; |
| 54 |
) > in_first || framework_failure_ "failed to prepare 'in_first' file" |
| 55 |
|
| 56 |
|
| 57 |
## Prepare file with many rows, to test transpose/reverse |
| 58 |
seq -w 16000 | paste - - - - > in_4k_rows || |
| 59 |
framework_failure_ "failed to prepare 'in_4k_rows' file" |
| 60 |
seq -w 16000 | paste - - - - - - - - > in_2k_rows || |
| 61 |
framework_failure_ "failed to prepare 'in_2k_rows' file" |
| 62 |
|
| 63 |
## Prepare file with many columns and 100 rows, to test transpose/reverse |
| 64 |
( for i in $(seq 0 99) ; do |
| 65 |
seq $((i*1000)) $((i*1000+999)) | paste -s |
| 66 |
done |
| 67 |
) > in_1k_cols || framework_failure_ "failed to prepare 'in_1k_cols' file" |
| 68 |
|
| 69 |
## Prepare large file |
| 70 |
seq -w 1000000 > 1M || framework_failure_ "failed to prepare '1M' file" |
| 71 |
( shuf 1M ; shuf 1M ; |
| 72 |
shuf 1M ; shuf 1M ; |
| 73 |
seq -w 999000 1001000 ) > 4M || |
| 74 |
framework_failure_ "failed to prepare '4M' file" |
| 75 |
seq -w 1001000 > 1M1K || framework_failure_ "failed to prepare '1M1K' file" |
| 76 |
|
| 77 |
## Prepare a file with very wide fields |
| 78 |
for i in $(seq 1 193 2000) $(seq 500 -7 100); |
| 79 |
do |
| 80 |
seq $i | paste -s -d "" - ; |
| 81 |
done > wide || |
| 82 |
framework_failure_ "failed to prepare 'wide' file" |
| 83 |
|
| 84 |
fail=0 |
| 85 |
|
| 86 |
seq 10000 | valgrind --track-origins=yes --show-reachable=yes \ |
| 87 |
--leak-check=full --error-exitcode=1 \ |
| 88 |
datamash unique 1 > /dev/null || |
| 89 |
{ warn_ "unique 1 - failed" ; fail=1 ; } |
| 90 |
|
| 91 |
seq 10000 | sed 's/^/group /' | |
| 92 |
valgrind --track-origins=yes --leak-check=full \ |
| 93 |
--show-reachable=yes --error-exitcode=1 \ |
| 94 |
datamash -g 1 unique 1 > /dev/null || |
| 95 |
{ warn_ "-g 1 unique 1 - failed" ; fail=1 ; } |
| 96 |
|
| 97 |
seq 10000 | valgrind --track-origins=yes --leak-check=full \ |
| 98 |
--show-reachable=yes --error-exitcode=1 \ |
| 99 |
datamash countunique 1 > /dev/null || |
| 100 |
{ warn_ "countunique 1 - failed" ; fail=1 ; } |
| 101 |
|
| 102 |
seq 10000 | valgrind --track-origins=yes --leak-check=full \ |
| 103 |
--show-reachable=yes --error-exitcode=1 \ |
| 104 |
datamash collapse 1 > /dev/null || |
| 105 |
{ warn_ "collapse 1 - failed" ; fail=1 ; } |
| 106 |
|
| 107 |
(echo "values" ; seq 10000 ) | |
| 108 |
valgrind --track-origins=yes --leak-check=full \ |
| 109 |
--show-reachable=yes --error-exitcode=1 \ |
| 110 |
datamash -H countunique 1 > /dev/null || |
| 111 |
{ warn_ "-H collapse 1 - failed" ; fail=1 ; } |
| 112 |
|
| 113 |
(echo "values" ; seq 10000 ) | |
| 114 |
valgrind --track-origins=yes --leak-check=full \ |
| 115 |
--show-reachable=yes --error-exitcode=1 \ |
| 116 |
datamash -g 1 -H countunique 1 > /dev/null || |
| 117 |
{ warn_ "-g 1 -H collapse 1 - failed" ; fail=1 ; } |
| 118 |
|
| 119 |
cat "in_first" | valgrind --track-origins=yes --leak-check=full \ |
| 120 |
--show-reachable=yes --error-exitcode=1 \ |
| 121 |
datamash -W first 2 > /dev/null || |
| 122 |
{ warn_ "first 2 - failed" ; fail=1 ; } |
| 123 |
|
| 124 |
cat "in_first" | valgrind --track-origins=yes --leak-check=full \ |
| 125 |
--show-reachable=yes --error-exitcode=1 \ |
| 126 |
datamash -W -g 1 first 2 > /dev/null || |
| 127 |
{ warn_ "-g 1 first 2 - failed" ; fail=1 ; } |
| 128 |
|
| 129 |
cat "in_first" | valgrind --track-origins=yes --leak-check=full \ |
| 130 |
--show-reachable=yes --error-exitcode=1 \ |
| 131 |
datamash -W -g 1 last 2 > /dev/null || |
| 132 |
{ warn_ "-g 1 last 2 - failed" ; fail=1 ; } |
| 133 |
|
| 134 |
## Test transpose and reverse on multiple (medium-sized) inputs |
| 135 |
for INFILE in in_4k_rows in_2k_rows in_1k_cols ; |
| 136 |
do |
| 137 |
for CMD in transpose reverse ; |
| 138 |
do |
| 139 |
cat "$INFILE" | valgrind --track-origins=yes --leak-check=full \ |
| 140 |
--show-reachable=yes --error-exitcode=1 \ |
| 141 |
datamash "$CMD" > /dev/null || |
| 142 |
{ warn_ "$CMD failed on $INFILE" ; fail=1 ; } |
| 143 |
|
| 144 |
done |
| 145 |
done |
| 146 |
|
| 147 |
## Test remove-duplicates (using gnulib's hash) |
| 148 |
cat 4M | valgrind --track-origins=yes --leak-check=full \ |
| 149 |
--show-reachable=yes --error-exitcode=1 \ |
| 150 |
datamash rmdup 1 > rmdup_1M_1.t || |
| 151 |
{ warn_ "rmdup failed on 4M" ; fail=1 ; } |
| 152 |
sort < rmdup_1M_1.t > rmdup_1M_1 \ |
| 153 |
|| framework_failure_ "failed to sort rmdup_1M_1.t" |
| 154 |
cmp rmdup_1M_1 1M1K || |
| 155 |
{ warn_ "rmdup failed on 4M (output differences)" ; |
| 156 |
fail=1 ; } |
| 157 |
|
| 158 |
## Test remove-duplicates (using gnulib's hash), |
| 159 |
## with smaller memory buffers |
| 160 |
cat 4M | valgrind --track-origins=yes --leak-check=full \ |
| 161 |
--show-reachable=yes --error-exitcode=1 \ |
| 162 |
datamash ---rmdup-test rmdup 1 > rmdup_1M_2.t || |
| 163 |
{ warn_ "rmdup failed on 4M (2)" ; fail=1 ; } |
| 164 |
sort < rmdup_1M_2.t > rmdup_1M_2 \ |
| 165 |
|| framework_failure_ "failed to sort rmdup_1M_2.t" |
| 166 |
cmp rmdup_1M_2 1M1K || |
| 167 |
{ warn_ "rmdup (2) failed on 4M (output differences)" ; |
| 168 |
fail=1 ; } |
| 169 |
|
| 170 |
## Test Base64 encode/decode |
| 171 |
cat wide | valgrind --track-origins=yes --leak-check=full \ |
| 172 |
--show-reachable=yes --error-exitcode=1 \ |
| 173 |
datamash base64 1 > wide_base64 || |
| 174 |
{ warn_ "base64 failed on wide" ; fail=1 ; } |
| 175 |
cat wide_base64 | valgrind --track-origins=yes --leak-check=full \ |
| 176 |
--show-reachable=yes --error-exitcode=1 \ |
| 177 |
datamash debase64 1 > wide_orig || |
| 178 |
{ warn_ "debase64 failed on wide_base64" ; fail=1 ; } |
| 179 |
|
| 180 |
## Test Covariance (and paired-columns) |
| 181 |
cat in_4k_rows | valgrind --track-origins=yes --leak-check=full \ |
| 182 |
--show-reachable=yes --error-exitcode=1 \ |
| 183 |
datamash pcov 1:2 > /dev/null || |
| 184 |
{ warn_ "pcov 1:2 failed on in_4k_rows" ; fail=1 ; } |
| 185 |
|
| 186 |
cmp wide wide_orig || |
| 187 |
{ warn_ "base64 decoding failed (decoded output does not match original)"; |
| 188 |
fail=1 ; } |
| 189 |
|
| 190 |
## Test large output formats |
| 191 |
## datamash exits with an error on x86, because some numbers in the file "wide" |
| 192 |
## are too big for "long double" 80-bit floating point |
| 193 |
cat wide | valgrind --track-origins=yes --leak-check=full \ |
| 194 |
--show-reachable=yes --error-exitcode=2 \ |
| 195 |
datamash --format "%05000.5000f" sum 1 > /dev/null || |
| 196 |
{ test $? -eq 2 && { warn_ "custom-format failed" ; fail=1 ; } ; } |
| 197 |
|
| 198 |
Exit $fail |