xref: /illumos-gate/usr/src/contrib/ast/src/cmd/ksh93/tests/enum.sh (revision b30d193948be5a7794d7ae3ba0ed9c2f72c88e0f)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1982-2012 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
30enum Color_t=(red green blue orange yellow)
31enum -i Sex_t=(Male Female)
32for ((i=0; i < 1000; i++))
33do
34Color_t x
35[[ $x == red ]] || err_exit 'Color_t does not default to red'
36x=orange
37[[ $x == orange ]] || err_exit '$x should be orange'
38( x=violet) 2> /dev/null && err_exit 'x=violet should fail'
39x[2]=green
40[[ ${x[2]} == green ]] || err_exit '${x[2]} should be green'
41(( x[2] == 1 )) || err_exit '((x[2]!=1))'
42[[ $((x[2])) == 1 ]] || err_exit '$((x[2]))!=1'
43[[ $x == orange ]] || err_exit '$x is no longer orange'
44Color_t -A y
45y[foo]=yellow
46[[ ${y[foo]} == yellow ]] || err_exit '${y[foo]} != yellow'
47(( y[foo] == 4 )) || err_exit '(( y[foo] != 4))'
48unset y
49typeset -a [Color_t] z
50z[green]=xyz
51[[ ${z[green]} == xyz ]] || err_exit '${z[green]} should be xyz'
52[[ ${z[1]} == xyz ]] || err_exit '${z[1]} should be xyz'
53z[orange]=bam
54[[ ${!z[@]} == 'green orange' ]] || err_exit '${!z[@]} == "green orange"'
55unset x
56Sex_t x
57[[ $x == Male ]] || err_exit 'Sex_t not defaulting to Male'
58x=female
59[[ $x == Female ]] || err_exit 'Sex_t not case sensitive'
60unset x y z
61done
62(
63typeset -T X_t=( typeset name=aha )
64typeset -a[X_t] arr
65) 2> /dev/null
66[[ $? == 1 ]] || err_exit 'typeset -a[X_t] should generate an error message when X-t is not an enumeriation type'
67
68typeset -a [Color_t] arr
69arr[green]=foo
70[[ ${arr[1]} == ${arr[green]}  ]] || err_exit 'arr[1] != arr[green]'
71read -A arr <<<  'x y z xx yy'
72[[ ${arr[1]} == ${arr[green]}  ]] || err_exit 'arr[1] != arr[green] after read'
73
74exit $((Errors<125?Errors:125))
75