########################################################################
# #
# 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"
}
alias err_exit='err_exit $LINENO'
integer Errors=0
integer n=2
typeset -T Type_t=(
typeset -i z=5
)
for ((i=0; i < 10; i++))
do
Type_t r s
typeset -C x=r.x
typeset -C z=y
typeset -C s.y=z
.sh.q=$y
function foo
{
nameref x=$1 y=$2
typeset z=$x
y=$x
}
typeset -C y=z
typeset -C y=z
(
q=${z}
z=abc
)
done
[[ $(typeset -p frame) == 'Frame_t frame=(typeset file;typeset lineno)' ]] || err_exit 'empty fields in type not displayed'
typeset -C y=x
for ((i=0; i < 1; i++))
do typeset -m zzz=x
typeset -m x=zzz
typeset -m r=t
typeset -m t=r
done
unset x y zzz
x=(a b c)
typeset -m x[1]=x[2]
typeset -T Pt_t=(float x=1. y=0.)
Pt_t p=(y=2)
print -r -- ${p.y}
+++
expected=2
[[ "$got" == "$expected" ]] || err_exit "typedefs in dot script failed -- expected '$expected', got '$got'"
typeset -T X_t=(
typeset s=${_.x}
create()
{
}
)
X_t x
Y_t z
typeset -T X_t=(
f()
{
print ok
}
)
+++
typeset -T X_t=(
x=1
f()
{
print ok
}
)
X_t z=(
function f
{
print override f
}
)
+++
typeset -T X_t=(
x=1
f()
{
print ok
}
)
X_t z=(
function g
{
print override f
}
)
+++
# compound variables containing type variables
Type_t r
var=(
typeset x=foobar
Type_t r
integer z=5
)
typeset -T A_t=(
typeset x=aha
typeset b=${_.x}
)
unset x
A_t x
[[ "$got" == "$expected" ]] || err_exit "type '_' reference failed -- expected '$expected', got '$got'"
typeset -T Tst_t=(
function f
{
A_t a
}
function g
{
print foo
}
)
[[ "$got" == "$expected" ]] || err_exit "_.g where g is a function in type discipline method failed -- expected '$expected', got '$got'"
typeset -T B_t=(
integer -a arr
function f
{
print ${_.arr[*]}
}
)
unset x
B_t x
expected='0 1'
[[ "$got" == "$expected" ]] || err_exit "array assignment of subscripts in type discipline arithmetic failed -- expected '$expected', got '$got'"
typeset -T Fileinfo_t=(
size=-1
typeset -a text=()
integer mtime=-1
)
function bar
{
finfo.size=${#finfo.text[@]}
}
expected='Fileinfo_t -A _Dbg_filenames=([foo]=(size=3;typeset -a text=(line1 line2 line3);typeset -l -i mtime=-1))'
got=$(typeset -p _Dbg_filenames)
}
function f1
{
typeset -n v=$1
print -r -- "$v"
}
typeset -T A_t=(
function f { f1 _ ;}
)
A_t a
+++++
[[ $( $SHELL << \+++
typeset -T A_t=(
)
typeset -T B_t=(
typeset arr
A_t a
f()
{
typeset -p _.a
}
)
B_t b
+++
expected='A_t x=(name=xxx)'
[[ $( $SHELL << \+++
typeset -T A_t=(
typeset name
)
typeset -p x
+++
typeset -T Foo_t=(
integer x=3
integer y=4
)
typeset -T benchmark_t=(
integer num_iterations
)
function do_benchmarks
{
nameref tst=b
integer num_iterations
}
benchmark_t b=(num_iterations=5)
[[ $(do_benchmarks) == 5 ]] || err_exit 'scoping of nameref of type variables in arithmetic expressions not working'
function cat_content
{
cat <<- EOF
(
foo_t -a foolist=(
( val=3 )
( val=4 )
( val=5 )
)
)
EOF
return 0
}
typeset -T foo_t=(
integer val=-1
function print
{
print -- ${_.val}
}
)
function do_something
{
nameref li=$1 # "li" may be an index or associative array
li[2].print
}
cat_content | read -C x
[[ $(do_something x.foolist) == 5 ]] || err_exit 'subscripts not honored for arrays of type with disciplines'
typeset -T benchcmd_t=(
float x=1
float y=2
)
unset x
compound x=(
float o
benchcmd_t -a m
integer h
)
expected=$'(\n\ttypeset -l -i h=0\n\tbenchcmd_t -a m\n\ttypeset -l -E o=0\n)'
[[ $x == "$expected" ]] || err_exit 'compound variable with array of types with no elements not working'
expected=$'Std_file_t db.file[/etc/profile]=(action=preserve;typeset -A sum=([8242e663d6f7bb4c5427a0e58e2925f3]=1);)'
{
got=$($SHELL <<- \EOF
MAGIC='stdinstall (at&t research) 2009-08-25'
typeset -T Std_file_t=(
typeset action
typeset -A sum
)
typeset -T Std_t=(
typeset magic=$MAGIC
Std_file_t -A file
)
Std_t db=(magic='stdinstall (at&t research) 2009-08-25';Std_file_t -A file=( [/./home/gsf/.env.sh]=(action=preserve;typeset -A sum=([9b67ab407d01a52b3e73e3945b9a3ee0]=1);)[/etc/profile]=(action=preserve;typeset -A sum=([8242e663d6f7bb4c5427a0e58e2925f3]=1);)[/home/gsf/.profile]=(action=preserve;typeset -A sum=([3ce23137335219672bf2865d003a098e]=1);));)
typeset -p db.file[/etc/profile]
EOF)
} 2> /dev/null
[[ $got == "$expected" ]] || err_exit 'types with arrays of types as members fails'
typeset -T x_t=(
integer dummy
function set
{
[[ ${.sh.name} == v ]] || err_exit "name=${.sh.name} should be v"
[[ ${.sh.subscript} == 4 ]] || err_exit "subscript=${.sh.subscript} should be 4"
[[ ${.sh.value} == hello ]] || err_exit "value=${.sh.value} should be hello"
}
)
x_t -a v
v[4]="hello"
typeset -T oset=(
typeset -A s
)
oset foo bar
: ${foo.s[a]:=foobar}
: ${bar.s[d]:=foobar}
[[ ${bar.s[a]} == foobar ]] && err_exit '${var:=val} for types assigns to type instead of type instance'
typeset -T olist=(
typeset -a l
)
olist foo
foo.l[1]=x
[[ ${!foo.l[*]} == *0* ]] && '0-th elment of foo.l should not be set'
typeset -T oset2=( typeset -A foo )
oset2 bar
: ${bar.foo[a]}
bar.foo[a]=b
[[ ${#bar.foo[*]} == 1 ]] || err_exit "bar.foo should have 1 element not ${#bar.foo[*]}"
[[ ${bar.foo[*]} == b ]] || err_exit "bar.foo[*] should be 'b' not ${bar.foo[*]}"
[[ ${bar.foo[a]} == b ]] || err_exit "bar.foo[a] should be 'b' not ${bar.foo[*]}"
{ x=$( $SHELL 2> /dev/null << \++EOF++
typeset -T ab_t=(
integer a=1 b=2
function increment
{
(( _.a++, _.b++ ))
}
)
function ar_n
{
nameref sn=$2
sn.increment
$1 && printf "a=%d, b=%d\n" sn.a sn.b
}
function ar
{
ab_t -S -a s
[[ -v s[5] ]] || s[5]=( )
ar_n $1 s[5]
}
x=$(ar false ; ar false ; ar true ; printf ";")
y=$(ar false ; ar false ; ar true ; printf ";")
print -r -- "\"$x\"" == "\"$y\""
++EOF++
) ;} 2> /dev/null
[[ $x == *a=4*b=5* ]] || err_exit 'static types in a function not working'
{ eval "[[ $x ]]";} 2> /dev/null || err_exit 'arrays of types leaving side effects in subshells'
typeset -T y_t=(
typeset dummy
function print_b
{
print "B"
}
)
y_t a b=(
function print_b
{
print "1"
}
)
[[ $(a.print_b) == B ]] || err_exit 'default discipline not working'
[[ $(b.print_b) == 1 ]] || err_exit 'discipline override not working'
$SHELL 2> /dev/null -c 'true || { typeset -T Type_t=(typeset name=foo);
Type_t z=(name=bar) ;}' || err_exit 'unable to parse type command until typeset -T executes'
cd "$tmp"
FPATH=$PWD
PATH=$PWD:$PATH
cat > A_t <<- \EOF
typeset -T A_t=(
B_t b
)
EOF
typeset -T B_t=(
integer n=5
)
unset n
else err_exit 'unable to load types dynamically'
fi
# check that typeset -T reproduces a type.
typeset -T foo_t=(
integer x=3 y=4
float z=1.2
len()
{
}
function count
{
print z=$z
}
)
typeset -T
print 'typeset -T'
+++EOF
else
err_exit 'typeset -T not supported'
fi
[[ $($SHELL -c 'typeset -T x=( typeset -a h ) ; x j; print -v j.h') ]] && err_exit 'type with indexed array without elements inserts element 0'
[[ $($SHELL -c 'typeset -T x=( integer -a s ) ; compound c ; x c.i ; c.i.s[4]=666 ; print -v c') == *'[0]'* ]] && err_exit 'type with indexed array with non-zero element inserts element 0'
{ $SHELL -c '(sleep 3;kill $$)& typeset -T x=( typeset -a s );compound c;x c.i;c.i.s[7][5][3]=hello;x c.j=c.i;[[ ${c.i} == "${c.j}" ]]';} 2> /dev/null
exitval=$?
then err_exit 'clone of multi-dimensional array timed out'
fi
typeset -T foobar_t=(
float x=1 y=0
slen()
{
}
len()
{
}
)
unset z
foobar_t z=(x=3 y=4)
x1.ce[3][4]=45
compound c
unset b
typeset -T a_t=(
typeset a="hello"
)
typeset -T b_t=(
a_t b
)
compound b
compound -a b.ca
exp='typeset -C b=(typeset -C -a ca=( [4]=(b_t b=(a_t b=(a=hello))));)'
got=$(typeset -p b)
typeset -T u_t=(
integer dummy
unset()
{
print unset
}
)
unset z
u_t -a x | read z
[[ $z == unset ]] && err_exit 'unset discipline called on type creation'
[[ $z ]] && err_exit '"typeset -T foo=bar" should not creates type foo'
{
typeset -T board_t=(
compound -a board_y
function binsert
{
nameref figure=$1
integer y=$2 x=$3
typeset -m "_.board_y[y].board_x[x].field=figure"
}
)
function main
{
compound c=(
board_t b
)
for ((i=0 ; i < 2 ; i++ )) ; do
c.b.binsert p 1 $i
done
exp='typeset -C c=(board_t b=(typeset -a board_y=( [1]=(typeset -a board_x=( [0]=(field=(hello=world;))[1]=(field=(hello=world)));));))'
}
then err_exit 'typeset -m in type discipline causes exception'
else err_exit 'typeset -m in type discipline gives wrong value'
fi
fi
typeset -T pawn_t=(
{
print 'PAWN'
}
)
function main
{
compound c=(
compound -a board
)
for ((i=2 ; i < 8 ; i++ )) ; do
done
}
typeset -T Bar_t=(
typeset -a foo
)
[[ ${bar.foo[0]} == bam ]] || err_exit 'appending to empty array variable in type does not create element 0'