Debian Codegraph @

mypyc/test-data/run-i16.test

raw
1 [case testI16BasicOps]
2 from typing import Any, Tuple
3
4 from mypy_extensions import i16, i32, i64
5
6 from testutil import assertRaises
7
8 def test_box_and_unbox() -> None:
9 values = (list(range(-2**15, -2**15 + 100)) +
10 list(range(-1000, 1000)) +
11 list(range(2**15 - 100, 2**15)))
12 for i in values:
13 o: Any = i
14 x: i16 = o
15 o2: Any = x
16 assert o == o2
17 assert x == i
18 with assertRaises(ValueError, "int too large to convert to i16"):
19 o = 2**15
20 x2: i16 = o
21 with assertRaises(ValueError, "int too large to convert to i16"):
22 o = -2**15 - 1
23 x3: i16 = o
24
25 def div_by_7(x: i16) -> i16:
26 return x // 7
27 def div_by_neg_7(x: i16) -> i16:
28 return x // -7
29
30 def div(x: i16, y: i16) -> i16:
31 return x // y
32
33 def test_divide_by_constant() -> None:
34 for i in range(-1000, 1000):
35 assert div_by_7(i) == i // 7
36 for i in range(-2**15, -2**15 + 1000):
37 assert div_by_7(i) == i // 7
38 for i in range(2**15 - 1000, 2**15):
39 assert div_by_7(i) == i // 7
40
41 def test_divide_by_negative_constant() -> None:
42 for i in range(-1000, 1000):
43 assert div_by_neg_7(i) == i // -7
44 for i in range(-2**15, -2**15 + 1000):
45 assert div_by_neg_7(i) == i // -7
46 for i in range(2**15 - 1000, 2**15):
47 assert div_by_neg_7(i) == i // -7
48
49 def test_divide_by_variable() -> None:
50 values = (list(range(-50, 50)) +
51 list(range(-2**15, -2**15 + 10)) +
52 list(range(2**15 - 10, 2**15)))
53 for x in values:
54 for y in values:
55 if y != 0:
56 if x // y == 2**15:
57 with assertRaises(OverflowError, "integer division overflow"):
58 div(x, y)
59 else:
60 assert div(x, y) == x // y
61 else:
62 with assertRaises(ZeroDivisionError, "integer division or modulo by zero"):
63 div(x, y)
64
65 def mod_by_7(x: i16) -> i16:
66 return x % 7
67
68 def mod_by_neg_7(x: i16) -> i16:
69 return x // -7
70
71 def mod(x: i16, y: i16) -> i16:
72 return x % y
73
74 def test_mod_by_constant() -> None:
75 for i in range(-1000, 1000):
76 assert mod_by_7(i) == i % 7
77 for i in range(-2**15, -2**15 + 1000):
78 assert mod_by_7(i) == i % 7
79 for i in range(2**15 - 1000, 2**15):
80 assert mod_by_7(i) == i % 7
81
82 def test_mod_by_negative_constant() -> None:
83 for i in range(-1000, 1000):
84 assert mod_by_neg_7(i) == i // -7
85 for i in range(-2**15, -2**15 + 1000):
86 assert mod_by_neg_7(i) == i // -7
87 for i in range(2**15 - 1000, 2**15):
88 assert mod_by_neg_7(i) == i // -7
89
90 def test_mod_by_variable() -> None:
91 values = (list(range(-50, 50)) +
92 list(range(-2**15, -2**15 + 10)) +
93 list(range(2**15 - 10, 2**15)))
94 for x in values:
95 for y in values:
96 if y != 0:
97 assert mod(x, y) == x % y
98 else:
99 with assertRaises(ZeroDivisionError, "integer division or modulo by zero"):
100 mod(x, y)
101
102 def test_simple_arithmetic_ops() -> None:
103 zero: i16 = int()
104 one: i16 = zero + 1
105 two: i16 = one + 1
106 neg_one: i16 = -one
107 assert one + one == 2
108 assert one + two == 3
109 assert one + neg_one == 0
110 assert one - one == 0
111 assert one - two == -1
112 assert one * one == 1
113 assert one * two == 2
114 assert two * two == 4
115 assert two * neg_one == -2
116 assert neg_one * one == -1
117 assert neg_one * neg_one == 1
118 assert two * 0 == 0
119 assert 0 * two == 0
120 assert -one == -1
121 assert -two == -2
122 assert -neg_one == 1
123 assert -zero == 0
124
125 def test_bitwise_ops() -> None:
126 x: i16 = 13855 + int()
127 y: i16 = 367 + int()
128 z: i16 = -11091 + int()
129 zero: i16 = int()
130 one: i16 = zero + 1
131 two: i16 = zero + 2
132 neg_one: i16 = -one
133
134 assert x & y == 15
135 assert x & z == 5133
136 assert z & z == z
137 assert x & zero == 0
138
139 assert x | y == 14207
140 assert x | z == -2369
141 assert z | z == z
142 assert x | 0 == x
143
144 assert x ^ y == 14192
145 assert x ^ z == -7502
146 assert z ^ z == 0
147 assert z ^ 0 == z
148
149 assert x << one == 27710
150 assert x << two == -10116
151 assert z << two == 21172
152 assert z << 0 == z
153
154 assert x >> one == 6927
155 assert x >> two == 3463
156 assert z >> two == -2773
157 assert z >> 0 == z
158
159 assert ~x == -13856
160 assert ~z == 11090
161 assert ~zero == -1
162 assert ~neg_one == 0
163
164 def eq(x: i16, y: i16) -> bool:
165 return x == y
166
167 def test_eq() -> None:
168 assert eq(int(), int())
169 assert eq(5 + int(), 5 + int())
170 assert eq(-5 + int(), -5 + int())
171 assert not eq(int(), 1 + int())
172 assert not eq(5 + int(), 6 + int())
173 assert not eq(-5 + int(), -6 + int())
174 assert not eq(-5 + int(), 5 + int())
175
176 def test_comparisons() -> None:
177 one: i16 = 1 + int()
178 one2: i16 = 1 + int()
179 two: i16 = 2 + int()
180 assert one < two
181 assert not (one < one2)
182 assert not (two < one)
183 assert two > one
184 assert not (one > one2)
185 assert not (one > two)
186 assert one <= two
187 assert one <= one2
188 assert not (two <= one)
189 assert two >= one
190 assert one >= one2
191 assert not (one >= two)
192 assert one == one2
193 assert not (one == two)
194 assert one != two
195 assert not (one != one2)
196
197 def test_mixed_comparisons() -> None:
198 i16_3: i16 = int() + 3
199 int_5 = int() + 5
200 assert i16_3 < int_5
201 assert int_5 > i16_3
202 b = i16_3 > int_5
203 assert not b
204
205 int_largest = int() + (1 << 15) - 1
206 assert int_largest > i16_3
207 int_smallest = int() - (1 << 15)
208 assert i16_3 > int_smallest
209
210 int_too_big = int() + (1 << 15)
211 int_too_small = int() - (1 << 15) - 1
212 with assertRaises(ValueError):
213 assert i16_3 < int_too_big
214 with assertRaises(ValueError):
215 assert int_too_big < i16_3
216 with assertRaises(ValueError):
217 assert i16_3 > int_too_small
218 with assertRaises(ValueError):
219 assert int_too_small < i16_3
220
221 def test_mixed_arithmetic_and_bitwise_ops() -> None:
222 i16_3: i16 = int() + 3
223 int_5 = int() + 5
224 assert i16_3 + int_5 == 8
225 assert int_5 - i16_3 == 2
226 assert i16_3 << int_5 == 96
227 assert int_5 << i16_3 == 40
228 assert i16_3 ^ int_5 == 6
229 assert int_5 | i16_3 == 7
230
231 int_largest = int() + (1 << 15) - 1
232 assert int_largest - i16_3 == 32764
233 int_smallest = int() - (1 << 15)
234 assert int_smallest + i16_3 == -32765
235
236 int_too_big = int() + (1 << 15)
237 int_too_small = int() - (1 << 15) - 1
238 with assertRaises(ValueError):
239 assert i16_3 & int_too_big
240 with assertRaises(ValueError):
241 assert int_too_small & i16_3
242
243 def test_coerce_to_and_from_int() -> None:
244 for shift in range(0, 16):
245 for sign in 1, -1:
246 for delta in range(-5, 5):
247 n = sign * (1 << shift) + delta
248 if -(1 << 15) <= n < (1 << 15):
249 x: i16 = n
250 m: int = x
251 assert m == n
252
253 def test_explicit_conversion_to_i16() -> None:
254 x = i16(5)
255 assert x == 5
256 y = int() - 113
257 x = i16(y)
258 assert x == -113
259 n64: i64 = 1733
260 x = i16(n64)
261 assert x == 1733
262 n32: i32 = -1733
263 x = i16(n32)
264 assert x == -1733
265 z = i16(x)
266 assert z == -1733
267
268 def test_explicit_conversion_overflow() -> None:
269 max_i16 = int() + 2**15 - 1
270 x = i16(max_i16)
271 assert x == 2**15 - 1
272 assert int(x) == max_i16
273
274 min_i16 = int() - 2**15
275 y = i16(min_i16)
276 assert y == -2**15
277 assert int(y) == min_i16
278
279 too_big = int() + 2**15
280 with assertRaises(ValueError):
281 x = i16(too_big)
282
283 too_small = int() - 2**15 - 1
284 with assertRaises(ValueError):
285 x = i16(too_small)
286
287 def test_i16_from_large_small_literal() -> None:
288 x = i16(2**15 - 1)
289 assert x == 2**15 - 1
290 x = i16(-2**15)
291 assert x == -2**15
292
293 def test_i16_truncate_from_i64() -> None:
294 large = i64(2**32 + 65536 + 157 + int())
295 x = i16(large)
296 assert x == 157
297 small = i64(-2**32 - 65536 - 157 + int())
298 x = i16(small)
299 assert x == -157
300 large2 = i64(2**15 + int())
301 x = i16(large2)
302 assert x == -2**15
303 small2 = i64(-2**15 - 1 - int())
304 x = i16(small2)
305 assert x == 2**15 - 1
306
307 def test_i16_truncate_from_i32() -> None:
308 large = i32(2**16 + 2**30 + 5 + int())
309 assert i16(large) == 5
310 small = i32(-2**16 - 2**30 - 1 + int())
311 assert i16(small) == -1
312
313 def from_float(x: float) -> i16:
314 return i16(x)
315
316 def test_explicit_conversion_from_float() -> None:
317 assert from_float(0.0) == 0
318 assert from_float(1.456) == 1
319 assert from_float(-1234.567) == -1234
320 assert from_float(2**15 - 1) == 2**15 - 1
321 assert from_float(-2**15) == -2**15
322 # The error message could be better, but this is acceptable
323 with assertRaises(ValueError, "int too large to convert to i16"):
324 assert from_float(float(2**15))
325 with assertRaises(ValueError, "int too large to convert to i16"):
326 # One ulp below the lowest valid i64 value
327 from_float(float(-2**15 - 1))
328
329 def test_tuple_i16() -> None:
330 a: i16 = 1
331 b: i16 = 2
332 t = (a, b)
333 a, b = t
334 assert a == 1
335 assert b == 2
336 x: Any = t
337 tt: Tuple[i16, i16] = x
338 assert tt == (1, 2)