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++ ))
25}
26alias err_exit='err_exit $LINENO'
27
28Command=${0##*/}
29integer Errors=0
30if	[[ 'hi there' != "hi there" ]]
31then	err_exit "single quotes not the same as double quotes"
32fi
33x='hi there'
34if	[[ $x != 'hi there' ]]
35then	err_exit "$x not the same as 'hi there'"
36fi
37if	[[ $x != "hi there" ]]
38then	err_exit "$x not the same as \"hi there \""
39fi
40if	[[ \a\b\c\*\|\"\ \\ != 'abc*|" \' ]]
41then	err_exit " \\ differs from '' "
42fi
43if	[[ "ab\'\"\$(" != 'ab\'\''"$(' ]]
44then	err_exit " \"\" differs from '' "
45fi
46if	[[ $(print -r - 'abc*|" \') !=  'abc*|" \' ]]
47then	err_exit "\$(print -r - '') differs from ''"
48fi
49if	[[ $(print -r - "abc*|\" \\") !=  'abc*|" \' ]]
50then	err_exit "\$(print -r - '') differs from ''"
51fi
52if	[[ "$(print -r - 'abc*|" \')" !=  'abc*|" \' ]]
53then	err_exit "\"\$(print -r - '')\" differs from ''"
54fi
55if	[[ "$(print -r - "abc*|\" \\")" !=  'abc*|" \' ]]
56then	err_exit "\"\$(print -r - "")\" differs from ''"
57fi
58if	[[ $(print -r - $(print -r - 'abc*|" \')) !=  'abc*|" \' ]]
59then	err_exit "nested \$(print -r - '') differs from ''"
60fi
61if	[[ "$(print -r - $(print -r - 'abc*|" \'))" !=  'abc*|" \' ]]
62then	err_exit "\"nested \$(print -r - '')\" differs from ''"
63fi
64if	[[ $(print -r - "$(print -r - 'abc*|" \')") !=  'abc*|" \' ]]
65then	err_exit "nested \"\$(print -r - '')\" differs from ''"
66fi
67unset x
68if	[[ ${x-$(print -r - "abc*|\" \\")} !=  'abc*|" \' ]]
69then	err_exit "\${x-\$(print -r - '')} differs from ''"
70fi
71if	[[ ${x-$(print -r - "a}c*|\" \\")} !=  'a}c*|" \' ]]
72then	err_exit "\${x-\$(print -r - '}')} differs from ''"
73fi
74x=$((echo foo)|(cat))
75if	[[ $x != foo  ]]
76then	err_exit "((cmd)|(cmd)) failed"
77fi
78x=$(print -r -- "\"$HOME\"")
79if	[[ $x != '"'$HOME'"' ]]
80then	err_exit "nested double quotes failed"
81fi
82unset z
83: ${z="a{b}c"}
84if	[[ $z != 'a{b}c' ]]
85then	err_exit '${z="a{b}c"} not correct'
86fi
87unset z
88: "${z="a{b}c"}"
89if	[[ $z != 'a{b}c' ]]
90then	err_exit '"${z="a{b}c"}" not correct'
91fi
92if	[[ $(print -r -- "a\*b") !=  'a\*b' ]]
93then	err_exit '$(print -r -- "a\*b") differs from  a\*b'
94fi
95unset x
96if	[[ $(print -r -- "a\*b$x") !=  'a\*b' ]]
97then	err_exit '$(print -r -- "a\*b$x") differs from  a\*b'
98fi
99x=hello
100set -- ${x+foo bar bam}
101if	(( $# !=3 ))
102then	err_exit '${x+foo bar bam} does not yield three arguments'
103fi
104set -- ${x+foo "bar bam"}
105if	(( $# !=2 ))
106then	err_exit '${x+foo "bar bam"} does not yield two arguments'
107fi
108set -- ${x+foo 'bar bam'}
109if	(( $# !=2 ))
110then	err_exit '${x+foo '\''bar bam'\''} does not yield two arguments'
111fi
112set -- ${x+foo $x bam}
113if	(( $# !=3 ))
114then	err_exit '${x+foo $x bam} does not yield three arguments'
115fi
116set -- ${x+foo "$x" bam}
117if	(( $# !=3 ))
118then	err_exit '${x+foo "$x" bam} does not yield three arguments'
119fi
120set -- ${x+"foo $x bam"}
121if	(( $# !=1 ))
122then	err_exit '${x+"foo $x bam"} does not yield one argument'
123fi
124set -- "${x+foo $x bam}"
125if	(( $# !=1 ))
126then	err_exit '"${x+foo $x bam}" does not yield one argument'
127fi
128set -- ${x+foo "$x "bam}
129if	(( $# !=2 ))
130then	err_exit '${x+foo "$x "bam} does not yield two arguments'
131fi
132x="ab$'cd"
133if	[[ $x != 'ab$'"'cd" ]]
134then	err_exit '$'"' inside double quotes not working"
135fi
136x=`print 'ab$'`
137if	[[ $x != 'ab$' ]]
138then	err_exit '$'"' inside `` quotes not working"
139fi
140unset a
141x=$(print -r -- "'\
142\
143")
144if	[[ $x != "'" ]]
145then	err_exit 'line continuation in double strings not working'
146fi
147x=$(print -r -- "'\
148$a\
149")
150if	[[ $x != "'" ]]
151then	err_exit 'line continuation in expanded double strings not working'
152fi
153x='\*'
154if	[[ $(print -r -- $x) != '\*' ]]
155then	err_exit 'x="\\*";$x != \*'
156fi
157x='   hello    world    '
158set -- $x
159if	(( $# != 2 ))
160then	err_exit 'field splitting error'
161fi
162x=$(print -r -- '1234567890123456789012345678901234567890123456789012345678901234567890 \
1631234567890123456789012345678901234567890123456789012345678901234567890 \
1641234567890123456789012345678901234567890123456789012345678901234567890 \
1651234567890123456789012345678901234567890123456789012345678901234567890 \
1661234567890123456789012345678901234567890123456789012345678901234567890 \
1671234567890123456789012345678901234567890123456789012345678901234567890 \
1681234567890123456789012345678901234567890123456789012345678901234567890 \
1691234567890123456789012345678901234567890123456789012345678901234567890 \
1701234567890123456789012345678901234567890123456789012345678901234567890 \
1711234567890123456789012345678901234567890123456789012345678901234567890 \
1721234567890123456789012345678901234567890123456789012345678901234567890 \
1731234567890123456789012345678901234567890123456789012345678901234567890 \
1741234567890123456789012345678901234567890123456789012345678901234567890 \
1751234567890123456789012345678901234567890123456789012345678901234567890 \
1761234567890123456789012345678901234567890123456789012345678901234567890')
177if	(( ${#x} != (15*73-3) ))
178then	err_exit "length of x, ${#x}, is incorrect should be $((15*73-3))"
179fi
180x='$hi'
181if	[[ $x\$ != '$hi$' ]]
182then	err_exit ' $x\$, with x=$hi, does not expand to $hi$'
183fi
184if	[[ $x$ != '$hi$' ]]
185then	err_exit ' $x$, with x=$hi, does not expand to $hi$'
186fi
187set -- $(/bin/echo foo;sleep 1;/bin/echo bar)
188if	[[ $# != 2 ]]
189then	err_exit 'word splitting after command substitution not working'
190fi
191unset q
192if	[[ "${q:+'}q${q:+'}" != q ]]
193then	err_exit 'expansion of "{q:+'\''}" not correct when q unset'
194fi
195q=1
196if	[[ "${q:+'}q${q:+'}" != "'q'" ]]
197then	err_exit 'expansion of "{q:+'\''}" not correct when q set'
198fi
199x=$'x\' #y'
200if	[[ $x != "x' #y" ]]
201then	err_exit "$'x\' #y'" not working
202fi
203x=$q$'x\' #y'
204if	[[ $x != "1x' #y" ]]
205then	err_exit "$q$'x\' #y'" not working
206fi
207IFS=,
208x='a,b\,c,d'
209set -- $x
210if	[[ $2 != 'b\' ]]
211then	err_exit "field splitting of $x with IFS=$IFS not working"
212fi
213foo=bar
214bar=$(print -r -- ${foo+\\n\ })
215if	[[ $bar != '\n ' ]]
216then	err_exit '${foo+\\n\ } expansion error'
217fi
218unset bar
219bar=$(print -r -- ${foo+\\n\ $bar})
220if	[[ $bar != '\n ' ]]
221then	err_exit '${foo+\\n\ $bar} expansion error with bar unset'
222fi
223x='\\(..\\)|&\|\|\\&\\|'
224if	[[ $(print -r -- $x) != "$x" ]]
225then	err_exit '$x, where x=\\(..\\)|&\|\|\\&\\| not working'
226fi
227x='\\('
228if	[[ $(print -r -- a${x}b) != a"${x}"b ]]
229then	err_exit 'a${x}b, where x=\\( not working'
230fi
231x=
232if	[[ $(print -r -- $x'\\1') != '\\1' ]]
233then	err_exit 'backreference inside single quotes broken'
234fi
235set -- ''
236set -- "$@"
237if	(( $# != 1 ))
238then	err_exit '"$@" not preserving nulls'
239fi
240x=
241if	[[ $(print -r s"!\2${x}\1\a!") != 's!\2\1\a!' ]]
242then	err_exit  'print -r s"!\2${x}\1\a!" not equal s!\2\1\a!'
243fi
244if	[[ $(print -r $'foo\n\n\n') != foo ]]
245then	err_exit 'trailing newlines on comsubstitution not removed'
246fi
247unset x
248if	[[ ${x:='//'} != '//' ]]
249then	err_exit '${x:='//'} != "//"'
250fi
251if	[[ $(print -r "\"hi$\"")	!= '"hi$"' ]]
252then	err_exit '$\ not correct inside ""'
253fi
254unset x
255if	[[ "${x-a\}b}" != 'a}b' ]]
256then	err_exit '"${x-a\}b}" !=  "a}b"'
257fi
258if	[[ "\}\]$x\*\{\[\\" != '\}\]\*\{\[\' ]]
259then	err_exit '"\}\]$x\*\{\[\\" !=  "\}\]\*\{\[\"'
260fi
261foo=yes
262if	[[ $(print -r -- {\$foo}) != '{$foo}' ]]
263then	err_exit '{\$foo}' not expanded correctly
264fi
265[[ foo == $(
266###########################################################
267###########################################################
268###########################################################
269###########################################################
270###########################################################
271###########################################################
272###########################################################
273###########################################################
274###########################################################
275###########################################################
276###########################################################
277###########################################################
278###########################################################
279###########################################################
280###########################################################
281###########################################################
282###########################################################
283###########################################################
284###########################################################
285###########################################################
286###########################################################
287###########################################################
288###########################################################
289###########################################################
290###########################################################
291###########################################################
292###########################################################
293###########################################################
294###########################################################
295###########################################################
296###########################################################
297###########################################################
298###########################################################
299###########################################################
300###########################################################
301###########################################################
302###########################################################
303###########################################################
304print foo) ]] || err_exit "command subsitution with long comments broken"
305subject='some/other/words'
306re='@(?*)/@(?*)/@(?*)'
307[[ ${subject/${re}/\3} != words ]] && err_exit 'string replacement with \3 not working'
308[[ ${subject/${re}/'\3'} != '\3' ]] && err_exit 'string replacement with '"'\3'"' not working'
309[[ ${subject/${re}/"\\3"} != '\3' ]] && err_exit 'string replacement with "\\3" not working'
310[[ ${subject/${re}/"\3"} != '\3' ]] && err_exit 'string replacement with "\3" not working'
311string='\3'
312[[ ${subject/${re}/${string}} != words ]] && err_exit 'string replacement with $string not working with string=\3'
313[[ $(print -r "${subject/${re}/${string}}") != words ]] && err_exit 'string replacement with $string not working with string=\3 using print'
314[[ ${subject/${re}/"${string}"} != '\3' ]] && err_exit 'string replacement with "$string" not working with  string=\3'
315[[ $(print -r "${subject/${re}/"${string}"}") != '\3' ]] && err_exit 'string replacement with "$string" not working with  string=\3 using print'
316string='\\3'
317[[ ${subject/${re}/${string}} != '\3' ]] && err_exit 'string replacement with $string not working with string=\\3'
318[[ ${subject/${re}/"${string}"} != '\\3' ]] && err_exit 'string replacement with "$string" not working with  string=\\3'
319[[ ${subject/${re}/\4} != '\4' ]] && err_exit 'string replacement with \4 not working'
320[[ ${subject/${re}/'\4'} != '\4' ]] && err_exit 'string replacement with '\4' not working'
321string='\4'
322[[ ${subject/${re}/${string}} != '\4' ]] && err_exit 'string replacement with $string not working with string=\4'
323[[ ${subject/${re}/"${string}"} != '\4' ]] && err_exit 'string replacement with "$string" not working with  string=\4'
324string='&foo'
325[[ ${subject/${re}/${string}} != '&foo' ]] && err_exit 'string replacement with $string not working with string=&foo'
326[[ ${subject/${re}/"${string}"} != '&foo' ]] && err_exit 'string replacement with "$string" not working with  string=&foo'
327{
328x=x
329x=${x:-`id | sed 's/^[^(]*(\([^)]*\)).*/\1/'`}
330} 2> /dev/null || err_exit 'skipping over `` failed'
331[[ $x == x ]] || err_exit 'assignment ${x:=`...`} failed'
332[[ $($SHELL -c 'print a[') == 'a[' ]] || err_exit "unbalanced '[' in command arg fails"
333$SHELL -c $'false && (( `wc -l /dev/null | nawk \'{print $1}\'` > 2 )) && true;:' 2> /dev/null ||  err_exit 'syntax error with ` in arithmetic expression'
334{ $SHELL  -c '((  1`: "{ }"` ))' ;} 2> /dev/null || err_exit 'problem with ` inside (())'
335varname=foobarx
336x=`print '"\$'${varname}'"'`
337[[ $x == '"$foobarx"' ]] ||  err_exit $'\\$\' not handled correctly inside ``'
338
339copy1=5 copynum=1
340foo="`eval echo "$"{copy$copynum"-0}"`"
341[[ $foo == "$copy1" ]] || err_exit '$"..." not being ignored inside ``'
342
343[[ $($SHELL -c 'set --  ${1+"$@"}; print $#' cmd '') == 1 ]] || err_exit '${1+"$@"} with one empty argument fails'
344[[ $($SHELL -c 'set --  ${1+"$@"}; print $#' cmd foo '') == 2 ]] || err_exit '${1+"$@"} with one non-empty and on empty argument fails'
345[[ $($SHELL -c 'set --  ${1+"$@"}; print $#' cmd "" '') == 2 ]] || err_exit '${1+"$@"} with two empty arguments fails'
346[[ $($SHELL -c 'set --  ${1+"$@"}; print $#' cmd "" '' '') == 3 ]] || err_exit '${1+"$@"} with three empty arguments fails'
347[[ $($SHELL -c 'set --  "$@"; print $#' cmd '') == 1 ]] || err_exit '"$@" with one empty argument fails'
348[[ $($SHELL -c 'set --  "${@:2}"; print $#' cmd '') == 0 ]] || err_exit '"$@" with one empty argument fails'
349[[ $($SHELL -c 'set --  "$@"; print $#' cmd foo '') == 2 ]] || err_exit '"$@" with one non-empty and on empty argument fails'
350[[ $($SHELL -c 'set --  "$@"; print $#' cmd "" '') == 2 ]] || err_exit '"$@" with two empty arguments fails'
351[[ $($SHELL -c 'set --  "$@"; print $#' cmd "" '' '') == 3 ]] || err_exit '"$@" with three empty arguments fails'
352args=('')
353set -- "${args[@]}"
354[[ $# == 1 ]] || err_exit '"${args[@]}"} with one empty argument fails'
355set -- ${1+"${args[@]}"}
356[[ $# == 1 ]] || err_exit '${1+"${args[@]}"} with one empty argument fails'
357args=(foo '')
358set -- "${args[@]}"
359[[ $# == 2 ]] || err_exit '"${args[@]}"} with one non-empty and one empty argument fails'
360set -- ${1+"${args[@]}"}
361[[ $# == 2 ]] || err_exit '${1+"${args[@]}"} with one non-empty and one empty argument fails'
362
363unset ARGS
364set --
365ARGS=("$@")
366set -- "${ARGS[@]}"
367(( $# )) &&  err_exit 'set -- "${ARGS[@]}" for empty array should not produce arguments'
368
369exit $((Errors<125?Errors:125))
370