blob: 8f7b727aeb1223678e3ca0e8dc791550c771500f [file]
[case testU32BasicOps]
from typing import Any, Final, Tuple
from mypy_extensions import u32, i16, i32, i64, u8
from testutil import assertRaises
ERROR: Final = 239
def test_box_and_unbox() -> None:
for i in [0, 1, 255, 256, 1000, 32767, 65534, 4294967295]:
o: Any = i
x: u32 = o
o2: Any = x
assert o == o2
assert x == i
with assertRaises(ValueError, "int too large or small to convert to u32"):
o = 4294967296
x2: u32 = o
with assertRaises(ValueError, "int too large or small to convert to u32"):
o = -1
x3: u32 = o
def div_by_7(x: u32) -> u32:
return x // 7
def div(x: u32, y: u32) -> u32:
return x // y
def test_divide_by_constant() -> None:
for i in [0, 1, 100, 1000, 4294967295]:
assert div_by_7(i) == i // 7
def test_divide_by_variable() -> None:
for x in [0, 1, 255, 256, 1000, 4294967295]:
for y in [0, 1, 7, 255, 256, 1000, 4294967295]:
if y != 0:
assert div(x, y) == x // y
else:
with assertRaises(ZeroDivisionError, "integer division or modulo by zero"):
div(x, y)
def mod_by_7(x: u32) -> u32:
return x % 7
def mod(x: u32, y: u32) -> u32:
return x % y
def test_mod_by_constant() -> None:
for i in [0, 1, 100, 1000, 4294967295]:
assert mod_by_7(i) == i % 7
def test_mod_by_variable() -> None:
for x in [0, 1, 255, 256, 1000, 4294967295]:
for y in [0, 1, 7, 255, 256, 1000, 4294967295]:
if y != 0:
assert mod(x, y) == x % y
else:
with assertRaises(ZeroDivisionError, "integer division or modulo by zero"):
mod(x, y)
def test_simple_arithmetic_ops() -> None:
zero: u32 = int()
one: u32 = zero + 1
two: u32 = one + 1
neg_one: u32 = -one
assert neg_one == 4294967295
assert one + one == 2
assert one + two == 3
assert one + neg_one == 0
assert one - one == 0
assert one - two == 4294967295
assert one * one == 1
assert one * two == 2
assert two * two == 4
assert two * neg_one == 4294967294
assert neg_one * one == 4294967295
assert neg_one * neg_one == 1
assert two * 0 == 0
assert 0 * two == 0
assert -one == 4294967295
assert -two == 4294967294
assert -neg_one == 1
assert -zero == 0
def test_bitwise_ops() -> None:
x: u32 = 47384 + int()
y: u32 = 20239 + int()
z: u32 = 28913 + int()
zero: u32 = int()
one: u32 = zero + 1
two: u32 = zero + 2
neg_one: u32 = -one
assert x & y == 2312
assert x & z == 12304
assert z & z == z
assert x & zero == 0
assert x | y == 65311
assert x | z == 63993
assert z | z == z
assert x | 0 == x
assert x ^ y == 62999
assert x ^ z == 51689
assert z ^ z == 0
assert z ^ 0 == z
assert x << one == 94768
assert x << two == 189536
assert z << two == 115652
assert z << 0 == z
assert x >> one == 23692
assert x >> two == 11846
assert z >> two == 7228
assert z >> 0 == z
for i in [0, 1, 255, 256, 1000, 32767, 4294967295]:
t: u32 = i
assert ~t == (~(i + int()) & 0xffffffff)
def eq(x: u32, y: u32) -> bool:
return x == y
def test_eq() -> None:
assert eq(int(), int())
assert eq(5 + int(), 5 + int())
assert not eq(int(), 1 + int())
assert not eq(5 + int(), 6 + int())
def test_comparisons() -> None:
one: u32 = 1 + int()
one2: u32 = 1 + int()
two: u32 = 2 + int()
assert one < two
assert not (one < one2)
assert not (two < one)
assert two > one
assert not (one > one2)
assert not (one > two)
assert one <= two
assert one <= one2
assert not (two <= one)
assert two >= one
assert one >= one2
assert not (one >= two)
assert one == one2
assert not (one == two)
assert one != two
assert not (one != one2)
def test_mixed_comparisons() -> None:
u32_3: u32 = int() + 3
int_5 = int() + 5
assert u32_3 < int_5
assert int_5 > u32_3
b = u32_3 > int_5
assert not b
int_largest = int() + 4294967295
assert int_largest > u32_3
int_smallest = int()
assert u32_3 > int_smallest
int_too_big = int() + 4294967296
int_too_small = int() -1
with assertRaises(ValueError):
assert u32_3 < int_too_big
with assertRaises(ValueError):
assert int_too_big < u32_3
with assertRaises(ValueError):
assert u32_3 > int_too_small
with assertRaises(ValueError):
assert int_too_small < u32_3
def test_mixed_arithmetic_and_bitwise_ops() -> None:
u32_3: u32 = int() + 3
int_5 = int() + 5
assert u32_3 + int_5 == 8
assert int_5 - u32_3 == 2
assert u32_3 << int_5 == 96
assert int_5 << u32_3 == 40
assert u32_3 ^ int_5 == 6
assert int_5 | u32_3 == 7
int_largest = int() + 4294967295
assert int_largest - u32_3 == 4294967292
int_smallest = int()
assert int_smallest + u32_3 == 3
int_too_big = int() + 4294967296
int_too_small = int() - 1
with assertRaises(ValueError):
assert u32_3 & int_too_big
with assertRaises(ValueError):
assert int_too_small & u32_3
def test_coerce_to_and_from_int() -> None:
for n in [0, 1, 255, 256, 1000, 32767, 4294967295]:
x: u32 = n
m: int = x
assert m == n
def test_coerce_from_len_and_negative_int() -> None:
a = [1, 2, 3]
x: u32 = len(a)
assert x == 3
n = 0
for i in range(-2, 2):
if i < 0:
with assertRaises(ValueError, "int too large or small to convert to u32"):
y: u32 = i
else:
y = i
assert y == i
n += 1
assert n == 2
def test_explicit_conversion_to_u32() -> None:
x = u32(5)
assert x == 5
y = int() + ERROR
x = u32(y)
assert x == ERROR
n64: i64 = 233
x = u32(n64)
assert x == 233
n32: i32 = 234
x = u32(n32)
assert x == 234
z = u32(x)
assert z == 234
n16: i16 = 231
x = u32(n16)
assert x == 231
n8: u8 = 232
x = u32(n8)
assert x == 232
def test_explicit_conversion_overflow() -> None:
max_u32 = int() + 4294967295
x = u32(max_u32)
assert x == 4294967295
assert int(x) == max_u32
min_u32 = int()
y = u32(min_u32)
assert y == 0
assert int(y) == min_u32
too_big = int() + 4294967296
with assertRaises(ValueError):
x = u32(too_big)
too_small = int() - 1
with assertRaises(ValueError):
x = u32(too_small)
def test_u32_from_large_small_literal() -> None:
x = u32(4294967295)
assert x == 4294967295
x = u32(0)
assert x == 0
def test_u32_truncate_from_i64() -> None:
large = i64(2**32 + 4294967296 + 157 + int())
x = u32(large)
assert x == 157
small = i64(-2**32 - 4294967296 - 157 + int())
x = u32(small)
assert x == 4294967296 - 157
large2 = i64(2**16 + int())
x = u32(large2)
assert x == 65536
small2 = i64(-2**16 - 1 - int())
x = u32(small2)
assert x == 4294901759
def test_u32_truncate_from_i32() -> None:
large = i32(2**20 + 2**16 + 5 + int())
assert u32(large) == 1114117
small = i32(-2**20 - 2**16 - 1 + int())
assert u32(small) == 4293853183
def from_float(x: float) -> u32:
return u32(x)
def test_explicit_conversion_from_float() -> None:
assert from_float(0.0) == 0
assert from_float(1.456) == 1
assert from_float(234.567) == 234
assert from_float(4294967295) == 4294967295
assert from_float(0) == 0
assert from_float(-0.999) == 0
# The error message could be better, but this is acceptable
with assertRaises(ValueError, "int too large or small to convert to u32"):
assert from_float(float(4294967296))
with assertRaises(ValueError, "int too large or small to convert to u32"):
# One ulp below the lowest valid i64 value
from_float(float(-1.0))
def test_tuple_u32() -> None:
a: u32 = 1
b: u32 = 2
t = (a, b)
a, b = t
assert a == 1
assert b == 2
x: Any = t
tt: Tuple[u32, u32] = x
assert tt == (1, 2)
def test_convert_u32_to_native_int() -> None:
for i in [0, 1, 255, 1000, 32767, 32768, 65535]:
x: u32 = i
assert u8(x) == (i & 0xff)
assert i16(x) == (i if i < 32768 else i - 65536)
assert i32(x) == i
assert i64(x) == i
def test_short_int_and_digit_boundaries() -> None:
for i in [2**30 - 1, 2**30, 2**31 - 1, 2**31, 2**32 - 2]:
o: Any = i
x: u32 = o
o2: Any = x
assert o2 == i
n = i + int()
y: u32 = n
assert y == i
m: int = y
assert m == i
assert u32(n) == i
def test_high_bit_conversions() -> None:
big: u32 = 2**31 + int()
top: u32 = 2**32 - 1 + int()
assert i64(big) == 2**31
assert i64(top) == 2**32 - 1
assert i32(big) == -2**31
assert i32(top) == -1
assert i16(top) == -1
assert u8(top) == 255
n16: i16 = -1 - int()
assert u32(n16) == 2**32 - 1
n32: i32 = -2**31 + int()
assert u32(n32) == 2**31
assert big > 1
assert big >> 1 == 2**30