PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/rlib/test/test_rarithmetic.py

https://bitbucket.org/pypy/pypy/
Python | 376 lines | 318 code | 45 blank | 13 comment | 52 complexity | 752f2ae84c28ec8ebecdd8d3fff407b9 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin, OORtypeMixin
  2. from pypy.rlib.rarithmetic import *
  3. import sys
  4. import py
  5. maxint_mask = (sys.maxint*2 + 1)
  6. machbits = 0
  7. i = 1
  8. l = 1L
  9. while i == l and type(i) is int:
  10. i *= 2
  11. l *= 2
  12. machbits += 1
  13. #print machbits
  14. class Test_r_int:
  15. def setup_method(self,method):
  16. space = self.space
  17. def test__add__(self):
  18. self.binary_test(lambda x, y: x + y)
  19. def test__sub__(self):
  20. self.binary_test(lambda x, y: x - y)
  21. def test__mul__(self):
  22. self.binary_test(lambda x, y: x * y)
  23. x = 3; y = [2]
  24. assert x*y == r_int(x)*y
  25. assert y*x == y*r_int(x)
  26. def test__div__(self):
  27. self.binary_test(lambda x, y: x // y)
  28. def test__mod__(self):
  29. self.binary_test(lambda x, y: x % y)
  30. def test__divmod__(self):
  31. self.binary_test(divmod)
  32. def test__lshift__(self):
  33. self.binary_test(lambda x, y: x << y, (1, 2, 3))
  34. def test__rshift__(self):
  35. self.binary_test(lambda x, y: x >> y, (1, 2, 3))
  36. def test__or__(self):
  37. self.binary_test(lambda x, y: x | y)
  38. def test__and__(self):
  39. self.binary_test(lambda x, y: x & y)
  40. def test__xor__(self):
  41. self.binary_test(lambda x, y: x ^ y)
  42. def test__neg__(self):
  43. self.unary_test(lambda x: -x)
  44. def test__pos__(self):
  45. self.unary_test(lambda x: +x)
  46. def test__invert__(self):
  47. self.unary_test(lambda x: ~x)
  48. def test__pow__(self):
  49. self.binary_test(lambda x, y: x**y, (2, 3))
  50. self.binary_test(lambda x, y: pow(x, y, 42L), (2, 3, 5, 1000))
  51. def unary_test(self, f):
  52. for arg in (-10, -1, 0, 3, 12345):
  53. res = f(arg)
  54. cmp = f(r_int(arg))
  55. assert res == cmp
  56. def binary_test(self, f, rargs = None):
  57. if not rargs:
  58. rargs = (-10, -1, 3, 55)
  59. for larg in (-10, -1, 0, 3, 1234):
  60. for rarg in rargs:
  61. for types in ((int, r_int), (r_int, int), (r_int, r_int)):
  62. res = f(larg, rarg)
  63. left, right = types
  64. cmp = f(left(larg), right(rarg))
  65. assert res == cmp
  66. class Test_r_uint:
  67. def setup_method(self,method):
  68. space = self.space
  69. def test__add__(self):
  70. self.binary_test(lambda x, y: x + y)
  71. def test__sub__(self):
  72. self.binary_test(lambda x, y: x - y)
  73. def test__mul__(self):
  74. self.binary_test(lambda x, y: x * y)
  75. x = 3; y = [2]
  76. assert x*y == r_uint(x)*y
  77. assert y*x == y*r_uint(x)
  78. def test__div__(self):
  79. self.binary_test(lambda x, y: x // y)
  80. def test__mod__(self):
  81. self.binary_test(lambda x, y: x % y)
  82. def test__divmod__(self):
  83. self.binary_test(divmod)
  84. def test__lshift__(self):
  85. self.binary_test(lambda x, y: x << y, (1, 2, 3))
  86. def test__rshift__(self):
  87. self.binary_test(lambda x, y: x >> y, (1, 2, 3))
  88. def test__or__(self):
  89. self.binary_test(lambda x, y: x | y)
  90. def test__and__(self):
  91. self.binary_test(lambda x, y: x & y)
  92. def test__xor__(self):
  93. self.binary_test(lambda x, y: x ^ y)
  94. def test__neg__(self):
  95. self.unary_test(lambda x: -x)
  96. def test__pos__(self):
  97. self.unary_test(lambda x: +x)
  98. def test__invert__(self):
  99. self.unary_test(lambda x: ~x)
  100. def test__pow__(self):
  101. self.binary_test(lambda x, y: x**y, (2, 3))
  102. # pow is buggy, dowsn't allow our type
  103. #self.binary_test(lambda x, y: pow(x, y, 42), (2, 3, 5, 1000))
  104. def test_back_to_int(self):
  105. #assert int(r_uint(-1)) == -1
  106. # ^^^ that looks wrong IMHO: int(x) should not by itself return
  107. # an integer that has a different value than x, especially
  108. # if x is a subclass of long.
  109. assert int(r_uint(1)) == 1
  110. def unary_test(self, f):
  111. for arg in (0, 3, 12345):
  112. res = f(arg) & maxint_mask
  113. cmp = f(r_uint(arg))
  114. assert res == cmp
  115. def binary_test(self, f, rargs = None, translated=False):
  116. mask = maxint_mask
  117. if not rargs:
  118. rargs = (1, 3, 55)
  119. # when translated merging different int types is not allowed
  120. if translated:
  121. alltypes = [(r_uint, r_uint)]
  122. else:
  123. alltypes = [(int, r_uint), (r_uint, int), (r_uint, r_uint)]
  124. for larg in (0, 1, 2, 3, 1234):
  125. for rarg in rargs:
  126. for types in alltypes:
  127. res = f(larg, rarg)
  128. left, right = types
  129. cmp = f(left(larg), right(rarg))
  130. if type(res) is tuple:
  131. res = res[0] & mask, res[1] & mask
  132. else:
  133. res = res & mask
  134. assert res == cmp
  135. def test_from_float(self):
  136. assert r_uint(2.3) == 2
  137. assert r_uint(sys.maxint * 1.234) == long(sys.maxint * 1.234)
  138. def test_to_float(self):
  139. assert float(r_uint(2)) == 2.0
  140. val = long(sys.maxint * 1.234)
  141. assert float(r_uint(val)) == float(val)
  142. def test_mixed_types():
  143. types = [r_uint, r_ulonglong]
  144. for left in types:
  145. for right in types:
  146. x = left(3) + right(5)
  147. expected = max(types.index(left), types.index(right))
  148. assert types.index(type(x)) == expected
  149. def test_limits():
  150. for cls in r_uint, r_ulonglong:
  151. mask = cls.MASK
  152. assert cls(mask) == mask
  153. assert cls(mask+1) == 0
  154. for cls in r_int, r_longlong:
  155. mask = cls.MASK>>1
  156. assert cls(mask) == mask
  157. assert cls(-mask-1) == -mask-1
  158. py.test.raises(OverflowError, "cls(mask) + 1")
  159. py.test.raises(OverflowError, "cls(-mask-1) - 1")
  160. def test_intmask():
  161. assert intmask(1) == 1
  162. assert intmask(sys.maxint) == sys.maxint
  163. minint = -sys.maxint-1
  164. assert intmask(minint) == minint
  165. assert intmask(2*sys.maxint+1) == -1
  166. assert intmask(sys.maxint*2) == -2
  167. assert intmask(sys.maxint*2+2) == 0
  168. assert intmask(2*(sys.maxint*1+1)) == 0
  169. assert intmask(1 << (machbits-1)) == 1 << (machbits-1)
  170. assert intmask(sys.maxint+1) == minint
  171. assert intmask(minint-1) == sys.maxint
  172. assert intmask(r_uint(-1)) == -1
  173. assert intmask(r_ulonglong(-1)) == -1
  174. def test_intmask_small():
  175. from pypy.rpython.lltypesystem import rffi
  176. for tp in [rffi.r_signedchar, rffi.r_short, rffi.r_int,
  177. rffi.r_long, rffi.r_longlong]:
  178. x = intmask(tp(5))
  179. assert (type(x), x) == (int, 5)
  180. x = intmask(tp(-5))
  181. assert (type(x), x) == (int, -5)
  182. for tp in [rffi.r_uchar, rffi.r_ushort, rffi.r_uint,
  183. rffi.r_ulong, rffi.r_ulonglong]:
  184. x = intmask(tp(5))
  185. assert (type(x), x) == (int, 5)
  186. def test_bug_creating_r_int():
  187. minint = -sys.maxint-1
  188. assert r_int(r_int(minint)) == minint
  189. def test_ovfcheck():
  190. one = 1
  191. x = sys.maxint
  192. minusx = -sys.maxint
  193. n = -sys.maxint-1
  194. y = sys.maxint-1
  195. # sanity
  196. raises(AssertionError, ovfcheck, r_uint(0))
  197. # not overflowing
  198. try:
  199. ovfcheck(y+one)
  200. except OverflowError:
  201. assert False
  202. else:
  203. pass
  204. try:
  205. ovfcheck(minusx-one)
  206. except OverflowError:
  207. assert False
  208. else:
  209. pass
  210. try:
  211. ovfcheck(x-x)
  212. except OverflowError:
  213. assert False
  214. else:
  215. pass
  216. try:
  217. ovfcheck(n-n)
  218. except OverflowError:
  219. assert False
  220. else:
  221. pass
  222. # overflowing
  223. try:
  224. ovfcheck(x+one)
  225. except OverflowError:
  226. pass
  227. else:
  228. assert False
  229. try:
  230. ovfcheck(x+x)
  231. except OverflowError:
  232. pass
  233. else:
  234. assert False
  235. try:
  236. ovfcheck(n-one)
  237. except OverflowError:
  238. pass
  239. else:
  240. assert False
  241. try:
  242. ovfcheck(n-y)
  243. except OverflowError:
  244. pass
  245. else:
  246. assert False
  247. def test_ovfcheck_float_to_int():
  248. assert ovfcheck_float_to_int(1.0) == 1
  249. assert ovfcheck_float_to_int(0.0) == 0
  250. assert ovfcheck_float_to_int(13.0) == 13
  251. assert ovfcheck_float_to_int(-1.0) == -1
  252. assert ovfcheck_float_to_int(-13.0) == -13
  253. # strange things happening for float to int on 64 bit:
  254. # int(float(i)) != i because of rounding issues
  255. x = sys.maxint
  256. while int(float(x)) > sys.maxint:
  257. x -= 1
  258. assert ovfcheck_float_to_int(float(x)) == int(float(x))
  259. x = sys.maxint + 1
  260. while int(float(x)) <= sys.maxint:
  261. x += 1
  262. py.test.raises(OverflowError, ovfcheck_float_to_int, x)
  263. x = -sys.maxint-1
  264. while int(float(x)) < -sys.maxint-1:
  265. x += 1
  266. assert ovfcheck_float_to_int(float(x)) == int(float(x))
  267. x = -sys.maxint-1
  268. while int(float(x)) >= -sys.maxint-1:
  269. x -= 1
  270. py.test.raises(OverflowError, ovfcheck_float_to_int, x)
  271. def test_abs():
  272. assert type(abs(r_longlong(1))) is r_longlong
  273. def test_r_singlefloat():
  274. x = r_singlefloat(2.5) # exact number
  275. assert float(x) == 2.5
  276. x = r_singlefloat(2.1) # approximate number, bits are lost
  277. assert float(x) != 2.1
  278. assert abs(float(x) - 2.1) < 1E-6
  279. def test_r_singlefloat_eq():
  280. x = r_singlefloat(2.5) # exact number
  281. y = r_singlefloat(2.5)
  282. assert x == y
  283. assert not x != y
  284. assert not x == 2.5
  285. assert x != 2.5
  286. py.test.raises(TypeError, "x>y")
  287. class BaseTestRarithmetic(BaseRtypingTest):
  288. def test_compare_singlefloat_crashes(self):
  289. from pypy.rlib.rarithmetic import r_singlefloat
  290. from pypy.rpython.error import MissingRTypeOperation
  291. def f(x):
  292. a = r_singlefloat(x)
  293. b = r_singlefloat(x+1)
  294. return a == b
  295. py.test.raises(MissingRTypeOperation, "self.interpret(f, [42.0])")
  296. class TestLLtype(BaseTestRarithmetic, LLRtypeMixin):
  297. pass
  298. class TestOOtype(BaseTestRarithmetic, OORtypeMixin):
  299. pass
  300. def test_int_real_union():
  301. from pypy.rpython.lltypesystem.rffi import r_int_real
  302. assert compute_restype(r_int_real, r_int_real) is r_int_real
  303. def test_compute_restype_incompatible():
  304. from pypy.rpython.lltypesystem.rffi import r_int_real, r_short, r_ushort
  305. testcases = [(r_uint, r_longlong), (r_int_real, r_uint),
  306. (r_short, r_ushort)]
  307. for t1, t2 in testcases:
  308. py.test.raises(AssertionError, compute_restype, t1, t2)
  309. py.test.raises(AssertionError, compute_restype, t2, t1)
  310. def test_most_neg_value_of():
  311. assert most_neg_value_of_same_type(123) == -sys.maxint-1
  312. assert most_neg_value_of_same_type(r_uint(123)) == 0
  313. llmin = -(2**(r_longlong.BITS-1))
  314. assert most_neg_value_of_same_type(r_longlong(123)) == llmin
  315. assert most_neg_value_of_same_type(r_ulonglong(123)) == 0
  316. def test_r_ulonglong():
  317. x = r_longlong(-1)
  318. y = r_ulonglong(x)
  319. assert long(y) == 2**r_ulonglong.BITS - 1
  320. def test_highest_bit():
  321. py.test.raises(AssertionError, highest_bit, 0)
  322. py.test.raises(AssertionError, highest_bit, 14)
  323. for i in xrange(31):
  324. assert highest_bit(2**i) == i
  325. def test_int_between():
  326. assert int_between(1, 1, 3)
  327. assert int_between(1, 2, 3)
  328. assert not int_between(1, 0, 2)
  329. assert not int_between(1, 5, 2)
  330. assert not int_between(1, 2, 2)
  331. assert not int_between(1, 1, 1)