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	let Errors+=1
25}
26alias err_exit='err_exit $LINENO'
27
28Command=${0##*/}
29integer Errors=0
30
31tmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
32trap "cd /; rm -rf $tmp" EXIT
33
34r=readonly u=Uppercase l=Lowercase i=22 i8=10 L=abc L5=def uL5=abcdef xi=20
35x=export t=tagged H=hostname LZ5=026 RZ5=026 Z5=123 lR5=ABcdef R5=def n=l
36for option in u l i i8 L L5 LZ5 RZ5 Z5 r x H t R5 uL5 lR5 xi n
37do	typeset -$option $option
38done
39(r=newval) 2> /dev/null && err_exit readonly attribute fails
40i=i+5
41if	((i != 27))
42then	err_exit integer attributes fails
43fi
44if	[[ $i8 != 8#12 ]]
45then	err_exit integer base 8 fails
46fi
47if	[[ $u != UPPERCASE ]]
48then	err_exit uppercase fails
49fi
50if	[[ $l != lowercase ]]
51then	err_exit lowercase fails
52fi
53if	[[ $n != lowercase ]]
54then	err_exit reference variables fail
55fi
56if	[[ t=tagged != $(typeset -t) ]]
57then	err_exit tagged fails
58fi
59if	[[ t != $(typeset +t) ]]
60then	err_exit tagged fails
61fi
62if	[[ $Z5 != 00123 ]]
63then	err_exit zerofill fails
64fi
65if	[[ $RZ5 != 00026 ]]
66then	err_exit right zerofill fails
67fi
68L=12345
69if	[[ $L != 123 ]]
70then	err_exit leftjust fails
71fi
72if	[[ $L5 != "def  " ]]
73then	err_exit leftjust fails
74fi
75if	[[ $uL5 != ABCDE ]]
76then	err_exit leftjust uppercase fails
77fi
78if	[[ $lR5 != bcdef ]]
79then	err_exit rightjust fails
80fi
81if	[[ $R5 != "  def" ]]
82then	err_exit rightjust fails
83fi
84if	[[ $($SHELL -c 'echo $x') != export ]]
85then	err_exit export fails
86fi
87if	[[ $($SHELL -c 'xi=xi+4;echo $xi') != 24 ]]
88then	err_exit export attributes fails
89fi
90x=$(foo=abc $SHELL <<!
91	foo=bar
92	$SHELL -c  'print \$foo'
93!
94)
95if	[[ $x != bar ]]
96then	err_exit 'environment variables require re-export'
97fi
98(typeset + ) > /dev/null 2>&1 || err_exit 'typeset + not working'
99(typeset -L-5 buf="A" 2>/dev/null)
100if [[ $? == 0 ]]
101then	err_exit 'typeset allows negative field for left/right adjust'
102fi
103a=b
104readonly $a=foo
105if	[[ $b != foo ]]
106then	err_exit 'readonly $a=b not working'
107fi
108if	[[ $(export | grep '^PATH=') != PATH=* ]]
109then	err_exit 'export not working'
110fi
111picture=(
112	bitmap=/fruit
113	size=(typeset -E x=2.5)
114)
115string="$(print $picture)"
116if [[ "${string}" != *'size=( typeset -E'* ]]
117then	err_exit 'print of compound exponential variable not working'
118fi
119sz=(typeset -E y=2.2)
120string="$(print $sz)"
121if [[ "${sz}" == *'typeset -E -F'* ]]
122then 	err_exit 'print of exponential shows both -E and -F attributes'
123fi
124print 'typeset -i m=48/4+1;print -- $m' > $tmp/script
125chmod +x $tmp/script
126typeset -Z2 m
127if	[[ $($tmp/script) != 13 ]]
128then	err_exit 'attributes not cleared for script execution'
129fi
130print 'print VAR=$VAR' > $tmp/script
131typeset -L70 VAR=var
132$tmp/script > $tmp/script.1
133[[ $(< $tmp/script.1) == VAR= ]] || err_exit 'typeset -L should not be inherited'
134typeset -Z  LAST=00
135unset -f foo
136function foo
137{
138        if [[ $1 ]]
139        then    LAST=$1
140        else    ((LAST++))
141        fi
142}
143foo 1
144if	(( ${#LAST} != 2 ))
145then	err_exit 'LAST!=2'
146fi
147foo
148if	(( ${#LAST} != 2 ))
149then	err_exit 'LAST!=2'
150fi
151[[ $(set | grep LAST) == LAST=02 ]] || err_exit "LAST not correct in set list"
152set -a
153unset foo
154foo=bar
155if	[[ $(export | grep ^foo=) != 'foo=bar' ]]
156then	err_exit 'all export not working'
157fi
158unset foo
159read foo <<!
160bar
161!
162if	[[ $(export | grep ^foo=) != 'foo=bar' ]]
163then	err_exit 'all export not working with read'
164fi
165if	[[ $(typeset | grep PS2) == PS2 ]]
166then	err_exit 'typeset without arguments outputs names without attributes'
167fi
168unset a z q x
169w1=hello
170w2=world
171t1="$w1 $w2"
172if	(( 'a' == 97 ))
173then	b1=aGVsbG8gd29ybGQ=
174	b2=aGVsbG8gd29ybGRoZWxsbyB3b3JsZA==
175else	b1=iIWTk5ZAppaZk4Q=
176	b2=iIWTk5ZAppaZk4SIhZOTlkCmlpmThA==
177fi
178z=$b1
179typeset -b x=$b1
180[[ $x == "$z" ]] || print -u2 'binary variable not expanding correctly'
181[[  $(printf "%B" x) == $t1 ]] || err_exit 'typeset -b not working'
182typeset -b -Z5 a=$b1
183[[  $(printf "%B" a) == $w1 ]] || err_exit 'typeset -b -Z5 not working'
184typeset -b q=$x$x
185[[ $q == $b2 ]] || err_exit 'typeset -b not working with concatination'
186[[  $(printf "%B" q) == $t1$t1 ]] || err_exit 'typeset -b concatination not working'
187x+=$b1
188[[ $x == $b2 ]] || err_exit 'typeset -b not working with append'
189[[  $(printf "%B" x) == $t1$t1 ]] || err_exit 'typeset -b append not working'
190typeset -b -Z20 z=$b1
191(( $(printf "%B" z | wc -c) == 20 )) || err_exit 'typeset -b -Z20 not storing 20 bytes'
192{
193	typeset -b v1 v2
194	read -N11 v1
195	read -N22 v2
196} << !
197hello worldhello worldhello world
198!
199[[ $v1 == "$b1" ]] || err_exit "v1=$v1 should be $b1"
200[[ $v2 == "$x" ]] || err_exit "v1=$v2 should be $x"
201if	env '!=1' >/dev/null 2>&1
202then	[[ $(env '!=1' $SHELL -c 'echo ok' 2>/dev/null) == ok ]] || err_exit 'malformed environment terminates shell'
203fi
204unset var
205typeset -b var
206printf '12%Z34' | read -r -N 5 var
207[[ $var == MTIAMzQ= ]] || err_exit 'binary files with zeros not working'
208unset var
209if	command typeset -usi var=0xfffff 2> /dev/null
210then	(( $var == 0xffff )) || err_exit 'unsigned short integers not working'
211else	err_exit 'typeset -usi cannot be used for unsigned short'
212fi
213[[ $($SHELL -c 'unset foo;typeset -Z2 foo; print ${foo:-3}' 2> /dev/null) == 3 ]]  || err_exit  '${foo:-3} not 3 when typeset -Z2 field undefined'
214[[ $($SHELL -c 'unset foo;typeset -Z2 foo; print ${foo:=3}' 2> /dev/null) == 03 ]]  || err_exit  '${foo:=-3} not 3 when typeset -Z2 foo undefined'
215unset foo bar
216unset -f fun
217function fun
218{
219	export foo=hello
220	typeset -x  bar=world
221	[[ $foo == hello ]] || err_exit 'export scoping problem in function'
222}
223fun
224[[ $(export | grep foo) == 'foo=hello' ]] || err_exit 'export not working in functions'
225[[ $(export | grep bar) ]] && err_exit 'typeset -x not local'
226[[ $($SHELL -c 'typeset -r IFS=;print -r $(pwd)' 2> /dev/null) == "$(pwd)" ]] || err_exit 'readonly IFS causes command substitution to fail'
227fred[66]=88
228[[ $(typeset -pa) == *fred* ]] || err_exit 'typeset -pa not working'
229unset x y z
230typeset -LZ3 x=abcd y z=00abcd
231y=03
232[[ $y == "3  " ]] || err_exit '-LZ3 not working for value 03'
233[[ $x == "abc" ]] || err_exit '-LZ3 not working for value abcd'
234[[ $x == "abc" ]] || err_exit '-LZ3 not working for value 00abcd'
235unset x z
236set +a
237[[ $(typeset -p z) ]] && err_exit "typeset -p for z undefined failed"
238unset z
239x='typeset -i z=45'
240eval "$x"
241[[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed"
242[[ $(typeset +p z) == "${x%=*}" ]] || err_exit "typeset +p for '$x' failed"
243unset z
244x='typeset -a z=(a b c)'
245eval "$x"
246[[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed"
247[[ $(typeset +p z) == "${x%=*}" ]] || err_exit "typeset +p for '$x' failed"
248unset z
249x='typeset -C z=(
250	foo=bar
251	xxx=bam
252)'
253eval "$x"
254x=${x//$'\t'}
255x=${x//$'(\n'/'('}
256x=${x//$'\n'/';'}
257x=${x%';)'}')'
258[[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed"
259[[ $(typeset +p z) == "${x%%=*}" ]] || err_exit "typeset +p for '$x' failed"
260unset z
261x='typeset -A z=([bar]=bam [xyz]=bar)'
262eval "$x"
263[[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed"
264[[ $(typeset +p z) == "${x%%=*}" ]] || err_exit "typeset +p for '$x' failed"
265unset z
266foo=abc
267x='typeset -n z=foo'
268eval "$x"
269[[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed"
270[[ $(typeset +p z) == "${x%%=*}" ]] || err_exit "typeset +p for '$x' failed"
271typeset +n z
272unset foo z
273typeset -T Pt_t=(
274	float x=1 y=2
275)
276Pt_t z
277x=${z//$'\t'}
278x=${x//$'(\n'/'('}
279x=${x//$'\n'/';'}
280x=${x%';)'}')'
281[[ $(typeset -p z) == "Pt_t z=$x" ]] || err_exit "typeset -p for type failed"
282[[ $(typeset +p z) == "Pt_t z" ]] || err_exit "typeset +p for type failed"
283unset z
284function foo
285{
286	typeset -p bar
287}
288bar=xxx
289[[ $(foo) == bar=xxx ]] || err_exit 'typeset -p not working inside a function'
290unset foo
291typeset -L5 foo
292[[ $(typeset -p foo) == 'typeset -L 5 foo' ]] || err_exit 'typeset -p not working for variables with attributes but without a value'
293{ $SHELL  <<- EOF
294	typeset -L3 foo=aaa
295	typeset -L6 foo=bbbbbb
296	[[ \$foo == bbbbbb ]]
297EOF
298}  || err_exit 'typeset -L should not preserve old attributes'
299{ $SHELL <<- EOF
300	typeset -R3 foo=aaa
301	typeset -R6 foo=bbbbbb
302	[[ \$foo == bbbbbb ]]
303EOF
304} 2> /dev/null || err_exit 'typeset -R should not preserve old attributes'
305
306expected='YWJjZGVmZ2hpag=='
307unset foo
308typeset -b -Z10 foo
309read foo <<< 'abcdefghijklmnop'
310[[ $foo == "$expected" ]] || err_exit 'read foo, where foo is "typeset -b -Z10" not working'
311unset foo
312typeset -b -Z10 foo
313read -N10 foo <<< 'abcdefghijklmnop'
314[[ $foo == "$expected" ]] || err_exit 'read -N10 foo, where foo is "typeset -b -Z10" not working'
315unset foo
316typeset  -b -A foo
317read -N10 foo[4] <<< 'abcdefghijklmnop'
318[[ ${foo[4]} == "$expected" ]] || err_exit 'read -N10 foo, where foo is "typeset  -b -A" foo not working'
319unset foo
320typeset  -b -a foo
321read -N10 foo[4] <<< 'abcdefghijklmnop'
322[[ ${foo[4]} == "$expected" ]] || err_exit 'read -N10 foo, where foo is "typeset  -b -a" foo not working'
323[[ $(printf %B foo[4]) == abcdefghij ]] || err_exit 'printf %B for binary associative array element not working'
324[[ $(printf %B foo[4]) == abcdefghij ]] || err_exit 'printf %B for binary indexed array element not working'
325unset foo
326
327$SHELL 2> /dev/null -c 'export foo=(bar=3)' && err_exit 'compound variables cannot be exported'
328
329$SHELL -c 'builtin date' >/dev/null 2>&1 &&
330{
331
332# check env var changes against a builtin that uses the env var
333
334SEC=1234252800
335ETZ=EST5EDT
336EDT=03
337PTZ=PST8PDT
338PDT=00
339
340CMD="date -f%H \\#$SEC"
341
342export TZ=$ETZ
343
344set -- \
345	"$EDT $PDT $EDT"	""		"TZ=$PTZ"	"" \
346	"$EDT $PDT $EDT"	""		"TZ=$PTZ"	"TZ=$ETZ" \
347	"$EDT $PDT $EDT"	"TZ=$ETZ"	"TZ=$PTZ"	"TZ=$ETZ" \
348	"$PDT $EDT $PDT"	"TZ=$PTZ"	""		"TZ=$PTZ" \
349	"$PDT $EDT $PDT"	"TZ=$PTZ"	"TZ=$ETZ"	"TZ=$PTZ" \
350	"$EDT $PDT $EDT"	"foo=bar"	"TZ=$PTZ"	"TZ=$ETZ" \
351
352while	(( $# >= 4 ))
353do	exp=$1
354	got=$(print $($SHELL -c "builtin date; $2 $CMD; $3 $CMD; $4 $CMD"))
355	[[ $got == $exp ]] || err_exit "[ '$2'  '$3'  '$4' ] env sequence failed -- expected '$exp', got '$got'"
356	shift 4
357done
358
359}
360
361unset v
362typeset -H v=/dev/null
363[[ $v == *nul* ]] || err_exit 'typeset -H for /dev/null not working'
364
365unset x
366(typeset +C x) 2> /dev/null && err_exit 'typeset +C should be an error'
367(typeset +A x) 2> /dev/null && err_exit 'typeset +A should be an error'
368(typeset +a x) 2> /dev/null && err_exit 'typeset +a should be an error'
369
370unset x
371{
372x=$($SHELL -c 'integer -s x=5;print -r -- $x')
373} 2> /dev/null
374[[ $x == 5 ]] || err_exit 'integer -s not working'
375
376[[ $(typeset -l) == *namespace*.sh* ]] && err_exit 'typeset -l should not contain namespace .sh'
377
378unset got
379typeset -u got
380exp=100
381((got=$exp))
382[[ $got == $exp ]] || err_exit "typeset -l fails on numeric value -- expected '$exp', got '$got'"
383unset got
384
385unset s
386typeset -a -u s=( hello world chicken )
387[[ ${s[2]} == CHICKEN ]] || err_exit 'typeset -u not working with indexed arrays'
388unset s
389typeset -A -u s=( [1]=hello [0]=world [2]=chicken )
390[[ ${s[2]} == CHICKEN ]] || err_exit 'typeset -u not working with associative arrays'
391expected=$'(\n\t[0]=WORLD\n\t[1]=HELLO\n\t[2]=CHICKEN\n)'
392[[ $(print -v s) == "$expected" ]] || err_exit 'typeset -u for associative array does not display correctly'
393
394unset s
395if	command typeset -M totitle s 2> /dev/null
396then	[[ $(typeset +p s) == 'typeset -M totitle s' ]] || err_exit 'typeset -M totitle does not display correctly with typeset -p'
397fi
398
399{ $SHELL  <<-  \EOF
400	compound -a a1
401	for ((i=1 ; i < 100 ; i++ ))
402        do	[[ "$( typeset + a1[$i] )" == '' ]] && a1[$i].text='hello'
403	done
404	[[ ${a1[70].text} == hello ]]
405EOF
406} 2> /dev/null
407(( $? )) && err_exit  'typeset + a[i] not working'
408
409typeset groupDB="" userDB=""
410typeset -l -L1 DBPick=""
411[[ -n "$groupDB" ]]  && err_exit 'typeset -l -L1 causes unwanted side effect'
412
413HISTFILE=foo
414typeset -u PS1='hello --- '
415HISTFILE=foo
416[[ $HISTFILE == foo ]] || err_exit  'typeset -u PS1 affects HISTFILE'
417
418typeset -a a=( aA= ZQ= bA= bA= bw= Cg= )
419typeset -b x
420for 	(( i=0 ; i < ${#a[@]} ; i++ ))
421do 	x+="${a[i]}"
422done
423[[ $(printf "%B" x) == hello ]] || err_exit "append for typeset -b not working: got '$(printf "%B" x)' should get hello"
424
425(
426	trap 'exit $?' EXIT
427	$SHELL -c 'typeset v=foo; [[ $(typeset -p v[0]) == v=foo ]]'
428) 2> /dev/null || err_exit 'typeset -p v[0] not working for simple variable v'
429
430unset x
431expected='typeset -a x=(a\=3 b\=4)'
432typeset -a x=( a=3 b=4)
433[[ $(typeset -p x) == "$expected" ]] || err_exit 'assignment elements in typeset -a assignment not working'
434
435unset z
436z='typeset -a q=(a b c)'
437$SHELL -c "$z; [[ \$(typeset -pa) == '$z' ]]" || err_exit 'typeset -pa does not list only index arrays'
438z='typeset -C z=(foo=bar)'
439$SHELL -c "$z; [[ \$(typeset -pC) == '$z' ]]" || err_exit 'typeset -pC does not list only compound variables'
440unset y
441z='typeset -A y=([a]=foo)'
442$SHELL -c "$z; [[ \$(typeset -pA) == '$z' ]]" || err_exit 'typeset -pA does not list only associative arrays'
443
444$SHELL 2> /dev/null -c 'typeset -C arr=( aa bb cc dd )' && err_exit 'invalid compound variable assignment not reported'
445
446unset x
447typeset -l x=
448[[ ${x:=foo} == foo ]] || err_exit '${x:=foo} with x unset, not foo when x is a lowercase variable'
449
450unset x
451typeset -L4 x=$'\001abcdef'
452[[ ${#x} == 5 ]] || err_exit "width of character '\01' is not zero"
453
454unset x
455typeset -L x=-1
456command typeset -F x=0-1 2> /dev/null || err_exit 'typeset -F after typeset -L fails'
457
458unset val
459typeset -i val=10#0-3
460typeset -Z val=0-1
461[[ $val == 0-1 ]] || err_exit 'integer attribute not cleared for subsequent typeset'
462
463unset x
464typeset -L -Z x=foo
465[[ $(typeset -p x) == 'typeset -Z 3 -L 3 x=foo' ]] || err_exit '-LRZ without [n] not defaulting to width of variable'
466
467unset foo
468typeset -Z2 foo=3
469[[ $(typeset -p foo) == 'typeset -Z 2 -R 2 foo=03' ]] || err_exit '-Z2  not working'
470export foo
471[[ $(typeset -p foo) == 'typeset -x -Z 2 -R 2 foo=03' ]] || err_exit '-Z2  not working after export'
472
473exit $((Errors<125?Errors:125))
474