Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/ksh93/tests/arith.sh
1810 views
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 <[email protected]> #
18
# #
19
########################################################################
20
function err_exit
21
{
22
print -u2 -n "\t"
23
print -u2 -r ${Command}[$1]: "${@:2}"
24
let Errors+=1
25
}
26
alias err_exit='err_exit $LINENO'
27
28
Command=${0##*/}
29
integer Errors=0
30
31
tmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
32
trap "cd /; rm -rf $tmp" EXIT
33
34
trap '' FPE # NOTE: osf.alpha requires this (no ieee math)
35
36
integer x=1 y=2 z=3
37
if (( 2+2 != 4 ))
38
then err_exit 2+2!=4
39
fi
40
if ((x+y!=z))
41
then err_exit x+y!=z
42
fi
43
if (($x+$y!=$z))
44
then err_exit $x+$y!=$z
45
fi
46
if (((x|y)!=z))
47
then err_exit "(x|y)!=z"
48
fi
49
if ((y >= z))
50
then err_exit "y>=z"
51
fi
52
if ((y+3 != z+2))
53
then err_exit "y+3!=z+2"
54
fi
55
if ((y<<2 != 1<<3))
56
then err_exit "y<<2!=1<<3"
57
fi
58
if ((133%10 != 3))
59
then err_exit "133%10!=3"
60
if (( 2.5 != 2.5 ))
61
then err_exit 2.5!=2.5
62
fi
63
fi
64
d=0
65
((d || 1)) || err_exit 'd=0; ((d||1))'
66
if (( d++!=0))
67
then err_exit "d++!=0"
68
fi
69
if (( --d!=0))
70
then err_exit "--d!=0"
71
fi
72
if (( (d++,6)!=6 && d!=1))
73
then err_exit '(d++,6)!=6 && d!=1'
74
fi
75
d=0
76
if (( (1?2+1:3*4+d++)!=3 || d!=0))
77
then err_exit '(1?2+1:3*4+d++) !=3'
78
fi
79
for ((i=0; i < 20; i++))
80
do :
81
done
82
if (( i != 20))
83
then err_exit 'for (( expr)) failed'
84
fi
85
for ((i=0; i < 20; i++)); do : ; done
86
if (( i != 20))
87
then err_exit 'for (( expr));... failed'
88
fi
89
for ((i=0; i < 20; i++)) do : ; done
90
if (( i != 20))
91
then err_exit 'for (( expr))... failed'
92
fi
93
if (( (i?0:1) ))
94
then err_exit '(( (i?0:1) )) failed'
95
fi
96
if (( (1 || 1 && 0) != 1 ))
97
then err_exit '( (1 || 1 && 0) != 1) failed'
98
fi
99
if (( (_=1)+(_x=0)-_ ))
100
then err_exit '(_=1)+(_x=0)-_ failed'
101
fi
102
if (( (3^6) != 5))
103
then err_exit '((3^6) != 5) failed'
104
fi
105
integer x=1
106
if (( (x=-x) != -1 ))
107
then err_exit '(x=-x) != -1 failed'
108
fi
109
i=2
110
if (( 1$(($i))3 != 123 ))
111
then err_exit ' 1$(($i))3 failed'
112
fi
113
((pi=4*atan(1.)))
114
point=(
115
float x
116
float y
117
)
118
(( point.x = cos(pi/6), point.y = sin(pi/6) ))
119
if (( point.x*point.x + point.y*point.y > 1.01 ))
120
then err_exit 'cos*cos +sin*sin > 1.01'
121
fi
122
if (( point.x*point.x + point.y*point.y < .99 ))
123
then err_exit 'cos*cos +sin*sin < .99'
124
fi
125
if [[ $((y=x=1.5)) != 1 ]]
126
then err_exit 'typecast not working in arithmetic evaluation'
127
fi
128
typeset -E x=1.5
129
( ((x++)) ) 2>/dev/null
130
if [[ $? == 0 ]]
131
then err_exit 'postincrement of floating point allowed'
132
fi
133
( ((++x)) ) 2>/dev/null
134
if [[ $? == 0 ]]
135
then err_exit 'preincrement of floating point allowed'
136
fi
137
x=1.5
138
( ((x%1.1)) ) 2>/dev/null
139
if [[ $? == 0 ]]
140
then err_exit 'floating point allowed with % operator'
141
fi
142
x=.125
143
if [[ $(( 4 * x/2 )) != 0.25 ]]
144
then err_exit '(( 4 * x/2 )) is not 0.25, with x=.125'
145
fi
146
if [[ $(( pow(2,3) )) != 8 ]]
147
then err_exit '$(( pow(2,3) )) != 8'
148
fi
149
( [[ $(( pow(2,(3)) )) == 8 ]] ) 2> /dev/null
150
if (( $? ))
151
then err_exit '$(( pow(2,(3)) )) != 8'
152
fi
153
unset x
154
integer x=1; integer x=1
155
if [[ $x != 1 ]]
156
then err_exit 'two consecutive integer x=1 not working'
157
fi
158
unset z
159
{ z=$(typeset -RZ2 z2; (( z2 = 8 )); print $z2) ;} 2>/dev/null
160
if [[ $z != "08" ]]
161
then err_exit "typeset -RZ2 leading 0 decimal not working [z=$z]"
162
fi
163
{ z=$(typeset -RZ3 z3; (( z3 = 8 )); print $z3) ;} 2>/dev/null
164
if [[ $z != "008" ]]
165
then err_exit "typeset -RZ3 leading 0 decimal not working [z=$z]"
166
fi
167
unset z
168
typeset -Z3 z=010
169
(( z=z+1))
170
if [[ $z != 011 ]]
171
then err_exit "leading 0's in -Z not treated as decimal"
172
fi
173
unset x
174
integer x=0
175
if [[ $((x+=1)) != 1 ]] || ((x!=1))
176
then err_exit "+= not working"
177
x=1
178
fi
179
x=1
180
if [[ $((x*=5)) != 5 ]] || ((x!=5))
181
then err_exit "*= not working"
182
x=5
183
fi
184
if [[ $((x%=4)) != 1 ]] || ((x!=1))
185
then err_exit "%= not working"
186
x=1
187
fi
188
if [[ $((x|=6)) != 7 ]] || ((x!=7))
189
then err_exit "|= not working"
190
x=7
191
fi
192
if [[ $((x&=5)) != 5 ]] || ((x!=5))
193
then err_exit "&= not working"
194
x=5
195
fi
196
function newscope
197
{
198
float x=1.5
199
(( x += 1 ))
200
print -r -- $x
201
}
202
if [[ $(newscope) != 2.5 ]]
203
then err_exit "arithmetic using wrong scope"
204
fi
205
unset x
206
integer y[3]=9 y[4]=2 i=3
207
(( x = y[3] + y[4] ))
208
if [[ $x != 11 ]]
209
then err_exit "constant index array arithmetic failure"
210
fi
211
(( x = $empty y[3] + y[4] ))
212
if [[ $x != 11 ]]
213
then err_exit "empty constant index array arithmetic failure"
214
fi
215
(( x = y[i] + y[i+1] ))
216
if [[ $x != 11 ]]
217
then err_exit "variable subscript index array arithmetic failure"
218
fi
219
integer a[5]=3 a[2]=4
220
(( x = y[a[5]] + y[a[2]] ))
221
if [[ $x != 11 ]]
222
then err_exit "nested subscript index array arithmetic failure"
223
fi
224
unset y
225
typeset -Ai y
226
y[three]=9 y[four]=2
227
three=four
228
four=three
229
(( x = y[three] + y[four] ))
230
if [[ $x != 11 ]]
231
then err_exit "constant associative array arithmetic failure"
232
fi
233
(( x = y[$three] + y[$four] ))
234
if [[ $x != 11 ]]
235
then err_exit "variable subscript associative array arithmetic failure"
236
fi
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"
239
unset z
240
float z=7.5
241
if { (( z%2 != 1));} 2> /dev/null
242
then err_exit '% not working on floating point'
243
fi
244
chr=(a ' ' '=' '\r' '\n' '\\' '\"' '$' "\\'" '[' ']' '(' ')' '<' '\xab' '\040' '`' '{' '}' '*' '\E')
245
if (('a' == 97))
246
then val=(97 32 61 13 10 92 34 36 39 91 93 40 41 60 171 32 96 123 125 42 27)
247
else val=(129 64 126 13 21 224 127 91 125 173 189 77 93 76 171 32 121 192 208 92 39 21)
248
fi
249
q=0
250
for ((i=0; i < ${#chr[@]}; i++))
251
do 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
266
done
267
unset x
268
typeset -ui x=4294967293
269
[[ $x != 4294967293 ]] && err_exit "unsigned integers not working"
270
x=32767
271
x=x+1
272
[[ $x != 32768 ]] && err_exit "unsigned integer addition not working"
273
unset x
274
float x=99999999999999999999999999
275
if (( x < 1e20 ))
276
then err_exit 'large integer constants not working'
277
fi
278
unset x y
279
function foobar
280
{
281
nameref x=$1
282
(( x +=1 ))
283
print $x
284
}
285
x=0 y=4
286
if [[ $(foobar y) != 5 ]]
287
then err_exit 'name references in arithmetic statements in functions broken'
288
fi
289
if (( 2**3 != pow(2,3) ))
290
then err_exit '2**3 not working'
291
fi
292
if (( 2**3*2 != pow(2,3)*2 ))
293
then err_exit '2**3*2 not working'
294
fi
295
if (( 4**3**2 != pow(4,pow(3,2)) ))
296
then err_exit '4**3**2 not working'
297
fi
298
if (( (4**3)**2 != pow(pow(4,3),2) ))
299
then err_exit '(4**3)**2 not working'
300
fi
301
typeset -Z3 x=11
302
typeset -i x
303
if (( x != 11 ))
304
then err_exit '-Z3 not treated as decimal'
305
fi
306
unset x
307
typeset -ui x=-1
308
(( x >= 0 )) || err_exit 'unsigned integer not working'
309
(( $x >= 0 )) || err_exit 'unsigned integer not working as $x'
310
unset x
311
typeset -ui42 x=50
312
if [[ $x != 42#18 ]]
313
then err_exit 'display of unsigned integers in non-decimal bases wrong'
314
fi
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'
317
typeset -E20 val=123.01234567890
318
[[ $val == 123.0123456789 ]] || err_exit "rounding error val=$val"
319
if [[ $(print x$((10))=foo) != x10=foo ]]
320
then err_exit 'parsing error with x$((10))=foo'
321
fi
322
$SHELL -c 'typeset x$((10))=foo' 2> /dev/null || err_exit 'typeset x$((10)) parse error'
323
unset x
324
x=$(( exp(log(2.0)) ))
325
(( x > 1.999 && x < 2.001 )) || err_exit 'composite functions not working'
326
unset x y n
327
typeset -Z8 x=0 y=0
328
integer n
329
for (( n=0; n < 20; n++ ))
330
do let "x = $x+1"
331
(( y = $y+1 ))
332
done
333
(( x == n )) || err_exit 'let with zero filled fields not working'
334
(( y == n )) || err_exit '((...)) with zero filled fields not working'
335
typeset -RZ3 x=10
336
[[ $(($x)) == 10 && $((1$x)) == 1010 ]] || err_exit 'zero filled fields not preserving leading zeros'
337
unset 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'
339
unset i ip ipx
340
typeset -i hex=( 172 30 18 1)
341
typeset -ui ip=0 ipx=0
342
integer i
343
for ((i=0; i < 4; i++))
344
do (( ip = (ip<<8) | hex[i]))
345
done
346
for ((i=0; i < 4; i++))
347
do (( ipx = ip % 256 ))
348
(( ip /= 256 ))
349
(( ipx != hex[3-i] )) && err_exit "hex digit $((3-i)) not correct"
350
done
351
unset x
352
x=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'
355
unset x
356
typeset -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'
359
typeset -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'
362
c010=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"
365
i=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
370
unset i; typeset -i i=01-2
371
(( i == -1 )) || err_exit "01-2 is not -1"
372
373
cat > $tmp/script <<-\!
374
tests=$*
375
typeset -A blop
376
function blop.get
377
{
378
.sh.value=777
379
}
380
function 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
}
417
mkobj bla
418
!
419
chmod +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'
427
unset foo
428
typeset -F1 foo=123456789.19
429
[[ $foo == 123456789.2 ]] || err_exit 'typeset -F1 not working correctly'
430
431
# divide by zero
432
433
for expr in '1/(1/2)' '8%(1/2)' '8%(1.0/2)'
434
do [[ $( ( $SHELL -c "( (($expr)) ) || print ok" ) 2>/dev/null ) == ok ]] || err_exit "divide by zero not trapped: $expr"
435
done
436
437
for expr in '1/(1.0/2)' '1/(1/2.0)'
438
do [[ $( ( $SHELL -c "( print -r -- \$(($expr)) )" ) 2>/dev/null ) == 2 ]] || err_exit "invalid value for: $expr"
439
done
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'
443
unset 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'
447
if (( Inf+1 == Inf ))
448
then 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'
473
else err_exit 'Inf and NaN not working'
474
fi
475
unset x y n r
476
n=14.555
477
float x=$n y
478
y=$(printf "%a" x)
479
r=$y
480
[[ $r == $n ]] || err_exit "output of printf %a not self preserving -- expected $x, got $y"
481
unset x y r
482
float x=-0 y=-0.0
483
r=-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
494
float t
495
typeset a b r
496
v="-0.0 0.0 +0.0 -1.0 1.0 +1.0"
497
for a in $v
498
do 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
503
done
504
505
typeset -l y y_ascii
506
(( y=sin(90) ))
507
y_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
528
EOF) 2> /dev/null || err_exit 'error with comma expression'
529
530
N=(89551 89557)
531
i=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
535
unset a x
536
x=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'
540
unset a
541
x=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'
545
unset a
546
x=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
551
unset a i
552
typeset -a a
553
i=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
558
unset a
559
typeset -i a
560
for ((i=0;i<1000;i++))
561
do ((a[RANDOM%2]++))
562
done
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
568
function .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
}
584
EOF
585
586
587
588
function .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
599
enum color_t=(red green blue yellow)
600
color_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
608
unset z
609
integer -a z=( [1]=90 )
610
function 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
}
617
x z[1]
618
619
unset x
620
float x
621
x=$( ($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
624
unset x
625
typeset -X x=16
626
{ (( $x == 16 )) ;} 2> /dev/null || err_exit 'expansions of hexfloat not working in arithmetic expansions'
627
628
unset foo
629
function foobar
630
{
631
(( foo = 8))
632
}
633
typeset -i foo
634
foobar
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
639
unset F
640
function f
641
{
642
((F=1))
643
}
644
f
645
[[ $F == 1 ]] || err_exit 'scoping bug with arithmetic expression'
646
647
F=1
648
function f
649
{
650
typeset F
651
((F=2))
652
}
653
[[ $F == 1 ]] || err_exit 'scoping bug2 with arithmetic expression'
654
655
unset play foo x z
656
typeset -A play
657
x=foo
658
play[$x]=(x=2)
659
for ((i=0; i < 2; i++))
660
do (( play[$x].y , z++ ))
661
done
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
669
x=(3 6 12)
670
(( x[2] /= x[0]))
671
(( x[2] == 4 )) || err_exit '(( x[2] /= x[0])) fails for index array'
672
673
x=([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
677
got=$($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')
678
exp='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
681
unset x
682
let x=010
683
[[ $x == 10 ]] || err_exit 'let treating 010 as octal'
684
set -o letoctal
685
let x=010
686
[[ $x == 8 ]] || err_exit 'let not treating 010 as octal with letoctal on'
687
688
float z=0
689
integer aa=2 a=1
690
typeset -A A
691
A[a]=(typeset -A AA)
692
A[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
695
associative 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'
697
unset A[a]
698
699
A[a]=(typeset -a AA)
700
A[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
703
index 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'
706
unset A
707
708
typeset -a A
709
A[a]=(typeset -A AA)
710
A[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
713
associative 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'
715
unset A[a]
716
717
A[a]=(typeset -a AA)
718
A[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
721
index 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 ++"
725
unset A
726
727
unset r x
728
integer x
729
r=020
730
(($r == 16)) || err_exit 'leading 0 not treated as octal inside ((...))'
731
x=$(($r))
732
(( x == 16 )) || err_exit 'leading 0 not treated as octal inside $((...))'
733
x=$r
734
((x == 20 )) || err_exit 'leading 0 should not be treated as octal outside ((...))'
735
print -- -020 | read x
736
((x == -20)) || err_exit 'numbers with leading -0 should not be treated as octal outside ((...))'
737
print -- -8#20 | read x
738
((x == -16)) || err_exit 'numbers with leading -8# should be treated as octal'
739
740
unset x
741
x=0x1
742
let "$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'
744
for i in 1
745
do (($x == 1)) || err_exit 'arithmetic in for loop with $x, where $x is hex constant not working'
746
done
747
x=010
748
let "$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'
751
x010=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 ((...))'
754
let "(3+$x)==13" || err_exit 'let should not recognize leading 0 as octal'
755
unset x
756
typeset -RZ3 x=10
757
(( $x == 10 )) || err_exit 'leading 0 in -RZ should not create octal constant with ((...))'
758
let "$x==10" || err_exit 'leading 0 in -RZ should not create octal constant with let'
759
760
unset v x
761
x=0x1.0000000000000000000000000000p+6
762
v=$(printf $'%.28a\n' 64)
763
[[ $v == "$x" ]] || err_exit "'printf %.28a 64' failed -- expected '$x', got '$v'"
764
765
exit $((Errors<125?Errors:125))
766
767