[BACK]Return to t_fsplit.sh CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / tests / util / sh

File: [cvs.NetBSD.org] / src / tests / util / sh / Attic / t_fsplit.sh (download)

Revision 1.2, Wed Apr 30 13:11:00 2008 UTC (12 years, 11 months ago) by martin
Branch: MAIN
CVS Tags: yamt-pf42-base4, yamt-pf42-base3, yamt-pf42-base2, yamt-pagecache-base3, yamt-pagecache-base2, yamt-pagecache-base, wrstuden-revivesa-base-3, wrstuden-revivesa-base-2, wrstuden-revivesa-base-1, wrstuden-revivesa-base, wrstuden-revivesa, netbsd-6-base, netbsd-6-1-RELEASE, netbsd-6-1-RC4, netbsd-6-1-RC3, netbsd-6-1-RC2, netbsd-6-1-RC1, netbsd-6-1-5-RELEASE, netbsd-6-1-4-RELEASE, netbsd-6-1-3-RELEASE, netbsd-6-1-2-RELEASE, netbsd-6-1-1-RELEASE, netbsd-6-1, netbsd-6-0-RELEASE, netbsd-6-0-RC2, netbsd-6-0-RC1, netbsd-6-0-6-RELEASE, netbsd-6-0-5-RELEASE, netbsd-6-0-4-RELEASE, netbsd-6-0-3-RELEASE, netbsd-6-0-2-RELEASE, netbsd-6-0-1-RELEASE, netbsd-6-0, netbsd-6, netbsd-5-base, netbsd-5-2-RELEASE, netbsd-5-2-RC1, netbsd-5-2-3-RELEASE, netbsd-5-2-2-RELEASE, netbsd-5-2-1-RELEASE, netbsd-5-2, netbsd-5-1-RELEASE, netbsd-5-1-RC4, netbsd-5-1-RC3, netbsd-5-1-RC2, netbsd-5-1-RC1, netbsd-5-1-5-RELEASE, netbsd-5-1-4-RELEASE, netbsd-5-1-3-RELEASE, netbsd-5-1-2-RELEASE, netbsd-5-1-1-RELEASE, netbsd-5-1, netbsd-5-0-RELEASE, netbsd-5-0-RC4, netbsd-5-0-RC3, netbsd-5-0-RC2, netbsd-5-0-RC1, netbsd-5-0-2-RELEASE, netbsd-5-0-1-RELEASE, netbsd-5-0, netbsd-5, matt-premerge-20091211, matt-nb6-plus-nbase, matt-nb6-plus-base, matt-nb6-plus, matt-nb5-pq3-base, matt-nb5-pq3, matt-nb5-mips64-u2-k2-k4-k7-k8-k9, matt-nb5-mips64-u1-k1-k5, matt-nb5-mips64-premerge-20101231, matt-nb5-mips64-premerge-20091211, matt-nb5-mips64-k15, matt-nb5-mips64, matt-nb4-mips64-k7-u2a-k9b, matt-mips64-premerge-20101231, matt-mips64-base2, jym-xensuspend-nbase, jym-xensuspend-base, jym-xensuspend, hpcarm-cleanup-nbase, cherry-xenmp-base, cherry-xenmp, bouyer-quota2-nbase, bouyer-quota2-base, bouyer-quota2
Branch point for: yamt-pagecache
Changes since 1.1: +1 -8 lines

Convert TNF licenses to new 2 clause variant

# $NetBSD: t_fsplit.sh,v 1.2 2008/04/30 13:11:00 martin Exp $
#
# Copyright (c) 2007 The NetBSD Foundation, Inc.
# All rights reserved.
#
# 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.
#

# The standard
# http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html
# explains (section 2.6) that Field splitting should be performed on the
# result of variable expansions.
# In particular this means that in ${x-word}, 'word' must be expanded as if
# the "${x-" and "}" were absent from the input line.
#
# So: sh -c 'set ${x-a b c}; echo $#' should give 3.
#

nl='
'

check()
{
	result="$(eval $1)"
	# Remove newlines
	oifs="$IFS"
	IFS="$nl"
	result="$(echo $result)"
	IFS="$oifs"
	if [ "$2" != "$result" ]
	then
		atf_fail "expected [$2], found [$result]"
	fi
}

atf_test_case for
for_head() {
	atf_set "descr" "Checks field splitting in for loops"
}
for_body() {
	unset x

	# Since I managed to break this, leave the test in
	check 'for f in $x; do echo x${f}y; done' ''
}

atf_test_case default_val
default_val_head() {
	atf_set "descr" "Checks field splitting in variable default values"
}
default_val_body() {
	unset x

	# Check that IFS is applied to text from ${x-...} unless it is inside
	# any set of "..."
	check 'set ${x-a b c}; echo $#' 3
	check 'for i in ${x-a b c};            do echo "z${i}z"; done' 'zaz zbz zcz'
	check 'for i in ${x-"a b" c};          do echo "z${i}z"; done' 'za bz zcz'
	check 'for i in ${x-"a ${x-b c}" d};   do echo "z${i}z"; done' 'za b cz zdz'
	check 'for i in ${x-"a ${x-"b c"}" d}; do echo "z${i}z"; done' 'za b cz zdz'
	check 'for i in ${x-a ${x-"b c"} d};   do echo "z${i}z"; done' 'zaz zb cz zdz'
	check 'for i in ${x-a ${x-b c} d};     do echo "z${i}z"; done' 'zaz zbz zcz zdz'
}

atf_test_case ifs_alpha
ifs_alpha_head() {
	atf_set "descr" "Checks that field splitting works with alphabetic" \
	                "characters"
}
ifs_alpha_body() {
	unset x

	# repeat with an alphabetic in IFS
	check 'IFS=q; set ${x-aqbqc}; echo $#' 3
	check 'IFS=q; for i in ${x-aqbqc};            do echo "z${i}z"; done' 'zaz zbz zcz'
	check 'IFS=q; for i in ${x-"aqb"qc};          do echo "z${i}z"; done' 'zaqbz zcz'
	check 'IFS=q; for i in ${x-"aq${x-bqc}"qd};   do echo "z${i}z"; done' 'zaqbqcz zdz'
	check 'IFS=q; for i in ${x-"aq${x-"bqc"}"qd}; do echo "z${i}z"; done' 'zaqbqcz zdz'
	check 'IFS=q; for i in ${x-aq${x-"bqc"}qd};  do echo "z${i}z"; done' 'zaz zbqcz zdz'
}

atf_test_case quote
quote_head() {
	atf_set "descr" "Checks that field splitting works with multi-word" \
	                "fields"
}
quote_body() {
	unset x

	# Some quote propagation checks
	check 'set "${x-a b c}";   echo $#' 1
	check 'set "${x-"a b" c}"; echo $1' 'a b c'
	check 'for i in "${x-a b c}"; do echo "z${i}z"; done' 'za b cz'
}

atf_test_case dollar_at
dollar_at_head() {
	atf_set "descr" "Checks that field splitting works when expanding" \
	                "\$@"
}
dollar_at_body() {
	unset x

	# Check we get "$@" right
	check 'set "";        for i;           do echo "z${i}z"; done' 'zz'
	check 'set "";        for i in "$@";   do echo "z${i}z"; done' 'zz'
	check 'set "" "";     for i;           do echo "z${i}z"; done' 'zz zz'
	check 'set "" "";     for i in "$@";   do echo "z${i}z"; done' 'zz zz'
	check 'set "" "";     for i in $@;     do echo "z${i}z"; done' ''
	check 'set "a b" c;   for i;           do echo "z${i}z"; done' 'za bz zcz'
	check 'set "a b" c;   for i in "$@";   do echo "z${i}z"; done' 'za bz zcz'
	check 'set "a b" c;   for i in $@;     do echo "z${i}z"; done' 'zaz zbz zcz'
	check 'set " a b " c; for i in "$@";   do echo "z${i}z"; done' 'z a b z zcz'
	check 'set --;        for i in x"$@"x; do echo "z${i}z"; done' 'zxxz'
	check 'set a;         for i in x"$@"x; do echo "z${i}z"; done' 'zxaxz'
	check 'set a b;       for i in x"$@"x; do echo "z${i}z"; done' 'zxaz zbxz'
}

atf_test_case ifs
ifs_head() {
	atf_set "descr" "Checks that IFS correctly configures field" \
	                "splitting behavior"
}
ifs_body() {
	unset x

	# Some IFS tests
	check 't="-- ";    IFS=" ";  set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '0'
	check 't=" x";     IFS=" x"; set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '1'
	check 't=" x ";    IFS=" x"; set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '1'
	check 't=axb;      IFS="x";  set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '2 a:b'
	check 't="a x b";  IFS="x";  set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '2 a : b'
	check 't="a xx b"; IFS="x";  set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '3 a :: b'
	check 't="a xx b"; IFS="x "; set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '3 a::b'
	# A recent 'clarification' means that a single trailing IFS non-whitespace
	# doesn't generate an empty parameter
	check 't="xax";  IFS="x";     set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '2 :a'
	check 't="xax "; IFS="x ";   set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '2 :a'
	# Verify that IFS isn't being applied where it shouldn't be.
	check 'IFS="x";             set axb; IFS=":"; r="$*"; IFS=; echo $# $r' '1 axb'
}

atf_test_case var_length
var_length_head() {
	atf_set "descr" "Checks that field splitting works when expanding" \
	                "a variable's length"
}
var_length_body() {
	unset x

	# Check that we apply IFS to ${#var}
	long=12345678123456781234567812345678
	long=$long$long$long$long
	check 'echo ${#long}; IFS=2; echo ${#long}; set 1 ${#long};echo $#' '128 1 8 3'
	check 'IFS=2; set ${x-${#long}};   IFS=" "; echo $* $#' '1 8 2'
	check 'IFS=2; set ${x-"${#long}"}; IFS=" "; echo $* $#' '128 1'
}

atf_init_test_cases() {
	atf_add_test_case for
	atf_add_test_case default_val
	atf_add_test_case ifs_alpha
	atf_add_test_case quote
	atf_add_test_case dollar_at
	atf_add_test_case ifs
	atf_add_test_case var_length
}