run-integers.test 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. # Test cases for integers (compile and run)
  2. [case testInc]
  3. def inc(x: int) -> int:
  4. return x + 1
  5. [file driver.py]
  6. from native import inc
  7. print(inc(3))
  8. print(inc(-5))
  9. print(inc(10**20))
  10. [out]
  11. 4
  12. -4
  13. 100000000000000000001
  14. [case testCount]
  15. def count(n: int) -> int:
  16. i = 1
  17. while i <= n:
  18. i = i + 1
  19. return i
  20. [file driver.py]
  21. from native import count
  22. print(count(0))
  23. print(count(1))
  24. print(count(5))
  25. [out]
  26. 1
  27. 2
  28. 6
  29. [case testIntMathOps]
  30. # This tests integer math things that are either easier to test in Python than
  31. # in our C tests or are tested here because (for annoying reasons) we don't run
  32. # the C unit tests in our 32-bit CI.
  33. def multiply(x: int, y: int) -> int:
  34. return x * y
  35. # these stringify their outputs because that will catch if exceptions are mishandled
  36. def floor_div(x: int, y: int) -> str:
  37. return str(x // y)
  38. def remainder(x: int, y: int) -> str:
  39. return str(x % y)
  40. [file driver.py]
  41. from native import multiply, floor_div, remainder
  42. def test_multiply(x, y):
  43. assert multiply(x, y) == x * y
  44. def test_floor_div(x, y):
  45. assert floor_div(x, y) == str(x // y)
  46. def test_remainder(x, y):
  47. assert remainder(x, y) == str(x % y)
  48. test_multiply(10**6, 10**6)
  49. test_multiply(2**15, 2**15-1)
  50. test_multiply(2**14, 2**14)
  51. test_multiply(10**12, 10**12)
  52. test_multiply(2**30, 2**30-1)
  53. test_multiply(2**29, 2**29)
  54. test_floor_div(-2**62, -1)
  55. test_floor_div(-2**30, -1)
  56. try:
  57. floor_div(10, 0)
  58. except ZeroDivisionError:
  59. pass
  60. else:
  61. assert False, "Expected ZeroDivisionError"
  62. test_remainder(-2**62, -1)
  63. test_remainder(-2**30, -1)
  64. try:
  65. remainder(10, 0)
  66. except ZeroDivisionError:
  67. pass
  68. else:
  69. assert False, "Expected ZeroDivisionError"
  70. [case testBigIntLiteral]
  71. def big_int() -> None:
  72. a_62_bit = 4611686018427387902
  73. max_62_bit = 4611686018427387903
  74. b_63_bit = 4611686018427387904
  75. c_63_bit = 9223372036854775806
  76. max_63_bit = 9223372036854775807
  77. d_64_bit = 9223372036854775808
  78. max_32_bit = 2147483647
  79. max_32_bit_plus1 = 2147483648
  80. max_31_bit = 1073741823
  81. max_31_bit_plus1 = 1073741824
  82. neg = -1234567
  83. min_signed_63_bit = -4611686018427387904
  84. underflow = -4611686018427387905
  85. min_signed_64_bit = -9223372036854775808
  86. min_signed_31_bit = -1073741824
  87. min_signed_31_bit_plus1 = -1073741823
  88. min_signed_31_bit_minus1 = -1073741825
  89. min_signed_32_bit = -2147483648
  90. print(a_62_bit)
  91. print(max_62_bit)
  92. print(b_63_bit)
  93. print(c_63_bit)
  94. print(max_63_bit)
  95. print(d_64_bit)
  96. print('==')
  97. print(max_32_bit)
  98. print(max_32_bit_plus1)
  99. print(max_31_bit)
  100. print(max_31_bit_plus1)
  101. print(neg)
  102. print(min_signed_63_bit)
  103. print(underflow)
  104. print(min_signed_64_bit)
  105. print(min_signed_31_bit)
  106. print(min_signed_31_bit_plus1)
  107. print(min_signed_31_bit_minus1)
  108. print(min_signed_32_bit)
  109. [file driver.py]
  110. from native import big_int
  111. big_int()
  112. [out]
  113. 4611686018427387902
  114. 4611686018427387903
  115. 4611686018427387904
  116. 9223372036854775806
  117. 9223372036854775807
  118. 9223372036854775808
  119. ==
  120. 2147483647
  121. 2147483648
  122. 1073741823
  123. 1073741824
  124. -1234567
  125. -4611686018427387904
  126. -4611686018427387905
  127. -9223372036854775808
  128. -1073741824
  129. -1073741823
  130. -1073741825
  131. -2147483648
  132. [case testNeg]
  133. def neg(x: int) -> int:
  134. return -x
  135. [file driver.py]
  136. from native import neg
  137. assert neg(5) == -5
  138. assert neg(-5) == 5
  139. assert neg(1073741823) == -1073741823
  140. assert neg(-1073741823) == 1073741823
  141. assert neg(1073741824) == -1073741824
  142. assert neg(-1073741824) == 1073741824
  143. assert neg(2147483647) == -2147483647
  144. assert neg(-2147483647) == 2147483647
  145. assert neg(2147483648) == -2147483648
  146. assert neg(-2147483648) == 2147483648
  147. assert neg(4611686018427387904) == -4611686018427387904
  148. assert neg(-4611686018427387904) == 4611686018427387904
  149. assert neg(9223372036854775807) == -9223372036854775807
  150. assert neg(-9223372036854775807) == 9223372036854775807
  151. assert neg(9223372036854775808) == -9223372036854775808
  152. assert neg(-9223372036854775808) == 9223372036854775808
  153. [case testIsinstanceIntAndNotBool]
  154. def test_isinstance_int_and_not_bool(value: object) -> bool:
  155. return isinstance(value, int) and not isinstance(value, bool)
  156. [file driver.py]
  157. from native import test_isinstance_int_and_not_bool
  158. assert test_isinstance_int_and_not_bool(True) == False
  159. assert test_isinstance_int_and_not_bool(1) == True
  160. [case testIntOps]
  161. from typing import Any
  162. from testutil import assertRaises
  163. def check_and(x: int, y: int) -> None:
  164. # eval() can be trusted to calculate expected result
  165. expected = eval('{} & {}'.format(x, y))
  166. actual = x & y
  167. assert actual == expected, '{} & {}: got {}, expected {}'.format(x, y, actual, expected)
  168. def check_or(x: int, y: int) -> None:
  169. # eval() can be trusted to calculate expected result
  170. expected = eval('{} | {}'.format(x, y))
  171. actual = x | y
  172. assert actual == expected, '{} | {}: got {}, expected {}'.format(x, y, actual, expected)
  173. def check_xor(x: int, y: int) -> None:
  174. # eval() can be trusted to calculate expected result
  175. expected = eval('{} ^ {}'.format(x, y))
  176. actual = x ^ y
  177. assert actual == expected, '{} ^ {}: got {}, expected {}'.format(x, y, actual, expected)
  178. def check_bitwise(x: int, y: int) -> None:
  179. for l, r in (x, y), (y, x):
  180. for ll, rr in (l, r), (-l, r), (l, -r), (-l, -r):
  181. check_and(ll, rr)
  182. check_or(ll, rr)
  183. check_xor(ll, rr)
  184. SHIFT = 30
  185. DIGIT0a = 615729753
  186. DIGIT0b = 832796681
  187. DIGIT1a = 744342356 << SHIFT
  188. DIGIT1b = 321006080 << SHIFT
  189. DIGIT2a = 643582106 << (SHIFT * 2)
  190. DIGIT2b = 656420725 << (SHIFT * 2)
  191. DIGIT50 = 315723472 << (SHIFT * 50)
  192. DIGIT100a = 1020652627 << (SHIFT * 100)
  193. DIGIT100b = 923752451 << (SHIFT * 100)
  194. BIG_SHORT = 3491190729721336556
  195. MAX_SHORT = (1 << 62) - 1
  196. MIN_SHORT = -(1 << 62)
  197. MAX_SHORT_32 = (1 << 30) - 1
  198. MIN_SHORT_32 = -(1 << 30)
  199. def test_and_or_xor() -> None:
  200. check_bitwise(0, 0)
  201. check_bitwise(0, 1)
  202. check_bitwise(1, 1)
  203. check_bitwise(DIGIT0a, DIGIT0b)
  204. check_bitwise(DIGIT1a, DIGIT1b)
  205. check_bitwise(DIGIT2a, DIGIT2b)
  206. check_bitwise(DIGIT100a, DIGIT100b)
  207. check_bitwise(DIGIT0a, DIGIT0b + DIGIT2a)
  208. check_bitwise(DIGIT0a, DIGIT0b + DIGIT50)
  209. check_bitwise(DIGIT50 + DIGIT1a, DIGIT100a + DIGIT2b)
  210. check_bitwise(BIG_SHORT, DIGIT0a)
  211. check_bitwise(BIG_SHORT, DIGIT0a + DIGIT1a)
  212. check_bitwise(BIG_SHORT, DIGIT0a + DIGIT1a + DIGIT2a)
  213. check_bitwise(BIG_SHORT, DIGIT0a + DIGIT1a + DIGIT2a + DIGIT50)
  214. for x in range(-25, 25):
  215. for y in range(-25, 25):
  216. check_bitwise(x, y)
  217. def test_bitwise_inplace() -> None:
  218. # Basic sanity checks; these should use the same code as the non-in-place variants
  219. for x, y in (DIGIT0a, DIGIT1a), (DIGIT2a, DIGIT0a + DIGIT2b):
  220. n = x
  221. n &= y
  222. assert n == x & y
  223. n = x
  224. n |= y
  225. assert n == x | y
  226. n = x
  227. n ^= y
  228. assert n == x ^ y
  229. def check_invert(x: int) -> None:
  230. # Use eval() as the source of truth
  231. assert ~x == eval('~{}'.format(x))
  232. assert ~(-x) == eval('~({})'.format(-x))
  233. def test_invert() -> None:
  234. check_invert(0)
  235. check_invert(1)
  236. check_invert(DIGIT0a)
  237. check_invert(DIGIT0a + DIGIT1a)
  238. check_invert(DIGIT0a + DIGIT1a + DIGIT2a)
  239. check_invert(DIGIT0a + DIGIT1a + DIGIT2a + DIGIT50)
  240. check_invert(BIG_SHORT)
  241. for delta in -1, 0, 1:
  242. check_invert(MAX_SHORT + delta)
  243. check_invert(MIN_SHORT + delta)
  244. check_invert(MAX_SHORT_32 + delta)
  245. check_invert(MIN_SHORT_32 + delta)
  246. def check_right_shift(x: int, n: int) -> None:
  247. if n < 0:
  248. try:
  249. x >> n
  250. except ValueError:
  251. return
  252. assert False, "no exception raised"
  253. # Use eval() as the source of truth
  254. expected = eval('{} >> {}'.format(x, n))
  255. actual = x >> n
  256. assert actual == expected, "{} >> {}: got {}, expected {}".format(x, n, actual, expected)
  257. def test_right_shift() -> None:
  258. for x in 0, 1, 1235, DIGIT0a, DIGIT0a + DIGIT1a, DIGIT0a + DIGIT50:
  259. for n in 0, 1, 2, 3, 4, 10, 40, 10000, DIGIT1a, -1, -1334444, -DIGIT1a:
  260. check_right_shift(x, n)
  261. check_right_shift(-x, n)
  262. x = DIGIT0a
  263. x >>= 1
  264. assert x == DIGIT0a >> 1
  265. x = DIGIT50
  266. x >>= 5
  267. assert x == DIGIT50 >> 5
  268. for i in range(256):
  269. check_right_shift(1, i)
  270. check_right_shift(137, i)
  271. check_right_shift(MAX_SHORT, i)
  272. check_right_shift(MAX_SHORT_32, i)
  273. check_right_shift(MAX_SHORT + 1, i)
  274. check_right_shift(MAX_SHORT_32 + 1, i)
  275. for x in 1, DIGIT50:
  276. try:
  277. # It's okay if this raises an exception
  278. assert x >> DIGIT2a == 0
  279. except Exception:
  280. pass
  281. try:
  282. x >> -DIGIT2a
  283. assert False
  284. except Exception:
  285. pass
  286. def check_left_shift(x: int, n: int) -> None:
  287. if n < 0:
  288. try:
  289. x << n
  290. except ValueError:
  291. return
  292. assert False, "no exception raised"
  293. # Use eval() as the source of truth
  294. expected = eval('{} << {}'.format(x, n))
  295. actual = x << n
  296. assert actual == expected, "{} << {}: got {}, expected {}".format(x, n, actual, expected)
  297. def test_left_shift() -> None:
  298. for x in 0, 1, 1235, DIGIT0a, DIGIT0a + DIGIT1a, DIGIT0a + DIGIT50:
  299. for n in 0, 1, 2, 10, 40, 10000, -1, -1334444:
  300. check_left_shift(x, n)
  301. check_left_shift(-x, n)
  302. x = DIGIT0a
  303. x <<= 1
  304. assert x == DIGIT0a << 1
  305. x = DIGIT50
  306. x <<= 5
  307. assert x == DIGIT50 << 5
  308. for shift in range(256):
  309. check_left_shift(1, shift)
  310. check_left_shift(137, shift)
  311. for x in 1, DIGIT50:
  312. try:
  313. x << DIGIT50
  314. assert False
  315. except Exception:
  316. pass
  317. try:
  318. x << -DIGIT50
  319. assert False
  320. except Exception:
  321. pass
  322. def is_true(x: int) -> bool:
  323. if x:
  324. return True
  325. else:
  326. return False
  327. def is_true2(x: int) -> bool:
  328. return bool(x)
  329. def is_false(x: int) -> bool:
  330. if not x:
  331. return True
  332. else:
  333. return False
  334. def test_int_as_bool() -> None:
  335. assert not is_true(0)
  336. assert not is_true2(0)
  337. assert is_false(0)
  338. for x in 1, 55, -1, -7, 1 << 50, 1 << 101, -(1 << 50), -(1 << 101):
  339. assert is_true(x)
  340. assert is_true2(x)
  341. assert not is_false(x)
  342. def bool_as_int(b: bool) -> int:
  343. return b
  344. def bool_as_int2(b: bool) -> int:
  345. return int(b)
  346. def test_bool_as_int() -> None:
  347. assert bool_as_int(False) == 0
  348. assert bool_as_int(True) == 1
  349. assert bool_as_int2(False) == 0
  350. assert bool_as_int2(True) == 1
  351. def no_op_conversion(n: int) -> int:
  352. return int(n)
  353. def test_no_op_conversion() -> None:
  354. for x in 1, 55, -1, -7, 1 << 50, 1 << 101, -(1 << 50), -(1 << 101):
  355. assert no_op_conversion(x) == x
  356. def test_floor_divide() -> None:
  357. for x in range(-100, 100):
  358. for y in range(-100, 100):
  359. if y != 0:
  360. assert x // y == getattr(x, "__floordiv__")(y)
  361. def test_mod() -> None:
  362. for x in range(-100, 100):
  363. for y in range(-100, 100):
  364. if y != 0:
  365. assert x % y == getattr(x, "__mod__")(y)
  366. def test_constant_fold() -> None:
  367. assert str(-5 + 3) == "-2"
  368. assert str(15 - 3) == "12"
  369. assert str(1000 * 1000) == "1000000"
  370. assert str(12325 // 12 ) == "1027"
  371. assert str(87645 % 321) == "12"
  372. assert str(674253 | 76544) == "748493"
  373. assert str(765 ^ 82) == "687"
  374. assert str(6546 << 3) == "52368"
  375. assert str(6546 >> 7) == "51"
  376. assert str(3**5) == "243"
  377. assert str(~76) == "-77"
  378. try:
  379. 2 / 0
  380. except ZeroDivisionError:
  381. pass
  382. else:
  383. assert False, "no exception raised"
  384. x = int()
  385. y = int() - 1
  386. assert x == -1 or y != -3
  387. assert -1 <= x
  388. assert -1 == y
  389. # Use int() to avoid constant propagation
  390. i30 = (1 << 30) + int()
  391. assert i30 == 1 << 30
  392. i31 = (1 << 31) + int()
  393. assert i31 == 1 << 31
  394. i32 = (1 << 32) + int()
  395. assert i32 == 1 << 32
  396. i62 = (1 << 62) + int()
  397. assert i62 == 1 << 62
  398. i63 = (1 << 63) + int()
  399. assert i63 == 1 << 63
  400. i64 = (1 << 64) + int()
  401. assert i64 == 1 << 64
  402. n30 = -(1 << 30) + int()
  403. assert n30 == -(1 << 30)
  404. n31 = -(1 << 31) + int()
  405. assert n31 == -(1 << 31)
  406. n32 = -(1 << 32) + int()
  407. assert n32 == -(1 << 32)
  408. n62 = -(1 << 62) + int()
  409. assert n62 == -(1 << 62)
  410. n63 = -(1 << 63) + int()
  411. assert n63 == -(1 << 63)
  412. n64 = -(1 << 64) + int()
  413. assert n64 == -(1 << 64)
  414. def div_by_2(x: int) -> int:
  415. return x // 2
  416. def div_by_3(x: int) -> int:
  417. return x // 3
  418. def div_by_4(x: int) -> int:
  419. return x // 4
  420. def test_floor_divide_by_literal() -> None:
  421. for i in range(-100, 100):
  422. i_boxed: Any = i
  423. assert div_by_2(i) == i_boxed // int('2')
  424. assert div_by_3(i) == i_boxed // int('3')
  425. assert div_by_4(i) == i_boxed // int('4')
  426. def test_true_divide() -> None:
  427. for x in range(-150, 100):
  428. for y in range(-150, 100):
  429. if y != 0:
  430. assert x / y == getattr(x, "__truediv__")(y)
  431. large1 = (123 + int())**123
  432. large2 = (121 + int())**121
  433. assert large1 / large2 == getattr(large1, "__truediv__")(large2)
  434. assert large1 / 135 == getattr(large1, "__truediv__")(135)
  435. assert large1 / -2 == getattr(large1, "__truediv__")(-2)
  436. assert 17 / large2 == getattr(17, "__truediv__")(large2)
  437. huge = 10**1000 + int()
  438. with assertRaises(OverflowError, "integer division result too large for a float"):
  439. huge / 2
  440. with assertRaises(OverflowError, "integer division result too large for a float"):
  441. huge / -2
  442. assert 1 / huge == 0.0
  443. [case testIntMinMax]
  444. def test_int_min_max() -> None:
  445. x: int = 200
  446. y: int = 30
  447. assert min(x, y) == 30
  448. assert max(x, y) == 200
  449. assert min(y, x) == 30
  450. assert max(y, x) == 200
  451. def test_int_hybrid_min_max() -> None:
  452. from typing import Any
  453. x: object = 30
  454. y: Any = 20.0
  455. assert min(x, y) == 20.0
  456. assert max(x, y) == 30
  457. u: object = 20
  458. v: float = 30.0
  459. assert min(u, v) == 20
  460. assert max(u, v) == 30.0
  461. def test_int_incompatible_min_max() -> None:
  462. x: int = 2
  463. y: str = 'aaa'
  464. try:
  465. print(min(x, y))
  466. except TypeError as e:
  467. assert str(e) == "'<' not supported between instances of 'str' and 'int'"
  468. try:
  469. print(max(x, y))
  470. except TypeError as e:
  471. assert str(e) == "'>' not supported between instances of 'str' and 'int'"
  472. def test_int_bool_min_max() -> None:
  473. x: int = 2
  474. y: bool = False
  475. z: bool = True
  476. assert min(x, y) == False
  477. assert min(x, z) == True
  478. assert max(x, y) == 2
  479. assert max(x, z) == 2
  480. u: int = -10
  481. assert min(u, y) == -10
  482. assert min(u, z) == -10
  483. assert max(u, y) == False
  484. assert max(u, z) == True