cc93c1c11cd0786103e1c82e53dba105c5fcfc2c
[platform/upstream/coreutils.git] / tests / cut / Test.pm
1 package Test;
2 require 5.002;
3 use strict;
4
5 $Test::input_via_default = {REDIR => 0, FILE => 0, PIPE => 0};
6
7 my @tv = (
8 # test flags            input           expected output expected return code
9 #
10 ['1', '-d: -f1,3-',     "a:b:c\n",              "a:c\n",                0],
11 ['2', '-d: -f1,3-',     "a:b:c\n",              "a:c\n",                0],
12 ['3', '-d: -f2-',       "a:b:c\n",              "b:c\n",                0],
13 ['4', '-d: -f4',        "a:b:c\n",              "\n",                   0],
14 ['5', '-d: -f4',        "",                     "",                     0],
15 ['6', '-c4',            "123\n",                "\n",                   0],
16 ['7', '-c4',            "123",                  "\n",                   0],
17 ['8', '-c4',            "123\n1",               "\n\n",                 0],
18 ['9', '-c4',            "",                     "",                     0],
19 ['a', '-s -d: -f3-',    "a:b:c\n",              "c\n",                  0],
20 ['b', '-s -d: -f2,3',   "a:b:c\n",              "b:c\n",                0],
21 ['c', '-s -d: -f1,3',   "a:b:c\n",              "a:c\n",                0],
22 # Trailing colon should not be output
23 ['d', '-s -d: -f1,3',   "a:b:c:\n",             "a:c\n",                0],
24 ['e', '-s -d: -f3-',    "a:b:c:\n",             "c:\n",                 0],
25 ['f', '-s -d: -f3-4',   "a:b:c:\n",             "c:\n",                 0],
26 ['g', '-s -d: -f3,4',   "a:b:c:\n",             "c:\n",                 0],
27 # Make sure -s suppresses non-delimited lines
28 ['h', '-s -d: -f2,3',   "abc\n",                "",                     0],
29 #
30 ['i', '-d: -f1-3',      ":::\n",                "::\n",                 0],
31 ['j', '-d: -f1-4',      ":::\n",                ":::\n",                0],
32 ['k', '-d: -f2-3',      ":::\n",                ":\n",                  0],
33 ['l', '-d: -f2-4',      ":::\n",                "::\n",                 0],
34 ['m', '-s -d: -f1-3',   ":::\n",                "::\n",                 0],
35 ['n', '-s -d: -f1-4',   ":::\n",                ":::\n",                0],
36 ['o', '-s -d: -f2-3',   ":::\n",                ":\n",                  0],
37 ['p', '-s -d: -f2-4',   ":::\n",                "::\n",                 0],
38 ['q', '-s -d: -f2-4',   ":::\n:\n",             "::\n\n",               0],
39 ['r', '-s -d: -f2-4',   ":::\n:1\n",            "::\n1\n",              0],
40 ['s', '-s -d: -f1-4',   ":::\n:a\n",            ":::\n:a\n",            0],
41 ['t', '-s -d: -f3-',    ":::\n:1\n",            ":\n\n",                0],
42 # Make sure it handles empty input properly, with and without -s.
43 ['u', '-s -f3-',        "",                     "",                     0],
44 ['v', '-f3-',           "",                     "",                     0],
45 # Make sure it handles empty input properly.
46 ['w', '-b 1',           "",                     "",                     0],
47 ['x', '-s -d: -f2-4',   ":\n",                  "\n",                   0],
48 # Errors
49 # -s may be used only with -f
50 ['y', '-s -b4',         ":\n",                  "",                     1],
51 # You must specify bytes or fields (or chars)
52 ['z', '',               ":\n",                  "",                     1],
53 # Empty field list
54 ['empty-fl', '-f \'\'', ":\n",                  "",                     1],
55 # Missing field list
56 ['missing-fl', '-f',    ":\n",                  "",                     1],
57 # Empty byte list
58 ['empty-bl', '-b \'\'', ":\n",                  "",                     1],
59 # Missing byte list
60 ['missing-bl', '-b',    ":\n",                  "",                     1],
61
62 # This test fails with cut from textutils-1.22.
63 ['empty-f1', '-f1',     "",                     "",                     0],
64
65 ['empty-f2', '-f2',     "",                     "",                     0],
66
67 ['o-delim', '-d: -f2,3 --out=_', "a:b:c\n",     "b_c\n",                0],
68 ['nul-idelim', "-d '' -f2,3 --out=_", "a\0b\0c\n", "b_c\n",             0],
69 ['nul-odelim', "-d: -f2,3 --out=", "a:b:c\n",   "b\0c\n",               0],
70 ['multichar-od', "-d: -f2,3 --out=_._", "a:b:c\n", "b_._c\n",           0],
71
72 # Prior to 1.22i, you couldn't use a delimiter that would sign-extend.
73 ['8bit-delim', "'-d\255' -f2,3 --out=_", "a\255b\255c\n", "b_c\n",      0],
74
75 # New functionality:
76 ['out-delim1', '-c1-3,5- --output-d=:', "abcdefg\n", "abc:efg\n",       0],
77 # A totally overlapped field shouldn't change anything:
78 ['out-delim2', '-c1-3,2,5- --output-d=:', "abcdefg\n", "abc:efg\n",     0],
79 # Partial overlap: index `2' is not at the start of a range.
80 ['out-delim3', '-c1-3,2-4,6 --output-d=:', "abcdefg\n", "abcd:f\n",     0],
81 ['out-delim3a', '-c1-3,2-4,6- --output-d=:', "abcdefg\n", "abcd:fg\n",  0],
82 # Ensure that the following two commands produce the same output.
83 # Before an off-by-one fix, the output from the former would not contain a `:'.
84 ['out-delim4', '-c4-,2-3 --output-d=:', "abcdefg\n", "bc:defg\n",       0],
85 ['out-delim5', '-c2-3,4- --output-d=:', "abcdefg\n", "bc:defg\n",       0],
86 # This test would fail for cut from coreutils-5.0.1 and earlier.
87 ['out-delim6', '-c2,1-3 --output-d=:', "abc\n", "abc\n",        0],
88 #
89 ['od-abut',    '-b1-2,3-4 --output-d=:', "abcd\n", "ab:cd\n",   0],
90 ['od-overlap', '-b1-2,2   --output-d=:', "abc\n",  "ab\n",      0],
91 ['od-overlap2', '-b1-2,2- --output-d=:', "abc\n",  "abc\n",     0],
92 ['od-overlap3', '-b1-3,2- --output-d=:', "abcd\n",  "abcd\n",   0],
93 ['od-overlap4', '-b1-3,2-3 --output-d=:', "abcd\n",  "abc\n",   0],
94 ['od-overlap5', '-b1-3,1-4 --output-d=:', "abcde\n",  "abcd\n", 0],
95
96 );
97
98 # Don't use a pipe for failing tests.  Otherwise, sometimes they
99 # fail so early they'd evoke the `Broken pipe' message.
100 my $t;
101 foreach $t (@tv)
102   {
103     my ($test_name, $flags, $in, $exp, $ret) = @$t;
104     $Test::input_via{$test_name} = {REDIR => 0, FILE => 0} if $ret;
105   }
106
107 sub test_vector
108 {
109   return @tv;
110 }
111
112 1;