########################################################################
# #
# 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
function fun
{
integer i
unset xxx
for i in 0 1
do xxx[$i]=$i
done
}
if [[ $x != zero ]]
then err_exit '$x is not element 0'
fi
if [[ ${x[0]} != zero ]]
then err_exit '${x[0] is not element 0'
fi
if (( ${#x[0]} != 4 ))
then err_exit "length of ${x[0]} is not 4"
fi
if (( ${#x[@]} != 6 ))
then err_exit 'number of elements of x is not 6'
fi
if [[ ${x[2]} != two ]]
then err_exit ' element two is not 2'
fi
if [[ ${x[@]:2:1} != two ]]
then err_exit ' ${x[@]:2:1} is not two'
fi
set -A y -- ${x[*]}
if [[ $y != zero ]]
then err_exit '$x is not element 0'
fi
if [[ ${y[0]} != zero ]]
then err_exit '${y[0] is not element 0'
fi
if (( ${#y[@]} != 7 ))
then err_exit 'number of elements of y is not 7'
fi
if [[ ${y[2]} != two ]]
then err_exit ' element two is not 2'
fi
if [[ ${y[2]} != two ]]
then err_exit ' element two is not 2'
fi
if [[ ${y[0]} != nine ]]
then err_exit '${y[0] is not nine'
fi
unset y[4]
if (( ${#y[@]} != 6 ))
then err_exit 'number of elements of y is not 6'
fi
if (( ${#y[4]} != 0 ))
then err_exit 'string length of unset element is not 0'
fi
unset foo
if (( ${#foo[@]} != 0 ))
then err_exit 'number of elements of unset variable foo is not 0'
fi
foo=''
if (( ${#foo[0]} != 0 ))
then err_exit 'string length of null element is not 0'
fi
if (( ${#foo[@]} != 1 ))
then err_exit 'number of elements of null variable foo is not 1'
fi
unset foo
unset foo[0]
unset foo[3]
if (( ${#foo[@]} != 0 ))
then err_exit 'number of elements of left in variable foo is not 0'
fi
unset foo
unset foo[3]
unset foo[0]
if (( ${#foo[@]} != 0 ))
then err_exit 'number of elements of left in variable foo again is not 0'
fi
if (( ${#xxx[@]} != 2 ))
then err_exit 'number of elements of left in variable xxx is not 2'
fi
if (( ${#xxx[@]} != 2 ))
then err_exit 'number of elements of left in variable xxx again is not 2'
fi
set -A foo -- "${x[@]}"
if (( ${#foo[@]} != 6 ))
then err_exit 'number of elements of foo is not 6'
fi
if (( ${#PWD[@]} != 1 ))
then err_exit 'number of elements of PWD is not 1'
fi
unset x
if (( ${#x[@]} != 2 ))
then err_exit 'number of elements of x is not 2'
fi
s[1]=1 c[1]=foo
if [[ ${c[s[1]]} != foo ]]
then err_exit 'c[1]=foo s[1]=1; ${c[s[1]]} != foo'
fi
unset s
typeset -Ai s
y=* z=[
s[$y]=1
s[$z]=2
if (( ${#s[@]} != 2 ))
then err_exit 'number of elements of is not 2'
fi
(( s[$z] = s[$z] + ${s[$y]} ))
if [[ ${s[$z]} != 3 ]]
then err_exit '[[ ${s[$z]} != 3 ]]'
fi
if (( s[$z] != 3 ))
then err_exit '(( s[$z] != 3 ))'
fi
(( s[$y] = s[$y] + ${s[$z]} ))
if [[ ${s[$y]} != 4 ]]
then err_exit '[[ ${s[$y]} != 4 ]]'
fi
if (( s[$y] != 4 ))
then err_exit '(( s[$y] != 4 ))'
fi
set -A y 2 4 6
typeset -i y
z=${y[@]}
typeset -R12 y
typeset -i y
if [[ ${y[@]} != "$z" ]]
then err_exit 'error in array conversion from int to R12'
fi
if (( ${#y[@]} != 3 ))
then err_exit 'error in count of array conversion from int to R12'
fi
unset abcdefg
: ${abcdefg[1]}
unset x y
x=1
typeset -i y[$x]=4
if [[ ${y[1]} != 4 ]]
then err_exit 'arithmetic expressions in typeset not working'
fi
unset foo
typeset -A foo
then err_exit 'initial value not preserved when typecast to associative'
fi
unset foo
typeset -A foo
if [[ ${#foo[*]} != 3 ]]
then err_exit 'conversion of indexed to associative array failed'
fi
set a b c d e f g h i j k l m
if [[ ${#} != 13 ]]
then err_exit '${#} not 13'
fi
unset xxx
if [[ ${!xxx[@]} != 0 ]]
then err_exit '${!xxx[@]} for scalar not 0'
fi
if [[ ${11} != k ]]
then err_exit '${11} not working'
fi
if [[ ${@:4:1} != d ]]
then err_exit '${@:4:1} not working'
fi
then err_exit '${!foovar@} does not expand correctly'
fi
then err_exit '${!foovar1} != foovar1'
fi
unset xxx
: ${xxx[3]}
if [[ ${!xxx[@]} ]]
then err_exit '${!xxx[@]} should be null'
fi
integer i=0
{
set -x
xxx[++i]=1
set +x
if (( i != 1))
then err_exit 'execution trace side effects with array subscripts'
fi
unset list
if (( ${#list[@]} != 0))
then err_exit '$(set -A list ...) leaves side effects'
fi
unset list
then err_exit 'array not restored after subshell'
fi
if [[ $(print -r "${xpath[@]##*/}") != 'bin bin ucb bin . sbin sbin' ]]
then err_exit '${xpath[@]##*/} not applied to each element'
fi
integer n=-1
then err_exit 'array offset of -1 not working'
fi
then err_exit 'array offset of -3:1 not working'
fi
$SHELL -c 'x=(if then else fi)' 2> /dev/null || err_exit 'reserved words in x=() assignment not working'
unset foo
if [[ ${#foo[@]} != 2 ]]
then err_exit 'array getting unset before right hand side evaluation'
fi
foo=(143 3643 38732)
export foo
typeset -i foo
then err_exit 'exporting indexed array not exporting 0-th element'
fi
( $SHELL -c '
unset foo
typeset -A foo=([0]=143 [1]=3643 [2]=38732)
export foo
typeset -i foo
[[ $($SHELL -c "print $foo") == 143 ]]'
err_exit 'exporting associative array not exporting 0-th element'
unset foo
typeset -A foo
unset foo
typeset -A foo
integer foo=0
unset foo
do : ${foo[$i]}
done
then err_exit 'error in subscript names'
fi
unset x
x=( 1 2 3)
(x[1]=8)
[[ ${x[1]} == 2 ]] || err_exit 'index array produce side effects in subshells'
x=( 1 2 3)
(
x+=(8)
[[ ${#x[@]} == 4 ]] || err_exit 'index array append in subshell error'
)
[[ ${#x[@]} == 3 ]] || err_exit 'index array append in subshell effects parent'
(x[two]=8)
unset x
(
x+=( [four]=4 )
[[ ${#x[@]} == 4 ]] || err_exit 'associative array append in subshell error'
)
[[ ${#x[@]} == 3 ]] || err_exit 'associative array append in subshell effects parent'
unset x
integer i
for ((i=0; i < 40; i++))
do x[i]=$i
done
[[ ${#x[@]} == 40 ]] || err_exit 'index arrays loosing values'
[[ $( ($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'
unset foo
for ((i=0; i < 2; i++))
(
set +A x bam
foo=0
typeset -A bam
unset bam
[[ $($SHELL -c "var=(foo bar);export var;$tmp/script") == foo ]] || err_exit 'export array not exporting just first element'
unset foo
set --allexport
set --noallexport
unset foo
typeset -A foo
!
[[ $($SHELL -c "typeset -A foo;$tmp/script") == foo ]] 2> /dev/null || err_exit 'empty associative arrays not being cleared correctly before scripts'
[[ $($SHELL -c "typeset -A foo;foo[abc]=abc;$tmp/script") == foo ]] 2> /dev/null || err_exit 'associative arrays not being cleared correctly before scripts'
unset foo
unset foo
function EMPTY
{
typeset i
typeset -n ARRAY=$1
do unset ARRAY[$i]
done
}
unset foo
typeset -A foo
foo[x]=y
unset foo
set -- "${foo[@]:1}"
unset bar
exp=4
unset bar
(( bar == $exp )) || err_exit "subscript of unset variable not evaluated -- expected '$exp', got '$got'"
foo[5]=4
bar[4]=3
foo[4]=5
x=$bar[4]
[[ $x == 4 ]] && err_exit '$bar[4] should not be an array in an assignment'
typeset -i test_variable=0
typeset -A test_array
test_array[1]=100
read test_array[2] <<-!
2
!
read test_array[3] <<-!
3
!
test_array[3]=4
print "val=${test_array[3]}"
++EOF+++
print -r -- ${pastebin.form[0].name}
+++EOF+++
bar=1
unset foo
[[ $(typeset | grep 'foo$') == *index* ]] || err_exit 'append to indexed array not preserving array type'
unset foo
unset foo
[[ $($SHELL -c 'typeset -a foo;typeset | grep "foo$"' 2> /dev/null) == *index* ]] || err_exit 'typeset fails for indexed array with no elements'
[[ $(typeset | grep xxxxx$) == *'indexed array'* ]] || err_exit 'array of one element not an indexed array'
unset foo
foo[1]=(x=3 y=4)
{ [[ ${!foo[1].*} == 'foo[1].x foo[1].y' ]] ;} 2> /dev/null || err_exit '${!foo[sub].*} not expanding correctly'
unset x
$SHELL -c $'x=(foo\n\tbar\nbam\n)' 2> /dev/null || err_exit 'compound array assignment with new-lines not working'
$SHELL -c $'x=(foo\n\tbar:\nbam\n)' 2> /dev/null || err_exit 'compound array assignment with labels not working'
$SHELL -c $'x=(foo\n\tdone\nbam\n)' 2> /dev/null || err_exit 'compound array assignment with reserved words not working'
[[ $($SHELL -c 'typeset -A A; print $(( A[foo].bar ))' 2> /dev/null) == 0 ]] || err_exit 'unset variable not evaluating to 0'
unset a
typeset -A a
a[a].z=1
a[z].z=2
unset a[a]
[[ ${!a[@]} == z ]] || err_exit '"unset a[a]" unsets entire array'
unset a
unset a
a[6]=six
[[ ${#FILTER[@]} == 2 ]] || err_exit "FILTER array should have two elements not ${#FILTER[@]}"
unset x
function x.get
{
print sub=${.sh.subscript}
}
x[2]=
z=$(: ${x[1]} )
[[ $z == sub=1 ]] || err_exit 'get function not invoked for index array'
unset x
typeset -A x
function x.get
{
print sub=${.sh.subscript}
}
x[2]=
z=$(: ${x[1]} )
[[ $z == sub=1 ]] || err_exit 'get function not invoked for associative array'
unset y
i=1
a=(11 22)
typeset -m y=a[i]
[[ $y == 22 ]] || err_exit 'typeset -m for index array not working'
[[ ${a[i]} || ${a[0]} != 11 ]] && err_exit 'typeset -m for index array not deleting element'
unset y
a=([0]=11 [1]=22)
typeset -m y=a[$i]
[[ $y == 22 ]] || err_exit 'typeset -m for associative array not working'
[[ ${a[$i]} || ${a[0]} != 11 ]] && err_exit 'typeset -m for associative array not deleting element'
unset x a j
typeset -a a=( [0]="aa" [1]="bb" [2]="cc" )
typeset -m 'j=a[0]'
typeset -m 'a[0]=a[1]'
typeset -m 'a[1]=j'
unset a j
typeset -A a=( [0]="aa" [1]="bb" [2]="cc" )
typeset -m 'j=a[0]'
typeset -m 'a[0]=a[1]'
typeset -m 'a[1]=j'
unset a j
z=(a b c)
unset x
typeset -m x[1]=z
unset x z
z=([0]=a [1]=b [2]=c)
typeset -m x[1]=z
[[ ${x[1][@]} == 'a b c' ]] || err_exit 'moving associative array to index array element not working'
{
typeset -a arr=(
float
)
unset arr
{
typeset -r -a arr=(
float
)
{
typeset -a arr2=(
typeset +r
)
unset arr2
$SHELL 2> /dev/null -c $'typeset -a arr=(\nfor)' || err_exit 'typeset -a should allow reserved words as first argument'
$SHELL 2> /dev/null -c $'typeset -r -a arr=(\nfor)' || err_exit 'typeset -r -a should allow reserved words as first argument'
typeset arr2[6]
[[ ${#arr2[@]} == 0 ]] || err_exit 'declartion "typeset array[6]" should not show any elements'
[[ ${arr2[1]} == def ]] || err_exit 'declaration "typeset array[6]" causes arrays causes wrong side effects'
unset foo
typeset foo[7]
[[ ${#foo[@]} == 0 ]] || err_exit 'typeset foo[7] should not have one element'
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'
unset foo
[[ ${foo[@]} == 'bb cc' ]] || err_exit "indexed array assignment using parts of array for values gives wrong result of ${foo[@]}"
unset foo
[[ ${foo[@]} == 'bb cc' ]] || err_exit "associative array assignment using parts of array for values gives wrong result of ${foo[@]}"
unset foo
unset foo
typeset -a foo=( a b )
typeset -p foo[10]
unset foo
exp='typeset -a foo=((11 22) (66) )'
x=$(
foo[1]=(66)
typeset -p foo
unset foo
exp='typeset -a foo=((11 22) (x=3))'
x=$(
foo[1]=(x=3)
typeset -p foo
#test for cloning a very large index array - can core dump
(
(
print '('
integer i
for ((i=0 ; i < 16384 ; i++ )) ; do
done
printf 'typeset -a ar=(\n'
for ((i=0 ; i < 16384 ; i++ )) ; do
done
print ')'
print ')'
) | read -C hugecpv
v=$(typeset -p hugecpv)
unset foo
typeset -a foo
unset isnull
typeset -A isnull
: ${isnull[@]}
unset arr2
arr2=()
typeset -A arr2
unset arr2
arr3=(x=3)
typeset -A arr3
[[ $(typeset -p arr3) == 'typeset -A arr3=()' ]] || err_exit 'typeset -A does not first unset compound variable.'
arr4=(x=3)
typeset -a arr4
[[ $(typeset -p arr4) == 'typeset -a arr4' ]] || err_exit 'typeset -a does not first unset compound variable.'
[[ $(typeset -p arr5) == 'typeset -a arr5=(foo bar)' ]] || err_exit 'typeset expanding non-declaration aliases'
typeset -A Foo