1#
2# CDDL HEADER START
3#
4# The contents of this file are subject to the terms of the
5# Common Development and Distribution License (the "License").
6# You may not use this file except in compliance with the License.
7#
8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9# or http://www.opensolaris.org/os/licensing.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21
22#
23# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24#
25
26#
27# This test module contains misc compound tests which do not have
28# their own module yet.
29#
30#
31
32# test setup
33function err_exit
34{
35	print -u2 -n "\t"
36	print -u2 -r ${Command}[$1]: "${@:2}"
37	(( Errors < 127 && Errors++ ))
38}
39alias err_exit='err_exit $LINENO'
40
41set -o nounset
42Command=${0##*/}
43integer Errors=0
44
45# global utility functions
46compound bracketstat=(
47	integer bopen=0
48	integer bclose=0
49)
50
51function count_brackets
52{
53	typeset x="$1"
54	typeset c
55
56	integer i
57	(( bracketstat.bopen=0 , bracketstat.bclose=0 ))
58
59	for (( i=0 ; i < ${#x} ; i++ )) ; do
60	        c="${x:i:1}"
61		[[ "$c" == "(" ]] && (( bracketstat.bopen++ ))
62		[[ "$c" == ")" ]] && (( bracketstat.bclose++ ))
63	done
64
65	(( bracketstat.bopen != bracketstat.bclose )) && return 1
66
67	return 0
68}
69
70
71typeset ocwd
72typeset tmpdir
73
74# create temporary test directory
75ocwd="$PWD"
76tmpdir="$(mktemp -t -d "test_sun_solaris_compound_misc.XXXXXXXX")" || err_exit "Cannot create temporary directory"
77
78cd "${tmpdir}" || { err_exit "cd ${tmpdir} failed." ; exit $((Errors)) ; }
79
80# ksh93 <= ast-ksh.2010-03-09 prints garbage for compound x=( compound -a nodes=( [4]=( ) ) );typeset -p x
81function test_compound_indexed_array_init_1
82{
83	compound vx=( compound -a nodes=( [4]=( ) )  )
84	compound vy
85	compound -a vy.nodes=( [4]=( ) )
86	compound vz
87	compound -a vz.nodes
88	vz.nodes[4]=( )
89
90	cx="$(typeset -p vx)" ; cx="${cx//vx/tt}"
91	cy="$(typeset -p vy)" ; cy="${cy//vy/tt}"
92	cz="$(typeset -p vz)" ; cz="${cz//vz/tt}"
93	[[ "$cx" == "$cy" ]] || err_exit "'$cx' != '$cy'"
94	[[ "$cx" == "$cz" ]] || err_exit "'$cx' != '$cz'"
95	[[ "$cy" == "$cz" ]] || err_exit "'$cy' != '$cz'"
96
97	count_brackets "$cx" || err_exit "Brackets not balanced for '$cx'"
98	count_brackets "$cy" || err_exit "Brackets not balanced for '$cy'"
99	count_brackets "$cz" || err_exit "Brackets not balanced for '$cz'"
100	count_brackets "$(print -v vx)" || err_exit "Brackets not balanced for '$(print -v vx)'"
101	count_brackets "$(print -v vy)" || err_exit "Brackets not balanced for '$(print -v vy)'"
102	count_brackets "$(print -v vz)" || err_exit "Brackets not balanced for '$(print -v vz)'"
103	count_brackets "$(print -C vx)" || err_exit "Brackets not balanced for '$(print -C vx)'"
104	count_brackets "$(print -C vy)" || err_exit "Brackets not balanced for '$(print -C vy)'"
105	count_brackets "$(print -C vz)" || err_exit "Brackets not balanced for '$(print -C vz)'"
106
107	cx="$(typeset +p vx.nodes)" ;    [[ "$cx" == *-C* && "$cx" == *-a* ]] || err_exit "'$cx' lacks -C/-a attribute"
108	cy="$(typeset +p vy.nodes)" ;    [[ "$cy" == *-C* && "$cy" == *-a* ]] || err_exit "'$cy' lacks -C/-a attribute"
109	cz="$(typeset +p vz.nodes)" ;    [[ "$cz" == *-C* && "$cz" == *-a* ]] || err_exit "'$cz' lacks -C/-a attribute"
110	cx="$(typeset +p vx.nodes[4])" ; [[ "$cx" == *-C*                  ]] || err_exit "'$cx' lacks -C attribute"
111	cy="$(typeset +p vy.nodes[4])" ; [[ "$cy" == *-C*                  ]] || err_exit "'$cy' lacks -C attribute"
112	cz="$(typeset +p vz.nodes[4])" ; [[ "$cz" == *-C*                  ]] || err_exit "'$cz' lacks -C attribute"
113
114	return 0
115}
116
117# ksh93 <= ast-ksh.2010-03-09 prints garbage for compound x=( compound -a nodes=( [4]=( ) ) );typeset -p x
118# this test is the same as test_compound_indexed_array_init_1 but "-a" was replaced with "-A"
119function test_compound_associative_array_init_1
120{
121	compound vx=( compound -A nodes=( [4]=( ) )  )
122	compound vy
123	compound -A vy.nodes=( [4]=( ) )
124	compound vz
125	compound -A vz.nodes
126	vz.nodes[4]=( )
127
128	cx="$(typeset -p vx)" ; cx="${cx//vx/tt}"
129	cy="$(typeset -p vy)" ; cy="${cy//vy/tt}"
130	cz="$(typeset -p vz)" ; cz="${cz//vz/tt}"
131	[[ "$cx" == "$cy" ]] || err_exit "'$cx' != '$cy'"
132	[[ "$cx" == "$cz" ]] || err_exit "'$cx' != '$cz'"
133	[[ "$cy" == "$cz" ]] || err_exit "'$cy' != '$cz'"
134
135	count_brackets "$cx" || err_exit "Brackets not balanced for '$cx'"
136	count_brackets "$cy" || err_exit "Brackets not balanced for '$cy'"
137	count_brackets "$cz" || err_exit "Brackets not balanced for '$cz'"
138	count_brackets "$(print -v vx)" || err_exit "Brackets not balanced for '$(print -v vx)'"
139	count_brackets "$(print -v vy)" || err_exit "Brackets not balanced for '$(print -v vy)'"
140	count_brackets "$(print -v vz)" || err_exit "Brackets not balanced for '$(print -v vz)'"
141	count_brackets "$(print -C vx)" || err_exit "Brackets not balanced for '$(print -C vx)'"
142	count_brackets "$(print -C vy)" || err_exit "Brackets not balanced for '$(print -C vy)'"
143	count_brackets "$(print -C vz)" || err_exit "Brackets not balanced for '$(print -C vz)'"
144
145	cx="$(typeset +p vx.nodes)" ;    [[ "$cx" == *-C* && "$cx" == *-A* ]] || err_exit "'$cx' lacks -C/-A attribute"
146	cy="$(typeset +p vy.nodes)" ;    [[ "$cy" == *-C* && "$cy" == *-A* ]] || err_exit "'$cy' lacks -C/-A attribute"
147	cz="$(typeset +p vz.nodes)" ;    [[ "$cz" == *-C* && "$cz" == *-A* ]] || err_exit "'$cz' lacks -C/-A attribute"
148	cx="$(typeset +p vx.nodes[4])" ; [[ "$cx" == *-C*                  ]] || err_exit "'$cx' lacks -C attribute"
149	cy="$(typeset +p vy.nodes[4])" ; [[ "$cy" == *-C*                  ]] || err_exit "'$cy' lacks -C attribute"
150	cz="$(typeset +p vz.nodes[4])" ; [[ "$cz" == *-C*                  ]] || err_exit "'$cz' lacks -C attribute"
151
152	return 0
153}
154
155# run tests
156test_compound_indexed_array_init_1
157test_compound_associative_array_init_1
158
159cd "${ocwd}"
160rmdir "${tmpdir}" || err_exit "Cannot remove temporary directory ${tmpdir}".
161
162# tests done
163exit $((Errors))
164