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