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