Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/ksh93/tests/arrays2.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
for ((i=0; i < 4; i++ ))
31
do for ((j=0; j < 5; j++ ))
32
do a[i][j]=$i$j
33
done
34
done
35
for ((i=0; i < 4; i++ ))
36
do for ((j=0; j < 5; j++ ))
37
do [[ ${a[i][j]} == "$i$j" ]] || err_exit "\${a[$i][$j]} != $i$j"
38
done
39
done
40
for ((i=0; i < 4; i++ ))
41
do j=0;for k in ${a[i][@]}
42
do [[ $k == "$i$j" ]] || err_exit "\${a[i][@]} != $i$j"
43
(( j++ ))
44
done
45
done
46
unset a
47
a=(
48
( 00 01 02 03 04 )
49
( 10 11 12 13 14 15)
50
( 20 21 22 23 24 )
51
( 30 31 32 33 34 )
52
)
53
54
function check
55
{
56
nameref a=$1
57
nameref b=a[2]
58
typeset c=$1
59
integer i j
60
for ((i=0; i < 4; i++ ))
61
do for ((j=0; j < 5; j++ ))
62
do [[ ${a[$i][$j]} == "$i$j" ]] || err_exit "\${$c[$i][$j]} != $i$j"
63
done
64
done
65
(( ${#a[@]} == 4 )) || err_exit "\${#$c[@]} not 4"
66
(( ${#a[0][@]} == 5 )) || err_exit "\${#$c[0][@]} not 5"
67
(( ${#a[1][@]} == 6 )) || err_exit "\${#$c[1][@]} not 6"
68
set -s -- ${!a[@]}
69
[[ ${@} == '0 1 2 3' ]] || err_exit "\${!$c[@]} not 0 1 2 3"
70
set -s -- ${!a[0][@]}
71
[[ ${@} == '0 1 2 3 4' ]] || err_exit "\${!$c[0][@]} not 0 1 2 3 4"
72
set -s -- ${!a[1][@]}
73
[[ ${@} == '0 1 2 3 4 5' ]] || err_exit "\${!$c[1][@]} not 0 1 2 3 4 5"
74
[[ $a == 00 ]] || err_exit "\$$c is not 00"
75
[[ ${a[0]} == 00 ]] || err_exit "\${$a[0]} is not 00"
76
[[ ${a[0][0]} == 00 ]] || err_exit "${a[0][0]} is not 00"
77
[[ ${a[0][0][0]} == 00 ]] || err_exit "\${$c[0][0][0]} is not 00"
78
[[ ${a[0][0][1]} == '' ]] || err_exit "\${$c[0][0][1]} is not empty"
79
[[ ${b[3]} == 23 ]] || err_exit "${!b}[3] not = 23"
80
}
81
82
check a
83
84
unset a
85
typeset -A a
86
for ((i=0; i < 4; i++ ))
87
do for ((j=0; j < 5; j++ ))
88
do a[$i][j]=$i$j
89
done
90
done
91
for ((i=0; i < 4; i++ ))
92
do for ((j=0; j < 5; j++ ))
93
do [[ ${a[$i][j]} == "$i$j" ]] || err_exit "\${a[$i][$j]} == $i$j"
94
done
95
done
96
a[1][5]=15
97
b=(
98
[0]=( 00 01 02 03 04 )
99
[1]=( 10 11 12 13 14 15)
100
[2]=( 20 21 22 23 24 )
101
[3]=( 30 31 32 33 34 )
102
)
103
check b
104
[[ ${a[1][@]} == "${b[1][@]}" ]] || err_exit "a[1] not equal to b[1]"
105
c=(
106
[0]=( [0]=00 [1]=01 [2]=02 [3]=03 [4]=04 )
107
[1]=( [0]=10 [1]=11 [2]=12 [3]=13 [4]=14 [5]=15)
108
[2]=( [0]=20 [1]=21 [2]=22 [3]=23 [4]=24 )
109
[3]=( [0]=30 [1]=31 [2]=32 [3]=33 [4]=34 )
110
)
111
check c
112
typeset -A d
113
d[0]=( [0]=00 [1]=01 [2]=02 [3]=03 [4]=04 )
114
d[1]=( [0]=10 [1]=11 [2]=12 [3]=13 [4]=14 [5]=15)
115
d[2]=( [0]=20 [1]=21 [2]=22 [3]=23 [4]=24 )
116
d[3]=( [0]=30 [1]=31 [2]=32 [3]=33 [4]=34 )
117
check d
118
unset a b c d
119
[[ ${a-set} ]] || err_exit "a is set after unset"
120
[[ ${b-set} ]] || err_exit "b is set after unset"
121
[[ ${c-set} ]] || err_exit "c is set after unset"
122
[[ ${d-set} ]] || err_exit "c is set after unset"
123
124
$SHELL 2> /dev/null <<\+++ || err_exit 'input of 3 dimensional array not working'
125
typeset x=(
126
( (g G) (h H) (i I) )
127
( (d D) (e E) (f F) )
128
( (a A) (b B) (c C) )
129
)
130
[[ ${x[0][0][0]} == g ]] || err_exit '${x[0][0][0]} == G'
131
[[ ${x[1][1][0]} == e ]] || err_exit '${x[1][1][0]} == e'
132
[[ ${x[1][1][1]} == E ]] || err_exit '${x[2][2][1]} == C'
133
[[ ${x[0][2][1]} == I ]] || err_exit '${x[0][2][1]} == I'
134
+++
135
136
typeset -a -si x=( [0]=(1 2 3) [1]=(4 5 6) [2]=(7 8 9) )
137
[[ ${x[1][1]} == 5 ]] || err_exit 'changing two dimensional indexed array to short integer failed'
138
unset x
139
typeset -A -si x=( [0]=(1 2 3) [1]=(4 5 6) [2]=(7 8 9) )
140
[[ ${x[1][2]} == 6 ]] || err_exit 'changing two dimensional associative array to short integer failed'
141
142
unset ar x y
143
integer -a ar
144
integer i x y
145
for (( i=0 ; i < 100 ; i++ ))
146
do (( ar[y][x++]=i ))
147
(( x > 9 )) && (( y++ , x=0 ))
148
done
149
[[ ${#ar[0][*]} == 10 ]] || err_exit "\${#ar[0][*]} is '${#ar[0][*]}', should be 10"
150
[[ ${#ar[*]} == 10 ]] || err_exit "\${#ar[*]} is '${#ar[*]}', should be 10"
151
[[ ${ar[5][5]} == 55 ]] || err_exit "ar[5][5] is '${ar[5][5]}', should be 55"
152
153
unset ar
154
integer -a ar
155
x=0 y=0
156
for (( i=0 ; i < 81 ; i++ ))
157
do nameref ar_y=ar[$y]
158
(( ar_y[x++]=i ))
159
(( x > 8 )) && (( y++ , x=0 ))
160
typeset +n ar_y
161
done
162
[[ ${#ar[0][*]} == 9 ]] || err_exit "\${#ar[0][*]} is '${#ar[0][*]}', should be 9"
163
[[ ${#ar[*]} == 9 ]] || err_exit "\${#ar[*]} is '${#ar[*]}', should be 9"
164
[[ ${ar[4][4]} == 40 ]] || err_exit "ar[4][4] is '${ar[4][4]}', should be 40"
165
166
$SHELL 2> /dev/null -c 'compound c;float -a c.ar;(( c.ar[2][3][3] = 5))' || 'multi-dimensional arrays in arithemtic expressions not working'
167
168
expected='typeset -a -l -E c.ar=([2]=([3]=([3]=5) ) )'
169
unset c
170
float c.ar
171
c.ar[2][3][3]=5
172
[[ $(typeset -p c.ar) == "$expected" ]] || err_exit "c.ar[2][3][3]=5;typeset -c c.ar expands to $(typeset -p c.ar)"
173
174
unset values
175
float -a values=( [1][3]=90 [1][4]=89 )
176
function fx
177
{
178
nameref arg=$1
179
[[ ${arg[0..5]} == '90 89' ]] || err_exit '${arg[0..5]} not correct where arg is a nameref to values[1]'
180
}
181
fx values[1]
182
183
function test_short_integer
184
{
185
compound out=( typeset stdout stderr ; integer res )
186
compound -r -a tests=(
187
( cmd='integer -s -r -a x=( 1 2 3 ) ; print "${x[2]}"' stdoutpattern='3' )
188
( cmd='integer -s -r -A x=( [0]=1 [1]=2 [2]=3 ) ; print "${x[2]}"' stdoutpattern='3' )
189
# 2D integer arrays: the following two tests crash for both "integer -s" and "integer"
190
( cmd='integer -r -a x=( [0]=( [0]=1 [1]=2 [2]=3 ) [1]=( [0]=4 [1]=5 [2]=6 ) [2]=( [0]=7 [1]=8 [2]=9 ) ) ; print "${x[1][1]}"' stdoutpattern='5' )
191
( cmd='integer -s -r -a x=( [0]=( [0]=1 [1]=2 [2]=3 ) [1]=( [0]=4 [1]=5 [2]=6 ) [2]=( [0]=7 [1]=8 [2]=9 ) ) ; print "${x[1][1]}"' stdoutpattern='5' )
192
)
193
typeset testname
194
integer i
195
196
for (( i=0 ; i < ${#tests[@]} ; i++ )) ; do
197
nameref tst=tests[i]
198
testname="${0}/${i}"
199
200
out.stderr="${ { out.stdout="${ ${SHELL} -o nounset -o errexit -c "${tst.cmd}" ; (( out.res=$? )) ; }" ; } 2>&1 ; }"
201
202
[[ "${out.stdout}" == ${tst.stdoutpattern} ]] || err_exit "${testname}: Expected stdout to match $(printf '%q\n' "${tst.stdoutpattern}"), got $(printf '%q\n' "${out.stdout}")"
203
[[ "${out.stderr}" == '' ]] || err_exit "${testname}: Expected empty stderr, got $(printf '%q\n' "${out.stderr}")"
204
(( out.res == 0 )) || err_exit "${testname}: Unexpected exit code ${out.res}"
205
done
206
207
return 0
208
}
209
# run tests
210
test_short_integer
211
212
typeset -a arr=( ( 00 ) ( 01 ) ( 02 ) ( 03 ) ( 04 ) ( 05 ) ( 06 ) ( 07 ) ( 08 ) ( 09 ) ( 10 ) )
213
typeset -i i=10 j=0
214
{ y=$( echo ${arr[i][j]} ) ;} 2> /dev/null
215
[[ $y == 10 ]] || err_exit '${arr[10][0] should be 10 '
216
217
unset cx l
218
compound cx
219
typeset -a cx.ar[4][4]
220
print -v cx > /dev/null
221
print -v cx | read -C l 2> /dev/null || err_exit 'read -C fails from output of print -v'
222
[[ ${cx%cx=} == "${l%l=}" ]] || err_exit 'print -v for compound variable with fixed 2d array not working'
223
224
exit $((Errors<125?Errors:125))
225
226