maint: update all copyright year number ranges
[coreutils.git] / tests / misc / numfmt.pl
blob43f92f91959507cf656c823af8b97c6f820a8b17
1 #!/usr/bin/perl
2 # Basic tests for "numfmt".
4 # Copyright (C) 2012-2017 Free Software Foundation, Inc.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 use strict;
21 (my $program_name = $0) =~ s|.*/||;
22 my $prog = 'numfmt';
24 my $limits = getlimits ();
26 # TODO: add localization tests with "grouping"
27 # Turn off localization of executable's output.
28 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
30 my $locale = $ENV{LOCALE_FR_UTF8};
31 ! defined $locale || $locale eq 'none'
32 and $locale = 'C';
34 my $try = "Try '$prog --help' for more information.\n";
36 my @Tests =
38 ['1', '1234', {OUT => "1234"}],
39 ['2', '--from=si 1K', {OUT => "1000"}],
40 ['3', '--from=iec 1K', {OUT => "1024"}],
41 ['4', '--from=auto 1K', {OUT => "1000"}],
42 ['5', '--from=auto 1Ki', {OUT => "1024"}],
43 ['5.1', '--from=iec-i 1Ki', {OUT => "1024"}],
45 ['6', {IN_PIPE => "1234\n"}, {OUT => "1234"}],
46 ['7', '--from=si', {IN_PIPE => "2K\n"}, {OUT => "2000"}],
47 ['7a', '--invalid=fail', {IN_PIPE => "no_NL"}, {OUT => "no_NL"},
48 {ERR => "$prog: invalid number: 'no_NL'\n"},
49 {EXIT => '2'}],
51 ['8', '--to=si 2000', {OUT => "2.0K"}],
52 ['9', '--to=si 2001', {OUT => "2.1K"}],
53 ['10', '--to=si 1999', {OUT => "2.0K"}],
54 ['11', '--to=si --round=down 2001', {OUT => "2.0K"}],
55 ['12', '--to=si --round=down 1999', {OUT => "1.9K"}],
56 ['13', '--to=si --round=up 1901', {OUT => "2.0K"}],
57 ['14', '--to=si --round=down 1901', {OUT => "1.9K"}],
58 ['15', '--to=si --round=nearest 1901', {OUT => "1.9K"}],
59 ['16', '--to=si --round=nearest 1945', {OUT => "1.9K"}],
60 ['17', '--to=si --round=nearest 1955', {OUT => "2.0K"}],
62 ['18', '--to=iec 2048', {OUT => "2.0K"}],
63 ['19', '--to=iec 2049', {OUT => "2.1K"}],
64 ['20', '--to=iec 2047', {OUT => "2.0K"}],
65 ['21', '--to=iec --round=down 2049', {OUT => "2.0K"}],
66 ['22', '--to=iec --round=down 2047', {OUT => "1.9K"}],
67 ['23', '--to=iec --round=up 2040', {OUT => "2.0K"}],
68 ['24', '--to=iec --round=down 2040', {OUT => "1.9K"}],
69 ['25', '--to=iec --round=nearest 1996', {OUT => "1.9K"}],
70 ['26', '--to=iec --round=nearest 1997', {OUT => "2.0K"}],
71 ['27', '--to=iec-i 2048', {OUT => "2.0Ki"}],
73 ['neg-1', '-- -1234', {OUT => "-1234"}],
74 ['neg-2', '--padding=5 -- -1234', {OUT => "-1234"}],
75 ['neg-3', '--padding=6 -- -1234', {OUT => " -1234"}],
76 ['neg-4', '--to=iec -- 9100 -9100', {OUT => "8.9K\n-8.9K"}],
77 ['neg-5', '-- -0.1', {OUT => "-0.1"}],
78 ['neg-6', '-- -0', {OUT => "0"}],
79 ['neg-7', '-- -0.-1',
80 {ERR => "$prog: invalid number: '-0.-1'\n"},
81 {EXIT => '2'}],
83 ['float-1', '1.1', {OUT => "1.1"}],
84 ['float-2', '1.22', {OUT => "1.22"}],
85 ['float-3', '1.22.',
86 {ERR => "$prog: invalid suffix in input: '1.22.'\n"},
87 {EXIT => '2'}],
89 ['unit-1', '--from-unit=512 4', {OUT => "2048"}],
90 ['unit-2', '--to-unit=512 2048', {OUT => "4"}],
91 ['unit-3', '--from-unit=512 --from=si 4M', {OUT => "2048000000"}],
92 ['unit-4', '--from-unit=512 --from=iec --to=iec 4M', {OUT => "2.0G"}],
93 ['unit-5', '--from-unit=AA --from=iec --to=iec 4M',
94 {ERR => "$prog: invalid unit size: 'AA'\n"},
95 {EXIT => '1'}],
96 ['unit-6', '--from-unit=54W --from=iec --to=iec 4M',
97 {ERR => "$prog: invalid unit size: '54W'\n"},
98 {EXIT => '1'}],
99 ['unit-7', '--from-unit=K 30', {OUT=>"30000"}],
100 ['unit-7.1', '--from-unit=Ki 30', {OUT=>"30720"}],
101 ['unit-7.2', '--from-unit=i 0',
102 {ERR => "$prog: invalid unit size: 'i'\n"},
103 {EXIT => '1'}],
104 ['unit-7.3', '--from-unit=1i 0',
105 {ERR => "$prog: invalid unit size: '1i'\n"},
106 {EXIT => '1'}],
107 ['unit-8', '--from-unit='.$limits->{UINTMAX_OFLOW}.' --to=iec 30',
108 {ERR => "$prog: invalid unit size: '$limits->{UINTMAX_OFLOW}'\n"},
109 {EXIT => '1'}],
110 ['unit-9', '--from-unit=0 1',
111 {ERR => "$prog: invalid unit size: '0'\n"},
112 {EXIT => '1'}],
113 ['unit-10', '--to-unit=0 1',
114 {ERR => "$prog: invalid unit size: '0'\n"},
115 {EXIT => '1'}],
117 # Test Suffix logic
118 ['suf-1', '4000', {OUT=>'4000'}],
119 ['suf-2', '4Q',
120 {ERR => "$prog: invalid suffix in input: '4Q'\n"},
121 {EXIT => '2'}],
122 ['suf-2.1', '4M',
123 {ERR => "$prog: rejecting suffix " .
124 "in input: '4M' (consider using --from)\n"},
125 {EXIT => '2'}],
126 ['suf-3', '--from=si 4M', {OUT=>'4000000'}],
127 ['suf-4', '--from=si 4Q',
128 {ERR => "$prog: invalid suffix in input: '4Q'\n"},
129 {EXIT => '2'}],
130 ['suf-5', '--from=si 4MQ',
131 {ERR => "$prog: invalid suffix in input '4MQ': 'Q'\n"},
132 {EXIT => '2'}],
134 ['suf-6', '--from=iec 4M', {OUT=>'4194304'}],
135 ['suf-7', '--from=auto 4M', {OUT=>'4000000'}],
136 ['suf-8', '--from=auto 4Mi', {OUT=>'4194304'}],
137 ['suf-9', '--from=auto 4MiQ',
138 {ERR => "$prog: invalid suffix in input '4MiQ': 'Q'\n"},
139 {EXIT => '2'}],
140 ['suf-10', '--from=auto 4QiQ',
141 {ERR => "$prog: invalid suffix in input: '4QiQ'\n"},
142 {EXIT => '2'}],
144 # characters after a white space are OK - printed as-is
145 ['suf-11', '"4 M"', {OUT=>'4 M'}],
147 # Custom suffix
148 ['suf-12', '--suffix=Foo 70Foo', {OUT=>'70Foo'}],
149 ['suf-13', '--suffix=Foo 70', {OUT=>'70Foo'}],
150 ['suf-14', '--suffix=Foo --from=si 70K', {OUT=>'70000Foo'}],
151 ['suf-15', '--suffix=Foo --from=si 70KFoo', {OUT=>'70000Foo'}],
152 ['suf-16', '--suffix=Foo --to=si 7000Foo', {OUT=>'7.0KFoo'}],
153 ['suf-17', '--suffix=Foo --to=si 7000Bar',
154 {ERR => "$prog: invalid suffix in input: '7000Bar'\n"},
155 {EXIT => '2'}],
156 ['suf-18', '--suffix=Foo --to=si 7000FooF',
157 {ERR => "$prog: invalid suffix in input: '7000FooF'\n"},
158 {EXIT => '2'}],
159 # space(s) between number and suffix. Note only field 1 is used
160 # by default so specify the NUL delimiter to consider the whole "line".
161 ['suf-19', "-d '' --from=si '4.0 K'", {OUT => "4000"}],
163 ## GROUPING
165 # "C" locale - no grouping (locale-specific tests, below)
166 ['grp-1', '--from=si --grouping 7M', {OUT=>'7000000'}],
167 ['grp-2', '--from=si --to=si --grouping 7M',
168 {ERR => "$prog: grouping cannot be combined with --to\n"},
169 {EXIT => '1'}],
172 ## Padding
173 ['pad-1', '--padding=10 5', {OUT=>' 5'}],
174 ['pad-2', '--padding=-10 5', {OUT=>'5 '}],
175 ['pad-3', '--padding=A 5',
176 {ERR => "$prog: invalid padding value 'A'\n"},
177 {EXIT => '1'}],
178 ['pad-3.1', '--padding=0 5',
179 {ERR => "$prog: invalid padding value '0'\n"},
180 {EXIT => '1'}],
181 ['pad-4', '--padding=10 --to=si 50000', {OUT=>' 50K'}],
182 ['pad-5', '--padding=-10 --to=si 50000', {OUT=>'50K '}],
184 # padding too narrow
185 ['pad-6', '--padding=2 --to=si 1000', {OUT=>'1.0K'}],
188 # Padding + suffix
189 ['pad-7', '--padding=10 --suffix=foo --to=si 50000',
190 {OUT=>' 50Kfoo'}],
191 ['pad-8', '--padding=-10 --suffix=foo --to=si 50000',
192 {OUT=>'50Kfoo '}],
195 # Delimiters
196 ['delim-1', '--delimiter=: --from=auto 40M:', {OUT=>'40000000:'}],
197 ['delim-2', '--delimiter="" --from=auto "40 M"',{OUT=>'40000000'}],
198 ['delim-3', '--delimiter=" " --from=auto "40M Foo"',{OUT=>'40000000 Foo'}],
199 ['delim-4', '--delimiter=: --from=auto 40M:60M', {OUT=>'40000000:60M'}],
200 ['delim-5', '-d: --field=2 --from=auto :40M:60M', {OUT=>':40000000:60M'}],
201 ['delim-6', '-d: --field 3 --from=auto 40M:60M', {OUT=>"40M:60M"}],
202 ['delim-err-1', '-d,, --to=si 1', {EXIT=>1},
203 {ERR => "$prog: the delimiter must be a single character\n"}],
205 #Fields
206 ['field-1', '--field A',
207 {ERR => "$prog: invalid field value 'A'\n$try"},
208 {EXIT => '1'}],
209 ['field-2', '--field 2 --from=auto "Hello 40M World 90G"',
210 {OUT=>'Hello 40000000 World 90G'}],
211 ['field-3', '--field 3 --from=auto "Hello 40M World 90G"',
212 {OUT=>"Hello 40M "},
213 {ERR=>"$prog: invalid number: 'World'\n"},
214 {EXIT => 2},],
215 # Last field - no text after number
216 ['field-4', '--field 4 --from=auto "Hello 40M World 90G"',
217 {OUT=>"Hello 40M World 90000000000"}],
218 # Last field - a delimiter after the number
219 ['field-5', '--field 4 --from=auto "Hello 40M World 90G "',
220 {OUT=>"Hello 40M World 90000000000 "}],
222 # Mix Fields + Delimiters
223 ['field-6', '--delimiter=: --field 2 --from=auto "Hello:40M:World:90G"',
224 {OUT=>"Hello:40000000:World:90G"}],
226 # not enough fields
227 ['field-8', '--field 3 --to=si "Hello World"', {OUT=>"Hello World"}],
229 # Multiple fields
230 ['field-range-1', '--field 2,4 --to=si "1000 2000 3000 4000 5000"',
231 {OUT=>"1000 2.0K 3000 4.0K 5000"}],
233 ['field-range-2', '--field 2-4 --to=si "1000 2000 3000 4000 5000"',
234 {OUT=>"1000 2.0K 3.0K 4.0K 5000"}],
236 ['field-range-3', '--field 1,2,3-5 --to=si "1000 2000 3000 4000 5000"',
237 {OUT=>"1.0K 2.0K 3.0K 4.0K 5.0K"}],
239 ['field-range-4', '--field 1-5 --to=si "1000 2000 3000 4000 5000"',
240 {OUT=>"1.0K 2.0K 3.0K 4.0K 5.0K"}],
242 ['field-range-5', '--field 1-3,5 --to=si "1000 2000 3000 4000 5000"',
243 {OUT=>"1.0K 2.0K 3.0K 4000 5.0K"}],
245 ['field-range-6', '--field 3- --to=si "1000 2000 3000 4000 5000"',
246 {OUT=>"1000 2000 3.0K 4.0K 5.0K"}],
248 ['field-range-7', '--field -3 --to=si "1000 2000 3000 4000 5000"',
249 {OUT=>"1.0K 2.0K 3.0K 4000 5000"}],
251 ['field-range-8', '--field 1-2,4-5 --to=si "1000 2000 3000 4000 5000"',
252 {OUT=>"1.0K 2.0K 3000 4.0K 5.0K"}],
253 ['field-range-9', '--field 4-5,1-2 --to=si "1000 2000 3000 4000 5000"',
254 {OUT=>"1.0K 2.0K 3000 4.0K 5.0K"}],
256 ['field-range-10','--field 1-3,2-4 --to=si "1000 2000 3000 4000 5000"',
257 {OUT=>"1.0K 2.0K 3.0K 4.0K 5000"}],
258 ['field-range-11','--field 2-4,1-3 --to=si "1000 2000 3000 4000 5000"',
259 {OUT=>"1.0K 2.0K 3.0K 4.0K 5000"}],
261 ['field-range-12','--field 1-1,3-3 --to=si "1000 2000 3000 4000 5000"',
262 {OUT=>"1.0K 2000 3.0K 4000 5000"}],
264 ['field-range-13', '--field 1,-2 --to=si "1000 2000 3000"',
265 {OUT=>"1.0K 2.0K 3000"}],
267 ['field-range-14', '--field -2,4- --to=si "1000 2000 3000 4000 5000"',
268 {OUT=>"1.0K 2.0K 3000 4.0K 5.0K"}],
269 ['field-range-15', '--field -2,-4 --to=si "1000 2000 3000 4000 5000"',
270 {OUT=>"1.0K 2.0K 3.0K 4.0K 5000"}],
271 ['field-range-16', '--field 2-,4- --to=si "1000 2000 3000 4000 5000"',
272 {OUT=>"1000 2.0K 3.0K 4.0K 5.0K"}],
273 ['field-range-17', '--field 4-,2- --to=si "1000 2000 3000 4000 5000"',
274 {OUT=>"1000 2.0K 3.0K 4.0K 5.0K"}],
276 # white space are valid field separators
277 # (undocumented? but works in cut as well).
278 ['field-range-18', '--field "1,2 4" --to=si "1000 2000 3000 4000 5000"',
279 {OUT=>"1.0K 2.0K 3000 4.0K 5000"}],
281 # Unlike 'cut', a lone '-' means 'all fields', even as part of a list
282 # of fields.
283 ['field-range-19','--field 3,- --to=si "1000 2000 3000 4000 5000"',
284 {OUT=>"1.0K 2.0K 3.0K 4.0K 5.0K"}],
286 ['all-fields-1', '--field=- --to=si "1000 2000 3000 4000 5000"',
287 {OUT=>"1.0K 2.0K 3.0K 4.0K 5.0K"}],
289 ['field-range-err-1', '--field -foo --to=si 10',
290 {EXIT=>1}, {ERR=>"$prog: invalid field value 'foo'\n$try"}],
291 ['field-range-err-2', '--field --3 --to=si 10',
292 {EXIT=>1}, {ERR=>"$prog: invalid field range\n$try"}],
293 ['field-range-err-3', '--field 0 --to=si 10',
294 {EXIT=>1}, {ERR=>"$prog: fields are numbered from 1\n$try"}],
295 ['field-range-err-4', '--field 3-2 --to=si 10',
296 {EXIT=>1}, {ERR=>"$prog: invalid decreasing range\n$try"}],
297 ['field-range-err-6', '--field - --field 1- --to=si 10',
298 {EXIT=>1}, {ERR=>"$prog: multiple field specifications\n"}],
299 ['field-range-err-7', '--field -1 --field 1- --to=si 10',
300 {EXIT=>1}, {ERR=>"$prog: multiple field specifications\n"}],
301 ['field-range-err-8', '--field -1 --field 1,2,3 --to=si 10',
302 {EXIT=>1}, {ERR=>"$prog: multiple field specifications\n"}],
303 ['field-range-err-9', '--field 1- --field 1,2,3 --to=si 10',
304 {EXIT=>1}, {ERR=>"$prog: multiple field specifications\n"}],
305 ['field-range-err-10','--field 1,2,3 --field 1- --to=si 10',
306 {EXIT=>1}, {ERR=>"$prog: multiple field specifications\n"}],
307 ['field-range-err-11','--field 1-2-3 --to=si 10',
308 {EXIT=>1}, {ERR=>"$prog: invalid field range\n$try"}],
309 ['field-range-err-12','--field 0-1 --to=si 10',
310 {EXIT=>1}, {ERR=>"$prog: fields are numbered from 1\n$try"}],
311 ['field-range-err-13','--field '.$limits->{SIZE_MAX}.',22 --to=si 10',
312 {EXIT=>1}, {ERR=>"$prog: field number " .
313 "'".$limits->{SIZE_MAX}."' is too large\n$try"}],
315 # Auto-consume white-space, setup auto-padding
316 ['whitespace-1', '--to=si --field 2 "A 500 B"', {OUT=>"A 500 B"}],
317 ['whitespace-2', '--to=si --field 2 "A 5000 B"', {OUT=>"A 5.0K B"}],
318 ['whitespace-3', '--to=si " 500"', {OUT=>" 500"}],
319 ['whitespace-4', '--to=si " 6500"', {OUT=>" 6.5K"}],
320 # NOTE: auto-padding is not enabled if the value is on the first
321 # field and there's no white-space before it.
322 ['whitespace-5', '--to=si "6000000"', {OUT=>"6.0M"}],
323 # but if there is whitespace, assume auto-padding is desired.
324 ['whitespace-6', '--to=si " 6000000"', {OUT=>" 6.0M"}],
326 # auto-padding - lines have same padding-width
327 # (padding_buffer will be alloc'd just once)
328 ['whitespace-7', '--to=si --field 2',
329 {IN_PIPE=>"rootfs 100000\n" .
330 "udevxx 2000000\n"},
331 {OUT =>"rootfs 100K\n" .
332 "udevxx 2.0M"}],
333 # auto-padding - second line requires a
334 # larger padding (padding-buffer needs to be realloc'd)
335 ['whitespace-8', '--to=si --field 2',
336 {IN_PIPE=>"rootfs 100000\n" .
337 "udev 20000000\n"},
338 {OUT =>"rootfs 100K\n" .
339 "udev 20M"}],
342 # Corner-cases:
343 # weird mix of identical suffix,delimiters
344 # The priority is:
345 # 1. delimiters (and fields) are parsed (in process_line()
346 # 2. optional custom suffix is removed (in process_suffixed_number())
347 # 3. Remaining suffixes must be valid SI/IEC (in human_xstrtol())
349 # custom suffix comes BEFORE SI/IEC suffix,
350 # so these are 40 of "M", not 40,000,000.
351 ['mix-1', '--suffix=M --from=si 40M', {OUT=>"40M"}],
353 # These are forty-million Ms .
354 ['mix-2', '--suffix=M --from=si 40MM', {OUT=>"40000000M"}],
356 ['mix-3', '--suffix=M --from=auto 40MM', {OUT=>"40000000M"}],
357 ['mix-4', '--suffix=M --from=auto 40MiM', {OUT=>"41943040M"}],
358 ['mix-5', '--suffix=M --to=si --from=si 4MM', {OUT=>"4.0MM"}],
360 # This might be confusing to the user, but it's legit:
361 # The M in the output is the custom suffix, not Mega.
362 ['mix-6', '--suffix=M 40', {OUT=>"40M"}],
363 ['mix-7', '--suffix=M 4000000', {OUT=>"4000000M"}],
364 ['mix-8', '--suffix=M --to=si 4000000', {OUT=>"4.0MM"}],
366 # The output 'M' is the custom suffix.
367 ['mix-10', '--delimiter=M --suffix=M 40', {OUT=>"40M"}],
369 # The INPUT 'M' is a delimiter (delimiters are top priority)
370 # The output contains one M for custom suffix, and one 'M' delimiter.
371 ['mix-11', '--delimiter=M --suffix=M 40M', {OUT=>"40MM"}],
373 # Same as above, the "M" is NOT treated as a mega SI prefix,
374 ['mix-12', '--delimiter=M --from=si --suffix=M 40M', {OUT=>"40MM"}],
376 # The 'M' is treated as a delimiter, and so the input value is '4000'
377 ['mix-13', '--delimiter=M --to=si --from=auto 4000M5000M9000',
378 {OUT=>"4.0KM5000M9000"}],
379 # 'M' is the delimiter, so the second input field is '5000'
380 ['mix-14', '--delimiter=M --field 2 --from=auto --to=si 4000M5000M9000',
381 {OUT=>"4000M5.0KM9000"}],
385 ## Header testing
387 # header - silently ignored with command line parameters
388 ['header-1', '--header --to=iec 4096', {OUT=>"4.0K"}],
390 # header warning with --debug
391 ['header-2', '--debug --header --to=iec 4096', {OUT=>"4.0K"},
392 {ERR=>"$prog: --header ignored with command-line input\n"}],
394 ['header-3', '--header=A',
395 {ERR=>"$prog: invalid header value 'A'\n"},
396 {EXIT => 1},],
397 ['header-4', '--header=0',
398 {ERR=>"$prog: invalid header value '0'\n"},
399 {EXIT => 1},],
400 ['header-5', '--header=-6',
401 {ERR=>"$prog: invalid header value '-6'\n"},
402 {EXIT => 1},],
403 ['header-6', '--debug --header --to=iec',
404 {IN_PIPE=>"size\n5000\n90000\n"},
405 {OUT=>"size\n4.9K\n88K"}],
406 ['header-7', '--debug --header=3 --to=iec',
407 {IN_PIPE=>"hello\nworld\nsize\n5000\n90000\n"},
408 {OUT=>"hello\nworld\nsize\n4.9K\n88K"}],
409 # header, but no actual content
410 ['header-8', '--header=2 --to=iec',
411 {IN_PIPE=>"hello\nworld\n"},
412 {OUT=>"hello\nworld"}],
413 # not enough header lines
414 ['header-9', '--header=3 --to=iec',
415 {IN_PIPE=>"hello\nworld\n"},
416 {OUT=>"hello\nworld"}],
419 ## human_strtod testing
421 # NO_DIGITS_FOUND
422 ['strtod-1', '--from=si "foo"',
423 {ERR=>"$prog: invalid number: 'foo'\n"},
424 {EXIT=> 2}],
425 ['strtod-2', '--from=si ""',
426 {ERR=>"$prog: invalid number: ''\n"},
427 {EXIT=> 2}],
429 # FRACTION_NO_DIGITS_FOUND
430 ['strtod-5', '--from=si 12.',
431 {ERR=>"$prog: invalid number: '12.'\n"},
432 {EXIT=>2}],
433 ['strtod-6', '--from=si 12.K',
434 {ERR=>"$prog: invalid number: '12.K'\n"},
435 {EXIT=>2}],
437 # whitespace is not allowed after decimal-point
438 ['strtod-6.1', '--from=si --delimiter=, "12. 2"',
439 {ERR=>"$prog: invalid number: '12. 2'\n"},
440 {EXIT=>2}],
442 # INVALID_SUFFIX
443 ['strtod-9', '--from=si 12.2Q',
444 {ERR=>"$prog: invalid suffix in input: '12.2Q'\n"},
445 {EXIT=>2}],
447 # VALID_BUT_FORBIDDEN_SUFFIX
448 ['strtod-10', '12M',
449 {ERR => "$prog: rejecting suffix " .
450 "in input: '12M' (consider using --from)\n"},
451 {EXIT=>2}],
453 # MISSING_I_SUFFIX
454 ['strtod-11', '--from=iec-i 12M',
455 {ERR => "$prog: missing 'i' suffix in input: " .
456 "'12M' (e.g Ki/Mi/Gi)\n"},
457 {EXIT=>2}],
460 # Test double_to_human()
463 # 1K and smaller
464 ['dbl-to-human-1','--to=si 800', {OUT=>"800"}],
465 ['dbl-to-human-2','--to=si 0', {OUT=>"0"}],
466 ['dbl-to-human-2.1','--to=si 999', {OUT=>"999"}],
467 ['dbl-to-human-2.2','--to=si 1000', {OUT=>"1.0K"}],
468 #NOTE: the following are consistent with "ls -lh" output
469 ['dbl-to-human-2.3','--to=iec 999', {OUT=>"999"}],
470 ['dbl-to-human-2.4','--to=iec 1023', {OUT=>"1023"}],
471 ['dbl-to-human-2.5','--to=iec 1024', {OUT=>"1.0K"}],
472 ['dbl-to-human-2.6','--to=iec 1025', {OUT=>"1.1K"}],
473 ['dbl-to-human-2.7','--to=iec 0', {OUT=>"0"}],
474 # no "i" suffix if output has no suffix
475 ['dbl-to-human-2.8','--to=iec-i 0', {OUT=>"0"}],
477 # values resulting in "N.Nx" output
478 ['dbl-to-human-3','--to=si 8000', {OUT=>"8.0K"}],
479 ['dbl-to-human-3.1','--to=si 8001', {OUT=>"8.1K"}],
480 ['dbl-to-human-4','--to=si --round=down 8001', {OUT=>"8.0K"}],
482 ['dbl-to-human-5','--to=si --round=down 3500', {OUT=>"3.5K"}],
483 ['dbl-to-human-6','--to=si --round=nearest 3500', {OUT=>"3.5K"}],
484 ['dbl-to-human-7','--to=si --round=up 3500', {OUT=>"3.5K"}],
486 ['dbl-to-human-8','--to=si --round=down 3501', {OUT=>"3.5K"}],
487 ['dbl-to-human-9','--to=si --round=nearest 3501', {OUT=>"3.5K"}],
488 ['dbl-to-human-10','--to=si --round=up 3501', {OUT=>"3.6K"}],
490 ['dbl-to-human-11','--to=si --round=nearest 3550', {OUT=>"3.6K"}],
491 ['dbl-to-human-12','--to=si --from=si 999.89K', {OUT=>"1.0M"}],
492 ['dbl-to-human-13','--to=si --from=si 9.9K', {OUT=>"9.9K"}],
493 ['dbl-to-human-14','--to=si 9900', {OUT=>"9.9K"}],
494 ['dbl-to-human-15','--to=iec --from=si 3.3K', {OUT=>"3.3K"}],
495 ['dbl-to-human-16','--to=iec --round=down --from=si 3.3K', {OUT=>"3.2K"}],
497 # values resulting in 'NNx' output
498 ['dbl-to-human-17','--to=si 9999', {OUT=>"10K"}],
499 ['dbl-to-human-18','--to=si --round=down 35000', {OUT=>"35K"}],
500 ['dbl-to-human-19','--to=iec 35000', {OUT=>"35K"}],
501 ['dbl-to-human-20','--to=iec --round=down 35000', {OUT=>"34K"}],
502 ['dbl-to-human-21','--to=iec 35000000', {OUT=>"34M"}],
503 ['dbl-to-human-22','--to=iec --round=down 35000000', {OUT=>"33M"}],
504 ['dbl-to-human-23','--to=si 35000001', {OUT=>"36M"}],
505 ['dbl-to-human-24','--to=si --from=si 9.99M', {OUT=>"10M"}],
506 ['dbl-to-human-25','--to=si --from=iec 9.99M', {OUT=>"11M"}],
507 ['dbl-to-human-25.1','--to=iec 99999', {OUT=>"98K"}],
509 # values resulting in 'NNNx' output
510 ['dbl-to-human-26','--to=si 999000000000', {OUT=>"999G"}],
511 ['dbl-to-human-27','--to=iec 999000000000', {OUT=>"931G"}],
512 ['dbl-to-human-28','--to=si 123600000000000', {OUT=>"124T"}],
513 ['dbl-to-human-29','--to=si 998123', {OUT=>"999K"}],
514 ['dbl-to-human-30','--to=si --round=nearest 998123', {OUT=>"998K"}],
515 ['dbl-to-human-31','--to=si 99999', {OUT=>"100K"}],
516 ['dbl-to-human-32','--to=iec 102399', {OUT=>"100K"}],
517 ['dbl-to-human-33','--to=iec-i 102399', {OUT=>"100Ki"}],
520 # Default --round=from-zero
521 ['round-1','--to-unit=1024 -- 6000 -6000',
522 {OUT=>"6\n-6"}],
523 ['round-2','--to-unit=1024 -- 6000.0 -6000.0',
524 {OUT=>"5.9\n-5.9"}],
525 ['round-3','--to-unit=1024 -- 6000.00 -6000.00',
526 {OUT=>"5.86\n-5.86"}],
527 ['round-4','--to-unit=1024 -- 6000.000 -6000.000',
528 {OUT=>"5.860\n-5.860"}],
529 ['round-5','--to-unit=1024 -- 6000.0000 -6000.0000',
530 {OUT=>"5.8594\n-5.8594"}],
531 # --round=up
532 ['round-1-up','--round=up --to-unit=1024 -- 6000 -6000',
533 {OUT=>"6\n-5"}],
534 ['round-2-up','--round=up --to-unit=1024 -- 6000.0 -6000.0',
535 {OUT=>"5.9\n-5.8"}],
536 ['round-3-up','--round=up --to-unit=1024 -- 6000.00 -6000.00',
537 {OUT=>"5.86\n-5.85"}],
538 ['round-4-up','--round=up --to-unit=1024 -- 6000.000 -6000.000',
539 {OUT=>"5.860\n-5.859"}],
540 ['round-5-up','--round=up --to-unit=1024 -- 6000.0000 -6000.0000',
541 {OUT=>"5.8594\n-5.8593"}],
542 # --round=down
543 ['round-1-down','--round=down --to-unit=1024 -- 6000 -6000',
544 {OUT=>"5\n-6"}],
545 ['round-2-down','--round=down --to-unit=1024 -- 6000.0 -6000.0',
546 {OUT=>"5.8\n-5.9"}],
547 ['round-3-down','--round=down --to-unit=1024 -- 6000.00 -6000.00',
548 {OUT=>"5.85\n-5.86"}],
549 ['round-4-down','--round=down --to-unit=1024 -- 6000.000 -6000.000',
550 {OUT=>"5.859\n-5.860"}],
551 ['round-5-down','--round=down --to-unit=1024 -- 6000.0000 -6000.0000',
552 {OUT=>"5.8593\n-5.8594"}],
553 # --round=towards-zero
554 ['round-1-to-zero','--ro=towards-zero --to-u=1024 -- 6000 -6000',
555 {OUT=>"5\n-5"}],
556 ['round-2-to-zero','--ro=towards-zero --to-u=1024 -- 6000.0 -6000.0',
557 {OUT=>"5.8\n-5.8"}],
558 ['round-3-to-zero','--ro=towards-zero --to-u=1024 -- 6000.00 -6000.00',
559 {OUT=>"5.85\n-5.85"}],
560 ['round-4-to-zero','--ro=towards-zero --to-u=1024 -- 6000.000 -6000.000',
561 {OUT=>"5.859\n-5.859"}],
562 ['round-5-to-zero','--ro=towards-zero --to-u=1024 -- 6000.0000 -6000.0000',
563 {OUT=>"5.8593\n-5.8593"}],
564 # --round=nearest
565 ['round-1-near','--ro=nearest --to-u=1024 -- 6000 -6000',
566 {OUT=>"6\n-6"}],
567 ['round-2-near','--ro=nearest --to-u=1024 -- 6000.0 -6000.0',
568 {OUT=>"5.9\n-5.9"}],
569 ['round-3-near','--ro=nearest --to-u=1024 -- 6000.00 -6000.00',
570 {OUT=>"5.86\n-5.86"}],
571 ['round-4-near','--ro=nearest --to-u=1024 -- 6000.000 -6000.000',
572 {OUT=>"5.859\n-5.859"}],
573 ['round-5-near','--ro=nearest --to-u=1024 -- 6000.0000 -6000.0000',
574 {OUT=>"5.8594\n-5.8594"}],
577 # Leading zeros weren't handled appropriately before 8.24
578 ['leading-1','0000000000000000000000000001', {OUT=>"1"}],
579 ['leading-2','.1', {OUT=>"0.1"}],
580 ['leading-3','bad.1',
581 {ERR => "$prog: invalid number: 'bad.1'\n"},
582 {EXIT => 2}],
583 ['leading-4','..1',
584 {ERR => "$prog: invalid suffix in input: '..1'\n"},
585 {EXIT => 2}],
586 ['leading-5','1.',
587 {ERR => "$prog: invalid number: '1.'\n"},
588 {EXIT => 2}],
590 # precision override
591 ['precision-1','--format=%.4f 9991239123 --to=si', {OUT=>"9.9913G"}],
592 ['precision-2','--format=%.1f 9991239123 --to=si', {OUT=>"10.0G"}],
593 ['precision-3','--format=%.1f 1', {OUT=>"1.0"}],
594 ['precision-4','--format=%.1f 1.12', {OUT=>"1.2"}],
595 ['precision-5','--format=%.1f 9991239123 --to-unit=G', {OUT=>"10.0"}],
596 ['precision-6','--format="% .1f" 9991239123 --to-unit=G', {OUT=>"10.0"}],
597 ['precision-7','--format=%.-1f 1.1',
598 {ERR => "$prog: invalid precision in format '%.-1f'\n"},
599 {EXIT => 1}],
600 ['precision-8','--format=%.+1f 1.1',
601 {ERR => "$prog: invalid precision in format '%.+1f'\n"},
602 {EXIT => 1}],
603 ['precision-9','--format="%. 1f" 1.1',
604 {ERR => "$prog: invalid precision in format '%. 1f'\n"},
605 {EXIT => 1}],
607 # debug warnings
608 ['debug-1', '--debug 4096', {OUT=>"4096"},
609 {ERR=>"$prog: no conversion option specified\n"}],
610 # '--padding' is a valid conversion option - no warning should be printed
611 ['debug-1.1', '--debug --padding 10 4096', {OUT=>" 4096"}],
612 ['debug-2', '--debug --grouping --from=si 4.0K', {OUT=>"4000"},
613 {ERR=>"$prog: grouping has no effect in this locale\n"}],
615 # dev-debug messages - the actual messages don't matter
616 # just ensure the program works, and for code coverage testing.
617 ['devdebug-1', '---debug --from=si 4.9K', {OUT=>"4900"},
618 {ERR=>""},
619 {ERR_SUBST=>"s/.*//msg"}],
620 ['devdebug-2', '---debug 4900', {OUT=>"4900"},
621 {ERR=>""},
622 {ERR_SUBST=>"s/.*//msg"}],
623 ['devdebug-3', '---debug --from=auto 4Mi', {OUT=>"4194304"},
624 {ERR=>""},
625 {ERR_SUBST=>"s/.*//msg"}],
626 ['devdebug-4', '---debug --to=si 4000000', {OUT=>"4.0M"},
627 {ERR=>""},
628 {ERR_SUBST=>"s/.*//msg"}],
629 ['devdebug-5', '---debug --to=si --padding=5 4000000', {OUT=>" 4.0M"},
630 {ERR=>""},
631 {ERR_SUBST=>"s/.*//msg"}],
632 ['devdebug-6', '---debug --suffix=Foo 1234Foo', {OUT=>"1234Foo"},
633 {ERR=>""},
634 {ERR_SUBST=>"s/.*//msg"}],
635 ['devdebug-7', '---debug --suffix=Foo 1234', {OUT=>"1234Foo"},
636 {ERR=>""},
637 {ERR_SUBST=>"s/.*//msg"}],
638 ['devdebug-9', '---debug --grouping 10000', {OUT=>"10000"},
639 {ERR=>""},
640 {ERR_SUBST=>"s/.*//msg"}],
641 ['devdebug-10', '---debug --format %f 10000', {OUT=>"10000"},
642 {ERR=>""},
643 {ERR_SUBST=>"s/.*//msg"}],
644 ['devdebug-11', '---debug --format "%\'-10f" 10000',{OUT=>"10000 "},
645 {ERR=>""},
646 {ERR_SUBST=>"s/.*//msg"}],
648 # Invalid parameters
649 ['help-1', '--foobar',
650 {ERR=>"$prog: unrecognized option\n$try"},
651 {ERR_SUBST=>"s/option.*/option/; s/unknown/unrecognized/"},
652 {EXIT=>1}],
654 ## Format string - check error detection
655 ['fmt-err-1', '--format ""',
656 {ERR=>"$prog: format '' has no % directive\n"},
657 {EXIT=>1}],
658 ['fmt-err-2', '--format "hello"',
659 {ERR=>"$prog: format 'hello' has no % directive\n"},
660 {EXIT=>1}],
661 ['fmt-err-3', '--format "hello%"',
662 {ERR=>"$prog: format 'hello%' ends in %\n"},
663 {EXIT=>1}],
664 ['fmt-err-4', '--format "%d"',
665 {ERR=>"$prog: invalid format '%d', " .
666 "directive must be %[0]['][-][N][.][N]f\n"},
667 {EXIT=>1}],
668 ['fmt-err-5', '--format "% -43 f"',
669 {ERR=>"$prog: invalid format '% -43 f', " .
670 "directive must be %[0]['][-][N][.][N]f\n"},
671 {EXIT=>1}],
672 ['fmt-err-6', '--format "%f %f"',
673 {ERR=>"$prog: format '%f %f' has too many % directives\n"},
674 {EXIT=>1}],
675 ['fmt-err-7', '--format "%'.$limits->{LONG_OFLOW}.'f"',
676 {ERR=>"$prog: invalid format '%$limits->{LONG_OFLOW}f'".
677 " (width overflow)\n"},
678 {EXIT=>1}],
679 ['fmt-err-9', '--format "%f" --grouping',
680 {ERR=>"$prog: --grouping cannot be combined with --format\n"},
681 {EXIT=>1}],
682 ['fmt-err-10', '--format "%\'f" --to=si',
683 {ERR=>"$prog: grouping cannot be combined with --to\n"},
684 {EXIT=>1}],
685 ['fmt-err-11', '--debug --format "%\'f" 5000', {OUT=>"5000"},
686 {ERR=>"$prog: grouping has no effect in this locale\n"}],
688 ## Format string - check some corner cases
689 ['fmt-1', '--format "%% %f" 5000', {OUT=>"%%5000"}],
690 ['fmt-2', '--format "%f %%" 5000', {OUT=>"5000 %%"}],
692 ['fmt-3', '--format "--%f--" 5000000', {OUT=>"--5000000--"}],
693 ['fmt-4', '--format "--%f--" --to=si 5000000', {OUT=>"--5.0M--"}],
695 ['fmt-5', '--format "--%10f--" --to=si 5000000',{OUT=>"-- 5.0M--"}],
696 ['fmt-6', '--format "--%-10f--" --to=si 5000000',{OUT=>"--5.0M --"}],
697 ['fmt-7', '--format "--%10f--" 5000000',{OUT=>"-- 5000000--"}],
698 ['fmt-8', '--format "--%-10f--" 5000000',{OUT=>"--5000000 --"}],
700 # too-short width
701 ['fmt-9', '--format "--%5f--" 5000000',{OUT=>"--5000000--"}],
703 # Format + Suffix
704 ['fmt-10', '--format "--%10f--" --suffix Foo 50', {OUT=>"-- 50Foo--"}],
705 ['fmt-11', '--format "--%-10f--" --suffix Foo 50',{OUT=>"--50Foo --"}],
707 # Grouping in C locale - no grouping effect
708 ['fmt-12', '--format "%\'f" 50000',{OUT=>"50000"}],
709 ['fmt-13', '--format "%\'10f" 50000', {OUT=>" 50000"}],
710 ['fmt-14', '--format "%\'-10f" 50000',{OUT=>"50000 "}],
712 # Very large format strings
713 ['fmt-15', '--format "--%100000f--" --to=si 4200',
714 {OUT=>"--" . " " x 99996 . "4.2K--" }],
716 # --format padding overrides --padding
717 ['fmt-16', '--format="%6f" --padding=66 1234',{OUT=>" 1234"}],
719 # zero padding
720 ['fmt-17', '--format="%06f" 1234',{OUT=>"001234"}],
721 # also support spaces (which are ignored as spacing is handled separately)
722 ['fmt-18', '--format="%0 6f" 1234',{OUT=>"001234"}],
723 # handle generic padding in combination
724 ['fmt-22', '--format="%06f" --padding=7 1234',{OUT=>" 001234"}],
725 ['fmt-23', '--format="%06f" --padding=-7 1234',{OUT=>"001234 "}],
728 ## Check all errors again, this time with --invalid=fail
729 ## Input will be printed without conversion,
730 ## and exit code will be 2
731 ['ign-err-1', '--invalid=fail 4Q',
732 {ERR => "$prog: invalid suffix in input: '4Q'\n"},
733 {OUT => "4Q\n"},
734 {EXIT => 2}],
735 ['ign-err-2', '--invalid=fail 4M',
736 {ERR => "$prog: rejecting suffix " .
737 "in input: '4M' (consider using --from)\n"},
738 {OUT => "4M\n"},
739 {EXIT => 2}],
740 ['ign-err-3', '--invalid=fail --from=si 4MQ',
741 {ERR => "$prog: invalid suffix in input '4MQ': 'Q'\n"},
742 {OUT => "4MQ\n"},
743 {EXIT => 2}],
744 ['ign-err-4', '--invalid=fail --suffix=Foo --to=si 7000FooF',
745 {ERR => "$prog: invalid suffix in input: '7000FooF'\n"},
746 {OUT => "7000FooF\n"},
747 {EXIT => 2}],
748 ['ign-err-5','--invalid=fail --field 3 --from=auto "Hello 40M World 90G"',
749 {ERR => "$prog: invalid number: 'World'\n"},
750 {OUT => "Hello 40M World 90G\n"},
751 {EXIT => 2}],
752 ['ign-err-7', '--invalid=fail --from=si "foo"',
753 {ERR => "$prog: invalid number: 'foo'\n"},
754 {OUT => "foo\n"},
755 {EXIT=> 2}],
756 ['ign-err-8', '--invalid=fail 12M',
757 {ERR => "$prog: rejecting suffix " .
758 "in input: '12M' (consider using --from)\n"},
759 {OUT => "12M\n"},
760 {EXIT => 2}],
761 ['ign-err-9', '--invalid=fail --from=iec-i 12M',
762 {ERR => "$prog: missing 'i' suffix in input: " .
763 "'12M' (e.g Ki/Mi/Gi)\n"},
764 {OUT => "12M\n"},
765 {EXIT=>2}],
767 ## Ignore Errors with multiple conversions
768 ['ign-err-m1', '--invalid=ignore --to=si 1000 2000 bad 3000',
769 {OUT => "1.0K\n2.0K\nbad\n3.0K"},
770 {EXIT => 0}],
771 ['ign-err-m1.1', '--invalid=ignore --to=si',
772 {IN_PIPE => "1000\n2000\nbad\n3000\n"},
773 {OUT => "1.0K\n2.0K\nbad\n3.0K"},
774 {EXIT => 0}],
775 ['ign-err-m1.3', '--invalid=fail --debug --to=si 1000 2000 3000',
776 {OUT => "1.0K\n2.0K\n3.0K"},
777 {EXIT => 0}],
778 ['ign-err-m2', '--invalid=fail --to=si 1000 Foo 3000',
779 {OUT => "1.0K\nFoo\n3.0K\n"},
780 {ERR => "$prog: invalid number: 'Foo'\n"},
781 {EXIT => 2}],
782 ['ign-err-m2.1', '--invalid=warn --to=si',
783 {IN_PIPE => "1000\nFoo\n3000\n"},
784 {OUT => "1.0K\nFoo\n3.0K"},
785 {ERR => "$prog: invalid number: 'Foo'\n"},
786 {EXIT => 0}],
788 # --debug will trigger a final warning at EOF
789 ['ign-err-m2.2', '--invalid=fail --debug --to=si 1000 Foo 3000',
790 {OUT => "1.0K\nFoo\n3.0K\n"},
791 {ERR => "$prog: invalid number: 'Foo'\n" .
792 "$prog: failed to convert some of the input numbers\n"},
793 {EXIT => 2}],
795 ['ign-err-m3', '--invalid=fail --field 2 --from=si --to=iec',
796 {IN_PIPE => "A 1K x\nB 2M y\nC 3G z\n"},
797 {OUT => "A 1000 x\nB 2.0M y\nC 2.8G z"},
798 {EXIT => 0}],
799 # invalid input on one of the fields
800 ['ign-err-m3.1', '--invalid=fail --field 2 --from=si --to=iec',
801 {IN_PIPE => "A 1K x\nB Foo y\nC 3G z\n"},
802 {OUT => "A 1000 x\nB Foo y\nC 2.8G z\n"},
803 {ERR => "$prog: invalid number: 'Foo'\n"},
804 {EXIT => 2}],
807 # test null-terminated lines
808 my @NullDelim_Tests =
810 # Input from STDIN
811 ['z1', '-z --to=iec',
812 {IN_PIPE => "1025\x002048\x00"}, {OUT=>"1.1K\x002.0K\x00"}],
814 # Input from the commandline - terminated by NULL vs NL
815 ['z3', ' --to=iec 1024', {OUT=>"1.0K\n"}],
816 ['z2', '-z --to=iec 1024', {OUT=>"1.0K\x00"}],
818 # Input from STDIN, with fields
819 ['z4', '-z --field=3 --to=si',
820 {IN_PIPE => "A B 1001 C\x00" .
821 "D E 2002 F\x00"},
822 {OUT => "A B 1.1K C\x00" .
823 "D E 2.1K F\x00"}],
825 # Input from STDIN, with fields and embedded NL
826 ['z5', '-z --field=3 --to=si',
827 {IN_PIPE => "A\nB 1001 C\x00" .
828 "D E\n2002 F\x00"},
829 {OUT => "A B 1.1K C\x00" .
830 "D E 2.1K F\x00"}],
833 my @Limit_Tests =
835 # Large Values
836 ['large-1','1000000000000000', {OUT=>"1000000000000000"}],
837 # 18 digits is OK
838 ['large-2','1000000000000000000', {OUT=>"1000000000000000000"}],
839 # 19 digits is too much (without output scaling)
840 ['large-3','10000000000000000000',
841 {ERR => "$prog: value too large to be printed: '1e+19' " .
842 "(consider using --to)\n"},
843 {EXIT=>2}],
844 ['large-4','1000000000000000000.0',
845 {ERR => "$prog: value/precision too large to be printed: " .
846 "'1e+18/1' (consider using --to)\n"},
847 {EXIT=>2}],
850 # Test input:
851 # Up to 27 digits is OK.
852 ['large-3.1', '--to=si 1', {OUT=> "1"}],
853 ['large-3.2', '--to=si 10', {OUT=> "10"}],
854 ['large-3.3', '--to=si 100', {OUT=> "100"}],
855 ['large-3.4', '--to=si 1000', {OUT=>"1.0K"}],
856 ['large-3.5', '--to=si 10000', {OUT=> "10K"}],
857 ['large-3.6', '--to=si 100000', {OUT=>"100K"}],
858 ['large-3.7', '--to=si 1000000', {OUT=>"1.0M"}],
859 ['large-3.8', '--to=si 10000000', {OUT=> "10M"}],
860 ['large-3.9', '--to=si 100000000', {OUT=>"100M"}],
861 ['large-3.10','--to=si 1000000000', {OUT=>"1.0G"}],
862 ['large-3.11','--to=si 10000000000', {OUT=> "10G"}],
863 ['large-3.12','--to=si 100000000000', {OUT=>"100G"}],
864 ['large-3.13','--to=si 1000000000000', {OUT=>"1.0T"}],
865 ['large-3.14','--to=si 10000000000000', {OUT=> "10T"}],
866 ['large-3.15','--to=si 100000000000000', {OUT=>"100T"}],
867 ['large-3.16','--to=si 1000000000000000', {OUT=>"1.0P"}],
868 ['large-3.17','--to=si 10000000000000000', {OUT=> "10P"}],
869 ['large-3.18','--to=si 100000000000000000', {OUT=>"100P"}],
870 ['large-3.19','--to=si 1000000000000000000', {OUT=>"1.0E"}],
871 ['large-3.20','--to=si 10000000000000000000', {OUT=> "10E"}],
872 ['large-3.21','--to=si 210000000000000000000', {OUT=>"210E"}],
873 ['large-3.22','--to=si 3210000000000000000000', {OUT=>"3.3Z"}],
874 ['large-3.23','--to=si 43210000000000000000000', {OUT=> "44Z"}],
875 ['large-3.24','--to=si 543210000000000000000000', {OUT=>"544Z"}],
876 ['large-3.25','--to=si 6543210000000000000000000', {OUT=>"6.6Y"}],
877 ['large-3.26','--to=si 76543210000000000000000000', {OUT=> "77Y"}],
878 ['large-3.27','--to=si 876543210000000000000000000', {OUT=>"877Y"}],
880 # More than 27 digits is not OK
881 ['large-3.28','--to=si 9876543210000000000000000000',
882 {ERR => "$prog: value too large to be converted: " .
883 "'9876543210000000000000000000'\n"},
884 {EXIT => 2}],
886 # Test Output
887 ['large-4.1', '--from=si 9.7M', {OUT=>"9700000"}],
888 ['large-4.2', '--from=si 10M', {OUT =>"10000000"}],
889 ['large-4.3', '--from=si 200M', {OUT =>"200000000"}],
890 ['large-4.4', '--from=si 3G', {OUT =>"3000000000"}],
891 ['large-4.5', '--from=si 40G', {OUT =>"40000000000"}],
892 ['large-4.6', '--from=si 500G', {OUT =>"500000000000"}],
893 ['large-4.7', '--from=si 6T', {OUT =>"6000000000000"}],
894 ['large-4.8', '--from=si 70T', {OUT =>"70000000000000"}],
895 ['large-4.9', '--from=si 800T', {OUT =>"800000000000000"}],
896 ['large-4.10','--from=si 9P', {OUT =>"9000000000000000"}],
897 ['large-4.11','--from=si 10P', {OUT =>"10000000000000000"}],
898 ['large-4.12','--from=si 200P', {OUT =>"200000000000000000"}],
899 ['large-4.13','--from=si 3E', {OUT =>"3000000000000000000"}],
901 # More than 18 digits of output without scaling - no good.
902 ['large-4.14','--from=si 40E',
903 {ERR => "$prog: value too large to be printed: '4e+19' " .
904 "(consider using --to)\n"},
905 {EXIT => 2}],
906 ['large-4.15','--from=si 500E',
907 {ERR => "$prog: value too large to be printed: '5e+20' " .
908 "(consider using --to)\n"},
909 {EXIT => 2}],
910 ['large-4.16','--from=si 6Z',
911 {ERR => "$prog: value too large to be printed: '6e+21' " .
912 "(consider using --to)\n"},
913 {EXIT => 2}],
914 ['large-4.17','--from=si 70Z',
915 {ERR => "$prog: value too large to be printed: '7e+22' " .
916 "(consider using --to)\n"},
917 {EXIT => 2}],
918 ['large-4.18','--from=si 800Z',
919 {ERR => "$prog: value too large to be printed: '8e+23' " .
920 "(consider using --to)\n"},
921 {EXIT => 2}],
922 ['large-4.19','--from=si 9Y',
923 {ERR => "$prog: value too large to be printed: '9e+24' " .
924 "(consider using --to)\n"},
925 {EXIT => 2}],
926 ['large-4.20','--from=si 10Y',
927 {ERR => "$prog: value too large to be printed: '1e+25' " .
928 "(consider using --to)\n"},
929 {EXIT => 2}],
930 ['large-4.21','--from=si 200Y',
931 {ERR => "$prog: value too large to be printed: '2e+26' " .
932 "(consider using --to)\n"},
933 {EXIT => 2}],
935 ['large-5.1','--to=si 1000000000000000000', {OUT=>"1.0E"}],
936 ['large-5','--from=si --to=si 2E', {OUT=>"2.0E"}],
937 ['large-6','--from=si --to=si 3.4Z', {OUT=>"3.4Z"}],
938 ['large-7','--from=si --to=si 80Y', {OUT=>"80Y"}],
939 ['large-8','--from=si --to=si 9000Z', {OUT=>"9.0Y"}],
941 ['large-10','--from=si --to=si 999Y', {OUT=>"999Y"}],
942 ['large-11','--from=si --to=iec 999Y', {OUT=>"827Y"}],
943 ['large-12','--from=si --round=down --to=iec 999Y', {OUT=>"826Y"}],
945 # units can also affect the output
946 ['large-13','--from=si --from-unit=1000000 9P',
947 {ERR => "$prog: value too large to be printed: '9e+21' " .
948 "(consider using --to)\n"},
949 {EXIT => 2}],
950 ['large-13.1','--from=si --from-unit=1000000 --to=si 9P', {OUT=>"9.0Z"}],
952 # Numbers>999Y are never acceptable, regardless of scaling
953 ['large-14','--from=si --to=si 999Y', {OUT=>"999Y"}],
954 ['large-14.1','--from=si --to=si 1000Y',
955 {ERR => "$prog: value too large to be printed: '1e+27' " .
956 "(cannot handle values > 999Y)\n"},
957 {EXIT => 2}],
958 ['large-14.2','--from=si --to=si --from-unit=10000 1Y',
959 {ERR => "$prog: value too large to be printed: '1e+28' " .
960 "(cannot handle values > 999Y)\n"},
961 {EXIT => 2}],
963 # intmax_t overflow when rounding caused this to fail before 8.24
964 ['large-15',$limits->{INTMAX_OFLOW}, {OUT=>$limits->{INTMAX_OFLOW}}],
965 ['large-16','9.300000000000000000', {OUT=>'9.300000000000000000'}],
967 # INTEGRAL_OVERFLOW
968 ['strtod-3', '--from=si "1234567890123456789012345678901234567890'.
969 '1234567890123456789012345678901234567890"',
970 {ERR=>"$prog: value too large to be converted: '" .
971 "1234567890123456789012345678901234567890" .
972 "1234567890123456789012345678901234567890'\n",
974 {EXIT=> 2}],
976 # FRACTION_OVERFLOW
977 ['strtod-7', '--from=si "12.1234567890123456789012345678901234567890'.
978 '1234567890123456789012345678901234567890"',
979 {ERR=>"$prog: value too large to be converted: '" .
980 "12.1234567890123456789012345678901234567890" .
981 "1234567890123456789012345678901234567890'\n",
983 {EXIT=> 2}],
985 ['debug-4', '--to=si --debug 12345678901234567890',
986 {OUT=>"13E"},
987 {ERR=>"$prog: large input value '12345678901234567890':" .
988 " possible precision loss\n"}],
989 ['debug-5', '--to=si --from=si --debug 1.12345678901234567890Y',
990 {OUT=>"1.2Y"},
991 {ERR=>"$prog: large input value '1.12345678901234567890Y':" .
992 " possible precision loss\n"}],
994 ['ign-err-10','--invalid=fail 10000000000000000000',
995 {ERR => "$prog: value too large to be printed: '1e+19' " .
996 "(consider using --to)\n"},
997 {OUT => "10000000000000000000\n"},
998 {EXIT=>2}],
999 ['ign-err-11','--invalid=fail --to=si 9876543210000000000000000000',
1000 {ERR => "$prog: value too large to be converted: " .
1001 "'9876543210000000000000000000'\n"},
1002 {OUT => "9876543210000000000000000000\n"},
1003 {EXIT => 2}],
1005 # Restrict these tests to systems with LDBL_DIG == 18
1006 (system "$prog ---debug 1 2>&1|grep 'MAX_UNSCALED_DIGITS: 18' > /dev/null") == 0
1007 and push @Tests, @Limit_Tests;
1009 my @Locale_Tests =
1011 # Locale that supports grouping, but without '--grouping' parameter
1012 ['lcl-grp-1', '--from=si 7M', {OUT=>"7000000"},
1013 {ENV=>"LC_ALL=$locale"}],
1015 # Locale with grouping
1016 ['lcl-grp-2', '--from=si --grouping 7M', {OUT=>"7 000 000"},
1017 {ENV=>"LC_ALL=$locale"}],
1019 # Locale with grouping and debug - no debug warning message
1020 ['lcl-grp-3', '--from=si --debug --grouping 7M', {OUT=>"7 000 000"},
1021 {ENV=>"LC_ALL=$locale"}],
1023 # Input with locale'd decimal-point
1024 ['lcl-stdtod-1', '--from=si 12,2K', {OUT=>"12200"},
1025 {ENV=>"LC_ALL=$locale"}],
1027 ['lcl-dbl-to-human-1', '--to=si 1100', {OUT=>"1,1K"},
1028 {ENV=>"LC_ALL=$locale"}],
1030 # Format + Grouping
1031 ['lcl-fmt-1', '--format "%\'f" 50000',{OUT=>"50 000"},
1032 {ENV=>"LC_ALL=$locale"}],
1033 ['lcl-fmt-2', '--format "--%\'10f--" 50000', {OUT=>"-- 50 000--"},
1034 {ENV=>"LC_ALL=$locale"}],
1035 ['lcl-fmt-3', '--format "--%\'-10f--" 50000',{OUT=>"--50 000 --"},
1036 {ENV=>"LC_ALL=$locale"}],
1037 ['lcl-fmt-4', '--format "--%-10f--" --to=si 5000000',
1038 {OUT=>"--5,0M --"},
1039 {ENV=>"LC_ALL=$locale"}],
1040 # handle zero/grouping in combination
1041 ['lcl-fmt-5', '--format="%\'06f" 1234',{OUT=>"01 234"},
1042 {ENV=>"LC_ALL=$locale"}],
1043 ['lcl-fmt-6', '--format="%0\'6f" 1234',{OUT=>"01 234"},
1044 {ENV=>"LC_ALL=$locale"}],
1045 ['lcl-fmt-7', '--format="%0\'\'6f" 1234',{OUT=>"01 234"},
1046 {ENV=>"LC_ALL=$locale"}],
1049 if ($locale ne 'C')
1051 # Reset locale to 'C' if LOCALE_FR_UTF8 doesn't output as expected
1052 # as determined by the separate printf program.
1053 open(LOC_NUM, "env LC_ALL=$locale printf \"%'d\" 1234|")
1054 or die "Can't fork command: $!";
1055 my $loc_num = <LOC_NUM>;
1056 close(LOC_NUM) || die "Failed to read grouped number from printf";
1057 if ($loc_num ne '1 234')
1059 warn "skipping locale grouping tests as 1234 groups like $loc_num\n";
1060 $locale = 'C';
1063 push @Tests, @Locale_Tests if $locale ne 'C';
1065 ## Check all valid/invalid suffixes
1066 foreach my $suf ( 'A' .. 'Z', 'a' .. 'z' ) {
1067 if ( $suf =~ /^[KMGTPEZY]$/ )
1069 push @Tests, ["auto-suf-si-$suf","--from=si --to=si 1$suf",
1070 {OUT=>"1.0$suf"}];
1071 push @Tests, ["auto-suf-iec-$suf","--from=iec --to=iec 1$suf",
1072 {OUT=>"1.0$suf"}];
1073 push @Tests, ["auto-suf-auto-$suf","--from=auto --to=iec 1${suf}i",
1074 {OUT=>"1.0$suf"}];
1075 push @Tests, ["auto-suf-iec-to-ieci-$suf","--from=iec --to=iec-i 1${suf}",
1076 {OUT=>"1.0${suf}i"}];
1077 push @Tests, ["auto-suf-ieci-to-iec-$suf",
1078 "--from=iec-i --to=iec 1${suf}i",{OUT=>"1.0${suf}"}];
1080 else
1082 push @Tests, ["auto-suf-si-$suf","--from=si --to=si 1$suf",
1083 {ERR=>"$prog: invalid suffix in input: '1${suf}'\n"},
1084 {EXIT=>2}];
1088 # Prepend the command line argument and append a newline to end
1089 # of each expected 'OUT' string.
1090 my $t;
1092 Test:
1093 foreach $t (@Tests)
1095 # Don't fiddle with expected OUT string if there's a nonzero exit status.
1096 foreach my $e (@$t)
1098 ref $e eq 'HASH' && exists $e->{EXIT} && $e->{EXIT}
1099 and next Test;
1102 foreach my $e (@$t)
1104 ref $e eq 'HASH' && exists $e->{OUT}
1105 and $e->{OUT} .= "\n"
1109 # Add test for null-terminated lines (after adjusting the OUT string, above).
1110 push @Tests, @NullDelim_Tests;
1112 my $save_temps = $ENV{SAVE_TEMPS};
1113 my $verbose = $ENV{VERBOSE};
1115 my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
1116 exit $fail;