xref: /titanic_41/usr/src/lib/libshell/common/tests/sun_solaris_compound_nameref.sh (revision 3e14f97f673e8a630f076077de35afdd43dc1587)
1#
2# CDDL HEADER START
3#
4# The contents of this file are subject to the terms of the
5# Common Development and Distribution License (the "License").
6# You may not use this file except in compliance with the License.
7#
8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9# or http://www.opensolaris.org/os/licensing.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21
22#
23# Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24#
25
26
27
28# test setup
29function err_exit
30{
31	print -u2 -n "\t"
32	print -u2 -r ${Command}[$1]: "${@:2}"
33	(( Errors < 127 && Errors++ ))
34}
35alias err_exit='err_exit $LINENO'
36
37set -o nounset
38Command=${0##*/}
39integer Errors=0
40
41#
42# name reference test #001
43# Note we run this test in a seperate shell to make sure the memory
44# corruption originally reported can be reproduced (which precisely
45# depends on ordering in the testcase)
46(
47cat <<EOF
48	function err_exit
49	{
50		print -u2 -n "\t"
51		print -u2 -r \${Command}[\$1]: "\${@:2}"
52		(( Errors++ ))
53	}
54	alias err_exit='err_exit \$LINENO'
55
56	function function2
57	{
58		nameref v=\$1
59
60		v.x=19
61		v.y=20
62	}
63
64	function function1
65	{
66		typeset compound_var=()
67
68		function2 compound_var
69
70		printf "x=%d, y=%d\n" compound_var.x compound_var.y 
71	}
72
73	x="\$(function1)"
74
75	[[ "\$x" == 'x=19, y=20' ]] || err_exit "expected 'x=19, y=20', got '\${x}'"
76
77EOF
78) | ${SHELL}
79(( Errors+=$? ))
80
81
82#
83# name reference test #002
84# Originally derived from the xmldocumenttree1.sh demo which failed
85# with ast-ksh.2009-04-15 since the nodepath+nodesnum nameref calls
86# were removing the compound variable members nodes+nodesnum (caused
87# by a scoping bug)
88#
89(
90cat <<EOF
91	compound xdoc
92	compound -A xdoc.nodes
93	integer xdoc.nodesnum=0
94
95	function test1
96	{
97        	nameref doc=xdoc
98        	nameref nodepath="doc.nodes"
99        	nameref nodesnum="doc.nodesnum"
100        	print -v doc
101	}
102
103	test1
104EOF
105) | out=$( ${SHELL} ) || err_exit "shell returned exit code $?"
106
107(( ${ wc -l <<<"${out}" ; } == 4 )) || err_exit "Expected four lines of output, got ${out}"
108(set -o errexit ; read -C tmp <<<"${out}" ; [[ "$(typeset +p tmp.nodes)" == *-A* ]]) || err_exit "missing variable tmp.nodes"
109(set -o errexit ; read -C tmp <<<"${out}" ; [[ -v tmp.nodesnum                   ]]) || err_exit "missing variable tmp.nodesnum"
110
111
112#
113# name reference test #003a
114# ast-ksh.2009-06-30 failed with the following compound variable/nameref test
115#
116(
117cat <<EOF
118	compound -A addrsp
119
120	nameref sp=addrsp
121        
122	sp[14]=( size=1 )
123        
124	if [[ -v sp[19] ]] ; then
125        	print "should not happen"
126	else
127        	print "Ok"
128	fi
129EOF
130) | out=$( ${SHELL} ) || err_exit "shell returned exit code $?"
131[[ "${out}" == "Ok" ]] || err_exit "Expected 'Ok', got ${out}"
132
133
134#
135# name reference test #003b
136# (same as test #003a but uses a function)
137# ast-ksh.2009-06-30 failed with the following compound variable/nameref test
138#
139(
140cat <<EOF
141	compound -A addrsp
142	
143	function t1
144	{
145		nameref sp=\$1
146        
147		sp[14]=( size=1 )
148        
149		if [[ -v sp[19] ]] ; then
150        		print "should not happen"
151		else
152        		print "Ok"
153		fi
154	}
155
156	t1 addrsp
157EOF
158) | out=$( ${SHELL} ) || err_exit "shell returned exit code $?"
159[[ "${out}" == "Ok" ]] || err_exit "Expected 'Ok', got ${out}"
160
161
162#
163# name reference test #004a
164# (same as #003a but uses an indexed array instead of an associative one)
165# ast-ksh.2009-06-30 failed with the following compound variable/nameref test
166#
167(
168cat <<EOF
169	compound -a addrsp
170
171	nameref sp=addrsp
172        
173	sp[14]=( size=1 )
174        
175	if [[ -v sp[19] ]] ; then
176        	print "should not happen"
177	else
178        	print "Ok"
179	fi
180EOF
181) | out=$( ${SHELL} ) || err_exit "shell returned exit code $?"
182[[ "${out}" == "Ok" ]] || err_exit "Expected 'Ok', got ${out}"
183
184
185#
186# name reference test #004b
187# (same as test #004a but uses a function)
188# ast-ksh.2009-06-30 failed with the following compound variable/nameref test
189#
190(
191cat <<EOF
192	compound -a addrsp
193	
194	function t1
195	{
196		nameref sp=\$1
197        
198		sp[14]=( size=1 )
199        
200		if [[ -v sp[19] ]] ; then
201        		print "should not happen"
202		else
203        		print "Ok"
204		fi
205	}
206
207	t1 addrsp
208EOF
209) | out=$( ${SHELL} ) || err_exit "shell returned exit code $?"
210[[ "${out}" == "Ok" ]] || err_exit "Expected 'Ok', got ${out}"
211
212
213# tests done
214exit $((Errors))
215