irbuild-u8.test 9.1 KB

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