PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/objspace/std/test/test_longobject.py

https://bitbucket.org/pypy/pypy/
Python | 367 lines | 312 code | 43 blank | 12 comment | 10 complexity | 9ea5511a7584da11a0fb14636f100012 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. import py
  2. from pypy.objspace.std import longobject as lobj
  3. from rpython.rlib.rbigint import rbigint
  4. class TestW_LongObject:
  5. def test_bigint_w(self):
  6. space = self.space
  7. fromlong = lobj.W_LongObject.fromlong
  8. assert isinstance(space.bigint_w(fromlong(42)), rbigint)
  9. assert space.bigint_w(fromlong(42)).eq(rbigint.fromint(42))
  10. assert space.bigint_w(fromlong(-1)).eq(rbigint.fromint(-1))
  11. w_obj = space.wrap("hello world")
  12. space.raises_w(space.w_TypeError, space.bigint_w, w_obj)
  13. w_obj = space.wrap(123.456)
  14. space.raises_w(space.w_TypeError, space.bigint_w, w_obj)
  15. w_obj = fromlong(42)
  16. assert space.unwrap(w_obj) == 42
  17. def test_overflow_error(self):
  18. space = self.space
  19. fromlong = lobj.W_LongObject.fromlong
  20. w_big = fromlong(10**900)
  21. space.raises_w(space.w_OverflowError, space.float_w, w_big)
  22. def test_rint_variants(self):
  23. py.test.skip("XXX broken!")
  24. from rpython.rtyper.tool.rfficache import platform
  25. space = self.space
  26. for r in platform.numbertype_to_rclass.values():
  27. if r is int:
  28. continue
  29. print r
  30. values = [0, -1, r.MASK>>1, -(r.MASK>>1)-1]
  31. for x in values:
  32. if not r.SIGNED:
  33. x &= r.MASK
  34. w_obj = space.wrap(r(x))
  35. assert space.bigint_w(w_obj).eq(rbigint.fromint(x))
  36. class AppTestLong:
  37. def test_trunc(self):
  38. import math
  39. assert math.trunc(1L) == 1L
  40. assert math.trunc(-1L) == -1L
  41. def test_add(self):
  42. x = 123L
  43. assert int(x + 12443L) == 123 + 12443
  44. x = -20
  45. assert x + 2 + 3L + True == -14L
  46. def test_sub(self):
  47. x = 58543L
  48. assert int(x - 12332L) == 58543 - 12332
  49. x = 237123838281233L
  50. assert x * 12 == x * 12L
  51. def test_mul(self):
  52. x = 363L
  53. assert x * 2 ** 40 == x << 40
  54. def test_truediv(self):
  55. exec "from __future__ import division; a = 31415926L / 10000000L"
  56. assert a == 3.1415926
  57. def test_floordiv(self):
  58. x = 31415926L
  59. a = x // 10000000L
  60. assert a == 3L
  61. def test_numerator_denominator(self):
  62. assert (1L).numerator == 1L
  63. assert (1L).denominator == 1L
  64. assert (42L).numerator == 42L
  65. assert (42L).denominator == 1L
  66. def test_compare(self):
  67. Z = 0
  68. ZL = 0L
  69. for BIG in (1L, 1L << 62, 1L << 9999):
  70. assert Z == ZL
  71. assert not (Z != ZL)
  72. assert ZL == Z
  73. assert not (ZL != Z)
  74. assert not (Z == BIG)
  75. assert Z != BIG
  76. assert not (BIG == Z)
  77. assert BIG != Z
  78. assert not (ZL == BIG)
  79. assert ZL != BIG
  80. assert Z <= ZL
  81. assert not (Z < ZL)
  82. assert Z <= BIG
  83. assert Z < BIG
  84. assert not (BIG <= Z)
  85. assert not (BIG < Z)
  86. assert ZL <= ZL
  87. assert not (ZL < ZL)
  88. assert ZL <= BIG
  89. assert ZL < BIG
  90. assert not (BIG <= ZL)
  91. assert not (BIG < ZL)
  92. assert not (Z <= -BIG)
  93. assert not (Z < -BIG)
  94. assert -BIG <= Z
  95. assert -BIG < Z
  96. assert not (ZL <= -BIG)
  97. assert not (ZL < -BIG)
  98. assert -BIG <= ZL
  99. assert -BIG < ZL
  100. #
  101. assert not (BIG < int(BIG))
  102. assert (BIG <= int(BIG))
  103. assert (BIG == int(BIG))
  104. assert not (BIG != int(BIG))
  105. assert not (BIG > int(BIG))
  106. assert (BIG >= int(BIG))
  107. #
  108. assert (BIG < int(BIG)+1)
  109. assert (BIG <= int(BIG)+1)
  110. assert not (BIG == int(BIG)+1)
  111. assert (BIG != int(BIG)+1)
  112. assert not (BIG > int(BIG)+1)
  113. assert not (BIG >= int(BIG)+1)
  114. #
  115. assert not (BIG < int(BIG)-1)
  116. assert not (BIG <= int(BIG)-1)
  117. assert not (BIG == int(BIG)-1)
  118. assert (BIG != int(BIG)-1)
  119. assert (BIG > int(BIG)-1)
  120. assert (BIG >= int(BIG)-1)
  121. #
  122. assert not (int(BIG) < BIG)
  123. assert (int(BIG) <= BIG)
  124. assert (int(BIG) == BIG)
  125. assert not (int(BIG) != BIG)
  126. assert not (int(BIG) > BIG)
  127. assert (int(BIG) >= BIG)
  128. #
  129. assert not (int(BIG)+1 < BIG)
  130. assert not (int(BIG)+1 <= BIG)
  131. assert not (int(BIG)+1 == BIG)
  132. assert (int(BIG)+1 != BIG)
  133. assert (int(BIG)+1 > BIG)
  134. assert (int(BIG)+1 >= BIG)
  135. #
  136. assert (int(BIG)-1 < BIG)
  137. assert (int(BIG)-1 <= BIG)
  138. assert not (int(BIG)-1 == BIG)
  139. assert (int(BIG)-1 != BIG)
  140. assert not (int(BIG)-1 > BIG)
  141. assert not (int(BIG)-1 >= BIG)
  142. def test_conversion(self):
  143. class long2(long):
  144. pass
  145. x = 1L
  146. x = long2(x<<100)
  147. y = int(x)
  148. assert type(y) == long
  149. assert type(+long2(5)) is long
  150. assert type(long2(5) << 0) is long
  151. assert type(long2(5) >> 0) is long
  152. assert type(long2(5) + 0) is long
  153. assert type(long2(5) - 0) is long
  154. assert type(long2(5) * 1) is long
  155. assert type(1 * long2(5)) is long
  156. assert type(0 + long2(5)) is long
  157. assert type(-long2(0)) is long
  158. assert type(long2(5) // 1) is long
  159. def test_pow(self):
  160. x = 0L
  161. assert pow(x, 0L, 1L) == 0L
  162. assert pow(-1L, -1L) == -1.0
  163. def test_getnewargs(self):
  164. assert 0L .__getnewargs__() == (0L,)
  165. assert (-1L) .__getnewargs__() == (-1L,)
  166. def test_divmod(self):
  167. def check_division(x, y):
  168. q, r = divmod(x, y)
  169. pab, pba = x*y, y*x
  170. assert pab == pba
  171. assert q == x//y
  172. assert r == x%y
  173. assert x == q*y + r
  174. if y > 0:
  175. assert 0 <= r < y
  176. else:
  177. assert y < r <= 0
  178. for x in [-1L, 0L, 1L, 2L ** 100 - 1, -2L ** 100 - 1]:
  179. for y in [-105566530L, -1L, 1L, 1034522340L]:
  180. print "checking division for %s, %s" % (x, y)
  181. check_division(x, y)
  182. # special case from python tests:
  183. s1 = 33
  184. s2 = 2
  185. x = 16565645174462751485571442763871865344588923363439663038777355323778298703228675004033774331442052275771343018700586987657790981527457655176938756028872904152013524821759375058141439
  186. x >>= s1*16
  187. y = 10953035502453784575
  188. y >>= s2*16
  189. x = 0x3FE0003FFFFC0001FFFL
  190. y = 0x9800FFC1L
  191. check_division(x, y)
  192. raises(ZeroDivisionError, "x // 0L")
  193. def test_format(self):
  194. assert repr(12345678901234567890) == '12345678901234567890L'
  195. assert str(12345678901234567890) == '12345678901234567890'
  196. assert hex(0x1234567890ABCDEFL) == '0x1234567890abcdefL'
  197. assert oct(01234567012345670L) == '01234567012345670L'
  198. def test_bits(self):
  199. x = 0xAAAAAAAAL
  200. assert x | 0x55555555L == 0xFFFFFFFFL
  201. assert x & 0x55555555L == 0x00000000L
  202. assert x ^ 0x55555555L == 0xFFFFFFFFL
  203. assert -x | 0x55555555L == -0xAAAAAAA9L
  204. assert x | 0x555555555L == 0x5FFFFFFFFL
  205. assert x & 0x555555555L == 0x000000000L
  206. assert x ^ 0x555555555L == 0x5FFFFFFFFL
  207. def test_hash(self):
  208. # ints have the same hash as equal longs
  209. for i in range(-4, 14):
  210. assert hash(i) == hash(long(i))
  211. # might check too much -- it's ok to change the hashing algorithm
  212. assert hash(123456789L) == 123456789
  213. assert hash(1234567890123456789L) in (
  214. -1895067127, # with 32-bit platforms
  215. 1234567890123456789) # with 64-bit platforms
  216. def test_math_log(self):
  217. import math
  218. raises(ValueError, math.log, 0L)
  219. raises(ValueError, math.log, -1L)
  220. raises(ValueError, math.log, -2L)
  221. raises(ValueError, math.log, -(1L << 10000))
  222. #raises(ValueError, math.log, 0)
  223. raises(ValueError, math.log, -1)
  224. raises(ValueError, math.log, -2)
  225. def test_long(self):
  226. import sys
  227. n = -sys.maxint-1
  228. assert long(n) == n
  229. assert str(long(n)) == str(n)
  230. a = buffer('123')
  231. assert long(a) == 123L
  232. def test_huge_longs(self):
  233. import operator
  234. x = 1L
  235. huge = x << 40000L
  236. raises(OverflowError, float, huge)
  237. raises(OverflowError, operator.truediv, huge, 3)
  238. raises(OverflowError, operator.truediv, huge, 3L)
  239. def test_just_trunc(self):
  240. class myint(object):
  241. def __trunc__(self):
  242. return 42
  243. assert long(myint()) == 42
  244. def test_override___long__(self):
  245. class mylong(long):
  246. def __long__(self):
  247. return 42L
  248. assert long(mylong(21)) == 42L
  249. class myotherlong(long):
  250. pass
  251. assert long(myotherlong(21)) == 21L
  252. def test___long__(self):
  253. class A(object):
  254. def __long__(self):
  255. return 42
  256. assert long(A()) == 42L
  257. class B(object):
  258. def __int__(self):
  259. return 42
  260. raises(TypeError, long, B())
  261. # but!: (blame CPython 2.7)
  262. class Integral(object):
  263. def __int__(self):
  264. return 42
  265. class TruncReturnsNonLong(object):
  266. def __trunc__(self):
  267. return Integral()
  268. assert long(TruncReturnsNonLong()) == 42
  269. def test_long_before_string(self):
  270. class A(str):
  271. def __long__(self):
  272. return 42
  273. assert long(A('abc')) == 42
  274. def test_conjugate(self):
  275. assert (7L).conjugate() == 7L
  276. assert (-7L).conjugate() == -7L
  277. class L(long):
  278. pass
  279. assert type(L(7).conjugate()) is long
  280. class L(long):
  281. def __pos__(self):
  282. return 43
  283. assert L(7).conjugate() == 7L
  284. def test_bit_length(self):
  285. assert 8L.bit_length() == 4
  286. assert (-1<<40).bit_length() == 41
  287. assert ((2**31)-1).bit_length() == 31
  288. def test_negative_zero(self):
  289. x = eval("-0L")
  290. assert x == 0L
  291. def test_mix_int_and_long(self):
  292. class IntLongMixClass(object):
  293. def __int__(self):
  294. return 42L
  295. def __long__(self):
  296. return 64
  297. mixIntAndLong = IntLongMixClass()
  298. as_long = long(mixIntAndLong)
  299. assert type(as_long) is long
  300. assert as_long == 64
  301. def test_long_real(self):
  302. class A(long): pass
  303. b = A(5).real
  304. assert type(b) is long
  305. def test__int__(self):
  306. class A(long):
  307. def __int__(self):
  308. return 42
  309. assert int(long(3)) == long(3)
  310. assert int(A(13)) == 42
  311. def test_long_error_msg(self):
  312. e = raises(TypeError, long, [])
  313. assert str(e.value) == (
  314. "long() argument must be a string or a number, not 'list'")
  315. def test_coerce(self):
  316. assert 3L.__coerce__(4L) == (3L, 4L)
  317. assert 3L.__coerce__(4) == (3, 4)
  318. assert 3L.__coerce__(object()) == NotImplemented
  319. def test_linear_long_base_16(self):
  320. # never finishes if long(_, 16) is not linear-time
  321. size = 100000
  322. n = "a" * size
  323. expected = (2 << (size * 4)) // 3
  324. assert long(n, 16) == expected