| [case testU16BasicOps] | |
| from typing import Any, Final, Tuple, cast | |
| from mypy_extensions import u16, i8, 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, 65535]: | |
| o: Any = i | |
| x: u16 = o | |
| o2: Any = x | |
| assert o == o2 | |
| assert x == i | |
| with assertRaises(ValueError, "int too large or small to convert to u16"): | |
| o = 65536 | |
| x2: u16 = o | |
| with assertRaises(ValueError, "int too large or small to convert to u16"): | |
| o = -1 | |
| x3: u16 = o | |
| def div_by_7(x: u16) -> u16: | |
| return x // 7 | |
| def div(x: u16, y: u16) -> u16: | |
| return x // y | |
| def test_divide_by_constant() -> None: | |
| for i in [0, 1, 100, 1000, 65535]: | |
| assert div_by_7(i) == i // 7 | |
| def test_divide_by_variable() -> None: | |
| for x in [0, 1, 255, 256, 1000, 65535]: | |
| for y in [0, 1, 7, 255, 256, 1000, 65535]: | |
| 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: u16) -> u16: | |
| return x % 7 | |
| def mod(x: u16, y: u16) -> u16: | |
| return x % y | |
| def test_mod_by_constant() -> None: | |
| for i in [0, 1, 100, 1000, 65535]: | |
| assert mod_by_7(i) == i % 7 | |
| def test_mod_by_variable() -> None: | |
| for x in [0, 1, 255, 256, 1000, 65535]: | |
| for y in [0, 1, 7, 255, 256, 1000, 65535]: | |
| 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: u16 = int() | |
| one: u16 = zero + 1 | |
| two: u16 = one + 1 | |
| neg_one: u16 = -one | |
| assert neg_one == 65535 | |
| assert one + one == 2 | |
| assert one + two == 3 | |
| assert one + neg_one == 0 | |
| assert one - one == 0 | |
| assert one - two == 65535 | |
| assert one * one == 1 | |
| assert one * two == 2 | |
| assert two * two == 4 | |
| assert two * neg_one == 65534 | |
| assert neg_one * one == 65535 | |
| assert neg_one * neg_one == 1 | |
| assert two * 0 == 0 | |
| assert 0 * two == 0 | |
| assert -one == 65535 | |
| assert -two == 65534 | |
| assert -neg_one == 1 | |
| assert -zero == 0 | |
| assert abs(one) == 1 | |
| assert abs(neg_one) == 65535 | |
| assert abs(zero) == 0 | |
| def test_bitwise_ops() -> None: | |
| x: u16 = 47384 + int() | |
| y: u16 = 20239 + int() | |
| z: u16 = 28913 + int() | |
| zero: u16 = int() | |
| one: u16 = zero + 1 | |
| two: u16 = zero + 2 | |
| neg_one: u16 = -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 == 29232 | |
| assert x << two == 58464 | |
| assert z << two == 50116 | |
| 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, 65535]: | |
| t: u16 = i | |
| assert ~t == (~(i + int()) & 0xffff) | |
| def eq(x: u16, y: u16) -> 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: u16 = 1 + int() | |
| one2: u16 = 1 + int() | |
| two: u16 = 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: | |
| u16_3: u16 = int() + 3 | |
| int_5 = int() + 5 | |
| assert u16_3 < int_5 | |
| assert int_5 > u16_3 | |
| b = u16_3 > int_5 | |
| assert not b | |
| int_largest = int() + 65535 | |
| assert int_largest > u16_3 | |
| int_smallest = int() | |
| assert u16_3 > int_smallest | |
| int_too_big = int() + 65536 | |
| int_too_small = int() -1 | |
| with assertRaises(ValueError): | |
| assert u16_3 < int_too_big | |
| with assertRaises(ValueError): | |
| assert int_too_big < u16_3 | |
| with assertRaises(ValueError): | |
| assert u16_3 > int_too_small | |
| with assertRaises(ValueError): | |
| assert int_too_small < u16_3 | |
| def test_mixed_arithmetic_and_bitwise_ops() -> None: | |
| u16_3: u16 = int() + 3 | |
| int_5 = int() + 5 | |
| assert u16_3 + int_5 == 8 | |
| assert int_5 - u16_3 == 2 | |
| assert u16_3 << int_5 == 96 | |
| assert int_5 << u16_3 == 40 | |
| assert u16_3 ^ int_5 == 6 | |
| assert int_5 | u16_3 == 7 | |
| int_largest = int() + 65535 | |
| assert int_largest - u16_3 == 65532 | |
| int_smallest = int() | |
| assert int_smallest + u16_3 == 3 | |
| int_too_big = int() + 65536 | |
| int_too_small = int() - 1 | |
| with assertRaises(ValueError): | |
| assert u16_3 & int_too_big | |
| with assertRaises(ValueError): | |
| assert int_too_small & u16_3 | |
| def test_coerce_to_and_from_int() -> None: | |
| for n in [0, 1, 255, 256, 1000, 32767, 65535]: | |
| x: u16 = n | |
| m: int = x | |
| assert m == n | |
| def test_explicit_conversion_to_u16() -> None: | |
| x = u16(5) | |
| assert x == 5 | |
| y = int() + ERROR | |
| x = u16(y) | |
| assert x == ERROR | |
| n64: i64 = 233 | |
| x = u16(n64) | |
| assert x == 233 | |
| n32: i32 = 234 | |
| x = u16(n32) | |
| assert x == 234 | |
| z = u16(x) | |
| assert z == 234 | |
| n16: i16 = 231 | |
| x = u16(n16) | |
| assert x == 231 | |
| n8: u8 = 232 | |
| x = u16(n8) | |
| assert x == 232 | |
| def test_explicit_conversion_overflow() -> None: | |
| max_u16 = int() + 65535 | |
| x = u16(max_u16) | |
| assert x == 65535 | |
| assert int(x) == max_u16 | |
| min_u16 = int() | |
| y = u16(min_u16) | |
| assert y == 0 | |
| assert int(y) == min_u16 | |
| too_big = int() + 65536 | |
| with assertRaises(ValueError): | |
| x = u16(too_big) | |
| too_small = int() - 1 | |
| with assertRaises(ValueError): | |
| x = u16(too_small) | |
| def test_u16_from_large_small_literal() -> None: | |
| x = u16(65535) | |
| assert x == 65535 | |
| x = u16(0) | |
| assert x == 0 | |
| def neg_literal_to_int() -> int: | |
| return -u16(1) | |
| def eq_neg_literal(x: u16) -> bool: | |
| return x == -u16(1) | |
| def gt_neg_literal(x: u16) -> bool: | |
| return x > -u16(5) | |
| def and_neg_literal(x: u16) -> u16: | |
| return x & -u16(8) | |
| def div_neg_literal(x: u16) -> u16: | |
| return x // -u16(5) | |
| def test_negate_literal() -> None: | |
| assert -u16(0) == 0 | |
| assert -u16(1) == 65535 | |
| assert int(-u16(1)) == 2**16 - 1 | |
| assert neg_literal_to_int() == 2**16 - 1 | |
| assert str(-u16(1)) == "65535" | |
| assert -u16(65535) == 1 | |
| assert 0 - u16(1) == 65535 | |
| assert ~u16(0) == 65535 | |
| one: u16 = 1 + int() | |
| assert -u16(1) == -one | |
| assert -u16(5) == -(one * 5) | |
| assert eq_neg_literal(65535) | |
| assert not eq_neg_literal(1) | |
| assert gt_neg_literal(65535) | |
| assert not gt_neg_literal(65531) | |
| assert and_neg_literal(65535) == 65528 | |
| assert div_neg_literal(65535) == 1 | |
| assert div_neg_literal(5) == 0 | |
| ONE_FINAL: Final[u16] = 1 | |
| MAX_FINAL: Final[u16] = 65535 | |
| NEG_ONE_FINAL: Final[u16] = -ONE_FINAL | |
| class FinalAttrs: | |
| TWO: Final[u16] = 2 | |
| def neg_final() -> u16: | |
| return -ONE_FINAL | |
| def neg_final_to_int() -> int: | |
| return -ONE_FINAL | |
| def test_final_wraparound() -> None: | |
| one: u16 = 1 + int() | |
| max_val: u16 = 65535 + int() | |
| assert -ONE_FINAL == -one == 65535 | |
| assert neg_final() == 65535 | |
| assert neg_final_to_int() == 65535 | |
| assert ONE_FINAL - 2 == one - 2 == 65535 | |
| assert ~ONE_FINAL == ~one == 65534 | |
| assert MAX_FINAL + 1 == max_val + 1 == 0 | |
| assert MAX_FINAL * 2 == max_val * 2 == 65534 | |
| assert (ONE_FINAL - 2) // 2 == (one - 2) // 2 == 32767 | |
| assert -ONE_FINAL >> 1 == -one >> 1 == 32767 | |
| assert -FinalAttrs.TWO == -(one * 2) == 65534 | |
| assert NEG_ONE_FINAL == 65535 | |
| def test_u16_truncate_from_i64() -> None: | |
| large = i64(2**32 + 65536 + 157 + int()) | |
| x = u16(large) | |
| assert x == 157 | |
| small = i64(-2**32 - 65536 - 157 + int()) | |
| x = u16(small) | |
| assert x == 65536 - 157 | |
| large2 = i64(2**16 + int()) | |
| x = u16(large2) | |
| assert x == 0 | |
| small2 = i64(-2**16 - 1 - int()) | |
| x = u16(small2) | |
| assert x == 65535 | |
| def test_u16_truncate_from_i32() -> None: | |
| large = i32(2**20 + 2**16 + 5 + int()) | |
| assert u16(large) == 5 | |
| small = i32(-2**20 - 2**16 - 1 + int()) | |
| assert u16(small) == 65535 | |
| def from_float(x: float) -> u16: | |
| return u16(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(65535) == 65535 | |
| 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 u16"): | |
| assert from_float(float(65536)) | |
| with assertRaises(ValueError, "int too large or small to convert to u16"): | |
| # One ulp below the lowest valid i64 value | |
| from_float(float(-1.0)) | |
| def test_tuple_u16() -> None: | |
| a: u16 = 1 | |
| b: u16 = 2 | |
| t = (a, b) | |
| a, b = t | |
| assert a == 1 | |
| assert b == 2 | |
| x: Any = t | |
| tt: Tuple[u16, u16] = x | |
| assert tt == (1, 2) | |
| def test_convert_u16_to_native_int() -> None: | |
| for i in [0, 1, 255, 1000, 32767, 65535]: | |
| x: u16 = 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 i64_to_u16(x: i64) -> u16: | |
| return cast(u16, x) | |
| def i32_to_u16(x: i32) -> u16: | |
| return cast(u16, x) | |
| def i16_to_u16(x: i16) -> u16: | |
| return cast(u16, x) | |
| def i8_to_u16(x: i8) -> u16: | |
| return cast(u16, x) | |
| def test_coerce_from_signed_native_int() -> None: | |
| for n in (0, 1, 255, 32767, 65535): | |
| assert i64_to_u16(n) == n | |
| assert i32_to_u16(n) == n | |
| for n in (0, 1, 255, 32767): | |
| assert i16_to_u16(n) == n | |
| for n in (0, 1, 127): | |
| assert i8_to_u16(n) == n | |
| msg = "int too large or small to convert to u16" | |
| for n in (-1, 65536, 2**63 - 1, -2**63): | |
| with assertRaises(ValueError, msg): | |
| i64_to_u16(n) | |
| for n in (-1, 65536, 2**31 - 1, -2**31): | |
| with assertRaises(ValueError, msg): | |
| i32_to_u16(n) | |
| for n in (-1, -32768): | |
| with assertRaises(ValueError, msg): | |
| i16_to_u16(n) | |
| for n in (-1, -128): | |
| with assertRaises(ValueError, msg): | |
| i8_to_u16(n) |