xref: /titanic_41/usr/src/lib/libshell/common/tests/arrays.sh (revision a31148363f598def767ac48c5d82e1572e44b935)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1982-2009 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
28Command=${0##*/}
29integer Errors=0
30
31tmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
32trap "cd /; rm -rf $tmp" EXIT
33
34function fun
35{
36	integer i
37	unset xxx
38	for i in 0 1
39	do	xxx[$i]=$i
40	done
41}
42
43set -A x zero one two three four 'five six'
44if	[[ $x != zero ]]
45then	err_exit '$x is not element 0'
46fi
47if	[[ ${x[0]} != zero ]]
48then	err_exit '${x[0] is not element 0'
49fi
50if	(( ${#x[0]} != 4 ))
51then	err_exit "length of ${x[0]} is not 4"
52fi
53if	(( ${#x[@]} != 6  ))
54then	err_exit 'number of elements of x is not 6'
55fi
56if	[[ ${x[2]} != two  ]]
57then	err_exit ' element two is not 2'
58fi
59if	[[ ${x[@]:2:1} != two  ]]
60then	err_exit ' ${x[@]:2:1} is not two'
61fi
62set -A y -- ${x[*]}
63if	[[ $y != zero ]]
64then	err_exit '$x is not element 0'
65fi
66if	[[ ${y[0]} != zero ]]
67then	err_exit '${y[0] is not element 0'
68fi
69if	(( ${#y[@]} != 7  ))
70then	err_exit 'number of elements of y is not 7'
71fi
72if	[[ ${y[2]} != two  ]]
73then	err_exit ' element two is not 2'
74fi
75set +A y nine ten
76if	[[ ${y[2]} != two  ]]
77then	err_exit ' element two is not 2'
78fi
79if	[[ ${y[0]} != nine ]]
80then	err_exit '${y[0] is not nine'
81fi
82unset y[4]
83if	(( ${#y[@]} != 6  ))
84then	err_exit 'number of elements of y is not 6'
85fi
86if	(( ${#y[4]} != 0  ))
87then	err_exit 'string length of unset element is not 0'
88fi
89unset foo
90if	(( ${#foo[@]} != 0  ))
91then	err_exit 'number of elements of unset variable foo is not 0'
92fi
93foo=''
94if	(( ${#foo[0]} != 0  ))
95then	err_exit 'string length of null element is not 0'
96fi
97if	(( ${#foo[@]} != 1  ))
98then	err_exit 'number of elements of null variable foo is not 1'
99fi
100unset foo
101foo[0]=foo
102foo[3]=bar
103unset foo[0]
104unset foo[3]
105if	(( ${#foo[@]} != 0  ))
106then	err_exit 'number of elements of left in variable foo is not 0'
107fi
108unset foo
109foo[3]=bar
110foo[0]=foo
111unset foo[3]
112unset foo[0]
113if	(( ${#foo[@]} != 0  ))
114then	err_exit 'number of elements of left in variable foo again is not 0'
115fi
116fun
117if	(( ${#xxx[@]} != 2  ))
118then	err_exit 'number of elements of left in variable xxx is not 2'
119fi
120fun
121if	(( ${#xxx[@]} != 2  ))
122then	err_exit 'number of elements of left in variable xxx again is not 2'
123fi
124set -A foo -- "${x[@]}"
125if	(( ${#foo[@]} != 6  ))
126then	err_exit 'number of elements of foo is not 6'
127fi
128if	(( ${#PWD[@]} != 1  ))
129then	err_exit 'number of elements of PWD is not 1'
130fi
131unset x
132x[2]=foo x[4]=bar
133if	(( ${#x[@]} != 2  ))
134then	err_exit 'number of elements of x is not 2'
135fi
136s[1]=1 c[1]=foo
137if	[[ ${c[s[1]]} != foo ]]
138then	err_exit 'c[1]=foo s[1]=1; ${c[s[1]]} != foo'
139fi
140unset s
141typeset -Ai s
142y=* z=[
143s[$y]=1
144s[$z]=2
145if	(( ${#s[@]} != 2  ))
146then	err_exit 'number of elements of  is not 2'
147fi
148(( s[$z] = s[$z] + ${s[$y]} ))
149if	[[ ${s[$z]} != 3  ]]
150then	err_exit '[[ ${s[$z]} != 3  ]]'
151fi
152if	(( s[$z] != 3 ))
153then	err_exit '(( s[$z] != 3 ))'
154fi
155(( s[$y] = s[$y] + ${s[$z]} ))
156if	[[ ${s[$y]} != 4  ]]
157then	err_exit '[[ ${s[$y]} != 4  ]]'
158fi
159if	(( s[$y] != 4 ))
160then	err_exit '(( s[$y] != 4 ))'
161fi
162set -A y 2 4 6
163typeset -i y
164z=${y[@]}
165typeset -R12 y
166typeset -i y
167if      [[ ${y[@]} != "$z" ]]
168then    err_exit 'error in array conversion from int to R12'
169fi
170if      (( ${#y[@]} != 3  ))
171then    err_exit 'error in count of array conversion from int to R12'
172fi
173unset abcdefg
174:  ${abcdefg[1]}
175set | grep '^abcdefg$' >/dev/null && err_exit 'empty array variable in set list'
176unset x y
177x=1
178typeset -i y[$x]=4
179if	[[ ${y[1]} != 4 ]]
180then    err_exit 'arithmetic expressions in typeset not working'
181fi
182unset foo
183typeset foo=bar
184typeset -A foo
185if	[[ ${foo[0]} != bar ]]
186then	err_exit 'initial value not preserved when typecast to associative'
187fi
188unset foo
189foo=(one two)
190typeset -A foo
191foo[two]=3
192if	[[ ${#foo[*]} != 3 ]]
193then	err_exit 'conversion of indexed to associative array failed'
194fi
195set a b c d e f g h i j k l m
196if	[[ ${#} != 13 ]]
197then	err_exit '${#} not 13'
198fi
199unset xxx
200xxx=foo
201if	[[ ${!xxx[@]} != 0 ]]
202then	err_exit '${!xxx[@]} for scalar not 0'
203fi
204if	[[ ${11} != k ]]
205then	err_exit '${11} not working'
206fi
207if	[[ ${@:4:1} != d ]]
208then	err_exit '${@:4:1} not working'
209fi
210foovar1=abc
211foovar2=def
212if	[[ ${!foovar@} != +(foovar[[:alnum:]]?([ ])) ]]
213then	err_exit '${!foovar@} does not expand correctly'
214fi
215if	[[ ${!foovar1} != foovar1 ]]
216then	err_exit '${!foovar1} != foovar1'
217fi
218unset xxx
219: ${xxx[3]}
220if	[[ ${!xxx[@]} ]]
221then	err_exit '${!xxx[@]} should be null'
222fi
223integer i=0
224{
225	set -x
226	xxx[++i]=1
227	set +x
228} 2> /dev/null
229if	(( i != 1))
230then	err_exit 'execution trace side effects with array subscripts'
231fi
232unset list
233: $(set -A list foo bar)
234if	(( ${#list[@]} != 0))
235then	err_exit '$(set -A list ...) leaves side effects'
236fi
237unset list
238list= (foo bar bam)
239( set -A list one two three four)
240if	[[ ${list[1]} != bar ]]
241then	err_exit 'array not restored after subshell'
242fi
243XPATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:.:/sbin:/usr/sbin
244xpath=( $( IFS=: ; echo $XPATH ) )
245if	[[ $(print -r  "${xpath[@]##*/}") != 'bin bin ucb bin . sbin sbin' ]]
246then	err_exit '${xpath[@]##*/} not applied to each element'
247fi
248foo=( zero one '' three four '' six)
249integer n=-1
250if	[[ ${foo[@]:n} != six ]]
251then	err_exit 'array offset of -1 not working'
252fi
253if	[[ ${foo[@]: -3:1} != four ]]
254then	err_exit 'array offset of -3:1 not working'
255fi
256$SHELL -c 'x=(if then else fi)' 2> /dev/null  || err_exit 'reserved words in x=() assignment not working'
257unset foo
258foo=one
259foo=( $foo two)
260if	[[ ${#foo[@]} != 2 ]]
261then	err_exit 'array getting unset before right hand side evaluation'
262fi
263foo=(143 3643 38732)
264export foo
265typeset -i foo
266if	[[ $($SHELL -c 'print $foo') != 143 ]]
267then	err_exit 'exporting indexed array not exporting 0-th element'
268fi
269( $SHELL   -c '
270	unset foo
271	typeset -A foo=([0]=143 [1]=3643 [2]=38732)
272	export foo
273	typeset -i foo
274	[[ $($SHELL -c "print $foo") == 143 ]]'
275) 2> /dev/null ||
276		err_exit 'exporting associative array not exporting 0-th element'
277unset foo
278typeset -A foo
279foo[$((10))]=ok 2> /dev/null || err_exit 'arithmetic expression as subscript not working'
280unset foo
281typeset -A foo
282integer foo=0
283[[ $foo == 0 ]] || err_exit 'zero element of associative array not being set'
284unset foo
285typeset -A foo=( [two]=1)
286for i in one three four five
287do	: ${foo[$i]}
288done
289if	[[ ${!foo[@]} != two ]]
290then	err_exit 'error in subscript names'
291fi
292unset x
293x=( 1 2 3)
294(x[1]=8)
295[[ ${x[1]} == 2 ]] || err_exit 'index array produce side effects in subshells'
296x=( 1 2 3)
297(
298	x+=(8)
299	[[ ${#x[@]} == 4 ]] || err_exit 'index array append in subshell error'
300)
301[[ ${#x[@]} == 3 ]] || err_exit 'index array append in subshell effects parent'
302x=( [one]=1 [two]=2 [three]=3)
303(x[two]=8)
304[[ ${x[two]} == 2 ]] || err_exit 'associative array produce side effects in subshells'
305unset x
306x=( [one]=1 [two]=2 [three]=3)
307(
308	x+=( [four]=4 )
309	[[ ${#x[@]} == 4 ]] || err_exit 'associative array append in subshell error'
310)
311[[ ${#x[@]} == 3 ]] || err_exit 'associative array append in subshell effects parent'
312unset x
313integer i
314for ((i=0; i < 40; i++))
315do	x[i]=$i
316done
317[[  ${#x[@]} == 40 ]] || err_exit 'index arrays loosing values'
318[[ $( ($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'
319unset foo
320[[ $(foo=good
321for ((i=0; i < 2; i++))
322do	[[ ${foo[i]} ]] && print ok
323done) == ok ]] || err_exit 'invalid optimization for subscripted variables'
324(
325x=([foo]=bar)
326set +A x bam
327) 2> /dev/null && err_exit 'set +A with associative array should be an error'
328unset bam foo
329foo=0
330typeset -A bam
331unset bam[foo]
332bam[foo]=value
333[[ $bam == value ]] && err_exit 'unset associative array element error'
334: only first element of an array can be exported
335unset bam
336print 'print ${var[0]} ${var[1]}' > $tmp/script
337chmod +x $tmp/script
338[[ $($SHELL -c "var=(foo bar);export var;$tmp/script") == foo ]] || err_exit 'export array not exporting just first element'
339unset foo
340set -o allexport
341foo=one
342foo[1]=two
343foo[0]=three
344[[ $foo == three ]] || err_exit 'export all not working with arrays'
345cat > $tmp/script <<- \!
346	typeset -A foo
347	print foo${foo[abc]}
348!
349# 04-05-24 bug fix
350unset foo
351[[ $($SHELL -c "typeset -A foo;$tmp/script")  == foo ]] 2> /dev/null || err_exit 'empty associative arrays not being cleared correctly before scripts'
352[[ $($SHELL -c "typeset -A foo;foo[abc]=abc;$tmp/script") == foo ]] 2> /dev/null || err_exit 'associative arrays not being cleared correctly before scripts'
353unset foo
354foo=(one two)
355[[ ${foo[@]:1} == two ]] || err_exit '${foo[@]:1} == two'
356[[ ! ${foo[@]:2} ]] || err_exit '${foo[@]:2} not null'
357unset foo
358foo=one
359[[ ! ${foo[@]:1} ]] || err_exit '${foo[@]:1} not null'
360function EMPTY
361{
362        typeset i
363        typeset -n ARRAY=$1
364        for i in ${!ARRAY[@]}
365        do      unset ARRAY[$i]
366        done
367}
368unset foo
369typeset -A foo
370foo[bar]=bam
371foo[x]=y
372EMPTY foo
373[[ $(typeset | grep foo$) == *associative* ]] || err_exit 'array lost associative attribute'
374[[ ! ${foo[@]}  ]] || err_exit 'array not empty'
375[[ ! ${!foo[@]}  ]] || err_exit 'array names not empty'
376unset foo
377foo=bar
378set -- "${foo[@]:1}"
379(( $# == 0 )) || err_exit '${foo[@]:1} should not have any values'
380unset bar
381: ${_foo[bar=4]}
382(( bar == 4 )) || err_exit 'subscript of unset variable not evaluated'
383unset foo bar
384foo[5]=4
385bar[4]=3
386bar[0]=foo
387foo[0]=bam
388foo[4]=5
389[[ ${!foo[2+2]} == 'foo[4]' ]] || err_exit '${!var[sub]} should be var[sub]'
390[[ ${bar[${foo[5]}]} == 3 ]] || err_exit  'array subscript cannot be an array instance'
391[[ $bar[4] == 3 ]] || err_exit '$bar[x] != ${bar[x]} inside [[ ]]'
392(( $bar[4] == 3  )) || err_exit '$bar[x] != ${bar[x]} inside (( ))'
393[[ $bar[$foo[5]] == 3 ]]  || err_exit '$bar[foo[x]] != ${bar[foo[x]]} inside [[ ]]'
394(( $bar[$foo[5]] == 3  )) || err_exit '$bar[foo[x]] != ${bar[foo[x]]} inside (( ))'
395x=$bar[4]
396[[ $x == 4 ]] && err_exit '$bar[4] should not be an array in an assignment'
397x=${bar[$foo[5]]}
398(( $x == 3 )) || err_exit '${bar[$foo[sub]]} not working'
399[[ $($SHELL  <<- \++EOF+++
400	typeset -i test_variable=0
401	typeset -A test_array
402	test_array[1]=100
403	read test_array[2] <<-!
404	2
405	!
406	read test_array[3] <<-!
407	3
408	!
409	test_array[3]=4
410	print "val=${test_array[3]}"
411++EOF+++
412) == val=4 ]] 2> /dev/null || err_exit 'after reading array[j] and assign array[j] fails'
413[[ $($SHELL <<- \+++EOF+++
414	pastebin=( typeset -a form)
415	pastebin.form+=( name="name"   data="clueless" )
416	print -r -- ${pastebin.form[0].name}
417+++EOF+++
418) == name ]] 2> /dev/null ||  err_exit 'indexed array in compound variable not working'
419unset foo bar
420: ${foo[bar=2]}
421[[ $bar == 2 ]] || err_exit 'subscript not evaluated for unset variable'
422unset foo bar
423bar=1
424typeset -a foo=([1]=ok [2]=no)
425[[ $foo[bar] == ok ]] || err_exit 'typeset -a not working for simple assignment'
426unset foo
427typeset -a foo=([1]=(x=ok) [2]=(x=no))
428[[ $(typeset | grep 'foo$') == *index* ]] || err_exit 'typeset -a not creating an indexed array'
429foo+=([5]=good)
430[[ $(typeset | grep 'foo$') == *index* ]] || err_exit 'append to indexed array not preserving array type'
431unset foo
432typeset -A foo=([1]=ok [2]=no)
433[[ $foo[bar] == ok ]] && err_exit 'typeset -A not working for simple assignment'
434unset foo
435typeset -A foo=([1]=(x=ok) [2]=(x=no))
436[[ ${foo[bar].x} == ok ]] && err_exit 'typeset -A not working for compound assignment'
437[[ $($SHELL -c 'typeset -a foo;typeset | grep "foo$"'  2> /dev/null) == *index* ]] || err_exit 'typeset fails for indexed array with no elements'
438xxxxx=(one)
439[[ $(typeset | grep xxxxx$) == *'indexed array'* ]] || err_exit 'array of one element not an indexed array'
440unset foo
441foo[1]=(x=3 y=4)
442{ [[ ${!foo[1].*} == 'foo[1].x foo[1].y' ]] ;} 2> /dev/null || err_exit '${!foo[sub].*} not expanding correctly'
443unset x
444x=( typeset -a foo=( [0]="a" [1]="b" [2]="c" ))
445[[  ${@x.foo} == 'typeset -a'* ]] || err_exit 'x.foo is not an indexed array'
446x=( typeset -A foo=( [0]="a" [1]="b" [2]="c" ))
447[[  ${@x.foo} == 'typeset -A'* ]] || err_exit 'x.foo is not an associative array'
448$SHELL -c $'x=(foo\n\tbar\nbam\n)' 2> /dev/null || err_exit 'compound array assignment with new-lines not working'
449$SHELL -c $'x=(foo\n\tbar:\nbam\n)' 2> /dev/null || err_exit 'compound array assignment with labels not working'
450$SHELL -c $'x=(foo\n\tdone\nbam\n)' 2> /dev/null || err_exit 'compound array assignment with reserved words not working'
451[[ $($SHELL -c 'typeset -A A; print $(( A[foo].bar ))' 2> /dev/null) == 0 ]] || err_exit 'unset variable not evaluating to 0'
452unset a
453typeset -A a
454a[a].z=1
455a[z].z=2
456unset a[a]
457[[ ${!a[@]} == z ]] || err_exit '"unset a[a]" unsets entire array'
458unset a
459a=([x]=1 [y]=2 [z]=(foo=3 bar=4))
460eval "b=$(printf "%B\n" a)"
461eval "c=$(printf "%#B\n" a)"
462[[ ${a[*]} == "${b[*]}" ]] || err_exit 'printf %B not preserving values for arrays'
463[[ ${a[*]} == "${c[*]}" ]] || err_exit 'printf %#B not preserving values for arrays'
464unset a
465a=(zero one two three four)
466a[6]=six
467[[ ${a[-1]} == six ]] || err_exit 'a[-1] should be six'
468[[ ${a[-3]} == four ]] || err_exit 'a[-3] should be four'
469[[ ${a[-3..-1]} == 'four six' ]] || err_exit "a[-3,-1] should be 'four six'"
470
471FILTER=(typeset scope)
472FILTER[0].scope=include
473FILTER[1].scope=exclude
474[[ ${#FILTER[@]} == 2 ]] ||  err_exit "FILTER array should have two elements not ${#FILTER[@]}"
475
476exit $((Errors))
477