| 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 |
# Until a better way comes along to auto-use Coreutils Perl modules |
| 29 |
# as in the coreutils' autotools system. |
| 30 |
use Coreutils; |
| 31 |
use CuSkip; |
| 32 |
use CuTmpdir qw(datamash); |
| 33 |
use MIME::Base64 ; |
| 34 |
|
| 35 |
(my $program_name = $0) =~ s|.*/||; |
| 36 |
my $prog_bin = 'datamash'; |
| 37 |
|
| 38 |
## Cross-Compiling portability hack: |
| 39 |
## under qemu/binfmt, argv[0] (which is used to report errors) will contain |
| 40 |
## the full path of the binary, if the binary is on the $PATH. |
| 41 |
## So we try to detect what is the actual returned value of the program |
| 42 |
## in case of an error. |
| 43 |
my $prog = `$prog_bin ---print-progname`; |
| 44 |
$prog = $prog_bin unless $prog; |
| 45 |
|
| 46 |
# TODO: add localization tests with "grouping" |
| 47 |
# Turn off localization of executable's output. |
| 48 |
@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3; |
| 49 |
|
| 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 ---print-nan`; |
| 56 |
die "test infrastructure failed: can't determine 'nan' string" unless $nan; |
| 57 |
my $inf = `$prog ---print-inf`; |
| 58 |
die "test infrastructure failed: can't determine 'inf' string" unless $inf; |
| 59 |
|
| 60 |
=pod |
| 61 |
Equivalent R code |
| 62 |
|
| 63 |
pop.sd=function(x)(sqrt(var(x)*(length(x)-1)/length(x))) |
| 64 |
smp.sd=sd |
| 65 |
|
| 66 |
# alternatively, use the built-in covariance function: |
| 67 |
# smp.cov=cov |
| 68 |
smp.cov <- function(x,y) { |
| 69 |
stopifnot(identical(length(x), length(y))) |
| 70 |
sum((x - mean(x)) * (y - mean(y))) / (length(x) - 1) |
| 71 |
} |
| 72 |
pop.cov <- function(x,y) { |
| 73 |
stopifnot(identical(length(x), length(y))) |
| 74 |
sum((x - mean(x)) * (y - mean(y))) / (length(x) ) |
| 75 |
} |
| 76 |
|
| 77 |
# alternative, use the built-in covariance fuction: |
| 78 |
# smp.pearsoncor=cor |
| 79 |
smp.pearsoncor=function(x,y) { smp.cov(x,y)/ ( smp.sd(x)*smp.sd(y) ) } |
| 80 |
pop.pearsoncor=function(x,y) { pop.cov(x,y)/ ( pop.sd(x)*pop.sd(y) ) } |
| 81 |
|
| 82 |
in1.x=c(-0.49,0.14,1.62,2.76,-0.46,3.28,-0.01,2.90,2.46,1.52) |
| 83 |
in1.y=c(-0.21,-0.16,1.86,1.81,0.39,4.17,0.38,1.90,2.69,0.78) |
| 84 |
|
| 85 |
in2.x = c(1.599,-1.011,-1.687,5.070,6.944,7.934,2.134,5.150, |
| 86 |
10.197,11.427,10.379,14.867,11.399,13.479,18.328,16.573, |
| 87 |
17.804,18.694,16.690,21.805) |
| 88 |
in2.y = seq(20) |
| 89 |
|
| 90 |
=cut |
| 91 |
|
| 92 |
my $in1=<<"EOF"; |
| 93 |
-0.49 -0.21 |
| 94 |
0.14 -0.16 |
| 95 |
1.62 1.86 |
| 96 |
2.76 1.81 |
| 97 |
-0.46 0.39 |
| 98 |
3.28 4.17 |
| 99 |
-0.01 0.38 |
| 100 |
2.90 1.90 |
| 101 |
2.46 2.69 |
| 102 |
1.52 0.78 |
| 103 |
EOF |
| 104 |
|
| 105 |
|
| 106 |
my $out1_scov=<<'EOF'; |
| 107 |
1.802 |
| 108 |
EOF |
| 109 |
|
| 110 |
my $out1_pcov=<<'EOF'; |
| 111 |
1.622 |
| 112 |
EOF |
| 113 |
|
| 114 |
my $out1_pcov_hdr=<<'EOF'; |
| 115 |
pcov(field-1,field-2) |
| 116 |
1.622 |
| 117 |
EOF |
| 118 |
|
| 119 |
my $out1_dotprod_hdr=<<'EOF'; |
| 120 |
dotprod(field-1,field-2) |
| 121 |
34.896 |
| 122 |
EOF |
| 123 |
|
| 124 |
my $in2=<<'EOF'; |
| 125 |
1.599 1 |
| 126 |
-1.011 2 |
| 127 |
-1.687 3 |
| 128 |
5.070 4 |
| 129 |
6.944 5 |
| 130 |
7.934 6 |
| 131 |
2.134 7 |
| 132 |
5.150 8 |
| 133 |
10.197 9 |
| 134 |
11.427 10 |
| 135 |
10.379 11 |
| 136 |
14.867 12 |
| 137 |
11.399 13 |
| 138 |
13.479 14 |
| 139 |
18.328 15 |
| 140 |
16.573 16 |
| 141 |
17.804 17 |
| 142 |
18.694 18 |
| 143 |
16.690 19 |
| 144 |
21.805 20 |
| 145 |
EOF |
| 146 |
|
| 147 |
my $out2_p=<<'EOF'; |
| 148 |
0.944 |
| 149 |
EOF |
| 150 |
|
| 151 |
my $out2_s=<<'EOF'; |
| 152 |
0.944 |
| 153 |
EOF |
| 154 |
|
| 155 |
my $in3=<<'EOF'; |
| 156 |
1 2 |
| 157 |
EOF |
| 158 |
|
| 159 |
my $in4=<<'EOF'; |
| 160 |
NA NA |
| 161 |
EOF |
| 162 |
|
| 163 |
my $in5=<<'EOF'; |
| 164 |
1 2 |
| 165 |
2 NA |
| 166 |
3 6 |
| 167 |
EOF |
| 168 |
|
| 169 |
my $in6=<<'EOF'; |
| 170 |
x y |
| 171 |
1 0.5 |
| 172 |
2 1 |
| 173 |
3 1.5 |
| 174 |
4 2 |
| 175 |
EOF |
| 176 |
|
| 177 |
my $out6_pcov_hdr=<<'EOF'; |
| 178 |
pcov(x,y) |
| 179 |
0.625 |
| 180 |
EOF |
| 181 |
|
| 182 |
my $out6_scov_hdr=<<'EOF'; |
| 183 |
scov(x,y) |
| 184 |
0.833 |
| 185 |
EOF |
| 186 |
|
| 187 |
my $out6_ppears_hdr=<<'EOF'; |
| 188 |
ppearson(x,y) |
| 189 |
1 |
| 190 |
EOF |
| 191 |
|
| 192 |
my $out6_spears_hdr=<<'EOF'; |
| 193 |
spearson(x,y) |
| 194 |
1 |
| 195 |
EOF |
| 196 |
|
| 197 |
my $out6_dotprod_hdr=<<'EOF'; |
| 198 |
dotprod(x,y) |
| 199 |
15 |
| 200 |
EOF |
| 201 |
|
| 202 |
my @Tests = |
| 203 |
( |
| 204 |
['c1', 'scov 1:2', {IN_PIPE=>$in1}, {OUT=>$out1_scov}], |
| 205 |
['c2', 'pcov 1:2', {IN_PIPE=>$in1}, {OUT=>$out1_pcov}], |
| 206 |
['dp1', 'dotprod 1:2', {IN_PIPE=>$in1}, {OUT=>"34.896\n"}], |
| 207 |
|
| 208 |
# Pair with output headers - only one field and header should be printed |
| 209 |
['c3', '--header-out pcov 1:2', {IN_PIPE=>$in1}, {OUT=>$out1_pcov_hdr}], |
| 210 |
['c3_hin_p', '-W --header-in --header-out pcov x:y', |
| 211 |
{IN_PIPE=>$in6}, {OUT=>$out6_pcov_hdr}], |
| 212 |
['c3_hin_s', '-W --header-in --header-out scov x:y', |
| 213 |
{IN_PIPE=>$in6}, {OUT=>$out6_scov_hdr}], |
| 214 |
|
| 215 |
['p1', 'ppearson 1:2', {IN_PIPE=>$in2}, {OUT=>$out2_p}], |
| 216 |
['p1_hin', '-W --header-in --header-out ppearson x:y', |
| 217 |
{IN_PIPE=>$in6}, {OUT=>$out6_ppears_hdr}], |
| 218 |
['p2', 'spearson 1:2', {IN_PIPE=>$in2}, {OUT=>$out2_s}], |
| 219 |
['p2_hin', '-W --header-in --header-out spearson x:y', |
| 220 |
{IN_PIPE=>$in6}, {OUT=>$out6_spears_hdr}], |
| 221 |
|
| 222 |
['dp2', '--header-out dotprod 1:2', |
| 223 |
{IN_PIPE=>$in1}, {OUT=>$out1_dotprod_hdr}], |
| 224 |
['dp3', '-W --header-in --header-out dotprod x:y', |
| 225 |
{IN_PIPE=>$in6}, {OUT=>$out6_dotprod_hdr}], |
| 226 |
|
| 227 |
# Test operations on edge-cases of input (one items, no items, |
| 228 |
# different number of items) |
| 229 |
['c4', 'scov 1:2', {IN_PIPE=>$in3}, {OUT=>"$nan\n"}], |
| 230 |
['p4', 'spearson 1:2', {IN_PIPE=>$in3}, {OUT=>"$nan\n"}], |
| 231 |
|
| 232 |
['c5', '--narm scov 1:2', {IN_PIPE=>$in4}, {OUT=>"$nan\n"}], |
| 233 |
['p5', '--narm spearson 1:2', {IN_PIPE=>$in4}, {OUT=>"$nan\n"}], |
| 234 |
['dp5', '--narm dotprod 1:2', {IN_PIPE=>$in4}, {OUT=>"$nan\n"}], |
| 235 |
|
| 236 |
['c6', '--narm scov 1:2', {IN_PIPE=>$in5}, {EXIT=>1}, |
| 237 |
{ERR=>"$prog: input error for operation 'scov': " . |
| 238 |
"fields 1,2 have different number of items\n"}], |
| 239 |
['p6', '--narm spearson 1:2', {IN_PIPE=>$in5}, {EXIT=>1}, |
| 240 |
{ERR=>"$prog: input error for operation 'spearson': " . |
| 241 |
"fields 1,2 have different number of items\n"}], |
| 242 |
['dp6', '--narm dotprod 1:2', {IN_PIPE=>$in5}, {EXIT=>1}, |
| 243 |
{ERR=>"$prog: input error for operation 'dotprod': " . |
| 244 |
"fields 1,2 have different number of items\n"}], |
| 245 |
); |
| 246 |
|
| 247 |
my $save_temps = $ENV{SAVE_TEMPS}; |
| 248 |
my $verbose = $ENV{VERBOSE}; |
| 249 |
|
| 250 |
## |
| 251 |
## For each test, trim the resulting value to maximum three digits |
| 252 |
## after the decimal point. |
| 253 |
## |
| 254 |
for my $t (@Tests) { |
| 255 |
push @{$t}, {OUT_SUBST=>'s/^(-?\d+\.\d{1,3})\d*/\1/'}; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose); |
| 260 |
exit $fail; |