Debian Codegraph @

mypyc/test-data/irbuild-i16.test

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