irbuild-i16.test 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. # Test cases for i16 native ints. Focus on things that are different from i64; no need to
  2. # duplicate all i64 test cases here.
  3. [case testI16BinaryOp]
  4. from mypy_extensions import i16
  5. def add_op(x: i16, y: i16) -> i16:
  6. x = y + x
  7. y = x + 5
  8. y += x
  9. y += 7
  10. x = 5 + y
  11. return x
  12. def compare(x: i16, y: i16) -> None:
  13. a = x == y
  14. b = x == -5
  15. c = x < y
  16. d = x < -5
  17. e = -5 == x
  18. f = -5 < x
  19. [out]
  20. def add_op(x, y):
  21. x, y, r0, r1, r2, r3, r4 :: i16
  22. L0:
  23. r0 = y + x
  24. x = r0
  25. r1 = x + 5
  26. y = r1
  27. r2 = y + x
  28. y = r2
  29. r3 = y + 7
  30. y = r3
  31. r4 = 5 + y
  32. x = r4
  33. return x
  34. def compare(x, y):
  35. x, y :: i16
  36. r0 :: bit
  37. a :: bool
  38. r1 :: bit
  39. b :: bool
  40. r2 :: bit
  41. c :: bool
  42. r3 :: bit
  43. d :: bool
  44. r4 :: bit
  45. e :: bool
  46. r5 :: bit
  47. f :: bool
  48. L0:
  49. r0 = x == y
  50. a = r0
  51. r1 = x == -5
  52. b = r1
  53. r2 = x < y :: signed
  54. c = r2
  55. r3 = x < -5 :: signed
  56. d = r3
  57. r4 = -5 == x
  58. e = r4
  59. r5 = -5 < x :: signed
  60. f = r5
  61. return 1
  62. [case testI16UnaryOp]
  63. from mypy_extensions import i16
  64. def unary(x: i16) -> i16:
  65. y = -x
  66. x = ~y
  67. y = +x
  68. return y
  69. [out]
  70. def unary(x):
  71. x, r0, y, r1 :: i16
  72. L0:
  73. r0 = 0 - x
  74. y = r0
  75. r1 = y ^ -1
  76. x = r1
  77. y = x
  78. return y
  79. [case testI16DivisionByConstant]
  80. from mypy_extensions import i16
  81. def div_by_constant(x: i16) -> i16:
  82. x = x // 5
  83. x //= 17
  84. return x
  85. [out]
  86. def div_by_constant(x):
  87. x, r0, r1 :: i16
  88. r2, r3, r4 :: bit
  89. r5 :: i16
  90. r6 :: bit
  91. r7, r8, r9 :: i16
  92. r10, r11, r12 :: bit
  93. r13 :: i16
  94. r14 :: bit
  95. r15 :: i16
  96. L0:
  97. r0 = x / 5
  98. r1 = r0
  99. r2 = x < 0 :: signed
  100. r3 = 5 < 0 :: signed
  101. r4 = r2 == r3
  102. if r4 goto L3 else goto L1 :: bool
  103. L1:
  104. r5 = r1 * 5
  105. r6 = r5 == x
  106. if r6 goto L3 else goto L2 :: bool
  107. L2:
  108. r7 = r1 - 1
  109. r1 = r7
  110. L3:
  111. x = r1
  112. r8 = x / 17
  113. r9 = r8
  114. r10 = x < 0 :: signed
  115. r11 = 17 < 0 :: signed
  116. r12 = r10 == r11
  117. if r12 goto L6 else goto L4 :: bool
  118. L4:
  119. r13 = r9 * 17
  120. r14 = r13 == x
  121. if r14 goto L6 else goto L5 :: bool
  122. L5:
  123. r15 = r9 - 1
  124. r9 = r15
  125. L6:
  126. x = r9
  127. return x
  128. [case testI16ModByConstant]
  129. from mypy_extensions import i16
  130. def mod_by_constant(x: i16) -> i16:
  131. x = x % 5
  132. x %= 17
  133. return x
  134. [out]
  135. def mod_by_constant(x):
  136. x, r0, r1 :: i16
  137. r2, r3, r4, r5 :: bit
  138. r6, r7, r8 :: i16
  139. r9, r10, r11, r12 :: bit
  140. r13 :: i16
  141. L0:
  142. r0 = x % 5
  143. r1 = r0
  144. r2 = x < 0 :: signed
  145. r3 = 5 < 0 :: signed
  146. r4 = r2 == r3
  147. if r4 goto L3 else goto L1 :: bool
  148. L1:
  149. r5 = r1 == 0
  150. if r5 goto L3 else goto L2 :: bool
  151. L2:
  152. r6 = r1 + 5
  153. r1 = r6
  154. L3:
  155. x = r1
  156. r7 = x % 17
  157. r8 = r7
  158. r9 = x < 0 :: signed
  159. r10 = 17 < 0 :: signed
  160. r11 = r9 == r10
  161. if r11 goto L6 else goto L4 :: bool
  162. L4:
  163. r12 = r8 == 0
  164. if r12 goto L6 else goto L5 :: bool
  165. L5:
  166. r13 = r8 + 17
  167. r8 = r13
  168. L6:
  169. x = r8
  170. return x
  171. [case testI16DivModByVariable]
  172. from mypy_extensions import i16
  173. def divmod(x: i16, y: i16) -> i16:
  174. a = x // y
  175. return a % y
  176. [out]
  177. def divmod(x, y):
  178. x, y, r0, a, r1 :: i16
  179. L0:
  180. r0 = CPyInt16_Divide(x, y)
  181. a = r0
  182. r1 = CPyInt16_Remainder(a, y)
  183. return r1
  184. [case testI16BinaryOperationWithOutOfRangeOperand]
  185. from mypy_extensions import i16
  186. def out_of_range(x: i16) -> None:
  187. x + (-32769)
  188. (-32770) + x
  189. x * 32768
  190. x + 32767 # OK
  191. (-32768) + x # OK
  192. [out]
  193. main:4: error: Value -32769 is out of range for "i16"
  194. main:5: error: Value -32770 is out of range for "i16"
  195. main:6: error: Value 32768 is out of range for "i16"
  196. [case testI16BoxAndUnbox]
  197. from typing import Any
  198. from mypy_extensions import i16
  199. def f(x: Any) -> Any:
  200. y: i16 = x
  201. return y
  202. [out]
  203. def f(x):
  204. x :: object
  205. r0, y :: i16
  206. r1 :: object
  207. L0:
  208. r0 = unbox(i16, x)
  209. y = r0
  210. r1 = box(i16, y)
  211. return r1
  212. [case testI16MixedCompare1]
  213. from mypy_extensions import i16
  214. def f(x: int, y: i16) -> bool:
  215. return x == y
  216. [out]
  217. def f(x, y):
  218. x :: int
  219. y :: i16
  220. r0 :: native_int
  221. r1, r2, r3 :: bit
  222. r4 :: native_int
  223. r5, r6 :: i16
  224. r7 :: bit
  225. L0:
  226. r0 = x & 1
  227. r1 = r0 == 0
  228. if r1 goto L1 else goto L4 :: bool
  229. L1:
  230. r2 = x < 65536 :: signed
  231. if r2 goto L2 else goto L4 :: bool
  232. L2:
  233. r3 = x >= -65536 :: signed
  234. if r3 goto L3 else goto L4 :: bool
  235. L3:
  236. r4 = x >> 1
  237. r5 = truncate r4: native_int to i16
  238. r6 = r5
  239. goto L5
  240. L4:
  241. CPyInt16_Overflow()
  242. unreachable
  243. L5:
  244. r7 = r6 == y
  245. return r7
  246. [case testI16MixedCompare2]
  247. from mypy_extensions import i16
  248. def f(x: i16, y: int) -> bool:
  249. return x == y
  250. [out]
  251. def f(x, y):
  252. x :: i16
  253. y :: int
  254. r0 :: native_int
  255. r1, r2, r3 :: bit
  256. r4 :: native_int
  257. r5, r6 :: i16
  258. r7 :: bit
  259. L0:
  260. r0 = y & 1
  261. r1 = r0 == 0
  262. if r1 goto L1 else goto L4 :: bool
  263. L1:
  264. r2 = y < 65536 :: signed
  265. if r2 goto L2 else goto L4 :: bool
  266. L2:
  267. r3 = y >= -65536 :: signed
  268. if r3 goto L3 else goto L4 :: bool
  269. L3:
  270. r4 = y >> 1
  271. r5 = truncate r4: native_int to i16
  272. r6 = r5
  273. goto L5
  274. L4:
  275. CPyInt16_Overflow()
  276. unreachable
  277. L5:
  278. r7 = x == r6
  279. return r7
  280. [case testI16ConvertToInt]
  281. from mypy_extensions import i16
  282. def i16_to_int(a: i16) -> int:
  283. return a
  284. [out]
  285. def i16_to_int(a):
  286. a :: i16
  287. r0 :: native_int
  288. r1 :: int
  289. L0:
  290. r0 = extend signed a: i16 to native_int
  291. r1 = r0 << 1
  292. return r1
  293. [case testI16OperatorAssignmentMixed]
  294. from mypy_extensions import i16
  295. def f(a: i16) -> None:
  296. x = 0
  297. x += a
  298. [out]
  299. def f(a):
  300. a :: i16
  301. x :: int
  302. r0 :: native_int
  303. r1, r2, r3 :: bit
  304. r4 :: native_int
  305. r5, r6, r7 :: i16
  306. r8 :: native_int
  307. r9 :: int
  308. L0:
  309. x = 0
  310. r0 = x & 1
  311. r1 = r0 == 0
  312. if r1 goto L1 else goto L4 :: bool
  313. L1:
  314. r2 = x < 65536 :: signed
  315. if r2 goto L2 else goto L4 :: bool
  316. L2:
  317. r3 = x >= -65536 :: signed
  318. if r3 goto L3 else goto L4 :: bool
  319. L3:
  320. r4 = x >> 1
  321. r5 = truncate r4: native_int to i16
  322. r6 = r5
  323. goto L5
  324. L4:
  325. CPyInt16_Overflow()
  326. unreachable
  327. L5:
  328. r7 = r6 + a
  329. r8 = extend signed r7: i16 to native_int
  330. r9 = r8 << 1
  331. x = r9
  332. return 1
  333. [case testI16InitializeFromLiteral]
  334. from mypy_extensions import i16, i64
  335. def f() -> None:
  336. x: i16 = 0
  337. y: i16 = -127
  338. z: i16 = 5 + 7
  339. [out]
  340. def f():
  341. x, y, z :: i16
  342. L0:
  343. x = 0
  344. y = -127
  345. z = 12
  346. return 1
  347. [case testI16ExplicitConversionFromNativeInt]
  348. from mypy_extensions import i64, i32, i16
  349. def from_i16(x: i16) -> i16:
  350. return i16(x)
  351. def from_i32(x: i32) -> i16:
  352. return i16(x)
  353. def from_i64(x: i64) -> i16:
  354. return i16(x)
  355. [out]
  356. def from_i16(x):
  357. x :: i16
  358. L0:
  359. return x
  360. def from_i32(x):
  361. x :: i32
  362. r0 :: i16
  363. L0:
  364. r0 = truncate x: i32 to i16
  365. return r0
  366. def from_i64(x):
  367. x :: i64
  368. r0 :: i16
  369. L0:
  370. r0 = truncate x: i64 to i16
  371. return r0
  372. [case testI16ExplicitConversionFromInt]
  373. from mypy_extensions import i16
  374. def f(x: int) -> i16:
  375. return i16(x)
  376. [out]
  377. def f(x):
  378. x :: int
  379. r0 :: native_int
  380. r1, r2, r3 :: bit
  381. r4 :: native_int
  382. r5, r6 :: i16
  383. L0:
  384. r0 = x & 1
  385. r1 = r0 == 0
  386. if r1 goto L1 else goto L4 :: bool
  387. L1:
  388. r2 = x < 65536 :: signed
  389. if r2 goto L2 else goto L4 :: bool
  390. L2:
  391. r3 = x >= -65536 :: signed
  392. if r3 goto L3 else goto L4 :: bool
  393. L3:
  394. r4 = x >> 1
  395. r5 = truncate r4: native_int to i16
  396. r6 = r5
  397. goto L5
  398. L4:
  399. CPyInt16_Overflow()
  400. unreachable
  401. L5:
  402. return r6
  403. [case testI16ExplicitConversionFromLiteral]
  404. from mypy_extensions import i16
  405. def f() -> None:
  406. x = i16(0)
  407. y = i16(11)
  408. z = i16(-3)
  409. a = i16(32767)
  410. b = i16(32768) # Truncate
  411. c = i16(-32768)
  412. d = i16(-32769) # Truncate
  413. [out]
  414. def f():
  415. x, y, z, a, b, c, d :: i16
  416. L0:
  417. x = 0
  418. y = 11
  419. z = -3
  420. a = 32767
  421. b = -32768
  422. c = -32768
  423. d = 32767
  424. return 1
  425. [case testI16ExplicitConversionFromVariousTypes]
  426. from mypy_extensions import i16
  427. def bool_to_i16(b: bool) -> i16:
  428. return i16(b)
  429. def str_to_i16(s: str) -> i16:
  430. return i16(s)
  431. class C:
  432. def __int__(self) -> i16:
  433. return 5
  434. def instance_to_i16(c: C) -> i16:
  435. return i16(c)
  436. def float_to_i16(x: float) -> i16:
  437. return i16(x)
  438. [out]
  439. def bool_to_i16(b):
  440. b :: bool
  441. r0 :: i16
  442. L0:
  443. r0 = extend b: builtins.bool to i16
  444. return r0
  445. def str_to_i16(s):
  446. s :: str
  447. r0 :: object
  448. r1 :: i16
  449. L0:
  450. r0 = CPyLong_FromStr(s)
  451. r1 = unbox(i16, r0)
  452. return r1
  453. def C.__int__(self):
  454. self :: __main__.C
  455. L0:
  456. return 5
  457. def instance_to_i16(c):
  458. c :: __main__.C
  459. r0 :: i16
  460. L0:
  461. r0 = c.__int__()
  462. return r0
  463. def float_to_i16(x):
  464. x :: float
  465. r0 :: int
  466. r1 :: native_int
  467. r2, r3, r4 :: bit
  468. r5 :: native_int
  469. r6, r7 :: i16
  470. L0:
  471. r0 = CPyTagged_FromFloat(x)
  472. r1 = r0 & 1
  473. r2 = r1 == 0
  474. if r2 goto L1 else goto L4 :: bool
  475. L1:
  476. r3 = r0 < 65536 :: signed
  477. if r3 goto L2 else goto L4 :: bool
  478. L2:
  479. r4 = r0 >= -65536 :: signed
  480. if r4 goto L3 else goto L4 :: bool
  481. L3:
  482. r5 = r0 >> 1
  483. r6 = truncate r5: native_int to i16
  484. r7 = r6
  485. goto L5
  486. L4:
  487. CPyInt16_Overflow()
  488. unreachable
  489. L5:
  490. return r7