xref: /titanic_51/usr/src/lib/libshell/common/tests/comvar.sh (revision 3e14f97f673e8a630f076077de35afdd43dc1587)
1da2e3ebdSchin########################################################################
2da2e3ebdSchin#                                                                      #
3da2e3ebdSchin#               This software is part of the ast package               #
4*3e14f97fSRoger A. Faulkner#          Copyright (c) 1982-2010 AT&T Intellectual Property          #
5da2e3ebdSchin#                      and is licensed under the                       #
6da2e3ebdSchin#                  Common Public License, Version 1.0                  #
77c2fbfb3SApril Chin#                    by AT&T Intellectual Property                     #
8da2e3ebdSchin#                                                                      #
9da2e3ebdSchin#                A copy of the License is available at                 #
10da2e3ebdSchin#            http://www.opensource.org/licenses/cpl1.0.txt             #
11da2e3ebdSchin#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
12da2e3ebdSchin#                                                                      #
13da2e3ebdSchin#              Information and Software Systems Research               #
14da2e3ebdSchin#                            AT&T Research                             #
15da2e3ebdSchin#                           Florham Park NJ                            #
16da2e3ebdSchin#                                                                      #
17da2e3ebdSchin#                  David Korn <dgk@research.att.com>                   #
18da2e3ebdSchin#                                                                      #
19da2e3ebdSchin########################################################################
20da2e3ebdSchinfunction err_exit
21da2e3ebdSchin{
22da2e3ebdSchin	print -u2 -n "\t"
23da2e3ebdSchin	print -u2 -r ${Command}[$1]: "${@:2}"
24da2e3ebdSchin	let Errors+=1
25da2e3ebdSchin}
26da2e3ebdSchinalias err_exit='err_exit $LINENO'
27da2e3ebdSchin
28da2e3ebdSchin#test for compound variables
29da2e3ebdSchinCommand=${0##*/}
30da2e3ebdSchininteger Errors=0
31da2e3ebdSchinPoint=(
32da2e3ebdSchin	float x=1. y=0.
33da2e3ebdSchin)
34da2e3ebdSchineval p="$Point"
35da2e3ebdSchinif	(( (p.x*p.x + p.y*p.y) > 1.01 ))
36da2e3ebdSchinthen	err_exit 'compound variable not working'
37da2e3ebdSchinfi
38da2e3ebdSchinnameref foo=p
39da2e3ebdSchinif	[[ ${foo.x} != ${Point.x} ]]
40da2e3ebdSchinthen	err_exit 'reference to compound object not working'
41da2e3ebdSchinfi
42da2e3ebdSchinunset foo
43da2e3ebdSchinrec=(
44da2e3ebdSchin	name='Joe Blow'
45da2e3ebdSchin	born=(
46da2e3ebdSchin		month=jan
47da2e3ebdSchin		integer day=16
48da2e3ebdSchin		year=1980
49da2e3ebdSchin	)
50da2e3ebdSchin)
51da2e3ebdSchineval newrec="$rec"
52da2e3ebdSchinif	[[ ${newrec.name} != "${rec.name}" ]]
53da2e3ebdSchinthen	err_exit 'copying a compound object not working'
54da2e3ebdSchinfi
55da2e3ebdSchinif	(( newrec.born.day != 16 ))
56da2e3ebdSchinthen	err_exit 'copying integer field of  compound object not working'
57da2e3ebdSchinfi
58da2e3ebdSchinp_t=(
59da2e3ebdSchin        integer z=0
60da2e3ebdSchin        typeset -A tokens
61da2e3ebdSchin)
62da2e3ebdSchinunset x
63da2e3ebdSchintypeset -A x
64da2e3ebdSchinx=( [foo]=bar )
65da2e3ebdSchinif	[[ ${x[@]} != bar ]]
66da2e3ebdSchinthen	err_exit 'compound assignemnt of associative arrays not working'
67da2e3ebdSchinfi
68da2e3ebdSchinunset -n foo x
69da2e3ebdSchinunset foo x
70da2e3ebdSchinfoo=( x=3)
71da2e3ebdSchinnameref x=foo
72da2e3ebdSchinif	[[ ${!x.@} != foo.x ]]
73da2e3ebdSchinthen	err_exit 'name references not expanded on prefix matching'
74da2e3ebdSchinfi
75da2e3ebdSchinunset x
767c2fbfb3SApril Chinunset -n x
77da2e3ebdSchin(
78da2e3ebdSchin	x=()
79da2e3ebdSchin	x.foo.bar=7
80da2e3ebdSchin	[[ ${x.foo.bar} == 7 ]] || err_exit '[[ ${x.foo.bar} != 7 ]]'
81da2e3ebdSchin	(( x.foo.bar == 7  ))|| err_exit '(( x.foo.bar != 7 ))'
82da2e3ebdSchin	[[ ${x.foo} == *bar=7*  ]] || err_exit '[[ ${x.foo} != *bar=7* ]]'
83da2e3ebdSchin)
84da2e3ebdSchinfoo=(integer x=3)
85da2e3ebdSchinif	[[ ${foo} != *x=3* ]]
86da2e3ebdSchinthen	err_exit "compound variable with integer subvariable not working"
87da2e3ebdSchinfi
88da2e3ebdSchin$SHELL -c $'x=(foo=bar)\n[[ x == x ]]' 2> /dev/null ||
89da2e3ebdSchin	err_exit '[[ ... ]] not working after compound assignment'
90da2e3ebdSchinunset foo
91da2e3ebdSchin[[ ${!foo.@} ]] && err_exit 'unset compound variable leaves subvariables'
92da2e3ebdSchinsuitable=(
93da2e3ebdSchin  label="Table Viewer"
94da2e3ebdSchin  langs="ksh"
95da2e3ebdSchin  uselang=ksh
96da2e3ebdSchin  launch=no
97da2e3ebdSchin  groups="default"
98da2e3ebdSchin  default=(
99da2e3ebdSchin    label="Table Viewer Preferences"
100da2e3ebdSchin    entrylist=" \
101da2e3ebdSchin      vieworigin viewsize viewcolor viewfontname viewfontsize \
102da2e3ebdSchin      showheader header showfooter footer showtitle title showlegends \
103da2e3ebdSchin      class_td_lg1_style class_tr_tr1_style \
104da2e3ebdSchin      class_th_th1_style class_td_td1_style \
105da2e3ebdSchin      fields fieldorder \
106da2e3ebdSchin    "
107da2e3ebdSchin    entries=(
108da2e3ebdSchin      vieworigin=(
109da2e3ebdSchin        type=coord var=vieworigin val="0 0" label="Window Position"
110da2e3ebdSchin      )
111da2e3ebdSchin      viewsize=(
112da2e3ebdSchin        type=coord var=viewsize val="400 400" label="Window Size"
113da2e3ebdSchin      )
114da2e3ebdSchin      viewcolor=(
115da2e3ebdSchin        type=2colors var=viewcolor val="gray black"
116da2e3ebdSchin        label="Window Colors"
117da2e3ebdSchin      )
118da2e3ebdSchin      viewfontname=(
119da2e3ebdSchin        type=fontname var=viewfontname val="Times-Roman"
120da2e3ebdSchin        label="Window Font Name"
121da2e3ebdSchin      )
122da2e3ebdSchin      viewfontsize=(
123da2e3ebdSchin        type=fontsize var=viewfontsize val=14 label="Window Font Size"
124da2e3ebdSchin      )
125da2e3ebdSchin
126da2e3ebdSchin      showheader=(
127da2e3ebdSchin        type=yesno var=showheader val=no label="Show Header"
128da2e3ebdSchin      )
129da2e3ebdSchin      header=(
130da2e3ebdSchin        type=text var=header val="" label="Header"
131da2e3ebdSchin      )
132da2e3ebdSchin
133da2e3ebdSchin      showfooter=(
134da2e3ebdSchin        type=yesno var=showfooter val=no label="Show Footer"
135da2e3ebdSchin      )
136da2e3ebdSchin      footer=(
137da2e3ebdSchin        type=text var=footer val="" label="Footer"
138da2e3ebdSchin      )
139da2e3ebdSchin
140da2e3ebdSchin      showtitle=(
141da2e3ebdSchin        type=yesno var=showtitle val=yes label="Show Title"
142da2e3ebdSchin      )
143da2e3ebdSchin      title=(
144da2e3ebdSchin        type=text var=title val="SWIFTUI - Table View" label="Title"
145da2e3ebdSchin      )
146da2e3ebdSchin
147da2e3ebdSchin      showlegends=(
148da2e3ebdSchin        type=yesno var=showlegends val=yes label="Show Legends"
149da2e3ebdSchin      )
150da2e3ebdSchin
151da2e3ebdSchin      class_td_lg1_style=(
152da2e3ebdSchin        type=style var=class_td_lg1_style
153da2e3ebdSchin        val="color: black; font-family: Times-Roman; font-size: 14pt"
154da2e3ebdSchin        label="Legend 1 Style"
155da2e3ebdSchin      )
156da2e3ebdSchin
157da2e3ebdSchin      class_tr_tr1_style=(
158da2e3ebdSchin        type=style var=class_tr_tr1_style val="background: black"
159da2e3ebdSchin        label="Table Row 1 Style"
160da2e3ebdSchin      )
161da2e3ebdSchin
162da2e3ebdSchin      class_th_th1_style=(
163da2e3ebdSchin        type=style var=class_th_th1_style
164da2e3ebdSchin        val="color: black; font-family: Times-Roman; font-size: 14pt; text-align: left"
165da2e3ebdSchin        label="Table Header 1 Style"
166da2e3ebdSchin      )
167da2e3ebdSchin
168da2e3ebdSchin      class_td_td1_style=(
169da2e3ebdSchin        type=style var=class_td_td1_style
170da2e3ebdSchin        val="color: black; font-family: Times-Roman; font-size: 14pt; text-align: left"
171da2e3ebdSchin        label="Table Cell 1 Style"
172da2e3ebdSchin      )
173da2e3ebdSchin
174da2e3ebdSchin      fields=(
175da2e3ebdSchin        type=text var=fields val= label="List of Fields"
176da2e3ebdSchin      )
177da2e3ebdSchin      fieldorder=(
178da2e3ebdSchin        type=text var=fieldorder val= label="Order of Fields"
179da2e3ebdSchin      )
180da2e3ebdSchin    )
181da2e3ebdSchin  )
182da2e3ebdSchin)
183da2e3ebdSchin[[ "${suitable}" == *entrylist=* ]] || err_exit 'compound variable expansion omitting fields'
184da2e3ebdSchinfoo=( bar=foo  barbar=bar)
185da2e3ebdSchin[[ $foo == *bar=foo* ]] || err_exit 'no prefix elements in compound variable output'
186da2e3ebdSchinfunction localvar
187da2e3ebdSchin{
188da2e3ebdSchin	typeset point=(typeset -i x=3 y=4)
189da2e3ebdSchin	(( (point.x*point.x + point.y*point.y) == 25 )) || err_exit "local compound variable not working"
190da2e3ebdSchin}
191da2e3ebdSchinpoint=(integer x=6 y=8)
192da2e3ebdSchinlocalvar
193da2e3ebdSchin	(( (point.x*point.x + point.y*point.y) == 100 )) || err_exit "global compound variable not preserved"
194da2e3ebdSchin[[ $($SHELL -c 'foo=();foo.[x]=(y z); print ${foo.x[@]}') == 'y z' ]] 2> /dev/null || err_exit 'foo=( [x]=(y z)  not working'
1957c2fbfb3SApril Chinfunction staticvar
1967c2fbfb3SApril Chin{
1977c2fbfb3SApril Chin	if	[[ $1 ]]
1987c2fbfb3SApril Chin	then	print -r -- "$point"
1997c2fbfb3SApril Chin		return
2007c2fbfb3SApril Chin	fi
2017c2fbfb3SApril Chin        typeset -S point=(typeset -i x=3 y=4)
2027c2fbfb3SApril Chin        (( (point.x*point.x + point.y*point.y) == 25 )) || err_exit "local compound variable not working"
2037c2fbfb3SApril Chin	point.y=5
2047c2fbfb3SApril Chin	point.z=foobar
2057c2fbfb3SApril Chin}
2067c2fbfb3SApril Chinstaticvar
2077c2fbfb3SApril Chin        (( (point.x*point.x + point.y*point.y) == 100 )) || err_exit "global compound variable not preserved"
2087c2fbfb3SApril Chin[[ $(staticvar x) == $'(\n\ttypeset -i x=3\n\ttypeset -i y=5\n\tz=foobar\n)' ]] || err_exit 'static variables in function not working'
2097c2fbfb3SApril Chininteger x=3
2107c2fbfb3SApril Chin( typeset -S x=+++)2> /dev/null  || err_exit "typeset -S doesn't unset first"
2117c2fbfb3SApril Chin
212da2e3ebdSchinunset z
213da2e3ebdSchin( [[ ${z.foo.bar:-abc} == abc ]] 2> /dev/null) || err_exit ':- not working with compound variables'
2147c2fbfb3SApril Chinstack=()
2157c2fbfb3SApril Chintypeset -a stack.items=([0]=foo [1]=bar)
2167c2fbfb3SApril Chin[[ ${stack.items[0]} == foo ]] || err_exit 'typeset -a variable not expanding correctly'
2177c2fbfb3SApril Chin$SHELL -c 'typeset -a info=( [1]=( passwd=( since=2005-07-20) ))'  || err_exit 'problem with embedded index array in compound variable'
2187c2fbfb3SApril Chinx=(foo=([1]=(y=([2]=(z=4)))))
2197c2fbfb3SApril Chin[[ $x == *'.y'=* ]] && err_exit 'expansion with bogus leading . in name'
2207c2fbfb3SApril Chinunset z
2217c2fbfb3SApril Chinz=1
2227c2fbfb3SApril Chinfunction foo
2237c2fbfb3SApril Chin{
2247c2fbfb3SApril Chin	z=3
2257c2fbfb3SApril Chin	[[ ${a.z} == 3 ]] && err_exit "\${a.z} should not be 3"
2267c2fbfb3SApril Chin	print hi
2277c2fbfb3SApril Chin}
2287c2fbfb3SApril China=( b=$(foo) )
2297c2fbfb3SApril Chin[[ ${a.z} == 3 ]] &&  err_exit 'a.z should not be set to 3'
2307c2fbfb3SApril Chinfunction a.b.get
2317c2fbfb3SApril Chin{
2327c2fbfb3SApril Chin	.sh.value=foo
2337c2fbfb3SApril Chin}
2347c2fbfb3SApril Chin{ b=( b1=${a.b} ) ;} 2> /dev/null
2357c2fbfb3SApril Chin[[ ${b.b1} == foo ]] || err_exit '${b.b1} should be foo'
2367c2fbfb3SApril Chinfunction dcl1
2377c2fbfb3SApril Chin{
2387c2fbfb3SApril Chin     eval 'a=1
2397c2fbfb3SApril Chin     function a.set
2407c2fbfb3SApril Chin     { print ${.sh.name}=${.sh.value}; }'
2417c2fbfb3SApril Chin}
2427c2fbfb3SApril Chinfunction dcl2
2437c2fbfb3SApril Chin{
2447c2fbfb3SApril Chin     eval 'b=(typeset x=0; typeset y=0 )
2457c2fbfb3SApril Chin     function b.x.set
2467c2fbfb3SApril Chin     { print ${.sh.name}=${.sh.value}; }'
2477c2fbfb3SApril Chin}
2487c2fbfb3SApril Chindcl1
2497c2fbfb3SApril Chin[[ ${ a=123;} == 'a=123' ]] || err_exit 'should be a=123'
2507c2fbfb3SApril Chindcl2
2517c2fbfb3SApril Chin[[ ${ b.x=456;} == 'b.x=456' ]] || err_exit 'should be b.x=456'
2527c2fbfb3SApril Chineval 'b=(typeset x=0; typeset y=0 )
2537c2fbfb3SApril Chinfunction b.x.set
2547c2fbfb3SApril Chin{ print ${.sh.name}=${.sh.value}; }' > /dev/null
2557c2fbfb3SApril Chin[[ ${ b.x=789;} == 'b.x=789' ]] || err_exit 'should be b.x=789'
2567c2fbfb3SApril Chinunset a b
2577c2fbfb3SApril Chinfunction func
2587c2fbfb3SApril Chin{
2597c2fbfb3SApril Chin	typeset X
2607c2fbfb3SApril Chin	X=( bar=2 )
2617c2fbfb3SApril Chin}
262da2e3ebdSchin
2637c2fbfb3SApril ChinX=( foo=1 )
2647c2fbfb3SApril Chinfunc
2657c2fbfb3SApril Chin[[ $X == $'(\n\tfoo=1\n)' ]] || err_exit 'scoping problem with compound variables'
2667c2fbfb3SApril Chinunset foo
2677c2fbfb3SApril Chintypeset -A foo=([a]=aa;[b]=bb;[c]=cc)
2687c2fbfb3SApril Chin[[ ${foo[c]} == cc ]] || err_exit 'associative array assignment with; not working'
2697c2fbfb3SApril Chin[[ $({ $SHELL -c 'x=(); typeset -a x.foo; x.foo=bar; print -r -- "$x"' ;} 2> /dev/null) == $'(\n\ttypeset -a foo=bar\n)' ]] || err_exit 'indexed array in compound variable with only element 0 defined fails'
2707c2fbfb3SApril Chinunset foo
2717c2fbfb3SApril Chinfoo=(typeset -a bar)
2727c2fbfb3SApril Chin[[ $foo  == *'typeset -a bar'* ]] || err_exit 'array attribute -a not preserved in compound variable'
2737c2fbfb3SApril Chinunset s
2747c2fbfb3SApril Chintypeset -A s=( [foo]=(y=2 z=3) [bar]=(y=4 z=5))
2757c2fbfb3SApril Chin[[ ${s[@]} == *z=*z=* ]] || err_exit 'missing elements in compound associative array'
2767c2fbfb3SApril Chinunset nodes
2777c2fbfb3SApril Chintypeset -A nodes
2787c2fbfb3SApril Chinnodes[0]+=( integer x=5)
2797c2fbfb3SApril Chin[[ ${nodes[0].x} == 5 ]] || err_exit '${nodes[0].x} should be 5'
2807c2fbfb3SApril Chinunset foo
2817c2fbfb3SApril Chintypeset -C foo
2827c2fbfb3SApril Chinfoo.bar=abc
2837c2fbfb3SApril Chin[[ $foo == $'(\n\tbar=abc\n)' ]] || err_exit 'typeset -C not working for foo'
2847c2fbfb3SApril Chintypeset -C foo=(bar=def)
2857c2fbfb3SApril Chin[[ $foo == $'(\n\tbar=def\n)' ]] || err_exit 'typeset -C not working when initialized'
2867c2fbfb3SApril Chinfoo=(
2877c2fbfb3SApril Chin	hello=ok
2887c2fbfb3SApril Chin	yes=( bam=2 yes=4)
2897c2fbfb3SApril Chin	typeset -A array=([one]=one [two]=2)
2907c2fbfb3SApril Chin	last=me
2917c2fbfb3SApril Chin)
2927c2fbfb3SApril Chineval foo2="$foo"
2937c2fbfb3SApril Chinfoo2.hello=notok foo2.yes.yex=no foo2.extra=yes.
2947c2fbfb3SApril Chintypeset -C bar bam
2957c2fbfb3SApril Chin{
2967c2fbfb3SApril Chin	read -Cu3 bar
2977c2fbfb3SApril Chin	read -Cu3 bam
2987c2fbfb3SApril Chin	read -ru3
2997c2fbfb3SApril Chin} 3<<- ++++
3007c2fbfb3SApril Chin	"$foo"
3017c2fbfb3SApril Chin	"$foo2"
3027c2fbfb3SApril Chin	last line
3037c2fbfb3SApril Chin++++
3047c2fbfb3SApril Chin[[ $? == 0 ]] || err_exit ' read -C failed'
3057c2fbfb3SApril Chin[[ $bar == "$foo" ]] || err_exit '$foo != $bar'
3067c2fbfb3SApril Chin[[ $bam == "$foo2" ]] || err_exit '$foo2 != $bmr'
3077c2fbfb3SApril Chin[[ $REPLY == 'last line' ]] || err_exit "\$REPLY=$REPLY should be 'last line"
3087c2fbfb3SApril Chintypeset x=( typeset -a foo=( [1][3]=hello [9][2]="world" ) )
3097c2fbfb3SApril Chineval y="(typeset -a foo=$(printf "%B\n" x.foo) )"
3107c2fbfb3SApril Chin[[ $x == "$y" ]] || err_exit '$x.foo != $y.foo with %B'
3117c2fbfb3SApril Chineval y="(typeset -a foo=$(printf "%#B\n" x.foo) )"
3127c2fbfb3SApril Chin[[ $x == "$y" ]] || err_exit '$x.foo != $y.foo with %#B'
3137c2fbfb3SApril Chineval y="$(printf "%B\n" x)"
3147c2fbfb3SApril Chin[[ $x == "$y" ]] || err_exit '$x != $y with %B'
3157c2fbfb3SApril Chineval y="$(printf "%#B\n" x)"
3167c2fbfb3SApril Chin[[ $x == "$y" ]] || err_exit '$x != $y with %#B'
3177c2fbfb3SApril Chiny=$(set | grep ^x=) 2> /dev/null
3187c2fbfb3SApril Chineval "${y/#x/y}"
3197c2fbfb3SApril Chin[[ $x == "$y" ]] || err_exit '$x != $y with set | grep'
3207c2fbfb3SApril Chinunset x y z
3217c2fbfb3SApril Chinx=( float x=0 y=1; z=([foo]=abc [bar]=def))
3227c2fbfb3SApril Chintypeset -C y=x
3237c2fbfb3SApril Chin[[ $x == "$y" ]] || err_exit '$x != $y with typeset -C'
3247c2fbfb3SApril Chinunset y
3257c2fbfb3SApril Chiny=()
3267c2fbfb3SApril Chiny=x
3277c2fbfb3SApril Chin[[ $x == "$y" ]] || err_exit '$x != $y when x=y and x and y are -C '
3287c2fbfb3SApril Chinfunction foobar
3297c2fbfb3SApril Chin{
3307c2fbfb3SApril Chin	typeset -C z
3317c2fbfb3SApril Chin	z=x
3327c2fbfb3SApril Chin	[[ $x == "$z" ]] || err_exit '$x != $z when x=z and x and z are -C '
3337c2fbfb3SApril Chin	y=z
3347c2fbfb3SApril Chin}
3357c2fbfb3SApril Chin[[ $x == "$y" ]] || err_exit '$x != $y when x=y -C copied in a function '
3367c2fbfb3SApril Chinz=(foo=abc)
3377c2fbfb3SApril Chiny+=z
3387c2fbfb3SApril Chin[[ $y == *foo=abc* ]] || err_exit 'z not appended to y'
3397c2fbfb3SApril Chinunset y.foo
3407c2fbfb3SApril Chin[[ $x == "$y" ]] || err_exit '$x != $y when y.foo deleted'
3417c2fbfb3SApril Chinunset x y
3427c2fbfb3SApril Chinx=( foo=(z=abc d=ghi) bar=abc; typeset -A r=([x]=3  [y]=4))
3437c2fbfb3SApril Chinunset x
3447c2fbfb3SApril Chinx=()
3457c2fbfb3SApril Chin[[ $x == $'(\n)' ]] || err_exit 'unset compound variable is not empty'
3467c2fbfb3SApril Chin
3477c2fbfb3SApril Chinunset z
3487c2fbfb3SApril Chinz=()
3497c2fbfb3SApril Chinz.foo=( [one]=hello [two]=(x=3 y=4) [three]=hi)
3507c2fbfb3SApril Chinz.bar[0]=hello
3517c2fbfb3SApril Chinz.bar[2]=world
3527c2fbfb3SApril Chinz.bar[1]=(x=4 y=5)
3537c2fbfb3SApril Chinexp='(
3547c2fbfb3SApril Chin	typeset -a bar=(
3557c2fbfb3SApril Chin		[0]=hello
3567c2fbfb3SApril Chin		[2]=world
3577c2fbfb3SApril Chin		[1]=(
3587c2fbfb3SApril Chin			x=4
3597c2fbfb3SApril Chin			y=5
3607c2fbfb3SApril Chin		)
3617c2fbfb3SApril Chin	)
3627c2fbfb3SApril Chin	typeset -A foo=(
3637c2fbfb3SApril Chin		[one]=hello
3647c2fbfb3SApril Chin		[three]=hi
3657c2fbfb3SApril Chin		[two]=(
3667c2fbfb3SApril Chin			x=3
3677c2fbfb3SApril Chin			y=4
3687c2fbfb3SApril Chin		)
3697c2fbfb3SApril Chin	)
3707c2fbfb3SApril Chin)'
3717c2fbfb3SApril Chingot=$z
3727c2fbfb3SApril Chin[[ $got == "$exp" ]] || {
3737c2fbfb3SApril Chin	exp=$(printf %q "$exp")
3747c2fbfb3SApril Chin	got=$(printf %q "$got")
3757c2fbfb3SApril Chin	err_exit "compound indexed array pretty print failed -- expected $exp, got $got"
3767c2fbfb3SApril Chin}
3777c2fbfb3SApril Chin
3787c2fbfb3SApril Chintypeset -A record
3797c2fbfb3SApril Chinrecord[a]=(
3807c2fbfb3SApril Chin	typeset -a x=(
3817c2fbfb3SApril Chin		[1]=(
3827c2fbfb3SApril Chin			X=1
3837c2fbfb3SApril Chin		)
3847c2fbfb3SApril Chin	)
3857c2fbfb3SApril Chin)
3867c2fbfb3SApril Chinexp=$'(\n\ttypeset -a x=(\n\t\t[1]=(\n\t\t\tX=1\n\t\t)\n\t)\n)'
3877c2fbfb3SApril Chingot=${record[a]}
3887c2fbfb3SApril Chin[[ $got == "$exp" ]] || {
3897c2fbfb3SApril Chin	exp=$(printf %q "$exp")
3907c2fbfb3SApril Chin	got=$(printf %q "$got")
3917c2fbfb3SApril Chin	err_exit "compound indexed array pretty print failed -- expected $exp, got $got"
3927c2fbfb3SApril Chin}
3937c2fbfb3SApril Chin
3947c2fbfb3SApril Chinunset r
3957c2fbfb3SApril Chinr=(
3967c2fbfb3SApril Chin	typeset -a x=(
3977c2fbfb3SApril Chin		[1]=(
3987c2fbfb3SApril Chin			X=1
3997c2fbfb3SApril Chin		)
4007c2fbfb3SApril Chin	)
4017c2fbfb3SApril Chin)
4027c2fbfb3SApril Chinexp=$'(\n\ttypeset -a x=(\n\t\t[1]=(\n\t\t\tX=1\n\t\t)\n\t)\n)'
4037c2fbfb3SApril Chingot=$r
4047c2fbfb3SApril Chin[[ $got == "$exp" ]] || {
4057c2fbfb3SApril Chin	exp=$(printf %q "$exp")
4067c2fbfb3SApril Chin	got=$(printf %q "$got")
4077c2fbfb3SApril Chin	err_exit "compound indexed array pretty print failed -- expected $exp, got $got"
4087c2fbfb3SApril Chin}
4097c2fbfb3SApril Chin
4107c2fbfb3SApril Chin# array of compund variables
4117c2fbfb3SApril Chintypeset -C data=(
4127c2fbfb3SApril Chin        typeset -a samples
4137c2fbfb3SApril Chin)
4147c2fbfb3SApril Chindata.samples+=(
4157c2fbfb3SApril Chin	type1="greeting1"
4167c2fbfb3SApril Chin	timestamp1="now1"
4177c2fbfb3SApril Chin	command1="grrrr1"
4187c2fbfb3SApril Chin)
4197c2fbfb3SApril Chindata.samples+=(
4207c2fbfb3SApril Chin	type2="greeting2"
4217c2fbfb3SApril Chin	timestamp2="now2"
4227c2fbfb3SApril Chin	command2="grrrr2"
4237c2fbfb3SApril Chin)
4247c2fbfb3SApril Chin
4257c2fbfb3SApril Chin[[ $data == %(()) ]] || err_exit "unbalanced parenthesis with compound variable containing array of compound variables"
42634f9b3eeSRoland Mainztypeset -C  -A hello=( [foo]=bar)
42734f9b3eeSRoland Mainz[[ $(typeset -p hello) == 'typeset -C -A hello=([foo]=bar)' ]] || err_exit 'typeset -A -C with intial assignment not working'
42834f9b3eeSRoland Mainz# this caused a core dump before ksh93t+
42934f9b3eeSRoland Mainz[[ $($SHELL -c 'foo=(x=3 y=4);function bar { typeset z=4;: $z;};bar;print ${!foo.@}') == 'foo.x foo.y' ]] 2> /dev/null || err_exit '${!foo.@} after function not working'
43034f9b3eeSRoland Mainz
43134f9b3eeSRoland Mainzfunction foo
43234f9b3eeSRoland Mainz{
43334f9b3eeSRoland Mainz	typeset tmp
43434f9b3eeSRoland Mainz	read -C tmp
43534f9b3eeSRoland Mainz	read -C tmp
43634f9b3eeSRoland Mainz}
43734f9b3eeSRoland Mainzfoo 2> /dev/null <<-  \EOF ||  err_exit 'deleting compound variable in function failed'
43834f9b3eeSRoland Mainz	(
43934f9b3eeSRoland Mainz		typeset -A myarray3=(
44034f9b3eeSRoland Mainz			[a]=( foo=bar)
44134f9b3eeSRoland Mainz			[b]=( foo=bar)
44234f9b3eeSRoland Mainz			[c d]=( foo=bar)
44334f9b3eeSRoland Mainz			[e]=( foo=bar)
44434f9b3eeSRoland Mainz			[f]=( foo=bar)
44534f9b3eeSRoland Mainz			[g]=( foo=bar)
44634f9b3eeSRoland Mainz			[h]=( foo=bar)
44734f9b3eeSRoland Mainz			[i]=( foo=bar)
44834f9b3eeSRoland Mainz			[j]=( foo=bar)
44934f9b3eeSRoland Mainz		)
45034f9b3eeSRoland Mainz	)
45134f9b3eeSRoland Mainz	hello
45234f9b3eeSRoland MainzEOF
45334f9b3eeSRoland Mainz
45434f9b3eeSRoland Mainztypeset -C -a mica01
45534f9b3eeSRoland Mainzmica01[4]=( a_string="foo bar" )
45634f9b3eeSRoland Mainztypeset -C more_content=(
45734f9b3eeSRoland Mainz	some_stuff="hello"
45834f9b3eeSRoland Mainz)
45934f9b3eeSRoland Mainzmica01[4]+=more_content
46034f9b3eeSRoland Mainzexpected=$'typeset -C -a mica01=([4]=(a_string=\'foo bar\';some_stuff=hello;))'
46134f9b3eeSRoland Mainz[[ $(typeset -p mica01) == "$expected" ]] || err_exit 'appened to indexed array compound variable not working'
46234f9b3eeSRoland Mainz
46334f9b3eeSRoland Mainzunset x
46434f9b3eeSRoland Mainzcompound x=( integer x ; )
46534f9b3eeSRoland Mainz[[ ! -v x.x ]] && err_exit 'x.x should be set'
46634f9b3eeSRoland Mainzexpected=$'(\n\ttypeset -l -i x=0\n)'
46734f9b3eeSRoland Mainz[[ $(print -v x) == "$expected" ]] || err_exit "'print -v x' should be $expected"
46834f9b3eeSRoland Mainz
46934f9b3eeSRoland Mainztypeset -C -A hello19=(
47034f9b3eeSRoland Mainz	[19]=(
47134f9b3eeSRoland Mainz		one="xone 19"
47234f9b3eeSRoland Mainz		two="xtwo 19"
47334f9b3eeSRoland Mainz	)
47434f9b3eeSRoland Mainz	[23]=(
47534f9b3eeSRoland Mainz		one="xone 23"
47634f9b3eeSRoland Mainz		two="xtwo 23"
47734f9b3eeSRoland Mainz	)
47834f9b3eeSRoland Mainz)
47934f9b3eeSRoland Mainzexpected="typeset -C -A hello19=([19]=(one='xone 19';two='xtwo 19';) [23]=(one='xone 23';two='xtwo 23';))"
48034f9b3eeSRoland Mainz[[ $(typeset -p hello19) == "$expected" ]] || print -u2 'typeset -p hello19 incorrect'
48134f9b3eeSRoland Mainzexpected=$'(\n\tone=\'xone 19\'\n\ttwo=\'xtwo 19\'\n) (\n\tone=\'xone 23\'\n\ttwo=\'xtwo 23\'\n)'
48234f9b3eeSRoland Mainz[[ ${hello19[@]} == "$expected" ]] || print -u2 '${hello19[@]} incorrect'
48334f9b3eeSRoland Mainz
48434f9b3eeSRoland Mainztypeset -C -A foo1=( abc="alphabet" ) foo2=( abc="alphabet" )
48534f9b3eeSRoland Mainzfunction add_one
48634f9b3eeSRoland Mainz{
48734f9b3eeSRoland Mainz	nameref left_op=$1
48834f9b3eeSRoland Mainz	typeset -C info
48934f9b3eeSRoland Mainz	info.hello="world"
49034f9b3eeSRoland Mainz	nameref x=info
49134f9b3eeSRoland Mainz	left_op+=x
49234f9b3eeSRoland Mainz}
49334f9b3eeSRoland Mainznameref node1="foo1[1234]"
49434f9b3eeSRoland Mainzadd_one "node1"
49534f9b3eeSRoland Mainzadd_one "foo2[1234]"
49634f9b3eeSRoland Mainz[[ "${foo1[1234]}" == "${foo2[1234]}" ]] || err_exit "test failed\n$(diff -u <( print -r -- "${foo1[1234]}") <(print -r -- "${foo2[1234]}"))."
49734f9b3eeSRoland Mainz
49834f9b3eeSRoland Mainztypeset -C tree
49934f9b3eeSRoland Mainzfunction f1
50034f9b3eeSRoland Mainz{
50134f9b3eeSRoland Mainz        nameref tr=$1
50234f9b3eeSRoland Mainz        typeset -A tr.subtree
50334f9b3eeSRoland Mainz        typeset -C node
50434f9b3eeSRoland Mainz        node.one="hello"
50534f9b3eeSRoland Mainz        node.two="world"
50634f9b3eeSRoland Mainz
50734f9b3eeSRoland Mainz        # move local note into the array
50834f9b3eeSRoland Mainz        typeset -m tr.subtree["a_node"]=node
50934f9b3eeSRoland Mainz}
51034f9b3eeSRoland Mainzf1 tree
51134f9b3eeSRoland Mainzexpected=$'(\n\ttypeset -A subtree=(\n\t\t[a_node]=(\n\t\t\tone=hello\n\t\t\ttwo=world\n\t\t)\n\t)\n)'
51234f9b3eeSRoland Mainz[[ $tree == "$expected" ]] ||  err_exit 'move of compound local variable to global variable not working'
51334f9b3eeSRoland Mainz
51434f9b3eeSRoland Mainztypeset -C -A array
51534f9b3eeSRoland Mainzfloat array[12].amount=2.9
51634f9b3eeSRoland Mainzexpected='typeset -C -A array=([12]=(typeset -l -E amount=2.9;))'
51734f9b3eeSRoland Mainz[[ $(typeset -p array) == "$expected" ]] || err_exit 'typeset with compound  variable with compound variable array not working'
51834f9b3eeSRoland Mainz
51934f9b3eeSRoland Mainztypeset -T foo_t=(
52034f9b3eeSRoland Mainz        function diff
52134f9b3eeSRoland Mainz        {
52234f9b3eeSRoland Mainz		print 1.0
52334f9b3eeSRoland Mainz                return 0
52434f9b3eeSRoland Mainz        }
52534f9b3eeSRoland Mainz)
52634f9b3eeSRoland Mainzfoo_t sw
52734f9b3eeSRoland Mainzcompound output=(
52834f9b3eeSRoland Mainz        integer one=1
52934f9b3eeSRoland Mainz        float mydiff=sw.diff
53034f9b3eeSRoland Mainz        float end=.314
53134f9b3eeSRoland Mainz)
53234f9b3eeSRoland Mainz[[ $output == *end=* ]] ||  err_exit "The field 'name' end is missing"
53334f9b3eeSRoland Mainz
53434f9b3eeSRoland Mainzcompound cpv1=( integer f=2 )
53534f9b3eeSRoland Mainzcompound x=(
53634f9b3eeSRoland Mainz	integer a=1
53734f9b3eeSRoland Mainz	compound b=cpv1
53834f9b3eeSRoland Mainz)
53934f9b3eeSRoland Mainz[[ $x == *f=2* ]] ||  err_exit "The field b containg 'f=2' is missing"
54034f9b3eeSRoland Mainz
5417c2fbfb3SApril Chinexit $((Errors))
542