| 1 |
#!/usr/bin/env perl |
| 2 |
=pod |
| 3 |
Unit Tests for GNU Datamash - perform simple calculation on input data |
| 4 |
|
| 5 |
Copyright (C) 2013-2021 Assaf Gordon <[email protected]> |
| 6 |
Copyright (C) 2022-2025 Timothy Rice <[email protected]> |
| 7 |
|
| 8 |
This file is part of GNU Datamash. |
| 9 |
|
| 10 |
GNU Datamash is free software: you can redistribute it and/or modify |
| 11 |
it under the terms of the GNU General Public License as published by |
| 12 |
the Free Software Foundation, either version 3 of the License, or |
| 13 |
(at your option) any later version. |
| 14 |
|
| 15 |
GNU Datamash is distributed in the hope that it will be useful, |
| 16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
GNU General Public License for more details. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License |
| 21 |
along with GNU Datamash. If not, see <https://www.gnu.org/licenses/>. |
| 22 |
|
| 23 |
Written by Assaf Gordon. |
| 24 |
=cut |
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
|
| 28 |
# Until a better way comes along to auto-use Coreutils Perl modules |
| 29 |
# as in the coreutils' autotools system. |
| 30 |
use Coreutils; |
| 31 |
use CuSkip; |
| 32 |
use CuTmpdir qw(datamash); |
| 33 |
use MIME::Base64 ; |
| 34 |
|
| 35 |
(my $program_name = $0) =~ s|.*/||; |
| 36 |
my $prog_bin = 'datamash'; |
| 37 |
|
| 38 |
|
| 39 |
# MAX_IDENTIFIER_LENGTH is defined in op-scanner.h |
| 40 |
my $ident_too_long = "x" x 512 ; |
| 41 |
|
| 42 |
## Cross-Compiling portability hack: |
| 43 |
## under qemu/binfmt, argv[0] (which is used to report errors) will contain |
| 44 |
## the full path of the binary, if the binary is on the $PATH. |
| 45 |
## So we try to detect what is the actual returned value of the program |
| 46 |
## in case of an error. |
| 47 |
my $prog = `$prog_bin ---print-progname`; |
| 48 |
$prog = $prog_bin unless $prog; |
| 49 |
|
| 50 |
# TODO: add localization tests with "grouping" |
| 51 |
# Turn off localization of executable's output. |
| 52 |
@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3; |
| 53 |
|
| 54 |
my @Tests = |
| 55 |
( |
| 56 |
# Invalid numeric value for column prasing should be treated as named column |
| 57 |
['e1', 'sum 1x', {IN_PIPE=>""}, {EXIT=>1}, |
| 58 |
{ERR=>"$prog: invalid numeric value '1x'\n"}], |
| 59 |
|
| 60 |
# Processing mode without operation |
| 61 |
['e2','groupby 1', {IN_PIPE=>""}, {EXIT=>1}, |
| 62 |
{ERR=>"$prog: missing operation\n"}], |
| 63 |
|
| 64 |
# invalid operation after valid mode |
| 65 |
['e3','groupby 1 foobar 2', {IN_PIPE=>""}, {EXIT=>1}, |
| 66 |
{ERR=>"$prog: invalid operation 'foobar'\n"}], |
| 67 |
|
| 68 |
# missing field number after processing mode |
| 69 |
['e4','groupby', {IN_PIPE=>""}, {EXIT=>1}, |
| 70 |
{ERR=>"$prog: missing field for operation 'groupby'\n"}], |
| 71 |
|
| 72 |
# Field range with invalid syntax |
| 73 |
['e20','sum 1-', {IN_PIPE=>""}, {EXIT=>1}, |
| 74 |
{ERR=>"$prog: invalid field range for operation 'sum'\n"}], |
| 75 |
['e21','sum 1-x', {IN_PIPE=>""}, {EXIT=>1}, |
| 76 |
{ERR=>"$prog: field range for 'sum' must be numeric\n"}], |
| 77 |
['e22','sum 4-2', {IN_PIPE=>""}, {EXIT=>1}, |
| 78 |
{ERR=>"$prog: invalid field range for operation 'sum'\n"}], |
| 79 |
# zero in range |
| 80 |
['e23','sum 0-2', {IN_PIPE=>""}, {EXIT=>1}, |
| 81 |
{ERR=>"$prog: invalid field '0' for operation 'sum'\n"}], |
| 82 |
['e24','sum 1-0', {IN_PIPE=>""}, {EXIT=>1}, |
| 83 |
{ERR=>"$prog: invalid field '0' for operation 'sum'\n"}], |
| 84 |
#Negative in range |
| 85 |
['e25','sum 1--5', {IN_PIPE=>""}, {EXIT=>1}, |
| 86 |
{ERR=>"$prog: invalid field range for operation 'sum'\n"}], |
| 87 |
|
| 88 |
# Test field pair syntax |
| 89 |
['e41','pcov 1', {IN_PIPE=>""}, {EXIT=>1}, |
| 90 |
{ERR=>"$prog: operation 'pcov' requires field pairs\n"}], |
| 91 |
['e42','pcov 1:', {IN_PIPE=>""}, {EXIT=>1}, |
| 92 |
{ERR=>"$prog: invalid field pair for operation 'pcov'\n"}], |
| 93 |
['e43','pcov :', {IN_PIPE=>""}, {EXIT=>1}, |
| 94 |
{ERR=>"$prog: invalid field pair for operation 'pcov'\n"}], |
| 95 |
['e44','pcov :1', {IN_PIPE=>""}, {EXIT=>1}, |
| 96 |
{ERR=>"$prog: invalid field pair for operation 'pcov'\n"}], |
| 97 |
['e46','pcov hello:world', {IN_PIPE=>""}, {EXIT=>1}, |
| 98 |
{ERR=>"$prog: -H or --header-in must be used with named columns\n"}], |
| 99 |
['e47','sum 1:3', {IN_PIPE=>""}, {EXIT=>1}, |
| 100 |
{ERR=>"$prog: operation 'sum' cannot use pair of fields\n"}], |
| 101 |
['e50','dotprod 1', {IN_PIPE=>""}, {EXIT=>1}, |
| 102 |
{ERR=>"$prog: operation 'dotprod' requires field pairs\n"}], |
| 103 |
['e51','dotprod 1:', {IN_PIPE=>""}, {EXIT=>1}, |
| 104 |
{ERR=>"$prog: invalid field pair for operation 'dotprod'\n"}], |
| 105 |
['e52','dotprod :', {IN_PIPE=>""}, {EXIT=>1}, |
| 106 |
{ERR=>"$prog: invalid field pair for operation 'dotprod'\n"}], |
| 107 |
['e53','dotprod :1', {IN_PIPE=>""}, {EXIT=>1}, |
| 108 |
{ERR=>"$prog: invalid field pair for operation 'dotprod'\n"}], |
| 109 |
['e54','dotprod hello:world', {IN_PIPE=>""}, {EXIT=>1}, |
| 110 |
{ERR=>"$prog: -H or --header-in must be used with named columns\n"}], |
| 111 |
|
| 112 |
# Test scanner edge-cases |
| 113 |
# Floating point value |
| 114 |
['e60','sum 4.5', {IN_PIPE=>""}, {EXIT=>1}, |
| 115 |
{ERR=>"$prog: invalid field '4.5' for operation 'sum'\n"}], |
| 116 |
['e61','sum 4.', {IN_PIPE=>""}, {EXIT=>1}, |
| 117 |
{ERR=>"$prog: invalid field '4.' for operation 'sum'\n"}], |
| 118 |
|
| 119 |
# invalid numbers |
| 120 |
['e62','sum 4a', {IN_PIPE=>""}, {EXIT=>1}, |
| 121 |
{ERR=>"$prog: invalid numeric value '4a'\n"}], |
| 122 |
['e63','sum 4_', {IN_PIPE=>""}, {EXIT=>1}, |
| 123 |
{ERR=>"$prog: invalid numeric value '4_'\n"}], |
| 124 |
# Overflow strtol |
| 125 |
['e64','sum 1234567890123456789012345678901234567', {IN_PIPE=>""}, {EXIT=>1}, |
| 126 |
{ERR=>"$prog: invalid numeric value " . |
| 127 |
"'1234567890123456789012345678901234567'\n"}], |
| 128 |
# Invalid charcters |
| 129 |
['e65','sum "foo^bar"', {IN_PIPE=>""}, {EXIT=>1}, |
| 130 |
{ERR=>"$prog: invalid operand '^bar'\n"}], |
| 131 |
|
| 132 |
# Empty columns |
| 133 |
['e66','sum 1,,', {IN_PIPE=>""}, {EXIT=>1}, |
| 134 |
{ERR=>"$prog: missing field for operation 'sum'\n"}], |
| 135 |
|
| 136 |
# Range with names instead of numbers |
| 137 |
['e67','sum foo-bar', {IN_PIPE=>""}, {EXIT=>1}, |
| 138 |
{ERR=>"$prog: field range for 'sum' must be numeric\n"}], |
| 139 |
|
| 140 |
# Invalid numeric value for column prasing should be treated as named column |
| 141 |
['e70', 'sum 1x', {IN_PIPE=>""}, {EXIT=>1}, |
| 142 |
{ERR=>"$prog: invalid numeric value '1x'\n"}], |
| 143 |
|
| 144 |
# Processing mode without operation |
| 145 |
['e71','groupby 1', {IN_PIPE=>""}, {EXIT=>1}, |
| 146 |
{ERR=>"$prog: missing operation\n"}], |
| 147 |
|
| 148 |
# invalid operation after valid mode |
| 149 |
['e72','groupby 1 foobar 2', {IN_PIPE=>""}, {EXIT=>1}, |
| 150 |
{ERR=>"$prog: invalid operation 'foobar'\n"}], |
| 151 |
|
| 152 |
# missing field number after processing mode |
| 153 |
['e73','groupby', {IN_PIPE=>""}, {EXIT=>1}, |
| 154 |
{ERR=>"$prog: missing field for operation 'groupby'\n"}], |
| 155 |
|
| 156 |
# Bin and optional parameters |
| 157 |
['e80','bin:10:30 1', {IN_PIPE=>""}, {EXIT=>1}, |
| 158 |
{ERR=>"$prog: too many parameters for operation 'bin'\n"}], |
| 159 |
['e81','bin: 1', {IN_PIPE=>""}, {EXIT=>1}, |
| 160 |
{ERR=>"$prog: missing parameter for operation 'bin'\n"}], |
| 161 |
|
| 162 |
# NOTE about the message: because the parser first parses parameters, |
| 163 |
# then checks if the operation actually needs parameters, the |
| 164 |
# error first complains about 'missing' because there are colons |
| 165 |
# but no numeric values. |
| 166 |
['e82','sum: 1', {IN_PIPE=>""}, {EXIT=>1}, |
| 167 |
{ERR=>"$prog: missing parameter for operation 'sum'\n"}], |
| 168 |
|
| 169 |
['e83','bin:10: 1', {IN_PIPE=>""}, {EXIT=>1}, |
| 170 |
{ERR=>"$prog: missing parameter for operation 'bin'\n"}], |
| 171 |
|
| 172 |
# These ensures the '1' is not accidentally parsed as the field number |
| 173 |
['e84','bin:10:1', {IN_PIPE=>""}, {EXIT=>1}, |
| 174 |
{ERR=>"$prog: missing field for operation 'bin'\n"}], |
| 175 |
['e85','bin:10, 1', {IN_PIPE=>""}, {EXIT=>1}, |
| 176 |
{ERR=>"$prog: missing field for operation 'bin'\n"}], |
| 177 |
['e86','bin:, 1', {IN_PIPE=>""}, {EXIT=>1}, |
| 178 |
{ERR=>"$prog: invalid parameter , for operation 'bin'\n"}], |
| 179 |
['e87','bin, 1', {IN_PIPE=>""}, {EXIT=>1}, |
| 180 |
{ERR=>"$prog: missing field for operation 'bin'\n"}], |
| 181 |
['e88','bin:- 1', {IN_PIPE=>""}, {EXIT=>1}, |
| 182 |
{ERR=>"$prog: invalid parameter - for operation 'bin'\n"}], |
| 183 |
['e89','sum:10 1', {IN_PIPE=>""}, {EXIT=>1}, |
| 184 |
{ERR=>"$prog: too many parameters for operation 'sum'\n"}], |
| 185 |
|
| 186 |
# Invalid field specifications for primary operations |
| 187 |
['e90', 'groupby 1:2', {IN_PIPE=>""}, {EXIT=>1}, |
| 188 |
{ERR=>"$prog: invalid field pair for operation 'groupby'\n"}], |
| 189 |
|
| 190 |
# values for strbin operation |
| 191 |
['e92','strbin:- 1', {IN_PIPE=>""}, {EXIT=>1}, |
| 192 |
{ERR=>"$prog: invalid parameter - for operation 'strbin'\n"}], |
| 193 |
['e93','strbin:0 1', {IN_PIPE=>""}, {EXIT=>1}, |
| 194 |
{ERR=>"$prog: strbin bucket size must not be zero\n"}], |
| 195 |
|
| 196 |
# values for percentile operation |
| 197 |
['e94','perc:0 1', {IN_PIPE=>""}, {EXIT=>1}, |
| 198 |
{ERR=>"$prog: invalid percentile value 0\n"}], |
| 199 |
['e95','perc:101 1', {IN_PIPE=>""}, {EXIT=>1}, |
| 200 |
{ERR=>"$prog: invalid percentile value 101\n"}], |
| 201 |
['e96','perc:foo 1', {IN_PIPE=>""}, {EXIT=>1}, |
| 202 |
{ERR=>"$prog: invalid parameter foo for operation 'perc'\n"}], |
| 203 |
['e97','perc:-32 1', {IN_PIPE=>""}, {EXIT=>1}, |
| 204 |
{ERR=>"$prog: invalid parameter - for operation 'perc'\n"}], |
| 205 |
['e98','perc:1:2 1', {IN_PIPE=>""}, {EXIT=>1}, |
| 206 |
{ERR=>"$prog: too many parameters for operation 'perc'\n"}], |
| 207 |
|
| 208 |
# Invalid output delimiters |
| 209 |
['e100', '--output-delimiter', {IN_PIPE=>""}, {EXIT=>1}, |
| 210 |
{ERR_SUBST=>'s/requires an argument -- output-delimiter/' . |
| 211 |
'\'--output-delimiter\' requires an argument/'}, |
| 212 |
{ERR=>"$prog: option '--output-delimiter' requires an argument\n". |
| 213 |
"Try '$prog --help' for more information.\n"}], |
| 214 |
['e101', '--output-delimiter ""', {IN_PIPE=>""}, {EXIT=>1}, |
| 215 |
{ERR=>"$prog: the delimiter must be a single character\n"}], |
| 216 |
['e102', '--output-delimiter "XX"', {IN_PIPE=>""}, {EXIT=>1}, |
| 217 |
{ERR=>"$prog: the delimiter must be a single character\n"}], |
| 218 |
|
| 219 |
# values for trimmean operation |
| 220 |
['e103','trimmean:12 1', {IN_PIPE=>""}, {EXIT=>1}, |
| 221 |
{ERR=>"$prog: invalid trim mean value 12 (expected 0 <= X <= 0.5)\n"}], |
| 222 |
['e104','trimmean:0.51 1', {IN_PIPE=>""}, {EXIT=>1}, |
| 223 |
{ERR=>"$prog: invalid trim mean value 0.51 (expected 0 <= X <= 0.5)\n"}], |
| 224 |
['e105','trimmean:-32 1', {IN_PIPE=>""}, {EXIT=>1}, |
| 225 |
{ERR=>"$prog: invalid parameter - for operation 'trimmean'\n"}], |
| 226 |
['e106','trimmean:1:2 1', {IN_PIPE=>""}, {EXIT=>1}, |
| 227 |
{ERR=>"$prog: too many parameters for operation 'trimmean'\n"}], |
| 228 |
|
| 229 |
# Rounding |
| 230 |
['e110','--round ""', {IN_PIPE=>""}, {EXIT=>1}, |
| 231 |
{ERR=>"$prog: missing rounding digits value\n"}], |
| 232 |
['e111','--round "3a"', {IN_PIPE=>""}, {EXIT=>1}, |
| 233 |
{ERR=>"$prog: invalid rounding digits value '3a'\n"}], |
| 234 |
# Rounding Currently hard-coded to 1 to 50 decimal-point digits. |
| 235 |
['e112','--round "0"', {IN_PIPE=>""}, {EXIT=>1}, |
| 236 |
{ERR=>"$prog: invalid rounding digits value '0'\n"}], |
| 237 |
['e113','--round "51"', {IN_PIPE=>""}, {EXIT=>1}, |
| 238 |
{ERR=>"$prog: invalid rounding digits value '51'\n"}], |
| 239 |
|
| 240 |
# Custom Output Formats |
| 241 |
['e120','--format ""', {IN_PIPE=>""}, {EXIT=>1}, |
| 242 |
{ERR=>"$prog: format '' has no % directive\n"}], |
| 243 |
['e121','--format "foobar"', {IN_PIPE=>""}, {EXIT=>1}, |
| 244 |
{ERR=>"$prog: format 'foobar' has no % directive\n"}], |
| 245 |
['e122','--format "aa%%ff"', {IN_PIPE=>""}, {EXIT=>1}, |
| 246 |
{ERR=>"$prog: format 'aa%%ff' has no % directive\n"}], |
| 247 |
['e123','--format "%Lg"', {IN_PIPE=>""}, {EXIT=>1}, |
| 248 |
{ERR=>"$prog: format '%Lg' has unknown/invalid type %L directive\n"}], |
| 249 |
['e124','--format "%*g"', {IN_PIPE=>""}, {EXIT=>1}, |
| 250 |
{ERR=>"$prog: format '%*g' has unknown/invalid type %* directive\n"}], |
| 251 |
['e125','--format "%g %f"', {IN_PIPE=>""}, {EXIT=>1}, |
| 252 |
{ERR=>"$prog: format '%g %f' has too many % directives\n"}], |
| 253 |
['e126','--format "%"', {IN_PIPE=>""}, {EXIT=>1}, |
| 254 |
{ERR=>"$prog: format '%' missing valid type after '%'\n"}], |
| 255 |
['e127','--format "%3"', {IN_PIPE=>""}, {EXIT=>1}, |
| 256 |
{ERR=>"$prog: format '%3' missing valid type after '%'\n"}], |
| 257 |
['e128','--format "%#.4"', {IN_PIPE=>""}, {EXIT=>1}, |
| 258 |
{ERR=>"$prog: format '%#.4' missing valid type after '%'\n"}], |
| 259 |
['e129','--format "%f%"', {IN_PIPE=>""}, {EXIT=>1}, |
| 260 |
{ERR=>"$prog: format '%f%' has too many % directives\n"}], |
| 261 |
['e130','--format "%f%3"', {IN_PIPE=>""}, {EXIT=>1}, |
| 262 |
{ERR=>"$prog: format '%f%3' has too many % directives\n"}], |
| 263 |
|
| 264 |
|
| 265 |
# identifier scanning errors |
| 266 |
['e140',"-H sum 'foo\\'", {IN_PIPE=>""}, {EXIT=>1}, |
| 267 |
{ERR=>"$prog: backslash at end of identifier\n"}], |
| 268 |
['e141',"-H sum $ident_too_long", {IN_PIPE=>""}, {EXIT=>1}, |
| 269 |
{ERR=>"$prog: identifier name too long\n"}], |
| 270 |
|
| 271 |
# invalid numeric input data |
| 272 |
['e150', 'sum 1', {IN_PIPE=>"1.2.3\n"}, {EXIT=>1}, |
| 273 |
{ERR=>"$prog: invalid numeric value in line 1 field 1: '1.2.3'\n"}], |
| 274 |
['e151', 'sum 1', {IN_PIPE=>"1.2.3\t4.5\n"}, {EXIT=>1}, |
| 275 |
{ERR=>"$prog: invalid numeric value in line 1 field 1: '1.2.3'\n"}], |
| 276 |
['e152', 'sum 2', {IN_PIPE=>"1.2\t3.4.5\n"}, {EXIT=>1}, |
| 277 |
{ERR=>"$prog: invalid numeric value in line 1 field 2: '3.4.5'\n"}], |
| 278 |
|
| 279 |
# bad seeds |
| 280 |
['e153', '-S1.1 rand 1', |
| 281 |
{IN_PIPE=>"1\n"}, {EXIT=>1}, |
| 282 |
{ERR=>"$prog: invalid seed\n"}], |
| 283 |
['e154', '-S-1 rand 1', |
| 284 |
{IN_PIPE=>"1\n"}, {EXIT=>1}, |
| 285 |
{ERR=>"$prog: invalid seed\n"}], |
| 286 |
['e155', '-Sa rand 1', |
| 287 |
{IN_PIPE=>"1\n"}, {EXIT=>1}, |
| 288 |
{ERR=>"$prog: invalid seed\n"}], |
| 289 |
); |
| 290 |
|
| 291 |
my $save_temps = $ENV{SAVE_TEMPS}; |
| 292 |
my $verbose = $ENV{VERBOSE}; |
| 293 |
|
| 294 |
my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose); |
| 295 |
exit $fail; |