ee5fca05531d49407da767135272805665635b45
[platform/upstream/coreutils.git] / tests / tail / Test.pm
1 package Test;
2 require 5.002;
3 use strict;
4
5 my @tv = (
6 # test name, options, input, expected output, expected return code
7 #
8 ['obs-plus-c1', '+2c', 'abcd', 'bcd', 0],
9 ['obs-plus-c2', '+8c', 'abcd', '', 0],
10 ['obs-c3', '-1c', 'abcd', 'd', 0],
11 ['obs-c4', '-9c', 'abcd', 'abcd', 0],
12 ['obs-c5', '-12c', 'x' . ('y' x 12) . 'z', ('y' x 11) . 'z', 0],
13
14
15 ['obs-l1', '-1l', 'x', 'x', 0],
16 ['obs-l2', '-1l', "x\ny\n", "y\n", 0],
17 ['obs-l3', '-1l', "x\ny", "y", 0],
18 ['obs-plus-l4', '+1l', "x\ny\n", "x\ny\n", 0],
19 ['obs-plus-l5', '+2l', "x\ny\n", "y\n", 0],
20
21 # Same as -l tests, but without the `l'.
22 ['obs-1', '-1', 'x', 'x', 0],
23 ['obs-2', '-1', "x\ny\n", "y\n", 0],
24 ['obs-3', '-1', "x\ny", "y", 0],
25 ['obs-plus-4', '+1', "x\ny\n", "x\ny\n", 0],
26 ['obs-plus-5', '+2', "x\ny\n", "y\n", 0],
27
28 # This is equivalent to +10c
29 ['obs-plus-x1', '+c', 'x' . ('y' x 10) . 'z', 'yyz', 0],
30 # This is equivalent to +10l
31 ['obs-plus-x2', '+l', "x\n" . ("y\n" x 10) . 'z', "y\ny\nz", 0],
32 # With no number, this is like -10l
33 ['obs-l', '-l', "x\n" . ("y\n" x 10) . 'z', ("y\n" x 9) . 'z', 0],
34
35 ['obs-b', '-b', "x\n" x (512 * 10 / 2 + 1), "x\n" x (512 * 10 / 2), 0],
36
37 # This should get
38 # `tail: cannot open `+cl' for reading: No such file or directory'
39 ['err-1', '+cl', '', '', 1],
40
41 # This should get `tail: l: invalid number of bytes'
42 ['err-2', '-cl', '', '', 1],
43
44 # This should get
45 # `tail: cannot open `+2cz' for reading: No such file or directory'
46 ['err-3', '+2cz', '', '', 1],
47
48 # This should get `tail: invalid option -- 2'
49 ['err-4', '-2cX', '', '', 1],
50
51 # Since the number is larger than 2^64, this should provoke
52 # the diagnostic: `tail: 99999999999999999999: invalid number of bytes'
53 # on all systems... probably, for now, maybe.
54 ['err-5', '-c99999999999999999999', '', '', 1],
55 ['err-6', '-c', '', '', 1],
56
57 # Same as -n 10
58 ['minus-1', '-', '', '', 0],
59 ['minus-2', '-', "x\n" . ("y\n" x 10) . 'z', ("y\n" x 9) . 'z', 0],
60
61 ['c-2', '-c 2', "abcd\n", "d\n", 0],
62 ['c-2-minus', '-c 2 --', "abcd\n", "d\n", 0],
63 ['c2', '-c2', "abcd\n", "d\n", 0],
64 ['c2-minus', '-c2 --', "abcd\n", "d\n", 0],
65
66 ['n-1', '-n 10', "x\n" . ("y\n" x 10) . 'z', ("y\n" x 9) . 'z', 0],
67 ['n-2', '-n -10', "x\n" . ("y\n" x 10) . 'z', ("y\n" x 9) . 'z', 0],
68 ['n-3', '-n +10', "x\n" . ("y\n" x 10) . 'z', "y\ny\nz", 0],
69
70 # Accept +0 as synonym for +1.
71 ['n-4',  '-n +0', "y\n" x 5, "y\n" x 5, 0],
72 ['n-4a', '-n +1', "y\n" x 5, "y\n" x 5, 0],
73
74 # Note that -0 is *not* a synonym for -1.
75 ['n-5',  '-n -0', "y\n" x 5, '', 0],
76 ['n-5a', '-n -1', "y\n" x 5, "y\n", 0],
77 ['n-5b', '-n  0', "y\n" x 5, '', 0],
78
79 # With textutils-1.22, this failed.
80 ['f-pipe-1', '-f -n 1', "a\nb\n", "b\n", 0],
81 );
82
83 sub test_vector
84 {
85   my $t;
86   foreach $t (@tv)
87     {
88       my ($test_name, $flags, $in, $exp, $ret) = @$t;
89
90       if ($test_name =~ /^(obs-plus-|minus-)/)
91         {
92           $Test::env{$test_name} = ['_POSIX2_VERSION=199209'];
93         }
94       if ($test_name =~ /^(err-6|c-2)$/)
95         {
96           $Test::env{$test_name} = ['_POSIX2_VERSION=200112'];
97         }
98       if ($test_name =~ /^f-pipe-/)
99         {
100           $Test::env{$test_name} = ['POSIXLY_CORRECT=1'];
101         }
102
103       # If you run the minus* tests with a FILE arg they'd hang.
104       # If you run the err-1 or err-3 tests with a FILE, they'd misinterpret
105       # the arg unless we are using the obsolete form.
106       if ($test_name =~ /^(minus|err-[13])/)
107         {
108           $Test::input_via{$test_name} = {REDIR => 0, PIPE => 0};
109         }
110       elsif ($test_name =~ /^f-/)
111         {
112           # Using redirection or a file would make this hang.
113           $Test::input_via{$test_name} = {PIPE => 0};
114         }
115       else
116         {
117           $Test::input_via{$test_name} = {REDIR => 0, FILE => 0, PIPE => 0}
118         }
119     }
120
121   return @tv;
122 }
123
124 1;