blob: bc6b341d1464287fa579fd47bdca51c33433b3fe [file] [log] [blame]
Denis Vlasenkob7aaae92009-04-02 20:17:49 +00001#ash# set +o posix
2#ash# declare -i iv jv
3
4echo "Format: 'expected actual'"
5
6iv=$(( 3 + 5 * 32 ))
7echo 163 $iv
8#ash# iv=iv+3
9#ash# echo 166 $iv
10iv=2
11jv=iv
12
13: $((jv *= 2)) ##hush## let "jv *= 2"
14echo 4 $jv
15jv=$(( $jv << 2 ))
16echo 16 $jv
17
18: $((jv=$jv / 2)) ##hush## let jv="$jv / 2"
19echo 8 $jv
20#ash# jv="jv >> 2"
21: $((jv=jv >> 2)) ##hush## let jv="jv >> 2"
22echo 2 $jv
23
24iv=$((iv+ $jv))
25echo 4 $iv
26echo 2 $((iv -= jv))
27echo 2 $iv
28echo 1 $(( iv == jv ))
29echo 0 $(( iv != $jv ))
30echo 0 $(( iv < jv ))
31echo 0 $(( $iv > $jv ))
32echo 1 $(( iv <= $jv ))
33echo 1 $(( $iv >= jv ))
34
35echo 2 $jv
36echo -3 $(( ~$jv ))
37echo -2 $(( ~1 ))
38echo 1 $(( ! 0 ))
39
40echo 0 $(( jv % 2 ))
41echo 2 $(( $iv % 4 ))
42
43echo 131072 $(( iv <<= 16 ))
44echo 29 $(( iv %= 33 ))
45
46echo 33 $(( 33 & 55 ))
47echo 49 $(( 33 | 17 ))
48
49echo 1 $(( iv && $jv ))
50echo 1 $(( $iv || jv ))
51
52echo 0 $(( iv && 0 ))
53echo 0 $(( iv & 0 ))
54echo 1 $(( iv && 1 ))
55echo 1 $(( iv & 1 ))
56
57echo 1 $(( $jv || 0 ))
58echo 2 $(( jv | 0 ))
59echo 3 $(( jv | 1 ))
60echo 1 $(( $jv || 1 ))
61
62: $((iv *= jv)) ##hush## let 'iv *= jv'
63echo 58 $iv
64echo 2 $jv
65: $((jv += $iv)) ##hush## let "jv += $iv"
66echo 60 $jv
67
68echo 1 $(( jv /= iv ))
69echo 256 $(( jv <<= 8 ))
70echo 16 $(( jv >>= 4 ))
71
72echo 62 $(( iv |= 4 ))
73echo 4 $(( iv &= 4 ))
74
75echo 29 $(( iv += (jv + 9)))
76echo 5 $(( (iv + 4) % 7 ))
77
Denys Vlasenkoa6ad3972010-05-22 00:26:06 +020078echo unary plus, minus
Denis Vlasenkob7aaae92009-04-02 20:17:49 +000079echo -4 $(( +4 - 8 ))
80echo 4 $(( -4 + 8 ))
81
Denys Vlasenkoa6ad3972010-05-22 00:26:06 +020082echo conditional expressions
Denis Vlasenkob7aaae92009-04-02 20:17:49 +000083echo 1 $(( 4<5 ? 1 : 32))
84echo 32 $(( 4>5 ? 1 : 32))
85echo 32 $(( 4>(2+3) ? 1 : 32))
86echo 1 $(( 4<(2+3) ? 1 : 32))
87echo 1 $(( (2+2)<(2+3) ? 1 : 32))
88echo 32 $(( (2+2)>(2+3) ? 1 : 32))
89
Denys Vlasenkoa6ad3972010-05-22 00:26:06 +020090echo 'check that parentheses in `cmd` are interpreted correctly'
91# \x28 is '('
92echo 3 $(( ( `printf '(\x28 1'` + `echo 2\)\)` ) ))
93
94echo check that the unevaluated part of the ternary operator does not do evaluation or assignment
Denis Vlasenkob7aaae92009-04-02 20:17:49 +000095x=i+=2
96y=j+=2
97#ash# declare -i i=1 j=1
98 i=1
99 j=1
100echo 20 $((1 ? 20 : (x+=2)))
101#ash# echo $i,$x # ash mishandles this
102echo 30 $((0 ? (y+=2) : 30))
103#ash# echo $j,$y # ash mishandles this
104
105x=i+=2
106y=j+=2
107#ash# declare -i i=1 j=1
108 i=1
109 j=1
110echo 20 $((1 ? 20 : (x+=2)))
111#ash# echo $i,$x # ash mishandles this
112echo 30 $((0 ? (y+=2) : 30))
113#ash# echo $i,$y # ash mishandles this
114
Denys Vlasenkoa6ad3972010-05-22 00:26:06 +0200115echo check precedence of assignment vs. conditional operator
Denis Vlasenkob7aaae92009-04-02 20:17:49 +0000116# should be an error
117#ash# declare -i x=2
118 x=2
119#ashnote# bash reports error but continues, ash aborts - using subshell to 'emulate' bash:
120( y=$((1 ? 20 : x+=2)) )
121
Denys Vlasenkoa6ad3972010-05-22 00:26:06 +0200122echo check precedence of assignment vs. conditional operator
Denis Vlasenkob7aaae92009-04-02 20:17:49 +0000123#ash# declare -i x=2
124 x=2
125# ash says "line NNN: syntax error: 0 ? x+=2 : 20"
126#ash# echo 20 $((0 ? x+=2 : 20))
127
Denys Vlasenkoa6ad3972010-05-22 00:26:06 +0200128echo associativity of assignment-operator operator
Denis Vlasenkob7aaae92009-04-02 20:17:49 +0000129#ash# declare -i i=1 j=2 k=3
130i=1
131j=2
132k=3
133echo 6 $((i += j += k))
134echo 6,5,3 $i,$j,$k
135
Denys Vlasenkoa6ad3972010-05-22 00:26:06 +0200136echo octal, hex
Denis Vlasenkob7aaae92009-04-02 20:17:49 +0000137echo 263 $(( 0x100 | 007 ))
138echo 255 $(( 0xff ))
139#ash# echo 255 $(( 16#ff ))
140#ash# echo 127 $(( 16#FF/2 ))
141#ash# echo 36 $(( 8#44 ))
142
143echo 40 $(( 8 ^ 32 ))
144
145#ash# # other bases
146#ash# echo 10 $(( 16#a ))
147#ash# echo 10 $(( 32#a ))
148#ash# echo 10 $(( 56#a ))
149#ash# echo 10 $(( 64#a ))
150#ash#
151#ash# echo 10 $(( 16#A ))
152#ash# echo 10 $(( 32#A ))
153#ash# echo 36 $(( 56#A ))
154#ash# echo 36 $(( 64#A ))
155#ash#
156#ash# echo 62 $(( 64#@ ))
157#ash# echo 63 $(( 64#_ ))
158
159#ash# # weird bases (error)
160#ash# echo $(( 3425#56 ))
161
162#ash# # missing number after base
163#ash# echo 0 $(( 2# ))
164
165# these should generate errors
166( echo $(( 7 = 43 )) )
167#ash# echo $(( 2#44 ))
168( echo $(( 44 / 0 )) )
169( let 'jv += $iv' )
170( echo $(( jv += \$iv )) )
171( let 'rv = 7 + (43 * 6' )
172
173#ash# # more errors
174#ash# declare -i i
175#ash# i=0#4
176#ash# i=2#110#11
177
178((echo abc; echo def;); echo ghi)
179
180#ash# if (((4+4) + (4 + 7))); then
181#ash# echo ok
182#ash# fi
183
184#ash# (()) # make sure the null expression works OK
185
186#ash# a=(0 2 4 6)
187#ash# echo 6 $(( a[1] + a[2] ))
188#ash# echo 1 $(( (a[1] + a[2]) == a[3] ))
189#ash# (( (a[1] + a[2]) == a[3] )) ; echo 0 $?
190
191# test pushing and popping the expression stack
192unset A
193A="4 + "
194( echo A $(( ( 4 + A ) + 4 )) )
195A="3 + 5"
196echo 16 $(( ( 4 + A ) + 4 ))
197
198# badly-formed conditional expressions
199( echo $(( 4 ? : $A )) )
200( echo $(( 1 ? 20 )) )
201( echo $(( 4 ? 20 : )) )
202
203# precedence and short-circuit evaluation
204B=9
205echo 9 $B
206
207# error
208( echo $(( 0 && B=42 )); echo 9 $B )
209
210# error
211( echo $(( 1 || B=88 )); echo 9 $B )
212
213# ash mistakenly evaluates B=... below
214#ash# echo 0 $(( 0 && (B=42) ))
215echo 9 $B
216#ash# echo 0 $(( (${$} - $$) && (B=42) ))
217echo 9 $B
218#ash# echo 1 $(( 1 || (B=88) ))
219echo 9 $B
220
221
222# until command with (( )) command
223x=7
224
225echo 7 $x
226#ash# until (( x == 4 ))
227 until test "$x" = 4
228do
229 echo $x
230 x=4
231done
232
233echo 4 $x
234
235# exponentiation
236echo 32767 $(( 2**15 - 1))
237echo 32768 $(( 2**(16-1)))
238echo 131072 $(( 2**16*2 ))
239echo 2147483647 $(( 2**31-1))
240echo 1 $(( 2**0 ))
241
242# {pre,post}-{inc,dec}rement and associated errors
243
244x=4
245
246echo 4 $x
247echo 4 $(( x++ ))
248echo 5 $x
249echo 5 $(( x-- ))
250echo 4 $x
251
252echo 3 $(( --x ))
253echo 3 $x
254
255echo 4 $(( ++x ))
256echo 4 $x
257
258# bash 3.2 apparently thinks that ++7 is 7
259#ash# echo 7 $(( ++7 ))
260( echo $(( 7-- )) )
261
262( echo $(( --x=7 )) )
263( echo $(( ++x=7 )) )
264
265( echo $(( x++=7 )) )
266( echo $(( x--=7 )) )
267
268echo 4 $x
269
270echo 7 $(( +7 ))
271echo -7 $(( -7 ))
272
273# bash 3.2 apparently thinks that ++7 is 7
274#ash# echo $(( ++7 ))
275#ash# echo $(( --7 ))
276
277${THIS_SH} ./arith1.sub
278${THIS_SH} ./arith2.sub
279
280x=4
281y=7
282
283#ash# (( x=8 , y=12 ))
284 x=8
285 y=12
286echo $x $y
287
288#ash# # should be an error
289#ash# (( x=9 y=41 ))
290
291# These are errors
292unset b
293( echo $((a b)) )
294#ash# ((a b))
295
296n=42
297printf "%d\n" $n
298printf "%i\n" $n
299#ash# echo $(( 8#$(printf "%o\n" $n) ))
300printf "%u\n" $n
301#ash# echo $(( 16#$(printf "%x\n" $n) ))
302#ash# echo $(( 16#$(printf "%X\n" $n) ))
303
304# causes longjmp botches through bash-2.05b
305a[b[c]d]=e