]>
Commit | Line | Data |
---|---|---|
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 | 14 | set +o posix |
ccc6cda3 JA |
15 | declare -i iv jv |
16 | ||
17 | iv=$(( 3 + 5 * 32 )) | |
18 | echo $iv | |
19 | iv=iv+3 | |
20 | echo $iv | |
21 | iv=2 | |
22 | jv=iv | |
23 | ||
24 | let "jv *= 2" | |
25 | echo $jv | |
26 | jv=$(( $jv << 2 )) | |
27 | echo $jv | |
28 | ||
29 | let jv="$jv / 2" | |
30 | echo $jv | |
31 | jv="jv >> 2" | |
32 | echo $jv | |
33 | ||
34 | iv=$((iv+ $jv)) | |
35 | echo $iv | |
36 | echo $((iv -= jv)) | |
37 | echo $iv | |
38 | echo $(( iv == jv )) | |
39 | echo $(( iv != $jv )) | |
40 | echo $(( iv < jv )) | |
41 | echo $(( $iv > $jv )) | |
42 | echo $(( iv <= $jv )) | |
43 | echo $(( $iv >= jv )) | |
44 | ||
45 | echo $jv | |
46 | echo $(( ~$jv )) | |
47 | echo $(( ~1 )) | |
48 | echo $(( ! 0 )) | |
49 | ||
50 | echo $(( jv % 2 )) | |
51 | echo $(( $iv % 4 )) | |
52 | ||
53 | echo $(( iv <<= 16 )) | |
54 | echo $(( iv %= 33 )) | |
55 | ||
56 | echo $(( 33 & 55 )) | |
57 | echo $(( 33 | 17 )) | |
58 | ||
59 | echo $(( iv && $jv )) | |
60 | echo $(( $iv || jv )) | |
61 | ||
62 | echo $(( iv && 0 )) | |
63 | echo $(( iv & 0 )) | |
64 | echo $(( iv && 1 )) | |
65 | echo $(( iv & 1 )) | |
66 | ||
67 | echo $(( $jv || 0 )) | |
68 | echo $(( jv | 0 )) | |
69 | echo $(( jv | 1 )) | |
70 | echo $(( $jv || 1 )) | |
71 | ||
72 | let 'iv *= jv' | |
73 | echo $iv | |
74 | echo $jv | |
75 | let "jv += $iv" | |
76 | echo $jv | |
77 | ||
78 | echo $(( jv /= iv )) | |
79 | echo $(( jv <<= 8 )) | |
80 | echo $(( jv >>= 4 )) | |
81 | ||
82 | echo $(( iv |= 4 )) | |
83 | echo $(( iv &= 4 )) | |
84 | ||
85 | echo $(( iv += (jv + 9))) | |
86 | echo $(( (iv + 4) % 7 )) | |
87 | ||
88 | # unary plus, minus | |
89 | echo $(( +4 - 8 )) | |
90 | echo $(( -4 + 8 )) | |
91 | ||
92 | # conditional expressions | |
93 | echo $(( 4<5 ? 1 : 32)) | |
94 | echo $(( 4>5 ? 1 : 32)) | |
95 | echo $(( 4>(2+3) ? 1 : 32)) | |
96 | echo $(( 4<(2+3) ? 1 : 32)) | |
97 | echo $(( (2+2)<(2+3) ? 1 : 32)) | |
98 | echo $(( (2+2)>(2+3) ? 1 : 32)) | |
99 | ||
3185942a JA |
100 | # bug in bash versions through bash-3.2 |
101 | S=105 | |
102 | W=$((S>99?4:S>9?3:S>0?2:0)) | |
103 | echo $W | |
104 | unset W S | |
105 | ||
ccc6cda3 JA |
106 | # check that the unevaluated part of the ternary operator does not do |
107 | # evaluation or assignment | |
108 | x=i+=2 | |
109 | y=j+=2 | |
110 | declare -i i=1 j=1 | |
111 | echo $((1 ? 20 : (x+=2))) | |
112 | echo $i,$x | |
113 | echo $((0 ? (y+=2) : 30)) | |
114 | echo $j,$y | |
115 | ||
116 | x=i+=2 | |
117 | y=j+=2 | |
118 | declare -i i=1 j=1 | |
119 | echo $((1 ? 20 : (x+=2))) | |
120 | echo $i,$x | |
121 | echo $((0 ? (y+=2) : 30)) | |
122 | echo $i,$y | |
123 | ||
124 | # check precedence of assignment vs. conditional operator | |
125 | # should be an error | |
126 | declare -i x=2 | |
127 | y=$((1 ? 20 : x+=2)) | |
128 | ||
129 | # check precedence of assignment vs. conditional operator | |
130 | declare -i x=2 | |
131 | echo $((0 ? x+=2 : 20)) | |
132 | ||
133 | # associativity of assignment-operator operator | |
134 | declare -i i=1 j=2 k=3 | |
135 | echo $((i += j += k)) | |
136 | echo $i,$j,$k | |
137 | ||
138 | # octal, hex | |
139 | echo $(( 0x100 | 007 )) | |
140 | echo $(( 0xff )) | |
141 | echo $(( 16#ff )) | |
142 | echo $(( 16#FF/2 )) | |
143 | echo $(( 8#44 )) | |
144 | ||
145 | echo $(( 8 ^ 32 )) | |
146 | ||
147 | # other bases | |
148 | echo $(( 16#a )) | |
149 | echo $(( 32#a )) | |
150 | echo $(( 56#a )) | |
151 | echo $(( 64#a )) | |
152 | ||
153 | echo $(( 16#A )) | |
154 | echo $(( 32#A )) | |
155 | echo $(( 56#A )) | |
156 | echo $(( 64#A )) | |
157 | ||
ccc6cda3 | 158 | echo $(( 64#@ )) |
f73dda09 | 159 | echo $(( 64#_ )) |
ccc6cda3 JA |
160 | |
161 | # weird bases | |
162 | echo $(( 3425#56 )) | |
163 | ||
8868edaf | 164 | # missing number after base now generates an error |
ccc6cda3 JA |
165 | echo $(( 2# )) |
166 | ||
167 | # these should generate errors | |
168 | echo $(( 7 = 43 )) | |
169 | echo $(( 2#44 )) | |
170 | echo $(( 44 / 0 )) | |
171 | let 'jv += $iv' | |
172 | echo $(( jv += \$iv )) | |
173 | let 'rv = 7 + (43 * 6' | |
174 | ||
175 | # more errors | |
176 | declare -i i | |
177 | i=0#4 | |
178 | i=2#110#11 | |
d166f048 JA |
179 | |
180 | ((echo abc; echo def;); echo ghi) | |
181 | ||
182 | if (((4+4) + (4 + 7))); then | |
183 | echo ok | |
184 | fi | |
185 | ||
186 | (()) # make sure the null expression works OK | |
187 | ||
188 | a=(0 2 4 6) | |
189 | echo $(( a[1] + a[2] )) | |
190 | echo $(( (a[1] + a[2]) == a[3] )) | |
191 | (( (a[1] + a[2]) == a[3] )) ; echo $? | |
192 | ||
193 | # test pushing and popping the expression stack | |
194 | unset A | |
195 | A="4 + " | |
196 | echo $(( ( 4 + A ) + 4 )) | |
197 | A="3 + 5" | |
198 | echo $(( ( 4 + A ) + 4 )) | |
199 | ||
200 | # badly-formed conditional expressions | |
201 | echo $(( 4 ? : $A )) | |
202 | echo $(( 1 ? 20 )) | |
203 | echo $(( 4 ? 20 : )) | |
204 | ||
205 | # precedence and short-circuit evaluation | |
206 | B=9 | |
207 | echo $B | |
208 | ||
209 | echo $(( 0 && B=42 )) | |
210 | echo $B | |
211 | ||
212 | echo $(( 1 || B=88 )) | |
213 | echo $B | |
214 | ||
215 | echo $(( 0 && (B=42) )) | |
216 | echo $B | |
217 | ||
218 | echo $(( (${$} - $$) && (B=42) )) | |
219 | echo $B | |
220 | ||
221 | echo $(( 1 || (B=88) )) | |
222 | echo $B | |
223 | ||
224 | # until command with (( )) command | |
225 | x=7 | |
226 | ||
227 | echo $x | |
228 | until (( x == 4 )) | |
229 | do | |
230 | echo $x | |
231 | x=4 | |
232 | done | |
233 | ||
234 | echo $x | |
cce855bc JA |
235 | |
236 | # exponentiation | |
237 | echo $(( 2**15 - 1)) | |
238 | echo $(( 2**(16-1))) | |
239 | echo $(( 2**16*2 )) | |
240 | echo $(( 2**31-1)) | |
241 | echo $(( 2**0 )) | |
bb70624e JA |
242 | |
243 | # {pre,post}-{inc,dec}rement and associated errors | |
244 | ||
245 | x=4 | |
246 | ||
247 | echo $x | |
248 | echo $(( x++ )) | |
249 | echo $x | |
250 | echo $(( x-- )) | |
251 | echo $x | |
252 | ||
253 | echo $(( --x )) | |
254 | echo $x | |
255 | ||
256 | echo $(( ++x )) | |
257 | echo $x | |
258 | ||
259 | echo $(( ++7 )) | |
260 | echo $(( 7-- )) | |
261 | ||
262 | echo $(( --x=7 )) | |
263 | echo $(( ++x=7 )) | |
264 | ||
265 | echo $(( x++=7 )) | |
266 | echo $(( x--=7 )) | |
267 | ||
268 | echo $x | |
269 | ||
270 | echo $(( +7 )) | |
271 | echo $(( -7 )) | |
272 | ||
273 | echo $(( ++7 )) | |
274 | echo $(( --7 )) | |
275 | ||
a0c0a00f CR |
276 | # combinations of expansions |
277 | echo $(( "`echo 1+1`" )) | |
278 | echo $(( `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 | |
286 | echo $(( 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 |
302 | x=4 |
303 | y=7 | |
304 | ||
305 | (( x=8 , y=12 )) | |
306 | ||
307 | echo $x $y | |
308 | ||
309 | # should be an error | |
310 | (( x=9 y=41 )) | |
311 | ||
312 | # These are errors | |
f73dda09 | 313 | unset b |
bb70624e JA |
314 | echo $((a b)) |
315 | ((a b)) | |
28ef6c31 JA |
316 | |
317 | n=42 | |
318 | printf "%d\n" $n | |
319 | printf "%i\n" $n | |
320 | echo $(( 8#$(printf "%o\n" $n) )) | |
321 | printf "%u\n" $n | |
322 | echo $(( 16#$(printf "%x\n" $n) )) | |
323 | echo $(( 16#$(printf "%X\n" $n) )) | |
b80f6443 | 324 | |
4491c030 CR |
325 | # allow reserved words after an arithmetic command just because |
326 | if ((expr)) then ((expr)) fi | |
327 | ||
d233b485 CR |
328 | # these are errors |
329 | foo=1 | |
330 | echo $(( 'foo' )) | |
331 | ||
b80f6443 JA |
332 | # causes longjmp botches through bash-2.05b |
333 | a[b[c]d]=e |