Debian Codegraph @

tests/datamash-tests.pl

raw
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 ## Portability hack
47 ## Check if the system's sort supports stable sorting ('-s').
48 ## If it doesn't - skip some tests
49 my $rc = system("sort -s < /dev/null > /dev/null 2>/dev/null");
50 die "testing framework failure: failed to execute sort -s"
51 if ( ($rc == -1) || ($rc & 127) );
52 my $sort_exit_code = ($rc >> 8);
53 my $have_stable_sort = ($sort_exit_code==0);
54
55
56 # TODO: add localization tests with "grouping"
57 # Turn off localization of executable's output.
58 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
59
60 # note: '5' appears twice
61 my $in1 = join("\n", qw/1 2 3 4 5 6 7 5 8 9 10/) . "\n";
62
63 # Mix of spaces and tabs
64 my $in2 = "1 2\t 3\n" .
65 "4\t5 6\n";
66 my $in_minmax = join("\n", qw/5 90 -7e2 3 200 0.1e-3 42/) . "\n";
67
68 # Lots of whitespace
69 my $in3 = "1 \t 2\t\t\t3\t\t\n" .
70 "4\t\t\t5 6\n";
71
72 # Only whitespace
73 my $ws1 = "\t\t\t\n";
74 my $ws2 = " \t \n";
75
76 # Leading whitespace
77 my $lws1 = "\t\t1\n";
78 my $lws2 = "\t 1\n";
79
80 # trailing whitespace
81 my $tws1 = "1\t\t\n";
82 my $tws2 = "1 \t\n";
83
84 # both leading and trailing whitespace
85 my $ltws1 = "\t\t1\t\t\n";
86 my $ltws2 = " \t1\t \n";
87 my $ltws3 = "\t 1:2 \t\n";
88
89 my $in_g1=<<'EOF';
90 A 100
91 A 10
92 A 50
93 A 35
94 EOF
95
96 my $in_g2=<<'EOF';
97 A 100
98 A 10
99 A 50
100 A 35
101 B 66
102 B 77
103 B 55
104 EOF
105 my $in_g2_tab = $in_g2;
106 $in_g2_tab =~ s/ /\t/gms;
107
108 my $in_g3=<<'EOF';
109 A 3 W
110 A 5 W
111 A 7 W
112 A 11 X
113 A 13 X
114 B 17 Y
115 B 19 Z
116 C 23 Z
117 EOF
118
119 my $in_g4=<<'EOF';
120 A 5
121 K 6
122 P 2
123 EOF
124
125 my $in_g5=<<'EOF';
126 A 1
127 AA 2
128 AAA 3
129 EOF
130
131 my $in_hdr1=<<'EOF';
132 x y z
133 A 1 10
134 A 2 10
135 A 3 10
136 A 4 10
137 A 4 10
138 B 5 10
139 B 6 20
140 B 7 30
141 C 8 11
142 C 9 22
143 C 1 33
144 C 2 44
145 EOF
146
147 # Header line, with custom field separator
148 my $in_hdr2=<<'EOF';
149 x:y:z
150 A:3:W
151 A:5:W
152 A:7:W
153 A:11:X
154 A:13:X
155 B:17:Y
156 B:19:Z
157 C:23:Z
158 EOF
159
160 # Same as $in_hdr1, but with whitespace delimiters
161 my $in_hdr3=<<"EOF";
162 x \t y z
163 A 1 10
164 A 2 10
165 A 3 10
166 A 4 10
167 A 4 10
168 B 5 10
169 B 6\t20
170 B 7 30
171 C\t 8 \t11
172 C\t9\t\t22
173 C 1 33
174 C 2 44
175 EOF
176
177 my $in_cnt_uniq1=<<'EOF';
178 x y
179 A 1
180 A 2
181 A 1
182 A 2
183 A 1
184 A 2
185 B 1
186 B 2
187 B 1
188 B 2
189 B 1
190 B 2
191 EOF
192
193 my $in_cnt_uniq2=<<'EOF';
194 a B
195 A b
196 A B
197 A C
198 EOF
199
200 # When using whitespace, the second column is 1,2,3.
201 # When using Tab, the second column is 10,20,30.
202 my $in_tab1=<<"EOF";
203 A 1\t10
204 B 2\t20
205 C 3\t30
206 EOF
207
208 # When using whitespace, this input has fours columns.
209 # When using Tab, this input has three columns.
210 # The lines are unsorted. When sorted by the second column,
211 # The output will depend on whether using whitespace or TAB.
212 my $in_sort1=<<"EOF";
213 ! A\tx\t1
214 @ B\tk\t2
215 # D\tj\t3
216 @ B\tx\t5
217 # C\tj\t4
218 ^ C\tg\t6
219 EOF
220
221 # This (sorted) input will return different results based on case-sensitivity
222 my $in_case_sorted=<<'EOF';
223 a X 1
224 a x 3
225 A X 2
226 A x 5
227 b Y 4
228 B Y 6
229 EOF
230 #
231 # This (unsorted) input will return different results based on case-sensitivity
232 my $in_case_unsorted=<<'EOF';
233 a X 1
234 A X 2
235 a x 3
236 b Y 4
237 A x 5
238 B Y 6
239 EOF
240
241 my $in_sort_quote1=<<"EOF";
242 A'1
243 B'2
244 A'3
245 B'4
246 EOF
247
248 my $in_sort_quote2=<<'EOF';
249 A"1
250 B"2
251 A"3
252 B"4
253 EOF
254
255 ## NUL as end-of-line character
256 my $in_nul1="A 1\x00A 2\x00B 3\x00B 4\x00";
257
258 ## Invalid numeric input
259 my $in_invalid_num1=<<'EOF';
260 A 1
261 A 2
262 B 3a
263 B 4
264 EOF
265
266 my $in_precision1=<<'EOF';
267 0.3
268 3e10
269 EOF
270
271 my $in_precision2=<<'EOF';
272 0.3
273 3e14
274 EOF
275
276 my $in_large_buffer1 =
277 "A 1\n" .
278 "A " . "FooBar" x 512 . "\n" .
279 "A " . "FooBar" x 1024 . "\n" .
280 "A 2\n" .
281 "B 3\n" .
282 "B " . "FooBar" x 1024 . "\n" .
283 "B " . "FooBar" x 512 . "\n" .
284 "B 4\n";
285
286 my $in_large_buffer2 =
287 "A " . "FooBar" x 100 . "\n" .
288 "A " . "FooBar" x 200 . "\n" .
289 "B " . "FooBar" x 300 . "\n" .
290 "B " . "FooBar" x 400 . "\n" ;
291
292 my $out_large_buffer_first =
293 "A " . "FooBar" x 100 . "\n" .
294 "B " . "FooBar" x 300 . "\n" ;
295
296 my $out_large_buffer_last =
297 "A " . "FooBar" x 200 . "\n" .
298 "B " . "FooBar" x 400 . "\n" ;
299
300 # Input with variable (and large) number of fields
301 my $in_wide1 =
302 "A " x 10 . "x\n" .
303 "A " x 21 . "x\n" .
304 "A " x 99 . "x\n" .
305 "A " x 1000 . "x\n" .
306 "A " x 2 . "x\n" ;
307
308
309 my $in_empty1=<<'EOF';
310 A::2
311 B::3
312 B::4
313 EOF
314
315 my $in_dup1=<<'EOF';
316 X:Y
317 1:a
318 2:b
319 3:a
320 2:c
321 EOF
322
323 my $in_hdr_only=<<'EOF';
324 X:Y:Z
325 EOF
326
327 my $in_multiple_groups=<<'EOF';
328 a a a foo
329 a a a bar
330 a a b baz
331 a b b alice
332 a b c bob
333 b b c eve
334 b c d frank
335 b c d foo
336 b c d bar
337 b d d baz
338 c d a alice
339 c d a bob
340 c e b eve
341 c e b frank
342 c e c foo
343 d a c bar
344 d a d baz
345 d a d alice
346 d b e bob
347 d b e eve
348 e b a frank
349 e c a foo
350 e c b bar
351 e c b baz
352 e d c alice
353 EOF
354
355 my $out_multiple_groups=<<'EOF';
356 a a a bar,foo
357 a a b baz
358 a b b alice
359 a b c bob
360 b b c eve
361 b c d bar,foo,frank
362 b d d baz
363 c d a alice,bob
364 c e b eve,frank
365 c e c foo
366 d a c bar
367 d a d alice,baz
368 d b e bob,eve
369 e b a frank
370 e c a foo
371 e c b bar,baz
372 e d c alice
373 EOF
374
375 =pod
376 Example:
377 my $data = "a 1\nb 2\n";
378 my $out = transform_column($data, 2, \&md5_hex);
379 # out => md5_hex("1") . "\n" . md5_hex("2") . "\n" ;
380 =cut
381 sub transform_column($$$)
382 {
383 my $input_text = shift;
384 my $input_column = shift;
385 my $function = shift;
386
387 return join "",
388 map { "$_\n" }
389 map { &$function($_->[ $input_column - 1 ]) }
390 map { [ split / / ] }
391 split("\n", $input_text);
392 }
393
394 sub single_line_base64($)
395 {
396 my $in = shift;
397 return encode_base64($in,'');
398 }
399
400 my $out_g1_base64 = transform_column ($in_g1, 2, \&single_line_base64);
401 my $out_g1_debase64 = transform_column ($out_g1_base64, 1, \&decode_base64);
402
403 my @Tests =
404 (
405 # Basic tests, single field, single group, default everything
406 ['b1', 'count 1' , {IN_PIPE=>$in1}, {OUT => "11\n"}],
407 ['b2', 'sum 1', {IN_PIPE=>$in1}, {OUT => "60\n"}],
408 ['b3', 'min 1', {IN_PIPE=>$in1}, {OUT => "1\n"}],
409 ['b4', 'max 1', {IN_PIPE=>$in1}, {OUT => "10\n"}],
410 ['b5', 'absmin 1', {IN_PIPE=>$in1}, {OUT => "1\n"}],
411 ['b6', 'absmax 1', {IN_PIPE=>$in1}, {OUT => "10\n"}],
412 ['b7', 'median 1', {IN_PIPE=>$in1}, {OUT => "5\n"}],
413 ['b8', 'mode 1', {IN_PIPE=>$in1}, {OUT => "5\n"}],
414 ['b9', 'antimode 1', {IN_PIPE=>$in1}, {OUT => "1\n"}],
415 ['b10', 'unique 1', {IN_PIPE=>$in1}, {OUT => "1,10,2,3,4,5,6,7,8,9\n"}],
416 ['b11', '--collapse-delimiter=^ unique 1',
417 {IN_PIPE=>$in1}, {OUT => "1^10^2^3^4^5^6^7^8^9\n"}],
418 ['b11.1', '-c^ unique 1',
419 {IN_PIPE=>$in1}, {OUT => "1^10^2^3^4^5^6^7^8^9\n"}],
420 ['b12', 'collapse 1', {IN_PIPE=>$in1}, {OUT => "1,2,3,4,5,6,7,5,8,9,10\n"}],
421 ['b13', '--collapse-delimiter=^ collapse 1',
422 {IN_PIPE=>$in1}, {OUT => "1^2^3^4^5^6^7^5^8^9^10\n"}],
423 ['b13.1', '-c ^ collapse 1',
424 {IN_PIPE=>$in1}, {OUT => "1^2^3^4^5^6^7^5^8^9^10\n"}],
425
426 # on a different architecture, would printf(%Lg) print something else?
427 # Use OUT_SUBST to trim output to 1.3 digits
428 ['b14', 'mean 1', {IN_PIPE=>$in1}, {OUT => "5.454\n"},
429 {OUT_SUBST=>'s/^(\d\.\d{3}).*/\1/'}],
430 ['b15', 'pstdev 1', {IN_PIPE=>$in1}, {OUT => "2.742\n"},
431 {OUT_SUBST=>'s/^(\d\.\d{3}).*/\1/'}],
432 ['b16', 'sstdev 1', {IN_PIPE=>$in1}, {OUT => "2.876\n"},
433 {OUT_SUBST=>'s/^(\d\.\d{3}).*/\1/'}],
434 ['b17', 'pvar 1', {IN_PIPE=>$in1}, {OUT => "7.520\n"},
435 {OUT_SUBST=>'s/^(\d\.\d{3}).*/\1/'}],
436 ['b18', 'svar 1', {IN_PIPE=>$in1}, {OUT => "8.272\n"},
437 {OUT_SUBST=>'s/^(\d\.\d{3}).*/\1/'}],
438 ['b19', 'countunique 1', {IN_PIPE=>$in1}, {OUT => "10\n"}],
439 ['b20', 'first 1', {IN_PIPE=>$in1}, {OUT => "1\n"}],
440 ['b21', 'last 1', {IN_PIPE=>$in1}, {OUT => "10\n"}],
441 # This test just ensures the 'rand' operation is functioning.
442 # It does not verify randomness (see datamash-rand.sh test for that).
443 ['b22', 'rand 1', {IN_PIPE=>$in1}, {OUT => "\n"},
444 {OUT_SUBST=>'s/[0-9]+//'}],
445
446 ## Some error checkings
447 ['e1', 'sum', {IN_PIPE=>""}, {EXIT=>1},
448 {ERR=>"$prog: missing field for operation 'sum'\n"}],
449 ['e2', 'foobar', {IN_PIPE=>""}, {EXIT=>1},
450 {ERR=>"$prog: invalid operation 'foobar'\n"}],
451 ['e3', '', {IN_PIPE=>""}, {EXIT=>1},
452 {ERR=>"$prog: missing operation specifiers\n" .
453 "Try '$prog --help' for more information.\n"}],
454 ['e4', 'sum 1' , {IN_PIPE=>"a\n"}, {EXIT=>1},
455 {ERR=>"$prog: invalid numeric value in line 1 field 1: 'a'\n"}],
456 ['e5', '-g 4, sum 1' , {IN_PIPE=>"a\n"}, {EXIT=>1},
457 {ERR=>"$prog: missing field for operation 'groupby'\n"}],
458 ['e6', '-g 4,x sum 1' , {IN_PIPE=>"a\n"}, {EXIT=>1},
459 {ERR=>"$prog: -H or --header-in must be used with named columns\n"}],
460 ['e7', '-g ,x sum 1' , {IN_PIPE=>"a\n"}, {EXIT=>1},
461 {ERR=>"$prog: missing field for operation 'groupby'\n"}],
462 ['e8', '-g 1,0 sum 1' , {IN_PIPE=>"a\n"}, {EXIT=>1},
463 {ERR=>"$prog: invalid field '0' for operation 'groupby'\n"}],
464 ['e9', '-g 1X0 sum 1' , {IN_PIPE=>"a\n"}, {EXIT=>1},
465 {ERR=>"$prog: invalid numeric value '1X0'\n"}],
466 ['e10', '-g 1 -t XX sum 1' , {IN_PIPE=>"a\n"}, {EXIT=>1},
467 {ERR=>"$prog: the delimiter must be a single character\n"}],
468 ['e10.1', '-g 1 -t "" sum 1' , {IN_PIPE=>"a\n"}, {EXIT=>1},
469 {ERR=>"$prog: the delimiter must be a single character\n"}],
470 ['e11', '--foobar' , {IN_PIPE=>"a\n"}, {EXIT=>1},
471 {ERR=>"$prog: unrecognized option foobar\n" .
472 "Try '$prog --help' for more information.\n"},
473 # This ERR_SUBST is needed because on some systems (e.g. OpenBSD),
474 # The error message from 'getopt_long' is slightly
475 # different than GNU libc's.
476 {ERR_SUBST=>'s/(unknown|unrecognized) option.*(foobar).*' .
477 '/unrecognized option $2/'}],
478 ['e12', '-t" " -H unique 4' , {IN_PIPE=>$in_hdr1}, {EXIT=>1},
479 {ERR=>"$prog: invalid input: field 4 requested, " .
480 "line 1 has only 3 fields\n"}],
481 ['e13', '-t" " sum 6' , {IN_PIPE=>$in_g3}, {EXIT=>1},
482 {ERR=>"$prog: invalid input: field 6 requested, " .
483 "line 1 has only 3 fields\n"}],
484 ['e14', '--header-in -t: sum 6' , {IN_PIPE=>$in_hdr2}, {EXIT=>1},
485 {ERR=>"$prog: invalid input: field 6 requested, " .
486 "line 2 has only 3 fields\n"}],
487 ['e15', 'sum foo' , {IN_PIPE=>"a"}, {EXIT=>1},
488 {ERR=>"$prog: -H or --header-in must be used with named columns\n"}],
489 ['e16', '-t" " sum 2' , {IN_PIPE=>$in_invalid_num1}, {EXIT=>1},
490 {ERR=>"$prog: invalid numeric value in line 3 field 2: '3a'\n"}],
491 ['e17', 'sum 1' , {IN_PIPE=>"1e-20000\n"}, {EXIT=>1},
492 {ERR=>"$prog: invalid numeric value in line 1 field 1: '1e-20000'\n"}],
493 ['e18', 'sum 0' , {IN_PIPE=>"a"}, {EXIT=>1},
494 {ERR=>"$prog: invalid field '0' for operation 'sum'\n"}],
495 ['e19', '-- sum -2' , {IN_PIPE=>"a"}, {EXIT=>1},
496 {ERR=>"$prog: invalid field range for operation 'sum'\n"}],
497 ['e21', 'sum ""' , {IN_PIPE=>"a"}, {EXIT=>1},
498 {ERR=>"$prog: missing field for operation 'sum'\n"}],
499 ['e22', '-t" " -g 7 unique 1' , {IN_PIPE=>$in_hdr1}, {EXIT=>1},
500 {ERR=>"$prog: invalid input: field 7 requested, " .
501 "line 2 has only 3 fields\n"}],
502 ['e23', '-t" " -g -2 unique 1' , {IN_PIPE=>$in_hdr1}, {EXIT=>1},
503 {ERR=>"$prog: invalid field '-' for operation 'groupby'\n"}],
504 ['e24', '-t" " --header-out -g 5 count 1', {IN_PIPE=>$in_hdr1}, {EXIT=>1},
505 {ERR=>"$prog: invalid input: field 5 requested, " .
506 "line 1 has only 3 fields\n"}],
507 ['e25', '-g 1,,2 sum 1' , {IN_PIPE=>"a\n"}, {EXIT=>1},
508 {ERR=>"$prog: missing field for operation 'groupby'\n"}],
509 ['e26', '-g 4-1 sum 1', {IN_PIPE=>"a\n"}, {EXIT=>1},
510 {ERR=>"$prog: invalid field range for operation 'groupby'\n"}],
511 ['e27', '-g 1- sum 1', {IN_PIPE=>"a\n"}, {EXIT=>1},
512 {ERR=>"$prog: invalid field range for operation 'groupby'\n"}],
513 ['e28', '--collapse-delimiter=foo', {IN_PIPE=>"a\n"}, {EXIT=>1},
514 {ERR=>"$prog: the delimiter must be a single character\n"}],
515 ['e29', '-c foo', {IN_PIPE=>"a\n"}, {EXIT=>1},
516 {ERR=>"$prog: the delimiter must be a single character\n"}],
517
518 # No newline at the end of the lines
519 ['nl1', 'sum 1', {IN_PIPE=>"99"}, {OUT=>"99\n"}],
520 ['nl2', 'sum 1', {IN_PIPE=>"1\n99"}, {OUT=>"100\n"}],
521
522 # empty input = empty output, regardless of options
523 [ 'emp1', 'count 1', {IN_PIPE=>""}, {OUT=>""}],
524 [ 'emp3', '--header-in count 2', {IN_PIPE=>""},{OUT=>""}],
525 [ 'emp4', '--header-out count 2', {IN_PIPE=>""},{OUT=>""}],
526 [ 'emp9', '-g3 count 2', {IN_PIPE=>""},{OUT=>""}],
527
528 ## Field extraction
529 ['f1', '-W sum 1', {IN_PIPE=>$in2}, {OUT=>"5\n"}],
530 ['f2', '-W sum 2', {IN_PIPE=>$in2}, {OUT=>"7\n"}],
531 ['f3', '-W sum 3', {IN_PIPE=>$in2}, {OUT=>"9\n"}],
532 ['f4', '-W sum 3 sum 1', {IN_PIPE=>$in2}, {OUT=>"9\t5\n"}],
533 ['f5', '-t: sum 4', {IN_PIPE=>"11:12::13:14"}, {OUT=>"13\n"}],
534 # collase non-last field (followed by whitespace, not new-line)
535 ['f6', '-t" " unique 1', {IN_PIPE=>$in_g2}, {OUT=>"A,B\n"}],
536 ['f7', 'unique 1', {IN_PIPE=>$in_g2_tab}, {OUT=>"A,B\n"}],
537 # Differences between TAB (detail), Space, and whitespace delimiters
538 ['f8', 'collapse 1', {IN_PIPE=>$in2}, {OUT=>"1 2,4\n"}],
539 ['f9', 'collapse 2', {IN_PIPE=>$in2}, {OUT=>" 3,5 6\n"}],
540 ['f10', '-t" " collapse 1', {IN_PIPE=>$in2}, {OUT=>"1,4\t5\n"}],
541 ['f11', '-t" " collapse 2', {IN_PIPE=>$in2}, {OUT=>"2\t,6\n"}],
542 ['f12', '-W collapse 1', {IN_PIPE=>$in2}, {OUT=>"1,4\n"}],
543 ['f13', '-W collapse 2', {IN_PIPE=>$in2}, {OUT=>"2,5\n"}],
544 ['f14', '-W collapse 3', {IN_PIPE=>$in2}, {OUT=>"3,6\n"}],
545 ['f15', '-W collapse 1', {IN_PIPE=>$in3}, {OUT=>"1,4\n"}],
546 ['f16', '-W collapse 2', {IN_PIPE=>$in3}, {OUT=>"2,5\n"}],
547 ['f17', '-W collapse 3', {IN_PIPE=>$in3}, {OUT=>"3,6\n"}],
548
549 # Empty fields (2 is the empty field in 'in_empty1')
550 ['f18', '-t: -g1 count 3', {IN_PIPE=>$in_empty1}, {OUT=>"A:1\nB:2\n"}],
551 # String operations on an empty field should work
552 ['f19', '-t: -g1 count 2', {IN_PIPE=>$in_empty1}, {OUT=>"A:1\nB:2\n"}],
553 ['f20', '-t: -g1 countunique 2', {IN_PIPE=>$in_empty1}, {OUT=>"A:1\nB:1\n"}],
554 #TODO: should collapsing two empty string result in a single comma?
555 ['f21', '-t: -g1 collapse 2', {IN_PIPE=>$in_empty1}, {OUT=>"A:\nB:,\n"}],
556 ['f22', '-t: -g1 unique 2', {IN_PIPE=>$in_empty1}, {OUT=>"A:\nB:\n"}],
557 # Numeric operation on an empty field should not work
558 ['f23', '-t: -g1 sum 2', {IN_PIPE=>$in_empty1}, {EXIT=>1},
559 {ERR=>"$prog: invalid numeric value in line 1 field 2: ''\n"}],
560 ['f24', '-W -g1,2,3 uniq 4',
561 {IN_PIPE=>$in_multiple_groups}, {OUT=>$out_multiple_groups}],
562 ['f25', '-W -g1-3 uniq 4',
563 {IN_PIPE=>$in_multiple_groups}, {OUT=>$out_multiple_groups}],
564
565 # whitespace only, different field delimiters
566 ['ws1.1', 'check', {IN_PIPE=>$ws1}, {OUT=>"1 line, 4 fields\n"}],
567 ['ws1.2', '-W check', {IN_PIPE=>$ws1}, {OUT=>"1 line, 1 field\n"}],
568 ['ws1.3', '-t: check', {IN_PIPE=>$ws1}, {OUT=>"1 line, 1 field\n"}],
569 ['ws2.1', 'check', {IN_PIPE=>$ws2}, {OUT=>"1 line, 2 fields\n"}],
570 ['ws2.2', '-W check', {IN_PIPE=>$ws2}, {OUT=>"1 line, 1 field\n"}],
571 ['ws2.3', '-t: check', {IN_PIPE=>$ws2}, {OUT=>"1 line, 1 field\n"}],
572
573 # leading whitespace, different field delimiters
574 ['lws1.1', 'check', {IN_PIPE=>$lws1}, {OUT=>"1 line, 3 fields\n"}],
575 ['lws1.2', '-W check', {IN_PIPE=>$lws1}, {OUT=>"1 line, 1 field\n"}],
576 ['lws1.3', '-t: check', {IN_PIPE=>$lws1}, {OUT=>"1 line, 1 field\n"}],
577 ['lws2.1', 'check', {IN_PIPE=>$lws2}, {OUT=>"1 line, 2 fields\n"}],
578 ['lws2.2', '-W check', {IN_PIPE=>$lws2}, {OUT=>"1 line, 1 field\n"}],
579 ['lws2.3', '-t: check', {IN_PIPE=>$lws2}, {OUT=>"1 line, 1 field\n"}],
580
581 # trailing whitespace, different field delimiters
582 ['tws1.1', 'check', {IN_PIPE=>$tws1}, {OUT=>"1 line, 3 fields\n"}],
583 ['tws1.2', '-W check', {IN_PIPE=>$tws1}, {OUT=>"1 line, 2 fields\n"}],
584 ['tws1.3', '-t: check', {IN_PIPE=>$tws1}, {OUT=>"1 line, 1 field\n"}],
585 ['tws2.1', 'check', {IN_PIPE=>$tws2}, {OUT=>"1 line, 2 fields\n"}],
586 ['tws2.2', '-W check', {IN_PIPE=>$tws2}, {OUT=>"1 line, 2 fields\n"}],
587 ['tws2.3', '-t: check', {IN_PIPE=>$tws2}, {OUT=>"1 line, 1 field\n"}],
588
589 # both leading and trailing whitespace, different field delimiters
590 ['ltws1.1', 'check', {IN_PIPE=>$ltws1}, {OUT=>"1 line, 5 fields\n"}],
591 ['ltws1.2', '-W check', {IN_PIPE=>$ltws1}, {OUT=>"1 line, 2 fields\n"}],
592 ['ltws1.3', '-t: check', {IN_PIPE=>$ltws1}, {OUT=>"1 line, 1 field\n"}],
593 ['ltws2.1', 'check', {IN_PIPE=>$ltws2}, {OUT=>"1 line, 3 fields\n"}],
594 ['ltws2.2', '-W check', {IN_PIPE=>$ltws2}, {OUT=>"1 line, 2 fields\n"}],
595 ['ltws2.3', '-t: check', {IN_PIPE=>$ltws2}, {OUT=>"1 line, 1 field\n"}],
596 ['ltws3.1', 'check', {IN_PIPE=>$ltws3}, {OUT=>"1 line, 3 fields\n"}],
597 ['ltws3.2', '-W check', {IN_PIPE=>$ltws3}, {OUT=>"1 line, 2 fields\n"}],
598 ['ltws3.3', '-t: check', {IN_PIPE=>$ltws3}, {OUT=>"1 line, 2 fields\n"}],
599
600 # Test Absolute min/max
601 ['mm1', 'min 1', {IN_PIPE=>$in_minmax}, {OUT=>"-700\n"}],
602 ['mm2', 'max 1', {IN_PIPE=>$in_minmax}, {OUT=>"200\n"}],
603 ['mm3', 'absmin 1', {IN_PIPE=>$in_minmax}, {OUT=>"0.0001\n"}],
604 ['mm4', 'absmax 1', {IN_PIPE=>$in_minmax}, {OUT=>"-700\n"}],
605
606 #
607 # Test Grouping
608 #
609
610 # Single group (key in column 1)
611 ['g1.1', '-t" " -g1 sum 2', {IN_PIPE=>$in_g1}, {OUT=>"A 195\n"}],
612 ['g2.1', '-t" " -g1 median 2', {IN_PIPE=>$in_g1}, {OUT=>"A 42.5\n"}],
613 ['g3.1', '-t" " -g1 collapse 2', {IN_PIPE=>$in_g1},
614 {OUT=>"A 100,10,50,35\n"}],
615 ['g4.1', '-t" " -g1 count 2', {IN_PIPE=>$in_g5},
616 {OUT=>"A 1\nAA 1\nAAA 1\n"}],
617 # Group on the last column
618 ['grp5', '-t" " -g3 count 1', {IN_PIPE=>$in_hdr1},
619 {OUT=>"z 1\n10 6\n20 1\n30 1\n11 1\n22 1\n33 1\n44 1\n"}],
620
621 # 3 groups, single line per group, custom delimiter
622 ['g7.1', '-g2 -t= mode 1', {IN_PIPE=>"1=A\n2=B\n3=C\n"},
623 {OUT=>"A=1\nB=2\nC=3\n"}],
624
625 # Multiple keys (from different columns)
626 ['g8.1', '-t" " -g1,3 sum 2', {IN_PIPE=>$in_g3},
627 {OUT=>"A W 15\nA X 24\nB Y 17\nB Z 19\nC Z 23\n"}],
628 ['g8.2', '-t" " -g1-2 count 2', {IN_PIPE=>$in_g4},
629 {OUT=>"A 5 1\nK 6 1\nP 2 1\n"}],
630
631 # count on non-numeric fields
632 ['cnt1', '-t" " -g 1 count 1', {IN_PIPE=>$in_g2},
633 {OUT=>"A 4\nB 3\n"}],
634
635 # Input Header
636 ['hdr1', '-t" " -g 1 --header-in count 2',{IN_PIPE=>$in_hdr1},
637 {OUT=>"A 5\nB 3\nC 4\n"}],
638
639 # Input and output header
640 ['hdr2', '-t" " -g 1 --header-in --header-out count 2',{IN_PIPE=>$in_hdr1},
641 {OUT=>"GroupBy(x) count(y)\nA 5\nB 3\nC 4\n"}],
642
643 # Output Header
644 ['hdr4', '-t" " -g 1 --header-out count 2', {IN_PIPE=>$in_g3},
645 {OUT=>"GroupBy(field-1) count(field-2)\nA 5\nB 2\nC 1\n"}],
646
647 # Header without grouping
648 ['hdr6', '-t" " --header-out count 2', {IN_PIPE=>$in_g3},
649 {OUT=>"count(field-2)\n8\n"}],
650
651 # Output Header, multiple ops
652 ['hdr7', '-t" " -g 1 --header-out count 2 unique 3', {IN_PIPE=>$in_g3},
653 {OUT=>"GroupBy(field-1) count(field-2) unique(field-3)\n" .
654 "A 5 W,X\nB 2 Y,Z\nC 1 Z\n"}],
655
656 # Headers, non white-space separator
657 ['hdr8', '-g 1 -H -t: count 2 unique 3', {IN_PIPE=>$in_hdr2},
658 {OUT=>"GroupBy(x):count(y):unique(z)\nA:5:W,X\nB:2:Y,Z\nC:1:Z\n"}],
659
660 # Headers, non white-space separator, 3 operations
661 ['hdr9', '-g 1 -H -t: count 2 unique 3 sum 2', {IN_PIPE=>$in_hdr2},
662 {OUT=>"GroupBy(x):count(y):unique(z):sum(y)\n" .
663 "A:5:W,X:39\nB:2:Y,Z:36\nC:1:Z:23\n"}],
664
665 # Headers, white-space separator, 3 operations
666 ['hdr10', '-W -g 1 --header-in --header-out count 2',{IN_PIPE=>$in_hdr3},
667 {OUT=>"GroupBy(x)\tcount(y)\nA\t5\nB\t3\nC\t4\n"}],
668
669 # Group + output headers on the last column
670 ['hdr11', '-t" " --header-out -g3 count 1', {IN_PIPE=>$in_hdr1},
671 {OUT=>"GroupBy(field-3) count(field-1)\n" .
672 "z 1\n10 6\n20 1\n30 1\n11 1\n22 1\n33 1\n44 1\n"}],
673 # Group + input headers on last column
674 ['hdr12', '-t" " --header-in -g3 count 1', {IN_PIPE=>$in_hdr1},
675 {OUT=>"10 6\n20 1\n30 1\n11 1\n22 1\n33 1\n44 1\n"}],
676 # Group + input/output headers on last column
677 ['hdr13', '-t" " -H -g3 count 1', {IN_PIPE=>$in_hdr1},
678 {OUT=>"GroupBy(z) count(x)\n10 6\n20 1\n30 1\n11 1\n22 1\n33 1\n44 1\n"}],
679 # Input has only one header line (no data lines), and the user requested
680 # header-in and header-out => header line should be printed
681 ['hdr14', '-t: -H sum 1', {IN_PIPE=>$in_hdr_only},
682 {OUT=>"sum(X)\n"}],
683 ['hdr16', '-t: -s -g1 -H sum 2', {IN_PIPE=>$in_hdr_only},
684 {OUT=>"GroupBy(X):sum(Y)\n"}],
685 ['hdr18', '-t: --header-in sum 1', {IN_PIPE=>$in_hdr_only},
686 {OUT=>""}],
687 ['hdr19', '-t: -H reverse', {IN_PIPE=>$in_hdr_only},
688 {OUT=>"Z:Y:X\n"}],
689 ['hdr20', '-t: --header-in reverse', {IN_PIPE=>$in_hdr_only},
690 {OUT=>""}],
691 ['hdr21', '-t: -H rmdup 1', {IN_PIPE=>$in_hdr_only},
692 {OUT=>$in_hdr_only}],
693 ['hdr22', '-t: --header-in rmdup 1', {IN_PIPE=>$in_hdr_only},
694 {OUT=>""}],
695 ['hdr23', '-t: --header-in rmdup 1', {IN_PIPE=>""}, {OUT=>""}],
696 ['hdr24', '-t: -H rmdup 1', {IN_PIPE=>""}, {OUT=>""}],
697
698 # percentile operation has special header handling (which includes
699 # the percentile value).
700 ['hdr25', '-W -H perc 2', {IN_PIPE=>$in_hdr1},
701 {OUT=>"perc:95(y)\n8.45\n"},],
702 ['hdr26', '-W -H perc:50 2', {IN_PIPE=>$in_hdr1},
703 {OUT=>"perc:50(y)\n4\n"}],
704
705 # Test single line per group
706 ['sl1', '-t" " -g 1 mean 2', {IN_PIPE=>$in_g4},
707 {OUT=>"A 5\nK 6\nP 2\n"}],
708
709 # Test countunique operation
710 ['cuq1', '-t" " -g 1 countunique 3', {IN_PIPE=>$in_g3},
711 {OUT=>"A 2\nB 2\nC 1\n"}],
712 ['cuq2', '-t" " -g 1 countunique 2', {IN_PIPE=>$in_g4},
713 {OUT=>"A 1\nK 1\nP 1\n"}],
714 ['cuq3', '-t" " --header-in -g 1 countunique 2', {IN_PIPE=>$in_cnt_uniq1},
715 {OUT=>"A 2\nB 2\n"}],
716 # countunique, case-insensitive
717 ['cuq4', '-t" " -g 1 countunique 2', {IN_PIPE=>$in_cnt_uniq2},
718 {OUT=>"a 1\nA 3\n"}],
719 ['cuq5', '-i -t" " -g 1 countunique 2', {IN_PIPE=>$in_cnt_uniq2},
720 {OUT=>"a 2\n"}],
721
722 # Test Tab vs White-space field separator
723 ['tab1', "sum 2", {IN_PIPE=>$in_tab1}, {OUT=>"60\n"}],
724 ['tab2', '-W sum 2', {IN_PIPE=>$in_tab1}, {OUT=>"6\n"}],
725
726 # Test Auto-Sorting
727 # With default separator (White-space), the second column is A,B,C,D
728 ['sort1', '-W -s -g 2 unique 3', {IN_PIPE=>$in_sort1},
729 {OUT=>"A\tx\nB\tk,x\nC\tg,j\nD\tj\n"}],
730 # With TAB separator, the second column is g,j,k,x
731 ['sort2', '-s -g 2 unique 3', {IN_PIPE=>$in_sort1},
732 {OUT=>"g\t6\nj\t3,4\nk\t2\nx\t1,5\n"}],
733 # Control check: if we do not sort, the some groups will appear twice
734 # because the input is not sorted.
735 ['sort3', '-g 2 unique 3', {IN_PIPE=>$in_sort1},
736 {OUT=>"x\t1\nk\t2\nj\t3\nx\t5\nj\t4\ng\t6\n"}],
737 ['sort4', '-s -g 1 sum 2 -t "\'" ', {IN_PIPE=>$in_sort_quote1},
738 {OUT=>"A'4\nB'6\n"}],
739 ['sort5', '-s -g 1 sum 2 -t "\"" ', {IN_PIPE=>$in_sort_quote2},
740 {OUT=>"A\"4\nB\"6\n"}],
741
742 # Test Case-sensitivity, on sorted input (no 'sort' piping)
743 # on both grouping and string operations
744 ['case1', '-t" " -g 1 sum 3', {IN_PIPE=>$in_case_sorted},
745 {OUT=>"a 4\nA 7\nb 4\nB 6\n"}],
746 ['case2', '-t" " -i -g 1 sum 3', {IN_PIPE=>$in_case_sorted},
747 {OUT=>"a 11\nb 10\n"}],
748 ['case3', '-t" " -g 1 unique 2', {IN_PIPE=>$in_case_sorted},
749 {OUT=>"a X,x\nA X,x\nb Y\nB Y\n"}],
750 ['case4', '-t" " -i -g 1 unique 2', {IN_PIPE=>$in_case_sorted},
751 {OUT=>"a X\nb Y\n"}],
752 ['case4.1', '-i -t" " -g 1 unique 2', {IN_PIPE=>$in_cnt_uniq2},
753 {OUT=>"a B,C\n"}],
754
755 # Test Case-sensitivity, on non-sorted input (with 'sort' piping)
756 # on both grouping and string operations.
757 ['case5', '-t" " -s -g 1 sum 3', {IN_PIPE=>$in_case_unsorted},
758 {OUT=>"A 7\nB 6\na 4\nb 4\n"}],
759 ['case6', '-t" " -s -g 1 unique 2', {IN_PIPE=>$in_case_unsorted},
760 {OUT=>"A X,x\nB Y\na X,x\nb Y\n"}],
761
762 ## Test nul-terminated lines
763 ['nul1', '-t" " -z -g 1 sum 2', {IN_PIPE=>$in_nul1},
764 {OUT=>"A 3\x00B 7\x00"}],
765 ['nul2', '-t" " --zero-terminated -g 1 sum 2', {IN_PIPE=>$in_nul1},
766 {OUT=>"A 3\x00B 7\x00"}],
767
768 # Test --help (but don't verify the output)
769 ['help1', '--help', {IN_PIPE=>""}, {OUT => ""},
770 {OUT_SUBST=>'s/^.*//gms'}],
771 ['help2', '-h', {IN_PIPE=>""}, {OUT => ""},
772 {OUT_SUBST=>'s/^.*//gms'}],
773 ['ver1', '--version', {IN_PIPE=>""}, {OUT => ""},
774 {OUT_SUBST=>'s/^.*//gms'}],
775 ['ver2', '-V', {IN_PIPE=>""}, {OUT => ""},
776 {OUT_SUBST=>'s/^.*//gms'}],
777
778 # Test output precision (number of digits) for numerical operations.
779 # The current precision is 14 digits (hard-coded).
780 ['prcs1', 'sum 1', {IN_PIPE=>"1e1"}, {OUT => "10\n"}],
781 ['prcs2', 'sum 1', {IN_PIPE=>"1e7"}, {OUT => "10000000\n"}],
782 ['prcs3', 'sum 1', {IN_PIPE=>"1e9"}, {OUT => "1000000000\n"}],
783 ['prcs4', 'sum 1', {IN_PIPE=>"1.8e12"}, {OUT => "1800000000000\n"}],
784 ['prcs5', 'sum 1', {IN_PIPE=>"1.234e13"}, {OUT => "12340000000000\n"}],
785 ['prcs6', 'sum 1', {IN_PIPE=>"1.234e14"}, {OUT => "1.234e+14\n"}],
786 ['prcs7', 'sum 1', {IN_PIPE=>"-1.8e12"}, {OUT => "-1800000000000\n"}],
787 ['prcs8', 'sum 1', {IN_PIPE=>"-1.234e13"}, {OUT => "-12340000000000\n"}],
788 ['prcs9', 'sum 1', {IN_PIPE=>"-1.234e14"}, {OUT => "-1.234e+14\n"}],
789 ['prcs10', 'sum 1', {IN_PIPE=>$in_precision1}, {OUT => "30000000000.3\n"}],
790 ['prcs11', 'sum 1', {IN_PIPE=>$in_precision2}, {OUT => "3e+14\n"}],
791
792 # Test first,last operations (and 'field_op_replace_string()')
793 ['fst1', '-t" " first 2', {IN_PIPE=>$in_g1}, {OUT=>"100\n"}],
794 ['fst2', '-t" " -g 1 first 2', {IN_PIPE=>$in_g1}, {OUT=>"A 100\n"}],
795 ['fst3', '-t" " -g 1 first 2', {IN_PIPE=>$in_g2}, {OUT=>"A 100\nB 66\n"}],
796 ['fst4', '-t" " -g 1 first 2', {IN_PIPE=>$in_g4}, {OUT=>"A 5\nK 6\nP 2\n"}],
797 ['fst5', '-t" " -g 1 first 2', {IN_PIPE=>$in_large_buffer1},
798 {OUT=>"A 1\nB 3\n"}],
799 ['fst6', '-t" " -g 1 first 2', {IN_PIPE=>$in_large_buffer2},
800 {OUT=>$out_large_buffer_first}],
801
802 ['lst1', '-t" " last 2', {IN_PIPE=>$in_g1}, {OUT=>"35\n"}],
803 ['lst2', '-t" " -g 1 last 2', {IN_PIPE=>$in_g1}, {OUT=>"A 35\n"}],
804 ['lst3', '-t" " -g 1 last 2', {IN_PIPE=>$in_g2}, {OUT=>"A 35\nB 55\n"}],
805 ['lst4', '-t" " -g 1 last 2', {IN_PIPE=>$in_g4}, {OUT=>"A 5\nK 6\nP 2\n"}],
806 ['lst5', '-t" " -g 1 last 2', {IN_PIPE=>$in_large_buffer1},
807 {OUT=>"A 2\nB 4\n"}],
808 ['lst6', '-t" " -g 1 last 2', {IN_PIPE=>$in_large_buffer2},
809 {OUT=>$out_large_buffer_last}],
810
811 ## Test md5/sha1/sha256/sha512 operations.
812 ## NOTE: this just ensures the operations don't fail, and produces the
813 ## expected length of output strings.
814 ## validation of the calculation is tested in datamash-{md5,sha}.pl.
815 ['md5-1', '-W md5 2', {IN_PIPE=>$in_g1},
816 {OUT_SUBST=>'s/^[a-f0-9]{32}$//'}, {OUT=>"\n\n\n\n"}],
817 ['sha1-1', '-W sha1 2', {IN_PIPE=>$in_g1},
818 {OUT_SUBST=>'s/^[a-f0-9]{40}$//'}, {OUT=>"\n\n\n\n"}],
819 ['sha256-1', '-W sha256 2', {IN_PIPE=>$in_g1},
820 {OUT_SUBST=>'s/^[a-f0-9]{64}$//'}, {OUT=>"\n\n\n\n"}],
821 ['sha512-1', '-W sha512 2', {IN_PIPE=>$in_g1},
822 {OUT_SUBST=>'s/^[a-f0-9]{128}$//'}, {OUT=>"\n\n\n\n"}],
823
824 ## Test Base64
825 ['base64-1','-W base64 2', {IN_PIPE=>$in_g1}, {OUT=>$out_g1_base64}],
826 ['debase64-1','-W debase64 1', {IN_PIPE=>$out_g1_base64},
827 {OUT=>$out_g1_debase64}],
828 # Test invalid base64 input
829 ['debase64-2', '-W debase64 1', {IN_PIPE=>$in_g1}, {EXIT=>1},
830 {ERR=>"$prog: invalid base64 value in line 1 field 1: 'A'\n"}],
831
832 ## Mixing grouping,line,transpose/reverse operators should fail
833 ['mixop1', 'sum 1 md5 2', {EXIT=>1},
834 {ERR=>"$prog: conflicting operation found: expecting groupby operations," .
835 " but found line operation 'md5'\n"}],
836 ['mixop2', 'md5 1 sum 2', {EXIT=>1},
837 {ERR=>"$prog: conflicting operation found: expecting line operations," .
838 " but found groupby operation 'sum'\n"}],
839 ['mixop3', 'md5 1 transpose 2', {EXIT=>1},
840 {ERR=>"$prog: conflicting operation 'transpose'\n"}],
841
842 # Test large (and increasing) number of fields
843 ['wide1', '-t" " -g 1 countunique 2', {IN_PIPE=>$in_wide1}, {OUT=>"A 1\n"}],
844
845 # Test rmdup operation
846 ['rmdp1', '-t: rmdup 1', {IN_PIPE=>""}, {OUT=>""}],
847 ['rmdp2', '-t: rmdup 1', {IN_PIPE=>$in_dup1},
848 {OUT=>"X:Y\n1:a\n2:b\n3:a\n"}],
849 ['rmdp3', '-t: rmdup 2', {IN_PIPE=>$in_dup1},
850 {OUT=>"X:Y\n1:a\n2:b\n2:c\n"}],
851 # in+out header: same as no header at all (assuming the header is unique)
852 ['rmdp4', '-t: -H rmdup 1', {IN_PIPE=>$in_dup1},
853 {OUT=>"X:Y\n1:a\n2:b\n3:a\n"}],
854 ['rmdp5', '-t: --header-in rmdup 1', {IN_PIPE=>$in_dup1},
855 {OUT=>"1:a\n2:b\n3:a\n"}],
856 ['rmdp6', '-t: rmdup 4', {IN_PIPE=>$in_dup1}, {EXIT=>1},
857 {ERR=>"$prog: invalid input: field 4 requested, " .
858 "line 1 has only 2 fields\n"}],
859 ## rmdup with named columns
860 ['rmdp7', '-t: -H rmdup X', {IN_PIPE=>$in_dup1},
861 {OUT=>"X:Y\n1:a\n2:b\n3:a\n"}],
862 ['rmdp8', '-t: rmdup X', {IN_PIPE=>$in_dup1}, {EXIT=>1},
863 {ERR=>"$prog: -H or --header-in must be used with named columns\n"}],
864 # rmdup with different output delimiter
865 ['rmdp9', '-t: --output-delimiter % rmdup 1', {IN_PIPE=>$in_dup1},
866 {OUT=>"X%Y\n1%a\n2%b\n3%a\n"}],
867
868 # Test noop operation
869 ['noop1', 'noop', {IN_PIPE=>""}, {OUT=>""}],
870 ['noop2', 'noop', {IN_PIPE=>$in_dup1}, {OUT=>""}],
871 ['noop3', '--full noop', {IN_PIPE=>$in_dup1}, {OUT=>$in_dup1}],
872
873
874 # Test named columns - for operations
875 ## with empty input, invalid column name is not an error
876 ['nc2', '-H sum foo', {IN_PIPE=>""}, {OUT=>""}],
877 ['nc3', '--header-in sum foo', {IN_PIPE=>""}, {OUT=>""}],
878 ## with named columns, -H or --header-in are required.
879 ['nc1', 'sum foo', {IN_PIPE=>""}, {EXIT=>1},
880 {ERR=>"$prog: -H or --header-in must be used with named columns\n"}],
881 ['nc4', 'sum foo', {IN_PIPE=>$in_hdr1}, {EXIT=>1},
882 {ERR=>"$prog: -H or --header-in must be used with named columns\n"}],
883 ## Invalid column name
884 ['nc5', '--header-in sum foo', {IN_PIPE=>$in_hdr1}, {EXIT=>1},
885 {ERR=>"$prog: column name 'foo' not found in input file\n"}],
886 ['nc6', '-H sum foo', {IN_PIPE=>$in_hdr1}, {EXIT=>1},
887 {ERR=>"$prog: column name 'foo' not found in input file\n"}],
888 ## Valid column names, and identical column number
889 ['nc7', '-t" " -H sum y sum 2', {IN_PIPE=>$in_hdr1},
890 {OUT=>"sum(y) sum(y)\n52 52\n"}],
891 ## Valid column names + sorting
892 ['nc8', '-s -g 1 -t" " -H sum y', {IN_PIPE=>$in_hdr1},
893 {OUT=>"GroupBy(x) sum(y)\nA 14\nB 18\nC 20\n"}],
894
895 # Test named columns - for grouping
896 ## with empty input, invalid column name is not an error
897 ['ng1', '-H -g foo sum 1', {IN_PIPE=>""}, {OUT=>""}],
898 ['ng2', '--header-in -g foo sum foo', {IN_PIPE=>""}, {OUT=>""}],
899 ## with named columns, -H or --header-in are required
900 ['ng3', '-g foo count 1', {IN_PIPE=>""}, {EXIT=>1},
901 {ERR=>"$prog: -H or --header-in must be used with named columns\n"}],
902 ['ng4', '-g foo count 1', {IN_PIPE=>$in_hdr1}, {EXIT=>1},
903 {ERR=>"$prog: -H or --header-in must be used with named columns\n"}],
904 ## Invalid column name
905 ['ng5', '--header-in -g foo sum 1', {IN_PIPE=>$in_hdr1}, {EXIT=>1},
906 {ERR=>"$prog: column name 'foo' not found in input file\n"}],
907 ['ng6', '-H -g foo sum 1', {IN_PIPE=>$in_hdr1}, {EXIT=>1},
908 {ERR=>"$prog: column name 'foo' not found in input file\n"}],
909 ## Valid column names in grouping
910 ['ng7', '-t" " -H -g x sum 2', {IN_PIPE=>$in_hdr1},
911 {OUT=>"GroupBy(x) sum(y)\nA 14\nB 18\nC 20\n"}],
912 ## group by name+number
913 ['ng8', '-t":" -H -g x,3 sum 2', {IN_PIPE=>$in_hdr2},
914 {OUT=>"GroupBy(x):GroupBy(z):sum(y)\n" .
915 "A:W:15\nA:X:24\nB:Y:17\nB:Z:19\nC:Z:23\n"}],
916 ## group by name+name
917 ['ng9', '-t":" -H -g x,z sum 2', {IN_PIPE=>$in_hdr2},
918 {OUT=>"GroupBy(x):GroupBy(z):sum(y)\n" .
919 "A:W:15\nA:X:24\nB:Y:17\nB:Z:19\nC:Z:23\n"}],
920 ## group by number+name
921 ['ng10', '-t":" -H -g 1,z sum 2', {IN_PIPE=>$in_hdr2},
922 {OUT=>"GroupBy(x):GroupBy(z):sum(y)\n" .
923 "A:W:15\nA:X:24\nB:Y:17\nB:Z:19\nC:Z:23\n"}],
924 ## group by name + sorting
925 ['ng11', '-s -t" " -H -g x sum 2', {IN_PIPE=>$in_hdr1},
926 {OUT=>"GroupBy(x) sum(y)\nA 14\nB 18\nC 20\n"}],
927 );
928
929 if ($have_stable_sort) {
930 push @Tests, (
931 # last with sort, test the 'stable' sort
932 ['lst7', '-t" " --sort -g 1 last 3', {IN_PIPE=>$in_case_unsorted},
933 {OUT=>"A 5\nB 6\na 3\nb 4\n"}],
934 ['lst8', '-t" " --sort -i -g 1 last 3', {IN_PIPE=>$in_case_unsorted},
935 {OUT=>"A 5\nB 6\n"}],
936 # First with sort, test the 'stable' sort
937 ['fst7', '-t" " --sort -g 1 first 3', {IN_PIPE=>$in_case_unsorted},
938 {OUT=>"A 2\nB 6\na 1\nb 4\n"}],
939 ['fst8', '-t" " --sort -i -g 1 first 3', {IN_PIPE=>$in_case_unsorted},
940 {OUT=>"a 1\nb 4\n"}],
941 # NOTE: 'sort' is used with '-s' (stable sort),
942 # so with case-insensitive sort, the first appearing letter is
943 # reported (the lowercase a/b in case7 & case8).
944 ['case7', '-t" " -s -i -g 1 sum 3', {IN_PIPE=>$in_case_unsorted},
945 {OUT=>"a 11\nb 10\n"}],
946 ['case8', '-t" " -s -i -g 1 unique 2', {IN_PIPE=>$in_case_unsorted},
947 {OUT=>"a X\nb Y\n"}],
948 );
949 }
950
951 my $save_temps = $ENV{SAVE_TEMPS};
952 my $verbose = $ENV{VERBOSE};
953
954 my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
955 exit $fail;