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
30
31typeset -T Pt_t=(
32	float x=1
33	float y=0
34	len()
35	{
36		print -r $((sqrt(_.x*_.x + _.y*_.y)))
37	}
38)
39
40for ((i=0; i < 100; i++))
41do
42Pt_t p
43[[ ${p.x} == 1 ]] || err_exit '${p[x]} is not 1'
44(( p.x == 1 )) || err_ext 'p[x] is not 1'
45[[ $(p.len) == 1 ]] || err_exit '$(p.len) != 1'
46[[ ${p.len} == 1 ]] || err_exit '${p.len} != 1'
47(( p.len == 1  )) || err_exit '((p.len != 1))'
48Pt_t q=(y=2)
49(( q.x == 1 )) || err_exit 'q.x is not 1'
50(( (q.len - sqrt(5)) < 10e-10 )) || err_exit 'q.len != sqrt(5)'
51q.len()
52{
53	print -r $((abs(_.x)+abs(_.y) ))
54}
55(( q.len == 3 )) || err_exit 'q.len is not 3'
56p=q
57[[ ${p.y} == 2 ]] || err_exit '${p[y]} is not 2'
58[[ ${@p} == Pt_t ]] || err_exit 'type of p is not Pt_t'
59[[ ${@q} == Pt_t ]] || err_exit 'type of q is not Pt_t'
60(( p.len == 3 )) || err_exit 'p.len is not 3'
61unset p q
62Pt_t pp=( (  x=3 y=4) (  x=5 y=12) (y=2) )
63(( pp[0].len == 5 )) || err_exit 'pp[0].len != 5'
64(( pp[1].len == 13 )) || err_exit 'pp[0].len != 12'
65(( (pp[2].len - sqrt(5)) < 10e-10 )) || err_exit 'pp[2].len != sqrt(5)'
66[[ ${pp[1]} == $'(\n\ttypeset -l -E x=5\n\ttypeset -l -E y=12\n)' ]] || err_exit '${pp[1] is not correct'
67[[ ${!pp[@]} == '0 1 2' ]] || err_exit '${pp[@] != "0 1 2"'
68pp+=( x=6 y=8)
69(( pp[3].len == 10 )) || err_exit 'pp[3].len != 10'
70[[ ${!pp[@]} == '0 1 2 3' ]] || err_exit '${pp[@] != "0 1 2 3"'
71pp[4]=pp[1]
72[[ ${pp[4]} == $'(\n\ttypeset -l -E x=5\n\ttypeset -l -E y=12\n)' ]] || err_exit '${pp[4] is not correct'
73unset pp
74Pt_t pp=( [one]=(  x=3 y=4) [two]=(  x=5 y=12) [three]=(y=2) )
75(( pp[one].len == 5 )) || err_exit 'pp[one].len != 5'
76(( pp[two].len == 13 )) || err_exit 'pp[two].len != 12'
77[[ ${pp[two]} == $'(\n\ttypeset -l -E x=5\n\ttypeset -l -E y=12\n)' ]] || err_exit '${pp[two] is not correct'
78[[ ${!pp[@]} == 'one three two' ]] || err_exit '${pp[@] != "one three two"'
79[[ ${@pp[1]} == Pt_t ]] || err_exit 'type of pp[1] is not Pt_t'
80unset pp
81done
82# redefinition of point
83typeset -T Pt_t=(
84	Pt_t _=(x=3 y=6)
85	float z=2
86	len()
87	{
88		print -r $((sqrt(_.x*_.x + _.y*_.y + _.z*_.z)))
89	}
90)
91Pt_t p
92[[ ${p.y} == 6 ]] || err_exit '${p.y} != 6'
93(( p.len == 7 )) || err_exit '((p.len !=7))'
94
95z=()
96Pt_t -a z.p
97z.p[1]=(y=2)
98z.p[2]=(y=5)
99z.p[3]=(x=6 y=4)
100eval y="$z"
101[[ $y == "$z" ]] || err_exit 'expansion of indexed array of types is incorrect'
102eval "$(typeset -p y)"
103[[ $y == "$z" ]] || err_exit 'typeset -p z for indexed array of types is incorrect'
104unset z y
105z=()
106Pt_t -A z.p
107z.p[1]=(y=2)
108z.p[2]=(y=5)
109z.p[3]=(x=6 y=4)
110eval y="$z"
111[[ $y == "$z" ]] || err_exit 'expansion of associative array of types is incorrect'
112eval "$(typeset -p y)"
113[[ $y == "$z" ]] || err_exit 'typeset -p z for associative of types is incorrect'
114unset z y
115
116typeset -T A_t=(
117        Pt_t  -a  b
118)
119typeset -T B_t=(
120        Pt_t  -A  b
121)
122A_t r
123r.b[1]=(y=2)
124r.b[2]=(y=5)
125eval s="$r"
126[[ $r == "$s" ]] || err_exit 'expansion of type containing index array of types is incorrect'
127eval "$(typeset -p s)"
128[[ $y == "$z" ]] || err_exit 'typeset -p z for type containing index of types is incorrect'
129unset r s
130B_t r
131r.b[1]=(y=2)
132r.b[2]=(y=5)
133eval s="$r"
134[[ $r == "$s" ]] || err_exit 'expansion of type containing index array of types is incorrect'
135eval "$(typeset -p s)"
136[[ $y == "$z" ]] || err_exit 'typeset -p z for type containing index of types is incorrect'
137
138exit $((Errors<125?Errors:125))
139