types.sh revision 34f9b3eef6fdadbda0a846aa4d68691ac40eace5
########################################################################
# #
# This software is part of the ast package #
# Copyright (c) 1982-2009 AT&T Intellectual Property #
# and is licensed under the #
# Common Public License, Version 1.0 #
# by AT&T Intellectual Property #
# #
# A copy of the License is available at #
# (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) #
# #
# 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=2;typeset -C -a text=([0]=line1 [1]=line2 [2]=line3);typeset -l -i mtime=-1;))'
got=$(typeset -p _Dbg_filenames)
err_exit "copy to associative array of types in function failed -- expected '$expected', got '$got'"
}
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'
exit $Errors