1#!/usr/bin/perl
2# Exercise head's --bytes=-N option.
3
4# Copyright (C) 2003-2023 Free Software Foundation, Inc.
5
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.
10
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.
15
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/>.
18
19use strict;
20
21(my $program_name = $0) =~ s|.*/||;
22
23$ENV{PROG} = 'head';
24
25# Turn off localization of executable's output.
26@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
27
28# This should match the definition in head.c.
29my $READ_BUFSIZE = 8192;
30
31my @Tests =
32  (
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'}],
49
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"}],
57  );
58
59if ($ENV{RUN_EXPENSIVE_TESTS})
60  {
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)
67      {
68        for my $n_elide (0..20)
69          {
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;
81          }
82      }
83
84    $s =~ s/(.)/$1\n/g;
85    $s .= 'u'; # test without trailing '\n'
86    for my $file_size (0..21)
87      {
88        for my $n_elide (0..21)
89          {
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;
101          }
102      }
103  }
104
105my $save_temps = $ENV{DEBUG};
106my $verbose = $ENV{VERBOSE};
107
108my $prog = $ENV{PROG} || die "$0: \$PROG not specified in environment\n";
109my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
110exit $fail;
111