1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1982-2011 AT&T Intellectual Property          #
5#                      and is licensed under the                       #
6#                 Eclipse Public License, Version 1.0                  #
7#                    by AT&T Intellectual Property                     #
8#                                                                      #
9#                A copy of the License is available at                 #
10#          http://www.eclipse.org/org/documents/epl-v10.html           #
11#         (with md5 checksum b35adb5213ca9657e911e9befb180842)         #
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
96unset b bb
97done
98
99typeset -T Cube_t=(
100	Box_t	_=(y=5)
101	float	z=1
102	depth()
103	{
104		print -r -- $((_.z))
105	}
106	len()
107	{
108		print -r $((sqrt(_.x*_.x + _.y*_.y + _.z*_.z)))
109		(( _.count++))
110	}
111	float x=8
112	fun()
113	{
114		print 'hello world'
115	}
116)
117
118
119for ((i=0; i < n; i++))
120do
121Box_t b=(name=box2)
122[[ ${b.name} == box2 ]] || err_exit "\${b.name} incorrect -- expected box2, got '${b.name}'"
123(( b.len == 5 )) || err_exit "b.len incorrect for box2 -- expected 5, got '$(( b.len ))'"
124(( b.count == 1 )) || err_exit "b.count incorrect -- expected 1, got '$(( b.count ))'"
125Cube_t c=(name=cube1)
126[[ $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'
127[[ ${c.x} == 8 ]] || err_exit '${c.x} != 8'
128[[ ${c.depth} == 1 ]] || err_exit '${c.depth} != 1'
129[[ ${c.name} == cube1 ]] || err_exit '${c.name} != cube1 '
130[[ $(c.fun) == 'hello world' ]] || err_exit '$(c.fun) != "hello world"'
131[[ ${c.fun} == 'hello world' ]] || err_exit '${c.fun} != "hello world"'
132(( abs(c.len - sqrt(90)) < 1e-10 )) || err_exit 'c.len != sqrt(90)'
133(( c.count == 2 )) || err_exit 'c.count != 2'
134(( c.count == b.count )) || err_exit 'c.count != b.count'
135c.count=0
136Cube_t d=c
137[[ $d == "$c" ]] || err_exit '$d != $c'
138eval "Cube_t zzz=$c"
139[[ $zzz == "$c" ]] || err_exit '$zzz != $c'
140Cube_t zzz=c
141[[ $zzz == "$c" ]] || err_exit '$zzz != $c without eval'
142xxx=$(typeset -p c)
143eval "${xxx/c=/ccc=}"
144[[ $ccc == "$c" ]] || err_exit '$ccc != $c'
145unset b c d zzz xxx ccc
146done
147for ((i=0; i < n; i++))
148do
149Cube_t cc
150cc[2]=(x=2 y=3 name=two colors+=([table]=white) items+=(pencil) z=6)
151[[ ${cc[0].x} == 8 ]] || err_exit 'cc[0].x !=8'
152[[ ${cc[2].y} == 3 ]] || err_exit '${cc[2].y} != 3'
153(( cc[2].y == 3 )) || err_exit '(( cc[2].y != 3))'
154[[ ${cc[2].colors[table]} == white ]] || err_exit '${cc[2].colors[table]} != white'
155[[ ${cc[2].items[2]} == pencil ]] || err_exit '${cc[2].items[2]} != pencil'
156(( cc[2].len == 7 )) || err_exit '(( cc[2].len != 7 ))'
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].count == 2 )) || err_exit 'cc[2].count != 2'
160unset cc[2].x cc[2].y cc[2].z
161(( cc[2].len == cc[0].len )) || err_exit 'cc[2].len != cc[0].len'
162(( cc[2].len == cc.len )) || err_exit 'cc[2].len != cc.len'
163(( cc[2].count == 6 )) || err_exit 'cc[2].count != 6'
164unset cc[2].name cc[2].colors cc[2].items
165[[ $cc == "${cc[2]}" ]] || err_exit '$cc != ${cc[2]}'
166cc.count=0
167unset cc
168Cube_t -A cc
169cc[two]=(x=2 y=3 name=two colors+=([table]=white) items+=(pencil) z=6)
170Cube_t cc[one]
171[[ ${#cc[@]} == 2 ]] || err_exit '${#cc[@]} != 2'
172[[ ${cc[two].y} == 3 ]] || err_exit '${cc[two].y} != 3'
173(( cc[two].y == 3 )) || err_exit '(( cc[two].y != 3))'
174[[ ${cc[two].colors[table]} == white ]] || err_exit '${cc[two].colors[table]} != white'
175[[ ${cc[two].items[2]} == pencil ]] || err_exit '${cc[two].items[2]} != pencil'
176(( cc[two].len == 7 )) || err_exit '(( cc[two].len != 7 ))'
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].count == 2 )) || err_exit 'cc[two].count != 2'
180unset cc[two].x cc[two].y cc[two].z
181(( cc[two].len == cc[one].len )) || err_exit 'cc[two].len != cc[one].len'
182(( cc[two].count == 4 )) || err_exit 'cc[two].count != 4'
183unset cc[two].name unset cc[two].colors cc[two].items
184[[ ${cc[one]} == "${cc[two]}" ]] || err_exit '${cc[one]} != ${cc[two]}'
185cc[two].count=0
186unset cc
187Cube_t cc=(
188	[one]=
189	[two]=(x=2 y=3 name=two colors+=([table]=white) z=6)
190)
191[[ ${#cc[@]} == 2 ]] || err_exit '${#cc[@]} != 2'
192[[ ${cc[two].y} == 3 ]] || err_exit '${cc[two].y} != 3'
193(( cc[two].y == 3 )) || err_exit '(( cc[two].y != 3))'
194[[ ${cc[two].colors[table]} == white ]] || err_exit '${cc[two].colors[table]} != white'
195(( cc[two].len == 7 )) || err_exit '(( cc[two].len != 7 ))'
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].count == 2 )) || err_exit 'cc[two].count != 2'
199unset cc[two].x cc[two].y cc[two].z
200(( cc[two].len == cc[one].len )) || err_exit 'cc[two].len != cc[one].len'
201(( cc[two].count == 4 )) || err_exit 'cc[two].count != 4'
202cc[three]=cc[two]
203[[ ${cc[two]} == "${cc[three]}" ]] || err_exit "\${cc[two]}='${cc[two]}' != \${cc[three]}='${cc[three]}'"
204[[ $cc[two] == "${cc[three]}" ]] || err_exit "\$cc[two]='${cc[two]}' != \${cc[three]}='${cc[three]}'"
205exp=3
206got=${#cc[@]}
207[[ $got == $exp ]] || err_exit "\${#cc[@]} failed -- expected '$exp', got '$got'"
208unset cc[two].name unset cc[two].colors
209cc[two].count=0
210unset cc
211done
212
213exit $((Errors<125?Errors:125))
214