xref: /titanic_50/usr/src/lib/libshell/common/tests/builtins.sh (revision 3e14f97f673e8a630f076077de35afdd43dc1587)
1da2e3ebdSchin########################################################################
2da2e3ebdSchin#                                                                      #
3da2e3ebdSchin#               This software is part of the ast package               #
4*3e14f97fSRoger A. Faulkner#          Copyright (c) 1982-2010 AT&T Intellectual Property          #
5da2e3ebdSchin#                      and is licensed under the                       #
6da2e3ebdSchin#                  Common Public License, Version 1.0                  #
77c2fbfb3SApril Chin#                    by AT&T Intellectual Property                     #
8da2e3ebdSchin#                                                                      #
9da2e3ebdSchin#                A copy of the License is available at                 #
10da2e3ebdSchin#            http://www.opensource.org/licenses/cpl1.0.txt             #
11da2e3ebdSchin#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
12da2e3ebdSchin#                                                                      #
13da2e3ebdSchin#              Information and Software Systems Research               #
14da2e3ebdSchin#                            AT&T Research                             #
15da2e3ebdSchin#                           Florham Park NJ                            #
16da2e3ebdSchin#                                                                      #
17da2e3ebdSchin#                  David Korn <dgk@research.att.com>                   #
18da2e3ebdSchin#                                                                      #
19da2e3ebdSchin########################################################################
20da2e3ebdSchinfunction err_exit
21da2e3ebdSchin{
22da2e3ebdSchin	print -u2 -n "\t"
23da2e3ebdSchin	print -u2 -r ${Command}[$1]: "${@:2}"
24da2e3ebdSchin	let Errors+=1
25da2e3ebdSchin}
26da2e3ebdSchinalias err_exit='err_exit $LINENO'
27da2e3ebdSchin
28da2e3ebdSchinCommand=${0##*/}
29da2e3ebdSchininteger Errors=0
3034f9b3eeSRoland Mainz
3134f9b3eeSRoland Mainztmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
3234f9b3eeSRoland Mainztrap "cd /; rm -rf $tmp" EXIT
3334f9b3eeSRoland Mainz
3434f9b3eeSRoland Mainz# test shell builtin commands
35da2e3ebdSchinbuiltin getconf
36da2e3ebdSchin: ${foo=bar} || err_exit ": failed"
37da2e3ebdSchin[[ $foo = bar ]] || err_exit ": side effects failed"
38da2e3ebdSchinset -- - foobar
39da2e3ebdSchin[[ $# = 2 && $1 = - && $2 = foobar ]] || err_exit "set -- - foobar failed"
40da2e3ebdSchinset -- -x foobar
41da2e3ebdSchin[[ $# = 2 && $1 = -x && $2 = foobar ]] || err_exit "set -- -x foobar failed"
42da2e3ebdSchingetopts :x: foo || err_exit "getopts :x: returns false"
43da2e3ebdSchin[[ $foo = x && $OPTARG = foobar ]] || err_exit "getopts :x: failed"
44da2e3ebdSchinOPTIND=1
45da2e3ebdSchingetopts :r:s var -r
46da2e3ebdSchinif	[[ $var != : || $OPTARG != r ]]
47da2e3ebdSchinthen	err_exit "'getopts :r:s var -r' not working"
48da2e3ebdSchinfi
49da2e3ebdSchinOPTIND=1
507c2fbfb3SApril Chingetopts :d#u OPT -d 16177
517c2fbfb3SApril Chinif	[[ $OPT != d || $OPTARG != 16177 ]]
527c2fbfb3SApril Chinthen	err_exit "'getopts :d#u OPT=d OPTARG=16177' failed -- OPT=$OPT OPTARG=$OPTARG"
53da2e3ebdSchinfi
54da2e3ebdSchinOPTIND=1
55da2e3ebdSchinwhile getopts 'ab' option -a -b
56da2e3ebdSchindo	[[ $OPTIND == $((OPTIND)) ]] || err_exit "OPTIND optimization bug"
57da2e3ebdSchindone
58da2e3ebdSchin
59da2e3ebdSchinUSAGE=$'[-][S:server?Operate on the specified \asubservice\a:]:[subservice:=pmserver]
60da2e3ebdSchin    {
61da2e3ebdSchin        [p:pmserver]
62da2e3ebdSchin        [r:repserver]
63da2e3ebdSchin        [11:notifyd]
64da2e3ebdSchin    }'
65da2e3ebdSchinset pmser p rep r notifyd -11
66da2e3ebdSchinwhile	(( $# > 1 ))
67da2e3ebdSchindo	OPTIND=1
68da2e3ebdSchin	getopts "$USAGE" OPT -S $1
69da2e3ebdSchin	[[ $OPT == S && $OPTARG == $2 ]] || err_exit "OPT=$OPT OPTARG=$OPTARG -- expected OPT=S OPTARG=$2"
70da2e3ebdSchin	shift 2
71da2e3ebdSchindone
72da2e3ebdSchin
73da2e3ebdSchinfalse ${foo=bar} &&  err_exit "false failed"
74da2e3ebdSchinread <<!
75da2e3ebdSchinhello world
76da2e3ebdSchin!
77da2e3ebdSchin[[ $REPLY = 'hello world' ]] || err_exit "read builtin failed"
78da2e3ebdSchinprint x:y | IFS=: read a b
79da2e3ebdSchinif	[[ $a != x ]]
80da2e3ebdSchinthen	err_exit "IFS=: read ... not working"
81da2e3ebdSchinfi
82da2e3ebdSchinread <<!
83da2e3ebdSchinhello \
84da2e3ebdSchinworld
85da2e3ebdSchin!
86da2e3ebdSchin[[ $REPLY = 'hello world' ]] || err_exit "read continuation failed"
87da2e3ebdSchinread -d x <<!
88da2e3ebdSchinhello worldxfoobar
89da2e3ebdSchin!
90da2e3ebdSchin[[ $REPLY = 'hello world' ]] || err_exit "read builtin failed"
91da2e3ebdSchinread <<\!
92da2e3ebdSchinhello \
93da2e3ebdSchin	world \
94da2e3ebdSchin
95da2e3ebdSchin!
96da2e3ebdSchin[[ $REPLY == 'hello 	world' ]] || err_exit "read continuation2 failed"
97da2e3ebdSchinprint "one\ntwo" | { read line
98da2e3ebdSchin	print $line | /bin/cat > /dev/null
99da2e3ebdSchin	read line
100da2e3ebdSchin}
101da2e3ebdSchinread <<\!
102da2e3ebdSchin\
103da2e3ebdSchina\
104da2e3ebdSchin\
105da2e3ebdSchin\
106da2e3ebdSchinb
107da2e3ebdSchin!
108da2e3ebdSchinif	[[ $REPLY != ab ]]
109da2e3ebdSchinthen	err_exit "read multiple continuation failed"
110da2e3ebdSchinfi
111da2e3ebdSchinif	[[ $line != two ]]
112da2e3ebdSchinthen	err_exit "read from pipeline failed"
113da2e3ebdSchinfi
114da2e3ebdSchinline=two
115da2e3ebdSchinread line < /dev/null
116da2e3ebdSchinif	[[ $line != "" ]]
117da2e3ebdSchinthen	err_exit "read from /dev/null failed"
118da2e3ebdSchinfi
119da2e3ebdSchinif	[[ $(print -R -) != - ]]
120da2e3ebdSchinthen	err_exit "print -R not working correctly"
121da2e3ebdSchinfi
122da2e3ebdSchinif	[[ $(print -- -) != - ]]
123da2e3ebdSchinthen	err_exit "print -- not working correctly"
124da2e3ebdSchinfi
125da2e3ebdSchinprint -f "hello%nbar\n" size > /dev/null
126da2e3ebdSchinif	((	size != 5 ))
127da2e3ebdSchinthen	err_exit "%n format of printf not working"
128da2e3ebdSchinfi
129da2e3ebdSchinprint -n -u2 2>&1-
130da2e3ebdSchin[[ -w /dev/fd/1 ]] || err_exit "2<&1- with built-ins has side effects"
131da2e3ebdSchinx=$0
132da2e3ebdSchinif	[[ $(eval 'print $0') != $x ]]
133da2e3ebdSchinthen	err_exit '$0 not correct for eval'
134da2e3ebdSchinfi
1357c2fbfb3SApril Chin$SHELL -c 'read x <<< hello' 2> /dev/null || err_exit 'syntax <<< not recognized'
1367c2fbfb3SApril Chin($SHELL -c 'read x[1] <<< hello') 2> /dev/null || err_exit 'read x[1] not working'
137da2e3ebdSchinunset x
138da2e3ebdSchinreadonly x
139da2e3ebdSchinset -- $(readonly)
140da2e3ebdSchinif      [[ " $@ " != *" x "* ]]
141da2e3ebdSchinthen    err_exit 'unset readonly variables are not displayed'
142da2e3ebdSchinfi
143da2e3ebdSchinif	[[ $(	for i in foo bar
144da2e3ebdSchin		do	print $i
145da2e3ebdSchin			continue 10
146da2e3ebdSchin		done
147da2e3ebdSchin	    ) != $'foo\nbar' ]]
148da2e3ebdSchinthen	err_exit 'continue breaks out of loop'
149da2e3ebdSchinfi
150da2e3ebdSchin(continue bad 2>/dev/null && err_exit 'continue bad should return an error')
151da2e3ebdSchin(break bad 2>/dev/null && err_exit 'break bad should return an error')
152da2e3ebdSchin(continue 0 2>/dev/null && err_exit 'continue 0 should return an error')
153da2e3ebdSchin(break 0 2>/dev/null && err_exit 'break 0 should return an error')
154da2e3ebdSchinbreakfun() { break;}
155da2e3ebdSchincontinuefun() { continue;}
156da2e3ebdSchinfor fun in break continue
157da2e3ebdSchindo	if	[[ $(	for i in foo
158da2e3ebdSchin			do	${fun}fun
159da2e3ebdSchin				print $i
160da2e3ebdSchin			done
161da2e3ebdSchin		) != foo ]]
162da2e3ebdSchin	then	err_exit "$fun call in ${fun}fun breaks out of for loop"
163da2e3ebdSchin	fi
164da2e3ebdSchindone
165da2e3ebdSchinif	[[ $(print -f "%b" "\a\n\v\b\r\f\E\03\\oo") != $'\a\n\v\b\r\f\E\03\\oo' ]]
166da2e3ebdSchinthen	err_exit 'print -f "%b" not working'
167da2e3ebdSchinfi
1687c2fbfb3SApril Chinif	[[ $(print -f "%P" "[^x].*b\$") != '*[!x]*b' ]]
169da2e3ebdSchinthen	err_exit 'print -f "%P" not working'
170da2e3ebdSchinfi
171da2e3ebdSchinif	[[ $(abc: for i in foo bar;do print $i;break abc;done) != foo ]]
172da2e3ebdSchinthen	err_exit 'break labels not working'
173da2e3ebdSchinfi
174da2e3ebdSchinif	[[ $(command -v if)	!= if ]]
175da2e3ebdSchinthen	err_exit	'command -v not working'
176da2e3ebdSchinfi
177da2e3ebdSchinread -r var <<\!
178da2e3ebdSchin
179da2e3ebdSchin!
180da2e3ebdSchinif	[[ $var != "" ]]
181da2e3ebdSchinthen	err_exit "read -r of blank line not working"
182da2e3ebdSchinfi
18334f9b3eeSRoland Mainzmkdir -p $tmp/a/b/c 2>/dev/null || err_exit  "mkdir -p failed"
18434f9b3eeSRoland Mainz$SHELL -c "cd $tmp/a/b; cd c" 2>/dev/null || err_exit "initial script relative cd fails"
18534f9b3eeSRoland Mainz
18634f9b3eeSRoland Mainztrap 'print TERM' TERM
18734f9b3eeSRoland Mainzexp=$'trap -- \'print TERM\' TERM\ntrap -- \'cd /; rm -rf '$tmp$'\' EXIT'
18834f9b3eeSRoland Mainzgot=$(trap)
18934f9b3eeSRoland Mainz[[ $got == $exp ]] || err_exit "\$(trap) failed -- expected \"$exp\", got \"$got\""
19034f9b3eeSRoland Mainzexp='print TERM'
19134f9b3eeSRoland Mainzgot=$(trap -p TERM)
19234f9b3eeSRoland Mainz[[ $got == $exp ]] || err_exit "\$(trap -p TERM) failed -- expected \"$exp\", got \"$got\""
19334f9b3eeSRoland Mainz
1947c2fbfb3SApril Chin[[ $($SHELL -c 'trap "print ok" SIGTERM; kill -s SIGTERM $$' 2> /dev/null) == ok ]] || err_exit 'SIGTERM not recognized'
1957c2fbfb3SApril Chin[[ $($SHELL -c 'trap "print ok" sigterm; kill -s sigterm $$' 2> /dev/null) == ok ]] || err_exit 'SIGTERM not recognized'
1967c2fbfb3SApril Chin[[ $($SHELL -c '( trap "" TERM);kill $$;print bad' == bad) ]] 2> /dev/null && err_exit 'trap ignored in subshell causes it to be ignored by parent'
197da2e3ebdSchin${SHELL} -c 'kill -1 -$$' 2> /dev/null
198da2e3ebdSchin[[ $(kill -l $?) == HUP ]] || err_exit 'kill -1 -pid not working'
199da2e3ebdSchin${SHELL} -c 'kill -1 -$$' 2> /dev/null
200da2e3ebdSchin[[ $(kill -l $?) == HUP ]] || err_exit 'kill -n1 -pid not working'
201da2e3ebdSchin${SHELL} -c 'kill -s HUP -$$' 2> /dev/null
202da2e3ebdSchin[[ $(kill -l $?) == HUP ]] || err_exit 'kill -HUP -pid not working'
203da2e3ebdSchinn=123
204da2e3ebdSchintypeset -A base
205da2e3ebdSchinbase[o]=8#
206da2e3ebdSchinbase[x]=16#
207da2e3ebdSchinbase[X]=16#
208da2e3ebdSchinfor i in d i o u x X
209da2e3ebdSchindo	if	(( $(( ${base[$i]}$(printf "%$i" $n) )) != n  ))
210da2e3ebdSchin	then	err_exit "printf %$i not working"
211da2e3ebdSchin	fi
212da2e3ebdSchindone
213da2e3ebdSchinif	[[ $( trap 'print done' EXIT) != done ]]
214da2e3ebdSchinthen	err_exit 'trap on EXIT not working'
215da2e3ebdSchinfi
216da2e3ebdSchinif	[[ $( trap 'print done' EXIT; trap - EXIT) == done ]]
217da2e3ebdSchinthen	err_exit 'trap on EXIT not being cleared'
218da2e3ebdSchinfi
219da2e3ebdSchinif	[[ $(type test) != 'test is a shell builtin' ]]
220da2e3ebdSchinthen	err_exit 'whence -v test not a builtin'
221da2e3ebdSchinfi
222da2e3ebdSchinbuiltin -d test
223da2e3ebdSchinif	[[ $(type test) == *builtin* ]]
224da2e3ebdSchinthen	err_exit 'whence -v test after builtin -d incorrect'
225da2e3ebdSchinfi
226da2e3ebdSchintypeset -Z3 percent=$(printf '%o\n' "'%'")
227da2e3ebdSchinforrmat=\\${percent}s
228da2e3ebdSchinif      [[ $(printf "$forrmat") != %s ]]
229da2e3ebdSchinthen    err_exit "printf $forrmat not working"
230da2e3ebdSchinfi
231da2e3ebdSchinif	(( $(printf 'x\0y' | wc -c) != 3 ))
232da2e3ebdSchinthen	err_exit 'printf \0 not working'
233da2e3ebdSchinfi
234da2e3ebdSchinif	[[ $(printf "%bx%s\n" 'f\to\cbar') != $'f\to' ]]
235da2e3ebdSchinthen	err_exit 'printf %bx%s\n  not working'
236da2e3ebdSchinfi
237da2e3ebdSchinalpha=abcdefghijklmnop
238da2e3ebdSchinif	[[ $(printf "%10.*s\n" 5 $alpha) != '     abcde' ]]
239da2e3ebdSchinthen	err_exit 'printf %10.%s\n  not working'
240da2e3ebdSchinfi
241da2e3ebdSchinfloat x2=.0000625
242da2e3ebdSchinif	[[ $(printf "%10.5E\n" x2) != 6.25000E-05 ]]
243da2e3ebdSchinthen	err_exit 'printf "%10.5E" not normalizing correctly'
244da2e3ebdSchinfi
245da2e3ebdSchinx2=.000000001
246da2e3ebdSchinif	[[ $(printf "%g\n" x2 2>/dev/null) != 1e-09 ]]
247da2e3ebdSchinthen	err_exit 'printf "%g" not working correctly'
248da2e3ebdSchinfi
249da2e3ebdSchin#FIXME#($SHELL read -s foobar <<\!
250da2e3ebdSchin#FIXME#testing
251da2e3ebdSchin#FIXME#!
252da2e3ebdSchin#FIXME#) 2> /dev/null || err_exit ksh read -s var fails
253da2e3ebdSchinif	[[ $(printf +3 2>/dev/null) !=   +3 ]]
254da2e3ebdSchinthen	err_exit 'printf is not processing formats beginning with + correctly'
255da2e3ebdSchinfi
256da2e3ebdSchinif	printf "%d %d\n" 123bad 78 >/dev/null 2>/dev/null
257da2e3ebdSchinthen	err_exit "printf not exiting non-zero with conversion errors"
258da2e3ebdSchinfi
259da2e3ebdSchinif	[[ $(trap --version 2> /dev/null;print done) != done ]]
260da2e3ebdSchinthen	err_exit 'trap builtin terminating after --version'
261da2e3ebdSchinfi
262da2e3ebdSchinif	[[ $(set --version 2> /dev/null;print done) != done ]]
263da2e3ebdSchinthen	err_exit 'set builtin terminating after --veresion'
264da2e3ebdSchinfi
265da2e3ebdSchinunset -f foobar
266da2e3ebdSchinfunction foobar
267da2e3ebdSchin{
268da2e3ebdSchin	print 'hello world'
269da2e3ebdSchin}
270da2e3ebdSchinOPTIND=1
271da2e3ebdSchinif	[[ $(getopts  $'[+?X\ffoobar\fX]' v --man 2>&1) != *'Xhello world'X* ]]
272da2e3ebdSchinthen	err_exit '\f...\f not working in getopts usage strings'
273da2e3ebdSchinfi
274da2e3ebdSchinif	[[ $(printf '%H\n' $'<>"& \'\tabc') != '&lt;&gt;&quot;&amp;&nbsp;&apos;&#9;abc' ]]
275da2e3ebdSchinthen	err_exit 'printf %H not working'
276da2e3ebdSchinfi
277da2e3ebdSchinif	[[ $(printf '%R %R %R %R\n' 'a.b' '*.c' '^'  '!(*.*)') != '^a\.b$ \.c$ ^\^$ ^(.*\..*)!$' ]]
278da2e3ebdSchinthen	err_exit 'printf %R not working'
279da2e3ebdSchinfi
280da2e3ebdSchinif	[[ $(printf '%..:c\n' abc) != a:b:c ]]
281da2e3ebdSchinthen	err_exit "printf '%..:c' not working"
282da2e3ebdSchinfi
283da2e3ebdSchinif	[[ $(printf '%..*c\n' : abc) != a:b:c ]]
284da2e3ebdSchinthen	err_exit "printf '%..*c' not working"
285da2e3ebdSchinfi
286da2e3ebdSchinif	[[ $(printf '%..:s\n' abc def ) != abc:def ]]
287da2e3ebdSchinthen	err_exit "printf '%..:s' not working"
288da2e3ebdSchinfi
289da2e3ebdSchinif	[[ $(printf '%..*s\n' : abc def) != abc:def ]]
290da2e3ebdSchinthen	err_exit "printf '%..*s' not working"
291da2e3ebdSchinfi
292da2e3ebdSchin[[ $(printf '%q\n') == '' ]] || err_exit 'printf "%q" with missing arguments'
293da2e3ebdSchin# we won't get hit by the one second boundary twice, right?
294da2e3ebdSchin[[ $(printf '%T\n' now) == "$(date)" ]] ||
295da2e3ebdSchin[[ $(printf '%T\n' now) == "$(date)" ]] ||
296da2e3ebdSchinerr_exit 'printf "%T" now'
297da2e3ebdSchinbehead()
298da2e3ebdSchin{
299da2e3ebdSchin	read line
300da2e3ebdSchin	left=$(cat)
301da2e3ebdSchin}
302da2e3ebdSchinprint $'line1\nline2' | behead
303da2e3ebdSchinif	[[ $left != line2 ]]
304da2e3ebdSchinthen	err_exit "read reading ahead on a pipe"
305da2e3ebdSchinfi
30634f9b3eeSRoland Mainzread -n1 y <<!
30734f9b3eeSRoland Mainzabc
30834f9b3eeSRoland Mainz!
30934f9b3eeSRoland Mainzexp=a
31034f9b3eeSRoland Mainzif      [[ $y != $exp ]]
31134f9b3eeSRoland Mainzthen    err_exit "read -n1 failed -- expected '$exp', got '$y'"
31234f9b3eeSRoland Mainzfi
31334f9b3eeSRoland Mainzprint -n $'{ read -r line;print $line;}\nhello' > $tmp/script
31434f9b3eeSRoland Mainzchmod 755 $tmp/script
31534f9b3eeSRoland Mainzif	[[ $($SHELL < $tmp/script) != hello ]]
316da2e3ebdSchinthen	err_exit 'read of incomplete line not working correctly'
317da2e3ebdSchinfi
318da2e3ebdSchinset -f
319da2e3ebdSchinset -- *
320da2e3ebdSchinif      [[ $1 != '*' ]]
321da2e3ebdSchinthen    err_exit 'set -f not working'
322da2e3ebdSchinfi
323da2e3ebdSchinunset pid1 pid2
324da2e3ebdSchinfalse &
325da2e3ebdSchinpid1=$!
326da2e3ebdSchinpid2=$(
327da2e3ebdSchin	wait $pid1
328da2e3ebdSchin	(( $? == 127 )) || err_exit "job known to subshell"
329da2e3ebdSchin	print $!
330da2e3ebdSchin)
331da2e3ebdSchinwait $pid1
332da2e3ebdSchin(( $? == 1 )) || err_exit "wait not saving exit value"
333da2e3ebdSchinwait $pid2
334da2e3ebdSchin(( $? == 127 )) || err_exit "subshell job known to parent"
335da2e3ebdSchinenv=
33634f9b3eeSRoland Mainzv=$(getconf LIBPATH)
33734f9b3eeSRoland Mainzfor v in ${v//,/ }
33834f9b3eeSRoland Mainzdo	v=${v#*:}
33934f9b3eeSRoland Mainz	v=${v%%:*}
34034f9b3eeSRoland Mainz	eval [[ \$$v ]] && env="$env $v=\"\$$v\""
341da2e3ebdSchindone
342da2e3ebdSchinif	[[ $(foo=bar; eval foo=\$foo $env exec -c \$SHELL -c \'print \$foo\') != bar ]]
343da2e3ebdSchinthen	err_exit '"name=value exec -c ..." not working'
344da2e3ebdSchinfi
345da2e3ebdSchin$SHELL -c 'OPTIND=-1000000; getopts a opt -a' 2> /dev/null
346da2e3ebdSchin[[ $? == 1 ]] || err_exit 'getopts with negative OPTIND not working'
347da2e3ebdSchingetopts 'n#num' opt  -n 3
348da2e3ebdSchin[[ $OPTARG == 3 ]] || err_exit 'getopts with numerical arguments failed'
349da2e3ebdSchinif	[[ $($SHELL -c $'printf \'%2$s %1$s\n\' world hello') != 'hello world' ]]
350da2e3ebdSchinthen	err_exit 'printf %2$s %1$s not working'
351da2e3ebdSchinfi
3527c2fbfb3SApril Chinval=$(( 'C' ))
3537c2fbfb3SApril Chinset -- \
3547c2fbfb3SApril Chin	"'C"	$val	0	\
3557c2fbfb3SApril Chin	"'C'"	$val	0	\
3567c2fbfb3SApril Chin	'"C'	$val	0	\
3577c2fbfb3SApril Chin	'"C"'	$val	0	\
3587c2fbfb3SApril Chin	"'CX"	$val	1	\
3597c2fbfb3SApril Chin	"'CX'"	$val	1	\
3607c2fbfb3SApril Chin	"'C'X"	$val	1	\
3617c2fbfb3SApril Chin	'"CX'	$val	1	\
3627c2fbfb3SApril Chin	'"CX"'	$val	1	\
3637c2fbfb3SApril Chin	'"C"X'	$val	1
3647c2fbfb3SApril Chinwhile (( $# >= 3 ))
3657c2fbfb3SApril Chindo	arg=$1 val=$2 code=$3
3667c2fbfb3SApril Chin	shift 3
3677c2fbfb3SApril Chin	for fmt in '%d' '%g'
3687c2fbfb3SApril Chin	do	out=$(printf "$fmt" "$arg" 2>/dev/null)
3697c2fbfb3SApril Chin		err=$(printf "$fmt" "$arg" 2>&1 >/dev/null)
3707c2fbfb3SApril Chin		printf "$fmt" "$arg" >/dev/null 2>&1
3717c2fbfb3SApril Chin		ret=$?
37234f9b3eeSRoland Mainz		[[ $out == $val ]] || err_exit "printf $fmt $arg failed -- expected '$val', got '$out'"
3737c2fbfb3SApril Chin		if	(( $code ))
3747c2fbfb3SApril Chin		then	[[ $err ]] || err_exit "printf $fmt $arg failed, error message expected"
3757c2fbfb3SApril Chin		else	[[ $err ]] && err_exit "$err: printf $fmt $arg failed, error message not expected -- got '$err'"
3767c2fbfb3SApril Chin		fi
3777c2fbfb3SApril Chin		(( $ret == $code )) || err_exit "printf $fmt $arg failed -- expected exit code $code, got $ret"
3787c2fbfb3SApril Chin	done
3797c2fbfb3SApril Chindone
380da2e3ebdSchin((n=0))
381da2e3ebdSchin((n++)); ARGC[$n]=1 ARGV[$n]=""
382da2e3ebdSchin((n++)); ARGC[$n]=2 ARGV[$n]="-a"
383da2e3ebdSchin((n++)); ARGC[$n]=4 ARGV[$n]="-a -v 2"
384da2e3ebdSchin((n++)); ARGC[$n]=4 ARGV[$n]="-a -v 2 x"
385da2e3ebdSchin((n++)); ARGC[$n]=4 ARGV[$n]="-a -v 2 x y"
386da2e3ebdSchinfor ((i=1; i<=n; i++))
387da2e3ebdSchindo	set -- ${ARGV[$i]}
388da2e3ebdSchin	OPTIND=0
389da2e3ebdSchin	while	getopts -a tst "av:" OPT
390da2e3ebdSchin	do	:
391da2e3ebdSchin	done
392da2e3ebdSchin	if	[[ $OPTIND != ${ARGC[$i]} ]]
3937c2fbfb3SApril Chin	then	err_exit "\$OPTIND after getopts loop incorrect -- expected ${ARGC[$i]}, got $OPTIND"
394da2e3ebdSchin	fi
395da2e3ebdSchindone
3967c2fbfb3SApril Chinoptions=ab:c
3977c2fbfb3SApril Chinoptarg=foo
3987c2fbfb3SApril Chinset -- -a -b $optarg -c bar
3997c2fbfb3SApril Chinwhile	getopts $options opt
4007c2fbfb3SApril Chindo	case $opt in
4017c2fbfb3SApril Chin	a|c)	[[ $OPTARG ]] && err_exit "getopts $options \$OPTARG for flag $opt failed, expected \"\", got \"$OPTARG\"" ;;
4027c2fbfb3SApril Chin	b)	[[ $OPTARG == $optarg ]] || err_exit "getopts $options \$OPTARG failed -- \"$optarg\" expected, got \"$OPTARG\"" ;;
4037c2fbfb3SApril Chin	*)	err_exit "getopts $options failed -- got flag $opt" ;;
4047c2fbfb3SApril Chin	esac
4057c2fbfb3SApril Chindone
40634f9b3eeSRoland Mainz
40734f9b3eeSRoland Mainzunset a
40834f9b3eeSRoland Mainz{ read -N3 a; read -N1 b;}  <<!
40934f9b3eeSRoland Mainzabcdefg
41034f9b3eeSRoland Mainz!
41134f9b3eeSRoland Mainzexp=abc
41234f9b3eeSRoland Mainz[[ $a == $exp ]] || err_exit "read -N3 here-document failed -- expected '$exp', got '$a'"
41334f9b3eeSRoland Mainzexp=d
41434f9b3eeSRoland Mainz[[ $b == $exp ]] || err_exit "read -N1 here-document failed -- expected '$exp', got '$b'"
41534f9b3eeSRoland Mainzread -n3 a <<!
41634f9b3eeSRoland Mainzabcdefg
41734f9b3eeSRoland Mainz!
41834f9b3eeSRoland Mainzexp=abc
41934f9b3eeSRoland Mainz[[ $a == $exp ]] || err_exit "read -n3 here-document failed -- expected '$exp', got '$a'"
42034f9b3eeSRoland Mainz#(print -n a;sleep 1; print -n bcde) | { read -N3 a; read -N1 b;}
42134f9b3eeSRoland Mainz#[[ $a == $exp ]] || err_exit "read -N3 from pipe failed -- expected '$exp', got '$a'"
42234f9b3eeSRoland Mainz#exp=d
42334f9b3eeSRoland Mainz#[[ $b == $exp ]] || err_exit "read -N1 from pipe failed -- expected '$exp', got '$b'"
42434f9b3eeSRoland Mainz#(print -n a;sleep 1; print -n bcde) | read -n3 a
42534f9b3eeSRoland Mainz#exp=a
42634f9b3eeSRoland Mainz#[[ $a == $exp ]] || err_exit "read -n3 from pipe failed -- expected '$exp', got '$a'"
42734f9b3eeSRoland Mainz#rm -f $tmp/fifo
42834f9b3eeSRoland Mainz#if	mkfifo $tmp/fifo 2> /dev/null
42934f9b3eeSRoland Mainz#then	(print -n a; sleep 1;print -n bcde)  > $tmp/fifo &
43034f9b3eeSRoland Mainz#	{
43134f9b3eeSRoland Mainz#	read -u5 -n3 -t2 a || err_exit 'read -n3 from fifo timedout'
43234f9b3eeSRoland Mainz#	read -u5 -n1 -t2 b || err_exit 'read -n1 from fifo timedout'
43334f9b3eeSRoland Mainz#	} 5< $tmp/fifo
43434f9b3eeSRoland Mainz#	exp=a
43534f9b3eeSRoland Mainz#	[[ $a == $exp ]] || err_exit "read -n3 from fifo failed -- expected '$exp', got '$a'"
43634f9b3eeSRoland Mainz#	rm -f $tmp/fifo
43734f9b3eeSRoland Mainz#	mkfifo $tmp/fifo 2> /dev/null
43834f9b3eeSRoland Mainz#	(print -n a; sleep 1;print -n bcde) > $tmp/fifo &
43934f9b3eeSRoland Mainz#	{
44034f9b3eeSRoland Mainz#	read -u5 -N3 -t2 a || err_exit 'read -N3 from fifo timed out'
44134f9b3eeSRoland Mainz#	read -u5 -N1 -t2 b || err_exit 'read -N1 from fifo timedout'
44234f9b3eeSRoland Mainz#	} 5< $tmp/fifo
44334f9b3eeSRoland Mainz#	exp=abc
44434f9b3eeSRoland Mainz#	[[ $a == $exp ]] || err_exit "read -N3 from fifo failed -- expected '$exp', got '$a'"
44534f9b3eeSRoland Mainz#	exp=d
44634f9b3eeSRoland Mainz#	[[ $b == $exp ]] || err_exit "read -N1 from fifo failed -- expected '$exp', got '$b'"
44734f9b3eeSRoland Mainz#fi
44834f9b3eeSRoland Mainz#rm -f $tmp/fifo
44934f9b3eeSRoland Mainz
450da2e3ebdSchinfunction longline
451da2e3ebdSchin{
452da2e3ebdSchin	integer i
453da2e3ebdSchin	for((i=0; i < $1; i++))
454da2e3ebdSchin	do	print argument$i
455da2e3ebdSchin	done
456da2e3ebdSchin}
457da2e3ebdSchin# test command -x option
458da2e3ebdSchininteger sum=0 n=10000
459da2e3ebdSchinif	! ${SHELL:-ksh} -c 'print $#' count $(longline $n) > /dev/null  2>&1
460da2e3ebdSchinthen	for i in $(command command -x ${SHELL:-ksh} -c 'print $#;[[ $1 != argument0 ]]' count $(longline $n) 2> /dev/null)
461da2e3ebdSchin	do	((sum += $i))
462da2e3ebdSchin	done
463da2e3ebdSchin	(( sum == n )) || err_exit "command -x processed only $sum arguments"
464da2e3ebdSchin	command -p command -x ${SHELL:-ksh} -c 'print $#;[[ $1 == argument0 ]]' count $(longline $n) > /dev/null  2>&1
465da2e3ebdSchin	[[ $? != 1 ]] && err_exit 'incorrect exit status for command -x'
466da2e3ebdSchinfi
467da2e3ebdSchin# test command -x option with extra arguments
468da2e3ebdSchininteger sum=0 n=10000
469da2e3ebdSchinif      ! ${SHELL:-ksh} -c 'print $#' count $(longline $n) > /dev/null  2>&1
470da2e3ebdSchinthen    for i in $(command command -x ${SHELL:-ksh} -c 'print $#;[[ $1 != argument0 ]]' count $(longline $n) one two three) #2> /dev/null)
471da2e3ebdSchin	do      ((sum += $i))
472da2e3ebdSchin	done
473da2e3ebdSchin	(( sum  > n )) || err_exit "command -x processed only $sum arguments"
474da2e3ebdSchin	(( (sum-n)%3==0 )) || err_exit "command -x processed only $sum arguments"
475da2e3ebdSchin	(( sum == n+3)) && err_exit "command -x processed only $sum arguments"
476da2e3ebdSchin	command -p command -x ${SHELL:-ksh} -c 'print $#;[[ $1 == argument0 ]]' count $(longline $n) > /dev/null  2>&1
477da2e3ebdSchin	[[ $? != 1 ]] && err_exit 'incorrect exit status for command -x'
478da2e3ebdSchinfi
479da2e3ebdSchin# test for debug trap
480da2e3ebdSchin[[ $(typeset -i i=0
481da2e3ebdSchin	trap 'print $i' DEBUG
482da2e3ebdSchin	while (( i <2))
483da2e3ebdSchin	do	(( i++))
484da2e3ebdSchin	done) == $'0\n0\n1\n1\n2' ]]  || err_exit  "DEBUG trap not working"
485da2e3ebdSchingetconf UNIVERSE - ucb
486da2e3ebdSchin[[ $($SHELL -c 'echo -3') == -3 ]] || err_exit "echo -3 not working in ucb universe"
487da2e3ebdSchintypeset -F3 start_x=SECONDS total_t delay=0.02
488da2e3ebdSchintypeset reps=50 leeway=5
48934f9b3eeSRoland Mainz#sleep $(( 2 * leeway * reps * delay )) |
49034f9b3eeSRoland Mainz#for (( i=0 ; i < reps ; i++ ))
49134f9b3eeSRoland Mainz#do	read -N1 -t $delay
49234f9b3eeSRoland Mainz#done
49334f9b3eeSRoland Mainz#(( total_t = SECONDS - start_x ))
49434f9b3eeSRoland Mainz#if	(( total_t > leeway * reps * delay ))
49534f9b3eeSRoland Mainz#then	err_exit "read -t in pipe taking $total_t secs - $(( reps * delay )) minimum - too long"
49634f9b3eeSRoland Mainz#elif	(( total_t < reps * delay ))
49734f9b3eeSRoland Mainz#then	err_exit "read -t in pipe taking $total_t secs - $(( reps * delay )) minimum - too fast"
49834f9b3eeSRoland Mainz#fi
49934f9b3eeSRoland Mainz#$SHELL -c 'sleep $(printf "%a" .95)' 2> /dev/null || err_exit "sleep doesn't except %a format constants"
50034f9b3eeSRoland Mainz#$SHELL -c 'test \( ! -e \)' 2> /dev/null ; [[ $? == 1 ]] || err_exit 'test \( ! -e \) not working'
5017c2fbfb3SApril Chin[[ $(ulimit) == "$(ulimit -fS)" ]] || err_exit 'ulimit is not the same as ulimit -fS'
50234f9b3eeSRoland Mainztmpfile=$tmp/file.2
5037c2fbfb3SApril Chinprint $'\nprint -r -- "${.sh.file} ${LINENO} ${.sh.lineno}"' > $tmpfile
5047c2fbfb3SApril Chin[[ $( . "$tmpfile") == "$tmpfile 2 1" ]] || err_exit 'dot command not working'
5057c2fbfb3SApril Chinprint -r -- "'xxx" > $tmpfile
5067c2fbfb3SApril Chin[[ $($SHELL -c ". $tmpfile"$'\n print ok' 2> /dev/null) == ok ]] || err_exit 'syntax error in dot command affects next command'
5077c2fbfb3SApril Chin
50834f9b3eeSRoland Mainz#float sec=$SECONDS del=4
50934f9b3eeSRoland Mainz#exec 3>&2 2>/dev/null
51034f9b3eeSRoland Mainz#$SHELL -c "( sleep 1; kill -ALRM \$\$ ) & sleep $del" 2> /dev/null
51134f9b3eeSRoland Mainz#exitval=$?
51234f9b3eeSRoland Mainz#(( sec = SECONDS - sec ))
51334f9b3eeSRoland Mainz#exec 2>&3-
51434f9b3eeSRoland Mainz#(( exitval )) && err_exit "sleep doesn't exit 0 with ALRM interupt"
51534f9b3eeSRoland Mainz#(( sec > (del - 1) )) || err_exit "ALRM signal causes sleep to terminate prematurely -- expected 3 sec, got $sec"
51634f9b3eeSRoland Mainztypeset -r z=3
51734f9b3eeSRoland Mainzy=5
51834f9b3eeSRoland Mainzfor i in 123 z  %x a.b.c
51934f9b3eeSRoland Mainzdo	( unset $i)  2>/dev/null && err_exit "unset $i should fail"
52034f9b3eeSRoland Mainzdone
52134f9b3eeSRoland Mainza=()
52234f9b3eeSRoland Mainzfor i in y y  y[8] t[abc] y.d a.b  a
52334f9b3eeSRoland Mainzdo	unset $i ||  print -u2  "err_exit unset $i should not fail"
52434f9b3eeSRoland Mainzdone
52534f9b3eeSRoland Mainz[[ $($SHELL -c 'y=3; unset 123 y;print $?$y') == 1 ]] 2> /dev/null ||  err_exit 'y is not getting unset with unset 123 y'
52634f9b3eeSRoland Mainz[[ $($SHELL -c 'trap foo TERM; (trap;(trap) )') == 'trap -- foo TERM' ]] || err_exit 'traps not getting reset when subshell is last process'
5277c2fbfb3SApril Chin
528da2e3ebdSchinexit $((Errors))
529