3
0

arith.tests 5.3 KB

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