1#!/usr/bin/perl
2# -*- perl -*-
3# Test comm
4
5# Copyright (C) 2008-2020 Free Software Foundation, Inc.
6
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.
11
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.
16
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/>.
19
20require 5.003;
21use strict;
22
23(my $program_name = $0) =~ s|.*/||;
24
25my $prog = 'comm';
26
27# Turn off localization of executable's ouput.
28@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
29
30my @inputs = ({IN=>{a=>"1\n3\n3\n3"}}, {IN=>{b=>"2\n2\n3\n3\n3"}});
31my @zinputs = ({IN=>{za=>"1\0003\0003\0003"}},
32               {IN=>{zb=>"2\0002\0003\0003\0003"}});
33
34my @Tests =
35  (
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"} ],
39
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"} ],
43
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"} ],
47
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"} ],
51
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"} ],
55
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"} ],
59
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"} ],
63
64   # suppress all output
65   ['opt-123', '-1', '-2', '-3', @inputs, {OUT=>""} ],
66
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"} ],
70
71   # show summary only, suppressing regular output
72   ['total-123', '--total', '-123', @inputs, {OUT=>"1\t2\t3\ttotal\n"} ],
73
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"}],
78
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"}],
83
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"}],
88
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"}],
94
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"}],
99
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"}],
103
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"}],
110
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"}],
117
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"}],
123
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"}],
129
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"}],
133
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"}],
139
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"}],
146
147   # alternate delimiter: ','
148   ['delim-comma', '--output-delimiter=,', @inputs,
149    {OUT=>"1\n,2\n,2\n,,3\n,,3\n,,3\n"} ],
150
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"} ],
154
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
161   # invalid dual delimiter
162   ['delim-dual', '--output-delimiter=,', '--output-delimiter=+', @inputs,
163    {EXIT=>1}, {ERR => "$prog: multiple output delimiters specified\n"}],
164
165   # valid dual delimiter specification
166   ['delim-dual2', '--output-delimiter=,', '--output-delimiter=,', @inputs,
167    {OUT=>"1\n,2\n,2\n,,3\n,,3\n,,3\n"} ],
168
169   # show summary, zero-terminated
170   ['totalz-all', '--total', '-z', @zinputs,
171    {OUT=>"1\000\t2\000\t2\000\t\t3\000\t\t3\000\t\t3\000"
172        . "1\t2\t3\ttotal\000"} ],
173
174   # show summary only (-123), zero-terminated and with ',' as delimiter
175   ['totalz-123', '--total', '-z123', '--output-delimiter=,', @zinputs,
176    {OUT=>"1,2,3,total\000"} ],
177 );
178
179my $save_temps = $ENV{DEBUG};
180my $verbose = $ENV{VERBOSE};
181
182my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
183exit $fail;
184