18462SApril.Chin@Sun.COM#
28462SApril.Chin@Sun.COM# CDDL HEADER START
38462SApril.Chin@Sun.COM#
48462SApril.Chin@Sun.COM# The contents of this file are subject to the terms of the
58462SApril.Chin@Sun.COM# Common Development and Distribution License (the "License").
68462SApril.Chin@Sun.COM# You may not use this file except in compliance with the License.
78462SApril.Chin@Sun.COM#
88462SApril.Chin@Sun.COM# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
98462SApril.Chin@Sun.COM# or http://www.opensolaris.org/os/licensing.
108462SApril.Chin@Sun.COM# See the License for the specific language governing permissions
118462SApril.Chin@Sun.COM# and limitations under the License.
128462SApril.Chin@Sun.COM#
138462SApril.Chin@Sun.COM# When distributing Covered Code, include this CDDL HEADER in each
148462SApril.Chin@Sun.COM# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
158462SApril.Chin@Sun.COM# If applicable, add the following below this CDDL HEADER, with the
168462SApril.Chin@Sun.COM# fields enclosed by brackets "[]" replaced with your own identifying
178462SApril.Chin@Sun.COM# information: Portions Copyright [yyyy] [name of copyright owner]
188462SApril.Chin@Sun.COM#
198462SApril.Chin@Sun.COM# CDDL HEADER END
208462SApril.Chin@Sun.COM#
218462SApril.Chin@Sun.COM
228462SApril.Chin@Sun.COM#
23*12068SRoger.Faulkner@Oracle.COM# Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
248462SApril.Chin@Sun.COM#
258462SApril.Chin@Sun.COM
268462SApril.Chin@Sun.COMfunction err_exit2
278462SApril.Chin@Sun.COM{
288462SApril.Chin@Sun.COM	print -u2 -n "\t"
298462SApril.Chin@Sun.COM	print -u2 -r ${Command}[$1]: "${@:2}"
30*12068SRoger.Faulkner@Oracle.COM	(( Errors < 127 && Errors++ ))
318462SApril.Chin@Sun.COM}
328462SApril.Chin@Sun.COM
338462SApril.Chin@Sun.COMfunction testfunc
348462SApril.Chin@Sun.COM{
358462SApril.Chin@Sun.COM	integer line_number=$1
368462SApril.Chin@Sun.COM	typeset cmd="$2"
378462SApril.Chin@Sun.COM	typeset expected_output="$3"
388462SApril.Chin@Sun.COM	typeset output
398462SApril.Chin@Sun.COM
408462SApril.Chin@Sun.COM	output="$($SHELL -c "${cmd}" 2>&1 )"
418462SApril.Chin@Sun.COM
428462SApril.Chin@Sun.COM	[[ "${output}" != "${expected_output}" ]] && err_exit2 ${line_number} "${output} != ${expected_output}"
438462SApril.Chin@Sun.COM}
448462SApril.Chin@Sun.COMalias testfunc='testfunc $LINENO'
458462SApril.Chin@Sun.COMalias err_exit='err_exit2 $LINENO'
468462SApril.Chin@Sun.COM
4710898Sroland.mainz@nrubsig.orgset -o nounset
4810898Sroland.mainz@nrubsig.orgCommand=${0##*/}
498462SApril.Chin@Sun.COMinteger Errors=0
508462SApril.Chin@Sun.COM
5110898Sroland.mainz@nrubsig.org
528462SApril.Chin@Sun.COM# string
538462SApril.Chin@Sun.COMtestfunc '(function l { typeset -S x ;     x+="#" ; $1 && print "$x" ; } ; l false ; l false   ; l true)'  "###"
548462SApril.Chin@Sun.COMtestfunc 'function  l { typeset -S x=">" ; x+="#" ; $1 && print "$x" ; } ; l false ; l false   ; l true'   ">###"
558462SApril.Chin@Sun.COMtestfunc 'function  l { typeset -S x=">" ; x+="#" ; $1 && print "$x" ; } ; l false ; (l false) ; l true'   ">##"
568462SApril.Chin@Sun.COMtestfunc 'function  l { typeset -S x=">" ; x+="#" ; $1 && print "$x" ; } ; l false; ( ulimit -c 0 ; l false) ; l true' ">##"
578462SApril.Chin@Sun.COM
588462SApril.Chin@Sun.COM# integer
598462SApril.Chin@Sun.COMtestfunc '(function l { typeset -S -i x ;  x+=1 ;   $1 && print "$x" ; } ; l false ; l false   ; l true )' "3"
608462SApril.Chin@Sun.COMtestfunc '(function l { typeset -S -i x ;  x+=1 ;   $1 && print "$x" ; } ; l false ; (l false) ; l true )' "2"
618462SApril.Chin@Sun.COM
628462SApril.Chin@Sun.COM# float
638462SApril.Chin@Sun.COMtestfunc '(function l { float -S x=0.5 ;  (( x+=.5 )) ;   $1 && print "$x" ; } ; l false ; l false   ; l true )' "2"
648462SApril.Chin@Sun.COMtestfunc '(function l { float -S x=0.5 ;  (( x+=.5 )) ;   $1 && print "$x" ; } ; l false ; (l false) ; l true )' "1.5"
658462SApril.Chin@Sun.COM
668462SApril.Chin@Sun.COM# compound variable
678462SApril.Chin@Sun.COM[[ "${
688462SApril.Chin@Sun.COM	function l
698462SApril.Chin@Sun.COM	{
708462SApril.Chin@Sun.COM		typeset -S s=( a=0 b=0 )
718462SApril.Chin@Sun.COM
728462SApril.Chin@Sun.COM		(( s.a++, s.b++ ))
738462SApril.Chin@Sun.COM
748462SApril.Chin@Sun.COM		$1 && printf 'a=%d, b=%d\n' s.a s.b
758462SApril.Chin@Sun.COM	}
768462SApril.Chin@Sun.COM	l false ; l false ; l true
778462SApril.Chin@Sun.COM}" != "a=3, b=3" ]] && err_exit "static compound var failed"
788462SApril.Chin@Sun.COM
798462SApril.Chin@Sun.COM
808462SApril.Chin@Sun.COM# array variable
818462SApril.Chin@Sun.COM[[ "$(
828462SApril.Chin@Sun.COM	function ar
838462SApril.Chin@Sun.COM	{
848462SApril.Chin@Sun.COM		typeset -a -S s=( "hello" )
858462SApril.Chin@Sun.COM
868462SApril.Chin@Sun.COM		s+=( "an element" )
878462SApril.Chin@Sun.COM
888462SApril.Chin@Sun.COM		$1 && { printf '%s' "${s[@]}" ; printf '\n' ; }
898462SApril.Chin@Sun.COM	}
908462SApril.Chin@Sun.COM	ar false ; ar false ; ar true
918462SApril.Chin@Sun.COM)" != "helloan elementan elementan element" ]] && err_exit "static array var failed"
928462SApril.Chin@Sun.COM
938462SApril.Chin@Sun.COM
948462SApril.Chin@Sun.COM# Test visibilty of "global" vs. "static" variables. if we have a "static" variable in a
958462SApril.Chin@Sun.COM# function and "unset" it we should see a global variable with the same
968462SApril.Chin@Sun.COM# name, right ?
978462SApril.Chin@Sun.COMinteger hx=5
988462SApril.Chin@Sun.COMfunction test_hx_scope
998462SApril.Chin@Sun.COM{
1008462SApril.Chin@Sun.COM	integer -S hx=9
1018462SApril.Chin@Sun.COM	$2 && unset hx
1028462SApril.Chin@Sun.COM	$1 && printf "hx=%d\n" hx
1038462SApril.Chin@Sun.COM}
1048462SApril.Chin@Sun.COMtest_hx_scope false false
1058462SApril.Chin@Sun.COMtest_hx_scope false false
1068462SApril.Chin@Sun.COM# first test the "unset" call in a $(...) subshell...
1078462SApril.Chin@Sun.COM[[ "$( test_hx_scope true true  )" != "hx=5" ]] && err_exit "can't see global variable hx after unsetting static variable hx"
1088462SApril.Chin@Sun.COM# ... end then test whether the value has changed.
1098462SApril.Chin@Sun.COM[[ "${ test_hx_scope true false }" != "hx=9" ]] && err_exit "hx variable somehow changed"
1108462SApril.Chin@Sun.COM
1118462SApril.Chin@Sun.COM
1128462SApril.Chin@Sun.COM# tests done
1138462SApril.Chin@Sun.COMexit $((Errors))
114