test_int.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. import sys
  2. import unittest
  3. L = [
  4. ('0', 0),
  5. ('1', 1),
  6. ('9', 9),
  7. ('10', 10),
  8. ('99', 99),
  9. ('100', 100),
  10. ('314', 314),
  11. (' 314', 314),
  12. ('314 ', 314),
  13. (' \t\t 314 \t\t ', 314),
  14. (repr(sys.maxsize), sys.maxsize),
  15. (' 1x', ValueError),
  16. (' 1 ', 1),
  17. (' 1\02 ', ValueError),
  18. ('', ValueError),
  19. (' ', ValueError),
  20. (' \t\t ', ValueError),
  21. ("\u0200", ValueError)
  22. ]
  23. class IntTestCases(unittest.TestCase):
  24. def test_basic(self):
  25. self.assertEqual(int(314), 314)
  26. self.assertEqual(int(3.14), 3)
  27. # Check that conversion from float truncates towards zero
  28. self.assertEqual(int(-3.14), -3)
  29. self.assertEqual(int(3.9), 3)
  30. self.assertEqual(int(-3.9), -3)
  31. self.assertEqual(int(3.5), 3)
  32. self.assertEqual(int(-3.5), -3)
  33. self.assertEqual(int("-3"), -3)
  34. self.assertEqual(int(" -3 "), -3)
  35. """ fails in skulpt
  36. self.assertEqual(int("\N{EM SPACE}-3\N{EN SPACE}"), -3)
  37. """
  38. # Different base:
  39. self.assertEqual(int("10",16), 16)
  40. # Test conversion from strings and various anomalies
  41. for s, v in L:
  42. for sign in "", "+", "-":
  43. for prefix in "", " ", "\t", " \t\t ":
  44. ss = prefix + sign + s
  45. vv = v
  46. if sign == "-" and v is not ValueError:
  47. vv = -v
  48. try:
  49. self.assertEqual(int(ss), vv)
  50. except ValueError:
  51. pass
  52. """ this causes some errors in skulpt due to other int sizes
  53. s = repr(-1-sys.maxsize)
  54. x = int(s)
  55. self.assertEqual(x+1, -sys.maxsize)
  56. self.assertIsInstance(x, int)
  57. # should return int
  58. self.assertEqual(int(s[1:]), sys.maxsize+1)
  59. """
  60. # should return int
  61. """
  62. skulpt does currently return here a long
  63. """
  64. x = int(1e100)
  65. self.assertIsInstance(x, long)
  66. x = int(-1e100)
  67. self.assertIsInstance(x, long)
  68. # SF bug 434186: 0x80000000/2 != 0x80000000>>1.
  69. # Worked by accident in Windows release build, but failed in debug build.
  70. # Failed in all Linux builds.
  71. x = -1-sys.maxsize
  72. self.assertEqual(x >> 1, x//2)
  73. x = int('1' * 600)
  74. """
  75. skulpt does currently return here a long
  76. self.assertIsInstance(x, int)
  77. """
  78. self.assertIsInstance(x, long)
  79. self.assertRaises(TypeError, int, 1, 12)
  80. self.assertEqual(int('0o123', 0), 83)
  81. self.assertEqual(int('0x123', 16), 291)
  82. # Bug 1679: "0x" is not a valid hex literal
  83. self.assertRaises(ValueError, int, "0x", 16)
  84. self.assertRaises(ValueError, int, "0x", 0)
  85. self.assertRaises(ValueError, int, "0o", 8)
  86. self.assertRaises(ValueError, int, "0o", 0)
  87. self.assertRaises(ValueError, int, "0b", 2)
  88. self.assertRaises(ValueError, int, "0b", 0)
  89. # SF bug 1334662: int(string, base) wrong answers
  90. # Various representations of 2**32 evaluated to 0
  91. # rather than 2**32 in previous versions
  92. self.assertEqual(int('100000000000000000000000000000000', 2), 4294967296)
  93. self.assertEqual(int('102002022201221111211', 3), 4294967296)
  94. self.assertEqual(int('10000000000000000', 4), 4294967296)
  95. self.assertEqual(int('32244002423141', 5), 4294967296)
  96. self.assertEqual(int('1550104015504', 6), 4294967296)
  97. self.assertEqual(int('211301422354', 7), 4294967296)
  98. self.assertEqual(int('40000000000', 8), 4294967296)
  99. self.assertEqual(int('12068657454', 9), 4294967296)
  100. self.assertEqual(int('4294967296', 10), 4294967296)
  101. self.assertEqual(int('1904440554', 11), 4294967296)
  102. self.assertEqual(int('9ba461594', 12), 4294967296)
  103. self.assertEqual(int('535a79889', 13), 4294967296)
  104. self.assertEqual(int('2ca5b7464', 14), 4294967296)
  105. self.assertEqual(int('1a20dcd81', 15), 4294967296)
  106. self.assertEqual(int('100000000', 16), 4294967296)
  107. self.assertEqual(int('a7ffda91', 17), 4294967296)
  108. self.assertEqual(int('704he7g4', 18), 4294967296)
  109. self.assertEqual(int('4f5aff66', 19), 4294967296)
  110. self.assertEqual(int('3723ai4g', 20), 4294967296)
  111. self.assertEqual(int('281d55i4', 21), 4294967296)
  112. self.assertEqual(int('1fj8b184', 22), 4294967296)
  113. self.assertEqual(int('1606k7ic', 23), 4294967296)
  114. self.assertEqual(int('mb994ag', 24), 4294967296)
  115. self.assertEqual(int('hek2mgl', 25), 4294967296)
  116. self.assertEqual(int('dnchbnm', 26), 4294967296)
  117. self.assertEqual(int('b28jpdm', 27), 4294967296)
  118. self.assertEqual(int('8pfgih4', 28), 4294967296)
  119. self.assertEqual(int('76beigg', 29), 4294967296)
  120. self.assertEqual(int('5qmcpqg', 30), 4294967296)
  121. self.assertEqual(int('4q0jto4', 31), 4294967296)
  122. self.assertEqual(int('4000000', 32), 4294967296)
  123. self.assertEqual(int('3aokq94', 33), 4294967296)
  124. self.assertEqual(int('2qhxjli', 34), 4294967296)
  125. self.assertEqual(int('2br45qb', 35), 4294967296)
  126. self.assertEqual(int('1z141z4', 36), 4294967296)
  127. # tests with base 0
  128. # this fails on 3.0, but in 2.x the old octal syntax is allowed
  129. self.assertEqual(int(' 0o123 ', 0), 83)
  130. self.assertEqual(int(' 0o123 ', 0), 83)
  131. self.assertEqual(int('000', 0), 0)
  132. self.assertEqual(int('0o123', 0), 83)
  133. self.assertEqual(int('0x123', 0), 291)
  134. self.assertEqual(int('0b100', 0), 4)
  135. self.assertEqual(int(' 0O123 ', 0), 83)
  136. self.assertEqual(int(' 0X123 ', 0), 291)
  137. self.assertEqual(int(' 0B100 ', 0), 4)
  138. # without base still base 10
  139. self.assertEqual(int('0123'), 123)
  140. self.assertEqual(int('0123', 10), 123)
  141. # tests with prefix and base != 0
  142. self.assertEqual(int('0x123', 16), 291)
  143. self.assertEqual(int('0o123', 8), 83)
  144. self.assertEqual(int('0b100', 2), 4)
  145. self.assertEqual(int('0X123', 16), 291)
  146. self.assertEqual(int('0O123', 8), 83)
  147. self.assertEqual(int('0B100', 2), 4)
  148. # the code has special checks for the first character after the
  149. # type prefix
  150. self.assertRaises(ValueError, int, '0b2', 2)
  151. self.assertRaises(ValueError, int, '0b02', 2)
  152. self.assertRaises(ValueError, int, '0B2', 2)
  153. self.assertRaises(ValueError, int, '0B02', 2)
  154. self.assertRaises(ValueError, int, '0o8', 8)
  155. self.assertRaises(ValueError, int, '0o08', 8)
  156. self.assertRaises(ValueError, int, '0O8', 8)
  157. self.assertRaises(ValueError, int, '0O08', 8)
  158. self.assertRaises(ValueError, int, '0xg', 16)
  159. self.assertRaises(ValueError, int, '0x0g', 16)
  160. self.assertRaises(ValueError, int, '0Xg', 16)
  161. self.assertRaises(ValueError, int, '0X0g', 16)
  162. # SF bug 1334662: int(string, base) wrong answers
  163. # Checks for proper evaluation of 2**32 + 1
  164. self.assertEqual(int('100000000000000000000000000000001', 2), 4294967297)
  165. self.assertEqual(int('102002022201221111212', 3), 4294967297)
  166. self.assertEqual(int('10000000000000001', 4), 4294967297)
  167. self.assertEqual(int('32244002423142', 5), 4294967297)
  168. self.assertEqual(int('1550104015505', 6), 4294967297)
  169. self.assertEqual(int('211301422355', 7), 4294967297)
  170. self.assertEqual(int('40000000001', 8), 4294967297)
  171. self.assertEqual(int('12068657455', 9), 4294967297)
  172. self.assertEqual(int('4294967297', 10), 4294967297)
  173. self.assertEqual(int('1904440555', 11), 4294967297)
  174. self.assertEqual(int('9ba461595', 12), 4294967297)
  175. self.assertEqual(int('535a7988a', 13), 4294967297)
  176. self.assertEqual(int('2ca5b7465', 14), 4294967297)
  177. self.assertEqual(int('1a20dcd82', 15), 4294967297)
  178. self.assertEqual(int('100000001', 16), 4294967297)
  179. self.assertEqual(int('a7ffda92', 17), 4294967297)
  180. self.assertEqual(int('704he7g5', 18), 4294967297)
  181. self.assertEqual(int('4f5aff67', 19), 4294967297)
  182. self.assertEqual(int('3723ai4h', 20), 4294967297)
  183. self.assertEqual(int('281d55i5', 21), 4294967297)
  184. self.assertEqual(int('1fj8b185', 22), 4294967297)
  185. self.assertEqual(int('1606k7id', 23), 4294967297)
  186. self.assertEqual(int('mb994ah', 24), 4294967297)
  187. self.assertEqual(int('hek2mgm', 25), 4294967297)
  188. self.assertEqual(int('dnchbnn', 26), 4294967297)
  189. self.assertEqual(int('b28jpdn', 27), 4294967297)
  190. self.assertEqual(int('8pfgih5', 28), 4294967297)
  191. self.assertEqual(int('76beigh', 29), 4294967297)
  192. self.assertEqual(int('5qmcpqh', 30), 4294967297)
  193. self.assertEqual(int('4q0jto5', 31), 4294967297)
  194. self.assertEqual(int('4000001', 32), 4294967297)
  195. self.assertEqual(int('3aokq95', 33), 4294967297)
  196. self.assertEqual(int('2qhxjlj', 34), 4294967297)
  197. self.assertEqual(int('2br45qc', 35), 4294967297)
  198. self.assertEqual(int('1z141z5', 36), 4294967297)
  199. def test_small_ints(self):
  200. # Bug #3236: Return small longs from PyLong_FromString
  201. self.assertIs(int('10'), 10)
  202. self.assertIs(int('-1'), -1)
  203. """
  204. skulpt does currently not support byte strings
  205. self.assertIs(int(b'10'), 10)
  206. self.assertIs(int(b'-1'), -1)
  207. """
  208. def test_no_args(self):
  209. self.assertEqual(int(), 0)
  210. def test_keyword_args(self):
  211. # Test invoking int() using keyword arguments.
  212. #self.assertEqual(int(x=1.2), 1)
  213. self.assertEqual(int('100', base=2), 4)
  214. #self.assertEqual(int(x='100', base=2), 4)
  215. #self.assertRaises(TypeError, int, base=10)
  216. #self.assertRaises(TypeError, int, base=0)
  217. def test_int_base_limits(self):
  218. """Testing the supported limits of the int() base parameter."""
  219. self.assertEqual(int('0', 5), 0)
  220. self.assertRaises(ValueError, int, '0', 1)
  221. self.assertRaises(ValueError, int, '0', 37)
  222. self.assertRaises(ValueError, int, '0', -909) # An old magic value base from Python 2.
  223. self.assertRaises(ValueError, int, '0', 0-(2**234))
  224. self.assertRaises(ValueError, int, '0', 2**234)
  225. # Bases 2 through 36 are supported.
  226. for base in range(2,37):
  227. self.assertEqual(int('0', base=base), 0)
  228. def test_int_base_bad_types(self):
  229. """Not integer types are not valid bases; issue16772."""
  230. self.assertRaises(TypeError, int, '0', 5.5)
  231. self.assertRaises(TypeError, int, '0', 5.0)
  232. def test_int_base_indexable(self):
  233. class MyIndexable(object):
  234. def __init__(self, value):
  235. self.value = value
  236. def __index__(self):
  237. return self.value
  238. # Check out of range bases.
  239. for base in 2**100, -2**100, 1, 37:
  240. self.assertRaises(ValueError, int, '43', base)
  241. # Check in-range bases.
  242. self.assertEqual(int('101', base=MyIndexable(2)), 5)
  243. self.assertEqual(int('101', base=MyIndexable(10)), 101)
  244. self.assertEqual(int('101', base=MyIndexable(36)), 1 + 36**2)
  245. """
  246. skulpt does not support bytes, byteArray, etc..
  247. def test_non_numeric_input_types(self):
  248. # Test possible non-numeric types for the argument x, including
  249. # subclasses of the explicitly documented accepted types.
  250. class CustomStr(str): pass
  251. class CustomBytes(bytes): pass
  252. class CustomByteArray(bytearray): pass
  253. values = [b'100',
  254. bytearray(b'100'),
  255. CustomStr('100'),
  256. CustomBytes(b'100'),
  257. CustomByteArray(b'100')]
  258. for x in values:
  259. msg = 'x has type %s' % type(x).__name__
  260. self.assertEqual(int(x), 100, msg=msg)
  261. self.assertEqual(int(x, 2), 4, msg=msg)
  262. """
  263. def test_string_float(self):
  264. self.assertRaises(ValueError, int, '1.2')
  265. # removed test due to incompatibilites with skulpt
  266. # skulpt does not really implement the numeric tower
  267. # as specified in PEP3141 and therefore __trunc__
  268. # is not really working
  269. #def test_intconversion(self):
  270. def test_int_subclass_with_int(self):
  271. # skulpt does not support subclassing builtin types
  272. #class MyInt(int):
  273. class MyInt():
  274. def __int__(self):
  275. return 42
  276. # skulpt does not support subclassing builtin types
  277. #class BadInt(int):
  278. class BadInt():
  279. def __int__(self):
  280. return 42.0
  281. my_int = MyInt(7)
  282. # not possible due to unsupported subclassing
  283. #self.assertEqual(my_int, 7)
  284. self.assertEqual(int(my_int), 42)
  285. self.assertRaises(TypeError, int, BadInt())
  286. def test_int_returns_int_subclass(self):
  287. class TruncReturnsIntSubclass:
  288. def __trunc__(self):
  289. return True
  290. good_int = TruncReturnsIntSubclass()
  291. n = int(good_int)
  292. self.assertEqual(n, 1)
  293. def test_error_message(self):
  294. """ skulpt does not support with statements
  295. def check(s, base=None):
  296. with self.assertRaises(ValueError,
  297. msg="int(%r, %r)" % (s, base)) as cm:
  298. if base is None:
  299. int(s)
  300. else:
  301. int(s, base)
  302. self.assertEqual(cm.exception.args[0],
  303. "invalid literal for int() with base %d: %r" %
  304. (10 if base is None else base, s))
  305. check('\xbd')
  306. check('123\xbd')
  307. check(' 123 456 ')
  308. check('123\x00')
  309. # SF bug 1545497: embedded NULs were not detected with explicit base
  310. check('123\x00', 10)
  311. check('123\x00 245', 20)
  312. check('123\x00 245', 16)
  313. check('123\x00245', 20)
  314. check('123\x00245', 16)
  315. # byte string with embedded NUL
  316. check(b'123\x00')
  317. check(b'123\x00', 10)
  318. # non-UTF-8 byte string
  319. check(b'123\xbd')
  320. check(b'123\xbd', 10)
  321. # lone surrogate in Unicode string
  322. check('123\ud800')
  323. check('123\ud800', 10)
  324. """
  325. def check(s, base=None):
  326. if base is None:
  327. int(s)
  328. else:
  329. int(s, base)
  330. self.assertRaises(ValueError, check, '\xbd', None)
  331. self.assertRaises(ValueError, check, '123\xbd', None)
  332. self.assertRaises(ValueError, check, ' 123 456 ', None)
  333. self.assertRaises(ValueError, check, '123\x00', None)
  334. # SF bug 1545497: embedded NULs were not detected with explicit base
  335. self.assertRaises(ValueError, check, '123\x00', 10)
  336. self.assertRaises(ValueError, check, '123\x00', 10)
  337. self.assertRaises(ValueError, check, '123\x00 245', 20)
  338. self.assertRaises(ValueError, check, '123\x00 245', 16)
  339. self.assertRaises(ValueError, check, '123\x00245', 20)
  340. self.assertRaises(ValueError, check, '123\x00245', 16)
  341. # byte string with embedded NUL
  342. #self.assertRaises(ValueError, check, b'123\x00')
  343. #self.assertRaises(ValueError, check, b'123\x00', 10)
  344. # non-UTF-8 byte string
  345. #self.assertRaises(ValueError, check, b'123\xbd')
  346. #self.assertRaises(ValueError, check, b'123\xbd', 10)
  347. # lone surrogate in Unicode string
  348. self.assertRaises(ValueError, check, '123\ud800', None)
  349. self.assertRaises(ValueError, check, '123\ud800', 10)
  350. def test_conjugate(self):
  351. self.assertEqual(int(3).conjugate(), 3)
  352. self.assertEqual(int(-3).conjugate(), -3)
  353. self.assertEqual(bool(True).conjugate(), 1)
  354. self.assertEqual(bool(False).conjugate(), 0)
  355. def test_modulo(self):
  356. # helper
  357. def mod(a, b):
  358. return a % b
  359. self.assertRaises(ZeroDivisionError, mod, 5, 0)
  360. self.assertEqual(mod(5, 1), 0)
  361. self.assertEqual(mod(5, 2), 1)
  362. self.assertEqual(mod(5, 4), 1)
  363. self.assertEqual(mod(5, 5), 0)
  364. self.assertEqual(mod(5, 6), 5)
  365. self.assertEqual(mod(0, 1), 0)
  366. self.assertEqual(mod(-5, 6), 1)
  367. self.assertEqual(mod(-5, -2), -1)
  368. def test_division(self):
  369. self.assertEqual(3/2, 1)
  370. self.assertEqual(3//2, 1)
  371. self.assertEqual(3/2.0, 1.5)
  372. self.assertEqual(3//2.0, 1.0)
  373. self.assertEqual(-3/2, -2)
  374. self.assertEqual(-3//2, -2)
  375. self.assertEqual(-3/2.0, -1.5)
  376. self.assertEqual(-3//2.0, -2.0)
  377. def test_lshift_type(self):
  378. # Bug #620: lshift of 0 should not become long
  379. # 0 << 0
  380. x = 0 << 0
  381. self.assertEqual(x, 0)
  382. self.assertIsInstance(x, int)
  383. x = 0L << 0
  384. self.assertEqual(x, 0L)
  385. self.assertIsInstance(x, long)
  386. # 0 <<
  387. x = 0 << 1
  388. self.assertEqual(x, 0)
  389. self.assertIsInstance(x, int)
  390. x = 0 << 1000
  391. self.assertEqual(x, 0)
  392. self.assertIsInstance(x, int)
  393. x = 0L << 1
  394. self.assertEqual(x, 0L)
  395. self.assertIsInstance(x, long)
  396. x = 0L << 1000
  397. self.assertEqual(x, 0L)
  398. self.assertIsInstance(x, long)
  399. # << 0
  400. x = 1 << 0
  401. self.assertEqual(x, 1)
  402. self.assertIsInstance(x, int)
  403. x = 1L << 0
  404. self.assertEqual(x, 1L)
  405. self.assertIsInstance(x, long)
  406. class IntTest(unittest.TestCase):
  407. def test_int_inherited(self):
  408. class c:
  409. def __int__(self):
  410. return 3
  411. class d(c):
  412. def k(self):
  413. self.b = 2
  414. self.assertEqual(int(c()), 3)
  415. self.assertEqual(int(d()), 3)
  416. if __name__ == '__main__':
  417. unittest.main()