xref: /titanic_50/usr/src/lib/libshell/common/tests/arrays2.sh (revision 174bc6499d233e329ecd3d98a880a7b07df16bfa)
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########################################################################
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
30for	((i=0; i < 4; i++ ))
31do	for	((j=0; j < 5; j++ ))
32	do	a[i][j]=$i$j
33	done
34done
35for	((i=0; i < 4; i++ ))
36do	for	((j=0; j < 5; j++ ))
37	do	[[ ${a[i][j]} == "$i$j" ]] || err_exit "\${a[$i][$j]} != $i$j"
38	done
39done
40for	((i=0; i < 4; i++ ))
41do	j=0;for k in ${a[i][@]}
42	do	[[ $k == "$i$j" ]] || err_exit "\${a[i][@]} != $i$j"
43		(( j++ ))
44	done
45done
46unset a
47a=(
48	( 00 01 02 03 04 )
49	( 10 11 12 13 14 15)
50	( 20 21 22 23 24 )
51	( 30 31 32 33 34 )
52)
53
54function check
55{
56	nameref a=$1
57	nameref b=a[2]
58	typeset c=$1
59	integer i j
60	for	((i=0; i < 4; i++ ))
61	do	for	((j=0; j < 5; j++ ))
62		do	[[ ${a[$i][$j]} == "$i$j" ]] || err_exit "\${$c[$i][$j]} != $i$j"
63		done
64	done
65	(( ${#a[@]} == 4 )) || err_exit "\${#$c[@]} not 4"
66	(( ${#a[0][@]} == 5 )) || err_exit "\${#$c[0][@]} not 5"
67	(( ${#a[1][@]} == 6 )) || err_exit "\${#$c[1][@]} not 6"
68	set -s -- ${!a[@]}
69	[[ ${@} == '0 1 2 3' ]] || err_exit "\${!$c[@]} not 0 1 2 3"
70	set -s -- ${!a[0][@]}
71	[[ ${@} == '0 1 2 3 4' ]] || err_exit "\${!$c[0][@]} not 0 1 2 3 4"
72	set -s -- ${!a[1][@]}
73	[[ ${@} == '0 1 2 3 4 5' ]] || err_exit "\${!$c[1][@]} not 0 1 2 3 4 5"
74	[[ $a == 00 ]] || err_exit  "\$$c is not 00"
75	[[ ${a[0]} == 00 ]] || err_exit  "\${$a[0]} is not 00"
76	[[ ${a[0][0]} == 00 ]] || err_exit  "${a[0][0]} is not 00"
77	[[ ${a[0][0][0]} == 00 ]] || err_exit  "\${$c[0][0][0]} is not 00"
78	[[ ${a[0][0][1]} == '' ]] || err_exit  "\${$c[0][0][1]} is not empty"
79	[[ ${b[3]} == 23 ]] || err_exit "${!b}[3] not = 23"
80}
81
82check a
83
84unset a
85typeset -A a
86for	((i=0; i < 4; i++ ))
87do	for	((j=0; j < 5; j++ ))
88	do	a[$i][j]=$i$j
89	done
90done
91for	((i=0; i < 4; i++ ))
92do	for	((j=0; j < 5; j++ ))
93	do	[[ ${a[$i][j]} == "$i$j" ]] || err_exit "\${a[$i][$j]} == $i$j"
94	done
95done
96a[1][5]=15
97b=(
98	[0]=( 00 01 02 03 04 )
99	[1]=( 10 11 12 13 14 15)
100	[2]=( 20 21 22 23 24 )
101	[3]=( 30 31 32 33 34 )
102)
103check b
104[[ ${a[1][@]} == "${b[1][@]}" ]] || err_exit "a[1] not equal to b[1]"
105c=(
106	[0]=( [0]=00 [1]=01 [2]=02 [3]=03 [4]=04 )
107	[1]=( [0]=10 [1]=11 [2]=12 [3]=13 [4]=14 [5]=15)
108	[2]=( [0]=20 [1]=21 [2]=22 [3]=23 [4]=24 )
109	[3]=( [0]=30 [1]=31 [2]=32 [3]=33 [4]=34 )
110)
111check c
112typeset -A d
113d[0]=( [0]=00 [1]=01 [2]=02 [3]=03 [4]=04 )
114d[1]=( [0]=10 [1]=11 [2]=12 [3]=13 [4]=14 [5]=15)
115d[2]=( [0]=20 [1]=21 [2]=22 [3]=23 [4]=24 )
116d[3]=( [0]=30 [1]=31 [2]=32 [3]=33 [4]=34 )
117check d
118unset a b c d
119[[ ${a-set} ]] || err_exit "a is set after unset"
120[[ ${b-set} ]] || err_exit "b is set after unset"
121[[ ${c-set} ]] || err_exit "c is set after unset"
122[[ ${d-set} ]] || err_exit "c is set after unset"
123exit $((Errors))
124