arith.tests 5.5 KB

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