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