]> git.ipfire.org Git - thirdparty/bash.git/blame - tests/arith.tests
Bash-5.2-rc4 release
[thirdparty/bash.git] / tests / arith.tests
CommitLineData
8868edaf
CR
1# This program is free software: you can redistribute it and/or modify
2# it under the terms of the GNU General Public License as published by
3# the Free Software Foundation, either version 3 of the License, or
4# (at your option) any later version.
5#
6# This program is distributed in the hope that it will be useful,
7# but WITHOUT ANY WARRANTY; without even the implied warranty of
8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9# GNU General Public License for more details.
10#
11# You should have received a copy of the GNU General Public License
12# along with this program. If not, see <http://www.gnu.org/licenses/>.
13#
d166f048 14set +o posix
ccc6cda3
JA
15declare -i iv jv
16
17iv=$(( 3 + 5 * 32 ))
18echo $iv
19iv=iv+3
20echo $iv
21iv=2
22jv=iv
23
24let "jv *= 2"
25echo $jv
26jv=$(( $jv << 2 ))
27echo $jv
28
29let jv="$jv / 2"
30echo $jv
31jv="jv >> 2"
32echo $jv
33
34iv=$((iv+ $jv))
35echo $iv
36echo $((iv -= jv))
37echo $iv
38echo $(( iv == jv ))
39echo $(( iv != $jv ))
40echo $(( iv < jv ))
41echo $(( $iv > $jv ))
42echo $(( iv <= $jv ))
43echo $(( $iv >= jv ))
44
45echo $jv
46echo $(( ~$jv ))
47echo $(( ~1 ))
48echo $(( ! 0 ))
49
50echo $(( jv % 2 ))
51echo $(( $iv % 4 ))
52
53echo $(( iv <<= 16 ))
54echo $(( iv %= 33 ))
55
56echo $(( 33 & 55 ))
57echo $(( 33 | 17 ))
58
59echo $(( iv && $jv ))
60echo $(( $iv || jv ))
61
62echo $(( iv && 0 ))
63echo $(( iv & 0 ))
64echo $(( iv && 1 ))
65echo $(( iv & 1 ))
66
67echo $(( $jv || 0 ))
68echo $(( jv | 0 ))
69echo $(( jv | 1 ))
70echo $(( $jv || 1 ))
71
72let 'iv *= jv'
73echo $iv
74echo $jv
75let "jv += $iv"
76echo $jv
77
78echo $(( jv /= iv ))
79echo $(( jv <<= 8 ))
80echo $(( jv >>= 4 ))
81
82echo $(( iv |= 4 ))
83echo $(( iv &= 4 ))
84
85echo $(( iv += (jv + 9)))
86echo $(( (iv + 4) % 7 ))
87
88# unary plus, minus
89echo $(( +4 - 8 ))
90echo $(( -4 + 8 ))
91
92# conditional expressions
93echo $(( 4<5 ? 1 : 32))
94echo $(( 4>5 ? 1 : 32))
95echo $(( 4>(2+3) ? 1 : 32))
96echo $(( 4<(2+3) ? 1 : 32))
97echo $(( (2+2)<(2+3) ? 1 : 32))
98echo $(( (2+2)>(2+3) ? 1 : 32))
99
3185942a
JA
100# bug in bash versions through bash-3.2
101S=105
102W=$((S>99?4:S>9?3:S>0?2:0))
103echo $W
104unset W S
105
ccc6cda3
JA
106# check that the unevaluated part of the ternary operator does not do
107# evaluation or assignment
108x=i+=2
109y=j+=2
110declare -i i=1 j=1
111echo $((1 ? 20 : (x+=2)))
112echo $i,$x
113echo $((0 ? (y+=2) : 30))
114echo $j,$y
115
116x=i+=2
117y=j+=2
118declare -i i=1 j=1
119echo $((1 ? 20 : (x+=2)))
120echo $i,$x
121echo $((0 ? (y+=2) : 30))
122echo $i,$y
123
124# check precedence of assignment vs. conditional operator
125# should be an error
126declare -i x=2
127y=$((1 ? 20 : x+=2))
128
129# check precedence of assignment vs. conditional operator
130declare -i x=2
131echo $((0 ? x+=2 : 20))
132
133# associativity of assignment-operator operator
134declare -i i=1 j=2 k=3
135echo $((i += j += k))
136echo $i,$j,$k
137
138# octal, hex
139echo $(( 0x100 | 007 ))
140echo $(( 0xff ))
141echo $(( 16#ff ))
142echo $(( 16#FF/2 ))
143echo $(( 8#44 ))
144
145echo $(( 8 ^ 32 ))
146
147# other bases
148echo $(( 16#a ))
149echo $(( 32#a ))
150echo $(( 56#a ))
151echo $(( 64#a ))
152
153echo $(( 16#A ))
154echo $(( 32#A ))
155echo $(( 56#A ))
156echo $(( 64#A ))
157
ccc6cda3 158echo $(( 64#@ ))
f73dda09 159echo $(( 64#_ ))
ccc6cda3
JA
160
161# weird bases
162echo $(( 3425#56 ))
163
8868edaf 164# missing number after base now generates an error
ccc6cda3
JA
165echo $(( 2# ))
166
167# these should generate errors
168echo $(( 7 = 43 ))
169echo $(( 2#44 ))
170echo $(( 44 / 0 ))
171let 'jv += $iv'
172echo $(( jv += \$iv ))
173let 'rv = 7 + (43 * 6'
174
175# more errors
176declare -i i
177i=0#4
178i=2#110#11
d166f048
JA
179
180((echo abc; echo def;); echo ghi)
181
182if (((4+4) + (4 + 7))); then
183 echo ok
184fi
185
186(()) # make sure the null expression works OK
187
188a=(0 2 4 6)
189echo $(( a[1] + a[2] ))
190echo $(( (a[1] + a[2]) == a[3] ))
191(( (a[1] + a[2]) == a[3] )) ; echo $?
192
193# test pushing and popping the expression stack
194unset A
195A="4 + "
196echo $(( ( 4 + A ) + 4 ))
197A="3 + 5"
198echo $(( ( 4 + A ) + 4 ))
199
200# badly-formed conditional expressions
201echo $(( 4 ? : $A ))
202echo $(( 1 ? 20 ))
203echo $(( 4 ? 20 : ))
204
205# precedence and short-circuit evaluation
206B=9
207echo $B
208
209echo $(( 0 && B=42 ))
210echo $B
211
212echo $(( 1 || B=88 ))
213echo $B
214
215echo $(( 0 && (B=42) ))
216echo $B
217
218echo $(( (${$} - $$) && (B=42) ))
219echo $B
220
221echo $(( 1 || (B=88) ))
222echo $B
223
224# until command with (( )) command
225x=7
226
227echo $x
228until (( x == 4 ))
229do
230 echo $x
231 x=4
232done
233
234echo $x
cce855bc
JA
235
236# exponentiation
237echo $(( 2**15 - 1))
238echo $(( 2**(16-1)))
239echo $(( 2**16*2 ))
240echo $(( 2**31-1))
241echo $(( 2**0 ))
bb70624e
JA
242
243# {pre,post}-{inc,dec}rement and associated errors
244
245x=4
246
247echo $x
248echo $(( x++ ))
249echo $x
250echo $(( x-- ))
251echo $x
252
253echo $(( --x ))
254echo $x
255
256echo $(( ++x ))
257echo $x
258
259echo $(( ++7 ))
260echo $(( 7-- ))
261
262echo $(( --x=7 ))
263echo $(( ++x=7 ))
264
265echo $(( x++=7 ))
266echo $(( x--=7 ))
267
268echo $x
269
270echo $(( +7 ))
271echo $(( -7 ))
272
273echo $(( ++7 ))
274echo $(( --7 ))
275
a0c0a00f
CR
276# combinations of expansions
277echo $(( "`echo 1+1`" ))
278echo $(( `echo 1+1` ))
279
b80f6443
JA
280${THIS_SH} ./arith1.sub
281${THIS_SH} ./arith2.sub
495aee44
CR
282${THIS_SH} ./arith3.sub
283${THIS_SH} ./arith4.sub
284
285# make sure arithmetic expansion handles ints > 2**31 - 1 using intmax_t
286echo $(( 2147483645 + 4 ))
b80f6443 287
ac50fbac
CR
288# other tests using INTMAX_MIN and INTMAX_MAX that cause exceptions if not
289# handled correctly -- problem through bash-4.2
290${THIS_SH} ./arith5.sub
291
292# problems with suppressing evaluation present through bash-4.2
293${THIS_SH} ./arith6.sub
294
a0c0a00f
CR
295# problems with parsing arithmetic expressions containing colons that are
296# part of word expansions such as substring extraction
297${THIS_SH} ./arith7.sub
298
d233b485
CR
299# problems with evaluation of conditional expressions
300${THIS_SH} ./arith8.sub
301
bb70624e
JA
302x=4
303y=7
304
305(( x=8 , y=12 ))
306
307echo $x $y
308
309# should be an error
310(( x=9 y=41 ))
311
312# These are errors
f73dda09 313unset b
bb70624e
JA
314echo $((a b))
315((a b))
28ef6c31
JA
316
317n=42
318printf "%d\n" $n
319printf "%i\n" $n
320echo $(( 8#$(printf "%o\n" $n) ))
321printf "%u\n" $n
322echo $(( 16#$(printf "%x\n" $n) ))
323echo $(( 16#$(printf "%X\n" $n) ))
b80f6443 324
4491c030
CR
325# allow reserved words after an arithmetic command just because
326if ((expr)) then ((expr)) fi
327
d233b485
CR
328# these are errors
329foo=1
330echo $(( 'foo' ))
331
b80f6443
JA
332# causes longjmp botches through bash-2.05b
333a[b[c]d]=e