3 # Exercise head's --bytes=-N option.
5 # Copyright (C) 2003, 2005 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 2 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, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 $PERL -e 1 > /dev/null 2>&1 || {
26 echo 1>&2 "$0: configure didn't find a usable version of Perl," \
27 "so can't run this test"
31 exec $PERL -w -I$srcdir/.. -MCoreutils -- - <<\EOF
36 (my $program_name = $0) =~ s|.*/||;
40 # Turn off localization of executable's ouput.
41 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
43 # This should match the definition in head.c.
44 my $READ_BUFSIZE = 4096;
48 # Elide the exact size of the file.
49 ['elide-b1', "--bytes=-2", {IN=>"a\n"}, {OUT=>''}],
50 # Elide more than the size of the file.
51 ['elide-b2', "--bytes=-2", {IN=>"a"}, {OUT=>''}],
52 # Leave just one byte.
53 ['elide-b3', "--bytes=-2", {IN=>"abc"}, {OUT=>'a'}],
54 # Make it so the elided bytes straddle the end of the first
55 # $READ_BUFSIZE block.
56 ['elide-b4', "--bytes=-2",
57 {IN=> 'a' x ($READ_BUFSIZE-3) . "\nbcd"},
58 {OUT=>'a' x ($READ_BUFSIZE-3) . "\nb"}],
59 # Make it so the elided bytes straddle the end of the 2nd
60 # $READ_BUFSIZE block.
61 ['elide-b5', "--bytes=-2",
62 {IN=> 'a' x (2 * $READ_BUFSIZE - 2) . 'bcd'},
63 {OUT=>'a' x (2 * $READ_BUFSIZE - 2) . 'b'}],
65 ['elide-l0', "--lines=-1", {IN=>''}, {OUT=>''}],
66 ['elide-l1', "--lines=-1", {IN=>"a\n"}, {OUT=>''}],
67 ['elide-l2', "--lines=-1", {IN=>"a"}, {OUT=>''}],
68 ['elide-l3', "--lines=-1", {IN=>"a\nb"}, {OUT=>"a\n"}],
69 ['elide-l4', "--lines=-1", {IN=>"a\nb\n"}, {OUT=>"a\n"}],
72 if ($ENV{RUN_EXPENSIVE_TESTS})
74 # Brute force: use all combinations of file sizes [0..20] and
75 # number of bytes to elide [0..20]. For better coverage, recompile
76 # head with -DHEAD_TAIL_PIPE_READ_BUFSIZE=4 and
77 # -DHEAD_TAIL_PIPE_BYTECOUNT_THRESHOLD=8
78 my $s = "abcdefghijklmnopqrst";
79 for my $file_size (0..20)
81 for my $n_elide (0..20)
83 my $input = substr $s, 0, $file_size;
84 my $out_len = $n_elide < $file_size ? $file_size - $n_elide : 0;
85 my $output = substr $input, 0, $out_len;
86 my $t = ["elideb$file_size-$n_elide", "--bytes=-$n_elide",
87 {IN=>$input}, {OUT=>$output}];
90 # Insert the ---presume-input-pipe option.
92 $u[1] .= ' ---presume-input-pipe';
98 for my $file_size (0..20)
100 for my $n_elide (0..20)
102 my $input = substr $s, 0, 2 * $file_size;
103 my $out_len = $n_elide < $file_size ? $file_size - $n_elide : 0;
104 my $output = substr $input, 0, 2 * $out_len;
105 my $t = ["elidel$file_size-$n_elide", "--lines=-$n_elide",
106 {IN=>$input}, {OUT=>$output}];
109 # Insert the ---presume-input-pipe option.
111 $u[1] .= ' ---presume-input-pipe';
117 my $save_temps = $ENV{DEBUG};
118 my $verbose = $ENV{VERBOSE};
120 my $prog = $ENV{PROG} || die "$0: \$PROG not specified in environment\n";
121 my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);