| 1 |
#!/usr/bin/env perl |
| 2 |
=pod |
| 3 |
Unit Tests for GNU Datamash - perform simple calculation on input data |
| 4 |
Tests for 'check' operation mode |
| 5 |
|
| 6 |
Copyright (C) 2013-2021 Assaf Gordon <[email protected]> |
| 7 |
Copyright (C) 2022-2025 Timothy Rice <[email protected]> |
| 8 |
|
| 9 |
This file is part of GNU Datamash. |
| 10 |
|
| 11 |
GNU Datamash is free software: you can redistribute it and/or modify |
| 12 |
it under the terms of the GNU General Public License as published by |
| 13 |
the Free Software Foundation, either version 3 of the License, or |
| 14 |
(at your option) any later version. |
| 15 |
|
| 16 |
GNU Datamash is distributed in the hope that it will be useful, |
| 17 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
GNU General Public License for more details. |
| 20 |
|
| 21 |
You should have received a copy of the GNU General Public License |
| 22 |
along with GNU Datamash. If not, see <https://www.gnu.org/licenses/>. |
| 23 |
|
| 24 |
Written by Assaf Gordon. |
| 25 |
=cut |
| 26 |
use strict; |
| 27 |
use warnings; |
| 28 |
use List::Util qw/max/; |
| 29 |
use Data::Dumper; |
| 30 |
|
| 31 |
# Until a better way comes along to auto-use Coreutils Perl modules |
| 32 |
# as in the coreutils' autotools system. |
| 33 |
use Coreutils; |
| 34 |
use CuSkip; |
| 35 |
use CuTmpdir qw(datamash); |
| 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 --foobar 2>&1 | head -n 1 | cut -f1 -d:`; |
| 46 |
chomp $prog if $prog; |
| 47 |
$prog = $prog_bin unless $prog; |
| 48 |
|
| 49 |
# Turn off localization of executable's output. |
| 50 |
@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3; |
| 51 |
|
| 52 |
|
| 53 |
my $in1=<<'EOF'; |
| 54 |
A 1 ! |
| 55 |
B 2 @ |
| 56 |
C 3 # |
| 57 |
D 4 $ |
| 58 |
E 5 % |
| 59 |
EOF |
| 60 |
|
| 61 |
my $in2=<<'EOF'; |
| 62 |
A 1 |
| 63 |
B |
| 64 |
C 3 |
| 65 |
EOF |
| 66 |
|
| 67 |
my $in3=<<'EOF'; |
| 68 |
A |
| 69 |
EOF |
| 70 |
|
| 71 |
my $in4=<<'EOF'; |
| 72 |
#comment |
| 73 |
A |
| 74 |
;comment |
| 75 |
|
| 76 |
B |
| 77 |
EOF |
| 78 |
|
| 79 |
|
| 80 |
my @Tests = |
| 81 |
( |
| 82 |
# Simple transpose and reverse |
| 83 |
['c1', 'check', {IN_PIPE=>$in1}, {OUT=>"5 lines, 3 fields\n"}], |
| 84 |
|
| 85 |
# Variations on command-line parsing |
| 86 |
['c2', 'check 3 field', {IN_PIPE=>$in1}, {OUT=>"5 lines, 3 fields\n"}], |
| 87 |
['c3', 'check 3 fields', {IN_PIPE=>$in1}, {OUT=>"5 lines, 3 fields\n"}], |
| 88 |
['c4', 'check 3 col', {IN_PIPE=>$in1}, {OUT=>"5 lines, 3 fields\n"}], |
| 89 |
['c5', 'check 3 columns', {IN_PIPE=>$in1}, {OUT=>"5 lines, 3 fields\n"}], |
| 90 |
['c6', 'check 3 column', {IN_PIPE=>$in1}, {OUT=>"5 lines, 3 fields\n"}], |
| 91 |
|
| 92 |
['c7', 'check field 3', {IN_PIPE=>$in1}, {OUT=>"5 lines, 3 fields\n"}], |
| 93 |
['c8', 'check fields 3', {IN_PIPE=>$in1}, {OUT=>"5 lines, 3 fields\n"}], |
| 94 |
['c9', 'check col 3', {IN_PIPE=>$in1}, {OUT=>"5 lines, 3 fields\n"}], |
| 95 |
['c10', 'check columns 3', {IN_PIPE=>$in1}, {OUT=>"5 lines, 3 fields\n"}], |
| 96 |
['c11', 'check column 3', {IN_PIPE=>$in1}, {OUT=>"5 lines, 3 fields\n"}], |
| 97 |
|
| 98 |
['c12', 'check 5 lines', {IN_PIPE=>$in1}, {OUT=>"5 lines, 3 fields\n"}], |
| 99 |
['c13', 'check 5 line', {IN_PIPE=>$in1}, {OUT=>"5 lines, 3 fields\n"}], |
| 100 |
['c14', 'check 5 rows', {IN_PIPE=>$in1}, {OUT=>"5 lines, 3 fields\n"}], |
| 101 |
['c15', 'check 5 row', {IN_PIPE=>$in1}, {OUT=>"5 lines, 3 fields\n"}], |
| 102 |
|
| 103 |
['c16', 'check lines 5', {IN_PIPE=>$in1}, {OUT=>"5 lines, 3 fields\n"}], |
| 104 |
['c17', 'check line 5', {IN_PIPE=>$in1}, {OUT=>"5 lines, 3 fields\n"}], |
| 105 |
['c18', 'check row 5', {IN_PIPE=>$in1}, {OUT=>"5 lines, 3 fields\n"}], |
| 106 |
['c19', 'check rows 5', {IN_PIPE=>$in1}, {OUT=>"5 lines, 3 fields\n"}], |
| 107 |
|
| 108 |
|
| 109 |
# Duplicated options |
| 110 |
['e1', 'check rows 5 lines 6', {IN_PIPE=>$in1}, {EXIT=>1}, |
| 111 |
{ERR=>"$prog: number of lines/rows already set in operation 'check'\n"}], |
| 112 |
['e2', 'check fields 6 fields 1', {IN_PIPE=>$in1}, {EXIT=>1}, |
| 113 |
{ERR=>"$prog: number of fields/columns already set " . |
| 114 |
"in operation 'check'\n"}], |
| 115 |
|
| 116 |
# Invalid values |
| 117 |
['e3', 'check 0 lines', {IN_PIPE=>$in1}, {EXIT=>1}, |
| 118 |
{ERR=>"$prog: invalid value zero for lines/fields in operation 'check'\n"}], |
| 119 |
['e4', 'check 0 fields', {IN_PIPE=>$in1}, {EXIT=>1}, |
| 120 |
{ERR=>"$prog: invalid value zero for lines/fields in operation 'check'\n"}], |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
# Check lines |
| 125 |
['c40', 'check 4 lines', {IN_PIPE=>$in1}, {EXIT=>1}, |
| 126 |
{ERR=>"$prog: check failed: input had 5 lines (expecting 4)\n"}], |
| 127 |
['c41', 'check 6 lines', {IN_PIPE=>$in1}, {EXIT=>1}, |
| 128 |
{ERR=>"$prog: check failed: input had 5 lines (expecting 6)\n"}], |
| 129 |
['c42', 'check 6 lines', {IN_PIPE=>""}, {EXIT=>1}, |
| 130 |
{ERR=>"$prog: check failed: input had 0 lines (expecting 6)\n"}], |
| 131 |
|
| 132 |
# Check fields |
| 133 |
['c60', 'check 2 fields', {IN_PIPE=>$in1}, {EXIT=>1}, |
| 134 |
{ERR=>"line 1 (3 fields):\n" . |
| 135 |
" A\t1\t!\n" . |
| 136 |
"$prog: check failed: line 1 has 3 fields (expecting 2)\n"}], |
| 137 |
|
| 138 |
|
| 139 |
# Check matrix structure, no expected number of fields |
| 140 |
['c61', 'check', {IN_PIPE=>$in2}, {EXIT=>1}, |
| 141 |
{ERR=>"line 1 (2 fields):\n" . |
| 142 |
" A\t1\n" . |
| 143 |
"line 2 (1 fields):\n" . |
| 144 |
" B\n" . |
| 145 |
"$prog: check failed: line 2 has 1 fields (previous line had 2)\n"}], |
| 146 |
|
| 147 |
# With expected number of fields |
| 148 |
['c62', 'check 2 fields', {IN_PIPE=>$in2}, {EXIT=>1}, |
| 149 |
{ERR=>"line 2 (1 fields):\n" . |
| 150 |
" B\n" . |
| 151 |
"$prog: check failed: line 2 has 1 fields (expecting 2)\n"}], |
| 152 |
|
| 153 |
# no special treatment for comments or empty lines by default |
| 154 |
['c63', 'check', {IN_PIPE=>$in4}, {EXIT=>1}, |
| 155 |
{ERR=>"line 3 (1 fields):\n" . |
| 156 |
" ;comment\n" . |
| 157 |
"line 4 (0 fields):\n" . |
| 158 |
" \n" . |
| 159 |
"$prog: check failed: line 4 has 0 fields (previous line had 1)\n"}], |
| 160 |
# --skip-comments skips only comment lines, but not empty lines |
| 161 |
['c64', '--skip-comments check', {IN_PIPE=>$in4}, {EXIT=>1}, |
| 162 |
{ERR=>"line 1 (1 fields):\n" . |
| 163 |
" A\n" . |
| 164 |
"line 2 (0 fields):\n" . |
| 165 |
" \n" . |
| 166 |
"$prog: check failed: line 2 has 0 fields (previous line had 1)\n"}], |
| 167 |
# --vnlog skips both comment and empty lines, but only '#' starts a comment |
| 168 |
# (the first line is actually the vnlog header and requires the same number |
| 169 |
# of fields as the data lines, but this test is about ignoring empty lines) |
| 170 |
['c65', '--vnlog check', {IN_PIPE=>$in4}, {OUT=>"3 lines, 1 field\n"}], |
| 171 |
); |
| 172 |
|
| 173 |
my $save_temps = $ENV{SAVE_TEMPS}; |
| 174 |
my $verbose = $ENV{VERBOSE}; |
| 175 |
|
| 176 |
my $fail = run_tests ($program_name, $prog_bin, \@Tests, $save_temps, $verbose); |
| 177 |
exit $fail; |