| 1 |
[case testI32BasicOps] |
| 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**31, -2**31 + 100)) + |
| 10 |
list(range(-1000, 1000)) + |
| 11 |
list(range(2**31 - 100, 2**31))) |
| 12 |
for i in values: |
| 13 |
o: Any = i |
| 14 |
x: i32 = o |
| 15 |
o2: Any = x |
| 16 |
assert o == o2 |
| 17 |
assert x == i |
| 18 |
with assertRaises(ValueError, "int too large to convert to i32"): |
| 19 |
o = 2**31 |
| 20 |
x2: i32 = o |
| 21 |
with assertRaises(ValueError, "int too large to convert to i32"): |
| 22 |
o = -2**32 - 1 |
| 23 |
x3: i32 = o |
| 24 |
|
| 25 |
def div_by_7(x: i32) -> i32: |
| 26 |
return x // 7 |
| 27 |
def div_by_neg_7(x: i32) -> i32: |
| 28 |
return x // -7 |
| 29 |
|
| 30 |
def div(x: i32, y: i32) -> i32: |
| 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**31, -2**31 + 1000): |
| 37 |
assert div_by_7(i) == i // 7 |
| 38 |
for i in range(2**31 - 1000, 2**31): |
| 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**31, -2**31 + 1000): |
| 45 |
assert div_by_neg_7(i) == i // -7 |
| 46 |
for i in range(2**31 - 1000, 2**31): |
| 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**31, -2**31 + 10)) + |
| 52 |
list(range(2**31 - 10, 2**31))) |
| 53 |
for x in values: |
| 54 |
for y in values: |
| 55 |
if y != 0: |
| 56 |
if x // y == 2**31: |
| 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: i32) -> i32: |
| 66 |
return x % 7 |
| 67 |
|
| 68 |
def mod_by_neg_7(x: i32) -> i32: |
| 69 |
return x // -7 |
| 70 |
|
| 71 |
def mod(x: i32, y: i32) -> i32: |
| 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**31, -2**31 + 1000): |
| 78 |
assert mod_by_7(i) == i % 7 |
| 79 |
for i in range(2**31 - 1000, 2**31): |
| 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**31, -2**31 + 1000): |
| 86 |
assert mod_by_neg_7(i) == i // -7 |
| 87 |
for i in range(2**31 - 1000, 2**31): |
| 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**31, -2**31 + 10)) + |
| 93 |
list(range(2**31 - 10, 2**31))) |
| 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: i32 = int() |
| 104 |
one: i32 = zero + 1 |
| 105 |
two: i32 = one + 1 |
| 106 |
neg_one: i32 = -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: i32 = 1920687484 + int() |
| 127 |
y: i32 = 383354614 + int() |
| 128 |
z: i32 = -1879040563 + int() |
| 129 |
zero: i32 = int() |
| 130 |
one: i32 = zero + 1 |
| 131 |
two: i32 = zero + 2 |
| 132 |
neg_one: i32 = -one |
| 133 |
|
| 134 |
assert x & y == 307823732 |
| 135 |
assert x & z == 268442956 |
| 136 |
assert z & z == z |
| 137 |
assert x & zero == 0 |
| 138 |
|
| 139 |
assert x | y == 1996218366 |
| 140 |
assert x | z == -226796035 |
| 141 |
assert z | z == z |
| 142 |
assert x | 0 == x |
| 143 |
|
| 144 |
assert x ^ y == 1688394634 |
| 145 |
assert x ^ z == -495238991 |
| 146 |
assert z ^ z == 0 |
| 147 |
assert z ^ 0 == z |
| 148 |
|
| 149 |
assert x << one == -453592328 |
| 150 |
assert x << two == -907184656 |
| 151 |
assert z << two == 1073772340 |
| 152 |
assert z << 0 == z |
| 153 |
|
| 154 |
assert x >> one == 960343742 |
| 155 |
assert x >> two == 480171871 |
| 156 |
assert z >> two == -469760141 |
| 157 |
assert z >> 0 == z |
| 158 |
|
| 159 |
assert ~x == -1920687485 |
| 160 |
assert ~z == 1879040562 |
| 161 |
assert ~zero == -1 |
| 162 |
assert ~neg_one == 0 |
| 163 |
|
| 164 |
def eq(x: i32, y: i32) -> 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: i32 = 1 + int() |
| 178 |
one2: i32 = 1 + int() |
| 179 |
two: i32 = 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 |
i32_3: i32 = int() + 3 |
| 199 |
int_5 = int() + 5 |
| 200 |
assert i32_3 < int_5 |
| 201 |
assert int_5 > i32_3 |
| 202 |
b = i32_3 > int_5 |
| 203 |
assert not b |
| 204 |
|
| 205 |
int_largest = int() + (1 << 31) - 1 |
| 206 |
assert int_largest > i32_3 |
| 207 |
int_smallest = int() - (1 << 31) |
| 208 |
assert i32_3 > int_smallest |
| 209 |
|
| 210 |
int_too_big = int() + (1 << 31) |
| 211 |
int_too_small = int() - (1 << 31) - 1 |
| 212 |
with assertRaises(ValueError): |
| 213 |
assert i32_3 < int_too_big |
| 214 |
with assertRaises(ValueError): |
| 215 |
assert int_too_big < i32_3 |
| 216 |
with assertRaises(ValueError): |
| 217 |
assert i32_3 > int_too_small |
| 218 |
with assertRaises(ValueError): |
| 219 |
assert int_too_small < i32_3 |
| 220 |
|
| 221 |
def test_mixed_arithmetic_and_bitwise_ops() -> None: |
| 222 |
i32_3: i32 = int() + 3 |
| 223 |
int_5 = int() + 5 |
| 224 |
assert i32_3 + int_5 == 8 |
| 225 |
assert int_5 - i32_3 == 2 |
| 226 |
assert i32_3 << int_5 == 96 |
| 227 |
assert int_5 << i32_3 == 40 |
| 228 |
assert i32_3 ^ int_5 == 6 |
| 229 |
assert int_5 | i32_3 == 7 |
| 230 |
|
| 231 |
int_largest = int() + (1 << 31) - 1 |
| 232 |
assert int_largest - i32_3 == 2147483644 |
| 233 |
int_smallest = int() - (1 << 31) |
| 234 |
assert int_smallest + i32_3 == -2147483645 |
| 235 |
|
| 236 |
int_too_big = int() + (1 << 31) |
| 237 |
int_too_small = int() - (1 << 31) - 1 |
| 238 |
with assertRaises(ValueError): |
| 239 |
assert i32_3 & int_too_big |
| 240 |
with assertRaises(ValueError): |
| 241 |
assert int_too_small & i32_3 |
| 242 |
|
| 243 |
def test_coerce_to_and_from_int() -> None: |
| 244 |
for shift in range(0, 32): |
| 245 |
for sign in 1, -1: |
| 246 |
for delta in range(-5, 5): |
| 247 |
n = sign * (1 << shift) + delta |
| 248 |
if -(1 << 31) <= n < (1 << 31): |
| 249 |
x: i32 = n |
| 250 |
m: int = x |
| 251 |
assert m == n |
| 252 |
|
| 253 |
def test_explicit_conversion_to_i32() -> None: |
| 254 |
x = i32(5) |
| 255 |
assert x == 5 |
| 256 |
y = int() - 113 |
| 257 |
x = i32(y) |
| 258 |
assert x == -113 |
| 259 |
n64: i64 = 1733 |
| 260 |
x = i32(n64) |
| 261 |
assert x == 1733 |
| 262 |
n32: i32 = -1733 |
| 263 |
x = i32(n32) |
| 264 |
assert x == -1733 |
| 265 |
z = i32(x) |
| 266 |
assert z == -1733 |
| 267 |
a: i16 = int() + 19764 |
| 268 |
assert i32(a) == 19764 |
| 269 |
a = int() - 1 |
| 270 |
assert i32(a) == -1 |
| 271 |
|
| 272 |
def test_explicit_conversion_overflow() -> None: |
| 273 |
max_i32 = int() + 2**31 - 1 |
| 274 |
x = i32(max_i32) |
| 275 |
assert x == 2**31 - 1 |
| 276 |
assert int(x) == max_i32 |
| 277 |
|
| 278 |
min_i32 = int() - 2**31 |
| 279 |
y = i32(min_i32) |
| 280 |
assert y == -2**31 |
| 281 |
assert int(y) == min_i32 |
| 282 |
|
| 283 |
too_big = int() + 2**31 |
| 284 |
with assertRaises(ValueError): |
| 285 |
x = i32(too_big) |
| 286 |
|
| 287 |
too_small = int() - 2**31 - 1 |
| 288 |
with assertRaises(ValueError): |
| 289 |
x = i32(too_small) |
| 290 |
|
| 291 |
def test_i32_from_large_small_literal() -> None: |
| 292 |
x = i32(2**31 - 1) |
| 293 |
assert x == 2**31 - 1 |
| 294 |
x = i32(-2**31) |
| 295 |
assert x == -2**31 |
| 296 |
|
| 297 |
def test_i32_truncate_from_i64() -> None: |
| 298 |
large = i64(2**32 + 157 + int()) |
| 299 |
x = i32(large) |
| 300 |
assert x == 157 |
| 301 |
small = i64(-2**32 - 157 + int()) |
| 302 |
x = i32(small) |
| 303 |
assert x == -157 |
| 304 |
large2 = i64(2**31 + int()) |
| 305 |
x = i32(large2) |
| 306 |
assert x == -2**31 |
| 307 |
small2 = i64(-2**31 - 1 - int()) |
| 308 |
x = i32(small2) |
| 309 |
assert x == 2**31 - 1 |
| 310 |
|
| 311 |
def from_float(x: float) -> i32: |
| 312 |
return i32(x) |
| 313 |
|
| 314 |
def test_explicit_conversion_from_float() -> None: |
| 315 |
assert from_float(0.0) == 0 |
| 316 |
assert from_float(1.456) == 1 |
| 317 |
assert from_float(-1234.567) == -1234 |
| 318 |
assert from_float(2**31 - 1) == 2**31 - 1 |
| 319 |
assert from_float(-2**31) == -2**31 |
| 320 |
# The error message could be better, but this is acceptable |
| 321 |
with assertRaises(ValueError, "int too large to convert to i32"): |
| 322 |
assert from_float(float(2**31)) |
| 323 |
with assertRaises(ValueError, "int too large to convert to i32"): |
| 324 |
# One ulp below the lowest valid i64 value |
| 325 |
from_float(float(-2**31 - 2048)) |
| 326 |
|
| 327 |
def test_tuple_i32() -> None: |
| 328 |
a: i32 = 1 |
| 329 |
b: i32 = 2 |
| 330 |
t = (a, b) |
| 331 |
a, b = t |
| 332 |
assert a == 1 |
| 333 |
assert b == 2 |
| 334 |
x: Any = t |
| 335 |
tt: Tuple[i32, i32] = x |
| 336 |
assert tt == (1, 2) |