tests: add test for locale decimal processing
[coreutils.git] / tests / misc / csplit-suppress-matched.pl
blobc5eb9dceefb9d02d06db756b7dbdbbf4b43ecc90
1 #!/usr/bin/perl
3 # Copyright (C) 2013-2019 Free Software Foundation, Inc.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
18 use strict;
20 my $limits = getlimits ();
22 my $prog = 'csplit';
24 # Turn off localization of executable's output.
25 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
27 # Input from 'seq 6'
28 my $IN_SEQ_6 =<<EOF;
35 EOF
37 # Input from a possible run of 'uniq --group'
38 # (groups separated by empty lines)
39 my $IN_UNIQ =<<EOF;
57 EOF
59 # Standard Coreutils::run_tests() structure, except the addition of
60 # "OUTPUTS" array, containing the expected content of the output files.
61 # See code below for conversion into PRE/CMP/POST checks.
62 my @csplit_tests =
64 # without --suppress-matched,
65 # the newline (matched line) appears in the output files
66 ["re-base", "-q - '/^\$/' '{*}'", {IN_PIPE => $IN_UNIQ},
67 {OUTPUTS => [ "a\na\nYY\n", "\nXX\nb\nb\nYY\n","\nXX\nc\nYY\n",
68 "\nXX\nd\nd\nd\n" ] }],
70 # the newline (matched line) does not appears in the output files
71 ["re-1", " --suppress-matched -q - '/^\$/' '{*}'", {IN_PIPE => $IN_UNIQ},
72 {OUTPUTS => ["a\na\nYY\n", "XX\nb\nb\nYY\n", "XX\nc\nYY\n",
73 "XX\nd\nd\nd\n"]}],
75 # the 'XX' (matched line + offset 1) does not appears in the output files.
76 # the newline appears in the files (before each split, at the end of the file)
77 ["re-2", "--suppress-matched -q - '/^\$/1' '{*}'", {IN_PIPE => $IN_UNIQ},
78 {OUTPUTS => ["a\na\nYY\n\n","b\nb\nYY\n\n","c\nYY\n\n","d\nd\nd\n"]}],
80 # the 'YY' (matched line + offset of -1) does not appears in the output files
81 # the newline appears in the files (as the first line of the new split)
82 ["re-3", " --suppress-matched -q - '/^\$/-1' '{*}'", {IN_PIPE => $IN_UNIQ},
83 {OUTPUTS => ["a\na\n", "\nXX\nb\nb\n", "\nXX\nc\n", "\nXX\nd\nd\nd\n"]}],
85 # Test two consecutive matched lines
86 # without suppress-matched, the second file should contain a single newline.
87 ["re-4.1", "-q - '/^\$/' '{*}'", {IN_PIPE => "a\n\n\nb\n"},
88 {OUTPUTS => [ "a\n", "\n", "\nb\n" ]}],
89 # suppress-matched will cause the second file to be empty.
90 ["re-4.2", "--suppress-match -q - '/^\$/' '{*}'", {IN_PIPE => "a\n\n\nb\n"},
91 {OUTPUTS => [ "a\n", "", "b\n" ]}],
92 # suppress-matched + elide-empty should output just two files.
93 ["re-4.3", "--suppress-match -zq - '/^\$/' '{*}'", {IN_PIPE => "a\n\n\nb\n"},
94 {OUTPUTS => [ "a\n", "b\n" ]}],
97 # Test a matched-line as the last line
98 # default: last file with newline should be created.
99 ["re-5.1", "-q - '/^\$/' '{*}'", {IN_PIPE => "a\n\nb\n\n"},
100 {OUTPUTS => [ "a\n", "\nb\n", "\n" ]}],
101 # suppress-matched - last empty files should be created.
102 ["re-5.2", "--suppress-match -q - '/^\$/' '{*}'", {IN_PIPE => "a\n\nb\n\n"},
103 {OUTPUTS => [ "a\n", "b\n", "" ]}],
104 # suppress-matched + elide-empty: just two files should be created.
105 ["re-5.3", "--suppress-match -zq - '/^\$/' '{*}'", {IN_PIPE => "a\n\nb\n\n"},
106 {OUTPUTS => [ "a\n", "b\n" ]}],
108 # without suppress-matched,
109 # the matched lines (2/4/6) appears in the output files
110 ["int-base", '-q - 2 4 6', {IN_PIPE => $IN_SEQ_6},
111 {OUTPUTS => [ "1\n", "2\n3\n", "4\n5\n", "6\n" ]}],
112 # suppress matched - the matching lines (2/4/6) should not appear.
113 ["int-1", '--suppress-matched -q - 2 4 6', {IN_PIPE => $IN_SEQ_6},
114 {OUTPUTS => [ "1\n", "3\n", "5\n", "" ]}],
115 # suppress matched + elide-empty
116 ["int-2", '--suppress-matched -zq - 2 4 6', {IN_PIPE => $IN_SEQ_6},
117 {OUTPUTS => [ "1\n", "3\n", "5\n" ]}],
122 =pod
123 The following loop translate the above @Tests to a Cureutils::run_tests()
124 compatible structure. It converts "OUTPUTS" key into "CMP" + "POST" keys:
125 1. Each element in the OUTPUTS key is expected to be an output file
126 from csplit (named xx00, xx01, xx02...)
127 create a "CMP" key for each one, with the output and the filename.
128 2. Add a "POST" key, ensuring no extra files have been created.
129 (e.g. if there are 4 expected outputs, xx00 to xx03,
130 ensure xx04 doesn't exist).
131 3. Add a "PRE" key, deleting all existing 'xx*' files.
133 Example:
135 Before conversion:
136 my @csplit_tests =
138 ["1", '-z -q - 2 4 6',
139 {IN_PIPE => "1\n2\n3\n4\n5\n6\n"},
140 {OUTPUTS => [ "1\n", "2\n3\n", "4\n5\n", "6\n" ],
144 After conversion:
146 my @csplit_tests =
148 ["1", '-z -q - 2 4 6',
149 {IN_PIPE => "1\n2\n3\n4\n5\n6\n"},
150 {PRE => sub { unlink glob './xx??' ; }},
151 {CMP => ["1\n", {'xx00'=> undef}]},
152 {CMP => ["2\n3\n", {'xx01'=> undef}]},
153 {CMP => ["4\n5\n", {'xx02'=> undef}]},
154 {CMP => ["6\n", {'xx03'=> undef}]},
155 {POST => sub { die "extra file" if -e 'xx04'}},
158 =cut
159 my @Tests;
160 foreach my $t (@csplit_tests)
162 my ($test_name, $cmdline, @others) = @$t;
163 my $new_ent = [$test_name, $cmdline];
165 my $out_file_num = 0 ;
167 foreach my $e (@others)
169 die "Internal error: expecting a hash (e.g. IN_PIPE/OUTPUTS/ERR)" .
170 "in test '$test_name', got $e"
171 unless ref $e && (ref $e eq 'HASH');
173 my ($key, $value) = each %$e;
174 if ($key eq 'OUTPUTS')
176 # Convert each expected OUTPUT to a 'CMP' key.
177 foreach my $output (@$value)
179 my $filename = sprintf("xx%02d",$out_file_num++);
180 my $cmp = {CMP => [ $output, { $filename => undef}]};
181 push @$new_ent, $cmp;
184 # Add a 'POST' check
185 # Ensure no extra files have been created.
186 my $filename = sprintf("xx%02d",$out_file_num++);
187 my $post = { POST => sub { die "Test failed: an extraneous file " .
188 "'$filename' has been created\n"
189 if -e $filename; } } ;
190 push @$new_ent, $post;
192 # before running each test, cleanup the 'xx00' files
193 # from previous runs.
194 my $pre = { PRE => sub { unlink glob "./xx??"; } };
195 push @$new_ent, $pre;
197 else
199 # pass other entities as-is (e.g. OUT, ERR, OUT_SUBST, EXIT)
200 # run_tests() will know how to handle them.
201 push @$new_ent, $e;
205 push @Tests, $new_ent;
208 my $save_temps = $ENV{DEBUG};
209 my $verbose = $ENV{VERBOSE};
211 my $fail = run_tests ($prog, $prog, \@Tests, $save_temps, $verbose);
212 exit $fail;