1#!/usr/bin/perl
2
3# Copyright (C) 2008-2023 Free Software Foundation, Inc.
4
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14
15# You should have received a copy of the GNU General Public License
16# along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
18use strict;
19
20my $prog = 'tr';
21
22# Turn off localization of executable's output.
23@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
24
25my $map_all_to_1 =
26  "$prog: when translating with complemented character classes,\n"
27    . "string2 must map all characters in the domain to one\n";
28
29my @Tests =
30(
31  ['1', qw(abcd '[]*]'),   {IN=>'abcd'}, {OUT=>']]]]'}],
32  ['2', qw(abc '[%*]xyz'), {IN=>'abc'}, {OUT=>'xyz'}],
33  ['3', qw('' '[.*]'),     {IN=>'abc'}, {OUT=>'abc'}],
34
35  # Test --truncate-set1 behavior when string1 is longer than string2
36  ['4', qw(-t abcd xy), {IN=>'abcde'}, {OUT=>'xycde'}],
37  # Test bsd behavior (the default) when string1 is longer than string2
38  ['5', qw(abcd xy), {IN=>'abcde'}, {OUT=>'xyyye'}],
39  # Do it the posix way
40  ['6', qw(abcd 'x[y*]'), {IN=>'abcde'}, {OUT=>'xyyye'}],
41  ['7', qw(-s a-p '%[.*]$'), {IN=>'abcdefghijklmnop'}, {OUT=>'%.$'}],
42  ['8', qw(-s a-p '[.*]$'), {IN=>'abcdefghijklmnop'}, {OUT=>'.$'}],
43  ['9', qw(-s a-p '%[.*]'), {IN=>'abcdefghijklmnop'}, {OUT=>'%.'}],
44  ['a', qw(-s '[a-z]'), {IN=>'aabbcc'}, {OUT=>'abc'}],
45  ['b', qw(-s '[a-c]'), {IN=>'aabbcc'}, {OUT=>'abc'}],
46  ['c', qw(-s '[a-b]'), {IN=>'aabbcc'}, {OUT=>'abcc'}],
47  ['d', qw(-s '[b-c]'), {IN=>'aabbcc'}, {OUT=>'aabc'}],
48  ['e', qw(-s '[\0-\5]'),
49   {IN=>"\0\0a\1\1b\2\2\2c\3\3\3d\4\4\4\4e\5\5"}, {OUT=>"\0a\1b\2c\3d\4e\5"}],
50  # tests of delete
51  ['f', qw(-d '[=[=]'), {IN=>'[[[[[[[]]]]]]]]'}, {OUT=>']]]]]]]]'}],
52  ['g', qw(-d '[=]=]'), {IN=>'[[[[[[[]]]]]]]]'}, {OUT=>'[[[[[[['}],
53  ['h', qw(-d '[:xdigit:]'), {IN=>'0123456789acbdefABCDEF'}, {OUT=>''}],
54  ['i', qw(-d '[:xdigit:]'), {IN=>'w0x1y2z3456789acbdefABCDEFz'},
55   {OUT=>'wxyzz'}],
56  ['j', qw(-d '[:digit:]'), {IN=>'0123456789'}, {OUT=>''}],
57  ['k', qw(-d '[:digit:]'),
58   {IN=>'a0b1c2d3e4f5g6h7i8j9k'}, {OUT=>'abcdefghijk'}],
59  ['l', qw(-d '[:lower:]'), {IN=>'abcdefghijklmnopqrstuvwxyz'}, {OUT=>''}],
60  ['m', qw(-d '[:upper:]'), {IN=>'ABCDEFGHIJKLMNOPQRSTUVWXYZ'}, {OUT=>''}],
61  ['n', qw(-d '[:lower:][:upper:]'),
62   {IN=>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'}, {OUT=>''}],
63  ['o', qw(-d '[:alpha:]'),
64   {IN=>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'}, {OUT=>''}],
65  ['p', qw(-d '[:alnum:]'),
66   {IN=>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'},
67   {OUT=>''}],
68  ['q', qw(-d '[:alnum:]'),
69   {IN=>'.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.'},
70   {OUT=>'..'}],
71  ['r', qw(-ds '[:alnum:]' .),
72   {IN=>'.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.'},
73   {OUT=>'.'}],
74
75  # The classic example, with string2 BSD-style
76  ['s', qw(-cs '[:alnum:]' '\n'),
77   {IN=>'The big black fox jumped over the fence.'},
78   {OUT=>"The\nbig\nblack\nfox\njumped\nover\nthe\nfence\n"}],
79
80  # The classic example, POSIX-style
81  ['t', qw(-cs '[:alnum:]' '[\n*]'),
82   {IN=>'The big black fox jumped over the fence.'},
83   {OUT=>"The\nbig\nblack\nfox\njumped\nover\nthe\nfence\n"}],
84  ['u', qw(-ds b a), {IN=>'aabbaa'}, {OUT=>'a'}],
85  ['v', qw(-ds '[:xdigit:]' Z), {IN=>'ZZ0123456789acbdefABCDEFZZ'}, {OUT=>'Z'}],
86
87  # Try some data with 8th bit set in case something is mistakenly
88  # sign-extended.
89  ['w', qw(-ds '\350' '\345'),
90   {IN=>"\300\301\377\345\345\350\345"},
91   {OUT=>"\300\301\377\345"}],
92  ['x', qw(-s abcdefghijklmn '[:*016]'),
93   {IN=>'abcdefghijklmnop'}, {OUT=>':op'}],
94  ['y', qw(-d a-z), {IN=>'abc $code'}, {OUT=>' $'}],
95  ['z', qw(-ds a-z '$.'), {IN=>'a.b.c $$$$code\\'}, {OUT=>'. $\\'}],
96
97  # Make sure that a-a is accepted.
98  ['range-a-a', qw(a-a z), {IN=>'abc'}, {OUT=>'zbc'}],
99  #
100  ['null', qw(a ''), {IN=>''}, {OUT=>''}, {EXIT=>1},
101   {ERR=>"$prog: when not truncating set1, string2 must be non-empty\n"}],
102  ['upcase', qw('[:lower:]' '[:upper:]'),
103   {IN=>'abcxyzABCXYZ'},
104   {OUT=>'ABCXYZABCXYZ'}],
105  ['dncase', qw('[:upper:]' '[:lower:]'),
106   {IN=>'abcxyzABCXYZ'},
107   {OUT=>'abcxyzabcxyz'}],
108  #
109  ['rep-cclass', qw('a[=*2][=c=]' xyyz), {IN=>'a=c'}, {OUT=>'xyz'}],
110  ['rep-1', qw('[:*3][:digit:]' a-m), {IN=>':1239'}, {OUT=>'cefgm'}],
111  ['rep-2', qw('a[b*512]c' '1[x*]2'), {IN=>'abc'}, {OUT=>'1x2'}],
112  ['rep-3', qw('a[b*513]c' '1[x*]2'), {IN=>'abc'}, {OUT=>'1x2'}],
113  # Another couple octal repeat count tests.
114  ['o-rep-1', qw('[b*08]' '[x*]'), {IN=>''}, {OUT=>''}, {EXIT=>1},
115   {ERR=>"$prog: invalid repeat count '08' in [c*n] construct\n"}],
116  ['o-rep-2', qw('[b*010]cd' '[a*7]BC[x*]'), {IN=>'bcd'}, {OUT=>'BCx'}],
117
118  ['esc', qw('a\-z' A-Z), {IN=>'abc-z'}, {OUT=>'AbcBC'}],
119  ['bs-055', qw('a\055b' def), {IN=>"a\055b"}, {OUT=>'def'}],
120  ['bs-at-end', qw('\\' x), {IN=>"\\"}, {OUT=>'x'},
121   {ERR=>"$prog: warning: an unescaped backslash at end of "
122    . "string is not portable\n"}],
123
124  #
125  # From Ross
126  ['ross-0a', qw(-cs '[:upper:]' 'X[Y*]'), {IN=>''}, {OUT=>''}, {EXIT=>1},
127   {ERR=>$map_all_to_1}],
128  ['ross-0b', qw(-cs '[:cntrl:]' 'X[Y*]'), {IN=>''}, {OUT=>''}, {EXIT=>1},
129   {ERR=>$map_all_to_1}],
130  ['ross-1a', qw(-cs '[:upper:]' '[X*]'),
131   {IN=>'AMZamz123.-+AMZ'}, {OUT=>'AMZXAMZ'}],
132  ['ross-1b', qw(-cs '[:upper:][:digit:]' '[Z*]'), {IN=>''}, {OUT=>''}],
133  ['ross-2', qw(-dcs '[:lower:]' n-rs-z),
134   {IN=>'amzAMZ123.-+amz'}, {OUT=>'amzamz'}],
135  ['ross-3', qw(-ds '[:xdigit:]' '[:alnum:]'),
136   {IN=>'.ZABCDEFGzabcdefg.0123456788899.GG'}, {OUT=>'.ZGzg..G'}],
137  ['ross-4', qw(-dcs '[:alnum:]' '[:digit:]'), {IN=>''}, {OUT=>''}],
138  ['ross-5', qw(-dc '[:lower:]'), {IN=>''}, {OUT=>''}],
139  ['ross-6', qw(-dc '[:upper:]'), {IN=>''}, {OUT=>''}],
140
141  # Ensure that these fail.
142  # Prior to 2.0.20, each would evoke a failed assertion.
143  ['empty-eq', qw('[==]' x), {IN=>''}, {OUT=>''}, {EXIT=>1},
144   {ERR=>"$prog: missing equivalence class character '[==]'\n"}],
145  ['empty-cc', qw('[::]' x), {IN=>''}, {OUT=>''}, {EXIT=>1},
146   {ERR=>"$prog: missing character class name '[::]'\n"}],
147
148  # Weird repeat counts.
149  ['repeat-bs-9', qw(abc '[b*\9]'), {IN=>'abcd'}, {OUT=>'[b*d'}],
150  ['repeat-0', qw(abc '[b*0]'), {IN=>'abcd'}, {OUT=>'bbbd'}],
151  ['repeat-zeros', qw(abc '[b*00000000000000000000]'),
152   {IN=>'abcd'}, {OUT=>'bbbd'}],
153  ['repeat-compl', qw(-c '[a*65536]\n' '[b*]'), {IN=>'abcd'}, {OUT=>'abbb'}],
154  ['repeat-xC', qw(-C '[a*65536]\n' '[b*]'), {IN=>'abcd'}, {OUT=>'abbb'}],
155
156  # From Glenn Fowler.
157  ['fowler-1', qw(ah -H), {IN=>'aha'}, {OUT=>'-H-'}],
158
159  # Up to coreutils-6.9, this would provoke a failed assertion.
160  ['no-abort-1', qw(-c a '[b*256]'), {IN=>'abc'}, {OUT=>'abb'}],
161);
162
163@Tests = triple_test \@Tests;
164
165# tr takes its input only from stdin, not from a file argument, so
166# remove the tests that provide file arguments and keep only the ones
167# generated by triple_test (identifiable by their .r and .p suffixes).
168@Tests = grep {$_->[0] =~ /\.[pr]$/} @Tests;
169
170my $save_temps = $ENV{DEBUG};
171my $verbose = $ENV{VERBOSE};
172
173my $fail = run_tests ($prog, $prog, \@Tests, $save_temps, $verbose);
174exit $fail;
175