134f9b3eeSRoland Mainz#
234f9b3eeSRoland Mainz# CDDL HEADER START
334f9b3eeSRoland Mainz#
434f9b3eeSRoland Mainz# The contents of this file are subject to the terms of the
534f9b3eeSRoland Mainz# Common Development and Distribution License (the "License").
634f9b3eeSRoland Mainz# You may not use this file except in compliance with the License.
734f9b3eeSRoland Mainz#
834f9b3eeSRoland Mainz# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
934f9b3eeSRoland Mainz# or http://www.opensolaris.org/os/licensing.
1034f9b3eeSRoland Mainz# See the License for the specific language governing permissions
1134f9b3eeSRoland Mainz# and limitations under the License.
1234f9b3eeSRoland Mainz#
1334f9b3eeSRoland Mainz# When distributing Covered Code, include this CDDL HEADER in each
1434f9b3eeSRoland Mainz# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1534f9b3eeSRoland Mainz# If applicable, add the following below this CDDL HEADER, with the
1634f9b3eeSRoland Mainz# fields enclosed by brackets "[]" replaced with your own identifying
1734f9b3eeSRoland Mainz# information: Portions Copyright [yyyy] [name of copyright owner]
1834f9b3eeSRoland Mainz#
1934f9b3eeSRoland Mainz# CDDL HEADER END
2034f9b3eeSRoland Mainz#
2134f9b3eeSRoland Mainz
2234f9b3eeSRoland Mainz#
233e14f97fSRoger A. Faulkner# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
2434f9b3eeSRoland Mainz#
2534f9b3eeSRoland Mainz
2634f9b3eeSRoland Mainz#
2734f9b3eeSRoland Mainz# This test checks whether "typeset -m" correctly moves local variables
2834f9b3eeSRoland Mainz# into a global variable tree.
2934f9b3eeSRoland Mainz#
3034f9b3eeSRoland Mainz# This was reported as CR #6805792 ("XXXX"):
3134f9b3eeSRoland Mainz# -------- snip --------
3234f9b3eeSRoland Mainz# The following attempt to move a local node into an associative array
3334f9b3eeSRoland Mainz# fails like this:
3434f9b3eeSRoland Mainz# -- snip --
3534f9b3eeSRoland Mainz# typeset -C tree
3634f9b3eeSRoland Mainz# function f1
3734f9b3eeSRoland Mainz# {
3834f9b3eeSRoland Mainz#        nameref tr=$1
39*b30d1939SAndy Fiddaman#
4034f9b3eeSRoland Mainz#        typeset -A tr.subtree
41*b30d1939SAndy Fiddaman#
4234f9b3eeSRoland Mainz#        typeset -C node
43*b30d1939SAndy Fiddaman#
4434f9b3eeSRoland Mainz#        node.one="hello"
4534f9b3eeSRoland Mainz#        node.two="world"
46*b30d1939SAndy Fiddaman#
4734f9b3eeSRoland Mainz#        # move local note into the array
4834f9b3eeSRoland Mainz#        typeset -m tr.subtree["a_node"]=node
49*b30d1939SAndy Fiddaman#
5034f9b3eeSRoland Mainz#        return 0
5134f9b3eeSRoland Mainz# }
5234f9b3eeSRoland Mainz# f1 tree
5334f9b3eeSRoland Mainz# printf "%B\n" tree
5434f9b3eeSRoland Mainz# print "ok"
5534f9b3eeSRoland Mainz# exit 0
5634f9b3eeSRoland Mainz# -- snip --
5734f9b3eeSRoland Mainz# The output looks like this:
5834f9b3eeSRoland Mainz# -- snip --
5934f9b3eeSRoland Mainz# $ ksh93
6034f9b3eeSRoland Mainz# varmovetest1.sh
6134f9b3eeSRoland Mainz# (
6234f9b3eeSRoland Mainz# (
6334f9b3eeSRoland Mainz# )
6434f9b3eeSRoland Mainz# ok
6534f9b3eeSRoland Mainz# -- snip --
6634f9b3eeSRoland Mainz# ... but AFAIK it should print:
6734f9b3eeSRoland Mainz# -- snip --
6834f9b3eeSRoland Mainz# (
6934f9b3eeSRoland Mainz#        typeset -A subtree=(
7034f9b3eeSRoland Mainz#                [a_node]=(
7134f9b3eeSRoland Mainz#                        one=hello
7234f9b3eeSRoland Mainz#                        two=world
7334f9b3eeSRoland Mainz#                )
7434f9b3eeSRoland Mainz#        )
7534f9b3eeSRoland Mainz# )
7634f9b3eeSRoland Mainz# ok
7734f9b3eeSRoland Mainz# -- snip --
7834f9b3eeSRoland Mainz# -------- snip --------
7934f9b3eeSRoland Mainz#
8034f9b3eeSRoland Mainz
8134f9b3eeSRoland Mainz# test setup
8234f9b3eeSRoland Mainzfunction err_exit
8334f9b3eeSRoland Mainz{
8434f9b3eeSRoland Mainz	print -u2 -n "\t"
8534f9b3eeSRoland Mainz	print -u2 -r ${Command}[$1]: "${@:2}"
863e14f97fSRoger A. Faulkner	(( Errors < 127 && Errors++ ))
8734f9b3eeSRoland Mainz}
8834f9b3eeSRoland Mainzalias err_exit='err_exit $LINENO'
8934f9b3eeSRoland Mainz
9034f9b3eeSRoland Mainzset -o nounset
9134f9b3eeSRoland MainzCommand=${0##*/}
9234f9b3eeSRoland Mainzinteger Errors=0
9334f9b3eeSRoland Mainz
9434f9b3eeSRoland Mainz
9534f9b3eeSRoland Mainz## test start
9634f9b3eeSRoland Mainzcompound tree1 tree2
9734f9b3eeSRoland Mainz
9834f9b3eeSRoland Mainz# add node to tree which uses "typeset -m" to move a local variable
9934f9b3eeSRoland Mainz# into tree1.subtree["a_node"]
10034f9b3eeSRoland Mainzfunction f1
10134f9b3eeSRoland Mainz{
10234f9b3eeSRoland Mainz	nameref tr=$1
103*b30d1939SAndy Fiddaman
10434f9b3eeSRoland Mainz	typeset -A tr.subtree
105*b30d1939SAndy Fiddaman
10634f9b3eeSRoland Mainz	compound node
107*b30d1939SAndy Fiddaman
10834f9b3eeSRoland Mainz	node.one="dummy1"
10934f9b3eeSRoland Mainz	node.two="dummy2"
110*b30d1939SAndy Fiddaman
11134f9b3eeSRoland Mainz	# We use the nameref's here since ast-ksh,2008-12-12 crashes
11234f9b3eeSRoland Mainz	# when this function returns because "nodeone" and "nodetwo"
11334f9b3eeSRoland Mainz	# still reference "node" which was renamed.
11434f9b3eeSRoland Mainz	# (note that "f1" must be first function and the first being
11534f9b3eeSRoland Mainz	# called, otherwise the crash will not occur)
11634f9b3eeSRoland Mainz	nameref nodeone=node.one
11734f9b3eeSRoland Mainz	nameref nodetwo=node.two
11834f9b3eeSRoland Mainz	nodeone="hello"
11934f9b3eeSRoland Mainz	nodetwo="world"
120*b30d1939SAndy Fiddaman
12134f9b3eeSRoland Mainz	# move local note into the array
12234f9b3eeSRoland Mainz	typeset -m tr.subtree["a_node"]=node
123*b30d1939SAndy Fiddaman
12434f9b3eeSRoland Mainz	return 0
12534f9b3eeSRoland Mainz}
12634f9b3eeSRoland Mainz
12734f9b3eeSRoland Mainz# Alternative version which uses "nameref" instead of "typeset -m"
12834f9b3eeSRoland Mainzfunction f2
12934f9b3eeSRoland Mainz{
13034f9b3eeSRoland Mainz	nameref tr=$1
131*b30d1939SAndy Fiddaman
13234f9b3eeSRoland Mainz	typeset -A tr.subtree
133*b30d1939SAndy Fiddaman
13434f9b3eeSRoland Mainz	nameref node=tr.subtree["a_node"]
135*b30d1939SAndy Fiddaman
13634f9b3eeSRoland Mainz	node.one="hello"
13734f9b3eeSRoland Mainz	node.two="world"
138*b30d1939SAndy Fiddaman
13934f9b3eeSRoland Mainz	return 0
14034f9b3eeSRoland Mainz}
14134f9b3eeSRoland Mainz
14234f9b3eeSRoland Mainzf1 tree1
14334f9b3eeSRoland Mainzf2 tree2
14434f9b3eeSRoland Mainz
14534f9b3eeSRoland Mainz[[ "${tree1.subtree["a_node"].one}" == "hello" ]] || err_exit "Expected tree1.subtree[\"a_node\"].one == 'hello', got ${tree1.subtree["a_node"].one}"
14634f9b3eeSRoland Mainz[[ "${tree1.subtree["a_node"].two}" == "world" ]] || err_exit "Expected tree1.subtree[\"a_node\"].two == 'world', got ${tree1.subtree["a_node"].two}"
14734f9b3eeSRoland Mainz[[ "${tree1}" == "${tree2}" ]] || err_exit "tree1 and tree2 differ:"$'\n'"$(diff -u <( printf '%B\n' tree1 ) <( printf '%B\n' tree2 ) )"
14834f9b3eeSRoland Mainz
14934f9b3eeSRoland Mainz
15034f9b3eeSRoland Mainz# tests done
15134f9b3eeSRoland Mainzexit $((Errors))
152