sun_solaris_compoundvario.sh revision 3e14f97f673e8a630f076077de35afdd43dc1587
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
#
# test setup
function err_exit
{
print -u2 -n "\t"
}
alias err_exit='err_exit $LINENO'
# "nounset" disabled for now
#set -o nounset
integer Errors=0
compound bracketstat=(
integer bopen=0
integer bclose=0
)
function count_brackets
{
typeset x="$1"
typeset c
integer i
for (( i=0 ; i < ${#x} ; i++ )) ; do
c="${x:i:1}"
done
return 0
}
# compound variable "cat" nr.1, using $ print "%B\n" ... #
function cpvcat1
{
set -o errexit
compound tmp
return 0
}
# compound variable "cat" nr.2, using $ print "%#B\n" ... #
function cpvcat2
{
set -o errexit
compound tmp
return 0
}
# compound variable "cat" nr.3, using $ print -C ... #
function cpvcat3
{
set -o errexit
compound tmp
return 0
}
# compound variable "cat" nr.4, using $ print -v ... #
function cpvcat4
{
set -o errexit
compound tmp
return 0
}
typeset s
# Test 1:
# Check whether "read -C" leaves the file pointer at the next line
# (and does not read beyond that point).
# Data layout is:
# -- snip --
# <compound var>
# hello
# -- snip --
# (additionally we test some extra stuff like bracket count)
s=${
compound x=(
a=1 b=2
typeset -a myarray=( 1 2 3 4 5 6 7 8 9 10 )
typeset -A myarray3=(
[a]=(
float m1=0.5
float m2=0.6
foo="hello"
)
[b]=(
foo="bar"
)
["c d"]=(
integer at=90
)
[e]=(
compound nested_cpv=(
typeset -a myarray=( 1 2 3 4 5 6 7 8 9 10 )
typeset str=$'a \'string'
)
)
[f]=(
typeset g="f"
)
[a_nan]=(
)
[a_hexfloat]=(
typeset -X my_hexfloat=1.1
)
)
)
{
print "hello"
read -C y
read s
}
print "x${s}x"
count_brackets "$y" || err_exit "y: bracket open ${bracketstat.bopen} != bracket close ${bracketstat.bclose}"
count_brackets "$(print -v y)" || err_exit "y: bracket open ${bracketstat.bopen} != bracket close ${bracketstat.bclose}"
count_brackets "$(print -C y)" || err_exit "y: bracket open ${bracketstat.bopen} != bracket close ${bracketstat.bclose}"
# cleanup
unset x y || err_exit "unset failed"
# Test 2:
# Same as test 1 except one more compound var following the "hello"
# line.
# Data layout is:
# -- snip --
# <compound var>
# hello
# <compound var>
# -- snip --
s=${
compound x=(
a=1 b=2
typeset -a myarray=( 1 2 3 4 5 6 7 8 9 10 )
compound -A myarray3=(
[a]=(
float m1=0.5
float m2=0.6
foo="hello"
)
[b]=(
foo="bar"
)
["c d"]=(
integer at=90
)
[e]=(
compound nested_cpv=(
typeset -a myarray=( 1 2 3 4 5 6 7 8 9 10 )
typeset str=$'a \'string'
)
)
[f]=(
typeset g="f"
)
[a_nan]=(
)
[a_hexfloat]=(
typeset -X my_hexfloat=1.1
)
)
)
{
print "hello"
read -C y1
read s
read -C y2
}
print "x${s}x"
(( ${#y1.myarray3[e].nested_cpv.myarray[@]} == 10 )) || err_exit "Expected 10 elements in y1.myarray3[e].nested_cpv, got ${#y1.myarray3[e].nested_cpv[@]}"
(( ${#y2.myarray3[e].nested_cpv.myarray[@]} == 10 )) || err_exit "Expected 10 elements in y2.myarray3[e].nested_cpv, got ${#y2.myarray3[e].nested_cpv[@]}"
count_brackets "$y1" || err_exit "y1: bracket open ${bracketstat.bopen} != bracket close ${bracketstat.bclose}"
count_brackets "$(print -v y1)" || err_exit "y1: bracket open ${bracketstat.bopen} != bracket close ${bracketstat.bclose}"
count_brackets "$(print -C y1)" || err_exit "y1: bracket open ${bracketstat.bopen} != bracket close ${bracketstat.bclose}"
count_brackets "$y2" || err_exit "y2: bracket open ${bracketstat.bopen} != bracket close ${bracketstat.bclose}"
count_brackets "$(print -v y2)" || err_exit "y2: bracket open ${bracketstat.bopen} != bracket close ${bracketstat.bclose}"
count_brackets "$(print -C y2)" || err_exit "y2: bracket open ${bracketstat.bopen} != bracket close ${bracketstat.bclose}"
# cleanup
# Test 3: Test compound variable copy operator vs. "read -C"
compound x=(
a=1 b=2
typeset -a myarray=( 1 2 3 4 5 6 7 8 9 10 )
compound -A myarray3=(
[a]=(
float m1=0.5
float m2=0.6
foo="hello"
)
[b]=(
foo="bar"
)
["c d"]=(
integer at=90
)
[e]=(
compound nested_cpv=(
typeset -a myarray=( 1 2 3 4 5 6 7 8 9 10 )
typeset str=$'a \'string'
)
)
[f]=(
typeset g="f"
)
[a_nan]=(
)
[a_hexfloat]=(
typeset -X my_hexfloat=1.1
)
)
)
count_brackets "${x_copy}" || err_exit "x_copy: bracket open ${bracketstat.bopen} != bracket close ${bracketstat.bclose}"
count_brackets "$(print -v x_copy)" || err_exit "x_copy: bracket open ${bracketstat.bopen} != bracket close ${bracketstat.bclose}"
count_brackets "$(print -C x_copy)" || err_exit "x_copy: bracket open ${bracketstat.bopen} != bracket close ${bracketstat.bclose}"
compound nested_cpv_copy
nested_cpv_copy=x.myarray3[e].nested_cpv || err_exit "x.myarray3[e].nested_cpv copy failed"
(( ${#nested_cpv_copy.myarray[@]} == 10 )) || err_exit "Expected 10 elements in nested_cpv_copy.myarray, got ${#nested_cpv_copy.myarray[@]}"
# unset branch "x.myarray3[e].nested_cpv" of the variable tree "x" ...
unset x.myarray3[e].nested_cpv || err_exit "unset x.myarray3[e].nested_cpv failed"
# ... and restore it from the saved copy
printf "%B\n" nested_cpv_copy | cpvcat1 | cpvcat2 | cpvcat3 | cpvcat4 | read -C x.myarray3[e].nested_cpv || err_exit "read failed"
# compare copy of the original tree and the modified one
count_brackets "${x}" || err_exit "x: bracket open ${bracketstat.bopen} != bracket close ${bracketstat.bclose}"
count_brackets "$(print -v x)" || err_exit "x: bracket open ${bracketstat.bopen} != bracket close ${bracketstat.bclose}"
count_brackets "$(print -C x)" || err_exit "x: bracket open ${bracketstat.bopen} != bracket close ${bracketstat.bclose}"
(( ${#x.myarray3[e].nested_cpv.myarray[@]} == 10 )) || err_exit "Expected 10 elements in x.myarray3[e].nested_cpv, got ${#x.myarray3[e].nested_cpv[@]}"
# cleanup
unset x x_copy nested_cpv_copy || err_exit "unset failed"
# Test 4: Test "read -C" failure for missing bracket at the end
typeset s
s=$($SHELL -c 'compound myvar ; print "( unfinished=1" | read -C myvar 2>/dev/null || print "error $?"') || err_exit "shell failed"
# Test 5: Test "read -C" failure for missing bracket at the beginning
typeset s
s=$($SHELL -c 'compound myvar ; print " unfinished=1 )" | read -C myvar 2>/dev/null || print "error $?"') || err_exit "shell failed"
# tests done