Imported from ../bash-4.0-rc1.tar.gz.
[platform/upstream/bash.git] / tests / assoc.tests
1 # TEST - basic declaration and assignment
2 typeset -A fluff
3 declare -A
4
5 fluff[foo]=one
6 fluff[bar]=two
7
8 declare -A
9 declare -p fluff
10
11 unset fluff[foo]
12 declare -p fluff
13
14 fluff[bar]=newval
15 declare -p fluff
16
17 unset fluff
18
19 # TEST - compount assignment and variable attributes
20 declare -A wheat chaff
21 wheat=( [zero]=0 [one]=a [two]=b [three]=c )
22
23 declare -i chaff
24 chaff=( [zero]=1+4 [one]=3+7 four )
25
26 declare -A waste=( [pid]=42134 [version]=4.0-devel [source]=$0 [lineno]=$LINENO )
27 declare -r waste
28
29 declare -A
30
31 declare +i chaff
32 chaff[hello world]=flip
33 declare -p chaff
34
35 # TEST - errors
36 unset waste
37 chaff[*]=12
38 chaff=( [one]=a [*]=12 )
39
40 # TEST - key expansion -- no word splitting
41 chaff[hello world]=flip
42 declare -p chaff
43 echo ${chaff[hello world]}
44
45 chaff[box]="multiple words"
46
47 recho ${chaff[@]}
48 recho "${chaff[@]}"
49
50 recho ${chaff[*]}
51 recho "${chaff[*]}"
52
53 unset chaff
54 declare -A chaff[200]
55 declare +A chaff
56
57 chaff[*]=12
58 chaff=( [one]=a [*]=12 )
59
60 # TEST - keys and values containing spaces
61 unset wheat
62 declare -A wheat
63 wheat=([six]=6 [foo bar]="qux qix" )
64
65 declare -p wheat
66
67 unset wheat
68 declare -A wheat=([six]=6 [foo bar]="qux qix" )
69
70 recho ${wheat[foo bar]}
71 recho "${wheat[foo bar]}"
72
73 declare -p wheat
74
75 # TEST - basic expansions: number of elements and value length
76 unset wheat
77 typeset -A wheat
78 wheat=([six]=6 [foo bar]="qux qix" )
79
80 recho ${#wheat[@]}
81
82 recho ${#wheat[foo bar]}
83
84 # TEST - appending assignment operator
85 unset wheat
86 typeset -A wheat
87 wheat=([six]=6 [foo bar]="qux qix" )
88
89 wheat[foo bar]+=' blat'
90
91 recho ${wheat[foo bar]}
92 recho "${wheat[foo bar]}"
93 unset wheat
94
95 flix=9
96 typeset -Ai wheat
97 wheat=([six]=6 [foo bar]=flix )
98
99 wheat[foo bar]+=7
100
101 recho ${wheat[foo bar]}
102 recho "${wheat[foo bar]}"
103 unset flix wheat
104
105 # TEST - index expansion: no word splitting or globbing
106 typeset -A wheat
107 cd /tmp
108 touch '[sfiri]'
109 wheat=([s*]=6 [foo bar]=flix )
110
111 recho ${wheat[@]}
112 rm '[sfiri]'
113 cd $OLDPWD
114
115 # TEST -- associative array keys expansion
116 unset wheat
117 typeset -A wheat
118
119 wheat=([six]=6 [foo bar]=flix )
120
121 recho ${!wheat[@]}
122 recho "${!wheat[@]}"
123
124 # TEST -- associative array pattern removal
125 unset xpath
126 typeset -A xpath
127
128 xpath=( [0]=/bin [one]=/bin [two]=/usr/bin [three]=/usr/ucb [four]=/usr/local/bin)
129 xpath+=( [five]=/sbin [six]=/usr/sbin [seven]=. )
130
131 echo ${#xpath[@]}
132
133 echo ${xpath[@]}
134 echo ${xpath[@]##*/}
135 echo ${xpath[0]##*/}
136 echo ${xpath[@]%%[!/]*}
137 echo ${xpath[0]%%[!/]*}
138 recho ${xpath##*/}
139 recho ${xpath%%[!/]*}
140 recho ${xpath[five]##*/}
141 recho ${xpath[five]%%[!/]*}
142
143 echo ${#xpath[*]}
144
145 echo ${xpath[*]}
146 echo ${xpath[*]##*/}
147 echo ${xpath[*]%%[!/]*}
148
149 # TEST -- associative array pattern substitution
150 unset xpath
151 typeset -A xpath
152
153 xpath=( [0]=/bin [one]=/bin [two]=/usr/bin [three]=/usr/ucb [four]=/usr/local/bin)
154 xpath+=( [five]=/sbin [six]=/usr/sbin [seven]=. )
155
156 echo ${#xpath[@]}
157 # default element is "0" (as a string)
158 echo ${#xpath} -- ${xpath["0"]}
159
160 echo ${xpath[@]//\//^}
161 echo "${xpath[@]//\//^}" | cat -v
162
163 zecho "${xpath[@]/\//\\}"
164 zecho "${xpath[@]//\//\\}"
165 zecho "${xpath[@]//[\/]/\\}"
166
167 ${THIS_SH} ./assoc1.sub
168
169 ${THIS_SH} ./assoc2.sub
170
171 ${THIS_SH} ./assoc3.sub
172
173 ${THIS_SH} ./assoc4.sub