PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/pypy/objspace/std/test/test_intobject.py

https://bitbucket.org/pypy/pypy/
Python | 612 lines | 529 code | 75 blank | 8 comment | 21 complexity | 5111b2c8b4f8575fffef531139bb3a17 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. # encoding: utf-8
  2. import py
  3. import sys
  4. from pypy.objspace.std import intobject as iobj
  5. from rpython.rlib.rarithmetic import r_uint, is_valid_int
  6. from rpython.rlib.rbigint import rbigint
  7. class TestW_IntObject:
  8. def _longshiftresult(self, x):
  9. """ calculate an overflowing shift """
  10. n = 1
  11. l = long(x)
  12. while 1:
  13. ires = x << n
  14. lres = l << n
  15. if not is_valid_int(ires) or lres != ires:
  16. return n
  17. n += 1
  18. def test_int_w(self):
  19. assert self.space.int_w(self.space.wrap(42)) == 42
  20. def test_uint_w(self):
  21. space = self.space
  22. assert space.uint_w(space.wrap(42)) == 42
  23. assert isinstance(space.uint_w(space.wrap(42)), r_uint)
  24. space.raises_w(space.w_ValueError, space.uint_w, space.wrap(-1))
  25. def test_bigint_w(self):
  26. space = self.space
  27. assert isinstance(space.bigint_w(space.wrap(42)), rbigint)
  28. assert space.bigint_w(space.wrap(42)).eq(rbigint.fromint(42))
  29. def test_repr(self):
  30. x = 1
  31. f1 = iobj.W_IntObject(x)
  32. result = f1.descr_repr(self.space)
  33. assert self.space.unwrap(result) == repr(x)
  34. def test_str(self):
  35. x = 12345
  36. f1 = iobj.W_IntObject(x)
  37. result = f1.descr_str(self.space)
  38. assert self.space.unwrap(result) == str(x)
  39. def test_hash(self):
  40. x = 42
  41. f1 = iobj.W_IntObject(x)
  42. result = f1.descr_hash(self.space)
  43. assert result.intval == hash(x)
  44. def test_compare(self):
  45. import operator
  46. optab = ['lt', 'le', 'eq', 'ne', 'gt', 'ge']
  47. for x in (-10, -1, 0, 1, 2, 1000, sys.maxint):
  48. for y in (-sys.maxint-1, -11, -9, -2, 0, 1, 3, 1111, sys.maxint):
  49. for op in optab:
  50. wx = iobj.W_IntObject(x)
  51. wy = iobj.W_IntObject(y)
  52. res = getattr(operator, op)(x, y)
  53. method = getattr(wx, 'descr_%s' % op)
  54. myres = method(self.space, wy)
  55. assert self.space.unwrap(myres) == res
  56. def test_add(self):
  57. space = self.space
  58. x = 1
  59. y = 2
  60. f1 = iobj.W_IntObject(x)
  61. f2 = iobj.W_IntObject(y)
  62. result = f1.descr_add(space, f2)
  63. assert result.intval == x+y
  64. x = sys.maxint
  65. y = 1
  66. f1 = iobj.W_IntObject(x)
  67. f2 = iobj.W_IntObject(y)
  68. v = f1.descr_add(space, f2)
  69. assert space.isinstance_w(v, space.w_long)
  70. assert space.bigint_w(v).eq(rbigint.fromlong(x + y))
  71. def test_sub(self):
  72. space = self.space
  73. x = 1
  74. y = 2
  75. f1 = iobj.W_IntObject(x)
  76. f2 = iobj.W_IntObject(y)
  77. result = f1.descr_sub(space, f2)
  78. assert result.intval == x-y
  79. x = sys.maxint
  80. y = -1
  81. f1 = iobj.W_IntObject(x)
  82. f2 = iobj.W_IntObject(y)
  83. v = f1.descr_sub(space, f2)
  84. assert space.isinstance_w(v, space.w_long)
  85. assert space.bigint_w(v).eq(rbigint.fromlong(sys.maxint - -1))
  86. def test_mul(self):
  87. space = self.space
  88. x = 2
  89. y = 3
  90. f1 = iobj.W_IntObject(x)
  91. f2 = iobj.W_IntObject(y)
  92. result = f1.descr_mul(space, f2)
  93. assert result.intval == x*y
  94. x = -sys.maxint-1
  95. y = -1
  96. f1 = iobj.W_IntObject(x)
  97. f2 = iobj.W_IntObject(y)
  98. v = f1.descr_mul(space, f2)
  99. assert space.isinstance_w(v, space.w_long)
  100. assert space.bigint_w(v).eq(rbigint.fromlong(x * y))
  101. def test_div(self):
  102. space = self.space
  103. for i in range(10):
  104. res = i//3
  105. f1 = iobj.W_IntObject(i)
  106. f2 = iobj.W_IntObject(3)
  107. result = f1.descr_div(space, f2)
  108. assert result.intval == res
  109. x = -sys.maxint-1
  110. y = -1
  111. f1 = iobj.W_IntObject(x)
  112. f2 = iobj.W_IntObject(y)
  113. v = f1.descr_div(space, f2)
  114. assert space.isinstance_w(v, space.w_long)
  115. assert space.bigint_w(v).eq(rbigint.fromlong(x / y))
  116. def test_mod(self):
  117. x = 1
  118. y = 2
  119. f1 = iobj.W_IntObject(x)
  120. f2 = iobj.W_IntObject(y)
  121. v = f1.descr_mod(self.space, f2)
  122. assert v.intval == x % y
  123. # not that mod cannot overflow
  124. def test_divmod(self):
  125. space = self.space
  126. x = 1
  127. y = 2
  128. f1 = iobj.W_IntObject(x)
  129. f2 = iobj.W_IntObject(y)
  130. ret = f1.descr_divmod(space, f2)
  131. v, w = space.unwrap(ret)
  132. assert (v, w) == divmod(x, y)
  133. x = -sys.maxint-1
  134. y = -1
  135. f1 = iobj.W_IntObject(x)
  136. f2 = iobj.W_IntObject(y)
  137. v = f1.descr_divmod(space, f2)
  138. w_q, w_r = space.fixedview(v, 2)
  139. assert space.isinstance_w(w_q, space.w_long)
  140. expected = divmod(x, y)
  141. assert space.bigint_w(w_q).eq(rbigint.fromlong(expected[0]))
  142. # no overflow possible
  143. assert space.unwrap(w_r) == expected[1]
  144. def test_pow_iii(self):
  145. x = 10
  146. y = 2
  147. z = 13
  148. f1 = iobj.W_IntObject(x)
  149. f2 = iobj.W_IntObject(y)
  150. f3 = iobj.W_IntObject(z)
  151. v = f1.descr_pow(self.space, f2, f3)
  152. assert v.intval == pow(x, y, z)
  153. f1, f2, f3 = [iobj.W_IntObject(i) for i in (10, -1, 42)]
  154. self.space.raises_w(self.space.w_TypeError,
  155. f1.descr_pow, self.space, f2, f3)
  156. f1, f2, f3 = [iobj.W_IntObject(i) for i in (10, 5, 0)]
  157. self.space.raises_w(self.space.w_ValueError,
  158. f1.descr_pow, self.space, f2, f3)
  159. def test_pow_iin(self):
  160. space = self.space
  161. x = 10
  162. y = 2
  163. f1 = iobj.W_IntObject(x)
  164. f2 = iobj.W_IntObject(y)
  165. v = f1.descr_pow(space, f2, space.w_None)
  166. assert v.intval == x ** y
  167. f1, f2 = [iobj.W_IntObject(i) for i in (10, 20)]
  168. v = f1.descr_pow(space, f2, space.w_None)
  169. assert space.isinstance_w(v, space.w_long)
  170. assert space.bigint_w(v).eq(rbigint.fromlong(pow(10, 20)))
  171. def test_neg(self):
  172. space = self.space
  173. x = 42
  174. f1 = iobj.W_IntObject(x)
  175. v = f1.descr_neg(space)
  176. assert v.intval == -x
  177. x = -sys.maxint-1
  178. f1 = iobj.W_IntObject(x)
  179. v = f1.descr_neg(space)
  180. assert space.isinstance_w(v, space.w_long)
  181. assert space.bigint_w(v).eq(rbigint.fromlong(-x))
  182. def test_pos(self):
  183. x = 42
  184. f1 = iobj.W_IntObject(x)
  185. v = f1.descr_pos(self.space)
  186. assert v.intval == +x
  187. x = -42
  188. f1 = iobj.W_IntObject(x)
  189. v = f1.descr_pos(self.space)
  190. assert v.intval == +x
  191. def test_abs(self):
  192. space = self.space
  193. x = 42
  194. f1 = iobj.W_IntObject(x)
  195. v = f1.descr_abs(space)
  196. assert v.intval == abs(x)
  197. x = -42
  198. f1 = iobj.W_IntObject(x)
  199. v = f1.descr_abs(space)
  200. assert v.intval == abs(x)
  201. x = -sys.maxint-1
  202. f1 = iobj.W_IntObject(x)
  203. v = f1.descr_abs(space)
  204. assert space.isinstance_w(v, space.w_long)
  205. assert space.bigint_w(v).eq(rbigint.fromlong(abs(x)))
  206. def test_invert(self):
  207. x = 42
  208. f1 = iobj.W_IntObject(x)
  209. v = f1.descr_invert(self.space)
  210. assert v.intval == ~x
  211. def test_lshift(self):
  212. space = self.space
  213. x = 12345678
  214. y = 2
  215. f1 = iobj.W_IntObject(x)
  216. f2 = iobj.W_IntObject(y)
  217. v = f1.descr_lshift(space, f2)
  218. assert v.intval == x << y
  219. y = self._longshiftresult(x)
  220. f1 = iobj.W_IntObject(x)
  221. f2 = iobj.W_IntObject(y)
  222. v = f1.descr_lshift(space, f2)
  223. assert space.isinstance_w(v, space.w_long)
  224. assert space.bigint_w(v).eq(rbigint.fromlong(x << y))
  225. def test_rshift(self):
  226. x = 12345678
  227. y = 2
  228. f1 = iobj.W_IntObject(x)
  229. f2 = iobj.W_IntObject(y)
  230. v = f1.descr_rshift(self.space, f2)
  231. assert v.intval == x >> y
  232. def test_and(self):
  233. x = 12345678
  234. y = 2
  235. f1 = iobj.W_IntObject(x)
  236. f2 = iobj.W_IntObject(y)
  237. v = f1.descr_and(self.space, f2)
  238. assert v.intval == x & y
  239. def test_xor(self):
  240. x = 12345678
  241. y = 2
  242. f1 = iobj.W_IntObject(x)
  243. f2 = iobj.W_IntObject(y)
  244. v = f1.descr_xor(self.space, f2)
  245. assert v.intval == x ^ y
  246. def test_or(self):
  247. x = 12345678
  248. y = 2
  249. f1 = iobj.W_IntObject(x)
  250. f2 = iobj.W_IntObject(y)
  251. v = f1.descr_or(self.space, f2)
  252. assert v.intval == x | y
  253. def test_int(self):
  254. f1 = iobj.W_IntObject(1)
  255. result = f1.int(self.space)
  256. assert result == f1
  257. def test_oct(self):
  258. x = 012345
  259. f1 = iobj.W_IntObject(x)
  260. result = f1.descr_oct(self.space)
  261. assert self.space.unwrap(result) == oct(x)
  262. def test_hex(self):
  263. x = 0x12345
  264. f1 = iobj.W_IntObject(x)
  265. result = f1.descr_hex(self.space)
  266. assert self.space.unwrap(result) == hex(x)
  267. class AppTestInt:
  268. def test_conjugate(self):
  269. assert (1).conjugate() == 1
  270. assert (-1).conjugate() == -1
  271. class I(int):
  272. pass
  273. assert I(1).conjugate() == 1
  274. class I(int):
  275. def __pos__(self):
  276. return 42
  277. assert I(1).conjugate() == 1
  278. def test_inplace(self):
  279. a = 1
  280. a += 1
  281. assert a == 2
  282. a -= 1
  283. assert a == 1
  284. def test_trunc(self):
  285. import math
  286. assert math.trunc(1) == 1
  287. assert math.trunc(-1) == -1
  288. def test_int_callable(self):
  289. assert 43 == int(43)
  290. def test_numerator_denominator(self):
  291. assert (1).numerator == 1
  292. assert (1).denominator == 1
  293. assert (42).numerator == 42
  294. assert (42).denominator == 1
  295. def test_int_string(self):
  296. assert 42 == int("42")
  297. assert 10000000000 == long("10000000000")
  298. def test_int_unicode(self):
  299. assert 42 == int(unicode('42'))
  300. def test_int_float(self):
  301. assert 4 == int(4.2)
  302. def test_int_str_repr(self):
  303. assert "42" == str(42)
  304. assert "42" == repr(42)
  305. raises(ValueError, int, '0x2A')
  306. def test_int_two_param(self):
  307. assert 42 == int('0x2A', 0)
  308. assert 42 == int('2A', 16)
  309. assert 42 == int('42', 10)
  310. raises(TypeError, int, 1, 10)
  311. raises(TypeError, int, '5', '9')
  312. def test_int_largenums(self):
  313. import sys
  314. for x in [-sys.maxint-1, -1, sys.maxint]:
  315. y = int(str(x))
  316. assert y == x
  317. assert type(y) is int
  318. def test_shift_zeros(self):
  319. assert (1 << 0) == 1
  320. assert (1 >> 0) == 1
  321. def test_overflow(self):
  322. import sys
  323. n = sys.maxint + 1
  324. assert isinstance(n, long)
  325. def test_pow(self):
  326. assert pow(2, -10) == 1/1024.
  327. def test_int_w_long_arg(self):
  328. assert int(10000000000) == 10000000000L
  329. assert int("10000000000") == 10000000000l
  330. raises(ValueError, int, "10000000000JUNK")
  331. raises(ValueError, int, "10000000000JUNK", 10)
  332. def test_int_subclass_ctr(self):
  333. import sys
  334. class j(int):
  335. pass
  336. assert j(100) == 100
  337. assert isinstance(j(100),j)
  338. assert j(100L) == 100
  339. assert j("100") == 100
  340. assert j("100",2) == 4
  341. assert isinstance(j("100",2),j)
  342. raises(OverflowError,j,sys.maxint+1)
  343. raises(OverflowError,j,str(sys.maxint+1))
  344. def test_int_subclass_ops(self):
  345. import sys
  346. class j(int):
  347. def __add__(self, other):
  348. return "add."
  349. def __iadd__(self, other):
  350. return "iadd."
  351. def __sub__(self, other):
  352. return "sub."
  353. def __isub__(self, other):
  354. return "isub."
  355. def __mul__(self, other):
  356. return "mul."
  357. def __imul__(self, other):
  358. return "imul."
  359. def __lshift__(self, other):
  360. return "lshift."
  361. def __ilshift__(self, other):
  362. return "ilshift."
  363. assert j(100) + 5 == "add."
  364. assert j(100) + str == "add."
  365. assert j(100) - 5 == "sub."
  366. assert j(100) - str == "sub."
  367. assert j(100) * 5 == "mul."
  368. assert j(100) * str == "mul."
  369. assert j(100) << 5 == "lshift."
  370. assert j(100) << str == "lshift."
  371. assert (5 + j(100), type(5 + j(100))) == ( 105, int)
  372. assert (5 - j(100), type(5 - j(100))) == ( -95, int)
  373. assert (5 * j(100), type(5 * j(100))) == ( 500, int)
  374. assert (5 << j(100), type(5 << j(100))) == (5 << 100, long)
  375. assert (j(100) >> 2, type(j(100) >> 2)) == ( 25, int)
  376. def test_int_subclass_int(self):
  377. class j(int):
  378. def __int__(self):
  379. return value
  380. def __repr__(self):
  381. return '<instance of j>'
  382. class subint(int):
  383. pass
  384. class sublong(long):
  385. pass
  386. value = 42L
  387. assert int(j()) == 42
  388. value = 4200000000000000000000000000000000L
  389. assert int(j()) == 4200000000000000000000000000000000L
  390. value = subint(42)
  391. assert int(j()) == 42 and type(int(j())) is subint
  392. value = sublong(4200000000000000000000000000000000L)
  393. assert (int(j()) == 4200000000000000000000000000000000L
  394. and type(int(j())) is sublong)
  395. value = 42.0
  396. raises(TypeError, int, j())
  397. value = "foo"
  398. raises(TypeError, int, j())
  399. def test_special_int(self):
  400. class a(object):
  401. def __int__(self):
  402. self.ar = True
  403. return None
  404. inst = a()
  405. raises(TypeError, int, inst)
  406. assert inst.ar == True
  407. class b(object):
  408. pass
  409. raises((AttributeError,TypeError), int, b())
  410. def test_special_long(self):
  411. class a(object):
  412. def __long__(self):
  413. self.ar = True
  414. return None
  415. inst = a()
  416. raises(TypeError, long, inst)
  417. assert inst.ar == True
  418. class b(object):
  419. pass
  420. raises((AttributeError,TypeError), long, b())
  421. def test_just_trunc(self):
  422. class myint(object):
  423. def __trunc__(self):
  424. return 42
  425. assert int(myint()) == 42
  426. def test_override___int__(self):
  427. class myint(int):
  428. def __int__(self):
  429. return 42
  430. assert int(myint(21)) == 42
  431. class myotherint(int):
  432. pass
  433. assert int(myotherint(21)) == 21
  434. def test_trunc_returns_non_int(self):
  435. class Integral(object):
  436. def __int__(self):
  437. return 42
  438. class TruncReturnsNonInt(object):
  439. def __trunc__(self):
  440. return Integral()
  441. assert int(TruncReturnsNonInt()) == 42
  442. def test_int_before_string(self):
  443. class Integral(str):
  444. def __int__(self):
  445. return 42
  446. assert int(Integral('abc')) == 42
  447. def test_getnewargs(self):
  448. assert 0 .__getnewargs__() == (0,)
  449. def test_cmp(self):
  450. skip("This is a 'wont fix' case")
  451. # We don't have __cmp__, we consistently have __eq__ & the others
  452. # instead. In CPython some types have __cmp__ and some types have
  453. # __eq__ & the others.
  454. assert 1 .__cmp__
  455. assert int .__cmp__
  456. def test_bit_length(self):
  457. for val, bits in [
  458. (0, 0),
  459. (1, 1),
  460. (10, 4),
  461. (150, 8),
  462. (-1, 1),
  463. (-2, 2),
  464. (-3, 2),
  465. (-4, 3),
  466. (-10, 4),
  467. (-150, 8),
  468. ]:
  469. assert val.bit_length() == bits
  470. def test_bit_length_max(self):
  471. import sys
  472. val = -sys.maxint-1
  473. bits = 32 if val == -2147483648 else 64
  474. assert val.bit_length() == bits
  475. def test_int_real(self):
  476. class A(int): pass
  477. b = A(5).real
  478. assert type(b) is int
  479. def test_int_error_msg(self):
  480. e = raises(TypeError, int, [])
  481. assert str(e.value) == (
  482. "int() argument must be a string or a number, not 'list'")
  483. def test_invalid_literal_message(self):
  484. import sys
  485. if '__pypy__' not in sys.builtin_module_names:
  486. skip('PyPy 2.x/CPython 3.4 only')
  487. for value in b' 1j ', u' 1٢٣٤j ':
  488. try:
  489. int(value)
  490. except ValueError as e:
  491. assert repr(value) in str(e)
  492. else:
  493. assert False, value
  494. def test_coerce(self):
  495. assert 3 .__coerce__(4) == (3, 4)
  496. assert 3 .__coerce__(4L) == NotImplemented
  497. def test_fake_int_as_base(self):
  498. class MyInt(object):
  499. def __init__(self, x):
  500. self.x = x
  501. def __int__(self):
  502. return self.x
  503. base = MyInt(24)
  504. assert int('10', base) == 24
  505. def test_truediv(self):
  506. import operator
  507. x = 1000000
  508. a = x / 2
  509. assert a == 500000
  510. a = operator.truediv(x, 2)
  511. assert a == 500000.0
  512. x = 63050394783186940
  513. a = x / 7
  514. assert a == 9007199254740991
  515. a = operator.truediv(x, 7)
  516. assert a == 9007199254740991.0
  517. def test_truediv_future(self):
  518. ns = dict(x=63050394783186940)
  519. exec("from __future__ import division; import operator; "
  520. "a = x / 7; b = operator.truediv(x, 7)", ns)
  521. assert ns['a'] == 9007199254740991.0
  522. assert ns['b'] == 9007199254740991.0
  523. def test_int_of_bool(self):
  524. x = int(False)
  525. assert x == 0
  526. assert type(x) is int
  527. assert str(x) == "0"
  528. class AppTestIntShortcut(AppTestInt):
  529. spaceconfig = {"objspace.std.intshortcut": True}
  530. def test_inplace(self):
  531. # ensure other inplace ops still work
  532. l = []
  533. l += xrange(5)
  534. assert l == list(range(5))
  535. a = 8.5
  536. a -= .5
  537. assert a == 8