]> git.ipfire.org Git - thirdparty/bash.git/blame - tests/assoc.tests
bash-5.0 distribution sources and documentation
[thirdparty/bash.git] / tests / assoc.tests
CommitLineData
3185942a
JA
1# TEST - basic declaration and assignment
2typeset -A fluff
3declare -A
4
5fluff[foo]=one
6fluff[bar]=two
7
8declare -A
9declare -p fluff
10
11unset fluff[foo]
12declare -p fluff
13
14fluff[bar]=newval
0001803f
CR
15declare fluff[qux]=assigned
16
3185942a
JA
17declare -p fluff
18
19unset fluff
20
21# TEST - compount assignment and variable attributes
22declare -A wheat chaff
23wheat=( [zero]=0 [one]=a [two]=b [three]=c )
24
25declare -i chaff
26chaff=( [zero]=1+4 [one]=3+7 four )
27
28declare -A waste=( [pid]=42134 [version]=4.0-devel [source]=$0 [lineno]=$LINENO )
29declare -r waste
30
31declare -A
32
33declare +i chaff
34chaff[hello world]=flip
35declare -p chaff
36
37# TEST - errors
a0c0a00f 38waste[stuff]=other
3185942a
JA
39unset waste
40chaff[*]=12
41chaff=( [one]=a [*]=12 )
42
43# TEST - key expansion -- no word splitting
44chaff[hello world]=flip
45declare -p chaff
46echo ${chaff[hello world]}
47
48chaff[box]="multiple words"
49
50recho ${chaff[@]}
51recho "${chaff[@]}"
52
53recho ${chaff[*]}
54recho "${chaff[*]}"
55
56unset chaff
57declare -A chaff[200]
58declare +A chaff
59
60chaff[*]=12
61chaff=( [one]=a [*]=12 )
62
63# TEST - keys and values containing spaces
64unset wheat
65declare -A wheat
66wheat=([six]=6 [foo bar]="qux qix" )
67
68declare -p wheat
69
70unset wheat
71declare -A wheat=([six]=6 [foo bar]="qux qix" )
72
73recho ${wheat[foo bar]}
74recho "${wheat[foo bar]}"
75
76declare -p wheat
77
78# TEST - basic expansions: number of elements and value length
79unset wheat
80typeset -A wheat
81wheat=([six]=6 [foo bar]="qux qix" )
82
83recho ${#wheat[@]}
84
85recho ${#wheat[foo bar]}
86
87# TEST - appending assignment operator
88unset wheat
89typeset -A wheat
90wheat=([six]=6 [foo bar]="qux qix" )
91
92wheat[foo bar]+=' blat'
93
94recho ${wheat[foo bar]}
95recho "${wheat[foo bar]}"
96unset wheat
97
98flix=9
99typeset -Ai wheat
100wheat=([six]=6 [foo bar]=flix )
101
102wheat[foo bar]+=7
103
104recho ${wheat[foo bar]}
105recho "${wheat[foo bar]}"
106unset flix wheat
107
108# TEST - index expansion: no word splitting or globbing
109typeset -A wheat
d233b485 110cd ${TMPDIR:=/tmp}
3185942a
JA
111touch '[sfiri]'
112wheat=([s*]=6 [foo bar]=flix )
113
114recho ${wheat[@]}
115rm '[sfiri]'
116cd $OLDPWD
117
118# TEST -- associative array keys expansion
119unset wheat
120typeset -A wheat
121
122wheat=([six]=6 [foo bar]=flix )
123
124recho ${!wheat[@]}
125recho "${!wheat[@]}"
126
127# TEST -- associative array pattern removal
128unset xpath
129typeset -A xpath
130
131xpath=( [0]=/bin [one]=/bin [two]=/usr/bin [three]=/usr/ucb [four]=/usr/local/bin)
132xpath+=( [five]=/sbin [six]=/usr/sbin [seven]=. )
133
134echo ${#xpath[@]}
135
136echo ${xpath[@]}
137echo ${xpath[@]##*/}
138echo ${xpath[0]##*/}
139echo ${xpath[@]%%[!/]*}
140echo ${xpath[0]%%[!/]*}
141recho ${xpath##*/}
142recho ${xpath%%[!/]*}
143recho ${xpath[five]##*/}
144recho ${xpath[five]%%[!/]*}
145
146echo ${#xpath[*]}
147
148echo ${xpath[*]}
149echo ${xpath[*]##*/}
150echo ${xpath[*]%%[!/]*}
151
152# TEST -- associative array pattern substitution
153unset xpath
154typeset -A xpath
155
156xpath=( [0]=/bin [one]=/bin [two]=/usr/bin [three]=/usr/ucb [four]=/usr/local/bin)
157xpath+=( [five]=/sbin [six]=/usr/sbin [seven]=. )
158
159echo ${#xpath[@]}
160# default element is "0" (as a string)
161echo ${#xpath} -- ${xpath["0"]}
162
163echo ${xpath[@]//\//^}
164echo "${xpath[@]//\//^}" | cat -v
165
166zecho "${xpath[@]/\//\\}"
167zecho "${xpath[@]//\//\\}"
168zecho "${xpath[@]//[\/]/\\}"
169
0001803f
CR
170# test assignment to key "0"
171unset T
172declare -A T
173T='([a]=1)'
174echo "${T[@]}"
175unset T
176
d233b485
CR
177# peculiar ksh93 semantics for unsubscripted assoc variable reference
178declare -A T
179T[0]='zero'
180if [ "$T" != "${T[0]}" ]; then
181 echo 'assoc.tests: $T and ${T[0]} mismatch'
182fi
183
3185942a
JA
184${THIS_SH} ./assoc1.sub
185
186${THIS_SH} ./assoc2.sub
187
188${THIS_SH} ./assoc3.sub
189
190${THIS_SH} ./assoc4.sub
0001803f
CR
191
192${THIS_SH} ./assoc5.sub
193
194${THIS_SH} ./assoc6.sub
ac50fbac
CR
195
196${THIS_SH} ./assoc7.sub
a0c0a00f
CR
197
198# test converting between scalars and assoc arrays
199unset assoc
200assoc=assoc
201declare -A assoc
202declare -p assoc
203echo ${assoc[@]}
204
205# weird syntax required to append to multiple existing array elements using
206# compound assignment syntax
207unset assoc
208declare -A assoc
209assoc=( [one]=one [two]=two [three]=three )
210assoc+=( [one]+=more [two]+=less )
211declare -p assoc
212
213readonly -A assoc
214declare -p assoc
215
216${THIS_SH} ./assoc8.sub
d233b485
CR
217
218# new shopt option to prevent multiple expansion of assoc array subscripts
219${THIS_SH} ./assoc9.sub
220
221${THIS_SH} ./assoc10.sub