xref: /titanic_44/usr/src/lib/libshell/common/tests/comvar.sh (revision 8475e04352e630e4bd0f59a283286ee2475a14ce)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1982-2008 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########################################################################
20function err_exit
21{
22	print -u2 -n "\t"
23	print -u2 -r ${Command}[$1]: "${@:2}"
24	let Errors+=1
25}
26alias err_exit='err_exit $LINENO'
27
28#test for compound variables
29Command=${0##*/}
30integer Errors=0
31Point=(
32	float x=1. y=0.
33)
34eval p="$Point"
35if	(( (p.x*p.x + p.y*p.y) > 1.01 ))
36then	err_exit 'compound variable not working'
37fi
38nameref foo=p
39if	[[ ${foo.x} != ${Point.x} ]]
40then	err_exit 'reference to compound object not working'
41fi
42unset foo
43rec=(
44	name='Joe Blow'
45	born=(
46		month=jan
47		integer day=16
48		year=1980
49	)
50)
51eval newrec="$rec"
52if	[[ ${newrec.name} != "${rec.name}" ]]
53then	err_exit 'copying a compound object not working'
54fi
55if	(( newrec.born.day != 16 ))
56then	err_exit 'copying integer field of  compound object not working'
57fi
58p_t=(
59        integer z=0
60        typeset -A tokens
61)
62unset x
63typeset -A x
64x=( [foo]=bar )
65if	[[ ${x[@]} != bar ]]
66then	err_exit 'compound assignemnt of associative arrays not working'
67fi
68unset -n foo x
69unset foo x
70foo=( x=3)
71nameref x=foo
72if	[[ ${!x.@} != foo.x ]]
73then	err_exit 'name references not expanded on prefix matching'
74fi
75unset x
76unset -n x
77(
78	x=()
79	x.foo.bar=7
80	[[ ${x.foo.bar} == 7 ]] || err_exit '[[ ${x.foo.bar} != 7 ]]'
81	(( x.foo.bar == 7  ))|| err_exit '(( x.foo.bar != 7 ))'
82	[[ ${x.foo} == *bar=7*  ]] || err_exit '[[ ${x.foo} != *bar=7* ]]'
83)
84foo=(integer x=3)
85if	[[ ${foo} != *x=3* ]]
86then	err_exit "compound variable with integer subvariable not working"
87fi
88$SHELL -c $'x=(foo=bar)\n[[ x == x ]]' 2> /dev/null ||
89	err_exit '[[ ... ]] not working after compound assignment'
90unset foo
91[[ ${!foo.@} ]] && err_exit 'unset compound variable leaves subvariables'
92suitable=(
93  label="Table Viewer"
94  langs="ksh"
95  uselang=ksh
96  launch=no
97  groups="default"
98  default=(
99    label="Table Viewer Preferences"
100    entrylist=" \
101      vieworigin viewsize viewcolor viewfontname viewfontsize \
102      showheader header showfooter footer showtitle title showlegends \
103      class_td_lg1_style class_tr_tr1_style \
104      class_th_th1_style class_td_td1_style \
105      fields fieldorder \
106    "
107    entries=(
108      vieworigin=(
109        type=coord var=vieworigin val="0 0" label="Window Position"
110      )
111      viewsize=(
112        type=coord var=viewsize val="400 400" label="Window Size"
113      )
114      viewcolor=(
115        type=2colors var=viewcolor val="gray black"
116        label="Window Colors"
117      )
118      viewfontname=(
119        type=fontname var=viewfontname val="Times-Roman"
120        label="Window Font Name"
121      )
122      viewfontsize=(
123        type=fontsize var=viewfontsize val=14 label="Window Font Size"
124      )
125
126      showheader=(
127        type=yesno var=showheader val=no label="Show Header"
128      )
129      header=(
130        type=text var=header val="" label="Header"
131      )
132
133      showfooter=(
134        type=yesno var=showfooter val=no label="Show Footer"
135      )
136      footer=(
137        type=text var=footer val="" label="Footer"
138      )
139
140      showtitle=(
141        type=yesno var=showtitle val=yes label="Show Title"
142      )
143      title=(
144        type=text var=title val="SWIFTUI - Table View" label="Title"
145      )
146
147      showlegends=(
148        type=yesno var=showlegends val=yes label="Show Legends"
149      )
150
151      class_td_lg1_style=(
152        type=style var=class_td_lg1_style
153        val="color: black; font-family: Times-Roman; font-size: 14pt"
154        label="Legend 1 Style"
155      )
156
157      class_tr_tr1_style=(
158        type=style var=class_tr_tr1_style val="background: black"
159        label="Table Row 1 Style"
160      )
161
162      class_th_th1_style=(
163        type=style var=class_th_th1_style
164        val="color: black; font-family: Times-Roman; font-size: 14pt; text-align: left"
165        label="Table Header 1 Style"
166      )
167
168      class_td_td1_style=(
169        type=style var=class_td_td1_style
170        val="color: black; font-family: Times-Roman; font-size: 14pt; text-align: left"
171        label="Table Cell 1 Style"
172      )
173
174      fields=(
175        type=text var=fields val= label="List of Fields"
176      )
177      fieldorder=(
178        type=text var=fieldorder val= label="Order of Fields"
179      )
180    )
181  )
182)
183[[ "${suitable}" == *entrylist=* ]] || err_exit 'compound variable expansion omitting fields'
184foo=( bar=foo  barbar=bar)
185[[ $foo == *bar=foo* ]] || err_exit 'no prefix elements in compound variable output'
186function localvar
187{
188	typeset point=(typeset -i x=3 y=4)
189	(( (point.x*point.x + point.y*point.y) == 25 )) || err_exit "local compound variable not working"
190}
191point=(integer x=6 y=8)
192localvar
193	(( (point.x*point.x + point.y*point.y) == 100 )) || err_exit "global compound variable not preserved"
194[[ $($SHELL -c 'foo=();foo.[x]=(y z); print ${foo.x[@]}') == 'y z' ]] 2> /dev/null || err_exit 'foo=( [x]=(y z)  not working'
195function staticvar
196{
197	if	[[ $1 ]]
198	then	print -r -- "$point"
199		return
200	fi
201        typeset -S point=(typeset -i x=3 y=4)
202        (( (point.x*point.x + point.y*point.y) == 25 )) || err_exit "local compound variable not working"
203	point.y=5
204	point.z=foobar
205}
206staticvar
207        (( (point.x*point.x + point.y*point.y) == 100 )) || err_exit "global compound variable not preserved"
208[[ $(staticvar x) == $'(\n\ttypeset -i x=3\n\ttypeset -i y=5\n\tz=foobar\n)' ]] || err_exit 'static variables in function not working'
209integer x=3
210( typeset -S x=+++)2> /dev/null  || err_exit "typeset -S doesn't unset first"
211
212unset z
213( [[ ${z.foo.bar:-abc} == abc ]] 2> /dev/null) || err_exit ':- not working with compound variables'
214stack=()
215typeset -a stack.items=([0]=foo [1]=bar)
216[[ ${stack.items[0]} == foo ]] || err_exit 'typeset -a variable not expanding correctly'
217$SHELL -c 'typeset -a info=( [1]=( passwd=( since=2005-07-20) ))'  || err_exit 'problem with embedded index array in compound variable'
218x=(foo=([1]=(y=([2]=(z=4)))))
219[[ $x == *'.y'=* ]] && err_exit 'expansion with bogus leading . in name'
220unset z
221z=1
222function foo
223{
224	z=3
225	[[ ${a.z} == 3 ]] && err_exit "\${a.z} should not be 3"
226	print hi
227}
228a=( b=$(foo) )
229[[ ${a.z} == 3 ]] &&  err_exit 'a.z should not be set to 3'
230function a.b.get
231{
232	.sh.value=foo
233}
234{ b=( b1=${a.b} ) ;} 2> /dev/null
235[[ ${b.b1} == foo ]] || err_exit '${b.b1} should be foo'
236function dcl1
237{
238     eval 'a=1
239     function a.set
240     { print ${.sh.name}=${.sh.value}; }'
241}
242function dcl2
243{
244     eval 'b=(typeset x=0; typeset y=0 )
245     function b.x.set
246     { print ${.sh.name}=${.sh.value}; }'
247}
248dcl1
249[[ ${ a=123;} == 'a=123' ]] || err_exit 'should be a=123'
250dcl2
251[[ ${ b.x=456;} == 'b.x=456' ]] || err_exit 'should be b.x=456'
252eval 'b=(typeset x=0; typeset y=0 )
253function b.x.set
254{ print ${.sh.name}=${.sh.value}; }' > /dev/null
255[[ ${ b.x=789;} == 'b.x=789' ]] || err_exit 'should be b.x=789'
256unset a b
257function func
258{
259	typeset X
260	X=( bar=2 )
261}
262
263X=( foo=1 )
264func
265[[ $X == $'(\n\tfoo=1\n)' ]] || err_exit 'scoping problem with compound variables'
266unset foo
267typeset -A foo=([a]=aa;[b]=bb;[c]=cc)
268[[ ${foo[c]} == cc ]] || err_exit 'associative array assignment with; not working'
269[[ $({ $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'
270unset foo
271foo=(typeset -a bar)
272[[ $foo  == *'typeset -a bar'* ]] || err_exit 'array attribute -a not preserved in compound variable'
273unset s
274typeset -A s=( [foo]=(y=2 z=3) [bar]=(y=4 z=5))
275[[ ${s[@]} == *z=*z=* ]] || err_exit 'missing elements in compound associative array'
276unset nodes
277typeset -A nodes
278nodes[0]+=( integer x=5)
279[[ ${nodes[0].x} == 5 ]] || err_exit '${nodes[0].x} should be 5'
280unset foo
281typeset -C foo
282foo.bar=abc
283[[ $foo == $'(\n\tbar=abc\n)' ]] || err_exit 'typeset -C not working for foo'
284typeset -C foo=(bar=def)
285[[ $foo == $'(\n\tbar=def\n)' ]] || err_exit 'typeset -C not working when initialized'
286foo=(
287	hello=ok
288	yes=( bam=2 yes=4)
289	typeset -A array=([one]=one [two]=2)
290	last=me
291)
292eval foo2="$foo"
293foo2.hello=notok foo2.yes.yex=no foo2.extra=yes.
294typeset -C bar bam
295{
296	read -Cu3 bar
297	read -Cu3 bam
298	read -ru3
299} 3<<- ++++
300	"$foo"
301	"$foo2"
302	last line
303++++
304[[ $? == 0 ]] || err_exit ' read -C failed'
305[[ $bar == "$foo" ]] || err_exit '$foo != $bar'
306[[ $bam == "$foo2" ]] || err_exit '$foo2 != $bmr'
307[[ $REPLY == 'last line' ]] || err_exit "\$REPLY=$REPLY should be 'last line"
308typeset x=( typeset -a foo=( [1][3]=hello [9][2]="world" ) )
309eval y="(typeset -a foo=$(printf "%B\n" x.foo) )"
310[[ $x == "$y" ]] || err_exit '$x.foo != $y.foo with %B'
311eval y="(typeset -a foo=$(printf "%#B\n" x.foo) )"
312[[ $x == "$y" ]] || err_exit '$x.foo != $y.foo with %#B'
313eval y="$(printf "%B\n" x)"
314[[ $x == "$y" ]] || err_exit '$x != $y with %B'
315eval y="$(printf "%#B\n" x)"
316[[ $x == "$y" ]] || err_exit '$x != $y with %#B'
317y=$(set | grep ^x=) 2> /dev/null
318eval "${y/#x/y}"
319[[ $x == "$y" ]] || err_exit '$x != $y with set | grep'
320unset x y z
321x=( float x=0 y=1; z=([foo]=abc [bar]=def))
322typeset -C y=x
323[[ $x == "$y" ]] || err_exit '$x != $y with typeset -C'
324unset y
325y=()
326y=x
327[[ $x == "$y" ]] || err_exit '$x != $y when x=y and x and y are -C '
328function foobar
329{
330	typeset -C z
331	z=x
332	[[ $x == "$z" ]] || err_exit '$x != $z when x=z and x and z are -C '
333	y=z
334}
335[[ $x == "$y" ]] || err_exit '$x != $y when x=y -C copied in a function '
336z=(foo=abc)
337y+=z
338[[ $y == *foo=abc* ]] || err_exit 'z not appended to y'
339unset y.foo
340[[ $x == "$y" ]] || err_exit '$x != $y when y.foo deleted'
341unset x y
342x=( foo=(z=abc d=ghi) bar=abc; typeset -A r=([x]=3  [y]=4))
343unset x
344x=()
345[[ $x == $'(\n)' ]] || err_exit 'unset compound variable is not empty'
346
347unset z
348z=()
349z.foo=( [one]=hello [two]=(x=3 y=4) [three]=hi)
350z.bar[0]=hello
351z.bar[2]=world
352z.bar[1]=(x=4 y=5)
353exp='(
354	typeset -a bar=(
355		[0]=hello
356		[2]=world
357		[1]=(
358			x=4
359			y=5
360		)
361	)
362	typeset -A foo=(
363		[one]=hello
364		[three]=hi
365		[two]=(
366			x=3
367			y=4
368		)
369	)
370)'
371got=$z
372[[ $got == "$exp" ]] || {
373	exp=$(printf %q "$exp")
374	got=$(printf %q "$got")
375	err_exit "compound indexed array pretty print failed -- expected $exp, got $got"
376}
377
378typeset -A record
379record[a]=(
380	typeset -a x=(
381		[1]=(
382			X=1
383		)
384	)
385)
386exp=$'(\n\ttypeset -a x=(\n\t\t[1]=(\n\t\t\tX=1\n\t\t)\n\t)\n)'
387got=${record[a]}
388[[ $got == "$exp" ]] || {
389	exp=$(printf %q "$exp")
390	got=$(printf %q "$got")
391	err_exit "compound indexed array pretty print failed -- expected $exp, got $got"
392}
393
394unset r
395r=(
396	typeset -a x=(
397		[1]=(
398			X=1
399		)
400	)
401)
402exp=$'(\n\ttypeset -a x=(\n\t\t[1]=(\n\t\t\tX=1\n\t\t)\n\t)\n)'
403got=$r
404[[ $got == "$exp" ]] || {
405	exp=$(printf %q "$exp")
406	got=$(printf %q "$got")
407	err_exit "compound indexed array pretty print failed -- expected $exp, got $got"
408}
409
410# array of compund variables
411typeset -C data=(
412        typeset -a samples
413)
414data.samples+=(
415	type1="greeting1"
416	timestamp1="now1"
417	command1="grrrr1"
418)
419data.samples+=(
420	type2="greeting2"
421	timestamp2="now2"
422	command2="grrrr2"
423)
424
425[[ $data == %(()) ]] || err_exit "unbalanced parenthesis with compound variable containing array of compound variables"
426exit $((Errors))
427