Debian Codegraph @

tests/datamash-parser.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 # 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 my $in1=<<"EOF";
51 A\t100\tx
52 A\t10\tx
53 B\t10\tx
54 B\t35\ty
55 EOF
56
57 my $in2=<<"EOF";
58 1 2 3 4 5
59 6 7 8 9 10
60 EOF
61
62 my $out2=<<'EOF';
63 7 9 11 13 15
64 EOF
65
66
67 ## NOTE: these tests check the parser behaviour,
68 ## while ignoring the exact wording of the error messages.
69 ## The 'datamash-error-msgs.pl' checks the exact message wording.
70 ## (ERR_SUBST is used to discard the text of STDERR)
71 my @Tests =
72 (
73 # no explicit mode - 'sum' implies 'group by' without any columns -
74 # operate on entire file.
75 ['p1', 'sum 2', {IN_PIPE=>$in1}, {OUT=>"155\n"}],
76
77 # 'old' syntax - with '-g'
78 ['p2', '-g 1 sum 2', {IN_PIPE=>$in1}, {OUT=>"A\t110\nB\t45\n"}],
79
80 # 'new' syntax - without '-g'
81 ['p3', 'groupby 1 sum 2', {IN_PIPE=>$in1}, {OUT=>"A\t110\nB\t45\n"}],
82 ['p4', 'gb 1 sum 2', {IN_PIPE=>$in1}, {OUT=>"A\t110\nB\t45\n"}],
83
84 # group by multiple columns
85 ['p5', 'gb 1,3 sum 2', {IN_PIPE=>$in1},
86 {OUT=>"A\tx\t110\nB\tx\t10\nB\ty\t35\n"}],
87 # operate by multiple columns with comma
88 ['p6', 'gb 1 last 2,3', {IN_PIPE=>$in1},
89 {OUT=>"A\t10\tx\nB\t35\ty\n"}],
90
91 # many groupby columns, force the parser to allocate more array items
92 ['p7', 'gb 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23' .
93 ',24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,44,44' .
94 ',45,46,47,48,49,50 sum 2', {IN_PIPE=>""}, {OUT=>""}],
95 # many operator columns, force the parser to allocate more array items
96 ['p8', 'gb 1 sum 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22' .
97 ',23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,44,44' .
98 ',45,46,47,48,49,50', {IN_PIPE=>""}, {OUT=>""}],
99
100 # Invalid numeric value for column prasing should be treated as named column
101 ['p9', 'sum 1x', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
102
103 # Processing mode without operation
104 ['p10','groupby 1', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
105
106 # invalid operation after valid mode
107 ['p11','groupby 1 foobar 2', {IN_PIPE=>""}, {EXIT=>1},
108 {ERR_SUBST=>'s/.*//s'}],
109
110 # missing field number after processing mode
111 ['p12','groupby', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
112
113 # field range syntax
114 ['p20','sum 1-44', {IN_PIPE=>""}, {OUT=>""}],
115
116 # compare results of equivalent syntaxes
117 ['p21','sum 1,2,3,4,5', {IN_PIPE=>$in2}, {OUT=>$out2}],
118 ['p22','sum 1-2,3-4,5', {IN_PIPE=>$in2}, {OUT=>$out2}],
119 ['p23','sum 1-2,3-5', {IN_PIPE=>$in2}, {OUT=>$out2}],
120 ['p24','sum 1-4,5', {IN_PIPE=>$in2}, {OUT=>$out2}],
121 ['p25','sum 1-5', {IN_PIPE=>$in2}, {OUT=>$out2}],
122 ['p26','sum 1 sum 2 sum 3 sum 4 sum 5',
123 {IN_PIPE=>$in2}, {OUT=>$out2}],
124 ['p27','sum 1,2 sum 3-5',
125 {IN_PIPE=>$in2}, {OUT=>$out2}],
126
127 # 'check' options
128 ['p30','check', {IN_PIPE=>""}, {OUT=>"0 lines, 0 fields\n"}],
129 ['p31','check foo', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
130 ['p32','check 10', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
131 ['p33','check lines lines', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
132 ['p34','check 1 line fields', {IN_PIPE=>""}, {EXIT=>1},
133 {ERR_SUBST=>'s/.*//s'}],
134 ['p35','check 10 foo', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
135
136
137 # Field range with invalid syntax
138 ['e20','sum 1-', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
139 ['e21','sum 1-x', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
140 ['e22','sum 4-2', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
141 # zero in range
142 ['e23','sum 0-2', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
143 ['e24','sum 1-0', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
144 #Negative in range
145 ['e25','sum 1--5', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
146
147 # Test field pair syntaax
148 ['p40','pcov 1:2', {IN_PIPE=>""}, {OUT=>""}],
149 ['e41','pcov 1', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
150 ['e42','pcov 1:', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
151 ['e43','pcov :', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
152 ['e44','pcov :1', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
153 ['e46','pcov hello:world', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
154 ['e47','sum 1:3', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
155
156 ['p50','dotprod 1:2', {IN_PIPE=>""}, {OUT=>""}],
157 ['e51','dotprod 1', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
158 ['e52','dotprod 1:', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
159 ['e53','dotprod :', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
160 ['e54','dotprod :1', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
161 ['e56','dotprod hello:world',
162 {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
163
164 # Test scanner edge-cases
165 # Floating point value
166 ['e60','sum 4.5', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
167
168 ['e61','sum 4.', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
169
170
171 # invalid numbers
172 ['e62','sum 4a', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
173
174 ['e63','sum 4_', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
175
176 # Overflow strtol
177 ['e64','sum 1234567890123456789012345678901234567', {IN_PIPE=>""}, {EXIT=>1},
178 {ERR_SUBST=>'s/.*//s'}],
179
180 # Invalid charcters
181 ['e65','sum "foo^bar"', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
182
183 # Empty columns
184 ['e66','sum 1,,', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
185
186 # Range with names instead of numbers
187 ['e67','sum foo-bar', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
188
189
190 # Valid identifiers with undersocres
191 ['s66','--header-in sum foo_bar', {IN_PIPE=>"foo_bar\n1\n"}, {OUT=>"1\n"}],
192 ['s67','--header-in sum _bar', {IN_PIPE=>"_bar\n1\n"}, {OUT=>"1\n"}],
193
194 # Binning, and optional parameters
195 ['b31','bin 1', {IN_PIPE=>""}, {OUT=>""}],
196 ['b32','bin:10 1', {IN_PIPE=>""}, {OUT=>""}],
197 ['b33','bin:1.5 1', {IN_PIPE=>""}, {OUT=>""}],
198 ['b34','bin 1,2', {IN_PIPE=>""}, {OUT=>""}],
199 ['b35','bin:1 1,2', {IN_PIPE=>""}, {OUT=>""}],
200 ['e70','bin:10:30 1', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
201 ['e71','bin: 1', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
202 ['e72','sum: 1', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
203 ['e73','bin:10: 1', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
204 ['e74','bin:10:1', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
205 ['e75','bin:10, 1', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
206 ['e76','bin:, 1', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
207 ['e77','bin, 1', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
208 ['e78','bin:- 1', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
209 ['e79','sum:10 1', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
210 ['e30','bin :10 1', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
211 ['e31','bin : 10 1', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
212 ['e32','bin : 1', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
213 ['e33','bin:1,2 1', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
214 ['e34','bin:-2 1', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
215 ['e35','bin 1:2', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
216
217
218 # Field specifications for primary operations
219 ['e90', 'groupby 1:2', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
220 ['e91', 'groupby 1-2', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
221
222 # Test corner cases to avoid introducing unintended changes in behavior
223 # The parser allows some optional whitespace
224 ['cc0', '" sum 1 , 2"', {IN_PIPE=>""}, {OUT=>""}],
225 ['cc1', '" sum 1, 2"', {IN_PIPE=>""}, {OUT=>""}],
226 ['cc2', '" sum 1 ,2"', {IN_PIPE=>""}, {OUT=>""}],
227 ['cc3', '" sum 1 - 3"', {IN_PIPE=>""}, {OUT=>""}],
228 ['cc4', '" sum 1- 3"', {IN_PIPE=>""}, {OUT=>""}],
229 ['cc5', '" sum 1 -3"', {IN_PIPE=>""}, {OUT=>""}],
230 ['cc6', '" pcov 1 : 2"', {IN_PIPE=>""}, {OUT=>""}],
231 ['cc7', '" gb 1 , 2 sum 3"', {IN_PIPE=>""}, {OUT=>""}],
232 # Trailing whitespace leads to failures (could be seen as parser bug)
233 ['cce0', '"sum 1 "', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
234 ['cce1', "'sum 1\t'", {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
235 ['cce2', '"pcov 1:2 "', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
236 ['cce3', '"gb 1,2 sum 3 "', {IN_PIPE=>""}, {EXIT=>1}, {ERR_SUBST=>'s/.*//s'}],
237 );
238
239 my $save_temps = $ENV{SAVE_TEMPS};
240 my $verbose = $ENV{VERBOSE};
241
242 my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
243 exit $fail;