xref: /freebsd/contrib/netbsd-tests/usr.bin/tr/t_basic.sh (revision 9268022b74279434ed6300244e3f977e56a8ceb5)
1*57718be8SEnji Cooper# $NetBSD: t_basic.sh,v 1.3 2013/08/11 01:50:02 dholland Exp $
2*57718be8SEnji Cooper#
3*57718be8SEnji Cooper# Copyright (c) 2013 The NetBSD Foundation, Inc.
4*57718be8SEnji Cooper# All rights reserved.
5*57718be8SEnji Cooper#
6*57718be8SEnji Cooper# This code is derived from software contributed to The NetBSD Foundation
7*57718be8SEnji Cooper# by David A. Holland.
8*57718be8SEnji Cooper#
9*57718be8SEnji Cooper# Redistribution and use in source and binary forms, with or without
10*57718be8SEnji Cooper# modification, are permitted provided that the following conditions
11*57718be8SEnji Cooper# are met:
12*57718be8SEnji Cooper# 1. Redistributions of source code must retain the above copyright
13*57718be8SEnji Cooper#    notice, this list of conditions and the following disclaimer.
14*57718be8SEnji Cooper# 2. Redistributions in binary form must reproduce the above copyright
15*57718be8SEnji Cooper#    notice, this list of conditions and the following disclaimer in the
16*57718be8SEnji Cooper#    documentation and/or other materials provided with the distribution.
17*57718be8SEnji Cooper#
18*57718be8SEnji Cooper# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19*57718be8SEnji Cooper# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20*57718be8SEnji Cooper# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21*57718be8SEnji Cooper# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22*57718be8SEnji Cooper# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*57718be8SEnji Cooper# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*57718be8SEnji Cooper# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*57718be8SEnji Cooper# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*57718be8SEnji Cooper# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*57718be8SEnji Cooper# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*57718be8SEnji Cooper# POSSIBILITY OF SUCH DAMAGE.
29*57718be8SEnji Cooper#
30*57718be8SEnji Cooper
31*57718be8SEnji Cooper#
32*57718be8SEnji Cooper# tr -d: delete character
33*57718be8SEnji Cooper#
34*57718be8SEnji Cooperatf_test_case dopt
35*57718be8SEnji Cooperdopt_head() {
36*57718be8SEnji Cooper	atf_set "descr" "Tests for tr -d"
37*57718be8SEnji Cooper}
38*57718be8SEnji Cooper
39*57718be8SEnji Cooperdopt_body() {
40*57718be8SEnji Cooper	atf_check -o inline:'abcde\n' -x 'echo abcde | tr -d x'
41*57718be8SEnji Cooper	atf_check -o inline:'abde\n' -x 'echo abcde | tr -d c'
42*57718be8SEnji Cooper	atf_check -o inline:'ace\n' -x 'echo abcde | tr -d bd'
43*57718be8SEnji Cooper	atf_check -o inline:'ae\n' -x 'echo abcde | tr -d b-d'
44*57718be8SEnji Cooper	atf_check -o inline:'b\n' -x 'echo abcde | tr -d ac-e'
45*57718be8SEnji Cooper	atf_check -o inline:'d\n' -x 'echo abcde | tr -d a-ce'
46*57718be8SEnji Cooper	atf_check -o inline:'aei\n' -x 'echo abcdefghi | tr -d b-df-h'
47*57718be8SEnji Cooper
48*57718be8SEnji Cooper	atf_check -o inline:'' -x 'echo abcde | tr -c -d x'
49*57718be8SEnji Cooper	atf_check -o inline:'c' -x 'echo abcde | tr -c -d c'
50*57718be8SEnji Cooper	atf_check -o inline:'bd' -x 'echo abcde | tr -c -d bd'
51*57718be8SEnji Cooper	atf_check -o inline:'bcd' -x 'echo abcde | tr -c -d b-d'
52*57718be8SEnji Cooper	atf_check -o inline:'acde' -x 'echo abcde | tr -c -d ac-e'
53*57718be8SEnji Cooper	atf_check -o inline:'abce' -x 'echo abcde | tr -c -d a-ce'
54*57718be8SEnji Cooper	atf_check -o inline:'bcdfgh' -x 'echo abcdefghi | tr -c -d b-df-h'
55*57718be8SEnji Cooper
56*57718be8SEnji Cooper	# see if escape codes work
57*57718be8SEnji Cooper	atf_check -o inline:'splice' -x '(echo spl; echo ice) | tr -d '"'\n'"
58*57718be8SEnji Cooper	atf_check -o inline:'splice' -x '(echo spl; echo ice) | tr -d '"'\012'"
59*57718be8SEnji Cooper
60*57718be8SEnji Cooper	# see if escape codes work when followed by other things
61*57718be8SEnji Cooper	atf_check -o inline:'slice' -x '(echo spl; echo ice) | tr -d '"'\n'p"
62*57718be8SEnji Cooper	atf_check -o inline:'slice' -x '(echo spl; echo ice) | tr -d '"'\012'p"
63*57718be8SEnji Cooper
64*57718be8SEnji Cooper	# see if the [=x=] syntax works
65*57718be8SEnji Cooper	atf_check -o inline:'abde\n' -x 'echo abcde | tr -d '"'[=c=]'"
66*57718be8SEnji Cooper	atf_check -o inline:'bde\n' -x 'echo abcde | tr -d '"'[=c=]'a"
67*57718be8SEnji Cooper
68*57718be8SEnji Cooper	# make sure 0 works
69*57718be8SEnji Cooper	# (ignore stderr as dd blabbers to it)
70*57718be8SEnji Cooper	atf_check -e ignore -o inline:'ab\n' \
71*57718be8SEnji Cooper	  -x '(echo -n a; dd if=/dev/zero bs=3 count=1; echo b) | tr -d '"'\0'"
72*57718be8SEnji Cooper
73*57718be8SEnji Cooper	# test posix classes
74*57718be8SEnji Cooper	atf_check -o inline:'.\n' -x 'echo aAzZ.123 | tr -d '"'[:alnum:]'"
75*57718be8SEnji Cooper	atf_check -o inline:'.123\n' -x 'echo aAzZ.123 | tr -d '"'[:alpha:]'"
76*57718be8SEnji Cooper	atf_check -o inline:'az\n' -x 'echo "a z" | tr -d '"'[:blank:]'"
77*57718be8SEnji Cooper	atf_check -o inline:'az' -x '(echo a; echo z) | tr -d '"'[:cntrl:]'"
78*57718be8SEnji Cooper	atf_check -o inline:'aAzZ.\n' -x 'echo aAzZ.123 | tr -d '"'[:digit:]'"
79*57718be8SEnji Cooper	atf_check -o inline:' \n' -x 'echo "a z.123" | tr -d '"'[:graph:]'"
80*57718be8SEnji Cooper	atf_check -o inline:'AZ.123\n' -x 'echo aAzZ.123 | tr -d '"'[:lower:]'"
81*57718be8SEnji Cooper	atf_check -o inline:'\n' -x 'echo aAzZ.123 | tr -d '"'[:print:]'"
82*57718be8SEnji Cooper	atf_check -o inline:'aAzZ12\n' -x 'echo aAzZ.12 | tr -d '"'[:punct:]'"
83*57718be8SEnji Cooper	atf_check -o inline:'az' -x 'echo "a z" | tr -d '"'[:space:]'"
84*57718be8SEnji Cooper	atf_check -o inline:'az.123\n' -x 'echo aAzZ.123 | tr -d '"'[:upper:]'"
85*57718be8SEnji Cooper	atf_check -o inline:'zZ.\n' -x 'echo aAzZ.123 | tr -d '"'[:xdigit:]'"
86*57718be8SEnji Cooper}
87*57718be8SEnji Cooper
88*57718be8SEnji Cooper#
89*57718be8SEnji Cooper# tr -s: squeeze duplicate character runs
90*57718be8SEnji Cooper#
91*57718be8SEnji Cooperatf_test_case sopt
92*57718be8SEnji Coopersopt_head() {
93*57718be8SEnji Cooper	atf_set "descr" "Tests for tr -s"
94*57718be8SEnji Cooper}
95*57718be8SEnji Cooper
96*57718be8SEnji Coopersopt_body() {
97*57718be8SEnji Cooper	atf_check -o inline:'abcde\n' -x 'echo abcde | tr -s x'
98*57718be8SEnji Cooper	atf_check -o inline:'abcde\n' -x 'echo abcde | tr -s c'
99*57718be8SEnji Cooper	atf_check -o inline:'abcde\n' -x 'echo abccccde | tr -s c'
100*57718be8SEnji Cooper	atf_check -o inline:'abcde\n' -x 'echo abbbcddde | tr -s bd'
101*57718be8SEnji Cooper	atf_check -o inline:'abcde\n' -x 'echo abbbcccddde | tr -s b-d'
102*57718be8SEnji Cooper
103*57718be8SEnji Cooper	atf_check -o inline:'acac\n' -x 'echo acac | tr -s c'
104*57718be8SEnji Cooper	atf_check -o inline:'acac\n' -x 'echo accacc | tr -s c'
105*57718be8SEnji Cooper
106*57718be8SEnji Cooper	atf_check -o inline:'abcde\n' -x 'echo abcde | tr -c -s x'
107*57718be8SEnji Cooper	atf_check -o inline:'abcde\n' -x 'echo abcde | tr -c -s c'
108*57718be8SEnji Cooper	atf_check -o inline:'abcccde\n' -x 'echo abcccde | tr -c -s c'
109*57718be8SEnji Cooper	atf_check -o inline:'abbbcddde\n' -x 'echo abbbcddde | tr -c -s bd'
110*57718be8SEnji Cooper	atf_check -o inline:'abbbccddde\n' -x 'echo abbbccddde | tr -c -s b-d'
111*57718be8SEnji Cooper	atf_check -o inline:'abcccde\n' -x 'echo aaabcccde | tr -c -s b-d'
112*57718be8SEnji Cooper}
113*57718be8SEnji Cooper
114*57718be8SEnji Cooper#
115*57718be8SEnji Cooper# tr -ds: both -d and -s at once
116*57718be8SEnji Cooper#
117*57718be8SEnji Cooperatf_test_case dsopt
118*57718be8SEnji Cooperdsopt_head() {
119*57718be8SEnji Cooper	atf_set "descr" "Tests for tr -ds"
120*57718be8SEnji Cooper}
121*57718be8SEnji Cooper
122*57718be8SEnji Cooperdsopt_body() {
123*57718be8SEnji Cooper	atf_check -o inline:'abcde\n' -x 'echo abcde | tr -ds x y'
124*57718be8SEnji Cooper	atf_check -o inline:'abde\n' -x 'echo abcde | tr -ds c x'
125*57718be8SEnji Cooper	atf_check -o inline:'abcde\n' -x 'echo abcde | tr -ds x c'
126*57718be8SEnji Cooper	atf_check -o inline:'abde\n' -x 'echo abcde | tr -ds c c'
127*57718be8SEnji Cooper	atf_check -o inline:'abde\n' -x 'echo abcccde | tr -ds c x'
128*57718be8SEnji Cooper	atf_check -o inline:'abcde\n' -x 'echo abcccde | tr -ds x c'
129*57718be8SEnji Cooper	atf_check -o inline:'abde\n' -x 'echo abcccde | tr -ds c c'
130*57718be8SEnji Cooper
131*57718be8SEnji Cooper	# -c complements only the first string
132*57718be8SEnji Cooper	atf_check -o inline:'' -x 'echo abcde | tr -c -ds x y'
133*57718be8SEnji Cooper	atf_check -o inline:'c' -x 'echo abcde | tr -c -ds c x'
134*57718be8SEnji Cooper	atf_check -o inline:'' -x 'echo abcde | tr -c -ds x c'
135*57718be8SEnji Cooper	atf_check -o inline:'c' -x 'echo abcde | tr -c -ds c c'
136*57718be8SEnji Cooper	atf_check -o inline:'ccc' -x 'echo abcccde | tr -c -ds c x'
137*57718be8SEnji Cooper	atf_check -o inline:'' -x 'echo abcccde | tr -c -ds x c'
138*57718be8SEnji Cooper	atf_check -o inline:'c' -x 'echo abcccde | tr -c -ds c c'
139*57718be8SEnji Cooper}
140*57718be8SEnji Cooper
141*57718be8SEnji Cooper#
142*57718be8SEnji Cooper# test substitution
143*57718be8SEnji Cooper#
144*57718be8SEnji Cooperatf_test_case subst
145*57718be8SEnji Coopersubst_head() {
146*57718be8SEnji Cooper	atf_set "descr" "Tests for tr substitution"
147*57718be8SEnji Cooper}
148*57718be8SEnji Cooper
149*57718be8SEnji Coopersubst_body() {
150*57718be8SEnji Cooper	atf_check -o inline:'abcde\n' -x 'echo abcde | tr a-c a-c'
151*57718be8SEnji Cooper	atf_check -o inline:'cbade\n' -x 'echo abcde | tr a-c cba'
152*57718be8SEnji Cooper	atf_check -o inline:'abcde\n' -x 'echo abcde | tr a-z a-z'
153*57718be8SEnji Cooper	atf_check -o inline:'bcdef\n' -x 'echo abcde | tr a-z b-za'
154*57718be8SEnji Cooper	atf_check -o inline:'zabcd\n' -x 'echo abcde | tr b-za a-z'
155*57718be8SEnji Cooper	atf_check -o inline:'bbbbb\n' -x 'echo ababa | tr a b'
156*57718be8SEnji Cooper	atf_check -o inline:'furrfu\n' -x 'echo sheesh | tr a-z n-za-m'
157*57718be8SEnji Cooper	atf_check -o inline:'furrfu\n' -x 'echo sheesh | tr n-za-m a-z'
158*57718be8SEnji Cooper
159*57718be8SEnji Cooper	atf_check -o inline:'ABCDE\n' -x 'echo abcde | tr a-z A-Z'
160*57718be8SEnji Cooper	atf_check -o inline:'ABC\n' \
161*57718be8SEnji Cooper	    -x 'echo abc | tr '"'[:lower:]' '[:upper:]'"
162*57718be8SEnji Cooper
163*57718be8SEnji Cooper	# If you don't give enough substitution chars the last is repeated.
164*57718be8SEnji Cooper	atf_check -o inline:'bozoo\n' -x 'echo abcde | tr a-z bozo'
165*57718be8SEnji Cooper	atf_check -o inline:'qaaaa\n' -x 'echo abcde | tr a-z qa'
166*57718be8SEnji Cooper
167*57718be8SEnji Cooper	# You can use -s with substitution.
168*57718be8SEnji Cooper	atf_check -o inline:'cbade\n' -x 'echo abcde | tr -s a-c cba'
169*57718be8SEnji Cooper	atf_check -o inline:'cbaddee\n' -x 'echo aabbccddee | tr -s a-c cba'
170*57718be8SEnji Cooper}
171*57718be8SEnji Cooper
172*57718be8SEnji Cooper#
173*57718be8SEnji Cooper# test substitution with -c (does not currently work)
174*57718be8SEnji Cooper#
175*57718be8SEnji Cooperatf_test_case csubst
176*57718be8SEnji Coopercsubst_head() {
177*57718be8SEnji Cooper	atf_set "descr" "Tests for tr substitution with -c"
178*57718be8SEnji Cooper}
179*57718be8SEnji Cooper
180*57718be8SEnji Coopercsubst_body() {
181*57718be8SEnji Cooper	atf_check -o inline:'abcde\n' -x \
182*57718be8SEnji Cooper	    'echo abcde | tr -c '"'\0-ac-\377' b"
183*57718be8SEnji Cooper	atf_check -o inline:'abcde\n' -x \
184*57718be8SEnji Cooper	    'echo abcde | tr -c '"'\0-ad-\377' bc"
185*57718be8SEnji Cooper	atf_check -o inline:'QUACK\n' -x \
186*57718be8SEnji Cooper	    'echo ABCDE | tr -c '"'\0-@' QUACK"
187*57718be8SEnji Cooper}
188*57718be8SEnji Cooper
189*57718be8SEnji Cooperatf_init_test_cases() {
190*57718be8SEnji Cooper	atf_add_test_case dopt
191*57718be8SEnji Cooper	atf_add_test_case sopt
192*57718be8SEnji Cooper	atf_add_test_case dsopt
193*57718be8SEnji Cooper	atf_add_test_case subst
194*57718be8SEnji Cooper	atf_add_test_case csubst
195*57718be8SEnji Cooper}
196