Debian Codegraph @

tests/datamash-tests-2.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 ##
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 # TODO: add localization tests with "grouping"
61 # Turn off localization of executable's output.
62 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
63
64
65 # Test the selection operation (first/last/min/max) together with "FULL":
66 # group by column 1 ("A" or "B"), and operate on column 2 (numeric).
67 # Ensure the matching "iX" is displayed, despite not being part of the
68 # operation (example: if 'min(2)' is the operation, then "B 8" should be
69 # selected, and "i7" must be displayed with "-full" (because "i7" is on the
70 # same line as the min(2) value zero).
71 my $in_full1=<<'EOF';
72 A 4 i1
73 A 3 i2
74 A 5 i3
75 B 1 i4
76 B 8 i5
77 B 0 i6
78 B 3 i7
79 EOF
80
81 # Test various combinations of N/A, NaN values
82 my $na1=<<'EOF';
83 1
84 2
85 NA
86 3
87 EOF
88
89 my $na2=<<'EOF';
90 1
91 2
92 nA
93 3
94 EOF
95
96 my $na3=<<'EOF';
97 1
98 2
99 N/A
100 3
101 EOF
102
103 my $na4=<<'EOF';
104 1
105 2
106 NaN
107 3
108 EOF
109
110 my $na5=<<'EOF';
111 1
112 2
113 nan
114 3
115 EOF
116
117 # This is not a valid input, should not be detected as "NA"
118 my $not_na1=<<'EOF';
119 1
120 2
121 NAK
122 3
123 EOF
124
125 # This is not a valid input, should not be detected as "NAN"
126 my $not_na2=<<'EOF';
127 1
128 2
129 NANA
130 3
131 EOF
132
133 my $na_first=<<'EOF';
134 NaN
135 2
136 5
137 8
138 EOF
139
140 my $na_last=<<'EOF';
141 NaN
142 2
143 5
144 11
145 EOF
146
147 my $na_mid1=<<'EOF';
148 1:2:3
149 4:NA:6
150 7:8:9
151 EOF
152
153 my $na_last1=<<'EOF';
154 1:2:3
155 4:5:6
156 7:8:NaN
157 EOF
158
159 my $na_all=<<'EOF';
160 NA
161 NA
162 NaN
163 EOF
164
165 my $bin_in1=<<'EOF';
166 0
167 3
168 7
169 9
170 10
171 11
172 15
173 EOF
174
175 # binning '$bin_in1' with default bucket-size=100
176 my $bin_out1_100=<<'EOF';
177 0
178 0
179 0
180 0
181 0
182 0
183 0
184 EOF
185
186 # binning '$bin_in1' with bucket-size=5
187 my $bin_out1_5=<<'EOF';
188 0
189 0
190 5
191 5
192 10
193 10
194 15
195 EOF
196
197 # binning '$bin_in1' with bucket-size=5.5
198 my $bin_out1_5_5=<<'EOF';
199 0
200 0
201 5.5
202 5.5
203 5.5
204 11
205 11
206 EOF
207
208 my $bin_in2=<<'EOF';
209 -101.9
210 -3.1
211 -3
212 -0.3
213 -0
214 0
215 0.3
216 3
217 103
218 EOF
219
220 my $bin_out2_100=<<'EOF';
221 -200
222 -100
223 -100
224 -100
225 0
226 0
227 0
228 0
229 100
230 EOF
231
232 my $bin_out2_3=<<'EOF';
233 -102
234 -6
235 -3
236 -3
237 0
238 0
239 0
240 3
241 102
242 EOF
243
244 my $round_in1=<<'EOF';
245 X
246 -1.1
247 -1.0
248 -0.9
249 -0.8
250 -0.7
251 -0.6
252 -0.5
253 -0.4
254 -0.3
255 -0.2
256 -0.1
257 0.0
258 0.1
259 0.2
260 0.3
261 0.4
262 0.5
263 0.6
264 0.7
265 0.8
266 0.9
267 1.0
268 1.1
269 EOF
270
271 my $round_out1=<<'EOF';
272 X round(X) floor(X) ceil(X) trunc(X) frac(X)
273 -1.1 -1 -2 -1 -1 -0.1
274 -1.0 -1 -1 -1 -1 0
275 -0.9 -1 -1 0 0 -0.9
276 -0.8 -1 -1 0 0 -0.8
277 -0.7 -1 -1 0 0 -0.7
278 -0.6 -1 -1 0 0 -0.6
279 -0.5 -1 -1 0 0 -0.5
280 -0.4 0 -1 0 0 -0.4
281 -0.3 0 -1 0 0 -0.3
282 -0.2 0 -1 0 0 -0.2
283 -0.1 0 -1 0 0 -0.1
284 0.0 0 0 0 0 0
285 0.1 0 0 1 0 0.1
286 0.2 0 0 1 0 0.2
287 0.3 0 0 1 0 0.3
288 0.4 0 0 1 0 0.4
289 0.5 1 0 1 0 0.5
290 0.6 1 0 1 0 0.6
291 0.7 1 0 1 0 0.7
292 0.8 1 0 1 0 0.8
293 0.9 1 0 1 0 0.9
294 1.0 1 1 1 1 0
295 1.1 1 1 2 1 0.1
296 EOF
297
298
299 my $in_comments=<<'EOF';
300 #foo 3
301 bar 5
302 ;baz 7
303 EOF
304
305 my $in_comments2=<<'EOF';
306 #foo 3
307 bar 5
308 ;baz 7
309 frob 9;
310 EOF
311
312 my $in_comments3=<<'EOF';
313 #foo 3
314 bar 5
315 ;baz 7
316 frob 9#
317 EOF
318
319 my $in_esc_ident=<<'EOF';
320 A_Chlor_T1h_r1-metaG B 9C -bar
321 1 2 3 4
322 10 20 30 40
323 EOF
324
325 #
326 my $in_dirname_basename=<<'EOF';
327 /foo/bar
328 /foo/
329 foo
330 /
331 .
332 ..
333
334 EOF
335
336 my $exp_dirname_basename=<<'EOF';
337 /foo bar
338 / foo
339 . foo
340 / /
341 . .
342 . ..
343 .
344 EOF
345
346 my $in_barename_extname=<<'EOF';
347 hello-world.txt
348 hello.world.txt
349 .hello
350 .txt
351 foo%bar
352 foo
353 foo.tar.gz
354 foo.txt.std
355 foo.org.lz
356 foo.a3
357 foo.gpg
358 foo.txt.xz.gpg
359 foo.log.bz2
360 foo.bar
361 foo.bloo
362 foo.bar.txt.log
363
364 EOF
365
366 my $exp_barename_extname=<<'EOF';
367 hello-world txt
368 hello.world txt
369 .hello
370 .txt
371 foo%bar
372 foo
373 foo tar.gz
374 foo txt.std
375 foo org.lz
376 foo a3
377 foo gpg
378 foo txt.xz.gpg
379 foo log.bz2
380 foo bar
381 foo bloo
382 foo.bar.txt log
383
384 EOF
385
386 my $in1_cut=<<'EOF';
387 a,b,c
388 1,X,6
389 EOF
390
391 my @Tests =
392 (
393 # Test 'min' + --full
394 # first, verify test without "--full"
395 ['slct1', '-t" " -g1 min 2', {IN_PIPE=>$in_full1}, {OUT=>"A 3\nB 0\n"}],
396
397 # Test 'max' + --full
398 # first, verify test without "--full"
399 ['slct4', '-t" " -g1 max 2', {IN_PIPE=>$in_full1}, {OUT=>"A 5\nB 8\n"}],
400
401 # Test 'first' + --full
402 # first, verify test without "--full"
403 ['slct7', '-t" " -g1 first 2', {IN_PIPE=>$in_full1}, {OUT=>"A 4\nB 1\n"}],
404 # more --full with --sort => see test 'sortslct1' below
405
406 # Test 'last' + --full
407 # first, verify test without "--full"
408 ['slct9', '-t" " -g1 last 2', {IN_PIPE=>$in_full1}, {OUT=>"A 5\nB 3\n"}],
409 # more --full with --sort => see test 'sortslct2' below
410
411
412 # Test --narm - ignoring NaN/NA values
413
414 ## Test with 'NA'
415 ['narm1', '--narm sum 1', {IN_PIPE=>$na1}, {OUT=>"6\n"}],
416 ['narm2', '--narm mean 1', {IN_PIPE=>$na1}, {OUT=>"2\n"}],
417 # without --narm, these should fail with invalid input
418 ['narm3', 'sum 1', {IN_PIPE=>$na1}, {EXIT=>1},
419 {ERR=>"$prog: invalid numeric value in line 3 field 1: 'NA'\n"}],
420
421 ## Test with 'nA'
422 ['narm4', '--narm sum 1', {IN_PIPE=>$na2}, {OUT=>"6\n"}],
423 ['narm5', '--narm mean 1', {IN_PIPE=>$na2}, {OUT=>"2\n"}],
424 # without --narm, these should fail with invalid input
425 ['narm6', 'sum 1', {IN_PIPE=>$na2}, {EXIT=>1},
426 {ERR=>"$prog: invalid numeric value in line 3 field 1: 'nA'\n"}],
427
428 ## Test with 'N/A'
429 ['narm7', '--narm sum 1', {IN_PIPE=>$na3}, {OUT=>"6\n"}],
430 ['narm8', '--narm mean 1', {IN_PIPE=>$na3}, {OUT=>"2\n"}],
431 # without --narm, these should fail with invalid input
432 ['narm9', 'sum 1', {IN_PIPE=>$na3}, {EXIT=>1},
433 {ERR=>"$prog: invalid numeric value in line 3 field 1: 'N/A'\n"}],
434
435 ## Test with 'NaN'
436 ['narm10', '--narm sum 1', {IN_PIPE=>$na4}, {OUT=>"6\n"}],
437 ['narm11', '--narm mean 1', {IN_PIPE=>$na4}, {OUT=>"2\n"}],
438 # without --narm, 'nan' should be processed, not skipped
439 ['narm12', 'sum 1', {IN_PIPE=>$na4}, {OUT=>"$nan\n"},
440 {OUT_SUBST=>'s/^-//'}],
441
442 ## Test with 'nan'
443 ['narm13', '--narm sum 1', {IN_PIPE=>$na5}, {OUT=>"6\n"}],
444 ['narm14', '--narm mean 1', {IN_PIPE=>$na5}, {OUT=>"2\n"}],
445 # without --narm, 'nan' should be processed, not skipped
446 ['narm15', 'sum 1', {IN_PIPE=>$na5}, {OUT=>"$nan\n"},
447 {OUT_SUBST=>'s/^-//'}],
448
449 # These input have strings starting with 'NA' or 'NAN' but should not
450 # be mistaken for them.
451 ['narm16', '--narm sum 1', {IN_PIPE=>$not_na1}, {EXIT=>1},
452 {ERR=>"$prog: invalid numeric value in line 3 field 1: 'NAK'\n"}],
453 ['narm17', '--narm sum 1', {IN_PIPE=>$not_na2}, {EXIT=>1},
454 {ERR=>"$prog: invalid numeric value in line 3 field 1: 'NANA'\n"}],
455
456 ## NA value as first/last line
457 ['narm18', '--narm mean 1', {IN_PIPE=>$na_first}, {OUT=>"5\n"}],
458 ['narm19', '--narm mean 1', {IN_PIPE=>$na_last}, {OUT=>"6\n"}],
459
460 ## NA in the middle of the line
461 ['narm20', '-t: --narm sum 1 sum 2 sum 3', {IN_PIPE=>$na_mid1},
462 {OUT=>"12:10:18\n"}],
463 ['narm21', '-t: sum 1 sum 2 sum 3', {IN_PIPE=>$na_mid1}, {EXIT=>1},
464 {ERR=>"$prog: invalid numeric value in line 2 field 2: 'NA'\n"}],
465
466 ## NA as the last field
467 ['narm22', '-t: --narm sum 1 sum 2 sum 3', {IN_PIPE=>$na_mid1},
468 {OUT=>"12:10:18\n"}],
469 ['narm23', '-t: sum 1 sum 2 sum 3', {IN_PIPE=>$na_mid1}, {EXIT=>1},
470 {ERR=>"$prog: invalid numeric value in line 2 field 2: 'NA'\n"}],
471
472 # N/A affect counts
473 ['narm24', '-t: count 1 count 2 count 3', {IN_PIPE=>$na_last1},
474 {OUT=>"3:3:3\n"}],
475 ['narm25', '-t: --narm count 1 count 2 count 3', {IN_PIPE=>$na_last1},
476 {OUT=>"3:3:2\n"}],
477 ['narm26', '-t: --narm sum 1 sum 2 sum 3', {IN_PIPE=>$na_last1},
478 {OUT=>"12:15:9\n"}],
479
480 # Check NA with string operations
481 ['narm27', '-t: --narm unique 2', {IN_PIPE=>$na_mid1},
482 {OUT=>"2,8\n"}],
483 ['narm28', '-t: unique 2', {IN_PIPE=>$na_mid1},
484 {OUT=>"2,8,NA\n"}],
485
486 # Data of all NAs
487 ['narm29', '--narm count 1', {IN_PIPE=>$na_all}, {OUT=>"0\n"}],
488 ['narm30', ' count 1', {IN_PIPE=>$na_all}, {OUT=>"3\n"}],
489
490 ['narm31', '--narm sum 1', {IN_PIPE=>$na_all}, {OUT=>"0\n"}],
491
492 ['narm32', '--narm mean 1', {IN_PIPE=>$na_all}, {OUT=>"$nan\n"}],
493
494 # Test all NA input with all operations
495 # tested to comply with R code of 'a = C(NA,NA,NA)'
496 # Try to be as consistent with R as possible.
497 # The tested function is mainly 'field_op_summarize_empty()'
498 ['narm40', '--narm count 1', {IN_PIPE=>$na_all}, {OUT=>"0\n"}],
499 ['narm41', '--narm sum 1', {IN_PIPE=>$na_all}, {OUT=>"0\n"}],
500 ['narm42', '--narm min 1', {IN_PIPE=>$na_all}, {OUT=>"-$inf\n"}],
501 ['narm43', '--narm max 1', {IN_PIPE=>$na_all}, {OUT=>"$inf\n"}],
502 ['narm44', '--narm absmin 1', {IN_PIPE=>$na_all}, {OUT=>"-$inf\n"}],
503 ['narm45', '--narm absmax 1', {IN_PIPE=>$na_all}, {OUT=>"$inf\n"}],
504 ['narm46', '--narm first 1', {IN_PIPE=>$na_all}, {OUT=>"N/A\n"}],
505 ['narm47', '--narm last 1', {IN_PIPE=>$na_all}, {OUT=>"N/A\n"}],
506 ['narm48', '--narm rand 1', {IN_PIPE=>$na_all}, {OUT=>"N/A\n"}],
507 ['narm49', '--narm mean 1', {IN_PIPE=>$na_all}, {OUT=>"$nan\n"}],
508 ['narm51', '--narm median 1', {IN_PIPE=>$na_all}, {OUT=>"$nan\n"}],
509 ['narm52', '--narm q1 1', {IN_PIPE=>$na_all}, {OUT=>"$nan\n"}],
510 ['narm53', '--narm q3 1', {IN_PIPE=>$na_all}, {OUT=>"$nan\n"}],
511 ['narm54', '--narm iqr 1', {IN_PIPE=>$na_all}, {OUT=>"$nan\n"}],
512 ['narm55', '--narm pstdev 1', {IN_PIPE=>$na_all}, {OUT=>"$nan\n"}],
513 ['narm56', '--narm sstdev 1', {IN_PIPE=>$na_all}, {OUT=>"$nan\n"}],
514 ['narm57', '--narm pvar 1', {IN_PIPE=>$na_all}, {OUT=>"$nan\n"}],
515 ['narm58', '--narm svar 1', {IN_PIPE=>$na_all}, {OUT=>"$nan\n"}],
516 ['narm59', '--narm mad 1', {IN_PIPE=>$na_all}, {OUT=>"$nan\n"}],
517 ['narm60', '--narm madraw 1', {IN_PIPE=>$na_all}, {OUT=>"$nan\n"}],
518 ['narm61', '--narm sskew 1', {IN_PIPE=>$na_all}, {OUT=>"$nan\n"}],
519 ['narm62', '--narm pskew 1', {IN_PIPE=>$na_all}, {OUT=>"$nan\n"}],
520 ['narm63', '--narm skurt 1', {IN_PIPE=>$na_all}, {OUT=>"$nan\n"}],
521 ['narm64', '--narm pkurt 1', {IN_PIPE=>$na_all}, {OUT=>"$nan\n"}],
522 ['narm65', '--narm jarque 1', {IN_PIPE=>$na_all}, {OUT=>"$nan\n"}],
523 ['narm66', '--narm dpo 1', {IN_PIPE=>$na_all}, {OUT=>"$nan\n"}],
524 ['narm67', '--narm mode 1', {IN_PIPE=>$na_all}, {OUT=>"$nan\n"}],
525 ['narm68', '--narm antimode 1',{IN_PIPE=>$na_all}, {OUT=>"$nan\n"}],
526 ['narm69', '--narm unique 1', {IN_PIPE=>$na_all}, {OUT=>"\n"}],
527 ['narm70', '--narm collapse 1',{IN_PIPE=>$na_all}, {OUT=>"\n"}],
528 ['narm71', '--narm countunique 1', {IN_PIPE=>$na_all}, {OUT=>"0\n"}],
529
530 ['narm72', '--narm base64 1', {IN_PIPE=>$na_all}, {OUT=>"\n\n\n"}],
531 ['narm73', '--narm md5 1', {IN_PIPE=>$na_all}, {OUT=>"\n\n\n"}],
532 ['narm74', '--narm sha1 1', {IN_PIPE=>$na_all}, {OUT=>"\n\n\n"}],
533 ['narm75', '--narm sha256 1', {IN_PIPE=>$na_all}, {OUT=>"\n\n\n"}],
534 ['narm76', '--narm sha512 1', {IN_PIPE=>$na_all}, {OUT=>"\n\n\n"}],
535 ['narm77', '--narm rmdup 1', {IN_PIPE=>$na_all}, {OUT=>"NA\nNaN\n"}],
536 ['narm78', '--narm reverse', {IN_PIPE=>$na_all}, {OUT=>"NA\nNA\nNaN\n"}],
537 ['narm79', '--narm transpose', {IN_PIPE=>$na_all}, {OUT=>"NA\tNA\tNaN\n"}],
538
539
540 # Test binning
541 ['bin1', 'bin 1', {IN_PIPE=>$bin_in1}, {OUT=>$bin_out1_100}],
542 ['bin2', 'bin:5 1', {IN_PIPE=>$bin_in1}, {OUT=>$bin_out1_5}],
543 ['bin3', 'bin:5.5 1', {IN_PIPE=>$bin_in1}, {OUT=>$bin_out1_5_5}],
544 ['bin4', 'bin 1', {IN_PIPE=>$bin_in2}, {OUT=>$bin_out2_100}],
545 ['bin5', 'bin:3 1', {IN_PIPE=>$bin_in2}, {OUT=>$bin_out2_3}],
546 ['bin6', 'bin:3. 1', {IN_PIPE=>$bin_in2}, {OUT=>$bin_out2_3}],
547 ['bin7', 'bin:3.0 1', {IN_PIPE=>$bin_in2}, {OUT=>$bin_out2_3}],
548 ['bin8', 'bin:3.e0 1', {IN_PIPE=>$bin_in2}, {OUT=>$bin_out2_3}],
549 ['bin9', 'bin:0.3e1 1', {IN_PIPE=>$bin_in2}, {OUT=>$bin_out2_3}],
550 ['bin10','bin:30.e-1 1', {IN_PIPE=>$bin_in2}, {OUT=>$bin_out2_3}],
551
552 # Test rounding functions
553 ['rnd1', '-H --full round 1 floor 1 ceil 1 trunc 1 frac 1',
554 {IN_PIPE=>$round_in1}, {OUT=>$round_out1}],
555
556 # Test range
557 ['rng1', '-t" " -g 1 range 2',{IN_PIPE=>$in_full1}, {OUT=>"A 2\nB 8\n"}],
558 ['rng2', '-W range 2', {IN_PIPE=>$in_full1}, {OUT=>"8\n"}],
559 ['rng3', 'range 1', {IN_PIPE=>""}, {OUT=>""}],
560 # Skip invalid values
561 ['rng4', '--narm range 1', {IN_PIPE=>"19\nNA\n9\n"}, {OUT=>"10\n"}],
562 ['rng5', '--narm range 1', {IN_PIPE=>"NA\n"}, {OUT=>"$nan\n"}],
563
564
565 # Test output-delimiter
566 ['odlm1', '-t: last 1 last 2', {IN_PIPE=>$na_mid1}, {OUT=>"7:8\n"}],
567 ['odlm2', '-t: --output-delimiter "%" last 1 last 2',
568 {IN_PIPE=>$na_mid1}, {OUT=>"7%8\n"}],
569 ['odlm3', '--output-delimiter "%" -t: last 1 last 2',
570 {IN_PIPE=>$na_mid1}, {OUT=>"7%8\n"}],
571
572 # output-delimiter with whitespace
573 ['odlm4', '-t " " last 1 last 2', {IN_PIPE=>$in_full1}, {OUT=>"B 3\n"}],
574 ['odlm5', '-W last 1 last 2', {IN_PIPE=>$in_full1}, {OUT=>"B\t3\n"}],
575 ['odlm6', '-W --output-delimiter ":" last 1 last 2',
576 {IN_PIPE=>$in_full1}, {OUT=>"B:3\n"}],
577 ['odlm7', '--output-delimiter ":" -W last 1 last 2',
578 {IN_PIPE=>$in_full1}, {OUT=>"B:3\n"}],
579
580 # Multiple output delimiters
581 ['odlm8', '--output-delimiter "x" -W --output-delimiter "y" last 1 last 2',
582 {IN_PIPE=>$in_full1}, {OUT=>"By3\n"}],
583
584
585 # Test -C/--skip-comments option
586 ['sc1', 'sum 2', {IN_PIPE=>$in_comments}, {OUT=>"15\n"}],
587 ['sc2', '-C sum 2', {IN_PIPE=>$in_comments}, {OUT=>"5\n"}],
588 ['sc3', 'reverse', {IN_PIPE=>$in_comments},
589 {OUT=>"3 #foo\n" .
590 "5 bar\n" .
591 "7 ;baz\n"}],
592 ['sc4', '-C reverse', {IN_PIPE=>$in_comments}, {OUT=>"5 bar\n"}],
593 # datamash supports only comment lines, but not inline comments
594 ['sce1', '-C sum 2', {IN_PIPE=>$in_comments2}, {EXIT=>1},
595 {ERR=>"$prog: invalid numeric value in line 2 field 2: '9;'\n"}],
596 ['sce2', '-C sum 2', {IN_PIPE=>$in_comments3}, {EXIT=>1},
597 {ERR=>"$prog: invalid numeric value in line 2 field 2: '9#'\n"}],
598
599
600 # Regression tests
601 # TODO: Move regression tests into a separate file when sufficiently many
602 # have accumulated to warrant the overhead of another test file.
603 # Bug in mode/antimode in 1.4 and earlier
604 ['bug_mode1', 'mode 1', {IN_PIPE=>"-1"}, {OUT=>"-1\n"}],
605 ['bug_antimode1', 'antimode 1', {IN_PIPE=>"-1"}, {OUT=>"-1\n"}],
606 # "Segmentation fault" when input contains embedded NUL characters
607 # (see https://lists.gnu.org/archive/html/bug-datamash/2020-11/msg00001.html)
608 ['regr001', 'countunique 1', {IN_PIPE=>"\x00" x 100}, {OUT=>"1\n"}],
609 ['regr002', 'unique 1', {IN_PIPE=>"\x00" x 100}, {OUT=>"\n"}],
610
611 ##
612 ## Backslash escaping in identifiers
613 ##
614
615 ## minus in field name
616 ['esc1', '-W -H sum "A_Chlor_T1h_r1\\-metaG"', {IN_PIPE=>$in_esc_ident},
617 {OUT=>"sum(A_Chlor_T1h_r1-metaG)\n11\n"}],
618 # field name starting with an identifier
619 ['esc2', '-W -H sum "\\9C"', {IN_PIPE=>$in_esc_ident},
620 {OUT=>"sum(9C)\n33\n"}],
621 # field name starting with minus
622 ['esc3', '-W -H sum "\\-bar"', {IN_PIPE=>$in_esc_ident},
623 {OUT=>"sum(-bar)\n44\n"}],
624
625
626 # dirname and basename
627 ['dnbn1', 'dirname 1 basename 1', {IN_PIPE=>$in_dirname_basename},
628 {OUT=>$exp_dirname_basename}],
629
630 # barename and extname
631 ['bnen1', 'barename 1 extname 1', {IN_PIPE=>$in_barename_extname},
632 {OUT=>$exp_barename_extname}],
633
634
635 ## GetNum variants
636 ['gn1', 'getnum 1', {IN_PIPE=>"moo-123.45"}, {OUT=>"123.45\n"}],
637 # Integer
638 ['gn2', 'getnum:i 1', {IN_PIPE=>"moo-123.45"}, {OUT=>"-123\n"}],
639 # Hex 0x123 = 291
640 ['gn3', 'getnum:h 1', {IN_PIPE=>"moo-123.45"}, {OUT=>"291\n"}],
641 # Octal 0123 = 83
642 ['gn4', 'getnum:o 1', {IN_PIPE=>"moo-123.45"}, {OUT=>"83\n"}],
643 # Natural
644 ['gn5', 'getnum:n 1', {IN_PIPE=>"moo-123.45"}, {OUT=>"123\n"}],
645 # Decimal
646 ['gn6', 'getnum:d 1', {IN_PIPE=>"moo-123.45"}, {OUT=>"-123.45\n"}],
647 # GetNum variants
648 ['gn6-1', 'getnum 1', {IN_PIPE=>"moo-103.89"}, {OUT=>"103.89\n"}],
649 # Integer
650 ['gn6-2', 'getnum:i 1', {IN_PIPE=>"moo-789.45"}, {OUT=>"-789\n"}],
651
652 ['gn10', 'getnum 1', {IN_PIPE=>"moo"}, {OUT=>"0\n"}],
653 ['gn11', 'getnum:d 1', {IN_PIPE=>"moo"}, {OUT=>"0\n"}],
654 ['gn12', 'getnum:i 1', {IN_PIPE=>"moo---4"}, {OUT=>"0\n"}],
655 ['gn13', 'getnum:d 1', {IN_PIPE=>"moo...4"}, {OUT=>"0\n"}],
656 ['gn14', 'getnum:i 1',
657 {IN_PIPE=>"moo99999999999999999999999999999" .
658 "1234312341432123451541341312431"},
659 {OUT=>"0\n"}],
660 ['gn15', 'getnum:d 1',
661 {IN_PIPE=>"moo" . "9" x 10000},
662 {OUT=>"0\n"}],
663 ['gn16', 'getnum:n 1', {IN_PIPE=>"moo" x 4000 . "42" . "bar" x 1000},
664 {OUT=>"42\n"}],
665 ['gn17', 'getnum 1', {IN_PIPE=>"abc\t123\n"}, {OUT=>"0\n"}],
666 ['gn18', 'getnum 2', {IN_PIPE=>"abc\t123\n"}, {OUT=>"123\n"}],
667 ['gn19', '-t, getnum 1', {IN_PIPE=>"abc,123\n"}, {OUT=>"0\n"}],
668 ['gn20', '-t, getnum 2', {IN_PIPE=>"abc,123\n"}, {OUT=>"123\n"}],
669 ['gn21', '-t: getnum 1', {IN_PIPE=>"abc:123\n"}, {OUT=>"0\n"}],
670 ['gn22', '-t: getnum 2', {IN_PIPE=>"abc:123\n"}, {OUT=>"123\n"}],
671
672
673 # cut operation
674 ['cut1','-t, cut 3,1,2', {IN_PIPE=>$in1_cut}, {OUT=>"c,a,b\n6,1,X\n"}],
675 ['cut2','-t, -H cut c,a', {IN_PIPE=>$in1_cut}, {OUT=>"cut(c),cut(a)\n6,1\n"}],
676
677 # Aliases (more-or-less: copy-paste above tests and s/orig/alias/ the
678 # command names.)
679 ['alias1','-t, echo 3,1,2', {IN_PIPE=>$in1_cut}, {OUT=>"c,a,b\n6,1,X\n"}],
680 ['alias2','-t, -H echo c,a', {IN_PIPE=>$in1_cut},
681 {OUT=>"cut(c),cut(a)\n6,1\n"}],
682 ['alias3', '-t: --narm uniq 2', {IN_PIPE=>$na_mid1},
683 {OUT=>"2,8\n"}],
684 ['alias4', '-t: uniq 2', {IN_PIPE=>$na_mid1},
685 {OUT=>"2,8,NA\n"}],
686 ['alias5', '--narm uniq 1', {IN_PIPE=>$na_all}, {OUT=>"\n"}],
687
688 # Decimal separator used as input field delimiter
689 ['ifdl01', '-t. cut 2,1', {IN_PIPE=>"1.2\n"}, {OUT=>"2.1\n"}],
690 ['ifdl02', '-t. cut 3,2,1', {IN_PIPE=>"1.2.3\n"}, {OUT=>"3.2.1\n"}],
691 ['ifdl03', '-t. unique 1,2', {IN_PIPE=>"1.2\n"}, {OUT=>"1.2\n"}],
692 ['ifdl04', '-t. unique 1,2,3', {IN_PIPE=>"1.2.3\n"}, {OUT=>"1.2.3\n"}],
693 ['ifdl05', '-t. count 1,2', {IN_PIPE=>"1.2\n"}, {OUT=>"1.1\n"}],
694 ['ifdl06', '-t. count 1,2,3', {IN_PIPE=>"1.2.3\n"}, {OUT=>"1.1.1\n"}],
695 ['ifdl07', '-t. countunique 1,2', {IN_PIPE=>"1.2\n"}, {OUT=>"1.1\n"}],
696 ['ifdl08', '-t. countunique 1,2,3', {IN_PIPE=>"1.2.3\n"}, {OUT=>"1.1.1\n"}],
697 ['ifdl09', '-t. rmdup 1', {IN_PIPE=>"1.2\n"}, {OUT=>"1.2\n"}],
698 ['ifdl10', '-t. rmdup 2', {IN_PIPE=>"1.2\n"}, {OUT=>"1.2\n"}],
699 ['ifdl11', '-t. rmdup 3', {IN_PIPE=>"1.2.3\n"}, {OUT=>"1.2.3\n"}],
700 ['ifdl12', '-t. sum 1,2', {IN_PIPE=>"1.2\n"}, {OUT=>"1.2\n"}],
701 ['ifdl13', '-t. sum 1,2,3', {IN_PIPE=>"1.2.3\n"}, {OUT=>"1.2.3\n"}],
702 ['ifdl14', '-t. getnum 1,2', {IN_PIPE=>"x1.2y\n"}, {OUT=>"1.2\n"}],
703 ['ifdl15', '-t. getnum 1', {IN_PIPE=>"x2.3y\n"}, {OUT=>"2\n"}],
704 ['ifdl16', '-t. getnum 2', {IN_PIPE=>"x2.3y\n"}, {OUT=>"3\n"}],
705
706 # Test mode operation
707 ['mode01', 'mode 1', {IN_PIPE=>"1\n1\n2\n"}, {OUT=>"1\n"}],
708 ['mode02', 'mode 1', {IN_PIPE=>"1\n2\n2\n"}, {OUT=>"2\n"}],
709 ['mode03', 'mode 1', {IN_PIPE=>"1\n2\n2\n3\n3\n3\n"}, {OUT=>"3\n"}],
710 ['mode04', 'mode 1', {IN_PIPE=>"1\n1\n1\n2\n2\n3\n"}, {OUT=>"1\n"}],
711 ['mode05', 'mode 1', {IN_PIPE=>"1\n1\n2\n2\n2\n3\n"}, {OUT=>"2\n"}],
712 ['mode06', 'mode 1', {IN_PIPE=>"1\n2\n2\n3\n3\n3\n"}, {OUT=>"3\n"}],
713 ['mode07', 'mode 1', {IN_PIPE=>"1\n1\n1\n2\n3\n3\n"}, {OUT=>"1\n"}],
714 ['mode08', 'mode 1',{IN_PIPE=>"1\n2\n2\n3\n3\n3\n4\n4\n4\n4\n"},{OUT=>"4\n"}],
715 ['mode09', 'mode 1',{IN_PIPE=>"1\n2\n2\n3\n3\n3\n3\n4\n4\n4\n"},{OUT=>"3\n"}],
716 ['mode10', 'mode 1',{IN_PIPE=>"1\n2\n2\n2\n2\n3\n3\n4\n4\n4\n"},{OUT=>"2\n"}],
717 ['mode11', 'mode 1',{IN_PIPE=>"1\n1\n1\n1\n2\n2\n3\n3\n3\n4\n"},{OUT=>"1\n"}],
718 ['mode12', 'mode 1', {IN_PIPE=>"9\n"}, {OUT=>"9\n"}],
719 ['mode13', 'mode 1',
720 {IN_PIPE=>"1\n1\n1\n2\n2\n2\n2\n3\n3\n4\n4\n4\n4\n4\n"}, {OUT=>"4\n"}],
721
722 # Test antimode operation
723 ['antimode01', 'antimode 1', {IN_PIPE=>"1\n1\n2\n"}, {OUT=>"2\n"}],
724 ['antimode02', 'antimode 1', {IN_PIPE=>"1\n2\n2\n"}, {OUT=>"1\n"}],
725 ['antimode03', 'antimode 1', {IN_PIPE=>"1\n2\n2\n3\n3\n3\n"}, {OUT=>"1\n"}],
726 ['antimode04', 'antimode 1', {IN_PIPE=>"1\n1\n2\n3\n3\n3\n"}, {OUT=>"2\n"}],
727 ['antimode05', 'antimode 1', {IN_PIPE=>"1\n1\n1\n2\n2\n3\n"}, {OUT=>"3\n"}],
728 ['antimode06', 'antimode 1', {IN_PIPE=>"1\n1\n2\n2\n2\n3\n"}, {OUT=>"3\n"}],
729 ['antimode07', 'antimode 1', {IN_PIPE=>"1\n2\n2\n3\n3\n3\n"}, {OUT=>"1\n"}],
730 ['antimode08', 'antimode 1', {IN_PIPE=>"1\n1\n1\n2\n3\n3\n"}, {OUT=>"2\n"}],
731 ['antimode09', 'antimode 1',
732 {IN_PIPE=>"1\n2\n2\n3\n3\n3\n4\n4\n4\n4\n"}, {OUT=>"1\n"}],
733 ['antimode10', 'antimode 1',
734 {IN_PIPE=>"1\n2\n2\n3\n3\n3\n3\n4\n4\n4\n"}, {OUT=>"1\n"}],
735 ['antimode11', 'antimode 1',
736 {IN_PIPE=>"1\n2\n2\n2\n2\n3\n3\n4\n4\n4\n"}, {OUT=>"1\n"}],
737 ['antimode12', 'antimode 1',
738 {IN_PIPE=>"1\n1\n1\n1\n2\n2\n3\n3\n3\n4\n"}, {OUT=>"4\n"}],
739 ['antimode13', 'antimode 1',
740 {IN_PIPE=>"1\n1\n1\n1\n2\n3\n3\n3\n4\n4\n"}, {OUT=>"2\n"}],
741 ['antimode14', 'antimode 1',
742 {IN_PIPE=>"1\n1\n1\n1\n2\n2\n3\n4\n4\n4\n"}, {OUT=>"3\n"}],
743 ['antimode15', 'antimode 1',
744 {IN_PIPE=>"1\n1\n2\n2\n2\n3\n4\n4\n4\n4\n"}, {OUT=>"3\n"}],
745 ['antimode16', 'antimode 1',
746 {IN_PIPE=>"1\n1\n2\n2\n3\n3\n4\n"}, {OUT=>"4\n"}],
747 ['antimode17', 'antimode 1', {IN_PIPE=>"9\n"}, {OUT=>"9\n"}],
748 ['antimode18', 'antimode 1',
749 {IN_PIPE=>"1\n1\n1\n2\n2\n2\n2\n3\n3\n4\n4\n4\n4\n4\n"}, {OUT=>"3\n"}],
750
751 );
752
753 my $save_temps = $ENV{SAVE_TEMPS};
754 my $verbose = $ENV{VERBOSE};
755
756 my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
757 exit $fail;