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
34trap '' FPE # NOTE: osf.alpha requires this (no ieee math)
35
36integer x=1 y=2 z=3
37if	(( 2+2 != 4 ))
38then	err_exit 2+2!=4
39fi
40if	((x+y!=z))
41then	err_exit x+y!=z
42fi
43if	(($x+$y!=$z))
44then	err_exit $x+$y!=$z
45fi
46if	(((x|y)!=z))
47then	err_exit "(x|y)!=z"
48fi
49if	((y >= z))
50then	err_exit "y>=z"
51fi
52if	((y+3 != z+2))
53then	err_exit "y+3!=z+2"
54fi
55if	((y<<2 != 1<<3))
56then	err_exit "y<<2!=1<<3"
57fi
58if	((133%10 != 3))
59then	err_exit "133%10!=3"
60	if	(( 2.5 != 2.5 ))
61	then	err_exit 2.5!=2.5
62	fi
63fi
64d=0
65((d || 1)) || err_exit 'd=0; ((d||1))'
66if	(( d++!=0))
67then	err_exit "d++!=0"
68fi
69if	(( --d!=0))
70then	err_exit "--d!=0"
71fi
72if	(( (d++,6)!=6 && d!=1))
73then	err_exit '(d++,6)!=6 && d!=1'
74fi
75d=0
76if	(( (1?2+1:3*4+d++)!=3 || d!=0))
77then	err_exit '(1?2+1:3*4+d++) !=3'
78fi
79for	((i=0; i < 20; i++))
80do	:
81done
82if	(( i != 20))
83then	err_exit 'for (( expr)) failed'
84fi
85for	((i=0; i < 20; i++)); do	: ; done
86if	(( i != 20))
87then	err_exit 'for (( expr));... failed'
88fi
89for	((i=0; i < 20; i++)) do	: ; done
90if	(( i != 20))
91then	err_exit 'for (( expr))... failed'
92fi
93if	(( (i?0:1) ))
94then	err_exit '(( (i?0:1) )) failed'
95fi
96if	(( (1 || 1 && 0) != 1 ))
97then	err_exit '( (1 || 1 && 0) != 1) failed'
98fi
99if	(( (_=1)+(_x=0)-_ ))
100then	err_exit '(_=1)+(_x=0)-_ failed'
101fi
102if	((  (3^6) != 5))
103then	err_exit '((3^6) != 5) failed'
104fi
105integer x=1
106if	(( (x=-x) != -1 ))
107then	err_exit '(x=-x) != -1 failed'
108fi
109i=2
110if	(( 1$(($i))3 != 123 ))
111then	err_exit ' 1$(($i))3 failed'
112fi
113((pi=4*atan(1.)))
114point=(
115	float x
116	float y
117)
118(( point.x = cos(pi/6), point.y = sin(pi/6) ))
119if	(( point.x*point.x + point.y*point.y > 1.01 ))
120then	err_exit 'cos*cos +sin*sin > 1.01'
121fi
122if	(( point.x*point.x + point.y*point.y < .99 ))
123then	err_exit 'cos*cos +sin*sin < .99'
124fi
125if [[ $((y=x=1.5)) != 1 ]]
126then	err_exit 'typecast not working in arithmetic evaluation'
127fi
128typeset -E x=1.5
129( ((x++))  ) 2>/dev/null
130if [[ $? == 0 ]]
131then	err_exit 'postincrement of floating point allowed'
132fi
133( ((++x))  ) 2>/dev/null
134if [[ $? == 0 ]]
135then	err_exit 'preincrement of floating point allowed'
136fi
137x=1.5
138( ((x%1.1))  ) 2>/dev/null
139if [[ $? == 0 ]]
140then	err_exit 'floating point allowed with % operator'
141fi
142x=.125
143if	[[ $(( 4 * x/2 )) != 0.25 ]]
144then	err_exit '(( 4 * x/2 )) is not 0.25, with x=.125'
145fi
146if	[[ $(( pow(2,3) )) != 8 ]]
147then	err_exit '$(( pow(2,3) )) != 8'
148fi
149( [[ $(( pow(2,(3)) )) == 8 ]] ) 2> /dev/null
150if	(( $? ))
151then	err_exit '$(( pow(2,(3)) )) != 8'
152fi
153unset x
154integer x=1; integer x=1
155if	[[ $x != 1 ]]
156then	err_exit 'two consecutive integer x=1 not working'
157fi
158unset z
159{ z=$(typeset -RZ2 z2; (( z2 = 8 )); print $z2) ;} 2>/dev/null
160if [[ $z != "08" ]]
161then	err_exit "typeset -RZ2 leading 0 decimal not working [z=$z]"
162fi
163{ z=$(typeset -RZ3 z3; (( z3 = 8 )); print $z3) ;} 2>/dev/null
164if [[ $z != "008" ]]
165then	err_exit "typeset -RZ3 leading 0 decimal not working [z=$z]"
166fi
167unset z
168typeset -Z3 z=010
169(( z=z+1))
170if	[[ $z != 011 ]]
171then	err_exit "leading 0's in -Z not treated as decimal"
172fi
173unset x
174integer x=0
175if	[[ $((x+=1)) != 1  ]] || ((x!=1))
176then	err_exit "+= not working"
177	x=1
178fi
179x=1
180if	[[ $((x*=5)) != 5  ]] || ((x!=5))
181then	err_exit "*= not working"
182	x=5
183fi
184if	[[ $((x%=4)) != 1  ]] || ((x!=1))
185then	err_exit "%= not working"
186	x=1
187fi
188if	[[ $((x|=6)) != 7  ]] || ((x!=7))
189then	err_exit "|= not working"
190	x=7
191fi
192if	[[ $((x&=5)) != 5  ]] || ((x!=5))
193then	err_exit "&= not working"
194	x=5
195fi
196function newscope
197{
198	float x=1.5
199	(( x += 1 ))
200	print -r -- $x
201}
202if	[[ $(newscope) != 2.5 ]]
203then	err_exit "arithmetic using wrong scope"
204fi
205unset x
206integer y[3]=9 y[4]=2 i=3
207(( x = y[3] + y[4] ))
208if	[[ $x != 11 ]]
209then	err_exit "constant index array arithmetic failure"
210fi
211(( x = $empty y[3] + y[4] ))
212if	[[ $x != 11 ]]
213then	err_exit "empty constant index array arithmetic failure"
214fi
215(( x = y[i] + y[i+1] ))
216if	[[ $x != 11 ]]
217then	err_exit "variable subscript index array arithmetic failure"
218fi
219integer a[5]=3 a[2]=4
220(( x = y[a[5]] + y[a[2]] ))
221if	[[ $x != 11 ]]
222then	err_exit "nested subscript index array arithmetic failure"
223fi
224unset y
225typeset -Ai y
226y[three]=9 y[four]=2
227three=four
228four=three
229(( x = y[three] + y[four] ))
230if	[[ $x != 11 ]]
231then	err_exit "constant associative array arithmetic failure"
232fi
233(( x = y[$three] + y[$four] ))
234if	[[ $x != 11 ]]
235then	err_exit "variable subscript associative array arithmetic failure"
236fi
237$SHELL -nc '((a = 1))' 2> /dev/null || err_exit "sh -n fails with arithmetic"
238$SHELL -nc '((a.b++))' 2> /dev/null || err_exit "sh -n fails with arithmetic2"
239unset z
240float z=7.5
241if	{ (( z%2 != 1));} 2> /dev/null
242then	err_exit '% not working on floating point'
243fi
244chr=(a ' ' '=' '\r' '\n' '\\' '\"' '$' "\\'" '[' ']' '(' ')' '<' '\xab' '\040' '`' '{' '}' '*' '\E')
245if	(('a' == 97))
246then	val=(97 32  61 13 10 92 34 36 39 91 93 40 41 60 171 32 96 123 125 42 27)
247else	val=(129 64 126 13 21 224 127 91 125 173 189 77 93 76 171 32 121 192 208 92 39 21)
248fi
249q=0
250for ((i=0; i < ${#chr[@]}; i++))
251do	if	(( '${chr[i]}' != ${val[i]} ))
252	then	err_exit "(( '${chr[i]}'  !=  ${val[i]} ))"
253	fi
254	if	[[ $(( '${chr[i]}' )) != ${val[i]} ]]
255	then	err_exit "(( '${chr[i]}' )) !=  ${val[i]}"
256	fi
257	if	[[ $(( L'${chr[i]}' )) != ${val[i]} ]]
258	then	err_exit "(( '${chr[i]}' )) !=  ${val[i]}"
259	fi
260	if	eval '((' "'${chr[i]}'" != ${val[i]} '))'
261	then	err_exit "eval (( '${chr[i]}'  !=  ${val[i]} ))"
262	fi
263	if	eval '((' "'${chr[i]}'" != ${val[i]} ' + $q ))'
264	then	err_exit "eval (( '${chr[i]}'  !=  ${val[i]} ))"
265	fi
266done
267unset x
268typeset -ui x=4294967293
269[[ $x != 4294967293 ]]  && err_exit "unsigned integers not working"
270x=32767
271x=x+1
272[[ $x != 32768 ]]  && err_exit "unsigned integer addition not working"
273unset x
274float x=99999999999999999999999999
275if	(( x < 1e20 ))
276then	err_exit 'large integer constants not working'
277fi
278unset x  y
279function foobar
280{
281	nameref x=$1
282	(( x +=1 ))
283	print $x
284}
285x=0 y=4
286if	[[ $(foobar y) != 5 ]]
287then	err_exit 'name references in arithmetic statements in functions broken'
288fi
289if	(( 2**3 != pow(2,3) ))
290then	err_exit '2**3 not working'
291fi
292if	(( 2**3*2 != pow(2,3)*2 ))
293then	err_exit '2**3*2 not working'
294fi
295if	(( 4**3**2 != pow(4,pow(3,2)) ))
296then	err_exit '4**3**2 not working'
297fi
298if	(( (4**3)**2 != pow(pow(4,3),2) ))
299then	err_exit '(4**3)**2 not working'
300fi
301typeset -Z3 x=11
302typeset -i x
303if	(( x != 11 ))
304then	err_exit '-Z3 not treated as decimal'
305fi
306unset x
307typeset -ui x=-1
308(( x >= 0 )) || err_exit 'unsigned integer not working'
309(( $x >= 0 )) || err_exit 'unsigned integer not working as $x'
310unset x
311typeset -ui42 x=50
312if	[[ $x != 42#18 ]]
313then	err_exit 'display of unsigned integers in non-decimal bases wrong'
314fi
315$SHELL -c 'i=0;(( ofiles[i] != -1 && (ofiles[i] < mins || mins == -1) ));exit 0' 2> /dev/null || err_exit 'lexical error with arithemtic expression'
316$SHELL -c '(( +1 == 1))' 2> /dev/null || err_exit 'unary + not working'
317typeset -E20 val=123.01234567890
318[[ $val == 123.0123456789 ]] || err_exit "rounding error val=$val"
319if	[[ $(print x$((10))=foo) != x10=foo ]]
320then	err_exit 'parsing error with x$((10))=foo'
321fi
322$SHELL -c 'typeset x$((10))=foo' 2> /dev/null || err_exit 'typeset x$((10)) parse error'
323unset x
324x=$(( exp(log(2.0)) ))
325(( x > 1.999 && x < 2.001 )) || err_exit 'composite functions not working'
326unset x y n
327typeset -Z8 x=0 y=0
328integer n
329for	(( n=0; n < 20; n++ ))
330do	let "x = $x+1"
331	(( y = $y+1 ))
332done
333(( x == n ))  || err_exit 'let with zero filled fields not working'
334(( y == n ))  || err_exit '((...)) with zero filled fields not working'
335typeset -RZ3 x=10
336[[ $(($x)) == 10 && $((1$x)) == 1010 ]] || err_exit 'zero filled fields not preserving leading zeros'
337unset y
338[[ $(let y=$x;print $y) == 10 && $(let y=1$x;print $y) == 1010 ]] || err_exit 'zero filled fields not preserving leading zeros with let'
339unset i ip ipx
340typeset -i hex=( 172 30 18 1)
341typeset -ui ip=0 ipx=0
342integer i
343for	((i=0; i < 4; i++))
344do	(( ip =  (ip<<8) | hex[i]))
345done
346for ((i=0; i < 4; i++))
347do	(( ipx = ip % 256 ))
348	(( ip /= 256 ))
349	(( ipx != hex[3-i] )) && err_exit "hex digit $((3-i)) not correct"
350done
351unset x
352x=010
353(( x == 10 )) || err_exit 'leading zeros in x treated as octal arithmetic with $((x))'
354(( $x == 8 )) || err_exit 'leading zeros not treated as octal arithmetic with $x'
355unset x
356typeset -Z x=010
357(( x == 10 )) || err_exit 'leading zeros not ignored for arithmetic'
358(( $x == 10 )) || err_exit 'leading zeros not ignored for arithmetic with $x'
359typeset -i i=x
360(( i == 10 )) || err_exit 'leading zeros not ignored for arithmetic assignment'
361(( ${x:0:1} == 0 )) || err_exit 'leading zero should not be stripped for x:a:b'
362c010=3
363(( c$x  == 3 )) || err_exit 'leading zero with variable should not be stripped'
364[[ $( ($SHELL -c '((++1))' 2>&1) 2>/dev/null ) == *++1:* ]] || err_exit "((++1)) not generating error message"
365i=2
366(( "22" == 22 )) || err_exit "double quoted constants fail"
367(( "2$i" == 22 )) || err_exit "double quoted variables fail"
368(( "18+$i+2" == 22 )) || err_exit "double quoted expressions fail"
369# 04-04-28 bug fix
370unset i; typeset -i i=01-2
371(( i == -1 )) || err_exit "01-2 is not -1"
372
373cat > $tmp/script <<-\!
374tests=$*
375typeset -A blop
376function blop.get
377{
378	.sh.value=777
379}
380function mkobj
381{
382	nameref obj=$1
383	obj=()
384	[[ $tests == *1* ]] && {
385		(( obj.foo = 1 ))
386		(( obj.bar = 2 ))
387		(( obj.baz = obj.foo + obj.bar ))	# ok
388		echo $obj
389	}
390	[[ $tests == *2* ]] && {
391		(( obj.faz = faz = obj.foo + obj.bar ))	# ok
392		echo $obj
393	}
394	[[ $tests == *3* ]] && {
395		# case 3, 'active' variable involved, w/ intermediate variable
396		(( obj.foz = foz = ${blop[1]} ))	# coredump
397		echo $obj
398	}
399	[[ $tests == *4* ]] && {
400		# case 4, 'active' variable, in two steps
401		(( foz = ${blop[1]} ))	# ok
402		(( obj.foz = foz ))		# ok
403		echo $obj
404	}
405	[[ $tests == *5* ]] && {
406		# case 5, 'active' variable involved, w/o intermediate variable
407		(( obj.fuz = ${blop[1]} ))	# coredump
408		echo $obj
409	}
410	[[ $tests == *6* ]] && {
411		echo $(( obj.baz = obj.foo + obj.bar ))	# coredump
412	}
413	[[ $tests == *7* ]] && {
414		echo $(( obj.foo + obj.bar ))	# coredump
415	}
416}
417mkobj bla
418!
419chmod +x $tmp/script
420[[ $($tmp/script 1) != '( bar=2 baz=3 foo=1 )' ]] 2>/dev/null && err_exit 'compound var arithmetic failed'
421[[ $($tmp/script 2) != '( faz=0 )' ]] 2>/dev/null && err_exit 'compound var arithmetic failed'
422[[ $($tmp/script 3) != '( foz=777 )' ]] 2>/dev/null && err_exit 'compound var arithmetic failed'
423[[ $($tmp/script 4) != '( foz=777 )' ]] 2>/dev/null && err_exit 'compound var arithmetic failed'
424[[ $($tmp/script 5) != '( fuz=777 )' ]] 2>/dev/null && err_exit 'compound var arithmetic failed'
425[[ $($tmp/script 6) != '0' ]] 2>/dev/null && err_exit 'compound var arithmetic failed'
426[[ $($tmp/script 7) != '0' ]] 2>/dev/null && err_exit 'compound var arithmetic failed'
427unset foo
428typeset -F1 foo=123456789.19
429[[ $foo == 123456789.2 ]] || err_exit 'typeset -F1 not working correctly'
430
431# divide by zero
432
433for expr in '1/(1/2)' '8%(1/2)' '8%(1.0/2)'
434do	[[ $( ( $SHELL -c "( (($expr)) )  || print ok" ) 2>/dev/null ) == ok ]] || err_exit "divide by zero not trapped: $expr"
435done
436
437for expr in '1/(1.0/2)' '1/(1/2.0)'
438do	[[ $( ( $SHELL -c "( print -r -- \$(($expr)) )" ) 2>/dev/null ) == 2 ]] || err_exit "invalid value for: $expr"
439done
440[[ $((5||0)) == 1 ]] || err_exit '$((5||0))'" == $((5||0)) should be 1"
441$SHELL -c 'integer x=3 y=2; (( (y += x += 2) == 7  && x==5))' 2> /dev/null || err_exit '((y += x += 2)) not working'
442$SHELL -c 'b=0; [[ $((b?a=1:b=9)) == 9 ]]' 2> /dev/null || err_exit 'b?a=1:b=9 not working'
443unset x
444(( x = 4*atan(1.0) ))
445[[ $x == "$((x))" ]] || err_exit  '$x !- $((x)) when x is pi'
446$SHELL -c  "[[  ${x//./} == {14,100}(\d) ]]" 2> /dev/null || err_exit 'pi has less than 14 significant places'
447if	(( Inf+1 == Inf ))
448then	set \
449		Inf		inf	\
450		-Inf		-inf	\
451		Nan		nan	\
452		-Nan		-nan	\
453		1.0/0.0		inf
454	while	(( $# >= 2 ))
455	do	x=$(printf "%g\n" $(($1)))
456		[[ $x == $2 ]] || err_exit "printf '%g\\n' \$(($1)) failed -- expected $2, got $x"
457		x=$(printf "%g\n" $1)
458		[[ $x == $2 ]] || err_exit "printf '%g\\n' $1 failed -- expected $2, got $x"
459		x=$(printf -- $(($1)))
460		[[ $x == $2 ]] || err_exit "print -- \$(($1)) failed -- expected $2, got $x"
461		shift 2
462	done
463	(( 1.0/0.0 == Inf )) || err_exit '1.0/0.0 != Inf'
464	[[ $(print -- $((0.0/0.0))) == ?(-)nan ]] || err_exit '0.0/0.0 != NaN'
465	(( Inf*Inf == Inf )) || err_exit 'Inf*Inf != Inf'
466	(( NaN != NaN )) || err_exit 'NaN == NaN'
467	(( -5*Inf == -Inf )) || err_exit '-5*Inf != -Inf'
468	[[ $(print -- $((sqrt(-1.0)))) == ?(-)nan ]]|| err_exit 'sqrt(-1.0) != NaN'
469	(( pow(1.0,Inf) == 1.0 )) || err_exit 'pow(1.0,Inf) != 1.0'
470	(( pow(Inf,0.0) == 1.0 )) || err_exit 'pow(Inf,0.0) != 1.0'
471	[[ $(print -- $((NaN/Inf))) == ?(-)nan ]] || err_exit 'NaN/Inf != NaN'
472	(( 4.0/Inf == 0.0 )) || err_exit '4.0/Inf != 0.0'
473else	err_exit 'Inf and NaN not working'
474fi
475unset x y n r
476n=14.555
477float x=$n y
478y=$(printf "%a" x)
479r=$y
480[[ $r == $n ]] || err_exit "output of printf %a not self preserving -- expected $x, got $y"
481unset x y r
482float x=-0 y=-0.0
483r=-0
484[[ $((-0)) == 0 ]] || err_exit '$((-0)) should be 0'
485[[ $(( -1*0)) == 0 ]] || err_exit '$(( -1*0)) should be 0'
486[[ $(( -1.0*0)) == -0 ]] || err_exit '$(( -1.0*0)) should be -0'
487[[ $(printf "%g %g %g\n" x $x $((x)) ) == '-0 -0 -0' ]] || err_exit '%g of x $x $((x)) for x=-0 should all be -0'
488[[ $(printf "%g %g %g\n" y $x $((y)) ) == '-0 -0 -0' ]] || err_exit '%g of y $y $((y)) for y=-0.0 should all be -0'
489$SHELL -c '(( x=));:' 2> /dev/null && err_exit '((x=)) should be an error'
490$SHELL -c '(( x+=));:' 2> /dev/null && err_exit '((x+=)) should be an error'
491$SHELL -c '(( x=+));:' 2> /dev/null && err_exit '((x=+)) should be an error'
492$SHELL -c 'x=();x.arr[0]=(z=3); ((x.arr[0].z=2))' 2> /dev/null || err_exit '(((x.arr[0].z=2)) should not be an error'
493
494float t
495typeset a b r
496v="-0.0 0.0 +0.0 -1.0 1.0 +1.0"
497for a in $v
498do	for b in $v
499	do	(( r = copysign(a,b) ))
500		(( t = copysign(a,b) ))
501		[[ $r == $t ]] || err_exit $(printf "float t=copysign(%3.1f,%3.1f) => %3.1f -- expected %3.1f\n" a b t r)
502	done
503done
504
505typeset -l y y_ascii
506(( y=sin(90) ))
507y_ascii=$y
508(( y == y_ascii )) || err_exit "no match,\n\t$(printf "%a\n" y)\n!=\n\t$(printf "%a\n" y_ascii)"
509
510( $SHELL  <<- \EOF
511	p=5
512	t[p]=6
513	while (( t[p] != 0 )) ; do
514		((
515		p+=1 ,
516		t[p]+=2 ,
517		p+=3 ,
518		t[p]+=5 ,
519		p+=1 ,
520		t[p]+=2 ,
521		p+=1 ,
522		t[p]+=1 ,
523		p-=6  ,
524		t[p]-=1
525		))
526	:
527	done
528EOF) 2> /dev/null ||  err_exit 'error with comma expression'
529
530N=(89551 89557)
531i=0 j=1
532[[ $(printf "%d" N[j]-N[i]) == 6 ]] || err_exit 'printf %d N[i]-N[j] failed'
533[[ $((N[j]-N[i])) == 6 ]] || err_exit  '$((N[j]-N[i])) incorrect'
534
535unset a x
536x=0
537((a[++x]++))
538(( x==1)) || err_exit '((a[++x]++)) should only increment x once'
539(( a[1]==1))  || err_exit 'a[1] not incremented'
540unset a
541x=0
542((a[x++]++))
543(( x==1)) || err_exit '((a[x++]++)) should only increment x once'
544(( a[0]==1))  || err_exit 'a[0] not incremented'
545unset a
546x=0
547((a[x+=2]+=1))
548(( x==2)) || err_exit '((a[x+=2]++)) should result in x==2'
549(( a[2]==1))  || err_exit 'a[0] not 1'
550
551unset a i
552typeset -a a
553i=1
554(( a[i]=1 ))
555(( a[0] == 0 )) || err_exit 'a[0] not 0'
556(( a[1] == 1 )) || err_exit 'a[1] not 1'
557
558unset a
559typeset -i a
560for ((i=0;i<1000;i++))
561do ((a[RANDOM%2]++))
562done
563(( (a[0]+a[1])==1000)) || err_exit '(a[0]+a[1])!=1000'
564
565(( 4.**3/10 == 6.4 )) || err_exit '4.**3/10!=6.4'
566(( (.5+3)/7 == .5 )) || err_exit '(.5+3)/7!==.5'
567
568function .sh.math.mysin x
569{
570        ((.sh.value = x - x**3/6. + x**5/120.-x**7/5040. + x**9/362880.))
571}
572
573(( abs(sin(.5)-mysin(.5)) < 1e-6 )) || err_exit 'mysin() not close to sin()'
574
575$SHELL 2> /dev/null  <<- \EOF || err_exit "arithmetic functions defined and referenced in compound command not working"
576{
577	function .sh.math.mysin x
578	{
579	        ((.sh.value = x-x**3/6. + x**5/120.-x**7/5040. + x**9/362880.))
580	}
581	(( abs(sin(.5)-mysin(.5)) < 1e-6 ))
582	exit 0
583}
584EOF
585
586
587
588function .sh.math.max x y z
589{
590	.sh.value=x
591	(( y > x )) && .sh.value=y
592	(( z > .sh.value )) && .sh.value=z
593}
594(( max(max(3,8,5),7,5)==8)) || err_exit 'max(max(3,8,5),7,5)!=8'
595(( max(max(3,8,5),7,9)==9)) || err_exit 'max(max(3,8,9),7,5)!=9'
596(( max(6,max(3,9,5),7)==9 )) || err_exit 'max(6,max(3,8,5),7)!=9'
597(( max(6,7, max(3,8,5))==8 )) || err_exit 'max(6,7,max(3,8,5))!=8'
598
599enum color_t=(red green blue yellow)
600color_t shirt pants=blue
601(( pants == blue )) || err_exit 'pants should be blue'
602(( shirt == red )) || err_exit 'pants should be red'
603(( shirt != green )) || err_exit 'shirt should not be green'
604(( pants != shirt )) || err_exit 'pants should be the same as shirt'
605(( pants = yellow ))
606(( pants == yellow )) || err_exit 'pants should be yellow'
607
608unset z
609integer -a z=( [1]=90 )
610function x
611{
612	nameref nz=$1
613	float x y
614	float x=$((log10(nz))) y=$((log10($nz)))
615	(( abs(x-y) < 1e-10 )) || err_exit '$nz and nz differs in arithmetic expression when nz is reference to array instance'
616}
617x z[1]
618
619unset x
620float x
621x=$( ($SHELL -c 'print -- $(( asinh(acosh(atanh(sin(cos(tan(atan(acos(asin(tanh(cosh(sinh(asinh(acosh(atanh(sin(cos(tan(atan(acos(asin(tanh(cosh(sinh(.5)))))))))))))))))))))))) )) ';:) 2> /dev/null)
622(( abs(x-.5) < 1.e-10 )) || err_exit 'bug in composite function evaluation'
623
624unset x
625typeset -X x=16
626{ (( $x == 16 )) ;} 2> /dev/null || err_exit 'expansions of hexfloat not working in arithmetic expansions'
627
628unset foo
629function foobar
630{
631	(( foo = 8))
632}
633typeset -i foo
634foobar
635(( foo == 8 )) || err_exit  'arithmetic assignment binding to the wrong scope'
636
637(( tgamma(4)/12 )) || err_exit 'floating point attribute for functions not preserved'
638
639unset F
640function f
641{
642 ((F=1))
643}
644f
645[[ $F == 1 ]] || err_exit 'scoping bug with arithmetic expression'
646
647F=1
648function f
649{
650 typeset F
651 ((F=2))
652}
653[[ $F == 1 ]] || err_exit 'scoping bug2 with arithmetic expression'
654
655unset play foo x z
656typeset -A play
657x=foo
658play[$x]=(x=2)
659for ((i=0; i < 2; i++))
660do	(( play[$x].y , z++  ))
661done
662(( z==2 )) || err_exit 'unset compound array variable error with for loop optimization'
663
664[[ $($SHELL 2> /dev/null -c 'print -- $(( ldexp(1, 4) ))' ) == 16 ]] || err_exit 'function ldexp not implement or not working correctly'
665
666
667$SHELL 2> /dev/null -c 'str="0x1.df768ed398ee1e01329a130627ae0000p-1";typeset -l -E x;((x=str))' || err_exit '((x=var)) fails for hexfloat with var begining with 0x1.nnn'
668
669x=(3 6 12)
670(( x[2] /= x[0]))
671(( x[2] == 4 ))  || err_exit '(( x[2] /= x[0])) fails for index array'
672
673x=([0]=3 [1]=6 [2]=12)
674(( x[2] /= x[0]))
675(( x[2] == 4 )) || err_exit '(( x[2] /= x[0])) fails for associative array'
676
677got=$($SHELL 2> /dev/null -c 'compound -a x;compound -a x[0].y; integer -a x[0].y[0].z; (( x[0].y[0].z[2]=3 )); typeset -p x')
678exp='typeset -C -a x=((typeset -C -a y=( [0]=(typeset -a -l -i z=([2]=3);));))'
679[[ $got == "$exp" ]] || err_exit '(( x[0].y[0].z[2]=3 )) not working'
680
681unset x
682let x=010
683[[ $x == 10 ]] || err_exit 'let treating 010 as octal'
684set -o letoctal
685let x=010
686[[ $x == 8 ]] || err_exit 'let not treating 010 as octal with letoctal on'
687
688float z=0
689integer aa=2 a=1
690typeset -A A
691A[a]=(typeset -A AA)
692A[a].AA[aa]=1
693(( z= A[a].AA[aa]++ ))
694(( z == 1 )) ||  err_exit "z should be 1 but is $z for associative array of
695associative array arithmetic"
696[[ ${A[a].AA[aa]} == 2 ]] || err_exit '${A[a].AA[aa]} should be 2 after ++ operation for associative array of associative array arithmetic'
697unset A[a]
698
699A[a]=(typeset -a AA)
700A[a].AA[aa]=1
701(( z += A[a].AA[aa++]++ ))
702(( z == 2 )) ||  err_exit "z should be 2 but is $z for associative array of
703index array arithmetic"
704(( aa == 3 )) || err_exit "subscript aa should be 3 but is $aa after ++"
705[[ ${A[a].AA[aa-1]} == 2 ]] || err_exit '${A[a].AA[aa]} should be 2 after ++ operation for ssociative array of index array arithmetic'
706unset A
707
708typeset -a A
709A[a]=(typeset -A AA)
710A[a].AA[aa]=1
711(( z += A[a].AA[aa]++ ))
712(( z == 3 )) ||  err_exit "z should be 3 but is $z for index array of
713associative array arithmetic"
714[[ ${A[a].AA[aa]} == 2 ]] || err_exit '${A[a].AA[aa]} should be 2 after ++ operation for index array of associative array arithmetic'
715unset A[a]
716
717A[a]=(typeset -a AA)
718A[a].AA[aa]=1
719(( z += A[a++].AA[aa++]++ ))
720(( z == 4 )) ||  err_exit "z should be 4 but is $z for index array of
721index array arithmetic"
722[[ ${A[a-1].AA[aa-1]} == 2 ]] || err_exit '${A[a].AA[aa]} should be 2 after ++ operation for index array of index array arithmetic'
723(( aa == 4 )) || err_exit "subscript aa should be 4 but is $aa after ++"
724(( a == 2 )) || err_exit "subscript a should be 2 but is $a after ++"
725unset A
726
727unset r x
728integer x
729r=020
730(($r == 16)) || err_exit 'leading 0 not treated as octal inside ((...))'
731x=$(($r))
732(( x == 16 )) || err_exit 'leading 0 not treated as octal inside $((...))'
733x=$r
734((x == 20 )) || err_exit 'leading 0 should not be treated as octal outside ((...))'
735print -- -020 | read x
736((x == -20)) || err_exit 'numbers with leading -0 should not be treated as octal outside ((...))'
737print -- -8#20 | read x
738((x == -16)) || err_exit 'numbers with leading -8# should be treated as octal'
739
740unset x
741x=0x1
742let "$x==1" || err_exit 'hex constants not working with let'
743(( $x == 1 )) || err_exit 'arithmetic with $x, where $x is hex constant not working'
744for i in 1
745do	(($x == 1)) || err_exit 'arithmetic in for loop with $x, where $x is hex constant not working'
746done
747x=010
748let "$x==10" || err_exit 'arithmetic with $x where $x is 010 should be decimal in let'
749(( 9.$x == 9.01 )) || err_exit 'arithmetic with 9.$x where x=010 should be 9.01'
750(( 9$x == 9010 )) || err_exit 'arithmetic with 9$x where x=010 should be 9010'
751x010=99
752((x$x == 99 )) || err_exit 'arithtmetic with x$x where x=010 should be $x010'
753(( 3+$x == 11 )) || err_exit '3+$x where x=010 should be 11 in ((...))'
754let "(3+$x)==13" || err_exit 'let should not recognize leading 0 as octal'
755unset x
756typeset -RZ3 x=10
757(( $x == 10 )) || err_exit 'leading 0 in -RZ should not create octal constant with ((...))'
758let "$x==10" || err_exit 'leading 0 in -RZ should not create octal constant with let'
759
760unset v x
761x=0x1.0000000000000000000000000000p+6
762v=$(printf $'%.28a\n' 64)
763[[ $v == "$x" ]] || err_exit "'printf %.28a 64' failed -- expected '$x', got '$v'"
764
765exit $((Errors<125?Errors:125))
766