Tizen 2.0 Release
[external/tizen-coreutils.git] / tests / misc / head-elide-tail
1 #!/bin/sh
2 # -*- perl -*-
3 # Exercise head's --bytes=-N option.
4
5 # Copyright (C) 2003, 2005 Free Software Foundation, Inc.
6
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.
11
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.
16
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
20 # 02110-1301, USA.
21
22 : ${PERL=perl}
23 : ${srcdir=.}
24
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"
28   exit 77
29 }
30
31 exec $PERL -w -I$srcdir/.. -MCoreutils -- - <<\EOF
32 #/
33 require 5.003;
34 use strict;
35
36 (my $program_name = $0) =~ s|.*/||;
37
38 $ENV{PROG} = 'head';
39
40 # Turn off localization of executable's ouput.
41 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
42
43 # This should match the definition in head.c.
44 my $READ_BUFSIZE = 4096;
45
46 my @Tests =
47   (
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'}],
64
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"}],
70   );
71
72 if ($ENV{RUN_EXPENSIVE_TESTS})
73   {
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)
80       {
81         for my $n_elide (0..20)
82           {
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}];
88             push @Tests, $t;
89             my @u = @$t;
90             # Insert the ---presume-input-pipe option.
91             $u[0] .= 'p';
92             $u[1] .= ' ---presume-input-pipe';
93             push @Tests, \@u;
94           }
95       }
96
97     $s =~ s/(.)/$1\n/g;
98     for my $file_size (0..20)
99       {
100         for my $n_elide (0..20)
101           {
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}];
107             push @Tests, $t;
108             my @u = @$t;
109             # Insert the ---presume-input-pipe option.
110             $u[0] .= 'p';
111             $u[1] .= ' ---presume-input-pipe';
112             push @Tests, \@u;
113           }
114       }
115   }
116
117 my $save_temps = $ENV{DEBUG};
118 my $verbose = $ENV{VERBOSE};
119
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);
122 exit $fail;
123 EOF