| 1 |
#!/usr/bin/env perl |
| 2 |
=pod |
| 3 |
Unit Tests for GNU Datamash - perform simple calculation on input data |
| 4 |
|
| 5 |
Copyright (C) 2022-2025 Timothy Rice <[email protected]> |
| 6 |
Copyright (C) 2013-2021 Assaf Gordon <[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 and Tim Rice. |
| 24 |
=cut |
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use List::Util qw/max/; |
| 28 |
use Data::Dumper; |
| 29 |
|
| 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 |
|
| 36 |
(my $program_name = $0) =~ s|.*/||; |
| 37 |
my $prog_bin = 'datamash'; |
| 38 |
|
| 39 |
## Cross-Compiling portability hack: |
| 40 |
## under qemu/binfmt, argv[0] (which is used to report errors) will contain |
| 41 |
## the full path of the binary, if the binary is on the $PATH. |
| 42 |
## So we try to detect what is the actual returned value of the program |
| 43 |
## in case of an error. |
| 44 |
my $prog = `$prog_bin --foobar 2>&1 | head -n 1 | cut -f1 -d:`; |
| 45 |
chomp $prog if $prog; |
| 46 |
$prog = $prog_bin unless $prog; |
| 47 |
|
| 48 |
# Turn off localization of executable's output. |
| 49 |
@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3; |
| 50 |
|
| 51 |
# An unsorted input with a header line |
| 52 |
my $INFILE=<<'EOF'; |
| 53 |
x y z |
| 54 |
A % 1 |
| 55 |
B ( 2 |
| 56 |
A & 3 |
| 57 |
B = 4 |
| 58 |
EOF |
| 59 |
|
| 60 |
my @INFILE_lines = split /\n/, $INFILE, -1; |
| 61 |
my $INFILE_NO_HEADER = join("\n", @INFILE_lines[1..$#INFILE_lines]); |
| 62 |
|
| 63 |
# The expected output with different option combinations |
| 64 |
my $exp_no_sort_no_header=<<'EOF'; |
| 65 |
x z |
| 66 |
A 1 |
| 67 |
B 2 |
| 68 |
A 3 |
| 69 |
B 4 |
| 70 |
EOF |
| 71 |
|
| 72 |
my $exp_no_sort_in_header=<<'EOF'; |
| 73 |
A 1 |
| 74 |
B 2 |
| 75 |
A 3 |
| 76 |
B 4 |
| 77 |
EOF |
| 78 |
|
| 79 |
my $exp_sort_in_header=<<'EOF'; |
| 80 |
A 1,3 |
| 81 |
B 2,4 |
| 82 |
EOF |
| 83 |
|
| 84 |
my $exp_no_sort_headers=<<'EOF'; |
| 85 |
GroupBy(x) unique(z) |
| 86 |
A 1 |
| 87 |
B 2 |
| 88 |
A 3 |
| 89 |
B 4 |
| 90 |
EOF |
| 91 |
|
| 92 |
my $exp_sort_headers=<<'EOF'; |
| 93 |
GroupBy(x) unique(z) |
| 94 |
A 1,3 |
| 95 |
B 2,4 |
| 96 |
EOF |
| 97 |
|
| 98 |
my $exp_sort_out_header=<<'EOF'; |
| 99 |
GroupBy(field-1) unique(field-3) |
| 100 |
A 1,3 |
| 101 |
B 2,4 |
| 102 |
EOF |
| 103 |
|
| 104 |
my @Tests = |
| 105 |
( |
| 106 |
# Simple transpose and reverse |
| 107 |
['sh01', '-t " " -g 1 unique 3', |
| 108 |
{IN_PIPE=>$INFILE}, {OUT=>$exp_no_sort_no_header}], |
| 109 |
['sh02', '-t " " -g 1 --header-in unique 3', |
| 110 |
{IN_PIPE=>$INFILE}, {OUT=>$exp_no_sort_in_header}], |
| 111 |
['sh03', '-t " " -g 1 --sort --header-in unique 3', |
| 112 |
{IN_PIPE=>$INFILE}, {OUT=>$exp_sort_in_header}], |
| 113 |
['sh04', '-t " " -g 1 --headers unique 3', |
| 114 |
{IN_PIPE=>$INFILE}, {OUT=>$exp_no_sort_headers}], |
| 115 |
['sh05', '-t " " -g 1 --sort --headers unique 3', |
| 116 |
{IN_PIPE=>$INFILE}, {OUT=>$exp_sort_headers}], |
| 117 |
['sh06', '-t " " -sH -g 1 unique 3', |
| 118 |
{IN_PIPE=>$INFILE}, {OUT=>$exp_sort_headers}], |
| 119 |
['sh07', '-t " " --sort --header-out -g 1 unique 3', |
| 120 |
{IN_PIPE=>$INFILE_NO_HEADER}, {OUT=>$exp_sort_out_header}], |
| 121 |
|
| 122 |
# Check sort-piping with empty input - should always produce empty output |
| 123 |
['sh08', '-t " " --sort unique 3', |
| 124 |
{IN_PIPE=>""}, {OUT=>""}], |
| 125 |
['sh09', '-t " " --sort --header-in unique 3', |
| 126 |
{IN_PIPE=>""}, {OUT=>""}], |
| 127 |
['sh10', '-t " " --sort --header-out unique 3', |
| 128 |
{IN_PIPE=>""}, {OUT=>""}], |
| 129 |
['sh11', '-t " " --sort --headers unique 3', |
| 130 |
{IN_PIPE=>""}, {OUT=>""}], |
| 131 |
|
| 132 |
); |
| 133 |
|
| 134 |
my $save_temps = $ENV{SAVE_TEMPS}; |
| 135 |
my $verbose = $ENV{VERBOSE}; |
| 136 |
|
| 137 |
my $fail = run_tests ($program_name, $prog_bin, \@Tests, $save_temps, $verbose); |
| 138 |
exit $fail; |