-
Notifications
You must be signed in to change notification settings - Fork 6
/
arrays.yaml
309 lines (255 loc) · 7.13 KB
/
arrays.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
name: "Arrays"
incompatible_configs: ["sh"]
common_test_files:
- path: "helpers.sh"
contents: |
stable_print_assoc_array() {
# TODO: enable use of nameref when implemented; for now
# we assume the name of the array is assoc_array
# local -n assoc_array=$1
local key
for key in $(printf "%s\n" "${!assoc_array[@]}" | sort -n); do
echo "\"${key}\" => ${assoc_array[${key}]}"
done
}
cases:
- name: "Basic indexed array definition and access"
stdin: |
var=(a b c)
echo "var: $var"
echo "var: ${var}"
echo "var[0]: ${var[0]}"
echo "var[1]: ${var[1]}"
echo "var[2]: ${var[2]}"
echo "var[3]: ${var[3]}"
echo "var[4]: ${var[4]}"
echo "var[*]: ${var[*]}"
declare -p var
- name: "Define array with expansion"
stdin: |
body='a b'
var=($body)
declare -p var
declare -a var2=($body)
declare -p var2
- name: "Arrays with empty values"
stdin: |
var1=(a "" c)
declare -p var1
var2=(a "$nonexistent" c)
declare -p var2
var3=("" "" "")
declare -p var3
- name: "Basic associative array definition and access"
stdin: |
declare -A var=([0]=99 [a]=3 [b]=4 [c]=5)
echo "var: $var"
echo "var: ${var}"
echo "var[0]: ${var[0]}"
echo "var[a]: ${var[a]}"
echo "var[b]: ${var[b]}"
echo "var[c]: ${var[c]}"
echo "var[d]: ${var[d]}"
key="a"
echo "var[key]: ${var[${key}]}"
- name: "Array updating via index"
stdin: |
var=(a b c)
var[1]="d"
echo "var[*]: ${var[*]}"
declare -p var
- name: "Replacing array with string"
stdin: |
var=(a b c)
var="x"
echo "var[*]: ${var[*]}"
declare -p var
- name: "Array replacing"
stdin: |
var=(a b c)
var=(d e f)
echo "var[*]: ${var[*]}"
declare -p var
- name: "Appending array to indexed array"
stdin: |
var=(a b c)
var+=(e f g)
echo "var[*]: ${var[*]}"
declare -p var
var+=("" "" "")
declare -p var
var+=(h "i" "$nonexistent" j)
var+=(j k l)
declare -p var
- name: "Appending array to declared-but-unset indexed array"
stdin: |
declare -a var
var+=(e f g)
echo "var[*]: ${var[*]}"
declare -p var
- name: "Appending item to indexed array"
stdin: |
var=(a b c)
var+=d
echo "var[*]: ${var[*]}"
declare -p var
- name: "Appending item to element of indexed array"
stdin: |
declare -a var=(1 2 3)
var[1]+=2
echo "var[*]: ${var[*]}"
declare -p var
- name: "Appending item to element of int indexed array"
stdin: |
declare -ai var=(1 2 3)
var[1]+=2
echo "var[*]: ${var[*]}"
declare -p var
- name: "Appending array to associative array"
stdin: |
source helpers.sh
declare -A assoc_array=([a]=1 [b]=2 [c]=3)
assoc_array+=([d]=4 [e]=5 [f]=6)
stable_print_assoc_array assoc_array
- name: "Appending array to declared-but-unset associative array"
stdin: |
source helpers.sh
declare -A assoc_array
assoc_array+=([d]=4 [e]=5 [f]=6)
stable_print_assoc_array assoc_array
- name: "Appending item to associative array"
stdin: |
source helpers.sh
declare -A assoc_array=([a]=1 [b]=2 [c]=3)
assoc_array+=d
stable_print_assoc_array assoc_array
- name: "Appending item to element of associative array"
stdin: |
source helpers.sh
declare -A assoc_array=([a]=1 [b]=2 [c]=3)
assoc_array[b]+=2
stable_print_assoc_array assoc_array
- name: "Appending item to element of int associative array"
stdin: |
source helpers.sh
declare -Ai assoc_array=([a]=1 [b]=2 [c]=3)
assoc_array[b]+=2
stable_print_assoc_array assoc_array
- name: "Appending array to item"
stdin: |
var=x
var+=(a b c)
echo "var[*]: ${var[*]}"
declare -p var
- name: "Fill associative array"
stdin: |
declare -Ag myarray
myfunc() {
local myarg=$1 element
shift
for element in "$@"; do
echo "Setting: myarray[$element]=$myarg"
myarray[$element]="${myarg}${element}"
done
}
myfunc 'first' bunzip2 bzcat pbunzip2 pbzcat lbunzip2 lbzcat
- name: "Declare via array index"
stdin: |
declare -A assoc_array["key"]="value"
declare -p assoc_array
declare -a indexed_array[5]="value"
declare -p indexed_array
declare implicit_array[5]="value"
declare -p implicit_array
declare -A no_value_assoc_array["key"]
declare -p no_value_assoc_array
declare no_value_indexed_array[33]
declare -p no_value_indexed_array
- name: "Create array via index update"
stdin: |
i=0
my_array[i++]="c"
echo "0: i=$i array=${my_array[@]}"
my_array[i++]="b"
echo "1: i=$i array=${my_array[@]}"
my_array[i++]="a"
echo "2: i=$i array=${my_array[@]}"
- name: "Expansion of array body"
stdin: |
elements="a b c"
array1=(${elements})
declare -p array1
array2=("${elements}")
declare -p array2
- name: "Copy array"
stdin: |
src=(a b c)
dest1=("${src[*]}")
declare -p dest1
dest2=(${src[*]})
declare -p dest2
dest3=("${src[@]}")
declare -p dest3
dest4=(${src[@]})
declare -p dest4
- name: "Multi-line definition of array"
stdin: |
array=(
a
b
c
)
declare -p array
- name: "Dump values from non-existent array"
stdin: |
for key in ${NONEXISTENT[@]}; do
echo "@-Key: ${key}"
done
for key in "${NONEXISTENT[@]}"; do
echo "@-Quoted Key: ${key}"
done
for key in ${NONEXISTENT[*]}; do
echo "*-Key: ${key}"
done
for key in "${NONEXISTENT[*]}"; do
echo "*-Quoted Key: ${key}"
done
- name: "Use non-existent var in indexed array index"
stdin: |
declare -a myarray=(10)
echo "Value: ${myarray[$non_existent_var]}"
- name: "Use non-existent var in associative array index"
ignore_stderr: true
stdin: |
declare -A myarray=([0]="default" ["a"]="other")
echo "Value: ${myarray[$non_existent_var]}"
- name: "Nested array reference"
stdin: |
x=(2 3 4)
echo ${x[${x[0]}]}
- name: "Append array-resulting expansion to array"
stdin: |
declare -a otherarray=("a" "b" "c")
declare -a ourarray=()
ourarray+=("${otherarray[@]/a/x}")
declare -p otherarray
declare -p ourarray
- name: "Array declaration in a function"
stdin: |
outer() {
inner=()
if test true; then
echo true
else
echo false
fi
}
outer
- name: "Array element expansion with non-standard IFS"
stdin: |
IFS='|'
declare -a myarray=(abc 1 f)
echo "\"\${myarray[@]}\": ${myarray[@]}"
echo "\"\${myarray[*]}\": ${myarray[*]}"
echo "\${myarray[@]}:" ${myarray[@]}
echo "\${myarray[*]}:" ${myarray[*]}