1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1982-2011 AT&T Intellectual Property          #
5#                      and is licensed under the                       #
6#                 Eclipse Public License, Version 1.0                  #
7#                    by AT&T Intellectual Property                     #
8#                                                                      #
9#                A copy of the License is available at                 #
10#          http://www.eclipse.org/org/documents/epl-v10.html           #
11#         (with md5 checksum b35adb5213ca9657e911e9befb180842)         #
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########################################################################
20#
21# CDDL HEADER START
22#
23# The contents of this file are subject to the terms of the
24# Common Development and Distribution License (the "License").
25# You may not use this file except in compliance with the License.
26#
27# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
28# or http://www.opensolaris.org/os/licensing.
29# See the License for the specific language governing permissions
30# and limitations under the License.
31#
32# When distributing Covered Code, include this CDDL HEADER in each
33# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
34# If applicable, add the following below this CDDL HEADER, with the
35# fields enclosed by brackets "[]" replaced with your own identifying
36# information: Portions Copyright [yyyy] [name of copyright owner]
37#
38# CDDL HEADER END
39#
40
41#
42# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
43# Use is subject to license terms.
44#
45
46#
47# This test checks whether "typeset -m" correctly moves local variables
48# into a global variable tree.
49#
50# This was reported as CR #XXXXXXXX ("XXXX"):
51# -- snip --
52#XXXX
53# -- snip --
54#
55
56function err_exit
57{
58	print -u2 -n "\t"
59	print -u2 -r ${Command}[$1]: "${@:2}"
60	(( Errors+=1 ))
61}
62
63alias err_exit='err_exit $LINENO'
64
65integer Errors=0
66
67## test start
68typeset -C tree1 tree2
69
70# add node to tree which uses "typeset -m" to move a local variable
71# into tree1.subtree["a_node"]
72function f1
73{
74	nameref tr=$1
75	typeset -A tr.subtree
76	typeset -C node
77	node.one="hello"
78	node.two="world"
79	# move local note into the array
80false
81	typeset -m tr.subtree["a_node"]=node
82	return 0
83}
84
85# Alternative version which uses "nameref" instead of "typeset -m"
86function f2
87{
88	nameref tr=$1
89	typeset -A tr.subtree
90	nameref node=tr.subtree["a_node"]
91	node.one="hello"
92	node.two="world"
93	return 0
94}
95
96f1 tree1
97f2 tree2
98
99[[ "${tree1.subtree["a_node"].one}" == "hello" ]] || err_exit "expected tree1.subtree[\"a_node\"].one == 'hello', got ${tree1.subtree["a_node"].one}"
100[[ "${tree1.subtree["a_node"].two}" == "world" ]] || err_exit "expected tree1.subtree[\"a_node\"].two == 'world', got ${tree1.subtree["a_node"].two}"
101[[ "${tree1}" == "${tree2}" ]] || err_exit "tree1 and tree2 differ:$'\n'"
102
103unset c
104compound c
105typeset -C -a c.ar
106c.ar[4]=( a4=1 )
107typeset -m "c.ar[5]=c.ar[4]"
108exp=$'(\n\ttypeset -C -a ar=(\n\t\t[5]=(\n\t\t\ta4=1\n\t\t)\n\t)\n)'
109[[ $(print -v c) == "$exp" ]] || err_exit 'typeset -m "c.ar[5]=c.ar[4]" not working'
110
111typeset -T x_t=( hello=world )
112function m
113{
114	compound c
115	compound -a c.x
116	x_t c.x[4][5][8].field
117	x_t x
118	typeset -m c.x[4][6][9].field=x
119	exp=$'(\n\ttypeset -C -a x=(\n\t\t[4]=(\n\t\t\t[5]=(\n\t\t\t\t[8]=(\n\t\t\t\t\tx_t field=(\n\t\t\t\t\t\thello=world\n\t\t\t\t\t)\n\t\t\t\t)\n\t\t\t)\n\t\t\t[6]=(\n\t\t\t\t[9]=(\n\t\t\t\t\tx_t field=(\n\t\t\t\t\t\thello=world\n\t\t\t\t\t)\n\t\t\t\t)\n\t\t\t)\n\t\t)\n\t)\n)'
120	[[ $(print -v c) == "$exp" ]] || err_exit "typeset -m c.x[4][6][9].field=x where x is a type is not working"
121}
122m
123
124function moveme
125{
126	nameref src=$2 dest=$1
127	typeset -m dest=src
128}
129function main
130{
131	compound a=( aa=1 )
132	compound -a ar
133	moveme ar[4] a 2> /dev/null || err_exit 'function moveme fails'
134	exp=$'(\n\t[4]=(\n\t\taa=1\n\t)\n)'
135	[[ $(print -v ar) == "$exp" ]] || err_exit 'typeset -m dest=src where dest and src are name references fails'
136}
137main
138
139
140{
141$SHELL <<- \EOF
142	function main
143	{
144		compound c=(
145			compound -a board
146		)
147		for ((i=0 ; i < 2 ; i++ )) ; do
148			compound el=(typeset id='pawn')
149			typeset -m "c.board[1][i]=el"
150		done
151		exp=$'(\n\ttypeset -C -a board=(\n\t\t[1]=(\n\t\t\t(\n\t\t\t\tid=pawn\n\t\t\t)\n\t\t\t(\n\t\t\t\tid=pawn\n\t\t\t)\n\t\t)\n\t)\n)'
152		[[ $(print -v c) == "$exp" ]] || exit 1
153	}
154	main
155EOF
156} 2> /dev/null
157if	((exitval=$?))
158then	if	[[ $(kill -l $exitval) == SEGV ]]
159	then	err_exit 'typeset -m "c.board[1][i]=el" core dumps'
160	else	err_exit 'typeset -m "c.board[1][i]=el" gives wrong value'
161	fi
162fi
163exit $((Errors<125?Errors:125))
164