PageRenderTime 36ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/rpython/test/test_rint.py

https://bitbucket.org/pypy/pypy/
Python | 422 lines | 383 code | 36 blank | 3 comment | 14 complexity | 7d0cf9645f3832c531aacfec6baa18a5 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. import py
  2. import sys, operator
  3. from pypy.translator.translator import TranslationContext
  4. from pypy.annotation import model as annmodel
  5. from pypy.rpython.test import snippet
  6. from pypy.rlib.rarithmetic import r_int, r_uint, r_longlong, r_ulonglong
  7. from pypy.rlib.rarithmetic import ovfcheck, r_int64, intmask, int_between
  8. from pypy.rlib import objectmodel
  9. from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin, OORtypeMixin
  10. from pypy.rpython.lltypesystem import lltype
  11. from pypy.rpython.ootypesystem import ootype
  12. from pypy.rpython.lltypesystem.lloperation import llop
  13. class TestSnippet(object):
  14. def _test(self, func, types):
  15. t = TranslationContext()
  16. t.buildannotator().build_types(func, types)
  17. t.buildrtyper().specialize()
  18. t.checkgraphs()
  19. def test_not1(self):
  20. self._test(snippet.not1, [int])
  21. def test_not2(self):
  22. self._test(snippet.not2, [int])
  23. def test_int1(self):
  24. self._test(snippet.int1, [int])
  25. def test_int_cast1(self):
  26. self._test(snippet.int_cast1, [int])
  27. def DONTtest_unary_operations(self):
  28. # XXX TODO test if all unary operations are implemented
  29. for opname in annmodel.UNARY_OPERATIONS:
  30. print 'UNARY_OPERATIONS:', opname
  31. def DONTtest_binary_operations(self):
  32. # XXX TODO test if all binary operations are implemented
  33. for opname in annmodel.BINARY_OPERATIONS:
  34. print 'BINARY_OPERATIONS:', opname
  35. class BaseTestRint(BaseRtypingTest):
  36. def test_char_constant(self):
  37. def dummyfn(i):
  38. return chr(i)
  39. res = self.interpret(dummyfn, [ord(' ')])
  40. assert res == ' '
  41. res = self.interpret(dummyfn, [0])
  42. assert res == '\0'
  43. res = self.interpret(dummyfn, [ord('a')])
  44. assert res == 'a'
  45. def test_str_of_int(self):
  46. def dummy(i):
  47. return str(i)
  48. res = self.interpret(dummy, [0])
  49. assert self.ll_to_string(res) == '0'
  50. res = self.interpret(dummy, [1034])
  51. assert self.ll_to_string(res) == '1034'
  52. res = self.interpret(dummy, [-123])
  53. assert self.ll_to_string(res) == '-123'
  54. res = self.interpret(dummy, [-sys.maxint-1])
  55. assert self.ll_to_string(res) == str(-sys.maxint-1)
  56. def test_hex_of_int(self):
  57. def dummy(i):
  58. return hex(i)
  59. res = self.interpret(dummy, [0])
  60. assert self.ll_to_string(res) == '0x0'
  61. res = self.interpret(dummy, [1034])
  62. assert self.ll_to_string(res) == '0x40a'
  63. res = self.interpret(dummy, [-123])
  64. assert self.ll_to_string(res) == '-0x7b'
  65. res = self.interpret(dummy, [-sys.maxint-1])
  66. res = self.ll_to_string(res)
  67. assert res == '-0x8' + '0' * (len(res)-4)
  68. def test_oct_of_int(self):
  69. def dummy(i):
  70. return oct(i)
  71. res = self.interpret(dummy, [0])
  72. assert self.ll_to_string(res) == '0'
  73. res = self.interpret(dummy, [1034])
  74. assert self.ll_to_string(res) == '02012'
  75. res = self.interpret(dummy, [-123])
  76. assert self.ll_to_string(res) == '-0173'
  77. res = self.interpret(dummy, [-sys.maxint-1])
  78. res = self.ll_to_string(res)
  79. assert res == '-' + oct(sys.maxint+1).replace('L', '').replace('l', '')
  80. def test_str_of_longlong(self):
  81. def f(i):
  82. return str(i)
  83. res = self.interpret(f, [r_int64(0)])
  84. assert self.ll_to_string(res) == '0'
  85. res = self.interpret(f, [r_int64(413974738222117)])
  86. assert self.ll_to_string(res) == '413974738222117'
  87. def test_unsigned(self):
  88. bigvalue = r_uint(sys.maxint + 17)
  89. def dummy(i):
  90. i = r_uint(i)
  91. j = bigvalue
  92. return i < j
  93. res = self.interpret(dummy,[0])
  94. assert res is True
  95. res = self.interpret(dummy, [-1])
  96. assert res is False # -1 ==> 0xffffffff
  97. def test_specializing_int_functions(self):
  98. def f(i):
  99. return i + 1
  100. f._annspecialcase_ = "specialize:argtype(0)"
  101. def g(n):
  102. if n > 0:
  103. return f(r_int64(0))
  104. else:
  105. return f(0)
  106. res = self.interpret(g, [0])
  107. assert res == 1
  108. res = self.interpret(g, [1])
  109. assert res == 1
  110. def test_downcast_int(self):
  111. def f(i):
  112. return int(i)
  113. res = self.interpret(f, [r_int64(0)])
  114. assert res == 0
  115. def test_isinstance_vs_int_types(self):
  116. class FakeSpace(object):
  117. def wrap(self, x):
  118. if x is None:
  119. return [None]
  120. if isinstance(x, str):
  121. return x
  122. if isinstance(x, r_int64):
  123. return int(x)
  124. return "XXX"
  125. wrap._annspecialcase_ = 'specialize:argtype(0)'
  126. space = FakeSpace()
  127. def wrap(x):
  128. return space.wrap(x)
  129. res = self.interpret(wrap, [r_int64(0)])
  130. assert res == 0
  131. def test_truediv(self):
  132. def f(n, m):
  133. return operator.truediv(n, m)
  134. res = self.interpret(f, [20, 4])
  135. assert type(res) is float
  136. assert res == 5.0
  137. def test_float_conversion(self):
  138. def f(ii):
  139. return float(ii)
  140. res = self.interpret(f, [r_int64(100000000)])
  141. assert type(res) is float
  142. assert res == 100000000.
  143. res = self.interpret(f, [r_int64(1234567890123456789)])
  144. assert type(res) is float
  145. assert self.float_eq(res, 1.2345678901234568e+18)
  146. def test_float_conversion_implicit(self):
  147. def f(ii):
  148. return 1.0 + ii
  149. res = self.interpret(f, [r_int64(100000000)])
  150. assert type(res) is float
  151. assert res == 100000001.
  152. res = self.interpret(f, [r_int64(1234567890123456789)])
  153. assert type(res) is float
  154. assert self.float_eq(res, 1.2345678901234568e+18)
  155. def test_rarithmetic(self):
  156. inttypes = [int, r_uint, r_int64, r_ulonglong]
  157. for inttype in inttypes:
  158. c = inttype()
  159. def f():
  160. return c
  161. res = self.interpret(f, [])
  162. assert res == f()
  163. assert type(res) == inttype
  164. for inttype in inttypes:
  165. def f():
  166. return inttype(0)
  167. res = self.interpret(f, [])
  168. assert res == f()
  169. assert type(res) == inttype
  170. for inttype in inttypes:
  171. def f(x):
  172. return x
  173. res = self.interpret(f, [inttype(0)])
  174. assert res == f(inttype(0))
  175. assert type(res) == inttype
  176. def test_and_or(self):
  177. inttypes = [int, r_uint, r_int64, r_ulonglong]
  178. for inttype in inttypes:
  179. def f(a, b, c):
  180. return a&b|c
  181. res = self.interpret(f, [inttype(0x1234), inttype(0x00FF), inttype(0x5600)])
  182. assert res == f(0x1234, 0x00FF, 0x5600)
  183. def test_neg_abs_ovf(self):
  184. for op in (operator.neg, abs):
  185. def f(x):
  186. try:
  187. return ovfcheck(op(x))
  188. except OverflowError:
  189. return 0
  190. res = self.interpret(f, [-1])
  191. assert res == 1
  192. res = self.interpret(f, [int(-1<<(r_int.BITS-1))])
  193. assert res == 0
  194. def test_lshift_rshift(self):
  195. for name, f in [('_lshift', lambda x, y: x << y),
  196. ('_rshift', lambda x, y: x >> y)]:
  197. for inttype in (int, r_uint, r_int64, r_ulonglong):
  198. res = self.interpret(f, [inttype(2147483647), 12])
  199. if inttype is int:
  200. assert res == intmask(f(2147483647, 12))
  201. else:
  202. assert res == inttype(f(2147483647, 12))
  203. #
  204. # check that '*_[lr]shift' take an inttype and an
  205. # int as arguments, without the need for a
  206. # 'cast_int_to_{uint,longlong,...}'
  207. _, _, graph = self.gengraph(f, [inttype, int])
  208. block = graph.startblock
  209. assert len(block.operations) == 1
  210. assert block.operations[0].opname.endswith(name)
  211. def test_cast_uint_to_longlong(self):
  212. if r_uint.BITS == r_longlong.BITS:
  213. py.test.skip("only on 32-bits")
  214. def f(x):
  215. return r_longlong(r_uint(x))
  216. res = self.interpret(f, [-42])
  217. assert res == (sys.maxint+1) * 2 - 42
  218. div_mod_iteration_count = 1000
  219. def test_div_mod(self):
  220. import random
  221. for inttype in (int, r_int64):
  222. def d(x, y):
  223. return x/y
  224. for i in range(self.div_mod_iteration_count):
  225. x = inttype(random.randint(-100000, 100000))
  226. y = inttype(random.randint(-100000, 100000))
  227. if not y: continue
  228. if (i & 31) == 0:
  229. x = (x//y) * y # case where x is exactly divisible by y
  230. res = self.interpret(d, [x, y])
  231. assert res == d(x, y)
  232. def m(x, y):
  233. return x%y
  234. for i in range(self.div_mod_iteration_count):
  235. x = inttype(random.randint(-100000, 100000))
  236. y = inttype(random.randint(-100000, 100000))
  237. if not y: continue
  238. if (i & 31) == 0:
  239. x = (x//y) * y # case where x is exactly divisible by y
  240. res = self.interpret(m, [x, y])
  241. assert res == m(x, y)
  242. def test_protected_div_mod(self):
  243. def div_unpro(x, y):
  244. return x//y
  245. def div_ovf(x, y):
  246. try:
  247. return ovfcheck(x//y)
  248. except OverflowError:
  249. return 42
  250. def div_zer(x, y):
  251. try:
  252. return x//y
  253. except ZeroDivisionError:
  254. return 84
  255. def div_ovf_zer(x, y):
  256. try:
  257. return ovfcheck(x//y)
  258. except OverflowError:
  259. return 42
  260. except ZeroDivisionError:
  261. return 84
  262. def mod_unpro(x, y):
  263. return x%y
  264. def mod_ovf(x, y):
  265. try:
  266. return ovfcheck(x%y)
  267. except OverflowError:
  268. return 42
  269. def mod_zer(x, y):
  270. try:
  271. return x%y
  272. except ZeroDivisionError:
  273. return 84
  274. def mod_ovf_zer(x, y):
  275. try:
  276. return ovfcheck(x%y)
  277. except OverflowError:
  278. return 42
  279. except ZeroDivisionError:
  280. return 84
  281. for inttype in (int, r_int64):
  282. args = [( 5, 2), (-5, 2), ( 5,-2), (-5,-2),
  283. ( 6, 2), (-6, 2), ( 6,-2), (-6,-2),
  284. (-sys.maxint, -1), (4, 0)]
  285. funcs = [div_unpro, div_ovf, div_zer, div_ovf_zer,
  286. mod_unpro, mod_ovf, mod_zer, mod_ovf_zer]
  287. for func in funcs:
  288. print func
  289. if 'ovf' in func.func_name and inttype is r_longlong:
  290. continue # don't have many llong_*_ovf operations...
  291. for x, y in args:
  292. x, y = inttype(x), inttype(y)
  293. try:
  294. res1 = func(x, y)
  295. if isinstance(res1, int):
  296. res1 = ovfcheck(res1)
  297. except (OverflowError, ZeroDivisionError):
  298. continue
  299. res2 = self.interpret(func, [x, y])
  300. assert res1 == res2
  301. def test_int_add_nonneg_ovf(self):
  302. def f(x):
  303. try:
  304. a = ovfcheck(x + 50)
  305. except OverflowError:
  306. return 0
  307. try:
  308. a += ovfcheck(100 + x)
  309. except OverflowError:
  310. return 1
  311. return a
  312. res = self.interpret(f, [-3])
  313. assert res == 144
  314. res = self.interpret(f, [sys.maxint-50])
  315. assert res == 1
  316. res = self.interpret(f, [sys.maxint])
  317. assert res == 0
  318. def test_cast_to_float_exc_check(self):
  319. def f(x):
  320. try:
  321. return float(x)
  322. except ValueError:
  323. return 3.0
  324. res = self.interpret(f, [3])
  325. assert res == 3
  326. def test_hash(self):
  327. def f(x):
  328. return objectmodel.compute_hash(x)
  329. res = self.interpret(f, [123456789])
  330. assert res == 123456789
  331. res = self.interpret(f, [r_int64(123456789012345678)])
  332. if sys.maxint == 2147483647:
  333. # check the way we compute such a hash so far
  334. assert res == -1506741426 + 9 * 28744523
  335. else:
  336. assert res == 123456789012345678
  337. def test_int_between(self):
  338. def fn(a, b, c):
  339. return int_between(a, b, c)
  340. assert self.interpret(fn, [1, 1, 3])
  341. assert self.interpret(fn, [1, 2, 3])
  342. assert not self.interpret(fn, [1, 0, 2])
  343. assert not self.interpret(fn, [1, 5, 2])
  344. assert not self.interpret(fn, [1, 2, 2])
  345. assert not self.interpret(fn, [1, 1, 1])
  346. class TestLLtype(BaseTestRint, LLRtypeMixin):
  347. pass
  348. class TestOOtype(BaseTestRint, OORtypeMixin):
  349. def test_oobox_int(self):
  350. def f():
  351. x = llop.oobox_int(ootype.Object, 42)
  352. return llop.oounbox_int(lltype.Signed, x)
  353. assert self.interpret(f, []) == 42