/pypy/translator/oosupport/test_template/operations.py

https://github.com/alemacgo/pypy · Python · 227 lines · 187 code · 40 blank · 0 comment · 19 complexity · 2d406b5d87e702cc25fe0dd26c434ec8 MD5 · raw file

  1. from pypy.rpython.lltypesystem.lloperation import llop
  2. from pypy.rpython.lltypesystem import lltype
  3. from pypy.rpython.ootypesystem import ootype
  4. from pypy.rlib.rarithmetic import r_uint, r_ulonglong, r_longlong, ovfcheck
  5. from pypy.rlib import rstack
  6. from pypy.annotation import model as annmodel
  7. import sys
  8. char = annmodel.SomeChar()
  9. def fn_eq(x, y):
  10. return x == y
  11. def fn_ne(x, y):
  12. return x != y
  13. def fn_ge(x, y):
  14. return x>=y
  15. def fn_le(x, y):
  16. return x<=y
  17. class BaseTestOperations(object):
  18. FLOAT_PRECISION = 8
  19. def _check(self, fn, annotation, args):
  20. res1 = fn(*args)
  21. res2 = self.interpret(fn, args, annotation)
  22. if type(res1) is float:
  23. assert round(res1, self.FLOAT_PRECISION) == round(res2, self.FLOAT_PRECISION)
  24. else:
  25. assert res1 == res2
  26. def _check_int(self, f):
  27. self._check(f, [int, int], (42, 13))
  28. def _check_r_uint(self, f):
  29. self._check(f, [r_uint, r_uint], (r_uint(sys.maxint+1), r_uint(42)))
  30. def _check_r_longlong(self, f):
  31. self._check(f, [r_longlong, r_longlong], (r_longlong(sys.maxint*3), r_longlong(42)))
  32. def _check_r_ulonglong(self, f):
  33. self._check(f, [r_ulonglong, r_ulonglong], (r_ulonglong(sys.maxint*3), r_ulonglong(42)))
  34. def _check_float(self, f):
  35. self._check(f, [float, float], (42.0, (10.0/3)))
  36. def _check_all(self, fn):
  37. self._check_int(fn)
  38. self._check_r_uint(fn)
  39. self._check_r_longlong(fn)
  40. self._check_r_ulonglong(fn)
  41. self._check_float(fn)
  42. def test_div_zero(self):
  43. def fn(x, y):
  44. try:
  45. return x/y
  46. except ZeroDivisionError:
  47. return -1
  48. assert self.interpret(fn, [10, 0]) == -1
  49. def test_div_ovf_zer(self):
  50. def fn(x, y):
  51. try:
  52. return ovfcheck(x // y)
  53. except OverflowError:
  54. return -41
  55. except ZeroDivisionError:
  56. return -42
  57. assert self.interpret(fn, [50, 3]) == 16
  58. assert self.interpret(fn, [50, 0]) == -42
  59. assert self.interpret(fn, [50, -3]) == -17
  60. assert self.interpret(fn, [-50, 3]) == -17
  61. assert self.interpret(fn, [-50, -3]) == 16
  62. assert self.interpret(fn, [-sys.maxint-1, -1]) == -41
  63. def test_mod_ovf_zer(self):
  64. def fn(x, y):
  65. try:
  66. return ovfcheck(x % y)
  67. except OverflowError:
  68. return -41
  69. except ZeroDivisionError:
  70. return -42
  71. assert self.interpret(fn, [10, 3]) == 1
  72. assert self.interpret(fn, [10, 0]) == -42
  73. assert self.interpret(fn, [10, -3]) == -2
  74. assert self.interpret(fn, [-10, 3]) == 2
  75. assert self.interpret(fn, [-10, -3]) == -1
  76. assert self.interpret(fn, [-sys.maxint-1, -1]) == -41
  77. def test_llong_and(self):
  78. def fn(x, y):
  79. return x & y
  80. assert self.interpret(fn, [r_longlong(10), r_longlong(11)]) == 10
  81. def test_two_overflows(self):
  82. def fn(x, y):
  83. res = -42
  84. try:
  85. res = ovfcheck(x+y)
  86. except OverflowError:
  87. res = 0
  88. try:
  89. res += ovfcheck(x+y)
  90. except OverflowError:
  91. res += 1
  92. return res
  93. assert self.interpret(fn, [sys.maxint, 2]) == 1
  94. def test_rshift(self):
  95. def fn(x, y):
  96. return x >> y
  97. assert self.interpret(fn, [r_longlong(32), 1]) == 16
  98. def test_uint_neg(self):
  99. def fn(x):
  100. return -x
  101. self._check(fn, [r_uint], [r_uint(sys.maxint+1)])
  102. def test_unichar_eq(self):
  103. def fn(x, y):
  104. const = [u'\u03b1', u'\u03b2']
  105. return const[x] == const[y]
  106. self._check(fn, [int, int], (0, 0))
  107. def test_unichar_ne(self):
  108. def fn(x, y):
  109. const = [u'\u03b1', u'\u03b2']
  110. return const[x] != const[y]
  111. self._check(fn, [int, int], (0, 1))
  112. def test_int_le(self):
  113. self._check(fn_le, [int, int], (42, 42))
  114. self._check(fn_le, [int, int], (13, 42))
  115. def test_int_ge(self):
  116. self._check(fn_ge, [int, int], (42, 42))
  117. self._check(fn_ge, [int, int], (13, 42))
  118. def test_char_cmp(self):
  119. self._check(fn_eq, [char, char], ('a', 'a'))
  120. self._check(fn_ne, [char, char], ('a', 'b'))
  121. self._check(fn_ge, [char, char], ('a', 'b'))
  122. self._check(fn_ge, [char, char], ('b', 'a'))
  123. self._check(fn_le, [char, char], ('a', 'b'))
  124. self._check(fn_le, [char, char], ('b', 'a'))
  125. def test_eq(self):
  126. self._check_all(fn_eq)
  127. def test_ne(self):
  128. self._check_all(fn_ne)
  129. def test_neg(self):
  130. def fn(x, y):
  131. return -x
  132. self._check_int(fn)
  133. self._check_r_longlong(fn)
  134. self._check_float(fn)
  135. def test_ge(self):
  136. self._check_all(fn_ge)
  137. def test_le(self):
  138. self._check_all(fn_le)
  139. def test_and_not(self):
  140. def fn(x, y):
  141. return x and (not y)
  142. self._check_int(fn)
  143. self._check_float(fn)
  144. def test_uint_shift(self):
  145. def fn(x, y):
  146. return x<<3 + y>>4
  147. self._check_r_uint(fn)
  148. def test_bitwise(self):
  149. def fn(x, y):
  150. return (x&y) | ~(x^y)
  151. self._check_int(fn)
  152. self._check_r_uint(fn)
  153. def test_modulo(self):
  154. def fn(x, y):
  155. return x%y
  156. self._check_int(fn)
  157. self._check_r_uint(fn)
  158. self._check_r_longlong(fn)
  159. self._check_r_ulonglong(fn)
  160. def test_operations(self):
  161. def fn(x, y):
  162. return (x*y) + (x-y) + (x/y)
  163. self._check_all(fn)
  164. def test_abs(self):
  165. def fn(x, y):
  166. return abs(x)
  167. self._check_all(fn)
  168. def test_is_true(self):
  169. def fn(x, y):
  170. return bool(x)
  171. self._check_all(fn)
  172. def test_ullong_rshift(self):
  173. def f(x):
  174. return x >> 1
  175. x = sys.maxint+1
  176. assert self.interpret(f, [r_ulonglong(x)]) == x >> 1
  177. def test_compare_big_ullongs(self):
  178. bigval = r_ulonglong(9223372036854775808L)
  179. def fn(x):
  180. if x > bigval: return 1
  181. if x == bigval: return 0
  182. if x < bigval: return -1
  183. return -2
  184. for val in (bigval-1, bigval, bigval+1):
  185. expected = fn(val)
  186. res = self.interpret(fn, [val])
  187. assert res == expected