xref: /netbsd-src/tests/usr.bin/tr/t_basic.sh (revision eac2170da1521e3eef9039164e52a2a8fefba495)
1*eac2170dSdholland# $NetBSD: t_basic.sh,v 1.3 2013/08/11 01:50:02 dholland Exp $
236890ffbSdholland#
336890ffbSdholland# Copyright (c) 2013 The NetBSD Foundation, Inc.
436890ffbSdholland# All rights reserved.
536890ffbSdholland#
636890ffbSdholland# This code is derived from software contributed to The NetBSD Foundation
736890ffbSdholland# by David A. Holland.
836890ffbSdholland#
936890ffbSdholland# Redistribution and use in source and binary forms, with or without
1036890ffbSdholland# modification, are permitted provided that the following conditions
1136890ffbSdholland# are met:
1236890ffbSdholland# 1. Redistributions of source code must retain the above copyright
1336890ffbSdholland#    notice, this list of conditions and the following disclaimer.
1436890ffbSdholland# 2. Redistributions in binary form must reproduce the above copyright
1536890ffbSdholland#    notice, this list of conditions and the following disclaimer in the
1636890ffbSdholland#    documentation and/or other materials provided with the distribution.
1736890ffbSdholland#
1836890ffbSdholland# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1936890ffbSdholland# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2036890ffbSdholland# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2136890ffbSdholland# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2236890ffbSdholland# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2336890ffbSdholland# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2436890ffbSdholland# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2536890ffbSdholland# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2636890ffbSdholland# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2736890ffbSdholland# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2836890ffbSdholland# POSSIBILITY OF SUCH DAMAGE.
2936890ffbSdholland#
3036890ffbSdholland
3136890ffbSdholland#
3236890ffbSdholland# tr -d: delete character
3336890ffbSdholland#
3436890ffbSdhollandatf_test_case dopt
3536890ffbSdhollanddopt_head() {
3636890ffbSdholland	atf_set "descr" "Tests for tr -d"
3736890ffbSdholland}
3836890ffbSdholland
3936890ffbSdhollanddopt_body() {
4036890ffbSdholland	atf_check -o inline:'abcde\n' -x 'echo abcde | tr -d x'
4136890ffbSdholland	atf_check -o inline:'abde\n' -x 'echo abcde | tr -d c'
4236890ffbSdholland	atf_check -o inline:'ace\n' -x 'echo abcde | tr -d bd'
4336890ffbSdholland	atf_check -o inline:'ae\n' -x 'echo abcde | tr -d b-d'
4436890ffbSdholland	atf_check -o inline:'b\n' -x 'echo abcde | tr -d ac-e'
4536890ffbSdholland	atf_check -o inline:'d\n' -x 'echo abcde | tr -d a-ce'
4636890ffbSdholland	atf_check -o inline:'aei\n' -x 'echo abcdefghi | tr -d b-df-h'
4736890ffbSdholland
4836890ffbSdholland	atf_check -o inline:'' -x 'echo abcde | tr -c -d x'
4936890ffbSdholland	atf_check -o inline:'c' -x 'echo abcde | tr -c -d c'
5036890ffbSdholland	atf_check -o inline:'bd' -x 'echo abcde | tr -c -d bd'
5136890ffbSdholland	atf_check -o inline:'bcd' -x 'echo abcde | tr -c -d b-d'
5236890ffbSdholland	atf_check -o inline:'acde' -x 'echo abcde | tr -c -d ac-e'
5336890ffbSdholland	atf_check -o inline:'abce' -x 'echo abcde | tr -c -d a-ce'
5436890ffbSdholland	atf_check -o inline:'bcdfgh' -x 'echo abcdefghi | tr -c -d b-df-h'
5536890ffbSdholland
5636890ffbSdholland	# see if escape codes work
5736890ffbSdholland	atf_check -o inline:'splice' -x '(echo spl; echo ice) | tr -d '"'\n'"
5836890ffbSdholland	atf_check -o inline:'splice' -x '(echo spl; echo ice) | tr -d '"'\012'"
5936890ffbSdholland
60*eac2170dSdholland	# see if escape codes work when followed by other things
61*eac2170dSdholland	atf_check -o inline:'slice' -x '(echo spl; echo ice) | tr -d '"'\n'p"
62*eac2170dSdholland	atf_check -o inline:'slice' -x '(echo spl; echo ice) | tr -d '"'\012'p"
63*eac2170dSdholland
64*eac2170dSdholland	# see if the [=x=] syntax works
65*eac2170dSdholland	atf_check -o inline:'abde\n' -x 'echo abcde | tr -d '"'[=c=]'"
66*eac2170dSdholland	atf_check -o inline:'bde\n' -x 'echo abcde | tr -d '"'[=c=]'a"
67*eac2170dSdholland
6836890ffbSdholland	# make sure 0 works
6936890ffbSdholland	# (ignore stderr as dd blabbers to it)
7036890ffbSdholland	atf_check -e ignore -o inline:'ab\n' \
7136890ffbSdholland	  -x '(echo -n a; dd if=/dev/zero bs=3 count=1; echo b) | tr -d '"'\0'"
7236890ffbSdholland
7336890ffbSdholland	# test posix classes
7436890ffbSdholland	atf_check -o inline:'.\n' -x 'echo aAzZ.123 | tr -d '"'[:alnum:]'"
7536890ffbSdholland	atf_check -o inline:'.123\n' -x 'echo aAzZ.123 | tr -d '"'[:alpha:]'"
7636890ffbSdholland	atf_check -o inline:'az\n' -x 'echo "a z" | tr -d '"'[:blank:]'"
7736890ffbSdholland	atf_check -o inline:'az' -x '(echo a; echo z) | tr -d '"'[:cntrl:]'"
7836890ffbSdholland	atf_check -o inline:'aAzZ.\n' -x 'echo aAzZ.123 | tr -d '"'[:digit:]'"
7936890ffbSdholland	atf_check -o inline:' \n' -x 'echo "a z.123" | tr -d '"'[:graph:]'"
8036890ffbSdholland	atf_check -o inline:'AZ.123\n' -x 'echo aAzZ.123 | tr -d '"'[:lower:]'"
8136890ffbSdholland	atf_check -o inline:'\n' -x 'echo aAzZ.123 | tr -d '"'[:print:]'"
8236890ffbSdholland	atf_check -o inline:'aAzZ12\n' -x 'echo aAzZ.12 | tr -d '"'[:punct:]'"
8336890ffbSdholland	atf_check -o inline:'az' -x 'echo "a z" | tr -d '"'[:space:]'"
8436890ffbSdholland	atf_check -o inline:'az.123\n' -x 'echo aAzZ.123 | tr -d '"'[:upper:]'"
8536890ffbSdholland	atf_check -o inline:'zZ.\n' -x 'echo aAzZ.123 | tr -d '"'[:xdigit:]'"
8636890ffbSdholland}
8736890ffbSdholland
8836890ffbSdholland#
8936890ffbSdholland# tr -s: squeeze duplicate character runs
9036890ffbSdholland#
9136890ffbSdhollandatf_test_case sopt
9236890ffbSdhollandsopt_head() {
9336890ffbSdholland	atf_set "descr" "Tests for tr -s"
9436890ffbSdholland}
9536890ffbSdholland
9636890ffbSdhollandsopt_body() {
9736890ffbSdholland	atf_check -o inline:'abcde\n' -x 'echo abcde | tr -s x'
9836890ffbSdholland	atf_check -o inline:'abcde\n' -x 'echo abcde | tr -s c'
9936890ffbSdholland	atf_check -o inline:'abcde\n' -x 'echo abccccde | tr -s c'
10036890ffbSdholland	atf_check -o inline:'abcde\n' -x 'echo abbbcddde | tr -s bd'
10136890ffbSdholland	atf_check -o inline:'abcde\n' -x 'echo abbbcccddde | tr -s b-d'
10236890ffbSdholland
10336890ffbSdholland	atf_check -o inline:'acac\n' -x 'echo acac | tr -s c'
10436890ffbSdholland	atf_check -o inline:'acac\n' -x 'echo accacc | tr -s c'
10536890ffbSdholland
10636890ffbSdholland	atf_check -o inline:'abcde\n' -x 'echo abcde | tr -c -s x'
10736890ffbSdholland	atf_check -o inline:'abcde\n' -x 'echo abcde | tr -c -s c'
10836890ffbSdholland	atf_check -o inline:'abcccde\n' -x 'echo abcccde | tr -c -s c'
10936890ffbSdholland	atf_check -o inline:'abbbcddde\n' -x 'echo abbbcddde | tr -c -s bd'
11036890ffbSdholland	atf_check -o inline:'abbbccddde\n' -x 'echo abbbccddde | tr -c -s b-d'
11136890ffbSdholland	atf_check -o inline:'abcccde\n' -x 'echo aaabcccde | tr -c -s b-d'
11236890ffbSdholland}
11336890ffbSdholland
11436890ffbSdholland#
11536890ffbSdholland# tr -ds: both -d and -s at once
11636890ffbSdholland#
11736890ffbSdhollandatf_test_case dsopt
11836890ffbSdhollanddsopt_head() {
11936890ffbSdholland	atf_set "descr" "Tests for tr -ds"
12036890ffbSdholland}
12136890ffbSdholland
12236890ffbSdhollanddsopt_body() {
12336890ffbSdholland	atf_check -o inline:'abcde\n' -x 'echo abcde | tr -ds x y'
12436890ffbSdholland	atf_check -o inline:'abde\n' -x 'echo abcde | tr -ds c x'
12536890ffbSdholland	atf_check -o inline:'abcde\n' -x 'echo abcde | tr -ds x c'
12636890ffbSdholland	atf_check -o inline:'abde\n' -x 'echo abcde | tr -ds c c'
12736890ffbSdholland	atf_check -o inline:'abde\n' -x 'echo abcccde | tr -ds c x'
12836890ffbSdholland	atf_check -o inline:'abcde\n' -x 'echo abcccde | tr -ds x c'
12936890ffbSdholland	atf_check -o inline:'abde\n' -x 'echo abcccde | tr -ds c c'
13036890ffbSdholland
13136890ffbSdholland	# -c complements only the first string
13236890ffbSdholland	atf_check -o inline:'' -x 'echo abcde | tr -c -ds x y'
13336890ffbSdholland	atf_check -o inline:'c' -x 'echo abcde | tr -c -ds c x'
13436890ffbSdholland	atf_check -o inline:'' -x 'echo abcde | tr -c -ds x c'
13536890ffbSdholland	atf_check -o inline:'c' -x 'echo abcde | tr -c -ds c c'
13636890ffbSdholland	atf_check -o inline:'ccc' -x 'echo abcccde | tr -c -ds c x'
13736890ffbSdholland	atf_check -o inline:'' -x 'echo abcccde | tr -c -ds x c'
13836890ffbSdholland	atf_check -o inline:'c' -x 'echo abcccde | tr -c -ds c c'
13936890ffbSdholland}
14036890ffbSdholland
14136890ffbSdholland#
14236890ffbSdholland# test substitution
14336890ffbSdholland#
14436890ffbSdhollandatf_test_case subst
14536890ffbSdhollandsubst_head() {
14636890ffbSdholland	atf_set "descr" "Tests for tr substitution"
14736890ffbSdholland}
14836890ffbSdholland
14936890ffbSdhollandsubst_body() {
15036890ffbSdholland	atf_check -o inline:'abcde\n' -x 'echo abcde | tr a-c a-c'
15136890ffbSdholland	atf_check -o inline:'cbade\n' -x 'echo abcde | tr a-c cba'
15236890ffbSdholland	atf_check -o inline:'abcde\n' -x 'echo abcde | tr a-z a-z'
15336890ffbSdholland	atf_check -o inline:'bcdef\n' -x 'echo abcde | tr a-z b-za'
15436890ffbSdholland	atf_check -o inline:'zabcd\n' -x 'echo abcde | tr b-za a-z'
15536890ffbSdholland	atf_check -o inline:'bbbbb\n' -x 'echo ababa | tr a b'
15636890ffbSdholland	atf_check -o inline:'furrfu\n' -x 'echo sheesh | tr a-z n-za-m'
15736890ffbSdholland	atf_check -o inline:'furrfu\n' -x 'echo sheesh | tr n-za-m a-z'
15836890ffbSdholland
15936890ffbSdholland	atf_check -o inline:'ABCDE\n' -x 'echo abcde | tr a-z A-Z'
16036890ffbSdholland	atf_check -o inline:'ABC\n' \
16136890ffbSdholland	    -x 'echo abc | tr '"'[:lower:]' '[:upper:]'"
16236890ffbSdholland
16336890ffbSdholland	# If you don't give enough substitution chars the last is repeated.
16436890ffbSdholland	atf_check -o inline:'bozoo\n' -x 'echo abcde | tr a-z bozo'
16536890ffbSdholland	atf_check -o inline:'qaaaa\n' -x 'echo abcde | tr a-z qa'
16636890ffbSdholland
16736890ffbSdholland	# You can use -s with substitution.
16836890ffbSdholland	atf_check -o inline:'cbade\n' -x 'echo abcde | tr -s a-c cba'
16936890ffbSdholland	atf_check -o inline:'cbaddee\n' -x 'echo aabbccddee | tr -s a-c cba'
17036890ffbSdholland}
17136890ffbSdholland
17236890ffbSdholland#
17336890ffbSdholland# test substitution with -c (does not currently work)
17436890ffbSdholland#
17536890ffbSdhollandatf_test_case csubst
17636890ffbSdhollandcsubst_head() {
17736890ffbSdholland	atf_set "descr" "Tests for tr substitution with -c"
17836890ffbSdholland}
17936890ffbSdholland
18036890ffbSdhollandcsubst_body() {
18136890ffbSdholland	atf_check -o inline:'abcde\n' -x \
18236890ffbSdholland	    'echo abcde | tr -c '"'\0-ac-\377' b"
18336890ffbSdholland	atf_check -o inline:'abcde\n' -x \
18436890ffbSdholland	    'echo abcde | tr -c '"'\0-ad-\377' bc"
18588d97c81Sdholland	atf_check -o inline:'QUACK\n' -x \
18688d97c81Sdholland	    'echo ABCDE | tr -c '"'\0-@' QUACK"
18736890ffbSdholland}
18836890ffbSdholland
18936890ffbSdhollandatf_init_test_cases() {
19036890ffbSdholland	atf_add_test_case dopt
19136890ffbSdholland	atf_add_test_case sopt
19236890ffbSdholland	atf_add_test_case dsopt
19336890ffbSdholland	atf_add_test_case subst
19436890ffbSdholland	atf_add_test_case csubst
19536890ffbSdholland}
196