]> git.ipfire.org Git - thirdparty/bash.git/blame - tests/assoc.tests
Bash-4.3 patch 7
[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
38unset waste
39chaff[*]=12
40chaff=( [one]=a [*]=12 )
41
42# TEST - key expansion -- no word splitting
43chaff[hello world]=flip
44declare -p chaff
45echo ${chaff[hello world]}
46
47chaff[box]="multiple words"
48
49recho ${chaff[@]}
50recho "${chaff[@]}"
51
52recho ${chaff[*]}
53recho "${chaff[*]}"
54
55unset chaff
56declare -A chaff[200]
57declare +A chaff
58
59chaff[*]=12
60chaff=( [one]=a [*]=12 )
61
62# TEST - keys and values containing spaces
63unset wheat
64declare -A wheat
65wheat=([six]=6 [foo bar]="qux qix" )
66
67declare -p wheat
68
69unset wheat
70declare -A wheat=([six]=6 [foo bar]="qux qix" )
71
72recho ${wheat[foo bar]}
73recho "${wheat[foo bar]}"
74
75declare -p wheat
76
77# TEST - basic expansions: number of elements and value length
78unset wheat
79typeset -A wheat
80wheat=([six]=6 [foo bar]="qux qix" )
81
82recho ${#wheat[@]}
83
84recho ${#wheat[foo bar]}
85
86# TEST - appending assignment operator
87unset wheat
88typeset -A wheat
89wheat=([six]=6 [foo bar]="qux qix" )
90
91wheat[foo bar]+=' blat'
92
93recho ${wheat[foo bar]}
94recho "${wheat[foo bar]}"
95unset wheat
96
97flix=9
98typeset -Ai wheat
99wheat=([six]=6 [foo bar]=flix )
100
101wheat[foo bar]+=7
102
103recho ${wheat[foo bar]}
104recho "${wheat[foo bar]}"
105unset flix wheat
106
107# TEST - index expansion: no word splitting or globbing
108typeset -A wheat
109cd /tmp
110touch '[sfiri]'
111wheat=([s*]=6 [foo bar]=flix )
112
113recho ${wheat[@]}
114rm '[sfiri]'
115cd $OLDPWD
116
117# TEST -- associative array keys expansion
118unset wheat
119typeset -A wheat
120
121wheat=([six]=6 [foo bar]=flix )
122
123recho ${!wheat[@]}
124recho "${!wheat[@]}"
125
126# TEST -- associative array pattern removal
127unset xpath
128typeset -A xpath
129
130xpath=( [0]=/bin [one]=/bin [two]=/usr/bin [three]=/usr/ucb [four]=/usr/local/bin)
131xpath+=( [five]=/sbin [six]=/usr/sbin [seven]=. )
132
133echo ${#xpath[@]}
134
135echo ${xpath[@]}
136echo ${xpath[@]##*/}
137echo ${xpath[0]##*/}
138echo ${xpath[@]%%[!/]*}
139echo ${xpath[0]%%[!/]*}
140recho ${xpath##*/}
141recho ${xpath%%[!/]*}
142recho ${xpath[five]##*/}
143recho ${xpath[five]%%[!/]*}
144
145echo ${#xpath[*]}
146
147echo ${xpath[*]}
148echo ${xpath[*]##*/}
149echo ${xpath[*]%%[!/]*}
150
151# TEST -- associative array pattern substitution
152unset xpath
153typeset -A xpath
154
155xpath=( [0]=/bin [one]=/bin [two]=/usr/bin [three]=/usr/ucb [four]=/usr/local/bin)
156xpath+=( [five]=/sbin [six]=/usr/sbin [seven]=. )
157
158echo ${#xpath[@]}
159# default element is "0" (as a string)
160echo ${#xpath} -- ${xpath["0"]}
161
162echo ${xpath[@]//\//^}
163echo "${xpath[@]//\//^}" | cat -v
164
165zecho "${xpath[@]/\//\\}"
166zecho "${xpath[@]//\//\\}"
167zecho "${xpath[@]//[\/]/\\}"
168
0001803f
CR
169# test assignment to key "0"
170unset T
171declare -A T
172T='([a]=1)'
173echo "${T[@]}"
174unset T
175
3185942a
JA
176${THIS_SH} ./assoc1.sub
177
178${THIS_SH} ./assoc2.sub
179
180${THIS_SH} ./assoc3.sub
181
182${THIS_SH} ./assoc4.sub
0001803f
CR
183
184${THIS_SH} ./assoc5.sub
185
186${THIS_SH} ./assoc6.sub
ac50fbac
CR
187
188${THIS_SH} ./assoc7.sub