xref: /titanic_41/usr/src/lib/libshell/common/tests/treemove.sh (revision d2b5b2d357ee3172eacb6860be1891259902203d)
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 2008 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26
27#
28# This test checks whether "typeset -m" correctly moves local variables
29# into a global variable tree.
30#
31# This was reported as CR #XXXXXXXX ("XXXX"):
32# -- snip --
33#XXXX
34# -- snip --
35#
36
37function err_exit
38{
39	print -u2 -n "\t"
40	print -u2 -r ${Command}[$1]: "${@:2}"
41	(( Errors+=1 ))
42}
43
44alias err_exit='err_exit $LINENO'
45
46integer Errors=0
47
48## test start
49typeset -C tree1 tree2
50
51# add node to tree which uses "typeset -m" to move a local variable
52# into tree1.subtree["a_node"]
53function f1
54{
55	nameref tr=$1
56	typeset -A tr.subtree
57	typeset -C node
58	node.one="hello"
59	node.two="world"
60	# move local note into the array
61false
62	typeset -m tr.subtree["a_node"]=node
63	return 0
64}
65
66# Alternative version which uses "nameref" instead of "typeset -m"
67function f2
68{
69	nameref tr=$1
70	typeset -A tr.subtree
71	nameref node=tr.subtree["a_node"]
72	node.one="hello"
73	node.two="world"
74	return 0
75}
76
77f1 tree1
78f2 tree2
79
80[[ "${tree1.subtree["a_node"].one}" == "hello" ]] || err_exit "expected tree1.subtree[\"a_node\"].one == 'hello', got ${tree1.subtree["a_node"].one}"
81[[ "${tree1.subtree["a_node"].two}" == "world" ]] || err_exit "expected tree1.subtree[\"a_node\"].two == 'world', got ${tree1.subtree["a_node"].two}"
82[[ "${tree1}" == "${tree2}" ]] || err_exit "tree1 and tree2 differ:$'\n'"
83
84# tests done
85exit $((Errors))
86