| 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 Dima Kogan <[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 |
|
| 26 |
use strict; |
| 27 |
use warnings; |
| 28 |
|
| 29 |
use lib '.'; |
| 30 |
# Until a better way comes along to auto-use Coreutils Perl modules |
| 31 |
# as in the coreutils' autotools system. |
| 32 |
use Coreutils; |
| 33 |
use CuSkip; |
| 34 |
use CuTmpdir qw(datamash); |
| 35 |
use MIME::Base64 ; |
| 36 |
|
| 37 |
(my $program_name = $0) =~ s|.*/||; |
| 38 |
my $prog_bin = 'datamash'; |
| 39 |
|
| 40 |
## Cross-Compiling portability hack: |
| 41 |
## under qemu/binfmt, argv[0] (which is used to report errors) will contain |
| 42 |
## the full path of the binary, if the binary is on the $PATH. |
| 43 |
## So we try to detect what is the actual returned value of the program |
| 44 |
## in case of an error. |
| 45 |
my $prog = `$prog_bin ---print-progname`; |
| 46 |
$prog = $prog_bin unless $prog; |
| 47 |
|
| 48 |
# TODO: add localization tests with "grouping" |
| 49 |
# Turn off localization of executable's output. |
| 50 |
@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3; |
| 51 |
|
| 52 |
|
| 53 |
my $in_basic=<<'EOF'; |
| 54 |
#! comment |
| 55 |
##comment |
| 56 |
## comment |
| 57 |
## comment |
| 58 |
|
| 59 |
|
| 60 |
#x y z |
| 61 |
|
| 62 |
|
| 63 |
4 2 3 # comment |
| 64 |
4 - 6# comment |
| 65 |
## comment |
| 66 |
- 8 9 |
| 67 |
EOF |
| 68 |
|
| 69 |
my $in_fake_comment=<<'EOF'; |
| 70 |
# w x y z |
| 71 |
1 2 3 4 |
| 72 |
; not a comment |
| 73 |
EOF |
| 74 |
|
| 75 |
my $in_fake_comment2=<<'EOF'; |
| 76 |
# w x y z |
| 77 |
1 2 3 4;no_comment |
| 78 |
5 6 7 ;no_comment |
| 79 |
8 9 ; no_comment |
| 80 |
EOF |
| 81 |
|
| 82 |
my $in_numeric_columns=<<'EOF'; |
| 83 |
# 0 1 2 |
| 84 |
1 2 3 |
| 85 |
4 - 6 |
| 86 |
- 8 9 |
| 87 |
EOF |
| 88 |
|
| 89 |
# trailing whitespace |
| 90 |
my $in_trailing_whitespace=<<'EOF'; |
| 91 |
# x y |
| 92 |
bar 5 |
| 93 |
bbb 4 |
| 94 |
EOF |
| 95 |
|
| 96 |
my $in_xy=<<'EOF'; |
| 97 |
# x y |
| 98 |
1 0.5 |
| 99 |
2 1 |
| 100 |
3 1.5 |
| 101 |
4 2 |
| 102 |
EOF |
| 103 |
|
| 104 |
my $in_header_comment=<<'EOF'; |
| 105 |
# x y # this is not a comment |
| 106 |
a 1 |
| 107 |
b 2 |
| 108 |
EOF |
| 109 |
|
| 110 |
my $in_header_comment2=<<'EOF'; |
| 111 |
|
| 112 |
# x y # z |
| 113 |
bar 5 1 2 |
| 114 |
bbb 4 7 8 |
| 115 |
|
| 116 |
|
| 117 |
EOF |
| 118 |
|
| 119 |
my $in_header_comment3=<<'EOF'; |
| 120 |
|
| 121 |
# ## |
| 122 |
3 |
| 123 |
15 |
| 124 |
EOF |
| 125 |
|
| 126 |
my $in_header_comment4=<<'EOF'; |
| 127 |
# # a |
| 128 |
1 2 |
| 129 |
EOF |
| 130 |
|
| 131 |
my $in_legend1=<<'EOF'; |
| 132 |
#!/my/interpreter |
| 133 |
##regular vnlog pre-legend comment |
| 134 |
#!unusual vnlog pre-legend comment |
| 135 |
## |
| 136 |
## lines matching regex [ \t]*#[ \t]* are also ignored |
| 137 |
# |
| 138 |
#x |
| 139 |
42 |
| 140 |
EOF |
| 141 |
|
| 142 |
my $in_legend2=<<'EOF'; |
| 143 |
#!/my/interpreter |
| 144 |
##regular vnlog pre-legend comment |
| 145 |
#!unusual vnlog pre-legend comment |
| 146 |
## |
| 147 |
## lines matching regex [ \t]*#[ \t]* are also ignored |
| 148 |
# |
| 149 |
#x |
| 150 |
42 |
| 151 |
EOF |
| 152 |
|
| 153 |
my $in_legend3=<<'EOF'; |
| 154 |
#!/my/interpreter |
| 155 |
##regular vnlog pre-legend comment |
| 156 |
#!unusual vnlog pre-legend comment |
| 157 |
## |
| 158 |
## lines matching regex [ \t]*#[ \t]* are also ignored |
| 159 |
# |
| 160 |
#x |
| 161 |
42 |
| 162 |
EOF |
| 163 |
|
| 164 |
my $in_legend4=<<'EOF'; |
| 165 |
#!/my/interpreter |
| 166 |
##regular vnlog pre-legend comment |
| 167 |
#!unusual vnlog pre-legend comment |
| 168 |
## |
| 169 |
## lines matching regex [ \t]*#[ \t]* are also ignored |
| 170 |
# |
| 171 |
# |
| 172 |
# |
| 173 |
# |
| 174 |
# |
| 175 |
# |
| 176 |
# |
| 177 |
# |
| 178 |
# x |
| 179 |
42 |
| 180 |
EOF |
| 181 |
|
| 182 |
# unusual field names are allowed in vnlog legends |
| 183 |
my $in_legend5=<<'EOF'; |
| 184 |
# # 7 1+ - , : ; .. %CPU TIME+ |
| 185 |
1 2 3 4 5 6 7 8 9 10 |
| 186 |
10 20 30 40 50 60 70 80 90 100 |
| 187 |
EOF |
| 188 |
|
| 189 |
# duplicate field names are not prohibited in vnlog legends, but how |
| 190 |
# they are processed is not specified -- datamash uses the first field |
| 191 |
# with the specified name |
| 192 |
my $in_legend6=<<'EOF'; |
| 193 |
# a a |
| 194 |
1 2 |
| 195 |
10 20 |
| 196 |
EOF |
| 197 |
|
| 198 |
my $out_groupby=<<'EOF'; |
| 199 |
# GroupBy(x) sum(z) |
| 200 |
4 9 |
| 201 |
- 9 |
| 202 |
EOF |
| 203 |
|
| 204 |
my $out_rmdup_x=<<'EOF'; |
| 205 |
# x y z |
| 206 |
4 2 3 |
| 207 |
- 8 9 |
| 208 |
EOF |
| 209 |
|
| 210 |
my $out_rmdup_y=<<'EOF'; |
| 211 |
# x y z |
| 212 |
4 2 3 |
| 213 |
4 - 6 |
| 214 |
- 8 9 |
| 215 |
EOF |
| 216 |
|
| 217 |
my $out_reverse=<<'EOF'; |
| 218 |
# z y x |
| 219 |
3 2 4 |
| 220 |
6 - 4 |
| 221 |
9 8 - |
| 222 |
EOF |
| 223 |
|
| 224 |
my $out_ct1=<<'EOF'; |
| 225 |
# GroupBy(x) GroupBy(y) count(x) |
| 226 |
0.5 1 1.5 2 |
| 227 |
1 1 - - - |
| 228 |
2 - 1 - - |
| 229 |
3 - - 1 - |
| 230 |
4 - - - 1 |
| 231 |
EOF |
| 232 |
|
| 233 |
my $out_ct2=<<'EOF'; |
| 234 |
# GroupBy(x) GroupBy(y) sum(y) |
| 235 |
0.5 1 1.5 2 |
| 236 |
1 0.5 - - - |
| 237 |
2 - 1 - - |
| 238 |
3 - - 1.5 - |
| 239 |
4 - - - 2 |
| 240 |
EOF |
| 241 |
|
| 242 |
my $out_pcov=<<'EOF'; |
| 243 |
# pcov(x,x) pcov(x,y) pcov(y,y) |
| 244 |
1.25 0.625 0.3125 |
| 245 |
EOF |
| 246 |
|
| 247 |
my $out_header_comment2_sum=<<'EOF'; |
| 248 |
# sum(#) sum(z) |
| 249 |
8 10 |
| 250 |
EOF |
| 251 |
|
| 252 |
my $out_header_comment3_sum=<<'EOF'; |
| 253 |
# sum(##) |
| 254 |
18 |
| 255 |
EOF |
| 256 |
|
| 257 |
my $out_header_comment4_sum=<<'EOF'; |
| 258 |
# sum(#) |
| 259 |
1 |
| 260 |
EOF |
| 261 |
|
| 262 |
my $out_legend_sum=<<'EOF'; |
| 263 |
# sum(x) |
| 264 |
42 |
| 265 |
EOF |
| 266 |
|
| 267 |
my $out_legend_sum2=<<'EOF'; |
| 268 |
# sum(#) sum(7) sum(1+) sum(-) sum(,) sum(:) sum(;) sum(..) sum(%CPU) sum(TIME+) |
| 269 |
11 22 33 44 55 66 77 88 99 110 |
| 270 |
EOF |
| 271 |
|
| 272 |
my $out_legend_sum3=<<'EOF'; |
| 273 |
# sum(a) |
| 274 |
11 |
| 275 |
EOF |
| 276 |
|
| 277 |
my @Tests = |
| 278 |
( |
| 279 |
['vnl-sum', '--vnlog sum z', |
| 280 |
{IN_PIPE=>$in_basic}, {OUT=>"# sum(z)\n18\n"}], |
| 281 |
|
| 282 |
['vnl-check', '--vnlog check', |
| 283 |
{IN_PIPE=>$in_basic}, {OUT=>"3 lines, 3 fields\n"}], |
| 284 |
|
| 285 |
# See also nc[5-6] in datamash-tests.pl |
| 286 |
['vnl-nc5', '--vnlog sum zzz', |
| 287 |
{IN_PIPE=>$in_basic}, |
| 288 |
{EXIT=>1}, {ERR=>"$prog: column name 'zzz' not found in input file\n"}], |
| 289 |
|
| 290 |
# Make sure trailing whitespace is ignored properly |
| 291 |
['vnl-eol-space', '--vnlog check', |
| 292 |
{IN_PIPE=>$in_trailing_whitespace}, {OUT=>"2 lines, 2 fields\n"}], |
| 293 |
|
| 294 |
# See also e4 in datamash-tests.pl |
| 295 |
['vnl-e4', '--vnlog sum x', |
| 296 |
{IN_PIPE=>$in_basic}, {OUT=>"# sum(x)\n"}, |
| 297 |
{EXIT=>1}, {ERR=>"$prog: invalid numeric value in line 4 field 1: '-'\n"}], |
| 298 |
|
| 299 |
['vnl-unique', '--vnlog unique x', |
| 300 |
{IN_PIPE=>$in_basic}, {OUT=>"# unique(x)\n-,4\n"}], |
| 301 |
|
| 302 |
['vnl-collapse', '--vnlog collapse x', |
| 303 |
{IN_PIPE=>$in_basic}, {OUT=>"# collapse(x)\n4,4,-\n"}], |
| 304 |
|
| 305 |
['vnl-data-before-legend', '--vnlog sum z', |
| 306 |
{IN_PIPE=>"5\n" . $in_basic}, |
| 307 |
{EXIT=>1}, {ERR=>"$prog: invalid vnlog data: " . |
| 308 |
"received record before header: '5'\n"}], |
| 309 |
|
| 310 |
['vnl-unnamed-field', '--vnlog sum 1', |
| 311 |
{IN_PIPE=>$in_basic}, |
| 312 |
{EXIT=>1}, {ERR=>"$prog: column name '1' not found in input file\n"}], |
| 313 |
|
| 314 |
['vnl-numeric-field', '--vnlog sum 2', |
| 315 |
{IN_PIPE=>$in_numeric_columns}, {OUT=>"# sum(2)\n18\n"}], |
| 316 |
|
| 317 |
['vnl-groupby', '--vnlog -g x sum z', |
| 318 |
{IN_PIPE=>$in_basic}, {OUT=>$out_groupby}], |
| 319 |
['vnl-rmdup-x', '--vnlog rmdup x', |
| 320 |
{IN_PIPE=>$in_basic}, {OUT=>$out_rmdup_x}], |
| 321 |
['vnl-rmdup-y', '--vnlog rmdup y', |
| 322 |
{IN_PIPE=>$in_basic}, {OUT=>$out_rmdup_y}], |
| 323 |
['vnl-reverse', '--vnlog reverse', |
| 324 |
{IN_PIPE=>$in_basic}, {OUT=>$out_reverse}], |
| 325 |
['vnl-getnum1', '--vnlog getnum A', |
| 326 |
{IN_PIPE=>"# A B\nx 1\ny 2\n"}, {OUT=>"# getnum(A)\n0\n0\n"}], |
| 327 |
['vnl-getnum2', '--vnlog getnum B', |
| 328 |
{IN_PIPE=>"# A B\nx 1\ny 2\n"}, {OUT=>"# getnum(B)\n1\n2\n"}], |
| 329 |
|
| 330 |
# empty input = empty output |
| 331 |
['vnl-empty1', '--vnlog count x', |
| 332 |
{IN_PIPE=>""}, {ERR=>""}], |
| 333 |
['vnl-empty2', '--vnlog count x', |
| 334 |
{IN_PIPE=>"# x"}, {OUT=>"# count(x)\n"}], |
| 335 |
|
| 336 |
# various errors |
| 337 |
['vnl-groupby-unmatched-field', '--vnlog -g zzz sum z', |
| 338 |
{IN_PIPE=>$in_basic}, |
| 339 |
{EXIT=>1}, {ERR=>"$prog: column name 'zzz' not found in input file\n"} ], |
| 340 |
|
| 341 |
['vnl-error-sum-empty-field', '--vnlog sum ""', |
| 342 |
{IN_PIPE=>$in_basic}, |
| 343 |
{EXIT=>1}, {ERR=>"$prog: missing field for operation 'sum'\n"}], |
| 344 |
|
| 345 |
['vnl-groupby-missing-field', '--vnlog -g x,,y sum z', |
| 346 |
{IN_PIPE=>$in_basic}, |
| 347 |
{EXIT=>1}, {ERR=>"$prog: missing field for operation 'groupby'\n"}], |
| 348 |
|
| 349 |
# Check that semicolon is *not* a comment. |
| 350 |
['vnl-semicolon', '--vnlog unique w', |
| 351 |
{IN_PIPE=>$in_fake_comment}, {EXIT=>0}, {OUT=>"# unique(w)\n1,;\n"}], |
| 352 |
['vnl-semicolon2', '--vnlog check', |
| 353 |
{IN_PIPE=>$in_fake_comment2},{EXIT=>0}, {OUT=>"3 lines, 4 fields\n"}], |
| 354 |
['vnl-semicolon3', '--vnlog unique y', |
| 355 |
{IN_PIPE=>$in_fake_comment2},{EXIT=>0}, |
| 356 |
{OUT=>"# unique(y)\n3,7,;\n"}], |
| 357 |
['vnl-semicolon4', '--vnlog unique z', |
| 358 |
{IN_PIPE=>$in_fake_comment2},{EXIT=>0}, |
| 359 |
{OUT=>"# unique(z)\n4;no_comment,;no_comment,no_comment\n"}], |
| 360 |
|
| 361 |
# Commandline errors |
| 362 |
['vnl-option-parsing-error1', '--vnlog -t: sum x', |
| 363 |
{IN_PIPE=>$in_basic}, |
| 364 |
{EXIT=>1}, {ERR=>"$prog: vnlog processing is whitespace-delimited\n"}], |
| 365 |
|
| 366 |
['vnl-basic-check-no-op-options', '--vnlog -C --header-in --header-out check', |
| 367 |
{IN_PIPE=>$in_basic}, {OUT=>"3 lines, 3 fields\n"}], |
| 368 |
|
| 369 |
['vnl-pcov', '--vnlog pcov x:x pcov x:y pcov y:y', |
| 370 |
{IN_PIPE=>$in_xy}, {OUT=>$out_pcov}], |
| 371 |
|
| 372 |
# transpose isn't handled in any special way. The result is not a vnl |
| 373 |
['vnl-transpose', '--vnlog transpose', |
| 374 |
{IN_PIPE=>$in_xy}, {OUT=>"1 2 3 4\n0.5 1 1.5 2\n"}], |
| 375 |
|
| 376 |
# crosstab isn't handled in any special way. The result is not a vnl |
| 377 |
['vnl-ct1', '--vnlog crosstab x,y', |
| 378 |
{IN_PIPE=>$in_xy}, {OUT=>$out_ct1}], |
| 379 |
['vnl-ct2', '--vnlog crosstab x,y sum y', |
| 380 |
{IN_PIPE=>$in_xy}, {OUT=>$out_ct2}], |
| 381 |
|
| 382 |
# the header line does not recognize trailing comments |
| 383 |
['vnl-no-header-comment1', '--vnlog check', |
| 384 |
{IN_PIPE=>$in_header_comment}, {OUT=>"2 lines, 2 fields\n"}], |
| 385 |
['vnl-no-header-comment2', '--vnlog sum y', |
| 386 |
{IN_PIPE=>$in_header_comment}, {OUT=>"# sum(y)\n3\n"}], |
| 387 |
['vnl-no-header-comment3', '--vnlog sum comment', |
| 388 |
{IN_PIPE=>$in_header_comment}, |
| 389 |
{EXIT=>1}, {OUT=>"# sum(comment)\n"}, |
| 390 |
{ERR=>"$prog: invalid input:" . |
| 391 |
" field 8 requested, line 2 has only 2 fields\n"}], |
| 392 |
['vnl-no-header-comment4', '--vnlog sum "\\#",z', |
| 393 |
{IN_PIPE=>$in_header_comment2}, {OUT=>$out_header_comment2_sum}], |
| 394 |
['vnl-no-header-comment5', '--vnlog sum "\\#\\#"', |
| 395 |
{IN_PIPE=>$in_header_comment3}, {OUT=>$out_header_comment3_sum}], |
| 396 |
['vnl-no-header-comment6', '--vnlog sum "\\#"', |
| 397 |
{IN_PIPE=>$in_header_comment4}, {OUT=>$out_header_comment4_sum}], |
| 398 |
|
| 399 |
# ignore specific lines before the legend |
| 400 |
['vnl-legend1', '--vnlog sum x', |
| 401 |
{IN_PIPE=>$in_legend1}, {OUT=>$out_legend_sum}], |
| 402 |
['vnl-legend2', '--vnlog sum x', |
| 403 |
{IN_PIPE=>$in_legend2}, {OUT=>$out_legend_sum}], |
| 404 |
['vnl-legend3', '--vnlog sum x', |
| 405 |
{IN_PIPE=>$in_legend3}, {OUT=>$out_legend_sum}], |
| 406 |
['vnl-legend4', '--vnlog sum x', |
| 407 |
{IN_PIPE=>$in_legend4}, {OUT=>$out_legend_sum}], |
| 408 |
|
| 409 |
# vnlog allows unusual field names |
| 410 |
['vnl-legend5', |
| 411 |
"--vnlog sum '\\#,7,\\1\\+,\\-,\\,,\\:,\\;,\\.\\.,\\%CPU,TIME\\+'", |
| 412 |
{IN_PIPE=>$in_legend5}, {OUT=>$out_legend_sum2}], |
| 413 |
# duplicate field names are not an error in vnlog |
| 414 |
['vnl-legend6', '--vnlog sum a', |
| 415 |
{IN_PIPE=>$in_legend6}, {OUT=>$out_legend_sum3}], |
| 416 |
|
| 417 |
); |
| 418 |
|
| 419 |
|
| 420 |
my $save_temps = $ENV{SAVE_TEMPS}; |
| 421 |
my $verbose = $ENV{VERBOSE}; |
| 422 |
|
| 423 |
my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose); |
| 424 |
exit $fail; |