global: convert indentation-TABs to spaces
[coreutils.git] / tests / misc / head-elide-tail
blob8fc8686b5c831e7d14bc07cfe4e69f4d473136bf
1 #!/usr/bin/perl
2 # Exercise head's --bytes=-N option.
4 # Copyright (C) 2003, 2005, 2007-2009 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 <http://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 = 4096;
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"}],
57 if ($ENV{RUN_EXPENSIVE_TESTS})
59 # Brute force: use all combinations of file sizes [0..20] and
60 # number of bytes to elide [0..20]. For better coverage, recompile
61 # head with -DHEAD_TAIL_PIPE_READ_BUFSIZE=4 and
62 # -DHEAD_TAIL_PIPE_BYTECOUNT_THRESHOLD=8
63 my $s = "abcdefghijklmnopqrst";
64 for my $file_size (0..20)
66 for my $n_elide (0..20)
68 my $input = substr $s, 0, $file_size;
69 my $out_len = $n_elide < $file_size ? $file_size - $n_elide : 0;
70 my $output = substr $input, 0, $out_len;
71 my $t = ["elideb$file_size-$n_elide", "--bytes=-$n_elide",
72 {IN=>$input}, {OUT=>$output}];
73 push @Tests, $t;
74 my @u = @$t;
75 # Insert the ---presume-input-pipe option.
76 $u[0] .= 'p';
77 $u[1] .= ' ---presume-input-pipe';
78 push @Tests, \@u;
82 $s =~ s/(.)/$1\n/g;
83 for my $file_size (0..20)
85 for my $n_elide (0..20)
87 my $input = substr $s, 0, 2 * $file_size;
88 my $out_len = $n_elide < $file_size ? $file_size - $n_elide : 0;
89 my $output = substr $input, 0, 2 * $out_len;
90 my $t = ["elidel$file_size-$n_elide", "--lines=-$n_elide",
91 {IN=>$input}, {OUT=>$output}];
92 push @Tests, $t;
93 my @u = @$t;
94 # Insert the ---presume-input-pipe option.
95 $u[0] .= 'p';
96 $u[1] .= ' ---presume-input-pipe';
97 push @Tests, \@u;
102 my $save_temps = $ENV{DEBUG};
103 my $verbose = $ENV{VERBOSE};
105 my $prog = $ENV{PROG} || die "$0: \$PROG not specified in environment\n";
106 my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
107 exit $fail;