xref: /titanic_41/usr/src/lib/libshell/common/tests/statics.sh (revision 6ab3986229f6d7d9a83c94e3a6e68fd05da5cff0)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1982-2010 AT&T Intellectual Property          #
5#                      and is licensed under the                       #
6#                  Common Public License, Version 1.0                  #
7#                    by AT&T Intellectual Property                     #
8#                                                                      #
9#                A copy of the License is available at                 #
10#            http://www.opensource.org/licenses/cpl1.0.txt             #
11#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
12#                                                                      #
13#              Information and Software Systems Research               #
14#                            AT&T Research                             #
15#                           Florham Park NJ                            #
16#                                                                      #
17#                  David Korn <dgk@research.att.com>                   #
18#                                                                      #
19########################################################################
20function err_exit2
21{
22	print -u2 -n "\t"
23	print -u2 -r ${Command}[$1]: "${@:2}"
24	(( Errors+=1 ))
25}
26
27function testfunc
28{
29	integer line_number=$1
30	typeset cmd="$2"
31	typeset expected_output="$3"
32	typeset output
33
34	output="$($SHELL -c "${cmd}" 2>&1 )"
35
36	[[ "${output}" != "${expected_output}" ]] && err_exit2 ${line_number} "${output} != ${expected_output}"
37}
38alias testfunc='testfunc $LINENO'
39alias err_exit='err_exit2 $LINENO'
40
41Command=${0##*/}
42integer Errors=0
43
44# string
45testfunc '(function l { typeset -S x ;     x+="#" ; $1 && print "$x" ; } ; l false ; l false   ; l true)'  "###"
46testfunc 'function  l { typeset -S x=">" ; x+="#" ; $1 && print "$x" ; } ; l false ; l false   ; l true'   ">###"
47testfunc 'function  l { typeset -S x=">" ; x+="#" ; $1 && print "$x" ; } ; l false ; (l false) ; l true'   ">##"
48testfunc 'function  l { typeset -S x=">" ; x+="#" ; $1 && print "$x" ; } ; l false; ( ulimit -c 0 ; l false) ; l true' ">##"
49
50# integer
51testfunc '(function l { typeset -S -i x ;  x+=1 ;   $1 && print "$x" ; } ; l false ; l false   ; l true )' "3"
52testfunc '(function l { typeset -S -i x ;  x+=1 ;   $1 && print "$x" ; } ; l false ; (l false) ; l true )' "2"
53
54# float
55testfunc '(function l { float -S x=0.5 ;  (( x+=.5 )) ;   $1 && print "$x" ; } ; l false ; l false   ; l true )' "2"
56testfunc '(function l { float -S x=0.5 ;  (( x+=.5 )) ;   $1 && print "$x" ; } ; l false ; (l false) ; l true )' "1.5"
57
58# compound variable
59[[ "${
60	function l
61	{
62		typeset -S s=( a=0 b=0 )
63
64		(( s.a++, s.b++ ))
65
66		$1 && printf 'a=%d, b=%d\n' s.a s.b
67	}
68	l false ; l false ; l true
69}" != "a=3, b=3" ]] && err_exit "static compound var failed"
70
71
72# array variable
73expected="helloan elementan elementan element"
74got=$(
75	function ar
76	{
77		typeset -a -S s=( "hello" )
78
79		s+=( "an element" )
80
81		$1 && { printf '%s' "${s[@]}" ; printf '\n' ; }
82	}
83	ar false ; ar false ; ar true
84)
85[[ $got != $expected ]] && err_exit "static array var failed -- expected '$expected', got '$got'"
86
87
88# Test visibilty of "global" vs. "static" variables. if we have a "static" variable in a
89# function and "unset" it we should see a global variable with the same
90# name, right ?
91integer hx=5
92function test_hx_scope
93{
94	integer -S hx=9
95	$2 && unset hx
96	$1 && printf "hx=%d\n" hx
97}
98test_hx_scope false false
99test_hx_scope false false
100# first test the "unset" call in a $(...) subshell...
101[[ "$( test_hx_scope true true  )" != "hx=5" ]] && err_exit "can't see global variable hx after unsetting static variable hx"
102# ... end then test whether the value has changed.
103[[ "${ test_hx_scope true false }" != "hx=9" ]] && err_exit "hx variable somehow changed"
104
105exit $((Errors))
106
107