summaryrefslogtreecommitdiff
path: root/shell/ash_test/ash-arith/arith.tests
blob: 272fc3487816c86e275bbf0ae1287ee966a26c63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#ash# set +o posix
#ash# declare -i iv jv

echo "Format: 'expected actual'"

iv=$(( 3 + 5 * 32 ))
echo 163 $iv
#ash# iv=iv+3
#ash# echo 166 $iv
iv=2
jv=iv

let "jv *= 2"
echo 4 $jv
jv=$(( $jv << 2 ))
echo 16 $jv

let jv="$jv / 2"
echo 8 $jv
#ash# jv="jv >> 2"
      let jv="jv >> 2"
echo 2 $jv

iv=$((iv+ $jv))
echo 4 $iv
echo 2 $((iv -= jv))
echo 2 $iv
echo 1 $(( iv == jv ))
echo 0 $(( iv != $jv ))
echo 0 $(( iv < jv ))
echo 0 $(( $iv > $jv ))
echo 1 $(( iv <= $jv ))
echo 1 $(( $iv >= jv ))

echo 2 $jv
echo -3 $(( ~$jv ))
echo -2 $(( ~1 ))
echo 1 $(( ! 0 ))

echo 0 $(( jv % 2 ))
echo 2 $(( $iv % 4 ))

echo 131072 $(( iv <<= 16 ))
echo 29 $(( iv %= 33 ))

echo 33 $(( 33 & 55 ))
echo 49 $(( 33 | 17 ))

echo 1 $(( iv && $jv ))
echo 1 $(( $iv || jv ))

echo 0 $(( iv && 0 ))
echo 0 $(( iv & 0 ))
echo 1 $(( iv && 1 ))
echo 1 $(( iv & 1 ))

echo 1 $(( $jv || 0 ))
echo 2 $(( jv | 0 ))
echo 3 $(( jv | 1 ))
echo 1 $(( $jv || 1 ))

let 'iv *= jv'
echo 58 $iv
echo 2 $jv
let "jv += $iv"
echo 60 $jv

echo 1 $(( jv /= iv ))
echo 256 $(( jv <<= 8 ))
echo 16 $(( jv >>= 4 ))

echo 62 $(( iv |= 4 ))
echo 4 $(( iv &= 4 ))

echo 29 $(( iv += (jv + 9)))
echo 5 $(( (iv + 4) % 7 ))

# unary plus, minus
echo -4 $(( +4 - 8 ))
echo 4 $(( -4 + 8 ))

# conditional expressions
echo 1 $(( 4<5 ? 1 : 32))
echo 32 $(( 4>5 ? 1 : 32))
echo 32 $(( 4>(2+3) ? 1 : 32))
echo 1 $(( 4<(2+3) ? 1 : 32))
echo 1 $(( (2+2)<(2+3) ? 1 : 32))
echo 32 $(( (2+2)>(2+3) ? 1 : 32))

# check that the unevaluated part of the ternary operator does not do
# evaluation or assignment
x=i+=2
y=j+=2
#ash# declare -i i=1 j=1
      i=1
      j=1
echo 20 $((1 ? 20 : (x+=2)))
#ash# echo $i,$x             # ash mishandles this
echo 30 $((0 ? (y+=2) : 30))
#ash# echo $j,$y             # ash mishandles this

x=i+=2
y=j+=2
#ash# declare -i i=1 j=1
      i=1
      j=1
echo 20 $((1 ? 20 : (x+=2)))
#ash# echo $i,$x             # ash mishandles this
echo 30 $((0 ? (y+=2) : 30))
#ash# echo $i,$y             # ash mishandles this

# check precedence of assignment vs. conditional operator
# should be an error
#ash# declare -i x=2
      x=2
#ashnote# bash reports error but continues, ash aborts - using subshell to 'emulate' bash:
(  y=$((1 ? 20 : x+=2))  )

# check precedence of assignment vs. conditional operator
#ash# declare -i x=2
      x=2
# ash says "line NNN: syntax error: 0 ? x+=2 : 20"
#ash# echo 20 $((0 ? x+=2 : 20))

# associativity of assignment-operator operator
#ash# declare -i i=1 j=2 k=3
i=1
j=2
k=3
echo 6 $((i += j += k))
echo 6,5,3 $i,$j,$k

# octal, hex
echo 263 $(( 0x100 | 007 ))
echo 255 $(( 0xff ))
#ash# echo 255 $(( 16#ff ))
#ash# echo 127 $(( 16#FF/2 ))
#ash# echo 36 $(( 8#44 ))

echo 40 $(( 8 ^ 32 ))

#ash# # other bases
#ash# echo 10 $(( 16#a ))
#ash# echo 10 $(( 32#a ))
#ash# echo 10 $(( 56#a ))
#ash# echo 10 $(( 64#a ))
#ash# 
#ash# echo 10 $(( 16#A ))
#ash# echo 10 $(( 32#A ))
#ash# echo 36 $(( 56#A ))
#ash# echo 36 $(( 64#A ))
#ash# 
#ash# echo 62 $(( 64#@ ))
#ash# echo 63 $(( 64#_ ))

#ash# # weird bases (error)
#ash# echo $(( 3425#56 ))

#ash# # missing number after base
#ash# echo 0 $(( 2# ))

# these should generate errors
(  echo $(( 7 = 43 ))      )
#ash# echo $(( 2#44 ))
(  echo $(( 44 / 0 ))      )
(  let 'jv += $iv'         )
(  echo $(( jv += \$iv ))  )
(  let 'rv = 7 + (43 * 6'  )

#ash# # more errors
#ash# declare -i i
#ash# i=0#4
#ash# i=2#110#11

((echo abc; echo def;); echo ghi)

#ash# if (((4+4) + (4 + 7))); then
#ash# 	echo ok
#ash# fi

#ash# (())	# make sure the null expression works OK

#ash# a=(0 2 4 6)
#ash# echo 6 $(( a[1] + a[2] ))
#ash# echo 1 $(( (a[1] + a[2]) == a[3] ))
#ash# (( (a[1] + a[2]) == a[3] )) ; echo 0 $?

# test pushing and popping the expression stack
unset A
A="4 + "
(  echo A $(( ( 4 + A ) + 4 ))  )
A="3 + 5"
echo 16 $(( ( 4 + A ) + 4 ))

# badly-formed conditional expressions
(  echo $(( 4 ? : $A ))  )
(  echo $(( 1 ? 20 ))    )
(  echo $(( 4 ? 20 : ))  )

# precedence and short-circuit evaluation
B=9
echo 9 $B

# error
(  echo $(( 0 && B=42 )); echo 9 $B  )

# error
(  echo $(( 1 || B=88 )); echo 9 $B  )

# ash mistakenly evaluates B=... below
#ash# echo 0 $(( 0 && (B=42) ))
echo 9 $B
#ash# echo 0 $(( (${$} - $$) && (B=42) ))
echo 9 $B
#ash# echo 1 $(( 1 || (B=88) ))
echo 9 $B


# until command with (( )) command
x=7

echo 7 $x
#ash# until (( x == 4 ))
      until test "$x" = 4
do
	echo $x
	x=4
done

echo 4 $x

# exponentiation
echo 32767 $(( 2**15 - 1))
echo 32768 $(( 2**(16-1)))
echo 131072 $(( 2**16*2 ))
echo 2147483647 $(( 2**31-1))
echo 1 $(( 2**0 ))

# {pre,post}-{inc,dec}rement and associated errors

x=4

echo 4 $x
echo 4 $(( x++ ))
echo 5 $x
echo 5 $(( x-- ))
echo 4 $x

echo 3 $(( --x ))
echo 3 $x

echo 4 $(( ++x ))
echo 4 $x

# bash 3.2 apparently thinks that ++7 is 7
#ash# echo 7 $(( ++7 ))
(  echo $(( 7-- ))    )

(  echo $(( --x=7 ))  )
(  echo $(( ++x=7 ))  )

(  echo $(( x++=7 ))  )
(  echo $(( x--=7 ))  )

echo 4 $x

echo 7 $(( +7 ))
echo -7 $(( -7 ))

# bash 3.2 apparently thinks that ++7 is 7
#ash# echo $(( ++7 ))
#ash# echo $(( --7 ))

${THIS_SH} ./arith1.sub
${THIS_SH} ./arith2.sub

x=4
y=7

#ash# (( x=8 , y=12 ))
      x=8
      y=12
echo $x $y

#ash# # should be an error
#ash# (( x=9 y=41 ))

# These are errors
unset b
(  echo $((a b))  )
#ash# ((a b))

n=42
printf "%d\n" $n
printf "%i\n" $n
#ash# echo $(( 8#$(printf "%o\n" $n) ))
printf "%u\n" $n
#ash# echo $(( 16#$(printf "%x\n" $n) ))
#ash# echo $(( 16#$(printf "%X\n" $n) ))

# causes longjmp botches through bash-2.05b
a[b[c]d]=e