Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/ksh93/tests/arrays.sh
1810 views
1
########################################################################
2
# #
3
# This software is part of the ast package #
4
# Copyright (c) 1982-2012 AT&T Intellectual Property #
5
# and is licensed under the #
6
# Eclipse Public License, Version 1.0 #
7
# by AT&T Intellectual Property #
8
# #
9
# A copy of the License is available at #
10
# http://www.eclipse.org/org/documents/epl-v10.html #
11
# (with md5 checksum b35adb5213ca9657e911e9befb180842) #
12
# #
13
# Information and Software Systems Research #
14
# AT&T Research #
15
# Florham Park NJ #
16
# #
17
# David Korn <[email protected]> #
18
# #
19
########################################################################
20
function err_exit
21
{
22
print -u2 -n "\t"
23
print -u2 -r ${Command}[$1]: "${@:2}"
24
let Errors+=1
25
}
26
alias err_exit='err_exit $LINENO'
27
28
Command=${0##*/}
29
integer Errors=0
30
31
tmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
32
trap "cd /; rm -rf $tmp" EXIT
33
34
function fun
35
{
36
integer i
37
unset xxx
38
for i in 0 1
39
do xxx[$i]=$i
40
done
41
}
42
43
set -A x zero one two three four 'five six'
44
if [[ $x != zero ]]
45
then err_exit '$x is not element 0'
46
fi
47
if [[ ${x[0]} != zero ]]
48
then err_exit '${x[0] is not element 0'
49
fi
50
if (( ${#x[0]} != 4 ))
51
then err_exit "length of ${x[0]} is not 4"
52
fi
53
if (( ${#x[@]} != 6 ))
54
then err_exit 'number of elements of x is not 6'
55
fi
56
if [[ ${x[2]} != two ]]
57
then err_exit ' element two is not 2'
58
fi
59
if [[ ${x[@]:2:1} != two ]]
60
then err_exit ' ${x[@]:2:1} is not two'
61
fi
62
set -A y -- ${x[*]}
63
if [[ $y != zero ]]
64
then err_exit '$x is not element 0'
65
fi
66
if [[ ${y[0]} != zero ]]
67
then err_exit '${y[0] is not element 0'
68
fi
69
if (( ${#y[@]} != 7 ))
70
then err_exit 'number of elements of y is not 7'
71
fi
72
if [[ ${y[2]} != two ]]
73
then err_exit ' element two is not 2'
74
fi
75
set +A y nine ten
76
if [[ ${y[2]} != two ]]
77
then err_exit ' element two is not 2'
78
fi
79
if [[ ${y[0]} != nine ]]
80
then err_exit '${y[0] is not nine'
81
fi
82
unset y[4]
83
if (( ${#y[@]} != 6 ))
84
then err_exit 'number of elements of y is not 6'
85
fi
86
if (( ${#y[4]} != 0 ))
87
then err_exit 'string length of unset element is not 0'
88
fi
89
unset foo
90
if (( ${#foo[@]} != 0 ))
91
then err_exit 'number of elements of unset variable foo is not 0'
92
fi
93
foo=''
94
if (( ${#foo[0]} != 0 ))
95
then err_exit 'string length of null element is not 0'
96
fi
97
if (( ${#foo[@]} != 1 ))
98
then err_exit 'number of elements of null variable foo is not 1'
99
fi
100
unset foo
101
foo[0]=foo
102
foo[3]=bar
103
unset foo[0]
104
unset foo[3]
105
if (( ${#foo[@]} != 0 ))
106
then err_exit 'number of elements of left in variable foo is not 0'
107
fi
108
unset foo
109
foo[3]=bar
110
foo[0]=foo
111
unset foo[3]
112
unset foo[0]
113
if (( ${#foo[@]} != 0 ))
114
then err_exit 'number of elements of left in variable foo again is not 0'
115
fi
116
fun
117
if (( ${#xxx[@]} != 2 ))
118
then err_exit 'number of elements of left in variable xxx is not 2'
119
fi
120
fun
121
if (( ${#xxx[@]} != 2 ))
122
then err_exit 'number of elements of left in variable xxx again is not 2'
123
fi
124
set -A foo -- "${x[@]}"
125
if (( ${#foo[@]} != 6 ))
126
then err_exit 'number of elements of foo is not 6'
127
fi
128
if (( ${#PWD[@]} != 1 ))
129
then err_exit 'number of elements of PWD is not 1'
130
fi
131
unset x
132
x[2]=foo x[4]=bar
133
if (( ${#x[@]} != 2 ))
134
then err_exit 'number of elements of x is not 2'
135
fi
136
s[1]=1 c[1]=foo
137
if [[ ${c[s[1]]} != foo ]]
138
then err_exit 'c[1]=foo s[1]=1; ${c[s[1]]} != foo'
139
fi
140
unset s
141
typeset -Ai s
142
y=* z=[
143
s[$y]=1
144
s[$z]=2
145
if (( ${#s[@]} != 2 ))
146
then err_exit 'number of elements of is not 2'
147
fi
148
(( s[$z] = s[$z] + ${s[$y]} ))
149
if [[ ${s[$z]} != 3 ]]
150
then err_exit '[[ ${s[$z]} != 3 ]]'
151
fi
152
if (( s[$z] != 3 ))
153
then err_exit '(( s[$z] != 3 ))'
154
fi
155
(( s[$y] = s[$y] + ${s[$z]} ))
156
if [[ ${s[$y]} != 4 ]]
157
then err_exit '[[ ${s[$y]} != 4 ]]'
158
fi
159
if (( s[$y] != 4 ))
160
then err_exit '(( s[$y] != 4 ))'
161
fi
162
set -A y 2 4 6
163
typeset -i y
164
z=${y[@]}
165
typeset -R12 y
166
typeset -i y
167
if [[ ${y[@]} != "$z" ]]
168
then err_exit 'error in array conversion from int to R12'
169
fi
170
if (( ${#y[@]} != 3 ))
171
then err_exit 'error in count of array conversion from int to R12'
172
fi
173
unset abcdefg
174
: ${abcdefg[1]}
175
set | grep '^abcdefg$' >/dev/null && err_exit 'empty array variable in set list'
176
unset x y
177
x=1
178
typeset -i y[$x]=4
179
if [[ ${y[1]} != 4 ]]
180
then err_exit 'arithmetic expressions in typeset not working'
181
fi
182
unset foo
183
typeset foo=bar
184
typeset -A foo
185
if [[ ${foo[0]} != bar ]]
186
then err_exit 'initial value not preserved when typecast to associative'
187
fi
188
unset foo
189
foo=(one two)
190
typeset -A foo
191
foo[two]=3
192
if [[ ${#foo[*]} != 3 ]]
193
then err_exit 'conversion of indexed to associative array failed'
194
fi
195
set a b c d e f g h i j k l m
196
if [[ ${#} != 13 ]]
197
then err_exit '${#} not 13'
198
fi
199
unset xxx
200
xxx=foo
201
if [[ ${!xxx[@]} != 0 ]]
202
then err_exit '${!xxx[@]} for scalar not 0'
203
fi
204
if [[ ${11} != k ]]
205
then err_exit '${11} not working'
206
fi
207
if [[ ${@:4:1} != d ]]
208
then err_exit '${@:4:1} not working'
209
fi
210
foovar1=abc
211
foovar2=def
212
if [[ ${!foovar@} != +(foovar[[:alnum:]]?([ ])) ]]
213
then err_exit '${!foovar@} does not expand correctly'
214
fi
215
if [[ ${!foovar1} != foovar1 ]]
216
then err_exit '${!foovar1} != foovar1'
217
fi
218
unset xxx
219
: ${xxx[3]}
220
if [[ ${!xxx[@]} ]]
221
then err_exit '${!xxx[@]} should be null'
222
fi
223
integer i=0
224
{
225
set -x
226
xxx[++i]=1
227
set +x
228
} 2> /dev/null
229
if (( i != 1))
230
then err_exit 'execution trace side effects with array subscripts'
231
fi
232
unset list
233
: $(set -A list foo bar)
234
if (( ${#list[@]} != 0))
235
then err_exit '$(set -A list ...) leaves side effects'
236
fi
237
unset list
238
list= (foo bar bam)
239
( set -A list one two three four)
240
if [[ ${list[1]} != bar ]]
241
then err_exit 'array not restored after subshell'
242
fi
243
XPATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:.:/sbin:/usr/sbin
244
xpath=( $( IFS=: ; echo $XPATH ) )
245
if [[ $(print -r "${xpath[@]##*/}") != 'bin bin ucb bin . sbin sbin' ]]
246
then err_exit '${xpath[@]##*/} not applied to each element'
247
fi
248
foo=( zero one '' three four '' six)
249
integer n=-1
250
if [[ ${foo[@]:n} != six ]]
251
then err_exit 'array offset of -1 not working'
252
fi
253
if [[ ${foo[@]: -3:1} != four ]]
254
then err_exit 'array offset of -3:1 not working'
255
fi
256
$SHELL -c 'x=(if then else fi)' 2> /dev/null || err_exit 'reserved words in x=() assignment not working'
257
unset foo
258
foo=one
259
foo=( $foo two)
260
if [[ ${#foo[@]} != 2 ]]
261
then err_exit 'array getting unset before right hand side evaluation'
262
fi
263
foo=(143 3643 38732)
264
export foo
265
typeset -i foo
266
if [[ $($SHELL -c 'print $foo') != 143 ]]
267
then err_exit 'exporting indexed array not exporting 0-th element'
268
fi
269
( $SHELL -c '
270
unset foo
271
typeset -A foo=([0]=143 [1]=3643 [2]=38732)
272
export foo
273
typeset -i foo
274
[[ $($SHELL -c "print $foo") == 143 ]]'
275
) 2> /dev/null ||
276
err_exit 'exporting associative array not exporting 0-th element'
277
unset foo
278
typeset -A foo
279
foo[$((10))]=ok 2> /dev/null || err_exit 'arithmetic expression as subscript not working'
280
unset foo
281
typeset -A foo
282
integer foo=0
283
[[ $foo == 0 ]] || err_exit 'zero element of associative array not being set'
284
unset foo
285
typeset -A foo=( [two]=1)
286
for i in one three four five
287
do : ${foo[$i]}
288
done
289
if [[ ${!foo[@]} != two ]]
290
then err_exit 'error in subscript names'
291
fi
292
unset x
293
x=( 1 2 3)
294
(x[1]=8)
295
[[ ${x[1]} == 2 ]] || err_exit 'index array produce side effects in subshells'
296
x=( 1 2 3)
297
(
298
x+=(8)
299
[[ ${#x[@]} == 4 ]] || err_exit 'index array append in subshell error'
300
)
301
[[ ${#x[@]} == 3 ]] || err_exit 'index array append in subshell effects parent'
302
x=( [one]=1 [two]=2 [three]=3)
303
(x[two]=8)
304
[[ ${x[two]} == 2 ]] || err_exit 'associative array produce side effects in subshells'
305
unset x
306
x=( [one]=1 [two]=2 [three]=3)
307
(
308
x+=( [four]=4 )
309
[[ ${#x[@]} == 4 ]] || err_exit 'associative array append in subshell error'
310
)
311
[[ ${#x[@]} == 3 ]] || err_exit 'associative array append in subshell effects parent'
312
unset x
313
integer i
314
for ((i=0; i < 40; i++))
315
do x[i]=$i
316
done
317
[[ ${#x[@]} == 40 ]] || err_exit 'index arrays loosing values'
318
[[ $( ($SHELL -c 'typeset -A var; (IFS=: ; set -A var a:b:c ;print ${var[@]});:' )2>/dev/null) == 'a b c' ]] || err_exit 'change associative to index failed'
319
unset foo
320
[[ $(foo=good
321
for ((i=0; i < 2; i++))
322
do [[ ${foo[i]} ]] && print ok
323
done) == ok ]] || err_exit 'invalid optimization for subscripted variables'
324
(
325
x=([foo]=bar)
326
set +A x bam
327
) 2> /dev/null && err_exit 'set +A with associative array should be an error'
328
unset bam foo
329
foo=0
330
typeset -A bam
331
unset bam[foo]
332
bam[foo]=value
333
[[ $bam == value ]] && err_exit 'unset associative array element error'
334
: only first element of an array can be exported
335
unset bam
336
print 'print ${var[0]} ${var[1]}' > $tmp/script
337
chmod +x $tmp/script
338
[[ $($SHELL -c "var=(foo bar);export var;$tmp/script") == foo ]] || err_exit 'export array not exporting just first element'
339
340
unset foo
341
set --allexport
342
foo=one
343
foo[1]=two
344
foo[0]=three
345
[[ $foo == three ]] || err_exit '--allexport not working with arrays'
346
set --noallexport
347
unset foo
348
349
cat > $tmp/script <<- \!
350
typeset -A foo
351
print foo${foo[abc]}
352
!
353
[[ $($SHELL -c "typeset -A foo;$tmp/script") == foo ]] 2> /dev/null || err_exit 'empty associative arrays not being cleared correctly before scripts'
354
[[ $($SHELL -c "typeset -A foo;foo[abc]=abc;$tmp/script") == foo ]] 2> /dev/null || err_exit 'associative arrays not being cleared correctly before scripts'
355
unset foo
356
foo=(one two)
357
[[ ${foo[@]:1} == two ]] || err_exit '${foo[@]:1} == two'
358
[[ ! ${foo[@]:2} ]] || err_exit '${foo[@]:2} not null'
359
unset foo
360
foo=one
361
[[ ! ${foo[@]:1} ]] || err_exit '${foo[@]:1} not null'
362
function EMPTY
363
{
364
typeset i
365
typeset -n ARRAY=$1
366
for i in ${!ARRAY[@]}
367
do unset ARRAY[$i]
368
done
369
}
370
unset foo
371
typeset -A foo
372
foo[bar]=bam
373
foo[x]=y
374
EMPTY foo
375
[[ $(typeset | grep foo$) == *associative* ]] || err_exit 'array lost associative attribute'
376
[[ ! ${foo[@]} ]] || err_exit 'array not empty'
377
[[ ! ${!foo[@]} ]] || err_exit 'array names not empty'
378
unset foo
379
foo=bar
380
set -- "${foo[@]:1}"
381
(( $# == 0 )) || err_exit '${foo[@]:1} should not have any values'
382
unset bar
383
exp=4
384
: ${_foo[bar=4]}
385
(( bar == 4 )) || err_exit "subscript of unset variable not evaluated -- expected '4', got '$got'"
386
unset bar
387
: ${_foo[bar=$exp]}
388
(( bar == $exp )) || err_exit "subscript of unset variable not evaluated -- expected '$exp', got '$got'"
389
unset foo bar
390
foo[5]=4
391
bar[4]=3
392
bar[0]=foo
393
foo[0]=bam
394
foo[4]=5
395
[[ ${!foo[2+2]} == 'foo[4]' ]] || err_exit '${!var[sub]} should be var[sub]'
396
[[ ${bar[${foo[5]}]} == 3 ]] || err_exit 'array subscript cannot be an array instance'
397
[[ $bar[4] == 3 ]] || err_exit '$bar[x] != ${bar[x]} inside [[ ]]'
398
(( $bar[4] == 3 )) || err_exit '$bar[x] != ${bar[x]} inside (( ))'
399
[[ $bar[$foo[5]] == 3 ]] || err_exit '$bar[foo[x]] != ${bar[foo[x]]} inside [[ ]]'
400
(( $bar[$foo[5]] == 3 )) || err_exit '$bar[foo[x]] != ${bar[foo[x]]} inside (( ))'
401
x=$bar[4]
402
[[ $x == 4 ]] && err_exit '$bar[4] should not be an array in an assignment'
403
x=${bar[$foo[5]]}
404
(( $x == 3 )) || err_exit '${bar[$foo[sub]]} not working'
405
[[ $($SHELL <<- \++EOF+++
406
typeset -i test_variable=0
407
typeset -A test_array
408
test_array[1]=100
409
read test_array[2] <<-!
410
2
411
!
412
read test_array[3] <<-!
413
3
414
!
415
test_array[3]=4
416
print "val=${test_array[3]}"
417
++EOF+++
418
) == val=4 ]] 2> /dev/null || err_exit 'after reading array[j] and assign array[j] fails'
419
[[ $($SHELL <<- \+++EOF+++
420
pastebin=( typeset -a form)
421
pastebin.form+=( name="name" data="clueless" )
422
print -r -- ${pastebin.form[0].name}
423
+++EOF+++
424
) == name ]] 2> /dev/null || err_exit 'indexed array in compound variable not working'
425
unset foo bar
426
: ${foo[bar=2]}
427
[[ $bar == 2 ]] || err_exit 'subscript not evaluated for unset variable'
428
unset foo bar
429
bar=1
430
typeset -a foo=([1]=ok [2]=no)
431
[[ $foo[bar] == ok ]] || err_exit 'typeset -a not working for simple assignment'
432
unset foo
433
typeset -a foo=([1]=(x=ok) [2]=(x=no))
434
[[ $(typeset | grep 'foo$') == *index* ]] || err_exit 'typeset -a not creating an indexed array'
435
foo+=([5]=good)
436
[[ $(typeset | grep 'foo$') == *index* ]] || err_exit 'append to indexed array not preserving array type'
437
unset foo
438
typeset -A foo=([1]=ok [2]=no)
439
[[ $foo[bar] == ok ]] && err_exit 'typeset -A not working for simple assignment'
440
unset foo
441
typeset -A foo=([1]=(x=ok) [2]=(x=no))
442
[[ ${foo[bar].x} == ok ]] && err_exit 'typeset -A not working for compound assignment'
443
[[ $($SHELL -c 'typeset -a foo;typeset | grep "foo$"' 2> /dev/null) == *index* ]] || err_exit 'typeset fails for indexed array with no elements'
444
xxxxx=(one)
445
[[ $(typeset | grep xxxxx$) == *'indexed array'* ]] || err_exit 'array of one element not an indexed array'
446
unset foo
447
foo[1]=(x=3 y=4)
448
{ [[ ${!foo[1].*} == 'foo[1].x foo[1].y' ]] ;} 2> /dev/null || err_exit '${!foo[sub].*} not expanding correctly'
449
unset x
450
x=( typeset -a foo=( [0]="a" [1]="b" [2]="c" ))
451
[[ ${@x.foo} == 'typeset -a'* ]] || err_exit 'x.foo is not an indexed array'
452
x=( typeset -A foo=( [0]="a" [1]="b" [2]="c" ))
453
[[ ${@x.foo} == 'typeset -A'* ]] || err_exit 'x.foo is not an associative array'
454
$SHELL -c $'x=(foo\n\tbar\nbam\n)' 2> /dev/null || err_exit 'compound array assignment with new-lines not working'
455
$SHELL -c $'x=(foo\n\tbar:\nbam\n)' 2> /dev/null || err_exit 'compound array assignment with labels not working'
456
$SHELL -c $'x=(foo\n\tdone\nbam\n)' 2> /dev/null || err_exit 'compound array assignment with reserved words not working'
457
[[ $($SHELL -c 'typeset -A A; print $(( A[foo].bar ))' 2> /dev/null) == 0 ]] || err_exit 'unset variable not evaluating to 0'
458
unset a
459
typeset -A a
460
a[a].z=1
461
a[z].z=2
462
unset a[a]
463
[[ ${!a[@]} == z ]] || err_exit '"unset a[a]" unsets entire array'
464
unset a
465
a=([x]=1 [y]=2 [z]=(foo=3 bar=4))
466
eval "b=$(printf "%B\n" a)"
467
eval "c=$(printf "%#B\n" a)"
468
[[ ${a[*]} == "${b[*]}" ]] || err_exit 'printf %B not preserving values for arrays'
469
[[ ${a[*]} == "${c[*]}" ]] || err_exit 'printf %#B not preserving values for arrays'
470
unset a
471
a=(zero one two three four)
472
a[6]=six
473
[[ ${a[-1]} == six ]] || err_exit 'a[-1] should be six'
474
[[ ${a[-3]} == four ]] || err_exit 'a[-3] should be four'
475
[[ ${a[-3..-1]} == 'four six' ]] || err_exit "a[-3,-1] should be 'four six'"
476
477
FILTER=(typeset scope)
478
FILTER[0].scope=include
479
FILTER[1].scope=exclude
480
[[ ${#FILTER[@]} == 2 ]] || err_exit "FILTER array should have two elements not ${#FILTER[@]}"
481
482
unset x
483
function x.get
484
{
485
print sub=${.sh.subscript}
486
}
487
x[2]=
488
z=$(: ${x[1]} )
489
[[ $z == sub=1 ]] || err_exit 'get function not invoked for index array'
490
491
unset x
492
typeset -A x
493
function x.get
494
{
495
print sub=${.sh.subscript}
496
}
497
x[2]=
498
z=$(: ${x[1]} )
499
[[ $z == sub=1 ]] || err_exit 'get function not invoked for associative array'
500
501
unset y
502
i=1
503
a=(11 22)
504
typeset -m y=a[i]
505
[[ $y == 22 ]] || err_exit 'typeset -m for index array not working'
506
[[ ${a[i]} || ${a[0]} != 11 ]] && err_exit 'typeset -m for index array not deleting element'
507
508
unset y
509
a=([0]=11 [1]=22)
510
typeset -m y=a[$i]
511
[[ $y == 22 ]] || err_exit 'typeset -m for associative array not working'
512
[[ ${a[$i]} || ${a[0]} != 11 ]] && err_exit 'typeset -m for associative array not deleting element'
513
unset x a j
514
515
typeset -a a=( [0]="aa" [1]="bb" [2]="cc" )
516
typeset -m 'j=a[0]'
517
typeset -m 'a[0]=a[1]'
518
typeset -m 'a[1]=j'
519
[[ ${a[@]} == 'bb aa cc' ]] || err_exit 'moving index array elements not working'
520
unset a j
521
522
typeset -A a=( [0]="aa" [1]="bb" [2]="cc" )
523
typeset -m 'j=a[0]'
524
typeset -m 'a[0]=a[1]'
525
typeset -m 'a[1]=j'
526
[[ ${a[@]} == 'bb aa cc' ]] || err_exit 'moving associative array elements not working'
527
unset a j
528
529
z=(a b c)
530
unset x
531
typeset -m x[1]=z
532
[[ ${x[1][@]} == 'a b c' ]] || err_exit 'moving indexed array to index array element not working'
533
534
unset x z
535
z=([0]=a [1]=b [2]=c)
536
typeset -m x[1]=z
537
[[ ${x[1][@]} == 'a b c' ]] || err_exit 'moving associative array to index array element not working'
538
539
{
540
typeset -a arr=(
541
float
542
)
543
} 2> /dev/null
544
[[ ${arr[0]} == float ]] || err_exit 'typeset -a should not expand alias for float'
545
unset arr
546
547
{
548
typeset -r -a arr=(
549
float
550
)
551
} 2> /dev/null
552
[[ ${arr[0]} == float ]] || err_exit 'typeset -r -a should not expand alias for float'
553
{
554
typeset -a arr2=(
555
typeset +r
556
)
557
} 2> /dev/null
558
[[ ${arr2[0]} == typeset ]] || err_exit 'typeset -a should not process declarations'
559
unset arr2
560
561
$SHELL 2> /dev/null -c $'typeset -a arr=(\nfor)' || err_exit 'typeset -a should allow reserved words as first argument'
562
563
$SHELL 2> /dev/null -c $'typeset -r -a arr=(\nfor)' || err_exit 'typeset -r -a should allow reserved words as first argument'
564
565
typeset arr2[6]
566
[[ ${#arr2[@]} == 0 ]] || err_exit 'declartion "typeset array[6]" should not show any elements'
567
568
arr2[1]=def
569
[[ ${arr2[1]} == def ]] || err_exit 'declaration "typeset array[6]" causes arrays causes wrong side effects'
570
571
unset foo
572
typeset foo[7]
573
[[ ${#foo[@]} == 0 ]] || err_exit 'typeset foo[7] should not have one element'
574
575
a=123 $SHELL 2> /dev/null -c 'integer a[5]=3 a[2]=4; unset a;x=0; ((a[++x]++));:' || err_exit 'unsetting array variable leaves side effect'
576
577
unset foo
578
foo=(aa bb cc)
579
foo=( ${foo[@]:1} )
580
[[ ${foo[@]} == 'bb cc' ]] || err_exit "indexed array assignment using parts of array for values gives wrong result of ${foo[@]}"
581
582
unset foo
583
foo=([xx]=aa [yy]=bb [zz]=cc)
584
foo=( ${foo[yy]} ${foo[zz]} )
585
[[ ${foo[@]} == 'bb cc' ]] || err_exit "associative array assignment using parts of array for values gives wrong result of ${foo[@]}"
586
587
unset foo
588
typeset -a foo=(abc=1 def=2)
589
[[ ${foo[1]} == def=2 ]] || err_exit "index array with elements containing = not working"
590
591
unset foo
592
typeset -a foo=( a b )
593
typeset -p foo[10]
594
[[ ${!foo[@]} == '0 1' ]] || err_exit 'typeset -p foo[10] has side effect'
595
596
unset foo
597
exp='typeset -a foo=((11 22) (66) )'
598
x=$(
599
typeset -a foo=( ( 11 22 ) ( 44 55 ) )
600
foo[1]=(66)
601
typeset -p foo
602
) 2> /dev/null
603
[[ $x == "$exp" ]] || err_exit 'setting element 1 to index fooay failed'
604
unset foo
605
exp='typeset -a foo=((11 22) (x=3))'
606
x=$(
607
typeset -a foo=( ( 11 22 ) ( 44 55 ) )
608
foo[1]=(x=3)
609
typeset -p foo
610
) 2> /dev/null
611
[[ $x == "$exp" ]] || err_exit 'setting element 1 of array to compound variable failed'
612
613
#test for cloning a very large index array - can core dump
614
(
615
trap 'x=$?;exit $(( $x!=0 ))' EXIT
616
$SHELL <<- \EOF
617
(
618
print '('
619
integer i
620
for ((i=0 ; i < 16384 ; i++ )) ; do
621
printf '\tinteger var%i=%i\n' i i
622
done
623
printf 'typeset -a ar=(\n'
624
for ((i=0 ; i < 16384 ; i++ )) ; do
625
printf '\t[%d]=%d\n' i i
626
done
627
print ')'
628
print ')'
629
) | read -C hugecpv
630
compound hugecpv2=hugecpv
631
v=$(typeset -p hugecpv)
632
[[ ${v/hugecpv/hugecpv2} == "$(typeset -p hugecpv2)" ]]
633
EOF
634
) 2> /dev/null || err_exit 'copying a large array fails'
635
636
unset foo
637
typeset -a foo
638
foo+=(bar)
639
[[ ${foo[0]} == bar ]] || 'appending to empty array not working'
640
641
unset isnull
642
typeset -A isnull
643
isnull[mdapp]=Y
644
: ${isnull[@]}
645
isnull[mdapp]=N
646
[[ ${isnull[*]} != *N* ]] && err_exit 'bug after ${arr[@]} with one element associative array'
647
648
unset arr2
649
arr2=()
650
typeset -A arr2
651
unset arr2
652
[[ $(typeset -p arr2) ]] && err_exit 'unset associative array of compound variables not working'
653
654
arr3=(x=3)
655
typeset -A arr3
656
[[ $(typeset -p arr3) == 'typeset -A arr3=()' ]] || err_exit 'typeset -A does not first unset compound variable.'
657
658
arr4=(x=3)
659
typeset -a arr4
660
[[ $(typeset -p arr4) == 'typeset -a arr4' ]] || err_exit 'typeset -a does not first unset compound variable.'
661
662
alias foo=bar
663
arr5=(foo bar)
664
[[ $(typeset -p arr5) == 'typeset -a arr5=(foo bar)' ]] || err_exit 'typeset expanding non-declaration aliases'
665
666
typeset -A Foo
667
Foo=( [a]=AA;[b]=BB)
668
[[ ${Foo[a]} == AA ]] || err_exit 'Fooa[a] is {Foo[a]} not AA'
669
670
exit $((Errors<125?Errors:125))
671
672