1######################################################################## 2# # 3# This software is part of the ast package # 4# Copyright (c) 1982-2010 AT&T Intellectual Property # 5# and is licensed under the # 6# Common Public License, Version 1.0 # 7# by AT&T Intellectual Property # 8# # 9# A copy of the License is available at # 10# http://www.opensource.org/licenses/cpl1.0.txt # 11# (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) # 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 (( Errors+=1 )) 25} 26alias err_exit='err_exit $LINENO' 27 28Command=${0##*/} 29integer Errors=0 30integer n=2 31 32typeset -T Box_t=( 33 float -h 'height in inches' x=2 34 float -h 'width in inches' y=4 35 comvar=(top=8 bottom=9) 36 integer -S count=0 37 items=(foo bar) 38 colors=([wall]=blue [floor]=red) 39 typeset name=unknown 40 typeset -L6 status=INIT 41 len() 42 { 43 print -r $((sqrt(_.x*_.x + _.y*_.y))) 44 (( _.count++)) 45 } 46 typeset -fh 'distance from the origin' len 47 depth() 48 { 49 print 0 50 } 51 float x=3 52) 53 54for ((i=0; i < n; i++)) 55do 56Box_t b=(name=box1) 57exp=3 got=${b.x} 58[[ "$got" == "$exp" ]] || err_exit "\${b.x} incorrect for iteration $i -- expected $exp, got '$got'" 59exp=5 got=$(( b.len )) 60(( got == exp )) || err_exit "b.len incorrect for iteration $i -- expected $exp, got '$got = sqrt(${b.x}*${b.x}+${b.y}*${b.y})'" 61exp=5 got=${b.len} 62[[ "$got" == "$exp" ]] || err_exit "\${b.len} incorrect for iteration $i -- expected $exp, got '$got = sqrt(${b.x}*${b.x}+${b.y}*${b.y})'" 63exp=box1 got=${b.name} 64[[ "$got" == "${exp}" ]] || err_exit "\${b.name} incorrect for iteration $i -- expected $exp, got '$got'" 65exp=2 got=$(( b.count )) 66(( got == exp )) || err_exit "b.count incorrect for iteration $i -- expected $exp, got '$got'" 67exp=2 got=${b.count} 68[[ "$got" == "$exp" ]] || err_exit "\${b.ccount} incorrect for iteration $i -- expected $exp, got '$got'" 69b.colors[wall]=green 70b.colors[door]=white 71exp=3 got=${#b.colors[@]} 72[[ "$got" == "$exp" ]] || err_exit "\${#b.colors[@]} incorrect for iteration $i -- expected $exp, got '$got'" 73b.comvar.bottom=11 74b.items[1]=bam 75b.items[2]=extra 76exp=3 got=${#b.items[@]} 77[[ ${#b.items[@]} == 3 ]] || err_exit "\${#b.items[@]} incorrect for iteration $i -- expected $exp, got '$got'" 78Box_t bb=b 79bb.colors[desk]=orange 80exp=4 got=${#b.colors[@]} 81[[ ${#bb.colors[@]} == 4 ]] || err_exit "\${#bb.colors[@]} incorrect for iteration $i -- expected $exp, got '$got'" 82unset b.colors 83exp=2 got=${#b.colors[@]} 84[[ ${#b.colors[@]} == 2 ]] || err_exit "\${#b.colors[@]} incorrect for iteration $i -- expected $exp, got '$got'" 85unset b.items 86exp=2 got=${#b.items[@]} 87[[ ${#b.items[@]} == 2 ]] || err_exit "\${#b.items[@]} incorrect for iteration $i -- expected $exp, got '$got'" 88unset bb.colors 89exp=2 got=${#bb.colors[@]} 90[[ ${#bb.colors[@]} == 2 ]] || err_exit "\${#bb.colors[@]} incorrect for iteration $i -- expected $exp, got '$got'" 91unset bb.items 92exp=2 got=${#bb.items[@]} 93[[ ${#bb.items[@]} == 2 ]] || err_exit "\${#bb.items[@]} incorrect for iteration $i -- expected $exp, got '$got'" 94[[ $b == "$bb" ]] || err_exit "\$b='$b' != \$bb='$bb'" 95b.count=0 96false 97unset b bb 98done 99 100typeset -T Cube_t=( 101 Box_t _=(y=5) 102 float z=1 103 depth() 104 { 105 print -r -- $((_.z)) 106 } 107 len() 108 { 109 print -r $((sqrt(_.x*_.x + _.y*_.y + _.z*_.z))) 110 (( _.count++)) 111 } 112 float x=8 113 fun() 114 { 115 print 'hello world' 116 } 117) 118 119 120for ((i=0; i < n; i++)) 121do 122Box_t b=(name=box2) 123[[ ${b.name} == box2 ]] || err_exit "\${b.name} incorrect -- expected box2, got '${b.name}'" 124(( b.len == 5 )) || err_exit "b.len incorrect for box2 -- expected 5, got '$(( b.len ))'" 125(( b.count == 1 )) || err_exit "b.count incorrect -- expected 1, got '$(( b.count ))'" 126Cube_t c=(name=cube1) 127[[ $c == $'(\n\ttypeset -l -E x=8\n\ttypeset -l -E y=5\n\tcomvar=(\n\t\ttop=8\n\t\tbottom=9\n\t)\n\ttypeset -S -l -i count=1\n\ttypeset -a items=(\n\t\tfoo\n\t\tbar\n\t)\n\ttypeset -A colors=(\n\t\t[floor]=red\n\t\t[wall]=blue\n\t)\n\tname=cube1\n\ttypeset -L 6 status=INIT\n\ttypeset -l -E z=1\n)' ]] || err_exit '$c not correct' 128[[ ${c.x} == 8 ]] || err_exit '${c.x} != 8' 129[[ ${c.depth} == 1 ]] || err_exit '${c.depth} != 1' 130[[ ${c.name} == cube1 ]] || err_exit '${c.name} != cube1 ' 131[[ $(c.fun) == 'hello world' ]] || err_exit '$(c.fun) != "hello world"' 132[[ ${c.fun} == 'hello world' ]] || err_exit '${c.fun} != "hello world"' 133(( abs(c.len - sqrt(90)) < 1e-10 )) || err_exit 'c.len != sqrt(90)' 134(( c.count == 2 )) || err_exit 'c.count != 2' 135(( c.count == b.count )) || err_exit 'c.count != b.count' 136c.count=0 137Cube_t d=c 138[[ $d == "$c" ]] || err_exit '$d != $c' 139eval "Cube_t zzz=$c" 140[[ $zzz == "$c" ]] || err_exit '$zzz != $c' 141Cube_t zzz=c 142[[ $zzz == "$c" ]] || err_exit '$zzz != $c without eval' 143xxx=$(typeset -p c) 144eval "${xxx/c=/ccc=}" 145[[ $ccc == "$c" ]] || err_exit '$ccc != $c' 146unset b c d zzz xxx ccc 147done 148for ((i=0; i < n; i++)) 149do 150Cube_t cc 151cc[2]=(x=2 y=3 name=two colors+=([table]=white) items+=(pencil) z=6) 152[[ ${cc[0].x} == 8 ]] || err_exit 'cc[0].x !=8' 153[[ ${cc[2].y} == 3 ]] || err_exit '${cc[2].y} != 3' 154(( cc[2].y == 3 )) || err_exit '(( cc[2].y != 3))' 155[[ ${cc[2].colors[table]} == white ]] || err_exit '${cc[2].colors[table]} != white' 156[[ ${cc[2].items[2]} == pencil ]] || err_exit '${cc[2].items[2]} != pencil' 157(( cc[2].len == 7 )) || err_exit '(( cc[2].len != 7 ))' 158[[ $(cc[2].len) == 7 ]] || err_exit '$(cc[2].len) != 7 ))' 159[[ ${cc[2].len} == 7 ]] || err_exit '${cc[2].len} != 7 ))' 160(( cc[2].count == 2 )) || err_exit 'cc[2].count != 2' 161unset cc[2].x cc[2].y cc[2].z 162(( cc[2].len == cc[0].len )) || err_exit 'cc[2].len != cc[0].len' 163(( cc[2].len == cc.len )) || err_exit 'cc[2].len != cc.len' 164(( cc[2].count == 6 )) || err_exit 'cc[2].count != 6' 165unset cc[2].name cc[2].colors cc[2].items 166[[ $cc == "${cc[2]}" ]] || err_exit '$cc != ${cc[2]}' 167cc.count=0 168unset cc 169Cube_t -A cc 170cc[two]=(x=2 y=3 name=two colors+=([table]=white) items+=(pencil) z=6) 171Cube_t cc[one] 172[[ ${#cc[@]} == 2 ]] || err_exit '${#cc[@]} != 2' 173[[ ${cc[two].y} == 3 ]] || err_exit '${cc[two].y} != 3' 174(( cc[two].y == 3 )) || err_exit '(( cc[two].y != 3))' 175[[ ${cc[two].colors[table]} == white ]] || err_exit '${cc[two].colors[table]} != white' 176[[ ${cc[two].items[2]} == pencil ]] || err_exit '${cc[two].items[2]} != pencil' 177(( cc[two].len == 7 )) || err_exit '(( cc[two].len != 7 ))' 178[[ $(cc[two].len) == 7 ]] || err_exit '$(cc[two].len) != 7 ))' 179[[ ${cc[two].len} == 7 ]] || err_exit '${cc[two].len} != 7 ))' 180(( cc[two].count == 2 )) || err_exit 'cc[two].count != 2' 181unset cc[two].x cc[two].y cc[two].z 182(( cc[two].len == cc[one].len )) || err_exit 'cc[two].len != cc[one].len' 183(( cc[two].count == 4 )) || err_exit 'cc[two].count != 4' 184unset cc[two].name unset cc[two].colors cc[two].items 185[[ ${cc[one]} == "${cc[two]}" ]] || err_exit '${cc[one]} != ${cc[two]}' 186cc[two].count=0 187unset cc 188Cube_t cc=( 189 [one]= 190 [two]=(x=2 y=3 name=two colors+=([table]=white) z=6) 191) 192[[ ${#cc[@]} == 2 ]] || err_exit '${#cc[@]} != 2' 193[[ ${cc[two].y} == 3 ]] || err_exit '${cc[two].y} != 3' 194(( cc[two].y == 3 )) || err_exit '(( cc[two].y != 3))' 195[[ ${cc[two].colors[table]} == white ]] || err_exit '${cc[two].colors[table]} != white' 196(( cc[two].len == 7 )) || err_exit '(( cc[two].len != 7 ))' 197[[ $(cc[two].len) == 7 ]] || err_exit '$(cc[two].len) != 7 ))' 198[[ ${cc[two].len} == 7 ]] || err_exit '${cc[two].len} != 7 ))' 199(( cc[two].count == 2 )) || err_exit 'cc[two].count != 2' 200unset cc[two].x cc[two].y cc[two].z 201(( cc[two].len == cc[one].len )) || err_exit 'cc[two].len != cc[one].len' 202(( cc[two].count == 4 )) || err_exit 'cc[two].count != 4' 203cc[three]=cc[two] 204[[ ${cc[two]} == "${cc[three]}" ]] || err_exit ' ${cc[two]} != ${cc[three]}' 205[[ $cc[two] == "${cc[three]}" ]] || err_exit ' $cc[two] != $cc[three]' 206[[ ${#cc[@]} == 3 ]] || err_exit '${#cc[@]} != 3' 207unset cc[two].name unset cc[two].colors 208cc[two].count=0 209unset cc 210done 211exit $Errors 212