b7a49a2cbe75409753b129b89b828284d7b4465d
[platform/upstream/coreutils.git] / tests / misc / head-elide-tail
1 #!/bin/sh
2 # -*- perl -*-
3 # Exercise head's --bytes=-N option.
4
5 : ${PERL=perl}
6 : ${srcdir=.}
7
8 $PERL -e 1 > /dev/null 2>&1 || {
9   echo 1>&2 "$0: configure didn't find a usable version of Perl," \
10     "so can't run this test"
11   exit 77
12 }
13
14 exec $PERL -w -I$srcdir/.. -MCoreutils -- - <<\EOF
15 #/
16 require 5.003;
17 use strict;
18
19 (my $program_name = $0) =~ s|.*/||;
20
21 $ENV{PROG} = 'head';
22
23 # Turn off localization of executable's ouput.
24 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
25
26 # This should match the definition in head.c.
27 my $READ_BUFSIZE = 4096;
28
29 my @Tests =
30   (
31    # Elide the exact size of the file.
32    ['elide-b1', "--bytes=-2", {IN=>"a\n"}, {OUT=>''}],
33    # Elide more than the size of the file.
34    ['elide-b2', "--bytes=-2", {IN=>"a"},   {OUT=>''}],
35    # Leave just one byte.
36    ['elide-b3', "--bytes=-2", {IN=>"abc"}, {OUT=>'a'}],
37    # Make it so the elided bytes straddle the end of the first
38    # $READ_BUFSIZE block.
39    ['elide-b4', "--bytes=-2",
40     {IN=> 'a' x ($READ_BUFSIZE-3) . "\nbcd"},
41     {OUT=>'a' x ($READ_BUFSIZE-3) . "\nb"}],
42    # Make it so the elided bytes straddle the end of the 2nd
43    # $READ_BUFSIZE block.
44    ['elide-b5', "--bytes=-2",
45     {IN=> 'a' x (2 * $READ_BUFSIZE - 2) . 'bcd'},
46     {OUT=>'a' x (2 * $READ_BUFSIZE - 2) . 'b'}],
47
48    ['elide-l0', "--lines=-1", {IN=>''}, {OUT=>''}],
49    ['elide-l1', "--lines=-1", {IN=>"a\n"}, {OUT=>''}],
50    ['elide-l2', "--lines=-1", {IN=>"a"}, {OUT=>''}],
51    ['elide-l3', "--lines=-1", {IN=>"a\nb"}, {OUT=>"a\n"}],
52    ['elide-l4', "--lines=-1", {IN=>"a\nb\n"}, {OUT=>"a\n"}],
53   );
54
55 if ($ENV{RUN_EXPENSIVE_TESTS})
56   {
57     # Brute force: use all combinations of file sizes [0..20] and
58     # number of bytes to elide [0..20].  For better coverage, recompile
59     # head with -DHEAD_TAIL_PIPE_READ_BUFSIZE=4 and
60     # -DHEAD_TAIL_PIPE_BYTECOUNT_THRESHOLD=8
61     my $s = "abcdefghijklmnopqrst";
62     for my $file_size (0..20)
63       {
64         for my $n_elide (0..20)
65           {
66             my $input = substr $s, 0, $file_size;
67             my $out_len = $n_elide < $file_size ? $file_size - $n_elide : 0;
68             my $output = substr $input, 0, $out_len;
69             my $t = ["elideb$file_size-$n_elide", "--bytes=-$n_elide",
70                      {IN=>$input}, {OUT=>$output}];
71             push @Tests, $t;
72             my @u = @$t;
73             # Insert the ---presume-input-pipe option.
74             $u[0] .= 'p';
75             $u[1] .= ' ---presume-input-pipe';
76             push @Tests, \@u;
77           }
78       }
79
80     $s =~ s/(.)/$1\n/g;
81     for my $file_size (0..20)
82       {
83         for my $n_elide (0..20)
84           {
85             my $input = substr $s, 0, 2 * $file_size;
86             my $out_len = $n_elide < $file_size ? $file_size - $n_elide : 0;
87             my $output = substr $input, 0, 2 * $out_len;
88             my $t = ["elidel$file_size-$n_elide", "--lines=-$n_elide",
89                      {IN=>$input}, {OUT=>$output}];
90             push @Tests, $t;
91             my @u = @$t;
92             # Insert the ---presume-input-pipe option.
93             $u[0] .= 'p';
94             $u[1] .= ' ---presume-input-pipe';
95             push @Tests, \@u;
96           }
97       }
98   }
99
100 my $save_temps = $ENV{DEBUG};
101 my $verbose = $ENV{VERBOSE};
102
103 my $prog = $ENV{PROG} || die "$0: \$PROG not specified in environment\n";
104 my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
105 exit $fail;
106 EOF