Debian Codegraph @

tests/datamash-output-format.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) 2018-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 script tests output format options
30 ##
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
39 (my $program_name = $0) =~ s|.*/||;
40 my $prog = 'datamash';
41
42 # TODO: add localization tests with "grouping"
43 # Turn off localization of executable's output.
44 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
45
46 my $in1=<<'EOF';
47 1.000004
48 0.000005
49 EOF
50
51 my @Tests =
52 (
53 # Test Rouding
54 ['r1', 'sum 1' , {IN_PIPE=>$in1}, {OUT => "1.000009\n"}],
55 ['r2', '--round 1 sum 1' , {IN_PIPE=>$in1}, {OUT => "1.0\n"}],
56 ['r3', '--round 3 sum 1' , {IN_PIPE=>$in1}, {OUT => "1.000\n"}],
57 ['r4', '--round 5 sum 1' , {IN_PIPE=>$in1}, {OUT => "1.00001\n"}],
58 ['r5', '--round 6 sum 1' , {IN_PIPE=>$in1}, {OUT => "1.000009\n"}],
59 ['r6', '--round 7 sum 1' , {IN_PIPE=>$in1}, {OUT => "1.0000090\n"}],
60
61 # Test short rounding option
62 ['r7', '-R 7 sum 1', {IN_PIPE=>$in1}, {OUT => "1.0000090\n"}],
63
64 # Test multiple rounding options
65 ['r8', '--round 3 -R 7 sum 1', {IN_PIPE=>$in1}, {OUT => "1.0000090\n"}],
66 ['r9', '--round 7 -R 3 sum 1', {IN_PIPE=>$in1}, {OUT => "1.000\n"}],
67
68
69 # Test Custom formats: %f
70 ['f1', '--format "%07.3f" sum 1', {IN_PIPE=>$in1}, {OUT => "001.000\n"}],
71 ['f2', '--format "%.7f" sum 1', {IN_PIPE=>$in1}, {OUT => "1.0000090\n"}],
72 ['f3', '--format "%10f" sum 1', {IN_PIPE=>$in1}, {OUT => " 1.000009\n"}],
73 ['f4', '--format "%-10f" sum 1', {IN_PIPE=>$in1}, {OUT => "1.000009 \n"}],
74 ['f5', '--format "%+10f" sum 1', {IN_PIPE=>$in1}, {OUT => " +1.000009\n"}],
75 # Test %#f (alternate form: always show decimal point)
76 ['f6', '--format "%.0f" sum 1', {IN_PIPE=>$in1}, {OUT => "1\n"}],
77 ['f7', '--format "%#.0f" sum 1', {IN_PIPE=>$in1}, {OUT => "1.\n"}],
78
79 # Test Custom formats: %g
80 ['g1', '--format "%g" sum 1', {IN_PIPE=>$in1}, {OUT => "1.00001\n"}],
81 ['g2', '--format "%10g" sum 1', {IN_PIPE=>$in1}, {OUT => " 1.00001\n"}],
82 ['g3', '--format "%010g" sum 1', {IN_PIPE=>$in1}, {OUT => "0001.00001\n"}],
83 ['g4', '--format "%.10g" sum 1', {IN_PIPE=>$in1}, {OUT => "1.000009\n"}],
84 ['g5', '--format "%.3g" sum 1', {IN_PIPE=>$in1}, {OUT => "1\n"}],
85 # Test %#g (alternate form: don't trim zero decimal digits)
86 ['g6', '--format "%.4g" sum 1', {IN_PIPE=>$in1}, {OUT => "1\n"}],
87 ['g7', '--format "%#.4g" sum 1', {IN_PIPE=>$in1}, {OUT => "1.000\n"}],
88
89 # Test Custom formats: %e
90 ['e1', '--format "%e" sum 1', {IN_PIPE=>$in1}, {OUT=>"1.000009e+00\n"}],
91 ['e2', '--format "%.3e" sum 1', {IN_PIPE=>$in1}, {OUT=>"1.000e+00\n"}],
92
93 # Test Custom formats: %a
94 # Disable the test for now. Valid output can differ (e.g. 0x8.000p-3 and
95 # 0x1.000p0 ).
96 # ['a1', '--format "%0.3a" sum 1', {IN_PIPE=>$in1}, {OUT=>"0x8.000p-3\n"}],
97
98
99 # Custom formats can use lots of memory
100 ['m1', '--format "%04000.0f" sum 1', {IN_PIPE=>$in1},
101 {OUT => "0" x 3999 . "1\n"}],
102
103 # due to binary floating representation, some decimal point digits won't be
104 # zero (e.g. 1.0000090000000000000000000000000523453254320000000... or
105 # 1.000008999999...).
106 # The OUT_SUBST replaces exactly 3995 digits (as expected from the format)
107 # with an "X".
108 ['m2', '--format "%.4000f" sum 1', {IN_PIPE=>$in1},
109 {OUT => "1.00000X\n"},
110 {OUT_SUBST => 's/^(1\.00000)([0-9]{3995})$/\1X/'}],
111 );
112
113
114 my $save_temps = $ENV{SAVE_TEMPS};
115 my $verbose = $ENV{VERBOSE};
116
117 my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
118 exit $fail;