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