]> git.ipfire.org Git - thirdparty/bash.git/blob - tests/assoc.tests
Bash-5.2-rc4 release
[thirdparty/bash.git] / tests / assoc.tests
1 # This program is free software: you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation, either version 3 of the License, or
4 # (at your option) any later version.
5 #
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU General Public License for more details.
10 #
11 # You should have received a copy of the GNU General Public License
12 # along with this program. If not, see <http://www.gnu.org/licenses/>.
13 #
14 # TEST - basic declaration and assignment
15 typeset -A fluff
16 declare -A
17
18 fluff[foo]=one
19 fluff[bar]=two
20
21 declare -A
22 declare -p fluff
23
24 unset fluff[foo]
25 declare -p fluff
26
27 fluff[bar]=newval
28 declare fluff[qux]=assigned
29
30 declare -p fluff
31
32 unset fluff
33
34 # TEST - compound assignment and variable attributes
35 declare -A wheat chaff
36 wheat=( [zero]=0 [one]=a [two]=b [three]=c )
37
38 declare -i chaff
39 chaff=( [zero]=1+4 [one]=3+7 four )
40
41 declare -A waste=( [pid]=42134 [version]=4.0-devel [source]=$0 [lineno]=$LINENO )
42 declare -r waste
43
44 declare -A
45
46 declare +i chaff
47 chaff[hello world]=flip
48 declare -p chaff
49
50 # TEST - no longer errors
51 waste[stuff]=other
52 unset waste
53 chaff[*]=12
54 chaff=( [one]=a [*]=12 )
55
56 # TEST - key expansion -- no word splitting
57 chaff[hello world]=flip
58 declare -p chaff
59 echo ${chaff[hello world]}
60
61 chaff[box]="multiple words"
62
63 recho ${chaff[@]}
64 recho "${chaff[@]}"
65
66 recho ${chaff[*]}
67 recho "${chaff[*]}"
68
69 unset chaff
70 declare -A chaff[200]
71 declare +A chaff
72
73 chaff[*]=12
74 chaff=( [one]=a [*]=12 )
75
76 # TEST - keys and values containing spaces
77 unset wheat
78 declare -A wheat
79 wheat=([six]=6 [foo bar]="qux qix" )
80
81 declare -p wheat
82
83 unset wheat
84 declare -A wheat=([six]=6 [foo bar]="qux qix" )
85
86 recho ${wheat[foo bar]}
87 recho "${wheat[foo bar]}"
88
89 declare -p wheat
90
91 # TEST - basic expansions: number of elements and value length
92 unset wheat
93 typeset -A wheat
94 wheat=([six]=6 [foo bar]="qux qix" )
95
96 recho ${#wheat[@]}
97
98 recho ${#wheat[foo bar]}
99
100 # TEST - appending assignment operator
101 unset wheat
102 typeset -A wheat
103 wheat=([six]=6 [foo bar]="qux qix" )
104
105 wheat[foo bar]+=' blat'
106
107 recho ${wheat[foo bar]}
108 recho "${wheat[foo bar]}"
109 unset wheat
110
111 flix=9
112 typeset -Ai wheat
113 wheat=([six]=6 [foo bar]=flix )
114
115 wheat[foo bar]+=7
116
117 recho ${wheat[foo bar]}
118 recho "${wheat[foo bar]}"
119 unset flix wheat
120
121 # TEST - index expansion: no word splitting or globbing
122 typeset -A wheat
123 cd ${TMPDIR:=/tmp}
124 touch '[sfiri]'
125 wheat=([s*]=6 [foo bar]=flix )
126
127 recho ${wheat[@]}
128 rm '[sfiri]'
129 cd $OLDPWD
130
131 # TEST -- associative array keys expansion
132 unset wheat
133 typeset -A wheat
134
135 wheat=([six]=6 [foo bar]=flix )
136
137 recho ${!wheat[@]}
138 recho "${!wheat[@]}"
139
140 # TEST -- associative array pattern removal
141 unset xpath
142 typeset -A xpath
143
144 xpath=( [0]=/bin [one]=/bin [two]=/usr/bin [three]=/usr/ucb [four]=/usr/local/bin)
145 xpath+=( [five]=/sbin [six]=/usr/sbin [seven]=. )
146
147 echo ${#xpath[@]}
148
149 echo ${xpath[@]}
150 echo ${xpath[@]##*/}
151 echo ${xpath[0]##*/}
152 echo ${xpath[@]%%[!/]*}
153 echo ${xpath[0]%%[!/]*}
154 recho ${xpath##*/}
155 recho ${xpath%%[!/]*}
156 recho ${xpath[five]##*/}
157 recho ${xpath[five]%%[!/]*}
158
159 echo ${#xpath[*]}
160
161 echo ${xpath[*]}
162 echo ${xpath[*]##*/}
163 echo ${xpath[*]%%[!/]*}
164
165 # TEST -- associative array pattern substitution
166 unset xpath
167 typeset -A xpath
168
169 xpath=( [0]=/bin [one]=/bin [two]=/usr/bin [three]=/usr/ucb [four]=/usr/local/bin)
170 xpath+=( [five]=/sbin [six]=/usr/sbin [seven]=. )
171
172 echo ${#xpath[@]}
173 # default element is "0" (as a string)
174 echo ${#xpath} -- ${xpath["0"]}
175
176 echo ${xpath[@]//\//^}
177 echo "${xpath[@]//\//^}" | cat -v
178
179 zecho "${xpath[@]/\//\\}"
180 zecho "${xpath[@]//\//\\}"
181 zecho "${xpath[@]//[\/]/\\}"
182
183 # test assignment to key "0"
184 unset T
185 declare -A T
186 T='([a]=1)'
187 echo "${T[@]}"
188 unset T
189
190 # peculiar ksh93 semantics for unsubscripted assoc variable reference
191 declare -A T
192 T[0]='zero'
193 if [ "$T" != "${T[0]}" ]; then
194 echo 'assoc.tests: $T and ${T[0]} mismatch'
195 fi
196
197 ${THIS_SH} ./assoc1.sub
198
199 ${THIS_SH} ./assoc2.sub
200
201 ${THIS_SH} ./assoc3.sub
202
203 ${THIS_SH} ./assoc4.sub
204
205 ${THIS_SH} ./assoc5.sub
206
207 ${THIS_SH} ./assoc6.sub
208
209 ${THIS_SH} ./assoc7.sub
210
211 # test converting between scalars and assoc arrays
212 unset assoc
213 assoc=assoc
214 declare -A assoc
215 declare -p assoc
216 echo ${assoc[@]}
217
218 # weird syntax required to append to multiple existing array elements using
219 # compound assignment syntax
220 unset assoc
221 declare -A assoc
222 assoc=( [one]=one [two]=two [three]=three )
223 assoc+=( [one]+=more [two]+=less )
224 declare -p assoc
225
226 readonly -A assoc
227 declare -p assoc
228
229 declare -A hash
230
231 hash=(["key"]="value1")
232 declare -p hash
233 hash=(["key"]="${hash["key"]} value2")
234 declare -p hash
235
236 unset hash
237
238 ${THIS_SH} ./assoc8.sub
239
240 # new shopt option to prevent multiple expansion of assoc array subscripts
241 ${THIS_SH} ./assoc9.sub
242
243 ${THIS_SH} ./assoc10.sub
244
245 # test assigning associative arrays using compound key/value pair assignments
246 ${THIS_SH} ./assoc11.sub
247
248 # more kvpair associative array assignment tests
249 ${THIS_SH} ./assoc12.sub
250
251 # assignment to @ and *
252 ${THIS_SH} ./assoc13.sub
253
254 # tests of the @k transformation on associative arrays
255 ${THIS_SH} ./assoc14.sub
256
257 # tests with subscripts and values containing 0x01 (some indexed array tests too)
258 ${THIS_SH} ./assoc15.sub
259
260 # tests with subscripts being expanded more than one in ${xxx} word expansions
261 ${THIS_SH} ./assoc16.sub
262
263 # tests with `[' and `]' subscripts and `unset'
264 ${THIS_SH} ./assoc17.sub
265
266 # tests with `[' and `]' subscripts and printf/read/wait builtins
267 ${THIS_SH} ./assoc18.sub
268