xref: /titanic_51/usr/src/lib/libshell/common/tests/arrays.sh (revision d8d4fa8f33f737d82c7dcd7ddd11b4342ce4fbca)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#           Copyright (c) 1982-2007 AT&T Knowledge Ventures            #
5#                      and is licensed under the                       #
6#                  Common Public License, Version 1.0                  #
7#                      by AT&T Knowledge Ventures                      #
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
28function fun
29{
30	integer i
31	unset xxx
32	for i in 0 1
33	do	xxx[$i]=$i
34	done
35}
36
37Command=${0##*/}
38integer Errors=0
39set -A x zero one two three four 'five six'
40if	[[ $x != zero ]]
41then	err_exit '$x is not element 0'
42fi
43if	[[ ${x[0]} != zero ]]
44then	err_exit '${x[0] is not element 0'
45fi
46if	(( ${#x[0]} != 4 ))
47then	err_exit "length of ${x[0]} is not 4"
48fi
49if	(( ${#x[@]} != 6  ))
50then	err_exit 'number of elements of x is not 6'
51fi
52if	[[ ${x[2]} != two  ]]
53then	err_exit ' element two is not 2'
54fi
55if	[[ ${x[@]:2:1} != two  ]]
56then	err_exit ' ${x[@]:2:1} is not two'
57fi
58set -A y -- ${x[*]}
59if	[[ $y != zero ]]
60then	err_exit '$x is not element 0'
61fi
62if	[[ ${y[0]} != zero ]]
63then	err_exit '${y[0] is not element 0'
64fi
65if	(( ${#y[@]} != 7  ))
66then	err_exit 'number of elements of y is not 7'
67fi
68if	[[ ${y[2]} != two  ]]
69then	err_exit ' element two is not 2'
70fi
71set +A y nine ten
72if	[[ ${y[2]} != two  ]]
73then	err_exit ' element two is not 2'
74fi
75if	[[ ${y[0]} != nine ]]
76then	err_exit '${y[0] is not nine'
77fi
78unset y[4]
79if	(( ${#y[@]} != 6  ))
80then	err_exit 'number of elements of y is not 6'
81fi
82if	(( ${#y[4]} != 0  ))
83then	err_exit 'string length of unset element is not 0'
84fi
85unset foo
86if	(( ${#foo[@]} != 0  ))
87then	err_exit 'number of elements of unset variable foo is not 0'
88fi
89foo=''
90if	(( ${#foo[0]} != 0  ))
91then	err_exit 'string length of null element is not 0'
92fi
93if	(( ${#foo[@]} != 1  ))
94then	err_exit 'number of elements of null variable foo is not 1'
95fi
96unset foo
97foo[0]=foo
98foo[3]=bar
99unset foo[0]
100unset foo[3]
101if	(( ${#foo[@]} != 0  ))
102then	err_exit 'number of elements of left in variable foo is not 0'
103fi
104unset foo
105foo[3]=bar
106foo[0]=foo
107unset foo[3]
108unset foo[0]
109if	(( ${#foo[@]} != 0  ))
110then	err_exit 'number of elements of left in variable foo again is not 0'
111fi
112fun
113if	(( ${#xxx[@]} != 2  ))
114then	err_exit 'number of elements of left in variable xxx is not 2'
115fi
116fun
117if	(( ${#xxx[@]} != 2  ))
118then	err_exit 'number of elements of left in variable xxx again is not 2'
119fi
120set -A foo -- "${x[@]}"
121if	(( ${#foo[@]} != 6  ))
122then	err_exit 'number of elements of foo is not 6'
123fi
124if	(( ${#PWD[@]} != 1  ))
125then	err_exit 'number of elements of PWD is not 1'
126fi
127unset x
128x[2]=foo x[4]=bar
129if	(( ${#x[@]} != 2  ))
130then	err_exit 'number of elements of x is not 2'
131fi
132s[1]=1 c[1]=foo
133if	[[ ${c[s[1]]} != foo ]]
134then	err_exit 'c[1]=foo s[1]=1; ${c[s[1]]} != foo'
135fi
136unset s
137typeset -Ai s
138y=* z=[
139s[$y]=1
140s[$z]=2
141if	(( ${#s[@]} != 2  ))
142then	err_exit 'number of elements of  is not 2'
143fi
144(( s[$z] = s[$z] + ${s[$y]} ))
145if	[[ ${s[$z]} != 3  ]]
146then	err_exit '[[ ${s[$z]} != 3  ]]'
147fi
148if	(( s[$z] != 3 ))
149then	err_exit '(( s[$z] != 3 ))'
150fi
151(( s[$y] = s[$y] + ${s[$z]} ))
152if	[[ ${s[$y]} != 4  ]]
153then	err_exit '[[ ${s[$y]} != 4  ]]'
154fi
155if	(( s[$y] != 4 ))
156then	err_exit '(( s[$y] != 4 ))'
157fi
158unset y
159set -A y 2 4 6
160typeset -i y
161z=${y[@]}
162typeset -R12 y
163typeset -i y
164if      [[ ${y[@]} != "$z" ]]
165then    err_exit 'error in array conversion from int to R12'
166fi
167if      (( ${#y[@]} != 3  ))
168then    err_exit 'error in count of array conversion from int to R12'
169fi
170unset abcdefg
171:  ${abcdefg[1]}
172set | grep '^abcdefg$' >/dev/null && err_exit 'empty array variable in set list'
173unset x y
174x=1
175typeset -i y[$x]=4
176if	[[ ${y[1]} != 4 ]]
177then    err_exit 'arithmetic expressions in typeset not working'
178fi
179unset foo
180typeset foo=bar
181typeset -A foo
182if	[[ ${foo[0]} != bar ]]
183then	err_exit 'initial value not preserved when typecast to associative'
184fi
185unset foo
186foo=(one two)
187typeset -A foo
188foo[two]=3
189if	[[ ${#foo[*]} != 3 ]]
190then	err_exit 'conversion of indexed to associative array failed'
191fi
192set a b c d e f g h i j k l m
193if	[[ ${#} != 13 ]]
194then	err_exit '${#} not 13'
195fi
196unset xxx
197xxx=foo
198if	[[ ${!xxx[@]} != 0 ]]
199then	err_exit '${!xxx[@]} for scalar not 0'
200fi
201if	[[ ${11} != k ]]
202then	err_exit '${11} not working'
203fi
204if	[[ ${@:4:1} != d ]]
205then	err_exit '${@:4:1} not working'
206fi
207foovar1=abc
208foovar2=def
209if	[[ ${!foovar@} != +(foovar[[:alnum:]]?([ ])) ]]
210then	err_exit '${!foovar@} does not expand correctly'
211fi
212if	[[ ${!foovar1} != foovar1 ]]
213then	err_exit '${!foovar1} != foovar1'
214fi
215unset xxx
216: ${xxx[3]}
217if	[[ ${!xxx[@]} ]]
218then	err_exit '${!xxx[@]} should be null'
219fi
220integer i=0
221{
222	set -x
223	xxx[++i]=1
224	set +x
225} 2> /dev/null
226if	(( i != 1))
227then	err_exit 'execution trace side effects with array subscripts'
228fi
229unset list
230: $(set -A list foo bar)
231if	(( ${#list[@]} != 0))
232then	err_exit '$(set -A list ...) leaves side effects'
233fi
234unset list
235list= (foo bar bam)
236( set -A list one two three four)
237if	[[ ${list[1]} != bar ]]
238then	err_exit 'array not restored after subshell'
239fi
240XPATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:.:/sbin:/usr/sbin
241xpath=( $( IFS=: ; echo $XPATH ) )
242if	[[ $(print -r  "${xpath[@]##*/}") != 'bin bin ucb bin . sbin sbin' ]]
243then	err_exit '${xpath[@]##*/} not applied to each element'
244fi
245foo=( zero one '' three four '' six)
246integer n=-1
247if	[[ ${foo[@]:n} != six ]]
248then	err_exit 'array offset of -1 not working'
249fi
250if	[[ ${foo[@]: -3:1} != four ]]
251then	err_exit 'array offset of -3:1 not working'
252fi
253$SHELL -c 'x=(if then else fi)' 2> /dev/null  || err_exit 'reserved words in x=() assignment not working'
254unset foo
255foo=one
256foo=( $foo two)
257if	[[ ${#foo[@]} != 2 ]]
258then	err_exit 'array getting unset before right hand side evaluation'
259fi
260foo=(143 3643 38732)
261export foo
262typeset -i foo
263if	[[ $($SHELL -c 'print $foo') != 143 ]]
264then	err_exit 'exporting indexed array not exporting 0-th element'
265fi
266( $SHELL   -c '
267	unset foo
268	typeset -A foo=([0]=143 [1]=3643 [2]=38732)
269	export foo
270	typeset -i foo
271	[[ $($SHELL -c "print $foo") == 143 ]]'
272) 2> /dev/null ||
273		err_exit 'exporting associative array not exporting 0-th element'
274unset foo
275typeset -A foo
276foo[$((10))]=ok 2> /dev/null || err_exit 'arithmetic expression as subscript not working'
277unset foo
278typeset -A foo
279integer foo=0
280[[ $foo == 0 ]] || err_exit 'zero element of associative array not being set'
281unset foo
282typeset -A foo=( [two]=1)
283for i in one three four five
284do	: ${foo[$i]}
285done
286if	[[ ${!foo[@]} != two ]]
287then	err_exit 'Error in subscript names'
288fi
289unset x
290x=( 1 2 3)
291(x[1]=8)
292[[ ${x[1]} == 2 ]] || err_exit 'index array produce side effects in subshells'
293x=( 1 2 3)
294(
295	x+=(8)
296	[[ ${#x[@]} == 4 ]] || err_exit 'index array append in subshell error'
297)
298[[ ${#x[@]} == 3 ]] || err_exit 'index array append in subshell effects parent'
299x=( [one]=1 [two]=2 [three]=3)
300(x[two]=8)
301[[ ${x[two]} == 2 ]] || err_exit 'associative array produce side effects in subshells'
302unset x
303x=( [one]=1 [two]=2 [three]=3)
304(
305	x+=( [four]=4 )
306	[[ ${#x[@]} == 4 ]] || err_exit 'associative array append in subshell error'
307)
308[[ ${#x[@]} == 3 ]] || err_exit 'associative array append in subshell effects parent'
309unset x
310integer i
311for ((i=0; i < 40; i++))
312do	x[i]=$i
313done
314[[  ${#x[@]} == 40 ]] || err_exit 'index arrays loosing values'
315[[ $( ($SHELL -c 'typeset -A var; (IFS=: ; set -A var a:b:c ;print ${var[@]});:' )2>/dev/null) == 'a b c' ]] || err_exit 'change associative to index failed'
316unset foo
317[[ $(foo=good
318for ((i=0; i < 2; i++))
319do	[[ ${foo[i]} ]] && print ok
320done) == ok ]] || err_exit 'invalid optimization for subscripted variables'
321(
322x=([foo]=bar)
323set +A x bam
324) 2> /dev/null && err_exit 'set +A with associative array should be an error'
325unset bam foo
326foo=0
327typeset -A bam
328unset bam[foo]
329bam[foo]=value
330[[ $bam == value ]] && err_exit 'unset associative array element error'
331: only first element of an array can be exported
332unset bam
333trap 'rm -f /tmp/sharr$$' EXIT
334print 'print ${var[0]} ${var[1]}' > /tmp/sharr$$
335chmod +x /tmp/sharr$$
336[[ $($SHELL -c "var=(foo bar);export var;/tmp/sharr$$") == foo ]] || err_exit 'export array not exporting just first element'
337unset foo
338set -o allexport
339foo=one
340foo[1]=two
341foo[0]=three
342[[ $foo == three ]] || err_exit 'export all not working with arrays'
343cat > /tmp/sharr$$ <<- \!
344	typeset -A foo
345	print foo${foo[abc]}
346!
347# 04-05-24 bug fix
348unset foo
349[[ $($SHELL -c "typeset -A foo;/tmp/sharr$$")  == foo ]] 2> /dev/null || err_exit 'empty associative arrays not being cleared correctly before scripts'
350[[ $($SHELL -c "typeset -A foo;foo[abc]=abc;/tmp/sharr$$") == foo ]] 2> /dev/null || err_exit 'associative arrays not being cleared correctly before scripts'
351unset foo
352foo=(one two)
353[[ ${foo[@]:1} == two ]] || err_exit '${foo[@]:1} == two'
354[[ ! ${foo[@]:2} ]] || err_exit '${foo[@]:2} not null'
355unset foo
356foo=one
357[[ ! ${foo[@]:1} ]] || err_exit '${foo[@]:1} not null'
358function EMPTY
359{
360        typeset i
361        typeset -n ARRAY=$1
362        for i in ${!ARRAY[@]}
363        do      unset ARRAY[$i]
364        done
365}
366unset foo
367typeset -A foo
368foo[bar]=bam
369foo[x]=y
370EMPTY foo
371[[ $(typeset | grep foo$) == *associative* ]] || err_exit 'array lost associative attribute'
372[[ ! ${foo[@]}  ]] || err_exit 'array not empty'
373[[ ! ${!foo[@]}  ]] || err_exit 'array names not empty'
374unset foo
375foo=bar
376set -- "${foo[@]:1}"
377(( $# == 0 )) || err_exit '${foo[@]:1} should not have any values'
378unset bar
379: ${_foo[bar=4]}
380(( bar == 4 )) || err_exit 'subscript of unset variable not evaluated'
381exit $((Errors))
382