xref: /titanic_51/usr/src/lib/libshell/common/tests/statics.sh (revision 3e14f97f673e8a630f076077de35afdd43dc1587)
17c2fbfb3SApril Chin########################################################################
27c2fbfb3SApril Chin#                                                                      #
37c2fbfb3SApril Chin#               This software is part of the ast package               #
4*3e14f97fSRoger A. Faulkner#          Copyright (c) 1982-2010 AT&T Intellectual Property          #
57c2fbfb3SApril Chin#                      and is licensed under the                       #
67c2fbfb3SApril Chin#                  Common Public License, Version 1.0                  #
77c2fbfb3SApril Chin#                    by AT&T Intellectual Property                     #
87c2fbfb3SApril Chin#                                                                      #
97c2fbfb3SApril Chin#                A copy of the License is available at                 #
107c2fbfb3SApril Chin#            http://www.opensource.org/licenses/cpl1.0.txt             #
117c2fbfb3SApril Chin#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
127c2fbfb3SApril Chin#                                                                      #
137c2fbfb3SApril Chin#              Information and Software Systems Research               #
147c2fbfb3SApril Chin#                            AT&T Research                             #
157c2fbfb3SApril Chin#                           Florham Park NJ                            #
167c2fbfb3SApril Chin#                                                                      #
177c2fbfb3SApril Chin#                  David Korn <dgk@research.att.com>                   #
187c2fbfb3SApril Chin#                                                                      #
197c2fbfb3SApril Chin########################################################################
207c2fbfb3SApril Chinfunction err_exit2
217c2fbfb3SApril Chin{
227c2fbfb3SApril Chin	print -u2 -n "\t"
237c2fbfb3SApril Chin	print -u2 -r ${Command}[$1]: "${@:2}"
247c2fbfb3SApril Chin	(( Errors+=1 ))
257c2fbfb3SApril Chin}
267c2fbfb3SApril Chin
277c2fbfb3SApril Chinfunction testfunc
287c2fbfb3SApril Chin{
297c2fbfb3SApril Chin	integer line_number=$1
307c2fbfb3SApril Chin	typeset cmd="$2"
317c2fbfb3SApril Chin	typeset expected_output="$3"
327c2fbfb3SApril Chin	typeset output
337c2fbfb3SApril Chin
347c2fbfb3SApril Chin	output="$($SHELL -c "${cmd}" 2>&1 )"
357c2fbfb3SApril Chin
367c2fbfb3SApril Chin	[[ "${output}" != "${expected_output}" ]] && err_exit2 ${line_number} "${output} != ${expected_output}"
377c2fbfb3SApril Chin}
387c2fbfb3SApril Chinalias testfunc='testfunc $LINENO'
397c2fbfb3SApril Chinalias err_exit='err_exit2 $LINENO'
407c2fbfb3SApril Chin
417c2fbfb3SApril ChinCommand=${0##*/}
427c2fbfb3SApril Chininteger Errors=0
437c2fbfb3SApril Chin
447c2fbfb3SApril Chin# string
457c2fbfb3SApril Chintestfunc '(function l { typeset -S x ;     x+="#" ; $1 && print "$x" ; } ; l false ; l false   ; l true)'  "###"
467c2fbfb3SApril Chintestfunc 'function  l { typeset -S x=">" ; x+="#" ; $1 && print "$x" ; } ; l false ; l false   ; l true'   ">###"
477c2fbfb3SApril Chintestfunc 'function  l { typeset -S x=">" ; x+="#" ; $1 && print "$x" ; } ; l false ; (l false) ; l true'   ">##"
487c2fbfb3SApril Chintestfunc 'function  l { typeset -S x=">" ; x+="#" ; $1 && print "$x" ; } ; l false; ( ulimit -c 0 ; l false) ; l true' ">##"
497c2fbfb3SApril Chin
507c2fbfb3SApril Chin# integer
517c2fbfb3SApril Chintestfunc '(function l { typeset -S -i x ;  x+=1 ;   $1 && print "$x" ; } ; l false ; l false   ; l true )' "3"
527c2fbfb3SApril Chintestfunc '(function l { typeset -S -i x ;  x+=1 ;   $1 && print "$x" ; } ; l false ; (l false) ; l true )' "2"
537c2fbfb3SApril Chin
547c2fbfb3SApril Chin# float
557c2fbfb3SApril Chintestfunc '(function l { float -S x=0.5 ;  (( x+=.5 )) ;   $1 && print "$x" ; } ; l false ; l false   ; l true )' "2"
567c2fbfb3SApril Chintestfunc '(function l { float -S x=0.5 ;  (( x+=.5 )) ;   $1 && print "$x" ; } ; l false ; (l false) ; l true )' "1.5"
577c2fbfb3SApril Chin
587c2fbfb3SApril Chin# compound variable
597c2fbfb3SApril Chin[[ "${
607c2fbfb3SApril Chin	function l
617c2fbfb3SApril Chin	{
627c2fbfb3SApril Chin		typeset -S s=( a=0 b=0 )
637c2fbfb3SApril Chin
647c2fbfb3SApril Chin		(( s.a++, s.b++ ))
657c2fbfb3SApril Chin
667c2fbfb3SApril Chin		$1 && printf 'a=%d, b=%d\n' s.a s.b
677c2fbfb3SApril Chin	}
687c2fbfb3SApril Chin	l false ; l false ; l true
697c2fbfb3SApril Chin}" != "a=3, b=3" ]] && err_exit "static compound var failed"
707c2fbfb3SApril Chin
717c2fbfb3SApril Chin
727c2fbfb3SApril Chin# array variable
737c2fbfb3SApril Chinexpected="helloan elementan elementan element"
747c2fbfb3SApril Chingot=$(
757c2fbfb3SApril Chin	function ar
767c2fbfb3SApril Chin	{
777c2fbfb3SApril Chin		typeset -a -S s=( "hello" )
787c2fbfb3SApril Chin
797c2fbfb3SApril Chin		s+=( "an element" )
807c2fbfb3SApril Chin
817c2fbfb3SApril Chin		$1 && { printf '%s' "${s[@]}" ; printf '\n' ; }
827c2fbfb3SApril Chin	}
837c2fbfb3SApril Chin	ar false ; ar false ; ar true
847c2fbfb3SApril Chin)
857c2fbfb3SApril Chin[[ $got != $expected ]] && err_exit "static array var failed -- expected '$expected', got '$got'"
867c2fbfb3SApril Chin
877c2fbfb3SApril Chin
887c2fbfb3SApril Chin# Test visibilty of "global" vs. "static" variables. if we have a "static" variable in a
897c2fbfb3SApril Chin# function and "unset" it we should see a global variable with the same
907c2fbfb3SApril Chin# name, right ?
917c2fbfb3SApril Chininteger hx=5
927c2fbfb3SApril Chinfunction test_hx_scope
937c2fbfb3SApril Chin{
947c2fbfb3SApril Chin	integer -S hx=9
957c2fbfb3SApril Chin	$2 && unset hx
967c2fbfb3SApril Chin	$1 && printf "hx=%d\n" hx
977c2fbfb3SApril Chin}
987c2fbfb3SApril Chintest_hx_scope false false
997c2fbfb3SApril Chintest_hx_scope false false
1007c2fbfb3SApril Chin# first test the "unset" call in a $(...) subshell...
1017c2fbfb3SApril Chin[[ "$( test_hx_scope true true  )" != "hx=5" ]] && err_exit "can't see global variable hx after unsetting static variable hx"
1027c2fbfb3SApril Chin# ... end then test whether the value has changed.
1037c2fbfb3SApril Chin[[ "${ test_hx_scope true false }" != "hx=9" ]] && err_exit "hx variable somehow changed"
1047c2fbfb3SApril Chin
1057c2fbfb3SApril Chinexit $((Errors))
1067c2fbfb3SApril Chin
107