Debian Codegraph @

tests/datamash-crosstab.pl

raw
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 my $prog = `$prog_bin ---print-progname`;
42
43 # Turn off localization of executable's output.
44 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
45
46 my $in1=<<'EOF';
47 a x 1
48 a y 2
49 a z 3
50 b x 4
51 b y 5
52 b z 6
53 c x 7
54 c y 8
55 c z 9
56 EOF
57
58 my $out1_first=<<'EOF';
59 x y z
60 a 1 2 3
61 b 4 5 6
62 c 7 8 9
63 EOF
64
65 my $out1_count=<<'EOF';
66 x y z
67 a 1 1 1
68 b 1 1 1
69 c 1 1 1
70 EOF
71
72 #unsorted input with duplicates
73 my $in2=<<'EOF';
74 a x 1
75 a y 2
76 a x 3
77 EOF
78
79 # when using 'first' operation
80 my $out2_first=<<'EOF';
81 x y
82 a 1 2
83 EOF
84
85 # when using 'last' operation without sorting the data
86 # this output is considered incorrect...
87 # TODO: perhaps warn the user about it (like join warns of unsorted input).
88 my $out2_last_unsorted=<<'EOF';
89 x y
90 a 1 2
91 EOF
92
93 # when using 'last' operation with sorting the data,
94 # correct value is shown
95 my $out2_last_sorted=<<'EOF';
96 x y
97 a 3 2
98 EOF
99
100 my $out2_count_unsorted=<<'EOF';
101 x y
102 a 1 1
103 EOF
104
105 my $out2_count_sorted=<<'EOF';
106 x y
107 a 2 1
108 EOF
109
110 # using 'sum' without sorting the data.
111 # this output is considered incorrect...
112 # TODO: perhaps warn the user about it (like join warns of unsorted input).
113 my $out2_sum_unsorted=<<'EOF';
114 x y
115 a 1 2
116 EOF
117
118 # using 'sum' with sorting the data
119 my $out2_sum_sorted=<<'EOF';
120 x y
121 a 4 2
122 EOF
123
124 #input with missing values (b/y is missing)
125 my $in3=<<'EOF';
126 a x 1
127 a y 2
128 b x 3
129 EOF
130
131 # default filler is 'N/A'
132 my $out3_na=<<'EOF';
133 x y
134 a 1 2
135 b 3 N/A
136 EOF
137
138 # custom filler 'XX'
139 my $out3_xx=<<'EOF';
140 x y
141 a 1 2
142 b 3 XX
143 EOF
144
145 my $in4=<<'EOF';
146 x y
147 1 0.5
148 2 1
149 3 1.5
150 4 2
151 EOF
152
153 my $out4_hdr=<<'EOF';
154 GroupBy(x) GroupBy(y) count(x)
155 0.5 1 1.5 2
156 1 1 N/A N/A N/A
157 2 N/A 1 N/A N/A
158 3 N/A N/A 1 N/A
159 4 N/A N/A N/A 1
160 EOF
161
162 my $out4_no_hdr=<<'EOF';
163 0.5 1 1.5 2
164 1 1 N/A N/A N/A
165 2 N/A 1 N/A N/A
166 3 N/A N/A 1 N/A
167 4 N/A N/A N/A 1
168 EOF
169
170 my @Tests =
171 (
172 ['c1','crosstab 1,2 first 3', {IN_PIPE=>$in1}, {OUT=>$out1_first}],
173 ['c2','ct 1,2 first 3', {IN_PIPE=>$in1}, {OUT=>$out1_first}],
174 ['c3','ct 1,2 count 1', {IN_PIPE=>$in1}, {OUT=>$out1_count}],
175 ['c4','ct 1-2 count 1', {IN_PIPE=>$in1}, {OUT=>$out1_count}],
176
177 # Default operation is count
178 ['c5','ct 1,2', {IN_PIPE=>$in1}, {OUT=>$out1_count}],
179
180 # test unsorted input with duplicates
181 ['c10','ct 1,2 first 3', {IN_PIPE=>$in2}, {OUT=>$out2_first}],
182
183 ['c11',' ct 1,2 last 3', {IN_PIPE=>$in2}, {OUT=>$out2_last_unsorted}],
184 ['c12','-s ct 1,2 last 3', {IN_PIPE=>$in2}, {OUT=>$out2_last_sorted}],
185
186 ['c13',' ct 1,2 sum 3', {IN_PIPE=>$in2}, {OUT=>$out2_sum_unsorted}],
187 ['c14','-s ct 1,2 sum 3', {IN_PIPE=>$in2}, {OUT=>$out2_sum_sorted}],
188
189 # test default operation (count) on unsorted data
190 ['c15',' ct 1,2 count 3', {IN_PIPE=>$in2}, {OUT=>$out2_count_unsorted}],
191 ['c16','-s ct 1,2 count 3', {IN_PIPE=>$in2}, {OUT=>$out2_count_sorted}],
192
193 # test headers
194 ['c17','-W --header-in --header-out ct x,y',
195 {IN_PIPE=>$in4}, {OUT=>$out4_hdr}],
196 ['c18','-W --header-in ct x,y',
197 {IN_PIPE=>$in4}, {OUT=>$out4_no_hdr}],
198
199 # Test missing values
200 ['c30','ct 1,2 first 3', {IN_PIPE=>$in3}, {OUT=>$out3_na}],
201 ['c31','--filler XX ct 1,2 first 3', {IN_PIPE=>$in3}, {OUT=>$out3_xx}],
202
203 # Test wrong usage
204 ['e1', 'ct', {IN_PIPE=>""}, {EXIT=>1},
205 {ERR=>"$prog: missing field for operation 'crosstab'\n"}],
206 ['e2', 'ct 1', {IN_PIPE=>""}, {EXIT=>1},
207 {ERR=>"$prog: crosstab requires exactly 2 fields, found 1\n"}],
208 ['e3', 'ct 1,2,3,4', {IN_PIPE=>""}, {EXIT=>1},
209 {ERR=>"$prog: crosstab requires exactly 2 fields, found 4\n"}],
210 ['e4', 'ct 1,2 md5 4', {IN_PIPE=>""}, {EXIT=>1},
211 {ERR=>"$prog: conflicting operation found: expecting crosstab " .
212 "operations, but found line operation 'md5'\n"}],
213 ['e5', 'ct 1,2 sum 1,2', {IN_PIPE=>""}, {EXIT=>1},
214 {ERR=>"$prog: crosstab supports one operation, found 2\n"}],
215 ['e6', 'ct 1,2 min 2 max 2', {IN_PIPE=>""}, {EXIT=>1},
216 {ERR=>"$prog: crosstab supports one operation, found 2\n"}],
217 ['e7', 'ct 1:2', {IN_PIPE=>""}, {EXIT=>1},
218 {ERR=>"$prog: invalid field pair for operation 'crosstab'\n"}],
219 ['e8', 'ct 1-3', {IN_PIPE=>""}, {EXIT=>1},
220 {ERR=>"$prog: crosstab requires exactly 2 fields, found 3\n"}],
221 );
222
223 my $save_temps = $ENV{SAVE_TEMPS};
224 my $verbose = $ENV{VERBOSE};
225
226 my $fail = run_tests ($program_name, $prog_bin, \@Tests, $save_temps, $verbose);
227 exit $fail;