8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libshell / common / tests / vartree1.sh
blob4701251549bd8a25be6c165fbf16f6e9e4c8c13e
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 ########################################################################
21 # variable tree test #001
22 # Propose of this test is whether ksh93 handles global variable trees
23 # and function-local variable trees the same way, including "nameref"
24 # and "unset" handling.
27 function err_exit
29 print -u2 -n "\t"
30 print -u2 -r ${Command}[$1]: "${@:2}"
31 (( Errors+=1 ))
34 alias err_exit='err_exit $LINENO'
36 function build_tree
38 #set -o errexit -o xtrace
39 typeset index
40 typeset s
41 typeset i
42 typeset dummy
43 typeset a b c d e f
45 nameref dest_tree="$1" # destination tree
46 nameref srcdata="$2" # source data
47 typeset tree_mode="$3" # mode to define the type of leads
49 typeset -A dest_tree.l1
51 for index in "${!srcdata.hashnodes[@]}" ; do
52 nameref node=srcdata.hashnodes["${index}"]
54 for i in "${node.xlfd[@]}" ; do
55 IFS='-' read dummy a b c d e f <<<"$i"
57 if [[ "$a" == "" ]] ; then
58 a="$dummy"
61 [[ "$a" == "" ]] && a='-'
62 [[ "$b" == "" ]] && b='-'
63 [[ "$c" == "" ]] && c='-'
65 if [[ "${dest_tree.l1["$a"]}" == "" ]] ; then
66 #if ! (unset dest_tree.l1["$a"]) ; then
67 typeset -A dest_tree.l1["$a"].l2
70 if [[ "${dest_tree.l1["$a"].l2["$b"]}" == "" ]] ; then
71 #if ! (unset dest_tree.l1["$a"].l2["$b"]) ; then
72 typeset -A dest_tree.l1["$a"].l2["$b"].l3
75 if [[ "${!dest_tree.l1["$a"].l2["$b"].l3["$c"].entries[*]}" == "" ]] ; then
76 typeset -A dest_tree.l1["$a"].l2["$b"].l3["$c"].entries
79 #dest_tree.l1["$a"].l2["$b"].l3["$c"].entries+=( "$index" )
80 typeset new_index
81 if [[ "${tree_mode}" == "leaf_name" ]] ; then
82 new_index=$(( ${#dest_tree.l1["$a"].l2["$b"].l3["$c"].entries[@]}+1 ))
83 else
84 new_index="${node.name}"
86 # skip if the leaf node already exists
87 if [[ "${dest_tree.l1["$a"].l2["$b"].l3["$c"].entries[${new_index}]}" != "" ]] ; then
88 continue
92 add_tree_leaf dest_tree.l1["$a"].l2["$b"].l3["$c"].entries[${new_index}] "${index}" "${tree_mode}"
93 done
94 done
96 return 0
99 function add_tree_leaf
101 nameref tree_leafnode="$1"
102 nameref data_node=srcdata.hashnodes["$2"]
103 typeset add_mode="$3"
105 case "${add_mode}" in
106 "leaf_name")
107 tree_leafnode="${data_node.name}"
108 return 0
110 "leaf_compound")
111 tree_leafnode=(
112 typeset name="${data_node.name}"
113 typeset -a filenames=( "${data_node.filenames[@]}" )
114 typeset -a comments=( "${data_node.comments[@]}" )
115 typeset -a xlfd=( "${data_node.xlfd[@]}" )
117 return 0
120 print -u2 -f "ERROR: Unknown mode %s in add_tree_leaf\n" "${add_mode}"
121 return 1
123 esac
125 # not reached
126 return 1
129 # "mysrcdata_local" and "mysrcdata_global" must be identical
130 typeset mysrcdata_global=(
131 typeset -A hashnodes=(
132 [abcd]=(
133 name='abcd'
134 typeset -a xlfd=(
135 '-urw-itc zapfchancery-medium-i-normal--0-0-0-0-p-0-iso8859-1'
136 '-urw-itc zapfdingbats-medium-r-normal--0-0-0-0-p-0-adobe-fontspecific'
137 '-urw-itc zapfdingbats-medium-r-normal--0-0-0-0-p-0-sun-fontspecific'
139 typeset -a comments=(
140 'comment 1'
141 'comment 2'
142 'comment 3'
144 typeset -a filenames=(
145 '/home/foo/abcd_1'
146 '/home/foo/abcd_2'
147 '/home/foo/abcd_3'
153 mytree_global=()
155 function main
157 # "mysrcdata_local" and "mysrcdata_global" must be identical
158 typeset mysrcdata_local=(
159 typeset -A hashnodes=(
160 [abcd]=(
161 name='abcd'
162 typeset -a xlfd=(
163 '-urw-itc zapfchancery-medium-i-normal--0-0-0-0-p-0-iso8859-1'
164 '-urw-itc zapfdingbats-medium-r-normal--0-0-0-0-p-0-adobe-fontspecific'
165 '-urw-itc zapfdingbats-medium-r-normal--0-0-0-0-p-0-sun-fontspecific'
167 typeset -a comments=(
168 'comment 1'
169 'comment 2'
170 'comment 3'
172 typeset -a filenames=(
173 '/home/foo/abcd_1'
174 '/home/foo/abcd_2'
175 '/home/foo/abcd_3'
181 # build tree using global tree variables
182 build_tree mytree_global mysrcdata_global leaf_compound || \
183 err_exit 'build_tree mytree_global mysrcdata_global leaf_compound returned an error'
185 (( $(print -r -- "${mytree_global}" | wc -l) > 10 )) || err_exit "compound tree 'mytree_global' too small"
187 # build tree using local tree variables
188 mytree_local=()
189 build_tree mytree_local mysrcdata_local leaf_compound || \
190 err_exit 'build_tree mytree_local mysrcdata_local leaf_compound returned an error'
192 (( $(print -r -- "${mytree_local}" | wc -l) > 10 )) || err_exit "compound tree 'mytree_local' too small"
194 # Compare trees
195 if [[ "${mytree_global}" != "${mytree_local}" ]] ; then
196 err_exit "compound trees 'mytree_local' and 'mytree_global' not identical"
199 unset 'mytree_global.l1[urw].l2[itc zapfdingbats].l3[medium].entries[abcd].filenames[0]' ||
200 err_exit "variable 'mytree_global.l1[urw].l2[itc zapfdingbats].l3[medium].entries[abcd].filenames[0]' not found"
202 [[ "${mytree_global}" != "${mytree_local}" ]] || err_exit "mytree_global and mytree_local should differ"
204 unset 'mytree_local.l1[urw].l2[itc zapfdingbats].l3[medium].entries[abcd].filenames[0]' ||
205 err_exit "variable 'mytree_local.l1[urw].l2[itc zapfdingbats].l3[medium].entries[abcd].filenames[0]' not found"
207 # Compare trees (after "unset")
208 if [[ "${mytree_global}" != "${mytree_local}" ]] ; then
209 err_exit "compound trees 'mytree_local' and 'mytree_global' not identical after unset"
213 main
214 exit $((Errors))