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