Debian Codegraph @

mypyc/test-data/irbuild-u8.test

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