| 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 |
## |
| 29 |
## This is a continuation of 'datamash-tests.pl' |
| 30 |
## split into two files, as it was getting too large. |
| 31 |
## |
| 32 |
|
| 33 |
# Until a better way comes along to auto-use Coreutils Perl modules |
| 34 |
# as in the coreutils' autotools system. |
| 35 |
use Coreutils; |
| 36 |
use CuSkip; |
| 37 |
use CuTmpdir qw(datamash); |
| 38 |
use MIME::Base64 ; |
| 39 |
|
| 40 |
(my $program_name = $0) =~ s|.*/||; |
| 41 |
my $prog_bin = 'datamash'; |
| 42 |
|
| 43 |
## Cross-Compiling portability hack: |
| 44 |
## under qemu/binfmt, argv[0] (which is used to report errors) will contain |
| 45 |
## the full path of the binary, if the binary is on the $PATH. |
| 46 |
## So we try to detect what is the actual returned value of the program |
| 47 |
## in case of an error. |
| 48 |
my $prog = `$prog_bin ---print-progname`; |
| 49 |
$prog = $prog_bin unless $prog; |
| 50 |
|
| 51 |
## Portability hack: |
| 52 |
## find the exact wording of 'nan' and inf (not-a-number). |
| 53 |
## It's lower case in GNU/Linux,FreeBSD,OpenBSD, |
| 54 |
## but is "NaN" on Illumos/OpenSolaris |
| 55 |
my $nan = `$prog_bin ---print-nan`; |
| 56 |
die "test infrastructure failed: can't determine 'nan' string" unless $nan; |
| 57 |
my $inf = `$prog_bin ---print-inf`; |
| 58 |
die "test infrastructure failed: can't determine 'inf' string" unless $inf; |
| 59 |
|
| 60 |
## Portability hack |
| 61 |
## Check if the system's sort supports stable sorting ('-s'). |
| 62 |
## If it doesn't - skip some tests |
| 63 |
my $rc = system("sort -s < /dev/null > /dev/null 2>/dev/null"); |
| 64 |
die "testing framework failure: failed to execute sort -s" |
| 65 |
if ( ($rc == -1) || ($rc & 127) ); |
| 66 |
my $sort_exit_code = ($rc >> 8); |
| 67 |
my $have_stable_sort = ($sort_exit_code==0); |
| 68 |
|
| 69 |
|
| 70 |
# TODO: add localization tests with "grouping" |
| 71 |
# Turn off localization of executable's output. |
| 72 |
@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3; |
| 73 |
|
| 74 |
|
| 75 |
# Test the selection operation (first/last/min/max) together with "FULL": |
| 76 |
# group by column 1 ("A" or "B"), and operate on column 2 (numeric). |
| 77 |
# Ensure the matching "iX" is displayed, despite not being part of the |
| 78 |
# operation (example: if 'min(2)' is the operation, then "B 8" should be |
| 79 |
# selected, and "i7" must be displayed with "-full" (because "i7" is on the |
| 80 |
# same line as the min(2) value zero). |
| 81 |
my $in_full1=<<'EOF'; |
| 82 |
A 4 i1 |
| 83 |
A 3 i2 |
| 84 |
A 5 i3 |
| 85 |
B 1 i4 |
| 86 |
B 8 i5 |
| 87 |
B 0 i6 |
| 88 |
B 3 i7 |
| 89 |
EOF |
| 90 |
|
| 91 |
my $full_deprecation = "$prog: Using -f/--full with non-linewise operations " . |
| 92 |
"is deprecated and will be disabled in a future release.\n"; |
| 93 |
|
| 94 |
my @Tests = |
| 95 |
( |
| 96 |
# Test 'min' + --full |
| 97 |
# Test with "--full", "i2" and "i6" should be displayed |
| 98 |
['slct2dep', '-t" " -f -g1 min 2', {IN_PIPE=>$in_full1}, |
| 99 |
{OUT=>"A 3 i2 3\nB 0 i6 0\n"}, |
| 100 |
{ERR=>"$full_deprecation"}], |
| 101 |
# --full with --sort => should not change results |
| 102 |
['slct3dep', '-s -t" " -f -g1 min 2', {IN_PIPE=>$in_full1}, |
| 103 |
{OUT=>"A 3 i2 3\nB 0 i6 0\n"}, |
| 104 |
{ERR=>"$full_deprecation"}], |
| 105 |
|
| 106 |
# Test 'max' + --full |
| 107 |
# Test with "--full", "i3" and "i7" should be displayed |
| 108 |
['slct5dep', '-t" " -f -g1 max 2', {IN_PIPE=>$in_full1}, |
| 109 |
{OUT=>"A 5 i3 5\nB 8 i5 8\n"}, |
| 110 |
{ERR=>"$full_deprecation"}], |
| 111 |
# --full with --sort => should not change results |
| 112 |
['slct6dep', '-s -t" " -f -g1 max 2', {IN_PIPE=>$in_full1}, |
| 113 |
{OUT=>"A 5 i3 5\nB 8 i5 8\n"}, |
| 114 |
{ERR=>"$full_deprecation"}], |
| 115 |
|
| 116 |
# Test 'first' + --full |
| 117 |
# Test with "--full", "i1" and "i4" should be displayed |
| 118 |
['slct8dep', '-t" " -f -g1 first 2', {IN_PIPE=>$in_full1}, |
| 119 |
{OUT=>"A 4 i1 4\nB 1 i4 1\n"}, |
| 120 |
{ERR=>"$full_deprecation"}], |
| 121 |
# more --full with --sort => see test 'sortslct1' below |
| 122 |
|
| 123 |
# Test 'last' + --full |
| 124 |
# Test with "--full", "i1" and "i4" should be displayed |
| 125 |
['slct10dep', '-t" " -f -g1 last 2', {IN_PIPE=>$in_full1}, |
| 126 |
{OUT=>"A 5 i3 5\nB 3 i7 3\n"}, |
| 127 |
{ERR=>"$full_deprecation"}], |
| 128 |
); |
| 129 |
|
| 130 |
if ($have_stable_sort) { |
| 131 |
push @Tests, ( |
| 132 |
# Test 'first' + --full + --sort |
| 133 |
# NOTE: This is subtle: |
| 134 |
# Sorting should be stable: only ordering the column which is used |
| 135 |
# for grouping (column 1 in this test). This means that the second |
| 136 |
# column (containing numbers) should NOT affect sorting, and the order |
| 137 |
# of the lines should not change. The results of this test |
| 138 |
# should be the same as 'slct8'. If the system doesn't have stable |
| 139 |
# 'sort', then the order will change. |
| 140 |
['sortslct1dep', '-s -t" " -f -g1 first 2', {IN_PIPE=>$in_full1}, |
| 141 |
{OUT=>"A 4 i1 4\nB 1 i4 1\n"}, |
| 142 |
{ERR=>"$full_deprecation"}], |
| 143 |
# Test 'last' + --full + --sort |
| 144 |
# See note above regarding 'first' - applies to 'last' as well. |
| 145 |
['sortslct2dep', '-s -t" " -f -g1 last 2', {IN_PIPE=>$in_full1}, |
| 146 |
{OUT=>"A 5 i3 5\nB 3 i7 3\n"}, |
| 147 |
{ERR=>"$full_deprecation"}], |
| 148 |
) |
| 149 |
} |
| 150 |
|
| 151 |
my $save_temps = $ENV{SAVE_TEMPS}; |
| 152 |
my $verbose = $ENV{VERBOSE}; |
| 153 |
|
| 154 |
my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose); |
| 155 |
exit $fail; |