########################################################################
# #
# This software is part of the ast package #
# Copyright (c) 1982-2012 AT&T Intellectual Property #
# and is licensed under the #
# Eclipse Public License, Version 1.0 #
# by AT&T Intellectual Property #
# #
# A copy of the License is available at #
# (with md5 checksum b35adb5213ca9657e911e9befb180842) #
# #
# Information and Software Systems Research #
# AT&T Research #
# Florham Park NJ #
# #
# David Korn <dgk@research.att.com> #
# #
########################################################################
function err_exit
{
print -u2 -n "\t"
let Errors+=1
}
alias err_exit='err_exit $LINENO'
integer Errors=0
for ((i=0; i < 4; i++ ))
do for ((j=0; j < 5; j++ ))
do a[i][j]=$i$j
done
done
for ((i=0; i < 4; i++ ))
do for ((j=0; j < 5; j++ ))
done
done
for ((i=0; i < 4; i++ ))
do j=0;for k in ${a[i][@]}
(( j++ ))
done
done
unset a
a=(
( 00 01 02 03 04 )
( 10 11 12 13 14 15)
( 20 21 22 23 24 )
( 30 31 32 33 34 )
)
function check
{
nameref a=$1
nameref b=a[2]
typeset c=$1
integer i j
for ((i=0; i < 4; i++ ))
do for ((j=0; j < 5; j++ ))
done
done
(( ${#a[@]} == 4 )) || err_exit "\${#$c[@]} not 4"
(( ${#a[0][@]} == 5 )) || err_exit "\${#$c[0][@]} not 5"
(( ${#a[1][@]} == 6 )) || err_exit "\${#$c[1][@]} not 6"
set -s -- ${!a[@]}
set -s -- ${!a[0][@]}
set -s -- ${!a[1][@]}
[[ $a == 00 ]] || err_exit "\$$c is not 00"
[[ ${a[0]} == 00 ]] || err_exit "\${$a[0]} is not 00"
[[ ${a[0][0]} == 00 ]] || err_exit "${a[0][0]} is not 00"
[[ ${a[0][0][0]} == 00 ]] || err_exit "\${$c[0][0][0]} is not 00"
[[ ${b[3]} == 23 ]] || err_exit "${!b}[3] not = 23"
}
check a
unset a
typeset -A a
for ((i=0; i < 4; i++ ))
do for ((j=0; j < 5; j++ ))
do a[$i][j]=$i$j
done
done
for ((i=0; i < 4; i++ ))
do for ((j=0; j < 5; j++ ))
done
done
a[1][5]=15
b=(
[0]=( 00 01 02 03 04 )
[1]=( 10 11 12 13 14 15)
[2]=( 20 21 22 23 24 )
[3]=( 30 31 32 33 34 )
)
check b
c=(
[0]=( [0]=00 [1]=01 [2]=02 [3]=03 [4]=04 )
[1]=( [0]=10 [1]=11 [2]=12 [3]=13 [4]=14 [5]=15)
[2]=( [0]=20 [1]=21 [2]=22 [3]=23 [4]=24 )
[3]=( [0]=30 [1]=31 [2]=32 [3]=33 [4]=34 )
)
check c
typeset -A d
d[0]=( [0]=00 [1]=01 [2]=02 [3]=03 [4]=04 )
d[1]=( [0]=10 [1]=11 [2]=12 [3]=13 [4]=14 [5]=15)
d[2]=( [0]=20 [1]=21 [2]=22 [3]=23 [4]=24 )
d[3]=( [0]=30 [1]=31 [2]=32 [3]=33 [4]=34 )
check d
unset a b c d
[[ ${a-set} ]] || err_exit "a is set after unset"
[[ ${b-set} ]] || err_exit "b is set after unset"
[[ ${c-set} ]] || err_exit "c is set after unset"
[[ ${d-set} ]] || err_exit "c is set after unset"
typeset x=(
( (g G) (h H) (i I) )
( (d D) (e E) (f F) )
( (a A) (b B) (c C) )
)
[[ ${x[0][0][0]} == g ]] || err_exit '${x[0][0][0]} == G'
[[ ${x[1][1][0]} == e ]] || err_exit '${x[1][1][0]} == e'
[[ ${x[1][1][1]} == E ]] || err_exit '${x[2][2][1]} == C'
[[ ${x[0][2][1]} == I ]] || err_exit '${x[0][2][1]} == I'
+++
[[ ${x[1][1]} == 5 ]] || err_exit 'changing two dimensional indexed array to short integer failed'
unset x
[[ ${x[1][2]} == 6 ]] || err_exit 'changing two dimensional associative array to short integer failed'
unset ar x y
integer -a ar
integer i x y
for (( i=0 ; i < 100 ; i++ ))
(( x > 9 )) && (( y++ , x=0 ))
done
[[ ${#ar[0][*]} == 10 ]] || err_exit "\${#ar[0][*]} is '${#ar[0][*]}', should be 10"
[[ ${#ar[*]} == 10 ]] || err_exit "\${#ar[*]} is '${#ar[*]}', should be 10"
unset ar
integer -a ar
x=0 y=0
for (( i=0 ; i < 81 ; i++ ))
(( x > 8 )) && (( y++ , x=0 ))
typeset +n ar_y
done
[[ ${#ar[0][*]} == 9 ]] || err_exit "\${#ar[0][*]} is '${#ar[0][*]}', should be 9"
[[ ${#ar[*]} == 9 ]] || err_exit "\${#ar[*]} is '${#ar[*]}', should be 9"
$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'
unset c
float c.ar
c.ar[2][3][3]=5
[[ $(typeset -p c.ar) == "$expected" ]] || err_exit "c.ar[2][3][3]=5;typeset -c c.ar expands to $(typeset -p c.ar)"
unset values
float -a values=( [1][3]=90 [1][4]=89 )
function fx
{
nameref arg=$1
[[ ${arg[0..5]} == '90 89' ]] || err_exit '${arg[0..5]} not correct where arg is a nameref to values[1]'
}
function test_short_integer
{
compound -r -a tests=(
# 2D integer arrays: the following two tests crash for both "integer -s" and "integer"
( 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' )
( 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' )
)
typeset testname
integer i
for (( i=0 ; i < ${#tests[@]} ; i++ )) ; do
testname="${0}/${i}"
out.stderr="${ { out.stdout="${ ${SHELL} -o nounset -o errexit -c "${tst.cmd}" ; (( out.res=$? )) ; }" ; } 2>&1 ; }"
[[ "${out.stdout}" == ${tst.stdoutpattern} ]] || err_exit "${testname}: Expected stdout to match $(printf '%q\n' "${tst.stdoutpattern}"), got $(printf '%q\n' "${out.stdout}")"
[[ "${out.stderr}" == '' ]] || err_exit "${testname}: Expected empty stderr, got $(printf '%q\n' "${out.stderr}")"
done
return 0
}
# run tests
typeset -i i=10 j=0
[[ $y == 10 ]] || err_exit '${arr[10][0] should be 10 '
unset cx l
compound cx
typeset -a cx.ar[4][4]
[[ ${cx%cx=} == "${l%l=}" ]] || err_exit 'print -v for compound variable with fixed 2d array not working'