1272343Sngie# $NetBSD: t_basic.sh,v 1.3 2013/08/11 01:50:02 dholland Exp $
2272343Sngie#
3272343Sngie# Copyright (c) 2013 The NetBSD Foundation, Inc.
4272343Sngie# All rights reserved.
5272343Sngie#
6272343Sngie# This code is derived from software contributed to The NetBSD Foundation
7272343Sngie# by David A. Holland.
8272343Sngie#
9272343Sngie# Redistribution and use in source and binary forms, with or without
10272343Sngie# modification, are permitted provided that the following conditions
11272343Sngie# are met:
12272343Sngie# 1. Redistributions of source code must retain the above copyright
13272343Sngie#    notice, this list of conditions and the following disclaimer.
14272343Sngie# 2. Redistributions in binary form must reproduce the above copyright
15272343Sngie#    notice, this list of conditions and the following disclaimer in the
16272343Sngie#    documentation and/or other materials provided with the distribution.
17272343Sngie#
18272343Sngie# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19272343Sngie# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20272343Sngie# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21272343Sngie# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22272343Sngie# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23272343Sngie# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24272343Sngie# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25272343Sngie# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26272343Sngie# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27272343Sngie# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28272343Sngie# POSSIBILITY OF SUCH DAMAGE.
29272343Sngie#
30272343Sngie
31272343Sngie#
32272343Sngie# tr -d: delete character
33272343Sngie#
34272343Sngieatf_test_case dopt
35272343Sngiedopt_head() {
36272343Sngie	atf_set "descr" "Tests for tr -d"
37272343Sngie}
38272343Sngie
39272343Sngiedopt_body() {
40272343Sngie	atf_check -o inline:'abcde\n' -x 'echo abcde | tr -d x'
41272343Sngie	atf_check -o inline:'abde\n' -x 'echo abcde | tr -d c'
42272343Sngie	atf_check -o inline:'ace\n' -x 'echo abcde | tr -d bd'
43272343Sngie	atf_check -o inline:'ae\n' -x 'echo abcde | tr -d b-d'
44272343Sngie	atf_check -o inline:'b\n' -x 'echo abcde | tr -d ac-e'
45272343Sngie	atf_check -o inline:'d\n' -x 'echo abcde | tr -d a-ce'
46272343Sngie	atf_check -o inline:'aei\n' -x 'echo abcdefghi | tr -d b-df-h'
47272343Sngie
48272343Sngie	atf_check -o inline:'' -x 'echo abcde | tr -c -d x'
49272343Sngie	atf_check -o inline:'c' -x 'echo abcde | tr -c -d c'
50272343Sngie	atf_check -o inline:'bd' -x 'echo abcde | tr -c -d bd'
51272343Sngie	atf_check -o inline:'bcd' -x 'echo abcde | tr -c -d b-d'
52272343Sngie	atf_check -o inline:'acde' -x 'echo abcde | tr -c -d ac-e'
53272343Sngie	atf_check -o inline:'abce' -x 'echo abcde | tr -c -d a-ce'
54272343Sngie	atf_check -o inline:'bcdfgh' -x 'echo abcdefghi | tr -c -d b-df-h'
55272343Sngie
56272343Sngie	# see if escape codes work
57272343Sngie	atf_check -o inline:'splice' -x '(echo spl; echo ice) | tr -d '"'\n'"
58272343Sngie	atf_check -o inline:'splice' -x '(echo spl; echo ice) | tr -d '"'\012'"
59272343Sngie
60272343Sngie	# see if escape codes work when followed by other things
61272343Sngie	atf_check -o inline:'slice' -x '(echo spl; echo ice) | tr -d '"'\n'p"
62272343Sngie	atf_check -o inline:'slice' -x '(echo spl; echo ice) | tr -d '"'\012'p"
63272343Sngie
64272343Sngie	# see if the [=x=] syntax works
65272343Sngie	atf_check -o inline:'abde\n' -x 'echo abcde | tr -d '"'[=c=]'"
66272343Sngie	atf_check -o inline:'bde\n' -x 'echo abcde | tr -d '"'[=c=]'a"
67272343Sngie
68272343Sngie	# make sure 0 works
69272343Sngie	# (ignore stderr as dd blabbers to it)
70272343Sngie	atf_check -e ignore -o inline:'ab\n' \
71272343Sngie	  -x '(echo -n a; dd if=/dev/zero bs=3 count=1; echo b) | tr -d '"'\0'"
72272343Sngie
73272343Sngie	# test posix classes
74272343Sngie	atf_check -o inline:'.\n' -x 'echo aAzZ.123 | tr -d '"'[:alnum:]'"
75272343Sngie	atf_check -o inline:'.123\n' -x 'echo aAzZ.123 | tr -d '"'[:alpha:]'"
76272343Sngie	atf_check -o inline:'az\n' -x 'echo "a z" | tr -d '"'[:blank:]'"
77272343Sngie	atf_check -o inline:'az' -x '(echo a; echo z) | tr -d '"'[:cntrl:]'"
78272343Sngie	atf_check -o inline:'aAzZ.\n' -x 'echo aAzZ.123 | tr -d '"'[:digit:]'"
79272343Sngie	atf_check -o inline:' \n' -x 'echo "a z.123" | tr -d '"'[:graph:]'"
80272343Sngie	atf_check -o inline:'AZ.123\n' -x 'echo aAzZ.123 | tr -d '"'[:lower:]'"
81272343Sngie	atf_check -o inline:'\n' -x 'echo aAzZ.123 | tr -d '"'[:print:]'"
82272343Sngie	atf_check -o inline:'aAzZ12\n' -x 'echo aAzZ.12 | tr -d '"'[:punct:]'"
83272343Sngie	atf_check -o inline:'az' -x 'echo "a z" | tr -d '"'[:space:]'"
84272343Sngie	atf_check -o inline:'az.123\n' -x 'echo aAzZ.123 | tr -d '"'[:upper:]'"
85272343Sngie	atf_check -o inline:'zZ.\n' -x 'echo aAzZ.123 | tr -d '"'[:xdigit:]'"
86272343Sngie}
87272343Sngie
88272343Sngie#
89272343Sngie# tr -s: squeeze duplicate character runs
90272343Sngie#
91272343Sngieatf_test_case sopt
92272343Sngiesopt_head() {
93272343Sngie	atf_set "descr" "Tests for tr -s"
94272343Sngie}
95272343Sngie
96272343Sngiesopt_body() {
97272343Sngie	atf_check -o inline:'abcde\n' -x 'echo abcde | tr -s x'
98272343Sngie	atf_check -o inline:'abcde\n' -x 'echo abcde | tr -s c'
99272343Sngie	atf_check -o inline:'abcde\n' -x 'echo abccccde | tr -s c'
100272343Sngie	atf_check -o inline:'abcde\n' -x 'echo abbbcddde | tr -s bd'
101272343Sngie	atf_check -o inline:'abcde\n' -x 'echo abbbcccddde | tr -s b-d'
102272343Sngie
103272343Sngie	atf_check -o inline:'acac\n' -x 'echo acac | tr -s c'
104272343Sngie	atf_check -o inline:'acac\n' -x 'echo accacc | tr -s c'
105272343Sngie
106272343Sngie	atf_check -o inline:'abcde\n' -x 'echo abcde | tr -c -s x'
107272343Sngie	atf_check -o inline:'abcde\n' -x 'echo abcde | tr -c -s c'
108272343Sngie	atf_check -o inline:'abcccde\n' -x 'echo abcccde | tr -c -s c'
109272343Sngie	atf_check -o inline:'abbbcddde\n' -x 'echo abbbcddde | tr -c -s bd'
110272343Sngie	atf_check -o inline:'abbbccddde\n' -x 'echo abbbccddde | tr -c -s b-d'
111272343Sngie	atf_check -o inline:'abcccde\n' -x 'echo aaabcccde | tr -c -s b-d'
112272343Sngie}
113272343Sngie
114272343Sngie#
115272343Sngie# tr -ds: both -d and -s at once
116272343Sngie#
117272343Sngieatf_test_case dsopt
118272343Sngiedsopt_head() {
119272343Sngie	atf_set "descr" "Tests for tr -ds"
120272343Sngie}
121272343Sngie
122272343Sngiedsopt_body() {
123272343Sngie	atf_check -o inline:'abcde\n' -x 'echo abcde | tr -ds x y'
124272343Sngie	atf_check -o inline:'abde\n' -x 'echo abcde | tr -ds c x'
125272343Sngie	atf_check -o inline:'abcde\n' -x 'echo abcde | tr -ds x c'
126272343Sngie	atf_check -o inline:'abde\n' -x 'echo abcde | tr -ds c c'
127272343Sngie	atf_check -o inline:'abde\n' -x 'echo abcccde | tr -ds c x'
128272343Sngie	atf_check -o inline:'abcde\n' -x 'echo abcccde | tr -ds x c'
129272343Sngie	atf_check -o inline:'abde\n' -x 'echo abcccde | tr -ds c c'
130272343Sngie
131272343Sngie	# -c complements only the first string
132272343Sngie	atf_check -o inline:'' -x 'echo abcde | tr -c -ds x y'
133272343Sngie	atf_check -o inline:'c' -x 'echo abcde | tr -c -ds c x'
134272343Sngie	atf_check -o inline:'' -x 'echo abcde | tr -c -ds x c'
135272343Sngie	atf_check -o inline:'c' -x 'echo abcde | tr -c -ds c c'
136272343Sngie	atf_check -o inline:'ccc' -x 'echo abcccde | tr -c -ds c x'
137272343Sngie	atf_check -o inline:'' -x 'echo abcccde | tr -c -ds x c'
138272343Sngie	atf_check -o inline:'c' -x 'echo abcccde | tr -c -ds c c'
139272343Sngie}
140272343Sngie
141272343Sngie#
142272343Sngie# test substitution
143272343Sngie#
144272343Sngieatf_test_case subst
145272343Sngiesubst_head() {
146272343Sngie	atf_set "descr" "Tests for tr substitution"
147272343Sngie}
148272343Sngie
149272343Sngiesubst_body() {
150272343Sngie	atf_check -o inline:'abcde\n' -x 'echo abcde | tr a-c a-c'
151272343Sngie	atf_check -o inline:'cbade\n' -x 'echo abcde | tr a-c cba'
152272343Sngie	atf_check -o inline:'abcde\n' -x 'echo abcde | tr a-z a-z'
153272343Sngie	atf_check -o inline:'bcdef\n' -x 'echo abcde | tr a-z b-za'
154272343Sngie	atf_check -o inline:'zabcd\n' -x 'echo abcde | tr b-za a-z'
155272343Sngie	atf_check -o inline:'bbbbb\n' -x 'echo ababa | tr a b'
156272343Sngie	atf_check -o inline:'furrfu\n' -x 'echo sheesh | tr a-z n-za-m'
157272343Sngie	atf_check -o inline:'furrfu\n' -x 'echo sheesh | tr n-za-m a-z'
158272343Sngie
159272343Sngie	atf_check -o inline:'ABCDE\n' -x 'echo abcde | tr a-z A-Z'
160272343Sngie	atf_check -o inline:'ABC\n' \
161272343Sngie	    -x 'echo abc | tr '"'[:lower:]' '[:upper:]'"
162272343Sngie
163272343Sngie	# If you don't give enough substitution chars the last is repeated.
164272343Sngie	atf_check -o inline:'bozoo\n' -x 'echo abcde | tr a-z bozo'
165272343Sngie	atf_check -o inline:'qaaaa\n' -x 'echo abcde | tr a-z qa'
166272343Sngie
167272343Sngie	# You can use -s with substitution.
168272343Sngie	atf_check -o inline:'cbade\n' -x 'echo abcde | tr -s a-c cba'
169272343Sngie	atf_check -o inline:'cbaddee\n' -x 'echo aabbccddee | tr -s a-c cba'
170272343Sngie}
171272343Sngie
172272343Sngie#
173272343Sngie# test substitution with -c (does not currently work)
174272343Sngie#
175272343Sngieatf_test_case csubst
176272343Sngiecsubst_head() {
177272343Sngie	atf_set "descr" "Tests for tr substitution with -c"
178272343Sngie}
179272343Sngie
180272343Sngiecsubst_body() {
181272343Sngie	atf_check -o inline:'abcde\n' -x \
182272343Sngie	    'echo abcde | tr -c '"'\0-ac-\377' b"
183272343Sngie	atf_check -o inline:'abcde\n' -x \
184272343Sngie	    'echo abcde | tr -c '"'\0-ad-\377' bc"
185272343Sngie	atf_check -o inline:'QUACK\n' -x \
186272343Sngie	    'echo ABCDE | tr -c '"'\0-@' QUACK"
187272343Sngie}
188272343Sngie
189272343Sngieatf_init_test_cases() {
190272343Sngie	atf_add_test_case dopt
191272343Sngie	atf_add_test_case sopt
192272343Sngie	atf_add_test_case dsopt
193272343Sngie	atf_add_test_case subst
194272343Sngie	atf_add_test_case csubst
195272343Sngie}
196