maint: silence deprecated module warning
[coreutils.git] / tests / head / head-elide-tail.pl
blob319cba537368eca2ba98ecbf9ef5994b1fb51d6d
1 #!/usr/bin/perl
2 # Exercise head's --bytes=-N option.
4 # Copyright (C) 2003-2024 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 <https://www.gnu.org/licenses/>.
19 use strict;
21 (my $program_name = $0) =~ s|.*/||;
23 $ENV{PROG} = 'head';
25 # Turn off localization of executable's output.
26 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
28 # This should match the definition in head.c.
29 my $READ_BUFSIZE = 8192;
31 my @Tests =
33 # Elide the exact size of the file.
34 ['elide-b1', "--bytes=-2", {IN=>"a\n"}, {OUT=>''}],
35 # Elide more than the size of the file.
36 ['elide-b2', "--bytes=-2", {IN=>"a"}, {OUT=>''}],
37 # Leave just one byte.
38 ['elide-b3', "--bytes=-2", {IN=>"abc"}, {OUT=>'a'}],
39 # Make it so the elided bytes straddle the end of the first
40 # $READ_BUFSIZE block.
41 ['elide-b4', "--bytes=-2",
42 {IN=> 'a' x ($READ_BUFSIZE-3) . "\nbcd"},
43 {OUT=>'a' x ($READ_BUFSIZE-3) . "\nb"}],
44 # Make it so the elided bytes straddle the end of the 2nd
45 # $READ_BUFSIZE block.
46 ['elide-b5', "--bytes=-2",
47 {IN=> 'a' x (2 * $READ_BUFSIZE - 2) . 'bcd'},
48 {OUT=>'a' x (2 * $READ_BUFSIZE - 2) . 'b'}],
50 ['elide-l0', "--lines=-1", {IN=>''}, {OUT=>''}],
51 ['elide-l1', "--lines=-1", {IN=>"a\n"}, {OUT=>''}],
52 ['elide-l2', "--lines=-1", {IN=>"a"}, {OUT=>''}],
53 ['elide-l3', "--lines=-1", {IN=>"a\nb"}, {OUT=>"a\n"}],
54 ['elide-l4', "--lines=-1", {IN=>"a\nb\n"}, {OUT=>"a\n"}],
55 ['elide-l5', "--lines=-0", {IN=>"a\nb\n"}, {OUT=>"a\nb\n"}],
56 ['elide-l6', "--lines=-0", {IN=>"a\nb"}, {OUT=>"a\nb"}],
59 if ($ENV{RUN_EXPENSIVE_TESTS})
61 # Brute force: use all combinations of file sizes [0..20] and
62 # number of bytes to elide [0..20]. For better coverage, recompile
63 # head with -DHEAD_TAIL_PIPE_READ_BUFSIZE=4 and
64 # -DHEAD_TAIL_PIPE_BYTECOUNT_THRESHOLD=8
65 my $s = "abcdefghijklmnopqrst";
66 for my $file_size (0..20)
68 for my $n_elide (0..20)
70 my $input = substr $s, 0, $file_size;
71 my $out_len = $n_elide < $file_size ? $file_size - $n_elide : 0;
72 my $output = substr $input, 0, $out_len;
73 my $t = ["elideb$file_size-$n_elide", "--bytes=-$n_elide",
74 {IN=>$input}, {OUT=>$output}];
75 push @Tests, $t;
76 my @u = @$t;
77 # Insert the ---presume-input-pipe option.
78 $u[0] .= 'p';
79 $u[1] .= ' ---presume-input-pipe';
80 push @Tests, \@u;
84 $s =~ s/(.)/$1\n/g;
85 $s .= 'u'; # test without trailing '\n'
86 for my $file_size (0..21)
88 for my $n_elide (0..21)
90 my $input = substr $s, 0, 2 * $file_size;
91 my $out_len = $n_elide < $file_size ? $file_size - $n_elide : 0;
92 my $output = substr $input, 0, 2 * $out_len;
93 my $t = ["elidel$file_size-$n_elide", "--lines=-$n_elide",
94 {IN=>$input}, {OUT=>$output}];
95 push @Tests, $t;
96 my @u = @$t;
97 # Insert the ---presume-input-pipe option.
98 $u[0] .= 'p';
99 $u[1] .= ' ---presume-input-pipe';
100 push @Tests, \@u;
105 my $save_temps = $ENV{DEBUG};
106 my $verbose = $ENV{VERBOSE};
108 my $prog = $ENV{PROG} || die "$0: \$PROG not specified in environment\n";
109 my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
110 exit $fail;