3
0

arith.tests 5.6 KB

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