dd: don’t trust st_size on /proc/files
[coreutils.git] / tests / misc / comm.pl
blob5bd5f56d75f50c60dc150d5e97f68aa6d427ed87
1 #!/usr/bin/perl
2 # -*- perl -*-
3 # Test comm
5 # Copyright (C) 2008-2024 Free Software Foundation, Inc.
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <https://www.gnu.org/licenses/>.
20 require 5.003;
21 use strict;
23 (my $program_name = $0) =~ s|.*/||;
25 my $prog = 'comm';
27 # Turn off localization of executable's output.
28 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
30 my @inputs = ({IN=>{a=>"1\n3\n3\n3"}}, {IN=>{b=>"2\n2\n3\n3\n3"}});
31 my @zinputs = ({IN=>{za=>"1\0003\0003\0003"}},
32 {IN=>{zb=>"2\0002\0003\0003\0003"}});
34 my @Tests =
36 # basic operation
37 ['basic', @inputs, {OUT=>"1\n\t2\n\t2\n\t\t3\n\t\t3\n\t\t3\n"} ],
38 ['zbasic', '-z', @zinputs, {OUT=>"1\0\t2\0\t2\0\t\t3\0\t\t3\0\t\t3\0"} ],
40 # suppress lines unique to file 1
41 ['opt-1', '-1', @inputs, {OUT=>"2\n2\n\t3\n\t3\n\t3\n"} ],
42 ['zopt-1', '-z', '-1', @zinputs, {OUT=>"2\0002\000\t3\000\t3\000\t3\000"} ],
44 # suppress lines unique to file 2
45 ['opt-2', '-2', @inputs, {OUT=>"1\n\t3\n\t3\n\t3\n"} ],
46 ['zopt-2', '-z', '-2', @zinputs, {OUT=>"1\000\t3\000\t3\000\t3\000"} ],
48 # suppress lines that appear in both files
49 ['opt-3', '-3', @inputs, {OUT=>"1\n\t2\n\t2\n"} ],
50 ['zopt-3', '-z', '-3', @zinputs, {OUT=>"1\000\t2\000\t2\000"} ],
52 # suppress lines unique to file 1 and lines unique to file 2
53 ['opt-12', '-1', '-2', @inputs, {OUT=>"3\n3\n3\n"} ],
54 ['zopt-12', '-12z', @zinputs, {OUT=>"3\0003\0003\000"} ],
56 # suppress lines unique to file 1 and those that appear in both files
57 ['opt-13', '-1', '-3', @inputs, {OUT=>"2\n2\n"} ],
58 ['zopt-13', '-13z', @zinputs, {OUT=>"2\0002\000"} ],
60 # suppress lines unique to file 2 and those that appear in both files
61 ['opt-23', '-2', '-3', @inputs, {OUT=>"1\n"} ],
62 ['zopt-23', '-23z', @zinputs, {OUT=>"1\000"} ],
64 # suppress all output
65 ['opt-123', '-1', '-2', '-3', @inputs, {OUT=>""} ],
67 # show summary: 1 only in file1, 2 only in file2, 3 in both files
68 ['total-all', '--total', @inputs, {OUT=>"1\n\t2\n\t2\n\t\t3\n\t\t3\n\t\t3\n"
69 . "1\t2\t3\ttotal\n"} ],
71 # show summary only, suppressing regular output
72 ['total-123', '--total', '-123', @inputs, {OUT=>"1\t2\t3\ttotal\n"} ],
74 # invalid missing command line argument (1)
75 ['missing-arg1', $inputs[0], {EXIT=>1},
76 {ERR => "$prog: missing operand after 'a'\n"
77 . "Try '$prog --help' for more information.\n"}],
79 # invalid missing command line argument (both)
80 ['missing-arg2', {EXIT=>1},
81 {ERR => "$prog: missing operand\n"
82 . "Try '$prog --help' for more information.\n"}],
84 # invalid extra command line argument
85 ['extra-arg', @inputs, 'no-such', {EXIT=>1},
86 {ERR => "$prog: extra operand 'no-such'\n"
87 . "Try '$prog --help' for more information.\n"}],
89 # out-of-order input
90 ['ooo', {IN=>{a=>"1\n3"}}, {IN=>{b=>"3\n2"}}, {EXIT=>1},
91 {OUT => "1\n\t\t3\n\t2\n"},
92 {ERR => "$prog: file 2 is not in sorted order\n"
93 . "$prog: input is not in sorted order\n"}],
95 # out-of-order input, fatal
96 ['ooo2', '--check-order', {IN=>{a=>"1\n3"}}, {IN=>{b=>"3\n2"}}, {EXIT=>1},
97 {OUT => "1\n\t\t3\n"},
98 {ERR => "$prog: file 2 is not in sorted order\n"}],
100 # out-of-order input, ignored
101 ['ooo3', '--nocheck-order', {IN=>{a=>"1\n3"}}, {IN=>{b=>"3\n2"}},
102 {OUT => "1\n\t\t3\n\t2\n"}],
104 # both inputs out-of-order
105 ['ooo4', {IN=>{a=>"3\n1\n0"}}, {IN=>{b=>"3\n2\n0"}}, {EXIT=>1},
106 {OUT => "\t\t3\n1\n0\n\t2\n\t0\n"},
107 {ERR => "$prog: file 1 is not in sorted order\n".
108 "$prog: file 2 is not in sorted order\n"
109 . "$prog: input is not in sorted order\n"}],
111 # both inputs out-of-order on last pair
112 ['ooo5', {IN=>{a=>"3\n1"}}, {IN=>{b=>"3\n2"}}, {EXIT=>1},
113 {OUT => "\t\t3\n1\n\t2\n"},
114 {ERR => "$prog: file 1 is not in sorted order\n".
115 "$prog: file 2 is not in sorted order\n"
116 . "$prog: input is not in sorted order\n"}],
118 # first input out-of-order extended
119 ['ooo5b', {IN=>{a=>"0\n3\n1"}}, {IN=>{b=>"2\n3"}}, {EXIT=>1},
120 {OUT => "0\n\t2\n\t\t3\n1\n"},
121 {ERR => "$prog: file 1 is not in sorted order\n"
122 . "$prog: input is not in sorted order\n"}],
124 # second input out-of-order extended
125 ['ooo5c', {IN=>{a=>"0\n3"}}, {IN=>{b=>"2\n3\n1"}}, {EXIT=>1},
126 {OUT => "0\n\t2\n\t\t3\n\t1\n"},
127 {ERR => "$prog: file 2 is not in sorted order\n"
128 . "$prog: input is not in sorted order\n"}],
130 # both inputs out-of-order, but fully pairable
131 ['ooo6', {IN=>{a=>"2\n1\n0"}}, {IN=>{b=>"2\n1\n0"}}, {EXIT=>0},
132 {OUT => "\t\t2\n\t\t1\n\t\t0\n"}],
134 # both inputs out-of-order, fully pairable, but forced to fail
135 ['ooo7', '--check-order', {IN=>{a=>"2\n1\n0"}}, {IN=>{b=>"2\n1\n0"}},
136 {EXIT=>1},
137 {OUT => "\t\t2\n"},
138 {ERR => "$prog: file 1 is not in sorted order\n"}],
140 # out-of-order, line 2 is a prefix of line 1
141 # until coreutils-7.2, this test would fail -- no disorder detected
142 ['ooo-prefix', '--check-order', {IN=>{a=>"Xa\nX\n"}}, {IN=>{b=>""}},
143 {EXIT=>1},
144 {OUT => "Xa\n"},
145 {ERR => "$prog: file 1 is not in sorted order\n"}],
147 # alternate delimiter: ','
148 ['delim-comma', '--output-delimiter=,', @inputs,
149 {OUT=>"1\n,2\n,2\n,,3\n,,3\n,,3\n"} ],
151 # two-character alternate delimiter: '++'
152 ['delim-2char', '--output-delimiter=++', @inputs,
153 {OUT=>"1\n++2\n++2\n++++3\n++++3\n++++3\n"} ],
155 # NUL delimiter
156 ['delim-empty', '--output-delimiter=', @inputs,
157 {OUT=>"1\n\0002\n\0002\n\000\0003\n\000\0003\n\000\0003\n"} ],
158 ['zdelim-empty', '-z', '-z --output-delimiter=', @zinputs,
159 {OUT=>"1\000\0002\000\0002\000\000\0003\000\000\0003\000\000\0003\000"} ],
160 ['total-delim-empty', '--total --output-delimiter=', @inputs,
161 {OUT=>"1\n\0002\n\0002\n\000\0003\n\000\0003\n\000\0003\n"
162 . "1\0002\0003\000total\n"} ],
164 # invalid dual delimiter
165 ['delim-dual', '--output-delimiter=,', '--output-delimiter=+', @inputs,
166 {EXIT=>1}, {ERR => "$prog: multiple output delimiters specified\n"}],
168 # valid dual delimiter specification
169 ['delim-dual2', '--output-delimiter=,', '--output-delimiter=,', @inputs,
170 {OUT=>"1\n,2\n,2\n,,3\n,,3\n,,3\n"} ],
172 # show summary, zero-terminated
173 ['totalz-all', '--total', '-z', @zinputs,
174 {OUT=>"1\000\t2\000\t2\000\t\t3\000\t\t3\000\t\t3\000"
175 . "1\t2\t3\ttotal\000"} ],
177 # show summary only (-123), zero-terminated and with ',' as delimiter
178 ['totalz-123', '--total', '-z123', '--output-delimiter=,', @zinputs,
179 {OUT=>"1,2,3,total\000"} ],
182 my $save_temps = $ENV{DEBUG};
183 my $verbose = $ENV{VERBOSE};
185 my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
186 exit $fail;