| 1 |
#!/usr/bin/env perl |
| 2 |
=pod |
| 3 |
Unit Tests for GNU Datamash - perform simple calculation on input data |
| 4 |
Tests for 'transpose' and 'reverse' operation modes. |
| 5 |
|
| 6 |
|
| 7 |
Copyright (C) 2013-2021 Assaf Gordon <[email protected]> |
| 8 |
Copyright (C) 2022-2025 Timothy Rice <[email protected]> |
| 9 |
|
| 10 |
This file is part of GNU Datamash. |
| 11 |
|
| 12 |
GNU Datamash is free software: you can redistribute it and/or modify |
| 13 |
it under the terms of the GNU General Public License as published by |
| 14 |
the Free Software Foundation, either version 3 of the License, or |
| 15 |
(at your option) any later version. |
| 16 |
|
| 17 |
GNU Datamash is distributed in the hope that it will be useful, |
| 18 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
GNU General Public License for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License |
| 23 |
along with GNU Datamash. If not, see <https://www.gnu.org/licenses/>. |
| 24 |
|
| 25 |
Written by Assaf Gordon. |
| 26 |
=cut |
| 27 |
use strict; |
| 28 |
use warnings; |
| 29 |
use List::Util qw/max/; |
| 30 |
use Data::Dumper; |
| 31 |
|
| 32 |
# Until a better way comes along to auto-use Coreutils Perl modules |
| 33 |
# as in the coreutils' autotools system. |
| 34 |
use Coreutils; |
| 35 |
use CuSkip; |
| 36 |
use CuTmpdir qw(datamash); |
| 37 |
|
| 38 |
(my $program_name = $0) =~ s|.*/||; |
| 39 |
my $prog_bin = 'datamash'; |
| 40 |
|
| 41 |
## Cross-Compiling portability hack: |
| 42 |
## under qemu/binfmt, argv[0] (which is used to report errors) will contain |
| 43 |
## the full path of the binary, if the binary is on the $PATH. |
| 44 |
## So we try to detect what is the actual returned value of the program |
| 45 |
## in case of an error. |
| 46 |
my $prog = `$prog_bin --foobar 2>&1 | head -n 1 | cut -f1 -d:`; |
| 47 |
chomp $prog if $prog; |
| 48 |
$prog = $prog_bin unless $prog; |
| 49 |
|
| 50 |
# Turn off localization of executable's output. |
| 51 |
@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3; |
| 52 |
|
| 53 |
sub perl_reverse_fields |
| 54 |
{ |
| 55 |
my $field_sep = shift; |
| 56 |
my $input = shift; |
| 57 |
return join ("\n", |
| 58 |
map { |
| 59 |
join($field_sep, |
| 60 |
reverse( |
| 61 |
split /$field_sep/, $_ |
| 62 |
) |
| 63 |
) |
| 64 |
} |
| 65 |
split /\n/, $input) . "\n"; |
| 66 |
} |
| 67 |
|
| 68 |
sub perl_transpose |
| 69 |
{ |
| 70 |
my $field_sep = shift; |
| 71 |
my $filler = shift ; |
| 72 |
my $input = shift; |
| 73 |
my @lines = map { [ split /$field_sep/, $_ ] } split /\n/, $input; |
| 74 |
my $max_field = max ( map{ scalar(@$_) } @lines ); |
| 75 |
|
| 76 |
my @output; |
| 77 |
foreach my $i ( 0 .. ( $max_field - 1) ) { |
| 78 |
my @new_line ; |
| 79 |
foreach my $l (@lines) { |
| 80 |
push @new_line, |
| 81 |
(scalar(@$l) > $i) ? $l->[$i] : $filler; |
| 82 |
} |
| 83 |
|
| 84 |
push @output, join($field_sep, @new_line); |
| 85 |
} |
| 86 |
|
| 87 |
return join("\n", @output) . "\n"; |
| 88 |
} |
| 89 |
|
| 90 |
my $in1=<<'EOF'; |
| 91 |
A 1 ! |
| 92 |
B 2 @ |
| 93 |
C 3 # |
| 94 |
D 4 $ |
| 95 |
E 5 % |
| 96 |
EOF |
| 97 |
|
| 98 |
my $in2 = $in1; |
| 99 |
$in2 =~ s/\t/:/gms; |
| 100 |
|
| 101 |
my $in3=<<'EOF'; |
| 102 |
A 1 |
| 103 |
B |
| 104 |
C 3 |
| 105 |
EOF |
| 106 |
|
| 107 |
my $in4=<<'EOF'; |
| 108 |
A |
| 109 |
B |
| 110 |
C |
| 111 |
EOF |
| 112 |
|
| 113 |
my $in5="A\tB\tC\tD\n"; |
| 114 |
|
| 115 |
my $in6="A\n"; |
| 116 |
|
| 117 |
my $in7=""; |
| 118 |
|
| 119 |
my $out1_rev = perl_reverse_fields ( "\t", $in1 ); |
| 120 |
my $out2_rev = perl_reverse_fields ( ":", $in2 ); |
| 121 |
my $out3_rev = perl_reverse_fields ( "\t", $in3 ); |
| 122 |
my $out4_rev = perl_reverse_fields ( "\t", $in4 ); |
| 123 |
my $out5_rev = perl_reverse_fields ( "\t", $in5 ); |
| 124 |
|
| 125 |
my $out1_tr = perl_transpose ( "\t", "N/A", $in1 ); |
| 126 |
my $out2_tr = perl_transpose ( ":", "N/A", $in2 ) ; |
| 127 |
my $out3_tr = perl_transpose ("\t", "N/A", $in3 ) ; |
| 128 |
my $out3_filler_tr = perl_transpose ("\t", "xxx", $in3 ) ; |
| 129 |
my $out4_tr = perl_transpose ("\t", "N/A", $in4 ) ; |
| 130 |
my $out5_tr = perl_transpose ("\t", "N/A", $in5 ) ; |
| 131 |
|
| 132 |
my $in_hdr1=<<'EOF'; |
| 133 |
X:Y |
| 134 |
1:a |
| 135 |
2:b |
| 136 |
EOF |
| 137 |
|
| 138 |
|
| 139 |
# Transposing with missing value in the last line |
| 140 |
# (bug in 1.1.0 would result in 'c' being silently dropped). |
| 141 |
my $in_missing1=<<'EOF'; |
| 142 |
a b c |
| 143 |
1 2 |
| 144 |
EOF |
| 145 |
my $out_missing1=<<'EOF'; |
| 146 |
a 1 |
| 147 |
b 2 |
| 148 |
c N/A |
| 149 |
EOF |
| 150 |
|
| 151 |
my @Tests = |
| 152 |
( |
| 153 |
# Simple transpose and reverse |
| 154 |
['tr1', 'transpose', {IN_PIPE=>$in1}, {OUT=>$out1_tr}], |
| 155 |
['rev1', 'reverse', {IN_PIPE=>$in1}, {OUT=>$out1_rev}], |
| 156 |
|
| 157 |
# non-tab delimiter |
| 158 |
['tr2', '-t: transpose', {IN_PIPE=>$in2}, {OUT=>$out2_tr}], |
| 159 |
['rev2', '-t: reverse', {IN_PIPE=>$in2}, {OUT=>$out2_rev}], |
| 160 |
|
| 161 |
# missing fields, strict mode |
| 162 |
['tr3', 'transpose', {IN_PIPE=>$in3}, {EXIT=>1}, |
| 163 |
{OUT_SUBST=>'s/.*//'}, |
| 164 |
{ERR=>"$prog: transpose input error: line 2 has 1 fields ". |
| 165 |
"(previous lines had 2);\n" . |
| 166 |
"see --help to disable strict mode\n"}], |
| 167 |
['rev3', 'reverse', {IN_PIPE=>$in3}, {EXIT=>1}, |
| 168 |
{OUT_SUBST=>'s/.*//s'}, |
| 169 |
{ERR=>"$prog: reverse-field input error: line 2 has 1 fields ". |
| 170 |
"(previous lines had 2);\n" . |
| 171 |
"see --help to disable strict mode\n"}], |
| 172 |
|
| 173 |
# missing fields, non-strict mode |
| 174 |
['tr4', '--no-strict transpose', {IN_PIPE=>$in3}, {OUT=>$out3_tr}], |
| 175 |
['rev4', '--no-strict reverse', {IN_PIPE=>$in3}, {OUT=>$out3_rev}], |
| 176 |
['tr4.1', '--no-strict --filler xxx transpose', |
| 177 |
{IN_PIPE=>$in3}, {OUT=>$out3_filler_tr}], |
| 178 |
|
| 179 |
|
| 180 |
# Single column |
| 181 |
['tr5', 'transpose', {IN_PIPE=>$in4}, {OUT=>$out4_tr}], |
| 182 |
['rev5', 'reverse', {IN_PIPE=>$in4}, {OUT=>$out4_rev}], |
| 183 |
|
| 184 |
# Single row |
| 185 |
['tr6', 'transpose', {IN_PIPE=>$in5}, {OUT=>$out5_tr}], |
| 186 |
['rev6', 'reverse', {IN_PIPE=>$in5}, {OUT=>$out5_rev}], |
| 187 |
|
| 188 |
# Single field |
| 189 |
['tr7', 'transpose', {IN_PIPE=>$in6}, {OUT=>$in6}], |
| 190 |
['rev7', 'reverse', {IN_PIPE=>$in6}, {OUT=>$in6}], |
| 191 |
|
| 192 |
# Empty input |
| 193 |
['tr8', 'transpose', {IN_PIPE=>$in7}, {OUT=>""}], |
| 194 |
['rev8', 'reverse', {IN_PIPE=>$in7}, {OUT=>""}], |
| 195 |
|
| 196 |
# Extra operands |
| 197 |
['tr9', 'transpose aaa', {IN_PIPE=>''}, {EXIT=>1}, |
| 198 |
{ERR=>"$prog: extra operand 'aaa'\n"}], |
| 199 |
['rev9', 'reverse aaa', {IN_PIPE=>''}, {EXIT=>1}, |
| 200 |
{ERR=>"$prog: extra operand 'aaa'\n"}], |
| 201 |
|
| 202 |
# empty input |
| 203 |
['tr10', 'transpose', {IN_PIPE=>""}, {OUT=>""}], |
| 204 |
['rev10', 'reverse', {IN_PIPE=>""}, {OUT=>""}], |
| 205 |
|
| 206 |
# Reverse with header combinations |
| 207 |
['rev-hdr1','-H reverse', {IN_PIPE=>""}, {OUT=>""}], |
| 208 |
['rev-hdr2','--header-in reverse', {IN_PIPE=>""}, {OUT=>""}], |
| 209 |
['rev-hdr3','-t: reverse', {IN_PIPE=>$in_hdr1}, |
| 210 |
{OUT=>"Y:X\na:1\nb:2\n"}], |
| 211 |
['rev-hdr4','-t: -H reverse', {IN_PIPE=>$in_hdr1}, |
| 212 |
{OUT=>"Y:X\na:1\nb:2\n"}], |
| 213 |
# first line is header line, discard it (there's no --header-out). |
| 214 |
['rev-hdr5','-t: --header-in reverse', {IN_PIPE=>$in_hdr1}, |
| 215 |
{OUT=>"a:1\nb:2\n"}], |
| 216 |
# Generate a new header, assuming the first line is a NOT header line. |
| 217 |
['rev-hdr6','-t: --header-out reverse', {IN_PIPE=>$in_hdr1}, |
| 218 |
{OUT=>"field-2:field-1\nY:X\na:1\nb:2\n"}], |
| 219 |
|
| 220 |
# bug uncovered by report in: |
| 221 |
# http://lists.gnu.org/archive/html/bug-datamash/2016-09/msg00000.html |
| 222 |
['msg1', '--no-strict transpose', {IN_PIPE=>$in_missing1}, |
| 223 |
{OUT=>$out_missing1}], |
| 224 |
); |
| 225 |
|
| 226 |
my $save_temps = $ENV{SAVE_TEMPS}; |
| 227 |
my $verbose = $ENV{VERBOSE}; |
| 228 |
|
| 229 |
my $fail = run_tests ($program_name, $prog_bin, \@Tests, $save_temps, $verbose); |
| 230 |
exit $fail; |