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