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 <dgk@research.att.com> # 18# # 19######################################################################## 20function err_exit 21{ 22 print -u2 -n "\t" 23 print -u2 -r ${Command}[$1]: "${@:2}" 24 let Errors+=1 25} 26alias err_exit='err_exit $LINENO' 27 28Command=${0##*/} 29integer Errors=0 30for ((i=0; i < 4; i++ )) 31do for ((j=0; j < 5; j++ )) 32 do a[i][j]=$i$j 33 done 34done 35for ((i=0; i < 4; i++ )) 36do for ((j=0; j < 5; j++ )) 37 do [[ ${a[i][j]} == "$i$j" ]] || err_exit "\${a[$i][$j]} != $i$j" 38 done 39done 40for ((i=0; i < 4; i++ )) 41do j=0;for k in ${a[i][@]} 42 do [[ $k == "$i$j" ]] || err_exit "\${a[i][@]} != $i$j" 43 (( j++ )) 44 done 45done 46unset a 47a=( 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 54function 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 82check a 83 84unset a 85typeset -A a 86for ((i=0; i < 4; i++ )) 87do for ((j=0; j < 5; j++ )) 88 do a[$i][j]=$i$j 89 done 90done 91for ((i=0; i < 4; i++ )) 92do for ((j=0; j < 5; j++ )) 93 do [[ ${a[$i][j]} == "$i$j" ]] || err_exit "\${a[$i][$j]} == $i$j" 94 done 95done 96a[1][5]=15 97b=( 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) 103check b 104[[ ${a[1][@]} == "${b[1][@]}" ]] || err_exit "a[1] not equal to b[1]" 105c=( 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) 111check c 112typeset -A d 113d[0]=( [0]=00 [1]=01 [2]=02 [3]=03 [4]=04 ) 114d[1]=( [0]=10 [1]=11 [2]=12 [3]=13 [4]=14 [5]=15) 115d[2]=( [0]=20 [1]=21 [2]=22 [3]=23 [4]=24 ) 116d[3]=( [0]=30 [1]=31 [2]=32 [3]=33 [4]=34 ) 117check d 118unset 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' 125typeset 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 136typeset -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' 138unset x 139typeset -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 142unset ar x y 143integer -a ar 144integer i x y 145for (( i=0 ; i < 100 ; i++ )) 146do (( ar[y][x++]=i )) 147 (( x > 9 )) && (( y++ , x=0 )) 148done 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 153unset ar 154integer -a ar 155x=0 y=0 156for (( i=0 ; i < 81 ; i++ )) 157do nameref ar_y=ar[$y] 158 (( ar_y[x++]=i )) 159 (( x > 8 )) && (( y++ , x=0 )) 160 typeset +n ar_y 161done 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 168expected='typeset -a -l -E c.ar=([2]=([3]=([3]=5) ) )' 169unset c 170float c.ar 171c.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 174unset values 175float -a values=( [1][3]=90 [1][4]=89 ) 176function 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} 181fx values[1] 182 183function 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 210test_short_integer 211 212typeset -a arr=( ( 00 ) ( 01 ) ( 02 ) ( 03 ) ( 04 ) ( 05 ) ( 06 ) ( 07 ) ( 08 ) ( 09 ) ( 10 ) ) 213typeset -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 217unset cx l 218compound cx 219typeset -a cx.ar[4][4] 220print -v cx > /dev/null 221print -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 224exit $((Errors<125?Errors:125)) 225